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