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 <sys/byteorder.h>
10 
11 #include <bluetooth/bluetooth.h>
12 #include <bluetooth/hci.h>
13 #include <bluetooth/buf.h>
14 
15 #include "hci_core.h"
16 #include "conn_internal.h"
17 #include "id.h"
18 #include "scan.h"
19 
20 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_CORE)
21 #define LOG_MODULE_NAME bt_adv
22 #include "common/log.h"
23 
24 enum adv_name_type {
25 	ADV_NAME_TYPE_NONE,
26 	ADV_NAME_TYPE_AD,
27 	ADV_NAME_TYPE_SD,
28 };
29 
get_adv_name_type(const struct bt_le_ext_adv * adv)30 enum adv_name_type get_adv_name_type(const struct bt_le_ext_adv *adv)
31 {
32 	if (atomic_test_bit(adv->flags, BT_ADV_INCLUDE_NAME_SD)) {
33 		return ADV_NAME_TYPE_SD;
34 	}
35 
36 	if (atomic_test_bit(adv->flags, BT_ADV_INCLUDE_NAME_AD)) {
37 		return ADV_NAME_TYPE_AD;
38 	}
39 
40 	return ADV_NAME_TYPE_NONE;
41 }
42 
get_adv_name_type_param(const struct bt_le_adv_param * param)43 enum adv_name_type get_adv_name_type_param(const struct bt_le_adv_param *param)
44 {
45 	if (param->options & BT_LE_ADV_OPT_USE_NAME) {
46 		if (param->options & BT_LE_ADV_OPT_FORCE_NAME_IN_AD) {
47 			return ADV_NAME_TYPE_AD;
48 		}
49 
50 		if ((param->options & BT_LE_ADV_OPT_EXT_ADV) &&
51 		    !(param->options & BT_LE_ADV_OPT_SCANNABLE)) {
52 			return ADV_NAME_TYPE_AD;
53 		}
54 
55 		return ADV_NAME_TYPE_SD;
56 	}
57 
58 	return ADV_NAME_TYPE_NONE;
59 }
60 
61 #if defined(CONFIG_BT_EXT_ADV)
62 static struct bt_le_ext_adv adv_pool[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
63 #endif /* defined(CONFIG_BT_EXT_ADV) */
64 
65 
66 #if defined(CONFIG_BT_EXT_ADV)
bt_le_ext_adv_get_index(struct bt_le_ext_adv * adv)67 uint8_t bt_le_ext_adv_get_index(struct bt_le_ext_adv *adv)
68 {
69 	ptrdiff_t index = adv - adv_pool;
70 
71 	__ASSERT(index >= 0 && index < ARRAY_SIZE(adv_pool),
72 		 "Invalid bt_adv pointer");
73 	return (uint8_t)index;
74 }
75 
adv_new(void)76 static struct bt_le_ext_adv *adv_new(void)
77 {
78 	struct bt_le_ext_adv *adv = NULL;
79 	int i;
80 
81 	for (i = 0; i < ARRAY_SIZE(adv_pool); i++) {
82 		if (!atomic_test_bit(adv_pool[i].flags, BT_ADV_CREATED)) {
83 			adv = &adv_pool[i];
84 			break;
85 		}
86 	}
87 
88 	if (!adv) {
89 		return NULL;
90 	}
91 
92 	(void)memset(adv, 0, sizeof(*adv));
93 	atomic_set_bit(adv_pool[i].flags, BT_ADV_CREATED);
94 	adv->handle = i;
95 
96 	return adv;
97 }
98 
adv_delete(struct bt_le_ext_adv * adv)99 static void adv_delete(struct bt_le_ext_adv *adv)
100 {
101 	atomic_clear_bit(adv->flags, BT_ADV_CREATED);
102 }
103 
104 #if defined(CONFIG_BT_BROADCASTER)
bt_adv_lookup_handle(uint8_t handle)105 static struct bt_le_ext_adv *bt_adv_lookup_handle(uint8_t handle)
106 {
107 	if (handle < ARRAY_SIZE(adv_pool) &&
108 	    atomic_test_bit(adv_pool[handle].flags, BT_ADV_CREATED)) {
109 		return &adv_pool[handle];
110 	}
111 
112 	return NULL;
113 }
114 #endif /* CONFIG_BT_BROADCASTER */
115 #endif /* defined(CONFIG_BT_EXT_ADV) */
116 
bt_le_ext_adv_foreach(void (* func)(struct bt_le_ext_adv * adv,void * data),void * data)117 void bt_le_ext_adv_foreach(void (*func)(struct bt_le_ext_adv *adv, void *data),
118 			   void *data)
119 {
120 #if defined(CONFIG_BT_EXT_ADV)
121 	for (size_t i = 0; i < ARRAY_SIZE(adv_pool); i++) {
122 		if (atomic_test_bit(adv_pool[i].flags, BT_ADV_CREATED)) {
123 			func(&adv_pool[i], data);
124 		}
125 	}
126 #else
127 	func(&bt_dev.adv, data);
128 #endif /* defined(CONFIG_BT_EXT_ADV) */
129 }
130 
adv_new_legacy(void)131 static struct bt_le_ext_adv *adv_new_legacy(void)
132 {
133 #if defined(CONFIG_BT_EXT_ADV)
134 	if (bt_dev.adv) {
135 		return NULL;
136 	}
137 
138 	bt_dev.adv = adv_new();
139 	return bt_dev.adv;
140 #else
141 	return &bt_dev.adv;
142 #endif
143 }
144 
bt_le_adv_delete_legacy(void)145 void bt_le_adv_delete_legacy(void)
146 {
147 #if defined(CONFIG_BT_EXT_ADV)
148 	if (bt_dev.adv) {
149 		atomic_clear_bit(bt_dev.adv->flags, BT_ADV_CREATED);
150 		bt_dev.adv = NULL;
151 	}
152 #endif
153 }
154 
bt_le_adv_lookup_legacy(void)155 struct bt_le_ext_adv *bt_le_adv_lookup_legacy(void)
156 {
157 #if defined(CONFIG_BT_EXT_ADV)
158 	return bt_dev.adv;
159 #else
160 	return &bt_dev.adv;
161 #endif
162 }
163 
bt_le_adv_set_enable_legacy(struct bt_le_ext_adv * adv,bool enable)164 int bt_le_adv_set_enable_legacy(struct bt_le_ext_adv *adv, bool enable)
165 {
166 	struct net_buf *buf;
167 	struct bt_hci_cmd_state_set state;
168 	int err;
169 
170 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_ENABLE, 1);
171 	if (!buf) {
172 		return -ENOBUFS;
173 	}
174 
175 	if (enable) {
176 		net_buf_add_u8(buf, BT_HCI_LE_ADV_ENABLE);
177 	} else {
178 		net_buf_add_u8(buf, BT_HCI_LE_ADV_DISABLE);
179 	}
180 
181 	bt_hci_cmd_state_set_init(buf, &state, adv->flags, BT_ADV_ENABLED, enable);
182 
183 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_ENABLE, buf, NULL);
184 	if (err) {
185 		return err;
186 	}
187 
188 	return 0;
189 }
190 
bt_le_adv_set_enable_ext(struct bt_le_ext_adv * adv,bool enable,const struct bt_le_ext_adv_start_param * param)191 int bt_le_adv_set_enable_ext(struct bt_le_ext_adv *adv,
192 			 bool enable,
193 			 const struct bt_le_ext_adv_start_param *param)
194 {
195 	struct net_buf *buf;
196 	struct bt_hci_cmd_state_set state;
197 	int err;
198 
199 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_ADV_ENABLE, 6);
200 	if (!buf) {
201 		return -ENOBUFS;
202 	}
203 
204 	if (enable) {
205 		net_buf_add_u8(buf, BT_HCI_LE_ADV_ENABLE);
206 	} else {
207 		net_buf_add_u8(buf, BT_HCI_LE_ADV_DISABLE);
208 	}
209 
210 	net_buf_add_u8(buf, 1);
211 
212 	net_buf_add_u8(buf, adv->handle);
213 	net_buf_add_le16(buf, param ? sys_cpu_to_le16(param->timeout) : 0);
214 	net_buf_add_u8(buf, param ? param->num_events : 0);
215 
216 	bt_hci_cmd_state_set_init(buf, &state, adv->flags, BT_ADV_ENABLED, enable);
217 
218 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_ADV_ENABLE, buf, NULL);
219 	if (err) {
220 		return err;
221 	}
222 
223 	return 0;
224 }
225 
bt_le_adv_set_enable(struct bt_le_ext_adv * adv,bool enable)226 int bt_le_adv_set_enable(struct bt_le_ext_adv *adv, bool enable)
227 {
228 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
229 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
230 		return bt_le_adv_set_enable_ext(adv, enable, NULL);
231 	}
232 
233 	return bt_le_adv_set_enable_legacy(adv, enable);
234 }
235 
valid_adv_ext_param(const struct bt_le_adv_param * param)236 static bool valid_adv_ext_param(const struct bt_le_adv_param *param)
237 {
238 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
239 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
240 		if (param->peer &&
241 		    !(param->options & BT_LE_ADV_OPT_EXT_ADV) &&
242 		    !(param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
243 			/* Cannot do directed non-connectable advertising
244 			 * without extended advertising.
245 			 */
246 			return false;
247 		}
248 
249 		if (param->peer &&
250 		    (param->options & BT_LE_ADV_OPT_EXT_ADV) &&
251 		    !(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
252 			/* High duty cycle directed connectable advertising
253 			 * shall not be used with Extended Advertising.
254 			 */
255 			return false;
256 		}
257 
258 		if (!(param->options & BT_LE_ADV_OPT_EXT_ADV) &&
259 		    param->options & (BT_LE_ADV_OPT_EXT_ADV |
260 				      BT_LE_ADV_OPT_NO_2M |
261 				      BT_LE_ADV_OPT_CODED |
262 				      BT_LE_ADV_OPT_ANONYMOUS |
263 				      BT_LE_ADV_OPT_USE_TX_POWER)) {
264 			/* Extended options require extended advertising. */
265 			return false;
266 		}
267 
268 		if ((param->options & BT_LE_ADV_OPT_EXT_ADV) &&
269 		    (param->options & BT_LE_ADV_OPT_SCANNABLE) &&
270 		    (param->options & BT_LE_ADV_OPT_FORCE_NAME_IN_AD)) {
271 			/* Advertising data is not permitted for an extended
272 			 * scannable advertiser.
273 			 */
274 			return false;
275 		}
276 	}
277 
278 	if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
279 	    param->peer &&
280 	    (param->options & BT_LE_ADV_OPT_USE_IDENTITY) &&
281 	    (param->options & BT_LE_ADV_OPT_DIR_ADDR_RPA)) {
282 		/* own addr type used for both RPAs in directed advertising. */
283 		return false;
284 	}
285 
286 	if (param->id >= bt_dev.id_count ||
287 	    !bt_addr_le_cmp(&bt_dev.id_addr[param->id], BT_ADDR_LE_ANY)) {
288 		return false;
289 	}
290 
291 	if (!(param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
292 		/*
293 		 * BT Core 4.2 [Vol 2, Part E, 7.8.5]
294 		 * The Advertising_Interval_Min and Advertising_Interval_Max
295 		 * shall not be set to less than 0x00A0 (100 ms) if the
296 		 * Advertising_Type is set to ADV_SCAN_IND or ADV_NONCONN_IND.
297 		 */
298 		if (bt_dev.hci_version < BT_HCI_VERSION_5_0 &&
299 		    param->interval_min < 0x00a0) {
300 			return false;
301 		}
302 	}
303 
304 	if ((param->options & (BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY |
305 			       BT_LE_ADV_OPT_DIR_ADDR_RPA)) &&
306 	    !param->peer) {
307 		return false;
308 	}
309 
310 	if ((param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY) ||
311 	    !param->peer) {
312 		if (param->interval_min > param->interval_max ||
313 		    param->interval_min < 0x0020 ||
314 		    param->interval_max > 0x4000) {
315 			return false;
316 		}
317 	}
318 
319 	if ((param->options & BT_LE_ADV_OPT_DISABLE_CHAN_37) &&
320 	    (param->options & BT_LE_ADV_OPT_DISABLE_CHAN_38) &&
321 	    (param->options & BT_LE_ADV_OPT_DISABLE_CHAN_39)) {
322 		return false;
323 	}
324 
325 	return true;
326 }
327 
valid_adv_param(const struct bt_le_adv_param * param)328 static bool valid_adv_param(const struct bt_le_adv_param *param)
329 {
330 	if (param->options & BT_LE_ADV_OPT_EXT_ADV) {
331 		return false;
332 	}
333 
334 	if (param->peer && !(param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
335 		return false;
336 	}
337 
338 	return valid_adv_ext_param(param);
339 }
340 
341 
342 struct bt_ad {
343 	const struct bt_data *data;
344 	size_t len;
345 };
346 
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)347 static int set_data_add_complete(uint8_t *set_data, uint8_t set_data_len_max,
348 			const struct bt_ad *ad, size_t ad_len, uint8_t *data_len)
349 {
350 	uint8_t set_data_len = 0;
351 
352 	for (size_t i = 0; i < ad_len; i++) {
353 		const struct bt_data *data = ad[i].data;
354 
355 		for (size_t j = 0; j < ad[i].len; j++) {
356 			size_t len = data[j].data_len;
357 			uint8_t type = data[j].type;
358 
359 			/* Check if ad fit in the remaining buffer */
360 			if ((set_data_len + len + 2) > set_data_len_max) {
361 				ssize_t shortened_len = set_data_len_max -
362 							(set_data_len + 2);
363 
364 				if (!(type == BT_DATA_NAME_COMPLETE &&
365 				      shortened_len > 0)) {
366 					BT_ERR("Too big advertising data");
367 					return -EINVAL;
368 				}
369 
370 				type = BT_DATA_NAME_SHORTENED;
371 				len = shortened_len;
372 			}
373 
374 			set_data[set_data_len++] = len + 1;
375 			set_data[set_data_len++] = type;
376 
377 			memcpy(&set_data[set_data_len], data[j].data, len);
378 			set_data_len += len;
379 		}
380 	}
381 
382 	*data_len = set_data_len;
383 	return 0;
384 }
385 
hci_set_ad(uint16_t hci_op,const struct bt_ad * ad,size_t ad_len)386 static int hci_set_ad(uint16_t hci_op, const struct bt_ad *ad, size_t ad_len)
387 {
388 	struct bt_hci_cp_le_set_adv_data *set_data;
389 	struct net_buf *buf;
390 	int err;
391 
392 	buf = bt_hci_cmd_create(hci_op, sizeof(*set_data));
393 	if (!buf) {
394 		return -ENOBUFS;
395 	}
396 
397 	set_data = net_buf_add(buf, sizeof(*set_data));
398 	(void)memset(set_data, 0, sizeof(*set_data));
399 
400 	err = set_data_add_complete(set_data->data, BT_GAP_ADV_MAX_ADV_DATA_LEN,
401 				    ad, ad_len, &set_data->len);
402 	if (err) {
403 		net_buf_unref(buf);
404 		return err;
405 	}
406 
407 	return bt_hci_cmd_send_sync(hci_op, buf, NULL);
408 }
409 
hci_set_adv_ext_complete(struct bt_le_ext_adv * adv,uint16_t hci_op,const struct bt_ad * ad,size_t ad_len)410 static int hci_set_adv_ext_complete(struct bt_le_ext_adv *adv, uint16_t hci_op,
411 				    const struct bt_ad *ad, size_t ad_len)
412 {
413 	struct bt_hci_cp_le_set_ext_adv_data *set_data;
414 	struct net_buf *buf;
415 	int err;
416 	uint8_t max_data_size;
417 
418 	buf = bt_hci_cmd_create(hci_op, sizeof(*set_data));
419 	if (!buf) {
420 		return -ENOBUFS;
421 	}
422 
423 	set_data = net_buf_add(buf, sizeof(*set_data));
424 	(void)memset(set_data, 0, sizeof(*set_data));
425 
426 	if (atomic_test_bit(adv->flags, BT_ADV_EXT_ADV)) {
427 		max_data_size = BT_HCI_LE_EXT_ADV_FRAG_MAX_LEN;
428 	} else {
429 		max_data_size = BT_GAP_ADV_MAX_ADV_DATA_LEN;
430 	}
431 
432 	err = set_data_add_complete(set_data->data, max_data_size, ad, ad_len,
433 				    &set_data->len);
434 
435 	if (err) {
436 		net_buf_unref(buf);
437 		return err;
438 	}
439 
440 	set_data->handle = adv->handle;
441 	set_data->op = BT_HCI_LE_EXT_ADV_OP_COMPLETE_DATA;
442 	set_data->frag_pref = BT_HCI_LE_EXT_ADV_FRAG_DISABLED;
443 
444 	return bt_hci_cmd_send_sync(hci_op, buf, NULL);
445 }
446 
hci_set_adv_ext_fragmented(struct bt_le_ext_adv * adv,uint16_t hci_op,const struct bt_ad * ad,size_t ad_len)447 static int hci_set_adv_ext_fragmented(struct bt_le_ext_adv *adv, uint16_t hci_op,
448 				      const struct bt_ad *ad, size_t ad_len)
449 {
450 	struct bt_hci_cp_le_set_ext_adv_data *set_data;
451 	struct net_buf *buf;
452 	int err;
453 
454 	for (size_t i = 0; i < ad_len; i++) {
455 
456 		const struct bt_data *data = ad[i].data;
457 
458 		for (size_t j = 0; j < ad[i].len; j++) {
459 			size_t len = data[j].data_len;
460 			uint8_t type = data[j].type;
461 			size_t offset = 0;
462 
463 			/* We can't necessarily set one AD field in a single step. */
464 			while (offset < data[j].data_len) {
465 				buf = bt_hci_cmd_create(hci_op, sizeof(*set_data));
466 				if (!buf) {
467 					return -ENOBUFS;
468 				}
469 
470 				set_data = net_buf_add(buf, sizeof(*set_data));
471 				(void)memset(set_data, 0, sizeof(*set_data));
472 
473 				set_data->handle = adv->handle;
474 				set_data->frag_pref = BT_HCI_LE_EXT_ADV_FRAG_DISABLED;
475 
476 				/* Determine the operation parameter value. */
477 				if ((i == 0) && (j == 0) && (offset == 0)) {
478 					set_data->op = BT_HCI_LE_EXT_ADV_OP_FIRST_FRAG;
479 				} else if ((i == ad_len - 1) && (j == ad[i].len - 1)) {
480 					/* The last AD field may be split into
481 					 * one or two commands.
482 					 */
483 					if (offset != 0) {
484 						/* We can always set the data in two operations
485 						 * Therefore, we know that this is the last.
486 						 */
487 						set_data->op = BT_HCI_LE_EXT_ADV_OP_LAST_FRAG;
488 					} else if (len + 2 <= BT_HCI_LE_EXT_ADV_FRAG_MAX_LEN) {
489 						/* First part fits. */
490 						set_data->op = BT_HCI_LE_EXT_ADV_OP_LAST_FRAG;
491 					} else {
492 						/* The data must be split into two
493 						 * commands.
494 						 */
495 						set_data->op = BT_HCI_LE_EXT_ADV_OP_INTERM_FRAG;
496 					}
497 				} else {
498 					set_data->op = BT_HCI_LE_EXT_ADV_OP_INTERM_FRAG;
499 				}
500 
501 				if (offset == 0) {
502 					set_data->len = MIN(len + 2,
503 							    BT_HCI_LE_EXT_ADV_FRAG_MAX_LEN);
504 				} else {
505 					/* No need to take min operation here,
506 					 * as we can always fit the second part.
507 					 */
508 					set_data->len = len - offset;
509 				}
510 
511 				if (offset == 0) {
512 					set_data->data[0] = len + 1;
513 					set_data->data[1] = type;
514 					memcpy(&set_data->data[2], data[j].data, set_data->len);
515 					offset += set_data->len - 2;
516 				} else {
517 					memcpy(&set_data->data[0], &data[j].data[offset],
518 					       set_data->len);
519 					offset += set_data->len;
520 				}
521 
522 				err = bt_hci_cmd_send_sync(hci_op, buf, NULL);
523 				if (err) {
524 					return err;
525 				}
526 			}
527 
528 		}
529 	}
530 
531 	return 0;
532 }
533 
hci_set_ad_ext(struct bt_le_ext_adv * adv,uint16_t hci_op,const struct bt_ad * ad,size_t ad_len)534 static int hci_set_ad_ext(struct bt_le_ext_adv *adv, uint16_t hci_op,
535 			  const struct bt_ad *ad, size_t ad_len)
536 {
537 	size_t total_len_bytes = 0;
538 
539 	for (size_t i = 0; i < ad_len; i++) {
540 		for (size_t j = 0; j < ad[i].len; j++) {
541 			total_len_bytes += ad[i].data[j].data_len + 2;
542 		}
543 	}
544 
545 	if ((total_len_bytes > BT_HCI_LE_EXT_ADV_FRAG_MAX_LEN) &&
546 	    atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
547 		/* It is not allowed to set advertising data in multiple
548 		 * operations while the advertiser is running.
549 		 */
550 		return -EAGAIN;
551 	}
552 
553 	if (total_len_bytes <= BT_HCI_LE_EXT_ADV_FRAG_MAX_LEN) {
554 		/* If possible, set all data at once.
555 		 * This allows us to update advertising data while advertising.
556 		 */
557 		return hci_set_adv_ext_complete(adv, hci_op, ad, ad_len);
558 	} else {
559 		return hci_set_adv_ext_fragmented(adv, hci_op, ad, ad_len);
560 	}
561 
562 	return 0;
563 }
564 
set_ad(struct bt_le_ext_adv * adv,const struct bt_ad * ad,size_t ad_len)565 static int set_ad(struct bt_le_ext_adv *adv, const struct bt_ad *ad,
566 		  size_t ad_len)
567 {
568 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
569 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
570 		return hci_set_ad_ext(adv, BT_HCI_OP_LE_SET_EXT_ADV_DATA,
571 				      ad, ad_len);
572 	}
573 
574 	return hci_set_ad(BT_HCI_OP_LE_SET_ADV_DATA, ad, ad_len);
575 }
576 
set_sd(struct bt_le_ext_adv * adv,const struct bt_ad * sd,size_t sd_len)577 static int set_sd(struct bt_le_ext_adv *adv, const struct bt_ad *sd,
578 		  size_t sd_len)
579 {
580 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
581 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
582 		return hci_set_ad_ext(adv, BT_HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
583 				      sd, sd_len);
584 	}
585 
586 	return hci_set_ad(BT_HCI_OP_LE_SET_SCAN_RSP_DATA, sd, sd_len);
587 }
588 
ad_has_name(const struct bt_data * ad,size_t ad_len)589 static inline bool ad_has_name(const struct bt_data *ad, size_t ad_len)
590 {
591 	size_t i;
592 
593 	for (i = 0; i < ad_len; i++) {
594 		if (ad[i].type == BT_DATA_NAME_COMPLETE ||
595 		    ad[i].type == BT_DATA_NAME_SHORTENED) {
596 			return true;
597 		}
598 	}
599 
600 	return false;
601 }
602 
ad_is_limited(const struct bt_data * ad,size_t ad_len)603 static bool ad_is_limited(const struct bt_data *ad, size_t ad_len)
604 {
605 	size_t i;
606 
607 	for (i = 0; i < ad_len; i++) {
608 		if (ad[i].type == BT_DATA_FLAGS &&
609 		    ad[i].data_len == sizeof(uint8_t) &&
610 		    ad[i].data != NULL) {
611 			if (ad[i].data[0] & BT_LE_AD_LIMITED) {
612 				return true;
613 			}
614 		}
615 	}
616 
617 	return false;
618 }
619 
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)620 static int le_adv_update(struct bt_le_ext_adv *adv,
621 			 const struct bt_data *ad, size_t ad_len,
622 			 const struct bt_data *sd, size_t sd_len,
623 			 bool ext_adv, bool scannable,
624 			 enum adv_name_type name_type)
625 {
626 	struct bt_ad d[2] = {};
627 	struct bt_data data;
628 	size_t d_len;
629 	int err;
630 
631 	if (name_type != ADV_NAME_TYPE_NONE) {
632 		const char *name = bt_get_name();
633 
634 		if ((ad && ad_has_name(ad, ad_len)) ||
635 		    (sd && ad_has_name(sd, sd_len))) {
636 			/* Cannot use name if name is already set */
637 			return -EINVAL;
638 		}
639 
640 		data = (struct bt_data)BT_DATA(
641 			BT_DATA_NAME_COMPLETE,
642 			name, strlen(name));
643 	}
644 
645 	if (!(ext_adv && scannable)) {
646 		d_len = 1;
647 		d[0].data = ad;
648 		d[0].len = ad_len;
649 
650 		if (name_type == ADV_NAME_TYPE_AD) {
651 			d[1].data = &data;
652 			d[1].len = 1;
653 			d_len = 2;
654 		}
655 
656 		err = set_ad(adv, d, d_len);
657 		if (err) {
658 			return err;
659 		}
660 	}
661 
662 	if (scannable) {
663 		d_len = 1;
664 		d[0].data = sd;
665 		d[0].len = sd_len;
666 
667 		if (name_type == ADV_NAME_TYPE_SD) {
668 			d[1].data = &data;
669 			d[1].len = 1;
670 			d_len = 2;
671 		}
672 
673 		err = set_sd(adv, d, d_len);
674 		if (err) {
675 			return err;
676 		}
677 	}
678 
679 	atomic_set_bit(adv->flags, BT_ADV_DATA_SET);
680 	return 0;
681 }
682 
bt_le_adv_update_data(const struct bt_data * ad,size_t ad_len,const struct bt_data * sd,size_t sd_len)683 int bt_le_adv_update_data(const struct bt_data *ad, size_t ad_len,
684 			  const struct bt_data *sd, size_t sd_len)
685 {
686 	struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
687 	bool scannable;
688 
689 	if (!adv) {
690 		return -EINVAL;
691 	}
692 
693 	if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
694 		return -EAGAIN;
695 	}
696 
697 	scannable = atomic_test_bit(adv->flags, BT_ADV_SCANNABLE);
698 
699 	return le_adv_update(adv, ad, ad_len, sd, sd_len, false, scannable,
700 			     get_adv_name_type(adv));
701 }
702 
get_filter_policy(uint32_t options)703 static uint8_t get_filter_policy(uint32_t options)
704 {
705 	if (!IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST)) {
706 		return BT_LE_ADV_FP_NO_FILTER;
707 	} else if ((options & BT_LE_ADV_OPT_FILTER_SCAN_REQ) &&
708 		   (options & BT_LE_ADV_OPT_FILTER_CONN)) {
709 		return BT_LE_ADV_FP_FILTER_BOTH;
710 	} else if (options & BT_LE_ADV_OPT_FILTER_SCAN_REQ) {
711 		return BT_LE_ADV_FP_FILTER_SCAN_REQ;
712 	} else if (options & BT_LE_ADV_OPT_FILTER_CONN) {
713 		return BT_LE_ADV_FP_FILTER_CONN_IND;
714 	} else {
715 		return BT_LE_ADV_FP_NO_FILTER;
716 	}
717 }
718 
get_adv_channel_map(uint32_t options)719 static uint8_t get_adv_channel_map(uint32_t options)
720 {
721 	uint8_t channel_map = 0x07;
722 
723 	if (options & BT_LE_ADV_OPT_DISABLE_CHAN_37) {
724 		channel_map &= ~0x01;
725 	}
726 
727 	if (options & BT_LE_ADV_OPT_DISABLE_CHAN_38) {
728 		channel_map &= ~0x02;
729 	}
730 
731 	if (options & BT_LE_ADV_OPT_DISABLE_CHAN_39) {
732 		channel_map &= ~0x04;
733 	}
734 
735 	return channel_map;
736 }
737 
le_adv_start_add_conn(const struct bt_le_ext_adv * adv,struct bt_conn ** out_conn)738 static int le_adv_start_add_conn(const struct bt_le_ext_adv *adv,
739 				 struct bt_conn **out_conn)
740 {
741 	struct bt_conn *conn;
742 
743 	bt_dev.adv_conn_id = adv->id;
744 
745 	if (!bt_addr_le_cmp(&adv->target_addr, BT_ADDR_LE_ANY)) {
746 		/* Undirected advertising */
747 		conn = bt_conn_add_le(adv->id, BT_ADDR_LE_NONE);
748 		if (!conn) {
749 			return -ENOMEM;
750 		}
751 
752 		bt_conn_set_state(conn, BT_CONN_CONNECT_ADV);
753 		*out_conn = conn;
754 		return 0;
755 	}
756 
757 	if (bt_conn_exists_le(adv->id, &adv->target_addr)) {
758 		return -EINVAL;
759 	}
760 
761 	conn = bt_conn_add_le(adv->id, &adv->target_addr);
762 	if (!conn) {
763 		return -ENOMEM;
764 	}
765 
766 	bt_conn_set_state(conn, BT_CONN_CONNECT_DIR_ADV);
767 	*out_conn = conn;
768 	return 0;
769 }
770 
le_adv_stop_free_conn(const struct bt_le_ext_adv * adv,uint8_t status)771 static void le_adv_stop_free_conn(const struct bt_le_ext_adv *adv, uint8_t status)
772 {
773 	struct bt_conn *conn;
774 
775 	if (!bt_addr_le_cmp(&adv->target_addr, BT_ADDR_LE_ANY)) {
776 		conn = bt_conn_lookup_state_le(adv->id, BT_ADDR_LE_NONE,
777 					       BT_CONN_CONNECT_ADV);
778 	} else {
779 		conn = bt_conn_lookup_state_le(adv->id, &adv->target_addr,
780 					       BT_CONN_CONNECT_DIR_ADV);
781 	}
782 
783 	if (conn) {
784 		conn->err = status;
785 		bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
786 		bt_conn_unref(conn);
787 	}
788 }
789 
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)790 int bt_le_adv_start_legacy(struct bt_le_ext_adv *adv,
791 			   const struct bt_le_adv_param *param,
792 			   const struct bt_data *ad, size_t ad_len,
793 			   const struct bt_data *sd, size_t sd_len)
794 {
795 	struct bt_hci_cp_le_set_adv_param set_param;
796 	struct bt_conn *conn = NULL;
797 	struct net_buf *buf;
798 	bool dir_adv = (param->peer != NULL), scannable = false;
799 	enum adv_name_type name_type;
800 
801 	int err;
802 
803 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
804 		return -EAGAIN;
805 	}
806 
807 	if (!valid_adv_param(param)) {
808 		return -EINVAL;
809 	}
810 
811 	if (!bt_id_adv_random_addr_check(param)) {
812 		return -EINVAL;
813 	}
814 
815 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
816 		return -EALREADY;
817 	}
818 
819 	(void)memset(&set_param, 0, sizeof(set_param));
820 
821 	set_param.min_interval = sys_cpu_to_le16(param->interval_min);
822 	set_param.max_interval = sys_cpu_to_le16(param->interval_max);
823 	set_param.channel_map  = get_adv_channel_map(param->options);
824 	set_param.filter_policy = get_filter_policy(param->options);
825 
826 	if (adv->id != param->id) {
827 		atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
828 	}
829 
830 	adv->id = param->id;
831 	bt_dev.adv_conn_id = adv->id;
832 
833 	err = bt_id_set_adv_own_addr(adv, param->options, dir_adv,
834 				     &set_param.own_addr_type);
835 	if (err) {
836 		return err;
837 	}
838 
839 	if (dir_adv) {
840 		bt_addr_le_copy(&adv->target_addr, param->peer);
841 	} else {
842 		bt_addr_le_copy(&adv->target_addr, BT_ADDR_LE_ANY);
843 	}
844 
845 	name_type = get_adv_name_type_param(param);
846 
847 	if (param->options & BT_LE_ADV_OPT_CONNECTABLE) {
848 		if (dir_adv) {
849 			if (param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY) {
850 				set_param.type = BT_HCI_ADV_DIRECT_IND_LOW_DUTY;
851 			} else {
852 				set_param.type = BT_HCI_ADV_DIRECT_IND;
853 			}
854 
855 			bt_addr_le_copy(&set_param.direct_addr, param->peer);
856 		} else {
857 			scannable = true;
858 			set_param.type = BT_HCI_ADV_IND;
859 		}
860 	} else if ((param->options & BT_LE_ADV_OPT_SCANNABLE) || sd ||
861 		   (name_type == ADV_NAME_TYPE_SD)) {
862 		scannable = true;
863 		set_param.type = BT_HCI_ADV_SCAN_IND;
864 	} else {
865 		set_param.type = BT_HCI_ADV_NONCONN_IND;
866 	}
867 
868 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_PARAM, sizeof(set_param));
869 	if (!buf) {
870 		return -ENOBUFS;
871 	}
872 
873 	net_buf_add_mem(buf, &set_param, sizeof(set_param));
874 
875 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_PARAM, buf, NULL);
876 	if (err) {
877 		return err;
878 	}
879 
880 	if (!dir_adv) {
881 		err = le_adv_update(adv, ad, ad_len, sd, sd_len, false,
882 				    scannable, name_type);
883 		if (err) {
884 			return err;
885 		}
886 	}
887 
888 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
889 	    (param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
890 		err = le_adv_start_add_conn(adv, &conn);
891 		if (err) {
892 			if (err == -ENOMEM && !dir_adv &&
893 			    !(param->options & BT_LE_ADV_OPT_ONE_TIME)) {
894 				goto set_adv_state;
895 			}
896 
897 			return err;
898 		}
899 	}
900 
901 	err = bt_le_adv_set_enable(adv, true);
902 	if (err) {
903 		BT_ERR("Failed to start advertiser");
904 		if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
905 			bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
906 			bt_conn_unref(conn);
907 		}
908 
909 		return err;
910 	}
911 
912 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
913 		/* If undirected connectable advertiser we have created a
914 		 * connection object that we don't yet give to the application.
915 		 * Since we don't give the application a reference to manage in
916 		 * this case, we need to release this reference here
917 		 */
918 		bt_conn_unref(conn);
919 	}
920 
921 set_adv_state:
922 	atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, !dir_adv &&
923 			  !(param->options & BT_LE_ADV_OPT_ONE_TIME));
924 
925 	atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_AD,
926 			  name_type == ADV_NAME_TYPE_AD);
927 
928 	atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_SD,
929 			  name_type == ADV_NAME_TYPE_SD);
930 
931 	atomic_set_bit_to(adv->flags, BT_ADV_CONNECTABLE,
932 			  param->options & BT_LE_ADV_OPT_CONNECTABLE);
933 
934 	atomic_set_bit_to(adv->flags, BT_ADV_SCANNABLE, scannable);
935 
936 	atomic_set_bit_to(adv->flags, BT_ADV_USE_IDENTITY,
937 			  param->options & BT_LE_ADV_OPT_USE_IDENTITY);
938 
939 	return 0;
940 }
941 
le_ext_adv_param_set(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param,bool has_scan_data)942 static int le_ext_adv_param_set(struct bt_le_ext_adv *adv,
943 				const struct bt_le_adv_param *param,
944 				bool  has_scan_data)
945 {
946 	struct bt_hci_cp_le_set_ext_adv_param *cp;
947 	bool dir_adv = param->peer != NULL, scannable;
948 	struct net_buf *buf, *rsp;
949 	int err;
950 	enum adv_name_type name_type;
951 
952 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_ADV_PARAM, sizeof(*cp));
953 	if (!buf) {
954 		return -ENOBUFS;
955 	}
956 
957 	cp = net_buf_add(buf, sizeof(*cp));
958 	(void)memset(cp, 0, sizeof(*cp));
959 
960 	err = bt_id_set_adv_own_addr(adv, param->options, dir_adv,
961 				     &cp->own_addr_type);
962 	if (err) {
963 		return err;
964 	}
965 
966 	if (dir_adv) {
967 		bt_addr_le_copy(&adv->target_addr, param->peer);
968 	} else {
969 		bt_addr_le_copy(&adv->target_addr, BT_ADDR_LE_ANY);
970 	}
971 
972 	name_type = get_adv_name_type_param(param);
973 
974 	cp->handle = adv->handle;
975 	sys_put_le24(param->interval_min, cp->prim_min_interval);
976 	sys_put_le24(param->interval_max, cp->prim_max_interval);
977 	cp->prim_channel_map = get_adv_channel_map(param->options);
978 	cp->filter_policy = get_filter_policy(param->options);
979 	cp->tx_power = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
980 
981 	cp->prim_adv_phy = BT_HCI_LE_PHY_1M;
982 	if (param->options & BT_LE_ADV_OPT_EXT_ADV) {
983 		if (param->options & BT_LE_ADV_OPT_NO_2M) {
984 			cp->sec_adv_phy = BT_HCI_LE_PHY_1M;
985 		} else {
986 			cp->sec_adv_phy = BT_HCI_LE_PHY_2M;
987 		}
988 	}
989 
990 	if (param->options & BT_LE_ADV_OPT_CODED) {
991 		cp->prim_adv_phy = BT_HCI_LE_PHY_CODED;
992 		cp->sec_adv_phy = BT_HCI_LE_PHY_CODED;
993 	}
994 
995 	if (!(param->options & BT_LE_ADV_OPT_EXT_ADV)) {
996 		cp->props |= BT_HCI_LE_ADV_PROP_LEGACY;
997 	}
998 
999 	if (param->options & BT_LE_ADV_OPT_USE_TX_POWER) {
1000 		cp->props |= BT_HCI_LE_ADV_PROP_TX_POWER;
1001 	}
1002 
1003 	if (param->options & BT_LE_ADV_OPT_ANONYMOUS) {
1004 		cp->props |= BT_HCI_LE_ADV_PROP_ANON;
1005 	}
1006 
1007 	if (param->options & BT_LE_ADV_OPT_NOTIFY_SCAN_REQ) {
1008 		cp->scan_req_notify_enable = BT_HCI_LE_ADV_SCAN_REQ_ENABLE;
1009 	}
1010 
1011 	if (param->options & BT_LE_ADV_OPT_CONNECTABLE) {
1012 		cp->props |= BT_HCI_LE_ADV_PROP_CONN;
1013 		if (!dir_adv && !(param->options & BT_LE_ADV_OPT_EXT_ADV)) {
1014 			/* When using non-extended adv packets then undirected
1015 			 * advertising has to be scannable as well.
1016 			 * We didn't require this option to be set before, so
1017 			 * it is implicitly set instead in this case.
1018 			 */
1019 			cp->props |= BT_HCI_LE_ADV_PROP_SCAN;
1020 		}
1021 	}
1022 
1023 	if ((param->options & BT_LE_ADV_OPT_SCANNABLE) || has_scan_data ||
1024 	    (name_type == ADV_NAME_TYPE_SD)) {
1025 		cp->props |= BT_HCI_LE_ADV_PROP_SCAN;
1026 	}
1027 
1028 	scannable = !!(cp->props & BT_HCI_LE_ADV_PROP_SCAN);
1029 
1030 	if (dir_adv) {
1031 		cp->props |= BT_HCI_LE_ADV_PROP_DIRECT;
1032 		if (!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
1033 			cp->props |= BT_HCI_LE_ADV_PROP_HI_DC_CONN;
1034 		}
1035 
1036 		bt_addr_le_copy(&cp->peer_addr, param->peer);
1037 	}
1038 
1039 	cp->sid = param->sid;
1040 
1041 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_ADV_PARAM, buf, &rsp);
1042 	if (err) {
1043 		return err;
1044 	}
1045 
1046 #if defined(CONFIG_BT_EXT_ADV)
1047 	struct bt_hci_rp_le_set_ext_adv_param *rp = (void *)rsp->data;
1048 
1049 	adv->tx_power = rp->tx_power;
1050 #endif /* defined(CONFIG_BT_EXT_ADV) */
1051 
1052 	net_buf_unref(rsp);
1053 
1054 	atomic_set_bit(adv->flags, BT_ADV_PARAMS_SET);
1055 
1056 	if (atomic_test_and_clear_bit(adv->flags, BT_ADV_RANDOM_ADDR_PENDING)) {
1057 		err = bt_id_set_adv_random_addr(adv, &adv->random_addr.a);
1058 		if (err) {
1059 			return err;
1060 		}
1061 	}
1062 
1063 	/* Flag only used by bt_le_adv_start API. */
1064 	atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, false);
1065 
1066 	atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_AD,
1067 			  name_type == ADV_NAME_TYPE_AD);
1068 
1069 	atomic_set_bit_to(adv->flags, BT_ADV_INCLUDE_NAME_SD,
1070 			  name_type == ADV_NAME_TYPE_SD);
1071 
1072 	atomic_set_bit_to(adv->flags, BT_ADV_CONNECTABLE,
1073 			  param->options & BT_LE_ADV_OPT_CONNECTABLE);
1074 
1075 	atomic_set_bit_to(adv->flags, BT_ADV_SCANNABLE, scannable);
1076 
1077 	atomic_set_bit_to(adv->flags, BT_ADV_USE_IDENTITY,
1078 			  param->options & BT_LE_ADV_OPT_USE_IDENTITY);
1079 
1080 	atomic_set_bit_to(adv->flags, BT_ADV_EXT_ADV,
1081 			  param->options & BT_LE_ADV_OPT_EXT_ADV);
1082 
1083 	return 0;
1084 }
1085 
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)1086 int bt_le_adv_start_ext(struct bt_le_ext_adv *adv,
1087 			const struct bt_le_adv_param *param,
1088 			const struct bt_data *ad, size_t ad_len,
1089 			const struct bt_data *sd, size_t sd_len)
1090 {
1091 	struct bt_le_ext_adv_start_param start_param = {
1092 		.timeout = 0,
1093 		.num_events = 0,
1094 	};
1095 	bool dir_adv = (param->peer != NULL);
1096 	struct bt_conn *conn = NULL;
1097 	int err;
1098 
1099 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1100 		return -EAGAIN;
1101 	}
1102 
1103 	if (!valid_adv_param(param)) {
1104 		return -EINVAL;
1105 	}
1106 
1107 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1108 		return -EALREADY;
1109 	}
1110 
1111 	adv->id = param->id;
1112 	err = le_ext_adv_param_set(adv, param, sd != NULL);
1113 	if (err) {
1114 		return err;
1115 	}
1116 
1117 	if (!dir_adv) {
1118 		err = bt_le_ext_adv_set_data(adv, ad, ad_len, sd, sd_len);
1119 		if (err) {
1120 			return err;
1121 		}
1122 	} else {
1123 		if (!(param->options & BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY)) {
1124 			start_param.timeout =
1125 				BT_GAP_ADV_HIGH_DUTY_CYCLE_MAX_TIMEOUT;
1126 			atomic_set_bit(adv->flags, BT_ADV_LIMITED);
1127 		}
1128 	}
1129 
1130 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1131 	    (param->options & BT_LE_ADV_OPT_CONNECTABLE)) {
1132 		err = le_adv_start_add_conn(adv, &conn);
1133 		if (err) {
1134 			if (err == -ENOMEM && !dir_adv &&
1135 			    !(param->options & BT_LE_ADV_OPT_ONE_TIME)) {
1136 				goto set_adv_state;
1137 			}
1138 
1139 			return err;
1140 		}
1141 	}
1142 
1143 	err = bt_le_adv_set_enable_ext(adv, true, &start_param);
1144 	if (err) {
1145 		BT_ERR("Failed to start advertiser");
1146 		if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1147 			bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1148 			bt_conn_unref(conn);
1149 		}
1150 
1151 		return err;
1152 	}
1153 
1154 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1155 		/* If undirected connectable advertiser we have created a
1156 		 * connection object that we don't yet give to the application.
1157 		 * Since we don't give the application a reference to manage in
1158 		 * this case, we need to release this reference here
1159 		 */
1160 		bt_conn_unref(conn);
1161 	}
1162 
1163 set_adv_state:
1164 	/* Flag always set to false by le_ext_adv_param_set */
1165 	atomic_set_bit_to(adv->flags, BT_ADV_PERSIST, !dir_adv &&
1166 			  !(param->options & BT_LE_ADV_OPT_ONE_TIME));
1167 
1168 	return 0;
1169 }
1170 
1171 static void adv_timeout(struct k_work *work);
1172 
bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv * adv)1173 int bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv *adv)
1174 {
1175 	return k_work_cancel_delayable(&adv->lim_adv_timeout_work);
1176 }
1177 
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)1178 int bt_le_adv_start(const struct bt_le_adv_param *param,
1179 		    const struct bt_data *ad, size_t ad_len,
1180 		    const struct bt_data *sd, size_t sd_len)
1181 {
1182 	struct bt_le_ext_adv *adv = adv_new_legacy();
1183 	int err;
1184 
1185 	if (!adv) {
1186 		return -ENOMEM;
1187 	}
1188 
1189 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1190 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1191 		err = bt_le_adv_start_ext(adv, param, ad, ad_len, sd, sd_len);
1192 	} else {
1193 		err = bt_le_adv_start_legacy(adv, param, ad, ad_len, sd, sd_len);
1194 	}
1195 
1196 	if (err) {
1197 		bt_le_adv_delete_legacy();
1198 	}
1199 
1200 	if (ad_is_limited(ad, ad_len)) {
1201 		k_work_init_delayable(&adv->lim_adv_timeout_work, adv_timeout);
1202 		k_work_reschedule(&adv->lim_adv_timeout_work,
1203 				  K_SECONDS(CONFIG_BT_LIM_ADV_TIMEOUT));
1204 	}
1205 
1206 	return err;
1207 }
1208 
bt_le_adv_stop(void)1209 int bt_le_adv_stop(void)
1210 {
1211 	struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
1212 	int err;
1213 
1214 	(void)bt_le_lim_adv_cancel_timeout(adv);
1215 
1216 	if (!adv) {
1217 		BT_ERR("No valid legacy adv");
1218 		return 0;
1219 	}
1220 
1221 	/* Make sure advertising is not re-enabled later even if it's not
1222 	 * currently enabled (i.e. BT_DEV_ADVERTISING is not set).
1223 	 */
1224 	atomic_clear_bit(adv->flags, BT_ADV_PERSIST);
1225 
1226 	if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1227 		/* Legacy advertiser exists, but is not currently advertising.
1228 		 * This happens when keep advertising behavior is active but
1229 		 * no conn object is available to do connectable advertising.
1230 		 */
1231 		bt_le_adv_delete_legacy();
1232 		return 0;
1233 	}
1234 
1235 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1236 	    atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1237 		le_adv_stop_free_conn(adv, 0);
1238 	}
1239 
1240 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1241 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1242 		err = bt_le_adv_set_enable_ext(adv, false, NULL);
1243 		if (err) {
1244 			return err;
1245 		}
1246 	} else {
1247 		err = bt_le_adv_set_enable_legacy(adv, false);
1248 		if (err) {
1249 			return err;
1250 		}
1251 	}
1252 
1253 	bt_le_adv_delete_legacy();
1254 
1255 #if defined(CONFIG_BT_OBSERVER)
1256 	if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1257 	      BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) &&
1258 	    !IS_ENABLED(CONFIG_BT_PRIVACY) &&
1259 	    !IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY)) {
1260 		/* If scan is ongoing set back NRPA */
1261 		if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
1262 			bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
1263 			bt_id_set_private_addr(BT_ID_DEFAULT);
1264 			bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
1265 		}
1266 	}
1267 #endif /* defined(CONFIG_BT_OBSERVER) */
1268 
1269 	return 0;
1270 }
1271 
1272 #if defined(CONFIG_BT_PERIPHERAL)
bt_le_adv_resume(void)1273 void bt_le_adv_resume(void)
1274 {
1275 	struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy();
1276 	struct bt_conn *conn;
1277 	bool persist_paused = false;
1278 	int err;
1279 
1280 	if (!adv) {
1281 		BT_DBG("No valid legacy adv");
1282 		return;
1283 	}
1284 
1285 	if (!(atomic_test_bit(adv->flags, BT_ADV_PERSIST) &&
1286 	      !atomic_test_bit(adv->flags, BT_ADV_ENABLED))) {
1287 		return;
1288 	}
1289 
1290 	if (!atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1291 		return;
1292 	}
1293 
1294 	err = le_adv_start_add_conn(adv, &conn);
1295 	if (err) {
1296 		BT_DBG("Host cannot resume connectable advertising (%d)", err);
1297 		return;
1298 	}
1299 
1300 	BT_DBG("Resuming connectable advertising");
1301 
1302 	if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1303 	    !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1304 		bt_id_set_adv_private_addr(adv);
1305 	}
1306 
1307 	err = bt_le_adv_set_enable(adv, true);
1308 	if (err) {
1309 		BT_DBG("Controller cannot resume connectable advertising (%d)",
1310 		       err);
1311 		bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1312 
1313 		/* Temporarily clear persist flag to avoid recursion in
1314 		 * bt_conn_unref if the flag is still set.
1315 		 */
1316 		persist_paused = atomic_test_and_clear_bit(adv->flags,
1317 							   BT_ADV_PERSIST);
1318 	}
1319 
1320 	/* Since we don't give the application a reference to manage in
1321 	 * this case, we need to release this reference here.
1322 	 */
1323 	bt_conn_unref(conn);
1324 	if (persist_paused) {
1325 		atomic_set_bit(adv->flags, BT_ADV_PERSIST);
1326 	}
1327 }
1328 #endif /* defined(CONFIG_BT_PERIPHERAL) */
1329 
1330 #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)1331 int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
1332 			   struct bt_le_ext_adv_info *info)
1333 {
1334 	info->id = adv->id;
1335 	info->tx_power = adv->tx_power;
1336 
1337 	return 0;
1338 }
1339 
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)1340 int bt_le_ext_adv_create(const struct bt_le_adv_param *param,
1341 			 const struct bt_le_ext_adv_cb *cb,
1342 			 struct bt_le_ext_adv **out_adv)
1343 {
1344 	struct bt_le_ext_adv *adv;
1345 	int err;
1346 
1347 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1348 		return -EAGAIN;
1349 	}
1350 
1351 	if (!valid_adv_ext_param(param)) {
1352 		return -EINVAL;
1353 	}
1354 
1355 	adv = adv_new();
1356 	if (!adv) {
1357 		return -ENOMEM;
1358 	}
1359 
1360 	adv->id = param->id;
1361 	adv->cb = cb;
1362 
1363 	err = le_ext_adv_param_set(adv, param, false);
1364 	if (err) {
1365 		adv_delete(adv);
1366 		return err;
1367 	}
1368 
1369 	*out_adv = adv;
1370 	return 0;
1371 }
1372 
bt_le_ext_adv_update_param(struct bt_le_ext_adv * adv,const struct bt_le_adv_param * param)1373 int bt_le_ext_adv_update_param(struct bt_le_ext_adv *adv,
1374 			       const struct bt_le_adv_param *param)
1375 {
1376 	if (!valid_adv_ext_param(param)) {
1377 		return -EINVAL;
1378 	}
1379 
1380 	if (IS_ENABLED(CONFIG_BT_PER_ADV) &&
1381 	    atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1382 		/* If params for per adv has been set, do not allow setting
1383 		 * connectable, scanable or use legacy adv
1384 		 */
1385 		if (param->options & BT_LE_ADV_OPT_CONNECTABLE ||
1386 		    param->options & BT_LE_ADV_OPT_SCANNABLE ||
1387 		    !(param->options & BT_LE_ADV_OPT_EXT_ADV) ||
1388 		    param->options & BT_LE_ADV_OPT_ANONYMOUS) {
1389 			return -EINVAL;
1390 		}
1391 	}
1392 
1393 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1394 		return -EINVAL;
1395 	}
1396 
1397 	if (param->id != adv->id) {
1398 		atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
1399 	}
1400 
1401 	return le_ext_adv_param_set(adv, param, false);
1402 }
1403 
bt_le_ext_adv_start(struct bt_le_ext_adv * adv,struct bt_le_ext_adv_start_param * param)1404 int bt_le_ext_adv_start(struct bt_le_ext_adv *adv,
1405 			struct bt_le_ext_adv_start_param *param)
1406 {
1407 	struct bt_conn *conn = NULL;
1408 	int err;
1409 
1410 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1411 		return -EALREADY;
1412 	}
1413 
1414 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1415 	    atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1416 		err = le_adv_start_add_conn(adv, &conn);
1417 		if (err) {
1418 			return err;
1419 		}
1420 	}
1421 
1422 	atomic_set_bit_to(adv->flags, BT_ADV_LIMITED, param &&
1423 			  (param->timeout > 0 || param->num_events > 0));
1424 
1425 	if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1426 		if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1427 		    !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1428 			bt_id_set_adv_private_addr(adv);
1429 		}
1430 	} else {
1431 		if (!atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1432 			bt_id_set_adv_private_addr(adv);
1433 		}
1434 	}
1435 
1436 	if (get_adv_name_type(adv) != ADV_NAME_TYPE_NONE &&
1437 	    !atomic_test_bit(adv->flags, BT_ADV_DATA_SET)) {
1438 		/* Set the advertiser name */
1439 		bt_le_ext_adv_set_data(adv, NULL, 0, NULL, 0);
1440 	}
1441 
1442 	err = bt_le_adv_set_enable_ext(adv, true, param);
1443 	if (err) {
1444 		BT_ERR("Failed to start advertiser");
1445 		if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1446 			bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1447 			bt_conn_unref(conn);
1448 		}
1449 
1450 		return err;
1451 	}
1452 
1453 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn) {
1454 		/* If undirected connectable advertiser we have created a
1455 		 * connection object that we don't yet give to the application.
1456 		 * Since we don't give the application a reference to manage in
1457 		 * this case, we need to release this reference here
1458 		 */
1459 		bt_conn_unref(conn);
1460 	}
1461 
1462 	return 0;
1463 }
1464 
bt_le_ext_adv_stop(struct bt_le_ext_adv * adv)1465 int bt_le_ext_adv_stop(struct bt_le_ext_adv *adv)
1466 {
1467 	(void)bt_le_lim_adv_cancel_timeout(adv);
1468 
1469 	atomic_clear_bit(adv->flags, BT_ADV_PERSIST);
1470 
1471 	if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1472 		return 0;
1473 	}
1474 
1475 	if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
1476 		atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
1477 
1478 #if defined(CONFIG_BT_SMP)
1479 		bt_id_pending_keys_update();
1480 #endif
1481 	}
1482 
1483 	if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1484 	    atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1485 		le_adv_stop_free_conn(adv, 0);
1486 	}
1487 
1488 	return bt_le_adv_set_enable_ext(adv, false, NULL);
1489 }
1490 
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)1491 int bt_le_ext_adv_set_data(struct bt_le_ext_adv *adv,
1492 			   const struct bt_data *ad, size_t ad_len,
1493 			   const struct bt_data *sd, size_t sd_len)
1494 {
1495 	bool ext_adv, scannable;
1496 
1497 	ext_adv = atomic_test_bit(adv->flags, BT_ADV_EXT_ADV);
1498 	scannable = atomic_test_bit(adv->flags, BT_ADV_SCANNABLE);
1499 
1500 	return le_adv_update(adv, ad, ad_len, sd, sd_len, ext_adv, scannable,
1501 			     get_adv_name_type(adv));
1502 }
1503 
bt_le_ext_adv_delete(struct bt_le_ext_adv * adv)1504 int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv)
1505 {
1506 	struct bt_hci_cp_le_remove_adv_set *cp;
1507 	struct net_buf *buf;
1508 	int err;
1509 
1510 	if (!BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1511 		return -ENOTSUP;
1512 	}
1513 
1514 	/* Advertising set should be stopped first */
1515 	if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1516 		return -EINVAL;
1517 	}
1518 
1519 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_REMOVE_ADV_SET, sizeof(*cp));
1520 	if (!buf) {
1521 		BT_WARN("No HCI buffers");
1522 		return -ENOBUFS;
1523 	}
1524 
1525 	cp = net_buf_add(buf, sizeof(*cp));
1526 	cp->handle = adv->handle;
1527 
1528 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REMOVE_ADV_SET, buf, NULL);
1529 	if (err) {
1530 		return err;
1531 	}
1532 
1533 	adv_delete(adv);
1534 
1535 	return 0;
1536 }
1537 #endif /* defined(CONFIG_BT_EXT_ADV) */
1538 
1539 
adv_timeout(struct k_work * work)1540 static void adv_timeout(struct k_work *work)
1541 {
1542 	int err = 0;
1543 	struct k_work_delayable *dwork;
1544 	struct bt_le_ext_adv *adv;
1545 
1546 	dwork = k_work_delayable_from_work(work);
1547 	adv = CONTAINER_OF(dwork, struct bt_le_ext_adv, lim_adv_timeout_work);
1548 
1549 #if defined(CONFIG_BT_EXT_ADV)
1550 	if (adv == bt_dev.adv) {
1551 		err = bt_le_adv_stop();
1552 	} else {
1553 		err = bt_le_ext_adv_stop(adv);
1554 	}
1555 #else
1556 	err = bt_le_adv_stop();
1557 #endif
1558 	BT_WARN("Failed to stop advertising: %d", err);
1559 }
1560 
1561 #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)1562 int bt_le_per_adv_set_param(struct bt_le_ext_adv *adv,
1563 			    const struct bt_le_per_adv_param *param)
1564 {
1565 	struct bt_hci_cp_le_set_per_adv_param *cp;
1566 	struct net_buf *buf;
1567 	int err;
1568 
1569 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1570 		return -ENOTSUP;
1571 	}
1572 
1573 	if (atomic_test_bit(adv->flags, BT_ADV_SCANNABLE)) {
1574 		return -EINVAL;
1575 	} else if (atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1576 		return -EINVAL;
1577 	} else if (!atomic_test_bit(adv->flags, BT_ADV_EXT_ADV)) {
1578 		return -EINVAL;
1579 	}
1580 
1581 	if (param->interval_min < BT_GAP_PER_ADV_MIN_INTERVAL ||
1582 	    param->interval_max > BT_GAP_PER_ADV_MAX_INTERVAL ||
1583 	    param->interval_min > param->interval_max) {
1584 		return -EINVAL;
1585 	}
1586 
1587 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_PARAM, sizeof(*cp));
1588 	if (!buf) {
1589 		return -ENOBUFS;
1590 	}
1591 
1592 	cp = net_buf_add(buf, sizeof(*cp));
1593 	(void)memset(cp, 0, sizeof(*cp));
1594 
1595 	cp->handle = adv->handle;
1596 	cp->min_interval = sys_cpu_to_le16(param->interval_min);
1597 	cp->max_interval = sys_cpu_to_le16(param->interval_max);
1598 
1599 	if (param->options & BT_LE_PER_ADV_OPT_USE_TX_POWER) {
1600 		cp->props |= BT_HCI_LE_ADV_PROP_TX_POWER;
1601 	}
1602 
1603 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_PARAM, buf, NULL);
1604 	if (err) {
1605 		return err;
1606 	}
1607 
1608 	atomic_set_bit(adv->flags, BT_PER_ADV_PARAMS_SET);
1609 
1610 	return 0;
1611 }
1612 
bt_le_per_adv_set_data(const struct bt_le_ext_adv * adv,const struct bt_data * ad,size_t ad_len)1613 int bt_le_per_adv_set_data(const struct bt_le_ext_adv *adv,
1614 			   const struct bt_data *ad, size_t ad_len)
1615 {
1616 	struct bt_hci_cp_le_set_per_adv_data *cp;
1617 	struct net_buf *buf;
1618 	struct bt_ad d = { .data = ad, .len = ad_len };
1619 	int err;
1620 
1621 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1622 		return -ENOTSUP;
1623 	}
1624 
1625 	if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1626 		return -EINVAL;
1627 	}
1628 
1629 	if (!ad_len || !ad) {
1630 		return -EINVAL;
1631 	}
1632 
1633 	if (ad_len > BT_HCI_LE_PER_ADV_FRAG_MAX_LEN) {
1634 		return -EINVAL;
1635 	}
1636 
1637 
1638 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_DATA, sizeof(*cp));
1639 	if (!buf) {
1640 		return -ENOBUFS;
1641 	}
1642 
1643 	cp = net_buf_add(buf, sizeof(*cp));
1644 	(void)memset(cp, 0, sizeof(*cp));
1645 
1646 	cp->handle = adv->handle;
1647 
1648 	/* TODO: If data is longer than what the controller can manage,
1649 	 * split the data. Read size from controller on boot.
1650 	 */
1651 	cp->op = BT_HCI_LE_PER_ADV_OP_COMPLETE_DATA;
1652 
1653 	err = set_data_add_complete(cp->data, BT_HCI_LE_PER_ADV_FRAG_MAX_LEN, &d, 1,
1654 				    &cp->len);
1655 	if (err) {
1656 		return err;
1657 	}
1658 
1659 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_DATA, buf, NULL);
1660 	if (err) {
1661 		return err;
1662 	}
1663 
1664 	return 0;
1665 }
1666 
bt_le_per_adv_enable(struct bt_le_ext_adv * adv,bool enable)1667 static int bt_le_per_adv_enable(struct bt_le_ext_adv *adv, bool enable)
1668 {
1669 	struct bt_hci_cp_le_set_per_adv_enable *cp;
1670 	struct net_buf *buf;
1671 	struct bt_hci_cmd_state_set state;
1672 	int err;
1673 
1674 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1675 		return -ENOTSUP;
1676 	}
1677 
1678 	/* TODO: We could setup some default ext adv params if not already set*/
1679 	if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) {
1680 		return -EINVAL;
1681 	}
1682 
1683 	if (atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED) == enable) {
1684 		return -EALREADY;
1685 	}
1686 
1687 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_ENABLE, sizeof(*cp));
1688 	if (!buf) {
1689 		return -ENOBUFS;
1690 	}
1691 
1692 	cp = net_buf_add(buf, sizeof(*cp));
1693 	(void)memset(cp, 0, sizeof(*cp));
1694 
1695 	cp->handle = adv->handle;
1696 	cp->enable = enable ? 1 : 0;
1697 
1698 	bt_hci_cmd_state_set_init(buf, &state, adv->flags,
1699 				  BT_PER_ADV_ENABLED, enable);
1700 
1701 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_ENABLE, buf, NULL);
1702 	if (err) {
1703 		return err;
1704 	}
1705 
1706 	return 0;
1707 }
1708 
bt_le_per_adv_start(struct bt_le_ext_adv * adv)1709 int bt_le_per_adv_start(struct bt_le_ext_adv *adv)
1710 {
1711 	return bt_le_per_adv_enable(adv, true);
1712 }
1713 
bt_le_per_adv_stop(struct bt_le_ext_adv * adv)1714 int bt_le_per_adv_stop(struct bt_le_ext_adv *adv)
1715 {
1716 	return bt_le_per_adv_enable(adv, false);
1717 }
1718 
1719 #if defined(CONFIG_BT_CONN)
bt_le_per_adv_set_info_transfer(const struct bt_le_ext_adv * adv,const struct bt_conn * conn,uint16_t service_data)1720 int bt_le_per_adv_set_info_transfer(const struct bt_le_ext_adv *adv,
1721 				    const struct bt_conn *conn,
1722 				    uint16_t service_data)
1723 {
1724 	struct bt_hci_cp_le_per_adv_set_info_transfer *cp;
1725 	struct net_buf *buf;
1726 
1727 
1728 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1729 		return -ENOTSUP;
1730 	} else if (!BT_FEAT_LE_PAST_SEND(bt_dev.le.features)) {
1731 		return -ENOTSUP;
1732 	}
1733 
1734 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_SET_INFO_TRANSFER,
1735 				sizeof(*cp));
1736 	if (!buf) {
1737 		return -ENOBUFS;
1738 	}
1739 
1740 	cp = net_buf_add(buf, sizeof(*cp));
1741 	(void)memset(cp, 0, sizeof(*cp));
1742 
1743 	cp->conn_handle = sys_cpu_to_le16(conn->handle);
1744 	cp->adv_handle = adv->handle;
1745 	cp->service_data = sys_cpu_to_le16(service_data);
1746 
1747 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_SET_INFO_TRANSFER, buf,
1748 				    NULL);
1749 }
1750 #endif /* CONFIG_BT_CONN */
1751 #endif /* CONFIG_BT_PER_ADV */
1752 
1753 #if defined(CONFIG_BT_EXT_ADV)
1754 #if defined(CONFIG_BT_BROADCASTER)
bt_hci_le_adv_set_terminated(struct net_buf * buf)1755 void bt_hci_le_adv_set_terminated(struct net_buf *buf)
1756 {
1757 	struct bt_hci_evt_le_adv_set_terminated *evt;
1758 	struct bt_le_ext_adv *adv;
1759 	uint16_t conn_handle;
1760 
1761 	evt = (void *)buf->data;
1762 	adv = bt_adv_lookup_handle(evt->adv_handle);
1763 	conn_handle = sys_le16_to_cpu(evt->conn_handle);
1764 
1765 	(void)bt_le_lim_adv_cancel_timeout(adv);
1766 #if (CONFIG_BT_ID_MAX > 1) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1)
1767 	bt_dev.adv_conn_id = adv->id;
1768 	for (int i = 0; i < ARRAY_SIZE(bt_dev.cached_conn_complete); i++) {
1769 		if (bt_dev.cached_conn_complete[i].valid &&
1770 		    bt_dev.cached_conn_complete[i].evt.handle == evt->conn_handle) {
1771 			if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
1772 				/* Process the cached connection complete event
1773 				 * now that the corresponding advertising set is known.
1774 				 *
1775 				 * If the advertiser has been stopped before the connection
1776 				 * complete event has been raised to the application, we
1777 				 * discard the event.
1778 				 */
1779 				bt_hci_le_enh_conn_complete(&bt_dev.cached_conn_complete[i].evt);
1780 			}
1781 			bt_dev.cached_conn_complete[i].valid = false;
1782 		}
1783 	}
1784 #endif
1785 
1786 	BT_DBG("status 0x%02x adv_handle %u conn_handle 0x%02x num %u",
1787 	       evt->status, evt->adv_handle, conn_handle,
1788 	       evt->num_completed_ext_adv_evts);
1789 
1790 	if (!adv) {
1791 		BT_ERR("No valid adv");
1792 		return;
1793 	}
1794 
1795 	atomic_clear_bit(adv->flags, BT_ADV_ENABLED);
1796 
1797 	if (evt->status && IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1798 	    atomic_test_bit(adv->flags, BT_ADV_CONNECTABLE)) {
1799 		/* Only set status for legacy advertising API.
1800 		 * This will call connected callback for high duty cycle
1801 		 * directed advertiser timeout.
1802 		 */
1803 		le_adv_stop_free_conn(adv, adv == bt_dev.adv ? evt->status : 0);
1804 	}
1805 
1806 	if (IS_ENABLED(CONFIG_BT_CONN) && !evt->status) {
1807 		struct bt_conn *conn = bt_conn_lookup_handle(conn_handle);
1808 
1809 		if (conn) {
1810 			if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1811 			    !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
1812 				/* Set Responder address unless already set */
1813 				conn->le.resp_addr.type = BT_ADDR_LE_RANDOM;
1814 				if (bt_addr_cmp(&conn->le.resp_addr.a,
1815 						BT_ADDR_ANY) == 0) {
1816 					bt_addr_copy(&conn->le.resp_addr.a,
1817 						     &adv->random_addr.a);
1818 				}
1819 			} else {
1820 				bt_addr_le_copy(&conn->le.resp_addr,
1821 					&bt_dev.id_addr[conn->id]);
1822 			}
1823 
1824 			if (adv->cb && adv->cb->connected) {
1825 				struct bt_le_ext_adv_connected_info info = {
1826 					.conn = conn,
1827 				};
1828 
1829 				adv->cb->connected(adv, &info);
1830 			}
1831 
1832 			bt_conn_unref(conn);
1833 		}
1834 	}
1835 
1836 	if (atomic_test_and_clear_bit(adv->flags, BT_ADV_LIMITED)) {
1837 		atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
1838 
1839 #if defined(CONFIG_BT_SMP)
1840 		bt_id_pending_keys_update();
1841 #endif
1842 
1843 		if (adv->cb && adv->cb->sent) {
1844 			struct bt_le_ext_adv_sent_info info = {
1845 				.num_sent = evt->num_completed_ext_adv_evts,
1846 			};
1847 
1848 			adv->cb->sent(adv, &info);
1849 		}
1850 	}
1851 
1852 	if (!atomic_test_bit(adv->flags, BT_ADV_PERSIST) && adv == bt_dev.adv) {
1853 		bt_le_adv_delete_legacy();
1854 	}
1855 }
1856 
bt_hci_le_scan_req_received(struct net_buf * buf)1857 void bt_hci_le_scan_req_received(struct net_buf *buf)
1858 {
1859 	struct bt_hci_evt_le_scan_req_received *evt;
1860 	struct bt_le_ext_adv *adv;
1861 
1862 	evt = (void *)buf->data;
1863 	adv = bt_adv_lookup_handle(evt->handle);
1864 
1865 	BT_DBG("handle %u peer %s", evt->handle, bt_addr_le_str(&evt->addr));
1866 
1867 	if (!adv) {
1868 		BT_ERR("No valid adv");
1869 		return;
1870 	}
1871 
1872 	if (adv->cb && adv->cb->scanned) {
1873 		struct bt_le_ext_adv_scanned_info info;
1874 		bt_addr_le_t id_addr;
1875 
1876 		if (evt->addr.type == BT_ADDR_LE_PUBLIC_ID ||
1877 		    evt->addr.type == BT_ADDR_LE_RANDOM_ID) {
1878 			bt_addr_le_copy(&id_addr, &evt->addr);
1879 			id_addr.type -= BT_ADDR_LE_PUBLIC_ID;
1880 		} else {
1881 			bt_addr_le_copy(&id_addr,
1882 					bt_lookup_id_addr(adv->id, &evt->addr));
1883 		}
1884 
1885 		info.addr = &id_addr;
1886 		adv->cb->scanned(adv, &info);
1887 	}
1888 }
1889 #endif /* defined(CONFIG_BT_BROADCASTER) */
1890 #endif /* defined(CONFIG_BT_EXT_ADV) */
1891