xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/controller/include/controller/ble_ll_conn.h (revision 042d53a763ad75cb1465103098bb88c245d95138)
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 "os/os.h"
24 #include "nimble/ble.h"
25 #include "nimble/hci_common.h"
26 #include "nimble/nimble_npl.h"
27 #include "controller/ble_ll_sched.h"
28 #include "controller/ble_ll_ctrl.h"
29 #include "controller/ble_phy.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /* Roles */
36 #define BLE_LL_CONN_ROLE_NONE           (0)
37 #define BLE_LL_CONN_ROLE_MASTER         (1)
38 #define BLE_LL_CONN_ROLE_SLAVE          (2)
39 
40 /* Connection states */
41 #define BLE_LL_CONN_STATE_IDLE          (0)
42 #define BLE_LL_CONN_STATE_CREATED       (1)
43 #define BLE_LL_CONN_STATE_ESTABLISHED   (2)
44 
45 /* Channel map size */
46 #define BLE_LL_CONN_CHMAP_LEN           (5)
47 
48 /* Definitions for source clock accuracy */
49 #define BLE_MASTER_SCA_251_500_PPM      (0)
50 #define BLE_MASTER_SCA_151_250_PPM      (1)
51 #define BLE_MASTER_SCA_101_150_PPM      (2)
52 #define BLE_MASTER_SCA_76_100_PPM       (3)
53 #define BLE_MASTER_SCA_51_75_PPM        (4)
54 #define BLE_MASTER_SCA_31_50_PPM        (5)
55 #define BLE_MASTER_SCA_21_30_PPM        (6)
56 #define BLE_MASTER_SCA_0_20_PPM         (7)
57 
58 /* Definition for RSSI when the RSSI is unknown */
59 #define BLE_LL_CONN_UNKNOWN_RSSI        (127)
60 
61 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
62 /*
63  * Encryption states for a connection
64  *
65  * NOTE: the states are ordered so that we can check to see if the state
66  * is greater than ENCRYPTED. If so, it means that the start or pause
67  * encryption procedure is running and we should not send data pdu's.
68  */
69 enum conn_enc_state {
70     CONN_ENC_S_UNENCRYPTED = 1,
71     CONN_ENC_S_ENCRYPTED,
72     CONN_ENC_S_ENC_RSP_WAIT,
73     CONN_ENC_S_PAUSE_ENC_RSP_WAIT,
74     CONN_ENC_S_PAUSED,
75     CONN_ENC_S_START_ENC_REQ_WAIT,
76     CONN_ENC_S_START_ENC_RSP_WAIT,
77     CONN_ENC_S_LTK_REQ_WAIT,
78     CONN_ENC_S_LTK_NEG_REPLY
79 };
80 
81 /*
82  * Note that the LTK is the key, the SDK is the plain text, and the
83  * session key is the cipher text portion of the encryption block.
84  */
85 struct ble_ll_conn_enc_data
86 {
87     uint8_t enc_state;
88     uint8_t tx_encrypted;
89     uint16_t enc_div;
90     uint16_t tx_pkt_cntr;
91     uint16_t rx_pkt_cntr;
92     uint64_t host_rand_num;
93     uint8_t iv[8];
94     struct ble_encryption_block enc_block;
95 };
96 #endif
97 
98 /* Connection state machine flags. */
99 union ble_ll_conn_sm_flags {
100     struct {
101         uint32_t pkt_rxd:1;
102         uint32_t terminate_ind_txd:1;
103         uint32_t terminate_ind_rxd:1;
104         uint32_t allow_slave_latency:1;
105         uint32_t slave_set_last_anchor:1;
106         uint32_t awaiting_host_reply:1;
107         uint32_t terminate_started:1;
108         uint32_t conn_update_sched:1;
109         uint32_t host_expects_upd_event:1;
110         uint32_t version_ind_sent:1;
111         uint32_t rxd_version_ind:1;
112         uint32_t chanmap_update_scheduled:1;
113         uint32_t conn_empty_pdu_txd:1;
114         uint32_t last_txd_md:1;
115         uint32_t conn_req_txd:1;
116         uint32_t send_ltk_req:1;
117         uint32_t encrypted:1;
118         uint32_t encrypt_chg_sent:1;
119         uint32_t le_ping_supp:1;
120         uint32_t csa2_supp:1;
121         uint32_t host_phy_update: 1;
122         uint32_t phy_update_sched: 1;
123         uint32_t ctrlr_phy_update: 1;
124         uint32_t phy_update_event: 1;
125         uint32_t peer_phy_update: 1; /* XXX:combine with ctrlr udpate bit? */
126         uint32_t aux_conn_req: 1;
127         uint32_t rxd_features:1;
128         uint32_t pending_hci_rd_features:1;
129     } cfbit;
130     uint32_t conn_flags;
131 } __attribute__((packed));
132 
133 /**
134  * Structure used for PHY data inside a connection.
135  *
136  * NOTE: the new phy's are the phys we will change to when a phy update
137  * procedure is ongoing and the event counter hits the instant.
138  *
139  * tx_phy_mode: chip specific phy mode for tx
140  * rx_phy_mode: chip specific phy mode for rx
141  * cur_tx_phy: value denoting current tx_phy (not a bitmask!)
142  * cur_rx_phy: value denoting current rx phy (not a bitmask!)
143  * new_tx_phy: value denoting new tx_phy (not a bitmask!)
144  * new_rx_phy: value denoting new rx phy (not a bitmask!)
145  * req_pref_tx_phy: tx phy sent in a phy request (may be different than host)
146  * req_pref_rx_phy: rx phy sent in a phy request (may be different than host)
147  * host_pref_tx_phys: bitmask of preferred transmit PHYs sent by host
148  * host_pref_rx_phys: bitmask of preferred receive PHYs sent by host
149  * phy_options: preferred phy options for coded phy
150  */
151 struct ble_ll_conn_phy_data
152 {
153     uint32_t tx_phy_mode: 2;
154     uint32_t rx_phy_mode: 2;
155     uint32_t cur_tx_phy: 2;
156     uint32_t cur_rx_phy: 2;
157     uint32_t new_tx_phy: 2;
158     uint32_t new_rx_phy: 2;
159     uint32_t host_pref_tx_phys_mask: 3;
160     uint32_t host_pref_rx_phys_mask: 3;
161     uint32_t req_pref_tx_phys_mask: 3;
162     uint32_t req_pref_rx_phys_mask: 3;
163     uint32_t phy_options: 2;
164 }  __attribute__((packed));
165 
166 #define CONN_CUR_TX_PHY_MASK(csm)   (1 << ((csm)->phy_data.cur_tx_phy - 1))
167 #define CONN_CUR_RX_PHY_MASK(csm)   (1 << ((csm)->phy_data.cur_rx_phy - 1))
168 
169 #define BLE_PHY_TRANSITION_INVALID    (0xFF)
170 
171 /* Connection state machine */
172 struct ble_ll_conn_sm
173 {
174     /* Connection state machine flags */
175     union ble_ll_conn_sm_flags csmflags;
176 
177     /* Current connection handle, state and role */
178     uint16_t conn_handle;
179     uint8_t conn_state;
180     uint8_t conn_role;          /* Can possibly be 1 bit */
181 
182     /* RSSI */
183     int8_t conn_rssi;
184 
185     /* For privacy */
186     int8_t rpa_index;
187 
188     /* Connection data length management */
189     uint8_t max_tx_octets;
190     uint8_t max_rx_octets;
191     uint8_t rem_max_tx_octets;
192     uint8_t rem_max_rx_octets;
193     uint8_t eff_max_tx_octets;
194     uint8_t eff_max_rx_octets;
195     uint16_t max_tx_time;
196     uint16_t max_rx_time;
197     uint16_t rem_max_tx_time;
198     uint16_t rem_max_rx_time;
199     uint16_t eff_max_tx_time;
200     uint16_t eff_max_rx_time;
201     uint8_t max_tx_octets_phy_mode[BLE_PHY_NUM_MODE];
202 
203 #if (BLE_LL_BT5_PHY_SUPPORTED == 1)
204     struct ble_ll_conn_phy_data phy_data;
205     uint16_t phy_instant;
206     uint8_t phy_tx_transition;
207 #endif
208 
209     /* Used to calculate data channel index for connection */
210     uint8_t chanmap[BLE_LL_CONN_CHMAP_LEN];
211     uint8_t req_chanmap[BLE_LL_CONN_CHMAP_LEN];
212     uint16_t chanmap_instant;
213     uint16_t channel_id; /* TODO could be union with hop and last chan used */
214     uint8_t hop_inc;
215     uint8_t data_chan_index;
216     uint8_t last_unmapped_chan;
217     uint8_t num_used_chans;
218 
219 #if MYNEWT_VAL(BLE_LL_STRICT_CONN_SCHEDULING)
220     uint8_t period_occ_mask;    /* mask: period 0 = 0x01, period 3 = 0x08 */
221 #endif
222 
223     /* Ack/Flow Control */
224     uint8_t tx_seqnum;          /* note: can be 1 bit */
225     uint8_t next_exp_seqnum;    /* note: can be 1 bit */
226     uint8_t cons_rxd_bad_crc;   /* note: can be 1 bit */
227     uint8_t last_rxd_sn;        /* note: cant be 1 bit given current code */
228     uint8_t last_rxd_hdr_byte;  /* note: possibly can make 1 bit since we
229                                    only use the MD bit now */
230 
231     /* connection event mgmt */
232     uint8_t reject_reason;
233     uint8_t host_reply_opcode;
234     uint8_t master_sca;
235     uint8_t tx_win_size;
236     uint8_t cur_ctrl_proc;
237     uint8_t disconnect_reason;
238     uint8_t rxd_disconnect_reason;
239     uint8_t vers_nr;
240     uint8_t conn_features;
241     uint8_t remote_features[7];
242     uint16_t pending_ctrl_procs;
243     uint16_t event_cntr;
244     uint16_t completed_pkts;
245     uint16_t comp_id;
246     uint16_t sub_vers_nr;
247     uint16_t auth_pyld_tmo;         /* could be ifdef'd. 10 msec units */
248 
249     uint32_t access_addr;
250     uint32_t crcinit;               /* only low 24 bits used */
251     /* XXX: do we need ce_end_time? Cant this be sched end time? */
252     uint32_t ce_end_time;   /* cputime at which connection event should end */
253     uint32_t terminate_timeout;
254     uint32_t last_scheduled;
255 
256     /* Connection timing */
257     uint16_t conn_itvl;
258     uint16_t slave_latency;
259     uint16_t supervision_tmo;
260     uint16_t min_ce_len;
261     uint16_t max_ce_len;
262     uint16_t tx_win_off;
263     uint32_t anchor_point;
264     uint8_t anchor_point_usecs;     /* XXX: can this be uint8_t ?*/
265     uint8_t conn_itvl_usecs;
266     uint32_t conn_itvl_ticks;
267     uint32_t last_anchor_point;     /* Slave only */
268     uint32_t slave_cur_tx_win_usecs;
269     uint32_t slave_cur_window_widening;
270     uint32_t last_rxd_pdu_cputime;  /* Used exclusively for supervision timer */
271 
272     /*
273      * Used to mark that direct advertising from the peer was using
274      * identity address as InitA
275      */
276     uint8_t inita_identity_used;
277 
278     /* address information */
279     uint8_t own_addr_type;
280     uint8_t peer_addr_type;
281     uint8_t peer_addr[BLE_DEV_ADDR_LEN];
282 
283     /*
284      * XXX: TODO. Could save memory. Have single event at LL and put these
285      * on a singly linked list. Only would need list pointer here.
286      */
287     /* Connection end event */
288     struct ble_npl_event conn_ev_end;
289 
290     /* Packet transmit queue */
291     struct os_mbuf *cur_tx_pdu;
292     STAILQ_HEAD(conn_txq_head, os_mbuf_pkthdr) conn_txq;
293 
294     /* List entry for active/free connection pools */
295     union {
296         SLIST_ENTRY(ble_ll_conn_sm) act_sle;
297         STAILQ_ENTRY(ble_ll_conn_sm) free_stqe;
298     };
299 
300     /* LL control procedure response timer */
301     struct ble_npl_callout ctrl_proc_rsp_timer;
302 
303     /* For scheduling connections */
304     struct ble_ll_sched_item conn_sch;
305 
306 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
307     struct ble_npl_callout auth_pyld_timer;
308 #endif
309 
310     /*
311      * XXX: a note on all these structures for control procedures. First off,
312      * all of these need to be ifdef'd to save memory. Another thing to
313      * consider is this: since most control procedures can only run when no
314      * others are running, can I use just one structure (a union)? Should I
315      * allocate these from a pool? Not sure what to do. For now, I just use
316      * a large chunk of memory per connection.
317      */
318 #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
319     struct ble_ll_conn_enc_data enc_data;
320 #endif
321     /*
322      * For connection update procedure. XXX: can make this a pointer and
323      * malloc it if we want to save space.
324      */
325     struct hci_conn_update conn_param_req;
326 
327     /* For connection update procedure */
328     struct ble_ll_conn_upd_req conn_update_req;
329 
330     /* XXX: for now, just store them all */
331     struct ble_ll_conn_params conn_cp;
332 
333     struct ble_ll_scan_sm *scansm;
334 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
335     struct hci_ext_create_conn initial_params;
336 #endif
337 
338 };
339 
340 /* Flags */
341 #define CONN_F_UPDATE_SCHED(csm)    ((csm)->csmflags.cfbit.conn_update_sched)
342 #define CONN_F_EMPTY_PDU_TXD(csm)   ((csm)->csmflags.cfbit.conn_empty_pdu_txd)
343 #define CONN_F_LAST_TXD_MD(csm)     ((csm)->csmflags.cfbit.last_txd_md)
344 #define CONN_F_CONN_REQ_TXD(csm)    ((csm)->csmflags.cfbit.conn_req_txd)
345 #define CONN_F_ENCRYPTED(csm)       ((csm)->csmflags.cfbit.encrypted)
346 #define CONN_F_ENC_CHANGE_SENT(csm) ((csm)->csmflags.cfbit.encrypt_chg_sent)
347 #define CONN_F_LE_PING_SUPP(csm)    ((csm)->csmflags.cfbit.le_ping_supp)
348 #define CONN_F_TERMINATE_STARTED(csm) ((csm)->csmflags.cfbit.terminate_started)
349 #define CONN_F_CSA2_SUPP(csm)       ((csm)->csmflags.cfbit.csa2_supp)
350 #define CONN_F_HOST_PHY_UPDATE(csm) ((csm)->csmflags.cfbit.host_phy_update)
351 #define CONN_F_PHY_UPDATE_SCHED(csm) ((csm)->csmflags.cfbit.phy_update_sched)
352 #define CONN_F_CTRLR_PHY_UPDATE(csm) ((csm)->csmflags.cfbit.ctrlr_phy_update)
353 #define CONN_F_PHY_UPDATE_EVENT(csm) ((csm)->csmflags.cfbit.phy_update_event)
354 #define CONN_F_PEER_PHY_UPDATE(csm)  ((csm)->csmflags.cfbit.peer_phy_update)
355 #define CONN_F_AUX_CONN_REQ(csm)  ((csm)->csmflags.cfbit.aux_conn_req)
356 
357 /* Role */
358 #define CONN_IS_MASTER(csm)         (csm->conn_role == BLE_LL_CONN_ROLE_MASTER)
359 #define CONN_IS_SLAVE(csm)          (csm->conn_role == BLE_LL_CONN_ROLE_SLAVE)
360 
361 /*
362  * Given a handle, returns an active connection state machine (or NULL if the
363  * handle does not exist
364  *
365  */
366 struct ble_ll_conn_sm *ble_ll_conn_find_active_conn(uint16_t handle);
367 
368 /* required for unit testing */
369 uint8_t ble_ll_conn_calc_dci(struct ble_ll_conn_sm *conn, uint16_t latency);
370 
371 #ifdef __cplusplus
372 }
373 #endif
374 
375 #endif /* H_BLE_LL_CONN_ */
376