Roll WebRTC 8837:8852, Libjingle 8836:8851
[chromium-blink-merge.git] / device / bluetooth / bluetooth_remote_gatt_descriptor_chromeos.cc
blobda3856691bc0499e62b31dcc010afa3eb2cccefa
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"
7 #include "base/bind.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"
15 namespace chromeos {
17 namespace {
19 // Stream operator for logging vector<uint8>.
20 std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) {
21 out << "[";
22 for (std::vector<uint8>::const_iterator iter = bytes.begin();
23 iter != bytes.end(); ++iter) {
24 out << base::StringPrintf("%02X", *iter);
26 return out << "]";
29 } // namespace
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_);
53 DCHECK(properties);
54 return device::BluetoothUUID(properties->uuid.value());
57 bool BluetoothRemoteGattDescriptorChromeOS::IsLocal() const {
58 return false;
61 const std::vector<uint8>&
62 BluetoothRemoteGattDescriptorChromeOS::GetValue() const {
63 BluetoothGattDescriptorClient::Properties* properties =
64 DBusThreadManager::Get()
65 ->GetBluetoothGattDescriptorClient()
66 ->GetProperties(object_path_);
68 DCHECK(properties);
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
81 // values here.
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: "
105 << new_value << ".";
107 DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->WriteValue(
108 object_path_,
109 new_value,
110 callback,
111 base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError,
112 weak_ptr_factory_.GetWeakPtr(),
113 error_callback));
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;
123 error_callback.Run(
124 BluetoothRemoteGattServiceChromeOS::DBusErrorToServiceError(error_name));
127 } // namespace chromeos