bluetooth_low_energy_win_fake.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_
  6. #include <memory>
  7. #include <set>
  8. #include <unordered_map>
  9. #include "base/memory/raw_ptr.h"
  10. #include "device/bluetooth/bluetooth_low_energy_win.h"
  11. namespace device {
  12. namespace win {
  13. struct BLEDevice;
  14. struct GattService;
  15. struct GattCharacteristic;
  16. struct GattCharacteristicObserver;
  17. struct GattDescriptor;
  18. // The key of BLEDevicesMap is the string of the BLE device address.
  19. typedef std::unordered_map<std::string, std::unique_ptr<BLEDevice>>
  20. BLEDevicesMap;
  21. // The key of GattServicesMap, GattCharacteristicsMap and GattDescriptorsMap is
  22. // the string of the attribute handle.
  23. typedef std::unordered_map<std::string, std::unique_ptr<GattService>>
  24. GattServicesMap;
  25. typedef std::unordered_map<std::string, std::unique_ptr<GattCharacteristic>>
  26. GattCharacteristicsMap;
  27. typedef std::unordered_map<std::string, std::unique_ptr<GattDescriptor>>
  28. GattDescriptorsMap;
  29. // The key of BLEAttributeHandleTable is the string of the BLE device address.
  30. typedef std::unordered_map<std::string, std::unique_ptr<std::set<USHORT>>>
  31. BLEAttributeHandleTable;
  32. // The key of GattCharacteristicObserverTable is GattCharacteristicObserver
  33. // pointer.
  34. // Note: The underlying data type of BLUETOOTH_GATT_EVENT_HANDLE is PVOID.
  35. typedef std::unordered_map<BLUETOOTH_GATT_EVENT_HANDLE,
  36. std::unique_ptr<GattCharacteristicObserver>>
  37. GattCharacteristicObserverTable;
  38. struct BLEDevice {
  39. BLEDevice();
  40. ~BLEDevice();
  41. std::unique_ptr<BluetoothLowEnergyDeviceInfo> device_info;
  42. GattServicesMap primary_services;
  43. bool marked_as_deleted;
  44. };
  45. struct GattService {
  46. GattService();
  47. ~GattService();
  48. std::unique_ptr<BTH_LE_GATT_SERVICE> service_info;
  49. GattServicesMap included_services;
  50. GattCharacteristicsMap included_characteristics;
  51. };
  52. struct GattCharacteristic {
  53. GattCharacteristic();
  54. ~GattCharacteristic();
  55. std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC> characteristic_info;
  56. std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value;
  57. GattDescriptorsMap included_descriptors;
  58. std::vector<HRESULT> read_errors;
  59. std::vector<HRESULT> write_errors;
  60. std::vector<HRESULT> notify_errors;
  61. std::vector<BLUETOOTH_GATT_EVENT_HANDLE> observers;
  62. };
  63. struct GattDescriptor {
  64. GattDescriptor();
  65. ~GattDescriptor();
  66. std::unique_ptr<BTH_LE_GATT_DESCRIPTOR> descriptor_info;
  67. std::unique_ptr<BTH_LE_GATT_DESCRIPTOR_VALUE> value;
  68. };
  69. struct GattCharacteristicObserver {
  70. GattCharacteristicObserver();
  71. ~GattCharacteristicObserver();
  72. PFNBLUETOOTH_GATT_EVENT_CALLBACK_CORRECTED callback;
  73. PVOID context;
  74. };
  75. // Fake implementation of BluetoothLowEnergyWrapper. Used for BluetoothTestWin.
  76. class BluetoothLowEnergyWrapperFake : public BluetoothLowEnergyWrapper {
  77. public:
  78. class Observer {
  79. public:
  80. Observer() {}
  81. ~Observer() {}
  82. virtual void OnReadGattCharacteristicValue() = 0;
  83. virtual void OnWriteGattCharacteristicValue(
  84. const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) = 0;
  85. virtual void OnStartCharacteristicNotification() = 0;
  86. virtual void OnWriteGattDescriptorValue(
  87. const std::vector<uint8_t>& value) = 0;
  88. };
  89. BluetoothLowEnergyWrapperFake();
  90. ~BluetoothLowEnergyWrapperFake() override;
  91. bool IsBluetoothLowEnergySupported() override;
  92. bool EnumerateKnownBluetoothLowEnergyDevices(
  93. std::vector<std::unique_ptr<BluetoothLowEnergyDeviceInfo>>* devices,
  94. std::string* error) override;
  95. bool EnumerateKnownBluetoothLowEnergyGattServiceDevices(
  96. std::vector<std::unique_ptr<BluetoothLowEnergyDeviceInfo>>* devices,
  97. std::string* error) override;
  98. bool EnumerateKnownBluetoothLowEnergyServices(
  99. const base::FilePath& device_path,
  100. std::vector<std::unique_ptr<BluetoothLowEnergyServiceInfo>>* services,
  101. std::string* error) override;
  102. HRESULT ReadCharacteristicsOfAService(
  103. base::FilePath& service_path,
  104. const PBTH_LE_GATT_SERVICE service,
  105. std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics,
  106. USHORT* out_counts) override;
  107. HRESULT ReadDescriptorsOfACharacteristic(
  108. base::FilePath& service_path,
  109. const PBTH_LE_GATT_CHARACTERISTIC characteristic,
  110. std::unique_ptr<BTH_LE_GATT_DESCRIPTOR>* out_included_descriptors,
  111. USHORT* out_counts) override;
  112. HRESULT ReadCharacteristicValue(
  113. base::FilePath& service_path,
  114. const PBTH_LE_GATT_CHARACTERISTIC characteristic,
  115. std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value) override;
  116. HRESULT WriteCharacteristicValue(
  117. base::FilePath& service_path,
  118. const PBTH_LE_GATT_CHARACTERISTIC characteristic,
  119. PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value,
  120. ULONG flags) override;
  121. HRESULT RegisterGattEvents(
  122. base::FilePath& service_path,
  123. BTH_LE_GATT_EVENT_TYPE type,
  124. PVOID event_parameter,
  125. PFNBLUETOOTH_GATT_EVENT_CALLBACK_CORRECTED callback,
  126. PVOID context,
  127. BLUETOOTH_GATT_EVENT_HANDLE* out_handle) override;
  128. HRESULT UnregisterGattEvent(
  129. BLUETOOTH_GATT_EVENT_HANDLE event_handle) override;
  130. HRESULT WriteDescriptorValue(
  131. base::FilePath& service_path,
  132. const PBTH_LE_GATT_DESCRIPTOR descriptor,
  133. PBTH_LE_GATT_DESCRIPTOR_VALUE new_value) override;
  134. BLEDevice* SimulateBLEDevice(std::string device_name,
  135. BLUETOOTH_ADDRESS device_address);
  136. BLEDevice* GetSimulatedBLEDevice(std::string device_address);
  137. void RemoveSimulatedBLEDevice(std::string device_address);
  138. // Note: |parent_service| may be nullptr to indicate a primary service.
  139. GattService* SimulateGattService(BLEDevice* device,
  140. GattService* parent_service,
  141. const BTH_LE_UUID& uuid);
  142. // Note: |parent_service| may be nullptr to indicate a primary service.
  143. void SimulateGattServiceRemoved(BLEDevice* device,
  144. GattService* parent_service,
  145. std::string attribute_handle);
  146. // Note: |chain_of_att_handle| contains the attribute handles of the services
  147. // in order from primary service to target service. The last item in
  148. // |chain_of_att_handle| is the target service's attribute handle.
  149. GattService* GetSimulatedGattService(
  150. BLEDevice* device,
  151. const std::vector<std::string>& chain_of_att_handle);
  152. GattCharacteristic* SimulateGattCharacterisc(
  153. std::string device_address,
  154. GattService* parent_service,
  155. const BTH_LE_GATT_CHARACTERISTIC& characteristic);
  156. void SimulateGattCharacteriscRemove(GattService* parent_service,
  157. std::string attribute_handle);
  158. GattCharacteristic* GetSimulatedGattCharacteristic(
  159. GattService* parent_service,
  160. std::string attribute_handle);
  161. void SimulateGattCharacteristicValue(GattCharacteristic* characteristic,
  162. const std::vector<uint8_t>& value);
  163. void SimulateCharacteristicValueChangeNotification(
  164. GattCharacteristic* characteristic);
  165. void SimulateGattCharacteristicSetNotifyError(
  166. GattCharacteristic* characteristic,
  167. HRESULT error);
  168. void SimulateGattCharacteristicReadError(GattCharacteristic* characteristic,
  169. HRESULT error);
  170. void SimulateGattCharacteristicWriteError(GattCharacteristic* characteristic,
  171. HRESULT error);
  172. void RememberCharacteristicForSubsequentAction(GattService* parent_service,
  173. std::string attribute_handle);
  174. void SimulateGattDescriptor(std::string device_address,
  175. GattCharacteristic* characteristic,
  176. const BTH_LE_UUID& uuid);
  177. void AddObserver(Observer* observer);
  178. private:
  179. // Get simulated characteristic by |service_path| and |characteristic| info.
  180. GattCharacteristic* GetSimulatedGattCharacteristic(
  181. base::FilePath& service_path,
  182. const PBTH_LE_GATT_CHARACTERISTIC characteristic);
  183. // Generate an unique attribute handle on |device_address|.
  184. USHORT GenerateAUniqueAttributeHandle(std::string device_address);
  185. // Generate device path for the BLE device with |device_address|.
  186. std::wstring GenerateBLEDevicePath(std::string device_address);
  187. // Generate GATT service device path of the service with
  188. // |service_attribute_handle|. |resident_device_path| is the BLE device this
  189. // GATT service belongs to.
  190. std::wstring GenerateGattServiceDevicePath(std::wstring resident_device_path,
  191. USHORT service_attribute_handle);
  192. // Extract device address from the device |path| generated by
  193. // GenerateBLEDevicePath or GenerateGattServiceDevicePath.
  194. std::wstring ExtractDeviceAddressFromDevicePath(std::wstring path);
  195. // Extract service attribute handles from the |path| generated by
  196. // GenerateGattServiceDevicePath.
  197. std::vector<std::string> ExtractServiceAttributeHandlesFromDevicePath(
  198. std::wstring path);
  199. // The canonical BLE device address string format is the
  200. // BluetoothDevice::CanonicalizeAddress.
  201. std::string BluetoothAddressToCanonicalString(const BLUETOOTH_ADDRESS& btha);
  202. // Table to store allocated attribute handle for a device.
  203. BLEAttributeHandleTable attribute_handle_table_;
  204. BLEDevicesMap simulated_devices_;
  205. raw_ptr<Observer> observer_;
  206. GattCharacteristicObserverTable gatt_characteristic_observers_;
  207. raw_ptr<GattCharacteristic> remembered_characteristic_;
  208. };
  209. } // namespace win
  210. } // namespace device
  211. #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_