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

fds_record_find axes using

Good morning I use the fds function to store and read data in flash memory. FDS_SUCCES is not returned by the fds_record_find function when reading data, so the read function does not work Help me solve this part Below is my code

#include "sdk_config.h"
#include "uuid_data.h"
#include "app_error.h" 
#include <string.h >
#include "app_util.h"

#define FILE_ID 0x1111
#define REC_KEY_ID 0x2222

static void fds_evt_handler(fds_evt_t const * const p_fds_evt)
{
    switch (p_fds_evt->id)
    {
        case FDS_EVT_INIT:
            if (p_fds_evt->result == FDS_SUCCESS)
            {
                // Initialization failed.
				printf("init success \n\r");
			}
			break;
		case FDS_EVT_WRITE:
			if(p_fds_evt->result ==FDS_SUCCESS)
			{
				printf("write success \n\r");
			}
			break;
        default:
            break;
    }
}

ret_code_t beacon_uuid_storage_init(void)
{
	ret_code_t ret = fds_register(fds_evt_handler);
	if (ret != FDS_SUCCESS)
	{
		// Registering of the event handler has failed.
	}

	ret = fds_init();
	if (ret != FDS_SUCCESS)
	{
		// Handle error.
	}
	return NRF_SUCCESS;
}

ret_code_t uuid_storage_write(uint16_t file_id,uint16_t key, uint8_t const *p_data,uint16_t length)
{
	fds_record_t record;
	fds_record_desc_t record_desc;
	fds_record_chunk_t record_chunk;
	
	record_chunk.p_data = p_data;
	record_chunk.length_words = BYTES_TO_WORDS(length);
	
	record.file_id = file_id;
	record.key = key;
	record.data.num_chunks = 1;
	record.data.p_chunks = &record_chunk;
	
	ret_code_t ret;
	
	ret = fds_record_write(&record_desc,&record);
	if(ret != FDS_SUCCESS)
	{
		printf("write fail \n\r");
		return ret;
	}
	else if(ret == FDS_SUCCESS)
	{
		printf("write success \n\r");
		return ret;
	}
	
}

ret_code_t uuid_storage_read(uint16_t file_id,uint16_t key,uint8_t *p_data)
{
	ret_code_t ret;
	fds_flash_record_t flash_record;
	fds_record_desc_t record_desc;
	fds_find_token_t ftok ;
	memset(&ftok, 0x00, sizeof(fds_find_token_t));
	
	 while (fds_record_find(file_id, key, &record_desc, &ftok) == FDS_SUCCESS)
	{
		printf("read success \n\r");
		ret = fds_record_open(&record_desc,&flash_record);
	
		memcpy(p_data , flash_record.p_data , (flash_record.p_header->tl.length_words * BYTES_PER_WORD));
	
		ret = fds_record_close(&record_desc);
	
		if(ret != FDS_SUCCESS)
		{
			printf("close fail \n\r");
			return ret;
		}
		return ret;
	}
}


ret_code_t uuid_del(uint16_t file_id,uint16_t key)
{
	fds_flash_record_t flash_record;
	fds_record_desc_t descriptor;
	fds_find_token_t ftok = {0};

	ret_code_t ret = fds_record_find(file_id,key,&descriptor,&ftok);
	ret = fds_record_delete(&descriptor);
	if (ret != FDS_SUCCESS)
	{
		printf("del fail \n\r");
		// Error.
	}
}
Related