1 /*
2 * Copyright (c) 2021 WinnerMicro 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 #include <string.h>
17 #include <stdio.h>
18 #include <stdint.h>
19 #include "ble_util.h"
20 #include "my_stdbool.h"
21 #include "ohos_bt_gatt.h"
22 #include "ohos_bt_gatt_server.h"
23
24 enum BLE_GATT_ATTR_MUN {
25 ATTR_NUM_0,
26 ATTR_NUM_1,
27 ATTR_NUM_2,
28 ATTR_NUM_3,
29 ATTR_NUM_4,
30 ATTR_MAX_NUM,
31 };
32
33 static int test_indicate(int *arg);
34
35 /* 写数据 */
char_write(uint8_t * data,int length)36 int char_write(uint8_t *data, int length)
37 {
38 tls_bt_dump_hexstring("char_write:", data, length);
39 }
40
41 /* 读数据 */
char_read(uint8_t * data,int * length)42 int char_read(uint8_t *data, int *length)
43 {
44 const int data_len = 2;
45
46 data[0] = '1';
47 data[1] = '1';
48 *length = data_len;
49 }
50
char2_write(uint8_t * data,int length)51 int char2_write(uint8_t *data, int length)
52 {
53 tls_bt_dump_hexstring("char2_write:", data, length);
54 }
55
char2_read(uint8_t * data,int * length)56 int char2_read(uint8_t *data, int *length)
57 {
58 const int data_len = 2;
59
60 data[0] = '2';
61 data[1] = '2';
62 *length = data_len;
63 }
64
65 /* 移除蓝牙服务 */
test_remove_service(int * arg)66 void test_remove_service(int *arg)
67 {
68 printf("Free server if=0\r\n");
69 BleStopAdv(0);
70 ble_server_free(0);
71 ble_server_start_service();
72 }
73
74 /* 添加蓝牙服务 */
test_add_service3(void)75 void test_add_service3(void)
76 {
77 int ret, server_if;
78 uint16_t uuid;
79
80 uint8_t uuid_128_service[16] = {
81 0x00, 0x91, 0x8A, 0xEF, 0x39, 0xDD, 0x84, 0xA4,
82 0xFC, 0x43, 0x77, 0xA2, 0x00, 0xE4, 0xF1, 0x15
83 };
84
85 uint8_t uuid_128_char1[16] = {
86 0x00, 0x91, 0x8A, 0xEF, 0x39, 0xDD, 0x84, 0xA4,
87 0xFC, 0x43, 0x77, 0xA2, 0x01, 0xE4, 0xF1, 0x15
88 };
89
90 BleGattService service;
91 BleGattOperateFunc func;
92 BleGattAttr attr[ATTR_MAX_NUM];
93
94 attr[ATTR_NUM_0].attrType = OHOS_BLE_ATTRIB_TYPE_SERVICE;
95 attr[ATTR_NUM_0].uuidType = OHOS_UUID_TYPE_128_BIT;
96 memcpy_s(attr[ATTR_NUM_0].uuid, sizeof(attr[ATTR_NUM_0].uuid),
97 uuid_128_service, sizeof(uuid_128_service));
98
99 attr[ATTR_NUM_1].attrType = OHOS_BLE_ATTRIB_TYPE_CHAR;
100 attr[ATTR_NUM_1].uuidType = OHOS_UUID_TYPE_128_BIT;
101 memcpy_s(attr[ATTR_NUM_1].uuid, sizeof(attr[ATTR_NUM_1].uuid),
102 uuid_128_char1, sizeof(uuid_128_char1));
103
104 func.write = char_write;
105 func.read = char_read;
106 attr[ATTR_NUM_1].func = func;
107 attr[ATTR_NUM_1].properties = 0x0A;
108 attr[ATTR_NUM_1].permission = 0x00;
109
110 attr[ATTR_NUM_2].attrType = OHOS_BLE_ATTRIB_TYPE_CHAR_USER_DESCR;
111 attr[ATTR_NUM_2].uuidType = OHOS_UUID_TYPE_16_BIT;
112 uuid = 0x2902;
113 memcpy_s(attr[ATTR_NUM_2].uuid, sizeof(attr[ATTR_NUM_2].uuid), &uuid, sizeof(uuid));
114 func.write = char_write;
115 func.read = char_read;
116 attr[ATTR_NUM_2].func = func;
117 attr[ATTR_NUM_2].permission = 0x11;
118
119 service.attrNum = ATTR_NUM_3;
120 service.attrList = &attr[ATTR_NUM_0];
121
122 ret = BleGattsStartServiceEx(&server_if, &service);
123 printf("adding service1, ret=%d, server_if=%d\r\n", ret, server_if);
124 }
125
126 uint8_t indicate_data[128] = {
127 0x01, 0x4F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x11, 0x09, 0x6e,
128 0x65, 0x74, 0x43, 0x66, 0x67, 0x56, 0x65, 0x72, 0x09, 0x00,
129 0x7b, 0x22, 0x76, 0x65, 0x72, 0x22, 0x3a, 0x32, 0x7d
130 };
131
test_indicate(int * arg)132 int test_indicate(int *arg)
133 {
134 (void)arg;
135 return OHOS_BT_STATUS_SUCCESS;
136 }
137
138 uint8_t uuid_128_service[16] = {
139 0x00, 0x91, 0x8A, 0xEF, 0x39, 0xDD, 0x84, 0xA4,
140 0xFC, 0x43, 0x77, 0xA2, 0x00, 0xE6, 0xF1, 0x15
141 };
142
143 uint8_t uuid_128_char1[16] = {
144 0x00, 0x91, 0x8A, 0xEF, 0x39, 0xDD, 0x84, 0xA4,
145 0xFC, 0x43, 0x77, 0xA2, 0x01, 0xE6, 0xF1, 0x15
146 };
147
148 uint8_t uuid_128_char2[16] = {
149 0x00, 0x91, 0x8A, 0xEF, 0x39, 0xDD, 0x84, 0xA4,
150 0xFC, 0x43, 0x77, 0xA2, 0x02, 0xE6, 0xF1, 0x15
151 };
152
test_add_service(void)153 void test_add_service(void)
154 {
155 int ret, server_if;
156 uint16_t uuid;
157
158 BleGattService service;
159 BleGattOperateFunc func;
160 BleGattAttr attr[ATTR_MAX_NUM];
161
162 attr[ATTR_NUM_0].attrType = OHOS_BLE_ATTRIB_TYPE_SERVICE;
163 attr[ATTR_NUM_0].uuidType = OHOS_UUID_TYPE_128_BIT;
164 memcpy_s(attr[ATTR_NUM_0].uuid, sizeof(attr[ATTR_NUM_0].uuid),
165 uuid_128_service, sizeof(uuid_128_service));
166
167 attr[ATTR_NUM_1].attrType = OHOS_BLE_ATTRIB_TYPE_CHAR;
168 attr[ATTR_NUM_1].uuidType = OHOS_UUID_TYPE_128_BIT;
169 memcpy_s(attr[ATTR_NUM_1].uuid, sizeof(attr[ATTR_NUM_1].uuid),
170 uuid_128_char1, sizeof(uuid_128_char1));
171
172 func.write = char_write;
173 func.read = char_read;
174 attr[ATTR_NUM_1].func = func;
175 attr[ATTR_NUM_1].properties = 0x22;
176 attr[ATTR_NUM_1].permission = 0x00;
177
178 attr[ATTR_NUM_2].attrType = OHOS_BLE_ATTRIB_TYPE_CHAR_USER_DESCR;
179 attr[ATTR_NUM_2].uuidType = OHOS_UUID_TYPE_16_BIT;
180 uuid = 0x2902;
181 memcpy_s(attr[ATTR_NUM_2].uuid, sizeof(attr[ATTR_NUM_2].uuid), &uuid, sizeof(uuid));
182 func.write = char_write;
183 func.read = char_read;
184 attr[ATTR_NUM_2].func = func;
185 attr[ATTR_NUM_2].permission = 0x11;
186
187 attr[ATTR_NUM_3].attrType = OHOS_BLE_ATTRIB_TYPE_CHAR;
188 attr[ATTR_NUM_3].uuidType = OHOS_UUID_TYPE_128_BIT;
189 memcpy_s(attr[ATTR_NUM_3].uuid, sizeof(attr[ATTR_NUM_3].uuid),
190 uuid_128_char2, sizeof(uuid_128_char2));
191
192 func.write = char_write;
193 func.read = char_read;
194 attr[ATTR_NUM_3].func = func;
195 attr[ATTR_NUM_3].properties = 0x08;
196 attr[ATTR_NUM_3].permission = 0x00;
197
198 attr[ATTR_NUM_4].attrType = OHOS_BLE_ATTRIB_TYPE_CHAR_USER_DESCR;
199 attr[ATTR_NUM_4].uuidType = OHOS_UUID_TYPE_16_BIT;
200 uuid = 0x2902;
201 memcpy_s(attr[ATTR_NUM_4].uuid, sizeof(attr[ATTR_NUM_4].uuid), &uuid, sizeof(uuid));
202 func.write = char2_write;
203 func.read = char2_read;
204 attr[ATTR_NUM_4].func = func;
205 attr[ATTR_NUM_4].permission = 0x11;
206
207 service.attrNum = ATTR_MAX_NUM;
208 service.attrList = &attr[ATTR_NUM_0];
209
210 ret = BleGattsStartServiceEx(&server_if, &service);
211 printf("adding service1, ret=%d, server_if=%d\r\n", ret, server_if);
212 }
213
test_add_service2(void)214 void test_add_service2(void)
215 {
216 int ret, server_if;
217 uint16_t uuid;
218
219 BleGattService service;
220 BleGattOperateFunc func;
221 BleGattAttr attr[ATTR_MAX_NUM];
222 attr[ATTR_NUM_0].attrType = OHOS_BLE_ATTRIB_TYPE_SERVICE;
223 attr[ATTR_NUM_0].uuidType = OHOS_UUID_TYPE_16_BIT;
224 uuid = 0x1826;
225 memcpy_s(attr[ATTR_NUM_0].uuid, sizeof(attr[ATTR_NUM_0].uuid), &uuid, sizeof(uuid));
226
227 attr[ATTR_NUM_1].attrType = OHOS_BLE_ATTRIB_TYPE_CHAR;
228 attr[ATTR_NUM_1].uuidType = OHOS_UUID_TYPE_16_BIT;
229 uuid = 0x2abc;
230 memcpy_s(attr[ATTR_NUM_1].uuid, sizeof(attr[ATTR_NUM_1].uuid), &uuid, sizeof(uuid));
231 func.write = char2_write;
232 func.read = char2_read;
233 attr[ATTR_NUM_1].func = func;
234 attr[ATTR_NUM_1].properties = 0x28;
235 attr[ATTR_NUM_1].permission = 0x00;
236 attr[ATTR_NUM_2].attrType = OHOS_BLE_ATTRIB_TYPE_CHAR;
237 attr[ATTR_NUM_2].uuidType = OHOS_UUID_TYPE_16_BIT;
238 uuid = 0x2ab8;
239 memcpy_s(attr[ATTR_NUM_2].uuid, sizeof(attr[ATTR_NUM_2].uuid), &uuid, sizeof(uuid));
240 func.write = char2_write;
241 attr[ATTR_NUM_2].func = func;
242 attr[ATTR_NUM_2].properties = 0x28;
243 attr[ATTR_NUM_2].permission = 0x00;
244 service.attrNum = ATTR_NUM_3;
245 service.attrList = &attr[ATTR_NUM_0];
246
247 ret = BleGattsStartServiceEx(&server_if, &service);
248 printf("adding service2, ret=%d, server_if=%d\r\n", ret, server_if);
249 }
250
251 /* 注册服务的回调 */
test_registerServerCallback(int status,int serverId,BtUuid * appUuid)252 void test_registerServerCallback(int status, int serverId, BtUuid *appUuid)
253 {
254 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
255 }
256
257 /* 连接服务的回调 */
test_connectServerCallback(int connId,int serverId,BdAddr * bdAddr)258 void test_connectServerCallback(int connId, int serverId, BdAddr *bdAddr)
259 {
260 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
261 }
262
263 /* 断开服务的回调 */
test_disconnectServerCallback(int connId,int serverId,BdAddr * bdAddr)264 void test_disconnectServerCallback(int connId, int serverId, BdAddr *bdAddr)
265 {
266 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
267 }
268
269 /* 添加服务的回调 */
test_serviceAddCallback(int status,int serverId,BtUuid * uuid,int srvcHandle)270 void test_serviceAddCallback(int status, int serverId, BtUuid *uuid, int srvcHandle)
271 {
272 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
273 }
274
test_includeServiceAddCallback(int status,int serverId,int srvcHandle,int includeSrvcHandle)275 void test_includeServiceAddCallback(int status, int serverId, int srvcHandle, int includeSrvcHandle)
276 {
277 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
278 }
279
test_characteristicAddCallback(int status,int serverId,BtUuid * uuid,int srvcHandle,int characteristicHandle)280 void test_characteristicAddCallback(int status, int serverId, BtUuid *uuid,
281 int srvcHandle, int characteristicHandle)
282 {
283 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
284 }
285
test_descriptorAddCallback(int status,int serverId,BtUuid * uuid,int srvcHandle,int descriptorHandle)286 void test_descriptorAddCallback(int status, int serverId, BtUuid *uuid,
287 int srvcHandle, int descriptorHandle)
288 {
289 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
290 }
291
292 /* 启动服务的回调 */
test_serviceStartCallback(int status,int serverId,int srvcHandle)293 void test_serviceStartCallback(int status, int serverId, int srvcHandle)
294 {
295 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
296 }
297
298 /* 停止服务的回调 */
test_serviceStopCallback(int status,int serverId,int srvcHandle)299 void test_serviceStopCallback(int status, int serverId, int srvcHandle)
300 {
301 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
302 }
303
304 /* 删除服务的回调 */
test_serviceDeleteCallback(int status,int serverId,int srvcHandle)305 void test_serviceDeleteCallback(int status, int serverId, int srvcHandle)
306 {
307 printf("%s serverId=%d\r\n", __FUNCTION__, serverId);
308 }
309
310 /* 读数据请求回调 */
test_requestReadCallback(BtReqReadCbPara readCbPara)311 void test_requestReadCallback(BtReqReadCbPara readCbPara)
312 {
313 printf("%s serverId=%d\r\n", __FUNCTION__, 0);
314 }
315
316 /* 写数据请求的回调 */
test_requestWriteCallback(BtReqWriteCbPara writeCbPara)317 void test_requestWriteCallback(BtReqWriteCbPara writeCbPara)
318 {
319 printf("%s serverId=%d\r\n", __FUNCTION__, 0);
320 }
321
322 /* 确认回复的回调 */
test_responseConfirmationCallback(int status,int handle)323 void test_responseConfirmationCallback(int status, int handle)
324 {
325 printf("%s serverId=%d\r\n", __FUNCTION__, 0);
326 }
327
test_indicationSentCallback(int connId,int status)328 void test_indicationSentCallback(int connId, int status)
329 {
330 printf("%s connId=%d\r\n", __FUNCTION__, connId);
331 }
332
333 /* 修改最大传输单元的数值 */
test_mtuChangeCallback(int connId,int mtu)334 void test_mtuChangeCallback(int connId, int mtu)
335 {
336 printf("%s connId=%d\r\n", __FUNCTION__, connId);
337 }
338
339 static BtGattServerCallbacks scb = {
340 test_registerServerCallback,
341 test_connectServerCallback,
342 test_disconnectServerCallback,
343 test_serviceAddCallback,
344 test_includeServiceAddCallback,
345 test_characteristicAddCallback,
346 test_descriptorAddCallback,
347 test_serviceStartCallback,
348 test_serviceStopCallback,
349 test_serviceDeleteCallback,
350 test_requestReadCallback,
351 test_requestWriteCallback,
352 test_responseConfirmationCallback,
353 test_indicationSentCallback,
354 test_mtuChangeCallback,
355 };
356
357 /* 使能广播的回调 */
test_gap_adv_enable_callback(int advId,int status)358 void test_gap_adv_enable_callback(int advId, int status)
359 {
360 (void)advId;
361 printf("%s status=%d\r\n", __FUNCTION__, status);
362 }
363
364 /* 禁用广播的回调 */
test_gap_adv_disable_callback(int advId,int status)365 void test_gap_adv_disable_callback(int advId, int status)
366 {
367 (void)advId;
368 printf("%s status=%d\r\n", __FUNCTION__, status);
369 }
370
371 /* 设置广播数据的回调 */
test_gap_adv_data_set_callback(int advId,int status)372 void test_gap_adv_data_set_callback(int advId, int status)
373 {
374 (void)advId;
375 printf("%s status=%d\r\n", __FUNCTION__, status);
376 }
377
378 /* 广播更新回调 */
test_gap_adv_update_callback(int advId,int status)379 void test_gap_adv_update_callback(int advId, int status)
380 {
381 (void)advId;
382 printf("%s status=%d\r\n", __FUNCTION__, status);
383 }
384
test_gap_sec_response_callback(const BdAddr * bdAddr)385 void test_gap_sec_response_callback(const BdAddr *bdAddr)
386 {
387 printf("%s addr ", __FUNCTION__);
388 for (int i = 0; i < OHOS_BD_ADDR_LEN; i++) printf("%02x:", bdAddr->addr[i]);
389 printf("\r\n");
390 }
391
392 /* 扫描结果的回调 */
test_gap_scan_result_callback(BtScanResultData * scanResultdata)393 void test_gap_scan_result_callback(BtScanResultData *scanResultdata)
394 {
395 int i;
396
397 printf("scan result: ");
398 for (int i = 0; i < OHOS_BD_ADDR_LEN; i++) printf("%02x:", scanResultdata->addr.addr[i]);
399 printf("\r\n");
400
401 for (int i = 0; i < scanResultdata->advLen; i++) printf("%02x", scanResultdata->advData[i]);
402 printf("\r\n");
403 }
404
405 /* 扫描参数设置的回调测试 */
test_gap_scan_param_set_callback(int clientId,int status)406 void test_gap_scan_param_set_callback(int clientId, int status)
407 {
408 (void)clientId;
409 printf("%s status=%d\r\n", __FUNCTION__, status);
410 }
411
412 static BtGattServerCallbacks gcb = {
413 test_gap_adv_enable_callback,
414 test_gap_adv_disable_callback,
415 test_gap_adv_data_set_callback,
416 test_gap_adv_update_callback,
417 test_gap_sec_response_callback,
418 test_gap_scan_result_callback,
419 test_gap_scan_param_set_callback,
420 };
421
ble_server_demo(void)422 void ble_server_demo(void)
423 {
424 StartAdvRawData raw;
425 BleAdvParams param;
426
427 memset_s(&raw, sizeof(raw), 0, sizeof(raw));
428 memset_s(¶m, sizeof(param), 0, sizeof(param));
429
430 uint8_t adv_data[] = {
431 0x02, 0x01, 0x06, 0x15, 0x16, 0xEE, 0xFD, 0x01, 0x01, 0x07, 0x04, 0x00, 0x11,
432 0xF8, 0x12, 0x44, 0x30, 0x30, 0x32, 0xFF, 0x00, 0x04, 0x02, 0x02, 0x01
433 };
434
435 uint8_t scan_data[] = {
436 0x0B, 0x09,
437 'H', 'e', 'l', 'l', 'o', ',', 'B', 'L', 'E', '!'
438 };
439
440 raw.advDataLen = sizeof(adv_data)/sizeof(adv_data[0]);
441 raw.advData = adv_data;
442
443 raw.rspDataLen = sizeof(scan_data)/sizeof(scan_data[0]);
444 raw.rspData = scan_data;
445
446 param.advFilterPolicy = 0;
447 param.advType = OHOS_BLE_ADV_IND;
448 param.channelMap = 0x07;
449 param.ownAddrType = 0;
450 param.duration = 0;
451 param.maxInterval = 0x60;
452 param.minInterval = 0x40;
453
454 /* register gap callback */
455 BleGattRegisterCallbacks(&gcb);
456
457 /* register gatt server callback */
458 BleGattsRegisterCallbacks(&scb);
459
460 printf("Adding service....\r\n");
461
462 test_add_service();
463 printf("Runnng service....\r\n");
464
465 printf("Starting advertisment...\r\n");
466 BleStartAdvEx(1, raw, param);
467 }
468