1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <stddef.h>
9 #include <string.h>
10 
11 #include <zephyr/toolchain.h>
12 #include <zephyr/sys/util.h>
13 
14 #include "util/util.h"
15 #include "util/memq.h"
16 #include "util/mem.h"
17 #include "util/dbuf.h"
18 
19 #include "hal/ccm.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/lll_df_types.h"
30 #include "lll_conn.h"
31 
32 #include "ull_adv_types.h"
33 
34 #include "ull_adv_internal.h"
35 #include "ull_central_internal.h"
36 
37 /* The HCI LE Set Host Channel Classification command allows the Host to
38  * specify a channel classification for the data, secondary advertising,
39  * periodic, and isochronous physical channels based on its local information.
40  */
41 static uint8_t map[5];
42 static uint8_t count;
43 
44 static void chan_map_set(uint8_t const *const chan_map);
45 
ll_chm_update(uint8_t const * const chm)46 uint8_t ll_chm_update(uint8_t const *const chm)
47 {
48 	chan_map_set(chm);
49 
50 #if defined(CONFIG_BT_CENTRAL)
51 	(void)ull_central_chm_update();
52 #endif /* CONFIG_BT_CENTRAL */
53 
54 #if (CONFIG_BT_CTLR_ADV_AUX_SET > 0)
55 	(void)ull_adv_aux_chm_update();
56 #endif /*(CONFIG_BT_CTLR_ADV_AUX_SET > 0) */
57 
58 #if defined(CONFIG_BT_CTLR_ADV_PERIODIC)
59 	(void)ull_adv_sync_chm_update();
60 #endif /* CONFIG_BT_CTLR_ADV_PERIODIC */
61 
62 #if defined(CONFIG_BT_CTLR_ADV_ISO)
63 	(void)ull_adv_iso_chm_update();
64 #endif /* CONFIG_BT_CTLR_ADV_ISO */
65 
66 	/* TODO: Should failure due to Channel Map Update being already in
67 	 *       progress be returned to caller?
68 	 */
69 	return 0;
70 }
71 
ull_chan_reset(void)72 void ull_chan_reset(void)
73 {
74 	/* Initial channel map indicating Used and Unused data channels. */
75 	map[0] = 0xFF;
76 	map[1] = 0xFF;
77 	map[2] = 0xFF;
78 	map[3] = 0xFF;
79 	map[4] = 0x1F;
80 	count = 37U;
81 }
82 
ull_chan_map_get(uint8_t * const chan_map)83 uint8_t ull_chan_map_get(uint8_t *const chan_map)
84 {
85 	(void)memcpy(chan_map, map, sizeof(map));
86 
87 	return count;
88 }
89 
chan_map_set(uint8_t const * const chan_map)90 static void chan_map_set(uint8_t const *const chan_map)
91 {
92 	(void)memcpy(map, chan_map, sizeof(map));
93 	count = util_ones_count_get(map, sizeof(map));
94 }
95