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.h>
11 #include <soc.h>
12 #include <bluetooth/hci.h>
13 #include <bluetooth/controller.h>
14 
15 #include "util/util.h"
16 #include "util/memq.h"
17 #include "util/mem.h"
18 
19 #include "pdu.h"
20 
21 #include "lll.h"
22 #include "lll/lll_adv_types.h"
23 #include "lll_adv.h"
24 #include "lll/lll_adv_pdu.h"
25 #include "lll_scan.h"
26 
27 #include "ull_adv_types.h"
28 #include "ull_scan_types.h"
29 #include "ull_adv_internal.h"
30 #include "ull_scan_internal.h"
31 
32 #include "ll.h"
33 
34 static uint8_t pub_addr[BDADDR_SIZE];
35 static uint8_t rnd_addr[BDADDR_SIZE];
36 
ll_addr_set(uint8_t addr_type,uint8_t const * const bdaddr)37 uint8_t ll_addr_set(uint8_t addr_type, uint8_t const *const bdaddr)
38 {
39 	if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
40 #if defined(CONFIG_BT_CTLR_ADV_EXT)
41 		if (ull_adv_is_enabled(0) && !ll_adv_cmds_is_ext()) {
42 #else /* !CONFIG_BT_CTLR_ADV_EXT */
43 		if (ull_adv_is_enabled(0)) {
44 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
45 			return BT_HCI_ERR_CMD_DISALLOWED;
46 		}
47 	}
48 
49 	if (IS_ENABLED(CONFIG_BT_OBSERVER) &&
50 	    (ull_scan_is_enabled(0) & (BIT(1) | BIT(2)))) {
51 		return BT_HCI_ERR_CMD_DISALLOWED;
52 	}
53 
54 	if (addr_type) {
55 		memcpy(rnd_addr, bdaddr, BDADDR_SIZE);
56 	} else {
57 		memcpy(pub_addr, bdaddr, BDADDR_SIZE);
58 	}
59 
60 	return 0;
61 }
62 
63 uint8_t *ll_addr_get(uint8_t addr_type)
64 {
65 	if (addr_type > BT_ADDR_LE_RANDOM) {
66 		return NULL;
67 	}
68 
69 	if (addr_type) {
70 		return rnd_addr;
71 	}
72 
73 	return pub_addr;
74 }
75 
76 uint8_t *ll_addr_read(uint8_t addr_type, uint8_t *const bdaddr)
77 {
78 	uint8_t *addr;
79 
80 	addr = ll_addr_get(addr_type);
81 	if (addr) {
82 		memcpy(bdaddr, addr, BDADDR_SIZE);
83 	}
84 
85 	return addr;
86 }
87 
88 void bt_ctlr_set_public_addr(const uint8_t *addr)
89 {
90     (void)memcpy(pub_addr, addr, sizeof(pub_addr));
91 }
92