sd_ble_gatts_value_set function produces Error Code 0x0005

The following code snippets produces Error code 0x0005 when trying to update a characteristic value via custom characteristic programming nd connected to nRFConnect app:

In a *.h file

/**@brief Custom Service structure. This contains various status information for the service. */
struct ble_cus_data_s
{
ble_cus_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Custom Service. */
uint16_t service_handle; /**< Handle of Custom Service (as provided by the BLE stack). */
ble_gatts_char_handles_t EPOCH_value_handles; /**< Handles related to the EPOCH Value characteristic. */
ble_gatts_char_handles_t Alco_value_handles; /**< Handles related to the Alcohol Value characteristic. */
ble_gatts_char_handles_t RelHum_value_handles; /**< Handles related to the Relative Humidity Value characteristic. */
ble_gatts_char_handles_t Temp_value_handles; /**< Handles related to the Temperature Value characteristic. */
uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
uint8_t uuid_type;
};

typedef struct ble_cus_data_s ble_cus_data_t; 

In a separate *.c:

static uint32_t custom_value_Alcohol_char_add(ble_cus_data_t * p_cus, const ble_cus_init_t * p_cus_init)
{
uint32_t err_code;
ble_gatts_char_md_t char_md;
ble_gatts_attr_md_t cccd_md;
ble_gatts_attr_t attr_char_value;
ble_uuid_t ble_uuid;
ble_gatts_attr_md_t attr_md;
// uint8_t initval = {0};

memset(&char_md, 0, sizeof(char_md));

char_md.char_props.read = 1;
char_md.char_props.write = 0; //No Write Author
char_md.char_props.notify = 1;
char_md.p_char_user_desc = NULL;
char_md.p_char_pf = NULL;
char_md.p_user_desc_md = NULL;
char_md.p_cccd_md = NULL;
char_md.p_sccd_md = NULL;

memset(&attr_md, 0, sizeof(attr_md));

attr_md.read_perm = p_cus_init->custom_value_char_attr_md.read_perm;
attr_md.write_perm = p_cus_init->custom_value_char_attr_md.write_perm;
attr_md.vloc = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth = 0;
attr_md.wr_auth = 0;
attr_md.vlen = 0;

ble_uuid.type = p_cus->uuid_type;
ble_uuid.uuid = UUID_ALCO_CHAR_DATA;

memset(&attr_char_value, 0, sizeof(attr_char_value));

attr_char_value.p_uuid = &ble_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = sizeof(uint32_t);//Alcohol Sensor Data Init Len
attr_char_value.init_offs = 0;
attr_char_value.max_len = sizeof(uint32_t);//Alcohol Sensor Data Max Len
//attr_char_value.p_value = (uint8_t *)&initval;

memset(&cccd_md, 0, sizeof(cccd_md));

// Read operation on Cccd should be possible without authentication.
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);

cccd_md.vloc = BLE_GATTS_VLOC_STACK;
/* This code belongs in custom_value_char_add() in ble_cus.c*/
char_md.char_props.notify = 1;
char_md.p_cccd_md = &cccd_md;

err_code = sd_ble_gatts_characteristic_add(p_cus->service_handle,
&char_md,
&attr_char_value,
&p_cus->Alco_value_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}

return NRF_SUCCESS;
}

uint32_t ble_Alco_PPM_value_update(ble_cus_data_t * p_cus, uint32_t Alco_PPM)
{
NRF_LOG_INFO("In ble_Alco_PPM_value_update. \r\n");
if (p_cus == NULL)
{
return NRF_ERROR_NULL;
}
//Reorganize Alco value into 4 bytes
static uint8_t Alco_PPM_bytes[4];
Alco_PPM_bytes[0] = (uint8_t) (Alco_PPM >> 24);
Alco_PPM_bytes[1] = (uint8_t) (Alco_PPM >> 16);
Alco_PPM_bytes[2] = (uint8_t) (Alco_PPM >> 8);
Alco_PPM_bytes[3] = (uint8_t) (Alco_PPM);
uint32_t err_code = NRF_SUCCESS;
ble_gatts_value_t gatts_value;

// Initialize value struct.
memset(&gatts_value, 0, sizeof(gatts_value));

gatts_value.len = 4;//sizeof(Alco_PPM);
gatts_value.offset = 0;
gatts_value.p_value = Alco_PPM_bytes;

// Update database.
err_code = sd_ble_gatts_value_set(p_cus->conn_handle,   //Error  0x00005 is produced
p_cus->Alco_value_handles.value_handle,
&gatts_value);
if (err_code != NRF_SUCCESS)
{
return err_code;
}

// Send value if connected and notifying.
if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID) )
{
ble_gatts_hvx_params_t hvx_params;

memset(&hvx_params, 0, sizeof(hvx_params));
//if ((p_cus->props.notify!=0) || (p_usr_attr->props.indicate!=0))
//{
// if (p_usr_attr->props.notify!=0)
// hvx_params.type=BLE_GATT_HVX_NOTIFICATION;
// if (p_usr_attr->props.indicate!=0)
// hvx_params.type=BLE_GATT_HVX_INDICATION;
hvx_params.handle = p_cus->Alco_value_handles.value_handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = gatts_value.offset;
hvx_params.p_len = &gatts_value.len;
hvx_params.p_data = gatts_value.p_value;

err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params);
if (err_code != NRF_SUCCESS &&
err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
err_code != NRF_ERROR_INVALID_STATE &&
err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING &&
err_code != BLE_GATTS_EVT_SYS_ATTR_MISSING &&
/*I'm not entirely sure if ignoring this error could have detrimental effects or not*/
err_code != NRF_ERROR_RESOURCES)
{
APP_ERROR_CHECK(err_code);
}

}
else
{
err_code = NRF_ERROR_INVALID_STATE;
}


return err_code;
}

It was noticed that the Alco_value_handles is all zeroes when a breakpoint is set at sd_ble_gatts_value_set line.

Setup:

Related