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

Change the advertisement name for DFU from application

Hello,

I'm trying to change the BLE local name from the application before rebooting into the secure bootloader.

I've noticed that I could use the nrf_dfu_set_adv_name call to set it but I am not sure if I use it correctly.

In my main I call my dfu_init at the very beginning and try to change the name later on.

int main() 
{
    dfu_init();
    
    // ...
    
    write_dfu_adv_name("FOOBARBAZ");
}   


Here is my implementation to change the name taken from components/ble/ble_services/ble_dfu/ble_dfu_unbonded.c
#include "nrf_dfu_ble_svci_bond_sharing.h"
#include "nrf_sdh_soc.h"

static bool dfu_is_writing_name = false;

/**@brief Define functions for async interface to set new advertisement name for DFU mode.  */
NRF_SVCI_ASYNC_FUNC_DEFINE(NRF_DFU_SVCI_SET_ADV_NAME, nrf_dfu_set_adv_name, nrf_dfu_adv_name_t);

// Register SoC observer for the Buttonless Secure DFU service
NRF_SDH_SOC_OBSERVER(m_dfu_buttonless_soc_obs, BLE_DFU_SOC_OBSERVER_PRIO, ble_dfu_buttonless_on_sys_evt, NULL);

uint32_t nrf_dfu_svci_vector_table_set(void);
uint32_t nrf_dfu_svci_vector_table_unset(void);

void dfu_init()
{
	nrf_dfu_svci_vector_table_set();
	nrf_dfu_set_adv_name_init();
	nrf_dfu_svci_vector_table_unset();
}

void ble_dfu_buttonless_on_sys_evt(uint32_t sys_evt, void * p_context)
{
	if (sys_evt != NRF_EVT_FLASH_OPERATION_ERROR && sys_evt != NRF_EVT_FLASH_OPERATION_SUCCESS)
		return;

	if (!dfu_is_writing_name)
		return;

	dfu_is_writing_name = false;

	if (nrf_dfu_set_adv_name_is_initialized())
	{
		uint32_t err_code = nrf_dfu_set_adv_name_on_sys_evt(sys_evt);

		if (err_code == NRF_SUCCESS)
		{
			// reboot into DFU mode
		}
	}
}

void write_dfu_adv_name(const char* inName)
{
	static nrf_dfu_adv_name_t dfuAdvName;
	dfuAdvName.crc = 0xFFFFFFFF;
	dfuAdvName.len = strlen(inName);
	memcpy(dfuAdvName.name, inName, dfuAdvName.len);
	dfuAdvName.name[dfuAdvName.len] = '\0';
	
	dfu_is_writing_name = true;
	uint32_t err_code = nrf_dfu_set_adv_name(&dfuAdvName);
	if (err_code == NRF_SUCCESS)
	{
	    dfu_is_writing_name = false;
	}
}


It works well and I see the device advertising with the new name but I want to be sure that I am doing everything correctly and not missing something that might crash randomly.
Parents Reply Children
No Data
Related