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 #if defined(CONFIG_BT_CTLR_PHY)
95 			uint8_t phy_rx_event:3;
96 #endif /* CONFIG_BT_CTLR_PHY */
97 
98 			uint32_t window_widening_periodic_us;
99 			uint32_t window_widening_max_us;
100 			uint32_t window_widening_prepare_us;
101 			uint32_t window_widening_event_us;
102 			uint32_t window_size_prepare_us;
103 			uint32_t window_size_event_us;
104 		} periph;
105 #endif /* CONFIG_BT_PERIPHERAL */
106 	};
107 
108 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
109 	struct {
110 		struct data_pdu_length local;
111 		struct data_pdu_length remote;
112 		struct data_pdu_length eff;
113 #if defined(CONFIG_BT_CTLR_PHY)
114 		uint16_t default_tx_time;
115 #endif
116 		uint16_t default_tx_octets;
117 		uint8_t update;
118 	} dle;
119 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
120 
121 #if defined(CONFIG_BT_CTLR_PHY)
122 	uint8_t phy_tx:3;
123 	uint8_t phy_flags:1;
124 	uint8_t phy_tx_time:3;
125 	uint8_t phy_rx:3;
126 #endif /* CONFIG_BT_CTLR_PHY */
127 
128 	MEMQ_DECLARE(tx);
129 	memq_link_t link_tx;
130 	memq_link_t *link_tx_free;
131 	uint8_t  packet_tx_head_len;
132 	uint8_t  packet_tx_head_offset;
133 
134 	uint8_t sn:1;
135 	uint8_t nesn:1;
136 	uint8_t empty:1;
137 
138 #if defined(CONFIG_BT_CTLR_LE_ENC)
139 	uint8_t enc_rx:1;
140 	uint8_t enc_tx:1;
141 
142 	struct ccm ccm_rx;
143 	struct ccm ccm_tx;
144 #endif /* CONFIG_BT_CTLR_LE_ENC */
145 
146 #if defined(CONFIG_BT_CTLR_SLOT_RESERVATION_UPDATE)
147 #if defined(CONFIG_BT_CTLR_DATA_LENGTH) || defined(CONFIG_BT_CTLR_PHY)
148 	uint8_t evt_len_upd:1;
149 	uint8_t evt_len_upd_delayed:1;
150 #endif /* CONFIG_BT_CTLR_DATA_LENGTH || CONFIG_BT_CTLR_PHY */
151 #endif /* CONFIG_BT_CTLR_SLOT_RESERVATION_UPDATE */
152 
153 #if defined(CONFIG_BT_CTLR_CONN_RSSI)
154 	uint8_t  rssi_latest;
155 #if defined(CONFIG_BT_CTLR_CONN_RSSI_EVENT)
156 	uint8_t  rssi_reported;
157 	uint8_t  rssi_sample_count;
158 #endif /* CONFIG_BT_CTLR_CONN_RSSI_EVENT */
159 #endif /* CONFIG_BT_CTLR_CONN_RSSI */
160 
161 #if defined(CONFIG_BT_CTLR_CONN_META)
162 	struct lll_conn_meta conn_meta;
163 #endif /* CONFIG_BT_CTLR_CONN_META */
164 
165 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
166 	int8_t tx_pwr_lvl;
167 #endif
168 
169 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX)
170 	struct lll_df_conn_rx_cfg df_rx_cfg;
171 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RX */
172 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_TX)
173 	struct lll_df_conn_tx_cfg df_tx_cfg;
174 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_TX */
175 };
176 
177 int lll_conn_init(void);
178 int lll_conn_reset(void);
179 void lll_conn_flush(uint16_t handle, struct lll_conn *lll);
180 
181 void lll_conn_prepare_reset(void);
182 int lll_conn_central_is_abort_cb(void *next, void *curr,
183 				 lll_prepare_cb_t *resume_cb);
184 int lll_conn_peripheral_is_abort_cb(void *next, void *curr,
185 				    lll_prepare_cb_t *resume_cb);
186 void lll_conn_abort_cb(struct lll_prepare_param *prepare_param, void *param);
187 void lll_conn_isr_rx(void *param);
188 void lll_conn_isr_tx(void *param);
189 void lll_conn_rx_pkt_set(struct lll_conn *lll);
190 void lll_conn_tx_pkt_set(struct lll_conn *lll, struct pdu_data *pdu_data_tx);
191 void lll_conn_pdu_tx_prep(struct lll_conn *lll, struct pdu_data **pdu_data_tx);
192 uint8_t lll_conn_force_md_cnt_set(uint8_t force_md_cnt);
193 
194 extern struct lll_conn *ull_conn_lll_get(uint16_t handle);
195 extern void ull_conn_lll_tx_demux_sched(struct lll_conn *lll);
196 extern void ull_conn_lll_ack_enqueue(uint16_t handle, struct node_tx *tx);
197 extern uint16_t ull_conn_lll_max_tx_octets_get(struct lll_conn *lll);
198