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 SOFTAP_MANAGER_STATE_MACHINE_H
17 #define SOFTAP_MANAGER_STATE_MACHINE_H
18 
19 #include "state.h"
20 #include "state_machine.h"
21 #include "wifi_logger.h"
22 #include "wifi_errcode.h"
23 #include <string>
24 #include "wifi_ap_msg.h"
25 #include "i_ap_service_callbacks.h"
26 #include "wifi_internal_msg.h"
27 #include "wifi_controller_define.h"
28 
29 namespace OHOS {
30 namespace Wifi {
31 class SoftapManagerMachine : public StateMachine {
32 public:
33     SoftapManagerMachine();
34     ~SoftapManagerMachine();
35 
36     class IdleState : public State {
37     public:
38         explicit IdleState(SoftapManagerMachine *softapManagerMachine);
39         ~IdleState() override;
40         void GoInState() override;
41         void GoOutState() override;
42         bool ExecuteStateMsg(InternalMessagePtr msg) override;
43 
44     private:
45         SoftapManagerMachine *pSoftapManagerMachine;
46         void HandleStartInIdleState(InternalMessagePtr msg);
47     };
48 
49     class DefaultState : public State {
50     public:
51         explicit DefaultState(SoftapManagerMachine *softapManagerMachine);
52         ~DefaultState() override;
53         void GoInState() override;
54         void GoOutState() override;
55         bool ExecuteStateMsg(InternalMessagePtr msg) override;
56 
57     private:
58         SoftapManagerMachine *pSoftapManagerMachine;
59     };
60 
61     class StartedState : public State {
62     public:
63         explicit StartedState(SoftapManagerMachine *softapManagerMachine);
64         ~StartedState() override;
65         void GoInState() override;
66         void GoOutState() override;
67         bool ExecuteStateMsg(InternalMessagePtr msg) override;
68 
69     private:
70         SoftapManagerMachine *pSoftapManagerMachine;
71     };
72 
73 public:
74     ErrCode InitSoftapManagerMachine();
75     ErrCode RegisterCallback(const SoftApModeCallback &callbacks);
76 
77 private:
78     template <typename T>
ParsePointer(T * & pointer)79     inline void ParsePointer(T *&pointer)
80     {
81         if (pointer != nullptr) {
82             delete pointer;
83             pointer = nullptr;
84         }
85     }
86 
87     template <typename T>
JudgmentEmpty(T * & pointer)88     inline ErrCode JudgmentEmpty(T *&pointer)
89     {
90         if (pointer == nullptr) {
91             return WIFI_OPT_FAILED;
92         }
93         return WIFI_OPT_SUCCESS;
94     }
95 
96     void BuildStateTree();
97     ErrCode InitSoftapManagerStates();
98     void StopSoftap();
99     DefaultState *pDefaultState;
100     IdleState *pIdleState;
101     StartedState *pStartedState;
102     SoftApModeCallback mcb;
103     static int mid;
104     std::string ifaceName{""};
105 };
106 } // namespace Wifi
107 } // namespace OHOS
108 #endif