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 PAN_STATEMACHINE_H
17 #define PAN_STATEMACHINE_H
18 #include <list>
19 #include <memory>
20 #include <string>
21 
22 #include "interface_adapter_manager.h"
23 #include "interface_adapter_classic.h"
24 #include "interface_adapter_ble.h"
25 #include "state_machine.h"
26 #include "pan_bnep.h"
27 #include "pan_defines.h"
28 #include "pan_network.h"
29 #include "timer.h"
30 #include "pan_message.h"
31 
32 namespace OHOS {
33 namespace bluetooth {
34 /**
35  * @brief Class for pan state machine.
36  */
37 class PanStateMachine : public utility::StateMachine {
38 public:
39     /**
40      * @brief Construct a new PanStateMachine object.
41      *
42      * @param address Device address.
43      */
44     explicit PanStateMachine(const std::string &address);
45 
46     /**
47      * @brief Destroy the PanStateMachine object.
48      */
49     ~PanStateMachine() = default;
50 
51     /**
52      * @brief Initialise the state machine.
53      */
54     void Init();
55 
56     /**
57      * @brief Check if current statemachine is removing.
58      *
59      * @return Returns <b>true</b> if the statemachine is removing; returns <b>false</b> if not.
60      */
61     bool IsRemoving() const;
62 
63     /**
64      * @brief Mark statemachine removing.
65      *
66      * @param isRemoving removing mark.
67      */
68     void SetRemoving(bool isRemoving);
69 
70     /**
71      * @brief Get the State Int object.
72      *
73      * @return Returns the state number.
74      */
75     int GetDeviceStateInt() const;
76 
77     uint16_t GetDeviceLcid();
78 
79     int GetDeviceType();
80 
81     void ProcessL2capConnectionEvent(const PanMessage &event);
82 
83     void ConnectionTimeout() const;
84     void StartConnectionTimer() const;
85     void StopConnectionTimer() const;
86     void DisonnectionTimeout() const;
87     void StartDisconnectionTimer() const;
88     void StopDisconnectionTimer() const;
89     void AddDeferredMessage(const PanMessage &msg);
90     void ProcessDeferredMessage();
91     std::string GetDeviceAdress();
92     void NotifyStateTransitions();
93     static std::string GetEventName(int what);
94 
95     void ProcessCloseReqEvent(const PanMessage &msg);
96     void ProcessOpenEvent(const PanMessage &msg);
97     void ProcessCloseEvent(const PanMessage &msg);
98     void ProcessReciveData(const PanMessage &msg);
99     void ProcessSendData(const PanMessage &msg);
100     void ProcessOpenComplete(const PanMessage &msg);
101     void setTethering(const bool enable);
102 
103     inline static const std::string DISCONNECTED = "Disconnected";
104     inline static const std::string CONNECTING = "Connecting";
105     inline static const std::string DISCONNECTING = "Disconnecting";
106     inline static const std::string CONNECTED = "Connected";
107 
108 private:
109     std::string address_;
110     bool isRemoving_ {false};
111     int preState_ {0};
112     std::list<PanMessage> deferMsgs_ {};
113     std::unique_ptr<utility::Timer> connTimer_ {nullptr};
114     std::unique_ptr<utility::Timer> disconnTimer_ {nullptr};
115     PanBnep panBnep_;
116     inline static const int CONNECTION_TIMEOUT_MS {60000};
117     inline static const int DISCONNECTION_TIMEOUT_MS {60000};
118     BT_DISALLOW_COPY_AND_ASSIGN(PanStateMachine);
119 };
120 
121 class PanState : public utility::StateMachine::State {
122 public:
PanState(const std::string & name,utility::StateMachine & statemachine,int stateInt,utility::StateMachine::State & parent)123     PanState(const std::string &name, utility::StateMachine &statemachine, int stateInt,
124         utility::StateMachine::State &parent)
125         : State(name, statemachine, parent), stateInt_(stateInt), stateMachine_((PanStateMachine &)statemachine)
126     {}
127 
PanState(const std::string & name,utility::StateMachine & statemachine,int stateInt)128     PanState(const std::string &name, utility::StateMachine &statemachine, int stateInt)
129         : State(name, statemachine), stateInt_(stateInt), stateMachine_((PanStateMachine &)statemachine)
130     {}
131 
~PanState()132     virtual ~PanState()
133     {}
GetStateInt()134     int GetStateInt() const
135     {
136         return stateInt_;
137     }
138 
139 protected:
140     int stateInt_ {PAN_STATE_DISCONNECTED};
141     PanStateMachine &stateMachine_;
142 };
143 
144 class PanDisconnectedState : public PanState {
145 public:
PanDisconnectedState(const std::string & name,utility::StateMachine & statemachine)146     PanDisconnectedState(const std::string &name, utility::StateMachine &statemachine)
147         : PanState(name, statemachine, PAN_STATE_DISCONNECTED)
148     {}
149     ~PanDisconnectedState() override = default;
150     void Entry() override;
151     void Exit() override;
152     bool Dispatch(const utility::Message &msg) override;
153 
154 private:
155     bool isReentry_ {false};
156 };
157 
158 class PanConnectingState : public PanState {
159 public:
PanConnectingState(const std::string & name,utility::StateMachine & statemachine)160     PanConnectingState(const std::string &name, utility::StateMachine &statemachine)
161         : PanState(name, statemachine, PAN_STATE_CONNECTING)
162     {}
163     ~PanConnectingState() override = default;
164     void Entry() override;
165     void Exit() override;
166     bool Dispatch(const utility::Message &msg) override;
167 
168 private:
169 };
170 
171 class PanDisconnectingState : public PanState {
172 public:
PanDisconnectingState(const std::string & name,utility::StateMachine & statemachine)173     PanDisconnectingState(const std::string &name, utility::StateMachine &statemachine)
174         : PanState(name, statemachine, PAN_STATE_DISCONNECTING)
175     {}
176     ~PanDisconnectingState() override = default;
177     void Entry() override;
178     void Exit() override;
179     bool Dispatch(const utility::Message &msg) override;
180 
181 private:
182 };
183 
184 class PanConnectedState : public PanState {
185 public:
PanConnectedState(const std::string & name,utility::StateMachine & statemachine)186     PanConnectedState(const std::string &name, utility::StateMachine &statemachine)
187         : PanState(name, statemachine, PAN_STATE_CONNECTED)
188     {}
189     ~PanConnectedState() override = default;
190     void Entry() override;
191     void Exit() override;
192     bool Dispatch(const utility::Message &msg) override;
193 
194 private:
195 };
196 }  // namespace bluetooth
197 }  // namespace OHOS
198 #endif  // Pan_STATEMACHINE_H
199