25#include "nimble/porting/nimble/include/syscfg/syscfg.h"
26#include "nimble/porting/nimble/include/os/os.h"
33#define BLE_ADV_INSTANCES (MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES) + 1)
36#define BLE_ENC_BLOCK_SIZE (16)
39#define BLE_ACL_MAX_PKT_SIZE 255
41struct ble_encryption_block
43 uint8_t key[BLE_ENC_BLOCK_SIZE];
44 uint8_t plain_text[BLE_ENC_BLOCK_SIZE];
45 uint8_t cipher_text[BLE_ENC_BLOCK_SIZE];
67struct ble_mbuf_hdr_rxinfo
76#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
79#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
91#define BLE_MBUF_HDR_F_CONNECT_IND_TXD (0x4000)
92#define BLE_MBUF_HDR_F_CONNECT_REQ_TXD (0x4000)
93#define BLE_MBUF_HDR_F_CONNECT_RSP_RXD (0x0008)
94#define BLE_MBUF_HDR_F_CONN_CREDIT (0x8000)
95#define BLE_MBUF_HDR_F_IGNORED (0x8000)
96#define BLE_MBUF_HDR_F_CONN_CREDIT_INT (0x4000)
97#define BLE_MBUF_HDR_F_SCAN_REQ_TXD (0x4000)
98#define BLE_MBUF_HDR_F_INITA_RESOLVED (0x2000)
99#define BLE_MBUF_HDR_F_TARGETA_RESOLVED (0x2000)
100#define BLE_MBUF_HDR_F_EXT_ADV_SEC (0x1000)
101#define BLE_MBUF_HDR_F_EXT_ADV (0x0800)
102#define BLE_MBUF_HDR_F_RESOLVED (0x0400)
103#define BLE_MBUF_HDR_F_AUX_PTR_WAIT (0x0200)
104#define BLE_MBUF_HDR_F_AUX_INVALID (0x0100)
105#define BLE_MBUF_HDR_F_CRC_OK (0x0080)
106#define BLE_MBUF_HDR_F_DEVMATCH (0x0040)
107#define BLE_MBUF_HDR_F_MIC_FAILURE (0x0020)
108#define BLE_MBUF_HDR_F_SCAN_RSP_TXD (0x0010)
109#define BLE_MBUF_HDR_F_SCAN_RSP_RXD (0x0008)
110#define BLE_MBUF_HDR_F_RXSTATE_MASK (0x0007)
113struct ble_mbuf_hdr_txinfo
124 struct ble_mbuf_hdr_rxinfo rxinfo;
125 struct ble_mbuf_hdr_txinfo txinfo;
127 uint32_t beg_cputime;
131#define BLE_MBUF_HDR_IGNORED(hdr) \
132 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_IGNORED))
134#define BLE_MBUF_HDR_SCAN_REQ_TXD(hdr) \
135 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_SCAN_REQ_TXD))
137#define BLE_MBUF_HDR_EXT_ADV_SEC(hdr) \
138 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_EXT_ADV_SEC))
140#define BLE_MBUF_HDR_EXT_ADV(hdr) \
141 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_EXT_ADV))
143#define BLE_MBUF_HDR_DEVMATCH(hdr) \
144 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_DEVMATCH))
146#define BLE_MBUF_HDR_SCAN_RSP_RXD(hdr) \
147 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_SCAN_RSP_RXD))
149#define BLE_MBUF_HDR_AUX_INVALID(hdr) \
150 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_AUX_INVALID))
152#define BLE_MBUF_HDR_WAIT_AUX(hdr) \
153 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_AUX_PTR_WAIT))
155#define BLE_MBUF_HDR_CRC_OK(hdr) \
156 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_CRC_OK))
158#define BLE_MBUF_HDR_MIC_FAILURE(hdr) \
159 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_MIC_FAILURE))
161#define BLE_MBUF_HDR_RESOLVED(hdr) \
162 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_RESOLVED))
164#define BLE_MBUF_HDR_INITA_RESOLVED(hdr) \
165 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_INITA_RESOLVED))
167#define BLE_MBUF_HDR_TARGETA_RESOLVED(hdr) \
168 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_TARGETA_RESOLVED))
170#define BLE_MBUF_HDR_RX_STATE(hdr) \
171 ((uint8_t)((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_RXSTATE_MASK))
173#define BLE_MBUF_HDR_PTR(om) \
174 (struct ble_mbuf_hdr *)((uint8_t *)om + sizeof(struct os_mbuf) + \
175 sizeof(struct os_mbuf_pkthdr))
178#define BLE_MBUF_PKTHDR_OVERHEAD \
179 (sizeof(struct os_mbuf_pkthdr) + sizeof(struct ble_mbuf_hdr))
181#define BLE_MBUF_MEMBLOCK_OVERHEAD \
182 (sizeof(struct os_mbuf) + BLE_MBUF_PKTHDR_OVERHEAD)
185#define BLE_MBUF_HS_HDR_LEN (2)
187#define BLE_DEV_ADDR_LEN (6)
188extern uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
189extern uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
195 BLE_ERR_SUCCESS = 0x00,
196 BLE_ERR_UNKNOWN_HCI_CMD = 0x01,
197 BLE_ERR_UNK_CONN_ID = 0x02,
198 BLE_ERR_HW_FAIL = 0x03,
199 BLE_ERR_PAGE_TMO = 0x04,
200 BLE_ERR_AUTH_FAIL = 0x05,
201 BLE_ERR_PINKEY_MISSING = 0x06,
202 BLE_ERR_MEM_CAPACITY = 0x07,
203 BLE_ERR_CONN_SPVN_TMO = 0x08,
204 BLE_ERR_CONN_LIMIT = 0x09,
205 BLE_ERR_SYNCH_CONN_LIMIT = 0x0a,
206 BLE_ERR_ACL_CONN_EXISTS = 0x0b,
207 BLE_ERR_CMD_DISALLOWED = 0x0c,
208 BLE_ERR_CONN_REJ_RESOURCES = 0x0d,
209 BLE_ERR_CONN_REJ_SECURITY = 0x0e,
210 BLE_ERR_CONN_REJ_BD_ADDR = 0x0f,
211 BLE_ERR_CONN_ACCEPT_TMO = 0x10,
212 BLE_ERR_UNSUPPORTED = 0x11,
213 BLE_ERR_INV_HCI_CMD_PARMS = 0x12,
214 BLE_ERR_REM_USER_CONN_TERM = 0x13,
215 BLE_ERR_RD_CONN_TERM_RESRCS = 0x14,
216 BLE_ERR_RD_CONN_TERM_PWROFF = 0x15,
217 BLE_ERR_CONN_TERM_LOCAL = 0x16,
218 BLE_ERR_REPEATED_ATTEMPTS = 0x17,
219 BLE_ERR_NO_PAIRING = 0x18,
220 BLE_ERR_UNK_LMP = 0x19,
221 BLE_ERR_UNSUPP_REM_FEATURE = 0x1a,
222 BLE_ERR_SCO_OFFSET = 0x1b,
223 BLE_ERR_SCO_ITVL = 0x1c,
224 BLE_ERR_SCO_AIR_MODE = 0x1d,
225 BLE_ERR_INV_LMP_LL_PARM = 0x1e,
226 BLE_ERR_UNSPECIFIED = 0x1f,
227 BLE_ERR_UNSUPP_LMP_LL_PARM = 0x20,
228 BLE_ERR_NO_ROLE_CHANGE = 0x21,
229 BLE_ERR_LMP_LL_RSP_TMO = 0x22,
230 BLE_ERR_LMP_COLLISION = 0x23,
231 BLE_ERR_LMP_PDU = 0x24,
232 BLE_ERR_ENCRYPTION_MODE = 0x25,
233 BLE_ERR_LINK_KEY_CHANGE = 0x26,
234 BLE_ERR_UNSUPP_QOS = 0x27,
235 BLE_ERR_INSTANT_PASSED = 0x28,
236 BLE_ERR_UNIT_KEY_PAIRING = 0x29,
237 BLE_ERR_DIFF_TRANS_COLL = 0x2a,
239 BLE_ERR_QOS_PARM = 0x2c,
240 BLE_ERR_QOS_REJECTED = 0x2d,
241 BLE_ERR_CHAN_CLASS = 0x2e,
242 BLE_ERR_INSUFFICIENT_SEC = 0x2f,
243 BLE_ERR_PARM_OUT_OF_RANGE = 0x30,
245 BLE_ERR_PENDING_ROLE_SW = 0x32,
247 BLE_ERR_RESERVED_SLOT = 0x34,
248 BLE_ERR_ROLE_SW_FAIL = 0x35,
249 BLE_ERR_INQ_RSP_TOO_BIG = 0x36,
250 BLE_ERR_SEC_SIMPLE_PAIR = 0x37,
251 BLE_ERR_HOST_BUSY_PAIR = 0x38,
252 BLE_ERR_CONN_REJ_CHANNEL = 0x39,
253 BLE_ERR_CTLR_BUSY = 0x3a,
254 BLE_ERR_CONN_PARMS = 0x3b,
255 BLE_ERR_DIR_ADV_TMO = 0x3c,
256 BLE_ERR_CONN_TERM_MIC = 0x3d,
257 BLE_ERR_CONN_ESTABLISHMENT = 0x3e,
258 BLE_ERR_MAC_CONN_FAIL = 0x3f,
259 BLE_ERR_COARSE_CLK_ADJ = 0x40,
260 BLE_ERR_TYPE0_SUBMAP_NDEF = 0x41,
261 BLE_ERR_UNK_ADV_INDENT = 0x42,
262 BLE_ERR_LIMIT_REACHED = 0x43,
263 BLE_ERR_OPERATION_CANCELLED = 0x44,
264 BLE_ERR_PACKET_TOO_LONG = 0x45,
269#define BLE_HW_ERR_DO_NOT_USE (0)
270#define BLE_HW_ERR_HCI_SYNC_LOSS (1)
273#define BLE_OWN_ADDR_PUBLIC (0x00)
274#define BLE_OWN_ADDR_RANDOM (0x01)
275#define BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT (0x02)
276#define BLE_OWN_ADDR_RPA_RANDOM_DEFAULT (0x03)
279#define BLE_ADDR_PUBLIC (0x00)
280#define BLE_ADDR_RANDOM (0x01)
281#define BLE_ADDR_PUBLIC_ID (0x02)
282#define BLE_ADDR_RANDOM_ID (0x03)
284#define BLE_ADDR_ANY (&(ble_addr_t) { 0, {0, 0, 0, 0, 0, 0} })
286#define BLE_ADDR_IS_RPA(addr) (((addr)->type == BLE_ADDR_RANDOM) && \
287 ((addr)->val[5] & 0xc0) == 0x40)
288#define BLE_ADDR_IS_NRPA(addr) (((addr)->type == BLE_ADDR_RANDOM) && \
289 ((addr)->val[5] & 0xc0) == 0x00)
290#define BLE_ADDR_IS_STATIC(addr) (((addr)->type == BLE_ADDR_RANDOM) && \
291 ((addr)->val[5] & 0xc0) == 0xc0)
299static inline int ble_addr_cmp(
const ble_addr_t *a,
const ble_addr_t *b)
303 type_diff = a->type - b->type;
304 if (type_diff != 0) {
308 return memcmp(a->val, b->val,
sizeof(a->val));