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 CONCRETE_MANAGER_STATE_MACHINE_H
17 #define CONCRETE_MANAGER_STATE_MACHINE_H
18 
19 #include <string>
20 #include "state_machine.h"
21 #include "wifi_logger.h"
22 #include "wifi_errcode.h"
23 #include "sta_service_callback.h"
24 #include "iscan_service_callbacks.h"
25 #include "wifi_internal_msg.h"
26 #include "wifi_controller_define.h"
27 #include "wifi_service_manager.h"
28 #include "state.h"
29 
30 namespace OHOS {
31 namespace Wifi {
32 class ConcreteMangerMachine : public StateMachine {
33 public:
34     ConcreteMangerMachine();
35     ~ConcreteMangerMachine();
36 
37     class IdleState : public State {
38     public:
39         explicit IdleState(ConcreteMangerMachine *concreteMangerMachine);
40         ~IdleState() override;
41         void GoInState() override;
42         void GoOutState() override;
43         bool ExecuteStateMsg(InternalMessagePtr msg) override;
44 
45     private:
46         ConcreteMangerMachine *pConcreteMangerMachine;
47         void HandleSwitchToConnectMode(InternalMessagePtr msg);
48         void HandleSwitchToScanOnlyMode(InternalMessagePtr msg);
49         void HandleStartInIdleState(InternalMessagePtr msg);
50         void HandleSwitchToSemiActiveMode(InternalMessagePtr msg);
51     };
52 
53     class DefaultState : public State {
54     public:
55         explicit DefaultState(ConcreteMangerMachine *concreteMangerMachine);
56         ~DefaultState() override;
57         void GoInState() override;
58         void GoOutState() override;
59         bool ExecuteStateMsg(InternalMessagePtr msg) override;
60 
61     private:
62         ConcreteMangerMachine *pConcreteMangerMachine;
63     };
64 
65     class ConnectState : public State {
66     public:
67         explicit ConnectState(ConcreteMangerMachine *concreteMangerMachine);
68         ~ConnectState() override;
69         void GoInState() override;
70         void GoOutState() override;
71         bool ExecuteStateMsg(InternalMessagePtr msg) override;
72 
73     private:
74         ConcreteMangerMachine *pConcreteMangerMachine;
75         void SwitchScanOnlyInConnectState();
76         void SwitchSemiActiveInConnectState();
77     };
78 
79     class ScanonlyState : public State {
80     public:
81         explicit ScanonlyState(ConcreteMangerMachine *concreteMangerMachine);
82         ~ScanonlyState() override;
83         void GoInState() override;
84         void GoOutState() override;
85         bool ExecuteStateMsg(InternalMessagePtr msg) override;
86 
87     private:
88         ConcreteMangerMachine *pConcreteMangerMachine;
89         void SwitchConnectInScanOnlyState();
90         void SwitchSemiActiveInScanOnlyState();
91     };
92 
93     class SemiActiveState : public State {
94     public:
95         explicit SemiActiveState(ConcreteMangerMachine *concreteMangerMachine);
96         ~SemiActiveState() override;
97         void GoInState() override;
98         void GoOutState() override;
99         bool ExecuteStateMsg(InternalMessagePtr msg) override;
100 
101     private:
102         ConcreteMangerMachine *pConcreteMangerMachine;
103         void SwitchConnectInSemiActiveState();
104         void SwitchScanOnlyInSemiActiveState();
105     };
106 
107 public:
108     ErrCode InitConcreteMangerMachine();
109     void RegisterCallback(ConcreteModeCallback &callback);
110     void SetTargetRole(ConcreteManagerRole role);
111 
112 private:
113     template <typename T>
ParsePointer(T * & pointer)114     inline void ParsePointer(T *&pointer)
115     {
116         if (pointer != nullptr) {
117             delete pointer;
118             pointer = nullptr;
119         }
120     }
121 
122     template <typename T>
JudgmentEmpty(T * & pointer)123     inline ErrCode JudgmentEmpty(T *&pointer)
124     {
125         if (pointer == nullptr) {
126             return WIFI_OPT_FAILED;
127         }
128         return WIFI_OPT_SUCCESS;
129     }
130 
131     void BuildStateTree();
132     ErrCode InitConcreteMangerStates();
133 
134     bool HandleCommonMessage(InternalMessagePtr msg);
135     void CheckAndContinueToStopWifi(InternalMessagePtr msg);
136     void HandleStaStop();
137     void HandleStaStart();
138     void HandleStaSemiActive();
139     ErrCode SwitchSemiFromEnable();
140     ErrCode SwitchEnableFromSemi();
141     void ReportClose();
142     void ClearIfaceName();
143 
144     DefaultState *pDefaultState;
145     IdleState *pIdleState;
146     ConnectState *pConnectState;
147     ScanonlyState *pScanonlyState;
148     SemiActiveState *pSemiActiveState;
149     static int mTargetRole;
150     ConcreteModeCallback mcb;
151     static int mid;
152     static std::string ifaceName;
153 };
154 } // namespace Wifi
155 } // namespace OHOS
156 #endif
157