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 handle;
47 	uint16_t interval;
48 	uint16_t latency;
49 
50 	uint16_t latency_prepare;
51 	uint16_t latency_event;
52 	uint16_t event_counter;
53 
54 	uint8_t data_chan_map[PDU_CHANNEL_MAP_SIZE];
55 	uint8_t data_chan_count:6;
56 	uint8_t data_chan_sel:1;
57 	uint8_t role:1;
58 
59 	union {
60 		struct {
61 			uint8_t data_chan_hop;
62 			uint8_t data_chan_use;
63 		};
64 
65 		uint16_t data_chan_id;
66 	};
67 
68 	union {
69 		struct {
70 			uint8_t initiated:1;
71 			uint8_t cancelled:1;
72 		} central;
73 #if defined(CONFIG_BT_PERIPHERAL)
74 		struct {
75 			uint8_t  initiated:1;
76 			uint8_t  cancelled:1;
77 			uint8_t  latency_enabled:1;
78 
79 			uint32_t window_widening_periodic_us;
80 			uint32_t window_widening_max_us;
81 			uint32_t window_widening_prepare_us;
82 			uint32_t window_widening_event_us;
83 			uint32_t window_size_prepare_us;
84 			uint32_t window_size_event_us;
85 		} periph;
86 #endif /* CONFIG_BT_PERIPHERAL */
87 	};
88 
89 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
90 	struct {
91 		struct data_pdu_length local;
92 		struct data_pdu_length remote;
93 		struct data_pdu_length eff;
94 #if defined(CONFIG_BT_CTLR_PHY)
95 		uint16_t default_tx_time;
96 #endif
97 		uint16_t default_tx_octets;
98 		uint8_t update;
99 	} dle;
100 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
101 
102 #if defined(CONFIG_BT_CTLR_PHY)
103 	uint8_t phy_tx:3;
104 	uint8_t phy_flags:1;
105 	uint8_t phy_tx_time:3;
106 	uint8_t phy_rx:3;
107 #endif /* CONFIG_BT_CTLR_PHY */
108 
109 	MEMQ_DECLARE(tx);
110 	memq_link_t link_tx;
111 	memq_link_t *link_tx_free;
112 	uint8_t  packet_tx_head_len;
113 	uint8_t  packet_tx_head_offset;
114 
115 	uint8_t sn:1;
116 	uint8_t nesn:1;
117 	uint8_t empty:1;
118 
119 #if defined(CONFIG_BT_CTLR_LE_ENC)
120 	uint8_t enc_rx:1;
121 	uint8_t enc_tx:1;
122 
123 	struct ccm ccm_rx;
124 	struct ccm ccm_tx;
125 #endif /* CONFIG_BT_CTLR_LE_ENC */
126 
127 #if defined(CONFIG_BT_CTLR_SLOT_RESERVATION_UPDATE)
128 #if defined(CONFIG_BT_CTLR_DATA_LENGTH) || defined(CONFIG_BT_CTLR_PHY)
129 	uint8_t evt_len_upd:1;
130 	uint8_t evt_len_upd_delayed:1;
131 #endif /* CONFIG_BT_CTLR_DATA_LENGTH || CONFIG_BT_CTLR_PHY */
132 #endif /* CONFIG_BT_CTLR_SLOT_RESERVATION_UPDATE */
133 
134 #if defined(CONFIG_BT_CTLR_CONN_RSSI)
135 	uint8_t  rssi_latest;
136 #if defined(CONFIG_BT_CTLR_CONN_RSSI_EVENT)
137 	uint8_t  rssi_reported;
138 	uint8_t  rssi_sample_count;
139 #endif /* CONFIG_BT_CTLR_CONN_RSSI_EVENT */
140 #endif /* CONFIG_BT_CTLR_CONN_RSSI */
141 
142 #if defined(CONFIG_BT_CTLR_CONN_META)
143 	struct lll_conn_meta conn_meta;
144 #endif /* CONFIG_BT_CTLR_CONN_META */
145 
146 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
147 	int8_t tx_pwr_lvl;
148 #endif
149 
150 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX)
151 	struct lll_df_conn_rx_cfg df_rx_cfg;
152 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RX */
153 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_TX)
154 	struct lll_df_conn_tx_cfg df_tx_cfg;
155 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_TX */
156 };
157 
158 int lll_conn_init(void);
159 int lll_conn_reset(void);
160 void lll_conn_flush(uint16_t handle, struct lll_conn *lll);
161 
162 void lll_conn_prepare_reset(void);
163 void lll_conn_abort_cb(struct lll_prepare_param *prepare_param, void *param);
164 void lll_conn_isr_rx(void *param);
165 void lll_conn_isr_tx(void *param);
166 void lll_conn_rx_pkt_set(struct lll_conn *lll);
167 void lll_conn_tx_pkt_set(struct lll_conn *lll, struct pdu_data *pdu_data_tx);
168 void lll_conn_pdu_tx_prep(struct lll_conn *lll, struct pdu_data **pdu_data_tx);
169 uint8_t lll_conn_force_md_cnt_set(uint8_t force_md_cnt);
170 
171 extern struct lll_conn *ull_conn_lll_get(uint16_t handle);
172 extern void ull_conn_lll_tx_demux_sched(struct lll_conn *lll);
173 extern void ull_conn_lll_ack_enqueue(uint16_t handle, struct node_tx *tx);
174 extern uint16_t ull_conn_lll_max_tx_octets_get(struct lll_conn *lll);
175