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 				&current_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_v2 *cp;
1125 
1126 	uint16_t opcode;
1127 	uint16_t size;
1128 	bool dir_adv = param->peer != NULL, scannable;
1129 	struct net_buf *buf, *rsp;
1130 	uint8_t own_addr_type;
1131 	int err;
1132 	enum adv_name_type name_type;
1133 	uint16_t props = 0;
1134 
1135 	adv->options = param->options;
1136 
1137 	err = bt_id_set_adv_own_addr(adv, param->options, dir_adv,
1138 				     &own_addr_type);
1139 	if (err) {
1140 		return err;
1141 	}
1142 
1143 	if (dir_adv) {
1144 		bt_addr_le_copy(&adv->target_addr, param->peer);
1145 	} else {
1146 		bt_addr_le_copy(&adv->target_addr, BT_ADDR_LE_ANY);
1147 	}
1148 
1149 	if (IS_ENABLED(CONFIG_BT_EXT_ADV_CODING_SELECTION) &&
1150 	    BT_FEAT_LE_ADV_CODING_SEL(bt_dev.le.features)) {
1151 		opcode = BT_HCI_OP_LE_SET_EXT_ADV_PARAM_V2;
1152 		size = sizeof(struct bt_hci_cp_le_set_ext_adv_param_v2);
1153 	} else {
1154 		opcode = BT_HCI_OP_LE_SET_EXT_ADV_PARAM;
1155 		size = sizeof(struct bt_hci_cp_le_set_ext_adv_param);
1156 	}
1157 
1158 	buf = bt_hci_cmd_create(opcode, size);
1159 	if (!buf) {
1160 		return -ENOBUFS;
1161 	}
1162 
1163 	cp = net_buf_add(buf, size);
1164 	(void)memset(cp, 0, size);
1165 
1166 	cp->handle = adv->handle;
1167 	sys_put_le24(param->interval_min, cp->prim_min_interval);
1168 	sys_put_le24(param->interval_max, cp->prim_max_interval);
1169 	cp->prim_channel_map = get_adv_channel_map(param->options);
1170 	cp->own_addr_type = own_addr_type;
1171 	cp->filter_policy = get_filter_policy(param->options);
1172 	cp->tx_power = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
1173 	cp->prim_adv_phy = BT_HCI_LE_PHY_1M;
1174 
1175 	if ((param->options & BT_LE_ADV_OPT_EXT_ADV) &&
1176 	    !(param->options & BT_LE_ADV_OPT_NO_2M)) {
1177 		cp->sec_adv_phy = BT_HCI_LE_PHY_2M;
1178 	} else {
1179 		cp->sec_adv_phy = BT_HCI_LE_PHY_1M;
1180 	}
1181 
1182 	if (param->options & BT_LE_ADV_OPT_CODED) {
1183 		cp->prim_adv_phy = BT_HCI_LE_PHY_CODED;
1184 		cp->sec_adv_phy = BT_HCI_LE_PHY_CODED;
1185 
1186 		if (IS_ENABLED(CONFIG_BT_EXT_ADV_CODING_SELECTION) &&
1187 		    opcode == BT_HCI_OP_LE_SET_EXT_ADV_PARAM_V2) {
1188 			uint8_t adv_phy_opt;
1189 
1190 			if (param->options & BT_LE_ADV_OPT_REQUIRE_S8_CODING) {
1191 				adv_phy_opt = BT_HCI_LE_ADV_PHY_OPTION_REQUIRE_S8;
1192 			} else if (param->options & BT_LE_ADV_OPT_REQUIRE_S2_CODING) {
1193 				adv_phy_opt = BT_HCI_LE_ADV_PHY_OPTION_REQUIRE_S2;
1194 			} else {
1195 				adv_phy_opt = BT_HCI_LE_ADV_PHY_OPTION_NO_REQUIRED;
1196 			}
1197 
1198 			cp->prim_adv_phy_opt = adv_phy_opt;
1199 			cp->sec_adv_phy_opt = adv_phy_opt;
1200 		}
1201 	}
1202 
1203 	if (!(param->options & BT_LE_ADV_OPT_EXT_ADV)) {
1204 		props |= BT_HCI_LE_ADV_PROP_LEGACY;
1205 	}
1206 
1207 	if (param->options & BT_LE_ADV_OPT_USE_TX_POWER) {
1208 		props |= BT_HCI_LE_ADV_PROP_TX_POWER;
1209 	}
1210 
1211 	if (param->options & BT_LE_ADV_OPT_ANONYMOUS) {
1212 		props |= BT_HCI_LE_ADV_PROP_ANON;
1213 	}
1214 
1215 	if (param->options & BT_LE_ADV_OPT_NOTIFY_SCAN_REQ) {
1216 		cp->scan_req_notify_enable = BT_HCI_LE_ADV_SCAN_REQ_ENABLE;
1217 	}
1218 
1219 	if (param->options & _BT_LE_ADV_OPT_CONNECTABLE) {
1220 		props |= BT_HCI_LE_ADV_PROP_CONN;
1221 		if (!dir_adv && !(param->options & BT_LE_ADV_OPT_EXT_ADV)) {
1222 			/* When using non-extended adv packets then undirected
1223 			 * advertising has to be scannable as well.
1224 			 * We didn't require this option to be set before, so
1225 			 * it is implicitly set instead in this case.
1226 			 */
1227 			props |= BT_HCI_LE_ADV_PROP_SCAN;
1228 		}
1229 	}
1230 
1231 	name_type = get_adv_name_type_param(param);
1232 
1233 	if ((param->options & BT_LE_ADV_OPT_SCANNABLE) || has_scan_data ||
1234 	    (name_type == ADV_NAME_TYPE_SD)) {
1235 		props |= BT_HCI_LE_ADV_PROP_SCAN;
1236 	}
1237 
1238 	scannable = !!(props & BT_HCI_LE_ADV_PROP_SCAN);
1239 
1240 	if (dir_adv) {
1241 		props |= BT_HCI_LE_ADV_PROP_DIRECT;
1242 		if (!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
1243 			props |= BT_HCI_LE_ADV_PROP_HI_DC_CONN;
1244 		}
1245 
1246 		bt_addr_le_copy(&cp->peer_addr, param->peer);
1247 	}
1248 
1249 	cp->sid = param->sid;
1250 
1251 	cp->sec_adv_max_skip = param->secondary_max_skip;
1252 
1253 	cp->props = sys_cpu_to_le16(props);
1254 
1255 	err = bt_hci_cmd_send_sync(opcode, buf, &rsp);
1256 	if (err) {
1257 		return err;
1258 	}
1259 
1260 #if defined(CONFIG_BT_EXT_ADV)
1261 	struct bt_hci_rp_le_set_ext_adv_param *rp = (void *)rsp->data;
1262 
1263 	adv->tx_power = rp->tx_power;
1264 #endif /* defined(CONFIG_BT_EXT_ADV) */
1265 
1266 	net_buf_unref(rsp);
1267 
1268 	atomic_set_bit(adv->flags, BT_ADV_PARAMS_SET);
1269 
1270 	if (atomic_test_and_clear_bit(adv->flags, BT_ADV_RANDOM_ADDR_PENDING)) {
1271 		err = bt_id_set_adv_random_addr(adv, &adv->random_addr.a);
1272 		if (err) {
1273 			return err;
1274 		}
1275 	}
1276 
1277 	/* Flag only used by bt_le_adv_start API. */
1278 	atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, false);
1279 
1280 	atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_AD,
1281 			  name_type == ADV_NAME_TYPE_AD);
1282 
1283 	atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_SD,
1284 			  name_type == ADV_NAME_TYPE_SD);
1285 
1286 	atomic_set_bit_to(adv->flags, BT_ADV_CONNECTABLE,
1287 			  param->options & _BT_LE_ADV_OPT_CONNECTABLE);
1288 
1289 	atomic_set_bit_to(adv->flags, BT_ADV_SCANNABLE, scannable);
1290 
1291 	atomic_set_bit_to(adv->flags, BT_ADV_USE_IDENTITY,
1292 			  param->options & BT_LE_ADV_OPT_USE_IDENTITY);
1293 
1294 	atomic_set_bit_to(adv->flags, BT_ADV_EXT_ADV,
1295 			  param->options & BT_LE_ADV_OPT_EXT_ADV);
1296 
1297 	return 0;
1298 }
1299 
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)1300 int bt_le_adv_start_ext(struct bt_le_ext_adv *adv,
1301 			const struct bt_le_adv_param *param,
1302 			const struct bt_data *ad, size_t ad_len,
1303 			const struct bt_data *sd, size_t sd_len)
1304 {
1305 	struct bt_le_ext_adv_start_param start_param = {
1306 		.timeout = 0,
1307 		.num_events = 0,
1308 	};
1309 	bool dir_adv = (param->peer != NULL);
1310 	struct bt_conn *conn = NULL;
1311 	int err;
1312 
1313 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1314 		return -EAGAIN;
1315 	}
1316 
1317 	if (!valid_adv_param(param)) {
1318 		return -EINVAL;
1319 	}
1320 
1321 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1322 		return -EALREADY;
1323 	}
1324 
1325 	adv->id = param->id;
1326 	err = le_ext_adv_param_set(adv, param, sd != NULL);
1327 	if (err) {
1328 		return err;
1329 	}
1330 
1331 	if (!dir_adv) {
1332 		if (IS_ENABLED(CONFIG_BT_EXT_ADV)) {
1333 			err = bt_le_ext_adv_set_data(adv, ad, ad_len, sd, sd_len);
1334 			if (err) {
1335 				return err;
1336 			}
1337 		}
1338 	} else {
1339 		if (!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
1340 			start_param.timeout =
1341 				BT_GAP_ADV_HIGH_DUTY_CYCLE_MAX_TIMEOUT;
1342 			atomic_set_bit(adv->flags, BT_ADV_LIMITED);
1343 		}
1344 	}
1345 
1346 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1347 	    (param->options & _BT_LE_ADV_OPT_CONNECTABLE)) {
1348 		err = le_adv_start_add_conn(adv, &conn);
1349 		if (err) {
1350 			if (err == -ENOMEM && !dir_adv &&
1351 			    !(param->options & _BT_LE_ADV_OPT_ONE_TIME)) {
1352 				goto set_adv_state;
1353 			}
1354 
1355 			return err;
1356 		}
1357 	}
1358 
1359 	err = bt_le_adv_set_enable_ext(adv, true, &start_param);
1360 	if (err) {
1361 		LOG_ERR("Failed to start advertiser");
1362 		if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1363 			bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1364 			bt_conn_unref(conn);
1365 		}
1366 
1367 		return err;
1368 	}
1369 
1370 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1371 		/* If undirected connectable advertiser we have created a
1372 		 * connection object that we don't yet give to the application.
1373 		 * Since we don't give the application a reference to manage in
1374 		 * this case, we need to release this reference here
1375 		 */
1376 		bt_conn_unref(conn);
1377 	}
1378 
1379 set_adv_state:
1380 	/* Flag always set to false by le_ext_adv_param_set */
1381 	atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, !dir_adv &&
1382 			  !(param->options & _BT_LE_ADV_OPT_ONE_TIME));
1383 
1384 	return 0;
1385 }
1386 
1387 static void adv_timeout(struct k_work *work);
1388 
bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv * adv)1389 int bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv *adv)
1390 {
1391 	return k_work_cancel_delayable(&adv->lim_adv_timeout_work);
1392 }
1393 
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)1394 int bt_le_adv_start(const struct bt_le_adv_param *param,
1395 		    const struct bt_data *ad, size_t ad_len,
1396 		    const struct bt_data *sd, size_t sd_len)
1397 {
1398 	struct bt_le_ext_adv *adv;
1399 	int err;
1400 
1401 	err = adv_create_legacy();
1402 	if (err) {
1403 		return err;
1404 	}
1405 
1406 	adv = bt_le_adv_lookup_legacy();
1407 
1408 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1409 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1410 		err = bt_le_adv_start_ext(adv, param, ad, ad_len, sd, sd_len);
1411 	} else {
1412 		err = bt_le_adv_start_legacy(adv, param, ad, ad_len, sd, sd_len);
1413 	}
1414 
1415 	if (err) {
1416 		bt_le_adv_delete_legacy();
1417 	}
1418 
1419 	if (ad_is_limited(ad, ad_len)) {
1420 		k_work_init_delayable(&adv->lim_adv_timeout_work, adv_timeout);
1421 		k_work_reschedule(&adv->lim_adv_timeout_work,
1422 				  K_SECONDS(CONFIG_BT_LIM_ADV_TIMEOUT));
1423 	}
1424 
1425 	return err;
1426 }
1427 
bt_le_adv_stop(void)1428 int bt_le_adv_stop(void)
1429 {
1430 	struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
1431 	int err;
1432 
1433 	if (!adv) {
1434 		LOG_ERR("No valid legacy adv");
1435 		return 0;
1436 	}
1437 
1438 	(void)bt_le_lim_adv_cancel_timeout(adv);
1439 
1440 	/* Make sure advertising is not re-enabled later even if it's not
1441 	 * currently enabled (i.e. BT_DEV_ADVERTISING is not set).
1442 	 */
1443 	atomic_clear_bit(adv->flags, BT_ADV_PERSIST);
1444 
1445 	if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1446 		/* Legacy advertiser exists, but is not currently advertising.
1447 		 * This happens when keep advertising behavior is active but
1448 		 * no conn object is available to do connectable advertising.
1449 		 */
1450 		bt_le_adv_delete_legacy();
1451 		return 0;
1452 	}
1453 
1454 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1455 	    atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1456 		le_adv_stop_free_conn(adv, 0);
1457 	}
1458 
1459 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1460 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1461 		err = bt_le_adv_set_enable_ext(adv, false, NULL);
1462 		if (err) {
1463 			return err;
1464 		}
1465 	} else {
1466 		err = bt_le_adv_set_enable_legacy(adv, false);
1467 		if (err) {
1468 			return err;
1469 		}
1470 	}
1471 
1472 	bt_le_adv_delete_legacy();
1473 
1474 #if defined(CONFIG_BT_OBSERVER)
1475 	if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1476 	      BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) &&
1477 	    !IS_ENABLED(CONFIG_BT_PRIVACY) &&
1478 	    !IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY)) {
1479 		/* If scan is ongoing set back NRPA */
1480 		if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
1481 			bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
1482 			bt_id_set_private_addr(BT_ID_DEFAULT);
1483 			bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
1484 		}
1485 	}
1486 #endif /* defined(CONFIG_BT_OBSERVER) */
1487 
1488 	return 0;
1489 }
1490 
1491 #if defined(CONFIG_BT_PERIPHERAL)
adv_get_options(const struct bt_le_ext_adv * adv)1492 static uint32_t adv_get_options(const struct bt_le_ext_adv *adv)
1493 {
1494 	uint32_t options = 0;
1495 
1496 	if (!atomic_test_bit(adv->flags, BT_ADV_PERSIST)) {
1497 		options |= _BT_LE_ADV_OPT_ONE_TIME;
1498 	}
1499 
1500 	if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1501 		options |= _BT_LE_ADV_OPT_CONNECTABLE;
1502 	}
1503 
1504 	if (atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1505 		options |= BT_LE_ADV_OPT_USE_IDENTITY;
1506 	}
1507 
1508 	return options;
1509 }
1510 
bt_le_adv_resume(void)1511 void bt_le_adv_resume(void)
1512 {
1513 	struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
1514 	struct bt_conn *conn;
1515 	bool persist_paused = false;
1516 	int err;
1517 
1518 	if (!adv) {
1519 		LOG_DBG("No valid legacy adv");
1520 		return;
1521 	}
1522 
1523 	if (!(atomic_test_bit(adv->flags, BT_ADV_PERSIST) &&
1524 	      !atomic_test_bit(adv->flags, BT_ADV_ENABLED))) {
1525 		return;
1526 	}
1527 
1528 	if (!atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1529 		return;
1530 	}
1531 
1532 	err = le_adv_start_add_conn(adv, &conn);
1533 	if (err) {
1534 		LOG_DBG("Host cannot resume connectable advertising (%d)", err);
1535 		return;
1536 	}
1537 
1538 	LOG_DBG("Resuming connectable advertising");
1539 
1540 	if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1541 	    !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1542 		bt_id_set_adv_private_addr(adv);
1543 	} else {
1544 		uint8_t own_addr_type;
1545 		bool dir_adv = adv_is_directed(adv);
1546 		uint32_t options = adv_get_options(adv);
1547 
1548 		/* Always set the address. Don't assume it has not changed. */
1549 		err = bt_id_set_adv_own_addr(adv, options, dir_adv, &own_addr_type);
1550 		if (err) {
1551 			LOG_ERR("Controller cannot resume connectable advertising (%d)", err);
1552 			return;
1553 		}
1554 	}
1555 
1556 	err = bt_le_adv_set_enable(adv, true);
1557 	if (err) {
1558 		LOG_DBG("Controller cannot resume connectable advertising (%d)", err);
1559 		bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1560 
1561 		/* Temporarily clear persist flag to avoid recursion in
1562 		 * bt_conn_unref if the flag is still set.
1563 		 */
1564 		persist_paused = atomic_test_and_clear_bit(adv->flags,
1565 							   BT_ADV_PERSIST);
1566 	}
1567 
1568 	/* Since we don't give the application a reference to manage in
1569 	 * this case, we need to release this reference here.
1570 	 */
1571 	bt_conn_unref(conn);
1572 	if (persist_paused) {
1573 		atomic_set_bit(adv->flags, BT_ADV_PERSIST);
1574 	}
1575 }
1576 #endif /* defined(CONFIG_BT_PERIPHERAL) */
1577 
1578 #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)1579 int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
1580 			   struct bt_le_ext_adv_info *info)
1581 {
1582 	info->id = adv->id;
1583 	info->tx_power = adv->tx_power;
1584 	info->addr = &adv->random_addr;
1585 
1586 	return 0;
1587 }
1588 
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)1589 int bt_le_ext_adv_create(const struct bt_le_adv_param *param,
1590 			 const struct bt_le_ext_adv_cb *cb,
1591 			 struct bt_le_ext_adv **out_adv)
1592 {
1593 	struct bt_le_ext_adv *adv;
1594 	int err;
1595 
1596 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1597 		return -EAGAIN;
1598 	}
1599 
1600 	CHECKIF(out_adv == NULL) {
1601 		LOG_DBG("out_adv is NULL");
1602 
1603 		return -EINVAL;
1604 	}
1605 
1606 	if (!valid_adv_ext_param(param)) {
1607 		return -EINVAL;
1608 	}
1609 
1610 	adv = adv_new();
1611 	if (!adv) {
1612 		return -ENOMEM;
1613 	}
1614 
1615 	adv->id = param->id;
1616 	adv->cb = cb;
1617 
1618 	err = le_ext_adv_param_set(adv, param, false);
1619 	if (err) {
1620 		adv_delete(adv);
1621 		return err;
1622 	}
1623 
1624 	*out_adv = adv;
1625 	return 0;
1626 }
1627 
bt_le_ext_adv_update_param(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param)1628 int bt_le_ext_adv_update_param(struct bt_le_ext_adv *adv,
1629 			       const struct bt_le_adv_param *param)
1630 {
1631 	CHECKIF(adv == NULL) {
1632 		LOG_DBG("adv is NULL");
1633 
1634 		return -EINVAL;
1635 	}
1636 
1637 	if (!valid_adv_ext_param(param)) {
1638 		return -EINVAL;
1639 	}
1640 
1641 	if (IS_ENABLED(CONFIG_BT_PER_ADV) &&
1642 	    atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1643 		/* If params for per adv has been set, do not allow setting
1644 		 * connectable, scanable or use legacy adv
1645 		 */
1646 		if (param->options & _BT_LE_ADV_OPT_CONNECTABLE ||
1647 		    param->options & BT_LE_ADV_OPT_SCANNABLE ||
1648 		    !(param->options & BT_LE_ADV_OPT_EXT_ADV) ||
1649 		    param->options & BT_LE_ADV_OPT_ANONYMOUS) {
1650 			return -EINVAL;
1651 		}
1652 	}
1653 
1654 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1655 		return -EINVAL;
1656 	}
1657 
1658 	if (param->id != adv->id) {
1659 		atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
1660 	}
1661 
1662 	return le_ext_adv_param_set(adv, param, false);
1663 }
1664 
bt_le_ext_adv_start(struct bt_le_ext_adv * adv,const struct bt_le_ext_adv_start_param * param)1665 int bt_le_ext_adv_start(struct bt_le_ext_adv *adv,
1666 			const struct bt_le_ext_adv_start_param *param)
1667 {
1668 	struct bt_conn *conn = NULL;
1669 	int err;
1670 
1671 	CHECKIF(adv == NULL) {
1672 		LOG_DBG("adv is NULL");
1673 
1674 		return -EINVAL;
1675 	}
1676 
1677 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1678 		return -EALREADY;
1679 	}
1680 
1681 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1682 	    atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1683 		err = le_adv_start_add_conn(adv, &conn);
1684 		if (err) {
1685 			return err;
1686 		}
1687 	}
1688 
1689 	atomic_set_bit_to(adv->flags, BT_ADV_LIMITED, param &&
1690 			  (param->timeout > 0 || param->num_events > 0));
1691 
1692 	if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1693 		if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1694 		    !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1695 			bt_id_set_adv_private_addr(adv);
1696 		}
1697 	} else {
1698 		if (!atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1699 			bt_id_set_adv_private_addr(adv);
1700 		}
1701 	}
1702 
1703 	if (get_adv_name_type(adv) != ADV_NAME_TYPE_NONE &&
1704 	    !atomic_test_bit(adv->flags, BT_ADV_DATA_SET)) {
1705 		/* Set the advertiser name */
1706 		bt_le_ext_adv_set_data(adv, NULL, 0, NULL, 0);
1707 	}
1708 
1709 	err = bt_le_adv_set_enable_ext(adv, true, param);
1710 	if (err) {
1711 		LOG_ERR("Failed to start advertiser");
1712 		if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1713 			bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1714 			bt_conn_unref(conn);
1715 		}
1716 
1717 		return err;
1718 	}
1719 
1720 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1721 		/* If undirected connectable advertiser we have created a
1722 		 * connection object that we don't yet give to the application.
1723 		 * Since we don't give the application a reference to manage in
1724 		 * this case, we need to release this reference here
1725 		 */
1726 		bt_conn_unref(conn);
1727 	}
1728 
1729 	return 0;
1730 }
1731 
bt_le_ext_adv_stop(struct bt_le_ext_adv * adv)1732 int bt_le_ext_adv_stop(struct bt_le_ext_adv *adv)
1733 {
1734 	CHECKIF(adv == NULL) {
1735 		LOG_DBG("adv is NULL");
1736 
1737 		return -EINVAL;
1738 	}
1739 
1740 	(void)bt_le_lim_adv_cancel_timeout(adv);
1741 
1742 	atomic_clear_bit(adv->flags, BT_ADV_PERSIST);
1743 
1744 	if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1745 		return 0;
1746 	}
1747 
1748 	if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
1749 		bt_id_adv_limited_stopped(adv);
1750 
1751 #if defined(CONFIG_BT_SMP)
1752 		bt_id_pending_keys_update();
1753 #endif
1754 	}
1755 
1756 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1757 	    atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1758 		le_adv_stop_free_conn(adv, 0);
1759 	}
1760 
1761 	return bt_le_adv_set_enable_ext(adv, false, NULL);
1762 }
1763 
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)1764 int bt_le_ext_adv_set_data(struct bt_le_ext_adv *adv,
1765 			   const struct bt_data *ad, size_t ad_len,
1766 			   const struct bt_data *sd, size_t sd_len)
1767 {
1768 	bool ext_adv, scannable;
1769 
1770 	CHECKIF(adv == NULL) {
1771 		LOG_DBG("adv is NULL");
1772 
1773 		return -EINVAL;
1774 	}
1775 
1776 	ext_adv = atomic_test_bit(adv->flags, BT_ADV_EXT_ADV);
1777 	scannable = atomic_test_bit(adv->flags, BT_ADV_SCANNABLE);
1778 
1779 	if (ext_adv) {
1780 		if ((scannable && ad_len) ||
1781 		    (!scannable && sd_len)) {
1782 			return -ENOTSUP;
1783 		}
1784 	}
1785 
1786 	return le_adv_update(adv, ad, ad_len, sd, sd_len, ext_adv, scannable,
1787 			     get_adv_name_type(adv));
1788 }
1789 
bt_le_ext_adv_delete(struct bt_le_ext_adv * adv)1790 int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv)
1791 {
1792 	struct bt_hci_cp_le_remove_adv_set *cp;
1793 	struct net_buf *buf;
1794 	int err;
1795 
1796 	if (!BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1797 		return -ENOTSUP;
1798 	}
1799 
1800 	CHECKIF(adv == NULL) {
1801 		LOG_DBG("adv is NULL");
1802 
1803 		return -EINVAL;
1804 	}
1805 
1806 	/* Advertising set should be stopped first */
1807 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1808 		return -EINVAL;
1809 	}
1810 
1811 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_ADV_SET, sizeof(*cp));
1812 	if (!buf) {
1813 		LOG_WRN("No HCI buffers");
1814 		return -ENOBUFS;
1815 	}
1816 
1817 	cp = net_buf_add(buf, sizeof(*cp));
1818 	cp->handle = adv->handle;
1819 
1820 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_ADV_SET, buf, NULL);
1821 	if (err) {
1822 		return err;
1823 	}
1824 
1825 	adv_delete(adv);
1826 
1827 	return 0;
1828 }
1829 #endif /* defined(CONFIG_BT_EXT_ADV) */
1830 
1831 
adv_timeout(struct k_work * work)1832 static void adv_timeout(struct k_work *work)
1833 {
1834 	int err = 0;
1835 	struct k_work_delayable *dwork;
1836 	struct bt_le_ext_adv *adv;
1837 
1838 	dwork = k_work_delayable_from_work(work);
1839 	adv = CONTAINER_OF(dwork, struct bt_le_ext_adv, lim_adv_timeout_work);
1840 
1841 #if defined(CONFIG_BT_EXT_ADV)
1842 	if (adv == bt_dev.adv) {
1843 		err = bt_le_adv_stop();
1844 	} else {
1845 		err = bt_le_ext_adv_stop(adv);
1846 	}
1847 #else
1848 	err = bt_le_adv_stop();
1849 #endif
1850 	if (err) {
1851 		LOG_WRN("Failed to stop advertising: %d", err);
1852 	}
1853 }
1854 
1855 #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)1856 int bt_le_per_adv_set_param(struct bt_le_ext_adv *adv,
1857 			    const struct bt_le_per_adv_param *param)
1858 {
1859 #if defined(CONFIG_BT_PER_ADV_RSP)
1860 	/* The v2 struct can be used even if we end up sending a v1 command
1861 	 * because they have the same layout for the common fields.
1862 	 * V2 simply adds fields at the end of the v1 command.
1863 	 */
1864 	struct bt_hci_cp_le_set_per_adv_param_v2 *cp;
1865 #else
1866 	struct bt_hci_cp_le_set_per_adv_param *cp;
1867 #endif /* CONFIG_BT_PER_ADV_RSP */
1868 
1869 	uint16_t opcode;
1870 	uint16_t size;
1871 	struct net_buf *buf;
1872 	int err;
1873 	uint16_t props = 0;
1874 
1875 	if (IS_ENABLED(CONFIG_BT_PER_ADV_RSP) && BT_FEAT_LE_PAWR_ADVERTISER(bt_dev.le.features)) {
1876 		opcode = BT_HCI_OP_LE_SET_PER_ADV_PARAM_V2;
1877 		size = sizeof(struct bt_hci_cp_le_set_per_adv_param_v2);
1878 	} else if (BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1879 		opcode = BT_HCI_OP_LE_SET_PER_ADV_PARAM;
1880 		size = sizeof(struct bt_hci_cp_le_set_per_adv_param);
1881 	} else {
1882 		return -ENOTSUP;
1883 	}
1884 
1885 	CHECKIF(adv == NULL) {
1886 		LOG_DBG("adv is NULL");
1887 
1888 		return -EINVAL;
1889 	}
1890 
1891 	if (atomic_test_bit(adv->flags, BT_ADV_SCANNABLE)) {
1892 		return -EINVAL;
1893 	} else if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1894 		return -EINVAL;
1895 	} else if (!atomic_test_bit(adv->flags, BT_ADV_EXT_ADV)) {
1896 		return -EINVAL;
1897 	}
1898 
1899 	if (param->interval_min < BT_GAP_PER_ADV_MIN_INTERVAL ||
1900 	    param->interval_max > BT_GAP_PER_ADV_MAX_INTERVAL ||
1901 	    param->interval_min > param->interval_max) {
1902 		return -EINVAL;
1903 	}
1904 
1905 	if (!BT_FEAT_LE_PER_ADV_ADI_SUPP(bt_dev.le.features) &&
1906 	    (param->options & BT_LE_PER_ADV_OPT_INCLUDE_ADI)) {
1907 		return -ENOTSUP;
1908 	}
1909 
1910 	buf = bt_hci_cmd_create(opcode, size);
1911 	if (!buf) {
1912 		return -ENOBUFS;
1913 	}
1914 
1915 	cp = net_buf_add(buf, size);
1916 	(void)memset(cp, 0, size);
1917 
1918 	cp->handle = adv->handle;
1919 	cp->min_interval = sys_cpu_to_le16(param->interval_min);
1920 	cp->max_interval = sys_cpu_to_le16(param->interval_max);
1921 
1922 	if (param->options & BT_LE_PER_ADV_OPT_USE_TX_POWER) {
1923 		props |= BT_HCI_LE_ADV_PROP_TX_POWER;
1924 	}
1925 
1926 	cp->props = sys_cpu_to_le16(props);
1927 
1928 #if defined(CONFIG_BT_PER_ADV_RSP)
1929 	if (opcode == BT_HCI_OP_LE_SET_PER_ADV_PARAM_V2) {
1930 		cp->num_subevents = param->num_subevents;
1931 		cp->subevent_interval = param->subevent_interval;
1932 		cp->response_slot_delay = param->response_slot_delay;
1933 		cp->response_slot_spacing = param->response_slot_spacing;
1934 		cp->num_response_slots = param->num_response_slots;
1935 	}
1936 #endif /* CONFIG_BT_PER_ADV_RSP */
1937 
1938 	err = bt_hci_cmd_send_sync(opcode, buf, NULL);
1939 	if (err) {
1940 		return err;
1941 	}
1942 
1943 	if (param->options & BT_LE_PER_ADV_OPT_INCLUDE_ADI) {
1944 		atomic_set_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI);
1945 	} else {
1946 		atomic_clear_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI);
1947 	}
1948 
1949 	atomic_set_bit(adv->flags, BT_PER_ADV_PARAMS_SET);
1950 
1951 	return 0;
1952 }
1953 
bt_le_per_adv_set_data(const struct bt_le_ext_adv * adv,const struct bt_data * ad,size_t ad_len)1954 int bt_le_per_adv_set_data(const struct bt_le_ext_adv *adv,
1955 			   const struct bt_data *ad, size_t ad_len)
1956 {
1957 	size_t total_len_bytes = 0;
1958 
1959 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1960 		return -ENOTSUP;
1961 	}
1962 
1963 	CHECKIF(adv == NULL) {
1964 		LOG_DBG("adv is NULL");
1965 
1966 		return -EINVAL;
1967 	}
1968 
1969 	if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1970 		return -EINVAL;
1971 	}
1972 
1973 	if (ad_len != 0 && ad == NULL) {
1974 		return -EINVAL;
1975 	}
1976 
1977 	for (size_t i = 0; i < ad_len; i++) {
1978 		total_len_bytes += ad[i].data_len + 2;
1979 	}
1980 
1981 	if ((total_len_bytes > BT_HCI_LE_PER_ADV_FRAG_MAX_LEN) &&
1982 	    atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED)) {
1983 		/* It is not allowed to set periodic advertising data
1984 		 * in multiple operations while it is running.
1985 		 */
1986 		return -EINVAL;
1987 	}
1988 
1989 	return hci_set_per_adv_data(adv, ad, ad_len);
1990 }
1991 
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)1992 int bt_le_per_adv_set_subevent_data(const struct bt_le_ext_adv *adv, uint8_t num_subevents,
1993 				    const struct bt_le_per_adv_subevent_data_params *params)
1994 {
1995 	struct bt_hci_cp_le_set_pawr_subevent_data *cp;
1996 	struct bt_hci_cp_le_set_pawr_subevent_data_element *element;
1997 	struct net_buf *buf;
1998 	size_t cmd_length = sizeof(*cp);
1999 
2000 	if (!BT_FEAT_LE_PAWR_ADVERTISER(bt_dev.le.features)) {
2001 		return -ENOTSUP;
2002 	}
2003 
2004 	CHECKIF(adv == NULL) {
2005 		LOG_DBG("adv is NULL");
2006 
2007 		return -EINVAL;
2008 	}
2009 
2010 	for (size_t i = 0; i < num_subevents; i++) {
2011 		cmd_length += sizeof(struct bt_hci_cp_le_set_pawr_subevent_data_element);
2012 		cmd_length += params[i].data->len;
2013 	}
2014 
2015 	if (cmd_length > 0xFF) {
2016 		return -EINVAL;
2017 	}
2018 
2019 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_SUBEVENT_DATA, (uint8_t)cmd_length);
2020 	if (!buf) {
2021 		return -ENOBUFS;
2022 	}
2023 
2024 	cp = net_buf_add(buf, sizeof(*cp));
2025 	cp->adv_handle = adv->handle;
2026 	cp->num_subevents = num_subevents;
2027 
2028 	for (size_t i = 0; i < num_subevents; i++) {
2029 		element = net_buf_add(buf, sizeof(*element));
2030 		element->subevent = params[i].subevent;
2031 		element->response_slot_start = params[i].response_slot_start;
2032 		element->response_slot_count = params[i].response_slot_count;
2033 		element->subevent_data_length = params[i].data->len;
2034 		net_buf_add_mem(buf, params[i].data->data, params[i].data->len);
2035 	}
2036 
2037 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_SUBEVENT_DATA, buf, NULL);
2038 }
2039 
bt_le_per_adv_enable(struct bt_le_ext_adv * adv,bool enable)2040 static int bt_le_per_adv_enable(struct bt_le_ext_adv *adv, bool enable)
2041 {
2042 	struct bt_hci_cp_le_set_per_adv_enable *cp;
2043 	struct net_buf *buf;
2044 	struct bt_hci_cmd_state_set state;
2045 	int err;
2046 
2047 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2048 		return -ENOTSUP;
2049 	}
2050 
2051 	CHECKIF(adv == NULL) {
2052 		LOG_DBG("adv is NULL");
2053 
2054 		return -EINVAL;
2055 	}
2056 
2057 	/* TODO: We could setup some default ext adv params if not already set*/
2058 	if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
2059 		return -EINVAL;
2060 	}
2061 
2062 	if (atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED) == enable) {
2063 		return -EALREADY;
2064 	}
2065 
2066 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_ENABLE, sizeof(*cp));
2067 	if (!buf) {
2068 		return -ENOBUFS;
2069 	}
2070 
2071 	cp = net_buf_add(buf, sizeof(*cp));
2072 	(void)memset(cp, 0, sizeof(*cp));
2073 
2074 	cp->handle = adv->handle;
2075 
2076 	if (enable) {
2077 		cp->enable = BT_HCI_LE_SET_PER_ADV_ENABLE_ENABLE;
2078 
2079 		if (atomic_test_bit(adv->flags, BT_PER_ADV_INCLUDE_ADI)) {
2080 			cp->enable |= BT_HCI_LE_SET_PER_ADV_ENABLE_ADI;
2081 		}
2082 	} else {
2083 		cp->enable = 0U;
2084 	}
2085 
2086 	bt_hci_cmd_state_set_init(buf, &state, adv->flags,
2087 				  BT_PER_ADV_ENABLED, enable);
2088 
2089 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_ENABLE, buf, NULL);
2090 	if (err) {
2091 		return err;
2092 	}
2093 
2094 	return 0;
2095 }
2096 
bt_le_per_adv_start(struct bt_le_ext_adv * adv)2097 int bt_le_per_adv_start(struct bt_le_ext_adv *adv)
2098 {
2099 	return bt_le_per_adv_enable(adv, true);
2100 }
2101 
bt_le_per_adv_stop(struct bt_le_ext_adv * adv)2102 int bt_le_per_adv_stop(struct bt_le_ext_adv *adv)
2103 {
2104 	return bt_le_per_adv_enable(adv, false);
2105 }
2106 
2107 #if defined(CONFIG_BT_PER_ADV_RSP)
bt_hci_le_per_adv_subevent_data_request(struct net_buf * buf)2108 void bt_hci_le_per_adv_subevent_data_request(struct net_buf *buf)
2109 {
2110 	struct bt_hci_evt_le_per_adv_subevent_data_request *evt;
2111 	struct bt_le_per_adv_data_request request;
2112 	struct bt_le_ext_adv *adv;
2113 
2114 	if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_subevent_data_request)) {
2115 		LOG_ERR("Invalid data request");
2116 
2117 		return;
2118 	}
2119 
2120 	evt = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_subevent_data_request));
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 	request.start = evt->subevent_start;
2129 	request.count = evt->subevent_data_count;
2130 
2131 	if (adv->cb && adv->cb->pawr_data_request) {
2132 		adv->cb->pawr_data_request(adv, &request);
2133 	}
2134 }
2135 
bt_hci_le_per_adv_response_report(struct net_buf * buf)2136 void bt_hci_le_per_adv_response_report(struct net_buf *buf)
2137 {
2138 	struct bt_hci_evt_le_per_adv_response_report *evt;
2139 	struct bt_hci_evt_le_per_adv_response *response;
2140 	struct bt_le_ext_adv *adv;
2141 	struct bt_le_per_adv_response_info info;
2142 	struct net_buf_simple data;
2143 
2144 	if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_response_report)) {
2145 		LOG_ERR("Invalid response report");
2146 
2147 		return;
2148 	}
2149 
2150 	evt = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_response_report));
2151 	adv = bt_hci_adv_lookup_handle(evt->adv_handle);
2152 	if (!adv) {
2153 		LOG_ERR("Unknown advertising handle %d", evt->adv_handle);
2154 
2155 		return;
2156 	}
2157 
2158 	info.subevent = evt->subevent;
2159 	info.tx_status = evt->tx_status;
2160 
2161 	for (uint8_t i = 0; i < evt->num_responses; i++) {
2162 		if (buf->len < sizeof(struct bt_hci_evt_le_per_adv_response)) {
2163 			LOG_ERR("Invalid response report");
2164 
2165 			return;
2166 		}
2167 
2168 		response = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_response));
2169 		info.tx_power = response->tx_power;
2170 		info.rssi = response->rssi;
2171 		info.cte_type = bt_get_df_cte_type(response->cte_type);
2172 		info.response_slot = response->response_slot;
2173 
2174 		if (buf->len < response->data_length) {
2175 			LOG_ERR("Invalid response report");
2176 
2177 			return;
2178 		}
2179 
2180 		if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL) {
2181 			LOG_WRN("Incomplete response report received, discarding");
2182 			(void)net_buf_pull_mem(buf, response->data_length);
2183 		} else if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_RX_FAILED) {
2184 			(void)net_buf_pull_mem(buf, response->data_length);
2185 
2186 			if (adv->cb && adv->cb->pawr_response) {
2187 				adv->cb->pawr_response(adv, &info, NULL);
2188 			}
2189 		} else if (response->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
2190 			net_buf_simple_init_with_data(&data,
2191 						      net_buf_pull_mem(buf, response->data_length),
2192 						      response->data_length);
2193 
2194 			if (adv->cb && adv->cb->pawr_response) {
2195 				adv->cb->pawr_response(adv, &info, &data);
2196 			}
2197 		} else {
2198 			LOG_ERR("Invalid data status %d", response->data_status);
2199 			(void)net_buf_pull_mem(buf, response->data_length);
2200 		}
2201 	}
2202 }
2203 #endif /* CONFIG_BT_PER_ADV_RSP */
2204 
2205 #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)2206 int bt_le_per_adv_set_info_transfer(const struct bt_le_ext_adv *adv,
2207 				    const struct bt_conn *conn,
2208 				    uint16_t service_data)
2209 {
2210 	struct bt_hci_cp_le_per_adv_set_info_transfer *cp;
2211 	struct net_buf *buf;
2212 
2213 
2214 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2215 		return -ENOTSUP;
2216 	} else if (!BT_FEAT_LE_PAST_SEND(bt_dev.le.features)) {
2217 		return -ENOTSUP;
2218 	}
2219 
2220 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_SET_INFO_TRANSFER,
2221 				sizeof(*cp));
2222 	if (!buf) {
2223 		return -ENOBUFS;
2224 	}
2225 
2226 	cp = net_buf_add(buf, sizeof(*cp));
2227 	(void)memset(cp, 0, sizeof(*cp));
2228 
2229 	cp->conn_handle = sys_cpu_to_le16(conn->handle);
2230 	cp->adv_handle = adv->handle;
2231 	cp->service_data = sys_cpu_to_le16(service_data);
2232 
2233 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_SET_INFO_TRANSFER, buf,
2234 				    NULL);
2235 }
2236 #endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER */
2237 #endif /* CONFIG_BT_PER_ADV */
2238 
2239 #if defined(CONFIG_BT_EXT_ADV)
2240 #if defined(CONFIG_BT_BROADCASTER)
bt_hci_le_adv_set_terminated(struct net_buf * buf)2241 void bt_hci_le_adv_set_terminated(struct net_buf *buf)
2242 {
2243 	struct bt_hci_evt_le_adv_set_terminated *evt;
2244 	struct bt_le_ext_adv *adv;
2245 	uint16_t conn_handle;
2246 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2247 	bool was_adv_enabled;
2248 #endif
2249 
2250 	evt = (void *)buf->data;
2251 	adv = bt_hci_adv_lookup_handle(evt->adv_handle);
2252 	conn_handle = sys_le16_to_cpu(evt->conn_handle);
2253 
2254 	LOG_DBG("status 0x%02x %s adv_handle %u conn_handle 0x%02x num %u",
2255 		evt->status, bt_hci_err_to_str(evt->status),
2256 		evt->adv_handle, conn_handle, evt->num_completed_ext_adv_evts);
2257 
2258 	if (!adv) {
2259 		LOG_ERR("No valid adv");
2260 		return;
2261 	}
2262 
2263 	(void)bt_le_lim_adv_cancel_timeout(adv);
2264 
2265 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2266 	was_adv_enabled = atomic_test_bit(adv->flags, BT_ADV_ENABLED);
2267 #endif
2268 
2269 	atomic_clear_bit(adv->flags, BT_ADV_ENABLED);
2270 
2271 #if defined(CONFIG_BT_CONN) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
2272 	bt_dev.adv_conn_id = adv->id;
2273 	for (int i = 0; i < ARRAY_SIZE(bt_dev.cached_conn_complete); i++) {
2274 		if (bt_dev.cached_conn_complete[i].valid &&
2275 		    bt_dev.cached_conn_complete[i].evt.handle == evt->conn_handle) {
2276 			if (was_adv_enabled) {
2277 				/* Process the cached connection complete event
2278 				 * now that the corresponding advertising set is known.
2279 				 *
2280 				 * If the advertiser has been stopped before the connection
2281 				 * complete event has been raised to the application, we
2282 				 * discard the event.
2283 				 */
2284 				bt_hci_le_enh_conn_complete(&bt_dev.cached_conn_complete[i].evt);
2285 			}
2286 			bt_dev.cached_conn_complete[i].valid = false;
2287 		}
2288 	}
2289 #endif
2290 
2291 	if (evt->status && IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
2292 	    atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
2293 		/* This will call connected callback for high duty cycle
2294 		 * directed advertiser timeout.
2295 		 */
2296 		le_adv_stop_free_conn(adv, evt->status);
2297 	}
2298 
2299 	if (IS_ENABLED(CONFIG_BT_CONN) && !evt->status) {
2300 		struct bt_conn *conn = bt_conn_lookup_handle(conn_handle, BT_CONN_TYPE_LE);
2301 
2302 		if (conn) {
2303 			if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
2304 			    !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
2305 				/* Set Responder address unless already set */
2306 				conn->le.resp_addr.type = BT_ADDR_LE_RANDOM;
2307 				if (bt_addr_eq(&conn->le.resp_addr.a, BT_ADDR_ANY)) {
2308 					bt_addr_copy(&conn->le.resp_addr.a,
2309 						     &adv->random_addr.a);
2310 				}
2311 			} else if (adv->options & BT_LE_ADV_OPT_USE_NRPA) {
2312 				bt_addr_le_copy(&conn->le.resp_addr,
2313 						&adv->random_addr);
2314 			} else {
2315 				bt_addr_le_copy(&conn->le.resp_addr,
2316 					&bt_dev.id_addr[conn->id]);
2317 			}
2318 
2319 			if (adv->cb && adv->cb->connected) {
2320 				struct bt_le_ext_adv_connected_info info = {
2321 					.conn = conn,
2322 				};
2323 
2324 				adv->cb->connected(adv, &info);
2325 			}
2326 
2327 			bt_conn_unref(conn);
2328 		}
2329 	}
2330 
2331 	if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
2332 		bt_id_adv_limited_stopped(adv);
2333 
2334 #if defined(CONFIG_BT_SMP)
2335 		bt_id_pending_keys_update();
2336 #endif
2337 
2338 		if (adv->cb && adv->cb->sent) {
2339 			struct bt_le_ext_adv_sent_info info = {
2340 				.num_sent = evt->num_completed_ext_adv_evts,
2341 			};
2342 
2343 			adv->cb->sent(adv, &info);
2344 		}
2345 	}
2346 
2347 	if (adv == bt_dev.adv) {
2348 		if (atomic_test_bit(adv->flags, BT_ADV_PERSIST)) {
2349 #if defined(CONFIG_BT_PERIPHERAL)
2350 			bt_le_adv_resume();
2351 #endif
2352 		} else {
2353 			bt_le_adv_delete_legacy();
2354 		}
2355 	}
2356 }
2357 
bt_hci_le_scan_req_received(struct net_buf * buf)2358 void bt_hci_le_scan_req_received(struct net_buf *buf)
2359 {
2360 	struct bt_hci_evt_le_scan_req_received *evt;
2361 	struct bt_le_ext_adv *adv;
2362 
2363 	evt = (void *)buf->data;
2364 	adv = bt_hci_adv_lookup_handle(evt->handle);
2365 
2366 	LOG_DBG("handle %u peer %s", evt->handle, bt_addr_le_str(&evt->addr));
2367 
2368 	if (!adv) {
2369 		LOG_ERR("No valid adv");
2370 		return;
2371 	}
2372 
2373 	if (adv->cb && adv->cb->scanned) {
2374 		struct bt_le_ext_adv_scanned_info info;
2375 		bt_addr_le_t id_addr;
2376 
2377 		if (bt_addr_le_is_resolved(&evt->addr)) {
2378 			bt_addr_le_copy_resolved(&id_addr, &evt->addr);
2379 		} else {
2380 			bt_addr_le_copy(&id_addr,
2381 					bt_lookup_id_addr(adv->id, &evt->addr));
2382 		}
2383 
2384 		info.addr = &id_addr;
2385 		adv->cb->scanned(adv, &info);
2386 	}
2387 }
2388 #endif /* defined(CONFIG_BT_BROADCASTER) */
2389 #endif /* defined(CONFIG_BT_EXT_ADV) */
2390