This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to advertise a different device name in the secure_bootloader example code?

Hello,

I am trying to change the device name in the ble advertisement in the secure_bootloader example.

Module model: nrf52832

Sdk: 15.2

I have tried to put the following code just above the main function:

static char deviceName[20] = "abc";
static nrf_dfu_adv_name_t _name;
uint32_t nrf_dfu_init_user(void)
{
    nrf_dfu_adv_name_t* name = &_name;
    uint32_t err;
    uint32_t deviceId = 12;
    NRF_LOG_DEBUG("***in custom nrf_dfu_init_user, id =  %d", deviceId);

    snprintf(&deviceName[strlen(deviceName)], strlen(deviceName), "%d", deviceId);
    
    strcpy(name->name, deviceName);
    name->len = strlen(deviceName);

    NRF_LOG_DEBUG("***advertise name length = %d", strlen(deviceName));
    NRF_LOG_DEBUG("***advertise name = %s", deviceName);

    err = nrf_dfu_settings_adv_name_write(name);
    return err;
}

It does not work as the error code of NRF_ERROR_SVC_HANDLER_MISSING is returned at the nrf_dfu_settings_adv_name_write() call.

It looks like the softdevice is not enabled when this function is being called.

What is the correct way to change the device name in the advertisement dynamically without touching the sdk?

Changing the NRF_DFU_BLE_ADV_NAME in the sdk_config.h file works for static names, but I will need to attach a number at the end of the static name so I need to do it during run time.

Many thanks.

Parents
  • Hei Edward,

    The function nrf_dfu_settings_adv_name_write() is made for setting the advertising name of the bootloader FROM the application. 
    If you are changing the device name from inside the bootloader, you should call sd_ble_gap_device_name_set() instead.
    Note that by default the bootloader will call this function inside gap_params_init(). So if you want to change that you can edit gap_params_init() and choose the dynamic name you wants. 
    After that if you also want to change the name in run time , you can call sd_ble_gap_device_name_set() again. 

Reply
  • Hei Edward,

    The function nrf_dfu_settings_adv_name_write() is made for setting the advertising name of the bootloader FROM the application. 
    If you are changing the device name from inside the bootloader, you should call sd_ble_gap_device_name_set() instead.
    Note that by default the bootloader will call this function inside gap_params_init(). So if you want to change that you can edit gap_params_init() and choose the dynamic name you wants. 
    After that if you also want to change the name in run time , you can call sd_ble_gap_device_name_set() again. 

Children
  • Hi Hung,

    Thanks for your prompt reply.

    The function gap_params_init() is called via nrf_bootloader_init() in main, which is deeply buried in the sdk, It's not a good idea to touch any of the sdk, because the same change will need to be applied to future releases of the sdk, if sdk needs to be updated in the future, and unforeseen problems may arise from touching the sdk.

    "After that if you also want to change the name in run time , you can call sd_ble_gap_device_name_set() again. "

    Are you referring to the runtime in bootloader mode? Do you mind pointing out where to call the function?

    The main function from secure_bootloader is shown below:

    int main(void)
    {
        uint32_t ret_val;
    
        // Protect MBR and bootloader code from being overwritten.
        ret_val = nrf_bootloader_flash_protect(0, MBR_SIZE, false);
        APP_ERROR_CHECK(ret_val);
        ret_val = nrf_bootloader_flash_protect(BOOTLOADER_START_ADDR, BOOTLOADER_SIZE, false);
        APP_ERROR_CHECK(ret_val);
    
        (void) NRF_LOG_INIT(nrf_bootloader_dfu_timer_counter_get);
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("Inside main");
    
        ret_val = nrf_bootloader_init(dfu_observer);
        APP_ERROR_CHECK(ret_val);
    
        // Either there was no DFU functionality enabled in this project or the DFU module detected
        // no ongoing DFU operation and found a valid main application.
        // Boot the main application.
        nrf_bootloader_app_start();
    
        // Should never be reached.
        NRF_LOG_INFO("After main");
    }

    The main loop will be entered from nrf_bootloader_init() in line 16, we can't call any function after nrf_bootloader_init() since it won't return (which enters the main loop), and we can't call sd_ble_gap_device_name_set() before nrf_bootloader_init() since the softdevice is enabled after the function. 

    The code snippet is taken from the nrf_bootloader.c in the sdk regarding nrf_bootloader_init():

    if (dfu_enter)
    {
        nrf_bootloader_wdt_init();
    
        scheduler_init();
    
        // Clear all DFU stop flags.
        dfu_enter_flags_clear();
    
        // Call user-defined init function if implemented
        ret_val = nrf_dfu_init_user();
        if (ret_val != NRF_SUCCESS)
        {
            return NRF_ERROR_INTERNAL;
        }
    
        nrf_bootloader_dfu_inactivity_timer_restart(initial_timeout, inactivity_timeout);
    
        ret_val = nrf_dfu_init(dfu_observer);
        if (ret_val != NRF_SUCCESS)
        {
            return NRF_ERROR_INTERNAL;
        }
    
        NRF_LOG_DEBUG("Enter main loop");
        loop_forever(); // This function will never return.
        NRF_LOG_ERROR("Should never come here: After looping forever.");
    }

    The soft device is enabled in the call nrf_dfu_init() in line 19, and the main loop is entered in line 26.

    And the sd_ble_gap_device_name_set() should be called between line 19 and line 26, to have the device name changed.

    Again this will touch the sdk which is not ideal.

    Without touching the sdk, what can be done to change the device name in the ble advertisement?

    Many thanks.

  • Hi Edward, 
    Could you please describe what exactly you want to achieve ? How do you plan to change the name ?
    It's possible to change the name from the application (not the bootloader) if you don't want to modify the bootloader code. 


  • Hi Hung,

    The product has an application and a bootloader (and running a softdevice!). The product has a device serial number which is stored in the uicr region. The application currently advertises the serial number as part of the device name 'device001'. When the application needs upgrading, it will go in to the bootloader mode do to the firmware upgrade via any handset(Android/ios). At this point, all the devices advertise as 'dfuTarg', which does not show any identify of the device.

    The goal here is to have some identity on the device in the bootloader mode, which can be achieved by setting the advertising device name to be 'dfuTarg001'. Note that the dynamic numerical part of the name matters, and currently that's stored in uicr. Mac addrrss works on Android but not ios so that's not an option..

    Thanks 

  • Hi Edward, 
    In this case you can have a look at the buttonless example and the protocol described here.

    In the example you can change the advertising name of the bootloader from the phone. 

    It's handled inside ble_dfu_buttonless_on_ctrl_pt_write(). You can find the adv name is changed by calling nrf_dfu_set_adv_name()

    So you can use the ble_dfu_unbonded.c library unchanged and use the phone to change the advertising name, or you can modify the application so that it call nrf_dfu_set_adv_name() automatically and set the device's serial number as the adv name. 
    Note that this call should be done inside the application, before it jumps to the bootloader. 

Related