Hi all,
I'm attempting to develop a feature that will allow our users to personalize their device name. The user will have the ability to change the device name while connected by writing a personalised name to flash memory. When the device is reset (power cycled), I want to read the device name from flash and use that to initialise the advertised device name inside m_ble_gap_params_init using sd_ble_gap_device_name_set on startup.
I'm using FDS to implement this on SDK 12. Writing and setting the name during connection works fine but, when reading back, the data is visible in memory but crashes my application.
Let's say my device name is "abcd", here is the Segger RTT output:
flash_name: abcd?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????
The application hangs.
Here is my function to write the name to memory:
void a_epic_fds_write ( uint32_t * in_data ){
fds_record_t record;
fds_record_desc_t record_desc;
fds_record_chunk_t record_chunk;
fds_find_token_t ftok={0};
record_chunk.p_data = in_data;
record_chunk.length_words = 1;
record.file_id = FILE_ID;
record.key = REC_KEY;
record.data.p_chunks = &record_chunk;
record.data.num_chunks = 1;
uint8_t err_code;
err_code = fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok);
if (err_code != FDS_SUCCESS)
{
BLE_UART_WRITE("FIND_ERR: %d\r\n", err_code);
if (err_code == 10) // Record not found so write it
{
err_code = fds_record_write(&record_desc, &record);
if (err_code == FDS_SUCCESS)
{
BLE_UART_WRITE("RECORD_WRITTEN");
err_code = m_ble_gap_set_device_name ((uint8_t *)in_data, strlen((char*)in_data));
if (err_code != NRF_SUCCESS)
{
BLE_UART_WRITE("SET_NAME_ERR: %d\r\n", err_code);
}
BLE_UART_WRITE("NAME_SET \r\n");
err_code = ble_advertising_restart_without_whitelist();
if (err_code != NRF_SUCCESS)
{
BLE_UART_WRITE("ADV_RESTART_ERR: %d\r\n", err_code);
}
BLE_UART_WRITE("ADV_RESTART_SUCCESS \r\n");
}
else
{
BLE_UART_WRITE("WRITE_ERR: %d\r\n", err_code);
}
}
}
else
{
BLE_UART_WRITE("RECORD_FOUND");
err_code = fds_record_update(&record_desc, &record); // Record already exists, just update it
if (err_code == FDS_SUCCESS)
{
BLE_UART_WRITE("RECORD_UPDATED");
BLE_UART_WRITE("in_data = %s\r\n", record_chunk.p_data);
err_code = m_ble_gap_set_device_name ((uint8_t *)in_data, strlen((char*)in_data));
if (err_code != NRF_SUCCESS)
{
BLE_UART_WRITE("SET_NAME_ERR: %d\r\n", err_code);
}
BLE_UART_WRITE("NAME_SET \r\n");
err_code = ble_advertising_restart_without_whitelist();
if (err_code != NRF_SUCCESS)
{
BLE_UART_WRITE("ADV_RESTART_ERR: %d\r\n", err_code);
}
BLE_UART_WRITE("ADV_RESTART_SUCCESS \r\n");
}
else
{
BLE_UART_WRITE("UPDATE_ERR: %d\r\n", err_code);
}
}
}
And this is the code to read:
static ret_code_t a_epic_fds_read(void)
{
fds_flash_record_t flash_record;
fds_record_desc_t record_desc;
fds_find_token_t ftok={0};
uint32_t err_code;
memset(&ftok, 0, sizeof(fds_find_token_t));
err_code = fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok);
if( err_code != FDS_SUCCESS )
{
SEGGER_RTT_printf(0, "FIND_ERROR: %d\r\n", err_code);
flash_name = (uint8_t*)dName;
return NRF_SUCCESS;
}
else
{
SEGGER_RTT_printf(0, "mBLE_RECORD_FOUND: %d\r\n", err_code);
err_code = fds_record_open(&record_desc, &flash_record);
if ( err_code != FDS_SUCCESS)
{
SEGGER_RTT_printf(0, "OPEN_ERROR: %d\r\n", err_code);
return err_code;
}
else
{
flash_name = (uint8_t *) flash_record.p_data;
SEGGER_RTT_printf(0, "flash_name: %s\r\n", flash_name);
}
}
return NRF_SUCCESS;
}
I'm not sure if I'm writing to memory correctly or whether I'm reading it correctly. Any help would be much appreciated.
Many thanks