• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 bluetooth {
35 class GattConnectionManager {
36 public:
37     class Device {
38     public:
39         struct ConnectionPriorityResult {
40             uint16_t interval_;
41             uint16_t latency_;
42             uint16_t timeout_;
43             int status;
44         };
45         static const std::string STATE_IDLE;
46         static const std::string STATE_CONNECTING;
47         static const std::string STATE_CONNECTED;
48         static const std::string STATE_DISCONNECTING;
49         static const std::string STATE_DISCONNECTED;
50 
51         class StateMachine : public utility::StateMachine {
52         public:
53             enum StateMessage {
54                 MSG_CONNECT,
55                 MSG_CONNECT_COMPLETE,
56                 MSG_DISCONNECT,
57                 MSG_DISCONNECT_COMPLETE,
58                 MSG_REQUEST_CONNECTION_PRIORITY,
59                 MSG_RESPONSE_CONNECTION_PRIORITY,
60                 MSG_DIRECT_CONNECT_TIMEOUT,
61                 MSG_RECONNECT_CAUSE_0X3E
62             };
63             explicit StateMachine(GattConnectionManager::Device &device);
64             ~StateMachine();
65 
66         private:
67             class StateBase : public utility::StateMachine::State {
68             public:
69                 StateBase(const std::string &name, utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~StateBase()70                 virtual ~StateBase()
71                 {}
72 
73             protected:
74                 GattConnectionManager::Device &device_;
75             };
76             struct Idle : public StateBase {
77                 Idle(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~IdleIdle78                 ~Idle(){};
79                 void Entry() override;
ExitIdle80                 void Exit() override{};
81                 bool Dispatch(const utility::Message &msg) override;
82             };
83             struct Connecting : public StateBase {
84                 Connecting(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~ConnectingConnecting85                 ~Connecting(){};
86                 void Entry() override;
ExitConnecting87                 void Exit() override{};
88                 bool Dispatch(const utility::Message &msg) override;
89             };
90             struct Connected : public StateBase {
91                 Connected(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~ConnectedConnected92                 ~Connected(){};
93                 void Entry() override;
ExitConnected94                 void Exit() override{};
95                 bool Dispatch(const utility::Message &msg) override;
96             };
97             struct Disconnecting : public StateBase {
98                 Disconnecting(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~DisconnectingDisconnecting99                 ~Disconnecting(){};
100                 void Entry() override;
ExitDisconnecting101                 void Exit() override{};
102                 bool Dispatch(const utility::Message &msg) override;
103             };
104             struct Disconnected : public StateBase {
105                 Disconnected(utility::StateMachine &stateMachine, GattConnectionManager::Device &device);
~DisconnectedDisconnected106                 ~Disconnected(){};
107                 void Entry() override;
ExitDisconnected108                 void Exit() override{};
109                 bool Dispatch(const utility::Message &msg) override;
110             };
111         };
112 
113         Device(const uint8_t *addr, uint8_t transport, uint8_t type, bool autoConnect = false);
114         explicit Device(const GattDevice &device, bool autoConnect = false);
115         ~Device();
116         bool ProcessMessage(int messageId, int arg1 = 0, void *arg2 = nullptr);
117         void CheckEncryption();
118         bool &AutoConnect();
119         uint8_t *Addr();
120         GattDevice &Info();
121         StateMachine &SM();
122         uint16_t &MTU();
123         uint16_t &Handle();
124         uint8_t &Role();
125         uint8_t &RetryTimes();
126         std::mutex &DeviceRWMutex();
127         utility::Timer &DirectConnect();
128 
129     private:
130         bool autoConnect_;
131         uint16_t mtu_;
132         uint16_t handle_;
133         uint8_t role_;
134         uint8_t retry_;
135         uint8_t addr_[RawAddress::BT_ADDRESS_BYTE_LEN] = {0};
136         GattDevice info_;
137         std::mutex deviceRWMutex_;
138         StateMachine sm_;
139         utility::Timer directConnect_;
140         void CopyAddress(const uint8_t *addr, size_t length);
141         Device() = delete;
142         DISALLOW_COPY_AND_ASSIGN(Device);
143         DISALLOW_MOVE_AND_ASSIGN(Device);
144     };
145 
GetInstance()146     static GattConnectionManager &GetInstance()
147     {
148         static GattConnectionManager instance;
149         return instance;
150     }
151     ~GattConnectionManager();
152 
153     int Connect(const GattDevice &device, bool autoConnect = false) const;
154     int Disconnect(const GattDevice &device) const;
155     int RegisterObserver(GattConnectionObserver &observer) const;
156     void DeregisterObserver(int registerId) const;
157     const std::string &GetDeviceState(const GattDevice &device) const;
158     void GetDevices(std::vector<GattDevice> &devices) const;
159     std::pair<uint16_t, uint16_t> GetMaximumNumberOfConnections() const;
160     std::tuple<std::string, uint16_t, uint16_t> GetDeviceInformation(const GattDevice &device) const;
161     uint8_t GetDeviceTransport(uint16_t handle) const;
162     int RequestConnectionPriority(uint16_t handle, int connPriority) const;
163     bool GetEncryptionInfo(uint16_t connectHandle) const;
164     bool GetEncryptionInfo(const GattDevice &device) const;
165     int SetConnectionType(const GattDevice &device, bool autoConnect) const;
166 
167     int StartUp(utility::Dispatcher &dispatcher);
168     int ShutDown() const;
169 
170 private:
171     static const uint8_t MAX_OBSERVER_NUM = 4;
172     static const uint16_t BT_PSM_GATT = 0x001F;
173     static const int DIRECT_CONNECT_TIMEOUT = 30000;
174     GattConnectionManager();
175     DECLARE_IMPL();
176 
177     DISALLOW_COPY_AND_ASSIGN(GattConnectionManager);
178 };
179 }  // namespace bluetooth
180 
181 #endif  // GATT_CONNECTION_MANAGER_H
182