• 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 OPP_STATEMACHINE_H
17 #define OPP_STATEMACHINE_H
18 #include <string>
19 #include "../obex/obex_client.h"
20 #include "opp_defines.h"
21 #include "opp_gap_client.h"
22 #include "opp_message.h"
23 #include "opp_sdp_client.h"
24 #include "state_machine.h"
25 #include "timer.h"
26 
27 namespace OHOS {
28 namespace bluetooth {
29 /**
30  * @brief Class for opp state machine.
31  */
32 class OppStateMachine : public utility::StateMachine {
33 public:
34     /**
35      * @brief Construct a new OppStateMachine object.
36      *
37      * @param address Device address.
38      */
39     explicit OppStateMachine(const std::string &address);
40 
41     /**
42      * @brief Destroy the OppStateMachine object.
43      */
44     ~OppStateMachine() = default;
45 
46     /**
47      * @brief Initialise the state machine.
48      */
49     void Init();
50 
51     /**
52      * @brief Check if current statemachine is removing.
53      *
54      * @return Returns <b>true</b> if the statemachine is removing; returns <b>false</b> if not.
55      */
56     bool IsRemoving() const;
57 
58     /**
59      * @brief Mark statemachine removing.
60      *
61      * @param isRemoving removing mark.
62      */
63     void SetRemoving(bool isRemoving);
64 
65     /**
66      * @brief Get the State Int object.
67      *
68      * @return Returns the state number.
69      */
70     int GetDeviceStateInt() const;
71 
72     void ConnectionTimeout() const;
73     void StartConnectionTimer() const;
74     void StopConnectionTimer() const;
75     void DisonnectionTimeout() const;
76     void StartDisconnectionTimer() const;
77     void StopDisconnectionTimer() const;
78 
79     std::string GetDeviceAdress();
80     static std::string GetEventName(int what);
81     void NotifyStateTransitions();
82 
83     void ProcessConnectReqEvent(const OppMessage &msg);
84     void ProcessDisconnectReqEvent(const OppMessage &msg);
85     void ProcessSdpCompleteEvent(const OppMessage &msg);
86     void ProcessGapCompleteEvent(const OppMessage &msg);
87     void ProcessConnectedEvent(const OppMessage &msg);
88     void ProcessDisconnectedEvent(const OppMessage &msg);
89 
90     inline static const std::string DISCONNECTED = "Disconnected";
91     inline static const std::string CONNECTING = "Connecting";
92     inline static const std::string DISCONNECTING = "Disconnecting";
93     inline static const std::string CONNECTED = "Connected";
94 
95 private:
96     std::string address_;
97     ObexClientConfig obexConfig_ {};
98     bool isRemoving_ {false};
99     int preState_ {0};
100     std::unique_ptr<OppSdpClient> sdpClient_ {nullptr};
101     std::unique_ptr<OppGapClient> gapClient_ {nullptr};
102     std::unique_ptr<utility::Timer> connTimer_ {nullptr};
103     std::unique_ptr<utility::Timer> disconnTimer_ {nullptr};
104     inline static const int connectionTimeoutMs {60000};
105     inline static const int disconnectionTimeoutMs {60000};
106     BT_DISALLOW_COPY_AND_ASSIGN(OppStateMachine);
107 };
108 
109 class OppState : public utility::StateMachine::State {
110 public:
OppState(const std::string & name,utility::StateMachine & statemachine,int stateInt,utility::StateMachine::State & parent)111     OppState(const std::string &name, utility::StateMachine &statemachine, int stateInt,
112         utility::StateMachine::State &parent)
113         : State(name, statemachine, parent), stateInt_(stateInt), stateMachine_((OppStateMachine &)statemachine)
114     {}
115 
OppState(const std::string & name,utility::StateMachine & statemachine,int stateInt)116     OppState(const std::string &name, utility::StateMachine &statemachine, int stateInt)
117         : State(name, statemachine), stateInt_(stateInt), stateMachine_((OppStateMachine &)statemachine)
118     {}
119 
~OppState()120     virtual ~OppState()
121     {}
GetStateInt()122     int GetStateInt() const
123     {
124         return stateInt_;
125     }
126 
127 protected:
128     int stateInt_ {OPP_STATE_DISCONNECTED};
129     OppStateMachine &stateMachine_;
130 };
131 
132 class OppDisconnectedState : public OppState {
133 public:
OppDisconnectedState(const std::string & name,utility::StateMachine & statemachine)134     OppDisconnectedState(const std::string &name, utility::StateMachine &statemachine)
135         : OppState(name, statemachine, OPP_STATE_DISCONNECTED)
136     {}
137     ~OppDisconnectedState() override = default;
138     void Entry() override;
139     void Exit() override;
140     bool Dispatch(const utility::Message &msg) override;
141 
142 private:
143     bool isReentry_ {false};
144 };
145 
146 class OppConnectingState : public OppState {
147 public:
OppConnectingState(const std::string & name,utility::StateMachine & statemachine)148     OppConnectingState(const std::string &name, utility::StateMachine &statemachine)
149         : OppState(name, statemachine, OPP_STATE_CONNECTING)
150     {}
151     ~OppConnectingState() override = default;
152     void Entry() override;
153     void Exit() override;
154     bool Dispatch(const utility::Message &msg) override;
155 };
156 
157 class OppDisconnectingState : public OppState {
158 public:
OppDisconnectingState(const std::string & name,utility::StateMachine & statemachine)159     OppDisconnectingState(const std::string &name, utility::StateMachine &statemachine)
160         : OppState(name, statemachine, OPP_STATE_DISCONNECTING)
161     {}
162     ~OppDisconnectingState() override = default;
163     void Entry() override;
164     void Exit() override;
165     bool Dispatch(const utility::Message &msg) override;
166 };
167 
168 class OppConnectedState : public OppState {
169 public:
OppConnectedState(const std::string & name,utility::StateMachine & statemachine)170     OppConnectedState(const std::string &name, utility::StateMachine &statemachine)
171         : OppState(name, statemachine, OPP_STATE_CONNECTED)
172     {}
173     ~OppConnectedState() override = default;
174     void Entry() override;
175     void Exit() override;
176     bool Dispatch(const utility::Message &msg) override;
177 };
178 }  // namespace bluetooth
179 }  // namespace OHOS
180 #endif  // OPP_STATEMACHINE_H
181