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.
4
5#include "chromeos/dbus/fake_bluetooth_gatt_descriptor_service_provider.h"
6
7#include "base/logging.h"
8#include "base/strings/string_util.h"
9#include "chromeos/dbus/dbus_thread_manager.h"
10#include "chromeos/dbus/fake_bluetooth_gatt_characteristic_service_provider.h"
11#include "chromeos/dbus/fake_bluetooth_gatt_manager_client.h"
12
13namespace chromeos {
14
15FakeBluetoothGattDescriptorServiceProvider::
16    FakeBluetoothGattDescriptorServiceProvider(
17        const dbus::ObjectPath& object_path,
18        Delegate* delegate,
19        const std::string& uuid,
20        const std::vector<std::string>& permissions,
21        const dbus::ObjectPath& characteristic_path)
22        : object_path_(object_path),
23          uuid_(uuid),
24          characteristic_path_(characteristic_path),
25          delegate_(delegate) {
26  VLOG(1) << "Creating Bluetooth GATT descriptor: " << object_path_.value();
27
28  DCHECK(object_path_.IsValid());
29  DCHECK(characteristic_path_.IsValid());
30  DCHECK(!uuid.empty());
31  DCHECK(delegate_);
32  DCHECK(StartsWithASCII(
33      object_path_.value(), characteristic_path_.value() + "/", true));
34
35  // TODO(armansito): Do something with |permissions|.
36
37  FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
38      static_cast<FakeBluetoothGattManagerClient*>(
39          DBusThreadManager::Get()->GetBluetoothGattManagerClient());
40  fake_bluetooth_gatt_manager_client->
41      RegisterDescriptorServiceProvider(this);
42}
43
44FakeBluetoothGattDescriptorServiceProvider::
45    ~FakeBluetoothGattDescriptorServiceProvider() {
46  VLOG(1) << "Cleaning up Bluetooth GATT descriptor: "
47          << object_path_.value();
48
49  FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
50      static_cast<FakeBluetoothGattManagerClient*>(
51          DBusThreadManager::Get()->GetBluetoothGattManagerClient());
52  fake_bluetooth_gatt_manager_client->
53      UnregisterDescriptorServiceProvider(this);
54}
55
56void FakeBluetoothGattDescriptorServiceProvider::SendValueChanged(
57    const std::vector<uint8>& value) {
58  VLOG(1) << "Sent descriptor value changed: " << object_path_.value()
59          << " UUID: " << uuid_;
60}
61
62void FakeBluetoothGattDescriptorServiceProvider::GetValue(
63    const Delegate::ValueCallback& callback,
64    const Delegate::ErrorCallback& error_callback) {
65  VLOG(1) << "GATT descriptor value Get request: " << object_path_.value()
66          << " UUID: " << uuid_;
67
68  // Check if this descriptor is registered.
69  FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
70      static_cast<FakeBluetoothGattManagerClient*>(
71          DBusThreadManager::Get()->GetBluetoothGattManagerClient());
72  FakeBluetoothGattCharacteristicServiceProvider* characteristic =
73      fake_bluetooth_gatt_manager_client->GetCharacteristicServiceProvider(
74          characteristic_path_);
75  if (!characteristic) {
76    VLOG(1) << "GATT characteristic for descriptor does not exist: "
77            << characteristic_path_.value();
78    return;
79  }
80  if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered(
81          characteristic->service_path())) {
82    VLOG(1) << "GATT descriptor not registered.";
83    error_callback.Run();
84    return;
85  }
86
87  // Pass on to the delegate.
88  DCHECK(delegate_);
89  delegate_->GetDescriptorValue(callback, error_callback);
90}
91
92void FakeBluetoothGattDescriptorServiceProvider::SetValue(
93    const std::vector<uint8>& value,
94    const base::Closure& callback,
95    const Delegate::ErrorCallback& error_callback) {
96  VLOG(1) << "GATT descriptor value Set request: " << object_path_.value()
97          << " UUID: " << uuid_;
98
99  // Check if this descriptor is registered.
100  FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
101      static_cast<FakeBluetoothGattManagerClient*>(
102          DBusThreadManager::Get()->GetBluetoothGattManagerClient());
103  FakeBluetoothGattCharacteristicServiceProvider* characteristic =
104      fake_bluetooth_gatt_manager_client->GetCharacteristicServiceProvider(
105          characteristic_path_);
106  if (!characteristic) {
107    VLOG(1) << "GATT characteristic for descriptor does not exist: "
108            << characteristic_path_.value();
109    return;
110  }
111  if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered(
112          characteristic->service_path())) {
113    VLOG(1) << "GATT descriptor not registered.";
114    error_callback.Run();
115    return;
116  }
117
118  // Pass on to the delegate.
119  DCHECK(delegate_);
120  delegate_->SetDescriptorValue(value, callback, error_callback);
121}
122
123}  // namespace chromeos
124