bluetooth_remote_gatt_service_winrt.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2018 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_REMOTE_GATT_SERVICE_WINRT_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_WINRT_H_
  6. #include <windows.devices.bluetooth.genericattributeprofile.h>
  7. #include <wrl/client.h>
  8. #include <stdint.h>
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. #include "base/logging.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "device/bluetooth/bluetooth_export.h"
  15. #include "device/bluetooth/bluetooth_remote_gatt_characteristic_winrt.h"
  16. #include "device/bluetooth/bluetooth_remote_gatt_service.h"
  17. #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
  18. namespace device {
  19. class BluetoothDevice;
  20. class BluetoothGattDiscovererWinrt;
  21. class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattServiceWinrt
  22. : public BluetoothRemoteGattService {
  23. public:
  24. static std::unique_ptr<BluetoothRemoteGattServiceWinrt> Create(
  25. BluetoothDevice* device,
  26. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  27. GenericAttributeProfile::IGattDeviceService>
  28. gatt_service);
  29. BluetoothRemoteGattServiceWinrt(const BluetoothRemoteGattServiceWinrt&) =
  30. delete;
  31. BluetoothRemoteGattServiceWinrt& operator=(
  32. const BluetoothRemoteGattServiceWinrt&) = delete;
  33. ~BluetoothRemoteGattServiceWinrt() override;
  34. // BluetoothRemoteGattService:
  35. std::string GetIdentifier() const override;
  36. BluetoothUUID GetUUID() const override;
  37. bool IsPrimary() const override;
  38. BluetoothDevice* GetDevice() const override;
  39. std::vector<BluetoothRemoteGattService*> GetIncludedServices() const override;
  40. void UpdateCharacteristics(BluetoothGattDiscovererWinrt* gatt_discoverer);
  41. ABI::Windows::Devices::Bluetooth::GenericAttributeProfile::IGattDeviceService*
  42. GetDeviceServiceForTesting();
  43. template <typename Interface>
  44. static GattErrorCode GetGattErrorCode(Interface* i) {
  45. Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IReference<uint8_t>>
  46. protocol_error_ref;
  47. HRESULT hr = i->get_ProtocolError(&protocol_error_ref);
  48. if (FAILED(hr)) {
  49. DVLOG(2) << "Getting Protocol Error Reference failed: "
  50. << logging::SystemErrorCodeToString(hr);
  51. return GattErrorCode::GATT_ERROR_UNKNOWN;
  52. }
  53. if (!protocol_error_ref) {
  54. DVLOG(2) << "Got Null Protocol Error Reference.";
  55. return GattErrorCode::GATT_ERROR_UNKNOWN;
  56. }
  57. uint8_t protocol_error;
  58. hr = protocol_error_ref->get_Value(&protocol_error);
  59. if (FAILED(hr)) {
  60. DVLOG(2) << "Getting Protocol Error Value failed: "
  61. << logging::SystemErrorCodeToString(hr);
  62. return GattErrorCode::GATT_ERROR_UNKNOWN;
  63. }
  64. DVLOG(2) << "Got Protocol Error: " << static_cast<int>(protocol_error);
  65. // GATT Protocol Errors are described in the Bluetooth Core Specification
  66. // Version 5.0 Vol 3, Part F, 3.4.1.1.
  67. switch (protocol_error) {
  68. case 0x01: // Invalid Handle
  69. return GATT_ERROR_FAILED;
  70. case 0x02: // Read Not Permitted
  71. return GATT_ERROR_NOT_PERMITTED;
  72. case 0x03: // Write Not Permitted
  73. return GATT_ERROR_NOT_PERMITTED;
  74. case 0x04: // Invalid PDU
  75. return GATT_ERROR_FAILED;
  76. case 0x05: // Insufficient Authentication
  77. return GATT_ERROR_NOT_AUTHORIZED;
  78. case 0x06: // Request Not Supported
  79. return GATT_ERROR_NOT_SUPPORTED;
  80. case 0x07: // Invalid Offset
  81. return GATT_ERROR_INVALID_LENGTH;
  82. case 0x08: // Insufficient Authorization
  83. return GATT_ERROR_NOT_AUTHORIZED;
  84. case 0x09: // Prepare Queue Full
  85. return GATT_ERROR_IN_PROGRESS;
  86. case 0x0A: // Attribute Not Found
  87. return GATT_ERROR_FAILED;
  88. case 0x0B: // Attribute Not Long
  89. return GATT_ERROR_FAILED;
  90. case 0x0C: // Insufficient Encryption Key Size
  91. return GATT_ERROR_FAILED;
  92. case 0x0D: // Invalid Attribute Value Length
  93. return GATT_ERROR_INVALID_LENGTH;
  94. case 0x0E: // Unlikely Error
  95. return GATT_ERROR_FAILED;
  96. case 0x0F: // Insufficient Encryption
  97. return GATT_ERROR_NOT_PAIRED;
  98. case 0x10: // Unsupported Group Type
  99. return GATT_ERROR_FAILED;
  100. case 0x11: // Insufficient Resources
  101. return GATT_ERROR_FAILED;
  102. default:
  103. return GATT_ERROR_UNKNOWN;
  104. }
  105. }
  106. static uint8_t ToProtocolError(GattErrorCode error_code);
  107. private:
  108. BluetoothRemoteGattServiceWinrt(
  109. BluetoothDevice* device,
  110. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  111. GenericAttributeProfile::IGattDeviceService>
  112. gatt_service,
  113. BluetoothUUID uuid,
  114. uint16_t attribute_handle);
  115. raw_ptr<BluetoothDevice> device_;
  116. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::
  117. GenericAttributeProfile::IGattDeviceService>
  118. gatt_service_;
  119. BluetoothUUID uuid_;
  120. uint16_t attribute_handle_;
  121. std::string identifier_;
  122. };
  123. } // namespace device
  124. #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_WINRT_H_