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