| OLD | NEW |
| 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 OnWriteGattCharacteristicValue( |
| 76 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) = 0; | 92 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) = 0; |
| 93 virtual void OnWriteGattDescriptorValue( |
| 94 const PBTH_LE_GATT_DESCRIPTOR_VALUE value) = 0; |
| 95 virtual void OnStartCharacteristicNotification() = 0; |
| 77 }; | 96 }; |
| 78 | 97 |
| 79 BluetoothLowEnergyWrapperFake(); | 98 BluetoothLowEnergyWrapperFake(); |
| 80 ~BluetoothLowEnergyWrapperFake() override; | 99 ~BluetoothLowEnergyWrapperFake() override; |
| 81 | 100 |
| 82 bool IsBluetoothLowEnergySupported() override; | 101 bool IsBluetoothLowEnergySupported() override; |
| 83 bool EnumerateKnownBluetoothLowEnergyDevices( | 102 bool EnumerateKnownBluetoothLowEnergyDevices( |
| 84 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, | 103 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, |
| 85 std::string* error) override; | 104 std::string* error) override; |
| 86 bool EnumerateKnownBluetoothLowEnergyGattServiceDevices( | 105 bool EnumerateKnownBluetoothLowEnergyGattServiceDevices( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 101 scoped_ptr<BTH_LE_GATT_DESCRIPTOR>* out_included_descriptors, | 120 scoped_ptr<BTH_LE_GATT_DESCRIPTOR>* out_included_descriptors, |
| 102 USHORT* out_counts) override; | 121 USHORT* out_counts) override; |
| 103 HRESULT ReadCharacteristicValue( | 122 HRESULT ReadCharacteristicValue( |
| 104 base::FilePath& service_path, | 123 base::FilePath& service_path, |
| 105 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | 124 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 106 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value) override; | 125 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value) override; |
| 107 HRESULT WriteCharacteristicValue( | 126 HRESULT WriteCharacteristicValue( |
| 108 base::FilePath& service_path, | 127 base::FilePath& service_path, |
| 109 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | 128 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 110 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) override; | 129 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) override; |
| 130 HRESULT RegisterGattEvents(base::FilePath& service_path, |
| 131 BTH_LE_GATT_EVENT_TYPE type, |
| 132 PVOID event_parameter, |
| 133 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback, |
| 134 PVOID context, |
| 135 BLUETOOTH_GATT_EVENT_HANDLE* out_handle) override; |
| 136 HRESULT UnregisterGattEvent( |
| 137 BLUETOOTH_GATT_EVENT_HANDLE event_handle) override; |
| 111 | 138 |
| 112 BLEDevice* SimulateBLEDevice(std::string device_name, | 139 BLEDevice* SimulateBLEDevice(std::string device_name, |
| 113 BLUETOOTH_ADDRESS device_address); | 140 BLUETOOTH_ADDRESS device_address); |
| 114 BLEDevice* GetSimulatedBLEDevice(std::string device_address); | 141 BLEDevice* GetSimulatedBLEDevice(std::string device_address); |
| 115 void RemoveSimulatedBLEDevice(std::string device_address); | 142 void RemoveSimulatedBLEDevice(std::string device_address); |
| 116 | 143 |
| 117 // Note: |parent_service| may be nullptr to indicate a primary service. | 144 // Note: |parent_service| may be nullptr to indicate a primary service. |
| 118 GattService* SimulateGattService(BLEDevice* device, | 145 GattService* SimulateGattService(BLEDevice* device, |
| 119 GattService* parent_service, | 146 GattService* parent_service, |
| 120 const BTH_LE_UUID& uuid); | 147 const BTH_LE_UUID& uuid); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 134 std::string device_address, | 161 std::string device_address, |
| 135 GattService* parent_service, | 162 GattService* parent_service, |
| 136 const BTH_LE_GATT_CHARACTERISTIC& characteristic); | 163 const BTH_LE_GATT_CHARACTERISTIC& characteristic); |
| 137 void SimulateGattCharacteriscRemove(GattService* parent_service, | 164 void SimulateGattCharacteriscRemove(GattService* parent_service, |
| 138 std::string attribute_handle); | 165 std::string attribute_handle); |
| 139 GattCharacteristic* GetSimulatedGattCharacteristic( | 166 GattCharacteristic* GetSimulatedGattCharacteristic( |
| 140 GattService* parent_service, | 167 GattService* parent_service, |
| 141 std::string attribute_handle); | 168 std::string attribute_handle); |
| 142 void SimulateGattCharacteristicValue(GattCharacteristic* characteristic, | 169 void SimulateGattCharacteristicValue(GattCharacteristic* characteristic, |
| 143 const std::vector<uint8_t>& value); | 170 const std::vector<uint8_t>& value); |
| 171 void SimulateCharacteristicValueChangeNotification( |
| 172 GattService* parent_service, |
| 173 GattCharacteristic* characteristic); |
| 144 void SimulateGattCharacteristicReadError(GattCharacteristic* characteristic, | 174 void SimulateGattCharacteristicReadError(GattCharacteristic* characteristic, |
| 145 HRESULT error); | 175 HRESULT error); |
| 146 void SimulateGattCharacteristicWriteError(GattCharacteristic* characteristic, | 176 void SimulateGattCharacteristicWriteError(GattCharacteristic* characteristic, |
| 147 HRESULT error); | 177 HRESULT error); |
| 178 void SimulateGattCharacteristicSetNotifyError( |
| 179 GattCharacteristic* characteristic, |
| 180 HRESULT error); |
| 148 void SimulateGattDescriptor(std::string device_address, | 181 void SimulateGattDescriptor(std::string device_address, |
| 149 GattCharacteristic* characteristic, | 182 GattCharacteristic* characteristic, |
| 150 const BTH_LE_UUID& uuid); | 183 const BTH_LE_UUID& uuid); |
| 184 GattDescriptor* GetSimulatedDescriptor( |
| 185 GattCharacteristic* parent_characteristic, |
| 186 std::string attribute_handle); |
| 187 void SimulateGattDescriptorWrite(GattDescriptor* descriptor, |
| 188 const std::vector<uint8_t>& value); |
| 189 void SimulateGattDescriptorWriteError(GattDescriptor* descriptor, |
| 190 HRESULT error); |
| 151 void AddObserver(Observer* observer); | 191 void AddObserver(Observer* observer); |
| 152 | 192 |
| 153 private: | 193 private: |
| 154 // Get simulated characteristic by |service_path| and |characteristic| info. | 194 // Get simulated characteristic by |service_path| and |characteristic| info. |
| 155 GattCharacteristic* GetSimulatedGattCharacteristic( | 195 GattCharacteristic* GetSimulatedGattCharacteristic( |
| 156 base::FilePath& service_path, | 196 base::FilePath& service_path, |
| 157 const PBTH_LE_GATT_CHARACTERISTIC characteristic); | 197 const PBTH_LE_GATT_CHARACTERISTIC characteristic); |
| 158 | 198 |
| 159 // Generate an unique attribute handle on |device_address|. | 199 // Generate an unique attribute handle on |device_address|. |
| 160 USHORT GenerateAUniqueAttributeHandle(std::string device_address); | 200 USHORT GenerateAUniqueAttributeHandle(std::string device_address); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 175 | 215 |
| 176 // Extract service attribute handles from the |path| generated by | 216 // Extract service attribute handles from the |path| generated by |
| 177 // GenerateGattServiceDevicePath. | 217 // GenerateGattServiceDevicePath. |
| 178 std::vector<std::string> ExtractServiceAttributeHandlesFromDevicePath( | 218 std::vector<std::string> ExtractServiceAttributeHandlesFromDevicePath( |
| 179 base::string16 path); | 219 base::string16 path); |
| 180 | 220 |
| 181 // The canonical BLE device address string format is the | 221 // The canonical BLE device address string format is the |
| 182 // BluetoothDevice::CanonicalizeAddress. | 222 // BluetoothDevice::CanonicalizeAddress. |
| 183 std::string BluetoothAddressToCanonicalString(const BLUETOOTH_ADDRESS& btha); | 223 std::string BluetoothAddressToCanonicalString(const BLUETOOTH_ADDRESS& btha); |
| 184 | 224 |
| 225 // Get client |characteristic| configuration descriptor which has standard |
| 226 // UUID = 00002902-0000-1000-8000-00805f9b34fb. |
| 227 GattDescriptor* GetCCCDescriptor(GattCharacteristic* characteristic); |
| 228 |
| 185 // Table to store allocated attribute handle for a device. | 229 // Table to store allocated attribute handle for a device. |
| 186 BLEAttributeHandleTable attribute_handle_table_; | 230 BLEAttributeHandleTable attribute_handle_table_; |
| 187 BLEDevicesMap simulated_devices_; | 231 BLEDevicesMap simulated_devices_; |
| 188 Observer* observer_; | 232 Observer* observer_; |
| 233 GattServiceObserverTable gatt_service_observers_; |
| 189 }; | 234 }; |
| 190 | 235 |
| 191 } // namespace win | 236 } // namespace win |
| 192 } // namespace device | 237 } // namespace device |
| 193 | 238 |
| 194 #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ | 239 #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ |
| OLD | NEW |