1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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 #ifndef __ESP_BT_DEFS_H__
16 #define __ESP_BT_DEFS_H__
17 
18 #include <stdint.h>
19 #include <stdbool.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #define ESP_BLUEDROID_STATUS_CHECK(status)           \
26     if (esp_bluedroid_get_status() != (status)) {    \
27         return ESP_ERR_INVALID_STATE;                \
28     }
29 
30 
31 /* relate to BT_STATUS_xxx in bt_def.h */
32 /// Status Return Value
33 typedef enum {
34     ESP_BT_STATUS_SUCCESS       = 0,            /* relate to BT_STATUS_SUCCESS in bt_def.h */
35     ESP_BT_STATUS_FAIL,                         /* relate to BT_STATUS_FAIL in bt_def.h */
36     ESP_BT_STATUS_NOT_READY,                    /* relate to BT_STATUS_NOT_READY in bt_def.h */
37     ESP_BT_STATUS_NOMEM,                        /* relate to BT_STATUS_NOMEM in bt_def.h */
38     ESP_BT_STATUS_BUSY,                         /* relate to BT_STATUS_BUSY in bt_def.h */
39     ESP_BT_STATUS_DONE          = 5,            /* relate to BT_STATUS_DONE in bt_def.h */
40     ESP_BT_STATUS_UNSUPPORTED,                  /* relate to BT_STATUS_UNSUPPORTED in bt_def.h */
41     ESP_BT_STATUS_PARM_INVALID,                 /* relate to BT_STATUS_PARM_INVALID in bt_def.h */
42     ESP_BT_STATUS_UNHANDLED,                    /* relate to BT_STATUS_UNHANDLED in bt_def.h */
43     ESP_BT_STATUS_AUTH_FAILURE,                 /* relate to BT_STATUS_AUTH_FAILURE in bt_def.h */
44     ESP_BT_STATUS_RMT_DEV_DOWN  = 10,           /* relate to BT_STATUS_RMT_DEV_DOWN in bt_def.h */
45     ESP_BT_STATUS_AUTH_REJECTED,                /* relate to BT_STATUS_AUTH_REJECTED in bt_def.h */
46     ESP_BT_STATUS_INVALID_STATIC_RAND_ADDR,     /* relate to BT_STATUS_INVALID_STATIC_RAND_ADDR in bt_def.h */
47     ESP_BT_STATUS_PENDING,                      /* relate to BT_STATUS_PENDING in bt_def.h */
48     ESP_BT_STATUS_UNACCEPT_CONN_INTERVAL,       /* relate to BT_UNACCEPT_CONN_INTERVAL in bt_def.h */
49     ESP_BT_STATUS_PARAM_OUT_OF_RANGE,           /* relate to BT_PARAM_OUT_OF_RANGE in bt_def.h */
50     ESP_BT_STATUS_TIMEOUT,                      /* relate to BT_STATUS_TIMEOUT in bt_def.h */
51     ESP_BT_STATUS_PEER_LE_DATA_LEN_UNSUPPORTED, /* relate to BTM_PEER_LE_DATA_LEN_UNSUPPORTED in stack/btm_api.h */
52     ESP_BT_STATUS_CONTROL_LE_DATA_LEN_UNSUPPORTED,/* relate to BTM_CONTROL_LE_DATA_LEN_UNSUPPORTED in stack/btm_api.h */
53     ESP_BT_STATUS_ERR_ILLEGAL_PARAMETER_FMT,    /* relate to HCI_ERR_ILLEGAL_PARAMETER_FMT in stack/hcidefs.h */
54     ESP_BT_STATUS_MEMORY_FULL   = 20,           /* relate to BT_STATUS_MEMORY_FULL in bt_def.h */
55     ESP_BT_STATUS_EIR_TOO_LARGE,                /* relate to BT_STATUS_EIR_TOO_LARGE in bt_def.h */
56 } esp_bt_status_t;
57 
58 
59 /*Define the bt octet 16 bit size*/
60 #define ESP_BT_OCTET16_LEN    16
61 typedef uint8_t esp_bt_octet16_t[ESP_BT_OCTET16_LEN];   /* octet array: size 16 */
62 
63 #define ESP_BT_OCTET8_LEN    8
64 typedef uint8_t esp_bt_octet8_t[ESP_BT_OCTET8_LEN];   /* octet array: size 8 */
65 
66 typedef uint8_t esp_link_key[ESP_BT_OCTET16_LEN];      /* Link Key */
67 
68 /// Default GATT interface id
69 #define ESP_DEFAULT_GATT_IF             0xff
70 
71 #define ESP_BLE_CONN_INT_MIN                0x0006       /*!< relate to BTM_BLE_CONN_INT_MIN in stack/btm_ble_api.h */
72 #define ESP_BLE_CONN_INT_MAX                0x0C80       /*!< relate to BTM_BLE_CONN_INT_MAX in stack/btm_ble_api.h */
73 #define ESP_BLE_CONN_LATENCY_MAX            499          /*!< relate to ESP_BLE_CONN_LATENCY_MAX in stack/btm_ble_api.h */
74 #define ESP_BLE_CONN_SUP_TOUT_MIN           0x000A       /*!< relate to BTM_BLE_CONN_SUP_TOUT_MIN in stack/btm_ble_api.h */
75 #define ESP_BLE_CONN_SUP_TOUT_MAX           0x0C80       /*!< relate to ESP_BLE_CONN_SUP_TOUT_MAX in stack/btm_ble_api.h */
76 #define ESP_BLE_CONN_PARAM_UNDEF            0xffff       /* use this value when a specific value not to be overwritten */ /* relate to ESP_BLE_CONN_PARAM_UNDEF in stack/btm_ble_api.h */
77 #define ESP_BLE_SCAN_PARAM_UNDEF            0xffffffff   /* relate to ESP_BLE_SCAN_PARAM_UNDEF in stack/btm_ble_api.h */
78 
79 /// Check the param is valid or not
80 #define ESP_BLE_IS_VALID_PARAM(x, min, max)  (((x) >= (min) && (x) <= (max)) || ((x) == ESP_BLE_CONN_PARAM_UNDEF))
81 
82 /// UUID type
83 typedef struct {
84 #define ESP_UUID_LEN_16     2
85 #define ESP_UUID_LEN_32     4
86 #define ESP_UUID_LEN_128    16
87     uint16_t len;							/*!< UUID length, 16bit, 32bit or 128bit */
88     union {
89         uint16_t    uuid16;                 /*!< 16bit UUID */
90         uint32_t    uuid32;                 /*!< 32bit UUID */
91         uint8_t     uuid128[ESP_UUID_LEN_128]; /*!< 128bit UUID */
92     } uuid;									/*!< UUID */
93 } __attribute__((packed)) esp_bt_uuid_t;
94 
95 /// Bluetooth device type
96 typedef enum {
97     ESP_BT_DEVICE_TYPE_BREDR   = 0x01,
98     ESP_BT_DEVICE_TYPE_BLE     = 0x02,
99     ESP_BT_DEVICE_TYPE_DUMO    = 0x03,
100 } esp_bt_dev_type_t;
101 
102 /// Bluetooth address length
103 #define ESP_BD_ADDR_LEN     6
104 
105 /// Bluetooth device address
106 typedef uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN];
107 
108 /// BLE device address type
109 typedef enum {
110     BLE_ADDR_TYPE_PUBLIC        = 0x00,
111     BLE_ADDR_TYPE_RANDOM        = 0x01,
112     BLE_ADDR_TYPE_RPA_PUBLIC    = 0x02,
113     BLE_ADDR_TYPE_RPA_RANDOM    = 0x03,
114 } esp_ble_addr_type_t;
115 
116 /// white list address type
117 typedef enum {
118     BLE_WL_ADDR_TYPE_PUBLIC        = 0x00,
119     BLE_WL_ADDR_TYPE_RANDOM        = 0x01,
120 } esp_ble_wl_addr_type_t;
121 
122 /// Used to exchange the encryption key in the init key & response key
123 #define ESP_BLE_ENC_KEY_MASK    (1 << 0)            /* relate to BTM_BLE_ENC_KEY_MASK in stack/btm_api.h */
124 /// Used to exchange the IRK key in the init key & response key
125 #define ESP_BLE_ID_KEY_MASK     (1 << 1)            /* relate to BTM_BLE_ID_KEY_MASK in stack/btm_api.h */
126 /// Used to exchange the CSRK key in the init key & response key
127 #define ESP_BLE_CSR_KEY_MASK    (1 << 2)            /* relate to BTM_BLE_CSR_KEY_MASK in stack/btm_api.h */
128 /// Used to exchange the link key(this key just used in the BLE & BR/EDR coexist mode) in the init key & response key
129 #define ESP_BLE_LINK_KEY_MASK   (1 << 3)            /* relate to BTM_BLE_LINK_KEY_MASK in stack/btm_api.h */
130 typedef uint8_t esp_ble_key_mask_t;            /* the key mask type */
131 
132 /// Minimum of the application id
133 #define ESP_APP_ID_MIN  0x0000
134 /// Maximum of the application id
135 #define ESP_APP_ID_MAX  0x7fff
136 
137 #define ESP_BD_ADDR_STR         "%02x:%02x:%02x:%02x:%02x:%02x"
138 #define ESP_BD_ADDR_HEX(addr)   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 #endif     /* __ESP_BT_DEFS_H__ */
145