NimBLE-Arduino 2.1.3
Loading...
Searching...
No Matches
ble.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef H_BLE_
21#define H_BLE_
22
23#include <inttypes.h>
24#include <string.h>
25#include "nimble/porting/nimble/include/syscfg/syscfg.h"
26#include "nimble/porting/nimble/include/os/os.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/* The number of advertising instances */
33#define BLE_ADV_INSTANCES (MYNEWT_VAL(BLE_MULTI_ADV_INSTANCES) + 1)
34
35/* BLE encryption block definitions */
36#define BLE_ENC_BLOCK_SIZE (16)
37
38/* 4 byte header + 251 byte payload. */
39#define BLE_ACL_MAX_PKT_SIZE 255
40
41struct ble_encryption_block
42{
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];
46};
47
48/*
49 * BLE MBUF structure:
50 *
51 * The BLE mbuf structure is as follows. Note that this structure applies to
52 * the packet header mbuf (not mbufs that are part of a "packet chain"):
53 * struct os_mbuf (16)
54 * struct os_mbuf_pkthdr (8)
55 * struct ble_mbuf_hdr (8)
56 * Data buffer (payload size, in bytes)
57 *
58 * The BLE mbuf header contains the following:
59 * flags: bitfield with the following values
60 * 0x01: Set if there was a match on the whitelist
61 * 0x02: Set if a connect request was transmitted upon receiving pdu
62 * 0x04: Set the first time we transmit the PDU (used to detect retry).
63 * channel: The logical BLE channel PHY channel # (0 - 39)
64 * crcok: flag denoting CRC check passed (1) or failed (0).
65 * rssi: RSSI, in dBm.
66 */
67struct ble_mbuf_hdr_rxinfo
68{
69 uint16_t flags;
70 uint8_t channel;
71 uint8_t handle;
72 int8_t rssi;
73 /* XXX: we could just use single phy_mode field */
74 int8_t phy;
75 uint8_t phy_mode;
76#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
77 int8_t rpa_index;
78#endif
79#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
80 void *user_data;
81#endif
82};
83
84/*
85 * Flag definitions for rxinfo
86 *
87 * Note: it's ok to have symbols with the same values as long as they cannot be
88 * set for the same PDU (e.g. one use by scanner, other one used by
89 * connection)
90 */
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)
111
112/* Transmit info. NOTE: no flags defined */
113struct ble_mbuf_hdr_txinfo
114{
115 uint8_t flags;
116 uint8_t hdr_byte;
117 uint16_t offset;
118 uint16_t pyld_len;
119};
120
121struct ble_mbuf_hdr
122{
123 union {
124 struct ble_mbuf_hdr_rxinfo rxinfo;
125 struct ble_mbuf_hdr_txinfo txinfo;
126 };
127 uint32_t beg_cputime;
128 uint32_t rem_usecs;
129};
130
131#define BLE_MBUF_HDR_IGNORED(hdr) \
132 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_IGNORED))
133
134#define BLE_MBUF_HDR_SCAN_REQ_TXD(hdr) \
135 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_SCAN_REQ_TXD))
136
137#define BLE_MBUF_HDR_EXT_ADV_SEC(hdr) \
138 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_EXT_ADV_SEC))
139
140#define BLE_MBUF_HDR_EXT_ADV(hdr) \
141 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_EXT_ADV))
142
143#define BLE_MBUF_HDR_DEVMATCH(hdr) \
144 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_DEVMATCH))
145
146#define BLE_MBUF_HDR_SCAN_RSP_RXD(hdr) \
147 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_SCAN_RSP_RXD))
148
149#define BLE_MBUF_HDR_AUX_INVALID(hdr) \
150 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_AUX_INVALID))
151
152#define BLE_MBUF_HDR_WAIT_AUX(hdr) \
153 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_AUX_PTR_WAIT))
154
155#define BLE_MBUF_HDR_CRC_OK(hdr) \
156 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_CRC_OK))
157
158#define BLE_MBUF_HDR_MIC_FAILURE(hdr) \
159 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_MIC_FAILURE))
160
161#define BLE_MBUF_HDR_RESOLVED(hdr) \
162 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_RESOLVED))
163
164#define BLE_MBUF_HDR_INITA_RESOLVED(hdr) \
165 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_INITA_RESOLVED))
166
167#define BLE_MBUF_HDR_TARGETA_RESOLVED(hdr) \
168 (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_TARGETA_RESOLVED))
169
170#define BLE_MBUF_HDR_RX_STATE(hdr) \
171 ((uint8_t)((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_RXSTATE_MASK))
172
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))
176
177/* BLE mbuf overhead per packet header mbuf */
178#define BLE_MBUF_PKTHDR_OVERHEAD \
179 (sizeof(struct os_mbuf_pkthdr) + sizeof(struct ble_mbuf_hdr))
180
181#define BLE_MBUF_MEMBLOCK_OVERHEAD \
182 (sizeof(struct os_mbuf) + BLE_MBUF_PKTHDR_OVERHEAD)
183
184/* Length of host user header. Only contains the peer's connection handle. */
185#define BLE_MBUF_HS_HDR_LEN (2)
186
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];
190
191/* BLE Error Codes (Core v4.2 Vol 2 part D) */
192enum ble_error_codes
193{
194 /* An "error" code of 0x0 means success */
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,
238 /* BLE_ERR_RESERVED = 0x2b */
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,
244 /* BLE_ERR_RESERVED = 0x31 */
245 BLE_ERR_PENDING_ROLE_SW = 0x32,
246 /* BLE_ERR_RESERVED = 0x33 */
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,
265 BLE_ERR_MAX = 0xff
266};
267
268/* HW error codes */
269#define BLE_HW_ERR_DO_NOT_USE (0) /* XXX: reserve this one for now */
270#define BLE_HW_ERR_HCI_SYNC_LOSS (1)
271
272/* Own Bluetooth Device address type */
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)
277
278/* Bluetooth Device address type */
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)
283
284#define BLE_ADDR_ANY (&(ble_addr_t) { 0, {0, 0, 0, 0, 0, 0} })
285
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)
292
293typedef struct {
294 uint8_t type;
295 uint8_t val[6];
296} ble_addr_t;
297
298
299static inline int ble_addr_cmp(const ble_addr_t *a, const ble_addr_t *b)
300{
301 int type_diff;
302
303 type_diff = a->type - b->type;
304 if (type_diff != 0) {
305 return type_diff;
306 }
307
308 return memcmp(a->val, b->val, sizeof(a->val));
309}
310
311#ifdef __cplusplus
312}
313#endif
314
315#endif /* H_BLE_ */