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"
29#include "./ble_ll_ctrl.h"
30#include "hal/hal_system.h"
33#include "hal/hal_timer.h"
40#if defined(MYNEWT) && MYNEWT_VAL(BLE_LL_HCI_VS_EVENT_ON_ASSERT)
42#define BLE_LL_ASSERT(cond) (void(0))
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) \
49 ble_ll_assert(BLE_LL_FILE, __LINE__); \
54#define BLE_LL_ASSERT(cond) assert(cond)
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)
61#define BLE_LL_BT5_PHY_SUPPORTED (0)
65#define BLE_LL_SUB_VERS_NR (0x0000)
68#define BLE_LL_JITTER_USECS (16)
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
90#error Invalid SCA value
96#define BLE_LL_CHAN_MAP_LEN (5)
106 uint64_t ll_supp_features;
112 uint8_t chan_map_num_used;
113 uint8_t chan_map[BLE_LL_CHAN_MAP_LEN];
115#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL) || MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
117 uint8_t ll_num_acl_pkts;
120 uint16_t ll_acl_pkt_size;
124 uint8_t ll_pref_tx_phys;
125 uint8_t ll_pref_rx_phys;
128 struct ble_npl_eventq ll_evq;
131 struct ble_npl_event ll_rx_pkt_ev;
132 struct ble_ll_pkt_q ll_rx_pkt_q;
135#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL) || MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
136 struct ble_npl_event ll_tx_pkt_ev;
138 struct ble_ll_pkt_q ll_tx_pkt_q;
140#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL) || MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
142 struct ble_npl_event ll_dbuf_overflow_ev;
145 struct ble_npl_event ll_comp_pkt_ev;
149 struct ble_npl_callout ll_hw_err_timer;
151extern struct ble_ll_obj g_ble_ll_data;
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)
220extern STATS_SECT_DECL(ble_ll_stats) ble_ll_stats;
223#define BLE_LL_STATE_STANDBY (0)
224#if MYNEWT_VAL(BLE_LL_ROLE_BROADCASTER)
225#define BLE_LL_STATE_ADV (1)
227#if MYNEWT_VAL(BLE_LL_ROLE_OBSERVER)
228#define BLE_LL_STATE_SCANNING (2)
230#if MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL) || MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
231#define BLE_LL_STATE_CONNECTION (4)
233#if MYNEWT_VAL(BLE_LL_DTM)
234#define BLE_LL_STATE_DTM (5)
236#if MYNEWT_VAL(BLE_LL_ROLE_OBSERVER) && MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV)
237#define BLE_LL_STATE_SYNC (6)
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)
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)
290#define BLE_LL_CONN_INITIAL_FEATURES (0x00000022)
291#define BLE_LL_CONN_CLEAR_FEATURE(connsm, feature) (connsm->conn_features &= ~(feature))
294#define BLE_LL_HOST_CONTROLLED_FEATURES (BLE_LL_FEAT_CONN_SUBRATING_HOST)
297#define BLE_LL_IFS (150)
298#define BLE_LL_MAFS (300)
305#define BLE_DEV_ADDR_LEN (6)
309 uint8_t u8[BLE_DEV_ADDR_LEN];
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)
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)
334#define BLE_ACCESS_ADDR_ADV (0x8E89BED6)
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)
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)
367#define BLE_LL_EXT_ADV_HDR_LEN (1)
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)
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)
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)
391#define BLE_ADV_PDU_HDR_CHSEL (0x20)
397#define BLE_ADV_PDU_HDR_TXADD_RAND (0x40)
398#define BLE_ADV_PDU_HDR_RXADD_RAND (0x80)
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)
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)
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))
460#define BLE_CONNECT_REQ_LEN (34)
461#define BLE_CONNECT_REQ_PDU_LEN (BLE_CONNECT_REQ_LEN + BLE_LL_PDU_HDR_LEN)
463#define BLE_SCAN_REQ_LEN (12)
464#define BLE_SCAN_RSP_MAX_LEN (37)
465#define BLE_SCAN_RSP_MAX_EXT_LEN (251)
467#define BLE_LL_ADDR_SUBTYPE_IDENTITY (0)
468#define BLE_LL_ADDR_SUBTYPE_RPA (1)
469#define BLE_LL_ADDR_SUBTYPE_NRPA (2)
472#define BLE_LL_ACAD_CHANNEL_MAP_UPDATE_IND 0x28
474struct ble_ll_acad_channel_map_update_ind {
477} __attribute__((packed));
481int ble_ll_reset(
void);
484int ble_ll_is_valid_random_addr(
const uint8_t *addr);
490int ble_ll_is_valid_own_addr_type(uint8_t own_addr_type,
491 const uint8_t *random_addr);
495uint32_t ble_ll_pdu_tx_time_get(uint16_t payload_len,
int phy_mode);
499uint16_t ble_ll_pdu_max_tx_octets_get(uint32_t usecs,
int phy_mode);
502int ble_ll_is_rpa(
const uint8_t *addr, uint8_t addr_type);
504int ble_ll_addr_subtype(
const uint8_t *addr, uint8_t addr_type);
507int ble_ll_addr_is_id(uint8_t *addr, uint8_t addr_type);
510int ble_ll_is_our_devaddr(uint8_t *addr,
int addr_type);
513uint8_t *ble_ll_get_our_devaddr(uint8_t addr_type);
520void ble_ll_acl_data_in(
struct os_mbuf *txpkt);
534struct os_mbuf *ble_ll_rxpdu_alloc(uint16_t len);
537void ble_ll_data_buffer_overflow(
void);
540void ble_ll_hw_error(
void);
546int ble_ll_rx_start(uint8_t *rxbuf, uint8_t chan,
struct ble_mbuf_hdr *hdr);
549int ble_ll_rx_end(uint8_t *rxbuf,
struct ble_mbuf_hdr *rxhdr);
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);
556void ble_ll_mbuf_init(
struct os_mbuf *m, uint8_t pdulen, uint8_t hdr);
559void ble_ll_state_set(uint8_t ll_state);
562uint8_t ble_ll_state_get(
void);
565void ble_ll_event_send(
struct ble_npl_event *ev);
568void ble_ll_rx_pdu_in(
struct os_mbuf *rxpdu);
577int ble_ll_set_public_addr(
const uint8_t *addr);
580int ble_ll_set_random_addr(
const uint8_t *cmdbuf, uint8_t len,
bool hci_adv_ext);
583void ble_ll_wfr_timer_exp(
void *arg);
586uint64_t ble_ll_read_supp_features(
void);
589int ble_ll_set_host_feat(
const uint8_t *cmdbuf, uint8_t len);
592uint64_t ble_ll_read_supp_states(
void);
595int ble_ll_chk_txrx_octets(uint16_t octets);
596int ble_ll_chk_txrx_time(uint16_t time);
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);
607ble_ll_get_addr_type(uint8_t txrxflag)
610 return BLE_HCI_ADV_OWN_ADDR_RANDOM;
612 return BLE_HCI_ADV_OWN_ADDR_PUBLIC;
615#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
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;
626#if MYNEWT_VAL(BLE_LL_DTM)
627void ble_ll_dtm_init(
void);