OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_SERVICE_PROVIDER_H_ | 5 #ifndef DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_SERVICE_PROVIDER_H_ |
6 #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_SERVICE_PROVIDER_H_ | 6 #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_SERVICE_PROVIDER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | |
10 #include <string> | 9 #include <string> |
11 #include <vector> | 10 #include <vector> |
12 | 11 |
13 #include "base/callback.h" | |
14 #include "base/macros.h" | 12 #include "base/macros.h" |
15 #include "dbus/bus.h" | 13 #include "dbus/bus.h" |
| 14 #include "dbus/message.h" |
16 #include "dbus/object_path.h" | 15 #include "dbus/object_path.h" |
17 #include "device/bluetooth/bluetooth_export.h" | 16 #include "device/bluetooth/bluetooth_export.h" |
| 17 #include "device/bluetooth/bluez/bluetooth_gatt_service_bluez.h" |
18 | 18 |
19 namespace bluez { | 19 namespace bluez { |
20 | 20 |
21 // BluetoothGattDescriptorServiceProvider is used to provide a D-Bus object that | 21 // BluetoothGattDescriptorServiceProvider is used to provide a D-Bus object that |
22 // represents a local GATT characteristic descriptor that the Bluetooth daemon | 22 // represents a local GATT characteristic descriptor that the Bluetooth daemon |
23 // can communicate with. | 23 // can communicate with. |
24 // | 24 // |
25 // Instantiate with a chosen D-Bus object path, delegate, and other fields. | 25 // Instantiate with a chosen D-Bus object path, delegate, and other fields. |
26 // The Bluetooth daemon communicates with a GATT descriptor using the | 26 // The Bluetooth daemon communicates with a GATT descriptor using the |
27 // standard DBus.Properties interface. While most properties of the GATT | 27 // standard DBus.Properties interface. While most properties of the GATT |
28 // descriptor interface are read-only and don't change throughout the | 28 // descriptor interface are read-only and don't change throughout the |
29 // life-time of the object, the "Value" property is both writeable and its | 29 // life-time of the object, the "Value" property is both writeable and its |
30 // value can change. Both Get and Set operations performed on the "Value" | 30 // value can change. Both Get and Set operations performed on the "Value" |
31 // property are delegated to the Delegate object, an instance of which is | 31 // property are delegated to the Delegate object, an instance of which is |
32 // mandatory during initialization. In addition, a "SendValueChanged" method is | 32 // mandatory during initialization. In addition, a "SendValueChanged" method is |
33 // provided, which emits a DBus.Properties.PropertyChanged signal for the | 33 // provided, which emits a DBus.Properties.PropertyChanged signal for the |
34 // "Value" property. | 34 // "Value" property. |
35 class DEVICE_BLUETOOTH_EXPORT BluetoothGattDescriptorServiceProvider { | 35 class DEVICE_BLUETOOTH_EXPORT BluetoothGattDescriptorServiceProvider { |
36 public: | 36 public: |
37 // Interface for reacting to GATT characteristic descriptor value requests. | |
38 class Delegate { | |
39 public: | |
40 virtual ~Delegate() {} | |
41 | |
42 // ValueCallback is used for methods that require a descriptor value | |
43 // to be returned. | |
44 typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback; | |
45 | |
46 // ErrorCallback is used by methods to report failure. | |
47 typedef base::Closure ErrorCallback; | |
48 | |
49 // This method will be called when a remote device requests to read the | |
50 // value of the exported GATT descriptor. Invoke |callback| with a value | |
51 // to return that value to the requester. Invoke |error_callback| to report | |
52 // a failure to read the value. This can happen, for example, if the | |
53 // descriptor has no read permission set. Either callback should be | |
54 // invoked after a reasonable amount of time, since the request will time | |
55 // out if left pending for too long. | |
56 virtual void GetDescriptorValue(const ValueCallback& callback, | |
57 const ErrorCallback& error_callback) = 0; | |
58 | |
59 // This method will be called, when a remote device requests to write the | |
60 // value of the exported GATT descriptor. Invoke |callback| to report | |
61 // that the value was successfully written. Invoke |error_callback| to | |
62 // report a failure to write the value. This can happen, for example, if the | |
63 // descriptor has no write permission set. Either callback should be | |
64 // invoked after a reasonable amount of time, since the request will time | |
65 // out if left pending for too long. | |
66 // | |
67 // The delegate should use this method to perform any side-effects that may | |
68 // occur based on the set value and potentially send a property changed | |
69 // signal to notify the Bluetooth daemon that the value has changed. | |
70 virtual void SetDescriptorValue(const std::vector<uint8_t>& value, | |
71 const base::Closure& callback, | |
72 const ErrorCallback& error_callback) = 0; | |
73 }; | |
74 | |
75 virtual ~BluetoothGattDescriptorServiceProvider(); | 37 virtual ~BluetoothGattDescriptorServiceProvider(); |
76 | 38 |
77 // Send a PropertyChanged signal to notify the Bluetooth daemon that the value | 39 // Send a PropertyChanged signal to notify the Bluetooth daemon that the value |
78 // of the "Value" property has changed to |value|. | 40 // of the "Value" property has changed to |value|. |
79 virtual void SendValueChanged(const std::vector<uint8_t>& value) = 0; | 41 virtual void SendValueChanged(const std::vector<uint8_t>& value) = 0; |
80 | 42 |
| 43 // Writes the descriptor's properties into the provided writer. If |
| 44 // value is not null, it is written also, otherwise no value property is |
| 45 // written. |
| 46 virtual void WriteProperties(dbus::MessageWriter* writer, |
| 47 const std::vector<uint8_t>* value) {} |
| 48 |
| 49 virtual const dbus::ObjectPath& object_path() const = 0; |
| 50 |
81 // Creates the instance, where |bus| is the D-Bus bus connection to export | 51 // Creates the instance, where |bus| is the D-Bus bus connection to export |
82 // the object onto, |uuid| is the 128-bit GATT descriptor UUID, |permissions| | 52 // the object onto, |uuid| is the 128-bit GATT descriptor UUID, |permissions| |
83 // is the list of attribute permissions, |characteristic_path| is the object | 53 // is the list of attribute permissions, |characteristic_path| is the object |
84 // path of the exported GATT characteristic the descriptor belongs to, | 54 // path of the exported GATT characteristic the descriptor belongs to, |
85 // |object_path| is the object path that the descriptor should have, and | 55 // |object_path| is the object path that the descriptor should have, and |
86 // |delegate| is the object that value Get/Set requests will be passed to and | 56 // |delegate| is the object that value Get/Set requests will be passed to and |
87 // responses generated from. | 57 // responses generated from. |
88 // | 58 // |
89 // Object paths of GATT descriptors must be hierarchical to the path of the | 59 // Object paths of GATT descriptors must be hierarchical to the path of the |
90 // GATT characteristic they belong to. Hence, |object_path| must have | 60 // GATT characteristic they belong to. Hence, |object_path| must have |
91 // |characteristic_path| as its prefix. Ownership of |delegate| is not taken, | 61 // |characteristic_path| as its prefix. Ownership of |delegate| is not taken, |
92 // thus the delegate should outlive this instance. A delegate should handle | 62 // thus the delegate should outlive this instance. A delegate should handle |
93 // only a single exported descriptor and own it. | 63 // only a single exported descriptor and own it. |
94 static BluetoothGattDescriptorServiceProvider* Create( | 64 static BluetoothGattDescriptorServiceProvider* Create( |
95 dbus::Bus* bus, | 65 dbus::Bus* bus, |
96 const dbus::ObjectPath& object_path, | 66 const dbus::ObjectPath& object_path, |
97 Delegate* delegate, | 67 BluetoothGattServiceBlueZ::AttributeValueDelegate* delegate, |
98 const std::string& uuid, | 68 const std::string& uuid, |
99 const std::vector<std::string>& permissions, | 69 const std::vector<std::string>& permissions, |
100 const dbus::ObjectPath& characteristic_path); | 70 const dbus::ObjectPath& characteristic_path); |
101 | 71 |
102 protected: | 72 protected: |
103 BluetoothGattDescriptorServiceProvider(); | 73 BluetoothGattDescriptorServiceProvider(); |
104 | 74 |
105 private: | 75 private: |
106 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorServiceProvider); | 76 DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorServiceProvider); |
107 }; | 77 }; |
108 | 78 |
109 } // namespace bluez | 79 } // namespace bluez |
110 | 80 |
111 #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_SERVICE_PROVIDER_H_ | 81 #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_DESCRIPTOR_SERVICE_PROVIDER_H_ |
OLD | NEW |