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