OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_BLUETOOTH_GATT_SERVICE_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string> | |
10 #include <vector> | 9 #include <vector> |
11 | 10 |
12 #include "base/callback.h" | 11 #include "base/callback.h" |
13 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
14 #include "base/macros.h" | 13 #include "base/macros.h" |
15 #include "device/bluetooth/bluetooth_export.h" | 14 #include "device/bluetooth/bluetooth_export.h" |
15 #include "device/bluetooth/bluetooth_gatt_service.h" | |
16 #include "device/bluetooth/bluetooth_uuid.h" | 16 #include "device/bluetooth/bluetooth_uuid.h" |
17 | 17 |
18 namespace device { | 18 namespace device { |
19 | 19 |
20 class BluetoothDevice; | 20 class BluetoothDevice; |
21 class BluetoothGattCharacteristic; | 21 class BluetoothRemoteGattCharacteristic; |
22 class BluetoothGattDescriptor; | 22 class BluetoothRemoteGattDescriptor; |
23 | 23 |
24 // BluetoothGattService represents a local or remote GATT service. A GATT | 24 // BluetoothLocalGattService represents a local GATT service. |
scheib
2016/04/20 01:23:30
" " -> " "
rkc
2016/04/20 16:31:54
Done.
| |
25 // service is hosted by a peripheral and represents a collection of data in | |
26 // the form of GATT characteristics and a set of included GATT services if this | |
27 // service is what is called "a primary service". | |
28 // | 25 // |
29 // Instances of the BluetoothGattService class are used for two functions: | 26 // Instances of the BluetoothLocalGattService class are used to represent a |
30 // 1. To represent GATT attribute hierarchies that have been received from a | 27 // locally hosted GATT attribute hierarchy when the local |
31 // remote Bluetooth GATT peripheral. Such BluetoothGattService instances | 28 // adapter is used in the "peripheral" role. Such instances are meant to be |
32 // are constructed and owned by a BluetoothDevice. | 29 // constructed directly and registered. Once registered, a GATT attribute |
30 // hierarchy will be visible to remote devices in the "central" role. | |
33 // | 31 // |
34 // 2. To represent a locally hosted GATT attribute hierarchy when the local | 32 // Note: We use virtual inheritance on the gatt service since it will be |
35 // adapter is used in the "peripheral" role. Such instances are meant to be | 33 // inherited by platform specific versions of the gatt service classes also. The |
36 // constructed directly and registered. Once registered, a GATT attribute | 34 // platform specific local gatt service classes will inherit both this class and |
37 // hierarchy will be visible to remote devices in the "central" role. | 35 // their gatt service class, hence causing an inheritance diamond. |
38 class DEVICE_BLUETOOTH_EXPORT BluetoothGattService { | 36 class DEVICE_BLUETOOTH_EXPORT BluetoothLocalGattService |
37 : public virtual BluetoothGattService { | |
39 public: | 38 public: |
40 // The Delegate class is used to send certain events that need to be handled | 39 // The Delegate class is used to send certain events that need to be handled |
41 // when the device is in peripheral mode. The delegate handles read and write | 40 // when the device is in peripheral mode. The delegate handles read and write |
42 // requests that are issued from remote clients. | 41 // requests that are issued from remote clients. |
43 class Delegate { | 42 class Delegate { |
44 public: | 43 public: |
45 // Callbacks used for communicating GATT request responses. | 44 // Callbacks used for communicating GATT request responses. |
46 typedef base::Callback<void(const std::vector<uint8_t>)> ValueCallback; | 45 typedef base::Callback<void(const std::vector<uint8_t>)> ValueCallback; |
47 typedef base::Closure ErrorCallback; | 46 typedef base::Closure ErrorCallback; |
48 | 47 |
49 // Called when a remote device in the central role requests to read the | 48 // Called when a remote device in the central role requests to read the |
50 // value of the characteristic |characteristic| starting at offset |offset|. | 49 // value of the characteristic |characteristic| starting at offset |offset|. |
51 // This method is only called if the characteristic was specified as | 50 // This method is only called if the characteristic was specified as |
52 // readable and any authentication and authorization challenges were | 51 // readable and any authentication and authorization challenges were |
53 // satisfied by the remote device. | 52 // satisfied by the remote device. |
54 // | 53 // |
55 // To respond to the request with success and return the requested value, | 54 // To respond to the request with success and return the requested value, |
56 // the delegate must invoke |callback| with the value. Doing so will | 55 // the delegate must invoke |callback| with the value. Doing so will |
57 // automatically update the value property of |characteristic|. To respond | 56 // automatically update the value property of |characteristic|. To respond |
58 // to the request with failure (e.g. if an invalid offset was given), | 57 // to the request with failure (e.g. if an invalid offset was given), |
59 // delegates must invoke |error_callback|. If neither callback parameter is | 58 // delegates must invoke |error_callback|. If neither callback parameter is |
60 // invoked, the request will time out and result in an error. Therefore, | 59 // invoked, the request will time out and result in an error. Therefore, |
61 // delegates MUST invoke either |callback| or |error_callback|. | 60 // delegates MUST invoke either |callback| or |error_callback|. |
62 virtual void OnCharacteristicReadRequest( | 61 virtual void OnCharacteristicReadRequest( |
63 const BluetoothGattService* service, | 62 const BluetoothLocalGattService* service, |
64 const BluetoothGattCharacteristic* characteristic, | 63 const BluetoothRemoteGattCharacteristic* characteristic, |
scheib
2016/04/20 01:23:30
Local Characteristic (and several more below)
rkc
2016/04/20 16:31:54
Done.
| |
65 int offset, | 64 int offset, |
66 const ValueCallback& callback, | 65 const ValueCallback& callback, |
67 const ErrorCallback& error_callback) = 0; | 66 const ErrorCallback& error_callback) = 0; |
68 | 67 |
69 // Called when a remote device in the central role requests to write the | 68 // Called when a remote device in the central role requests to write the |
70 // value of the characteristic |characteristic| starting at offset |offset|. | 69 // value of the characteristic |characteristic| starting at offset |offset|. |
71 // This method is only called if the characteristic was specified as | 70 // This method is only called if the characteristic was specified as |
72 // writable and any authentication and authorization challenges were | 71 // writable and any authentication and authorization challenges were |
73 // satisfied by the remote device. | 72 // satisfied by the remote device. |
74 // | 73 // |
75 // To respond to the request with success the delegate must invoke | 74 // To respond to the request with success the delegate must invoke |
76 // |callback| with the new value of the characteristic. Doing so will | 75 // |callback| with the new value of the characteristic. Doing so will |
77 // automatically update the value property of |characteristic|. To respond | 76 // automatically update the value property of |characteristic|. To respond |
78 // to the request with failure (e.g. if an invalid offset was given), | 77 // to the request with failure (e.g. if an invalid offset was given), |
79 // delegates must invoke |error_callback|. If neither callback parameter is | 78 // delegates must invoke |error_callback|. If neither callback parameter is |
80 // invoked, the request will time out and result in an error. Therefore, | 79 // invoked, the request will time out and result in an error. Therefore, |
81 // delegates MUST invoke either |callback| or |error_callback|. | 80 // delegates MUST invoke either |callback| or |error_callback|. |
82 virtual void OnCharacteristicWriteRequest( | 81 virtual void OnCharacteristicWriteRequest( |
83 const BluetoothGattService* service, | 82 const BluetoothLocalGattService* service, |
84 const BluetoothGattCharacteristic* characteristic, | 83 const BluetoothRemoteGattCharacteristic* characteristic, |
85 const std::vector<uint8_t>& value, | 84 const std::vector<uint8_t>& value, |
86 int offset, | 85 int offset, |
87 const ValueCallback& callback, | 86 const ValueCallback& callback, |
88 const ErrorCallback& error_callback) = 0; | 87 const ErrorCallback& error_callback) = 0; |
89 | 88 |
90 // Called when a remote device in the central role requests to read the | 89 // Called when a remote device in the central role requests to read the |
91 // value of the descriptor |descriptor| starting at offset |offset|. | 90 // value of the descriptor |descriptor| starting at offset |offset|. |
92 // This method is only called if the characteristic was specified as | 91 // This method is only called if the characteristic was specified as |
93 // readable and any authentication and authorization challenges were | 92 // readable and any authentication and authorization challenges were |
94 // satisfied by the remote device. | 93 // satisfied by the remote device. |
95 // | 94 // |
96 // To respond to the request with success and return the requested value, | 95 // To respond to the request with success and return the requested value, |
97 // the delegate must invoke |callback| with the value. Doing so will | 96 // the delegate must invoke |callback| with the value. Doing so will |
98 // automatically update the value property of |descriptor|. To respond | 97 // automatically update the value property of |descriptor|. To respond |
99 // to the request with failure (e.g. if an invalid offset was given), | 98 // to the request with failure (e.g. if an invalid offset was given), |
100 // delegates must invoke |error_callback|. If neither callback parameter is | 99 // delegates must invoke |error_callback|. If neither callback parameter is |
101 // invoked, the request will time out and result in an error. Therefore, | 100 // invoked, the request will time out and result in an error. Therefore, |
102 // delegates MUST invoke either |callback| or |error_callback|. | 101 // delegates MUST invoke either |callback| or |error_callback|. |
103 virtual void OnDescriptorReadRequest( | 102 virtual void OnDescriptorReadRequest( |
104 const BluetoothGattService* service, | 103 const BluetoothLocalGattService* service, |
105 const BluetoothGattDescriptor* descriptor, | 104 const BluetoothRemoteGattDescriptor* descriptor, |
106 int offset, | 105 int offset, |
107 const ValueCallback& callback, | 106 const ValueCallback& callback, |
108 const ErrorCallback& error_callback) = 0; | 107 const ErrorCallback& error_callback) = 0; |
109 | 108 |
110 // Called when a remote device in the central role requests to write the | 109 // Called when a remote device in the central role requests to write the |
111 // value of the descriptor |descriptor| starting at offset |offset|. | 110 // value of the descriptor |descriptor| starting at offset |offset|. |
112 // This method is only called if the characteristic was specified as | 111 // This method is only called if the characteristic was specified as |
113 // writable and any authentication and authorization challenges were | 112 // writable and any authentication and authorization challenges were |
114 // satisfied by the remote device. | 113 // satisfied by the remote device. |
115 // | 114 // |
116 // To respond to the request with success the delegate must invoke | 115 // To respond to the request with success the delegate must invoke |
117 // |callback| with the new value of the descriptor. Doing so will | 116 // |callback| with the new value of the descriptor. Doing so will |
118 // automatically update the value property of |descriptor|. To respond | 117 // automatically update the value property of |descriptor|. To respond |
119 // to the request with failure (e.g. if an invalid offset was given), | 118 // to the request with failure (e.g. if an invalid offset was given), |
120 // delegates must invoke |error_callback|. If neither callback parameter is | 119 // delegates must invoke |error_callback|. If neither callback parameter is |
121 // invoked, the request will time out and result in an error. Therefore, | 120 // invoked, the request will time out and result in an error. Therefore, |
122 // delegates MUST invoke either |callback| or |error_callback|. | 121 // delegates MUST invoke either |callback| or |error_callback|. |
123 virtual void OnDescriptorWriteRequest( | 122 virtual void OnDescriptorWriteRequest( |
124 const BluetoothGattService* service, | 123 const BluetoothLocalGattService* service, |
125 const BluetoothGattDescriptor* descriptor, | 124 const BluetoothRemoteGattDescriptor* descriptor, |
126 const std::vector<uint8_t>& value, | 125 const std::vector<uint8_t>& value, |
127 int offset, | 126 int offset, |
128 const ValueCallback& callback, | 127 const ValueCallback& callback, |
129 const ErrorCallback& error_callback) = 0; | 128 const ErrorCallback& error_callback) = 0; |
130 }; | 129 }; |
131 | 130 |
132 // Interacting with Characteristics and Descriptors can produce | 131 ~BluetoothLocalGattService() override; |
133 // this set of errors. | |
134 enum GattErrorCode { | |
135 GATT_ERROR_UNKNOWN = 0, | |
136 GATT_ERROR_FAILED, | |
137 GATT_ERROR_IN_PROGRESS, | |
138 GATT_ERROR_INVALID_LENGTH, | |
139 GATT_ERROR_NOT_PERMITTED, | |
140 GATT_ERROR_NOT_AUTHORIZED, | |
141 GATT_ERROR_NOT_PAIRED, | |
142 GATT_ERROR_NOT_SUPPORTED | |
143 }; | |
144 | 132 |
145 // The ErrorCallback is used by methods to asynchronously report errors. | 133 // Constructs a BluetoothLocalGattService that can be locally hosted when the |
146 using ErrorCallback = base::Callback<void(GattErrorCode error_code)>; | 134 // local |
scheib
2016/04/20 01:23:30
wrapping
rkc
2016/04/20 16:31:54
Done.
| |
147 | |
148 virtual ~BluetoothGattService(); | |
149 | |
150 // Constructs a BluetoothGattService that can be locally hosted when the local | |
151 // adapter is in the peripheral role. The resulting object can then be made | 135 // adapter is in the peripheral role. The resulting object can then be made |
152 // available by calling the "Register" method. This method constructs a | 136 // available by calling the "Register" method. This method constructs a |
153 // service with UUID |uuid|. Whether the constructed service is primary or | 137 // service with UUID |uuid|. Whether the constructed service is primary or |
154 // secondary is determined by |is_primary|. |delegate| is used to send certain | 138 // secondary is determined by |is_primary|. |delegate| is used to send certain |
155 // peripheral role events. If |delegate| is NULL, then this service will | 139 // peripheral role events. If |delegate| is NULL, then this service will |
156 // employ a default behavior when responding to read and write requests based | 140 // employ a default behavior when responding to read and write requests based |
157 // on the cached value of its characteristics and descriptors at a given time. | 141 // on the cached value of its characteristics and descriptors at a given time. |
158 static BluetoothGattService* Create(const BluetoothUUID& uuid, | 142 // TODO(rkc): Implement included services. |
159 bool is_primary, | 143 static BluetoothLocalGattService* Create( |
160 Delegate* delegate); | 144 const BluetoothUUID& uuid, |
161 | 145 bool is_primary, |
162 // Identifier used to uniquely identify a GATT service object. This is | 146 BluetoothLocalGattService* included_service, |
163 // different from the service UUID: while multiple services with the same UUID | 147 Delegate* delegate); |
164 // can exist on a Bluetooth device, the identifier returned from this method | |
165 // is unique among all services on the adapter. The contents of the identifier | |
166 // are platform specific. | |
167 virtual std::string GetIdentifier() const = 0; | |
168 | |
169 // The Bluetooth-specific UUID of the service. | |
170 virtual BluetoothUUID GetUUID() const = 0; | |
171 | |
172 // Returns true, if this service hosted locally. If false, then this service | |
173 // represents a remote GATT service. | |
174 virtual bool IsLocal() const = 0; | |
175 | |
176 // Indicates whether the type of this service is primary or secondary. A | |
177 // primary service describes the primary function of the peripheral that | |
178 // hosts it, while a secondary service only makes sense in the presence of a | |
179 // primary service. A primary service may include other primary or secondary | |
180 // services. | |
181 virtual bool IsPrimary() const = 0; | |
182 | |
183 // Returns the BluetoothDevice that this GATT service was received from, which | |
184 // also owns this service. Local services always return NULL. | |
185 virtual BluetoothDevice* GetDevice() const = 0; | |
186 | |
187 // List of characteristics that belong to this service. | |
188 virtual std::vector<BluetoothGattCharacteristic*> | |
189 GetCharacteristics() const = 0; | |
190 | |
191 // List of GATT services that are included by this service. | |
192 virtual std::vector<BluetoothGattService*> | |
193 GetIncludedServices() const = 0; | |
194 | |
195 // Returns the GATT characteristic with identifier |identifier| if it belongs | |
196 // to this GATT service. | |
197 virtual BluetoothGattCharacteristic* GetCharacteristic( | |
198 const std::string& identifier) const = 0; | |
199 | |
200 // Adds characteristics and included services to the local attribute hierarchy | |
201 // represented by this service. These methods only make sense for local | |
202 // services and won't have an effect if this instance represents a remote | |
203 // GATT service and will return false. While ownership of added | |
204 // characteristics are taken over by the service, ownership of an included | |
205 // service is not taken. | |
206 virtual bool AddCharacteristic( | |
207 BluetoothGattCharacteristic* characteristic) = 0; | |
208 virtual bool AddIncludedService(BluetoothGattService* service) = 0; | |
209 | 148 |
210 // Registers this GATT service. Calling Register will make this service and | 149 // Registers this GATT service. Calling Register will make this service and |
211 // all of its associated attributes available on the local adapters GATT | 150 // all of its associated attributes available on the local adapters GATT |
212 // database and the service UUID will be advertised to nearby devices if the | 151 // database and the service UUID will be advertised to nearby devices if the |
213 // local adapter is discoverable. Call Unregister to make this service no | 152 // local adapter is discoverable. Call Unregister to make this service no |
214 // longer available. | 153 // longer available. |
215 // | 154 // |
216 // These methods only make sense for services that are local and will hence | 155 // These methods only make sense for services that are local and will hence |
217 // fail if this instance represents a remote GATT service. |callback| is | 156 // fail if this instance represents a remote GATT service. |callback| is |
218 // called to denote success and |error_callback| to denote failure. | 157 // called to denote success and |error_callback| to denote failure. |
219 virtual void Register(const base::Closure& callback, | 158 virtual void Register(const base::Closure& callback, |
220 const ErrorCallback& error_callback) = 0; | 159 const ErrorCallback& error_callback) = 0; |
221 virtual void Unregister(const base::Closure& callback, | 160 virtual void Unregister(const base::Closure& callback, |
222 const ErrorCallback& error_callback) = 0; | 161 const ErrorCallback& error_callback) = 0; |
223 | 162 |
224 protected: | 163 protected: |
225 BluetoothGattService(); | 164 BluetoothLocalGattService(); |
226 | 165 |
227 private: | 166 private: |
228 DISALLOW_COPY_AND_ASSIGN(BluetoothGattService); | 167 DISALLOW_COPY_AND_ASSIGN(BluetoothLocalGattService); |
229 }; | 168 }; |
230 | 169 |
231 } // namespace device | 170 } // namespace device |
232 | 171 |
233 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_ | 172 #endif // DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_ |
OLD | NEW |