• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 GATT_CONNECTION_MANAGER_H
17 #define GATT_CONNECTION_MANAGER_H
18 
19 #include <cstdint>
20 #include <list>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <tuple>
25 #include <vector>
26 #include "base_def.h"
27 #include "dispatcher.h"
28 #include "gatt_connection_observer.h"
29 #include "gatt_data.h"
30 #include "raw_address.h"
31 #include "state_machine.h"
32 #include "timer.h"
33 
34 namespace OHOS {
35 namespace bluetooth {
36 class GattConnectionManager {
37 public:
38     class Device {
39     public:
40         struct ConnectionPriorityResult {
41             uint16_t interval_;
42             uint16_t latency_;
43             uint16_t timeout_;
44             int status;
45         };
46         static const std::string STATE_IDLE;
47         static const std::string STATE_CONNECTING;
48         static const std::string STATE_CONNECTED;
49         static const std::string STATE_DISCONNECTING;
50         static const std::string STATE_DISCONNECTED;
51 
52         class StateMachine : public utility::StateMachine {
53         public:
54             enum StateMessage {
55                 MSG_CONNECT,
56                 MSG_CONNECT_COMPLETE,
57                 MSG_DISCONNECT,
58                 MSG_DISCONNECT_COMPLETE,
59                 MSG_REQUEST_CONNECTION_PRIORITY,
60                 MSG_RESPONSE_CONNECTION_PRIORITY,
61                 MSG_DIRECT_CONNECT_TIMEOUT,
62                 MSG_RECONNECT_CAUSE_0X3E
63             };
64             explicit StateMachine(GattConnectionManager::Device &device);
65             ~StateMachine();
66 
67         private:
68             class StateBase : public utility::StateMachine::State {
69             public:
70                 StateBase(const std::string &name, utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~StateBase()71                 virtual ~StateBase()
72                 {}
73 
74             protected:
75                 GattConnectionManager::Device &device_;
76             };
77             struct Idle : public StateBase {
78                 Idle(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~IdleIdle79                 ~Idle() override{};
80                 void Entry() override;
ExitIdle81                 void Exit() override{};
82                 bool Dispatch(const utility::Message &msg) override;
83             };
84             struct Connecting : public StateBase {
85                 Connecting(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~ConnectingConnecting86                 ~Connecting() override{};
87                 void Entry() override;
ExitConnecting88                 void Exit() override{};
89                 bool Dispatch(const utility::Message &msg) override;
90             };
91             struct Connected : public StateBase {
92                 Connected(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~ConnectedConnected93                 ~Connected() override{};
94                 void Entry() override;
ExitConnected95                 void Exit() override{};
96                 bool Dispatch(const utility::Message &msg) override;
97             };
98             struct Disconnecting : public StateBase {
99                 Disconnecting(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~DisconnectingDisconnecting100                 ~Disconnecting() override{};
101                 void Entry() override;
ExitDisconnecting102                 void Exit() override{};
103                 bool Dispatch(const utility::Message &msg) override;
104             };
105             struct Disconnected : public StateBase {
106                 Disconnected(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~DisconnectedDisconnected107                 ~Disconnected() override{};
108                 void Entry() override;
ExitDisconnected109                 void Exit() override{};
110                 bool Dispatch(const utility::Message &msg) override;
111             };
112         };
113 
114         Device(const uint8_t *addr, uint8_t transport, uint8_t type, bool autoConnect = false);
115         explicit Device(const GattDevice &device, bool autoConnect = false);
116         ~Device();
117         bool ProcessMessage(int messageId, int arg1 = 0, void *arg2 = nullptr);
118         void CheckEncryption();
119         bool &AutoConnect();
120         uint8_t *Addr();
121         GattDevice &Info();
122         StateMachine &SM();
123         uint16_t &MTU();
124         uint16_t &Handle();
125         uint8_t &Role();
126         uint8_t &RetryTimes();
127         std::mutex &DeviceRWMutex();
128         utility::Timer &DirectConnect();
129 
130     private:
131         bool autoConnect_;
132         uint16_t mtu_;
133         uint16_t handle_;
134         uint8_t role_;
135         uint8_t retry_;
136         uint8_t addr_[RawAddress::BT_ADDRESS_BYTE_LEN] = {0};
137         GattDevice info_;
138         std::mutex deviceRWMutex_;
139         StateMachine sm_;
140         utility::Timer directConnect_;
141         void CopyAddress(const uint8_t *addr, size_t length);
142         Device() = delete;
143         BT_DISALLOW_COPY_AND_ASSIGN(Device);
144         BT_DISALLOW_MOVE_AND_ASSIGN(Device);
145     };
146 
GetInstance()147     static GattConnectionManager &GetInstance()
148     {
149         static GattConnectionManager instance;
150         return instance;
151     }
152     ~GattConnectionManager();
153 
154     int Connect(const GattDevice &device, bool autoConnect = false) const;
155     int Disconnect(const GattDevice &device) const;
156     int RegisterObserver(GattConnectionObserver &observer) const;
157     void DeregisterObserver(int registerId) const;
158     const std::string &GetDeviceState(const GattDevice &device) const;
159     void GetDevices(std::vector<GattDevice> &devices) const;
160     std::pair<uint16_t, uint16_t> GetMaximumNumberOfConnections() const;
161     std::tuple<std::string, uint16_t, uint16_t> GetDeviceInformation(const GattDevice &device) const;
162     uint8_t GetDeviceTransport(uint16_t handle) const;
163     int RequestConnectionPriority(uint16_t handle, int connPriority) const;
164     bool GetEncryptionInfo(uint16_t connectHandle) const;
165     bool GetEncryptionInfo(const GattDevice &device) const;
166     int SetConnectionType(const GattDevice &device, bool autoConnect) const;
167 
168     int StartUp(utility::Dispatcher &dispatcher);
169     int ShutDown() const;
170 
171 private:
172     static const uint8_t MAX_OBSERVER_NUM = 4;
173     static const uint16_t BT_PSM_GATT = 0x001F;
174     static const int DIRECT_CONNECT_TIMEOUT = 30000;
175     GattConnectionManager();
176     DECLARE_IMPL();
177 
178     BT_DISALLOW_COPY_AND_ASSIGN(GattConnectionManager);
179 };
180 }  // namespace bluetooth
181 }  // namespace OHOS
182 
183 #endif  // GATT_CONNECTION_MANAGER_H
184