1 /*
2 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
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 #include "string.h"
17 #include "lega_wlan_api.h"
18 #include "sonata_ble_api.h"
19 #include "app.h"
20 #include "sonata_utils_api.h"
21 #include "sonata_gap_api.h"
22 #include "sonata_gap.h"
23
24 #include "ohos_bt_gatt.h"
25 #include "ohos_bt_gatt_server.h"
26 #include "ohos_bt_gatt_client.h"
27
28 static BtGattServerCallbacks gatts_callback = {0};
29 static BtGattCallbacks bt_gattcallback = {0};
30
31 #define print_log printf
32 #define BT_STATUS_SUCCESS (0)
33 #define BT_STATUS_FAIL (-1)
34
35 #define ATT_BT_SIG_UUID_128 {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, \
36 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
37 #define ATT_SIG_UUID_128_LEN 0x0010
38 #define ATT_SIG_UUID_16_LEN 0x0002
39
40 int BleGattsSetEncryption(BdAddr bdAddr, BleSecAct secAct);
41
42 int BleGattsSendIndication(int serverId, GattsSendIndParam *param);
43 static int8_t test_value[5] = {0x2, 0x02, 0x02, 0x04, 0x05};
44 int test_handle = 0;
45
test_ind(void)46 void test_ind(void)
47 {
48 GattsSendIndParam indParam = {
49 .connectId = 0,
50 .attrHandle = test_handle + 2,
51 .confirm = 1,
52 .valueLen = 5,
53 .value = test_value,
54
55 };
56
57 static uint8_t count = 0;
58 count++;
59 test_value[4] = count;
60 BleGattsSendIndication(0, &indParam);
61 }
62
app_core_evt_ind_cb_h(app_core_evt_ind_t evt,void * p_param)63 int app_core_evt_ind_cb_h(app_core_evt_ind_t evt, void *p_param)
64 {
65
66 print_log("harmony : %s evt %d\r\n", __FUNCTION__, evt);
67 switch (evt) {
68 case BLE_SERVICE_ADD_CMP:
69 if (gatts_callback.serviceStartCb != NULL) {
70 app_reg_service_cmp_t *param = (app_reg_service_cmp_t *)p_param;
71 gatts_callback.serviceStartCb(param->status, 0, param->handler);
72 }
73 break;
74 case BLE_DEV_CONNECTED:
75 if (gatts_callback.connectServerCb != NULL) {
76 app_connect_status_ind_t *param = (app_connect_status_ind_t *)p_param;
77 gatts_callback.connectServerCb(param->connId, 0, (BdAddr *)param->addr);
78 }
79 break;
80 case BLE_DEV_DISCONNECTED:
81 if (gatts_callback.disconnectServerCb != NULL) {
82 app_connect_status_ind_t *param = (app_connect_status_ind_t *)p_param;
83 gatts_callback.disconnectServerCb(param->connId, 0, (BdAddr *)param->addr);
84 }
85 break;
86 case BLE_IND_SENT:
87 if (gatts_callback.indicationSentCb != NULL) {
88 app_ind_sent_ind_t *param = (app_ind_sent_ind_t *)p_param;
89 gatts_callback.indicationSentCb(param->connId, param->status);
90 }
91 break;
92 case BLE_MTU_CHANGE:
93 if (gatts_callback.mtuChangeCb != NULL) {
94 app_mtu_change_ind_t *param = (app_mtu_change_ind_t *)p_param;
95 gatts_callback.mtuChangeCb(param->connId, param->mtu);
96 }
97 break;
98 case BLE_ADV_START:
99 if (bt_gattcallback.advEnableCb != NULL) {
100 app_adv_status_ind_t *param = (app_adv_status_ind_t *)p_param;
101 bt_gattcallback.advEnableCb(param->advId, param->status);
102 }
103 break;
104 case BLE_ADV_STOP:
105 if (bt_gattcallback.advDisableCb != NULL) {
106 app_adv_status_ind_t *param = (app_adv_status_ind_t *)p_param;
107 bt_gattcallback.advDisableCb(param->advId, param->status);
108 }
109 break;
110 default :
111 break;
112 }
113 return 0;
114 }
115
116 /* Initialize the Bluetooth protocol stack. */
InitBtStack(void)117 int InitBtStack(void)
118 {
119 return BT_STATUS_SUCCESS;
120 }
121
122 /* Bluetooth protocol stack enable */
EnableBtStack(void)123 int EnableBtStack(void)
124 {
125 print_log("harmony : %s \r\n", __FUNCTION__);
126 app_register_core_evt_ind(app_core_evt_ind_cb_h);
127 app_set_connect_flag(1);
128 app_ble_stack_start(USER_OHOS_MODULE_ID);
129 return BT_STATUS_SUCCESS;
130 }
131
132 /* Bluetooth protocol stack disable */
DisableBtStack(void)133 int DisableBtStack(void)
134 {
135 print_log("harmony : %s \r\n", __FUNCTION__);
136 app_register_core_evt_ind(NULL);
137 app_set_connect_flag(0);
138 app_ble_stack_stop(USER_OHOS_MODULE_ID);
139 return BT_STATUS_SUCCESS;
140 }
141
SetDeviceName(const char * name,unsigned int len)142 int SetDeviceName(const char *name, unsigned int len)
143 {
144 print_log("harmony : %s \r\n", __FUNCTION__);
145 app_ble_set_device_name((uint8_t *)name, len);
146 return BT_STATUS_SUCCESS;
147 }
148
io_cap_h2a(BleIoCapMode mode)149 static uint8_t io_cap_h2a(BleIoCapMode mode)
150 {
151 uint8_t io_Cap_temp = mode;
152 return io_Cap_temp;
153 }
154
BleSetSecurityIoCap(BleIoCapMode mode)155 int BleSetSecurityIoCap(BleIoCapMode mode)
156 {
157 uint8_t app_io_cap = io_cap_h2a(mode);
158 app_set_security_io_cap(app_io_cap);
159 return BT_STATUS_SUCCESS;
160 }
161
sec_auth_req_h2a(BleAuthReqMode mode)162 static uint8_t sec_auth_req_h2a(BleAuthReqMode mode)
163 {
164 uint8_t auth_req_temp ;
165 switch (mode) {
166 case OHOS_BLE_AUTH_NO_BOND:
167 auth_req_temp = SONATA_GAP_AUTH_REQ_NO_MITM_NO_BOND;
168 break;
169 case OHOS_BLE_AUTH_BOND:
170 auth_req_temp = SONATA_GAP_AUTH_BOND;
171 break;
172 case OHOS_BLE_AUTH_REQ_MITM:
173 auth_req_temp = SONATA_GAP_AUTH_MITM;
174 break;
175 case OHOS_BLE_AUTH_REQ_SC_ONLY:
176 auth_req_temp = SONATA_GAP_AUTH_SEC_CON;
177 break;
178 case OHOS_BLE_AUTH_REQ_SC_BOND:
179 auth_req_temp = SONATA_GAP_AUTH_REQ_SEC_CON_BOND ;
180 break;
181 case OHOS_BLE_AUTH_REQ_SC_MITM:
182 auth_req_temp = SONATA_GAP_AUTH_REQ_SEC_CON_NO_BOND;
183 break;
184 case OHOS_BLE_AUTH_REQ_SC_MITM_BOND:
185 auth_req_temp = SONATA_GAP_AUTH_REQ_SEC_CON_BOND;
186 break;
187 }
188 return auth_req_temp;
189 }
190
BleSetSecurityAuthReq(BleAuthReqMode mode)191 int BleSetSecurityAuthReq(BleAuthReqMode mode)
192 {
193 print_log("harmony : %s %d\r\n", __FUNCTION__, mode);
194 uint8_t auth_req = sec_auth_req_h2a(mode);
195 app_set_security_auth_req(auth_req);
196 return BT_STATUS_SUCCESS;
197 }
198
BleGattSecurityRsp(BdAddr bdAddr,bool accept)199 int BleGattSecurityRsp(BdAddr bdAddr, bool accept)
200 {
201 print_log("harmony : %s %d\r\n", __FUNCTION__, accept);
202 app_gap_notify_pair_request_rsp((uint8_t *)bdAddr.addr, accept);
203 return BT_STATUS_SUCCESS;
204 }
205
BleGattsDisconnect(int serverId,BdAddr bdAddr,int connId)206 int BleGattsDisconnect(int serverId, BdAddr bdAddr, int connId)
207 {
208 if (0 != app_ble_disconnect_by_addr((uint8_t *)bdAddr.addr)) {
209 return BT_STATUS_FAIL;
210 }
211 return BT_STATUS_SUCCESS;
212 }
213
perm_2_perm(uint8_t user_perm,uint8_t user_prop,uint16_t * p_perm)214 static void perm_2_perm(uint8_t user_perm, uint8_t user_prop, uint16_t *p_perm)
215 {
216 if ((user_prop & OHOS_GATT_CHARACTER_PROPERTY_BIT_READ) == OHOS_GATT_CHARACTER_PROPERTY_BIT_READ) {
217 if ((user_perm & OHOS_GATT_PERMISSION_READ) == OHOS_GATT_PERMISSION_READ) {
218 *p_perm |= PRD_NA;
219 }
220 if ((user_perm & OHOS_GATT_PERMISSION_READ_ENCRYPTED) == OHOS_GATT_PERMISSION_READ_ENCRYPTED) {
221 *p_perm |= PRD_UA;
222 }
223 }
224
225 if ((user_prop & OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE) == OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE) {
226 if ((user_perm & OHOS_GATT_PERMISSION_WRITE) == OHOS_GATT_PERMISSION_WRITE) {
227 *p_perm |= PWR_NA;
228 }
229 if ((user_perm & OHOS_GATT_PERMISSION_WRITE_ENCRYPTED) == OHOS_GATT_PERMISSION_WRITE_ENCRYPTED) {
230 *p_perm |= PWR_UA;
231 }
232 }
233 if ((user_prop & OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP) == OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE_NO_RSP) {
234 if ((user_perm & OHOS_GATT_PERMISSION_WRITE) == OHOS_GATT_PERMISSION_WRITE) {
235 *p_perm |= PWC_NA;
236 }
237 if ((user_perm & OHOS_GATT_PERMISSION_WRITE_ENCRYPTED) == OHOS_GATT_PERMISSION_WRITE_ENCRYPTED) {
238 *p_perm |= PWC_UA;
239 }
240 }
241 if ((user_prop & OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY) == OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY) {
242 if ((user_perm & OHOS_GATT_PERMISSION_READ_ENCRYPTED) == OHOS_GATT_PERMISSION_READ_ENCRYPTED
243 || (user_perm & OHOS_GATT_PERMISSION_WRITE_ENCRYPTED) == OHOS_GATT_PERMISSION_WRITE_ENCRYPTED) {
244 *p_perm |= PNTF_UA;
245 } else {
246 *p_perm |= PNTF_NA;
247 }
248
249 }
250 if ((user_prop & OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE) == OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE) {
251 if ((user_perm & OHOS_GATT_PERMISSION_READ_ENCRYPTED) == OHOS_GATT_PERMISSION_READ_ENCRYPTED
252 || (user_perm & OHOS_GATT_PERMISSION_WRITE_ENCRYPTED) == OHOS_GATT_PERMISSION_WRITE_ENCRYPTED) {
253 *p_perm |= PIND_UA;
254 } else {
255 *p_perm |= PIND_NA;
256 }
257 }
258
259 print_log("[Harmony]user_perm= %x prop = %x out : %x\r\n", user_perm, user_prop, *p_perm);
260 }
261
ble_ohos_add_service(ble_gatt_att_reg_t * dst_attr,BleGattAttr * service_attr)262 ble_gatt_att_reg_t *ble_ohos_add_service(ble_gatt_att_reg_t *dst_attr, BleGattAttr *service_attr)
263 {
264 if (service_attr->attrType != OHOS_BLE_ATTRIB_TYPE_SERVICE) {
265 return dst_attr;
266 }
267 if (service_attr->uuidType == OHOS_UUID_TYPE_128_BIT) {
268 print_log("[Harmony]service %x %x %x %x\r\n", service_attr->uuid[0], service_attr->uuid[1], service_attr->uuid[2],
269 service_attr->uuid[3]);
270 memmove(dst_attr->att_desc.uuid, service_attr->uuid, sizeof(dst_attr->att_desc.uuid));
271 SONATA_PERM_SET(dst_attr->att_desc.ext_perm, UUID_LEN, 2);
272 SONATA_PERM_SET(dst_attr->att_desc.perm, SVC_UUID_LEN, 2);
273 } else if (service_attr->uuidType == OHOS_UUID_TYPE_16_BIT) {
274 memmove(dst_attr->att_desc.uuid, service_attr->uuid, 2);
275 }
276 return dst_attr;
277 }
278
ble_ohos_add_char(ble_gatt_att_reg_t * dst_attr,BleGattAttr * char_attr,bool isLast)279 ble_gatt_att_reg_t *ble_ohos_add_char(ble_gatt_att_reg_t *dst_attr, BleGattAttr *char_attr, bool isLast)
280 {
281 if (char_attr->attrType != OHOS_BLE_ATTRIB_TYPE_CHAR) {
282 return dst_attr;
283 }
284 print_log("[Harmony]char %x %x %x %x\r\n", char_attr->uuid[0], char_attr->uuid[1], char_attr->uuid[2],
285 char_attr->uuid[3]);
286
287 uint8_t char_uuid[ATT_SIG_UUID_128_LEN] = {0x03, 0x28, 0x0};
288
289 memmove(dst_attr->att_desc.uuid, char_uuid, ATT_SIG_UUID_128_LEN);
290
291 dst_attr->att_desc.perm = PRD_NA;
292 dst_attr->att_desc.max_len = 0;
293 dst_attr->att_desc.ext_perm = 0;
294
295 dst_attr++;
296
297 if (char_attr->uuidType == OHOS_UUID_TYPE_128_BIT) {
298 memmove(dst_attr->att_desc.uuid, char_attr->uuid, sizeof(dst_attr->att_desc.uuid));
299 SONATA_PERM_SET(dst_attr->att_desc.ext_perm, UUID_LEN, 2);
300 } else if (char_attr->uuidType == OHOS_UUID_TYPE_16_BIT) {
301 memmove(dst_attr->att_desc.uuid, char_attr->uuid, 2);
302 }
303 dst_attr->att_opr.ind_cb = char_attr->func.indicate;
304 dst_attr->att_opr.read_request = char_attr->func.read;
305 dst_attr->att_opr.write_request = char_attr->func.write;
306 perm_2_perm(char_attr->permission, char_attr->properties, &dst_attr->att_desc.perm);
307 dst_attr->att_desc.ext_perm |= PRI;
308 dst_attr->att_desc.max_len = 512;
309
310 if (((char_attr->properties & OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE) == OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE
311 || (char_attr->properties & OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY) == OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY)
312 && isLast) {
313 dst_attr++;
314
315 uint8_t char_uuid2[ATT_SIG_UUID_128_LEN] = {0x02, 0x29, 0x0};
316 memmove(dst_attr->att_desc.uuid, char_uuid2, ATT_SIG_UUID_128_LEN);
317
318 dst_attr->att_desc.perm = PRD_NA | PWR_NA;
319 dst_attr->att_desc.max_len = 2;
320 dst_attr->att_desc.ext_perm |= PRI;
321 }
322
323 return dst_attr;
324 }
325
BleGattServiceRead1(unsigned char * buff,unsigned int * len)326 int BleGattServiceRead1(unsigned char *buff, unsigned int *len)
327 {
328 print_log("harmony : %s \r\n", __FUNCTION__);
329 uint8_t readvalue[5] = {0x1, 0x2, 0x3, 0x4, 0x5};
330 *len = sizeof(readvalue);
331 memmove(buff, readvalue, *len);
332 test_ind();
333 return 0;
334 }
335
BleGattServiceWrite1(unsigned char * buff,unsigned int len)336 int BleGattServiceWrite1(unsigned char *buff, unsigned int len)
337 {
338 print_log("harmony : %s \r\n", __FUNCTION__);
339 test_ind();
340 return 0;
341 }
342
BleGattServiceIndicate1(unsigned char * buff,unsigned int len)343 int BleGattServiceIndicate1(unsigned char *buff, unsigned int len)
344 {
345 print_log("harmony : %s \r\n", __FUNCTION__);
346 return 0;
347 }
348
349 BleGattOperateFunc opr_func = {
350 .read = BleGattServiceRead1,
351 .write = BleGattServiceWrite1,
352 .indicate = BleGattServiceIndicate1,
353 };
354
355 BleGattAttr h_attr1 = {
356 .attrType = OHOS_BLE_ATTRIB_TYPE_SERVICE,
357 .permission = OHOS_GATT_PERMISSION_READ,
358 .uuidType = OHOS_UUID_TYPE_16_BIT,
359 .uuid = {0x11, 0x22, 0x33},
360 .value = NULL,
361 .valLen = 0,
362 .properties = 0,
363 .func = {0},
364
365 };
366 BleGattAttr h_attr2 = {
367 .attrType = OHOS_BLE_ATTRIB_TYPE_CHAR,
368 .permission = OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE,
369 .uuidType = OHOS_UUID_TYPE_16_BIT,
370 .uuid = {0x11, 0x22, 0x66},
371 .value = NULL,
372 .valLen = 0,
373 .properties = OHOS_GATT_CHARACTER_PROPERTY_BIT_WRITE | OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE,
374 .func = {
375 .read = BleGattServiceRead1,
376 .write = BleGattServiceWrite1,
377 .indicate = BleGattServiceIndicate1,
378 },
379
380 };
381
382 BleGattAttr h_attr3 = {
383 .attrType = OHOS_BLE_ATTRIB_TYPE_SERVICE,
384 .permission = OHOS_GATT_PERMISSION_READ,
385 .uuidType = OHOS_UUID_TYPE_16_BIT,
386 .uuid = {0x22, 0x22, 0x33},
387 .value = NULL,
388 .valLen = 0,
389 .properties = 0,
390 .func = {0},
391
392 };
393 BleGattAttr h_attr4 = {
394 .attrType = OHOS_BLE_ATTRIB_TYPE_CHAR,
395 .permission = OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE,
396 .uuidType = OHOS_UUID_TYPE_16_BIT,
397 .uuid = {0x33, 0x52, 0x66},
398 .value = NULL,
399 .valLen = 0,
400 .properties = OHOS_GATT_CHARACTER_PROPERTY_BIT_READ | OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE,
401 .func = {
402 .read = BleGattServiceRead1,
403 .write = BleGattServiceWrite1,
404 .indicate = BleGattServiceIndicate1,
405 },
406
407 };
408
409 BleGattAttr h_attr5 = {
410 .attrType = OHOS_BLE_ATTRIB_TYPE_CHAR,
411 .permission = OHOS_GATT_PERMISSION_READ | OHOS_GATT_PERMISSION_WRITE,
412 .uuidType = OHOS_UUID_TYPE_16_BIT,
413 .uuid = {0x33, 0x88, 0x66},
414 .value = NULL,
415 .valLen = 0,
416 .properties = OHOS_GATT_CHARACTER_PROPERTY_BIT_READ | OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE,
417 .func = {
418 .read = BleGattServiceRead1,
419 .write = BleGattServiceWrite1,
420 .indicate = BleGattServiceIndicate1,
421 },
422
423 };
424
test_h_add_service(void)425 void test_h_add_service(void)
426 {
427 BleGattService service_info;
428 BleGattAttr *attrList = malloc(3 * sizeof(BleGattAttr));
429 service_info.attrNum = 3;
430 service_info.attrList = attrList;
431 memmove(attrList, &h_attr1, sizeof(BleGattAttr));
432 attrList++;
433 memmove(attrList, &h_attr2, sizeof(BleGattAttr));
434 attrList++;
435 memmove(attrList, &h_attr4, sizeof(BleGattAttr));
436 int handler = 0;
437 BleGattsStartServiceEx(&handler, &service_info);
438 }
439
test_h_add_service2(void)440 void test_h_add_service2(void)
441 {
442 static uint8_t flagg = 0;
443 if (flagg == 1) {
444 return;
445 }
446 flagg++;
447 BleGattService service_info;
448 BleGattAttr *attrList = malloc(2 * sizeof(BleGattAttr));
449 if (attrList == NULL) {
450 return;
451 }
452 service_info.attrNum = 2;
453 service_info.attrList = attrList;
454 memmove(attrList, &h_attr3, sizeof(BleGattAttr));
455 attrList++;
456 memmove(attrList, &h_attr5, sizeof(BleGattAttr));
457 int handler = 0;
458 BleGattsStartServiceEx(&handler, &service_info);
459 }
460
get_service_num(unsigned int attrNum,uint8_t indicate_is_exit)461 static unsigned int get_service_num(unsigned int attrNum, uint8_t indicate_is_exit)
462 {
463 int num = 2 * (attrNum - 1) + 1 + indicate_is_exit;
464 return num;
465 }
466
BleGattsStartServiceEx(int * srvcHandle,BleGattService * srvcInfo)467 int BleGattsStartServiceEx(int *srvcHandle, BleGattService *srvcInfo)
468 {
469 if (NULL == srvcInfo) {
470 return -1;
471 }
472 print_log("[Harmony]BleGattsStartServiceEx 0x%x %d\r\n", srvcHandle, srvcInfo->attrNum);
473 unsigned int att_nb = srvcInfo->attrNum;
474 uint8_t indicate_is_exit = 0;
475 BleGattAttr *att_db = srvcInfo->attrList;
476 print_log("[Harmony]att nb %d \r\n", att_nb);
477 if ((srvcInfo->attrList[att_nb - 1].properties & OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE) ==
478 OHOS_GATT_CHARACTER_PROPERTY_BIT_INDICATE
479 || (srvcInfo->attrList[att_nb - 1].properties & OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY) ==
480 OHOS_GATT_CHARACTER_PROPERTY_BIT_NOTIFY) {
481 indicate_is_exit = 1;
482 }
483 att_nb = get_service_num(srvcInfo->attrNum, indicate_is_exit);
484 uint32_t len = (att_nb) * sizeof(ble_gatt_att_reg_t) ;
485 ble_gatt_att_reg_t *att_list;
486 ble_gatt_att_reg_t *att_temp;
487 att_list = (ble_gatt_att_reg_t *)malloc(len);
488 if (att_list == NULL) {
489 return -1;
490 }
491 att_temp = att_list;
492 memset(att_list, 0, len);
493
494 for (int i = 0; i < srvcInfo->attrNum; i++) {
495 if (att_db[i].attrType == OHOS_BLE_ATTRIB_TYPE_SERVICE) {
496 att_temp = ble_ohos_add_service(att_temp, &att_db[i]);
497
498 }
499
500 else if (att_db[i].attrType == OHOS_BLE_ATTRIB_TYPE_CHAR) {
501 bool IsLast_item = false;
502 if (i == srvcInfo->attrNum - 1) {
503 IsLast_item = true;
504 }
505 att_temp = ble_ohos_add_char(att_temp, &att_db[i], IsLast_item);
506 }
507 att_temp++;
508 }
509
510 uint16_t start_handle = *srvcHandle;
511 for (int i = 0; i < att_nb; i++) {
512 print_log("att %d %d %d\r\n", att_list[i].att_desc.perm, att_list[i].att_desc.ext_perm, att_list[i].att_desc.max_len);
513 print_log("uudi %x %x %x %x", att_list[i].att_desc.uuid[0], att_list[i].att_desc.uuid[1], att_list[i].att_desc.uuid[2],
514 att_list[i].att_desc.uuid[3]);
515 }
516 app_ble_gatt_add_svc_helper(&start_handle, att_nb, att_list);
517 int return_handle = start_handle;
518 return_handle = return_handle << 16;
519 *srvcHandle = return_handle;
520 test_handle = return_handle;
521 print_log("service handler 0x%lx", return_handle);
522 free(att_list);
523 return BT_STATUS_SUCCESS;
524 }
525
BleGattsStopServiceEx(int srvcHandle)526 int BleGattsStopServiceEx(int srvcHandle)
527 {
528 print_log("harmony : %s %d\r\n", __FUNCTION__, srvcHandle);
529
530 uint16_t handler = 0;
531 handler = srvcHandle >> 16;
532 return app_ble_disable_service_by_handler(handler);
533
534 }
535
BleGattRegisterCallbacks(BtGattCallbacks * func)536 int BleGattRegisterCallbacks(BtGattCallbacks *func)
537 {
538 print_log("harmony : %s \r\n", __FUNCTION__);
539 memmove(&bt_gattcallback, func, sizeof(BtGattCallbacks));
540 app_register_sec_cb((app_sec_req_cb)bt_gattcallback.securityRespondCb);
541 return BT_STATUS_SUCCESS;
542
543 }
544
BleGattsRegisterCallbacks(BtGattServerCallbacks * func)545 int BleGattsRegisterCallbacks(BtGattServerCallbacks *func)
546 {
547 print_log("harmony : %s \r\n", __FUNCTION__);
548 memmove(&gatts_callback, func, sizeof(BtGattServerCallbacks));
549 return BT_STATUS_SUCCESS;
550 }
551
get_indicate_att_offset(uint16_t offset)552 static uint16_t get_indicate_att_offset(uint16_t offset)
553 {
554 uint16_t r_offset = 0;
555 r_offset = 2 * (offset) ;
556 return r_offset;
557 }
558
BleGattsSendIndication(int serverId,GattsSendIndParam * param)559 int BleGattsSendIndication(int serverId, GattsSendIndParam *param)
560 {
561 if (NULL == param) {
562 return BT_STATUS_FAIL;
563 }
564 print_log("harmony : %s 0x%lx\r\n", __FUNCTION__, param->attrHandle);
565 uint16_t handler = 0;
566 uint16_t att_offset = 0;
567 int start_handler = param->attrHandle;
568 handler = start_handler >> 16;
569 att_offset = (start_handler & 0xFFFF);
570 att_offset = get_indicate_att_offset(att_offset);
571 print_log("harmony :hander %d ,offset 0x%lx\r\n", handler, att_offset);
572
573 if (param->confirm) {
574 app_ble_gatt_data_send(handler, att_offset, param->valueLen, param->value);
575 } else {
576 app_ble_gatt_data_send_notify(handler, att_offset, param->valueLen, param->value);
577 }
578 return BT_STATUS_SUCCESS;
579 }
580
ReadBtMacAddr(unsigned char * mac,unsigned int len)581 int ReadBtMacAddr(unsigned char *mac, unsigned int len)
582 {
583 uint8_t *temp_addr = sonata_get_bt_address();
584 mac[0] = temp_addr[5];
585 mac[1] = temp_addr[4];
586 mac[2] = temp_addr[3];
587 mac[3] = temp_addr[2];
588 mac[4] = temp_addr[1];
589 mac[5] = temp_addr[0];
590 return BT_STATUS_SUCCESS;
591
592 }
593
BleStartAdvEx(int * advId,const StartAdvRawData rawData,BleAdvParams advParam)594 int BleStartAdvEx(int *advId, const StartAdvRawData rawData, BleAdvParams advParam)
595 {
596 print_log("harmony : %s %d %d\r\n", __FUNCTION__, rawData.advDataLen, rawData.rspDataLen);
597 *advId = 0;
598 sonata_gap_directed_adv_create_param_t param;
599 memset(¶m, 0, sizeof(sonata_gap_directed_adv_create_param_t));
600 param.addr_type = advParam.peerAddrType;
601 param.disc_mode = SONATA_GAP_ADV_MODE_GEN_DISC;
602 param.max_tx_pwr = advParam.txPower;
603 param.filter_pol = advParam.advFilterPolicy;
604 param.adv_intv_min = advParam.minInterval;
605 param.adv_intv_max = advParam.maxInterval;
606 param.chnl_map = advParam.channelMap;
607 param.phy = SONATA_GAP_PHY_LE_1MBPS;
608 memmove(param.addr.addr, &advParam.peerAddr, SONATA_GAP_BD_ADDR_LEN);
609 if (advParam.advType == OHOS_BLE_ADV_IND) {
610 param.prop = SONATA_GAP_ADV_PROP_UNDIR_CONN_MASK;
611 } else if (advParam.advType == OHOS_BLE_ADV_DIRECT_IND_HIGH) {
612 param.prop = SONATA_GAP_ADV_PROP_DIR_CONN_HDC_MASK;
613 } else if (advParam.advType == OHOS_BLE_ADV_SCAN_IND) {
614 param.prop = SONATA_GAP_ADV_PROP_NON_CONN_SCAN_MASK;
615 } else if (advParam.advType == OHOS_BLE_ADV_NONCONN_IND) {
616 param.prop = SONATA_GAP_ADV_PROP_NON_CONN_SCAN_MASK;
617 } else if (advParam.advType == OHOS_BLE_ADV_DIRECT_IND_LOW) {
618 param.prop = SONATA_GAP_ADV_PROP_DIR_CONN_LDC_MASK;
619 }
620 ble_adv_data_set_t advdata;
621 ble_scan_data_set_t scandata;
622 advdata.advdataLen = rawData.advDataLen;
623 scandata.respdataLen = rawData.rspDataLen;
624 memmove(advdata.advdata, rawData.advData, advdata.advdataLen);
625 memmove(scandata.respdata, rawData.rspData, scandata.respdataLen);
626 app_ble_start_advertising_with_param(¶m, &advdata, &scandata, advParam.ownAddrType, advParam.duration, 0);
627 return BT_STATUS_SUCCESS;
628 }
629
sec_enc_req_h2a(BleSecAct secAct)630 static uint8_t sec_enc_req_h2a(BleSecAct secAct)
631 {
632 switch (secAct) {
633 case OHOS_BLE_SEC_NONE:
634 return 0;
635 case OHOS_BLE_SEC_ENCRYPT:
636 return SONATA_GAP_AUTH_BOND;
637 case OHOS_BLE_SEC_ENCRYPT_NO_MITM:
638 return SONATA_GAP_AUTH_SEC_CON | SONATA_GAP_AUTH_BOND;
639 case OHOS_BLE_SEC_ENCRYPT_MITM:
640 return SONATA_GAP_AUTH_MITM | SONATA_GAP_AUTH_BOND | SONATA_GAP_AUTH_MITM;
641 default:
642 return 0;
643 }
644 }
645
BleGattsSetEncryption(BdAddr bdAddr,BleSecAct secAct)646 int BleGattsSetEncryption(BdAddr bdAddr, BleSecAct secAct)
647 {
648 print_log("harmony : %s\r\n", __FUNCTION__);
649 if (OHOS_BLE_SEC_NONE == secAct) {
650 print_log("harmony :no sec req\r\n");
651 return BT_STATUS_SUCCESS;
652 }
653 uint8_t auth = sec_enc_req_h2a(secAct);
654 app_gap_connect_confirm(bdAddr.addr, auth);
655 return BT_STATUS_SUCCESS;
656 }
657
BleStopAdv(int advId)658 int BleStopAdv(int advId)
659 {
660 print_log("harmony : %s %d\r\n", __FUNCTION__, advId);
661 app_ble_stop_adv_without_id();
662 return BT_STATUS_SUCCESS;
663 }
664
665 // not used start!!!
666 /* gatt client register, callback return clientId */
BleGattcRegister(BtUuid appUuid)667 int BleGattcRegister(BtUuid appUuid)
668 {
669 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
670 return BT_STATUS_FAIL;
671 }
672
673 /* clientId from BleGattcRegister */
BleGattcUnRegister(int clientId)674 int BleGattcUnRegister(int clientId)
675 {
676 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
677 return BT_STATUS_FAIL;
678 }
679
680 /* advertising id specified by upper layer */
BleSetAdvData(int advId,const BleConfigAdvData * data)681 int BleSetAdvData(int advId, const BleConfigAdvData *data)
682 {
683 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
684 return BT_STATUS_FAIL;
685 }
686
BleStartAdv(int advId,const BleAdvParams * param)687 int BleStartAdv(int advId, const BleAdvParams *param)
688 {
689 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
690 return BT_STATUS_FAIL;
691 }
692
BleGattcRegisterCallbacks(BtGattClientCallbacks * func)693 int BleGattcRegisterCallbacks(BtGattClientCallbacks *func)
694 {
695 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
696 return BT_STATUS_FAIL;
697 }
698
BleGattsRegister(BtUuid appUuid)699 int BleGattsRegister(BtUuid appUuid)
700 {
701 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
702 return BT_STATUS_FAIL;
703 }
704
BleGattsUnRegister(int serverId)705 int BleGattsUnRegister(int serverId)
706 {
707 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
708 return BT_STATUS_FAIL;
709 }
710
BleGattsAddService(int serverId,BtUuid srvcUuid,bool isPrimary,int number)711 int BleGattsAddService(int serverId, BtUuid srvcUuid, bool isPrimary, int number)
712 {
713 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
714 return BT_STATUS_FAIL;
715 }
716
BleGattsDeleteService(int serverId,int srvcHandle)717 int BleGattsDeleteService(int serverId, int srvcHandle)
718 {
719 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
720 return BT_STATUS_FAIL;
721 }
722
BleGattsAddChar(int serverId,int srvcHandle,char * uuid,int properties,int permissions)723 int BleGattsAddChar(int serverId, int srvcHandle, char *uuid, int properties, int permissions)
724 {
725 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
726 return BT_STATUS_FAIL;
727 }
728
BleGattsAddDescr(int serverId,int srvcHandle,char * uuid,int permissions)729 int BleGattsAddDescr(int serverId, int srvcHandle, char *uuid, int permissions)
730 {
731 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
732 return BT_STATUS_FAIL;
733 }
734
BleGattsStartService(int serverId,int srvcHandle)735 int BleGattsStartService(int serverId, int srvcHandle)
736 {
737 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
738 return BT_STATUS_FAIL;
739 }
740
BleGattsStopService(int serverId,int srvcHandle)741 int BleGattsStopService(int serverId, int srvcHandle)
742 {
743 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
744 return BT_STATUS_FAIL;
745 }
746
BleGattsSendResponse(int serverId,GattsSendRspParam * param)747 int BleGattsSendResponse(int serverId, GattsSendRspParam *param)
748 {
749 print_log("harmony ERROR : %s should not be used\r\n", __FUNCTION__);
750 return BT_STATUS_FAIL;
751 }
752
753 // not used end!!!
754
755