• 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 #ifndef OHOS_BT_GATT_SERVER_H
17 #define OHOS_BT_GATT_SERVER_H
18 
19 #include "ohos_bt_def.h"
20 
21 typedef enum {
22     OHOS_BLE_SEC_NONE = 0x00,
23     OHOS_BLE_SEC_ENCRYPT,
24     OHOS_BLE_SEC_ENCRYPT_NO_MITM,
25     OHOS_BLE_SEC_ENCRYPT_MITM
26 } BleSecAct;
27 
28 typedef struct {
29     int connectId; /* connection index */
30     int status; /* read/write character status, GattStatus */
31     int attrHandle; /* attribute handle */
32     int valueLen;
33     char *value; /* response data from host */
34 } GattsSendRspParam;
35 
36 typedef struct {
37     int connectId; /* connection index */
38     int attrHandle; /* attribute handle */
39     int confirm; /* true(indication)-the client shall respond a conformation, false(notification)-send a notification */
40     int valueLen; /* response data from host */
41     char *value;
42 } GattsSendIndParam;
43 
44 typedef struct {
45     int connId; /* connect index */
46     int transId; /* BtTransportId */
47     BdAddr *bdAddr;
48     int attrHandle; /* The handle of the attribute to be read */
49     int offset; /* The offset of the first octet to be read */
50     bool isLong; /* If isLong is true, then this request is part of a Long Read procedure */
51 } BtReqReadCbPara;
52 
53 typedef struct {
54     int connId; /* connect index */
55     int transId; /* BtTransportId */
56     BdAddr *bdAddr;
57     int attrHandle; /* The handle of the attribute to be written */
58     int offset; /* The offset of the first octet to be written */
59     int length;
60     bool needRsp; /* if the remote device requires a response */
61     bool isPrep; /* if this write operation should be queued for later execution */
62     unsigned char *value;
63 } BtReqWriteCbPara;
64 
65 /* Callback invoked in response to register_server */
66 typedef void (*RegisterServerCallback)(int status, int serverId, BtUuid *appUuid);
67 
68 /* Callback indicating that a remote device has connected */
69 typedef void (*ConnectServerCallback)(int connId, int serverId, const BdAddr *bdAddr);
70 
71 /* Callback indicating that a remote device has disconnected */
72 typedef void (*DisconnectServerCallback)(int connId, int serverId, const BdAddr *bdAddr);
73 
74 /* Callback invoked in response to add service */
75 typedef void (*ServiceAddCallback)(int status, int serverId, BtUuid *uuid, int srvcHandle);
76 
77 /* Callback indicating that an included service has been added to a service */
78 typedef void (*IncludeServiceAddCallback)(int status, int serverId, int srvcHandle, int includeSrvcHandle);
79 
80 /* Callback invoked when a characteristic has been added to a service */
81 typedef void (*CharacteristicAddCallback)(int status, int serverId, BtUuid *uuid,
82         int srvcHandle, int characteristicHandle);
83 
84 /* Callback invoked when a descriptor has been added to a characteristic */
85 typedef void (*DescriptorAddCallback)(int status, int serverId, BtUuid *uuid,
86                                       int srvcHandle, int descriptorHandle);
87 
88 /* Callback invoked in response to start_service */
89 typedef void (*ServiceStartCallback)(int status, int serverId, int srvcHandle);
90 
91 /* Callback invoked in response to stop_service */
92 typedef void (*ServiceStopCallback)(int status, int serverId, int srvcHandle);
93 
94 /* Callback triggered when a service has been deleted */
95 typedef void (*ServiceDeleteCallback)(int status, int serverId, int srvcHandle);
96 
97 /* Callback invoked when a remote device has requested to read a characteristicor descriptor.
98  * The application must respond by calling send_response
99  */
100 typedef void (*RequestReadCallback)(BtReqReadCbPara readCbPara);
101 
102 /* Callback invoked when a remote device has requested to write to a characteristic or descriptor. */
103 typedef void (*RequestWriteCallback)(BtReqWriteCbPara writeCbPara);
104 
105 /* Callback triggered in response to send_response if the remote device sends a confirmation */
106 typedef void (*ResponseConfirmationCallback)(int status, int handle);
107 
108 /* Callback confirming that a notification or indication has been sent to a remote device */
109 typedef void (*IndicationSentCallback)(int connId, int status);
110 
111 /* Callback invoked when the MTU for a given connection changes */
112 typedef void (*MtuChangeCallback)(int connId, int mtu);
113 
114 typedef struct {
115     RegisterServerCallback registerServerCb;
116     ConnectServerCallback connectServerCb;
117     DisconnectServerCallback disconnectServerCb;
118     ServiceAddCallback serviceAddCb;
119     IncludeServiceAddCallback includeServiceAddCb;
120     CharacteristicAddCallback characteristicAddCb;
121     DescriptorAddCallback descriptorAddCb;
122     ServiceStartCallback serviceStartCb;
123     ServiceStopCallback serviceStopCb;
124     ServiceDeleteCallback serviceDeleteCb;
125     RequestReadCallback requestReadCb;
126     RequestWriteCallback requestWriteCb;
127     ResponseConfirmationCallback responseConfirmationCb;
128     IndicationSentCallback indicationSentCb;
129     MtuChangeCallback mtuChangeCb;
130 } BtGattServerCallbacks;
131 
132 typedef int (*BleGattServiceRead)(unsigned char *buff, unsigned int *len);
133 
134 typedef int (*BleGattServiceWrite)(unsigned char *buff, unsigned int len);
135 
136 typedef int (*BleGattServiceIndicate)(unsigned char *buff, unsigned int len);
137 
138 typedef struct {
139     BleGattServiceRead read;
140     BleGattServiceWrite write;
141     BleGattServiceIndicate indicate;
142 } BleGattOperateFunc;
143 
144 typedef struct {
145     BleAttribType attrType;
146     unsigned int permission; /* e.g. (OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE) */
147     UuidType uuidType;
148     unsigned char uuid[OHOS_BLE_UUID_MAX_LEN];
149     unsigned char *value;
150     unsigned char valLen;
151     /* e.g. (OHOS_GATT_CHARACTER_PROPERTY_BIT_BROADCAST | OHOS_GATT_CHARACTER_PROPERTY_BIT_READ) */
152     unsigned char properties;
153     BleGattOperateFunc func;
154 } BleGattAttr;
155 
156 typedef struct {
157     unsigned int attrNum;
158     BleGattAttr *attrList;
159 } BleGattService;
160 
161 /*
162  * @brief gatt server application register, callback return serverId
163  * @param[in] <appUuid> specified by upper layer
164  * @return 0-success, other-fail
165  */
166 int BleGattsRegister(BtUuid appUuid);
167 
168 /*
169  * @brief gatt server deregister
170  * @param[in] <clientId> server interface Id
171  * @return 0-success, other-fail
172  */
173 int BleGattsUnRegister(int serverId);
174 
175 /*
176  * @brief Cancel connection with remote device
177  * @param[in] <serverId> server interface id
178  * @param[in] <bdAddr>   remote address
179  * @param[in] <connId>   connection index.
180  * @return 0-success, other-fail
181  */
182 int BleGattsDisconnect(int serverId, BdAddr bdAddr, int connId);
183 
184 /*
185  * @brief add service
186  * @param[in] <serverId>  server interface id
187  * @param[in] <srvcUuid>  service uuid and uuid length
188  * @param[in] <isPrimary> is primary or secondary service.
189  * @param[in] <number>    service characther attribute number.
190  * @return 0-success, other-fail
191  */
192 int BleGattsAddService(int serverId, BtUuid srvcUuid, bool isPrimary, int number);
193 
194 /*
195  * @brief add characteristic
196  * @param[in] <serverId>    server interface id
197  * @param[in] <srvcHandle>  service handle
198  * @param[in] <characUuid>  characteristic uuid and uuid length
199  * @param[in] <properties>  e.g. (OHOS_GATT_CHARACTER_PROPERTY_BIT_BROADCAST | OHOS_GATT_CHARACTER_PROPERTY_BIT_READ)
200  * @param[in] <permissions> e.g. (OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE)
201  * @return 0-success, other-fail
202  */
203 int BleGattsAddCharacteristic(int serverId, int srvcHandle, BtUuid characUuid,
204                               int properties, int permissions);
205 
206 /*
207  * @brief add descriptor
208  * @param[in] <serverId>    server interface id
209  * @param[in] <srvcHandle>  service handle
210  * @param[in] <descUuid>    descriptor uuid and uuid length
211  * @param[in] <permissions> e.g. (OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE)
212  * @return 0-success, other-fail
213  */
214 int BleGattsAddDescriptor(int serverId, int srvcHandle, BtUuid descUuid, int permissions);
215 
216 /*
217  * @brief start service
218  * @param[in] <serverId>    server interface id
219  * @param[in] <srvcHandle>  service handle
220  * @return 0-success, other-fail
221  */
222 int BleGattsStartService(int serverId, int srvcHandle);
223 
224 /*
225  * @brief start service
226  * @param[in] <serverId>    server interface id
227  * @param[in] <srvcHandle>  service handle
228  * @return 0-success, other-fail
229  */
230 int BleGattsStopService(int serverId, int srvcHandle);
231 
232 /*
233  * @brief remove a service from the list of provided services
234  * @param[in] <serverId>   server interface id
235  * @param[in] <srvcHandle>  service handle
236  * @return 0-success, other-fail
237  */
238 int BleGattsDeleteService(int serverId, int srvcHandle);
239 
240 /*
241  * @brief remove all services from the list of provided services
242  * @param[in] <serverId>   server interface id
243  * @return 0-success, other-fail
244  */
245 int BleGattsClearServices(int serverId);
246 
247 /*
248  * @brief Send a response to a read or write request to a remote device.
249  * @param[in] <serverId> server interface id
250  * @param[in] <GattsSendRspParam> response param
251  * @return 0-success, other-fail
252  */
253 int BleGattsSendResponse(int serverId, GattsSendRspParam *param);
254 
255 /*
256  * @brief Send a notification or indication that a local characteristic has been updated
257  * @param[in] <serverId> server interface id
258  * @param[in] <GattsSendIndParam> indication param
259  * @return 0-success, other-fail
260  */
261 int BleGattsSendIndication(int serverId, GattsSendIndParam *param);
262 
263 /*
264  * @brief Set the encryption level of the data transmission channel during connection
265  * @param[in] <bdAddr> remote address
266  * @param[in] <secAct> BleSecAct
267  * @return 0-success, other-fail
268  */
269 int BleGattsSetEncryption(BdAddr bdAddr, BleSecAct secAct);
270 
271 /*
272  * @brief Callback invoked for gatt server function
273  * @param[in] <BtGattServerCallbacks> Callback funcs
274  * @return 0-success, other-fail
275  */
276 int BleGattsRegisterCallbacks(BtGattServerCallbacks *func);
277 
278 /*
279  * @brief Start sevice include add service/characteristic/Descriptor option.
280  * This API will not described in the development manual, only for Hilink.
281  * @return 0-success, other-fail
282  */
283 int BleGattsStartServiceEx(int *srvcHandle, BleGattService *srvcInfo);
284 
285 /*
286  * @brief Stop service.
287  * This API will not described in the development manual, only for Hilink.
288  * @return 0-success, other-fail
289  */
290 int BleGattsStopServiceEx(int srvcHandle);
291 #endif
292