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 #ifndef CHROMEOS_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
6 #define CHROMEOS_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "chromeos/chromeos_export.h"
14 #include "chromeos/dbus/dbus_client.h"
15 #include "dbus/object_path.h"
16 #include "dbus/property.h"
20 // BluetoothGattCharacteristicClient is used to communicate with remote GATT
21 // characteristic objects exposed by the Bluetooth daemon.
22 class CHROMEOS_EXPORT BluetoothGattCharacteristicClient
: public DBusClient
{
24 // Structure of properties associated with GATT characteristics.
25 struct Properties
: public dbus::PropertySet
{
26 // The 128-bit characteristic UUID. [read-only]
27 dbus::Property
<std::string
> uuid
;
29 // Object path of the GATT service the characteristic belongs to.
31 dbus::Property
<dbus::ObjectPath
> service
;
33 // The cached value of the characteristic. This property gets updated only
34 // after a successful read request and when a notification or indication is
35 // received. [read-only]
36 dbus::Property
<std::vector
<uint8_t>> value
;
38 // Whether or not this characteristic is currently sending ValueUpdated
39 // signals. [read-only]
40 dbus::Property
<bool> notifying
;
42 // List of flags representing the GATT "Characteristic Properties bit field"
43 // and properties read from the GATT "Characteristic Extended Properties"
44 // descriptor bit field. [read-only, optional]
45 dbus::Property
<std::vector
<std::string
>> flags
;
47 // Array of object paths representing the descriptors of this
48 // characteristic. [read-only]
49 dbus::Property
<std::vector
<dbus::ObjectPath
>> descriptors
;
51 Properties(dbus::ObjectProxy
* object_proxy
,
52 const std::string
& interface_name
,
53 const PropertyChangedCallback
& callback
);
54 ~Properties() override
;
57 // Interface for observing changes from a remote GATT characteristic.
60 virtual ~Observer() {}
62 // Called when the GATT characteristic with object path |object_path| is
63 // added to the system.
64 virtual void GattCharacteristicAdded(const dbus::ObjectPath
& object_path
) {}
66 // Called when the GATT characteristic with object path |object_path| is
67 // removed from the system.
68 virtual void GattCharacteristicRemoved(
69 const dbus::ObjectPath
& object_path
) {}
71 // Called when the GATT characteristic with object path |object_path| has a
72 // change in the value of the property named |property_name|.
73 virtual void GattCharacteristicPropertyChanged(
74 const dbus::ObjectPath
& object_path
,
75 const std::string
& property_name
) {}
78 // Callbacks used to report the result of asynchronous methods.
79 typedef base::Callback
<void(const std::string
& error_name
,
80 const std::string
& error_message
)> ErrorCallback
;
81 typedef base::Callback
<void(const std::vector
<uint8
>& value
)> ValueCallback
;
83 ~BluetoothGattCharacteristicClient() override
;
85 // Adds and removes observers for events on all remote GATT characteristics.
86 // Check the |object_path| parameter of observer methods to determine which
87 // GATT characteristic is issuing the event.
88 virtual void AddObserver(Observer
* observer
) = 0;
89 virtual void RemoveObserver(Observer
* observer
) = 0;
91 // Returns the list of GATT characteristic object paths known to the system.
92 virtual std::vector
<dbus::ObjectPath
> GetCharacteristics() = 0;
94 // Obtain the properties for the GATT characteristic with object path
95 // |object_path|. Values should be copied if needed.
96 virtual Properties
* GetProperties(const dbus::ObjectPath
& object_path
) = 0;
98 // Issues a request to read the value of GATT characteristic with object path
99 // |object_path| and returns the value in |callback| on success. On error,
100 // invokes |error_callback|.
101 virtual void ReadValue(const dbus::ObjectPath
& object_path
,
102 const ValueCallback
& callback
,
103 const ErrorCallback
& error_callback
) = 0;
105 // Issues a request to write the value of GATT characteristic with object path
106 // |object_path| with value |value|. Invokes |callback| on success and
107 // |error_callback| on failure.
108 virtual void WriteValue(const dbus::ObjectPath
& object_path
,
109 const std::vector
<uint8
>& value
,
110 const base::Closure
& callback
,
111 const ErrorCallback
& error_callback
) = 0;
113 // Starts a notification session from this characteristic with object path
114 // |object_path| if it supports value notifications or indications. Invokes
115 // |callback| on success and |error_callback| on failure.
116 virtual void StartNotify(const dbus::ObjectPath
& object_path
,
117 const base::Closure
& callback
,
118 const ErrorCallback
& error_callback
) = 0;
120 // Cancels any previous StartNotify transaction for characteristic with
121 // object path |object_path|. Invokes |callback| on success and
122 // |error_callback| on failure.
123 virtual void StopNotify(const dbus::ObjectPath
& object_path
,
124 const base::Closure
& callback
,
125 const ErrorCallback
& error_callback
) = 0;
127 // Creates the instance.
128 static BluetoothGattCharacteristicClient
* Create();
130 // Constants used to indicate exceptional error conditions.
131 static const char kNoResponseError
[];
132 static const char kUnknownCharacteristicError
[];
135 BluetoothGattCharacteristicClient();
138 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicClient
);
141 } // namespace chromeos
143 #endif // CHROMEOS_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_