23#include "nimble/porting/nimble/include/os/os.h"
24#include "nimble/nimble/include/nimble/ble.h"
25#include "nimble/nimble/include/nimble/hci_common.h"
26#include "nimble/nimble/include/nimble/nimble_npl.h"
28#include "ble_ll_sched.h"
29#include "ble_ll_ctrl.h"
37#define BLE_LL_CONN_ROLE_NONE (0)
39#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
40#define BLE_LL_CONN_ROLE_CENTRAL (1)
42#if MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
43#define BLE_LL_CONN_ROLE_PERIPHERAL (2)
47#define BLE_LL_CONN_STATE_IDLE (0)
48#define BLE_LL_CONN_STATE_CREATED (1)
49#define BLE_LL_CONN_STATE_ESTABLISHED (2)
52#define BLE_LL_CONN_UNKNOWN_RSSI (127)
54#define BLE_LL_CONN_HANDLE_ISO_OFFSET (0x0100)
56#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
65 CONN_ENC_S_UNENCRYPTED = 1,
67 CONN_ENC_S_ENC_RSP_TO_BE_SENT,
68 CONN_ENC_S_ENC_RSP_WAIT,
69 CONN_ENC_S_PAUSE_ENC_RSP_WAIT,
71 CONN_ENC_S_START_ENC_REQ_WAIT,
72 CONN_ENC_S_START_ENC_RSP_WAIT,
73 CONN_ENC_S_LTK_REQ_WAIT,
74 CONN_ENC_S_LTK_NEG_REPLY
87struct ble_ll_conn_enc_data
94 uint64_t host_rand_num;
96 struct ble_encryption_block enc_block;
101union ble_ll_conn_sm_flags {
104 uint32_t terminate_ind_txd:1;
105 uint32_t terminate_ind_rxd:1;
106 uint32_t terminate_ind_rxd_acked:1;
107 uint32_t allow_periph_latency:1;
108 uint32_t periph_set_last_anchor:1;
109 uint32_t awaiting_host_reply:1;
110 uint32_t terminate_started:1;
111 uint32_t conn_update_sched:1;
112 uint32_t host_expects_upd_event:1;
113 uint32_t version_ind_sent:1;
114 uint32_t rxd_version_ind:1;
115 uint32_t chanmap_update_scheduled:1;
116 uint32_t conn_empty_pdu_txd:1;
117 uint32_t last_txd_md:1;
118 uint32_t conn_req_txd:1;
119 uint32_t send_ltk_req:1;
120 uint32_t encrypted:1;
121 uint32_t encrypt_chg_sent:1;
122 uint32_t le_ping_supp:1;
123 uint32_t csa2_supp:1;
124 uint32_t host_phy_update: 1;
125 uint32_t phy_update_sched: 1;
126 uint32_t ctrlr_phy_update: 1;
127 uint32_t phy_update_event: 1;
128 uint32_t peer_phy_update: 1;
129 uint32_t aux_conn_req: 1;
130 uint32_t rxd_features:1;
131 uint32_t pending_hci_rd_features:1;
132 uint32_t pending_initiate_dle:1;
133 uint32_t subrate_trans:1;
134 uint32_t subrate_ind_txd:1;
135 uint32_t subrate_host_req:1;
138} __attribute__((packed));
160 uint32_t tx_phy_mode: 2;
161 uint32_t rx_phy_mode: 2;
162 uint32_t cur_tx_phy: 2;
163 uint32_t cur_rx_phy: 2;
164 uint32_t new_tx_phy: 2;
165 uint32_t new_rx_phy: 2;
166 uint32_t pref_mask_tx: 3;
167 uint32_t pref_mask_rx: 3;
168 uint32_t pref_mask_tx_req: 3;
169 uint32_t pref_mask_rx_req: 3;
170 uint32_t pref_opts: 2;
171} __attribute__((packed));
173#define CONN_CUR_TX_PHY_MASK(csm) (1 << ((csm)->phy_data.cur_tx_phy - 1))
174#define CONN_CUR_RX_PHY_MASK(csm) (1 << ((csm)->phy_data.cur_rx_phy - 1))
176struct hci_conn_update
179 uint16_t conn_itvl_min;
180 uint16_t conn_itvl_max;
181 uint16_t conn_latency;
182 uint16_t supervision_timeout;
187struct ble_ll_conn_subrate_params {
188 uint16_t subrate_factor;
189 uint16_t subrate_base_event;
190 uint16_t periph_latency;
192 uint16_t supervision_tmo;
195struct ble_ll_conn_subrate_req_params {
196 uint16_t subrate_min;
197 uint16_t subrate_max;
198 uint16_t max_latency;
200 uint16_t supervision_tmo;
207 union ble_ll_conn_sm_flags csmflags;
210 uint16_t conn_handle;
218 uint8_t max_tx_octets;
219 uint8_t max_rx_octets;
220 uint8_t rem_max_tx_octets;
221 uint8_t rem_max_rx_octets;
222 uint8_t eff_max_tx_octets;
223 uint8_t eff_max_rx_octets;
224 uint16_t max_tx_time;
225 uint16_t max_rx_time;
226 uint16_t rem_max_tx_time;
227 uint16_t rem_max_rx_time;
228 uint16_t eff_max_tx_time;
229 uint16_t eff_max_rx_time;
230#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
231 uint16_t host_req_max_tx_time;
234#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
236 uint16_t phy_instant;
237 uint8_t phy_tx_transition;
241 uint8_t chanmap[BLE_LL_CHAN_MAP_LEN];
242 uint8_t req_chanmap[BLE_LL_CHAN_MAP_LEN];
243 uint16_t chanmap_instant;
246 uint8_t data_chan_index;
247 uint8_t last_unmapped_chan;
248 uint8_t num_used_chans;
252 uint8_t next_exp_seqnum;
253 uint8_t cons_rxd_bad_crc;
255 uint8_t last_rxd_hdr_byte;
258#if MYNEWT_VAL(BLE_LL_CFG_FEAT_CTRL_TO_HOST_FLOW_CONTROL)
259 uint16_t cth_flow_pending;
263 uint8_t reject_reason;
264 uint8_t host_reply_opcode;
267 uint8_t cur_ctrl_proc;
268 uint8_t disconnect_reason;
269 uint8_t rxd_disconnect_reason;
271 uint8_t conn_features;
272 uint8_t remote_features[7];
273 uint16_t pending_ctrl_procs;
275 uint16_t completed_pkts;
277 uint16_t sub_vers_nr;
278 uint16_t auth_pyld_tmo;
280 uint32_t access_addr;
283 uint32_t ce_end_time;
284 uint32_t terminate_timeout;
285 uint32_t last_scheduled;
289 uint16_t supervision_tmo;
293 uint32_t anchor_point;
294 uint8_t anchor_point_usecs;
295 uint8_t conn_itvl_usecs;
296 uint32_t conn_itvl_ticks;
297 uint32_t last_anchor_point;
298 uint32_t periph_cur_tx_win_usecs;
299 uint32_t periph_cur_window_widening;
300 uint32_t last_rxd_pdu_cputime;
302 uint16_t periph_latency;
303#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_ENHANCED_CONN_UPDATE)
304 uint16_t acc_subrate_min;
305 uint16_t acc_subrate_max;
306 uint16_t acc_max_latency;
307 uint16_t acc_cont_num;
308 uint16_t acc_supervision_tmo;
310 uint16_t subrate_base_event;
311 uint16_t subrate_factor;
313 uint16_t last_pdu_event;
316 struct ble_ll_conn_subrate_params subrate_trans;
317 struct ble_ll_conn_subrate_req_params subrate_req;
324 uint8_t inita_identity_used;
327 uint8_t own_addr_type;
328 uint8_t peer_addr_type;
329 uint8_t peer_addr[BLE_DEV_ADDR_LEN];
330#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
331 uint8_t peer_addr_resolved;
339 struct ble_npl_event conn_ev_end;
347 SLIST_ENTRY(ble_ll_conn_sm) act_sle;
348 STAILQ_ENTRY(ble_ll_conn_sm) free_stqe;
352 struct ble_npl_callout ctrl_proc_rsp_timer;
355 struct ble_ll_sched_item conn_sch;
357#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
358 struct ble_npl_callout auth_pyld_timer;
369#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
370 struct ble_ll_conn_enc_data enc_data;
376 struct hci_conn_update conn_param_req;
379 struct ble_ll_conn_upd_req conn_update_req;
382 struct ble_ll_conn_params conn_cp;
384#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV_SYNC_TRANSFER)
385 uint8_t sync_transfer_mode;
386 uint16_t sync_transfer_skip;
387 uint32_t sync_transfer_sync_timeout;
390#if MYNEWT_VAL(BLE_LL_CONN_STRICT_SCHED)
391 uint16_t css_slot_idx;
392 uint16_t css_slot_idx_pending;
393 uint8_t css_period_idx;
398#define CONN_F_UPDATE_SCHED(csm) ((csm)->csmflags.cfbit.conn_update_sched)
399#define CONN_F_EMPTY_PDU_TXD(csm) ((csm)->csmflags.cfbit.conn_empty_pdu_txd)
400#define CONN_F_LAST_TXD_MD(csm) ((csm)->csmflags.cfbit.last_txd_md)
401#define CONN_F_CONN_REQ_TXD(csm) ((csm)->csmflags.cfbit.conn_req_txd)
402#define CONN_F_ENCRYPTED(csm) ((csm)->csmflags.cfbit.encrypted)
403#define CONN_F_ENC_CHANGE_SENT(csm) ((csm)->csmflags.cfbit.encrypt_chg_sent)
404#define CONN_F_LE_PING_SUPP(csm) ((csm)->csmflags.cfbit.le_ping_supp)
405#define CONN_F_TERMINATE_STARTED(csm) ((csm)->csmflags.cfbit.terminate_started)
406#define CONN_F_CSA2_SUPP(csm) ((csm)->csmflags.cfbit.csa2_supp)
407#define CONN_F_HOST_PHY_UPDATE(csm) ((csm)->csmflags.cfbit.host_phy_update)
408#define CONN_F_PHY_UPDATE_SCHED(csm) ((csm)->csmflags.cfbit.phy_update_sched)
409#define CONN_F_CTRLR_PHY_UPDATE(csm) ((csm)->csmflags.cfbit.ctrlr_phy_update)
410#define CONN_F_PHY_UPDATE_EVENT(csm) ((csm)->csmflags.cfbit.phy_update_event)
411#define CONN_F_PEER_PHY_UPDATE(csm) ((csm)->csmflags.cfbit.peer_phy_update)
412#define CONN_F_AUX_CONN_REQ(csm) ((csm)->csmflags.cfbit.aux_conn_req)
415#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
416#define CONN_IS_CENTRAL(csm) (csm->conn_role == BLE_LL_CONN_ROLE_CENTRAL)
418#define CONN_IS_CENTRAL(csm) (false)
421#if MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
422#define CONN_IS_PERIPHERAL(csm) (csm->conn_role == BLE_LL_CONN_ROLE_PERIPHERAL)
424#define CONN_IS_PERIPHERAL(csm) (false)
428ble_ll_conn_rem_feature_check(
struct ble_ll_conn_sm *connsm, uint64_t feature)
435 byte_idx = __builtin_ctzll(feature) / 8;
436 return connsm->remote_features[byte_idx] & (feature >> (byte_idx * 8));
441ble_ll_conn_rem_feature_add(
struct ble_ll_conn_sm *connsm, uint64_t feature)
448 byte_idx = __builtin_ctzll(feature) / 8;
449 connsm->remote_features[byte_idx] |= (feature >> (byte_idx * 8));
453struct ble_ll_conn_sm *ble_ll_conn_find_by_handle(uint16_t handle);
454struct ble_ll_conn_sm *ble_ll_conn_find_by_peer_addr(
const uint8_t* addr,
458uint8_t ble_ll_conn_calc_dci(
struct ble_ll_conn_sm *conn, uint16_t latency);
461void ble_ll_conn_get_anchor(
struct ble_ll_conn_sm *connsm, uint16_t conn_event,
462 uint32_t *anchor, uint8_t *anchor_usecs);
464#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
465int ble_ll_conn_move_anchor(
struct ble_ll_conn_sm *connsm, uint16_t offset);
468struct ble_ll_scan_addr_data;
469struct ble_ll_scan_pdu_data;
471uint8_t ble_ll_conn_tx_connect_ind_pducb(uint8_t *dptr,
void *pducb_arg,
473void ble_ll_conn_prepare_connect_ind(
struct ble_ll_conn_sm *connsm,
474 struct ble_ll_scan_pdu_data *pdu_data,
475 struct ble_ll_scan_addr_data *addrd,
479int ble_ll_conn_send_connect_req(
struct os_mbuf *rxpdu,
480 struct ble_ll_scan_addr_data *addrd,
483void ble_ll_conn_send_connect_req_cancel(
void);
485void ble_ll_conn_created_on_legacy(
struct os_mbuf *rxpdu,
486 struct ble_ll_scan_addr_data *addrd,
488#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
490void ble_ll_conn_created_on_aux(
struct os_mbuf *rxpdu,
491 struct ble_ll_scan_addr_data *addrd,
495#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_ENHANCED_CONN_UPDATE)
496int ble_ll_conn_subrate_req_hci(
struct ble_ll_conn_sm *connsm,
497 struct ble_ll_conn_subrate_req_params *srp);
498int ble_ll_conn_subrate_req_llcp(
struct ble_ll_conn_sm *connsm,
499 struct ble_ll_conn_subrate_req_params *srp);
500void ble_ll_conn_subrate_set(
struct ble_ll_conn_sm *connsm,
501 struct ble_ll_conn_subrate_params *sp);
Definition ble_ll_conn.h:159