1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CELLULAR_DATA_STATE_MACHINE_H
17 #define CELLULAR_DATA_STATE_MACHINE_H
18 
19 #include <map>
20 #include <memory>
21 
22 #include "apn_item.h"
23 #include "cellular_data_net_agent.h"
24 #include "data_connection_manager.h"
25 #include "data_connection_params.h"
26 #include "data_disconnect_params.h"
27 #include "network_state.h"
28 #include "state_machine.h"
29 #include "tel_event_handler.h"
30 
31 namespace OHOS {
32 namespace Telephony {
33 static const uint16_t DEFAULT_PORT = 0;
34 static const size_t HOST_SIZE = 1;
35 static const size_t HOST_PORT_SIZE = 2;
36 
37 class DataConnectionManager;
38 class CellularDataStateMachine : public StateMachine,
39     public std::enable_shared_from_this<CellularDataStateMachine> {
40 public:
CellularDataStateMachine(sptr<DataConnectionManager> & cdConnectionManager,std::shared_ptr<TelEventHandler> && cellularDataHandler)41     CellularDataStateMachine(
42         sptr<DataConnectionManager> &cdConnectionManager, std::shared_ptr<TelEventHandler> &&cellularDataHandler)
43         : StateMachine("CellularDataStateMachine"), cdConnectionManager_(cdConnectionManager),
44           cellularDataHandler_(std::move(cellularDataHandler)), cid_(0), capability_(0),
45           rilRat_(RadioTech::RADIO_TECHNOLOGY_UNKNOWN), apnId_(0)
46     {}
47     ~CellularDataStateMachine() = default;
48     bool operator==(const CellularDataStateMachine &stateMachine) const;
49     bool IsInactiveState() const;
50     bool IsActivatingState() const;
51     bool IsDisconnectingState() const;
52     bool IsActiveState() const;
53     bool IsDefaultState() const;
54     sptr<State> GetCurrentState() const;
55     void SetCapability(uint64_t capability);
56     uint64_t GetCapability() const;
57     int32_t GetCid() const;
58     int32_t GetSlotId() const;
59     std::string GetIpType();
60     sptr<ApnItem> GetApnItem() const;
61     void Init();
62     void UpdateHttpProxy(const std::string &proxyIpAddress);
63     void UpdateNetworkInfo(const SetupDataCallResultInfo &dataCallInfo);
64     void UpdateNetworkInfo();
65     void SetConnectionBandwidth(const uint32_t upBandwidth, const uint32_t downBandwidth);
66     void SetConnectionTcpBuffer(const std::string &tcpBuffer);
67     void SplitProxyIpAddress(const std::string &proxyIpAddress, std::string &host, uint16_t &port);
68     void UpdateNetworkInfoIfInActive(SetupDataCallResultInfo &info);
69 
70 protected:
71     sptr<State> activeState_;
72     sptr<State> inActiveState_;
73     sptr<State> activatingState_;
74     sptr<State> disconnectingState_;
75     sptr<State> defaultState_;
76     sptr<State> currentState_;
77     sptr<DataConnectionManager> cdConnectionManager_;
78     std::shared_ptr<TelEventHandler> cellularDataHandler_;
79     sptr<NetManagerStandard::NetLinkInfo> netLinkInfo_;
80     sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo_;
81 
82 private:
83     void SetCurrentState(const sptr<State> &&state);
84     void SetCid(const int32_t cid);
85     void DoConnect(const DataConnectionParams &connectionParams);
86     void FreeConnection(const DataDisconnectParams &params);
87     void ResolveIp(std::vector<AddressInfo> &ipInfoArray);
88     void ResolveDns(std::vector<AddressInfo> &dnsInfoArray);
89     void ResolveRoute(std::vector<AddressInfo> &routeInfoArray, const std::string &name);
90     void GetMtuSizeFromOpCfg(int32_t &mtuSize, int32_t slotId);
91     std::string GetIpType(std::vector<AddressInfo> ipInfoArray);
92     bool HasMatchedIpTypeAddrs(uint8_t ipType, uint8_t ipInfoArraySize, std::vector<AddressInfo> ipInfoArray);
93 
94 private:
95     friend class Active;
96     friend class Activating;
97     friend class Inactive;
98     friend class Default;
99     friend class Disconnecting;
100     int32_t cid_;
101     uint64_t capability_;
102     RadioTech rilRat_;
103     sptr<ApnItem> apnItem_;
104     int32_t apnId_;
105     std::mutex mtx_;
106     uint32_t upBandwidth_ = 0;
107     uint32_t downBandwidth_ = 0;
108     std::string tcpBuffer_;
109     int32_t connectId_ = 0;
110     int32_t cause_ = 0;
111     std::string ipType_ = "";
112     int64_t startTimeConnectTimeoutTask_ = 0;
113 };
114 } // namespace Telephony
115 } // namespace OHOS
116 #endif // CELLULAR_DATA_STATE_MACHINE_H
117