I am new to GATT services and characteristics. I have written one custom service with two characteristics for my custom board. I have used nrf 52832 with sdk 15.2.0 and referred following link https://github.com/bjornspockeli/custom_ble_service_example to build cutsom service. I want to store values enter through gatt charcteristics to fds. The snippet of code as follows
// ble_custom.c
uint8_t tempnum1[2] = {0};
uint8_t tempnum2[2] = {0};
static void on_write(ble_cus_t * p_cus, ble_evt_t const * p_ble_evt)
{
ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
uint32_t err_code;
// Custom Value Characteristic Written to.
if (p_evt_write->handle == p_cus->custom_value_handles_1.value_handle)
{
memset(tempnum1, 0, sizeof(tempnum1));
tempnum1[0] = p_evt_write->data[0] ;
tempnum2[1] = p_evt_write->data[1] ;
err_code = fds_update(tempnum1,sizeof(tempnum1),FILE_ID,REC_KEY);
APP_ERROR_CHECK(err_code);
}
if (p_evt_write->handle == p_cus->custom_value_handles_2.value_handle)
{
memset(tempnum2, 0, sizeof(tempnum2));
tempnum2[0] = p_evt_write->data[0] ;
tempnum2[1] = p_evt_write->data[1] ;
err_code = fds_update(tempnum2,sizeof(tempnum1),FILE_ID2,REC_KEY2);
APP_ERROR_CHECK(err_code);
}
}
In main after disconnecting event I read data. The snippet of code as follows.
// main.c
uin8_t num1[2]={0};
uint8_t num2[2] = {0};
//Read data from FDS
void read_data_from_fds()
{ ret_code_t err_code;
err_code = load_from_fds(num1,sizeof(num1),FILE_ID,REC_KEY);
APP_ERROR_CHECK(err_code);
err_code = load_from_fds(num2,sizeof(num2),FILE_ID2,REC_KEY2);
APP_ERROR_CHECK(err_code);
}
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
ret_code_t err_code;
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
APP_ERROR_CHECK(err_code);
break;
case BLE_GAP_EVT_DISCONNECTED:
read_data_from_fds();
advertising_start();
break;
}
The snippet of fds as follows
//fds.c
ret_code_t fds_update(uint8_t const * p_buff, uint32_t size,uint16_t fileid,uint16_t recKey)
{
ret_code_t err_code;
fds_find_token_t ftok;
static uint8_t m_deadbeef[2] = {0};
// Always clear token before running new file/record search.
memset(&ftok, 0x00, sizeof(fds_find_token_t));
// Search for data in FLASH.
err_code = fds_record_find(fileid, recKey, &m_record_desc, &ftok);
// If there is no record with given key and file ID,
// create default message and store in FLASH.
if (err_code == FDS_SUCCESS)
{
// Prepare record structure.
prepare_record(p_buff, size,fileid,recKey);
// Update FLASH file with new message.
err_code = fds_record_update(&m_record_desc, &m_record);
if (err_code == FDS_ERR_NO_SPACE_IN_FLASH)
{
err_code = fds_gc();
}
}
return err_code;
}
static ret_code_t fds_create(uint8_t const * p_buff, uint32_t size,uint16_t fileid,uint16_t recKey)
{
ret_code_t err_code;
// Prepare record structure.
prepare_record(p_buff, size,fileid,recKey);
// Create FLASH file with data
err_code = fds_record_write(&m_record_desc, &m_record);
if (err_code == FDS_ERR_NO_SPACE_IN_FLASH)
{
err_code = fds_gc();
}
return err_code;
}
ret_code_t load_from_fds(uint8_t * p_buff, uint32_t p_size,uint16_t fileid,uint16_t recKey)
{
ret_code_t err_code;
fds_find_token_t ftok;
fds_flash_record_t flash_record;
// Always clear token before running new file/record search.
memset(&ftok, 0x00, sizeof(fds_find_token_t));
// Search for data in FLASH.
err_code = fds_record_find(fileid, recKey, &m_record_desc, &ftok);
// If there is no record with given key and file ID,
// create default message and store in FLASH.
if (err_code == FDS_SUCCESS)
{
// Open record for read.
err_code = fds_record_open(&m_record_desc, &flash_record);
VERIFY_SUCCESS(err_code);
// Access the record through the flash_record structure.
memcpy(p_buff,
flash_record.p_data,
flash_record.p_header->length_words * sizeof(uint32_t));
// Close the record when done.
err_code = fds_record_close(&m_record_desc);
}
else if (err_code == FDS_ERR_NOT_FOUND)
{
// Create default data
// Create record with default data message.
err_code = fds_create(p_buff,p_size,fileid,recKey);
}
return err_code;
}.
Whenever I used service with one characteristics it updates data correctly for both characteristics.. But when I have used service with 2 characteristics then first characteristics data is updated to fds correctly but for second characteristics GATT event is disconnected and data is not updated to fds.