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.
5 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "chromeos/dbus/bluetooth_gatt_descriptor_client.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h"
13 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h"
19 // Stream operator for logging vector<uint8>.
20 std::ostream
& operator<<(std::ostream
& out
, const std::vector
<uint8
> bytes
) {
22 for (std::vector
<uint8
>::const_iterator iter
= bytes
.begin();
23 iter
!= bytes
.end(); ++iter
) {
24 out
<< base::StringPrintf("%02X", *iter
);
31 BluetoothRemoteGattDescriptorChromeOS::BluetoothRemoteGattDescriptorChromeOS(
32 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
,
33 const dbus::ObjectPath
& object_path
)
34 : object_path_(object_path
),
35 characteristic_(characteristic
),
36 weak_ptr_factory_(this) {
37 VLOG(1) << "Creating remote GATT descriptor with identifier: "
38 << GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
41 BluetoothRemoteGattDescriptorChromeOS::
42 ~BluetoothRemoteGattDescriptorChromeOS() {
45 std::string
BluetoothRemoteGattDescriptorChromeOS::GetIdentifier() const {
46 return object_path_
.value();
49 device::BluetoothUUID
BluetoothRemoteGattDescriptorChromeOS::GetUUID() const {
50 BluetoothGattDescriptorClient::Properties
* properties
=
51 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->
52 GetProperties(object_path_
);
54 return device::BluetoothUUID(properties
->uuid
.value());
57 bool BluetoothRemoteGattDescriptorChromeOS::IsLocal() const {
61 const std::vector
<uint8
>&
62 BluetoothRemoteGattDescriptorChromeOS::GetValue() const {
63 BluetoothGattDescriptorClient::Properties
* properties
=
64 DBusThreadManager::Get()
65 ->GetBluetoothGattDescriptorClient()
66 ->GetProperties(object_path_
);
70 return properties
->value
.value();
73 device::BluetoothGattCharacteristic
*
74 BluetoothRemoteGattDescriptorChromeOS::GetCharacteristic() const {
75 return characteristic_
;
78 device::BluetoothGattCharacteristic::Permissions
79 BluetoothRemoteGattDescriptorChromeOS::GetPermissions() const {
80 // TODO(armansito): Once BlueZ defines the permissions, return the correct
82 return device::BluetoothGattCharacteristic::PERMISSION_NONE
;
85 void BluetoothRemoteGattDescriptorChromeOS::ReadRemoteDescriptor(
86 const ValueCallback
& callback
,
87 const ErrorCallback
& error_callback
) {
88 VLOG(1) << "Sending GATT characteristic descriptor read request to "
89 << "descriptor: " << GetIdentifier() << ", UUID: "
90 << GetUUID().canonical_value();
92 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->ReadValue(
93 object_path_
, callback
,
94 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError
,
95 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
98 void BluetoothRemoteGattDescriptorChromeOS::WriteRemoteDescriptor(
99 const std::vector
<uint8
>& new_value
,
100 const base::Closure
& callback
,
101 const ErrorCallback
& error_callback
) {
102 VLOG(1) << "Sending GATT characteristic descriptor write request to "
103 << "characteristic: " << GetIdentifier() << ", UUID: "
104 << GetUUID().canonical_value() << ", with value: "
107 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->WriteValue(
111 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError
,
112 weak_ptr_factory_
.GetWeakPtr(),
116 void BluetoothRemoteGattDescriptorChromeOS::OnError(
117 const ErrorCallback
& error_callback
,
118 const std::string
& error_name
,
119 const std::string
& error_message
) {
120 VLOG(1) << "Operation failed: " << error_name
121 << ", message: " << error_message
;
124 BluetoothRemoteGattServiceChromeOS::DBusErrorToServiceError(error_name
));
127 } // namespace chromeos