1 /*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/slist.h>
11 #include <zephyr/sys/util.h>
12
13 #include <zephyr/bluetooth/hci_types.h>
14
15 #include "hal/ccm.h"
16
17 #include "util/util.h"
18 #include "util/mem.h"
19 #include "util/memq.h"
20 #include "util/dbuf.h"
21
22 #include "pdu_df.h"
23 #include "lll/pdu_vendor.h"
24 #include "pdu.h"
25
26 #include "ll.h"
27 #include "ll_settings.h"
28
29 #include "lll.h"
30 #include "lll/lll_df_types.h"
31 #include "lll_conn.h"
32 #include "lll_conn_iso.h"
33
34 #include "ull_tx_queue.h"
35
36 #include "isoal.h"
37 #include "ull_iso_types.h"
38 #include "ull_conn_iso_types.h"
39 #include "ull_conn_iso_internal.h"
40
41 #include "ull_conn_types.h"
42 #include "ull_llcp.h"
43 #include "ull_llcp_internal.h"
44 #include "ull_conn_internal.h"
45
46 #include <soc.h>
47 #include "hal/debug.h"
48
49 /* Hardcoded instant delta +6 */
50 #define CHMU_INSTANT_DELTA 6U
51
52 /* LLCP Local Procedure Channel Map Update FSM states */
53 enum {
54 LP_CHMU_STATE_IDLE = LLCP_STATE_IDLE,
55 LP_CHMU_STATE_WAIT_TX_CHAN_MAP_IND,
56 LP_CHMU_STATE_WAIT_INSTANT,
57 };
58
59 /* LLCP Local Procedure Channel Map Update FSM events */
60 enum {
61 /* Procedure run */
62 LP_CHMU_EVT_RUN,
63 };
64
65 /* LLCP Remote Procedure Channel Map Update FSM states */
66 enum {
67 RP_CHMU_STATE_IDLE = LLCP_STATE_IDLE,
68 RP_CHMU_STATE_WAIT_RX_CHAN_MAP_IND,
69 RP_CHMU_STATE_WAIT_INSTANT,
70 };
71
72 /* LLCP Remote Procedure Channel Map Update FSM events */
73 enum {
74 /* Procedure run */
75 RP_CHMU_EVT_RUN,
76
77 /* Indication received */
78 RP_CHMU_EVT_RX_CHAN_MAP_IND,
79 };
80
81 #if defined(CONFIG_BT_CENTRAL)
82 /*
83 * LLCP Local Procedure Channel Map Update FSM
84 */
lp_chmu_tx(struct ll_conn * conn,struct proc_ctx * ctx)85 static void lp_chmu_tx(struct ll_conn *conn, struct proc_ctx *ctx)
86 {
87 struct node_tx *tx;
88 struct pdu_data *pdu;
89
90 /* Allocate tx node */
91 tx = llcp_tx_alloc(conn, ctx);
92 LL_ASSERT(tx);
93
94 pdu = (struct pdu_data *)tx->pdu;
95
96 /* Encode LL Control PDU */
97 llcp_pdu_encode_chan_map_update_ind(ctx, pdu);
98
99 ctx->tx_opcode = pdu->llctrl.opcode;
100
101 /* Enqueue LL Control PDU towards LLL */
102 llcp_tx_enqueue(conn, tx);
103 }
104
lp_chmu_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)105 static void lp_chmu_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
106 {
107 ull_conn_chan_map_set(conn, ctx->data.chmu.chm);
108 llcp_lr_complete(conn);
109 ctx->state = LP_CHMU_STATE_IDLE;
110 }
111
lp_chmu_send_channel_map_update_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)112 static void lp_chmu_send_channel_map_update_ind(struct ll_conn *conn, struct proc_ctx *ctx,
113 uint8_t evt, void *param)
114 {
115 if (llcp_lr_ispaused(conn) || llcp_rr_get_collision(conn) ||
116 !llcp_tx_alloc_peek(conn, ctx)) {
117 ctx->state = LP_CHMU_STATE_WAIT_TX_CHAN_MAP_IND;
118 } else {
119 llcp_rr_set_incompat(conn, INCOMPAT_RESOLVABLE);
120
121 ctx->data.chmu.instant = ull_conn_event_counter(conn) + CHMU_INSTANT_DELTA;
122
123 lp_chmu_tx(conn, ctx);
124
125 ctx->state = LP_CHMU_STATE_WAIT_INSTANT;
126 }
127 }
128
lp_chmu_st_wait_tx_chan_map_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)129 static void lp_chmu_st_wait_tx_chan_map_ind(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
130 void *param)
131 {
132 switch (evt) {
133 case LP_CHMU_EVT_RUN:
134 lp_chmu_send_channel_map_update_ind(conn, ctx, evt, param);
135 break;
136 default:
137 /* Ignore other evts */
138 break;
139 }
140 }
141
lp_chmu_check_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)142 static void lp_chmu_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
143 void *param)
144 {
145 uint16_t event_counter = ull_conn_event_counter(conn);
146
147 if (is_instant_reached_or_passed(ctx->data.chmu.instant, event_counter)) {
148 llcp_rr_set_incompat(conn, INCOMPAT_NO_COLLISION);
149 lp_chmu_complete(conn, ctx, evt, param);
150 }
151 }
152
lp_chmu_st_wait_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)153 static void lp_chmu_st_wait_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
154 void *param)
155 {
156 switch (evt) {
157 case LP_CHMU_EVT_RUN:
158 lp_chmu_check_instant(conn, ctx, evt, param);
159 break;
160 default:
161 /* Ignore other evts */
162 break;
163 }
164 }
165
lp_chmu_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)166 static void lp_chmu_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
167 void *param)
168 {
169 switch (ctx->state) {
170 case LP_CHMU_STATE_IDLE:
171 /* Empty/fallthrough on purpose as idle state handling is equivalent to
172 * 'wait for tx state' - simply to attempt TX'ing chan map ind
173 */
174 case LP_CHMU_STATE_WAIT_TX_CHAN_MAP_IND:
175 lp_chmu_st_wait_tx_chan_map_ind(conn, ctx, evt, param);
176 break;
177 case LP_CHMU_STATE_WAIT_INSTANT:
178 lp_chmu_st_wait_instant(conn, ctx, evt, param);
179 break;
180 default:
181 /* Unknown state */
182 LL_ASSERT(0);
183 }
184 }
185
llcp_lp_chmu_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)186 void llcp_lp_chmu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
187 {
188 struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
189
190 switch (pdu->llctrl.opcode) {
191 default:
192 /* Invalid behaviour */
193 /* Invalid PDU received so terminate connection */
194 conn->llcp_terminate.reason_final = BT_HCI_ERR_LMP_PDU_NOT_ALLOWED;
195 llcp_lr_complete(conn);
196 ctx->state = LP_CHMU_STATE_IDLE;
197 break;
198 }
199 }
200
llcp_lp_chmu_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)201 void llcp_lp_chmu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
202 {
203 lp_chmu_execute_fsm(conn, ctx, LP_CHMU_EVT_RUN, param);
204 }
205
llcp_lp_chmu_awaiting_instant(struct proc_ctx * ctx)206 bool llcp_lp_chmu_awaiting_instant(struct proc_ctx *ctx)
207 {
208 return (ctx->state == LP_CHMU_STATE_WAIT_INSTANT);
209 }
210 #endif /* CONFIG_BT_CENTRAL */
211
212 #if defined(CONFIG_BT_PERIPHERAL)
213 /*
214 * LLCP Remote Procedure Channel Map Update FSM
215 */
rp_chmu_complete(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)216 static void rp_chmu_complete(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
217 {
218 ull_conn_chan_map_set(conn, ctx->data.chmu.chm);
219 llcp_rr_complete(conn);
220 ctx->state = RP_CHMU_STATE_IDLE;
221 }
222
rp_chmu_st_idle(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)223 static void rp_chmu_st_idle(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt, void *param)
224 {
225 switch (evt) {
226 case RP_CHMU_EVT_RUN:
227 ctx->state = RP_CHMU_STATE_WAIT_RX_CHAN_MAP_IND;
228 break;
229 default:
230 /* Ignore other evts */
231 break;
232 }
233 }
234
rp_chmu_st_wait_rx_channel_map_update_ind(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)235 static void rp_chmu_st_wait_rx_channel_map_update_ind(struct ll_conn *conn, struct proc_ctx *ctx,
236 uint8_t evt, void *param)
237 {
238 switch (evt) {
239 case RP_CHMU_EVT_RX_CHAN_MAP_IND:
240 llcp_pdu_decode_chan_map_update_ind(ctx, param);
241 if (is_instant_not_passed(ctx->data.chmu.instant,
242 ull_conn_event_counter(conn))) {
243
244 ctx->state = RP_CHMU_STATE_WAIT_INSTANT;
245 } else {
246 conn->llcp_terminate.reason_final = BT_HCI_ERR_INSTANT_PASSED;
247 llcp_rr_complete(conn);
248 ctx->state = RP_CHMU_STATE_IDLE;
249 }
250 break;
251 default:
252 /* Ignore other evts */
253 break;
254 }
255 }
256
rp_chmu_check_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)257 static void rp_chmu_check_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
258 void *param)
259 {
260 uint16_t event_counter = ull_conn_event_counter(conn);
261
262 if (((event_counter - ctx->data.chmu.instant) & 0xFFFF) <= 0x7FFF) {
263 rp_chmu_complete(conn, ctx, evt, param);
264 }
265 }
266
rp_chmu_st_wait_instant(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)267 static void rp_chmu_st_wait_instant(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
268 void *param)
269 {
270 switch (evt) {
271 case RP_CHMU_EVT_RUN:
272 rp_chmu_check_instant(conn, ctx, evt, param);
273 break;
274 default:
275 /* Ignore other evts */
276 break;
277 }
278 }
279
rp_chmu_execute_fsm(struct ll_conn * conn,struct proc_ctx * ctx,uint8_t evt,void * param)280 static void rp_chmu_execute_fsm(struct ll_conn *conn, struct proc_ctx *ctx, uint8_t evt,
281 void *param)
282 {
283 switch (ctx->state) {
284 case RP_CHMU_STATE_IDLE:
285 rp_chmu_st_idle(conn, ctx, evt, param);
286 break;
287 case RP_CHMU_STATE_WAIT_RX_CHAN_MAP_IND:
288 rp_chmu_st_wait_rx_channel_map_update_ind(conn, ctx, evt, param);
289 break;
290 case RP_CHMU_STATE_WAIT_INSTANT:
291 rp_chmu_st_wait_instant(conn, ctx, evt, param);
292 break;
293 default:
294 /* Unknown state */
295 LL_ASSERT(0);
296 }
297 }
298
llcp_rp_chmu_rx(struct ll_conn * conn,struct proc_ctx * ctx,struct node_rx_pdu * rx)299 void llcp_rp_chmu_rx(struct ll_conn *conn, struct proc_ctx *ctx, struct node_rx_pdu *rx)
300 {
301 struct pdu_data *pdu = (struct pdu_data *)rx->pdu;
302
303 switch (pdu->llctrl.opcode) {
304 case PDU_DATA_LLCTRL_TYPE_CHAN_MAP_IND:
305 rp_chmu_execute_fsm(conn, ctx, RP_CHMU_EVT_RX_CHAN_MAP_IND, pdu);
306 break;
307 default:
308 /* Invalid behaviour */
309 /* Invalid PDU received so terminate connection */
310 conn->llcp_terminate.reason_final = BT_HCI_ERR_LMP_PDU_NOT_ALLOWED;
311 llcp_rr_complete(conn);
312 ctx->state = RP_CHMU_STATE_IDLE;
313 break;
314 }
315 }
316
llcp_rp_chmu_run(struct ll_conn * conn,struct proc_ctx * ctx,void * param)317 void llcp_rp_chmu_run(struct ll_conn *conn, struct proc_ctx *ctx, void *param)
318 {
319 rp_chmu_execute_fsm(conn, ctx, RP_CHMU_EVT_RUN, param);
320 }
321
llcp_rp_chmu_awaiting_instant(struct proc_ctx * ctx)322 bool llcp_rp_chmu_awaiting_instant(struct proc_ctx *ctx)
323 {
324 return (ctx->state == RP_CHMU_STATE_WAIT_INSTANT);
325 }
326 #endif /* CONFIG_BT_PERIPHERAL */
327