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