OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H | |
6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "components/proximity_auth/ble/fake_wire_message.h" | |
13 #include "components/proximity_auth/connection.h" | |
14 #include "device/bluetooth/bluetooth_adapter.h" | |
15 #include "device/bluetooth/bluetooth_device.h" | |
16 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | |
17 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | |
18 #include "device/bluetooth/bluetooth_uuid.h" | |
19 | |
20 namespace proximity_auth { | |
21 | |
22 // Represents a connection with a remote device over Bluetooth low energy. The | |
23 // connection is a persistent bidirectional channel for sending and receiving | |
24 // wire messages. The remote device is the peripheral mode and the service | |
25 // contains two characteristics: one to send data and another to receive it. | |
26 class BluetoothLowEnergyConnection : public Connection, | |
27 public device::BluetoothAdapter::Observer { | |
28 public: | |
29 enum class ControlSignal : uint32 { | |
30 kInviteToConnectSignal = 0, | |
31 kInvitationResponseSignal = 1, | |
32 kSendSignal = 2, | |
33 kDisconnectSignal = 3, | |
34 }; | |
35 | |
36 // Constructs a Bluetooth low energy connection to the service with | |
Tim Song
2015/05/12 03:02:47
I think an diagram would be much more descriptive
sacomoto
2015/05/12 07:23:19
Done.
| |
37 // |remote_service_uuid| on the |remote_device|. The GATT connection with | |
38 // |remote_device| must be already established and |adapter| already | |
39 // initialized. | |
40 // | |
41 // The connection is complete when: | |
42 // (i) |to_peripheral_char_| and |from_peripheral_char_| were found; | |
43 // (ii) |notify_session_| was set for |from_peripheral_char_|; | |
44 // (iii) A kInviteToConnectSignal was sent; | |
45 // (iv) kInvitationResponseSignal was received. | |
46 // | |
47 // Conditions (i) and (ii) can happen in any order, but an invite to connect | |
48 // signal is sent only when (i) and (ii) are satisfied. | |
49 BluetoothLowEnergyConnection( | |
50 const RemoteDevice& remote_device, | |
51 scoped_refptr<device::BluetoothAdapter> adapter, | |
52 const device::BluetoothUUID remote_service_uuid, | |
53 const device::BluetoothUUID to_peripheral_char_uuid, | |
54 const device::BluetoothUUID from_peripheral_char_uuid, | |
55 scoped_ptr<device::BluetoothGattConnection> gatt_connection); | |
56 | |
57 ~BluetoothLowEnergyConnection(); | |
58 | |
59 // proximity_auth::Connection | |
60 void Connect() override; | |
61 void Disconnect() override; | |
62 | |
63 protected: | |
64 // proximity_auth::Connection | |
65 void SendMessageImpl(scoped_ptr<WireMessage> message) override; | |
66 scoped_ptr<WireMessage> DeserializeWireMessage( | |
67 bool* is_incomplete_message) override; | |
68 | |
69 // device::BluetoothAdapter::Observer | |
70 void DeviceRemoved(device::BluetoothAdapter* adapter, | |
71 device::BluetoothDevice* device) override; | |
72 void GattDiscoveryCompleteForService( | |
73 device::BluetoothAdapter* adapter, | |
74 device::BluetoothGattService* service) override; | |
75 void GattCharacteristicAdded( | |
76 device::BluetoothAdapter* adapter, | |
77 device::BluetoothGattCharacteristic* characteristic) override; | |
78 void GattCharacteristicValueChanged( | |
79 device::BluetoothAdapter* adapter, | |
80 device::BluetoothGattCharacteristic* characteristic, | |
81 const std::vector<uint8>& value) override; | |
82 | |
83 private: | |
84 struct RemoteCharacteristic { | |
85 device::BluetoothUUID uuid; | |
86 std::string id; | |
87 }; | |
88 | |
89 // Sends an invite to connect signal to the peripheral if the connection | |
90 // is ready. That is, satisfying the conditions: | |
91 // (i) |to_peripheral_char_| and |from_peripheral_char_| were found; | |
92 // (ii) |notify_session_| was set for |from_peripheral_char_|. | |
93 // | |
94 // The asynchronous events that can casue these conditions to be | |
95 // satisfied are: | |
96 // (i) a new characteristic is discovered (HandleCharacteristicUpdate); | |
97 // (ii) a new notify session is started (OnNotifySessionStarted). | |
98 void SendInviteToConnectSignal(); | |
99 | |
100 // Called when there is an error writing to the remote characteristic | |
101 // |to_peripheral_char_|. | |
102 void OnWriteRemoteCharacteristicError( | |
103 device::BluetoothGattService::GattErrorCode error); | |
104 | |
105 // Handles the discovery of a new characteristic. | |
106 void HandleCharacteristicUpdate( | |
107 device::BluetoothGattCharacteristic* characteristic); | |
108 | |
109 // This function verifies if the connection is complete and updates | |
110 // the status accordingly. The only asynchronous event | |
111 // that can cause the connection to be completed is receiving a | |
112 // kInvitationResponseSignal (GattCharateristcValueChanged). | |
113 void CompleteConnection(); | |
114 | |
115 // Starts a notify session for |from_peripheral_char_|. | |
116 void StartNotifySession(); | |
117 | |
118 // Called when there is an error starting a notification session for | |
119 // |from_peripheral_char_| characteristic. | |
120 void OnNotifySessionError(device::BluetoothGattService::GattErrorCode); | |
121 | |
122 // Called when a notification session is successfully started for | |
123 // |from_peripheral_char_| characteristic. | |
124 void OnNotifySessionStarted( | |
125 scoped_ptr<device::BluetoothGattNotifySession> notify_session); | |
126 | |
127 // Stops |notify_session_|. | |
128 void StopNotifySession(); | |
129 | |
130 // Updates the value of |to_peripheral_char_| and | |
131 // |from_peripheral_char_| | |
132 // when |characteristic| was found. | |
133 void UpdateCharacteristicsStatus( | |
134 device::BluetoothGattCharacteristic* characteristic); | |
135 | |
136 // Returns the Bluetooth address of the remote device. | |
137 const std::string& GetRemoteDeviceAddress(); | |
138 | |
139 // Returns the device corresponding to |remote_device_address_|. | |
140 device::BluetoothDevice* GetRemoteDevice(); | |
141 | |
142 // Returns the service corresponding to |remote_service_uuid_| in the current | |
143 // device. | |
144 device::BluetoothGattService* GetRemoteService(); | |
145 | |
146 // Returns the characteristic corresponding to |identifier| in the current | |
147 // service. | |
148 device::BluetoothGattCharacteristic* GetGattCharacteristic( | |
149 const std::string& identifier); | |
150 | |
151 // Convert the first 4 bytes from a byte vector to a uint32. | |
152 uint32 ToUint32(const std::vector<uint8>& bytes); | |
153 | |
154 // Convert an uint32 to a byte array. | |
155 const std::string ToString(const uint32 value); | |
156 | |
157 // The Bluetooth adapter over which the Bluetooth connection will be made. | |
158 scoped_refptr<device::BluetoothAdapter> adapter_; | |
159 | |
160 // The uuid of the service it looks for to establish a GattConnection. | |
161 const device::BluetoothUUID remote_service_uuid_; | |
Tim Song
2015/05/12 03:02:47
You can put |remote_service_uuid_| and |remote_ser
sacomoto
2015/05/12 07:23:19
Done.
Tim Song
2015/05/13 02:36:56
I don't see this change.
| |
162 | |
163 // The identifier of the service corresponding to |remote_service_uuid_|. | |
164 std::string remote_service_id_; | |
165 | |
166 // Characteristic used to send data to the remote device. | |
167 RemoteCharacteristic to_peripheral_char_; | |
168 | |
169 // Characteristic used to receive data from the remote device. | |
170 RemoteCharacteristic from_peripheral_char_; | |
171 | |
172 // The GATT connection with the remote device. | |
173 scoped_ptr<device::BluetoothGattConnection> connection_; | |
174 | |
175 // Indicates if there is a pending notification session. Used to ensure there | |
176 // is only one pending notify session. | |
177 bool notify_session_pending_; | |
178 | |
179 // The notify session for |from_peripheral_char|. | |
180 scoped_ptr<device::BluetoothGattNotifySession> notify_session_; | |
181 | |
182 // Indicates if there pending response to a invite to connect signal. | |
183 bool connect_signal_response_pending_; | |
184 | |
185 base::WeakPtrFactory<BluetoothLowEnergyConnection> weak_ptr_factory_; | |
186 | |
187 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnection); | |
188 }; | |
189 | |
190 } // namespace proximity_auth | |
191 | |
192 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_H | |
OLD | NEW |