NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
ble_ll.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_LL_
21#define H_BLE_LL_
22
23#include "nimble/porting/nimble/include/stats/stats.h"
24#include "nimble/nimble/include/nimble/nimble_opt.h"
25#include "nimble/nimble/include/nimble/nimble_npl.h"
26#include "ble_phy.h"
27
28#ifdef MYNEWT
29#include "./ble_ll_ctrl.h"
30#include "hal/hal_system.h"
31#endif
32#ifdef RIOT_VERSION
33#include "hal/hal_timer.h"
34#endif
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#if defined(MYNEWT) && MYNEWT_VAL(BLE_LL_HCI_VS_EVENT_ON_ASSERT)
41#ifdef NDEBUG
42#define BLE_LL_ASSERT(cond) (void(0))
43#else
44void ble_ll_assert(const char *file, unsigned line) __attribute((noreturn));
45#define BLE_LL_FILE (__builtin_strrchr(__FILE__, '/') ? \
46 __builtin_strrchr (__FILE__, '/') + 1 : __FILE__)
47#define BLE_LL_ASSERT(cond) \
48 if (!(cond)) { \
49 ble_ll_assert(BLE_LL_FILE, __LINE__); \
50 }
51#endif
52#else
53#ifndef BLE_LL_ASSERT
54#define BLE_LL_ASSERT(cond) assert(cond)
55#endif
56#endif
57
58#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY) || MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
59#define BLE_LL_BT5_PHY_SUPPORTED (1)
60#else
61#define BLE_LL_BT5_PHY_SUPPORTED (0)
62#endif
63
64/* Controller revision. */
65#define BLE_LL_SUB_VERS_NR (0x0000)
66
67/* Timing jitter as per spec is +/16 usecs */
68#define BLE_LL_JITTER_USECS (16)
69
70
71#if MYNEWT_VAL(BLE_LL_SCA) < 0
72#error Invalid SCA value
73#elif MYNEWT_VAL(BLE_LL_SCA) <= 20
74#define BLE_LL_SCA_ENUM 7
75#elif MYNEWT_VAL(BLE_LL_SCA) <= 30
76#define BLE_LL_SCA_ENUM 6
77#elif MYNEWT_VAL(BLE_LL_SCA) <= 50
78#define BLE_LL_SCA_ENUM 5
79#elif MYNEWT_VAL(BLE_LL_SCA) <= 75
80#define BLE_LL_SCA_ENUM 4
81#elif MYNEWT_VAL(BLE_LL_SCA) <= 100
82#define BLE_LL_SCA_ENUM 3
83#elif MYNEWT_VAL(BLE_LL_SCA) <= 150
84#define BLE_LL_SCA_ENUM 2
85#elif MYNEWT_VAL(BLE_LL_SCA) <= 250
86#define BLE_LL_SCA_ENUM 1
87#elif MYNEWT_VAL(BLE_LL_SCA) <= 500
88#define BLE_LL_SCA_ENUM 0
89#else
90#error Invalid SCA value
91#endif
92
93/* Packet queue header definition */
94STAILQ_HEAD(ble_ll_pkt_q, os_mbuf_pkthdr);
95
96#define BLE_LL_CHAN_MAP_LEN (5)
97
98/*
99 * Global Link Layer data object. There is only one Link Layer data object
100 * per controller although there may be many instances of the link layer state
101 * machine running.
102 */
103struct ble_ll_obj
104{
105 /* Supported features */
106 uint64_t ll_supp_features;
107
108 /* Current Link Layer state */
109 uint8_t ll_state;
110
111 /* Global channel map */
112 uint8_t chan_map_num_used;
113 uint8_t chan_map[BLE_LL_CHAN_MAP_LEN];
114
115#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL) || MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
116 /* Number of ACL data packets supported */
117 uint8_t ll_num_acl_pkts;
118
119 /* ACL data packet size */
120 uint16_t ll_acl_pkt_size;
121#endif
122
123 /* Preferred PHY's */
124 uint8_t ll_pref_tx_phys;
125 uint8_t ll_pref_rx_phys;
126
127 /* Task event queue */
128 struct ble_npl_eventq ll_evq;
129
130 /* Packet receive queue (and event). Holds received packets from PHY */
131 struct ble_npl_event ll_rx_pkt_ev;
132 struct ble_ll_pkt_q ll_rx_pkt_q;
133
134 /* Packet transmit queue */
135#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL) || MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
136 struct ble_npl_event ll_tx_pkt_ev;
137#endif
138 struct ble_ll_pkt_q ll_tx_pkt_q;
139
140#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL) || MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
141 /* Data buffer overflow event */
142 struct ble_npl_event ll_dbuf_overflow_ev;
143
144 /* Number of completed packets event */
145 struct ble_npl_event ll_comp_pkt_ev;
146#endif
147
148 /* HW error callout */
149 struct ble_npl_callout ll_hw_err_timer;
150};
151extern struct ble_ll_obj g_ble_ll_data;
152
153/* Link layer statistics */
154STATS_SECT_START(ble_ll_stats)
155 STATS_SECT_ENTRY(hci_cmds)
156 STATS_SECT_ENTRY(hci_cmd_errs)
157 STATS_SECT_ENTRY(hci_events_sent)
158 STATS_SECT_ENTRY(bad_ll_state)
159 STATS_SECT_ENTRY(bad_acl_hdr)
160 STATS_SECT_ENTRY(no_bufs)
161 STATS_SECT_ENTRY(rx_adv_pdu_crc_ok)
162 STATS_SECT_ENTRY(rx_adv_pdu_crc_err)
163 STATS_SECT_ENTRY(rx_adv_bytes_crc_ok)
164 STATS_SECT_ENTRY(rx_adv_bytes_crc_err)
165 STATS_SECT_ENTRY(rx_data_pdu_crc_ok)
166 STATS_SECT_ENTRY(rx_data_pdu_crc_err)
167 STATS_SECT_ENTRY(rx_data_bytes_crc_ok)
168 STATS_SECT_ENTRY(rx_data_bytes_crc_err)
169 STATS_SECT_ENTRY(rx_adv_malformed_pkts)
170 STATS_SECT_ENTRY(rx_adv_ind)
171 STATS_SECT_ENTRY(rx_adv_direct_ind)
172 STATS_SECT_ENTRY(rx_adv_nonconn_ind)
173 STATS_SECT_ENTRY(rx_adv_ext_ind)
174 STATS_SECT_ENTRY(rx_scan_reqs)
175 STATS_SECT_ENTRY(rx_scan_rsps)
176 STATS_SECT_ENTRY(rx_connect_reqs)
177 STATS_SECT_ENTRY(rx_scan_ind)
178 STATS_SECT_ENTRY(rx_aux_connect_rsp)
179 STATS_SECT_ENTRY(adv_txg)
180 STATS_SECT_ENTRY(adv_late_starts)
181 STATS_SECT_ENTRY(adv_resched_pdu_fail)
182 STATS_SECT_ENTRY(adv_drop_event)
183 STATS_SECT_ENTRY(sched_state_conn_errs)
184 STATS_SECT_ENTRY(sched_state_adv_errs)
185 STATS_SECT_ENTRY(scan_starts)
186 STATS_SECT_ENTRY(scan_stops)
187 STATS_SECT_ENTRY(scan_req_txf)
188 STATS_SECT_ENTRY(scan_req_txg)
189 STATS_SECT_ENTRY(scan_rsp_txg)
190 STATS_SECT_ENTRY(aux_missed_adv)
191 STATS_SECT_ENTRY(aux_scheduled)
192 STATS_SECT_ENTRY(aux_received)
193 STATS_SECT_ENTRY(aux_fired_for_read)
194 STATS_SECT_ENTRY(aux_allocated)
195 STATS_SECT_ENTRY(aux_freed)
196 STATS_SECT_ENTRY(aux_sched_cb)
197 STATS_SECT_ENTRY(aux_conn_req_tx)
198 STATS_SECT_ENTRY(aux_conn_rsp_tx)
199 STATS_SECT_ENTRY(aux_conn_rsp_err)
200 STATS_SECT_ENTRY(aux_scan_req_tx)
201 STATS_SECT_ENTRY(aux_scan_rsp_err)
202 STATS_SECT_ENTRY(aux_chain_cnt)
203 STATS_SECT_ENTRY(aux_chain_err)
204 STATS_SECT_ENTRY(aux_scan_drop)
205 STATS_SECT_ENTRY(adv_evt_dropped)
206 STATS_SECT_ENTRY(scan_timer_stopped)
207 STATS_SECT_ENTRY(scan_timer_restarted)
208 STATS_SECT_ENTRY(periodic_adv_drop_event)
209 STATS_SECT_ENTRY(periodic_chain_drop_event)
210 STATS_SECT_ENTRY(sync_event_failed)
211 STATS_SECT_ENTRY(sync_received)
212 STATS_SECT_ENTRY(sync_chain_failed)
213 STATS_SECT_ENTRY(sync_missed_err)
214 STATS_SECT_ENTRY(sync_crc_err)
215 STATS_SECT_ENTRY(sync_rx_buf_err)
216 STATS_SECT_ENTRY(sync_scheduled)
217 STATS_SECT_ENTRY(sched_state_sync_errs)
218 STATS_SECT_ENTRY(sched_invalid_pdu)
219STATS_SECT_END
220extern STATS_SECT_DECL(ble_ll_stats) ble_ll_stats;
221
222/* States */
223#define BLE_LL_STATE_STANDBY (0)
224#if MYNEWT_VAL(BLE_LL_ROLE_BROADCASTER)
225#define BLE_LL_STATE_ADV (1)
226#endif
227#if MYNEWT_VAL(BLE_LL_ROLE_OBSERVER)
228#define BLE_LL_STATE_SCANNING (2)
229#endif
230#if MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL) || MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
231#define BLE_LL_STATE_CONNECTION (4)
232#endif
233#if MYNEWT_VAL(BLE_LL_DTM)
234#define BLE_LL_STATE_DTM (5)
235#endif
236#if MYNEWT_VAL(BLE_LL_ROLE_OBSERVER) && MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV)
237#define BLE_LL_STATE_SYNC (6)
238#endif
239#if MYNEWT_VAL(BLE_LL_ROLE_OBSERVER) && MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
240#define BLE_LL_STATE_SCAN_AUX (7)
241#endif
242
243/* LL Features */
244#define BLE_LL_FEAT_LE_ENCRYPTION (0x0000000001)
245#define BLE_LL_FEAT_CONN_PARM_REQ (0x0000000002)
246#define BLE_LL_FEAT_EXTENDED_REJ (0x0000000004)
247#define BLE_LL_FEAT_PERIPH_INIT (0x0000000008)
248#define BLE_LL_FEAT_LE_PING (0x0000000010)
249#define BLE_LL_FEAT_DATA_LEN_EXT (0x0000000020)
250#define BLE_LL_FEAT_LL_PRIVACY (0x0000000040)
251#define BLE_LL_FEAT_EXT_SCAN_FILT (0x0000000080)
252#define BLE_LL_FEAT_LE_2M_PHY (0x0000000100)
253#define BLE_LL_FEAT_STABLE_MOD_ID_TX (0x0000000200)
254#define BLE_LL_FEAT_STABLE_MOD_ID_RX (0x0000000400)
255#define BLE_LL_FEAT_LE_CODED_PHY (0x0000000800)
256#define BLE_LL_FEAT_EXT_ADV (0x0000001000)
257#define BLE_LL_FEAT_PERIODIC_ADV (0x0000002000)
258#define BLE_LL_FEAT_CSA2 (0x0000004000)
259#define BLE_LL_FEAT_LE_POWER_CLASS_1 (0x0000008000)
260#define BLE_LL_FEAT_MIN_USED_CHAN (0x0000010000)
261#define BLE_LL_FEAT_CTE_REQ (0x0000020000)
262#define BLE_LL_FEAT_CTE_RSP (0x0000040000)
263#define BLE_LL_FEAT_CTE_TX (0x0000080000)
264#define BLE_LL_FEAT_CTE_RX (0x0000100000)
265#define BLE_LL_FEAT_CTE_AOD (0x0000200000)
266#define BLE_LL_FEAT_CTE_AOA (0x0000400000)
267#define BLE_LL_FEAT_CTE_RECV (0x0000800000)
268#define BLE_LL_FEAT_SYNC_TRANS_SEND (0x0001000000)
269#define BLE_LL_FEAT_SYNC_TRANS_RECV (0x0002000000)
270#define BLE_LL_FEAT_SCA_UPDATE (0x0004000000)
271#define BLE_LL_FEAT_REM_PKEY (0x0008000000)
272#define BLE_LL_FEAT_CIS_CENTRAL (0x0010000000)
273#define BLE_LL_FEAT_CIS_PERIPH (0x0020000000)
274#define BLE_LL_FEAT_ISO_BROADCASTER (0x0040000000)
275#define BLE_LL_FEAT_SYNC_RECV (0x0080000000)
276#define BLE_LL_FEAT_CIS_HOST (0x0100000000)
277#define BLE_LL_FEAT_POWER_CTRL_REQ (0x0200000000)
278#define BLE_LL_FEAT_POWER_CHANGE_IND (0x0400000000)
279#define BLE_LL_FEAT_PATH_LOSS_MON (0x0800000000)
280#define BLE_LL_FEAT_PERIODIC_ADV_ADI (0x1000000000)
281#define BLE_LL_FEAT_CONN_SUBRATING (0x2000000000)
282#define BLE_LL_FEAT_CONN_SUBRATING_HOST (0x4000000000)
283#define BLE_LL_FEAT_CHANNEL_CLASS (0x8000000000)
284
285/* This is initial mask, so if feature exchange will not happen,
286 * but host will want to use this procedure, we will try. If not
287 * succeed, feature bit will be cleared.
288 * Look at LL Features above to find out what is allowed
289 */
290#define BLE_LL_CONN_INITIAL_FEATURES (0x00000022)
291#define BLE_LL_CONN_CLEAR_FEATURE(connsm, feature) (connsm->conn_features &= ~(feature))
292
293/* All the features which can be controlled by the Host */
294#define BLE_LL_HOST_CONTROLLED_FEATURES (BLE_LL_FEAT_CONN_SUBRATING_HOST)
295
296/* LL timing */
297#define BLE_LL_IFS (150) /* usecs */
298#define BLE_LL_MAFS (300) /* usecs */
299
300/*
301 * BLE LL device address. Note that element 0 of the array is the LSB and
302 * is sent over the air first. Byte 5 is the MSB and is the last one sent over
303 * the air.
304 */
305#define BLE_DEV_ADDR_LEN (6) /* bytes */
306
307struct ble_dev_addr
308{
309 uint8_t u8[BLE_DEV_ADDR_LEN];
310};
311
312#define BLE_IS_DEV_ADDR_STATIC(addr) ((addr->u8[5] & 0xc0) == 0xc0)
313#define BLE_IS_DEV_ADDR_RESOLVABLE(addr) ((addr->u8[5] & 0xc0) == 0x40)
314#define BLE_IS_DEV_ADDR_UNRESOLVABLE(addr) ((addr->u8[5] & 0xc0) == 0x00)
315
316/*
317 * LL packet format
318 *
319 * -> Preamble (1/2 bytes)
320 * -> Access Address (4 bytes)
321 * -> PDU (2 to 257 octets)
322 * -> CRC (3 bytes)
323 */
324#define BLE_LL_PREAMBLE_LEN (1)
325#define BLE_LL_ACC_ADDR_LEN (4)
326#define BLE_LL_CRC_LEN (3)
327#define BLE_LL_PDU_HDR_LEN (2)
328#define BLE_LL_MAX_PAYLOAD_LEN (255)
329#define BLE_LL_MIN_PDU_LEN (BLE_LL_PDU_HDR_LEN)
330#define BLE_LL_MAX_PDU_LEN ((BLE_LL_PDU_HDR_LEN) + (BLE_LL_MAX_PAYLOAD_LEN))
331#define BLE_LL_CRCINIT_ADV (0x555555)
332
333/* Access address for advertising channels */
334#define BLE_ACCESS_ADDR_ADV (0x8E89BED6)
335
336/*
337 * Advertising PDU format:
338 * -> 2 byte header
339 * -> LSB contains pdu type, txadd and rxadd bits.
340 * -> MSB contains length (6 bits). Length is length of payload. Does
341 * not include the header length itself.
342 * -> Payload (max 37 bytes)
343 */
344#define BLE_ADV_PDU_HDR_TYPE_MASK (0x0F)
345#define BLE_ADV_PDU_HDR_CHSEL_MASK (0x20)
346#define BLE_ADV_PDU_HDR_TXADD_MASK (0x40)
347#define BLE_ADV_PDU_HDR_RXADD_MASK (0x80)
348
349/* Advertising channel PDU types */
350#define BLE_ADV_PDU_TYPE_ADV_IND (0)
351#define BLE_ADV_PDU_TYPE_ADV_DIRECT_IND (1)
352#define BLE_ADV_PDU_TYPE_ADV_NONCONN_IND (2)
353#define BLE_ADV_PDU_TYPE_SCAN_REQ (3)
354#define BLE_ADV_PDU_TYPE_SCAN_RSP (4)
355#define BLE_ADV_PDU_TYPE_CONNECT_IND (5)
356#define BLE_ADV_PDU_TYPE_ADV_SCAN_IND (6)
357#define BLE_ADV_PDU_TYPE_ADV_EXT_IND (7)
358#define BLE_ADV_PDU_TYPE_AUX_ADV_IND BLE_ADV_PDU_TYPE_ADV_EXT_IND
359#define BLE_ADV_PDU_TYPE_AUX_SCAN_RSP BLE_ADV_PDU_TYPE_ADV_EXT_IND
360#define BLE_ADV_PDU_TYPE_AUX_SYNC_IND BLE_ADV_PDU_TYPE_ADV_EXT_IND
361#define BLE_ADV_PDU_TYPE_AUX_CHAIN_IND BLE_ADV_PDU_TYPE_ADV_EXT_IND
362#define BLE_ADV_PDU_TYPE_AUX_CONNECT_REQ BLE_ADV_PDU_TYPE_CONNECT_IND
363#define BLE_ADV_PDU_TYPE_AUX_SCAN_REQ BLE_ADV_PDU_TYPE_SCAN_REQ
364#define BLE_ADV_PDU_TYPE_AUX_CONNECT_RSP (8)
365
366/* Extended Header Length (6b) + AdvMode (2b) */
367#define BLE_LL_EXT_ADV_HDR_LEN (1)
368
369#define BLE_LL_EXT_ADV_ADVA_BIT (0)
370#define BLE_LL_EXT_ADV_TARGETA_BIT (1)
371#define BLE_LL_EXT_ADV_CTE_INFO_BIT (2)
372#define BLE_LL_EXT_ADV_DATA_INFO_BIT (3)
373#define BLE_LL_EXT_ADV_AUX_PTR_BIT (4)
374#define BLE_LL_EXT_ADV_SYNC_INFO_BIT (5)
375#define BLE_LL_EXT_ADV_TX_POWER_BIT (6)
376
377#define BLE_LL_EXT_ADV_FLAGS_SIZE (1)
378#define BLE_LL_EXT_ADV_ADVA_SIZE (6)
379#define BLE_LL_EXT_ADV_TARGETA_SIZE (6)
380#define BLE_LL_EXT_ADV_CTE_INFO_SIZE (1)
381#define BLE_LL_EXT_ADV_DATA_INFO_SIZE (2)
382#define BLE_LL_EXT_ADV_AUX_PTR_SIZE (3)
383#define BLE_LL_EXT_ADV_SYNC_INFO_SIZE (18)
384#define BLE_LL_EXT_ADV_TX_POWER_SIZE (1)
385
386#define BLE_LL_EXT_ADV_MODE_NON_CONN (0x00)
387#define BLE_LL_EXT_ADV_MODE_CONN (0x01)
388#define BLE_LL_EXT_ADV_MODE_SCAN (0x02)
389
390/* If Channel Selection Algorithm #2 is supported */
391#define BLE_ADV_PDU_HDR_CHSEL (0x20)
392
393/*
394 * TxAdd and RxAdd bit definitions. A 0 is a public address; a 1 is a
395 * random address.
396 */
397#define BLE_ADV_PDU_HDR_TXADD_RAND (0x40)
398#define BLE_ADV_PDU_HDR_RXADD_RAND (0x80)
399
400/*
401 * Data Channel format
402 *
403 * -> Header (2 bytes)
404 * -> LSB contains llid, nesn, sn and md
405 * -> MSB contains length (8 bits)
406 * -> Payload (0 to 251)
407 * -> MIC (0 or 4 bytes)
408 */
409#define BLE_LL_DATA_HDR_LLID_MASK (0x03)
410#define BLE_LL_DATA_HDR_NESN_MASK (0x04)
411#define BLE_LL_DATA_HDR_SN_MASK (0x08)
412#define BLE_LL_DATA_HDR_MD_MASK (0x10)
413#define BLE_LL_DATA_HDR_RSRVD_MASK (0xE0)
414#define BLE_LL_DATA_PDU_MAX_PYLD (251)
415#define BLE_LL_DATA_MIC_LEN (4)
416
417/* LLID definitions */
418#define BLE_LL_LLID_RSRVD (0)
419#define BLE_LL_LLID_DATA_FRAG (1)
420#define BLE_LL_LLID_DATA_START (2)
421#define BLE_LL_LLID_CTRL (3)
422
423#define BLE_LL_LLID_IS_CTRL(hdr) \
424 (((hdr) & BLE_LL_DATA_HDR_LLID_MASK) == BLE_LL_LLID_CTRL)
425#define BLE_LL_LLID_IS_DATA(hdr) \
426 ((((hdr) & BLE_LL_DATA_HDR_LLID_MASK) == BLE_LL_LLID_DATA_START) || \
427 (((hdr) & BLE_LL_DATA_HDR_LLID_MASK) == BLE_LL_LLID_DATA_FRAG))
428
429/*
430 * CONNECT_REQ
431 * -> InitA (6 bytes)
432 * -> AdvA (6 bytes)
433 * -> LLData (22 bytes)
434 * -> Access address (4 bytes)
435 * -> CRC init (3 bytes)
436 * -> WinSize (1 byte)
437 * -> WinOffset (2 bytes)
438 * -> Interval (2 bytes)
439 * -> Latency (2 bytes)
440 * -> Timeout (2 bytes)
441 * -> Channel Map (5 bytes)
442 * -> Hop Increment (5 bits)
443 * -> SCA (3 bits)
444 *
445 * InitA is the initiators public (TxAdd=0) or random (TxAdd=1) address.
446 * AdvaA is the advertisers public (RxAdd=0) or random (RxAdd=1) address.
447 * LLData contains connection request data.
448 * aa: Link Layer's access address
449 * crc_init: The CRC initialization value used for CRC calculation.
450 * winsize: The transmit window size = winsize * 1.25 msecs
451 * winoffset: The transmit window offset = winoffset * 1.25 msecs
452 * interval: The connection interval = interval * 1.25 msecs.
453 * latency: connection slave latency = latency
454 * timeout: Connection supervision timeout = timeout * 10 msecs.
455 * chanmap: contains channel mapping indicating used and unused data
456 * channels. Only bits that are 1 are usable. LSB is channel 0.
457 * hop_inc: Hop increment used for frequency hopping. Random value in
458 * range of 5 to 16.
459 */
460#define BLE_CONNECT_REQ_LEN (34)
461#define BLE_CONNECT_REQ_PDU_LEN (BLE_CONNECT_REQ_LEN + BLE_LL_PDU_HDR_LEN)
462
463#define BLE_SCAN_REQ_LEN (12)
464#define BLE_SCAN_RSP_MAX_LEN (37)
465#define BLE_SCAN_RSP_MAX_EXT_LEN (251)
466
467#define BLE_LL_ADDR_SUBTYPE_IDENTITY (0)
468#define BLE_LL_ADDR_SUBTYPE_RPA (1)
469#define BLE_LL_ADDR_SUBTYPE_NRPA (2)
470
471/* ACAD data types */
472#define BLE_LL_ACAD_CHANNEL_MAP_UPDATE_IND 0x28
473
474struct ble_ll_acad_channel_map_update_ind {
475 uint8_t map[5];
476 uint16_t instant;
477} __attribute__((packed));
478
479/*--- External API ---*/
480/* Reset the Link Layer */
481int ble_ll_reset(void);
482
483/* 'Boolean' function returning true if address is a valid random address */
484int ble_ll_is_valid_random_addr(const uint8_t *addr);
485
486/*
487 * Check if given own_addr_type is valid for current controller configuration
488 * given the random address provided (when applicable)
489 */
490int ble_ll_is_valid_own_addr_type(uint8_t own_addr_type,
491 const uint8_t *random_addr);
492
493/* Calculate the amount of time in microseconds a PDU with payload length of
494 * 'payload_len' will take to transmit on a PHY 'phy_mode'. */
495uint32_t ble_ll_pdu_tx_time_get(uint16_t payload_len, int phy_mode);
496
497/* Calculate maximum octets of PDU payload which can be transmitted during
498 * 'usecs' on a PHY 'phy_mode'. */
499uint16_t ble_ll_pdu_max_tx_octets_get(uint32_t usecs, int phy_mode);
500
501/* Is this address a resolvable private address? */
502int ble_ll_is_rpa(const uint8_t *addr, uint8_t addr_type);
503
504int ble_ll_addr_subtype(const uint8_t *addr, uint8_t addr_type);
505
506/* Is this address an identity address? */
507int ble_ll_addr_is_id(uint8_t *addr, uint8_t addr_type);
508
509/* Is 'addr' our device address? 'addr_type' is public (0) or random (!=0) */
510int ble_ll_is_our_devaddr(uint8_t *addr, int addr_type);
511
512/* Get identity address 'addr_type' is public (0) or random (!=0) */
513uint8_t *ble_ll_get_our_devaddr(uint8_t addr_type);
514
520void ble_ll_acl_data_in(struct os_mbuf *txpkt);
521
534struct os_mbuf *ble_ll_rxpdu_alloc(uint16_t len);
535
536/* Tell the Link Layer there has been a data buffer overflow */
537void ble_ll_data_buffer_overflow(void);
538
539/* Tell the link layer there has been a hardware error */
540void ble_ll_hw_error(void);
541
542/*--- PHY interfaces ---*/
543struct ble_mbuf_hdr;
544
545/* Called by the PHY when a packet has started */
546int ble_ll_rx_start(uint8_t *rxbuf, uint8_t chan, struct ble_mbuf_hdr *hdr);
547
548/* Called by the PHY when a packet reception ends */
549int ble_ll_rx_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr);
550
551/* Helper callback to tx mbuf using ble_phy_tx() */
552uint8_t ble_ll_tx_mbuf_pducb(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte);
553uint8_t ble_ll_tx_flat_mbuf_pducb(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte);
554
555/*--- Controller API ---*/
556void ble_ll_mbuf_init(struct os_mbuf *m, uint8_t pdulen, uint8_t hdr);
557
558/* Set the link layer state */
559void ble_ll_state_set(uint8_t ll_state);
560
561/* Get the link layer state */
562uint8_t ble_ll_state_get(void);
563
564/* Send an event to LL task */
565void ble_ll_event_send(struct ble_npl_event *ev);
566
567/* Hand received pdu's to LL task */
568void ble_ll_rx_pdu_in(struct os_mbuf *rxpdu);
569
570/*
571 * Set public address
572 *
573 * This can be used to set controller public address from vendor specific storage,
574 * usually should be done in hal_bsp_init().
575 * Shall be *only* called before LL is initialized, i.e. before sysinit stage.
576 */
577int ble_ll_set_public_addr(const uint8_t *addr);
578
579/* Set random address */
580int ble_ll_set_random_addr(const uint8_t *cmdbuf, uint8_t len, bool hci_adv_ext);
581
582/* Wait for response timer expiration callback */
583void ble_ll_wfr_timer_exp(void *arg);
584
585/* Read set of features supported by the Link Layer */
586uint64_t ble_ll_read_supp_features(void);
587
588/* Set host supported features */
589int ble_ll_set_host_feat(const uint8_t *cmdbuf, uint8_t len);
590
591/* Read set of states supported by the Link Layer */
592uint64_t ble_ll_read_supp_states(void);
593
594/* Check if octets and time are valid. Returns 0 if not valid */
595int ble_ll_chk_txrx_octets(uint16_t octets);
596int ble_ll_chk_txrx_time(uint16_t time);
597
598/* Random numbers */
599int ble_ll_rand_init(void);
600void ble_ll_rand_sample(uint8_t rnum);
601int ble_ll_rand_data_get(uint8_t *buf, uint8_t len);
602void ble_ll_rand_prand_get(uint8_t *prand);
603int ble_ll_rand_start(void);
604uint32_t ble_ll_rand(void);
605
606static inline int
607ble_ll_get_addr_type(uint8_t txrxflag)
608{
609 if (txrxflag) {
610 return BLE_HCI_ADV_OWN_ADDR_RANDOM;
611 }
612 return BLE_HCI_ADV_OWN_ADDR_PUBLIC;
613}
614
615#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
616/* LTK 0x4C68384139F574D836BCF34E9DFB01BF */
617extern const uint8_t g_bletest_LTK[];
618extern uint16_t g_bletest_EDIV;
619extern uint64_t g_bletest_RAND;
620extern uint64_t g_bletest_SKDm;
621extern uint64_t g_bletest_SKDs;
622extern uint32_t g_bletest_IVm;
623extern uint32_t g_bletest_IVs;
624#endif
625
626#if MYNEWT_VAL(BLE_LL_DTM)
627void ble_ll_dtm_init(void);
628#endif
629
630#ifdef __cplusplus
631}
632#endif
633
634#endif /* H_LL_ */
Definition os_mbuf.h:70
Definition os_mbuf.h:86