1 /*
2  * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __BLE_H__
18 #define __BLE_H__
19 
20 #include <stdint.h>
21 
22 #include <aos/list.h>
23 #include <aos/uuid.h>
24 #include <aos/gatt.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define BLE_ARRAY_NUM(a) (sizeof(a) / sizeof((a)[0]))
31 #define BLE_MIN(a, b) ((a) < (b) ? (a) : (b))
32 #define BLE_CHAR_RANGE_CHECK(a, b, c, d) \
33     do { \
34         if (((c) >= (a)) && ((c) < ((a) + (b)))) \
35         { \
36             d = c - a; \
37         } \
38         else \
39         { \
40             return 0; \
41         } \
42     } while(0);
43 
44 #define ATT_ERR_INVALID_HANDLE          0x01
45 #define ATT_ERR_READ_NOT_PERMITTED      0x02
46 #define ATT_ERR_WRITE_NOT_PERMITTED     0x03
47 #define ATT_ERR_INVALID_PDU             0x04
48 #define ATT_ERR_AUTHENTICATION          0x05
49 #define ATT_ERR_NOT_SUPPORTED           0x06
50 #define ATT_ERR_INVALID_OFFSET          0x07
51 #define ATT_ERR_AUTHORIZATION           0x08
52 #define ATT_ERR_PREPARE_QUEUE_FULL      0x09
53 #define ATT_ERR_ATTRIBUTE_NOT_FOUND     0x0a
54 #define ATT_ERR_ATTRIBUTE_NOT_LONG      0x0b
55 #define ATT_ERR_ENCRYPTION_KEY_SIZE     0x0c
56 #define ATT_ERR_INVALID_ATTRIBUTE_LEN   0x0d
57 #define ATT_ERR_UNLIKELY                0x0e
58 #define ATT_ERR_INSUFFICIENT_ENCRYPTION 0x0f
59 #define ATT_ERR_UNSUPPORTED_GROUP_TYPE  0x10
60 #define ATT_ERR_INSUFFICIENT_RESOURCES  0x11
61 
62 typedef struct _conn_param_t {
63     uint16_t interval_min;
64     uint16_t interval_max;
65     uint16_t latency;
66     uint16_t timeout;
67 } conn_param_t;
68 
69 enum {
70     BLE_STACK_OK = 0,
71     BLE_STACK_ERR_NULL,
72     BLE_STACK_ERR_PARAM,
73     BLE_STACK_ERR_INTERNAL,
74     BLE_STACK_ERR_INIT,
75     BLE_STACK_ERR_CONN,
76     BLE_STACK_ERR_ALREADY,
77 };
78 
79 typedef enum {
80     DEV_ADDR_LE_PUBLIC    =  0x00,
81     DEV_ADDR_LE_RANDOM    =   0x01,
82     DEV_ADDR_LE_PUBLIC_ID  =  0x02,
83     DEV_ADDR_LE_RANDOM_ID  =  0x03,
84 } adv_addr_type_en;
85 
86 typedef enum {
87     ADV_IND =                 0x00,
88     ADV_DIRECT_IND =          0x01,
89     ADV_SCAN_IND =            0x02,
90     ADV_NONCONN_IND =         0x03,
91     ADV_DIRECT_IND_LOW_DUTY = 0x04,
92 } adv_type_en;
93 
94 #ifndef EVENT_BLE
95 #define EVENT_BLE 0x05000000
96 #endif
97 
98 typedef enum {
99     EVENT_STACK_INIT = EVENT_BLE,
100 
101     EVENT_GAP_CONN_CHANGE,
102     EVENT_GAP_DEV_FIND,
103     EVENT_GAP_CONN_PARAM_REQ,
104     EVENT_GAP_CONN_PARAM_UPDATE,
105     EVENT_GAP_CONN_SECURITY_CHANGE,
106     EVENT_GAP_ADV_TIMEOUT,
107 
108     EVENT_GATT_NOTIFY,
109     EVENT_GATT_INDICATE = EVENT_GATT_NOTIFY,
110     EVENT_GATT_CHAR_READ,
111     EVENT_GATT_CHAR_WRITE,
112     EVENT_GATT_INDICATE_CB,
113     EVENT_GATT_CHAR_READ_CB,
114     EVENT_GATT_CHAR_WRITE_CB,
115     EVENT_GATT_CHAR_CCC_CHANGE,
116     EVENT_GATT_CHAR_CCC_WRITE,
117     EVENT_GATT_CHAR_CCC_MATCH,
118     EVENT_GATT_MTU_EXCHANGE,
119 
120     EVENT_GATT_DISCOVERY_SVC,
121     EVENT_GATT_DISCOVERY_INC_SVC,
122     EVENT_GATT_DISCOVERY_CHAR,
123     EVENT_GATT_DISCOVERY_CHAR_DES,
124     EVENT_GATT_DISCOVERY_COMPLETE,
125 
126     EVENT_SMP_PASSKEY_DISPLAY,
127     EVENT_SMP_PASSKEY_CONFIRM,
128     EVENT_SMP_PASSKEY_ENTER,
129     EVENT_SMP_PAIRING_CONFIRM,
130     EVENT_SMP_PAIRING_COMPLETE,
131     EVENT_SMP_CANCEL,
132 
133     EVENT_STACK_UNKNOWN,
134 } ble_event_en;
135 
136 typedef enum {
137     DISCONNECTED,
138     CONNECTED,
139 } conn_state_en;
140 
141 #pragma pack(1)
142 
143 typedef struct _dev_addr_t {
144     uint8_t type;
145     uint8_t val[6];
146 } dev_addr_t;
147 
148 typedef struct _evt_data_gap_conn_change_t {
149     int16_t conn_handle;
150     int16_t err;
151     conn_state_en connected;
152 } evt_data_gap_conn_change_t;
153 
154 typedef struct _evt_data_gap_conn_param_req_t {
155     int16_t conn_handle;
156     int8_t accept;
157     conn_param_t param;
158 } evt_data_gap_conn_param_req_t;
159 
160 typedef struct _evt_data_gap_conn_param_update_t {
161     int16_t conn_handle;
162     uint16_t interval;
163     uint16_t latency;
164     uint16_t timeout;
165 } evt_data_gap_conn_param_update_t;
166 
167 typedef struct _evt_data_gap_security_change_t {
168     int16_t conn_handle;
169     uint16_t level;
170     uint8_t err;
171 } evt_data_gap_security_change_t;
172 
173 typedef struct _evt_data_gap_dev_find_t {
174     dev_addr_t dev_addr;
175     adv_type_en adv_type;
176     uint8_t adv_len;
177     int8_t rssi;
178     uint8_t adv_data[31];
179 } evt_data_gap_dev_find_t;
180 
181 typedef struct _evt_data_gatt_notify_t {
182     int16_t conn_handle;
183     int16_t char_handle;
184     uint8_t len;
185     const uint8_t *data;
186 } evt_data_gatt_notify_t;
187 
188 typedef evt_data_gatt_notify_t evt_data_gatt_indicate_t;
189 
190 typedef struct _evt_data_gatt_indicate_cb_t {
191     int16_t conn_handle;
192     int16_t char_handle;
193     int err;
194 } evt_data_gatt_indicate_cb_t;
195 
196 typedef struct _evt_data_gatt_write_cb_t {
197     int16_t conn_handle;
198     int16_t char_handle;
199     int err;
200 } evt_data_gatt_write_cb_t;
201 
202 typedef struct _evt_data_gatt_read_cb_t {
203     int16_t conn_handle;
204     int err;
205     uint16_t len;
206     const uint8_t *data;
207 } evt_data_gatt_read_cb_t;
208 
209 
210 typedef struct _evt_data_gatt_char_read_t {
211     int16_t conn_handle;
212     int16_t char_handle;
213     int32_t len;
214     uint8_t *data;
215     uint16_t offset;
216 } evt_data_gatt_char_read_t;
217 
218 typedef struct _evt_data_gatt_char_write_t {
219     int16_t conn_handle;
220     int16_t char_handle;
221     int32_t len;
222     const uint8_t *data;
223     uint16_t offset;
224     uint8_t flag;
225 } evt_data_gatt_char_write_t;
226 
227 typedef struct _evt_data_gatt_char_ccc_change_t {
228     int16_t char_handle;
229     ccc_value_en ccc_value;
230 } evt_data_gatt_char_ccc_change_t;
231 
232 typedef struct _evt_data_gatt_char_ccc_write_t {
233     int16_t conn_handle;
234     int16_t char_handle;
235     ccc_value_en ccc_value;
236     int8_t allow_write;
237 } evt_data_gatt_char_ccc_write_t;
238 
239 typedef struct _evt_data_gatt_char_ccc_match_t {
240     int16_t conn_handle;
241     int16_t char_handle;
242     int8_t is_matched;
243 } evt_data_gatt_char_ccc_match_t;
244 
245 typedef struct _evt_data_gatt_mtu_exchange_t {
246     int16_t conn_handle;
247     int err;
248 } evt_data_gatt_mtu_exchange_t;
249 
250 typedef struct _evt_data_smp_passkey_display_t {
251     int16_t conn_handle;
252     dev_addr_t peer_addr;
253     char *passkey;
254 } evt_data_smp_passkey_display_t;
255 
256 typedef struct _evt_data_smp_passkey_confirm_t {
257     int16_t conn_handle;
258     dev_addr_t peer_addr;
259     char *passkey;
260 } evt_data_smp_passkey_confirm_t;
261 
262 typedef struct _evt_data_smp_passkey_enter_t {
263     int16_t conn_handle;
264     dev_addr_t peer_addr;
265 } evt_data_smp_passkey_enter_t;
266 
267 typedef struct _evt_data_smp_pairing_confirm_t {
268     int16_t conn_handle;
269     dev_addr_t peer_addr;
270 } evt_data_smp_pairing_confirm_t;
271 
272 typedef struct _evt_data_smp_pairing_complete_t {
273     int16_t conn_handle;
274     dev_addr_t peer_addr;
275     int8_t bonded;
276     int16_t err;
277 } evt_data_smp_pairing_complete_t;
278 
279 typedef struct _evt_data_smp_cancel_t {
280     int16_t conn_handle;
281     dev_addr_t peer_addr;
282 } evt_data_smp_cancel_t;
283 
284 typedef struct _evt_data_gatt_discovery_svc_t {
285     int16_t conn_handle;
286     uint16_t start_handle;
287     uint16_t end_handle;
288     union {
289         uuid_t uuid;
290         struct ut_uuid_16 uuid16;
291         struct ut_uuid_32 uuid32;
292         struct ut_uuid_128 uuid128;
293     };
294 } evt_data_gatt_discovery_svc_t;
295 
296 typedef struct _evt_data_gatt_discovery_inc_svc_t {
297     int16_t conn_handle;
298     uint16_t attr_handle;
299     uint16_t start_handle;
300     uint16_t end_handle;
301     union {
302         uuid_t uuid;
303         struct ut_uuid_16 uuid16;
304         struct ut_uuid_32 uuid32;
305         struct ut_uuid_128 uuid128;
306     };
307 } evt_data_gatt_discovery_inc_svc_t;
308 
309 typedef struct _evt_data_gatt_discovery_char_t {
310     int16_t conn_handle;
311     uint16_t attr_handle;
312     uint16_t val_handle;
313     uint16_t props;
314     union {
315         uuid_t uuid;
316         struct ut_uuid_16 uuid16;
317         struct ut_uuid_32 uuid32;
318         struct ut_uuid_128 uuid128;
319     };
320 } evt_data_gatt_discovery_char_t;
321 
322 typedef struct _evt_data_gatt_discovery_char_des_t {
323     int16_t conn_handle;
324     uint16_t attr_handle;
325     union {
326         uuid_t uuid;
327         struct ut_uuid_16 uuid16;
328         struct ut_uuid_32 uuid32;
329         struct ut_uuid_128 uuid128;
330     };
331 } evt_data_gatt_discovery_char_des_t;
332 
333 typedef struct _evt_data_gatt_discovery_complete_t {
334     int16_t conn_handle;
335     int err;
336 } evt_data_gatt_discovery_complete_t;
337 
338 #pragma pack()
339 
340 typedef int (*event_callback_func_t)(ble_event_en event, void *event_data);
341 
342 typedef struct _ble_event_cb_t {
343     event_callback_func_t callback;
344     slist_t next;
345 } ble_event_cb_t;
346 
347 typedef struct _init_param_t {
348     char *dev_name;
349     dev_addr_t *dev_addr;
350     uint16_t conn_num_max;
351 } init_param_t;
352 
353 typedef enum {
354     AD_DATA_TYPE_FLAGS =          0x01,  /* AD flags */
355     AD_DATA_TYPE_UUID16_SOME =    0x02,  /* 16-bit UUID, more available */
356     AD_DATA_TYPE_UUID16_ALL =     0x03,  /* 16-bit UUID, all listed */
357     AD_DATA_TYPE_UUID32_SOME =    0x04,  /* 32-bit UUID, more available */
358     AD_DATA_TYPE_UUID32_ALL =     0x05,  /* 32-bit UUID, all listed */
359     AD_DATA_TYPE_UUID128_SOME =   0x06,  /* 128-bit UUID, more available */
360     AD_DATA_TYPE_UUID128_ALL =    0x07,  /* 128-bit UUID, all listed */
361     AD_DATA_TYPE_NAME_SHORTENED = 0x08,  /* Shortened name */
362     AD_DATA_TYPE_NAME_COMPLETE =  0x09,  /* Complete name */
363     AD_DATA_TYPE_TX_POWER =       0x0a,  /* Tx Power */
364     AD_DATA_TYPE_SOLICIT16 =      0x14,  /* Solicit UUIDs, 16-bit */
365     AD_DATA_TYPE_SOLICIT128 =     0x15,  /* Solicit UUIDs, 128-bit */
366     AD_DATA_TYPE_SVC_DATA16 =     0x16,  /* Service data, 16-bit UUID */
367     AD_DATA_TYPE_GAP_APPEARANCE = 0x19,  /* GAP appearance */
368     AD_DATA_TYPE_SOLICIT32 =      0x1f,  /* Solicit UUIDs, 32-bit */
369     AD_DATA_TYPE_SVC_DATA32 =     0x20,  /* Service data, 32-bit UUID */
370     AD_DATA_TYPE_SVC_DATA128 =    0x21,  /* Service data, 128-bit UUID */
371     AD_DATA_TYPE_URI =            0x24,  /* URI */
372     AD_DATA_TYPE_MESH_PROV =      0x29,  /* Mesh Provisioning PDU */
373     AD_DATA_TYPE_MESH_MESSAGE =   0x2a,  /* Mesh Networking PDU */
374     AD_DATA_TYPE_MESH_BEACON =    0x2b,  /* Mesh Beacon */
375 
376     AD_DATA_TYPE_MANUFACTURER_DATA = 0xff,  /* Manufacturer Specific Data */
377 } ad_date_type_en;
378 
379 typedef enum {
380     AD_FLAG_LIMITED = 0x01, /* Limited Discoverable */
381     AD_FLAG_GENERAL = 0x02, /* General Discoverable */
382     AD_FLAG_NO_BREDR = 0x04, /* BR/EDR not supported */
383 } ad_flag_en;
384 
385 typedef enum {
386     ADV_FAST_INT_MIN_1   = 0x0030,  /* 30 ms    */
387     ADV_FAST_INT_MAX_1   = 0x0060,  /* 60 ms    */
388     ADV_FAST_INT_MIN_2   = 0x00a0,  /* 100 ms   */
389     ADV_FAST_INT_MAX_2   = 0x00f0,  /* 150 ms   */
390     ADV_SLOW_INT_MIN     = 0x0640,  /* 1 s      */
391     ADV_SLOW_INT_MAX     = 0x0780,  /* 1.2 s    */
392     ADV_SCAN_INT_MIN_1   = 0x0120,  /* 112.5 ms   */
393     ADV_SCAN_INT_MAX_1   = 0x0140,  /* 200 ms   */
394 } adv_interval_en;
395 
396 typedef struct _ad_data_t {
397     uint8_t type;
398     uint8_t len;
399     uint8_t *data;
400 } ad_data_t;
401 
402 typedef enum {
403     ADV_FILTER_POLICY_ANY_REQ   = 0,  /* any scan request or connect request */
404     ADV_FILTER_POLICY_SCAN_REQ  = 1,  /* all connect request, white list scan request */
405     ADV_FILTER_POLICY_CONN_REQ  = 2,  /* all scan request, white list connect request */
406     ADV_FILTER_POLICY_ALL_REQ   = 3,  /* white list scan request and connect request */
407 } adv_filter_policy_en;
408 typedef enum {
409     ADV_CHAN_37 = 0x01,
410     ADV_CHAN_38 = 0x02,
411     ADV_CHAN_39 = 0x04,
412 } adv_chan_en;
413 #define ADV_DEFAULT_CHAN_MAP (ADV_CHAN_37 | ADV_CHAN_38 | ADV_CHAN_39)
414 typedef struct _adv_param_t {
415     adv_type_en type;
416     ad_data_t *ad;
417     ad_data_t *sd;
418     uint8_t ad_num;
419     uint8_t sd_num;
420     uint16_t interval_min;
421     uint16_t interval_max;
422     adv_filter_policy_en filter_policy;
423     adv_chan_en channel_map;
424     dev_addr_t direct_peer_addr;
425 } adv_param_t;
426 
427 typedef enum {
428     SCAN_PASSIVE = 0x00,
429     SCAN_ACTIVE = 0x01,
430 } scan_type_en;
431 
432 typedef enum {
433     SCAN_FILTER_DUP_DISABLE = 0x00,
434     SCAN_FILTER_DUP_ENABLE = 0x01,
435 } scan_filter_en;
436 
437 typedef enum {
438     SCAN_FAST_INTERVAL   = 0x0060,  /* 60 ms    */
439     SCAN_SLOW_INTERVAL_1 = 0x0800,  /* 1.28 s   */
440     SCAN_SLOW_INTERVAL_2 = 0x1000,  /* 2.56 s   */
441 } scan_interval_en;
442 
443 typedef enum {
444     SCAN_FAST_WINDOW     = 0x0030,  /* 30 ms    */
445     SCAN_SLOW_WINDOW     = 0x0012,  /* 11.25 ms */
446 } scan_window_en;
447 
448 typedef enum {
449     SCAN_FILTER_POLICY_ANY_ADV        = 0,  /* any adv packages  */
450     SCAN_FILTER_POLICY_WHITE_LIST     = 1,  /* white list adv packages */
451 } scan_filter_policy_en;
452 typedef struct _scan_param_t {
453     scan_type_en  type;
454 
455     scan_filter_en  filter_dup;
456 
457     uint16_t interval;
458 
459     uint16_t window;
460     scan_filter_policy_en scan_filter;
461 } scan_param_t;
462 
463 typedef enum {
464     CONN_INT_MIN_INTERVAL = 0x0018, /* 30 ms    */
465     CONN_INT_MAX_INTERVAL = 0x0028, /* 50 ms    */
466 } conn_interval_en;
467 
468 typedef enum {
469     IO_CAP_IN_NONE = 0x01,
470     IO_CAP_IN_YESNO = 0x02,
471     IO_CAP_IN_KEYBOARD = 0x04,
472 
473     IO_CAP_OUT_NONE = 0x08,
474     IO_CAP_OUT_DISPLAY = 0x10,
475 } io_cap_en;
476 
477 typedef struct _connect_info_t {
478     int16_t conn_handle;
479     uint8_t role;
480     uint16_t interval;
481     uint16_t latency;
482     uint16_t timeout;
483     dev_addr_t local_addr;
484     dev_addr_t peer_addr;
485 } connect_info_t;
486 
487 typedef enum {
488     /** Only for BR/EDR special cases, like SDP */
489     SECURITY_NONE,
490     /** No encryption and no authentication. */
491     SECURITY_LOW,
492     /** Encryption and no authentication (no MITM). */
493     SECURITY_MEDIUM,
494     /** Encryption and authentication (MITM). */
495     SECURITY_HIGH,
496     /** Authenticated Secure Connections */
497     SECURITY_FIPS,
498 } security_en;
499 
500 int ble_stack_init(init_param_t *param);
501 int ble_stack_set_name(const char *name);
502 int ble_stack_iocapability_set(uint8_t io_cap);
503 
504 int ble_stack_event_register(ble_event_cb_t *callback);
505 
506 int ble_stack_adv_start(adv_param_t *param);
507 int ble_stack_adv_stop();
508 
509 int ble_stack_scan_start(const scan_param_t *param);
510 int ble_stack_scan_stop();
511 
512 int ble_stack_get_local_addr(dev_addr_t *addr);
513 
514 int ble_stack_connect(dev_addr_t *peer_addr, conn_param_t *param, uint8_t auto_connect);
515 int ble_stack_disconnect(int16_t conn_handle);
516 int ble_stack_connect_info_get(int16_t conn_handle, connect_info_t *info);
517 int ble_stack_security(int16_t conn_handle, security_en level);
518 int ble_stack_connect_param_update(int16_t conn_handle, conn_param_t *param);
519 
520 int ble_stack_smp_passkey_entry(int16_t conn_handle, uint32_t passkey);
521 
522 int ble_stack_smp_cancel(int16_t conn_handle);
523 
524 int ble_stack_smp_passkey_confirm(int16_t conn_handle);
525 
526 int ble_stack_smp_pairing_confirm(int16_t conn_handle);
527 
528 int ble_stack_setting_load();
529 
530 int ble_stack_dev_unpair(dev_addr_t *peer_addr);
531 
532 int ble_stack_enc_key_size_get(int16_t conn_handle);
533 
534 int ble_stack_white_list_clear();
535 int ble_stack_white_list_add(dev_addr_t *peer_addr);
536 int ble_stack_white_list_remove(dev_addr_t *peer_addr);
537 int ble_stack_white_list_size();
538 int ble_stack_check_conn_params(const conn_param_t *param);
539 
540 
541 #ifdef __cplusplus
542 }
543 #endif
544 
545 #endif   /* __BLE_H__ */
546 
547