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_CHARACTERISTIC_SERVICE_PROVIDER_H_ | 5 #ifndef DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_SERVICE_PROVIDER_H_ |
6 #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_SERVICE_PROVIDER_H_ | 6 #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_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/dbus/bluetooth_gatt_attribute_value_delegate.h" |
18 | 18 |
19 namespace bluez { | 19 namespace bluez { |
20 | 20 |
21 // BluetoothGattCharacteristicServiceProvider is used to provide a D-Bus object | 21 // BluetoothGattCharacteristicServiceProvider is used to provide a D-Bus object |
22 // that represents a local GATT characteristic that the Bluetooth daemon can | 22 // that represents a local GATT characteristic that the Bluetooth daemon can |
23 // communicate with. | 23 // 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 characteristic using the | 26 // The Bluetooth daemon communicates with a GATT characteristic 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 // characteristic interface are read-only and don't change throughout the | 28 // characteristic 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 BluetoothGattCharacteristicServiceProvider { | 35 class DEVICE_BLUETOOTH_EXPORT BluetoothGattCharacteristicServiceProvider { |
36 public: | 36 public: |
37 // Interface for reacting to GATT characteristic value requests. | |
38 class Delegate { | |
39 public: | |
40 virtual ~Delegate() {} | |
41 | |
42 // ValueCallback is used for methods that require a characteristic 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 characteristic. 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 // characteristic 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 GetCharacteristicValue( | |
57 const ValueCallback& callback, | |
58 const ErrorCallback& error_callback) = 0; | |
59 | |
60 // This method will be called, when a remote device requests to write the | |
61 // value of the exported GATT characteristic. Invoke |callback| to report | |
62 // that the value was successfully written. Invoke |error_callback| to | |
63 // report a failure to write the value. This can happen, for example, if the | |
64 // characteristic has no write permission set. Either callback should be | |
65 // invoked after a reasonable amount of time, since the request will time | |
66 // out if left pending for too long. | |
67 // | |
68 // The delegate should use this method to perform any side-effects that may | |
69 // occur based on the set value and potentially send a property changed | |
70 // signal to notify the Bluetooth daemon that the value has changed. | |
71 virtual void SetCharacteristicValue( | |
72 const std::vector<uint8_t>& value, | |
73 const base::Closure& callback, | |
74 const ErrorCallback& error_callback) = 0; | |
75 }; | |
76 | |
77 virtual ~BluetoothGattCharacteristicServiceProvider(); | 37 virtual ~BluetoothGattCharacteristicServiceProvider(); |
78 | 38 |
79 // 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 |
80 // of the "Value" property has changed to |value|. | 40 // of the "Value" property has changed to |value|. |
81 virtual void SendValueChanged(const std::vector<uint8_t>& value) = 0; | 41 virtual void SendValueChanged(const std::vector<uint8_t>& value) = 0; |
82 | 42 |
| 43 // Writes the characteristics'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 |
83 // 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 |
84 // the object onto, |uuid| is the 128-bit GATT characteristic UUID, | 52 // the object onto, |uuid| is the 128-bit GATT characteristic UUID, |
85 // |flags| is the list of GATT characteristic properties, |permissions| is the | 53 // |flags| is the list of GATT characteristic properties, |permissions| is the |
86 // list of attribute permissions, |service_path| is the object path of the | 54 // list of attribute permissions, |service_path| is the object path of the |
87 // exported GATT service the characteristic belongs to, |object_path| is the | 55 // exported GATT service the characteristic belongs to, |object_path| is the |
88 // object path that the characteristic should have, and |delegate| is the | 56 // object path that the characteristic should have, and |delegate| is the |
89 // object that "Value" Get/Set requests will be passed to and responses | 57 // object that "Value" Get/Set requests will be passed to and responses |
90 // generated from. | 58 // generated from. |
91 // | 59 // |
92 // Object paths of GATT characteristics must be hierarchical to the path of | 60 // Object paths of GATT characteristics must be hierarchical to the path of |
93 // the GATT service they belong to. Hence, |object_path| must have | 61 // the GATT service they belong to. Hence, |object_path| must have |
94 // |service_path| as its prefix. Ownership of |delegate| is not taken, thus | 62 // |service_path| as its prefix. Ownership of |delegate| is not taken, thus |
95 // the delegate should outlive this instance. A delegate should handle only | 63 // the delegate should outlive this instance. A delegate should handle only |
96 // a single exported characteristic and own it. | 64 // a single exported characteristic and own it. |
97 static BluetoothGattCharacteristicServiceProvider* Create( | 65 static BluetoothGattCharacteristicServiceProvider* Create( |
98 dbus::Bus* bus, | 66 dbus::Bus* bus, |
99 const dbus::ObjectPath& object_path, | 67 const dbus::ObjectPath& object_path, |
100 Delegate* delegate, | 68 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, |
101 const std::string& uuid, | 69 const std::string& uuid, |
102 const std::vector<std::string>& flags, | 70 const std::vector<std::string>& flags, |
103 const std::vector<std::string>& permissions, | 71 const std::vector<std::string>& permissions, |
104 const dbus::ObjectPath& service_path); | 72 const dbus::ObjectPath& service_path); |
105 | 73 |
106 protected: | 74 protected: |
107 BluetoothGattCharacteristicServiceProvider(); | 75 BluetoothGattCharacteristicServiceProvider(); |
108 | 76 |
109 private: | 77 private: |
110 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicServiceProvider); | 78 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicServiceProvider); |
111 }; | 79 }; |
112 | 80 |
113 } // namespace bluez | 81 } // namespace bluez |
114 | 82 |
115 #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_SERVICE_PROVIDER_
H_ | 83 #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_SERVICE_PROVIDER_
H_ |
OLD | NEW |