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 DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "device/bluetooth/bluetooth_uuid.h"
16 class BluetoothGattCharacteristic
;
17 class BluetoothGattDescriptor
;
19 // BluetoothGattService represents a local or remote GATT service. A GATT
20 // service is hosted by a peripheral and represents a collection of data in
21 // the form of GATT characteristics and a set of included GATT services if this
22 // service is what is called "a primary service".
24 // Instances of the BluetoothGattService class are used for two functions:
25 // 1. To represent GATT attribute hierarchies that have been received from a
26 // remote Bluetooth GATT peripheral. Such BluetoothGattService instances
27 // are constructed and owned by a BluetoothDevice.
29 // 2. To represent a locally hosted GATT attribute hierarchy when the local
30 // adapter is used in the "peripheral" role. Such instances are meant to be
31 // constructed directly and registered. Once registered, a GATT attribute
32 // hierarchy will be visible to remote devices in the "central" role.
33 class BluetoothGattService
{
35 // The Delegate class is used to send certain events that need to be handled
36 // when the device is in peripheral mode. The delegate handles read and write
37 // requests that are issued from remote clients.
40 // Callbacks used for communicating GATT request responses.
41 typedef base::Callback
<void(const std::vector
<uint8
>)> ValueCallback
;
42 typedef base::Closure ErrorCallback
;
44 // Called when a remote device in the central role requests to read the
45 // value of the characteristic |characteristic| starting at offset |offset|.
46 // This method is only called if the characteristic was specified as
47 // readable and any authentication and authorization challanges were
48 // satisfied by the remote device.
50 // To respond to the request with success and return the requested value,
51 // the delegate must invoke |callback| with the value. Doing so will
52 // automatically update the value property of |characteristic|. To respond
53 // to the request with failure (e.g. if an invalid offset was given),
54 // delegates must invoke |error_callback|. If neither callback parameter is
55 // invoked, the request will time out and result in an error. Therefore,
56 // delegates MUST invoke either |callback| or |error_callback|.
57 virtual void OnCharacteristicReadRequest(
58 const BluetoothGattService
* service
,
59 const BluetoothGattCharacteristic
* characteristic
,
61 const ValueCallback
& callback
,
62 const ErrorCallback
& error_callback
) = 0;
64 // Called when a remote device in the central role requests to write the
65 // value of the characteristic |characteristic| starting at offset |offset|.
66 // This method is only called if the characteristic was specified as
67 // writeable and any authentication and authorization challanges were
68 // satisfied by the remote device.
70 // To respond to the request with success the delegate must invoke
71 // |callback| with the new value of the characteristic. Doing so will
72 // automatically update the value property of |characteristic|. To respond
73 // to the request with failure (e.g. if an invalid offset was given),
74 // delegates must invoke |error_callback|. If neither callback parameter is
75 // invoked, the request will time out and result in an error. Therefore,
76 // delegates MUST invoke either |callback| or |error_callback|.
77 virtual void OnCharacteristicWriteRequest(
78 const BluetoothGattService
* service
,
79 const BluetoothGattCharacteristic
* characteristic
,
80 const std::vector
<uint8
>& value
,
82 const ValueCallback
& callback
,
83 const ErrorCallback
& error_callback
) = 0;
85 // Called when a remote device in the central role requests to read the
86 // value of the descriptor |descriptor| starting at offset |offset|.
87 // This method is only called if the characteristic was specified as
88 // readable and any authentication and authorization challanges were
89 // satisfied by the remote device.
91 // To respond to the request with success and return the requested value,
92 // the delegate must invoke |callback| with the value. Doing so will
93 // automatically update the value property of |descriptor|. To respond
94 // to the request with failure (e.g. if an invalid offset was given),
95 // delegates must invoke |error_callback|. If neither callback parameter is
96 // invoked, the request will time out and result in an error. Therefore,
97 // delegates MUST invoke either |callback| or |error_callback|.
98 virtual void OnDescriptorReadRequest(
99 const BluetoothGattService
* service
,
100 const BluetoothGattDescriptor
* descriptor
,
102 const ValueCallback
& callback
,
103 const ErrorCallback
& error_callback
) = 0;
105 // Called when a remote device in the central role requests to write the
106 // value of the descriptor |descriptor| starting at offset |offset|.
107 // This method is only called if the characteristic was specified as
108 // writeable and any authentication and authorization challanges were
109 // satisfied by the remote device.
111 // To respond to the request with success the delegate must invoke
112 // |callback| with the new value of the descriptor. Doing so will
113 // automatically update the value property of |descriptor|. To respond
114 // to the request with failure (e.g. if an invalid offset was given),
115 // delegates must invoke |error_callback|. If neither callback parameter is
116 // invoked, the request will time out and result in an error. Therefore,
117 // delegates MUST invoke either |callback| or |error_callback|.
118 virtual void OnDescriptorWriteRequest(
119 const BluetoothGattService
* service
,
120 const BluetoothGattDescriptor
* descriptor
,
121 const std::vector
<uint8
>& value
,
123 const ValueCallback
& callback
,
124 const ErrorCallback
& error_callback
) = 0;
127 // Interface for observing changes from a BluetoothGattService. Properties
128 // of remote services are received asynchronously. The Observer interface can
129 // be used to be notified when the initial values of a service are received
130 // as well as when successive changes occur during its life cycle.
133 // Called when properties of the remote GATT service |service| have changed.
134 // This will get called for properties such as UUID, as well as for changes
135 // to the list of known characteristics and included services. Observers
136 // should read all GATT characteristic and descriptors objects and do any
137 // necessary set up required for a changed service. This method may be
138 // called several times, especially when the service is discovered for the
139 // first time. It will be called for each characteristic and characteristic
140 // descriptor that is discovered or removed. Hence this method should be
141 // used to check whether or not all characteristics of a service have been
142 // discovered that correspond to the profile implemented by the Observer.
143 virtual void GattServiceChanged(BluetoothGattService
* service
) {}
145 // Called when the remote GATT characteristic |characteristic| belonging to
146 // GATT service |service| has been discovered. Use this to issue any initial
147 // read/write requests to the characteristic but don't cache the pointer as
148 // it may become invalid. Instead, use the specially assigned identifier
149 // to obtain a characteristic and cache that identifier as necessary, as it
150 // can be used to retrieve the characteristic from its GATT service. The
151 // number of characteristics with the same UUID belonging to a service
152 // depends on the particular profile the remote device implements, hence the
153 // client of a GATT based profile will usually operate on the whole set of
154 // characteristics and not just one.
156 // This method will always be followed by a call to GattServiceChanged,
157 // which can be used by observers to get all the characteristics of a
158 // service and perform the necessary updates. GattCharacteristicAdded exists
159 // mostly for convenience.
160 virtual void GattCharacteristicAdded(
161 BluetoothGattService
* service
,
162 BluetoothGattCharacteristic
* characteristic
) {}
164 // Called when a GATT characteristic |characteristic| belonging to GATT
165 // service |service| has been removed. This method is for convenience
166 // and will be followed by a call to GattServiceChanged (except when called
167 // after the service gets removed) which should be used for bootstrapping a
168 // GATT based profile. See the documentation of GattCharacteristicAdded and
169 // GattServiceChanged for more information. Try to obtain the service from
170 // its device to see whether or not the service has been removed.
171 virtual void GattCharacteristicRemoved(
172 BluetoothGattService
* service
,
173 BluetoothGattCharacteristic
* characteristic
) {}
175 // Called when the value of a characteristic has changed. This might be a
176 // result of a read/write request to, or a notification/indication from, a
177 // remote GATT characteristic.
178 virtual void GattCharacteristicValueChanged(
179 BluetoothGattService
* service
,
180 BluetoothGattCharacteristic
* characteristic
,
181 const std::vector
<uint8
>& value
) {}
184 // The ErrorCallback is used by methods to asynchronously report errors.
185 typedef base::Closure ErrorCallback
;
187 virtual ~BluetoothGattService();
189 // Adds and removes observers for events on this GATT service. If monitoring
190 // multiple services, check the |service| parameter of observer methods to
191 // determine which service is issuing the event.
192 virtual void AddObserver(Observer
* observer
) = 0;
193 virtual void RemoveObserver(Observer
* observer
) = 0;
195 // Constructs a BluetoothGattService that can be locally hosted when the local
196 // adapter is in the peripheral role. The resulting object can then be made
197 // available by calling the "Register" method. This method constructs a
198 // service with UUID |uuid|. Whether the constructed service is primary or
199 // secondary is determined by |is_primary|. |delegate| is used to send certain
200 // peripheral role events. If |delegate| is NULL, then this service will
201 // employ a default behavior when responding to read and write requests based
202 // on the cached value of its characteristics and descriptors at a given time.
203 static BluetoothGattService
* Create(const BluetoothUUID
& uuid
,
207 // Identifier used to uniquely identify a GATT service object. This is
208 // different from the service UUID: while multiple services with the same UUID
209 // can exist on a Bluetooth device, the identifier returned from this method
210 // is unique among all services of a device. The contents of the identifier
211 // are platform specific.
212 virtual std::string
GetIdentifier() const = 0;
214 // The Bluetooth-specific UUID of the service.
215 virtual BluetoothUUID
GetUUID() const = 0;
217 // Returns true, if this service hosted locally. If false, then this service
218 // represents a remote GATT service.
219 virtual bool IsLocal() const = 0;
221 // Indicates whether the type of this service is primary or secondary. A
222 // primary service describes the primary function of the peripheral that
223 // hosts it, while a secondary service only makes sense in the presence of a
224 // primary service. A primary service may include other primary or secondary
226 virtual bool IsPrimary() const = 0;
228 // List of characteristics that belong to this service.
229 virtual std::vector
<BluetoothGattCharacteristic
*>
230 GetCharacteristics() const = 0;
232 // List of GATT services that are included by this service.
233 virtual std::vector
<BluetoothGattService
*>
234 GetIncludedServices() const = 0;
236 // Returns the GATT characteristic with identifier |identifier| if it belongs
237 // to this GATT service.
238 virtual BluetoothGattCharacteristic
* GetCharacteristic(
239 const std::string
& identifier
) = 0;
241 // Adds characteristics and included services to the local attribute hierarchy
242 // represented by this service. These methods only make sense for local
243 // services and won't have an effect if this instance represents a remote
244 // GATT service and will return false. While ownership of added
245 // characteristics are taken over by the service, ownership of an included
246 // service is not taken.
247 virtual bool AddCharacteristic(
248 BluetoothGattCharacteristic
* characteristic
) = 0;
249 virtual bool AddIncludedService(BluetoothGattService
* service
) = 0;
251 // Registers this GATT service. Calling Register will make this service and
252 // all of its associated attributes available on the local adapters GATT
253 // database and the service UUID will be advertised to nearby devices if the
254 // local adapter is discoverable. Call Unregister to make this service no
257 // These methods only make sense for services that are local and will hence
258 // fail if this instance represents a remote GATT service. |callback| is
259 // called to denote success and |error_callback| to denote failure.
260 virtual void Register(const base::Closure
& callback
,
261 const ErrorCallback
& error_callback
) = 0;
262 virtual void Unregister(const base::Closure
& callback
,
263 const ErrorCallback
& error_callback
) = 0;
266 BluetoothGattService();
269 DISALLOW_COPY_AND_ASSIGN(BluetoothGattService
);
272 } // namespace device
274 #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_SERVICE_H_