1 /* 2 * Copyright (C) 2021-2023 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 WIFICONTROLLER_WIFICONTROLLERMACHINE_H 17 #define WIFICONTROLLER_WIFICONTROLLERMACHINE_H 18 19 #include <string> 20 #include <vector> 21 #include "state.h" 22 #include "state_machine.h" 23 #include "wifi_logger.h" 24 #include "wifi_errcode.h" 25 #include "concrete_clientmode_manager.h" 26 #include "multi_sta_manager.h" 27 #ifdef FEATURE_AP_SUPPORT 28 #include "softap_manager.h" 29 #endif 30 31 namespace OHOS { 32 namespace Wifi { 33 class WifiControllerMachine : public StateMachine { 34 public: 35 WifiControllerMachine(); 36 ~WifiControllerMachine(); 37 38 class DisableState : public State { 39 public: 40 explicit DisableState(WifiControllerMachine *wifiControllerMachine); 41 ~DisableState() override; 42 void GoInState() override; 43 void GoOutState() override; 44 bool ExecuteStateMsg(InternalMessagePtr msg) override; 45 46 private: 47 WifiControllerMachine *pWifiControllerMachine; 48 }; 49 50 class EnableState : public State { 51 public: 52 explicit EnableState(WifiControllerMachine *wifiControllerMachine); 53 ~EnableState() override; 54 void GoInState() override; 55 void GoOutState() override; 56 bool ExecuteStateMsg(InternalMessagePtr msg) override; 57 void HandleStaStartFailure(int id); 58 void HandleStaRemoved(InternalMessagePtr msg); 59 void HandleWifi2Removed(InternalMessagePtr msg); 60 void HandleAPServiceStartFail(int id); 61 void HandleConcreteClientRemoved(InternalMessagePtr msg); 62 63 private: 64 void HandleApStart(int id); 65 void HandleWifiToggleChangeInEnabledState(InternalMessagePtr msg); 66 #ifdef FEATURE_AP_SUPPORT 67 void HandleSoftapToggleChangeInEnabledState(InternalMessagePtr msg); 68 void HandleApRemoved(InternalMessagePtr msg); 69 void HandleApStop(InternalMessagePtr msg); 70 bool HandleApMsg(InternalMessagePtr msg); 71 #endif 72 WifiControllerMachine *pWifiControllerMachine; 73 }; 74 75 class DefaultState : public State { 76 public: 77 explicit DefaultState(WifiControllerMachine *wifiControllerMachine); 78 ~DefaultState() override; 79 void GoInState() override; 80 void GoOutState() override; 81 bool ExecuteStateMsg(InternalMessagePtr msg) override; 82 83 private: 84 WifiControllerMachine *pWifiControllerMachine; 85 }; 86 87 public: 88 ErrCode InitWifiControllerMachine(); 89 90 void RemoveConcreteManager(int id); 91 void RemoveMultiStaManager(int id); 92 void HandleStaClose(int id); 93 void HandleWifi2Close(int id); 94 void HandleStaStart(int id); 95 void HandleWifi2Start(int id); 96 void HandleStaSemiActive(int id); 97 void HandleConcreteStop(int id); 98 void ClearWifiStartFailCount(); 99 #ifdef FEATURE_AP_SUPPORT 100 void RmoveSoftapManager(int id); 101 void HandleSoftapStop(int id); 102 void StartSoftapCloseTimer(); 103 void StopSoftapCloseTimer(); 104 #endif 105 void ShutdownWifi(bool shutDownAp = true); 106 107 private: 108 template <typename T> ParsePointer(T * & pointer)109 inline void ParsePointer(T *&pointer) 110 { 111 if (pointer != nullptr) { 112 delete pointer; 113 pointer = nullptr; 114 } 115 } 116 117 template <typename T> JudgmentEmpty(T * & pointer)118 inline ErrCode JudgmentEmpty(T *&pointer) 119 { 120 if (pointer == nullptr) { 121 return WIFI_OPT_FAILED; 122 } 123 return WIFI_OPT_SUCCESS; 124 } 125 126 void BuildStateTree(); 127 ErrCode InitWifiStates(); 128 bool HasAnyConcreteManager(); 129 bool HasAnyMultiStaManager(); 130 bool HasAnyManager(); 131 bool ConcreteIdExist(int id); 132 bool IsWifi2IdExist(int id); 133 void MakeConcreteManager(ConcreteManagerRole role, int id); 134 void MakeMultiStaManager(MultiStaManager::Role role, int instId); 135 #ifdef FEATURE_AP_SUPPORT 136 bool HasAnySoftApManager(); 137 bool SoftApIdExist(int id); 138 void MakeSoftapManager(SoftApManager::Role role, int id); 139 bool ShouldEnableSoftap(); 140 void StopAllSoftapManagers(); 141 void StopSoftapManager(int id); 142 SoftApManager *GetSoftApManager(int id); 143 #endif 144 bool ShouldDisableWifi(InternalMessagePtr msg); 145 bool ShouldEnableWifi(int id = 0); 146 ConcreteManagerRole GetWifiRole(); 147 void StopAllConcreteManagers(); 148 void StopConcreteManager(int id); 149 void StopAllMultiStaManagers(); 150 void StopMultiStaManager(int id); 151 void SwitchRole(ConcreteManagerRole role); 152 void HandleAirplaneOpen(); 153 void HandleAirplaneClose(); 154 static bool IsWifiEnable(int id = 0); 155 static bool IsSemiWifiEnable(); 156 static bool IsScanOnlyEnable(); 157 158 #ifndef HDI_CHIP_INTERFACE_SUPPORT 159 std::atomic<int> mApidStopWifi; 160 #endif 161 EnableState *pEnableState; 162 DisableState *pDisableState; 163 DefaultState *pDefaultState; 164 std::vector<ConcreteClientModeManager *> concreteManagers; 165 mutable std::mutex concreteManagerMutex; 166 static int mWifiStartFailCount; 167 #ifdef FEATURE_AP_SUPPORT 168 std::vector<SoftApManager *> softapManagers; 169 mutable std::mutex softapManagerMutex; 170 uint64_t stopSoftapTimerId_ {0}; 171 #endif 172 mutable std::mutex multiStaManagerMutex; 173 std::vector<MultiStaManager *> multiStaManagers; 174 }; 175 } // namespace Wifi 176 } // namespace OHOS 177 #endif // WIFICONTROLLER_WIFICONTROLLERMACHINE_H 178