1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <aos/errno.h>
8 #include <aos/kernel.h>
9 #include <k_api.h>
10 #include "board.h"
11 #include "aos/cli.h"
12 #include <aos/ble.h>
13 #include <bluetooth/bluetooth.h>
14 #include <bluetooth/conn.h>
15 #include <atomic.h>
16 #include <work.h>
17 #include <bluetooth/gatt.h>
18 #include <bluetooth/uuid.h>
19 static int bt_scan_start();
20 #define EXAMPLE_BLE_DEV_NAME "HaaS BLE"
21 #define DEVICE_ADDR {0xE8, 0x3B, 0xE3, 0x88, 0xB4, 0xC8}
22 static int bt_devices_find = 0;
handle_ble_cmd(char * pwbuf,int blen,int argc,char ** argv)23 static void handle_ble_cmd(char *pwbuf, int blen, int argc, char **argv)
24 {
25 int id;
26 char *onoff;
27 printf("enable ble_cmd\n");
28 bt_scan_start();
29 }
30
ble_scanf_result(uint32_t timeout)31 static int ble_scanf_result(uint32_t timeout)
32 {
33 #if 1
34 while (!bt_devices_find || timeout--) {
35 aos_msleep(500);
36 }
37 #endif
38 ble_stack_scan_stop();
39 if (bt_devices_find != 0) {
40 printf("====Result: BT test PASS !!!===\n");
41 return 0;
42 } else {
43 printf("\r\n=====BT test : FAIL===\r\n");
44 return -1;
45 }
46 }
47
device_find(ble_event_en event,void * event_data)48 static void device_find(ble_event_en event, void *event_data)
49 {
50 int ret = -1;
51 int uuid_peer = -1;
52 evt_data_gap_dev_find_t ee;
53 evt_data_gap_dev_find_t *e;
54 uint8_t find_dev = 0;
55 memcpy(&ee, event_data, sizeof(evt_data_gap_dev_find_t));
56
57 e = ⅇ
58
59 if (e->adv_type != ADV_IND) {
60 return;
61 }
62 if (e->adv_len > 31) {
63 return;
64 }
65 printf("find BT device %x-%x-%x-%x-%x-%x RssI:%d\n", e->dev_addr.val[0], e->dev_addr.val[1], e->dev_addr.val[2], e->dev_addr.val[3], e->dev_addr.val[4], e->dev_addr.val[5], e->rssi);
66 bt_devices_find++;
67 }
68
event_callback(ble_event_en event,void * event_data)69 static int event_callback(ble_event_en event, void *event_data)
70 {
71 switch (event) {
72 case EVENT_GAP_DEV_FIND:
73 device_find(event, event_data);
74 break;
75 default:
76 // printf("Unhandle event %d\n", event);
77 break;
78 }
79
80 return 0;
81 }
82
bt_scan_start()83 static int bt_scan_start()
84 {
85 int ret;
86 scan_param_t param = {
87 SCAN_PASSIVE,
88 SCAN_FILTER_DUP_ENABLE,
89 SCAN_FAST_INTERVAL,
90 SCAN_FAST_WINDOW,
91 };
92 bt_devices_find = 0;
93 ret = ble_stack_scan_start(¶m);
94
95 if (ret) {
96 return ret;
97 }
98
99 return 0;
100 }
101
102 static ble_event_cb_t ble_cb = {
103 .callback = event_callback,
104 };
105
example_BLE_init(void)106 int example_BLE_init(void)
107 {
108 int ret;
109 dev_addr_t addr = {DEV_ADDR_LE_RANDOM, DEVICE_ADDR};
110 init_param_t init = {
111 .dev_name = EXAMPLE_BLE_DEV_NAME,
112 .dev_addr = &addr,
113 .conn_num_max = 1,
114 };
115 /* we need first init hci driver */
116 hci_h4_driver_init();
117
118 /* bt stack init */
119 ret = ble_stack_init(&init);
120 if (ret) {
121 return -1;
122 }
123
124 ret = ble_stack_event_register(&ble_cb);
125 if (ret) {
126 return -1;
127 }
128
129 return 0;
130 }
131
ble_test_process()132 int ble_test_process()
133 {
134 int ret = 0;
135 printf("\r\n\r\n");
136 printf("***************************************************************\r\n");
137 printf("************************* BT Connect Test *******************\r\n");
138 printf("*How to test: find any BT devices as successed. *****\r\n");
139 printf("***************************************************************\r\n");
140 printf("=====BT Connect test : Start=====\r\n");
141 ret = example_BLE_init();
142 if (ret) {
143 return ret;
144 }
145 bt_scan_start();
146
147 return ble_scanf_result(5);
148 }
149
150 static struct cli_command ble_cmd = {
151 .name = "ble",
152 .help = "ble scan",
153 .function = handle_ble_cmd
154 };
155
ble_test(void)156 int ble_test(void)
157 {
158 aos_cli_register_command(&ble_cmd);
159 return ble_test_process();
160 }
161