xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/include/nimble/ble.h (revision 042d53a763ad75cb1465103098bb88c245d95138)
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 "syscfg/syscfg.h"
26 #include "os/os.h"
27 
28 #ifdef __cplusplus
29 extern "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 struct ble_encryption_block
39 {
40     uint8_t     key[BLE_ENC_BLOCK_SIZE];
41     uint8_t     plain_text[BLE_ENC_BLOCK_SIZE];
42     uint8_t     cipher_text[BLE_ENC_BLOCK_SIZE];
43 };
44 
45 /*
46  * BLE MBUF structure:
47  *
48  * The BLE mbuf structure is as follows. Note that this structure applies to
49  * the packet header mbuf (not mbufs that are part of a "packet chain"):
50  *      struct os_mbuf          (16)
51  *      struct os_mbuf_pkthdr   (8)
52  *      struct ble_mbuf_hdr     (8)
53  *      Data buffer             (payload size, in bytes)
54  *
55  * The BLE mbuf header contains the following:
56  *  flags: bitfield with the following values
57  *      0x01:   Set if there was a match on the whitelist
58  *      0x02:   Set if a connect request was transmitted upon receiving pdu
59  *      0x04:   Set the first time we transmit the PDU (used to detect retry).
60  *  channel: The logical BLE channel PHY channel # (0 - 39)
61  *  crcok: flag denoting CRC check passed (1) or failed (0).
62  *  rssi: RSSI, in dBm.
63  */
64 struct ble_mbuf_hdr_rxinfo
65 {
66     uint16_t flags;
67     uint8_t channel;
68     uint8_t handle;
69     int8_t  rssi;
70     /* XXX: we could just use single phy_mode field */
71     int8_t  phy;
72     uint8_t phy_mode;
73 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
74     void *user_data;
75 #endif
76 };
77 
78 /* Flag definitions for rxinfo  */
79 #define BLE_MBUF_HDR_F_EXT_ADV_SEC      (0x1000)
80 #define BLE_MBUF_HDR_F_EXT_ADV          (0x0800)
81 #define BLE_MBUF_HDR_F_RESOLVED         (0x0400)
82 #define BLE_MBUF_HDR_F_AUX_PTR_WAIT     (0x0200)
83 #define BLE_MBUF_HDR_F_AUX_INVALID      (0x0100)
84 #define BLE_MBUF_HDR_F_CRC_OK           (0x0080)
85 #define BLE_MBUF_HDR_F_DEVMATCH         (0x0040)
86 #define BLE_MBUF_HDR_F_MIC_FAILURE      (0x0020)
87 #define BLE_MBUF_HDR_F_SCAN_RSP_TXD     (0x0010)
88 #define BLE_MBUF_HDR_F_SCAN_RSP_CHK     (0x0008)
89 #define BLE_MBUF_HDR_F_RXSTATE_MASK     (0x0007)
90 
91 /* Transmit info. NOTE: no flags defined */
92 struct ble_mbuf_hdr_txinfo
93 {
94     uint8_t flags;
95     uint8_t offset;
96     uint8_t pyld_len;
97     uint8_t hdr_byte;
98 };
99 
100 struct ble_mbuf_hdr
101 {
102     union {
103         struct ble_mbuf_hdr_rxinfo rxinfo;
104         struct ble_mbuf_hdr_txinfo txinfo;
105     };
106     uint32_t beg_cputime;
107     uint32_t rem_usecs;
108 };
109 
110 #define BLE_MBUF_HDR_EXT_ADV_SEC(hdr) \
111     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_EXT_ADV_SEC))
112 
113 #define BLE_MBUF_HDR_EXT_ADV(hdr) \
114     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_EXT_ADV))
115 
116 #define BLE_MBUF_HDR_DEVMATCH(hdr) \
117     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_DEVMATCH))
118 
119 #define BLE_MBUF_HDR_SCAN_RSP_RCV(hdr) \
120     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_SCAN_RSP_CHK))
121 
122 #define BLE_MBUF_HDR_AUX_INVALID(hdr) \
123     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_AUX_INVALID))
124 
125 #define BLE_MBUF_HDR_WAIT_AUX(hdr)      \
126     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_AUX_PTR_WAIT))
127 
128 #define BLE_MBUF_HDR_CRC_OK(hdr)        \
129     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_CRC_OK))
130 
131 #define BLE_MBUF_HDR_MIC_FAILURE(hdr)   \
132     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_MIC_FAILURE))
133 
134 #define BLE_MBUF_HDR_RESOLVED(hdr)      \
135     (!!((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_RESOLVED))
136 
137 #define BLE_MBUF_HDR_RX_STATE(hdr)      \
138     ((uint8_t)((hdr)->rxinfo.flags & BLE_MBUF_HDR_F_RXSTATE_MASK))
139 
140 #define BLE_MBUF_HDR_PTR(om)            \
141     (struct ble_mbuf_hdr *)((uint8_t *)om + sizeof(struct os_mbuf) + \
142                             sizeof(struct os_mbuf_pkthdr))
143 
144 /* BLE mbuf overhead per packet header mbuf */
145 #define BLE_MBUF_PKTHDR_OVERHEAD        \
146     (sizeof(struct os_mbuf_pkthdr) + sizeof(struct ble_mbuf_hdr))
147 
148 #define BLE_MBUF_MEMBLOCK_OVERHEAD      \
149     (sizeof(struct os_mbuf) + BLE_MBUF_PKTHDR_OVERHEAD)
150 
151 /* Length of host user header.  Only contains the peer's connection handle. */
152 #define BLE_MBUF_HS_HDR_LEN     (2)
153 
154 #define BLE_DEV_ADDR_LEN        (6)
155 extern uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
156 extern uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
157 
158 /* BLE Error Codes (Core v4.2 Vol 2 part D) */
159 enum ble_error_codes
160 {
161     /* An "error" code of 0x0 means success */
162     BLE_ERR_SUCCESS             = 0x00,
163     BLE_ERR_UNKNOWN_HCI_CMD     = 0x01,
164     BLE_ERR_UNK_CONN_ID         = 0x02,
165     BLE_ERR_HW_FAIL             = 0x03,
166     BLE_ERR_PAGE_TMO            = 0x04,
167     BLE_ERR_AUTH_FAIL           = 0x05,
168     BLE_ERR_PINKEY_MISSING      = 0x06,
169     BLE_ERR_MEM_CAPACITY        = 0x07,
170     BLE_ERR_CONN_SPVN_TMO       = 0x08,
171     BLE_ERR_CONN_LIMIT          = 0x09,
172     BLE_ERR_SYNCH_CONN_LIMIT    = 0x0a,
173     BLE_ERR_ACL_CONN_EXISTS     = 0x0b,
174     BLE_ERR_CMD_DISALLOWED      = 0x0c,
175     BLE_ERR_CONN_REJ_RESOURCES  = 0x0d,
176     BLE_ERR_CONN_REJ_SECURITY   = 0x0e,
177     BLE_ERR_CONN_REJ_BD_ADDR    = 0x0f,
178     BLE_ERR_CONN_ACCEPT_TMO     = 0x10,
179     BLE_ERR_UNSUPPORTED         = 0x11,
180     BLE_ERR_INV_HCI_CMD_PARMS   = 0x12,
181     BLE_ERR_REM_USER_CONN_TERM  = 0x13,
182     BLE_ERR_RD_CONN_TERM_RESRCS = 0x14,
183     BLE_ERR_RD_CONN_TERM_PWROFF = 0x15,
184     BLE_ERR_CONN_TERM_LOCAL     = 0x16,
185     BLE_ERR_REPEATED_ATTEMPTS   = 0x17,
186     BLE_ERR_NO_PAIRING          = 0x18,
187     BLE_ERR_UNK_LMP             = 0x19,
188     BLE_ERR_UNSUPP_REM_FEATURE  = 0x1a,
189     BLE_ERR_SCO_OFFSET          = 0x1b,
190     BLE_ERR_SCO_ITVL            = 0x1c,
191     BLE_ERR_SCO_AIR_MODE        = 0x1d,
192     BLE_ERR_INV_LMP_LL_PARM     = 0x1e,
193     BLE_ERR_UNSPECIFIED         = 0x1f,
194     BLE_ERR_UNSUPP_LMP_LL_PARM  = 0x20,
195     BLE_ERR_NO_ROLE_CHANGE      = 0x21,
196     BLE_ERR_LMP_LL_RSP_TMO      = 0x22,
197     BLE_ERR_LMP_COLLISION       = 0x23,
198     BLE_ERR_LMP_PDU             = 0x24,
199     BLE_ERR_ENCRYPTION_MODE     = 0x25,
200     BLE_ERR_LINK_KEY_CHANGE     = 0x26,
201     BLE_ERR_UNSUPP_QOS          = 0x27,
202     BLE_ERR_INSTANT_PASSED      = 0x28,
203     BLE_ERR_UNIT_KEY_PAIRING    = 0x29,
204     BLE_ERR_DIFF_TRANS_COLL     = 0x2a,
205     /* BLE_ERR_RESERVED         = 0x2b */
206     BLE_ERR_QOS_PARM            = 0x2c,
207     BLE_ERR_QOS_REJECTED        = 0x2d,
208     BLE_ERR_CHAN_CLASS          = 0x2e,
209     BLE_ERR_INSUFFICIENT_SEC    = 0x2f,
210     BLE_ERR_PARM_OUT_OF_RANGE   = 0x30,
211     /* BLE_ERR_RESERVED         = 0x31 */
212     BLE_ERR_PENDING_ROLE_SW     = 0x32,
213     /* BLE_ERR_RESERVED         = 0x33 */
214     BLE_ERR_RESERVED_SLOT       = 0x34,
215     BLE_ERR_ROLE_SW_FAIL        = 0x35,
216     BLE_ERR_INQ_RSP_TOO_BIG     = 0x36,
217     BLE_ERR_SEC_SIMPLE_PAIR     = 0x37,
218     BLE_ERR_HOST_BUSY_PAIR      = 0x38,
219     BLE_ERR_CONN_REJ_CHANNEL    = 0x39,
220     BLE_ERR_CTLR_BUSY           = 0x3a,
221     BLE_ERR_CONN_PARMS          = 0x3b,
222     BLE_ERR_DIR_ADV_TMO         = 0x3c,
223     BLE_ERR_CONN_TERM_MIC       = 0x3d,
224     BLE_ERR_CONN_ESTABLISHMENT  = 0x3e,
225     BLE_ERR_MAC_CONN_FAIL       = 0x3f,
226     BLE_ERR_COARSE_CLK_ADJ      = 0x40,
227     BLE_ERR_TYPE0_SUBMAP_NDEF   = 0x41,
228     BLE_ERR_UNK_ADV_INDENT      = 0x42,
229     BLE_RR_LIMIT_REACHED        = 0x43,
230     BLE_ERR_MAX                 = 0xff
231 };
232 
233 int ble_err_from_os(int os_err);
234 
235 /* HW error codes */
236 #define BLE_HW_ERR_DO_NOT_USE           (0) /* XXX: reserve this one for now */
237 #define BLE_HW_ERR_HCI_SYNC_LOSS        (1)
238 
239 /* Own Bluetooth Device address type */
240 #define BLE_OWN_ADDR_PUBLIC                  (0x00)
241 #define BLE_OWN_ADDR_RANDOM                  (0x01)
242 #define BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT      (0x02)
243 #define BLE_OWN_ADDR_RPA_RANDOM_DEFAULT      (0x03)
244 
245 /* Bluetooth Device address type */
246 #define BLE_ADDR_PUBLIC      (0x00)
247 #define BLE_ADDR_RANDOM      (0x01)
248 #define BLE_ADDR_PUBLIC_ID   (0x02)
249 #define BLE_ADDR_RANDOM_ID   (0x03)
250 
251 #define BLE_ADDR_ANY (&(ble_addr_t) { 0, {0, 0, 0, 0, 0, 0} })
252 
253 #define BLE_ADDR_IS_RPA(addr)     (((addr)->type == BLE_ADDR_RANDOM) && \
254                                    ((addr)->val[5] & 0xc0) == 0x40)
255 #define BLE_ADDR_IS_NRPA(addr)    (((addr)->type == BLE_ADDR_RANDOM) && \
256                                    (((addr)->val[5] & 0xc0) == 0x00)
257 #define BLE_ADDR_IS_STATIC(addr)  (((addr)->type == BLE_ADDR_RANDOM) && \
258                                    (((addr)->val[5] & 0xc0) == 0xc0)
259 
260 typedef struct {
261     uint8_t type;
262     uint8_t val[6];
263 } ble_addr_t;
264 
265 
ble_addr_cmp(const ble_addr_t * a,const ble_addr_t * b)266 static inline int ble_addr_cmp(const ble_addr_t *a, const ble_addr_t *b)
267 {
268     int type_diff;
269 
270     type_diff = a->type - b->type;
271     if (type_diff != 0) {
272         return type_diff;
273     }
274 
275     return memcmp(a->val, b->val, sizeof(a->val));
276 }
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 
282 #endif /* H_BLE_ */
283