Skip to content

Commit 414e80c

Browse files
committed
samples: mcumgr: ble_mcumgr: Add settings test code
Adds code to get bluetooth name from retention settings Signed-off-by: Jamie McCrae <[email protected]>
1 parent 2273dc6 commit 414e80c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

samples/mcumgr/ble_mcumgr/prj.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@ CONFIG_BLE_MCUMGR=y
4646

4747
CONFIG_LOG=y
4848
CONFIG_LOG_MODE_MINIMAL=y
49+
50+
CONFIG_RETAINED_MEM=y
51+
CONFIG_RETENTION=y
52+
CONFIG_SETTINGS=y
53+
CONFIG_SETTINGS_RETENTION=y
54+
#CONFIG_SETTINGS_RUNTIME=y
55+
CONFIG_NCS_BM_SETTINGS_BLUETOOTH_NAME=y

samples/mcumgr/ble_mcumgr/src/main.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <zephyr/logging/log_ctrl.h>
2222
#include <ble_conn_params.h>
2323
#include <bluetooth/services/ble_mcumgr.h>
24+
#include <zephyr/settings/settings.h>
25+
#include <settings/bluetooth_name.h>
2426

2527
LOG_MODULE_REGISTER(app, CONFIG_APP_LOG_LEVEL);
2628

@@ -175,6 +177,9 @@ static int ble_change_address(void)
175177
int main(void)
176178
{
177179
int err;
180+
const char *custom_advertising_name;
181+
uint8_t custom_advertising_name_size;
182+
ble_gap_conn_sec_mode_t sec_mode = {0};
178183
struct ble_adv_config ble_adv_cfg = {
179184
.conn_cfg_tag = CONFIG_NRF_SDH_BLE_CONN_TAG,
180185
.evt_handler = ble_adv_evt_handler,
@@ -210,6 +215,13 @@ int main(void)
210215

211216
LOG_INF("Bluetooth enabled");
212217

218+
err = settings_subsys_init();
219+
220+
if (err) {
221+
LOG_ERR("Failed to enable settings, err %d", err);
222+
}
223+
224+
settings_load();
213225
err = ble_mcumgr_init();
214226

215227
if (err) {
@@ -237,14 +249,38 @@ int main(void)
237249
return 0;
238250
}
239251

252+
custom_advertising_name = bluetooth_name_value_get();
253+
custom_advertising_name_size = strlen(custom_advertising_name);
254+
255+
if (custom_advertising_name_size > 0) {
256+
/* Change advertising name to one from application */
257+
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
258+
err = sd_ble_gap_device_name_set(&sec_mode, custom_advertising_name,
259+
custom_advertising_name_size);
260+
261+
if (err) {
262+
LOG_ERR("Failed to change advertising name, err %d", err);
263+
return 0;
264+
}
265+
266+
err = ble_adv_data_encode(&ble_adv_cfg.adv_data, ble_adv.enc_adv_data[0],
267+
&ble_adv.adv_data.adv_data.len);
268+
269+
if (err) {
270+
LOG_ERR("Failed to update advertising data, err %d", err);
271+
return 0;
272+
}
273+
}
274+
240275
err = ble_adv_start(&ble_adv, BLE_ADV_MODE_FAST);
241276

242277
if (err) {
243278
LOG_ERR("Failed to start advertising, err %d", err);
244279
return 0;
245280
}
246281

247-
LOG_INF("Advertising as %s", CONFIG_BLE_ADV_NAME);
282+
LOG_INF("Advertising as %s", (custom_advertising_name_size > 0 ? custom_advertising_name :
283+
CONFIG_BLE_ADV_NAME));
248284

249285
while (notification_sent == false && device_disconnected == false) {
250286
while (LOG_PROCESS()) {

0 commit comments

Comments
 (0)