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

No return value for sd_ble_gatts_hvx()

Hi All,

I have a problem with the function sd_ble_gatts_hvx(). I have no return value when i call this function and i don't understand why ?

I have a function where i update a battery level value (uint8_t) and i notify the central, like this :

static void battery_level_handler(void *p_context) {

ble_lbs_t* p_m_lbs = &m_lbs;

if (p_m_lbs->conn_handle != BLE_CONN_HANDLE_INVALID) {
		
ble_gatts_hvx_params_t hvx_params;
uint32_t err_code;
uint16_t len = 1;
	
memset(&hvx_params, 0, sizeof(hvx_params));

    hvx_params.handle = p_m_lbs->battery_char_handles.value_handle;
    hvx_params.p_data = &initialValueBattery;
hvx_params.offset = 0;
    hvx_params.p_len  = &len;
    hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;

    err_code = sd_ble_gatts_hvx(p_m_lbs->conn_handle, &hvx_params);
// SEGGER_RTT_printf(CHANNEL_RTT, "err_code: %d\n", err_code);
APP_ERROR_CHECK(err_code);

	if(initialValueBattery <= 20) initialValueBattery = 100;

	initialValueBattery = initialValueBattery - 1;
	
	}

}

And i add the battery characteristic to the service like this :

static uint32_t battery_charac_add(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_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   = 1;
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_lbs->uuid_type;
ble_uuid.uuid = UUID_BATTERY_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       = 0;

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      = sizeof(uint8_t);
attr_char_value.p_value      = (uint8_t*) &initialValueBattery;

return sd_ble_gatts_characteristic_add(p_lbs->service_handle,
                                       &char_md,
                                       &attr_char_value,
                                       &p_lbs->battery_char_handles);

}

But i don't understand when i upload the code, the nRF51 reset when he reaches the line :

err_code = sd_ble_gatts_hvx(p_m_lbs->conn_handle, &hvx_params);

And the function sd_ble_gatts_hvx return no value.

I work with the SDK10 and S110 v8.0.0 (nrf51422 SOC).

Thanks for your help.

Parents
  • I tested your project, I just commented in app_timer_start(battery_level_timer, APP_TIMER_TICKS(time_measure_adc, APP_TIMER_PRESCALER), NULL); and turned off optimizations.

    I put a breakpoint on APP_ERROR_CHECK(err_code); and I got error code 0x3401, which is BLE_ERROR_GATTS_SYS_ATTR_MISSING. See this for how to fix it.

    I modified your code with:

        err_code = sd_ble_gatts_hvx(p_m_lbs->conn_handle, &hvx_params);
        SEGGER_RTT_printf(CHANNEL_RTT, "err_code: %d\n", err_code);
            
        if(err_code == BLE_ERROR_GATTS_SYS_ATTR_MISSING)
        {
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
            APP_ERROR_CHECK(err_code);
        }
        else
        {
            APP_ERROR_CHECK(err_code);
        }
    

    And put a breakpoint on APP_ERROR_CHECK(err_code); in the else, and then I got error code 0x0008, which is NRF_ERROR_INVALID_STATE. This is because notifications are not enabled, because the CCCD hasn't been set by the client.

    If you are not experiencing the same, please let me know.

Reply
  • I tested your project, I just commented in app_timer_start(battery_level_timer, APP_TIMER_TICKS(time_measure_adc, APP_TIMER_PRESCALER), NULL); and turned off optimizations.

    I put a breakpoint on APP_ERROR_CHECK(err_code); and I got error code 0x3401, which is BLE_ERROR_GATTS_SYS_ATTR_MISSING. See this for how to fix it.

    I modified your code with:

        err_code = sd_ble_gatts_hvx(p_m_lbs->conn_handle, &hvx_params);
        SEGGER_RTT_printf(CHANNEL_RTT, "err_code: %d\n", err_code);
            
        if(err_code == BLE_ERROR_GATTS_SYS_ATTR_MISSING)
        {
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
            APP_ERROR_CHECK(err_code);
        }
        else
        {
            APP_ERROR_CHECK(err_code);
        }
    

    And put a breakpoint on APP_ERROR_CHECK(err_code); in the else, and then I got error code 0x0008, which is NRF_ERROR_INVALID_STATE. This is because notifications are not enabled, because the CCCD hasn't been set by the client.

    If you are not experiencing the same, please let me know.

Children
Related