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