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 "net_conn_client.h" 29 #include "net_interface_callback_stub.h" 30 #include "state_machine.h" 31 #include "tel_event_handler.h" 32 33 namespace OHOS { 34 namespace Telephony { 35 static const uint16_t DEFAULT_PORT = 0; 36 static const size_t HOST_SIZE = 1; 37 static const size_t HOST_PORT_SIZE = 2; 38 static const int32_t DEFAULT_INTERNET_CONNECTION_SCORE = 60; 39 static const int32_t OTHER_CONNECTION_SCORE = 55; 40 41 class DataConnectionManager; 42 class CellularDataStateMachine : public StateMachine, 43 public std::enable_shared_from_this<CellularDataStateMachine> { 44 public: 45 class NetInterfaceCallback : public OHOS::NetManagerStandard::NetInterfaceStateCallbackStub { 46 public: NetInterfaceCallback(std::weak_ptr<CellularDataStateMachine> cellularDataStateMachine)47 explicit NetInterfaceCallback(std::weak_ptr<CellularDataStateMachine> cellularDataStateMachine) 48 : cellularDataStateMachine_(cellularDataStateMachine) {} 49 virtual ~NetInterfaceCallback() = default; 50 int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override; 51 private: 52 std::weak_ptr<CellularDataStateMachine> cellularDataStateMachine_; 53 }; CellularDataStateMachine(sptr<DataConnectionManager> & cdConnectionManager,std::shared_ptr<TelEventHandler> && cellularDataHandler)54 CellularDataStateMachine( 55 sptr<DataConnectionManager> &cdConnectionManager, std::shared_ptr<TelEventHandler> &&cellularDataHandler) 56 : StateMachine("CellularDataStateMachine"), cdConnectionManager_(cdConnectionManager), 57 cellularDataHandler_(std::move(cellularDataHandler)), cid_(0), capability_(0), 58 rilRat_(RadioTech::RADIO_TECHNOLOGY_UNKNOWN), apnId_(0) 59 {} 60 virtual ~CellularDataStateMachine(); 61 bool operator==(const CellularDataStateMachine &stateMachine) const; 62 bool IsInactiveState() const; 63 bool IsActivatingState() const; 64 bool IsDisconnectingState() const; 65 bool IsActiveState() const; 66 bool IsDefaultState() const; 67 sptr<State> GetCurrentState() const; 68 void SetCapability(uint64_t capability); 69 uint64_t GetCapability() const; 70 int32_t GetCid() const; 71 int32_t GetSlotId() const; 72 std::string GetIpType(); 73 sptr<ApnItem> GetApnItem() const; 74 void Init(); 75 void UpdateHttpProxy(const std::string &proxyIpAddress); 76 void UpdateNetworkInfo(const SetupDataCallResultInfo &dataCallInfo); 77 void UpdateNetworkInfo(); 78 void SetConnectionBandwidth(const uint32_t upBandwidth, const uint32_t downBandwidth); 79 void SetConnectionTcpBuffer(const std::string &tcpBuffer); 80 void SplitProxyIpAddress(const std::string &proxyIpAddress, std::string &host, uint16_t &port); 81 void UpdateNetworkInfoIfInActive(SetupDataCallResultInfo &info); 82 void UpdateReuseApnNetworkInfo(bool isAvailable); 83 void SetReuseApnCap(uint64_t cap); 84 uint64_t GetReuseApnCap() const; 85 void SetIfReuseSupplierId(bool isReused); 86 int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up); 87 void UnregisterNetInterfaceCallback(); 88 89 protected: 90 sptr<State> activeState_; 91 sptr<State> inActiveState_; 92 sptr<State> activatingState_; 93 sptr<State> disconnectingState_; 94 sptr<State> defaultState_; 95 sptr<State> currentState_; 96 sptr<DataConnectionManager> cdConnectionManager_; 97 std::shared_ptr<TelEventHandler> cellularDataHandler_; 98 sptr<NetManagerStandard::NetLinkInfo> netLinkInfo_; 99 sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo_; 100 101 private: 102 void SetCurrentState(const sptr<State> &&state); 103 void SetCid(const int32_t cid); 104 void DoConnect(const DataConnectionParams &connectionParams); 105 void FreeConnection(const DataDisconnectParams ¶ms); 106 void ResolveIp(std::vector<AddressInfo> &ipInfoArray); 107 void ResolveDns(std::vector<AddressInfo> &dnsInfoArray); 108 void ResolveRoute(std::vector<AddressInfo> &routeInfoArray, const std::string &name); 109 void GetMtuSizeFromOpCfg(int32_t &mtuSize, int32_t slotId); 110 std::string GetIpType(std::vector<AddressInfo> ipInfoArray); 111 bool HasMatchedIpTypeAddrs(uint8_t ipType, uint8_t ipInfoArraySize, std::vector<AddressInfo> ipInfoArray); 112 int32_t GetNetScoreBySlotId(int32_t slotId); 113 void GetNetworkSlicePara(const DataConnectionParams& connectionParams, sptr<ApnItem> apn); 114 void FillRSDFromNetCap(std::map<std::string, std::string> networkSliceParas, sptr<ApnItem> apn); 115 116 private: 117 friend class Active; 118 friend class Activating; 119 friend class Inactive; 120 friend class Default; 121 friend class Disconnecting; 122 int32_t cid_; 123 uint64_t capability_; 124 RadioTech rilRat_; 125 sptr<ApnItem> apnItem_; 126 int32_t apnId_; 127 std::mutex mtx_; 128 uint32_t upBandwidth_ = 0; 129 uint32_t downBandwidth_ = 0; 130 std::string tcpBuffer_; 131 int32_t connectId_ = 0; 132 int32_t cause_ = 0; 133 std::string ipType_ = ""; 134 int64_t startTimeConnectTimeoutTask_ = 0; 135 std::string ifName_ = ""; 136 sptr<OHOS::NetManagerStandard::INetInterfaceStateCallback> netInterfaceCallback_ = nullptr; 137 uint64_t reuseApnCap_ = NetManagerStandard::NetCap::NET_CAPABILITY_END; 138 }; 139 } // namespace Telephony 140 } // namespace OHOS 141 #endif // CELLULAR_DATA_STATE_MACHINE_H 142