summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.cc
blob: da3856691bc0499e62b31dcc010afa3eb2cccefa (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h"

#include "base/bind.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "chromeos/dbus/bluetooth_gatt_descriptor_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h"
#include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h"

namespace chromeos {

namespace {

// Stream operator for logging vector<uint8>.
std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) {
  out << "[";
  for (std::vector<uint8>::const_iterator iter = bytes.begin();
       iter != bytes.end(); ++iter) {
    out << base::StringPrintf("%02X", *iter);
  }
  return out << "]";
}

}  // namespace

BluetoothRemoteGattDescriptorChromeOS::BluetoothRemoteGattDescriptorChromeOS(
    BluetoothRemoteGattCharacteristicChromeOS* characteristic,
    const dbus::ObjectPath& object_path)
    : object_path_(object_path),
      characteristic_(characteristic),
      weak_ptr_factory_(this) {
  VLOG(1) << "Creating remote GATT descriptor with identifier: "
          << GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
}

BluetoothRemoteGattDescriptorChromeOS::
    ~BluetoothRemoteGattDescriptorChromeOS() {
}

std::string BluetoothRemoteGattDescriptorChromeOS::GetIdentifier() const {
  return object_path_.value();
}

device::BluetoothUUID BluetoothRemoteGattDescriptorChromeOS::GetUUID() const {
  BluetoothGattDescriptorClient::Properties* properties =
      DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->
          GetProperties(object_path_);
  DCHECK(properties);
  return device::BluetoothUUID(properties->uuid.value());
}

bool BluetoothRemoteGattDescriptorChromeOS::IsLocal() const {
  return false;
}

const std::vector<uint8>&
BluetoothRemoteGattDescriptorChromeOS::GetValue() const {
  BluetoothGattDescriptorClient::Properties* properties =
      DBusThreadManager::Get()
          ->GetBluetoothGattDescriptorClient()
          ->GetProperties(object_path_);

  DCHECK(properties);

  return properties->value.value();
}

device::BluetoothGattCharacteristic*
BluetoothRemoteGattDescriptorChromeOS::GetCharacteristic() const {
  return characteristic_;
}

device::BluetoothGattCharacteristic::Permissions
BluetoothRemoteGattDescriptorChromeOS::GetPermissions() const {
  // TODO(armansito): Once BlueZ defines the permissions, return the correct
  // values here.
  return device::BluetoothGattCharacteristic::PERMISSION_NONE;
}

void BluetoothRemoteGattDescriptorChromeOS::ReadRemoteDescriptor(
    const ValueCallback& callback,
    const ErrorCallback& error_callback) {
  VLOG(1) << "Sending GATT characteristic descriptor read request to "
          << "descriptor: " << GetIdentifier() << ", UUID: "
          << GetUUID().canonical_value();

  DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->ReadValue(
      object_path_, callback,
      base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError,
                 weak_ptr_factory_.GetWeakPtr(), error_callback));
}

void BluetoothRemoteGattDescriptorChromeOS::WriteRemoteDescriptor(
    const std::vector<uint8>& new_value,
    const base::Closure& callback,
    const ErrorCallback& error_callback) {
  VLOG(1) << "Sending GATT characteristic descriptor write request to "
          << "characteristic: " << GetIdentifier() << ", UUID: "
          << GetUUID().canonical_value() << ", with value: "
          << new_value << ".";

  DBusThreadManager::Get()->GetBluetoothGattDescriptorClient()->WriteValue(
      object_path_,
      new_value,
      callback,
      base::Bind(&BluetoothRemoteGattDescriptorChromeOS::OnError,
                 weak_ptr_factory_.GetWeakPtr(),
                 error_callback));
}

void BluetoothRemoteGattDescriptorChromeOS::OnError(
    const ErrorCallback& error_callback,
    const std::string& error_name,
    const std::string& error_message) {
  VLOG(1) << "Operation failed: " << error_name
          << ", message: " << error_message;

  error_callback.Run(
      BluetoothRemoteGattServiceChromeOS::DBusErrorToServiceError(error_name));
}

}  // namespace chromeos