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_adv_lookup_handle(uint8_t handle)249 static struct bt_le_ext_adv *bt_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_HCI_LE_EXT_ADV_FRAG_MAX_LEN) {
665 /* If possible, set all data at once.
666 * This allows us to update advertising data while advertising.
667 */
668 return hci_set_adv_ext_complete(adv, hci_op, total_len_bytes, ad, ad_len);
669 } else {
670 return hci_set_adv_ext_fragmented(adv, hci_op, ad, ad_len);
671 }
672
673 return 0;
674 }
675
set_ad(struct bt_le_ext_adv * adv,const struct bt_ad * ad,size_t ad_len)676 static int set_ad(struct bt_le_ext_adv *adv, const struct bt_ad *ad,
677 size_t ad_len)
678 {
679 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
680 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
681 return hci_set_ad_ext(adv, BT_HCI_OP_LE_SET_EXT_ADV_DATA,
682 ad, ad_len);
683 }
684
685 return hci_set_ad(BT_HCI_OP_LE_SET_ADV_DATA, ad, ad_len);
686 }
687
set_sd(struct bt_le_ext_adv * adv,const struct bt_ad * sd,size_t sd_len)688 static int set_sd(struct bt_le_ext_adv *adv, const struct bt_ad *sd,
689 size_t sd_len)
690 {
691 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
692 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
693 return hci_set_ad_ext(adv, BT_HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
694 sd, sd_len);
695 }
696
697 return hci_set_ad(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, sd, sd_len);
698 }
699
700 #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)701 static int hci_set_per_adv_data(const struct bt_le_ext_adv *adv,
702 const struct bt_data *ad, size_t ad_len)
703 {
704 int err;
705 struct ad_stream stream;
706 struct bt_ad d = { .data = ad, .len = ad_len };
707 bool is_first_iteration = true;
708
709 err = ad_stream_new(&stream, &d, 1);
710 if (err) {
711 return err;
712 }
713
714 while (!ad_stream_is_empty(&stream)) {
715 struct bt_hci_cp_le_set_per_adv_data *set_data;
716 struct net_buf *buf;
717 const size_t data_len = MIN(BT_HCI_LE_PER_ADV_FRAG_MAX_LEN, stream.remaining_size);
718 const size_t cmd_size = sizeof(*set_data) + data_len;
719
720 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_DATA, cmd_size);
721 if (!buf) {
722 return -ENOBUFS;
723 }
724
725 set_data = net_buf_add(buf, cmd_size);
726 (void)memset(set_data, 0, cmd_size);
727
728 set_data->handle = adv->handle;
729 set_data->len = ad_stream_read(&stream, set_data->data, data_len);
730
731 if (is_first_iteration && ad_stream_is_empty(&stream)) {
732 set_data->op = BT_HCI_LE_EXT_ADV_OP_COMPLETE_DATA;
733 } else if (is_first_iteration) {
734 set_data->op = BT_HCI_LE_EXT_ADV_OP_FIRST_FRAG;
735 } else if (ad_stream_is_empty(&stream)) {
736 set_data->op = BT_HCI_LE_EXT_ADV_OP_LAST_FRAG;
737 } else {
738 set_data->op = BT_HCI_LE_EXT_ADV_OP_INTERM_FRAG;
739 }
740
741 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_DATA, buf, NULL);
742 if (err) {
743 return err;
744 }
745
746 is_first_iteration = false;
747 }
748
749 return 0;
750 }
751 #endif /* CONFIG_BT_PER_ADV */
752
ad_has_name(const struct bt_data * ad,size_t ad_len)753 static inline bool ad_has_name(const struct bt_data *ad, size_t ad_len)
754 {
755 size_t i;
756
757 for (i = 0; i < ad_len; i++) {
758 if (ad[i].type == BT_DATA_NAME_COMPLETE ||
759 ad[i].type == BT_DATA_NAME_SHORTENED) {
760 return true;
761 }
762 }
763
764 return false;
765 }
766
ad_is_limited(const struct bt_data * ad,size_t ad_len)767 static bool ad_is_limited(const struct bt_data *ad, size_t ad_len)
768 {
769 size_t i;
770
771 for (i = 0; i < ad_len; i++) {
772 if (ad[i].type == BT_DATA_FLAGS &&
773 ad[i].data_len == sizeof(uint8_t) &&
774 ad[i].data != NULL) {
775 if (ad[i].data[0] & BT_LE_AD_LIMITED) {
776 return true;
777 }
778 }
779 }
780
781 return false;
782 }
783
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)784 static int le_adv_update(struct bt_le_ext_adv *adv,
785 const struct bt_data *ad, size_t ad_len,
786 const struct bt_data *sd, size_t sd_len,
787 bool ext_adv, bool scannable,
788 enum adv_name_type name_type)
789 {
790 struct bt_ad d[2] = {};
791 struct bt_data data;
792 size_t d_len;
793 int err;
794
795 if (name_type != ADV_NAME_TYPE_NONE) {
796 const char *name = bt_get_name();
797
798 if ((ad && ad_has_name(ad, ad_len)) ||
799 (sd && ad_has_name(sd, sd_len))) {
800 /* Cannot use name if name is already set */
801 return -EINVAL;
802 }
803
804 data = (struct bt_data)BT_DATA(
805 BT_DATA_NAME_COMPLETE,
806 name, strlen(name));
807 }
808
809 if (!(ext_adv && scannable)) {
810 d_len = 1;
811 d[0].data = ad;
812 d[0].len = ad_len;
813
814 if (name_type == ADV_NAME_TYPE_AD) {
815 d[1].data = &data;
816 d[1].len = 1;
817 d_len = 2;
818 }
819
820 err = set_ad(adv, d, d_len);
821 if (err) {
822 return err;
823 }
824 }
825
826 if (scannable) {
827 d_len = 1;
828 d[0].data = sd;
829 d[0].len = sd_len;
830
831 if (name_type == ADV_NAME_TYPE_SD) {
832 d[1].data = &data;
833 d[1].len = 1;
834 d_len = 2;
835 }
836
837 err = set_sd(adv, d, d_len);
838 if (err) {
839 return err;
840 }
841 }
842
843 atomic_set_bit(adv->flags, BT_ADV_DATA_SET);
844 return 0;
845 }
846
bt_le_adv_update_data(const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len)847 int bt_le_adv_update_data(const struct bt_data *ad, size_t ad_len,
848 const struct bt_data *sd, size_t sd_len)
849 {
850 struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
851 bool scannable;
852
853 if (!adv) {
854 return -EINVAL;
855 }
856
857 if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
858 return -EAGAIN;
859 }
860
861 scannable = atomic_test_bit(adv->flags, BT_ADV_SCANNABLE);
862
863 return le_adv_update(adv, ad, ad_len, sd, sd_len, false, scannable,
864 get_adv_name_type(adv));
865 }
866
get_filter_policy(uint32_t options)867 static uint8_t get_filter_policy(uint32_t options)
868 {
869 if (!IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST)) {
870 return BT_LE_ADV_FP_NO_FILTER;
871 } else if ((options & BT_LE_ADV_OPT_FILTER_SCAN_REQ) &&
872 (options & BT_LE_ADV_OPT_FILTER_CONN)) {
873 return BT_LE_ADV_FP_FILTER_BOTH;
874 } else if (options & BT_LE_ADV_OPT_FILTER_SCAN_REQ) {
875 return BT_LE_ADV_FP_FILTER_SCAN_REQ;
876 } else if (options & BT_LE_ADV_OPT_FILTER_CONN) {
877 return BT_LE_ADV_FP_FILTER_CONN_IND;
878 } else {
879 return BT_LE_ADV_FP_NO_FILTER;
880 }
881 }
882
get_adv_channel_map(uint32_t options)883 static uint8_t get_adv_channel_map(uint32_t options)
884 {
885 uint8_t channel_map = 0x07;
886
887 if (options & BT_LE_ADV_OPT_DISABLE_CHAN_37) {
888 channel_map &= ~0x01;
889 }
890
891 if (options & BT_LE_ADV_OPT_DISABLE_CHAN_38) {
892 channel_map &= ~0x02;
893 }
894
895 if (options & BT_LE_ADV_OPT_DISABLE_CHAN_39) {
896 channel_map &= ~0x04;
897 }
898
899 return channel_map;
900 }
901
adv_is_directed(const struct bt_le_ext_adv * adv)902 static inline bool adv_is_directed(const struct bt_le_ext_adv *adv)
903 {
904 /* The advertiser is assumed to be directed when the peer address has
905 * been set.
906 */
907 return !bt_addr_le_eq(&adv->target_addr, BT_ADDR_LE_ANY);
908 }
909
le_adv_start_add_conn(const struct bt_le_ext_adv * adv,struct bt_conn ** out_conn)910 static int le_adv_start_add_conn(const struct bt_le_ext_adv *adv,
911 struct bt_conn **out_conn)
912 {
913 struct bt_conn *conn;
914
915 bt_dev.adv_conn_id = adv->id;
916
917 if (!adv_is_directed(adv)) {
918 /* Undirected advertising */
919 conn = bt_conn_add_le(adv->id, BT_ADDR_LE_NONE);
920 if (!conn) {
921 return -ENOMEM;
922 }
923
924 bt_conn_set_state(conn, BT_CONN_CONNECTING_ADV);
925 *out_conn = conn;
926 return 0;
927 }
928
929 if (bt_conn_exists_le(adv->id, &adv->target_addr)) {
930 return -EINVAL;
931 }
932
933 conn = bt_conn_add_le(adv->id, &adv->target_addr);
934 if (!conn) {
935 return -ENOMEM;
936 }
937
938 bt_conn_set_state(conn, BT_CONN_CONNECTING_DIR_ADV);
939 *out_conn = conn;
940 return 0;
941 }
942
le_adv_stop_free_conn(const struct bt_le_ext_adv * adv,uint8_t status)943 static void le_adv_stop_free_conn(const struct bt_le_ext_adv *adv, uint8_t status)
944 {
945 struct bt_conn *conn;
946
947 if (!adv_is_directed(adv)) {
948 conn = bt_conn_lookup_state_le(adv->id, BT_ADDR_LE_NONE,
949 BT_CONN_CONNECTING_ADV);
950 } else {
951 conn = bt_conn_lookup_state_le(adv->id, &adv->target_addr,
952 BT_CONN_CONNECTING_DIR_ADV);
953 }
954
955 if (conn) {
956 conn->err = status;
957 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
958 bt_conn_unref(conn);
959 }
960 }
961
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)962 int bt_le_adv_start_legacy(struct bt_le_ext_adv *adv,
963 const struct bt_le_adv_param *param,
964 const struct bt_data *ad, size_t ad_len,
965 const struct bt_data *sd, size_t sd_len)
966 {
967 struct bt_hci_cp_le_set_adv_param set_param;
968 struct bt_conn *conn = NULL;
969 struct net_buf *buf;
970 bool dir_adv = (param->peer != NULL), scannable = false;
971 enum adv_name_type name_type;
972
973 int err;
974
975 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
976 return -EAGAIN;
977 }
978
979 if (!valid_adv_param(param)) {
980 return -EINVAL;
981 }
982
983 if (!bt_id_adv_random_addr_check(param)) {
984 return -EINVAL;
985 }
986
987 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
988 return -EALREADY;
989 }
990
991 (void)memset(&set_param, 0, sizeof(set_param));
992
993 set_param.min_interval = sys_cpu_to_le16(param->interval_min);
994 set_param.max_interval = sys_cpu_to_le16(param->interval_max);
995 set_param.channel_map = get_adv_channel_map(param->options);
996 set_param.filter_policy = get_filter_policy(param->options);
997
998 atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
999
1000 adv->id = param->id;
1001 bt_dev.adv_conn_id = adv->id;
1002
1003 err = bt_id_set_adv_own_addr(adv, param->options, dir_adv,
1004 &set_param.own_addr_type);
1005 if (err) {
1006 return err;
1007 }
1008
1009 if (dir_adv) {
1010 bt_addr_le_copy(&adv->target_addr, param->peer);
1011 } else {
1012 bt_addr_le_copy(&adv->target_addr, BT_ADDR_LE_ANY);
1013 }
1014
1015 name_type = get_adv_name_type_param(param);
1016
1017 if (param->options & BT_LE_ADV_OPT_CONNECTABLE) {
1018 if (dir_adv) {
1019 if (param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY) {
1020 set_param.type = BT_HCI_ADV_DIRECT_IND_LOW_DUTY;
1021 } else {
1022 set_param.type = BT_HCI_ADV_DIRECT_IND;
1023 }
1024
1025 bt_addr_le_copy(&set_param.direct_addr, param->peer);
1026 } else {
1027 scannable = true;
1028 set_param.type = BT_HCI_ADV_IND;
1029 }
1030 } else if ((param->options & BT_LE_ADV_OPT_SCANNABLE) || sd ||
1031 (name_type == ADV_NAME_TYPE_SD)) {
1032 scannable = true;
1033 set_param.type = BT_HCI_ADV_SCAN_IND;
1034 } else {
1035 set_param.type = BT_HCI_ADV_NONCONN_IND;
1036 }
1037
1038 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_PARAM, sizeof(set_param));
1039 if (!buf) {
1040 return -ENOBUFS;
1041 }
1042
1043 net_buf_add_mem(buf, &set_param, sizeof(set_param));
1044
1045 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_PARAM, buf, NULL);
1046 if (err) {
1047 return err;
1048 }
1049
1050 if (!dir_adv) {
1051 err = le_adv_update(adv, ad, ad_len, sd, sd_len, false,
1052 scannable, name_type);
1053 if (err) {
1054 return err;
1055 }
1056 }
1057
1058 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1059 (param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
1060 err = le_adv_start_add_conn(adv, &conn);
1061 if (err) {
1062 if (err == -ENOMEM && !dir_adv &&
1063 !(param->options & BT_LE_ADV_OPT_ONE_TIME)) {
1064 goto set_adv_state;
1065 }
1066
1067 return err;
1068 }
1069 }
1070
1071 err = bt_le_adv_set_enable(adv, true);
1072 if (err) {
1073 LOG_ERR("Failed to start advertiser");
1074 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1075 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1076 bt_conn_unref(conn);
1077 }
1078
1079 return err;
1080 }
1081
1082 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1083 /* If undirected connectable advertiser we have created a
1084 * connection object that we don't yet give to the application.
1085 * Since we don't give the application a reference to manage in
1086 * this case, we need to release this reference here
1087 */
1088 bt_conn_unref(conn);
1089 }
1090
1091 set_adv_state:
1092 atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, !dir_adv &&
1093 !(param->options & BT_LE_ADV_OPT_ONE_TIME));
1094
1095 atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_AD,
1096 name_type == ADV_NAME_TYPE_AD);
1097
1098 atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_SD,
1099 name_type == ADV_NAME_TYPE_SD);
1100
1101 atomic_set_bit_to(adv->flags, BT_ADV_CONNECTABLE,
1102 param->options & BT_LE_ADV_OPT_CONNECTABLE);
1103
1104 atomic_set_bit_to(adv->flags, BT_ADV_SCANNABLE, scannable);
1105
1106 atomic_set_bit_to(adv->flags, BT_ADV_USE_IDENTITY,
1107 param->options & BT_LE_ADV_OPT_USE_IDENTITY);
1108
1109 return 0;
1110 }
1111
le_ext_adv_param_set(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param,bool has_scan_data)1112 static int le_ext_adv_param_set(struct bt_le_ext_adv *adv,
1113 const struct bt_le_adv_param *param,
1114 bool has_scan_data)
1115 {
1116 struct bt_hci_cp_le_set_ext_adv_param *cp;
1117 bool dir_adv = param->peer != NULL, scannable;
1118 struct net_buf *buf, *rsp;
1119 int err;
1120 enum adv_name_type name_type;
1121 uint16_t props = 0;
1122
1123 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_ADV_PARAM, sizeof(*cp));
1124 if (!buf) {
1125 return -ENOBUFS;
1126 }
1127
1128 cp = net_buf_add(buf, sizeof(*cp));
1129 (void)memset(cp, 0, sizeof(*cp));
1130
1131 adv->options = param->options;
1132
1133 err = bt_id_set_adv_own_addr(adv, param->options, dir_adv,
1134 &cp->own_addr_type);
1135 if (err) {
1136 return err;
1137 }
1138
1139 if (dir_adv) {
1140 bt_addr_le_copy(&adv->target_addr, param->peer);
1141 } else {
1142 bt_addr_le_copy(&adv->target_addr, BT_ADDR_LE_ANY);
1143 }
1144
1145 name_type = get_adv_name_type_param(param);
1146
1147 cp->handle = adv->handle;
1148 sys_put_le24(param->interval_min, cp->prim_min_interval);
1149 sys_put_le24(param->interval_max, cp->prim_max_interval);
1150 cp->prim_channel_map = get_adv_channel_map(param->options);
1151 cp->filter_policy = get_filter_policy(param->options);
1152 cp->tx_power = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
1153
1154 cp->prim_adv_phy = BT_HCI_LE_PHY_1M;
1155 if ((param->options & BT_LE_ADV_OPT_EXT_ADV) &&
1156 !(param->options & BT_LE_ADV_OPT_NO_2M)) {
1157 cp->sec_adv_phy = BT_HCI_LE_PHY_2M;
1158 } else {
1159 cp->sec_adv_phy = BT_HCI_LE_PHY_1M;
1160 }
1161
1162 if (param->options & BT_LE_ADV_OPT_CODED) {
1163 cp->prim_adv_phy = BT_HCI_LE_PHY_CODED;
1164 cp->sec_adv_phy = BT_HCI_LE_PHY_CODED;
1165 }
1166
1167 if (!(param->options & BT_LE_ADV_OPT_EXT_ADV)) {
1168 props |= BT_HCI_LE_ADV_PROP_LEGACY;
1169 }
1170
1171 if (param->options & BT_LE_ADV_OPT_USE_TX_POWER) {
1172 props |= BT_HCI_LE_ADV_PROP_TX_POWER;
1173 }
1174
1175 if (param->options & BT_LE_ADV_OPT_ANONYMOUS) {
1176 props |= BT_HCI_LE_ADV_PROP_ANON;
1177 }
1178
1179 if (param->options & BT_LE_ADV_OPT_NOTIFY_SCAN_REQ) {
1180 cp->scan_req_notify_enable = BT_HCI_LE_ADV_SCAN_REQ_ENABLE;
1181 }
1182
1183 if (param->options & BT_LE_ADV_OPT_CONNECTABLE) {
1184 props |= BT_HCI_LE_ADV_PROP_CONN;
1185 if (!dir_adv && !(param->options & BT_LE_ADV_OPT_EXT_ADV)) {
1186 /* When using non-extended adv packets then undirected
1187 * advertising has to be scannable as well.
1188 * We didn't require this option to be set before, so
1189 * it is implicitly set instead in this case.
1190 */
1191 props |= BT_HCI_LE_ADV_PROP_SCAN;
1192 }
1193 }
1194
1195 if ((param->options & BT_LE_ADV_OPT_SCANNABLE) || has_scan_data ||
1196 (name_type == ADV_NAME_TYPE_SD)) {
1197 props |= BT_HCI_LE_ADV_PROP_SCAN;
1198 }
1199
1200 scannable = !!(props & BT_HCI_LE_ADV_PROP_SCAN);
1201
1202 if (dir_adv) {
1203 props |= BT_HCI_LE_ADV_PROP_DIRECT;
1204 if (!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
1205 props |= BT_HCI_LE_ADV_PROP_HI_DC_CONN;
1206 }
1207
1208 bt_addr_le_copy(&cp->peer_addr, param->peer);
1209 }
1210
1211 cp->sid = param->sid;
1212
1213 cp->props = sys_cpu_to_le16(props);
1214 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_ADV_PARAM, buf, &rsp);
1215 if (err) {
1216 return err;
1217 }
1218
1219 #if defined(CONFIG_BT_EXT_ADV)
1220 struct bt_hci_rp_le_set_ext_adv_param *rp = (void *)rsp->data;
1221
1222 adv->tx_power = rp->tx_power;
1223 #endif /* defined(CONFIG_BT_EXT_ADV) */
1224
1225 net_buf_unref(rsp);
1226
1227 atomic_set_bit(adv->flags, BT_ADV_PARAMS_SET);
1228
1229 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_RANDOM_ADDR_PENDING)) {
1230 err = bt_id_set_adv_random_addr(adv, &adv->random_addr.a);
1231 if (err) {
1232 return err;
1233 }
1234 }
1235
1236 /* Flag only used by bt_le_adv_start API. */
1237 atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, false);
1238
1239 atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_AD,
1240 name_type == ADV_NAME_TYPE_AD);
1241
1242 atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_SD,
1243 name_type == ADV_NAME_TYPE_SD);
1244
1245 atomic_set_bit_to(adv->flags, BT_ADV_CONNECTABLE,
1246 param->options & BT_LE_ADV_OPT_CONNECTABLE);
1247
1248 atomic_set_bit_to(adv->flags, BT_ADV_SCANNABLE, scannable);
1249
1250 atomic_set_bit_to(adv->flags, BT_ADV_USE_IDENTITY,
1251 param->options & BT_LE_ADV_OPT_USE_IDENTITY);
1252
1253 atomic_set_bit_to(adv->flags, BT_ADV_EXT_ADV,
1254 param->options & BT_LE_ADV_OPT_EXT_ADV);
1255
1256 return 0;
1257 }
1258
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)1259 int bt_le_adv_start_ext(struct bt_le_ext_adv *adv,
1260 const struct bt_le_adv_param *param,
1261 const struct bt_data *ad, size_t ad_len,
1262 const struct bt_data *sd, size_t sd_len)
1263 {
1264 struct bt_le_ext_adv_start_param start_param = {
1265 .timeout = 0,
1266 .num_events = 0,
1267 };
1268 bool dir_adv = (param->peer != NULL);
1269 struct bt_conn *conn = NULL;
1270 int err;
1271
1272 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1273 return -EAGAIN;
1274 }
1275
1276 if (!valid_adv_param(param)) {
1277 return -EINVAL;
1278 }
1279
1280 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1281 return -EALREADY;
1282 }
1283
1284 adv->id = param->id;
1285 err = le_ext_adv_param_set(adv, param, sd != NULL);
1286 if (err) {
1287 return err;
1288 }
1289
1290 if (!dir_adv) {
1291 if (IS_ENABLED(CONFIG_BT_EXT_ADV)) {
1292 err = bt_le_ext_adv_set_data(adv, ad, ad_len, sd, sd_len);
1293 if (err) {
1294 return err;
1295 }
1296 }
1297 } else {
1298 if (!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
1299 start_param.timeout =
1300 BT_GAP_ADV_HIGH_DUTY_CYCLE_MAX_TIMEOUT;
1301 atomic_set_bit(adv->flags, BT_ADV_LIMITED);
1302 }
1303 }
1304
1305 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1306 (param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
1307 err = le_adv_start_add_conn(adv, &conn);
1308 if (err) {
1309 if (err == -ENOMEM && !dir_adv &&
1310 !(param->options & BT_LE_ADV_OPT_ONE_TIME)) {
1311 goto set_adv_state;
1312 }
1313
1314 return err;
1315 }
1316 }
1317
1318 err = bt_le_adv_set_enable_ext(adv, true, &start_param);
1319 if (err) {
1320 LOG_ERR("Failed to start advertiser");
1321 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1322 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1323 bt_conn_unref(conn);
1324 }
1325
1326 return err;
1327 }
1328
1329 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1330 /* If undirected connectable advertiser we have created a
1331 * connection object that we don't yet give to the application.
1332 * Since we don't give the application a reference to manage in
1333 * this case, we need to release this reference here
1334 */
1335 bt_conn_unref(conn);
1336 }
1337
1338 set_adv_state:
1339 /* Flag always set to false by le_ext_adv_param_set */
1340 atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, !dir_adv &&
1341 !(param->options & BT_LE_ADV_OPT_ONE_TIME));
1342
1343 return 0;
1344 }
1345
1346 static void adv_timeout(struct k_work *work);
1347
bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv * adv)1348 int bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv *adv)
1349 {
1350 return k_work_cancel_delayable(&adv->lim_adv_timeout_work);
1351 }
1352
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)1353 int bt_le_adv_start(const struct bt_le_adv_param *param,
1354 const struct bt_data *ad, size_t ad_len,
1355 const struct bt_data *sd, size_t sd_len)
1356 {
1357 struct bt_le_ext_adv *adv = adv_get_legacy();
1358 int err;
1359
1360 if (!adv) {
1361 return -ENOMEM;
1362 }
1363
1364 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1365 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1366 err = bt_le_adv_start_ext(adv, param, ad, ad_len, sd, sd_len);
1367 } else {
1368 err = bt_le_adv_start_legacy(adv, param, ad, ad_len, sd, sd_len);
1369 }
1370
1371 if (err) {
1372 bt_le_adv_delete_legacy();
1373 }
1374
1375 if (ad_is_limited(ad, ad_len)) {
1376 k_work_init_delayable(&adv->lim_adv_timeout_work, adv_timeout);
1377 k_work_reschedule(&adv->lim_adv_timeout_work,
1378 K_SECONDS(CONFIG_BT_LIM_ADV_TIMEOUT));
1379 }
1380
1381 return err;
1382 }
1383
bt_le_adv_stop(void)1384 int bt_le_adv_stop(void)
1385 {
1386 struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
1387 int err;
1388
1389 if (!adv) {
1390 LOG_ERR("No valid legacy adv");
1391 return 0;
1392 }
1393
1394 (void)bt_le_lim_adv_cancel_timeout(adv);
1395
1396 /* Make sure advertising is not re-enabled later even if it's not
1397 * currently enabled (i.e. BT_DEV_ADVERTISING is not set).
1398 */
1399 atomic_clear_bit(adv->flags, BT_ADV_PERSIST);
1400
1401 if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1402 /* Legacy advertiser exists, but is not currently advertising.
1403 * This happens when keep advertising behavior is active but
1404 * no conn object is available to do connectable advertising.
1405 */
1406 bt_le_adv_delete_legacy();
1407 return 0;
1408 }
1409
1410 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1411 atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1412 le_adv_stop_free_conn(adv, 0);
1413 }
1414
1415 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1416 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1417 err = bt_le_adv_set_enable_ext(adv, false, NULL);
1418 if (err) {
1419 return err;
1420 }
1421 } else {
1422 err = bt_le_adv_set_enable_legacy(adv, false);
1423 if (err) {
1424 return err;
1425 }
1426 }
1427
1428 bt_le_adv_delete_legacy();
1429
1430 #if defined(CONFIG_BT_OBSERVER)
1431 if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1432 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) &&
1433 !IS_ENABLED(CONFIG_BT_PRIVACY) &&
1434 !IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY)) {
1435 /* If scan is ongoing set back NRPA */
1436 if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
1437 bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
1438 bt_id_set_private_addr(BT_ID_DEFAULT);
1439 bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
1440 }
1441 }
1442 #endif /* defined(CONFIG_BT_OBSERVER) */
1443
1444 return 0;
1445 }
1446
1447 #if defined(CONFIG_BT_PERIPHERAL)
adv_get_options(const struct bt_le_ext_adv * adv)1448 static uint32_t adv_get_options(const struct bt_le_ext_adv *adv)
1449 {
1450 uint32_t options = 0;
1451
1452 if (!atomic_test_bit(adv->flags, BT_ADV_PERSIST)) {
1453 options |= BT_LE_ADV_OPT_ONE_TIME;
1454 }
1455
1456 if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1457 options |= BT_LE_ADV_OPT_CONNECTABLE;
1458 }
1459
1460 if (atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1461 options |= BT_LE_ADV_OPT_USE_IDENTITY;
1462 }
1463
1464 return options;
1465 }
1466
bt_le_adv_resume(void)1467 void bt_le_adv_resume(void)
1468 {
1469 struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
1470 struct bt_conn *conn;
1471 bool persist_paused = false;
1472 int err;
1473
1474 if (!adv) {
1475 LOG_DBG("No valid legacy adv");
1476 return;
1477 }
1478
1479 if (!(atomic_test_bit(adv->flags, BT_ADV_PERSIST) &&
1480 !atomic_test_bit(adv->flags, BT_ADV_ENABLED))) {
1481 return;
1482 }
1483
1484 if (!atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1485 return;
1486 }
1487
1488 err = le_adv_start_add_conn(adv, &conn);
1489 if (err) {
1490 LOG_DBG("Host cannot resume connectable advertising (%d)", err);
1491 return;
1492 }
1493
1494 LOG_DBG("Resuming connectable advertising");
1495
1496 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1497 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1498 bt_id_set_adv_private_addr(adv);
1499 } else {
1500 uint8_t own_addr_type;
1501 bool dir_adv = adv_is_directed(adv);
1502 uint32_t options = adv_get_options(adv);
1503
1504 /* Always set the address. Don't assume it has not changed. */
1505 err = bt_id_set_adv_own_addr(adv, options, dir_adv, &own_addr_type);
1506 if (err) {
1507 LOG_ERR("Controller cannot resume connectable advertising (%d)", err);
1508 return;
1509 }
1510 }
1511
1512 err = bt_le_adv_set_enable(adv, true);
1513 if (err) {
1514 LOG_DBG("Controller cannot resume connectable advertising (%d)", err);
1515 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1516
1517 /* Temporarily clear persist flag to avoid recursion in
1518 * bt_conn_unref if the flag is still set.
1519 */
1520 persist_paused = atomic_test_and_clear_bit(adv->flags,
1521 BT_ADV_PERSIST);
1522 }
1523
1524 /* Since we don't give the application a reference to manage in
1525 * this case, we need to release this reference here.
1526 */
1527 bt_conn_unref(conn);
1528 if (persist_paused) {
1529 atomic_set_bit(adv->flags, BT_ADV_PERSIST);
1530 }
1531 }
1532 #endif /* defined(CONFIG_BT_PERIPHERAL) */
1533
1534 #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)1535 int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
1536 struct bt_le_ext_adv_info *info)
1537 {
1538 info->id = adv->id;
1539 info->tx_power = adv->tx_power;
1540 info->addr = &adv->random_addr;
1541
1542 return 0;
1543 }
1544
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)1545 int bt_le_ext_adv_create(const struct bt_le_adv_param *param,
1546 const struct bt_le_ext_adv_cb *cb,
1547 struct bt_le_ext_adv **out_adv)
1548 {
1549 struct bt_le_ext_adv *adv;
1550 int err;
1551
1552 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1553 return -EAGAIN;
1554 }
1555
1556 if (!valid_adv_ext_param(param)) {
1557 return -EINVAL;
1558 }
1559
1560 adv = adv_new();
1561 if (!adv) {
1562 return -ENOMEM;
1563 }
1564
1565 adv->id = param->id;
1566 adv->cb = cb;
1567
1568 err = le_ext_adv_param_set(adv, param, false);
1569 if (err) {
1570 adv_delete(adv);
1571 return err;
1572 }
1573
1574 *out_adv = adv;
1575 return 0;
1576 }
1577
bt_le_ext_adv_update_param(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param)1578 int bt_le_ext_adv_update_param(struct bt_le_ext_adv *adv,
1579 const struct bt_le_adv_param *param)
1580 {
1581 if (!valid_adv_ext_param(param)) {
1582 return -EINVAL;
1583 }
1584
1585 if (IS_ENABLED(CONFIG_BT_PER_ADV) &&
1586 atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1587 /* If params for per adv has been set, do not allow setting
1588 * connectable, scanable or use legacy adv
1589 */
1590 if (param->options & BT_LE_ADV_OPT_CONNECTABLE ||
1591 param->options & BT_LE_ADV_OPT_SCANNABLE ||
1592 !(param->options & BT_LE_ADV_OPT_EXT_ADV) ||
1593 param->options & BT_LE_ADV_OPT_ANONYMOUS) {
1594 return -EINVAL;
1595 }
1596 }
1597
1598 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1599 return -EINVAL;
1600 }
1601
1602 if (param->id != adv->id) {
1603 atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
1604 }
1605
1606 return le_ext_adv_param_set(adv, param, false);
1607 }
1608
bt_le_ext_adv_start(struct bt_le_ext_adv * adv,struct bt_le_ext_adv_start_param * param)1609 int bt_le_ext_adv_start(struct bt_le_ext_adv *adv,
1610 struct bt_le_ext_adv_start_param *param)
1611 {
1612 struct bt_conn *conn = NULL;
1613 int err;
1614
1615 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1616 return -EALREADY;
1617 }
1618
1619 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1620 atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1621 err = le_adv_start_add_conn(adv, &conn);
1622 if (err) {
1623 return err;
1624 }
1625 }
1626
1627 atomic_set_bit_to(adv->flags, BT_ADV_LIMITED, param &&
1628 (param->timeout > 0 || param->num_events > 0));
1629
1630 if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1631 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1632 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1633 bt_id_set_adv_private_addr(adv);
1634 }
1635 } else {
1636 if (!atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1637 bt_id_set_adv_private_addr(adv);
1638 }
1639 }
1640
1641 if (get_adv_name_type(adv) != ADV_NAME_TYPE_NONE &&
1642 !atomic_test_bit(adv->flags, BT_ADV_DATA_SET)) {
1643 /* Set the advertiser name */
1644 bt_le_ext_adv_set_data(adv, NULL, 0, NULL, 0);
1645 }
1646
1647 err = bt_le_adv_set_enable_ext(adv, true, param);
1648 if (err) {
1649 LOG_ERR("Failed to start advertiser");
1650 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1651 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1652 bt_conn_unref(conn);
1653 }
1654
1655 return err;
1656 }
1657
1658 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1659 /* If undirected connectable advertiser we have created a
1660 * connection object that we don't yet give to the application.
1661 * Since we don't give the application a reference to manage in
1662 * this case, we need to release this reference here
1663 */
1664 bt_conn_unref(conn);
1665 }
1666
1667 return 0;
1668 }
1669
bt_le_ext_adv_stop(struct bt_le_ext_adv * adv)1670 int bt_le_ext_adv_stop(struct bt_le_ext_adv *adv)
1671 {
1672 (void)bt_le_lim_adv_cancel_timeout(adv);
1673
1674 atomic_clear_bit(adv->flags, BT_ADV_PERSIST);
1675
1676 if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1677 return 0;
1678 }
1679
1680 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
1681 bt_id_adv_limited_stopped(adv);
1682
1683 #if defined(CONFIG_BT_SMP)
1684 bt_id_pending_keys_update();
1685 #endif
1686 }
1687
1688 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1689 atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1690 le_adv_stop_free_conn(adv, 0);
1691 }
1692
1693 return bt_le_adv_set_enable_ext(adv, false, NULL);
1694 }
1695
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)1696 int bt_le_ext_adv_set_data(struct bt_le_ext_adv *adv,
1697 const struct bt_data *ad, size_t ad_len,
1698 const struct bt_data *sd, size_t sd_len)
1699 {
1700 bool ext_adv, scannable;
1701
1702 ext_adv = atomic_test_bit(adv->flags, BT_ADV_EXT_ADV);
1703 scannable = atomic_test_bit(adv->flags, BT_ADV_SCANNABLE);
1704
1705 if (ext_adv) {
1706 if ((scannable && ad_len) ||
1707 (!scannable && sd_len)) {
1708 return -ENOTSUP;
1709 }
1710 }
1711
1712 return le_adv_update(adv, ad, ad_len, sd, sd_len, ext_adv, scannable,
1713 get_adv_name_type(adv));
1714 }
1715
bt_le_ext_adv_delete(struct bt_le_ext_adv * adv)1716 int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv)
1717 {
1718 struct bt_hci_cp_le_remove_adv_set *cp;
1719 struct net_buf *buf;
1720 int err;
1721
1722 if (!BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1723 return -ENOTSUP;
1724 }
1725
1726 /* Advertising set should be stopped first */
1727 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1728 return -EINVAL;
1729 }
1730
1731 buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_ADV_SET, sizeof(*cp));
1732 if (!buf) {
1733 LOG_WRN("No HCI buffers");
1734 return -ENOBUFS;
1735 }
1736
1737 cp = net_buf_add(buf, sizeof(*cp));
1738 cp->handle = adv->handle;
1739
1740 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_ADV_SET, buf, NULL);
1741 if (err) {
1742 return err;
1743 }
1744
1745 adv_delete(adv);
1746
1747 return 0;
1748 }
1749 #endif /* defined(CONFIG_BT_EXT_ADV) */
1750
1751
adv_timeout(struct k_work * work)1752 static void adv_timeout(struct k_work *work)
1753 {
1754 int err = 0;
1755 struct k_work_delayable *dwork;
1756 struct bt_le_ext_adv *adv;
1757
1758 dwork = k_work_delayable_from_work(work);
1759 adv = CONTAINER_OF(dwork, struct bt_le_ext_adv, lim_adv_timeout_work);
1760
1761 #if defined(CONFIG_BT_EXT_ADV)
1762 if (adv == bt_dev.adv) {
1763 err = bt_le_adv_stop();
1764 } else {
1765 err = bt_le_ext_adv_stop(adv);
1766 }
1767 #else
1768 err = bt_le_adv_stop();
1769 #endif
1770 if (err) {
1771 LOG_WRN("Failed to stop advertising: %d", err);
1772 }
1773 }
1774
1775 #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)1776 int bt_le_per_adv_set_param(struct bt_le_ext_adv *adv,
1777 const struct bt_le_per_adv_param *param)
1778 {
1779 #if defined(CONFIG_BT_PER_ADV_RSP)
1780 /* The v2 struct can be used even if we end up sending a v1 command
1781 * because they have the same layout for the common fields.
1782 * V2 simply adds fields at the end of the v1 command.
1783 */
1784 struct bt_hci_cp_le_set_per_adv_param_v2 *cp;
1785 #else
1786 struct bt_hci_cp_le_set_per_adv_param *cp;
1787 #endif /* CONFIG_BT_PER_ADV_RSP */
1788
1789 uint16_t opcode;
1790 uint16_t size;
1791 struct net_buf *buf;
1792 int err;
1793 uint16_t props = 0;
1794
1795 if (IS_ENABLED(CONFIG_BT_PER_ADV_RSP) && BT_FEAT_LE_PAWR_ADVERTISER(bt_dev.le.features)) {
1796 opcode = BT_HCI_OP_LE_SET_PER_ADV_PARAM_V2;
1797 size = sizeof(struct bt_hci_cp_le_set_per_adv_param_v2);
1798 } else if (BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1799 opcode = BT_HCI_OP_LE_SET_PER_ADV_PARAM;
1800 size = sizeof(struct bt_hci_cp_le_set_per_adv_param);
1801 } else {
1802 return -ENOTSUP;
1803 }
1804
1805 if (atomic_test_bit(adv->flags, BT_ADV_SCANNABLE)) {
1806 return -EINVAL;
1807 } else if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1808 return -EINVAL;
1809 } else if (!atomic_test_bit(adv->flags, BT_ADV_EXT_ADV)) {
1810 return -EINVAL;
1811 }
1812
1813 if (param->interval_min < BT_GAP_PER_ADV_MIN_INTERVAL ||
1814 param->interval_max > BT_GAP_PER_ADV_MAX_INTERVAL ||
1815 param->interval_min > param->interval_max) {
1816 return -EINVAL;
1817 }
1818
1819 if (!BT_FEAT_LE_PER_ADV_ADI_SUPP(bt_dev.le.features) &&
1820 (param->options & BT_LE_PER_ADV_OPT_INCLUDE_ADI)) {
1821 return -ENOTSUP;
1822 }
1823
1824 buf = bt_hci_cmd_create(opcode, size);
1825 if (!buf) {
1826 return -ENOBUFS;
1827 }
1828
1829 cp = net_buf_add(buf, size);
1830 (void)memset(cp, 0, size);
1831
1832 cp->handle = adv->handle;
1833 cp->min_interval = sys_cpu_to_le16(param->interval_min);
1834 cp->max_interval = sys_cpu_to_le16(param->interval_max);
1835
1836 if (param->options & BT_LE_PER_ADV_OPT_USE_TX_POWER) {
1837 props |= BT_HCI_LE_ADV_PROP_TX_POWER;
1838 }
1839
1840 cp->props = sys_cpu_to_le16(props);
1841
1842 #if defined(CONFIG_BT_PER_ADV_RSP)
1843 if (opcode == BT_HCI_OP_LE_SET_PER_ADV_PARAM_V2) {
1844 cp->num_subevents = param->num_subevents;
1845 cp->subevent_interval = param->subevent_interval;
1846 cp->response_slot_delay = param->response_slot_delay;
1847 cp->response_slot_spacing = param->response_slot_spacing;
1848 cp->num_response_slots = param->num_response_slots;
1849 }
1850 #endif /* CONFIG_BT_PER_ADV_RSP */
1851
1852 err = bt_hci_cmd_send_sync(opcode, buf, NULL);
1853 if (err) {
1854 return err;
1855 }
1856
1857 if (param->options & BT_LE_PER_ADV_OPT_INCLUDE_ADI) {
1858 atomic_set_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI);
1859 } else {
1860 atomic_clear_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI);
1861 }
1862
1863 atomic_set_bit(adv->flags, BT_PER_ADV_PARAMS_SET);
1864
1865 return 0;
1866 }
1867
bt_le_per_adv_set_data(const struct bt_le_ext_adv * adv,const struct bt_data * ad,size_t ad_len)1868 int bt_le_per_adv_set_data(const struct bt_le_ext_adv *adv,
1869 const struct bt_data *ad, size_t ad_len)
1870 {
1871 size_t total_len_bytes = 0;
1872
1873 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1874 return -ENOTSUP;
1875 }
1876
1877 if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1878 return -EINVAL;
1879 }
1880
1881 if (ad_len != 0 && ad == NULL) {
1882 return -EINVAL;
1883 }
1884
1885 for (size_t i = 0; i < ad_len; i++) {
1886 total_len_bytes += ad[i].data_len + 2;
1887 }
1888
1889 if ((total_len_bytes > BT_HCI_LE_PER_ADV_FRAG_MAX_LEN) &&
1890 atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED)) {
1891 /* It is not allowed to set periodic advertising data
1892 * in multiple operations while it is running.
1893 */
1894 return -EINVAL;
1895 }
1896
1897 return hci_set_per_adv_data(adv, ad, ad_len);
1898 }
1899
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)1900 int bt_le_per_adv_set_subevent_data(const struct bt_le_ext_adv *adv, uint8_t num_subevents,
1901 const struct bt_le_per_adv_subevent_data_params *params)
1902 {
1903 struct bt_hci_cp_le_set_pawr_subevent_data *cp;
1904 struct bt_hci_cp_le_set_pawr_subevent_data_element *element;
1905 struct net_buf *buf;
1906 size_t cmd_length = sizeof(*cp);
1907
1908 if (!BT_FEAT_LE_PAWR_ADVERTISER(bt_dev.le.features)) {
1909 return -ENOTSUP;
1910 }
1911
1912 for (size_t i = 0; i < num_subevents; i++) {
1913 cmd_length += sizeof(struct bt_hci_cp_le_set_pawr_subevent_data_element);
1914 cmd_length += params[i].data->len;
1915 }
1916
1917 if (cmd_length > 0xFF) {
1918 return -EINVAL;
1919 }
1920
1921 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_SUBEVENT_DATA, (uint8_t)cmd_length);
1922 if (!buf) {
1923 return -ENOBUFS;
1924 }
1925
1926 cp = net_buf_add(buf, sizeof(*cp));
1927 cp->adv_handle = adv->handle;
1928 cp->num_subevents = num_subevents;
1929
1930 for (size_t i = 0; i < num_subevents; i++) {
1931 element = net_buf_add(buf, sizeof(*element));
1932 element->subevent = params[i].subevent;
1933 element->response_slot_start = params[i].response_slot_start;
1934 element->response_slot_count = params[i].response_slot_count;
1935 element->subevent_data_length = params[i].data->len;
1936 net_buf_add_mem(buf, params[i].data->data, params[i].data->len);
1937 }
1938
1939 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_SUBEVENT_DATA, buf, NULL);
1940 }
1941
bt_le_per_adv_enable(struct bt_le_ext_adv * adv,bool enable)1942 static int bt_le_per_adv_enable(struct bt_le_ext_adv *adv, bool enable)
1943 {
1944 struct bt_hci_cp_le_set_per_adv_enable *cp;
1945 struct net_buf *buf;
1946 struct bt_hci_cmd_state_set state;
1947 int err;
1948
1949 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1950 return -ENOTSUP;
1951 }
1952
1953 /* TODO: We could setup some default ext adv params if not already set*/
1954 if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1955 return -EINVAL;
1956 }
1957
1958 if (atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED) == enable) {
1959 return -EALREADY;
1960 }
1961
1962 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_ENABLE, sizeof(*cp));
1963 if (!buf) {
1964 return -ENOBUFS;
1965 }
1966
1967 cp = net_buf_add(buf, sizeof(*cp));
1968 (void)memset(cp, 0, sizeof(*cp));
1969
1970 cp->handle = adv->handle;
1971
1972 if (enable) {
1973 cp->enable = BT_HCI_LE_SET_PER_ADV_ENABLE_ENABLE;
1974
1975 if (atomic_test_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI)) {
1976 cp->enable |= BT_HCI_LE_SET_PER_ADV_ENABLE_ADI;
1977 }
1978 } else {
1979 cp->enable = 0U;
1980 }
1981
1982 bt_hci_cmd_state_set_init(buf, &state, adv->flags,
1983 BT_PER_ADV_ENABLED, enable);
1984
1985 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_ENABLE, buf, NULL);
1986 if (err) {
1987 return err;
1988 }
1989
1990 return 0;
1991 }
1992
bt_le_per_adv_start(struct bt_le_ext_adv * adv)1993 int bt_le_per_adv_start(struct bt_le_ext_adv *adv)
1994 {
1995 return bt_le_per_adv_enable(adv, true);
1996 }
1997
bt_le_per_adv_stop(struct bt_le_ext_adv * adv)1998 int bt_le_per_adv_stop(struct bt_le_ext_adv *adv)
1999 {
2000 return bt_le_per_adv_enable(adv, false);
2001 }
2002
2003 #if defined(CONFIG_BT_PER_ADV_RSP)
bt_hci_le_per_adv_subevent_data_request(struct net_buf * buf)2004 void bt_hci_le_per_adv_subevent_data_request(struct net_buf *buf)
2005 {
2006 struct bt_hci_evt_le_per_adv_subevent_data_request *evt;
2007 struct bt_le_per_adv_data_request request;
2008 struct bt_le_ext_adv *adv;
2009
2010 if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_subevent_data_request)) {
2011 LOG_ERR("Invalid data request");
2012
2013 return;
2014 }
2015
2016 evt = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_subevent_data_request));
2017 adv = bt_adv_lookup_handle(evt->adv_handle);
2018 if (!adv) {
2019 LOG_ERR("Unknown advertising handle %d", evt->adv_handle);
2020
2021 return;
2022 }
2023
2024 request.start = evt->subevent_start;
2025 request.count = evt->subevent_data_count;
2026
2027 if (adv->cb && adv->cb->pawr_data_request) {
2028 adv->cb->pawr_data_request(adv, &request);
2029 }
2030 }
2031
bt_hci_le_per_adv_response_report(struct net_buf * buf)2032 void bt_hci_le_per_adv_response_report(struct net_buf *buf)
2033 {
2034 struct bt_hci_evt_le_per_adv_response_report *evt;
2035 struct bt_hci_evt_le_per_adv_response *response;
2036 struct bt_le_ext_adv *adv;
2037 struct bt_le_per_adv_response_info info;
2038 struct net_buf_simple data;
2039
2040 if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_response_report)) {
2041 LOG_ERR("Invalid response report");
2042
2043 return;
2044 }
2045
2046 evt = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_response_report));
2047 adv = bt_adv_lookup_handle(evt->adv_handle);
2048 if (!adv) {
2049 LOG_ERR("Unknown advertising handle %d", evt->adv_handle);
2050
2051 return;
2052 }
2053
2054 info.subevent = evt->subevent;
2055 info.tx_status = evt->tx_status;
2056
2057 for (uint8_t i = 0; i < evt->num_responses; i++) {
2058 if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_response)) {
2059 LOG_ERR("Invalid response report");
2060
2061 return;
2062 }
2063
2064 response = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_response));
2065 info.tx_power = response->tx_power;
2066 info.rssi = response->rssi;
2067 info.cte_type = bt_get_df_cte_type(response->cte_type);
2068 info.response_slot = response->response_slot;
2069
2070 if (buf->len < response->data_length) {
2071 LOG_ERR("Invalid response report");
2072
2073 return;
2074 }
2075
2076 if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL) {
2077 LOG_WRN("Incomplete response report received, discarding");
2078 (void)net_buf_pull_mem(buf, response->data_length);
2079 } else if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_RX_FAILED) {
2080 (void)net_buf_pull_mem(buf, response->data_length);
2081
2082 if (adv->cb && adv->cb->pawr_response) {
2083 adv->cb->pawr_response(adv, &info, NULL);
2084 }
2085 } else if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
2086 net_buf_simple_init_with_data(&data,
2087 net_buf_pull_mem(buf, response->data_length),
2088 response->data_length);
2089
2090 if (adv->cb && adv->cb->pawr_response) {
2091 adv->cb->pawr_response(adv, &info, &data);
2092 }
2093 } else {
2094 LOG_ERR("Invalid data status %d", response->data_status);
2095 (void)net_buf_pull_mem(buf, response->data_length);
2096 }
2097 }
2098 }
2099 #endif /* CONFIG_BT_PER_ADV_RSP */
2100
2101 #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)2102 int bt_le_per_adv_set_info_transfer(const struct bt_le_ext_adv *adv,
2103 const struct bt_conn *conn,
2104 uint16_t service_data)
2105 {
2106 struct bt_hci_cp_le_per_adv_set_info_transfer *cp;
2107 struct net_buf *buf;
2108
2109
2110 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2111 return -ENOTSUP;
2112 } else if (!BT_FEAT_LE_PAST_SEND(bt_dev.le.features)) {
2113 return -ENOTSUP;
2114 }
2115
2116 buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_SET_INFO_TRANSFER,
2117 sizeof(*cp));
2118 if (!buf) {
2119 return -ENOBUFS;
2120 }
2121
2122 cp = net_buf_add(buf, sizeof(*cp));
2123 (void)memset(cp, 0, sizeof(*cp));
2124
2125 cp->conn_handle = sys_cpu_to_le16(conn->handle);
2126 cp->adv_handle = adv->handle;
2127 cp->service_data = sys_cpu_to_le16(service_data);
2128
2129 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_SET_INFO_TRANSFER, buf,
2130 NULL);
2131 }
2132 #endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER */
2133 #endif /* CONFIG_BT_PER_ADV */
2134
2135 #if defined(CONFIG_BT_EXT_ADV)
2136 #if defined(CONFIG_BT_BROADCASTER)
bt_hci_le_adv_set_terminated(struct net_buf * buf)2137 void bt_hci_le_adv_set_terminated(struct net_buf *buf)
2138 {
2139 struct bt_hci_evt_le_adv_set_terminated *evt;
2140 struct bt_le_ext_adv *adv;
2141 uint16_t conn_handle;
2142 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2143 bool was_adv_enabled;
2144 #endif
2145
2146 evt = (void *)buf->data;
2147 adv = bt_adv_lookup_handle(evt->adv_handle);
2148 conn_handle = sys_le16_to_cpu(evt->conn_handle);
2149
2150 LOG_DBG("status 0x%02x adv_handle %u conn_handle 0x%02x num %u", evt->status,
2151 evt->adv_handle, conn_handle, evt->num_completed_ext_adv_evts);
2152
2153 if (!adv) {
2154 LOG_ERR("No valid adv");
2155 return;
2156 }
2157
2158 (void)bt_le_lim_adv_cancel_timeout(adv);
2159
2160 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2161 was_adv_enabled = atomic_test_bit(adv->flags, BT_ADV_ENABLED);
2162 #endif
2163
2164 atomic_clear_bit(adv->flags, BT_ADV_ENABLED);
2165
2166 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2167 bt_dev.adv_conn_id = adv->id;
2168 for (int i = 0; i < ARRAY_SIZE(bt_dev.cached_conn_complete); i++) {
2169 if (bt_dev.cached_conn_complete[i].valid &&
2170 bt_dev.cached_conn_complete[i].evt.handle == evt->conn_handle) {
2171 if (was_adv_enabled) {
2172 /* Process the cached connection complete event
2173 * now that the corresponding advertising set is known.
2174 *
2175 * If the advertiser has been stopped before the connection
2176 * complete event has been raised to the application, we
2177 * discard the event.
2178 */
2179 bt_hci_le_enh_conn_complete(&bt_dev.cached_conn_complete[i].evt);
2180 }
2181 bt_dev.cached_conn_complete[i].valid = false;
2182 }
2183 }
2184 #endif
2185
2186 if (evt->status && IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
2187 atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
2188 /* This will call connected callback for high duty cycle
2189 * directed advertiser timeout.
2190 */
2191 le_adv_stop_free_conn(adv, evt->status);
2192 }
2193
2194 if (IS_ENABLED(CONFIG_BT_CONN) && !evt->status) {
2195 struct bt_conn *conn = bt_conn_lookup_handle(conn_handle, BT_CONN_TYPE_LE);
2196
2197 if (conn) {
2198 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
2199 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
2200 /* Set Responder address unless already set */
2201 conn->le.resp_addr.type = BT_ADDR_LE_RANDOM;
2202 if (bt_addr_eq(&conn->le.resp_addr.a, BT_ADDR_ANY)) {
2203 bt_addr_copy(&conn->le.resp_addr.a,
2204 &adv->random_addr.a);
2205 }
2206 } else if (adv->options & BT_LE_ADV_OPT_USE_NRPA) {
2207 bt_addr_le_copy(&conn->le.resp_addr,
2208 &adv->random_addr);
2209 } else {
2210 bt_addr_le_copy(&conn->le.resp_addr,
2211 &bt_dev.id_addr[conn->id]);
2212 }
2213
2214 if (adv->cb && adv->cb->connected) {
2215 struct bt_le_ext_adv_connected_info info = {
2216 .conn = conn,
2217 };
2218
2219 adv->cb->connected(adv, &info);
2220 }
2221
2222 bt_conn_unref(conn);
2223 }
2224 }
2225
2226 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
2227 bt_id_adv_limited_stopped(adv);
2228
2229 #if defined(CONFIG_BT_SMP)
2230 bt_id_pending_keys_update();
2231 #endif
2232
2233 if (adv->cb && adv->cb->sent) {
2234 struct bt_le_ext_adv_sent_info info = {
2235 .num_sent = evt->num_completed_ext_adv_evts,
2236 };
2237
2238 adv->cb->sent(adv, &info);
2239 }
2240 }
2241
2242 if (adv == bt_dev.adv) {
2243 if (atomic_test_bit(adv->flags, BT_ADV_PERSIST)) {
2244 #if defined(CONFIG_BT_PERIPHERAL)
2245 bt_le_adv_resume();
2246 #endif
2247 } else {
2248 bt_le_adv_delete_legacy();
2249 }
2250 }
2251 }
2252
bt_hci_le_scan_req_received(struct net_buf * buf)2253 void bt_hci_le_scan_req_received(struct net_buf *buf)
2254 {
2255 struct bt_hci_evt_le_scan_req_received *evt;
2256 struct bt_le_ext_adv *adv;
2257
2258 evt = (void *)buf->data;
2259 adv = bt_adv_lookup_handle(evt->handle);
2260
2261 LOG_DBG("handle %u peer %s", evt->handle, bt_addr_le_str(&evt->addr));
2262
2263 if (!adv) {
2264 LOG_ERR("No valid adv");
2265 return;
2266 }
2267
2268 if (adv->cb && adv->cb->scanned) {
2269 struct bt_le_ext_adv_scanned_info info;
2270 bt_addr_le_t id_addr;
2271
2272 if (bt_addr_le_is_resolved(&evt->addr)) {
2273 bt_addr_le_copy_resolved(&id_addr, &evt->addr);
2274 } else {
2275 bt_addr_le_copy(&id_addr,
2276 bt_lookup_id_addr(adv->id, &evt->addr));
2277 }
2278
2279 info.addr = &id_addr;
2280 adv->cb->scanned(adv, &info);
2281 }
2282 }
2283 #endif /* defined(CONFIG_BT_BROADCASTER) */
2284 #endif /* defined(CONFIG_BT_EXT_ADV) */
2285