| 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 GattCharacteristic; | 18 struct GattCharacteristic; |
| 19 struct GattCharacteristicObserver; |
| 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 GattCharacteristicObserverTable is GattCharacteristicObserver |
| 36 // pointer. |
| 37 // Note: The underlying data type of BLUETOOTH_GATT_EVENT_HANDLE is PVOID. |
| 38 typedef std::unordered_map<BLUETOOTH_GATT_EVENT_HANDLE, |
| 39 scoped_ptr<GattCharacteristicObserver>> |
| 40 GattCharacteristicObserverTable; |
| 34 | 41 |
| 35 struct BLEDevice { | 42 struct BLEDevice { |
| 36 BLEDevice(); | 43 BLEDevice(); |
| 37 ~BLEDevice(); | 44 ~BLEDevice(); |
| 38 scoped_ptr<BluetoothLowEnergyDeviceInfo> device_info; | 45 scoped_ptr<BluetoothLowEnergyDeviceInfo> device_info; |
| 39 GattServicesMap primary_services; | 46 GattServicesMap primary_services; |
| 47 bool marked_as_deleted; |
| 40 }; | 48 }; |
| 41 | 49 |
| 42 struct GattService { | 50 struct GattService { |
| 43 GattService(); | 51 GattService(); |
| 44 ~GattService(); | 52 ~GattService(); |
| 45 scoped_ptr<BTH_LE_GATT_SERVICE> service_info; | 53 scoped_ptr<BTH_LE_GATT_SERVICE> service_info; |
| 46 GattServicesMap included_services; | 54 GattServicesMap included_services; |
| 47 GattCharacteristicsMap included_characteristics; | 55 GattCharacteristicsMap included_characteristics; |
| 48 }; | 56 }; |
| 49 | 57 |
| 50 struct GattCharacteristic { | 58 struct GattCharacteristic { |
| 51 GattCharacteristic(); | 59 GattCharacteristic(); |
| 52 ~GattCharacteristic(); | 60 ~GattCharacteristic(); |
| 53 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristic_info; | 61 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristic_info; |
| 54 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value; | 62 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value; |
| 55 GattDescriptorsMap included_descriptors; | 63 GattDescriptorsMap included_descriptors; |
| 56 std::vector<HRESULT> read_errors; | 64 std::vector<HRESULT> read_errors; |
| 57 std::vector<HRESULT> write_errors; | 65 std::vector<HRESULT> write_errors; |
| 66 std::vector<BLUETOOTH_GATT_EVENT_HANDLE> observers; |
| 58 }; | 67 }; |
| 59 | 68 |
| 60 struct GattDescriptor { | 69 struct GattDescriptor { |
| 61 GattDescriptor(); | 70 GattDescriptor(); |
| 62 ~GattDescriptor(); | 71 ~GattDescriptor(); |
| 63 scoped_ptr<BTH_LE_GATT_DESCRIPTOR> descriptor_info; | 72 scoped_ptr<BTH_LE_GATT_DESCRIPTOR> descriptor_info; |
| 64 scoped_ptr<BTH_LE_GATT_DESCRIPTOR_VALUE> value; | 73 scoped_ptr<BTH_LE_GATT_DESCRIPTOR_VALUE> value; |
| 65 }; | 74 }; |
| 66 | 75 |
| 76 struct GattCharacteristicObserver { |
| 77 GattCharacteristicObserver(); |
| 78 ~GattCharacteristicObserver(); |
| 79 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback; |
| 80 PVOID context; |
| 81 }; |
| 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 OnStartCharacteristicNotification() = 0; |
| 95 virtual void OnWriteGattDescriptorValue( |
| 96 const std::vector<uint8_t>& value) = 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 Loading... |
| 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; |
| 139 HRESULT WriteDescriptorValue( |
| 140 base::FilePath& service_path, |
| 141 const PBTH_LE_GATT_DESCRIPTOR descriptor, |
| 142 PBTH_LE_GATT_DESCRIPTOR_VALUE new_value) override; |
| 111 | 143 |
| 112 BLEDevice* SimulateBLEDevice(std::string device_name, | 144 BLEDevice* SimulateBLEDevice(std::string device_name, |
| 113 BLUETOOTH_ADDRESS device_address); | 145 BLUETOOTH_ADDRESS device_address); |
| 114 BLEDevice* GetSimulatedBLEDevice(std::string device_address); | 146 BLEDevice* GetSimulatedBLEDevice(std::string device_address); |
| 115 void RemoveSimulatedBLEDevice(std::string device_address); | 147 void RemoveSimulatedBLEDevice(std::string device_address); |
| 116 | 148 |
| 117 // Note: |parent_service| may be nullptr to indicate a primary service. | 149 // Note: |parent_service| may be nullptr to indicate a primary service. |
| 118 GattService* SimulateGattService(BLEDevice* device, | 150 GattService* SimulateGattService(BLEDevice* device, |
| 119 GattService* parent_service, | 151 GattService* parent_service, |
| 120 const BTH_LE_UUID& uuid); | 152 const BTH_LE_UUID& uuid); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 134 std::string device_address, | 166 std::string device_address, |
| 135 GattService* parent_service, | 167 GattService* parent_service, |
| 136 const BTH_LE_GATT_CHARACTERISTIC& characteristic); | 168 const BTH_LE_GATT_CHARACTERISTIC& characteristic); |
| 137 void SimulateGattCharacteriscRemove(GattService* parent_service, | 169 void SimulateGattCharacteriscRemove(GattService* parent_service, |
| 138 std::string attribute_handle); | 170 std::string attribute_handle); |
| 139 GattCharacteristic* GetSimulatedGattCharacteristic( | 171 GattCharacteristic* GetSimulatedGattCharacteristic( |
| 140 GattService* parent_service, | 172 GattService* parent_service, |
| 141 std::string attribute_handle); | 173 std::string attribute_handle); |
| 142 void SimulateGattCharacteristicValue(GattCharacteristic* characteristic, | 174 void SimulateGattCharacteristicValue(GattCharacteristic* characteristic, |
| 143 const std::vector<uint8_t>& value); | 175 const std::vector<uint8_t>& value); |
| 176 void SimulateCharacteristicValueChangeNotification( |
| 177 GattCharacteristic* characteristic); |
| 144 void SimulateGattCharacteristicReadError(GattCharacteristic* characteristic, | 178 void SimulateGattCharacteristicReadError(GattCharacteristic* characteristic, |
| 145 HRESULT error); | 179 HRESULT error); |
| 146 void SimulateGattCharacteristicWriteError(GattCharacteristic* characteristic, | 180 void SimulateGattCharacteristicWriteError(GattCharacteristic* characteristic, |
| 147 HRESULT error); | 181 HRESULT error); |
| 182 void RememberCharacteristicForSubsequentAction(GattService* parent_service, |
| 183 std::string attribute_handle); |
| 148 void SimulateGattDescriptor(std::string device_address, | 184 void SimulateGattDescriptor(std::string device_address, |
| 149 GattCharacteristic* characteristic, | 185 GattCharacteristic* characteristic, |
| 150 const BTH_LE_UUID& uuid); | 186 const BTH_LE_UUID& uuid); |
| 151 void AddObserver(Observer* observer); | 187 void AddObserver(Observer* observer); |
| 152 | 188 |
| 153 private: | 189 private: |
| 154 // Get simulated characteristic by |service_path| and |characteristic| info. | 190 // Get simulated characteristic by |service_path| and |characteristic| info. |
| 155 GattCharacteristic* GetSimulatedGattCharacteristic( | 191 GattCharacteristic* GetSimulatedGattCharacteristic( |
| 156 base::FilePath& service_path, | 192 base::FilePath& service_path, |
| 157 const PBTH_LE_GATT_CHARACTERISTIC characteristic); | 193 const PBTH_LE_GATT_CHARACTERISTIC characteristic); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 179 base::string16 path); | 215 base::string16 path); |
| 180 | 216 |
| 181 // The canonical BLE device address string format is the | 217 // The canonical BLE device address string format is the |
| 182 // BluetoothDevice::CanonicalizeAddress. | 218 // BluetoothDevice::CanonicalizeAddress. |
| 183 std::string BluetoothAddressToCanonicalString(const BLUETOOTH_ADDRESS& btha); | 219 std::string BluetoothAddressToCanonicalString(const BLUETOOTH_ADDRESS& btha); |
| 184 | 220 |
| 185 // Table to store allocated attribute handle for a device. | 221 // Table to store allocated attribute handle for a device. |
| 186 BLEAttributeHandleTable attribute_handle_table_; | 222 BLEAttributeHandleTable attribute_handle_table_; |
| 187 BLEDevicesMap simulated_devices_; | 223 BLEDevicesMap simulated_devices_; |
| 188 Observer* observer_; | 224 Observer* observer_; |
| 225 GattCharacteristicObserverTable gatt_characteristic_observers_; |
| 226 GattCharacteristic* remembered_characteristic_; |
| 189 }; | 227 }; |
| 190 | 228 |
| 191 } // namespace win | 229 } // namespace win |
| 192 } // namespace device | 230 } // namespace device |
| 193 | 231 |
| 194 #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ | 232 #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ |
| OLD | NEW |