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