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

sd_ble_gatts_hvx() freezes (doesn't return)

Hello,

After a characteristic (with write and notify properties) is written, the nRF51 sends some data through the UART. Then it receives data, also through the UART, and send a characteristic notification using sd_ble_gatts_hvx() function.

However this function never return. Since I don't have access to its code, can someone tell me what's going wrong?

This is how I add the characteristic:

static uint32_t vm_char_add(ble_pks_t * p_pks, const ble_paykey_init_t * p_pks_init)
{
	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;

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

    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;

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

	char_md.char_props.read	  = 0;
	char_md.char_props.write  = 1;
	char_md.char_props.write_wo_resp = 0;
	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         = &cccd_md;
	char_md.p_sccd_md         = NULL;

	ble_uuid.type = p_pks->uuid_type;
	ble_uuid.uuid = BLE_UUID_PKS_VM_CHAR;

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

	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);

	attr_md.vloc    = BLE_GATTS_VLOC_STACK;
	attr_md.rd_auth = 0;
	attr_md.wr_auth = 0;
	attr_md.vlen    = 1;

	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(uint8_t);
	attr_char_value.init_offs = 0;
	attr_char_value.max_len   = BLE_PKS_MAX_DATA_LEN;
	attr_char_value.p_value = NULL;

	return sd_ble_gatts_characteristic_add(p_pks->service_handle,
										   &char_md,
										   &attr_char_value,
										   &p_pks->vm_handles);
}

And this is how I send a notification:

static uint32_t send_notification(ble_pks_t *p_pks, uint16_t ch_value_handle, uint8_t *p_data, uint16_t length)
{
    ble_gatts_hvx_params_t hvx_params;

    VERIFY_PARAM_NOT_NULL(p_pks);

    if ((p_pks->conn_handle == BLE_CONN_HANDLE_INVALID))
    {
        return NRF_ERROR_INVALID_STATE;
    }
    if (length > BLE_PKS_MAX_DATA_LEN)
    {
        return NRF_ERROR_INVALID_PARAM;
    }

    memset(&hvx_params, 0, sizeof(hvx_params));
    hvx_params.handle = ch_value_handle;
    hvx_params.p_data = p_data;
    hvx_params.p_len  = &length;
    hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;

    uint32_t err_code = sd_ble_gatts_hvx(p_pks->conn_handle, &hvx_params);
    if(err_code != NRF_SUCCESS)
    {
    	NRF_LOG_ERROR("failed to notify %d\n",err_code);
    }
    return err_code;
}

Since the code hangs (infinite loop somewhere?) I end up losing the connection and I can only recover the nRF51 by reprograming / resetting it.

Why?

Parents Reply Children
No Data
Related