1 /*
2  * Copyright (c) 2016-2018 Nordic Semiconductor ASA
3  * Copyright (c) 2016 Vinayak Kariappa Chettimada
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 #include <stddef.h>
8 #include <string.h>
9 
10 #include <zephyr/kernel.h>
11 #include <soc.h>
12 #include <zephyr/bluetooth/hci_types.h>
13 #include <zephyr/bluetooth/controller.h>
14 
15 #include "hal/ccm.h"
16 
17 #include "util/util.h"
18 #include "util/memq.h"
19 #include "util/mem.h"
20 
21 #include "pdu_df.h"
22 #include "lll/pdu_vendor.h"
23 #include "pdu.h"
24 
25 #include "lll.h"
26 #include "lll/lll_adv_types.h"
27 #include "lll_adv.h"
28 #include "lll/lll_adv_pdu.h"
29 #include "lll_scan.h"
30 
31 #include "ull_adv_types.h"
32 #include "ull_scan_types.h"
33 #include "ull_adv_internal.h"
34 #include "ull_scan_internal.h"
35 
36 #include "ll.h"
37 
38 static uint8_t pub_addr[BDADDR_SIZE];
39 static uint8_t rnd_addr[BDADDR_SIZE];
40 
ll_addr_set(uint8_t addr_type,uint8_t const * const bdaddr)41 uint8_t ll_addr_set(uint8_t addr_type, uint8_t const *const bdaddr)
42 {
43 	if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
44 #if defined(CONFIG_BT_CTLR_ADV_EXT)
45 		if (ull_adv_is_enabled(0) && !ll_adv_cmds_is_ext()) {
46 #else /* !CONFIG_BT_CTLR_ADV_EXT */
47 		if (ull_adv_is_enabled(0)) {
48 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
49 			return BT_HCI_ERR_CMD_DISALLOWED;
50 		}
51 	}
52 
53 	if (IS_ENABLED(CONFIG_BT_OBSERVER) &&
54 	    (ull_scan_is_enabled(0) & (BIT(1) | BIT(2)))) {
55 		return BT_HCI_ERR_CMD_DISALLOWED;
56 	}
57 
58 	if (addr_type) {
59 		if (bdaddr) {
60 			(void)memcpy(rnd_addr, bdaddr, BDADDR_SIZE);
61 		} else {
62 			(void)memset(rnd_addr, 0, BDADDR_SIZE);
63 		}
64 	} else {
65 		if (bdaddr) {
66 			(void)memcpy(pub_addr, bdaddr, BDADDR_SIZE);
67 		} else {
68 			(void)memset(pub_addr, 0, BDADDR_SIZE);
69 		}
70 	}
71 
72 	return 0;
73 }
74 
75 uint8_t *ll_addr_get(uint8_t addr_type)
76 {
77 	if (addr_type > BT_ADDR_LE_RANDOM) {
78 		return NULL;
79 	}
80 
81 	if (addr_type) {
82 		return rnd_addr;
83 	}
84 
85 	return pub_addr;
86 }
87 
88 uint8_t *ll_addr_read(uint8_t addr_type, uint8_t *const bdaddr)
89 {
90 	uint8_t *addr;
91 
92 	addr = ll_addr_get(addr_type);
93 	if (addr) {
94 		memcpy(bdaddr, addr, BDADDR_SIZE);
95 	}
96 
97 	return addr;
98 }
99 
100 void bt_ctlr_set_public_addr(const uint8_t *addr)
101 {
102 	(void)memcpy(pub_addr, addr, sizeof(pub_addr));
103 }
104