NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
ble_ll_conn.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_CONN_
21#define H_BLE_LL_CONN_
22
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"
27#include "ble_ll.h"
28#include "ble_ll_sched.h"
29#include "ble_ll_ctrl.h"
30#include "ble_phy.h"
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/* Roles */
37#define BLE_LL_CONN_ROLE_NONE (0)
38
39#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
40#define BLE_LL_CONN_ROLE_CENTRAL (1)
41#endif
42#if MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
43#define BLE_LL_CONN_ROLE_PERIPHERAL (2)
44#endif
45
46/* Connection states */
47#define BLE_LL_CONN_STATE_IDLE (0)
48#define BLE_LL_CONN_STATE_CREATED (1)
49#define BLE_LL_CONN_STATE_ESTABLISHED (2)
50
51/* Definition for RSSI when the RSSI is unknown */
52#define BLE_LL_CONN_UNKNOWN_RSSI (127)
53
54#define BLE_LL_CONN_HANDLE_ISO_OFFSET (0x0100)
55
56#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
57/*
58 * Encryption states for a connection
59 *
60 * NOTE: the states are ordered so that we can check to see if the state
61 * is greater than ENCRYPTED. If so, it means that the start or pause
62 * encryption procedure is running and we should not send data pdu's.
63 */
64enum conn_enc_state {
65 CONN_ENC_S_UNENCRYPTED = 1,
66 CONN_ENC_S_ENCRYPTED,
67 CONN_ENC_S_ENC_RSP_TO_BE_SENT,
68 CONN_ENC_S_ENC_RSP_WAIT,
69 CONN_ENC_S_PAUSE_ENC_RSP_WAIT,
70 CONN_ENC_S_PAUSED,
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
75};
76
77/*
78 * Note that the LTK is the key, the SDK is the plain text, and the
79 * session key is the cipher text portion of the encryption block.
80 *
81 * NOTE: we have intentionally violated the specification by making the
82 * transmit and receive packet counters 32-bits as opposed to 39 (as per the
83 * specification). We do this to save code space, ram and calculation time. The
84 * only drawback is that any encrypted connection that sends more than 2^32
85 * packets will suffer a MIC failure and thus be disconnected.
86 */
87struct ble_ll_conn_enc_data
88{
89 uint8_t enc_state;
90 uint8_t tx_encrypted;
91 uint16_t enc_div;
92 uint32_t tx_pkt_cntr;
93 uint32_t rx_pkt_cntr;
94 uint64_t host_rand_num;
95 uint8_t iv[8];
96 struct ble_encryption_block enc_block;
97};
98#endif
99
100/* Connection state machine flags. */
101union ble_ll_conn_sm_flags {
102 struct {
103 uint32_t pkt_rxd:1;
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; /* XXX:combine with ctrlr udpate bit? */
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;
136 } cfbit;
137 uint32_t conn_flags;
138} __attribute__((packed));
139
159{
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));
172
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))
175
176struct hci_conn_update
177{
178 uint16_t handle;
179 uint16_t conn_itvl_min;
180 uint16_t conn_itvl_max;
181 uint16_t conn_latency;
182 uint16_t supervision_timeout;
183 uint16_t min_ce_len;
184 uint16_t max_ce_len;
185};
186
187struct ble_ll_conn_subrate_params {
188 uint16_t subrate_factor;
189 uint16_t subrate_base_event;
190 uint16_t periph_latency;
191 uint16_t cont_num;
192 uint16_t supervision_tmo;
193};
194
195struct ble_ll_conn_subrate_req_params {
196 uint16_t subrate_min;
197 uint16_t subrate_max;
198 uint16_t max_latency;
199 uint16_t cont_num;
200 uint16_t supervision_tmo;
201};
202
203/* Connection state machine */
204struct ble_ll_conn_sm
205{
206 /* Connection state machine flags */
207 union ble_ll_conn_sm_flags csmflags;
208
209 /* Current connection handle, state and role */
210 uint16_t conn_handle;
211 uint8_t conn_state;
212 uint8_t conn_role; /* Can possibly be 1 bit */
213
214 /* RSSI */
215 int8_t conn_rssi;
216
217 /* Connection data length management */
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;
232#endif
233
234#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
235 struct ble_ll_conn_phy_data phy_data;
236 uint16_t phy_instant;
237 uint8_t phy_tx_transition;
238#endif
239
240 /* Used to calculate data channel index for connection */
241 uint8_t chanmap[BLE_LL_CHAN_MAP_LEN];
242 uint8_t req_chanmap[BLE_LL_CHAN_MAP_LEN];
243 uint16_t chanmap_instant;
244 uint16_t channel_id; /* TODO could be union with hop and last chan used */
245 uint8_t hop_inc;
246 uint8_t data_chan_index;
247 uint8_t last_unmapped_chan;
248 uint8_t num_used_chans;
249
250 /* Ack/Flow Control */
251 uint8_t tx_seqnum; /* note: can be 1 bit */
252 uint8_t next_exp_seqnum; /* note: can be 1 bit */
253 uint8_t cons_rxd_bad_crc; /* note: can be 1 bit */
254 uint8_t last_rxd_sn; /* note: cant be 1 bit given current code */
255 uint8_t last_rxd_hdr_byte; /* note: possibly can make 1 bit since we
256 only use the MD bit now */
257
258#if MYNEWT_VAL(BLE_LL_CFG_FEAT_CTRL_TO_HOST_FLOW_CONTROL)
259 uint16_t cth_flow_pending;
260#endif
261
262 /* connection event mgmt */
263 uint8_t reject_reason;
264 uint8_t host_reply_opcode;
265 uint8_t central_sca;
266 uint8_t tx_win_size;
267 uint8_t cur_ctrl_proc;
268 uint8_t disconnect_reason;
269 uint8_t rxd_disconnect_reason;
270 uint8_t vers_nr;
271 uint8_t conn_features;
272 uint8_t remote_features[7];
273 uint16_t pending_ctrl_procs;
274 uint16_t event_cntr;
275 uint16_t completed_pkts;
276 uint16_t comp_id;
277 uint16_t sub_vers_nr;
278 uint16_t auth_pyld_tmo; /* could be ifdef'd. 10 msec units */
279
280 uint32_t access_addr;
281 uint32_t crcinit; /* only low 24 bits used */
282 /* XXX: do we need ce_end_time? Cant this be sched end time? */
283 uint32_t ce_end_time; /* cputime at which connection event should end */
284 uint32_t terminate_timeout;
285 uint32_t last_scheduled;
286
287 /* Connection timing */
288 uint16_t conn_itvl;
289 uint16_t supervision_tmo;
290 uint16_t min_ce_len;
291 uint16_t max_ce_len;
292 uint16_t tx_win_off;
293 uint32_t anchor_point;
294 uint8_t anchor_point_usecs; /* XXX: can this be uint8_t ?*/
295 uint8_t conn_itvl_usecs;
296 uint32_t conn_itvl_ticks;
297 uint32_t last_anchor_point; /* Slave only */
298 uint32_t periph_cur_tx_win_usecs;
299 uint32_t periph_cur_window_widening;
300 uint32_t last_rxd_pdu_cputime; /* Used exclusively for supervision timer */
301
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;
309
310 uint16_t subrate_base_event;
311 uint16_t subrate_factor;
312 uint16_t cont_num;
313 uint16_t last_pdu_event;
314
315 union {
316 struct ble_ll_conn_subrate_params subrate_trans;
317 struct ble_ll_conn_subrate_req_params subrate_req;
318 };
319#endif
320
321 /*
322 * Used to mark that identity address was used as InitA
323 */
324 uint8_t inita_identity_used;
325
326 /* address information */
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;
332#endif
333
334 /*
335 * XXX: TODO. Could save memory. Have single event at LL and put these
336 * on a singly linked list. Only would need list pointer here.
337 */
338 /* Connection end event */
339 struct ble_npl_event conn_ev_end;
340
341 /* Packet transmit queue */
342 struct os_mbuf *cur_tx_pdu;
343 STAILQ_HEAD(conn_txq_head, os_mbuf_pkthdr) conn_txq;
344
345 /* List entry for active/free connection pools */
346 union {
347 SLIST_ENTRY(ble_ll_conn_sm) act_sle;
348 STAILQ_ENTRY(ble_ll_conn_sm) free_stqe;
349 };
350
351 /* LL control procedure response timer */
352 struct ble_npl_callout ctrl_proc_rsp_timer;
353
354 /* For scheduling connections */
355 struct ble_ll_sched_item conn_sch;
356
357#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
358 struct ble_npl_callout auth_pyld_timer;
359#endif
360
361 /*
362 * XXX: a note on all these structures for control procedures. First off,
363 * all of these need to be ifdef'd to save memory. Another thing to
364 * consider is this: since most control procedures can only run when no
365 * others are running, can I use just one structure (a union)? Should I
366 * allocate these from a pool? Not sure what to do. For now, I just use
367 * a large chunk of memory per connection.
368 */
369#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
370 struct ble_ll_conn_enc_data enc_data;
371#endif
372 /*
373 * For connection update procedure. XXX: can make this a pointer and
374 * malloc it if we want to save space.
375 */
376 struct hci_conn_update conn_param_req;
377
378 /* For connection update procedure */
379 struct ble_ll_conn_upd_req conn_update_req;
380
381 /* XXX: for now, just store them all */
382 struct ble_ll_conn_params conn_cp;
383
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;
388#endif
389
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;
394#endif
395};
396
397/* Flags */
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)
413
414/* Role */
415#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
416#define CONN_IS_CENTRAL(csm) (csm->conn_role == BLE_LL_CONN_ROLE_CENTRAL)
417#else
418#define CONN_IS_CENTRAL(csm) (false)
419#endif
420
421#if MYNEWT_VAL(BLE_LL_ROLE_PERIPHERAL)
422#define CONN_IS_PERIPHERAL(csm) (csm->conn_role == BLE_LL_CONN_ROLE_PERIPHERAL)
423#else
424#define CONN_IS_PERIPHERAL(csm) (false)
425#endif
426
427static inline int
428ble_ll_conn_rem_feature_check(struct ble_ll_conn_sm *connsm, uint64_t feature)
429{
430 uint8_t byte_idx;
431
432 /* 8 lsb are conn features */
433 feature >>= 8;
434
435 byte_idx = __builtin_ctzll(feature) / 8;
436 return connsm->remote_features[byte_idx] & (feature >> (byte_idx * 8));
437}
438
439
440static inline void
441ble_ll_conn_rem_feature_add(struct ble_ll_conn_sm *connsm, uint64_t feature)
442{
443 uint8_t byte_idx;
444
445 /* 8 lsb are conn features */
446 feature >>= 8;
447
448 byte_idx = __builtin_ctzll(feature) / 8;
449 connsm->remote_features[byte_idx] |= (feature >> (byte_idx * 8));
450}
451
452
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,
455 uint8_t addr_type);
456
457/* required for unit testing */
458uint8_t ble_ll_conn_calc_dci(struct ble_ll_conn_sm *conn, uint16_t latency);
459
460/* used to get anchor point for connection event specified */
461void ble_ll_conn_get_anchor(struct ble_ll_conn_sm *connsm, uint16_t conn_event,
462 uint32_t *anchor, uint8_t *anchor_usecs);
463
464#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
465int ble_ll_conn_move_anchor(struct ble_ll_conn_sm *connsm, uint16_t offset);
466#endif
467
468struct ble_ll_scan_addr_data;
469struct ble_ll_scan_pdu_data;
470
471uint8_t ble_ll_conn_tx_connect_ind_pducb(uint8_t *dptr, void *pducb_arg,
472 uint8_t *hdr_byte);
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,
476 uint8_t channel);
477
478/* Send CONNECT_IND/AUX_CONNECT_REQ */
479int ble_ll_conn_send_connect_req(struct os_mbuf *rxpdu,
480 struct ble_ll_scan_addr_data *addrd,
481 uint8_t ext);
482/* Cancel connection after AUX_CONNECT_REQ was sent */
483void ble_ll_conn_send_connect_req_cancel(void);
484/* Signal connection created via CONNECT_IND */
485void ble_ll_conn_created_on_legacy(struct os_mbuf *rxpdu,
486 struct ble_ll_scan_addr_data *addrd,
487 uint8_t *targeta);
488#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
489/* Signal connection created via AUX_CONNECT_REQ */
490void ble_ll_conn_created_on_aux(struct os_mbuf *rxpdu,
491 struct ble_ll_scan_addr_data *addrd,
492 uint8_t *targeta);
493#endif
494
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);
502#endif
503
504#ifdef __cplusplus
505}
506#endif
507
508#endif /* H_BLE_LL_CONN_ */
Definition ble_ll_conn.h:159
Definition os_mbuf.h:70
Definition os_mbuf.h:86