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

Side by Side Diff: device/bluetooth/bluetooth_low_energy_win_fake.h

Issue 1749403002: Implement BluetoothRemoteGattCharacteristicWin::StartNotifySession and related unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add more unit tests Created 4 years, 9 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ 6 #define DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_
7 7
8 #include "device/bluetooth/bluetooth_low_energy_win.h" 8 #include "device/bluetooth/bluetooth_low_energy_win.h"
9 9
10 #include <set> 10 #include <set>
11 #include <unordered_map> 11 #include <unordered_map>
12 12
13 namespace device { 13 namespace device {
14 namespace win { 14 namespace win {
15 15
16 struct BLEDevice; 16 struct BLEDevice;
17 struct GattService; 17 struct GattService;
18 struct GattServiceObserver;
18 struct GattCharacteristic; 19 struct GattCharacteristic;
19 struct GattDescriptor; 20 struct GattDescriptor;
20 21
21 // The key of BLEDevicesMap is the string of the BLE device address. 22 // The key of BLEDevicesMap is the string of the BLE device address.
22 typedef std::unordered_map<std::string, scoped_ptr<BLEDevice>> BLEDevicesMap; 23 typedef std::unordered_map<std::string, scoped_ptr<BLEDevice>> BLEDevicesMap;
23 // The key of GattServicesMap, GattCharacteristicsMap and GattDescriptorsMap is 24 // The key of GattServicesMap, GattCharacteristicsMap and GattDescriptorsMap is
24 // the string of the attribute handle. 25 // the string of the attribute handle.
25 typedef std::unordered_map<std::string, scoped_ptr<GattService>> 26 typedef std::unordered_map<std::string, scoped_ptr<GattService>>
26 GattServicesMap; 27 GattServicesMap;
27 typedef std::unordered_map<std::string, scoped_ptr<GattCharacteristic>> 28 typedef std::unordered_map<std::string, scoped_ptr<GattCharacteristic>>
28 GattCharacteristicsMap; 29 GattCharacteristicsMap;
29 typedef std::unordered_map<std::string, scoped_ptr<GattDescriptor>> 30 typedef std::unordered_map<std::string, scoped_ptr<GattDescriptor>>
30 GattDescriptorsMap; 31 GattDescriptorsMap;
31 // The key of BLEAttributeHandleTable is the string of the BLE device address. 32 // The key of BLEAttributeHandleTable is the string of the BLE device address.
32 typedef std::unordered_map<std::string, scoped_ptr<std::set<USHORT>>> 33 typedef std::unordered_map<std::string, scoped_ptr<std::set<USHORT>>>
33 BLEAttributeHandleTable; 34 BLEAttributeHandleTable;
35 // The key of GattServiceObserverTable is GattServiceObserver pointer.
36 // Note: The underlying data type of BLUETOOTH_GATT_EVENT_HANDLE is PVOID.
37 typedef std::unordered_map<BLUETOOTH_GATT_EVENT_HANDLE,
38 scoped_ptr<GattServiceObserver>>
39 GattServiceObserverTable;
34 40
35 struct BLEDevice { 41 struct BLEDevice {
36 BLEDevice(); 42 BLEDevice();
37 ~BLEDevice(); 43 ~BLEDevice();
38 scoped_ptr<BluetoothLowEnergyDeviceInfo> device_info; 44 scoped_ptr<BluetoothLowEnergyDeviceInfo> device_info;
39 GattServicesMap primary_services; 45 GattServicesMap primary_services;
40 }; 46 };
41 47
42 struct GattService { 48 struct GattService {
43 GattService(); 49 GattService();
44 ~GattService(); 50 ~GattService();
45 scoped_ptr<BTH_LE_GATT_SERVICE> service_info; 51 scoped_ptr<BTH_LE_GATT_SERVICE> service_info;
46 GattServicesMap included_services; 52 GattServicesMap included_services;
47 GattCharacteristicsMap included_characteristics; 53 GattCharacteristicsMap included_characteristics;
54 std::vector<BLUETOOTH_GATT_EVENT_HANDLE> observers;
48 }; 55 };
49 56
50 struct GattCharacteristic { 57 struct GattCharacteristic {
51 GattCharacteristic(); 58 GattCharacteristic();
52 ~GattCharacteristic(); 59 ~GattCharacteristic();
53 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristic_info; 60 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristic_info;
54 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value; 61 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value;
55 GattDescriptorsMap included_descriptors; 62 GattDescriptorsMap included_descriptors;
56 std::vector<HRESULT> read_errors; 63 std::vector<HRESULT> read_errors;
57 std::vector<HRESULT> write_errors; 64 std::vector<HRESULT> write_errors;
65 std::vector<HRESULT> notify_errors;
58 }; 66 };
59 67
60 struct GattDescriptor { 68 struct GattDescriptor {
61 GattDescriptor(); 69 GattDescriptor();
62 ~GattDescriptor(); 70 ~GattDescriptor();
63 scoped_ptr<BTH_LE_GATT_DESCRIPTOR> descriptor_info; 71 scoped_ptr<BTH_LE_GATT_DESCRIPTOR> descriptor_info;
64 scoped_ptr<BTH_LE_GATT_DESCRIPTOR_VALUE> value; 72 scoped_ptr<BTH_LE_GATT_DESCRIPTOR_VALUE> value;
73 std::vector<HRESULT> write_errors;
74 };
75
76 struct GattServiceObserver {
77 GattServiceObserver();
78 ~GattServiceObserver();
79 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback;
80 PVOID context;
65 }; 81 };
66 82
67 // Fake implementation of BluetoothLowEnergyWrapper. Used for BluetoothTestWin. 83 // Fake implementation of BluetoothLowEnergyWrapper. Used for BluetoothTestWin.
68 class BluetoothLowEnergyWrapperFake : public BluetoothLowEnergyWrapper { 84 class BluetoothLowEnergyWrapperFake : public BluetoothLowEnergyWrapper {
69 public: 85 public:
70 class Observer { 86 class Observer {
71 public: 87 public:
72 Observer() {} 88 Observer() {}
73 ~Observer() {} 89 ~Observer() {}
74 90
75 virtual void onWriteGattCharacteristicValue( 91 virtual void OnReadGattCharacteristicValue() = 0;
92 virtual void OnWriteGattCharacteristicValue(
76 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) = 0; 93 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) = 0;
94 virtual void OnWriteGattDescriptorValue(
95 const PBTH_LE_GATT_DESCRIPTOR_VALUE value) = 0;
96 virtual void OnStartCharacteristicNotification() = 0;
77 }; 97 };
78 98
79 BluetoothLowEnergyWrapperFake(); 99 BluetoothLowEnergyWrapperFake();
80 ~BluetoothLowEnergyWrapperFake() override; 100 ~BluetoothLowEnergyWrapperFake() override;
81 101
82 bool IsBluetoothLowEnergySupported() override; 102 bool IsBluetoothLowEnergySupported() override;
83 bool EnumerateKnownBluetoothLowEnergyDevices( 103 bool EnumerateKnownBluetoothLowEnergyDevices(
84 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, 104 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices,
85 std::string* error) override; 105 std::string* error) override;
86 bool EnumerateKnownBluetoothLowEnergyGattServiceDevices( 106 bool EnumerateKnownBluetoothLowEnergyGattServiceDevices(
(...skipping 14 matching lines...) Expand all
101 scoped_ptr<BTH_LE_GATT_DESCRIPTOR>* out_included_descriptors, 121 scoped_ptr<BTH_LE_GATT_DESCRIPTOR>* out_included_descriptors,
102 USHORT* out_counts) override; 122 USHORT* out_counts) override;
103 HRESULT ReadCharacteristicValue( 123 HRESULT ReadCharacteristicValue(
104 base::FilePath& service_path, 124 base::FilePath& service_path,
105 const PBTH_LE_GATT_CHARACTERISTIC characteristic, 125 const PBTH_LE_GATT_CHARACTERISTIC characteristic,
106 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value) override; 126 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value) override;
107 HRESULT WriteCharacteristicValue( 127 HRESULT WriteCharacteristicValue(
108 base::FilePath& service_path, 128 base::FilePath& service_path,
109 const PBTH_LE_GATT_CHARACTERISTIC characteristic, 129 const PBTH_LE_GATT_CHARACTERISTIC characteristic,
110 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) override; 130 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) override;
131 HRESULT RegisterGattEvents(base::FilePath& service_path,
132 BTH_LE_GATT_EVENT_TYPE type,
133 PVOID event_parameter,
134 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback,
135 PVOID context,
136 BLUETOOTH_GATT_EVENT_HANDLE* out_handle) override;
137 HRESULT UnregisterGattEvent(
138 BLUETOOTH_GATT_EVENT_HANDLE event_handle) override;
111 139
112 BLEDevice* SimulateBLEDevice(std::string device_name, 140 BLEDevice* SimulateBLEDevice(std::string device_name,
113 BLUETOOTH_ADDRESS device_address); 141 BLUETOOTH_ADDRESS device_address);
114 BLEDevice* GetSimulatedBLEDevice(std::string device_address); 142 BLEDevice* GetSimulatedBLEDevice(std::string device_address);
115 void RemoveSimulatedBLEDevice(std::string device_address); 143 void RemoveSimulatedBLEDevice(std::string device_address);
116 144
117 // Note: |parent_service| may be nullptr to indicate a primary service. 145 // Note: |parent_service| may be nullptr to indicate a primary service.
118 GattService* SimulateGattService(BLEDevice* device, 146 GattService* SimulateGattService(BLEDevice* device,
119 GattService* parent_service, 147 GattService* parent_service,
120 const BTH_LE_UUID& uuid); 148 const BTH_LE_UUID& uuid);
(...skipping 13 matching lines...) Expand all
134 std::string device_address, 162 std::string device_address,
135 GattService* parent_service, 163 GattService* parent_service,
136 const BTH_LE_GATT_CHARACTERISTIC& characteristic); 164 const BTH_LE_GATT_CHARACTERISTIC& characteristic);
137 void SimulateGattCharacteriscRemove(GattService* parent_service, 165 void SimulateGattCharacteriscRemove(GattService* parent_service,
138 std::string attribute_handle); 166 std::string attribute_handle);
139 GattCharacteristic* GetSimulatedGattCharacteristic( 167 GattCharacteristic* GetSimulatedGattCharacteristic(
140 GattService* parent_service, 168 GattService* parent_service,
141 std::string attribute_handle); 169 std::string attribute_handle);
142 void SimulateGattCharacteristicValue(GattCharacteristic* characteristic, 170 void SimulateGattCharacteristicValue(GattCharacteristic* characteristic,
143 const std::vector<uint8_t>& value); 171 const std::vector<uint8_t>& value);
172 void SimulateCharacteristicValueChangeNotification(
173 GattService* parent_service,
174 GattCharacteristic* characteristic);
144 void SimulateGattCharacteristicReadError(GattCharacteristic* characteristic, 175 void SimulateGattCharacteristicReadError(GattCharacteristic* characteristic,
145 HRESULT error); 176 HRESULT error);
146 void SimulateGattCharacteristicWriteError(GattCharacteristic* characteristic, 177 void SimulateGattCharacteristicWriteError(GattCharacteristic* characteristic,
147 HRESULT error); 178 HRESULT error);
179 void SimulateGattCharacteristicSetNotifyError(
180 GattCharacteristic* characteristic,
181 HRESULT error);
148 void SimulateGattDescriptor(std::string device_address, 182 void SimulateGattDescriptor(std::string device_address,
149 GattCharacteristic* characteristic, 183 GattCharacteristic* characteristic,
150 const BTH_LE_UUID& uuid); 184 const BTH_LE_UUID& uuid);
185 GattDescriptor* GetSimulatedDescriptor(
186 GattCharacteristic* parent_characteristic,
187 std::string attribute_handle);
188 void SimulateGattDescriptorWrite(GattDescriptor* descriptor,
189 const std::vector<uint8_t>& value);
190 void SimulateGattDescriptorWriteError(GattDescriptor* descriptor,
191 HRESULT error);
151 void AddObserver(Observer* observer); 192 void AddObserver(Observer* observer);
152 193
153 private: 194 private:
154 // Get simulated characteristic by |service_path| and |characteristic| info. 195 // Get simulated characteristic by |service_path| and |characteristic| info.
155 GattCharacteristic* GetSimulatedGattCharacteristic( 196 GattCharacteristic* GetSimulatedGattCharacteristic(
156 base::FilePath& service_path, 197 base::FilePath& service_path,
157 const PBTH_LE_GATT_CHARACTERISTIC characteristic); 198 const PBTH_LE_GATT_CHARACTERISTIC characteristic);
158 199
159 // Generate an unique attribute handle on |device_address|. 200 // Generate an unique attribute handle on |device_address|.
160 USHORT GenerateAUniqueAttributeHandle(std::string device_address); 201 USHORT GenerateAUniqueAttributeHandle(std::string device_address);
(...skipping 14 matching lines...) Expand all
175 216
176 // Extract service attribute handles from the |path| generated by 217 // Extract service attribute handles from the |path| generated by
177 // GenerateGattServiceDevicePath. 218 // GenerateGattServiceDevicePath.
178 std::vector<std::string> ExtractServiceAttributeHandlesFromDevicePath( 219 std::vector<std::string> ExtractServiceAttributeHandlesFromDevicePath(
179 base::string16 path); 220 base::string16 path);
180 221
181 // The canonical BLE device address string format is the 222 // The canonical BLE device address string format is the
182 // BluetoothDevice::CanonicalizeAddress. 223 // BluetoothDevice::CanonicalizeAddress.
183 std::string BluetoothAddressToCanonicalString(const BLUETOOTH_ADDRESS& btha); 224 std::string BluetoothAddressToCanonicalString(const BLUETOOTH_ADDRESS& btha);
184 225
226 // Get client |characteristic| configuration descriptor which has standard
227 // UUID = 00002902-0000-1000-8000-00805f9b34fb.
228 GattDescriptor* GetCCCDescriptor(GattCharacteristic* characteristic);
229
185 // Table to store allocated attribute handle for a device. 230 // Table to store allocated attribute handle for a device.
186 BLEAttributeHandleTable attribute_handle_table_; 231 BLEAttributeHandleTable attribute_handle_table_;
187 BLEDevicesMap simulated_devices_; 232 BLEDevicesMap simulated_devices_;
188 Observer* observer_; 233 Observer* observer_;
234 GattServiceObserverTable gatt_service_observers_;
189 }; 235 };
190 236
191 } // namespace win 237 } // namespace win
192 } // namespace device 238 } // namespace device
193 239
194 #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ 240 #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698