1 /*
2  * Copyright (c) 2019 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/settings/settings.h>
8 
9 #include "ll_settings.h"
10 
11 #include <zephyr/logging/log.h>
12 
13 LOG_MODULE_REGISTER(bt_ctlr_ll_settings, LOG_LEVEL_DBG);
14 
15 #include "hal/debug.h"
16 
17 #if defined(CONFIG_BT_CTLR_VERSION_SETTINGS)
18 
19 static uint16_t company_id = CONFIG_BT_CTLR_COMPANY_ID;
20 static uint16_t subversion = CONFIG_BT_CTLR_SUBVERSION_NUMBER;
21 
ll_settings_company_id(void)22 uint16_t ll_settings_company_id(void)
23 {
24 	return company_id;
25 }
ll_settings_subversion_number(void)26 uint16_t ll_settings_subversion_number(void)
27 {
28 	return subversion;
29 }
30 
31 #endif /* CONFIG_BT_CTLR_VERSION_SETTINGS */
32 
33 #if defined(CONFIG_BT_CTLR_SMI_TX_SETTING)
34 
35 static uint8_t smi_tx;
36 
ll_settings_smi_tx(void)37 bool ll_settings_smi_tx(void)
38 {
39 	return smi_tx;
40 }
41 
42 #endif /* CONFIG_BT_CTLR_SMI_TX_SETTING */
43 
ctlr_set(const char * name,size_t len_rd,settings_read_cb read_cb,void * store)44 static int ctlr_set(const char *name, size_t len_rd,
45 		    settings_read_cb read_cb, void *store)
46 {
47 	ssize_t len;
48 	int nlen;
49 	const char *next;
50 
51 	nlen = settings_name_next(name, &next);
52 
53 #if defined(CONFIG_BT_CTLR_VERSION_SETTINGS)
54 	if (!strncmp(name, "company", nlen)) {
55 		len = read_cb(store, &company_id, sizeof(company_id));
56 		if (len < 0) {
57 			LOG_ERR("Failed to read Company Id from storage"
58 			       " (err %zd)", len);
59 		} else {
60 			LOG_DBG("Company Id set to %04x", company_id);
61 		}
62 		return 0;
63 	}
64 	if (!strncmp(name, "subver", nlen)) {
65 		len = read_cb(store, &subversion, sizeof(subversion));
66 		if (len < 0) {
67 			LOG_ERR("Failed to read Subversion from storage"
68 			       " (err %zd)", len);
69 		} else {
70 			LOG_DBG("Subversion set to %04x", subversion);
71 		}
72 		return 0;
73 	}
74 #endif /* CONFIG_BT_CTLR_VERSION_SETTINGS */
75 
76 #if defined(CONFIG_BT_CTLR_SMI_TX_SETTING)
77 	if (!strncmp(name, "smi_tx", nlen)) {
78 		len = read_cb(store, &smi_tx, sizeof(smi_tx));
79 		if (len < 0) {
80 			LOG_ERR("Failed to read SMI TX flag from storage"
81 			       " (err %zd)", len);
82 		} else {
83 			LOG_DBG("SMI TX flag set to %04x", smi_tx);
84 		}
85 		return 0;
86 	}
87 #endif /* CONFIG_BT_CTLR_SMI_TX_SETTING */
88 
89 	return 0;
90 }
91 
92 SETTINGS_STATIC_HANDLER_DEFINE(bt_ctlr, "bt/ctlr", NULL, ctlr_set, NULL, NULL);
93