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