Bluetooth LE create address id failed err -12 after enabling settings config

BOARD: nrf54l15 DK

SDK: 3.1.0

Hi, 

I am trying to setup my low power preipheral with a new ble address via bt_id_create.

Everthing works fine when I use the exact code from the ble fundementals course l2/l2_e3_sol.

To enable low power features like bonding and direct advertising I copied the kconfig from this sample https://github.com/zephyrproject-rtos/zephyr/tree/main/samples/bluetooth/direct_adv

Due to the 

 CONFIG_BT_SETTINGS=y
 CONFIG_SETTINGS=y
I have to call settings_load(); I think at least and than init the ble stack.
But after that the address creation failes with err -12
Any idea how this could be handled?
See the code and the config below:
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gap.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/addr.h>

static const struct bt_le_adv_param *adv_param = BT_LE_ADV_PARAM(
	(BT_LE_ADV_OPT_CONN |
	 BT_LE_ADV_OPT_USE_IDENTITY), /* Connectable advertising and use identity address */
	800,						  /* Min Advertising Interval 500ms (800*0.625ms) */
	801,						  /* Max Advertising Interval 500.625ms (801*0.625ms) */
	NULL);						  /* Set to NULL for undirected advertising */

LOG_MODULE_REGISTER(Lesson2_Exercise3, LOG_LEVEL_INF);

#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

static struct k_work adv_work;
static const struct bt_data ad[] = {
	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};

static void adv_work_handler(struct k_work *work)
{

	int err = bt_le_adv_start(adv_param, ad, ARRAY_SIZE(ad), NULL, 0);

	if (err)
	{
		printk("Advertising failed to start (err %d)\n", err);
		return;
	}
}
static void advertising_start(void)
{
	k_work_submit(&adv_work);
}
static void recycled_cb(void)
{
	printk("Connection object available from previous conn. Disconnect is complete!\n");
	advertising_start();
}

BT_CONN_CB_DEFINE(conn_callbacks) = {
	.recycled = recycled_cb,
};

int main(void)
{
	int err;

	err = bt_enable(NULL);
	if (err)
	{
		LOG_ERR("Bluetooth init failed (err %d)\n", err);
		return -1;
	}

	// Load settings because
	// CONFIG_BT_SETTINGS=y
	// CONFIG_SETTINGS=y
	if (IS_ENABLED(CONFIG_SETTINGS))
	{
	settings_load();
	}
	bt_addr_le_t addr;
	err = bt_addr_le_from_str("FF:EE:DD:CC:BB:AA", "random", &addr);
	if (err)
	{
		printk("Invalid BT address (err %d)\n", err);
	}

	err = bt_id_create(&addr, NULL);
	if (err < 0)
	{
		printk("Creating new ID failed (err %d)\n", err);
	}

	k_sleep(K_MSEC(100));

	k_work_init(&adv_work, adv_work_handler);
	advertising_start();
}
# Logger module
CONFIG_LOG=y

# Bluetooth LE
CONFIG_BT=y
# STEP 1 - Include the Peripheral Role support
CONFIG_BT_PERIPHERAL=y
# STEP 2 - Change the Bluetooth LE device name to Nordic_Peripheral
CONFIG_BT_DEVICE_NAME="Nordic_Peripheral"

# Increase stack size for the main thread and System Workqueue
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
CONFIG_MAIN_STACK_SIZE=2048

# alles für bonding und direct advertising
CONFIG_BT_SMP=y
CONFIG_BT_SIGNING=y
CONFIG_BT_DIS=y
CONFIG_BT_ATT_PREPARE_COUNT=1
CONFIG_BT_PRIVACY=y
CONFIG_BT_DEVICE_APPEARANCE=833

CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
CONFIG_BT_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y
Parents
  • Hi,

    The ordering of bt_enable, bt_id_create() and settings_load() need to be right for this to work, and I am wondering a bit about the configs as well. This is a working example of a modified l2/l2_e3_sol with settings and bonding enabled, while setting a new address.

    Full main.c:

    /*
     * Copyright (c) 2023 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    #include <zephyr/kernel.h>
    #include <zephyr/logging/log.h>
    #include <zephyr/bluetooth/bluetooth.h>
    #include <zephyr/bluetooth/conn.h>
    #include <zephyr/bluetooth/gap.h>
    /* STEP 3.2.1 - Include the header file of the UUID helper macros and definitions */
    #include <zephyr/bluetooth/uuid.h>
    /* STEP 4.1 - Include the header file for managing Bluetooth LE addresses */
    #include <zephyr/bluetooth/addr.h>
    
    #include <dk_buttons_and_leds.h>
    #include <zephyr/settings/settings.h>
    
    
    /* STEP 5.1 - Create the advertising parameter for connectable advertising */
    static const struct bt_le_adv_param *adv_param = BT_LE_ADV_PARAM(
    	(BT_LE_ADV_OPT_CONN |
    	 BT_LE_ADV_OPT_USE_IDENTITY), /* Connectable advertising and use identity address */
    	800, /* Min Advertising Interval 500ms (800*0.625ms) */
    	801, /* Max Advertising Interval 500.625ms (801*0.625ms) */
    	NULL); /* Set to NULL for undirected advertising */
    
    LOG_MODULE_REGISTER(Lesson2_Exercise3, LOG_LEVEL_INF);
    
    #define DEVICE_NAME CONFIG_BT_DEVICE_NAME
    #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
    
    #define RUN_STATUS_LED DK_LED1
    #define RUN_LED_BLINK_INTERVAL 1000
    static struct k_work adv_work;
    static const struct bt_data ad[] = {
    	/* STEP 3.1 - Set the flags and populate the device name in the advertising packet */
    	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    
    };
    
    static const struct bt_data sd[] = {
    	/* STEP 3.2.2 - Include the 16-bytes (128-Bits) UUID of the LBS service in the scan response packet */
    	BT_DATA_BYTES(BT_DATA_UUID128_ALL,
    		      BT_UUID_128_ENCODE(0x00001523, 0x1212, 0xefde, 0x1523, 0x785feabcd123)),
    };
    /* STEP 5.2 - Resume advertising after a disconnection */
    static void adv_work_handler(struct k_work *work)
    {
    	int err = bt_le_adv_start(adv_param, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    
    	if (err) {
    		printk("Advertising failed to start (err %d)\n", err);
    		return;
    	}
    
    	printk("Advertising successfully started\n");
    }
    static void advertising_start(void)
    {
    	k_work_submit(&adv_work);
    }
    static void recycled_cb(void)
    {
    	printk("Connection object available from previous conn. Disconnect is complete!\n");
    	advertising_start();
    }
    
    BT_CONN_CB_DEFINE(conn_callbacks) = {
    	.recycled = recycled_cb,
    };
    
    int main(void)
    {
    	int blink_status = 0;
    	int err;
    
    	LOG_INF("Starting Lesson 2 - Exercise 3 \n");
    
    	err = dk_leds_init();
    	if (err) {
    		LOG_ERR("LEDs init failed (err %d)\n", err);
    		return -1;
    	}
    
    	/* STEP 4.2 - Change the random static address */
    	bt_addr_le_t addr;
    	err = bt_addr_le_from_str("FF:EE:DD:CC:BB:AA", "random", &addr);
    	if (err) {
    		LOG_ERR("Invalid BT address (err %d)\n", err);
    	}
    
    	err = bt_id_create(&addr, NULL);
    	if (err < 0) {
    		LOG_ERR("Creating new ID failed (err %d)\n", err);
    	}
    
    	err = bt_enable(NULL);
    	if (err) {
    		LOG_ERR("Bluetooth init failed (err %d)\n", err);
    		return -1;
    	}
    
    	if (IS_ENABLED(CONFIG_SETTINGS))
    	{
    		settings_load();
    	}
    
    	LOG_INF("Bluetooth initialized\n");
    	/* STEP 5.3 - Start connectable advertising */
    	k_work_init(&adv_work, adv_work_handler);
    	advertising_start();
    
    	LOG_INF("Advertising successfully started\n");
    
    	for (;;) {
    		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
    		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
    	}
    }
    

    Additional configs:

    CONFIG_SETTINGS_RUNTIME=y
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y
    CONFIG_BT_SMP=y
    CONFIG_BT_SIGNING=y
    CONFIG_BT_BONDABLE=y

  • Hey, 

    thanks for your suggestions.

    I added the missing CONFIGs 

    CONFIG_SETTINGS_RUNTIME=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    And reordered my main to your exact structure, but no luck.
    Nothing changes. 
    And again I am wondering when and how the CONFIGs are applied. My code can bond with my central even though CONFIG_BT_BONDABLE=y is not in my config 
  • Hi,

    lou_42 said:
    And reordered my main to your exact structure, but no luck.

    That is odd. Can you try the exact same main file I uploaded and test again? Also make sure you do a full erase of the board when you flash in case there is other data in the settings partition.

    lou_42 said:
    And again I am wondering when and how the CONFIGs are applied. My code can bond with my central even though CONFIG_BT_BONDABLE=y is not in my config 

    Kconfig is a hiarachy of default values and values set in hither levels such as SoC and board level. The CONFIG_BT_BONDABLE=y snuk in in my copy-pasting but is not needed, as it is enabled by default.

  • Hi, 

    tried the exact same code and config. Still having the default address. But seeing an err -22 in the output.

    The main problem I am having on err -22 in general is how to figure out what exactly is the invalid parameter.


    *** Booting nRF Connect SDK v3.1.0-6c6e5b32496e ***
    *** Using Zephyr OS v4.1.99-1612683d4010 ***
    [00:00:00.004,796] <inf> Lesson2_Exercise3: Starting Lesson 2 - Exercise 3 
    
    [00:00:00.004,835] <err> Lesson2_Exercise3: Creating new ID failed (err -22)
    
    [00:00:00.006,641] <inf> fs_nvs: 2 Sectors of 4096 bytes
    [00:00:00.006,646] <inf> fs_nvs: alloc wra: 0, fa0
    [00:00:00.006,650] <inf> fs_nvs: data wra: 0, 30
    [00:00:00.006,725] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
                                                fc de 41 eb a2 d1 42 24  00 b5 f8 57 9f ac 9d 9e |..A...B$ ...W....
                                                aa c9 b4 34                                      |...4             
    [00:00:00.008,278] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
    [00:00:00.008,291] <inf> bt_hci_core: HW Variant: nRF54Lx (0x0005)
    [00:00:00.008,310] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 252.16862 Build 1121034987
    [00:00:00.008,449] <inf> bt_hci_core: No ID address. App must call settings_load()
    [00:00:00.008,688] <inf> bt_hci_core: HCI transport: SDC
    [00:00:00.008,736] <inf> bt_hci_core: Identity: F2:C8:DA:BF:FE:A1 (random)
    [00:00:00.008,750] <inf> bt_hci_core: HCI: version 6.1 (0x0f) revision 0x3069, manufacturer 0x0059
    [00:00:00.008,763] <inf> bt_hci_core: LMP: version 6.1 (0x0f) subver 0x3069
    [00:00:00.011,680] <inf> Lesson2_Exercise3: Bluetooth initialized
    
    Advertising successfully started
    [00:00:00.012,274] <inf> Lesson2_Exercise3: Advertising successfully started
    
    

  • I am not able to explain that. I have attached my modified project includign the build folder. Can you try the hex file there first and verify that it works, and then move on to using the exact same main.c and prj.conf yourself? l2_e3_sol.zip

    For reference, this is the log I get (and I verified that the device advertises with the specificied address):

    *** Booting nRF Connect SDK v3.1.0-6c6e5b32496e ***
    *** Using Zephyr OS v4.1.99-1612683d4010 ***
    [00:13:06.429,704] <inf> Lesson2_Exercise3: Starting Lesson 2 - Exercise 3 
    
    [00:13:06.431,793] <inf> fs_nvs: 2 Sectors of 4096 bytes
    [00:13:06.431,799] <inf> fs_nvs: alloc wra: 0, fd0
    [00:13:06.431,803] <inf> fs_nvs: data wra: 0, 0
    [00:13:06.431,867] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
                                                fc de 41 eb a2 d1 42 24  00 b5 f8 57 9f ac 9d 9e |..A...B$ ...W....
                                                aa c9 b4 34                                      |...4             
    [00:13:06.433,320] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
    [00:13:06.433,333] <inf> bt_hci_core: HW Variant: nRF54Lx (0x0005)
    [00:13:06.433,346] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 252.16862 Build 1121034987
    [00:13:06.433,505] <inf> bt_hci_core: HCI transport: SDC
    [00:13:06.433,555] <inf> bt_hci_core: Identity: FF:EE:DD:CC:BB:AA (random)
    [00:13:06.433,569] <inf> bt_hci_core: HCI: version 6.1 (0x0f) revision 0x3069, manufacturer 0x0059
    [00:13:06.433,582] <inf> bt_hci_core: LMP: version 6.1 (0x0f) subver 0x3069
    [00:13:06.435,667] <inf> Lesson2_Exercise3: Bluetooth initialized
    
    Advertising successfully started
    [00:13:06.436,237] <inf> Lesson2_Exercise3: Advertising successfully started

Reply
  • I am not able to explain that. I have attached my modified project includign the build folder. Can you try the hex file there first and verify that it works, and then move on to using the exact same main.c and prj.conf yourself? l2_e3_sol.zip

    For reference, this is the log I get (and I verified that the device advertises with the specificied address):

    *** Booting nRF Connect SDK v3.1.0-6c6e5b32496e ***
    *** Using Zephyr OS v4.1.99-1612683d4010 ***
    [00:13:06.429,704] <inf> Lesson2_Exercise3: Starting Lesson 2 - Exercise 3 
    
    [00:13:06.431,793] <inf> fs_nvs: 2 Sectors of 4096 bytes
    [00:13:06.431,799] <inf> fs_nvs: alloc wra: 0, fd0
    [00:13:06.431,803] <inf> fs_nvs: data wra: 0, 0
    [00:13:06.431,867] <inf> bt_sdc_hci_driver: SoftDevice Controller build revision: 
                                                fc de 41 eb a2 d1 42 24  00 b5 f8 57 9f ac 9d 9e |..A...B$ ...W....
                                                aa c9 b4 34                                      |...4             
    [00:13:06.433,320] <inf> bt_hci_core: HW Platform: Nordic Semiconductor (0x0002)
    [00:13:06.433,333] <inf> bt_hci_core: HW Variant: nRF54Lx (0x0005)
    [00:13:06.433,346] <inf> bt_hci_core: Firmware: Standard Bluetooth controller (0x00) Version 252.16862 Build 1121034987
    [00:13:06.433,505] <inf> bt_hci_core: HCI transport: SDC
    [00:13:06.433,555] <inf> bt_hci_core: Identity: FF:EE:DD:CC:BB:AA (random)
    [00:13:06.433,569] <inf> bt_hci_core: HCI: version 6.1 (0x0f) revision 0x3069, manufacturer 0x0059
    [00:13:06.433,582] <inf> bt_hci_core: LMP: version 6.1 (0x0f) subver 0x3069
    [00:13:06.435,667] <inf> Lesson2_Exercise3: Bluetooth initialized
    
    Advertising successfully started
    [00:13:06.436,237] <inf> Lesson2_Exercise3: Advertising successfully started

Children
No Data
Related