1 // Copyright (C) 2022 Beken Corporation
2 //
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 #include <string.h>
16 #include <stdio.h>
17 #include <stdbool.h>
18
19 #include "ohos_bt_gatt.h"
20 #include "ohos_bt_gatt_server.h"
21 #include "ohos_bt_def.h"
22
23 #include <common/bk_err.h>
24 #include <modules/ble.h>
25 #include <modules/ble_types.h>
26 #include "ble.h"
27 #include <components/system.h>
28 #include <os/mem.h>
29
30
31 struct scan_param
32 {
33 uint8_t filter_en; /**< The control of filter */
34 uint8_t channel_map; /**< Channel mapping */
35 uint16_t interval; /**< The scanning interval */
36 uint16_t window; /**< The scanning window */
37 };
38
39 struct adv_param
40 {
41 uint8_t advData[BK_BLE_MAX_ADV_DATA_LEN]; /**< Advertising data */
42 uint8_t advDataLen; /**< The length of advertising data */
43 uint8_t respData[BK_BLE_MAX_ADV_DATA_LEN]; /**< Scan response data */
44 uint8_t respDataLen; /**< The length of scan response data */
45 uint8_t channel_map; /**< Advertising channel map */
46 uint16_t interval_min; /**< Minimum advertising interval */
47 uint16_t interval_max; /**< Maximum advertising interval */
48 uint16_t duration; /**< Advertising duration */
49 };
50
51 //extern ble_err_t bk_ble_send_ntf_value(uint32_t len, uint8_t *buf, uint16_t prf_id, uint16_t att_idx);
52 //extern ble_err_t bk_ble_send_ind_value(uint32_t len, uint8_t *buf, uint16_t prf_id, uint16_t att_idx);
53 extern ble_err_t bk_ble_adv_start(uint8_t actv_idx, struct adv_param *adv, ble_cmd_cb_t callback);
54 extern ble_err_t bk_ble_adv_stop(uint8_t actv_idx, ble_cmd_cb_t callback);
55 extern ble_err_t bk_ble_scan_start(uint8_t actv_idx, struct scan_param *scan, ble_cmd_cb_t callback);
56 extern ble_err_t bk_ble_scan_stop(uint8_t actv_idx, ble_cmd_cb_t callback);
57
58
59 static BtGattCallbacks *gatt_callbackfunc=NULL;
60 static BtGattServerCallbacks * gatt_server_callbackfunc=NULL;
61
62 #define MAX_SERVER_NUM 5
63
64
65 enum
66 {
67 TEST_IDX_SVC,
68 TEST_IDX_FF01_VAL_CHAR,
69 TEST_IDX_FF01_VAL_VALUE,
70 TEST_IDX_FF02_VAL_CHAR,
71 TEST_IDX_FF02_VAL_VALUE,
72 TEST_IDX_FF02_VAL_IND_CFG,
73 TEST_IDX_FF03_VAL_CHAR,
74 TEST_IDX_FF03_VAL_VALUE,
75 TEST_IDX_FF03_VAL_NTF_CFG,
76 TEST_IDX_NB,
77 };
78
79 typedef struct{
80 unsigned char adv_idx;
81 unsigned char connidx;
82 unsigned char scan_idx;
83 unsigned char peer_addr[6];
84 unsigned char adv_type;
85 unsigned char advDataLen;
86 unsigned char respDataLen;
87 unsigned char advData[BK_BLE_MAX_ADV_DATA_LEN];
88 unsigned char respData[BK_BLE_MAX_ADV_DATA_LEN];
89
90 } BT_HAL_ENV_T;
91
92
93
94 typedef struct {
95 /** UUID length */
96 unsigned char uuidLen;
97 /** Server UUID ID**/
98 unsigned char server_id;
99 unsigned char max_att_handle;
100 unsigned char next_att_handle;
101 /** UUID field */
102 char *uuid;
103
104 ble_attm_desc_t *ohos_att_db;
105 } BtServer;
106
107
108 static BtServer *ohos_server_uuid_para[MAX_SERVER_NUM];
109 static BleGattOperateFunc g_ohos_Op_Func_cb[MAX_SERVER_NUM][2];
110 static BT_HAL_ENV_T hal_bt_env;
111 static struct scan_param ohos_scan_info;
112 static uint8_t svc_cnt =0;
113
114 enum
115 {
116 BK_Attr_IDX_SVC,
117 BK_Attr_IDX_CHAR,
118 BK_Attr_IDX_CFG,
119 BK_Attr_IDX_NB,
120 };
121 #define BK_ATT_DECL_PRIMARY_SERVICE {0x00,0x28,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
122 #define BK_ATT_DECL_CHARACTERISTIC {0x03,0x28,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
123 #define BK_ATT_DESC_CLIENT_CHAR_CFG {0x02,0x29,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
124
125 ble_attm_desc_t attrListPattern[BK_Attr_IDX_NB]={
126
127 // Service Declaration
128 [BK_Attr_IDX_SVC] = {BK_ATT_DECL_PRIMARY_SERVICE,BK_BLE_PERM_SET(RD, ENABLE), 0, 0},
129
130 // Level Characteristic Declaration
131 [BK_Attr_IDX_CHAR] = {BK_ATT_DECL_CHARACTERISTIC, BK_BLE_PERM_SET(RD, ENABLE), 0, 0},
132
133 // Level Characteristic - Client Characteristic Configuration Descriptor
134 [BK_Attr_IDX_CFG] = {BK_ATT_DESC_CLIENT_CHAR_CFG, BK_BLE_PERM_SET(RD, ENABLE)|BK_BLE_PERM_SET(WRITE_REQ,ENABLE), 0, 0},
135
136 };
137
138
139
140
ble_notice_cb(ble_notice_t notice,void * param)141 static void ble_notice_cb(ble_notice_t notice, void *param)
142 {
143 switch (notice) {
144 case BLE_5_STACK_OK:
145 bk_printf("ble stack ok");
146 break;
147 case BLE_5_WRITE_EVENT:
148 {
149 ble_write_req_t *w_req = (ble_write_req_t *)param;
150 bk_printf("write_cb:conn_idx:%d, prf_id:%d, add_id:%d, len:%d, data[0]:%02x\r\n",
151 w_req->conn_idx, w_req->prf_id, w_req->att_idx, w_req->len, w_req->value[0]);
152
153 BtReqWriteCbPara writeCbPara;
154 writeCbPara.connId=w_req->conn_idx;
155 writeCbPara.transId=OHOS_BT_TRANSPORT_LE;
156 writeCbPara.bdAddr=(BdAddr *)(&hal_bt_env.peer_addr[0]);
157 writeCbPara.attrHandle=w_req->att_idx;
158 writeCbPara.offset=0;
159 writeCbPara.length=w_req->len;
160 writeCbPara.value=w_req->value;
161 writeCbPara.needRsp=false;
162 writeCbPara.isPrep=false;
163
164 if(g_ohos_Op_Func_cb[w_req->prf_id][(w_req->att_idx>>1)-1].write){
165 bk_printf("fun:%x\r\n",g_ohos_Op_Func_cb[w_req->prf_id][(w_req->att_idx>>1)-1].write);
166 g_ohos_Op_Func_cb[w_req->prf_id][(w_req->att_idx>>1)-1].write(&(w_req->value[0]),w_req->len);
167 }
168 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->requestWriteCb)
169 gatt_server_callbackfunc->requestWriteCb(writeCbPara);
170 break;
171
172 }
173 case BLE_5_READ_EVENT:
174 {
175 ble_read_req_t *r_req = (ble_read_req_t *)param;
176 bk_printf("read_cb:conn_idx:%d, prf_id:%d, add_id:%d\r\n",
177 r_req->conn_idx, r_req->prf_id, r_req->att_idx);
178
179 #if 0
180 // r_req->value[0] = 0x99;
181 // r_req->value[1] = 0x34;
182 // r_req->value[2] = 0x56;
183 // r_req->length = 3;
184
185 if(g_ohos_Op_Func_cb[r_req->prf_id][(r_req->att_idx>>1)-1].read){
186 bk_printf("fun:%x\r\n",g_ohos_Op_Func_cb[r_req->prf_id][(r_req->att_idx>>1)-1].read);
187 g_ohos_Op_Func_cb[r_req->prf_id][(r_req->att_idx>>1)-1].read(&(r_req->value[0]),&(r_req->length));
188 }
189 #endif
190 BtReqReadCbPara readCbPara;
191 readCbPara.connId=r_req->conn_idx;
192 readCbPara.transId=OHOS_BT_TRANSPORT_LE;
193 readCbPara.bdAddr=(BdAddr *)(&hal_bt_env.peer_addr[0]);
194 readCbPara.attrHandle=r_req->att_idx;
195 readCbPara.offset=0;
196 readCbPara.isLong=0;
197
198 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->requestReadCb)
199 gatt_server_callbackfunc->requestReadCb(readCbPara);
200
201 break;
202 }
203
204 case BLE_5_REPORT_ADV:
205 {
206 ble_recv_adv_t *r_ind = (ble_recv_adv_t *)param;
207 bk_printf("r_ind:actv_idx:%d, adv_addr:%02x:%02x:%02x:%02x:%02x:%02x\r\n",
208 r_ind->actv_idx, r_ind->adv_addr[0], r_ind->adv_addr[1], r_ind->adv_addr[2],
209 r_ind->adv_addr[3], r_ind->adv_addr[4], r_ind->adv_addr[5]);
210 break;
211 }
212 case BLE_5_MTU_CHANGE:
213 {
214 ble_mtu_change_t *m_ind = (ble_mtu_change_t *)param;
215 bk_printf("m_ind:conn_idx:%d, mtu_size:%d\r\n", m_ind->conn_idx, m_ind->mtu_size);
216
217 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->mtuChangeCb)
218 gatt_server_callbackfunc->mtuChangeCb(m_ind->conn_idx, m_ind->mtu_size);
219
220 break;
221 }
222 case BLE_5_CONNECT_EVENT:
223 {
224 ble_conn_ind_t *c_ind = (ble_conn_ind_t *)param;
225 bk_printf("c_ind:conn_idx:%d, addr_type:%d, peer_addr:%02x:%02x:%02x:%02x:%02x:%02x\r\n",
226 c_ind->conn_idx, c_ind->peer_addr_type, c_ind->peer_addr[0], c_ind->peer_addr[1],
227 c_ind->peer_addr[2], c_ind->peer_addr[3], c_ind->peer_addr[4], c_ind->peer_addr[5]);
228
229 memcpy(&hal_bt_env.peer_addr, c_ind->peer_addr, BK_BLE_GAP_BD_ADDR_LEN);
230 BdAddr bdAddr;
231 memcpy(bdAddr.addr,c_ind->peer_addr,BK_BLE_GAP_BD_ADDR_LEN);
232
233 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->connectServerCb)
234 gatt_server_callbackfunc->connectServerCb(c_ind->conn_idx, 0, &bdAddr);
235
236 break;
237 }
238 case BLE_5_DISCONNECT_EVENT:
239 {
240 ble_discon_ind_t *d_ind = (ble_discon_ind_t *)param;
241 bk_printf("d_ind:conn_idx:%d,reason:%d\r\n", d_ind->conn_idx,d_ind->reason);
242
243 BdAddr bdAddr;
244 memcpy(bdAddr.addr,hal_bt_env.peer_addr,BK_BLE_GAP_BD_ADDR_LEN);
245
246 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->disconnectServerCb)
247 gatt_server_callbackfunc->disconnectServerCb(d_ind->conn_idx, 0, &bdAddr);;
248
249
250 break;
251 }
252 case BLE_5_ATT_INFO_REQ:
253 {
254 ble_att_info_req_t *a_ind = (ble_att_info_req_t *)param;
255 bk_printf("a_ind:conn_idx:%d\r\n", a_ind->conn_idx);
256 a_ind->length = 128;
257 a_ind->status = BK_ERR_BLE_SUCCESS;
258 break;
259 }
260
261 case BLE_5_TX_DONE:
262 {
263 bk_printf("BLE_5_TX_DONE\r\n");
264 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->indicationSentCb)
265 gatt_server_callbackfunc->indicationSentCb(0, 0);
266 break;
267 }
268
269 case BLE_5_CREATE_DB:
270 {
271 ble_create_db_t *cd_ind = (ble_create_db_t *)param;
272 bk_printf("cd_ind:prf_id:%d, status:%d\r\n", cd_ind->prf_id, cd_ind->status);
273
274 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->serviceStartCb)
275 gatt_server_callbackfunc->serviceStartCb(cd_ind->status,cd_ind->prf_id, 0);
276
277 break;
278 }
279 case BLE_5_INIT_CONNECT_EVENT:
280 {
281 ble_conn_ind_t *c_ind = (ble_conn_ind_t *)param;
282 bk_printf("BLE_5_INIT_CONNECT_EVENT:conn_idx:%d, addr_type:%d, peer_addr:%02x:%02x:%02x:%02x:%02x:%02x\r\n",
283 c_ind->conn_idx, c_ind->peer_addr_type, c_ind->peer_addr[0], c_ind->peer_addr[1],
284 c_ind->peer_addr[2], c_ind->peer_addr[3], c_ind->peer_addr[4], c_ind->peer_addr[5]);
285 break;
286 }
287 case BLE_5_INIT_DISCONNECT_EVENT:
288 {
289 ble_discon_ind_t *d_ind = (ble_discon_ind_t *)param;
290 bk_printf("BLE_5_INIT_DISCONNECT_EVENT:conn_idx:%d,reason:%d\r\n", d_ind->conn_idx,d_ind->reason);
291 break;
292 }
293 case BLE_5_SDP_REGISTER_FAILED:
294 bk_printf("BLE_5_SDP_REGISTER_FAILED\r\n");
295 break;
296 default:
297 break;
298 }
299 }
300
HAL_ble_cmd_cb(ble_cmd_t cmd,ble_cmd_param_t * param)301 static void HAL_ble_cmd_cb(ble_cmd_t cmd, ble_cmd_param_t *param)
302 {
303 bk_printf("cmd:%d idx:%d status:%d\r\n", cmd, param->cmd_idx, param->status);
304 switch(cmd){
305 case BLE_INIT_ADV:
306 if(gatt_callbackfunc&&gatt_callbackfunc->advEnableCb)
307 gatt_callbackfunc->advEnableCb(0,param->status);
308 break;
309 case BLE_DEINIT_ADV:
310 if(gatt_callbackfunc&&gatt_callbackfunc->advDisableCb)
311 gatt_callbackfunc->advDisableCb(0,param->status);
312 break;
313
314 case BLE_SET_ADV_DATA:
315 if(gatt_callbackfunc&&gatt_callbackfunc->advUpdateCb)
316 gatt_callbackfunc->advUpdateCb(0,param->status);
317 break;
318 default:
319 break;
320
321 }
322
323 }
324
ble_stack_init(void)325 static void ble_stack_init(void)
326 {
327 bk_ble_set_notice_cb(ble_notice_cb);
328 }
329
330
InitBtStack(void)331 int InitBtStack(void)
332 {
333 return OHOS_BT_STATUS_SUCCESS;
334 }
335
336
EnableBtStack(void)337 int EnableBtStack(void)
338 {
339 ble_stack_init();
340 svc_cnt = 0;
341 return OHOS_BT_STATUS_SUCCESS;
342 }
343
344
DisableBtStack(void)345 int DisableBtStack(void)
346 {
347 bk_printf("DisableBtStack NOT SUPPORT!!");
348 return OHOS_BT_STATUS_UNSUPPORTED;
349 }
350
351
SetDeviceName(const char * name,unsigned int len)352 int SetDeviceName(const char *name, unsigned int len)
353 {
354 bk_ble_appm_set_dev_name(len,(uint8_t*)name);
355 return OHOS_BT_STATUS_SUCCESS;
356 }
357
BleSetAdvData(int advId,const BleConfigAdvData * data)358 int BleSetAdvData(int advId, const BleConfigAdvData *data)
359 {
360 memcpy(&(hal_bt_env.advData[0]), &(data->advData[0]), data->advLength);
361 hal_bt_env.advDataLen = data->advLength;
362
363 memcpy(&(hal_bt_env.respData[0]), &(data->scanRspData[0]), data->scanRspLength);
364 hal_bt_env.respDataLen = data->scanRspLength;
365
366 if(gatt_callbackfunc&&gatt_callbackfunc->advDataCb)
367 gatt_callbackfunc->advDataCb(0,0);
368
369 return OHOS_BT_STATUS_SUCCESS;
370 }
371
BleUpdateAdv(int advId,const BleAdvParams * param)372 int BleUpdateAdv(int advId, const BleAdvParams *param)
373 {
374 bk_ble_adv_stop(advId, HAL_ble_cmd_cb);
375 BleStartAdv(advId,param);
376 return OHOS_BT_STATUS_SUCCESS;
377 }
378
BleStartAdv(int advId,const BleAdvParams * param)379 int BleStartAdv(int advId, const BleAdvParams *param)
380 {
381 struct adv_param hal_adv;
382
383 hal_adv.channel_map=param->channelMap;
384 hal_adv.interval_min=param->minInterval;
385 hal_adv.interval_max=param->maxInterval;
386 hal_adv.duration=param->duration;
387
388 memcpy(&(hal_adv.advData[0]), &(hal_bt_env.advData[0]), hal_bt_env.advDataLen);
389 hal_adv.advDataLen = hal_bt_env.advDataLen;
390
391 memcpy(&(hal_adv.respData[0]), &(hal_bt_env.respData[0]), hal_bt_env.respDataLen);
392 hal_adv.respDataLen = hal_bt_env.respDataLen;
393
394 #if 1
395 bk_printf("respDataLen:%d,advDataLen:%d,duration:%d,interval_min:%d,interval_max:%d,channel_map:%d\r\n",hal_adv.respDataLen,hal_adv.advDataLen,hal_adv.duration,hal_adv.interval_min,hal_adv.interval_max,hal_adv.channel_map);
396
397 for(int i=0;i<hal_adv.advDataLen;i++)
398 {
399 bk_printf("%x ",hal_adv.advData[i]);
400 }
401
402 bk_printf("\r\n");
403
404 for(int i=0;i<hal_adv.respDataLen;i++)
405 {
406 bk_printf("%x ",hal_adv.respData[i]);
407 }
408 bk_printf("\r\n");
409 #endif
410
411 hal_bt_env.adv_idx=0;//bk_ble_get_idle_actv_idx_handle();
412 bk_ble_adv_start(hal_bt_env.adv_idx,&hal_adv,HAL_ble_cmd_cb);
413
414 return OHOS_BT_STATUS_SUCCESS;
415 }
416
BleStartAdvEx(int * advId,const StartAdvRawData rawData,BleAdvParams advParam)417 int BleStartAdvEx(int *advId, const StartAdvRawData rawData, BleAdvParams advParam)
418 {
419 struct adv_param hal_adv;
420 //*advId=0;
421
422 hal_adv.channel_map=advParam.channelMap;
423 hal_adv.interval_min=advParam.minInterval;
424 hal_adv.interval_max=advParam.maxInterval;
425 hal_adv.duration=advParam.duration;
426 //advParam.advType;
427 memcpy(&(hal_adv.advData[0]), &(rawData.advData[0]), rawData.advDataLen);
428 hal_adv.advDataLen = rawData.advDataLen;
429 memcpy(&(hal_adv.respData[0]), &(rawData.rspData[0]), rawData.rspDataLen);
430 hal_adv.respDataLen = rawData.rspDataLen;
431
432 #if 1
433 bk_printf("respDataLen:%d,advDataLen:%d,duration:%d,interval_min:%d,interval_max:%d,channel_map:%d\r\n",hal_adv.respDataLen,hal_adv.advDataLen,hal_adv.duration,hal_adv.interval_min,hal_adv.interval_max,hal_adv.channel_map);
434
435 for(int i=0;i<hal_adv.advDataLen;i++)
436 {
437 bk_printf("%x ",hal_adv.advData[i]);
438 }
439
440 bk_printf("\r\n");
441
442 for(int i=0;i<hal_adv.respDataLen;i++)
443 {
444 bk_printf("%x ",hal_adv.respData[i]);
445 }
446 bk_printf("\r\n");
447 #endif
448
449 hal_bt_env.adv_idx=0;//bk_ble_get_idle_actv_idx_handle();
450 bk_ble_adv_start(hal_bt_env.adv_idx,&hal_adv,HAL_ble_cmd_cb);
451
452 return OHOS_BT_STATUS_SUCCESS;
453
454 }
455
BleStopAdv(int advId)456 int BleStopAdv(int advId)
457 {
458 bk_ble_adv_stop(hal_bt_env.adv_idx, HAL_ble_cmd_cb);
459 return OHOS_BT_STATUS_SUCCESS;
460 }
461
462
BleSetSecurityIoCap(BleIoCapMode mode)463 int BleSetSecurityIoCap(BleIoCapMode mode)
464 {
465 return OHOS_BT_STATUS_SUCCESS;
466 }
467
BleSetSecurityAuthReq(BleAuthReqMode mode)468 int BleSetSecurityAuthReq(BleAuthReqMode mode)
469 {
470 return OHOS_BT_STATUS_SUCCESS;
471 }
472
BleGattSecurityRsp(BdAddr bdAddr,bool accept)473 int BleGattSecurityRsp(BdAddr bdAddr, bool accept)
474 {
475 return OHOS_BT_STATUS_SUCCESS;
476 }
477
478 extern struct bd_addr_t common_default_bdaddr;
479
ReadBtMacAddr(unsigned char * mac,unsigned int len)480 int ReadBtMacAddr(unsigned char *mac, unsigned int len)
481 {
482 memcpy(mac, (uint8 *)&common_default_bdaddr, BK_BLE_GAP_BD_ADDR_LEN); //xxx
483 return OHOS_BT_STATUS_SUCCESS;
484 }
485
486
BleSetScanParameters(int clientId,BleScanParams * param)487 int BleSetScanParameters(int clientId, BleScanParams *param)
488 {
489 ohos_scan_info.channel_map = 7;
490 ohos_scan_info.interval = param->scanInterval;
491 ohos_scan_info.window = param->scanWindow;
492 hal_bt_env.scan_idx = bk_ble_get_idle_actv_idx_handle();
493
494 if(gatt_callbackfunc&&gatt_callbackfunc->scanParamSetCb)
495 gatt_callbackfunc->scanParamSetCb(0,0);
496
497 return OHOS_BT_STATUS_SUCCESS;
498 }
499
BleStartScan(void)500 int BleStartScan(void)
501 {
502 bk_ble_scan_start(hal_bt_env.scan_idx, &ohos_scan_info, HAL_ble_cmd_cb);
503 return OHOS_BT_STATUS_SUCCESS;
504 }
505
BleStopScan(void)506 int BleStopScan(void)
507 {
508 bk_ble_stop_scaning(hal_bt_env.scan_idx, HAL_ble_cmd_cb);
509 return OHOS_BT_STATUS_SUCCESS;
510 }
511
BleGattRegisterCallbacks(BtGattCallbacks * func)512 int BleGattRegisterCallbacks(BtGattCallbacks *func)
513 {
514 gatt_callbackfunc=func;
515 return OHOS_BT_STATUS_SUCCESS;
516 }
517
518
519
BleGattsRegister(BtUuid appUuid)520 int BleGattsRegister(BtUuid appUuid)
521 {
522 if(ohos_server_uuid_para[svc_cnt]==NULL)
523 {
524 bk_printf("%s:%d,svc:%d\r\n",__FUNCTION__,__LINE__,svc_cnt);
525 ohos_server_uuid_para[svc_cnt]=os_malloc(sizeof(BtServer));
526 ohos_server_uuid_para[svc_cnt]->uuid=os_malloc(appUuid.uuidLen);
527 memcpy(ohos_server_uuid_para[svc_cnt]->uuid,appUuid.uuid,appUuid.uuidLen);
528 ohos_server_uuid_para[svc_cnt]->uuidLen=appUuid.uuidLen;
529
530 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->registerServerCb)
531 gatt_server_callbackfunc->registerServerCb(0, 0, &appUuid);
532 }
533
534 return OHOS_BT_STATUS_SUCCESS;
535 }
536
537
BleGattsUnRegister(int serverId)538 int BleGattsUnRegister(int serverId)
539 {
540 return OHOS_BT_STATUS_SUCCESS;
541 }
542
543
BleGattsDisconnect(int serverId,BdAddr bdAddr,int connId)544 int BleGattsDisconnect(int serverId, BdAddr bdAddr, int connId)
545 {
546 bk_ble_disconnect(hal_bt_env.scan_idx, HAL_ble_cmd_cb);
547 return OHOS_BT_STATUS_SUCCESS;
548 }
549
BleGattsAddService(int serverId,BtUuid srvcUuid,bool isPrimary,int number)550 int BleGattsAddService(int serverId, BtUuid srvcUuid, bool isPrimary, int number)
551 {
552
553 if(serverId>= MAX_SERVER_NUM)
554 {
555 bk_printf("BleGattsAddService add server failed ,serverId is %d,max is %d\r\n",serverId, MAX_SERVER_NUM-1);
556 return OHOS_BT_STATUS_FAIL;
557 }
558
559 if(ohos_server_uuid_para[serverId]== NULL)
560 {
561 ohos_server_uuid_para[serverId]= os_malloc(sizeof(BtServer));
562 ohos_server_uuid_para[serverId]->uuid=os_malloc(srvcUuid.uuidLen);
563 ohos_server_uuid_para[serverId]->ohos_att_db=os_malloc(sizeof(ble_attm_desc_t)*number);
564 memcpy(ohos_server_uuid_para[serverId]->uuid,srvcUuid.uuid,srvcUuid.uuidLen);
565
566 //primary service declartion
567 ohos_server_uuid_para[serverId]->ohos_att_db[0].uuid[0]=0x00;
568 ohos_server_uuid_para[serverId]->ohos_att_db[0].uuid[1]=0x28;
569
570 ohos_server_uuid_para[serverId]->ohos_att_db[0].perm=BK_BLE_PERM_SET(RD, ENABLE);
571 ohos_server_uuid_para[serverId]->ohos_att_db[0].ext_perm=0;
572 ohos_server_uuid_para[serverId]->ohos_att_db[0].max_size=0;
573
574 ohos_server_uuid_para[serverId]->uuidLen=srvcUuid.uuidLen;
575 ohos_server_uuid_para[serverId]->server_id=serverId;
576 ohos_server_uuid_para[serverId]->max_att_handle=number;
577
578 ohos_server_uuid_para[serverId]->next_att_handle=1;
579
580 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->serviceAddCb)
581 gatt_server_callbackfunc->serviceAddCb(0, serverId, &srvcUuid, 0);
582
583 return OHOS_BT_STATUS_SUCCESS;
584
585 }
586
587 return OHOS_BT_STATUS_FAIL;
588
589 }
590
591
592 /* ????? */
BleGattsAddCharacteristic(int serverId,int srvcHandle,BtUuid characUuid,int properties,int permissions)593 int BleGattsAddCharacteristic(int serverId, int srvcHandle, BtUuid characUuid, int properties, int permissions)
594 {
595
596 int index;
597 int max_index;
598 uint32_t mode=0;
599
600 if(ohos_server_uuid_para[serverId])
601 {
602
603 index=ohos_server_uuid_para[serverId]->next_att_handle;
604 max_index=ohos_server_uuid_para[serverId]->max_att_handle;
605 if(index<max_index-1)
606 {
607 //characteristic declartion
608 ohos_server_uuid_para[serverId]->ohos_att_db[index].uuid[0]=0x03;
609 ohos_server_uuid_para[serverId]->ohos_att_db[index].uuid[1]=0x28;
610 ohos_server_uuid_para[serverId]->ohos_att_db[index].perm=BK_BLE_PERM_SET(RD, ENABLE);
611 ohos_server_uuid_para[serverId]->ohos_att_db[index].ext_perm=0;
612 ohos_server_uuid_para[serverId]->ohos_att_db[index].max_size=0;
613
614 index++;
615 //characterisitc value declartion
616 ohos_server_uuid_para[serverId]->next_att_handle++;
617 memcpy(ohos_server_uuid_para[serverId]->ohos_att_db[index].uuid,characUuid.uuid,characUuid.uuidLen);
618
619 if(properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_READ)
620 {
621 mode|=BK_BLE_PERM_SET(RD, ENABLE);
622 }
623 if(properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP)
624 {
625 mode|=BK_BLE_PERM_SET(WRITE_COMMAND,ENABLE);
626 }
627 if(properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE)
628 {
629 mode|=BK_BLE_PERM_SET(WRITE_REQ,ENABLE);
630 }
631 if(properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY)
632 {
633 mode|=BK_BLE_PERM_SET(NTF,ENABLE);
634 }
635 if(properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE)
636 {
637 mode|=BK_BLE_PERM_SET(IND,ENABLE);
638 }
639 ohos_server_uuid_para[serverId]->ohos_att_db[index].perm=mode;
640
641 if(characUuid.uuidLen==2)
642 {
643 ohos_server_uuid_para[serverId]->ohos_att_db[index].ext_perm=BK_BLE_PERM_SET(RI, ENABLE)|BK_BLE_PERM_SET(UUID_LEN, UUID_16);
644 }
645 else if(characUuid.uuidLen==16)
646 {
647 ohos_server_uuid_para[serverId]->ohos_att_db[index].ext_perm=BK_BLE_PERM_SET(RI, ENABLE)|BK_BLE_PERM_SET(UUID_LEN, UUID_128);
648 }
649 ohos_server_uuid_para[serverId]->ohos_att_db[index].max_size=128;
650 ohos_server_uuid_para[serverId]->next_att_handle++;
651
652 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->serviceAddCb)
653 gatt_server_callbackfunc->characteristicAddCb(0, serverId, &characUuid,srvcHandle, index);
654
655 return OHOS_BT_STATUS_SUCCESS;
656 }
657
658 }
659
660 return OHOS_BT_STATUS_FAIL;
661
662
663 }
664
665 /* Adds a descriptor to a specified characteristic */
BleGattsAddDescriptor(int serverId,int srvcHandle,BtUuid descUuid,int permissions)666 int BleGattsAddDescriptor(int serverId, int srvcHandle, BtUuid descUuid, int permissions)
667 {
668 int index,max_index;
669 if(ohos_server_uuid_para[serverId])
670 {
671 index=ohos_server_uuid_para[serverId]->next_att_handle;
672 max_index=ohos_server_uuid_para[serverId]->max_att_handle;
673 if(index<max_index)
674 {
675
676 memcpy(ohos_server_uuid_para[serverId]->ohos_att_db[index].uuid,descUuid.uuid,descUuid.uuidLen);
677 //bk_printf("3att uuid:%x %x\r\n",ohos_server_uuid_para[serverId]->ohos_att_db[index].uuid[0],ohos_server_uuid_para[serverId]->ohos_att_db[index].uuid[1]);
678 if(permissions&OHOS_GATT_PERMISSION_READ)
679 {
680 ohos_server_uuid_para[serverId]->ohos_att_db[index].perm|=BK_BLE_PERM_SET(RD, ENABLE);
681 }
682 if(permissions&OHOS_GATT_PERMISSION_WRITE)
683 {
684 ohos_server_uuid_para[serverId]->ohos_att_db[index].perm|=BK_BLE_PERM_SET(WRITE_REQ, ENABLE);
685 }
686
687 ohos_server_uuid_para[serverId]->ohos_att_db[index].ext_perm=0;
688 ohos_server_uuid_para[serverId]->ohos_att_db[index].max_size=0;
689 ohos_server_uuid_para[serverId]->next_att_handle++;
690
691 if(gatt_server_callbackfunc&&gatt_server_callbackfunc->descriptorAddCb)
692 gatt_server_callbackfunc->descriptorAddCb(0, serverId, &descUuid,srvcHandle, index);
693
694 return OHOS_BT_STATUS_SUCCESS;
695
696 }
697 }
698
699 return OHOS_BT_STATUS_FAIL;
700 }
701
BleGattsStartService(int serverId,int srvcHandle)702 int BleGattsStartService(int serverId, int srvcHandle)
703 {
704 ble_err_t status = BK_ERR_BLE_SUCCESS;
705
706 struct bk_ble_db_cfg ble_db_cfg;
707
708 ble_db_cfg.att_db = ohos_server_uuid_para[serverId]->ohos_att_db;
709 ble_db_cfg.att_db_nb = ohos_server_uuid_para[serverId]->max_att_handle;
710 ble_db_cfg.prf_task_id =serverId;
711 ble_db_cfg.start_hdl = 0;
712
713
714 if( ohos_server_uuid_para[serverId]->uuidLen==2)
715 {
716 ble_db_cfg.svc_perm = BK_BLE_PERM_SET(SVC_UUID_LEN, UUID_16);
717 }
718 else if( ohos_server_uuid_para[serverId]->uuidLen==16)
719 {
720 ble_db_cfg.svc_perm = BK_BLE_PERM_SET(SVC_UUID_LEN, UUID_128);
721 }
722
723
724 memcpy(&(ble_db_cfg.uuid[0]), &ohos_server_uuid_para[serverId]->uuid[0], 16);
725
726 status = bk_ble_create_db(&ble_db_cfg);
727
728 for(int i=0;i<ble_db_cfg.att_db_nb;i++)
729 {
730 bk_printf("index:%d,bd uuid:%x %x\r\n",i,ohos_server_uuid_para[serverId]->ohos_att_db[i].uuid[0],ohos_server_uuid_para[serverId]->ohos_att_db[i].uuid[1]);
731 bk_printf("index:%d,perm:%x ext_perm:%x max_size:%d\r\n",i,ohos_server_uuid_para[serverId]->ohos_att_db[i].perm,ohos_server_uuid_para[serverId]->ohos_att_db[i].ext_perm,ohos_server_uuid_para[serverId]->ohos_att_db[i].max_size);
732
733 }
734
735 bk_printf("ble_db_cfg.prf_task_id :%d,nb:%d,uuid:%x %x\r\n",ble_db_cfg.prf_task_id,ble_db_cfg.att_db_nb,ohos_server_uuid_para[serverId]->uuid[0],ohos_server_uuid_para[serverId]->uuid[1]);
736
737 status = bk_ble_create_db(&ble_db_cfg);
738
739 return (status==BK_ERR_BLE_SUCCESS)?OHOS_BT_STATUS_SUCCESS:OHOS_BT_STATUS_FAIL;
740 }
741
742 /*No implementation required */
BleGattsStopService(int serverId,int srvcHandle)743 int BleGattsStopService(int serverId, int srvcHandle)
744 {
745 (void)serverId;
746 (void)srvcHandle;
747
748 return OHOS_BT_STATUS_SUCCESS;
749 }
750
751 /*No implementation required */
BleGattsDeleteService(int serverId,int srvcHandle)752 int BleGattsDeleteService(int serverId, int srvcHandle)
753 {
754 (void)serverId;
755 (void)srvcHandle;
756
757 return OHOS_BT_STATUS_SUCCESS;
758 }
759
760 /*No implementation required */
BleGattsClearServices(int serverId)761 int BleGattsClearServices(int serverId)
762 {
763 (void)serverId;
764
765 return OHOS_BT_STATUS_SUCCESS;
766 }
767
768 /*No implementation required */
BleGattsSendResponse(int serverId,GattsSendRspParam * param)769 int BleGattsSendResponse(int serverId, GattsSendRspParam *param)
770 {
771 (void)serverId;
772 (void)param;
773
774 return OHOS_BT_STATUS_SUCCESS;
775 }
776
777
BleGattsSendIndication(int serverId,GattsSendIndParam * param)778 int BleGattsSendIndication(int serverId, GattsSendIndParam *param)
779 {
780 bk_printf("BleGattsSendIndication srvID:%d,attr_hdl:%d->%d,cfm:%d\r\n",serverId,param->attrHandle,param->attrHandle*2,param->confirm);
781 param->attrHandle *= 2;
782
783 if(1==param->confirm) //IND
784 {
785 bk_ble_send_ind_value(0,param->valueLen,(uint8_t *)param->value,serverId,param->attrHandle);
786 }
787 else if(0==param->confirm) //NTF
788 {
789 bk_ble_send_noti_value(0,param->valueLen,(uint8_t *)param->value,serverId,param->attrHandle);
790 }
791
792 return OHOS_BT_STATUS_SUCCESS;
793 }
794
BleGattsSetEncryption(BdAddr bdAddr,BleSecAct secAct)795 int BleGattsSetEncryption(BdAddr bdAddr, BleSecAct secAct)
796 {
797 return OHOS_BT_STATUS_SUCCESS;
798 }
799
BleGattsRegisterCallbacks(BtGattServerCallbacks * func)800 int BleGattsRegisterCallbacks(BtGattServerCallbacks *func)
801 {
802 gatt_server_callbackfunc=func;
803 return OHOS_BT_STATUS_SUCCESS;
804 }
805
BleConvertAttr2BK(ble_attm_desc_t * bk_attr,BleGattAttr * hi_attr)806 uint8_t BleConvertAttr2BK(ble_attm_desc_t *bk_attr,BleGattAttr *hi_attr)
807 {
808 uint8_t attr_uuidlen=0;
809 uint16_t ext_perm=0;
810 bk_attr->perm = bk_attr->ext_perm = 0;
811
812 switch(hi_attr->uuidType)
813 {
814 case OHOS_UUID_TYPE_16_BIT:
815 attr_uuidlen=2;
816 ext_perm = BK_BLE_PERM_SET(RI, ENABLE)|BK_BLE_PERM_SET(UUID_LEN, UUID_16);
817 break;
818
819 case OHOS_UUID_TYPE_32_BIT:
820 attr_uuidlen=4;
821 ext_perm = BK_BLE_PERM_SET(RI, ENABLE)|BK_BLE_PERM_SET(UUID_LEN, UUID_32);
822 break;
823
824 case OHOS_UUID_TYPE_128_BIT:
825 attr_uuidlen=16;
826 ext_perm = BK_BLE_PERM_SET(RI, ENABLE)|BK_BLE_PERM_SET(UUID_LEN, UUID_128);
827 break;
828 default:
829 attr_uuidlen=0;
830 }
831 memcpy(&(bk_attr->uuid[0]),&(hi_attr->uuid[0]),attr_uuidlen);
832
833 switch(hi_attr->attrType)
834 {
835 case OHOS_BLE_ATTRIB_TYPE_SERVICE:
836 bk_attr->ext_perm=0;
837 bk_attr->max_size=0;
838 if(hi_attr->permission&OHOS_GATT_PERMISSION_READ){
839 bk_attr->perm|=BK_BLE_PERM_SET(RD, ENABLE);
840 }
841 if(hi_attr->permission&OHOS_GATT_PERMISSION_WRITE){
842 bk_attr->perm|=BK_BLE_PERM_SET(WRITE_REQ,ENABLE);
843 }
844 break;
845 case OHOS_BLE_ATTRIB_TYPE_CHAR:
846 case OHOS_BLE_ATTRIB_TYPE_CHAR_VALUE:
847 case OHOS_BLE_ATTRIB_TYPE_CHAR_CLIENT_CONFIG:
848
849 if(hi_attr->properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_READ)
850 {
851 bk_attr->perm|=BK_BLE_PERM_SET(RD, ENABLE);
852 }
853 if(hi_attr->properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP)
854 {
855 bk_attr->perm|=BK_BLE_PERM_SET(WRITE_COMMAND,ENABLE);
856 }
857 if(hi_attr->properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE)
858 {
859 bk_attr->perm|=BK_BLE_PERM_SET(WRITE_REQ,ENABLE);
860 }
861 if(hi_attr->properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY)
862 {
863 bk_attr->perm|=BK_BLE_PERM_SET(NTF,ENABLE);
864 }
865 if(hi_attr->properties&OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE)
866 {
867 bk_attr->perm|=BK_BLE_PERM_SET(IND,ENABLE);
868 }
869
870 bk_attr->max_size=200;
871
872 bk_attr->ext_perm=ext_perm;
873 break;
874 default:
875 break;
876 }
877
878 printf("hi_perm:0x%x,prop:0x%x,bk_perm:0x%x,ext:%x\r\n",hi_attr->permission,hi_attr->properties,bk_attr->perm,bk_attr->ext_perm);
879 return attr_uuidlen;
880 }
881
BleGattsStartServiceEx(int * srvcHandle,BleGattService * srvcInfo)882 int BleGattsStartServiceEx(int *srvcHandle, BleGattService *srvcInfo)
883 {
884 BleGattAttr *attr_list=srvcInfo->attrList;
885 uint8_t attr_num=srvcInfo->attrNum;
886 // BtServer tmp_sever;
887 // uint8_t attr_index=0;
888 // uint8_t attr_uuidlen=0;
889 uint8_t attr_num_total = 0;
890 struct bk_ble_db_cfg ble_db_cfg;
891 int i;
892
893
894 //svc_cnt = (uint8_t)*srvcHandle;
895 if(svc_cnt>=MAX_SERVER_NUM)
896 {
897 bk_printf("add server failed ,max serverId is %d\r\n",MAX_SERVER_NUM-1);
898 return OHOS_BT_STATUS_FAIL;
899 }
900
901 if(ohos_server_uuid_para[svc_cnt]==NULL)
902 {
903 ohos_server_uuid_para[svc_cnt]=os_malloc(sizeof(BtServer));
904 ohos_server_uuid_para[svc_cnt]->uuid=os_malloc(16);
905 }
906
907 //get total attr_num
908 for(i=0;i<attr_num;i++){
909 switch(attr_list[i].attrType){
910
911 case OHOS_BLE_ATTRIB_TYPE_SERVICE:
912 attr_num_total += 1;
913 break;
914 case OHOS_BLE_ATTRIB_TYPE_CHAR:
915
916 case OHOS_BLE_ATTRIB_TYPE_CHAR_CLIENT_CONFIG:
917
918 case OHOS_BLE_ATTRIB_TYPE_CHAR_VALUE:
919
920 attr_num_total += 2;
921 default:
922 break;
923 }
924 if(attr_list[i].properties&(OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY|OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE)){
925 attr_num_total += 1;
926 }
927 }
928 bk_printf("----------attr_num:%x,total:%x----\r\n",attr_num,attr_num_total);
929
930 ohos_server_uuid_para[svc_cnt]->ohos_att_db=os_malloc(sizeof(ble_attm_desc_t)*attr_num_total);
931 ble_attm_desc_t *attr_bk_list=ohos_server_uuid_para[svc_cnt]->ohos_att_db;
932 bk_printf("%s:%d,attr_num:%d,srvID:%d\r\n",__FUNCTION__,__LINE__,attr_num_total,*srvcHandle);
933 unsigned char attr_idx = 0;
934 while(attr_list && attr_num)
935 {
936 if(attr_list->attrType == OHOS_BLE_ATTRIB_TYPE_SERVICE){
937
938 uint8_t uuidlen = BleConvertAttr2BK(attr_bk_list,attr_list);
939
940 memcpy(&(ble_db_cfg.uuid[0]), &(attr_list->uuid[0]), uuidlen); //svc uuid
941
942 memcpy(&(attr_bk_list->uuid[0]),&attrListPattern[BK_Attr_IDX_SVC].uuid[0],uuidlen);
943 if(uuidlen==16)
944 ble_db_cfg.svc_perm = BK_BLE_PERM_SET(SVC_UUID_LEN, UUID_128);
945 else if(uuidlen==4)
946 ble_db_cfg.svc_perm = BK_BLE_PERM_SET(SVC_UUID_LEN, UUID_32);
947 else
948 ble_db_cfg.svc_perm = BK_BLE_PERM_SET(SVC_UUID_LEN, UUID_16);
949
950 attr_bk_list++;
951 attr_list++;
952
953 }else if(attr_list->attrType == OHOS_BLE_ATTRIB_TYPE_CHAR){
954 memcpy(attr_bk_list,&attrListPattern[BK_Attr_IDX_CHAR],sizeof(ble_attm_desc_t));
955 attr_bk_list++;
956 BleConvertAttr2BK(attr_bk_list,attr_list);
957 bk_printf("reg fun[%d][%d] R 0x%x\r\n",svc_cnt,attr_idx,(attr_list->func).read);
958 bk_printf("reg fun[%d][%d] W 0x%x\r\n",svc_cnt,attr_idx,(attr_list->func).write);
959 bk_printf("reg fun[%d][%d] I 0x%x\r\n",svc_cnt,attr_idx,(attr_list->func).indicate);
960 memcpy(&g_ohos_Op_Func_cb[svc_cnt][attr_idx],&(attr_list->func),sizeof(BleGattOperateFunc));
961 //bk_printf("g fun[%d][%d] W:%x\r\n",svc_cnt,attr_idx,g_ohos_Op_Func_cb[svc_cnt][attr_idx].write);
962 attr_bk_list++;
963
964 if(attr_list->properties&(OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY|OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE)){
965 memcpy(attr_bk_list,&attrListPattern[BK_Attr_IDX_CFG],sizeof(ble_attm_desc_t));
966 attr_bk_list++;
967 }
968 attr_list++;
969 attr_idx++;
970 }else{
971 BleConvertAttr2BK(attr_bk_list,attr_list);
972 attr_bk_list++;
973 attr_list++;
974 }
975 attr_num--;
976
977 }
978
979 ble_err_t status = BK_ERR_BLE_SUCCESS;
980
981
982 ble_db_cfg.att_db = ohos_server_uuid_para[svc_cnt]->ohos_att_db;
983 ble_db_cfg.att_db_nb = attr_num_total;//srvcInfo->attrNum;
984 ble_db_cfg.prf_task_id =svc_cnt;
985 ble_db_cfg.start_hdl = 0;
986 #if 1
987 for(i=0,attr_bk_list=&ohos_server_uuid_para[svc_cnt]->ohos_att_db[0];i<attr_num_total;i++,attr_bk_list++)
988 {
989 for(int m=0;m<16;m++)
990 printf("%02x ",attr_bk_list->uuid[m]);
991 printf("perm:0x%02x,ext_perm:0x%02x,max_size:%d\r\n",attr_bk_list->perm,attr_bk_list->ext_perm,attr_bk_list->max_size);
992 printf("\r\n-----------------------\r\n");
993 }
994 #endif
995
996 status = bk_ble_create_db(&ble_db_cfg);
997 if (status == BK_ERR_BLE_SUCCESS) svc_cnt++;
998
999 return (status==BK_ERR_BLE_SUCCESS)?OHOS_BT_STATUS_SUCCESS:OHOS_BT_STATUS_FAIL;
1000 }
1001
BleGattsStopServiceEx(int srvcHandle)1002 int BleGattsStopServiceEx(int srvcHandle)
1003 {
1004 (void)srvcHandle;
1005 return OHOS_BT_STATUS_SUCCESS;
1006 }
1007
1008
1009