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