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 "chromeos/dbus/fake_bluetooth_gatt_descriptor_service_provider.h"
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"
15 FakeBluetoothGattDescriptorServiceProvider::
16 FakeBluetoothGattDescriptorServiceProvider(
17 const dbus::ObjectPath
& object_path
,
19 const std::string
& uuid
,
20 const std::vector
<std::string
>& permissions
,
21 const dbus::ObjectPath
& characteristic_path
)
22 : object_path_(object_path
),
24 characteristic_path_(characteristic_path
),
26 VLOG(1) << "Creating Bluetooth GATT descriptor: " << object_path_
.value();
28 DCHECK(object_path_
.IsValid());
29 DCHECK(characteristic_path_
.IsValid());
30 DCHECK(!uuid
.empty());
32 DCHECK(StartsWithASCII(
33 object_path_
.value(), characteristic_path_
.value() + "/", true));
35 // TODO(armansito): Do something with |permissions|.
37 FakeBluetoothGattManagerClient
* fake_bluetooth_gatt_manager_client
=
38 static_cast<FakeBluetoothGattManagerClient
*>(
39 DBusThreadManager::Get()->GetBluetoothGattManagerClient());
40 fake_bluetooth_gatt_manager_client
->
41 RegisterDescriptorServiceProvider(this);
44 FakeBluetoothGattDescriptorServiceProvider::
45 ~FakeBluetoothGattDescriptorServiceProvider() {
46 VLOG(1) << "Cleaning up Bluetooth GATT descriptor: "
47 << object_path_
.value();
49 FakeBluetoothGattManagerClient
* fake_bluetooth_gatt_manager_client
=
50 static_cast<FakeBluetoothGattManagerClient
*>(
51 DBusThreadManager::Get()->GetBluetoothGattManagerClient());
52 fake_bluetooth_gatt_manager_client
->
53 UnregisterDescriptorServiceProvider(this);
56 void FakeBluetoothGattDescriptorServiceProvider::SendValueChanged(
57 const std::vector
<uint8
>& value
) {
58 VLOG(1) << "Sent descriptor value changed: " << object_path_
.value()
59 << " UUID: " << uuid_
;
62 void 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_
;
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();
80 if (!fake_bluetooth_gatt_manager_client
->IsServiceRegistered(
81 characteristic
->service_path())) {
82 VLOG(1) << "GATT descriptor not registered.";
87 // Pass on to the delegate.
89 delegate_
->GetDescriptorValue(callback
, error_callback
);
92 void 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_
;
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();
111 if (!fake_bluetooth_gatt_manager_client
->IsServiceRegistered(
112 characteristic
->service_path())) {
113 VLOG(1) << "GATT descriptor not registered.";
114 error_callback
.Run();
118 // Pass on to the delegate.
120 delegate_
->SetDescriptorValue(value
, callback
, error_callback
);
123 } // namespace chromeos