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