• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /**
62      * execute get mutex
63      */
64     std::recursive_mutex &GetEventMutex();
65 
66 private:
67     bool TurnOnMainShareSettings();
68     bool TurnOffMainShareSettings();
69     int HandleInitInterfaceStateActive(const std::any &messageObj);
70     int HandleInitInterfaceStateInactive(const std::any &messageObj);
71     int HandleAliveInterfaceStateActive(const std::any &messageObj);
72     int HandleAliveInterfaceStateInactive(const std::any &messageObj);
73     int HandleAliveUpstreamMonitorCallback(const std::any &messageObj);
74     int HandleErrorInterfaceStateInactive(const std::any &messageObj);
75     int HandleErrorClear(const std::any &messageObj);
76     void InitStateEnter();
77     void AliveStateEnter();
78     void ErrorStateEnter();
79     void InitStateExit();
80     void AliveStateExit();
81     void ErrorStateExit() const;
82     void ChooseUpstreamNetwork();
83     void DisableForward();
84     int EraseSharedSubSM(const std::any &messageObj);
85 
86 private:
87     struct MainSmStateTable {
88         int event_;
89         int curState_;
90         HandleFunc func_;
91         int nextState_;
92     };
93 
94     std::recursive_mutex mutex_;
95     std::string netshareRequester_;
96     int32_t errorType_ = NETWORKSHARING_SHARING_NO_ERROR;
97     bool hasSetForward_ = false;
98     std::vector<std::shared_ptr<NetworkShareSubStateMachine>> subMachineList_;
99     std::shared_ptr<NetworkShareUpstreamMonitor> networkMonitor_ = nullptr;
100     int curState_ = MAINSTATE_INIT;
101     std::vector<MainSmStateTable> stateTable_;
102     std::string upstreamIfaceName_;
103 };
104 } // namespace NetManagerStandard
105 } // namespace OHOS
106 #endif // NETWORKSHARE_MAIN_STATEMACHINE_H
107