I had successfully write a structure data to fds. After which, I would like to update the same file and record key with new values of a structure. After updating, I will read one of the element from structure to ensure value is successfully updated.
I tried to open the record and copy the whole structure data out to another structure so that I can read it outside the fds_read function, but it seems that the copying in fds_read function is not working, as no data is printed out.
snippet of code in main:
struct Person P1;
uint16_t length = sizeof(P1)/sizeof(uint32_t);
err_code = _fds_write(&P1, length);
APP_ERROR_CHECK(err_code);
while (write_flag == 0);
write_flag = 0;
struct Person P2;
P2 = EmptyStruct;
strcpy(P2.name, "Bob"); //put new value in struct
strcpy(P2.hobby, "Swim"); //put new value in struct
err_code = fds_write(&P2, length);
APP_ERROR_CHECK(err_code);
while (write_flag == 0);
write_flag = 0;
//clear P1
P1 = EmptyStruct;
err_code = fds_read(&P1, length);
SEGGER_RTT_printf(0, "new name from P1 is: %s \n", P1.name); //nothing shown
SEGGER_RTT_printf(0, "new hobby from P1 is: %s \n", P1.hobby); //nothing shown
writing to fds:
static ret_code_t fds_write(struct Person *temp, uint16_t length) {
fds_record_t record = {0};
fds_record_desc_t record_desc = {0};
fds_record_chunk_t record_chunk = {0};
record_chunk.p_data = &temp;
record_chunk.length_words = length;
record.file_id = ACC_FILE_ID; //defined globally previously
record.key = ACC_REC_KEY; //defined globally previously
record.data.p_chunks = &record_chunk;
record.data.num_chunks = 1;
ret_code_t err_code = fds_record_exists(&record_desc);
if (err_code == NRF_SUCCESS) {
err_code = fds_record_update(&record_desc, &record);
SEGGER_RTT_printf(0, "Record ID = %d \r\n",record_desc.record_id);
} else if (err_code == FDS_ERR_NOT_FOUND) {
err_code = fds_record_write(&record_desc, &record);
SEGGER_RTT_printf(0, "Writing Record ID = %d \r\n",record_desc.record_id);
} else {
SEGGER_RTT_printf(0, "fds_record_exists fail: %d\n", err_code);
return err_code;
}
return NRF_SUCCESS;
}
reading from fds:
static ret_code_t fds_read(struct Person *temp, uint16_t length) {
ret_code_t err_code;
fds_flash_record_t flash_record = {0};
fds_record_desc_t record_desc = {0};
fds_find_token_t ftok = {0};
err_code = fds_record_find(ACC_FILE_ID, ACC_REC_KEY, &record_desc, &ftok);
err_code = fds_record_open(&record_desc, &flash_record);
SEGGER_RTT_printf(0, "Found Record ID to read from = %d\r\n",record_desc.record_id);
struct Person *tempstruct;
tempstruct = (struct Person* ) flash_record.p_data;
SEGGER_RTT_printf(0, "name in tempstruct is : %s \n",tempstruct->name);
temp = tempstruct;
SEGGER_RTT_printf(0, "name in temp is : %s \n",temp->name); //nothing shown here
// 2) temp = (struct Person*) malloc(sizeof(struct Person));
// 2) memcpy(temp, (struct Person*) flash_record.p_data, length);
// 3) temp = (struct Person* ) flash_record.p_data;
err_code = fds_record_close(&record_desc);
return NRF_SUCCESS;
}
I tried other ways as commented away as 2) and 3) in the fds_read function. All 3 ways does not work and no value is returned.
Am I doing something wrong when copying the whole structure out? What can I do to get the whole structure from fds_read out? Thank you.