bluetooth_low_energy_win.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2014 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_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/files/file_path.h"
  11. #include "device/bluetooth/bluetooth_export.h"
  12. #include "device/bluetooth/bluetooth_low_energy_defs_win.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace device {
  15. namespace win {
  16. //
  17. // Callback function signature for Bluetooth GATT events. This fixes a bug in
  18. // this typedef in the Windows 10.0.10586 SDK which is missing the CALLBACK
  19. // modifier. This corrected typedef should be used throughout Chromium except
  20. // when casting to the 'official' definition when calling Microsoft functions.
  21. // This allows Chromium to build with 10.0.14393 or later SDKs (which have the
  22. // fixed typedef) while doing the correct thing even when built with 10.0.10586.
  23. // The CALLBACK modifier affects how function parameters are cleaned up from the
  24. // stack and having a mismatch can lead to misalignment of the stack pointer.
  25. //
  26. typedef VOID(CALLBACK* PFNBLUETOOTH_GATT_EVENT_CALLBACK_CORRECTED)(
  27. _In_ BTH_LE_GATT_EVENT_TYPE EventType,
  28. _In_ PVOID EventOutParameter,
  29. _In_opt_ PVOID Context);
  30. // Represents a device registry property value
  31. class DEVICE_BLUETOOTH_EXPORT DeviceRegistryPropertyValue {
  32. public:
  33. // Creates a property value instance, where |property_type| is one of REG_xxx
  34. // registry value type (e.g. REG_SZ, REG_DWORD), |value| is a byte array
  35. // containing the property value and |value_size| is the number of bytes in
  36. // |value|. Note the returned instance takes ownership of the bytes in
  37. // |value|.
  38. static std::unique_ptr<DeviceRegistryPropertyValue> Create(
  39. DWORD property_type,
  40. std::unique_ptr<uint8_t[]> value,
  41. size_t value_size);
  42. DeviceRegistryPropertyValue(const DeviceRegistryPropertyValue&) = delete;
  43. DeviceRegistryPropertyValue& operator=(const DeviceRegistryPropertyValue&) =
  44. delete;
  45. ~DeviceRegistryPropertyValue();
  46. // Returns the vaue type a REG_xxx value (e.g. REG_SZ, REG_DWORD, ...)
  47. DWORD property_type() const { return property_type_; }
  48. std::string AsString() const;
  49. DWORD AsDWORD() const;
  50. private:
  51. DeviceRegistryPropertyValue(DWORD property_type,
  52. std::unique_ptr<uint8_t[]> value);
  53. DWORD property_type_;
  54. std::unique_ptr<uint8_t[]> value_;
  55. };
  56. // Represents the value associated to a DEVPROPKEY.
  57. class DEVICE_BLUETOOTH_EXPORT DevicePropertyValue {
  58. public:
  59. // Creates a property value instance, where |property_type| is one of
  60. // DEVPROP_TYPE_xxx value type , |value| is a byte array containing the
  61. // property value and |value_size| is the number of bytes in |value|. Note the
  62. // returned instance takes ownership of the bytes in |value|.
  63. DevicePropertyValue(DEVPROPTYPE property_type,
  64. std::unique_ptr<uint8_t[]> value,
  65. size_t value_size);
  66. DevicePropertyValue(const DevicePropertyValue&) = delete;
  67. DevicePropertyValue& operator=(const DevicePropertyValue&) = delete;
  68. ~DevicePropertyValue();
  69. DEVPROPTYPE property_type() const { return property_type_; }
  70. uint32_t AsUint32() const;
  71. private:
  72. DEVPROPTYPE property_type_;
  73. std::unique_ptr<uint8_t[]> value_;
  74. size_t value_size_;
  75. };
  76. struct DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyServiceInfo {
  77. BluetoothLowEnergyServiceInfo();
  78. ~BluetoothLowEnergyServiceInfo();
  79. BTH_LE_UUID uuid;
  80. // Attribute handle uniquely identifies this service on the device.
  81. USHORT attribute_handle = 0;
  82. };
  83. struct DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyDeviceInfo {
  84. BluetoothLowEnergyDeviceInfo();
  85. ~BluetoothLowEnergyDeviceInfo();
  86. base::FilePath path;
  87. std::string id;
  88. absl::optional<std::string> friendly_name;
  89. BLUETOOTH_ADDRESS address;
  90. bool visible;
  91. bool authenticated;
  92. bool connected;
  93. };
  94. bool DEVICE_BLUETOOTH_EXPORT
  95. ExtractBluetoothAddressFromDeviceInstanceIdForTesting(
  96. const std::string& instance_id,
  97. BLUETOOTH_ADDRESS* btha,
  98. std::string* error);
  99. // Wraps Windows APIs used to access Bluetooth Low Energy devices, providing an
  100. // interface that can be replaced with fakes in tests.
  101. class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyWrapper {
  102. public:
  103. BluetoothLowEnergyWrapper();
  104. virtual ~BluetoothLowEnergyWrapper();
  105. // Returns true only on Windows platforms supporting Bluetooth Low Energy.
  106. virtual bool IsBluetoothLowEnergySupported();
  107. // Enumerates the list of known (i.e. already paired) Bluetooth LE devices on
  108. // this machine. In case of error, returns false and sets |error| with an
  109. // error message describing the problem.
  110. // Note: This function returns an error if Bluetooth Low Energy is not
  111. // supported on this Windows platform.
  112. virtual bool EnumerateKnownBluetoothLowEnergyDevices(
  113. std::vector<std::unique_ptr<BluetoothLowEnergyDeviceInfo>>* devices,
  114. std::string* error);
  115. // Enumerates the list of known Bluetooth LE GATT service devices on this
  116. // machine (a Bluetooth LE device usually has more than one GATT
  117. // services that each of them has a device interface on the machine). In case
  118. // of error, returns false and sets |error| with an error message describing
  119. // the problem.
  120. // Note: This function returns an error if Bluetooth Low Energy is not
  121. // supported on this Windows platform.
  122. virtual bool EnumerateKnownBluetoothLowEnergyGattServiceDevices(
  123. std::vector<std::unique_ptr<BluetoothLowEnergyDeviceInfo>>* devices,
  124. std::string* error);
  125. // Enumerates the list of known (i.e. cached) GATT services for a given
  126. // Bluetooth LE device |device_path| into |services|. In case of error,
  127. // returns false and sets |error| with an error message describing the
  128. // problem.
  129. // Note: This function returns an error if Bluetooth Low Energy is not
  130. // supported on this Windows platform.
  131. virtual bool EnumerateKnownBluetoothLowEnergyServices(
  132. const base::FilePath& device_path,
  133. std::vector<std::unique_ptr<BluetoothLowEnergyServiceInfo>>* services,
  134. std::string* error);
  135. // Reads characteristics of |service| with service device path |service_path|.
  136. // The result will be stored in |*out_included_characteristics| and
  137. // |*out_counts|.
  138. virtual HRESULT ReadCharacteristicsOfAService(
  139. base::FilePath& service_path,
  140. const PBTH_LE_GATT_SERVICE service,
  141. std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics,
  142. USHORT* out_counts);
  143. // Reads included descriptors of |characteristic| in service with service
  144. // device path |service_path|. The result will be stored in
  145. // |*out_included_descriptors| and |*out_counts|.
  146. virtual HRESULT ReadDescriptorsOfACharacteristic(
  147. base::FilePath& service_path,
  148. const PBTH_LE_GATT_CHARACTERISTIC characteristic,
  149. std::unique_ptr<BTH_LE_GATT_DESCRIPTOR>* out_included_descriptors,
  150. USHORT* out_counts);
  151. // Reads |characteristic| value in service with service device path
  152. // |service_path|. The result will be stored in |*out_value|.
  153. virtual HRESULT ReadCharacteristicValue(
  154. base::FilePath& service_path,
  155. const PBTH_LE_GATT_CHARACTERISTIC characteristic,
  156. std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value);
  157. // Writes |characteristic| value in service with service device path
  158. // |service_path| to |*new_value|.
  159. virtual HRESULT WriteCharacteristicValue(
  160. base::FilePath& service_path,
  161. const PBTH_LE_GATT_CHARACTERISTIC characteristic,
  162. PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value,
  163. ULONG flags);
  164. // Register GATT events of |event_type| in the service with service device
  165. // path |service_path|. |event_parameter| is the event's parameter. |callback|
  166. // is the function to be invoked if the event happened. |context| is the input
  167. // parameter to be given back through |callback|. |*out_handle| stores the
  168. // unique handle in OS for this registration.
  169. virtual HRESULT RegisterGattEvents(
  170. base::FilePath& service_path,
  171. BTH_LE_GATT_EVENT_TYPE event_type,
  172. PVOID event_parameter,
  173. PFNBLUETOOTH_GATT_EVENT_CALLBACK_CORRECTED callback,
  174. PVOID context,
  175. BLUETOOTH_GATT_EVENT_HANDLE* out_handle);
  176. virtual HRESULT UnregisterGattEvent(BLUETOOTH_GATT_EVENT_HANDLE event_handle);
  177. // Writes |descriptor| value in service with service device path
  178. // |service_path| to |*new_value|.
  179. virtual HRESULT WriteDescriptorValue(base::FilePath& service_path,
  180. const PBTH_LE_GATT_DESCRIPTOR descriptor,
  181. PBTH_LE_GATT_DESCRIPTOR_VALUE new_value);
  182. };
  183. } // namespace win
  184. } // namespace device
  185. #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_H_