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_MAIN_STATEMACHINE_H 17 #define NETWORKSHARE_MAIN_STATEMACHINE_H 18 19 #include <any> 20 #include <map> 21 22 #include "networkshare_hisysevent.h" 23 #include "networkshare_sub_statemachine.h" 24 #include "networkshare_upstreammonitor.h" 25 26 namespace OHOS { 27 namespace NetManagerStandard { 28 struct MessageIfaceActive { 29 int value_; 30 std::shared_ptr<NetworkShareSubStateMachine> subsm_; 31 }; 32 33 struct MessageUpstreamInfo { 34 int cmd_; 35 std::shared_ptr<UpstreamNetworkInfo> upstreamInfo_; 36 }; 37 38 class NetworkShareMainStateMachine { 39 using HandleFunc = int (NetworkShareMainStateMachine::*)(const std::any &messageObj); 40 41 public: 42 NetworkShareMainStateMachine() = delete; 43 explicit NetworkShareMainStateMachine(std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor); 44 ~NetworkShareMainStateMachine() = default; 45 46 /** 47 * switch to error state when error occur 48 */ 49 void SwitcheToErrorState(int32_t errType); 50 51 /** 52 * execute state switch 53 */ 54 void MainSmStateSwitch(int newState); 55 56 /** 57 * execute event 58 */ 59 void MainSmEventHandle(int eventId, const std::any &messageObj); 60 61 private: 62 bool TurnOnMainShareSettings(); 63 bool TurnOffMainShareSettings(); 64 int HandleInitInterfaceStateActive(const std::any &messageObj); 65 int HandleInitInterfaceStateInactive(const std::any &messageObj); 66 int HandleAliveInterfaceStateActive(const std::any &messageObj); 67 int HandleAliveInterfaceStateInactive(const std::any &messageObj); 68 int HandleAliveUpstreamMonitorCallback(const std::any &messageObj); 69 int HandleErrorInterfaceStateInactive(const std::any &messageObj); 70 int HandleErrorClear(const std::any &messageObj); 71 void InitStateEnter(); 72 void AliveStateEnter(); 73 void ErrorStateEnter(); 74 void InitStateExit(); 75 void AliveStateExit(); 76 void ErrorStateExit() const; 77 void ChooseUpstreamNetwork(); 78 void DisableForward(); 79 int EraseSharedSubSM(const std::any &messageObj); 80 81 private: 82 struct MainSmStateTable { 83 int event_; 84 int curState_; 85 HandleFunc func_; 86 int nextState_; 87 }; 88 89 std::recursive_mutex mutex_; 90 std::string netshareRequester_; 91 int32_t errorType_ = NETWORKSHARING_SHARING_NO_ERROR; 92 bool hasSetForward_ = false; 93 std::vector<std::shared_ptr<NetworkShareSubStateMachine>> subMachineList_; 94 std::shared_ptr<NetworkShareUpstreamMonitor> networkMonitor_ = nullptr; 95 int curState_ = MAINSTATE_INIT; 96 std::vector<MainSmStateTable> stateTable_; 97 std::string upstreamIfaceName_; 98 }; 99 } // namespace NetManagerStandard 100 } // namespace OHOS 101 #endif // NETWORKSHARE_MAIN_STATEMACHINE_H 102