Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(797)

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_weave_server.h

Issue 2183523006: Chrome OS uWeave Characteristics Server (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@migration
Patch Set: change GetEid to take RemoteDevice Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_
6 #define COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <map>
12 #include <memory>
13 #include <vector>
14
15 #include "base/memory/ref_counted.h"
16 #include "base/task_runner.h"
17 #include "components/proximity_auth/ble/bluetooth_low_energy_advertisement_rotat or.h"
18 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_defines.h"
19 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_server_connec tion.h"
20 #include "components/proximity_auth/connection.h"
21 #include "components/proximity_auth/remote_device.h"
22 #include "device/bluetooth/bluetooth_adapter.h"
23 #include "device/bluetooth/bluetooth_device.h"
24 #include "device/bluetooth/bluetooth_local_gatt_characteristic.h"
25 #include "device/bluetooth/bluetooth_local_gatt_descriptor.h"
26 #include "device/bluetooth/bluetooth_local_gatt_service.h"
27
28 namespace base {
29 class TaskRunner;
30 }
31
32 namespace proximity_auth {
33 namespace weave {
34 namespace {
35
36 using BluetoothAdapter = device::BluetoothAdapter;
37 using BluetoothAdvertisement = device::BluetoothAdvertisement;
38 using BluetoothDevice = device::BluetoothDevice;
39 using BluetoothLocalGattCharacteristic =
40 device::BluetoothLocalGattCharacteristic;
41 using BluetoothLocalGattService = device::BluetoothLocalGattService;
42 using BluetoothLocalGattDescriptor = device::BluetoothLocalGattDescriptor;
43 using ValueCallback = base::Callback<void(const std::vector<uint8_t>&)>;
44 using ErrorCallback = base::Closure;
45 using ConnectionStatus = proximity_auth::Connection::Status;
46
47 } // namespace
48
49 class BluetoothLowEnergyWeaveServerConnection;
50
51 // Represent a server for device's connection requests.
52 // Listens for requests from registered devices on the RX and TX
53 // characteristics.
54 // A registered device is either being advertised to or connected with the
55 // server.
56 // If a device is being advertised to, the advertisement could either be
57 // registered on the adapter or waiting on the queue in advertisement rotator.
58 // The device must be in the ad_rotator_ but its corresponding advertisement
59 // may or may not be advertising at one moment.
60 class BluetoothLowEnergyWeaveServer
61 : public device::BluetoothLocalGattService::Delegate {
62 public:
63 class Factory {
64 public:
65 static std::unique_ptr<BluetoothLowEnergyWeaveServer> NewInstance();
66
67 // Exposed for testing.
68 static void SetInstanceForTesting(Factory* factory);
69
70 protected:
71 // Exposed for testing.
72 virtual std::unique_ptr<BluetoothLowEnergyWeaveServer> BuildInstance();
73
74 private:
75 static Factory* factory_instance_;
76 };
77
78 // TODO(jingxuy): The observer for this class.
79
80 // device::BluetoothLocalGattService::Delegate:
81 void OnCharacteristicReadRequest(
82 const BluetoothDevice* bluetooth_device,
83 const BluetoothLocalGattCharacteristic* characteristic,
84 int offset,
85 const ValueCallback& callback,
86 const ErrorCallback& error_callback) override;
87
88 // If the write request is from a known device and the TX characteristic,
89 // then forward |value| to the appropriate ServerConnection of that device.
90 void OnCharacteristicWriteRequest(
91 const BluetoothDevice* bluetooth_device,
92 const BluetoothLocalGattCharacteristic* characteristic,
93 const Packet& value,
94 int offset,
95 const base::Closure& callback,
96 const ErrorCallback& error_callback) override;
97
98 void OnDescriptorReadRequest(const BluetoothDevice* device,
99 const BluetoothLocalGattDescriptor* descriptor,
100 int offset,
101 const ValueCallback& callback,
102 const ErrorCallback& error_callback) override {}
103
104 void OnDescriptorWriteRequest(const BluetoothDevice* device,
105 const BluetoothLocalGattDescriptor* descriptor,
106 const std::vector<uint8_t>& value,
107 int offset,
108 const base::Closure& callback,
109 const ErrorCallback& error_callback) override {}
110
111 void OnNotificationsStart(
112 const BluetoothDevice* bluetooth_device,
113 const BluetoothLocalGattCharacteristic* characteristic) override;
114
115 void OnNotificationsStop(
116 const BluetoothDevice* bluetooth_device,
117 const BluetoothLocalGattCharacteristic* characteristic) override;
118
119 ~BluetoothLowEnergyWeaveServer();
120
121 // |remote_device| must not share a bluetooth_address with any device already
122 // registered.
123 // Registration effectively puts the device in advertisement queue.
124 void RegisterDevice(const RemoteDevice& remote_device);
125
126 // |remote_device| must already be registered.
127 // If the device is advertising, the advertisement will be taken down.
128 // If the device is connected, it will be disconnected immediately.
129 void UnregisterDevice(const RemoteDevice& remote_device);
130
131 bool HasDevice(std::string device_address);
Kyle Horimoto 2016/07/29 03:15:39 rkc@: Is this a reasonable function to use? I thou
jingxuy 2016/07/29 18:45:43 I had the same question sort of. But from the exte
Kyle Horimoto 2016/07/29 19:04:18 In that case, it is the Bluetooth MAC address from
jingxuy 2016/07/29 20:26:01 hmmmm, I think this actually post a pretty large p
Kyle Horimoto 2016/07/29 21:07:46 Interesting - I thought that the RemoteDevice obje
Kyle Horimoto 2016/07/29 21:11:34 An approach we've used on other platforms is just
132
133 const RemoteDevice& GetRemoteDevice(const BluetoothDevice* bluetooth_device);
134
135 // |remote_device| must already be registered and not advertisement enabled.
136 void EnableAdvertising(const RemoteDevice& remote_device);
Kyle Horimoto 2016/07/29 03:15:39 This class should rotate advertisements between th
jingxuy 2016/07/29 18:45:43 I put it here so that the serverConnection can sta
Kyle Horimoto 2016/07/29 19:04:18 The server connection should not know anything abo
jingxuy 2016/07/29 20:26:01 so you mean this class should be an observer of th
Kyle Horimoto 2016/07/29 21:07:45 Yeah, but I think it would be better to create a w
137
138 // |remote_device| must already be registered and advertisement enabled.
139 void DisableAdvertising(const RemoteDevice& remote_device);
140
141 protected:
142 BluetoothLowEnergyWeaveServer();
143
144 // Sets |task_runner_| for testing.
145 void SetTaskRunnerForTesting(scoped_refptr<base::TaskRunner> task_runner);
146
147 private:
148 bool IsAdvertising(std::string bluetooth_address);
149 bool IsConnected(std::string bluetooth_address);
150
151 // Rotating function for refreshing the EID seed. Will post itself on the
152 // task runner upon completion to run at the next 14 day refresh boundary.
153 void UpdateEidSeeds();
Kyle Horimoto 2016/07/29 03:15:39 We should try to rotate which devices we advertise
jingxuy 2016/07/29 18:45:43 You misunderstood. This function is for refreshing
Kyle Horimoto 2016/07/29 19:04:18 EidGenerator does this for you already; this class
jingxuy 2016/07/29 20:26:01 This does not do the refreshing. It calls EIDGener
Kyle Horimoto 2016/07/29 21:07:46 Yeah, but this should not happen every 14 days. Ea
154
155 // Rotating function for refreshing the EID. Will post itself on the task
156 // runner upon completion to run at the next 8 hour refresh boundary.
157 void UpdateEids();
Kyle Horimoto 2016/07/29 03:15:39 Same.
jingxuy 2016/07/29 18:45:43 This function is for refreshing advertisement alre
Kyle Horimoto 2016/07/29 19:04:18 Same - EidGenerator does this for you already.
jingxuy 2016/07/29 20:26:01 same as above.
Kyle Horimoto 2016/07/29 21:07:46 Same as above.
158
159 // Refresh the currently registered functions. Since the currently registered
160 // advertisements could not be modified, unregister them and tries to
161 // re-register them. If they are not re-registered, they are guarnateed to be
162 // on the front of the advertisement queue.
163 void RefreshAdvertisement();
164
165 // Rotating function for giving other devices a fair share of the advertising
166 // time slot. Will post itself on the task runner upon completion to run in
167 // 5 seconds.
168 void RotateAdvertisement();
jingxuy 2016/07/29 18:45:43 This function is for actually rotating 5 seconds.
jingxuy 2016/07/29 20:26:01 @Kyle
Kyle Horimoto 2016/07/29 21:07:46 Yep - is there a question I'm supposed to answer?
169
170 // Helper to attempt to register an advertisement.
171 void Advertise();
172
173 void UnregisterAdvertisement(std::string bluetooth_address);
174
175 // Remove the advertisement from map_to_advertisement_ because it's no longer
176 // advertising.
177 void UnregisterAdvertisementSuccess(std::string bluetooth_address);
178
179 // This should not happen.
180 void UnregisterAdvertisementError(std::string bluetooth_address,
181 BluetoothAdvertisement::ErrorCode error);
182
183 // If it's a success, then rotate the ad_rotator_ to go to the next
184 // advertisement and attempt to register that one.
185 void RegisterAdvertisementSuccess(
186 std::string bluetooth_address,
187 scoped_refptr<BluetoothAdvertisement> advertisement);
188
189 // Stop the rolling advertisement process by doing nothing.
190 // TODO(jingxuu): do I need to log anything?
191 void RegisterAdvertisementError(BluetoothAdvertisement::ErrorCode error) {}
192
193 // TODO(jingxuu): currently this is an empty function, unsure whether if
194 // anything needed to be done when the adapter is created.
195 void CreateAdapterCallback() {}
196
197 scoped_refptr<base::TaskRunner> task_runner_;
198
199 // All registered devices included here.
200 std::map<std::string, RemoteDevice> map_to_remote_device_;
201
202 std::unique_ptr<BluetoothLowEnergyAdvertisementRotator> ad_rotator_;
203
204 // The currently registered advertisements.
205 std::map<std::string, scoped_refptr<BluetoothAdvertisement>>
206 map_to_advertisement_;
207
208 // The currently active connections. One connection per device allowed.
209 std::map<std::string,
210 std::unique_ptr<BluetoothLowEnergyWeaveServerConnection>>
211 map_to_server_connection_;
212
213 scoped_refptr<BluetoothAdapter> adapter_;
214
215 base::WeakPtr<BluetoothLocalGattService> service_;
216
217 base::WeakPtr<BluetoothLocalGattCharacteristic> rx_characteristic_;
218 base::WeakPtr<BluetoothLocalGattCharacteristic> tx_characteristic_;
219
220 base::WeakPtrFactory<BluetoothLowEnergyWeaveServer> weak_ptr_factory_;
221 };
222
223 } // namespace weave
224
225 } // namespace proximity_auth
226
227 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_BLUETOOTH_LOW_ENERGY_WEAVE_SERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698