• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static const int32_t DEFAULT_INTERNET_CONNECTION_SCORE = 60;
37 static const int32_t OTHER_CONNECTION_SCORE = 55;
38 
39 class DataConnectionManager;
40 class CellularDataStateMachine : public StateMachine,
41     public std::enable_shared_from_this<CellularDataStateMachine> {
42 public:
CellularDataStateMachine(sptr<DataConnectionManager> & cdConnectionManager,std::shared_ptr<TelEventHandler> && cellularDataHandler)43     CellularDataStateMachine(
44         sptr<DataConnectionManager> &cdConnectionManager, std::shared_ptr<TelEventHandler> &&cellularDataHandler)
45         : StateMachine("CellularDataStateMachine"), cdConnectionManager_(cdConnectionManager),
46           cellularDataHandler_(std::move(cellularDataHandler)), cid_(0), capability_(0),
47           rilRat_(RadioTech::RADIO_TECHNOLOGY_UNKNOWN), apnId_(0)
48     {}
49     ~CellularDataStateMachine() = default;
50     bool operator==(const CellularDataStateMachine &stateMachine) const;
51     bool IsInactiveState() const;
52     bool IsActivatingState() const;
53     bool IsDisconnectingState() const;
54     bool IsActiveState() const;
55     bool IsDefaultState() const;
56     sptr<State> GetCurrentState() const;
57     void SetCapability(uint64_t capability);
58     uint64_t GetCapability() const;
59     int32_t GetCid() const;
60     int32_t GetSlotId() const;
61     std::string GetIpType();
62     sptr<ApnItem> GetApnItem() const;
63     void Init();
64     void UpdateHttpProxy(const std::string &proxyIpAddress);
65     void UpdateNetworkInfo(const SetupDataCallResultInfo &dataCallInfo);
66     void UpdateNetworkInfo();
67     void SetConnectionBandwidth(const uint32_t upBandwidth, const uint32_t downBandwidth);
68     void SetConnectionTcpBuffer(const std::string &tcpBuffer);
69     void SplitProxyIpAddress(const std::string &proxyIpAddress, std::string &host, uint16_t &port);
70     void UpdateNetworkInfoIfInActive(SetupDataCallResultInfo &info);
71     void UpdateReuseApnNetworkInfo(bool isAvailable);
72     void SetReuseApnCap(uint64_t cap);
73     uint64_t GetReuseApnCap() const;
74 
75 protected:
76     sptr<State> activeState_;
77     sptr<State> inActiveState_;
78     sptr<State> activatingState_;
79     sptr<State> disconnectingState_;
80     sptr<State> defaultState_;
81     sptr<State> currentState_;
82     sptr<DataConnectionManager> cdConnectionManager_;
83     std::shared_ptr<TelEventHandler> cellularDataHandler_;
84     sptr<NetManagerStandard::NetLinkInfo> netLinkInfo_;
85     sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo_;
86 
87 private:
88     void SetCurrentState(const sptr<State> &&state);
89     void SetCid(const int32_t cid);
90     void DoConnect(const DataConnectionParams &connectionParams);
91     void FreeConnection(const DataDisconnectParams &params);
92     void ResolveIp(std::vector<AddressInfo> &ipInfoArray);
93     void ResolveDns(std::vector<AddressInfo> &dnsInfoArray);
94     void ResolveRoute(std::vector<AddressInfo> &routeInfoArray, const std::string &name);
95     void GetMtuSizeFromOpCfg(int32_t &mtuSize, int32_t slotId);
96     std::string GetIpType(std::vector<AddressInfo> ipInfoArray);
97     bool HasMatchedIpTypeAddrs(uint8_t ipType, uint8_t ipInfoArraySize, std::vector<AddressInfo> ipInfoArray);
98     int32_t GetNetScoreBySlotId(int32_t slotId);
99 
100 private:
101     friend class Active;
102     friend class Activating;
103     friend class Inactive;
104     friend class Default;
105     friend class Disconnecting;
106     int32_t cid_;
107     uint64_t capability_;
108     RadioTech rilRat_;
109     sptr<ApnItem> apnItem_;
110     int32_t apnId_;
111     std::mutex mtx_;
112     uint32_t upBandwidth_ = 0;
113     uint32_t downBandwidth_ = 0;
114     std::string tcpBuffer_;
115     int32_t connectId_ = 0;
116     int32_t cause_ = 0;
117     std::string ipType_ = "";
118     int64_t startTimeConnectTimeoutTask_ = 0;
119     uint64_t reuseApnCap_ = NetManagerStandard::NetCap::NET_CAPABILITY_END;
120 };
121 } // namespace Telephony
122 } // namespace OHOS
123 #endif // CELLULAR_DATA_STATE_MACHINE_H
124