1 /*
2 * Copyright (c) 2017-2021 Nordic Semiconductor ASA
3 * Copyright (c) 2015-2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7 #include <sys/types.h>
8
9 #include <zephyr/sys/byteorder.h>
10
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/bluetooth/hci.h>
13 #include <zephyr/bluetooth/buf.h>
14
15 #include "addr_internal.h"
16 #include "hci_core.h"
17 #include "conn_internal.h"
18 #include "id.h"
19 #include "scan.h"
20
21 #include "common/bt_str.h"
22
23 #define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
24 #include <zephyr/logging/log.h>
25 LOG_MODULE_REGISTER(bt_adv);
26
27 enum adv_name_type {
28 ADV_NAME_TYPE_NONE,
29 ADV_NAME_TYPE_AD,
30 ADV_NAME_TYPE_SD,
31 };
32
33 struct bt_ad {
34 /* Pointer to an LTV structure */
35 const struct bt_data *data;
36 /* Number of elements in @p data */
37 size_t len;
38 };
39
40 struct ad_stream {
41 /* ad is a two dimensional array of struct bt_data elements. */
42 const struct bt_ad *ad;
43 /* The number of struct bt_ad elements. */
44 size_t ad_len;
45
46 /* The current index in the array of struct bt_ad elements */
47 size_t ad_index;
48 /* The current index in the array of ad.data elements */
49 size_t data_index;
50
51 /* Current LTV offset contains the data offset in the ad[x].data[y].data value array
52 * The length and type are included in this offset.
53 */
54 uint16_t current_ltv_offset;
55
56 /* The remaining size of total ad[i].data[j].data_len + 2 for LTV header */
57 size_t remaining_size;
58 };
59
ad_stream_new(struct ad_stream * stream,const struct bt_ad * ad,size_t ad_len)60 static int ad_stream_new(struct ad_stream *stream,
61 const struct bt_ad *ad, size_t ad_len)
62 {
63 (void)memset(stream, 0, sizeof(*stream));
64 stream->ad = ad;
65 stream->ad_len = ad_len;
66
67 for (size_t i = 0; i < ad_len; i++) {
68 for (size_t j = 0; j < ad[i].len; j++) {
69 /* LTV length + type + value */
70 stream->remaining_size += ad[i].data[j].data_len + 2;
71
72 if (stream->remaining_size > BT_GAP_ADV_MAX_EXT_ADV_DATA_LEN) {
73 return -EINVAL;
74 }
75 }
76 }
77
78 return 0;
79 }
80
81 /**
82 * @brief Returns true if the current stream is empty.
83 *
84 * @param stream AD stream, @ref ad_stream_new
85 *
86 * @returns true if the stream is now empty.
87 */
ad_stream_is_empty(const struct ad_stream * stream)88 static bool ad_stream_is_empty(const struct ad_stream *stream)
89 {
90 return stream->remaining_size == 0;
91 }
92
93 /**
94 * @brief Returns the bt_data structure that is currently being read
95 *
96 * If the structure has been fully read, the function iterates to the next
97 *
98 * @param stream AD stream, @ref ad_stream_new
99 *
100 * @returns The current LTV structure or NULL if there are no left.
101 */
ad_stream_current_ltv_update(struct ad_stream * stream)102 static const struct bt_data *ad_stream_current_ltv_update(struct ad_stream *stream)
103 {
104 const struct bt_data *current_ltv = &stream->ad[stream->ad_index].data[stream->data_index];
105 const bool done_reading_ltv = (stream->current_ltv_offset == current_ltv->data_len + 2);
106
107 if (done_reading_ltv) {
108 stream->current_ltv_offset = 0;
109
110 if (stream->data_index + 1 == stream->ad[stream->ad_index].len) {
111 stream->data_index = 0;
112 stream->ad_index++;
113 } else {
114 stream->data_index++;
115 }
116 }
117
118 if (stream->ad_index == stream->ad_len) {
119 return NULL;
120 } else {
121 return &stream->ad[stream->ad_index].data[stream->data_index];
122 }
123 }
124
125 /**
126 * @brief Read at max buf_len data from the flattened AD stream.
127 *
128 * The read data can contain multiple LTV AD structures.
129 *
130 * @param stream AD stream, @ref ad_stream_new
131 * @param buf Buffer where the data will be put
132 * @param buf_len Buffer length
133 *
134 * @returns The number of bytes read from the stream written to the provided buffer
135 */
ad_stream_read(struct ad_stream * stream,uint8_t * buf,uint8_t buf_len)136 static uint8_t ad_stream_read(struct ad_stream *stream, uint8_t *buf, uint8_t buf_len)
137 {
138 uint8_t read_len = 0;
139
140 while (read_len < buf_len) {
141 const struct bt_data *current_ltv = ad_stream_current_ltv_update(stream);
142
143 if (!current_ltv) {
144 break;
145 }
146
147 if (stream->current_ltv_offset == 0) {
148 buf[read_len] = current_ltv->data_len + 1;
149 stream->current_ltv_offset++;
150 read_len++;
151 } else if (stream->current_ltv_offset == 1) {
152 buf[read_len] = current_ltv->type;
153 stream->current_ltv_offset++;
154 read_len++;
155 } else {
156 const size_t remaining_data_len =
157 current_ltv->data_len - stream->current_ltv_offset + 2;
158 const size_t size_to_copy = MIN(buf_len - read_len, remaining_data_len);
159
160 (void)memcpy(&buf[read_len],
161 ¤t_ltv->data[stream->current_ltv_offset - 2],
162 size_to_copy);
163 stream->current_ltv_offset += size_to_copy;
164 read_len += size_to_copy;
165 }
166 }
167
168 __ASSERT_NO_MSG(stream->remaining_size >= read_len);
169 stream->remaining_size -= read_len;
170
171 return read_len;
172 }
173
get_adv_name_type(const struct bt_le_ext_adv * adv)174 enum adv_name_type get_adv_name_type(const struct bt_le_ext_adv *adv)
175 {
176 if (atomic_test_bit(adv->flags, BT_ADV_INCLUDE_NAME_SD)) {
177 return ADV_NAME_TYPE_SD;
178 }
179
180 if (atomic_test_bit(adv->flags, BT_ADV_INCLUDE_NAME_AD)) {
181 return ADV_NAME_TYPE_AD;
182 }
183
184 return ADV_NAME_TYPE_NONE;
185 }
186
get_adv_name_type_param(const struct bt_le_adv_param * param)187 enum adv_name_type get_adv_name_type_param(const struct bt_le_adv_param *param)
188 {
189 if (param->options & BT_LE_ADV_OPT_USE_NAME) {
190 if (param->options & BT_LE_ADV_OPT_FORCE_NAME_IN_AD) {
191 return ADV_NAME_TYPE_AD;
192 }
193
194 if ((param->options & BT_LE_ADV_OPT_EXT_ADV) &&
195 !(param->options & BT_LE_ADV_OPT_SCANNABLE)) {
196 return ADV_NAME_TYPE_AD;
197 }
198
199 return ADV_NAME_TYPE_SD;
200 }
201
202 return ADV_NAME_TYPE_NONE;
203 }
204
205 #if defined(CONFIG_BT_EXT_ADV)
206 static struct bt_le_ext_adv adv_pool[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
207 #endif /* defined(CONFIG_BT_EXT_ADV) */
208
209
210 #if defined(CONFIG_BT_EXT_ADV)
bt_le_ext_adv_get_index(struct bt_le_ext_adv * adv)211 uint8_t bt_le_ext_adv_get_index(struct bt_le_ext_adv *adv)
212 {
213 ptrdiff_t index = adv - adv_pool;
214
215 __ASSERT(index >= 0 && index < ARRAY_SIZE(adv_pool),
216 "Invalid bt_adv pointer");
217 return (uint8_t)index;
218 }
219
adv_new(void)220 static struct bt_le_ext_adv *adv_new(void)
221 {
222 struct bt_le_ext_adv *adv = NULL;
223 int i;
224
225 for (i = 0; i < ARRAY_SIZE(adv_pool); i++) {
226 if (!atomic_test_bit(adv_pool[i].flags, BT_ADV_CREATED)) {
227 adv = &adv_pool[i];
228 break;
229 }
230 }
231
232 if (!adv) {
233 return NULL;
234 }
235
236 (void)memset(adv, 0, sizeof(*adv));
237 atomic_set_bit(adv_pool[i].flags, BT_ADV_CREATED);
238 adv->handle = i;
239
240 return adv;
241 }
242
adv_delete(struct bt_le_ext_adv * adv)243 static void adv_delete(struct bt_le_ext_adv *adv)
244 {
245 atomic_clear_bit(adv->flags, BT_ADV_CREATED);
246 }
247
248 #if defined(CONFIG_BT_BROADCASTER)
bt_hci_adv_lookup_handle(uint8_t handle)249 struct bt_le_ext_adv *bt_hci_adv_lookup_handle(uint8_t handle)
250 {
251 if (handle < ARRAY_SIZE(adv_pool) &&
252 atomic_test_bit(adv_pool[handle].flags, BT_ADV_CREATED)) {
253 return &adv_pool[handle];
254 }
255
256 return NULL;
257 }
258 #endif /* CONFIG_BT_BROADCASTER */
259 #endif /* defined(CONFIG_BT_EXT_ADV) */
260
bt_le_ext_adv_foreach(void (* func)(struct bt_le_ext_adv * adv,void * data),void * data)261 void bt_le_ext_adv_foreach(void (*func)(struct bt_le_ext_adv *adv, void *data),
262 void *data)
263 {
264 #if defined(CONFIG_BT_EXT_ADV)
265 for (size_t i = 0; i < ARRAY_SIZE(adv_pool); i++) {
266 if (atomic_test_bit(adv_pool[i].flags, BT_ADV_CREATED)) {
267 func(&adv_pool[i], data);
268 }
269 }
270 #else
271 func(&bt_dev.adv, data);
272 #endif /* defined(CONFIG_BT_EXT_ADV) */
273 }
274
bt_adv_reset_adv_pool(void)275 void bt_adv_reset_adv_pool(void)
276 {
277 #if defined(CONFIG_BT_EXT_ADV)
278 (void)memset(&adv_pool, 0, sizeof(adv_pool));
279 #endif /* defined(CONFIG_BT_EXT_ADV) */
280
281 (void)memset(&bt_dev.adv, 0, sizeof(bt_dev.adv));
282 }
283
adv_get_legacy(void)284 static struct bt_le_ext_adv *adv_get_legacy(void)
285 {
286 #if defined(CONFIG_BT_EXT_ADV)
287 if (bt_dev.adv) {
288 return bt_dev.adv;
289 }
290
291 bt_dev.adv = adv_new();
292 return bt_dev.adv;
293 #else
294 return &bt_dev.adv;
295 #endif
296 }
297
bt_le_adv_delete_legacy(void)298 void bt_le_adv_delete_legacy(void)
299 {
300 #if defined(CONFIG_BT_EXT_ADV)
301 if (bt_dev.adv) {
302 atomic_clear_bit(bt_dev.adv->flags, BT_ADV_CREATED);
303 bt_dev.adv = NULL;
304 }
305 #endif
306 }
307
bt_le_adv_lookup_legacy(void)308 struct bt_le_ext_adv *bt_le_adv_lookup_legacy(void)
309 {
310 #if defined(CONFIG_BT_EXT_ADV)
311 return bt_dev.adv;
312 #else
313 return &bt_dev.adv;
314 #endif
315 }
316
bt_le_adv_set_enable_legacy(struct bt_le_ext_adv * adv,bool enable)317 int bt_le_adv_set_enable_legacy(struct bt_le_ext_adv *adv, bool enable)
318 {
319 struct net_buf *buf;
320 struct bt_hci_cmd_state_set state;
321 int err;
322
323 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_ENABLE, 1);
324 if (!buf) {
325 return -ENOBUFS;
326 }
327
328 if (enable) {
329 net_buf_add_u8(buf, BT_HCI_LE_ADV_ENABLE);
330 } else {
331 net_buf_add_u8(buf, BT_HCI_LE_ADV_DISABLE);
332 }
333
334 bt_hci_cmd_state_set_init(buf, &state, adv->flags, BT_ADV_ENABLED, enable);
335
336 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_ENABLE, buf, NULL);
337 if (err) {
338 return err;
339 }
340
341 return 0;
342 }
343
bt_le_adv_set_enable_ext(struct bt_le_ext_adv * adv,bool enable,const struct bt_le_ext_adv_start_param * param)344 int bt_le_adv_set_enable_ext(struct bt_le_ext_adv *adv,
345 bool enable,
346 const struct bt_le_ext_adv_start_param *param)
347 {
348 struct net_buf *buf;
349 struct bt_hci_cmd_state_set state;
350 int err;
351
352 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_ADV_ENABLE, 6);
353 if (!buf) {
354 return -ENOBUFS;
355 }
356
357 if (enable) {
358 net_buf_add_u8(buf, BT_HCI_LE_ADV_ENABLE);
359 } else {
360 net_buf_add_u8(buf, BT_HCI_LE_ADV_DISABLE);
361 }
362
363 net_buf_add_u8(buf, 1);
364
365 net_buf_add_u8(buf, adv->handle);
366 net_buf_add_le16(buf, param ? param->timeout : 0);
367 net_buf_add_u8(buf, param ? param->num_events : 0);
368
369 bt_hci_cmd_state_set_init(buf, &state, adv->flags, BT_ADV_ENABLED, enable);
370
371 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_ADV_ENABLE, buf, NULL);
372 if (err) {
373 return err;
374 }
375
376 return 0;
377 }
378
bt_le_adv_set_enable(struct bt_le_ext_adv * adv,bool enable)379 int bt_le_adv_set_enable(struct bt_le_ext_adv *adv, bool enable)
380 {
381 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
382 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
383 return bt_le_adv_set_enable_ext(adv, enable, NULL);
384 }
385
386 return bt_le_adv_set_enable_legacy(adv, enable);
387 }
388
valid_adv_ext_param(const struct bt_le_adv_param * param)389 static bool valid_adv_ext_param(const struct bt_le_adv_param *param)
390 {
391 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
392 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
393 if (param->peer &&
394 !(param->options & BT_LE_ADV_OPT_EXT_ADV) &&
395 !(param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
396 /* Cannot do directed non-connectable advertising
397 * without extended advertising.
398 */
399 return false;
400 }
401
402 if (param->peer &&
403 (param->options & BT_LE_ADV_OPT_EXT_ADV) &&
404 !(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
405 /* High duty cycle directed connectable advertising
406 * shall not be used with Extended Advertising.
407 */
408 return false;
409 }
410
411 if (!(param->options & BT_LE_ADV_OPT_EXT_ADV) &&
412 param->options & (BT_LE_ADV_OPT_EXT_ADV |
413 BT_LE_ADV_OPT_NO_2M |
414 BT_LE_ADV_OPT_CODED |
415 BT_LE_ADV_OPT_ANONYMOUS |
416 BT_LE_ADV_OPT_USE_TX_POWER)) {
417 /* Extended options require extended advertising. */
418 return false;
419 }
420
421 if ((param->options & BT_LE_ADV_OPT_EXT_ADV) &&
422 (param->options & BT_LE_ADV_OPT_SCANNABLE) &&
423 (param->options & BT_LE_ADV_OPT_FORCE_NAME_IN_AD)) {
424 /* Advertising data is not permitted for an extended
425 * scannable advertiser.
426 */
427 return false;
428 }
429 }
430
431 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
432 param->peer &&
433 (param->options & BT_LE_ADV_OPT_USE_IDENTITY) &&
434 (param->options & BT_LE_ADV_OPT_DIR_ADDR_RPA)) {
435 /* own addr type used for both RPAs in directed advertising. */
436 return false;
437 }
438
439 if (param->id >= bt_dev.id_count ||
440 bt_addr_le_eq(&bt_dev.id_addr[param->id], BT_ADDR_LE_ANY)) {
441 return false;
442 }
443
444 if (!(param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
445 /*
446 * BT Core 4.2 [Vol 2, Part E, 7.8.5]
447 * The Advertising_Interval_Min and Advertising_Interval_Max
448 * shall not be set to less than 0x00A0 (100 ms) if the
449 * Advertising_Type is set to ADV_SCAN_IND or ADV_NONCONN_IND.
450 */
451 if (bt_dev.hci_version < BT_HCI_VERSION_5_0 &&
452 param->interval_min < 0x00a0) {
453 return false;
454 }
455 }
456
457 if ((param->options & (BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY |
458 BT_LE_ADV_OPT_DIR_ADDR_RPA)) &&
459 !param->peer) {
460 return false;
461 }
462
463 if ((param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY) ||
464 !param->peer) {
465 if (param->interval_min > param->interval_max ||
466 param->interval_min < 0x0020 ||
467 param->interval_max > 0x4000) {
468 return false;
469 }
470 }
471
472 if ((param->options & BT_LE_ADV_OPT_DISABLE_CHAN_37) &&
473 (param->options & BT_LE_ADV_OPT_DISABLE_CHAN_38) &&
474 (param->options & BT_LE_ADV_OPT_DISABLE_CHAN_39)) {
475 return false;
476 }
477
478 return true;
479 }
480
valid_adv_param(const struct bt_le_adv_param * param)481 static bool valid_adv_param(const struct bt_le_adv_param *param)
482 {
483 if (param->options & BT_LE_ADV_OPT_EXT_ADV) {
484 return false;
485 }
486
487 if (param->peer && !(param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
488 return false;
489 }
490
491 return valid_adv_ext_param(param);
492 }
493
set_data_add_complete(uint8_t * set_data,uint8_t set_data_len_max,const struct bt_ad * ad,size_t ad_len,uint8_t * data_len)494 static int set_data_add_complete(uint8_t *set_data, uint8_t set_data_len_max,
495 const struct bt_ad *ad, size_t ad_len, uint8_t *data_len)
496 {
497 uint8_t set_data_len = 0;
498
499 for (size_t i = 0; i < ad_len; i++) {
500 const struct bt_data *data = ad[i].data;
501
502 for (size_t j = 0; j < ad[i].len; j++) {
503 size_t len = data[j].data_len;
504 uint8_t type = data[j].type;
505
506 /* Check if ad fit in the remaining buffer */
507 if ((set_data_len + len + 2) > set_data_len_max) {
508 ssize_t shortened_len = set_data_len_max -
509 (set_data_len + 2);
510
511 if (!(type == BT_DATA_NAME_COMPLETE &&
512 shortened_len > 0)) {
513 LOG_ERR("Too big advertising data");
514 return -EINVAL;
515 }
516
517 type = BT_DATA_NAME_SHORTENED;
518 len = shortened_len;
519 }
520
521 set_data[set_data_len++] = len + 1;
522 set_data[set_data_len++] = type;
523
524 memcpy(&set_data[set_data_len], data[j].data, len);
525 set_data_len += len;
526 }
527 }
528
529 *data_len = set_data_len;
530 return 0;
531 }
532
hci_set_ad(uint16_t hci_op,const struct bt_ad * ad,size_t ad_len)533 static int hci_set_ad(uint16_t hci_op, const struct bt_ad *ad, size_t ad_len)
534 {
535 struct bt_hci_cp_le_set_adv_data *set_data;
536 struct net_buf *buf;
537 int err;
538
539 buf = bt_hci_cmd_create(hci_op, sizeof(*set_data));
540 if (!buf) {
541 return -ENOBUFS;
542 }
543
544 set_data = net_buf_add(buf, sizeof(*set_data));
545 (void)memset(set_data, 0, sizeof(*set_data));
546
547 err = set_data_add_complete(set_data->data, BT_GAP_ADV_MAX_ADV_DATA_LEN,
548 ad, ad_len, &set_data->len);
549 if (err) {
550 net_buf_unref(buf);
551 return err;
552 }
553
554 return bt_hci_cmd_send_sync(hci_op, buf, NULL);
555 }
556
hci_set_adv_ext_complete(struct bt_le_ext_adv * adv,uint16_t hci_op,size_t total_data_len,const struct bt_ad * ad,size_t ad_len)557 static int hci_set_adv_ext_complete(struct bt_le_ext_adv *adv, uint16_t hci_op,
558 size_t total_data_len, const struct bt_ad *ad, size_t ad_len)
559 {
560 struct bt_hci_cp_le_set_ext_adv_data *set_data;
561 struct net_buf *buf;
562 size_t cmd_size;
563 int err;
564
565 /* Provide the opportunity to truncate the complete name */
566 if (!atomic_test_bit(adv->flags, BT_ADV_EXT_ADV) &&
567 total_data_len > BT_GAP_ADV_MAX_ADV_DATA_LEN) {
568 total_data_len = BT_GAP_ADV_MAX_ADV_DATA_LEN;
569 }
570
571 cmd_size = sizeof(*set_data) + total_data_len;
572
573 buf = bt_hci_cmd_create(hci_op, cmd_size);
574 if (!buf) {
575 return -ENOBUFS;
576 }
577
578 set_data = net_buf_add(buf, cmd_size);
579 (void)memset(set_data, 0, cmd_size);
580
581 err = set_data_add_complete(set_data->data, total_data_len,
582 ad, ad_len, &set_data->len);
583 if (err) {
584 net_buf_unref(buf);
585 return err;
586 }
587
588 set_data->handle = adv->handle;
589 set_data->op = BT_HCI_LE_EXT_ADV_OP_COMPLETE_DATA;
590 set_data->frag_pref = BT_HCI_LE_EXT_ADV_FRAG_DISABLED;
591
592 return bt_hci_cmd_send_sync(hci_op, buf, NULL);
593 }
594
hci_set_adv_ext_fragmented(struct bt_le_ext_adv * adv,uint16_t hci_op,const struct bt_ad * ad,size_t ad_len)595 static int hci_set_adv_ext_fragmented(struct bt_le_ext_adv *adv, uint16_t hci_op,
596 const struct bt_ad *ad, size_t ad_len)
597 {
598 int err;
599 struct ad_stream stream;
600 bool is_first_iteration = true;
601
602 err = ad_stream_new(&stream, ad, ad_len);
603 if (err) {
604 return err;
605 }
606
607 while (!ad_stream_is_empty(&stream)) {
608 struct bt_hci_cp_le_set_ext_adv_data *set_data;
609 struct net_buf *buf;
610 const size_t data_len = MIN(BT_HCI_LE_EXT_ADV_FRAG_MAX_LEN, stream.remaining_size);
611 const size_t cmd_size = sizeof(*set_data) + data_len;
612
613 buf = bt_hci_cmd_create(hci_op, cmd_size);
614 if (!buf) {
615 return -ENOBUFS;
616 }
617
618 set_data = net_buf_add(buf, cmd_size);
619
620 set_data->handle = adv->handle;
621 set_data->frag_pref = BT_HCI_LE_EXT_ADV_FRAG_ENABLED;
622 set_data->len = ad_stream_read(&stream, set_data->data, data_len);
623
624 if (is_first_iteration && ad_stream_is_empty(&stream)) {
625 set_data->op = BT_HCI_LE_EXT_ADV_OP_COMPLETE_DATA;
626 } else if (is_first_iteration) {
627 set_data->op = BT_HCI_LE_EXT_ADV_OP_FIRST_FRAG;
628 } else if (ad_stream_is_empty(&stream)) {
629 set_data->op = BT_HCI_LE_EXT_ADV_OP_LAST_FRAG;
630 } else {
631 set_data->op = BT_HCI_LE_EXT_ADV_OP_INTERM_FRAG;
632 }
633
634 err = bt_hci_cmd_send_sync(hci_op, buf, NULL);
635 if (err) {
636 return err;
637 }
638
639 is_first_iteration = false;
640 }
641
642 return 0;
643 }
644
hci_set_ad_ext(struct bt_le_ext_adv * adv,uint16_t hci_op,const struct bt_ad * ad,size_t ad_len)645 static int hci_set_ad_ext(struct bt_le_ext_adv *adv, uint16_t hci_op,
646 const struct bt_ad *ad, size_t ad_len)
647 {
648 size_t total_len_bytes = 0;
649
650 for (size_t i = 0; i < ad_len; i++) {
651 for (size_t j = 0; j < ad[i].len; j++) {
652 total_len_bytes += ad[i].data[j].data_len + 2;
653 }
654 }
655
656 if ((total_len_bytes > BT_HCI_LE_EXT_ADV_FRAG_MAX_LEN) &&
657 atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
658 /* It is not allowed to set advertising data in multiple
659 * operations while the advertiser is running.
660 */
661 return -EAGAIN;
662 }
663
664 if (total_len_bytes > bt_dev.le.max_adv_data_len) {
665 LOG_WRN("adv or scan rsp data too large (%d > max %d)", total_len_bytes,
666 bt_dev.le.max_adv_data_len);
667 return -EDOM;
668 }
669
670 if (total_len_bytes <= BT_HCI_LE_EXT_ADV_FRAG_MAX_LEN) {
671 /* If possible, set all data at once.
672 * This allows us to update advertising data while advertising.
673 */
674 return hci_set_adv_ext_complete(adv, hci_op, total_len_bytes, ad, ad_len);
675 } else {
676 return hci_set_adv_ext_fragmented(adv, hci_op, ad, ad_len);
677 }
678
679 return 0;
680 }
681
set_ad(struct bt_le_ext_adv * adv,const struct bt_ad * ad,size_t ad_len)682 static int set_ad(struct bt_le_ext_adv *adv, const struct bt_ad *ad,
683 size_t ad_len)
684 {
685 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
686 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
687 return hci_set_ad_ext(adv, BT_HCI_OP_LE_SET_EXT_ADV_DATA,
688 ad, ad_len);
689 }
690
691 return hci_set_ad(BT_HCI_OP_LE_SET_ADV_DATA, ad, ad_len);
692 }
693
set_sd(struct bt_le_ext_adv * adv,const struct bt_ad * sd,size_t sd_len)694 static int set_sd(struct bt_le_ext_adv *adv, const struct bt_ad *sd,
695 size_t sd_len)
696 {
697 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
698 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
699 return hci_set_ad_ext(adv, BT_HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
700 sd, sd_len);
701 }
702
703 return hci_set_ad(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, sd, sd_len);
704 }
705
706 #if defined(CONFIG_BT_PER_ADV)
hci_set_per_adv_data(const struct bt_le_ext_adv * adv,const struct bt_data * ad,size_t ad_len)707 static int hci_set_per_adv_data(const struct bt_le_ext_adv *adv,
708 const struct bt_data *ad, size_t ad_len)
709 {
710 int err;
711 struct ad_stream stream;
712 struct bt_ad d = { .data = ad, .len = ad_len };
713 bool is_first_iteration = true;
714
715 err = ad_stream_new(&stream, &d, 1);
716 if (err) {
717 return err;
718 }
719
720 while (!ad_stream_is_empty(&stream)) {
721 struct bt_hci_cp_le_set_per_adv_data *set_data;
722 struct net_buf *buf;
723 const size_t data_len = MIN(BT_HCI_LE_PER_ADV_FRAG_MAX_LEN, stream.remaining_size);
724 const size_t cmd_size = sizeof(*set_data) + data_len;
725
726 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_DATA, cmd_size);
727 if (!buf) {
728 return -ENOBUFS;
729 }
730
731 set_data = net_buf_add(buf, cmd_size);
732 (void)memset(set_data, 0, cmd_size);
733
734 set_data->handle = adv->handle;
735 set_data->len = ad_stream_read(&stream, set_data->data, data_len);
736
737 if (is_first_iteration && ad_stream_is_empty(&stream)) {
738 set_data->op = BT_HCI_LE_EXT_ADV_OP_COMPLETE_DATA;
739 } else if (is_first_iteration) {
740 set_data->op = BT_HCI_LE_EXT_ADV_OP_FIRST_FRAG;
741 } else if (ad_stream_is_empty(&stream)) {
742 set_data->op = BT_HCI_LE_EXT_ADV_OP_LAST_FRAG;
743 } else {
744 set_data->op = BT_HCI_LE_EXT_ADV_OP_INTERM_FRAG;
745 }
746
747 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_DATA, buf, NULL);
748 if (err) {
749 return err;
750 }
751
752 is_first_iteration = false;
753 }
754
755 return 0;
756 }
757 #endif /* CONFIG_BT_PER_ADV */
758
ad_has_name(const struct bt_data * ad,size_t ad_len)759 static inline bool ad_has_name(const struct bt_data *ad, size_t ad_len)
760 {
761 size_t i;
762
763 for (i = 0; i < ad_len; i++) {
764 if (ad[i].type == BT_DATA_NAME_COMPLETE ||
765 ad[i].type == BT_DATA_NAME_SHORTENED) {
766 return true;
767 }
768 }
769
770 return false;
771 }
772
ad_is_limited(const struct bt_data * ad,size_t ad_len)773 static bool ad_is_limited(const struct bt_data *ad, size_t ad_len)
774 {
775 size_t i;
776
777 for (i = 0; i < ad_len; i++) {
778 if (ad[i].type == BT_DATA_FLAGS &&
779 ad[i].data_len == sizeof(uint8_t) &&
780 ad[i].data != NULL) {
781 if (ad[i].data[0] & BT_LE_AD_LIMITED) {
782 return true;
783 }
784 }
785 }
786
787 return false;
788 }
789
le_adv_update(struct bt_le_ext_adv * adv,const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len,bool ext_adv,bool scannable,enum adv_name_type name_type)790 static int le_adv_update(struct bt_le_ext_adv *adv,
791 const struct bt_data *ad, size_t ad_len,
792 const struct bt_data *sd, size_t sd_len,
793 bool ext_adv, bool scannable,
794 enum adv_name_type name_type)
795 {
796 struct bt_ad d[2] = {};
797 struct bt_data data;
798 size_t d_len;
799 int err;
800
801 if (name_type != ADV_NAME_TYPE_NONE) {
802 const char *name = bt_get_name();
803
804 if ((ad && ad_has_name(ad, ad_len)) ||
805 (sd && ad_has_name(sd, sd_len))) {
806 /* Cannot use name if name is already set */
807 return -EINVAL;
808 }
809
810 data = (struct bt_data)BT_DATA(
811 BT_DATA_NAME_COMPLETE,
812 name, strlen(name));
813 }
814
815 if (!(ext_adv && scannable)) {
816 d_len = 1;
817 d[0].data = ad;
818 d[0].len = ad_len;
819
820 if (name_type == ADV_NAME_TYPE_AD) {
821 d[1].data = &data;
822 d[1].len = 1;
823 d_len = 2;
824 }
825
826 err = set_ad(adv, d, d_len);
827 if (err) {
828 return err;
829 }
830 }
831
832 if (scannable) {
833 d_len = 1;
834 d[0].data = sd;
835 d[0].len = sd_len;
836
837 if (name_type == ADV_NAME_TYPE_SD) {
838 d[1].data = &data;
839 d[1].len = 1;
840 d_len = 2;
841 }
842
843 err = set_sd(adv, d, d_len);
844 if (err) {
845 return err;
846 }
847 }
848
849 atomic_set_bit(adv->flags, BT_ADV_DATA_SET);
850 return 0;
851 }
852
bt_le_adv_update_data(const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len)853 int bt_le_adv_update_data(const struct bt_data *ad, size_t ad_len,
854 const struct bt_data *sd, size_t sd_len)
855 {
856 struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
857 bool scannable;
858
859 if (!adv) {
860 return -EINVAL;
861 }
862
863 if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
864 return -EAGAIN;
865 }
866
867 scannable = atomic_test_bit(adv->flags, BT_ADV_SCANNABLE);
868
869 return le_adv_update(adv, ad, ad_len, sd, sd_len, false, scannable,
870 get_adv_name_type(adv));
871 }
872
get_filter_policy(uint32_t options)873 static uint8_t get_filter_policy(uint32_t options)
874 {
875 if (!IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST)) {
876 return BT_LE_ADV_FP_NO_FILTER;
877 } else if ((options & BT_LE_ADV_OPT_FILTER_SCAN_REQ) &&
878 (options & BT_LE_ADV_OPT_FILTER_CONN)) {
879 return BT_LE_ADV_FP_FILTER_BOTH;
880 } else if (options & BT_LE_ADV_OPT_FILTER_SCAN_REQ) {
881 return BT_LE_ADV_FP_FILTER_SCAN_REQ;
882 } else if (options & BT_LE_ADV_OPT_FILTER_CONN) {
883 return BT_LE_ADV_FP_FILTER_CONN_IND;
884 } else {
885 return BT_LE_ADV_FP_NO_FILTER;
886 }
887 }
888
get_adv_channel_map(uint32_t options)889 static uint8_t get_adv_channel_map(uint32_t options)
890 {
891 uint8_t channel_map = 0x07;
892
893 if (options & BT_LE_ADV_OPT_DISABLE_CHAN_37) {
894 channel_map &= ~0x01;
895 }
896
897 if (options & BT_LE_ADV_OPT_DISABLE_CHAN_38) {
898 channel_map &= ~0x02;
899 }
900
901 if (options & BT_LE_ADV_OPT_DISABLE_CHAN_39) {
902 channel_map &= ~0x04;
903 }
904
905 return channel_map;
906 }
907
adv_is_directed(const struct bt_le_ext_adv * adv)908 static inline bool adv_is_directed(const struct bt_le_ext_adv *adv)
909 {
910 /* The advertiser is assumed to be directed when the peer address has
911 * been set.
912 */
913 return !bt_addr_le_eq(&adv->target_addr, BT_ADDR_LE_ANY);
914 }
915
le_adv_start_add_conn(const struct bt_le_ext_adv * adv,struct bt_conn ** out_conn)916 static int le_adv_start_add_conn(const struct bt_le_ext_adv *adv,
917 struct bt_conn **out_conn)
918 {
919 struct bt_conn *conn;
920
921 bt_dev.adv_conn_id = adv->id;
922
923 if (!adv_is_directed(adv)) {
924 /* Undirected advertising */
925 conn = bt_conn_add_le(adv->id, BT_ADDR_LE_NONE);
926 if (!conn) {
927 return -ENOMEM;
928 }
929
930 bt_conn_set_state(conn, BT_CONN_ADV_CONNECTABLE);
931 *out_conn = conn;
932 return 0;
933 }
934
935 if (bt_conn_exists_le(adv->id, &adv->target_addr)) {
936 return -EINVAL;
937 }
938
939 conn = bt_conn_add_le(adv->id, &adv->target_addr);
940 if (!conn) {
941 return -ENOMEM;
942 }
943
944 bt_conn_set_state(conn, BT_CONN_ADV_DIR_CONNECTABLE);
945 *out_conn = conn;
946 return 0;
947 }
948
le_adv_stop_free_conn(const struct bt_le_ext_adv * adv,uint8_t status)949 static void le_adv_stop_free_conn(const struct bt_le_ext_adv *adv, uint8_t status)
950 {
951 struct bt_conn *conn;
952
953 if (!adv_is_directed(adv)) {
954 conn = bt_conn_lookup_state_le(adv->id, BT_ADDR_LE_NONE,
955 BT_CONN_ADV_CONNECTABLE);
956 } else {
957 conn = bt_conn_lookup_state_le(adv->id, &adv->target_addr,
958 BT_CONN_ADV_DIR_CONNECTABLE);
959 }
960
961 if (conn) {
962 conn->err = status;
963 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
964 bt_conn_unref(conn);
965 }
966 }
967
bt_le_adv_start_legacy(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param,const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len)968 int bt_le_adv_start_legacy(struct bt_le_ext_adv *adv,
969 const struct bt_le_adv_param *param,
970 const struct bt_data *ad, size_t ad_len,
971 const struct bt_data *sd, size_t sd_len)
972 {
973 struct bt_hci_cp_le_set_adv_param set_param;
974 struct bt_conn *conn = NULL;
975 struct net_buf *buf;
976 bool dir_adv = (param->peer != NULL), scannable = false;
977 enum adv_name_type name_type;
978
979 int err;
980
981 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
982 return -EAGAIN;
983 }
984
985 if (!valid_adv_param(param)) {
986 return -EINVAL;
987 }
988
989 if (!bt_id_adv_random_addr_check(param)) {
990 return -EINVAL;
991 }
992
993 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
994 return -EALREADY;
995 }
996
997 (void)memset(&set_param, 0, sizeof(set_param));
998
999 set_param.min_interval = sys_cpu_to_le16(param->interval_min);
1000 set_param.max_interval = sys_cpu_to_le16(param->interval_max);
1001 set_param.channel_map = get_adv_channel_map(param->options);
1002 set_param.filter_policy = get_filter_policy(param->options);
1003
1004 atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
1005
1006 adv->id = param->id;
1007 bt_dev.adv_conn_id = adv->id;
1008
1009 err = bt_id_set_adv_own_addr(adv, param->options, dir_adv,
1010 &set_param.own_addr_type);
1011 if (err) {
1012 return err;
1013 }
1014
1015 if (dir_adv) {
1016 bt_addr_le_copy(&adv->target_addr, param->peer);
1017 } else {
1018 bt_addr_le_copy(&adv->target_addr, BT_ADDR_LE_ANY);
1019 }
1020
1021 name_type = get_adv_name_type_param(param);
1022
1023 if (param->options & BT_LE_ADV_OPT_CONNECTABLE) {
1024 if (dir_adv) {
1025 if (param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY) {
1026 set_param.type = BT_HCI_ADV_DIRECT_IND_LOW_DUTY;
1027 } else {
1028 set_param.type = BT_HCI_ADV_DIRECT_IND;
1029 }
1030
1031 bt_addr_le_copy(&set_param.direct_addr, param->peer);
1032 } else {
1033 scannable = true;
1034 set_param.type = BT_HCI_ADV_IND;
1035 }
1036 } else if ((param->options & BT_LE_ADV_OPT_SCANNABLE) || sd ||
1037 (name_type == ADV_NAME_TYPE_SD)) {
1038 scannable = true;
1039 set_param.type = BT_HCI_ADV_SCAN_IND;
1040 } else {
1041 set_param.type = BT_HCI_ADV_NONCONN_IND;
1042 }
1043
1044 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_PARAM, sizeof(set_param));
1045 if (!buf) {
1046 return -ENOBUFS;
1047 }
1048
1049 net_buf_add_mem(buf, &set_param, sizeof(set_param));
1050
1051 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_PARAM, buf, NULL);
1052 if (err) {
1053 return err;
1054 }
1055
1056 if (!dir_adv) {
1057 err = le_adv_update(adv, ad, ad_len, sd, sd_len, false,
1058 scannable, name_type);
1059 if (err) {
1060 return err;
1061 }
1062 }
1063
1064 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1065 (param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
1066 err = le_adv_start_add_conn(adv, &conn);
1067 if (err) {
1068 if (err == -ENOMEM && !dir_adv &&
1069 !(param->options & BT_LE_ADV_OPT_ONE_TIME)) {
1070 goto set_adv_state;
1071 }
1072
1073 return err;
1074 }
1075 }
1076
1077 err = bt_le_adv_set_enable(adv, true);
1078 if (err) {
1079 LOG_ERR("Failed to start advertiser");
1080 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1081 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1082 bt_conn_unref(conn);
1083 }
1084
1085 return err;
1086 }
1087
1088 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1089 /* If undirected connectable advertiser we have created a
1090 * connection object that we don't yet give to the application.
1091 * Since we don't give the application a reference to manage in
1092 * this case, we need to release this reference here
1093 */
1094 bt_conn_unref(conn);
1095 }
1096
1097 set_adv_state:
1098 atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, !dir_adv &&
1099 !(param->options & BT_LE_ADV_OPT_ONE_TIME));
1100
1101 atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_AD,
1102 name_type == ADV_NAME_TYPE_AD);
1103
1104 atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_SD,
1105 name_type == ADV_NAME_TYPE_SD);
1106
1107 atomic_set_bit_to(adv->flags, BT_ADV_CONNECTABLE,
1108 param->options & BT_LE_ADV_OPT_CONNECTABLE);
1109
1110 atomic_set_bit_to(adv->flags, BT_ADV_SCANNABLE, scannable);
1111
1112 atomic_set_bit_to(adv->flags, BT_ADV_USE_IDENTITY,
1113 param->options & BT_LE_ADV_OPT_USE_IDENTITY);
1114
1115 return 0;
1116 }
1117
le_ext_adv_param_set(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param,bool has_scan_data)1118 static int le_ext_adv_param_set(struct bt_le_ext_adv *adv,
1119 const struct bt_le_adv_param *param,
1120 bool has_scan_data)
1121 {
1122 struct bt_hci_cp_le_set_ext_adv_param *cp;
1123 bool dir_adv = param->peer != NULL, scannable;
1124 struct net_buf *buf, *rsp;
1125 int err;
1126 enum adv_name_type name_type;
1127 uint16_t props = 0;
1128
1129 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_ADV_PARAM, sizeof(*cp));
1130 if (!buf) {
1131 return -ENOBUFS;
1132 }
1133
1134 cp = net_buf_add(buf, sizeof(*cp));
1135 (void)memset(cp, 0, sizeof(*cp));
1136
1137 adv->options = param->options;
1138
1139 err = bt_id_set_adv_own_addr(adv, param->options, dir_adv,
1140 &cp->own_addr_type);
1141 if (err) {
1142 return err;
1143 }
1144
1145 if (dir_adv) {
1146 bt_addr_le_copy(&adv->target_addr, param->peer);
1147 } else {
1148 bt_addr_le_copy(&adv->target_addr, BT_ADDR_LE_ANY);
1149 }
1150
1151 name_type = get_adv_name_type_param(param);
1152
1153 cp->handle = adv->handle;
1154 sys_put_le24(param->interval_min, cp->prim_min_interval);
1155 sys_put_le24(param->interval_max, cp->prim_max_interval);
1156 cp->prim_channel_map = get_adv_channel_map(param->options);
1157 cp->filter_policy = get_filter_policy(param->options);
1158 cp->tx_power = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
1159
1160 cp->prim_adv_phy = BT_HCI_LE_PHY_1M;
1161 if ((param->options & BT_LE_ADV_OPT_EXT_ADV) &&
1162 !(param->options & BT_LE_ADV_OPT_NO_2M)) {
1163 cp->sec_adv_phy = BT_HCI_LE_PHY_2M;
1164 } else {
1165 cp->sec_adv_phy = BT_HCI_LE_PHY_1M;
1166 }
1167
1168 if (param->options & BT_LE_ADV_OPT_CODED) {
1169 cp->prim_adv_phy = BT_HCI_LE_PHY_CODED;
1170 cp->sec_adv_phy = BT_HCI_LE_PHY_CODED;
1171 }
1172
1173 if (!(param->options & BT_LE_ADV_OPT_EXT_ADV)) {
1174 props |= BT_HCI_LE_ADV_PROP_LEGACY;
1175 }
1176
1177 if (param->options & BT_LE_ADV_OPT_USE_TX_POWER) {
1178 props |= BT_HCI_LE_ADV_PROP_TX_POWER;
1179 }
1180
1181 if (param->options & BT_LE_ADV_OPT_ANONYMOUS) {
1182 props |= BT_HCI_LE_ADV_PROP_ANON;
1183 }
1184
1185 if (param->options & BT_LE_ADV_OPT_NOTIFY_SCAN_REQ) {
1186 cp->scan_req_notify_enable = BT_HCI_LE_ADV_SCAN_REQ_ENABLE;
1187 }
1188
1189 if (param->options & BT_LE_ADV_OPT_CONNECTABLE) {
1190 props |= BT_HCI_LE_ADV_PROP_CONN;
1191 if (!dir_adv && !(param->options & BT_LE_ADV_OPT_EXT_ADV)) {
1192 /* When using non-extended adv packets then undirected
1193 * advertising has to be scannable as well.
1194 * We didn't require this option to be set before, so
1195 * it is implicitly set instead in this case.
1196 */
1197 props |= BT_HCI_LE_ADV_PROP_SCAN;
1198 }
1199 }
1200
1201 if ((param->options & BT_LE_ADV_OPT_SCANNABLE) || has_scan_data ||
1202 (name_type == ADV_NAME_TYPE_SD)) {
1203 props |= BT_HCI_LE_ADV_PROP_SCAN;
1204 }
1205
1206 scannable = !!(props & BT_HCI_LE_ADV_PROP_SCAN);
1207
1208 if (dir_adv) {
1209 props |= BT_HCI_LE_ADV_PROP_DIRECT;
1210 if (!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
1211 props |= BT_HCI_LE_ADV_PROP_HI_DC_CONN;
1212 }
1213
1214 bt_addr_le_copy(&cp->peer_addr, param->peer);
1215 }
1216
1217 cp->sid = param->sid;
1218
1219 cp->props = sys_cpu_to_le16(props);
1220 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_ADV_PARAM, buf, &rsp);
1221 if (err) {
1222 return err;
1223 }
1224
1225 #if defined(CONFIG_BT_EXT_ADV)
1226 struct bt_hci_rp_le_set_ext_adv_param *rp = (void *)rsp->data;
1227
1228 adv->tx_power = rp->tx_power;
1229 #endif /* defined(CONFIG_BT_EXT_ADV) */
1230
1231 net_buf_unref(rsp);
1232
1233 atomic_set_bit(adv->flags, BT_ADV_PARAMS_SET);
1234
1235 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_RANDOM_ADDR_PENDING)) {
1236 err = bt_id_set_adv_random_addr(adv, &adv->random_addr.a);
1237 if (err) {
1238 return err;
1239 }
1240 }
1241
1242 /* Flag only used by bt_le_adv_start API. */
1243 atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, false);
1244
1245 atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_AD,
1246 name_type == ADV_NAME_TYPE_AD);
1247
1248 atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_SD,
1249 name_type == ADV_NAME_TYPE_SD);
1250
1251 atomic_set_bit_to(adv->flags, BT_ADV_CONNECTABLE,
1252 param->options & BT_LE_ADV_OPT_CONNECTABLE);
1253
1254 atomic_set_bit_to(adv->flags, BT_ADV_SCANNABLE, scannable);
1255
1256 atomic_set_bit_to(adv->flags, BT_ADV_USE_IDENTITY,
1257 param->options & BT_LE_ADV_OPT_USE_IDENTITY);
1258
1259 atomic_set_bit_to(adv->flags, BT_ADV_EXT_ADV,
1260 param->options & BT_LE_ADV_OPT_EXT_ADV);
1261
1262 return 0;
1263 }
1264
bt_le_adv_start_ext(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param,const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len)1265 int bt_le_adv_start_ext(struct bt_le_ext_adv *adv,
1266 const struct bt_le_adv_param *param,
1267 const struct bt_data *ad, size_t ad_len,
1268 const struct bt_data *sd, size_t sd_len)
1269 {
1270 struct bt_le_ext_adv_start_param start_param = {
1271 .timeout = 0,
1272 .num_events = 0,
1273 };
1274 bool dir_adv = (param->peer != NULL);
1275 struct bt_conn *conn = NULL;
1276 int err;
1277
1278 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1279 return -EAGAIN;
1280 }
1281
1282 if (!valid_adv_param(param)) {
1283 return -EINVAL;
1284 }
1285
1286 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1287 return -EALREADY;
1288 }
1289
1290 adv->id = param->id;
1291 err = le_ext_adv_param_set(adv, param, sd != NULL);
1292 if (err) {
1293 return err;
1294 }
1295
1296 if (!dir_adv) {
1297 if (IS_ENABLED(CONFIG_BT_EXT_ADV)) {
1298 err = bt_le_ext_adv_set_data(adv, ad, ad_len, sd, sd_len);
1299 if (err) {
1300 return err;
1301 }
1302 }
1303 } else {
1304 if (!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
1305 start_param.timeout =
1306 BT_GAP_ADV_HIGH_DUTY_CYCLE_MAX_TIMEOUT;
1307 atomic_set_bit(adv->flags, BT_ADV_LIMITED);
1308 }
1309 }
1310
1311 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1312 (param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
1313 err = le_adv_start_add_conn(adv, &conn);
1314 if (err) {
1315 if (err == -ENOMEM && !dir_adv &&
1316 !(param->options & BT_LE_ADV_OPT_ONE_TIME)) {
1317 goto set_adv_state;
1318 }
1319
1320 return err;
1321 }
1322 }
1323
1324 err = bt_le_adv_set_enable_ext(adv, true, &start_param);
1325 if (err) {
1326 LOG_ERR("Failed to start advertiser");
1327 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1328 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1329 bt_conn_unref(conn);
1330 }
1331
1332 return err;
1333 }
1334
1335 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1336 /* If undirected connectable advertiser we have created a
1337 * connection object that we don't yet give to the application.
1338 * Since we don't give the application a reference to manage in
1339 * this case, we need to release this reference here
1340 */
1341 bt_conn_unref(conn);
1342 }
1343
1344 set_adv_state:
1345 /* Flag always set to false by le_ext_adv_param_set */
1346 atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, !dir_adv &&
1347 !(param->options & BT_LE_ADV_OPT_ONE_TIME));
1348
1349 return 0;
1350 }
1351
1352 static void adv_timeout(struct k_work *work);
1353
bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv * adv)1354 int bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv *adv)
1355 {
1356 return k_work_cancel_delayable(&adv->lim_adv_timeout_work);
1357 }
1358
bt_le_adv_start(const struct bt_le_adv_param * param,const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len)1359 int bt_le_adv_start(const struct bt_le_adv_param *param,
1360 const struct bt_data *ad, size_t ad_len,
1361 const struct bt_data *sd, size_t sd_len)
1362 {
1363 struct bt_le_ext_adv *adv = adv_get_legacy();
1364 int err;
1365
1366 if (!adv) {
1367 return -ENOMEM;
1368 }
1369
1370 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1371 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1372 err = bt_le_adv_start_ext(adv, param, ad, ad_len, sd, sd_len);
1373 } else {
1374 err = bt_le_adv_start_legacy(adv, param, ad, ad_len, sd, sd_len);
1375 }
1376
1377 if (err) {
1378 bt_le_adv_delete_legacy();
1379 }
1380
1381 if (ad_is_limited(ad, ad_len)) {
1382 k_work_init_delayable(&adv->lim_adv_timeout_work, adv_timeout);
1383 k_work_reschedule(&adv->lim_adv_timeout_work,
1384 K_SECONDS(CONFIG_BT_LIM_ADV_TIMEOUT));
1385 }
1386
1387 return err;
1388 }
1389
bt_le_adv_stop(void)1390 int bt_le_adv_stop(void)
1391 {
1392 struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
1393 int err;
1394
1395 if (!adv) {
1396 LOG_ERR("No valid legacy adv");
1397 return 0;
1398 }
1399
1400 (void)bt_le_lim_adv_cancel_timeout(adv);
1401
1402 /* Make sure advertising is not re-enabled later even if it's not
1403 * currently enabled (i.e. BT_DEV_ADVERTISING is not set).
1404 */
1405 atomic_clear_bit(adv->flags, BT_ADV_PERSIST);
1406
1407 if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1408 /* Legacy advertiser exists, but is not currently advertising.
1409 * This happens when keep advertising behavior is active but
1410 * no conn object is available to do connectable advertising.
1411 */
1412 bt_le_adv_delete_legacy();
1413 return 0;
1414 }
1415
1416 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1417 atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1418 le_adv_stop_free_conn(adv, 0);
1419 }
1420
1421 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1422 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1423 err = bt_le_adv_set_enable_ext(adv, false, NULL);
1424 if (err) {
1425 return err;
1426 }
1427 } else {
1428 err = bt_le_adv_set_enable_legacy(adv, false);
1429 if (err) {
1430 return err;
1431 }
1432 }
1433
1434 bt_le_adv_delete_legacy();
1435
1436 #if defined(CONFIG_BT_OBSERVER)
1437 if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1438 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) &&
1439 !IS_ENABLED(CONFIG_BT_PRIVACY) &&
1440 !IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY)) {
1441 /* If scan is ongoing set back NRPA */
1442 if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
1443 bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
1444 bt_id_set_private_addr(BT_ID_DEFAULT);
1445 bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
1446 }
1447 }
1448 #endif /* defined(CONFIG_BT_OBSERVER) */
1449
1450 return 0;
1451 }
1452
1453 #if defined(CONFIG_BT_PERIPHERAL)
adv_get_options(const struct bt_le_ext_adv * adv)1454 static uint32_t adv_get_options(const struct bt_le_ext_adv *adv)
1455 {
1456 uint32_t options = 0;
1457
1458 if (!atomic_test_bit(adv->flags, BT_ADV_PERSIST)) {
1459 options |= BT_LE_ADV_OPT_ONE_TIME;
1460 }
1461
1462 if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1463 options |= BT_LE_ADV_OPT_CONNECTABLE;
1464 }
1465
1466 if (atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1467 options |= BT_LE_ADV_OPT_USE_IDENTITY;
1468 }
1469
1470 return options;
1471 }
1472
bt_le_adv_resume(void)1473 void bt_le_adv_resume(void)
1474 {
1475 struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
1476 struct bt_conn *conn;
1477 bool persist_paused = false;
1478 int err;
1479
1480 if (!adv) {
1481 LOG_DBG("No valid legacy adv");
1482 return;
1483 }
1484
1485 if (!(atomic_test_bit(adv->flags, BT_ADV_PERSIST) &&
1486 !atomic_test_bit(adv->flags, BT_ADV_ENABLED))) {
1487 return;
1488 }
1489
1490 if (!atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1491 return;
1492 }
1493
1494 err = le_adv_start_add_conn(adv, &conn);
1495 if (err) {
1496 LOG_DBG("Host cannot resume connectable advertising (%d)", err);
1497 return;
1498 }
1499
1500 LOG_DBG("Resuming connectable advertising");
1501
1502 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1503 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1504 bt_id_set_adv_private_addr(adv);
1505 } else {
1506 uint8_t own_addr_type;
1507 bool dir_adv = adv_is_directed(adv);
1508 uint32_t options = adv_get_options(adv);
1509
1510 /* Always set the address. Don't assume it has not changed. */
1511 err = bt_id_set_adv_own_addr(adv, options, dir_adv, &own_addr_type);
1512 if (err) {
1513 LOG_ERR("Controller cannot resume connectable advertising (%d)", err);
1514 return;
1515 }
1516 }
1517
1518 err = bt_le_adv_set_enable(adv, true);
1519 if (err) {
1520 LOG_DBG("Controller cannot resume connectable advertising (%d)", err);
1521 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1522
1523 /* Temporarily clear persist flag to avoid recursion in
1524 * bt_conn_unref if the flag is still set.
1525 */
1526 persist_paused = atomic_test_and_clear_bit(adv->flags,
1527 BT_ADV_PERSIST);
1528 }
1529
1530 /* Since we don't give the application a reference to manage in
1531 * this case, we need to release this reference here.
1532 */
1533 bt_conn_unref(conn);
1534 if (persist_paused) {
1535 atomic_set_bit(adv->flags, BT_ADV_PERSIST);
1536 }
1537 }
1538 #endif /* defined(CONFIG_BT_PERIPHERAL) */
1539
1540 #if defined(CONFIG_BT_EXT_ADV)
bt_le_ext_adv_get_info(const struct bt_le_ext_adv * adv,struct bt_le_ext_adv_info * info)1541 int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
1542 struct bt_le_ext_adv_info *info)
1543 {
1544 info->id = adv->id;
1545 info->tx_power = adv->tx_power;
1546 info->addr = &adv->random_addr;
1547
1548 return 0;
1549 }
1550
bt_le_ext_adv_create(const struct bt_le_adv_param * param,const struct bt_le_ext_adv_cb * cb,struct bt_le_ext_adv ** out_adv)1551 int bt_le_ext_adv_create(const struct bt_le_adv_param *param,
1552 const struct bt_le_ext_adv_cb *cb,
1553 struct bt_le_ext_adv **out_adv)
1554 {
1555 struct bt_le_ext_adv *adv;
1556 int err;
1557
1558 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1559 return -EAGAIN;
1560 }
1561
1562 if (!valid_adv_ext_param(param)) {
1563 return -EINVAL;
1564 }
1565
1566 adv = adv_new();
1567 if (!adv) {
1568 return -ENOMEM;
1569 }
1570
1571 adv->id = param->id;
1572 adv->cb = cb;
1573
1574 err = le_ext_adv_param_set(adv, param, false);
1575 if (err) {
1576 adv_delete(adv);
1577 return err;
1578 }
1579
1580 *out_adv = adv;
1581 return 0;
1582 }
1583
bt_le_ext_adv_update_param(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param)1584 int bt_le_ext_adv_update_param(struct bt_le_ext_adv *adv,
1585 const struct bt_le_adv_param *param)
1586 {
1587 if (!valid_adv_ext_param(param)) {
1588 return -EINVAL;
1589 }
1590
1591 if (IS_ENABLED(CONFIG_BT_PER_ADV) &&
1592 atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1593 /* If params for per adv has been set, do not allow setting
1594 * connectable, scanable or use legacy adv
1595 */
1596 if (param->options & BT_LE_ADV_OPT_CONNECTABLE ||
1597 param->options & BT_LE_ADV_OPT_SCANNABLE ||
1598 !(param->options & BT_LE_ADV_OPT_EXT_ADV) ||
1599 param->options & BT_LE_ADV_OPT_ANONYMOUS) {
1600 return -EINVAL;
1601 }
1602 }
1603
1604 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1605 return -EINVAL;
1606 }
1607
1608 if (param->id != adv->id) {
1609 atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
1610 }
1611
1612 return le_ext_adv_param_set(adv, param, false);
1613 }
1614
bt_le_ext_adv_start(struct bt_le_ext_adv * adv,const struct bt_le_ext_adv_start_param * param)1615 int bt_le_ext_adv_start(struct bt_le_ext_adv *adv,
1616 const struct bt_le_ext_adv_start_param *param)
1617 {
1618 struct bt_conn *conn = NULL;
1619 int err;
1620
1621 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1622 return -EALREADY;
1623 }
1624
1625 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1626 atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1627 err = le_adv_start_add_conn(adv, &conn);
1628 if (err) {
1629 return err;
1630 }
1631 }
1632
1633 atomic_set_bit_to(adv->flags, BT_ADV_LIMITED, param &&
1634 (param->timeout > 0 || param->num_events > 0));
1635
1636 if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1637 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1638 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1639 bt_id_set_adv_private_addr(adv);
1640 }
1641 } else {
1642 if (!atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1643 bt_id_set_adv_private_addr(adv);
1644 }
1645 }
1646
1647 if (get_adv_name_type(adv) != ADV_NAME_TYPE_NONE &&
1648 !atomic_test_bit(adv->flags, BT_ADV_DATA_SET)) {
1649 /* Set the advertiser name */
1650 bt_le_ext_adv_set_data(adv, NULL, 0, NULL, 0);
1651 }
1652
1653 err = bt_le_adv_set_enable_ext(adv, true, param);
1654 if (err) {
1655 LOG_ERR("Failed to start advertiser");
1656 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1657 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1658 bt_conn_unref(conn);
1659 }
1660
1661 return err;
1662 }
1663
1664 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1665 /* If undirected connectable advertiser we have created a
1666 * connection object that we don't yet give to the application.
1667 * Since we don't give the application a reference to manage in
1668 * this case, we need to release this reference here
1669 */
1670 bt_conn_unref(conn);
1671 }
1672
1673 return 0;
1674 }
1675
bt_le_ext_adv_stop(struct bt_le_ext_adv * adv)1676 int bt_le_ext_adv_stop(struct bt_le_ext_adv *adv)
1677 {
1678 (void)bt_le_lim_adv_cancel_timeout(adv);
1679
1680 atomic_clear_bit(adv->flags, BT_ADV_PERSIST);
1681
1682 if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1683 return 0;
1684 }
1685
1686 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
1687 bt_id_adv_limited_stopped(adv);
1688
1689 #if defined(CONFIG_BT_SMP)
1690 bt_id_pending_keys_update();
1691 #endif
1692 }
1693
1694 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1695 atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1696 le_adv_stop_free_conn(adv, 0);
1697 }
1698
1699 return bt_le_adv_set_enable_ext(adv, false, NULL);
1700 }
1701
bt_le_ext_adv_set_data(struct bt_le_ext_adv * adv,const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len)1702 int bt_le_ext_adv_set_data(struct bt_le_ext_adv *adv,
1703 const struct bt_data *ad, size_t ad_len,
1704 const struct bt_data *sd, size_t sd_len)
1705 {
1706 bool ext_adv, scannable;
1707
1708 ext_adv = atomic_test_bit(adv->flags, BT_ADV_EXT_ADV);
1709 scannable = atomic_test_bit(adv->flags, BT_ADV_SCANNABLE);
1710
1711 if (ext_adv) {
1712 if ((scannable && ad_len) ||
1713 (!scannable && sd_len)) {
1714 return -ENOTSUP;
1715 }
1716 }
1717
1718 return le_adv_update(adv, ad, ad_len, sd, sd_len, ext_adv, scannable,
1719 get_adv_name_type(adv));
1720 }
1721
bt_le_ext_adv_delete(struct bt_le_ext_adv * adv)1722 int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv)
1723 {
1724 struct bt_hci_cp_le_remove_adv_set *cp;
1725 struct net_buf *buf;
1726 int err;
1727
1728 if (!BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1729 return -ENOTSUP;
1730 }
1731
1732 /* Advertising set should be stopped first */
1733 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1734 return -EINVAL;
1735 }
1736
1737 buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_ADV_SET, sizeof(*cp));
1738 if (!buf) {
1739 LOG_WRN("No HCI buffers");
1740 return -ENOBUFS;
1741 }
1742
1743 cp = net_buf_add(buf, sizeof(*cp));
1744 cp->handle = adv->handle;
1745
1746 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_ADV_SET, buf, NULL);
1747 if (err) {
1748 return err;
1749 }
1750
1751 adv_delete(adv);
1752
1753 return 0;
1754 }
1755 #endif /* defined(CONFIG_BT_EXT_ADV) */
1756
1757
adv_timeout(struct k_work * work)1758 static void adv_timeout(struct k_work *work)
1759 {
1760 int err = 0;
1761 struct k_work_delayable *dwork;
1762 struct bt_le_ext_adv *adv;
1763
1764 dwork = k_work_delayable_from_work(work);
1765 adv = CONTAINER_OF(dwork, struct bt_le_ext_adv, lim_adv_timeout_work);
1766
1767 #if defined(CONFIG_BT_EXT_ADV)
1768 if (adv == bt_dev.adv) {
1769 err = bt_le_adv_stop();
1770 } else {
1771 err = bt_le_ext_adv_stop(adv);
1772 }
1773 #else
1774 err = bt_le_adv_stop();
1775 #endif
1776 if (err) {
1777 LOG_WRN("Failed to stop advertising: %d", err);
1778 }
1779 }
1780
1781 #if defined(CONFIG_BT_PER_ADV)
bt_le_per_adv_set_param(struct bt_le_ext_adv * adv,const struct bt_le_per_adv_param * param)1782 int bt_le_per_adv_set_param(struct bt_le_ext_adv *adv,
1783 const struct bt_le_per_adv_param *param)
1784 {
1785 #if defined(CONFIG_BT_PER_ADV_RSP)
1786 /* The v2 struct can be used even if we end up sending a v1 command
1787 * because they have the same layout for the common fields.
1788 * V2 simply adds fields at the end of the v1 command.
1789 */
1790 struct bt_hci_cp_le_set_per_adv_param_v2 *cp;
1791 #else
1792 struct bt_hci_cp_le_set_per_adv_param *cp;
1793 #endif /* CONFIG_BT_PER_ADV_RSP */
1794
1795 uint16_t opcode;
1796 uint16_t size;
1797 struct net_buf *buf;
1798 int err;
1799 uint16_t props = 0;
1800
1801 if (IS_ENABLED(CONFIG_BT_PER_ADV_RSP) && BT_FEAT_LE_PAWR_ADVERTISER(bt_dev.le.features)) {
1802 opcode = BT_HCI_OP_LE_SET_PER_ADV_PARAM_V2;
1803 size = sizeof(struct bt_hci_cp_le_set_per_adv_param_v2);
1804 } else if (BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1805 opcode = BT_HCI_OP_LE_SET_PER_ADV_PARAM;
1806 size = sizeof(struct bt_hci_cp_le_set_per_adv_param);
1807 } else {
1808 return -ENOTSUP;
1809 }
1810
1811 if (atomic_test_bit(adv->flags, BT_ADV_SCANNABLE)) {
1812 return -EINVAL;
1813 } else if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1814 return -EINVAL;
1815 } else if (!atomic_test_bit(adv->flags, BT_ADV_EXT_ADV)) {
1816 return -EINVAL;
1817 }
1818
1819 if (param->interval_min < BT_GAP_PER_ADV_MIN_INTERVAL ||
1820 param->interval_max > BT_GAP_PER_ADV_MAX_INTERVAL ||
1821 param->interval_min > param->interval_max) {
1822 return -EINVAL;
1823 }
1824
1825 if (!BT_FEAT_LE_PER_ADV_ADI_SUPP(bt_dev.le.features) &&
1826 (param->options & BT_LE_PER_ADV_OPT_INCLUDE_ADI)) {
1827 return -ENOTSUP;
1828 }
1829
1830 buf = bt_hci_cmd_create(opcode, size);
1831 if (!buf) {
1832 return -ENOBUFS;
1833 }
1834
1835 cp = net_buf_add(buf, size);
1836 (void)memset(cp, 0, size);
1837
1838 cp->handle = adv->handle;
1839 cp->min_interval = sys_cpu_to_le16(param->interval_min);
1840 cp->max_interval = sys_cpu_to_le16(param->interval_max);
1841
1842 if (param->options & BT_LE_PER_ADV_OPT_USE_TX_POWER) {
1843 props |= BT_HCI_LE_ADV_PROP_TX_POWER;
1844 }
1845
1846 cp->props = sys_cpu_to_le16(props);
1847
1848 #if defined(CONFIG_BT_PER_ADV_RSP)
1849 if (opcode == BT_HCI_OP_LE_SET_PER_ADV_PARAM_V2) {
1850 cp->num_subevents = param->num_subevents;
1851 cp->subevent_interval = param->subevent_interval;
1852 cp->response_slot_delay = param->response_slot_delay;
1853 cp->response_slot_spacing = param->response_slot_spacing;
1854 cp->num_response_slots = param->num_response_slots;
1855 }
1856 #endif /* CONFIG_BT_PER_ADV_RSP */
1857
1858 err = bt_hci_cmd_send_sync(opcode, buf, NULL);
1859 if (err) {
1860 return err;
1861 }
1862
1863 if (param->options & BT_LE_PER_ADV_OPT_INCLUDE_ADI) {
1864 atomic_set_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI);
1865 } else {
1866 atomic_clear_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI);
1867 }
1868
1869 atomic_set_bit(adv->flags, BT_PER_ADV_PARAMS_SET);
1870
1871 return 0;
1872 }
1873
bt_le_per_adv_set_data(const struct bt_le_ext_adv * adv,const struct bt_data * ad,size_t ad_len)1874 int bt_le_per_adv_set_data(const struct bt_le_ext_adv *adv,
1875 const struct bt_data *ad, size_t ad_len)
1876 {
1877 size_t total_len_bytes = 0;
1878
1879 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1880 return -ENOTSUP;
1881 }
1882
1883 if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1884 return -EINVAL;
1885 }
1886
1887 if (ad_len != 0 && ad == NULL) {
1888 return -EINVAL;
1889 }
1890
1891 for (size_t i = 0; i < ad_len; i++) {
1892 total_len_bytes += ad[i].data_len + 2;
1893 }
1894
1895 if ((total_len_bytes > BT_HCI_LE_PER_ADV_FRAG_MAX_LEN) &&
1896 atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED)) {
1897 /* It is not allowed to set periodic advertising data
1898 * in multiple operations while it is running.
1899 */
1900 return -EINVAL;
1901 }
1902
1903 return hci_set_per_adv_data(adv, ad, ad_len);
1904 }
1905
bt_le_per_adv_set_subevent_data(const struct bt_le_ext_adv * adv,uint8_t num_subevents,const struct bt_le_per_adv_subevent_data_params * params)1906 int bt_le_per_adv_set_subevent_data(const struct bt_le_ext_adv *adv, uint8_t num_subevents,
1907 const struct bt_le_per_adv_subevent_data_params *params)
1908 {
1909 struct bt_hci_cp_le_set_pawr_subevent_data *cp;
1910 struct bt_hci_cp_le_set_pawr_subevent_data_element *element;
1911 struct net_buf *buf;
1912 size_t cmd_length = sizeof(*cp);
1913
1914 if (!BT_FEAT_LE_PAWR_ADVERTISER(bt_dev.le.features)) {
1915 return -ENOTSUP;
1916 }
1917
1918 for (size_t i = 0; i < num_subevents; i++) {
1919 cmd_length += sizeof(struct bt_hci_cp_le_set_pawr_subevent_data_element);
1920 cmd_length += params[i].data->len;
1921 }
1922
1923 if (cmd_length > 0xFF) {
1924 return -EINVAL;
1925 }
1926
1927 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_SUBEVENT_DATA, (uint8_t)cmd_length);
1928 if (!buf) {
1929 return -ENOBUFS;
1930 }
1931
1932 cp = net_buf_add(buf, sizeof(*cp));
1933 cp->adv_handle = adv->handle;
1934 cp->num_subevents = num_subevents;
1935
1936 for (size_t i = 0; i < num_subevents; i++) {
1937 element = net_buf_add(buf, sizeof(*element));
1938 element->subevent = params[i].subevent;
1939 element->response_slot_start = params[i].response_slot_start;
1940 element->response_slot_count = params[i].response_slot_count;
1941 element->subevent_data_length = params[i].data->len;
1942 net_buf_add_mem(buf, params[i].data->data, params[i].data->len);
1943 }
1944
1945 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_SUBEVENT_DATA, buf, NULL);
1946 }
1947
bt_le_per_adv_enable(struct bt_le_ext_adv * adv,bool enable)1948 static int bt_le_per_adv_enable(struct bt_le_ext_adv *adv, bool enable)
1949 {
1950 struct bt_hci_cp_le_set_per_adv_enable *cp;
1951 struct net_buf *buf;
1952 struct bt_hci_cmd_state_set state;
1953 int err;
1954
1955 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1956 return -ENOTSUP;
1957 }
1958
1959 /* TODO: We could setup some default ext adv params if not already set*/
1960 if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1961 return -EINVAL;
1962 }
1963
1964 if (atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED) == enable) {
1965 return -EALREADY;
1966 }
1967
1968 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_ENABLE, sizeof(*cp));
1969 if (!buf) {
1970 return -ENOBUFS;
1971 }
1972
1973 cp = net_buf_add(buf, sizeof(*cp));
1974 (void)memset(cp, 0, sizeof(*cp));
1975
1976 cp->handle = adv->handle;
1977
1978 if (enable) {
1979 cp->enable = BT_HCI_LE_SET_PER_ADV_ENABLE_ENABLE;
1980
1981 if (atomic_test_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI)) {
1982 cp->enable |= BT_HCI_LE_SET_PER_ADV_ENABLE_ADI;
1983 }
1984 } else {
1985 cp->enable = 0U;
1986 }
1987
1988 bt_hci_cmd_state_set_init(buf, &state, adv->flags,
1989 BT_PER_ADV_ENABLED, enable);
1990
1991 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_ENABLE, buf, NULL);
1992 if (err) {
1993 return err;
1994 }
1995
1996 return 0;
1997 }
1998
bt_le_per_adv_start(struct bt_le_ext_adv * adv)1999 int bt_le_per_adv_start(struct bt_le_ext_adv *adv)
2000 {
2001 return bt_le_per_adv_enable(adv, true);
2002 }
2003
bt_le_per_adv_stop(struct bt_le_ext_adv * adv)2004 int bt_le_per_adv_stop(struct bt_le_ext_adv *adv)
2005 {
2006 return bt_le_per_adv_enable(adv, false);
2007 }
2008
2009 #if defined(CONFIG_BT_PER_ADV_RSP)
bt_hci_le_per_adv_subevent_data_request(struct net_buf * buf)2010 void bt_hci_le_per_adv_subevent_data_request(struct net_buf *buf)
2011 {
2012 struct bt_hci_evt_le_per_adv_subevent_data_request *evt;
2013 struct bt_le_per_adv_data_request request;
2014 struct bt_le_ext_adv *adv;
2015
2016 if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_subevent_data_request)) {
2017 LOG_ERR("Invalid data request");
2018
2019 return;
2020 }
2021
2022 evt = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_subevent_data_request));
2023 adv = bt_hci_adv_lookup_handle(evt->adv_handle);
2024 if (!adv) {
2025 LOG_ERR("Unknown advertising handle %d", evt->adv_handle);
2026
2027 return;
2028 }
2029
2030 request.start = evt->subevent_start;
2031 request.count = evt->subevent_data_count;
2032
2033 if (adv->cb && adv->cb->pawr_data_request) {
2034 adv->cb->pawr_data_request(adv, &request);
2035 }
2036 }
2037
bt_hci_le_per_adv_response_report(struct net_buf * buf)2038 void bt_hci_le_per_adv_response_report(struct net_buf *buf)
2039 {
2040 struct bt_hci_evt_le_per_adv_response_report *evt;
2041 struct bt_hci_evt_le_per_adv_response *response;
2042 struct bt_le_ext_adv *adv;
2043 struct bt_le_per_adv_response_info info;
2044 struct net_buf_simple data;
2045
2046 if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_response_report)) {
2047 LOG_ERR("Invalid response report");
2048
2049 return;
2050 }
2051
2052 evt = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_response_report));
2053 adv = bt_hci_adv_lookup_handle(evt->adv_handle);
2054 if (!adv) {
2055 LOG_ERR("Unknown advertising handle %d", evt->adv_handle);
2056
2057 return;
2058 }
2059
2060 info.subevent = evt->subevent;
2061 info.tx_status = evt->tx_status;
2062
2063 for (uint8_t i = 0; i < evt->num_responses; i++) {
2064 if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_response)) {
2065 LOG_ERR("Invalid response report");
2066
2067 return;
2068 }
2069
2070 response = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_response));
2071 info.tx_power = response->tx_power;
2072 info.rssi = response->rssi;
2073 info.cte_type = bt_get_df_cte_type(response->cte_type);
2074 info.response_slot = response->response_slot;
2075
2076 if (buf->len < response->data_length) {
2077 LOG_ERR("Invalid response report");
2078
2079 return;
2080 }
2081
2082 if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL) {
2083 LOG_WRN("Incomplete response report received, discarding");
2084 (void)net_buf_pull_mem(buf, response->data_length);
2085 } else if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_RX_FAILED) {
2086 (void)net_buf_pull_mem(buf, response->data_length);
2087
2088 if (adv->cb && adv->cb->pawr_response) {
2089 adv->cb->pawr_response(adv, &info, NULL);
2090 }
2091 } else if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
2092 net_buf_simple_init_with_data(&data,
2093 net_buf_pull_mem(buf, response->data_length),
2094 response->data_length);
2095
2096 if (adv->cb && adv->cb->pawr_response) {
2097 adv->cb->pawr_response(adv, &info, &data);
2098 }
2099 } else {
2100 LOG_ERR("Invalid data status %d", response->data_status);
2101 (void)net_buf_pull_mem(buf, response->data_length);
2102 }
2103 }
2104 }
2105 #endif /* CONFIG_BT_PER_ADV_RSP */
2106
2107 #if defined(CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER)
bt_le_per_adv_set_info_transfer(const struct bt_le_ext_adv * adv,const struct bt_conn * conn,uint16_t service_data)2108 int bt_le_per_adv_set_info_transfer(const struct bt_le_ext_adv *adv,
2109 const struct bt_conn *conn,
2110 uint16_t service_data)
2111 {
2112 struct bt_hci_cp_le_per_adv_set_info_transfer *cp;
2113 struct net_buf *buf;
2114
2115
2116 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2117 return -ENOTSUP;
2118 } else if (!BT_FEAT_LE_PAST_SEND(bt_dev.le.features)) {
2119 return -ENOTSUP;
2120 }
2121
2122 buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_SET_INFO_TRANSFER,
2123 sizeof(*cp));
2124 if (!buf) {
2125 return -ENOBUFS;
2126 }
2127
2128 cp = net_buf_add(buf, sizeof(*cp));
2129 (void)memset(cp, 0, sizeof(*cp));
2130
2131 cp->conn_handle = sys_cpu_to_le16(conn->handle);
2132 cp->adv_handle = adv->handle;
2133 cp->service_data = sys_cpu_to_le16(service_data);
2134
2135 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_SET_INFO_TRANSFER, buf,
2136 NULL);
2137 }
2138 #endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER */
2139 #endif /* CONFIG_BT_PER_ADV */
2140
2141 #if defined(CONFIG_BT_EXT_ADV)
2142 #if defined(CONFIG_BT_BROADCASTER)
bt_hci_le_adv_set_terminated(struct net_buf * buf)2143 void bt_hci_le_adv_set_terminated(struct net_buf *buf)
2144 {
2145 struct bt_hci_evt_le_adv_set_terminated *evt;
2146 struct bt_le_ext_adv *adv;
2147 uint16_t conn_handle;
2148 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2149 bool was_adv_enabled;
2150 #endif
2151
2152 evt = (void *)buf->data;
2153 adv = bt_hci_adv_lookup_handle(evt->adv_handle);
2154 conn_handle = sys_le16_to_cpu(evt->conn_handle);
2155
2156 LOG_DBG("status 0x%02x %s adv_handle %u conn_handle 0x%02x num %u",
2157 evt->status, bt_hci_err_to_str(evt->status),
2158 evt->adv_handle, conn_handle, evt->num_completed_ext_adv_evts);
2159
2160 if (!adv) {
2161 LOG_ERR("No valid adv");
2162 return;
2163 }
2164
2165 (void)bt_le_lim_adv_cancel_timeout(adv);
2166
2167 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2168 was_adv_enabled = atomic_test_bit(adv->flags, BT_ADV_ENABLED);
2169 #endif
2170
2171 atomic_clear_bit(adv->flags, BT_ADV_ENABLED);
2172
2173 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2174 bt_dev.adv_conn_id = adv->id;
2175 for (int i = 0; i < ARRAY_SIZE(bt_dev.cached_conn_complete); i++) {
2176 if (bt_dev.cached_conn_complete[i].valid &&
2177 bt_dev.cached_conn_complete[i].evt.handle == evt->conn_handle) {
2178 if (was_adv_enabled) {
2179 /* Process the cached connection complete event
2180 * now that the corresponding advertising set is known.
2181 *
2182 * If the advertiser has been stopped before the connection
2183 * complete event has been raised to the application, we
2184 * discard the event.
2185 */
2186 bt_hci_le_enh_conn_complete(&bt_dev.cached_conn_complete[i].evt);
2187 }
2188 bt_dev.cached_conn_complete[i].valid = false;
2189 }
2190 }
2191 #endif
2192
2193 if (evt->status && IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
2194 atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
2195 /* This will call connected callback for high duty cycle
2196 * directed advertiser timeout.
2197 */
2198 le_adv_stop_free_conn(adv, evt->status);
2199 }
2200
2201 if (IS_ENABLED(CONFIG_BT_CONN) && !evt->status) {
2202 struct bt_conn *conn = bt_conn_lookup_handle(conn_handle, BT_CONN_TYPE_LE);
2203
2204 if (conn) {
2205 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
2206 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
2207 /* Set Responder address unless already set */
2208 conn->le.resp_addr.type = BT_ADDR_LE_RANDOM;
2209 if (bt_addr_eq(&conn->le.resp_addr.a, BT_ADDR_ANY)) {
2210 bt_addr_copy(&conn->le.resp_addr.a,
2211 &adv->random_addr.a);
2212 }
2213 } else if (adv->options & BT_LE_ADV_OPT_USE_NRPA) {
2214 bt_addr_le_copy(&conn->le.resp_addr,
2215 &adv->random_addr);
2216 } else {
2217 bt_addr_le_copy(&conn->le.resp_addr,
2218 &bt_dev.id_addr[conn->id]);
2219 }
2220
2221 if (adv->cb && adv->cb->connected) {
2222 struct bt_le_ext_adv_connected_info info = {
2223 .conn = conn,
2224 };
2225
2226 adv->cb->connected(adv, &info);
2227 }
2228
2229 bt_conn_unref(conn);
2230 }
2231 }
2232
2233 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
2234 bt_id_adv_limited_stopped(adv);
2235
2236 #if defined(CONFIG_BT_SMP)
2237 bt_id_pending_keys_update();
2238 #endif
2239
2240 if (adv->cb && adv->cb->sent) {
2241 struct bt_le_ext_adv_sent_info info = {
2242 .num_sent = evt->num_completed_ext_adv_evts,
2243 };
2244
2245 adv->cb->sent(adv, &info);
2246 }
2247 }
2248
2249 if (adv == bt_dev.adv) {
2250 if (atomic_test_bit(adv->flags, BT_ADV_PERSIST)) {
2251 #if defined(CONFIG_BT_PERIPHERAL)
2252 bt_le_adv_resume();
2253 #endif
2254 } else {
2255 bt_le_adv_delete_legacy();
2256 }
2257 }
2258 }
2259
bt_hci_le_scan_req_received(struct net_buf * buf)2260 void bt_hci_le_scan_req_received(struct net_buf *buf)
2261 {
2262 struct bt_hci_evt_le_scan_req_received *evt;
2263 struct bt_le_ext_adv *adv;
2264
2265 evt = (void *)buf->data;
2266 adv = bt_hci_adv_lookup_handle(evt->handle);
2267
2268 LOG_DBG("handle %u peer %s", evt->handle, bt_addr_le_str(&evt->addr));
2269
2270 if (!adv) {
2271 LOG_ERR("No valid adv");
2272 return;
2273 }
2274
2275 if (adv->cb && adv->cb->scanned) {
2276 struct bt_le_ext_adv_scanned_info info;
2277 bt_addr_le_t id_addr;
2278
2279 if (bt_addr_le_is_resolved(&evt->addr)) {
2280 bt_addr_le_copy_resolved(&id_addr, &evt->addr);
2281 } else {
2282 bt_addr_le_copy(&id_addr,
2283 bt_lookup_id_addr(adv->id, &evt->addr));
2284 }
2285
2286 info.addr = &id_addr;
2287 adv->cb->scanned(adv, &info);
2288 }
2289 }
2290 #endif /* defined(CONFIG_BT_BROADCASTER) */
2291 #endif /* defined(CONFIG_BT_EXT_ADV) */
2292