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

BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST write

Hi,

I am trying to get the data written from a central (a smartphone in my case) to one of the characteristic of my peripheral using write request. The problem is that I am not able to get the data written by the central. Here are the differents pieces of code that are concerned by this task :

 /** @brief Function that will take care of some housekeeping of ble connections related to our service and characteristic */
void ble_helio_service_on_ble_evt(ble_hs_t * p_helio_service, ble_evt_t * p_ble_evt)
{
    // OUR_JOB: Step 3.D Implement switch case handling BLE events related to our service.
	switch (p_ble_evt->header.evt_id)
	{
	    case BLE_GAP_EVT_CONNECTED:
	        p_helio_service->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
	        break;

	    case BLE_GAP_EVT_DISCONNECTED:
	        p_helio_service->conn_handle = BLE_CONN_HANDLE_INVALID;
	        break;

	    case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
	    	//If there is a read request
	    	if((p_ble_evt->evt.gatts_evt.params.authorize_request.type) == BLE_GATTS_AUTHORIZE_TYPE_READ){
	    		SEGGER_RTT_WriteString(0,"BLE Read request. \n");
	    		on_read(p_helio_service,p_ble_evt);
	    	}//If there is a write request
	    	else if((p_ble_evt->evt.gatts_evt.params.authorize_request.type) == BLE_GATTS_AUTHORIZE_TYPE_WRITE){
	    		SEGGER_RTT_WriteString(0,"BLE Write request. \n");
	    		on_write(p_helio_service, p_ble_evt);
	    	}
	    	break;

	    /*case BLE_GATTS_EVT_WRITE:
	    	on_write(p_helio_service, p_ble_evt);
	    	break;*/

	    default:
	        // No implementation needed.
	        break;
	}
}

/**@brief Function for handling the Write event.
 *
 * @param[in]   p_hs	    Helio Service structure.
 * @param[in]   p_ble_evt   Event received from the BLE stack.
 */
static void on_write(ble_hs_t * p_hs, ble_evt_t * p_ble_evt)
{
    //ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;

	ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.authorize_request.request.write;

	ble_gatts_rw_authorize_reply_params_t authorize_reply_para;
	authorize_reply_para.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;


    /*if (p_evt_write->handle == p_hs->char_measure_handles.cccd_handle)
    {
        on_hrm_cccd_write(p_hs, p_evt_write);
    }*/

    ble_gatts_value_t rx_data;

    if (p_evt_write->handle == p_hs->char_data_handles.value_handle){

	    SEGGER_RTT_WriteString(0,"BLE Data Char Write. \n");

    	uint16_t len;
    	uint16_t offset;
    	//sd_ble_evt_get(data,&len);
    	/*rx_data.len = 13;
    	rx_data.offset = 0;
    	rx_data.p_value = data;*/

    	/*authorize_reply_para.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
	    authorize_reply_para.params.write.update = 1;*/
	    offset = authorize_reply_para.params.write.offset;
	    len = authorize_reply_para.params.write.len;
	    const uint8_t* data = authorize_reply_para.params.write.p_data;

	    //uint32_t err_code = sd_ble_evt_get(data,&len);

    	//Get data
    	//uint32_t err_code = sd_ble_gatts_value_get(p_hs->conn_handle, p_hs->char_data_handles.value_handle, &rx_data);

    	timestamp = ((uint32_t)(data[0]))|((uint32_t)(data[1])>>8)|((uint32_t)(data[2])>>16)|((uint32_t)(data[3])>>24);
    	timezone = ((int16_t)(data[4]))|((int16_t)(data[5])>>8);
    	SPF = data[6];
    	DUVDmax = (uint32_t)(data[7])|((uint32_t)(data[8])>>8)|((uint32_t)(data[9])>>16)|((uint32_t)(data[10])>>24);
    	duration = data[11];
    	uint8_t status = data[12];
    	DUVDTh = DUVDmax/3;

    	SEGGER_RTT_printf(0,"Length : %d \n",len);
    	SEGGER_RTT_printf(0,"Offset : %d \n",offset);
	    SEGGER_RTT_WriteString(0,"BLE Data Char Write message (data) : 0x");
	    for(uint8_t i = 0; i<(sizeof(data)/sizeof(uint8_t)); i++){
	    	SEGGER_RTT_printf(0,"%#02x",data[i]);
	    }
	    SEGGER_RTT_WriteString(0,"\n");
	    SEGGER_RTT_printf(0,"timestamp : %ld \n",timestamp);
	    SEGGER_RTT_printf(0,"timezone : %d \n",timezone);
	    SEGGER_RTT_printf(0,"SPF : %d \n",SPF);
	    SEGGER_RTT_printf(0,"DUVDmax : %ld \n",DUVDmax);
	    SEGGER_RTT_printf(0,"duration : %d \n",duration);
	    SEGGER_RTT_printf(0,"status : %d \n",status);
	    SEGGER_RTT_printf(0,"DUVDTh : %ld \n",DUVDTh);
      }
}

From now I only get 0x0 as data despite the fact I sent various data value with my phone. Could you please tell what is wrong in my code, and what to do in order to get the data sent from my phone ?

My target is an nRF51822 QFACA1, I am on SDK v11 and softdevice S130 v2.0

Best regards,

Guillaume

Related