1 /*
2 * Copyright (c) 2020 Demant
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/mayfly.h"
21 #include "util/dbuf.h"
22
23 #include "pdu_df.h"
24 #include "lll/pdu_vendor.h"
25 #include "pdu.h"
26
27 #include "ll.h"
28 #include "ll_settings.h"
29
30 #include "lll.h"
31 #include "lll_clock.h"
32 #include "lll/lll_df_types.h"
33 #include "lll_conn.h"
34 #include "lll_conn_iso.h"
35
36 #include "ull_tx_queue.h"
37
38 #include "isoal.h"
39 #include "ull_iso_types.h"
40 #include "ull_conn_iso_types.h"
41 #include "ull_conn_iso_internal.h"
42
43 #include "ull_conn_types.h"
44 #include "ull_llcp.h"
45 #include "ull_llcp_internal.h"
46
47 #include "ll_feat.h"
48
49 #include <soc.h>
50 #include "hal/debug.h"
51
52 /*
53 * we need to filter on octet 0; the following mask has 7 octets
54 * with all bits set to 1, the last octet is 0x00
55 */
56 #define FEAT_FILT_OCTET0 0xFFFFFFFFFFFFFF00
57
58 #if defined(CONFIG_BT_CTLR_LE_PING)
59 /*
60 * LE Ping Procedure Helpers
61 */
62
llcp_pdu_encode_ping_req(struct pdu_data * pdu)63 void llcp_pdu_encode_ping_req(struct pdu_data *pdu)
64 {
65 pdu->ll_id = PDU_DATA_LLID_CTRL;
66 pdu->len = PDU_DATA_LLCTRL_LEN(ping_req);
67 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_PING_REQ;
68 }
69
llcp_pdu_encode_ping_rsp(struct pdu_data * pdu)70 void llcp_pdu_encode_ping_rsp(struct pdu_data *pdu)
71 {
72 pdu->ll_id = PDU_DATA_LLID_CTRL;
73 pdu->len = PDU_DATA_LLCTRL_LEN(ping_rsp);
74 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_PING_RSP;
75 }
76 #endif /* CONFIG_BT_CTLR_LE_PING */
77 /*
78 * Unknown response helper
79 */
80
llcp_pdu_encode_unknown_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)81 void llcp_pdu_encode_unknown_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
82 {
83 pdu->ll_id = PDU_DATA_LLID_CTRL;
84 pdu->len = PDU_DATA_LLCTRL_LEN(unknown_rsp);
85 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_UNKNOWN_RSP;
86
87 pdu->llctrl.unknown_rsp.type = ctx->unknown_response.type;
88 }
89
llcp_pdu_decode_unknown_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)90 void llcp_pdu_decode_unknown_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
91 {
92 ctx->unknown_response.type = pdu->llctrl.unknown_rsp.type;
93 }
94
llcp_ntf_encode_unknown_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)95 void llcp_ntf_encode_unknown_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
96 {
97 struct pdu_data_llctrl_unknown_rsp *p;
98
99 pdu->ll_id = PDU_DATA_LLID_CTRL;
100 pdu->len = PDU_DATA_LLCTRL_LEN(unknown_rsp);
101 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_UNKNOWN_RSP;
102 p = &pdu->llctrl.unknown_rsp;
103 p->type = ctx->unknown_response.type;
104 }
105
106 /*
107 * Feature Exchange Procedure Helper
108 */
109
feature_filter(uint8_t * featuresin,uint64_t * featuresout)110 static void feature_filter(uint8_t *featuresin, uint64_t *featuresout)
111 {
112 uint64_t feat;
113
114 /*
115 * Note that in the split controller invalid bits are set
116 * to 1, in LLCP we set them to 0; the spec. does not
117 * define which value they should have
118 */
119 feat = sys_get_le64(featuresin);
120 feat &= LL_FEAT_BIT_MASK;
121 feat &= LL_FEAT_BIT_MASK_VALID;
122
123 *featuresout = feat;
124 }
125
llcp_pdu_encode_feature_req(struct ll_conn * conn,struct pdu_data * pdu)126 void llcp_pdu_encode_feature_req(struct ll_conn *conn, struct pdu_data *pdu)
127 {
128 struct pdu_data_llctrl_feature_req *p;
129
130 pdu->ll_id = PDU_DATA_LLID_CTRL;
131 pdu->len = PDU_DATA_LLCTRL_LEN(feature_req);
132 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_FEATURE_REQ;
133
134 #if defined(CONFIG_BT_CTLR_PER_INIT_FEAT_XCHG) && defined(CONFIG_BT_PERIPHERAL)
135 if (conn->lll.role == BT_HCI_ROLE_PERIPHERAL) {
136 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_PER_INIT_FEAT_XCHG;
137 }
138 #endif /* CONFIG_BT_CTLR_PER_INIT_FEAT_XCHG && CONFIG_BT_PERIPHERAL */
139
140 p = &pdu->llctrl.feature_req;
141 sys_put_le64(ll_feat_get(), p->features);
142 }
143
llcp_pdu_encode_feature_rsp(struct ll_conn * conn,struct pdu_data * pdu)144 void llcp_pdu_encode_feature_rsp(struct ll_conn *conn, struct pdu_data *pdu)
145 {
146 struct pdu_data_llctrl_feature_rsp *p;
147 uint64_t feature_rsp = ll_feat_get();
148
149 pdu->ll_id = PDU_DATA_LLID_CTRL;
150 pdu->len = PDU_DATA_LLCTRL_LEN(feature_rsp);
151 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_FEATURE_RSP;
152
153 p = &pdu->llctrl.feature_rsp;
154
155 /*
156 * we only filter on octet 0, remaining 7 octets are the features
157 * we support, as defined in LL_FEAT
158 */
159 feature_rsp &= (FEAT_FILT_OCTET0 | conn->llcp.fex.features_used);
160
161 sys_put_le64(feature_rsp, p->features);
162 }
163
llcp_ntf_encode_feature_rsp(struct ll_conn * conn,struct pdu_data * pdu)164 void llcp_ntf_encode_feature_rsp(struct ll_conn *conn, struct pdu_data *pdu)
165 {
166 struct pdu_data_llctrl_feature_rsp *p;
167
168 pdu->ll_id = PDU_DATA_LLID_CTRL;
169 pdu->len = PDU_DATA_LLCTRL_LEN(feature_rsp);
170 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_FEATURE_RSP;
171 p = &pdu->llctrl.feature_rsp;
172
173 sys_put_le64(conn->llcp.fex.features_peer, p->features);
174 }
175
features_used(uint64_t featureset)176 static uint64_t features_used(uint64_t featureset)
177 {
178 uint64_t x;
179
180 /* swap bits for role specific features */
181 x = ((featureset >> BT_LE_FEAT_BIT_CIS_CENTRAL) ^
182 (featureset >> BT_LE_FEAT_BIT_CIS_PERIPHERAL)) & 0x01;
183 x = (x << BT_LE_FEAT_BIT_CIS_CENTRAL) |
184 (x << BT_LE_FEAT_BIT_CIS_PERIPHERAL);
185 x ^= featureset;
186
187 return ll_feat_get() & x;
188 }
189
llcp_pdu_decode_feature_req(struct ll_conn * conn,struct pdu_data * pdu)190 void llcp_pdu_decode_feature_req(struct ll_conn *conn, struct pdu_data *pdu)
191 {
192 uint64_t featureset;
193
194 feature_filter(pdu->llctrl.feature_req.features, &featureset);
195 conn->llcp.fex.features_used = features_used(featureset);
196
197 featureset &= (FEAT_FILT_OCTET0 | conn->llcp.fex.features_used);
198 conn->llcp.fex.features_peer = featureset;
199
200 conn->llcp.fex.valid = 1;
201 }
202
llcp_pdu_decode_feature_rsp(struct ll_conn * conn,struct pdu_data * pdu)203 void llcp_pdu_decode_feature_rsp(struct ll_conn *conn, struct pdu_data *pdu)
204 {
205 uint64_t featureset;
206
207 feature_filter(pdu->llctrl.feature_rsp.features, &featureset);
208 conn->llcp.fex.features_used = features_used(featureset);
209 conn->llcp.fex.features_peer = featureset;
210 conn->llcp.fex.valid = 1;
211 }
212
213 #if defined(CONFIG_BT_CTLR_MIN_USED_CHAN)
214 /*
215 * Minimum used channels Procedure Helpers
216 */
217 #if defined(CONFIG_BT_PERIPHERAL)
llcp_pdu_encode_min_used_chans_ind(struct proc_ctx * ctx,struct pdu_data * pdu)218 void llcp_pdu_encode_min_used_chans_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
219 {
220 struct pdu_data_llctrl_min_used_chans_ind *p;
221
222 pdu->ll_id = PDU_DATA_LLID_CTRL;
223 pdu->len = PDU_DATA_LLCTRL_LEN(min_used_chans_ind);
224 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_MIN_USED_CHAN_IND;
225 p = &pdu->llctrl.min_used_chans_ind;
226 p->phys = ctx->data.muc.phys;
227 p->min_used_chans = ctx->data.muc.min_used_chans;
228 }
229 #endif /* CONFIG_BT_PERIPHERAL */
230
231 #if defined(CONFIG_BT_CENTRAL)
llcp_pdu_decode_min_used_chans_ind(struct ll_conn * conn,struct pdu_data * pdu)232 void llcp_pdu_decode_min_used_chans_ind(struct ll_conn *conn, struct pdu_data *pdu)
233 {
234 conn->llcp.muc.phys = pdu->llctrl.min_used_chans_ind.phys;
235 conn->llcp.muc.min_used_chans = pdu->llctrl.min_used_chans_ind.min_used_chans;
236 }
237 #endif /* CONFIG_BT_CENTRAL */
238 #endif /* CONFIG_BT_CTLR_MIN_USED_CHAN */
239
240 /*
241 * Termination Procedure Helper
242 */
llcp_pdu_encode_terminate_ind(struct proc_ctx * ctx,struct pdu_data * pdu)243 void llcp_pdu_encode_terminate_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
244 {
245 struct pdu_data_llctrl_terminate_ind *p;
246
247 pdu->ll_id = PDU_DATA_LLID_CTRL;
248 pdu->len = PDU_DATA_LLCTRL_LEN(terminate_ind);
249 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_TERMINATE_IND;
250 p = &pdu->llctrl.terminate_ind;
251 p->error_code = ctx->data.term.error_code;
252 }
253
llcp_pdu_decode_terminate_ind(struct proc_ctx * ctx,struct pdu_data * pdu)254 void llcp_pdu_decode_terminate_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
255 {
256 ctx->data.term.error_code = pdu->llctrl.terminate_ind.error_code;
257 }
258
259 /*
260 * Version Exchange Procedure Helper
261 */
llcp_pdu_encode_version_ind(struct pdu_data * pdu)262 void llcp_pdu_encode_version_ind(struct pdu_data *pdu)
263 {
264 uint16_t cid;
265 uint16_t svn;
266 struct pdu_data_llctrl_version_ind *p;
267
268 pdu->ll_id = PDU_DATA_LLID_CTRL;
269 pdu->len = PDU_DATA_LLCTRL_LEN(version_ind);
270 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_VERSION_IND;
271
272 p = &pdu->llctrl.version_ind;
273 p->version_number = LL_VERSION_NUMBER;
274 cid = sys_cpu_to_le16(ll_settings_company_id());
275 svn = sys_cpu_to_le16(ll_settings_subversion_number());
276 p->company_id = cid;
277 p->sub_version_number = svn;
278 }
279
llcp_ntf_encode_version_ind(struct ll_conn * conn,struct pdu_data * pdu)280 void llcp_ntf_encode_version_ind(struct ll_conn *conn, struct pdu_data *pdu)
281 {
282 struct pdu_data_llctrl_version_ind *p;
283
284 pdu->ll_id = PDU_DATA_LLID_CTRL;
285 pdu->len = PDU_DATA_LLCTRL_LEN(version_ind);
286 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_VERSION_IND;
287
288 p = &pdu->llctrl.version_ind;
289 p->version_number = conn->llcp.vex.cached.version_number;
290 p->company_id = sys_cpu_to_le16(conn->llcp.vex.cached.company_id);
291 p->sub_version_number = sys_cpu_to_le16(conn->llcp.vex.cached.sub_version_number);
292 }
293
llcp_pdu_decode_version_ind(struct ll_conn * conn,struct pdu_data * pdu)294 void llcp_pdu_decode_version_ind(struct ll_conn *conn, struct pdu_data *pdu)
295 {
296 conn->llcp.vex.valid = 1;
297 conn->llcp.vex.cached.version_number = pdu->llctrl.version_ind.version_number;
298 conn->llcp.vex.cached.company_id = sys_le16_to_cpu(pdu->llctrl.version_ind.company_id);
299 conn->llcp.vex.cached.sub_version_number =
300 sys_le16_to_cpu(pdu->llctrl.version_ind.sub_version_number);
301 }
302
303 #if defined(CONFIG_BT_CTLR_LE_ENC)
304 /*
305 * Encryption Start Procedure Helper
306 */
307
csrand_get(void * buf,size_t len)308 static int csrand_get(void *buf, size_t len)
309 {
310 if (mayfly_is_running()) {
311 return lll_csrand_isr_get(buf, len);
312 } else {
313 return lll_csrand_get(buf, len);
314 }
315 }
316
317 #if defined(CONFIG_BT_CENTRAL) || defined(CONFIG_BT_PERIPHERAL)
encode_enc_req(struct proc_ctx * ctx,struct pdu_data * pdu)318 static void encode_enc_req(struct proc_ctx *ctx, struct pdu_data *pdu)
319 {
320 struct pdu_data_llctrl_enc_req *p;
321
322 pdu->ll_id = PDU_DATA_LLID_CTRL;
323 pdu->len = PDU_DATA_LLCTRL_LEN(enc_req);
324 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_ENC_REQ;
325
326 p = &pdu->llctrl.enc_req;
327 memcpy(p->rand, ctx->data.enc.rand, sizeof(p->rand));
328 p->ediv[0] = ctx->data.enc.ediv[0];
329 p->ediv[1] = ctx->data.enc.ediv[1];
330 }
331 #endif /* CONFIG_BT_CENTRAL || CONFIG_BT_PERIPHERAL */
332
333 #if defined(CONFIG_BT_CENTRAL)
llcp_pdu_encode_enc_req(struct proc_ctx * ctx,struct pdu_data * pdu)334 void llcp_pdu_encode_enc_req(struct proc_ctx *ctx, struct pdu_data *pdu)
335 {
336 struct pdu_data_llctrl_enc_req *p;
337
338 encode_enc_req(ctx, pdu);
339
340 /* Optimal getting random data, p->ivm is packed right after p->skdm */
341 p = &pdu->llctrl.enc_req;
342 BUILD_ASSERT(offsetof(struct pdu_data_llctrl_enc_req, ivm) ==
343 offsetof(struct pdu_data_llctrl_enc_req, skdm) + sizeof(p->skdm),
344 "Member IVM must be after member SKDM");
345 csrand_get(p->skdm, sizeof(p->skdm) + sizeof(p->ivm));
346
347 }
348 #endif /* CONFIG_BT_CENTRAL */
349
350 #if defined(CONFIG_BT_PERIPHERAL)
llcp_ntf_encode_enc_req(struct proc_ctx * ctx,struct pdu_data * pdu)351 void llcp_ntf_encode_enc_req(struct proc_ctx *ctx, struct pdu_data *pdu)
352 {
353 encode_enc_req(ctx, pdu);
354 }
355
llcp_pdu_encode_enc_rsp(struct pdu_data * pdu)356 void llcp_pdu_encode_enc_rsp(struct pdu_data *pdu)
357 {
358 struct pdu_data_llctrl_enc_rsp *p;
359
360 pdu->ll_id = PDU_DATA_LLID_CTRL;
361 pdu->len = PDU_DATA_LLCTRL_LEN(enc_rsp);
362 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_ENC_RSP;
363
364 p = &pdu->llctrl.enc_rsp;
365 /* Optimal getting random data, p->ivs is packed right after p->skds */
366 BUILD_ASSERT(offsetof(struct pdu_data_llctrl_enc_rsp, ivs) ==
367 offsetof(struct pdu_data_llctrl_enc_rsp, skds) + sizeof(p->skds),
368 "Member IVS must be after member SKDS");
369 csrand_get(p->skds, sizeof(p->skds) + sizeof(p->ivs));
370 }
371
llcp_pdu_encode_start_enc_req(struct pdu_data * pdu)372 void llcp_pdu_encode_start_enc_req(struct pdu_data *pdu)
373 {
374 pdu->ll_id = PDU_DATA_LLID_CTRL;
375 pdu->len = PDU_DATA_LLCTRL_LEN(start_enc_req);
376 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_START_ENC_REQ;
377 }
378 #endif /* CONFIG_BT_PERIPHERAL */
379
llcp_pdu_encode_start_enc_rsp(struct pdu_data * pdu)380 void llcp_pdu_encode_start_enc_rsp(struct pdu_data *pdu)
381 {
382 pdu->ll_id = PDU_DATA_LLID_CTRL;
383 pdu->len = PDU_DATA_LLCTRL_LEN(start_enc_rsp);
384 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_START_ENC_RSP;
385 }
386
387 #if defined(CONFIG_BT_CENTRAL)
llcp_pdu_encode_pause_enc_req(struct pdu_data * pdu)388 void llcp_pdu_encode_pause_enc_req(struct pdu_data *pdu)
389 {
390 pdu->ll_id = PDU_DATA_LLID_CTRL;
391 pdu->len = PDU_DATA_LLCTRL_LEN(pause_enc_req);
392 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_REQ;
393 }
394 #endif /* CONFIG_BT_CENTRAL */
395
llcp_pdu_encode_pause_enc_rsp(struct pdu_data * pdu)396 void llcp_pdu_encode_pause_enc_rsp(struct pdu_data *pdu)
397 {
398 pdu->ll_id = PDU_DATA_LLID_CTRL;
399 pdu->len = PDU_DATA_LLCTRL_LEN(pause_enc_rsp);
400 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_PAUSE_ENC_RSP;
401 }
402 #endif /* CONFIG_BT_CTLR_LE_ENC */
403
llcp_pdu_encode_reject_ind(struct pdu_data * pdu,uint8_t error_code)404 void llcp_pdu_encode_reject_ind(struct pdu_data *pdu, uint8_t error_code)
405 {
406 pdu->ll_id = PDU_DATA_LLID_CTRL;
407 pdu->len = PDU_DATA_LLCTRL_LEN(reject_ind);
408 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_REJECT_IND;
409 pdu->llctrl.reject_ind.error_code = error_code;
410 }
411
llcp_pdu_encode_reject_ext_ind(struct pdu_data * pdu,uint8_t reject_opcode,uint8_t error_code)412 void llcp_pdu_encode_reject_ext_ind(struct pdu_data *pdu, uint8_t reject_opcode, uint8_t error_code)
413 {
414 pdu->ll_id = PDU_DATA_LLID_CTRL;
415 pdu->len = PDU_DATA_LLCTRL_LEN(reject_ext_ind);
416 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_REJECT_EXT_IND;
417 pdu->llctrl.reject_ext_ind.reject_opcode = reject_opcode;
418 pdu->llctrl.reject_ext_ind.error_code = error_code;
419 }
420
llcp_pdu_decode_reject_ext_ind(struct proc_ctx * ctx,struct pdu_data * pdu)421 void llcp_pdu_decode_reject_ext_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
422 {
423 ctx->reject_ext_ind.reject_opcode = pdu->llctrl.reject_ext_ind.reject_opcode;
424 ctx->reject_ext_ind.error_code = pdu->llctrl.reject_ext_ind.error_code;
425 }
426
llcp_ntf_encode_reject_ext_ind(struct proc_ctx * ctx,struct pdu_data * pdu)427 void llcp_ntf_encode_reject_ext_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
428 {
429 struct pdu_data_llctrl_reject_ext_ind *p;
430
431 pdu->ll_id = PDU_DATA_LLID_CTRL;
432 pdu->len = PDU_DATA_LLCTRL_LEN(reject_ext_ind);
433 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_REJECT_EXT_IND;
434
435 p = (void *)&pdu->llctrl.reject_ext_ind;
436 p->error_code = ctx->reject_ext_ind.error_code;
437 p->reject_opcode = ctx->reject_ext_ind.reject_opcode;
438 }
439
440 #ifdef CONFIG_BT_CTLR_PHY
441 /*
442 * PHY Update Procedure Helper
443 */
444
llcp_pdu_encode_phy_req(struct proc_ctx * ctx,struct pdu_data * pdu)445 void llcp_pdu_encode_phy_req(struct proc_ctx *ctx, struct pdu_data *pdu)
446 {
447 pdu->ll_id = PDU_DATA_LLID_CTRL;
448 pdu->len = PDU_DATA_LLCTRL_LEN(phy_req);
449 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_PHY_REQ;
450 pdu->llctrl.phy_req.rx_phys = ctx->data.pu.rx;
451 pdu->llctrl.phy_req.tx_phys = ctx->data.pu.tx;
452 }
453
llcp_pdu_decode_phy_req(struct proc_ctx * ctx,struct pdu_data * pdu)454 void llcp_pdu_decode_phy_req(struct proc_ctx *ctx, struct pdu_data *pdu)
455 {
456 ctx->data.pu.rx = pdu->llctrl.phy_req.tx_phys;
457 ctx->data.pu.tx = pdu->llctrl.phy_req.rx_phys;
458 }
459
460 #if defined(CONFIG_BT_PERIPHERAL)
llcp_pdu_encode_phy_rsp(struct ll_conn * conn,struct pdu_data * pdu)461 void llcp_pdu_encode_phy_rsp(struct ll_conn *conn, struct pdu_data *pdu)
462 {
463 pdu->ll_id = PDU_DATA_LLID_CTRL;
464 pdu->len = PDU_DATA_LLCTRL_LEN(phy_rsp);
465 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_PHY_RSP;
466 pdu->llctrl.phy_rsp.rx_phys = conn->phy_pref_rx;
467 pdu->llctrl.phy_rsp.tx_phys = conn->phy_pref_tx;
468 }
llcp_pdu_decode_phy_update_ind(struct proc_ctx * ctx,struct pdu_data * pdu)469 void llcp_pdu_decode_phy_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
470 {
471 ctx->data.pu.instant = sys_le16_to_cpu(pdu->llctrl.phy_upd_ind.instant);
472 ctx->data.pu.c_to_p_phy = pdu->llctrl.phy_upd_ind.c_to_p_phy;
473 ctx->data.pu.p_to_c_phy = pdu->llctrl.phy_upd_ind.p_to_c_phy;
474 }
475 #endif /* CONFIG_BT_PERIPHERAL */
476
477 #if defined(CONFIG_BT_CENTRAL)
llcp_pdu_encode_phy_update_ind(struct proc_ctx * ctx,struct pdu_data * pdu)478 void llcp_pdu_encode_phy_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
479 {
480 pdu->ll_id = PDU_DATA_LLID_CTRL;
481 pdu->len = PDU_DATA_LLCTRL_LEN(phy_upd_ind);
482 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_PHY_UPD_IND;
483 pdu->llctrl.phy_upd_ind.instant = sys_cpu_to_le16(ctx->data.pu.instant);
484 pdu->llctrl.phy_upd_ind.c_to_p_phy = ctx->data.pu.c_to_p_phy;
485 pdu->llctrl.phy_upd_ind.p_to_c_phy = ctx->data.pu.p_to_c_phy;
486 }
llcp_pdu_decode_phy_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)487 void llcp_pdu_decode_phy_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
488 {
489 ctx->data.pu.rx = pdu->llctrl.phy_rsp.tx_phys;
490 ctx->data.pu.tx = pdu->llctrl.phy_rsp.rx_phys;
491 }
492 #endif /* CONFIG_BT_CENTRAL */
493 #endif /* CONFIG_BT_CTLR_PHY */
494
495 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
496 /*
497 * Connection Update Procedure Helper
498 */
encode_conn_param_req_rsp_common(struct proc_ctx * ctx,struct pdu_data * pdu,struct pdu_data_llctrl_conn_param_req_rsp_common * p,uint8_t opcode)499 static void encode_conn_param_req_rsp_common(struct proc_ctx *ctx, struct pdu_data *pdu,
500 struct pdu_data_llctrl_conn_param_req_rsp_common *p,
501 uint8_t opcode)
502 {
503 pdu->ll_id = PDU_DATA_LLID_CTRL;
504 /* The '+ 1U' is to count in opcode octet, the first member of struct pdu_data_llctrl */
505 pdu->len = sizeof(struct pdu_data_llctrl_conn_param_req_rsp_common) + 1U;
506 pdu->llctrl.opcode = opcode;
507
508 p->interval_min = sys_cpu_to_le16(ctx->data.cu.interval_min);
509 p->interval_max = sys_cpu_to_le16(ctx->data.cu.interval_max);
510 p->latency = sys_cpu_to_le16(ctx->data.cu.latency);
511 p->timeout = sys_cpu_to_le16(ctx->data.cu.timeout);
512 p->preferred_periodicity = ctx->data.cu.preferred_periodicity;
513 p->reference_conn_event_count = sys_cpu_to_le16(ctx->data.cu.reference_conn_event_count);
514 p->offset0 = sys_cpu_to_le16(ctx->data.cu.offsets[0]);
515 p->offset1 = sys_cpu_to_le16(ctx->data.cu.offsets[1]);
516 p->offset2 = sys_cpu_to_le16(ctx->data.cu.offsets[2]);
517 p->offset3 = sys_cpu_to_le16(ctx->data.cu.offsets[3]);
518 p->offset4 = sys_cpu_to_le16(ctx->data.cu.offsets[4]);
519 p->offset5 = sys_cpu_to_le16(ctx->data.cu.offsets[5]);
520 }
521
522
llcp_pdu_encode_conn_param_req(struct proc_ctx * ctx,struct pdu_data * pdu)523 void llcp_pdu_encode_conn_param_req(struct proc_ctx *ctx, struct pdu_data *pdu)
524 {
525 encode_conn_param_req_rsp_common(ctx, pdu,
526 (struct pdu_data_llctrl_conn_param_req_rsp_common *)&pdu->llctrl.conn_param_req,
527 PDU_DATA_LLCTRL_TYPE_CONN_PARAM_REQ);
528 }
529
llcp_pdu_encode_conn_param_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)530 void llcp_pdu_encode_conn_param_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
531 {
532 encode_conn_param_req_rsp_common(ctx, pdu,
533 (struct pdu_data_llctrl_conn_param_req_rsp_common *)&pdu->llctrl.conn_param_rsp,
534 PDU_DATA_LLCTRL_TYPE_CONN_PARAM_RSP);
535 }
536
decode_conn_param_req_rsp_common(struct proc_ctx * ctx,struct pdu_data_llctrl_conn_param_req_rsp_common * p)537 static void decode_conn_param_req_rsp_common(struct proc_ctx *ctx,
538 struct pdu_data_llctrl_conn_param_req_rsp_common *p)
539 {
540 ctx->data.cu.interval_min = sys_le16_to_cpu(p->interval_min);
541 ctx->data.cu.interval_max = sys_le16_to_cpu(p->interval_max);
542 ctx->data.cu.latency = sys_le16_to_cpu(p->latency);
543 ctx->data.cu.timeout = sys_le16_to_cpu(p->timeout);
544 ctx->data.cu.preferred_periodicity = p->preferred_periodicity;
545 ctx->data.cu.reference_conn_event_count = sys_le16_to_cpu(p->reference_conn_event_count);
546 ctx->data.cu.offsets[0] = sys_le16_to_cpu(p->offset0);
547 ctx->data.cu.offsets[1] = sys_le16_to_cpu(p->offset1);
548 ctx->data.cu.offsets[2] = sys_le16_to_cpu(p->offset2);
549 ctx->data.cu.offsets[3] = sys_le16_to_cpu(p->offset3);
550 ctx->data.cu.offsets[4] = sys_le16_to_cpu(p->offset4);
551 ctx->data.cu.offsets[5] = sys_le16_to_cpu(p->offset5);
552 }
553
llcp_pdu_decode_conn_param_req(struct proc_ctx * ctx,struct pdu_data * pdu)554 void llcp_pdu_decode_conn_param_req(struct proc_ctx *ctx, struct pdu_data *pdu)
555 {
556 decode_conn_param_req_rsp_common(ctx,
557 (struct pdu_data_llctrl_conn_param_req_rsp_common *)&pdu->llctrl.conn_param_req);
558 }
559
llcp_pdu_decode_conn_param_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)560 void llcp_pdu_decode_conn_param_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
561 {
562 decode_conn_param_req_rsp_common(ctx,
563 (struct pdu_data_llctrl_conn_param_req_rsp_common *)&pdu->llctrl.conn_param_rsp);
564 }
565 #endif /* defined(CONFIG_BT_CTLR_CONN_PARAM_REQ) */
566
llcp_pdu_encode_conn_update_ind(struct proc_ctx * ctx,struct pdu_data * pdu)567 void llcp_pdu_encode_conn_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
568 {
569 struct pdu_data_llctrl_conn_update_ind *p;
570
571 pdu->ll_id = PDU_DATA_LLID_CTRL;
572 pdu->len = PDU_DATA_LLCTRL_LEN(conn_update_ind);
573 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CONN_UPDATE_IND;
574
575 p = (void *)&pdu->llctrl.conn_update_ind;
576 p->win_size = ctx->data.cu.win_size;
577 p->win_offset = sys_cpu_to_le16(ctx->data.cu.win_offset_us / CONN_INT_UNIT_US);
578 p->latency = sys_cpu_to_le16(ctx->data.cu.latency);
579 p->interval = sys_cpu_to_le16(ctx->data.cu.interval_max);
580 p->timeout = sys_cpu_to_le16(ctx->data.cu.timeout);
581 p->instant = sys_cpu_to_le16(ctx->data.cu.instant);
582 }
583
llcp_pdu_decode_conn_update_ind(struct proc_ctx * ctx,struct pdu_data * pdu)584 void llcp_pdu_decode_conn_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
585 {
586 struct pdu_data_llctrl_conn_update_ind *p;
587
588 p = (void *)&pdu->llctrl.conn_update_ind;
589 ctx->data.cu.win_size = p->win_size;
590 ctx->data.cu.win_offset_us = sys_le16_to_cpu(p->win_offset) * CONN_INT_UNIT_US;
591 ctx->data.cu.latency = sys_le16_to_cpu(p->latency);
592 ctx->data.cu.interval_max = sys_le16_to_cpu(p->interval);
593 ctx->data.cu.timeout = sys_le16_to_cpu(p->timeout);
594 ctx->data.cu.instant = sys_le16_to_cpu(p->instant);
595 }
596
597 /*
598 * Channel Map Update Procedure Helpers
599 */
llcp_pdu_encode_chan_map_update_ind(struct proc_ctx * ctx,struct pdu_data * pdu)600 void llcp_pdu_encode_chan_map_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
601 {
602 struct pdu_data_llctrl_chan_map_ind *p;
603
604 pdu->ll_id = PDU_DATA_LLID_CTRL;
605 pdu->len = PDU_DATA_LLCTRL_LEN(chan_map_ind);
606 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CHAN_MAP_IND;
607 p = &pdu->llctrl.chan_map_ind;
608 p->instant = sys_cpu_to_le16(ctx->data.chmu.instant);
609 memcpy(p->chm, ctx->data.chmu.chm, sizeof(p->chm));
610 }
611
llcp_pdu_decode_chan_map_update_ind(struct proc_ctx * ctx,struct pdu_data * pdu)612 void llcp_pdu_decode_chan_map_update_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
613 {
614 ctx->data.chmu.instant = sys_le16_to_cpu(pdu->llctrl.chan_map_ind.instant);
615 memcpy(ctx->data.chmu.chm, pdu->llctrl.chan_map_ind.chm, sizeof(ctx->data.chmu.chm));
616 }
617
618 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
619 /*
620 * Data Length Update Procedure Helpers
621 */
encode_length_req_rsp_common(struct pdu_data * pdu,struct pdu_data_llctrl_length_req_rsp_common * p,const uint8_t opcode,const struct data_pdu_length * dle)622 static void encode_length_req_rsp_common(struct pdu_data *pdu,
623 struct pdu_data_llctrl_length_req_rsp_common *p,
624 const uint8_t opcode,
625 const struct data_pdu_length *dle)
626 {
627 pdu->ll_id = PDU_DATA_LLID_CTRL;
628 /* The '+ 1U' is to count in opcode octet, the first member of struct pdu_data_llctrl */
629 pdu->len = sizeof(struct pdu_data_llctrl_length_req_rsp_common) + 1U;
630 pdu->llctrl.opcode = opcode;
631 p->max_rx_octets = sys_cpu_to_le16(dle->max_rx_octets);
632 p->max_tx_octets = sys_cpu_to_le16(dle->max_tx_octets);
633 p->max_rx_time = sys_cpu_to_le16(dle->max_rx_time);
634 p->max_tx_time = sys_cpu_to_le16(dle->max_tx_time);
635 }
636
llcp_pdu_encode_length_req(struct ll_conn * conn,struct pdu_data * pdu)637 void llcp_pdu_encode_length_req(struct ll_conn *conn, struct pdu_data *pdu)
638 {
639 encode_length_req_rsp_common(pdu,
640 (struct pdu_data_llctrl_length_req_rsp_common *)&pdu->llctrl.length_req,
641 PDU_DATA_LLCTRL_TYPE_LENGTH_REQ,
642 &conn->lll.dle.local);
643 }
644
llcp_pdu_encode_length_rsp(struct ll_conn * conn,struct pdu_data * pdu)645 void llcp_pdu_encode_length_rsp(struct ll_conn *conn, struct pdu_data *pdu)
646 {
647 encode_length_req_rsp_common(pdu,
648 (struct pdu_data_llctrl_length_req_rsp_common *)&pdu->llctrl.length_rsp,
649 PDU_DATA_LLCTRL_TYPE_LENGTH_RSP,
650 &conn->lll.dle.local);
651 }
652
llcp_ntf_encode_length_change(struct ll_conn * conn,struct pdu_data * pdu)653 void llcp_ntf_encode_length_change(struct ll_conn *conn, struct pdu_data *pdu)
654 {
655 encode_length_req_rsp_common(pdu,
656 (struct pdu_data_llctrl_length_req_rsp_common *)&pdu->llctrl.length_rsp,
657 PDU_DATA_LLCTRL_TYPE_LENGTH_RSP,
658 &conn->lll.dle.eff);
659 }
660
dle_remote_valid(const struct data_pdu_length * remote)661 static bool dle_remote_valid(const struct data_pdu_length *remote)
662 {
663 if (!IN_RANGE(remote->max_rx_octets, PDU_DC_PAYLOAD_SIZE_MIN,
664 PDU_DC_PAYLOAD_SIZE_MAX)) {
665 return false;
666 }
667
668 if (!IN_RANGE(remote->max_tx_octets, PDU_DC_PAYLOAD_SIZE_MIN,
669 PDU_DC_PAYLOAD_SIZE_MAX)) {
670 return false;
671 }
672
673 #if defined(CONFIG_BT_CTLR_PHY)
674 if (!IN_RANGE(remote->max_rx_time, PDU_DC_PAYLOAD_TIME_MIN,
675 PDU_DC_PAYLOAD_TIME_MAX_CODED)) {
676 return false;
677 }
678
679 if (!IN_RANGE(remote->max_tx_time, PDU_DC_PAYLOAD_TIME_MIN,
680 PDU_DC_PAYLOAD_TIME_MAX_CODED)) {
681 return false;
682 }
683 #else
684 if (!IN_RANGE(remote->max_rx_time, PDU_DC_PAYLOAD_TIME_MIN,
685 PDU_DC_PAYLOAD_TIME_MAX)) {
686 return false;
687 }
688
689 if (!IN_RANGE(remote->max_tx_time, PDU_DC_PAYLOAD_TIME_MIN,
690 PDU_DC_PAYLOAD_TIME_MAX)) {
691 return false;
692 }
693 #endif /* CONFIG_BT_CTLR_PHY */
694
695 return true;
696 }
decode_length_req_rsp_common(struct ll_conn * conn,struct pdu_data_llctrl_length_req_rsp_common * p)697 static void decode_length_req_rsp_common(struct ll_conn *conn,
698 struct pdu_data_llctrl_length_req_rsp_common *p)
699 {
700 struct data_pdu_length remote;
701
702 remote.max_rx_octets = sys_le16_to_cpu(p->max_rx_octets);
703 remote.max_tx_octets = sys_le16_to_cpu(p->max_tx_octets);
704 remote.max_rx_time = sys_le16_to_cpu(p->max_rx_time);
705 remote.max_tx_time = sys_le16_to_cpu(p->max_tx_time);
706
707 if (!dle_remote_valid(&remote)) {
708 return;
709 }
710
711 conn->lll.dle.remote = remote;
712 }
713
llcp_pdu_decode_length_req(struct ll_conn * conn,struct pdu_data * pdu)714 void llcp_pdu_decode_length_req(struct ll_conn *conn, struct pdu_data *pdu)
715 {
716 decode_length_req_rsp_common(conn,
717 (struct pdu_data_llctrl_length_req_rsp_common *)&pdu->llctrl.length_req);
718 }
719
llcp_pdu_decode_length_rsp(struct ll_conn * conn,struct pdu_data * pdu)720 void llcp_pdu_decode_length_rsp(struct ll_conn *conn, struct pdu_data *pdu)
721 {
722 decode_length_req_rsp_common(conn,
723 (struct pdu_data_llctrl_length_req_rsp_common *)&pdu->llctrl.length_rsp);
724 }
725 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
726
727 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
728 /*
729 * Constant Tone Request Procedure Helper
730 */
llcp_pdu_encode_cte_req(struct proc_ctx * ctx,struct pdu_data * pdu)731 void llcp_pdu_encode_cte_req(struct proc_ctx *ctx, struct pdu_data *pdu)
732 {
733 struct pdu_data_llctrl_cte_req *p;
734
735 pdu->ll_id = PDU_DATA_LLID_CTRL;
736 pdu->len = PDU_DATA_LLCTRL_LEN(cte_req);
737 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CTE_REQ;
738
739 p = &pdu->llctrl.cte_req;
740 p->min_cte_len_req = ctx->data.cte_req.min_len;
741 p->rfu = 0; /* Reserved for future use */
742 p->cte_type_req = ctx->data.cte_req.type;
743 }
744
llcp_pdu_decode_cte_rsp(struct proc_ctx * ctx,const struct pdu_data * pdu)745 void llcp_pdu_decode_cte_rsp(struct proc_ctx *ctx, const struct pdu_data *pdu)
746 {
747 if (pdu->cp == 0U || pdu->octet3.cte_info.time == 0U) {
748 ctx->data.cte_remote_rsp.has_cte = false;
749 } else {
750 ctx->data.cte_remote_rsp.has_cte = true;
751 }
752 }
753
llcp_ntf_encode_cte_req(struct pdu_data * pdu)754 void llcp_ntf_encode_cte_req(struct pdu_data *pdu)
755 {
756 pdu->ll_id = PDU_DATA_LLID_CTRL;
757 pdu->len = PDU_DATA_LLCTRL_LEN(cte_rsp);
758 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CTE_RSP;
759
760 /* Received LL_CTE_RSP PDU didn't have CTE */
761 pdu->cp = 0U;
762 }
763 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_REQ */
764
765 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP)
llcp_pdu_decode_cte_req(struct proc_ctx * ctx,struct pdu_data * pdu)766 void llcp_pdu_decode_cte_req(struct proc_ctx *ctx, struct pdu_data *pdu)
767 {
768 ctx->data.cte_remote_req.min_cte_len = pdu->llctrl.cte_req.min_cte_len_req;
769 ctx->data.cte_remote_req.cte_type = pdu->llctrl.cte_req.cte_type_req;
770 }
771
llcp_pdu_encode_cte_rsp(const struct proc_ctx * ctx,struct pdu_data * pdu)772 void llcp_pdu_encode_cte_rsp(const struct proc_ctx *ctx, struct pdu_data *pdu)
773 {
774 pdu->ll_id = PDU_DATA_LLID_CTRL;
775 pdu->len = PDU_DATA_LLCTRL_LEN(cte_rsp);
776 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CTE_RSP;
777
778 /* Set content of a PDU header first byte, that is not changed by LLL */
779 pdu->cp = 1U;
780 pdu->rfu = 0U;
781
782 pdu->octet3.cte_info.time = ctx->data.cte_remote_req.min_cte_len;
783 pdu->octet3.cte_info.type = ctx->data.cte_remote_req.cte_type;
784 }
785 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
786
787 #if defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
llcp_pdu_decode_cis_req(struct proc_ctx * ctx,struct pdu_data * pdu)788 void llcp_pdu_decode_cis_req(struct proc_ctx *ctx, struct pdu_data *pdu)
789 {
790 ctx->data.cis_create.cig_id = pdu->llctrl.cis_req.cig_id;
791 ctx->data.cis_create.cis_id = pdu->llctrl.cis_req.cis_id;
792 ctx->data.cis_create.cis_offset_min = sys_get_le24(pdu->llctrl.cis_req.cis_offset_min);
793 ctx->data.cis_create.cis_offset_max = sys_get_le24(pdu->llctrl.cis_req.cis_offset_max);
794 ctx->data.cis_create.conn_event_count =
795 sys_le16_to_cpu(pdu->llctrl.cis_req.conn_event_count);
796 ctx->data.cis_create.iso_interval = sys_le16_to_cpu(pdu->llctrl.cis_req.iso_interval);
797 /* The remainder of the req is decoded by ull_peripheral_iso_acquire, so
798 * no need to do it here too
799 ctx->data.cis_create.c_phy = pdu->llctrl.cis_req.c_phy;
800 ctx->data.cis_create.p_phy = pdu->llctrl.cis_req.p_phy;
801 ctx->data.cis_create.c_max_sdu = pdu->llctrl.cis_req.c_max_sdu;
802 ctx->data.cis_create.p_max_sdu = pdu->llctrl.cis_req.p_max_sdu;
803 ctx->data.cis_create.framed = pdu->llctrl.cis_req.framed;
804 ctx->data.cis_create.c_sdu_interval = sys_get_le24(pdu->llctrl.cis_req.c_sdu_interval);
805 ctx->data.cis_create.p_sdu_interval = sys_get_le24(pdu->llctrl.cis_req.p_sdu_interval);
806 ctx->data.cis_create.nse = pdu->llctrl.cis_req.nse;
807 ctx->data.cis_create.c_max_pdu = sys_le16_to_cpu(pdu->llctrl.cis_req.c_max_pdu);
808 ctx->data.cis_create.p_max_pdu = sys_le16_to_cpu(pdu->llctrl.cis_req.p_max_pdu);
809 ctx->data.cis_create.sub_interval = sys_get_le24(pdu->llctrl.cis_req.sub_interval);
810 ctx->data.cis_create.p_bn = pdu->llctrl.cis_req.p_bn;
811 ctx->data.cis_create.c_bn = pdu->llctrl.cis_req.c_bn;
812 ctx->data.cis_create.c_ft = pdu->llctrl.cis_req.c_ft;
813 ctx->data.cis_create.p_ft = pdu->llctrl.cis_req.p_ft;
814 */
815 }
816
llcp_pdu_decode_cis_ind(struct proc_ctx * ctx,struct pdu_data * pdu)817 void llcp_pdu_decode_cis_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
818 {
819 ctx->data.cis_create.conn_event_count =
820 sys_le16_to_cpu(pdu->llctrl.cis_ind.conn_event_count);
821 /* The remainder of the cis ind is decoded by ull_peripheral_iso_setup, so
822 * no need to do it here too
823 */
824 }
825
llcp_pdu_encode_cis_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)826 void llcp_pdu_encode_cis_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
827 {
828 struct pdu_data_llctrl_cis_rsp *p;
829
830 pdu->ll_id = PDU_DATA_LLID_CTRL;
831 pdu->len = PDU_DATA_LLCTRL_LEN(cis_rsp);
832 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CIS_RSP;
833
834 p = &pdu->llctrl.cis_rsp;
835 sys_put_le24(ctx->data.cis_create.cis_offset_max, p->cis_offset_max);
836 sys_put_le24(ctx->data.cis_create.cis_offset_min, p->cis_offset_min);
837 p->conn_event_count = sys_cpu_to_le16(ctx->data.cis_create.conn_event_count);
838 }
839 #endif /* defined(CONFIG_BT_CTLR_PERIPHERAL_ISO) */
840
841 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
llcp_pdu_encode_cis_req(struct proc_ctx * ctx,struct pdu_data * pdu)842 void llcp_pdu_encode_cis_req(struct proc_ctx *ctx, struct pdu_data *pdu)
843 {
844 struct pdu_data_llctrl_cis_req *p;
845
846 pdu->ll_id = PDU_DATA_LLID_CTRL;
847 pdu->len = offsetof(struct pdu_data_llctrl, cis_req) +
848 sizeof(struct pdu_data_llctrl_cis_req);
849 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CIS_REQ;
850
851 p = &pdu->llctrl.cis_req;
852
853 p->cig_id = ctx->data.cis_create.cig_id;
854 p->cis_id = ctx->data.cis_create.cis_id;
855 p->c_phy = ctx->data.cis_create.c_phy;
856 p->p_phy = ctx->data.cis_create.p_phy;
857 p->nse = ctx->data.cis_create.nse;
858 p->c_bn = ctx->data.cis_create.c_bn;
859 p->p_bn = ctx->data.cis_create.p_bn;
860 p->c_ft = ctx->data.cis_create.c_ft;
861 p->p_ft = ctx->data.cis_create.p_ft;
862
863 sys_put_le24(ctx->data.cis_create.c_sdu_interval, p->c_sdu_interval);
864 sys_put_le24(ctx->data.cis_create.p_sdu_interval, p->p_sdu_interval);
865 sys_put_le24(ctx->data.cis_create.cis_offset_max, p->cis_offset_max);
866 sys_put_le24(ctx->data.cis_create.cis_offset_min, p->cis_offset_min);
867 sys_put_le24(ctx->data.cis_create.sub_interval, p->sub_interval);
868
869 p->c_max_pdu = sys_cpu_to_le16(ctx->data.cis_create.c_max_pdu);
870 p->p_max_pdu = sys_cpu_to_le16(ctx->data.cis_create.p_max_pdu);
871 p->iso_interval = sys_cpu_to_le16(ctx->data.cis_create.iso_interval);
872 p->conn_event_count = sys_cpu_to_le16(ctx->data.cis_create.conn_event_count);
873
874 p->c_max_sdu_packed[0] = ctx->data.cis_create.c_max_sdu & 0xFF;
875 p->c_max_sdu_packed[1] = ((ctx->data.cis_create.c_max_sdu >> 8) & 0x0F) |
876 (ctx->data.cis_create.framed << 7);
877 p->p_max_sdu[0] = ctx->data.cis_create.p_max_sdu & 0xFF;
878 p->p_max_sdu[1] = (ctx->data.cis_create.p_max_sdu >> 8) & 0x0F;
879 }
880
llcp_pdu_decode_cis_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)881 void llcp_pdu_decode_cis_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
882 {
883 /* Limit response to valid range */
884 uint32_t cis_offset_min = sys_get_le24(pdu->llctrl.cis_rsp.cis_offset_min);
885 uint32_t cis_offset_max = sys_get_le24(pdu->llctrl.cis_rsp.cis_offset_max);
886
887 /* TODO: Fail procedure if offsets are invalid? */
888 if (cis_offset_min <= cis_offset_max &&
889 cis_offset_min >= ctx->data.cis_create.cis_offset_min &&
890 cis_offset_min <= ctx->data.cis_create.cis_offset_max &&
891 cis_offset_max <= ctx->data.cis_create.cis_offset_max &&
892 cis_offset_max >= ctx->data.cis_create.cis_offset_min) {
893 /* Offsets are valid */
894 ctx->data.cis_create.cis_offset_min = cis_offset_min;
895 ctx->data.cis_create.cis_offset_max = cis_offset_max;
896 }
897
898 ctx->data.cis_create.conn_event_count =
899 sys_le16_to_cpu(pdu->llctrl.cis_rsp.conn_event_count);
900 }
901
llcp_pdu_encode_cis_ind(struct proc_ctx * ctx,struct pdu_data * pdu)902 void llcp_pdu_encode_cis_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
903 {
904 struct pdu_data_llctrl_cis_ind *p;
905
906 pdu->ll_id = PDU_DATA_LLID_CTRL;
907 pdu->len = offsetof(struct pdu_data_llctrl, cis_ind) +
908 sizeof(struct pdu_data_llctrl_cis_ind);
909 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CIS_IND;
910
911 p = &pdu->llctrl.cis_ind;
912
913 p->aa[0] = ctx->data.cis_create.aa[0];
914 p->aa[1] = ctx->data.cis_create.aa[1];
915 p->aa[2] = ctx->data.cis_create.aa[2];
916 p->aa[3] = ctx->data.cis_create.aa[3];
917
918 sys_put_le24(ctx->data.cis_create.cis_offset_min, p->cis_offset);
919 sys_put_le24(ctx->data.cis_create.cig_sync_delay, p->cig_sync_delay);
920 sys_put_le24(ctx->data.cis_create.cis_sync_delay, p->cis_sync_delay);
921
922 p->conn_event_count = sys_cpu_to_le16(ctx->data.cis_create.conn_event_count);
923 }
924 #endif /* CONFIG_BT_CTLR_CENTRAL_ISO */
925
926 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO) || defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
llcp_pdu_encode_cis_terminate_ind(struct proc_ctx * ctx,struct pdu_data * pdu)927 void llcp_pdu_encode_cis_terminate_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
928 {
929 struct pdu_data_llctrl_cis_terminate_ind *p;
930
931 pdu->ll_id = PDU_DATA_LLID_CTRL;
932 pdu->len = PDU_DATA_LLCTRL_LEN(cis_terminate_ind);
933 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CIS_TERMINATE_IND;
934
935 p = &pdu->llctrl.cis_terminate_ind;
936 p->cig_id = ctx->data.cis_term.cig_id;
937 p->cis_id = ctx->data.cis_term.cis_id;
938 p->error_code = ctx->data.cis_term.error_code;
939 }
940
llcp_pdu_decode_cis_terminate_ind(struct proc_ctx * ctx,struct pdu_data * pdu)941 void llcp_pdu_decode_cis_terminate_ind(struct proc_ctx *ctx, struct pdu_data *pdu)
942 {
943 ctx->data.cis_term.cig_id = pdu->llctrl.cis_terminate_ind.cig_id;
944 ctx->data.cis_term.cis_id = pdu->llctrl.cis_terminate_ind.cis_id;
945 ctx->data.cis_term.error_code = pdu->llctrl.cis_terminate_ind.error_code;
946 }
947 #endif /* defined(CONFIG_BT_CTLR_CENTRAL_ISO) || defined(CONFIG_BT_CTLR_PERIPHERAL_ISO) */
948
949 #if defined(CONFIG_BT_CTLR_SCA_UPDATE)
950 /*
951 * SCA Update Procedure Helpers
952 */
llcp_pdu_encode_clock_accuracy_req(struct proc_ctx * ctx,struct pdu_data * pdu)953 void llcp_pdu_encode_clock_accuracy_req(struct proc_ctx *ctx, struct pdu_data *pdu)
954 {
955 struct pdu_data_llctrl_clock_accuracy_req *p = &pdu->llctrl.clock_accuracy_req;
956
957 pdu->ll_id = PDU_DATA_LLID_CTRL;
958 pdu->len = PDU_DATA_LLCTRL_LEN(clock_accuracy_req);
959 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CLOCK_ACCURACY_REQ;
960 /* Currently we do not support variable SCA, so we always 'report' current SCA */
961 p->sca = lll_clock_sca_local_get();
962 }
963
llcp_pdu_encode_clock_accuracy_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)964 void llcp_pdu_encode_clock_accuracy_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
965 {
966 struct pdu_data_llctrl_clock_accuracy_rsp *p = &pdu->llctrl.clock_accuracy_rsp;
967
968 pdu->ll_id = PDU_DATA_LLID_CTRL;
969 pdu->len = PDU_DATA_LLCTRL_LEN(clock_accuracy_rsp);
970 pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CLOCK_ACCURACY_RSP;
971 /* Currently we do not support variable SCA, so we always 'report' current SCA */
972 p->sca = lll_clock_sca_local_get();
973 }
974
llcp_pdu_decode_clock_accuracy_req(struct proc_ctx * ctx,struct pdu_data * pdu)975 void llcp_pdu_decode_clock_accuracy_req(struct proc_ctx *ctx, struct pdu_data *pdu)
976 {
977 struct pdu_data_llctrl_clock_accuracy_req *p = &pdu->llctrl.clock_accuracy_req;
978
979 ctx->data.sca_update.sca = p->sca;
980 }
981
llcp_pdu_decode_clock_accuracy_rsp(struct proc_ctx * ctx,struct pdu_data * pdu)982 void llcp_pdu_decode_clock_accuracy_rsp(struct proc_ctx *ctx, struct pdu_data *pdu)
983 {
984 struct pdu_data_llctrl_clock_accuracy_rsp *p = &pdu->llctrl.clock_accuracy_rsp;
985
986 ctx->data.sca_update.sca = p->sca;
987 }
988 #endif /* CONFIG_BT_CTLR_SCA_UPDATE */
989