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_characteristic_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_manager_client.h"
11
12namespace chromeos {
13
14FakeBluetoothGattCharacteristicServiceProvider::
15    FakeBluetoothGattCharacteristicServiceProvider(
16        const dbus::ObjectPath& object_path,
17        Delegate* delegate,
18        const std::string& uuid,
19        const std::vector<std::string>& flags,
20        const std::vector<std::string>& permissions,
21        const dbus::ObjectPath& service_path)
22        : object_path_(object_path),
23          uuid_(uuid),
24          service_path_(service_path),
25          delegate_(delegate) {
26  VLOG(1) << "Creating Bluetooth GATT characteristic: " << object_path_.value();
27
28  DCHECK(object_path_.IsValid());
29  DCHECK(service_path_.IsValid());
30  DCHECK(!uuid.empty());
31  DCHECK(delegate_);
32  DCHECK(StartsWithASCII(
33      object_path_.value(), service_path_.value() + "/", true));
34
35  // TODO(armansito): Do something with |flags| and |permissions|.
36
37  FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
38      static_cast<FakeBluetoothGattManagerClient*>(
39          DBusThreadManager::Get()->GetBluetoothGattManagerClient());
40  fake_bluetooth_gatt_manager_client->
41      RegisterCharacteristicServiceProvider(this);
42}
43
44FakeBluetoothGattCharacteristicServiceProvider::
45    ~FakeBluetoothGattCharacteristicServiceProvider() {
46  VLOG(1) << "Cleaning up Bluetooth GATT characteristic: "
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      UnregisterCharacteristicServiceProvider(this);
54}
55
56void FakeBluetoothGattCharacteristicServiceProvider::SendValueChanged(
57    const std::vector<uint8>& value) {
58  VLOG(1) << "Sent characteristic value changed: " << object_path_.value()
59          << " UUID: " << uuid_;
60}
61
62void FakeBluetoothGattCharacteristicServiceProvider::GetValue(
63    const Delegate::ValueCallback& callback,
64    const Delegate::ErrorCallback& error_callback) {
65  VLOG(1) << "GATT characteristic value Get request: " << object_path_.value()
66          << " UUID: " << uuid_;
67
68  // Check if this characteristic is registered.
69  FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
70      static_cast<FakeBluetoothGattManagerClient*>(
71          DBusThreadManager::Get()->GetBluetoothGattManagerClient());
72  if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered(service_path_)) {
73    VLOG(1) << "GATT characteristic not registered.";
74    error_callback.Run();
75    return;
76  }
77
78  // Pass on to the delegate.
79  DCHECK(delegate_);
80  delegate_->GetCharacteristicValue(callback, error_callback);
81}
82
83void FakeBluetoothGattCharacteristicServiceProvider::SetValue(
84    const std::vector<uint8>& value,
85    const base::Closure& callback,
86    const Delegate::ErrorCallback& error_callback) {
87  VLOG(1) << "GATT characteristic value Set request: " << object_path_.value()
88          << " UUID: " << uuid_;
89
90  // Check if this characteristic is registered.
91  FakeBluetoothGattManagerClient* fake_bluetooth_gatt_manager_client =
92      static_cast<FakeBluetoothGattManagerClient*>(
93          DBusThreadManager::Get()->GetBluetoothGattManagerClient());
94  if (!fake_bluetooth_gatt_manager_client->IsServiceRegistered(service_path_)) {
95    VLOG(1) << "GATT characteristic not registered.";
96    error_callback.Run();
97    return;
98  }
99
100  // Pass on to the delegate.
101  DCHECK(delegate_);
102  delegate_->SetCharacteristicValue(value, callback, error_callback);
103}
104
105}  // namespace chromeos
106