1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines a bluetooth system that provides basic bluetooth connection and profile functions,
21  *        including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc.
22  *
23  * @since 6
24  *
25  */
26 
27 /**
28  * @file bluetooth_gatt_server.h
29  *
30  * @brief gatt server interface.
31  *
32  * @since 6
33  *
34  */
35 
36 #ifndef BLUETOOTH_GATT_SERVER_H
37 #define BLUETOOTH_GATT_SERVER_H
38 
39 #include "bluetooth_def.h"
40 #include "bluetooth_gatt_service.h"
41 #include "bluetooth_remote_device.h"
42 #include <memory>
43 
44 namespace OHOS {
45 namespace Bluetooth {
46 /**
47  * @brief Class for Gatt Server callback functions.
48  *
49  * @since 6
50  *
51  */
52 class GattServerCallback {
53 public:
54     /**
55      * @brief The callback function to notify connection state update.
56      *
57      * @param device Remote device object.
58      * @param state Connection state.
59      * @since 6
60      *
61      */
62     virtual void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) = 0;
63 
64     /**
65      * @brief The callback function to notify service add.
66      *
67      * @param Service Added service object.
68      * @param ret Result of service add.
69      * @since 6
70      *
71      */
OnServiceAdded(GattService service,int ret)72     virtual void OnServiceAdded(GattService service, int ret)
73     {}
74 
75     /**
76      * @brief The callback function to notify characteristic read request.
77      *
78      * @param device Remote device object.
79      * @param characteristic Characteristic object.
80      * @param requestId Result of request.
81      * @since 6
82      *
83      */
OnCharacteristicReadRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)84     virtual void OnCharacteristicReadRequest(
85         const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId)
86     {}
87 
88     /**
89      * @brief The callback function to notify characteristic write request.
90      *
91      * @param device Remote device object.
92      * @param characteristic Characteristic object.
93      * @param requestId Result of request.
94      * @since 6
95      *
96      */
OnCharacteristicWriteRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)97     virtual void OnCharacteristicWriteRequest(
98         const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId)
99     {}
100 
101     /**
102      * @brief The callback function to notify descriptor read request.
103      *
104      * @param device Remote device object.
105      * @param characteristic Characteristic object.
106      * @param requestId Result of request.
107      * @since 6
108      *
109      */
OnDescriptorReadRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)110     virtual void OnDescriptorReadRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId)
111     {}
112 
113     /**
114      * @brief The callback function to notify descriptor write request.
115      *
116      * @param device Remote device object.
117      * @param characteristic Characteristic object.
118      * @param requestId Result of request.
119      * @since 6
120      *
121      */
OnDescriptorWriteRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)122     virtual void OnDescriptorWriteRequest(
123         const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId)
124     {}
125 
126     /**
127      * @brief The callback function to notify mtu update.
128      *
129      * @param device Remote device object.
130      * @param mtu Current mtu.
131      * @since 6
132      *
133      */
OnMtuUpdate(const BluetoothRemoteDevice & device,int mtu)134     virtual void OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu)
135     {}
136     /**
137      * @brief The callback function to notify characteristic changed.
138      *
139      * @param device Remote device object.
140      * @since 6
141      *
142      */
OnNotificationCharacteristicChanged(const BluetoothRemoteDevice & device,int result)143     virtual void OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int result)
144     {}
145     /**
146      * @brief The callback function to notify connection parameter changed
147      *
148      * @param device Remote device object.
149      * @param interval Interval object.
150      * @param latency Latency object.
151      * @param timeout Timeout object.
152      * @param status Status object.
153      * @since 6
154      *
155      */
OnConnectionParameterChanged(const BluetoothRemoteDevice & device,int interval,int latency,int timeout,int status)156     virtual void OnConnectionParameterChanged(
157         const BluetoothRemoteDevice &device, int interval, int latency, int timeout, int status)
158     {}
159 
160     /**
161      * @brief A destructor of GattServerCallback.
162      *
163      * @since 6
164      *
165      */
~GattServerCallback()166     virtual ~GattServerCallback()
167     {}
168 };
169 
170 /**
171  * @brief Class for Gatt Server API.
172  *
173  * @since 6
174  *
175  */
176 class BLUETOOTH_API GattServer : public std::enable_shared_from_this<GattServer> {
177 public:
178     /**
179      * @brief A constructor of GattServerCallback.
180      *
181      * @param device GattServerCallback callback object.
182      * @since 6
183      *
184      */
185     static std::shared_ptr<GattServer> CreateInstance(std::shared_ptr<GattServerCallback> callback);
186     /**
187      * @brief The function to add service.
188      *
189      * @param service Service object to add.
190      * @return int    api accept status.
191      * @since 6
192      *
193      */
194     int AddService(GattService &service);
195     /**
196      * @brief The function to remove service.
197      *
198      * @param service Service object to remove.
199      * @return int    api accept status.
200      * @since 6
201      *
202      */
203     int RemoveGattService(const GattService &service);
204     /**
205      * @brief The function to clear all services.
206      *
207      * @since 6
208      *
209      */
210     void ClearServices();
211     /**
212      * @brief The function to clear all services.
213      *
214      * @return int.
215      * @since 6
216      *
217      */
218     int Close();
219     /**
220      * @brief The function to get service by UUID.
221      *
222      * @param uuid UUID of service.
223      * @param isPrimary Type of service.
224      * @return service.
225      * @since 6
226      *
227      */
228     std::optional<std::reference_wrapper<GattService>> GetService(const UUID &uuid, bool isPrimary);
229     /**
230      * @brief The function to get all services.
231      *
232      * @return list of services.
233      * @since 6
234      *
235      */
236     std::list<GattService> &GetServices();
237     /**
238      * @brief The function to notify characteristic change.
239      *
240      * @param device Remote device object.
241      * @param characteristic Characteristic object.
242      * @param confirm Confirm the change.
243      * @return int    api accept status.
244      * @since 6
245      *
246      */
247     int NotifyCharacteristicChanged(
248         const BluetoothRemoteDevice &device, const GattCharacteristic &characteristic, bool confirm);
249     /**
250      * @brief The function to send responce.
251      *
252      * @param device Remote device object.
253      * @param requestId Result of the request.
254      * @param status Current status.
255      * @param offset Offset object.
256      * @param value Value object.
257      * @param length Length of value.
258      * @return int   api accept status.
259      * @since 6
260      *
261      */
262     int SendResponse(
263         const BluetoothRemoteDevice &device, int requestId, int status, int offset, const uint8_t *value, int length);
264 
265     /**
266      * @brief The function to connect client device.
267      *
268      * @param device Remote device object.
269      * @param isDirect Whether to directly connect to the remote device (true)
270      *                 or to automatically connect as soon as the remote device becomes available (false)
271      * @return int   api accept status.
272      * @since 6
273      *
274      */
275     int Connect(const BluetoothRemoteDevice &device, bool isDirect);
276 
277     /**
278      * @brief The function to cancel connection.
279      *
280      * @param device Remote device object.
281      * @return int   api accept status.
282      * @since 6
283      *
284      */
285     int CancelConnection(const BluetoothRemoteDevice &device);
286 
287     /**
288      * @brief A destructor of GattServer.
289      *
290      * @since 6
291      *
292      */
293     ~GattServer();
294     /**
295      * @brief The function to delete constructor of GattServer.
296      *
297      * @since 6
298      *
299      */
300     GattServer() = delete;
301 
302     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(GattServer);
303 
304 private:
305     explicit GattServer(std::shared_ptr<GattServerCallback> callback);
306     BLUETOOTH_DECLARE_IMPL();
307 
308     // The passkey pattern of C++
309     struct PassKey {
PassKeyPassKey310         PassKey() {};
311     };
312 public:
GattServer(PassKey,std::shared_ptr<GattServerCallback> callback)313     explicit GattServer(PassKey, std::shared_ptr<GattServerCallback> callback) : GattServer(callback) {};
314 
315 };
316 } // namespace Bluetooth
317 } // namespace OHOS
318 #endif  // BLUETOOTH_GATT_SERVER_H
319