1 /* 2 * Copyright (c) 2022 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 NETWORKSHARE_SUB_STATEMACHINE_H 17 #define NETWORKSHARE_SUB_STATEMACHINE_H 18 19 #include <any> 20 #include <cstring> 21 #include <map> 22 #include <mutex> 23 #include <set> 24 #include <sstream> 25 26 #include "net_manager_constants.h" 27 #include "net_manager_ext_constants.h" 28 #include "networkshare_configuration.h" 29 #include "networkshare_constants.h" 30 #include "networkshare_hisysevent.h" 31 #include "networkshare_state_common.h" 32 #include "dhcp_c_api.h" 33 34 namespace OHOS { 35 namespace NetManagerStandard { 36 class NetworkShareSubStateMachine : public std::enable_shared_from_this<NetworkShareSubStateMachine> { 37 using HandleFunc = int (NetworkShareSubStateMachine::*)(const std::any &messageObj); 38 39 public: 40 NetworkShareSubStateMachine() = delete; 41 NetworkShareSubStateMachine(const std::string &ifaceName, const SharingIfaceType &interfaceType, 42 const std::shared_ptr<NetworkShareConfiguration> &configuration); 43 ~NetworkShareSubStateMachine() = default; 44 45 /** 46 * get sub state machine share type 47 */ 48 SharingIfaceType GetNetShareType() const; 49 50 /** 51 * get sub state machine interface name 52 */ 53 const std::string &GetInterfaceName() const; 54 55 class SubStateMachineCallback { 56 public: 57 virtual void OnUpdateInterfaceState(const std::shared_ptr<NetworkShareSubStateMachine> ¶SubStateMachine, 58 int state, int lastError) = 0; 59 }; 60 61 /** 62 * register callback 63 */ 64 void RegisterSubSMCallback(const std::shared_ptr<SubStateMachineCallback> &callback); 65 66 /** 67 * execute state switch 68 */ 69 void SubSmStateSwitch(int newState); 70 71 /** 72 * execute event 73 */ 74 void SubSmEventHandle(int eventId, const std::any &messageObj); 75 76 /** 77 * get down interface name 78 */ 79 void GetDownIfaceName(std::string &downIface); 80 81 /** 82 * get up interface name 83 */ 84 void GetUpIfaceName(std::string &upIface); 85 86 void HandleConnection(); 87 88 private: 89 void CreateInitStateTable(); 90 void CreateSharedStateTable(); 91 void InitStateEnter(); 92 void SharedStateEnter(); 93 void UnavailableStateEnter(); 94 void InitStateExit(); 95 void SharedStateExit(); 96 void UnavailableStateExit(); 97 int HandleInitSharingRequest(const std::any &messageObj); 98 int HandleInitInterfaceDown(const std::any &messageObj); 99 int HandleSharedUnrequest(const std::any &messageObj); 100 int HandleSharedInterfaceDown(const std::any &messageObj); 101 int HandleSharedConnectionChange(const std::any &messageObj); 102 int HandleSharedErrors(const std::any &messageObj); 103 104 bool ConfigureShareDhcp(bool enabled); 105 bool RequestIpv4Address(std::shared_ptr<INetAddr> &netAddr); 106 bool StartDhcp(const std::shared_ptr<INetAddr> &netAddr); 107 bool SetRange(DhcpRange &range, const std::string &ipHead, const std::string &strStartip, 108 const std::string &strEndip, const std::string &mask); 109 bool StopDhcp(); 110 void HandleConnectionChanged(const std::shared_ptr<UpstreamNetworkInfo> &upstreamNetInfo); 111 void RemoveRoutesToLocalNetwork(); 112 void AddRoutesToLocalNetwork(); 113 void CleanupUpstreamInterface(); 114 bool HasChangeUpstreamIfaceSet(const std::string &newUpstreamIface); 115 bool GetWifiHotspotDhcpFlag(); 116 bool GetBtDestinationAddr(std::string &addrStr); 117 bool GetWifiApDestinationAddr(std::string &addrStr); 118 bool GetUsbDestinationAddr(std::string &addrStr); 119 bool CheckConfig(std::string &endIp, std::string &mask); 120 bool FindDestinationAddr(std::string &destination); 121 std::recursive_mutex &getUsefulMutex(); 122 123 private: 124 struct SubSmStateTable { 125 int32_t event_; 126 int32_t curState_; 127 HandleFunc func_; 128 int32_t nextState_; 129 }; 130 std::recursive_mutex mutex_; 131 std::string ifaceName_; 132 SharingIfaceType netShareType_; 133 int32_t lastError_ = NETMANAGER_EXT_SUCCESS; 134 std::string upstreamIfaceName_; 135 std::shared_ptr<SubStateMachineCallback> trackerCallback_ = nullptr; 136 std::shared_ptr<NetworkShareConfiguration> configuration_ = nullptr; 137 int32_t curState_ = SUBSTATE_INIT; 138 std::vector<SubSmStateTable> stateTable_; 139 }; 140 } // namespace NetManagerStandard 141 } // namespace OHOS 142 #endif // NETWORKSHARE_SUB_STATEMACHINE_H 143