blob: dc8b61a9b5c2c5dcdea6b939a96c8789ec457847 [file] [log] [blame]
rkc122239752016-04-20 23:59:081// Copyright 2016 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.
4
5#ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_
6#define DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_
7
8#include <stdint.h>
9#include <vector>
10
11#include "base/callback.h"
12#include "base/callback_forward.h"
13#include "base/macros.h"
rkcf27a4992016-04-21 18:22:1314#include "base/memory/weak_ptr.h"
15#include "device/bluetooth/bluetooth_adapter.h"
rkc122239752016-04-20 23:59:0816#include "device/bluetooth/bluetooth_export.h"
17#include "device/bluetooth/bluetooth_gatt_service.h"
18#include "device/bluetooth/bluetooth_uuid.h"
19
20namespace device {
21
22class BluetoothLocalGattCharacteristic;
23class BluetoothLocalGattDescriptor;
24
25// BluetoothLocalGattService represents a local GATT service.
26//
27// Instances of the BluetoothLocalGattService class are used to represent a
28// locally hosted GATT attribute hierarchy when the local
29// adapter is used in the "peripheral" role. Such instances are meant to be
30// constructed directly and registered. Once registered, a GATT attribute
31// hierarchy will be visible to remote devices in the "central" role.
rkcf27a4992016-04-21 18:22:1332// BT local GATT services will be owned by the adapter they are created with.
rkc122239752016-04-20 23:59:0833//
34// Note: We use virtual inheritance on the gatt service since it will be
35// inherited by platform specific versions of the gatt service classes also. The
36// platform specific local gatt service classes will inherit both this class and
37// their gatt service class, hence causing an inheritance diamond.
38class DEVICE_BLUETOOTH_EXPORT BluetoothLocalGattService
39 : public virtual BluetoothGattService {
40 public:
41 // The Delegate class is used to send certain events that need to be handled
42 // when the device is in peripheral mode. The delegate handles read and write
43 // requests that are issued from remote clients.
44 class Delegate {
45 public:
46 // Callbacks used for communicating GATT request responses.
rkca65ced972016-04-28 06:56:3847 typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback;
rkc122239752016-04-20 23:59:0848 typedef base::Closure ErrorCallback;
49
rkcd4ba1a52016-05-17 22:42:3450 // Called when a remote device |device| requests to read the value of the
51 // characteristic |characteristic| starting at offset |offset|.
rkc122239752016-04-20 23:59:0852 // This method is only called if the characteristic was specified as
53 // readable and any authentication and authorization challenges were
54 // satisfied by the remote device.
55 //
56 // To respond to the request with success and return the requested value,
57 // the delegate must invoke |callback| with the value. Doing so will
58 // automatically update the value property of |characteristic|. To respond
59 // to the request with failure (e.g. if an invalid offset was given),
60 // delegates must invoke |error_callback|. If neither callback parameter is
61 // invoked, the request will time out and result in an error. Therefore,
62 // delegates MUST invoke either |callback| or |error_callback|.
63 virtual void OnCharacteristicReadRequest(
rkcd4ba1a52016-05-17 22:42:3464 const BluetoothDevice* device,
rkc122239752016-04-20 23:59:0865 const BluetoothLocalGattCharacteristic* characteristic,
66 int offset,
67 const ValueCallback& callback,
68 const ErrorCallback& error_callback) = 0;
69
rkcd4ba1a52016-05-17 22:42:3470 // Called when a remote device |device| requests to write the value of the
71 // characteristic |characteristic| starting at offset |offset|.
rkc122239752016-04-20 23:59:0872 // This method is only called if the characteristic was specified as
73 // writable and any authentication and authorization challenges were
74 // satisfied by the remote device.
75 //
76 // To respond to the request with success the delegate must invoke
rkca65ced972016-04-28 06:56:3877 // |callback|. To respond to the request with failure delegates must invoke
78 // |error_callback|. If neither callback parameter is invoked, the request
79 // will time out and result in an error. Therefore, delegates MUST invoke
80 // either |callback| or |error_callback|.
rkc122239752016-04-20 23:59:0881 virtual void OnCharacteristicWriteRequest(
rkcd4ba1a52016-05-17 22:42:3482 const BluetoothDevice* device,
rkc122239752016-04-20 23:59:0883 const BluetoothLocalGattCharacteristic* characteristic,
84 const std::vector<uint8_t>& value,
85 int offset,
rkca65ced972016-04-28 06:56:3886 const base::Closure& callback,
rkc122239752016-04-20 23:59:0887 const ErrorCallback& error_callback) = 0;
88
rkcd4ba1a52016-05-17 22:42:3489 // Called when a remote device |device| requests to read the value of the
90 // descriptor |descriptor| starting at offset |offset|.
91 // This method is only called if the descriptor was specified as
rkc122239752016-04-20 23:59:0892 // readable and any authentication and authorization challenges were
93 // satisfied by the remote device.
94 //
95 // To respond to the request with success and return the requested value,
96 // the delegate must invoke |callback| with the value. Doing so will
97 // automatically update the value property of |descriptor|. To respond
98 // to the request with failure (e.g. if an invalid offset was given),
99 // delegates must invoke |error_callback|. If neither callback parameter is
100 // invoked, the request will time out and result in an error. Therefore,
101 // delegates MUST invoke either |callback| or |error_callback|.
102 virtual void OnDescriptorReadRequest(
rkcd4ba1a52016-05-17 22:42:34103 const BluetoothDevice* device,
rkc122239752016-04-20 23:59:08104 const BluetoothLocalGattDescriptor* descriptor,
105 int offset,
106 const ValueCallback& callback,
107 const ErrorCallback& error_callback) = 0;
108
rkcd4ba1a52016-05-17 22:42:34109 // Called when a remote device |devie| requests to write the value of the
110 // descriptor |descriptor| starting at offset |offset|.
111 // This method is only called if the descriptor was specified as
rkc122239752016-04-20 23:59:08112 // writable and any authentication and authorization challenges were
113 // satisfied by the remote device.
114 //
115 // To respond to the request with success the delegate must invoke
rkca65ced972016-04-28 06:56:38116 // |callback|. To respond to the request with failure delegates must invoke
117 // |error_callback|. If neither callback parameter is invoked, the request
118 // will time out and result in an error. Therefore, delegates MUST invoke
119 // either |callback| or |error_callback|.
rkc122239752016-04-20 23:59:08120 virtual void OnDescriptorWriteRequest(
rkcd4ba1a52016-05-17 22:42:34121 const BluetoothDevice* device,
rkc122239752016-04-20 23:59:08122 const BluetoothLocalGattDescriptor* descriptor,
123 const std::vector<uint8_t>& value,
124 int offset,
rkca65ced972016-04-28 06:56:38125 const base::Closure& callback,
rkc122239752016-04-20 23:59:08126 const ErrorCallback& error_callback) = 0;
rkcd1e6dc42016-05-12 23:12:47127
rkcd4ba1a52016-05-17 22:42:34128 // Called when a remote device |device| requests notifications to start for
rkcd1e6dc42016-05-12 23:12:47129 // |characteristic|. This is only called if the characteristic has
130 // specified the notify or indicate property.
131 virtual void OnNotificationsStart(
rkcd4ba1a52016-05-17 22:42:34132 const BluetoothDevice* device,
rkcd1e6dc42016-05-12 23:12:47133 const BluetoothLocalGattCharacteristic* characteristic) = 0;
134
rkcd4ba1a52016-05-17 22:42:34135 // Called when a remote device |device| requests notifications to stop for
rkcd1e6dc42016-05-12 23:12:47136 // |characteristic|. This is only called if the characteristic has
137 // specified the notify or indicate property.
138 virtual void OnNotificationsStop(
rkcd4ba1a52016-05-17 22:42:34139 const BluetoothDevice* device,
rkcd1e6dc42016-05-12 23:12:47140 const BluetoothLocalGattCharacteristic* characteristic) = 0;
rkc122239752016-04-20 23:59:08141 };
142
rkcf27a4992016-04-21 18:22:13143 // Creates a local GATT service to be used with |adapter| (which will own
144 // the created service object). A service can register or unregister itself
145 // at any time by calling its Register/Unregister methods. |delegate|
146 // receives read/write requests for characteristic/descriptor values. It
147 // needs to outlive this object.
rkc122239752016-04-20 23:59:08148 // TODO(rkc): Implement included services.
rkcf27a4992016-04-21 18:22:13149 static base::WeakPtr<BluetoothLocalGattService> Create(
150 BluetoothAdapter* adapter,
rkc122239752016-04-20 23:59:08151 const BluetoothUUID& uuid,
152 bool is_primary,
153 BluetoothLocalGattService* included_service,
rkcf27a4992016-04-21 18:22:13154 BluetoothLocalGattService::Delegate* delegate);
rkc122239752016-04-20 23:59:08155
156 // Registers this GATT service. Calling Register will make this service and
157 // all of its associated attributes available on the local adapters GATT
rkcf27a4992016-04-21 18:22:13158 // database. Call Unregister to make this service no longer available.
rkc122239752016-04-20 23:59:08159 virtual void Register(const base::Closure& callback,
160 const ErrorCallback& error_callback) = 0;
rkcf27a4992016-04-21 18:22:13161
162 // Unregisters this GATT service. This will remove the service from the list
163 // of services exposed by the adapter this service was registered on.
rkc122239752016-04-20 23:59:08164 virtual void Unregister(const base::Closure& callback,
165 const ErrorCallback& error_callback) = 0;
166
rkc5ca6c3f2016-05-10 03:50:57167 // Returns if this service is currently registered.
168 virtual bool IsRegistered() = 0;
169
170 // Deletes this service, invaliding the weak pointer returned by create and
171 // unregistering the service if it was registered.
172 virtual void Delete() = 0;
173
rkc5916b89a2016-05-04 07:42:09174 virtual BluetoothLocalGattCharacteristic* GetCharacteristic(
175 const std::string& identifier) = 0;
176
rkc122239752016-04-20 23:59:08177 protected:
178 BluetoothLocalGattService();
rkcf27a4992016-04-21 18:22:13179 ~BluetoothLocalGattService() override;
rkc122239752016-04-20 23:59:08180
181 private:
182 DISALLOW_COPY_AND_ASSIGN(BluetoothLocalGattService);
183};
184
185} // namespace device
186
187#endif // DEVICE_BLUETOOTH_BLUETOOTH_LOCAL_GATT_SERVICE_H_