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 {
66 device::BluetoothGattCharacteristic
*
67 BluetoothRemoteGattDescriptorChromeOS::GetCharacteristic() const {
68 return characteristic_
;
71 device::BluetoothGattCharacteristic::Permissions
72 BluetoothRemoteGattDescriptorChromeOS::GetPermissions() const {
73 // TODO(armansito): Once BlueZ defines the permissions, return the correct
75 return device::BluetoothGattCharacteristic::kPermissionNone
;
78 void BluetoothRemoteGattDescriptorChromeOS::ReadRemoteDescriptor(
79 const ValueCallback
& callback
,
80 const ErrorCallback
& error_callback
) {
81 VLOG(1) << "Sending GATT characteristic descriptor read request to "
82 << "descriptor: " << GetIdentifier() << ", UUID: "
83 << GetUUID().canonical_value();
85 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->ReadValue(
87 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnValueSuccess
,
88 weak_ptr_factory_
.GetWeakPtr(),
90 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError
,
91 weak_ptr_factory_
.GetWeakPtr(),
95 void BluetoothRemoteGattDescriptorChromeOS::WriteRemoteDescriptor(
96 const std::vector
<uint8
>& new_value
,
97 const base::Closure
& callback
,
98 const ErrorCallback
& error_callback
) {
99 VLOG(1) << "Sending GATT characteristic descriptor write request to "
100 << "characteristic: " << GetIdentifier() << ", UUID: "
101 << GetUUID().canonical_value() << ", with value: "
104 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->WriteValue(
108 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError
,
109 weak_ptr_factory_
.GetWeakPtr(),
113 void BluetoothRemoteGattDescriptorChromeOS::OnValueSuccess(
114 const ValueCallback
& callback
,
115 const std::vector
<uint8
>& value
) {
116 VLOG(1) << "Descriptor value read: " << value
;
117 cached_value_
= value
;
119 DCHECK(characteristic_
);
120 BluetoothRemoteGattServiceChromeOS
* service
=
121 static_cast<BluetoothRemoteGattServiceChromeOS
*>(
122 characteristic_
->GetService());
124 service
->NotifyDescriptorValueChanged(characteristic_
, this, value
);
128 void BluetoothRemoteGattDescriptorChromeOS::OnError(
129 const ErrorCallback
& error_callback
,
130 const std::string
& error_name
,
131 const std::string
& error_message
) {
132 VLOG(1) << "Operation failed: " << error_name
133 << ", message: " << error_message
;
134 error_callback
.Run();
137 } // namespace chromeos