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

Setting up DFU adv name with serial number stored in UICR

Hello,

SDK: 15.0.0

We already have a working bootloader using the default adv name setted in sdk_config.h. But we want to add to the DFU advertising the serial number of the device that it's already stored in UICR CUSTOMER. Looking at ble_dfu_unbonded.c looks like we can perform this from the main app but I can't guess what is the the way to do this.

Regards

  • Hi,

    If you can update the bootloader, then the cleanest would be to just do this in the bootloader, by modifying gap_params_init() in nrf_dfu_ble.c, so that it reads the serial number from FICR and use that instead of NRF_DFU_BLE_ADV_NAME from the bootloaders sdk_config.h.

    However, you are right that the bootloader supports setting the advertising name from the application, provided it is built with NRF_DFU_BLE_REQUIRES_BONDS set to 0 in the bootloaders sdk_config.h. You can see how it is handled in the bootloader by the implementation of gap_params_init():

    /**@brief     Function for initializing GAP.
     *
     * @details   This function sets up all necessary GAP (Generic Access Profile) parameters of
     *            the device. It also sets the permissions and appearance.
     */
    static uint32_t gap_params_init(void)
    {
        uint32_t                err_code;
        ble_gap_conn_sec_mode_t sec_mode;
        uint8_t const *         device_name;
        uint32_t                name_len;
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
    
    #if (!NRF_DFU_BLE_REQUIRES_BONDS)
    
        err_code = gap_address_change();
        VERIFY_SUCCESS(err_code);
    
        if ((m_flags & DFU_BLE_FLAG_USE_ADV_NAME) != 0)
        {
            NRF_LOG_DEBUG("Setting adv name: %s, length: %d", m_adv_name.name, m_adv_name.len);
            device_name = m_adv_name.name;
            name_len    = m_adv_name.len;
        }
        else
    #endif
        {
            NRF_LOG_DEBUG("Using default advertising name");
            device_name = (uint8_t const *)(NRF_DFU_BLE_ADV_NAME);
            name_len    = strlen(NRF_DFU_BLE_ADV_NAME);
        }
    
        err_code = sd_ble_gap_device_name_set(&sec_mode, device_name, name_len);
        VERIFY_SUCCESS(err_code);
    
        err_code = sd_ble_gap_ppcp_set(&m_gap_conn_params);
        return err_code;
    }
    

    The way this normally works is that the DFU master (typically a mobile phone) indicates to the DFU service running in the application that it should set a bootloader device name so that it can search for this name when it should reconnect after the device has entered DFU mode. In this case, this is handled by the DFU_OP_SET_ADV_NAME case in ble_dfu_buttonless_on_ctrl_pt_write() in ble_dfu_unbonded.c:

            case DFU_OP_SET_ADV_NAME:
                if(    (p_evt_write->data[1] > NRF_DFU_ADV_NAME_MAX_LENGTH)
                    || (p_evt_write->data[1] == 0))
                {
                    // New advertisement name too short or too long.
                    rsp_code = DFU_RSP_ADV_NAME_INVALID;
                }
                else
                {
                    memcpy(m_adv_name.name, &p_evt_write->data[2], p_evt_write->data[1]);
                    m_adv_name.len = p_evt_write->data[1];
                    err_code = set_adv_name(&m_adv_name);
                    if (err_code == NRF_SUCCESS)
                    {
                        rsp_code = DFU_RSP_SUCCESS;
                    }
                }
                break;

    You could use this same approach, but instead, call set_adv_name() from your application, after modifying it so that it is no longer static. Taking the buttonless DFU example in SDK 15, you can make it work by minor changes to ble_dfu_unbonded.c and main.c. as shown by these diffs (you could spend a bit more time to make it cleaner).

    diff --git a/components/ble/ble_services/ble_dfu/ble_dfu_unbonded.c b/components/ble/ble_services/ble_dfu/ble_dfu_unbonded.c
    index e6301bc..d13cc5e 100644
    --- a/components/ble/ble_services/ble_dfu/ble_dfu_unbonded.c
    +++ b/components/ble/ble_services/ble_dfu/ble_dfu_unbonded.c
    @@ -75,7 +75,7 @@ static nrf_dfu_adv_name_t   m_adv_name;
      * @retval DFU_RSP_BUSY     Advertisement name was not set because of an ongoing operation.
      * @retval Any other errors from the SVCI interface call.
      */
    -static uint32_t set_adv_name(nrf_dfu_adv_name_t * p_adv_name)
    +uint32_t set_adv_name(nrf_dfu_adv_name_t * p_adv_name)
     {
         uint32_t err_code;
     
    

    diff --git a/examples/ble_peripheral/ble_app_buttonless_dfu/main.c b/examples/ble_peripheral/ble_app_buttonless_dfu/main.c
    index d71ce59..39c6364 100644
    --- a/examples/ble_peripheral/ble_app_buttonless_dfu/main.c
    +++ b/examples/ble_peripheral/ble_app_buttonless_dfu/main.c
    @@ -891,11 +891,19 @@ static void idle_state_handle(void)
     }
     
     
    +uint32_t set_adv_name(nrf_dfu_adv_name_t * p_adv_name);
    +
    +
     /**@brief Function for application main entry.
      */
     int main(void)
     {
         bool erase_bonds;
    +    static nrf_dfu_adv_name_t bootloader_adv_name =
    +    {
    +        .name = "Test",
    +        .len = 4
    +    };
     
         // Initialize.
         log_init();
    @@ -911,6 +919,9 @@ int main(void)
         services_init();
         conn_params_init();
     
    +    uint32_t err_code = set_adv_name(&bootloader_adv_name);
    +    APP_ERROR_CHECK(err_code);
    +
         NRF_LOG_INFO("Buttonless DFU Application started.");
     
         // Start execution.
    

  • Hello,

    I have been testing this solution with, calling set_adv_name() from ble_dfu_evt_handler:

            case BLE_DFU_EVT_BOOTLOADER_ENTER:
                // YOUR_JOB: Write app-specific unwritten data to FLASH, control finalization of this
                //           by delaying reset by reporting false in app_shutdown_handler
                
                m_ready_for_reset = false;
                err_code = set_adv_name(&bootloader_adv_name);
                APP_ERROR_CHECK(err_code);
                m_ready_for_reset = true;
                NRF_LOG_INFO("Device will enter bootloader mode.");
                break;

    but when I switch to bootloader I can't see it with nrFConnect. I know the bootloader t's working because after the configured bootloader timeout my application starts automatically. so it seems to me that boatloader is reading the custom advertise name, but it is blank. debugging the application only I can see that bootloader_adv_name contains the expected name with the serial number

    what am I doing wrong?

    I'm using SDK v17.1.0

    Regards,

    Rocco

  • Hi Rocco,

    There is not enough context here to say much. Can you make a new case where you elaborate show more of what you have done and what you have found when testing/debugging?

Related