1 /*
2  * Copyright (c) 2018-2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #if defined(CONFIG_BT_CTLR_CONN_META)
7 #include "lll_conn_meta.h"
8 #endif /* CONFIG_BT_CTLR_CONN_META */
9 
10 #define LLL_CONN_RSSI_SAMPLE_COUNT 10
11 #define LLL_CONN_RSSI_THRESHOLD    4
12 
13 #define LLL_CONN_MIC_NONE 0
14 #define LLL_CONN_MIC_PASS 1
15 #define LLL_CONN_MIC_FAIL 2
16 
17 struct lll_tx {
18 	uint16_t handle;
19 	void *node;
20 };
21 
22 struct node_tx {
23 	union {
24 		void        *next;
25 		memq_link_t *link;
26 	};
27 
28 	uint8_t pdu[];
29 };
30 
31 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
32 struct data_pdu_length {
33 	uint16_t max_tx_octets;
34 	uint16_t max_rx_octets;
35 	uint16_t max_tx_time;
36 	uint16_t max_rx_time;
37 };
38 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
39 
40 struct lll_conn {
41 	struct lll_hdr hdr;
42 
43 	uint8_t access_addr[4];
44 	uint8_t crc_init[3];
45 
46 	uint16_t tifs_tx_us;
47 	uint16_t tifs_rx_us;
48 	uint16_t tifs_hcto_us;
49 	uint16_t tifs_cis_us;
50 
51 	uint16_t handle;
52 	uint16_t interval;
53 
54 	uint16_t latency;
55 	uint16_t latency_prepare;
56 	uint16_t lazy_prepare;
57 	uint16_t latency_event;
58 	uint16_t event_counter;
59 
60 	uint8_t data_chan_map[PDU_CHANNEL_MAP_SIZE];
61 	uint8_t data_chan_count:6;
62 	uint8_t data_chan_sel:1;
63 	uint8_t role:1;
64 
65 	union {
66 		struct {
67 			uint8_t data_chan_hop;
68 			uint8_t data_chan_use;
69 		};
70 
71 		uint16_t data_chan_id;
72 	};
73 
74 	union {
75 		struct {
76 			uint8_t initiated:1;
77 			uint8_t cancelled:1;
78 			uint8_t forced:1;
79 		};
80 
81 		struct {
82 			uint8_t initiated:1;
83 			uint8_t cancelled:1;
84 			uint8_t forced:1;
85 		} central;
86 
87 #if defined(CONFIG_BT_PERIPHERAL)
88 		struct {
89 			uint8_t  initiated:1;
90 			uint8_t  cancelled:1;
91 			uint8_t  forced:1;
92 			uint8_t  latency_enabled:1;
93 
94 			uint32_t window_widening_periodic_us;
95 			uint32_t window_widening_max_us;
96 			uint32_t window_widening_prepare_us;
97 			uint32_t window_widening_event_us;
98 			uint32_t window_size_prepare_us;
99 			uint32_t window_size_event_us;
100 		} periph;
101 #endif /* CONFIG_BT_PERIPHERAL */
102 	};
103 
104 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
105 	struct {
106 		struct data_pdu_length local;
107 		struct data_pdu_length remote;
108 		struct data_pdu_length eff;
109 #if defined(CONFIG_BT_CTLR_PHY)
110 		uint16_t default_tx_time;
111 #endif
112 		uint16_t default_tx_octets;
113 		uint8_t update;
114 	} dle;
115 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
116 
117 #if defined(CONFIG_BT_CTLR_PHY)
118 	uint8_t phy_tx:3;
119 	uint8_t phy_flags:1;
120 	uint8_t phy_tx_time:3;
121 	uint8_t phy_rx:3;
122 #endif /* CONFIG_BT_CTLR_PHY */
123 
124 	MEMQ_DECLARE(tx);
125 	memq_link_t link_tx;
126 	memq_link_t *link_tx_free;
127 	uint8_t  packet_tx_head_len;
128 	uint8_t  packet_tx_head_offset;
129 
130 	uint8_t sn:1;
131 	uint8_t nesn:1;
132 	uint8_t empty:1;
133 
134 #if defined(CONFIG_BT_CTLR_LE_ENC)
135 	uint8_t enc_rx:1;
136 	uint8_t enc_tx:1;
137 
138 	struct ccm ccm_rx;
139 	struct ccm ccm_tx;
140 #endif /* CONFIG_BT_CTLR_LE_ENC */
141 
142 #if defined(CONFIG_BT_CTLR_SLOT_RESERVATION_UPDATE)
143 #if defined(CONFIG_BT_CTLR_DATA_LENGTH) || defined(CONFIG_BT_CTLR_PHY)
144 	uint8_t evt_len_upd:1;
145 	uint8_t evt_len_upd_delayed:1;
146 #endif /* CONFIG_BT_CTLR_DATA_LENGTH || CONFIG_BT_CTLR_PHY */
147 #endif /* CONFIG_BT_CTLR_SLOT_RESERVATION_UPDATE */
148 
149 #if defined(CONFIG_BT_CTLR_CONN_RSSI)
150 	uint8_t  rssi_latest;
151 #if defined(CONFIG_BT_CTLR_CONN_RSSI_EVENT)
152 	uint8_t  rssi_reported;
153 	uint8_t  rssi_sample_count;
154 #endif /* CONFIG_BT_CTLR_CONN_RSSI_EVENT */
155 #endif /* CONFIG_BT_CTLR_CONN_RSSI */
156 
157 #if defined(CONFIG_BT_CTLR_CONN_META)
158 	struct lll_conn_meta conn_meta;
159 #endif /* CONFIG_BT_CTLR_CONN_META */
160 
161 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
162 	int8_t tx_pwr_lvl;
163 #endif
164 
165 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX)
166 	struct lll_df_conn_rx_cfg df_rx_cfg;
167 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RX */
168 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_TX)
169 	struct lll_df_conn_tx_cfg df_tx_cfg;
170 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_TX */
171 };
172 
173 int lll_conn_init(void);
174 int lll_conn_reset(void);
175 void lll_conn_flush(uint16_t handle, struct lll_conn *lll);
176 
177 void lll_conn_prepare_reset(void);
178 int lll_conn_central_is_abort_cb(void *next, void *curr,
179 				 lll_prepare_cb_t *resume_cb);
180 int lll_conn_peripheral_is_abort_cb(void *next, void *curr,
181 				    lll_prepare_cb_t *resume_cb);
182 void lll_conn_abort_cb(struct lll_prepare_param *prepare_param, void *param);
183 void lll_conn_isr_rx(void *param);
184 void lll_conn_isr_tx(void *param);
185 void lll_conn_rx_pkt_set(struct lll_conn *lll);
186 void lll_conn_tx_pkt_set(struct lll_conn *lll, struct pdu_data *pdu_data_tx);
187 void lll_conn_pdu_tx_prep(struct lll_conn *lll, struct pdu_data **pdu_data_tx);
188 uint8_t lll_conn_force_md_cnt_set(uint8_t force_md_cnt);
189 
190 extern struct lll_conn *ull_conn_lll_get(uint16_t handle);
191 extern void ull_conn_lll_tx_demux_sched(struct lll_conn *lll);
192 extern void ull_conn_lll_ack_enqueue(uint16_t handle, struct node_tx *tx);
193 extern uint16_t ull_conn_lll_max_tx_octets_get(struct lll_conn *lll);
194