• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
43 namespace OHOS {
44 namespace Bluetooth {
45 /**
46  * @brief Class for Gatt Server callback functions.
47  *
48  * @since 6
49  *
50  */
51 class GattServerCallback {
52 public:
53     /**
54      * @brief The callback function to notify connection state update.
55      *
56      * @param device Remote device object.
57      * @param state Connection state.
58      * @since 6
59      *
60      */
61     virtual void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) = 0;
62 
63     /**
64      * @brief The callback function to notify service add.
65      *
66      * @param Service Added service object.
67      * @param ret Result of service add.
68      * @since 6
69      *
70      */
OnServiceAdded(GattService * Service,int ret)71     virtual void OnServiceAdded(GattService *Service, int ret)
72     {}
73 
74     /**
75      * @brief The callback function to notify characteristic read request.
76      *
77      * @param device Remote device object.
78      * @param characteristic Characteristic object.
79      * @param requestId Result of request.
80      * @since 6
81      *
82      */
OnCharacteristicReadRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)83     virtual void OnCharacteristicReadRequest(
84         const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId)
85     {}
86 
87     /**
88      * @brief The callback function to notify characteristic write request.
89      *
90      * @param device Remote device object.
91      * @param characteristic Characteristic object.
92      * @param requestId Result of request.
93      * @since 6
94      *
95      */
OnCharacteristicWriteRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)96     virtual void OnCharacteristicWriteRequest(
97         const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId)
98     {}
99 
100     /**
101      * @brief The callback function to notify descriptor read request.
102      *
103      * @param device Remote device object.
104      * @param characteristic Characteristic object.
105      * @param requestId Result of request.
106      * @since 6
107      *
108      */
OnDescriptorReadRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)109     virtual void OnDescriptorReadRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId)
110     {}
111 
112     /**
113      * @brief The callback function to notify descriptor write request.
114      *
115      * @param device Remote device object.
116      * @param characteristic Characteristic object.
117      * @param requestId Result of request.
118      * @since 6
119      *
120      */
OnDescriptorWriteRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)121     virtual void OnDescriptorWriteRequest(
122         const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId)
123     {}
124 
125     /**
126      * @brief The callback function to notify mtu update.
127      *
128      * @param device Remote device object.
129      * @param mtu Current mtu.
130      * @since 6
131      *
132      */
OnMtuUpdate(const BluetoothRemoteDevice & device,int mtu)133     virtual void OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu)
134     {}
135     /**
136      * @brief The callback function to notify characteristic changed.
137      *
138      * @param device Remote device object.
139      * @since 6
140      *
141      */
OnNotificationCharacteristicChanged(const BluetoothRemoteDevice & device,int result)142     virtual void OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int result)
143     {}
144     /**
145      * @brief The callback function to notify connection parameter changed
146      *
147      * @param device Remote device object.
148      * @param interval Interval object.
149      * @param latency Latency object.
150      * @param timeout Timeout object.
151      * @param status Status object.
152      * @since 6
153      *
154      */
OnConnectionParameterChanged(const BluetoothRemoteDevice & device,int interval,int latency,int timeout,int status)155     virtual void OnConnectionParameterChanged(
156         const BluetoothRemoteDevice &device, int interval, int latency, int timeout, int status)
157     {}
158 
159     /**
160      * @brief A destructor of GattServerCallback.
161      *
162      * @since 6
163      *
164      */
~GattServerCallback()165     virtual ~GattServerCallback()
166     {}
167 };
168 
169 /**
170  * @brief Class for Gatt Server API.
171  *
172  * @since 6
173  *
174  */
175 class BLUETOOTH_API GattServer {
176 public:
177     /**
178      * @brief A constructor of GattServerCallback.
179      *
180      * @param device GattServerCallback callback object.
181      * @since 6
182      *
183      */
184     explicit GattServer(GattServerCallback &callback);
185     /**
186      * @brief The function to add service.
187      *
188      * @param service Service object to add.
189      * @return int    api accept status.
190      * @since 6
191      *
192      */
193     int AddService(GattService &service);
194     /**
195      * @brief The function to remove service.
196      *
197      * @param service Service object to remove.
198      * @return int    api accept status.
199      * @since 6
200      *
201      */
202     int RemoveGattService(const GattService &service);
203     /**
204      * @brief The function to clear all services.
205      *
206      * @since 6
207      *
208      */
209     void ClearServices();
210     /**
211      * @brief The function to clear all services.
212      *
213      * @return int.
214      * @since 6
215      *
216      */
217     int Close();
218     /**
219      * @brief The function to get service by UUID.
220      *
221      * @param uuid UUID of service.
222      * @param isPrimary Type of service.
223      * @return service.
224      * @since 6
225      *
226      */
227     std::optional<std::reference_wrapper<GattService>> GetService(const UUID &uuid, bool isPrimary);
228     /**
229      * @brief The function to get all services.
230      *
231      * @return list of services.
232      * @since 6
233      *
234      */
235     std::list<GattService> &GetServices();
236     /**
237      * @brief The function to notify characteristic change.
238      *
239      * @param device Remote device object.
240      * @param characteristic Characteristic object.
241      * @param confirm Confirm the change.
242      * @return int    api accept status.
243      * @since 6
244      *
245      */
246     int NotifyCharacteristicChanged(
247         const BluetoothRemoteDevice &device, const GattCharacteristic &characteristic, bool confirm);
248     /**
249      * @brief The function to send responce.
250      *
251      * @param device Remote device object.
252      * @param requestId Result of the request.
253      * @param status Current status.
254      * @param offset Offset object.
255      * @param value Value object.
256      * @param length Length of value.
257      * @return int   api accept status.
258      * @since 6
259      *
260      */
261     int SendResponse(
262         const BluetoothRemoteDevice &device, int requestId, int status, int offset, const uint8_t *value, int length);
263 
264     /**
265      * @brief The function to cancel connection.
266      *
267      * @param device Remote device object.
268      * @since 6
269      *
270      */
271     void CancelConnection(const BluetoothRemoteDevice &device);
272 
273     /**
274      * @brief A destructor of GattServer.
275      *
276      * @since 6
277      *
278      */
279     ~GattServer();
280     /**
281      * @brief The function to delete constructor of GattServer.
282      *
283      * @since 6
284      *
285      */
286     GattServer() = delete;
287 
288     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(GattServer);
289 
290 private:
291     BLUETOOTH_DECLARE_IMPL();
292 };
293 } // namespace Bluetooth
294 } // namespace OHOS
295 #endif  // BLUETOOTH_GATT_SERVER_H
296