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 event not generated


I want to read characteristics (central needs read data from peripheral whenever he want ) but don’t want to use notification.

I am trying this with authorization but it can not generate BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST event. I just didn’t get what are the exactly settings required for that. I made changes in led service characteristics as follows.

What are the exactly changes I have to done here?
OR
Is there any example available where such solution used?

uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init )
{
   uint32_t              err_code;
    ble_uuid_t            ble_uuid;
    ble_add_char_params_t add_char_params;
    ble_gatts_attr_md_t attr_md;

    // Initialize service structure.
    p_lbs->led_write_handler = p_lbs_init->led_write_handler;

    // Add service.
    ble_uuid128_t base_uuid = {LBS_UUID_BASE};
    err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type);
    VERIFY_SUCCESS(err_code);

    ble_uuid.type = p_lbs->uuid_type;
    ble_uuid.uuid = LBS_UUID_SERVICE;

    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle);
    VERIFY_SUCCESS(err_code);


    // Add LED characteristic.
    memset(&add_char_params, 0, sizeof(add_char_params));
    add_char_params.uuid             = LBS_UUID_LED_CHAR;
    add_char_params.uuid_type        = p_lbs->uuid_type;
    add_char_params.init_len             = sizeof(uint8_t);
    add_char_params.max_len            = sizeof(uint8_t);
    add_char_params.char_props.read  = 1;
    add_char_params.char_props.write = 1;

    attr_md.rd_auth = 1; 

    add_char_params.read_access  = SEC_OPEN;
    add_char_params.write_access = SEC_OPEN;

   err_code = characteristic_add(p_lbs->service_handle,
                                  &add_char_params,
                                  &p_lbs->led_char_handles);

}

void ble_lbs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{ 
  
    ble_lbs_t * p_lbs = (ble_lbs_t *)p_context;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GATTS_EVT_WRITE:
            on_write(p_lbs, p_ble_evt);
            break;

        case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
            on_read(p_lbs, p_ble_evt);
           break;
           
        default:
            // No implementation needed.
            break;
    }
}

static void on_read(ble_lbs_t * p_lbs, ble_evt_t const * p_ble_evt)
{
     uint32_t err_code;
     uint8_t update_data = 0xAB;
		
     ble_gatts_rw_authorize_reply_params_t reply_params;

     memset(&reply_params, 0, sizeof(reply_params));
	
     reply_params.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
     reply_params.params.read.p_data    = &update_data;
     reply_params.params.read.len       = sizeof(update_data);
     reply_params.params.read.offset    = 0;
     reply_params.params.read.update    = 0;
     reply_params.params.read.gatt_status = NRF_SUCCESS;
	
    sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gap_evt.conn_handle, &reply_params);
}

Thanks in advance.

Regards,

Pooja

Parents
  • I think you need to fill the attr_md structure completely. the rest of the members in this structure seems to be filled with uninitialized garbage values?

  • Hi Susheel,

    No, I have checked by feeling all parameters of attr_md structure. As per you said changes done when characteristics add. But still didn't generate BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST event. What exactly characters needed for read authorization? Is there any additional setting required in sdk_config file? 

    uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init )
    {
        uint32_t              err_code;
        ble_uuid_t            ble_uuid;
        ble_add_char_params_t add_char_params;
        ble_gatts_attr_md_t attr_md;
    
        
        // Initialize service structure.
        p_lbs->led_write_handler = p_lbs_init->led_write_handler;
    
        // Add service.
        ble_uuid128_t base_uuid = {LBS_UUID_BASE};
        err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type);
        VERIFY_SUCCESS(err_code);
    
        ble_uuid.type = p_lbs->uuid_type;
        ble_uuid.uuid = LBS_UUID_SERVICE;
    
        err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle);
        VERIFY_SUCCESS(err_code);
    
        memset(&add_char_params, 0, sizeof(add_char_params));
        add_char_params.uuid             = LBS_UUID_LED_CHAR;
        add_char_params.uuid_type        = p_lbs->uuid_type;
        add_char_params.init_len         = sizeof(uint8_t);
        add_char_params.max_len          = sizeof(uint8_t);
        add_char_params.char_props.read  = 1;
        add_char_params.char_props.write = 1;
        
        add_char_params.read_access  = SEC_OPEN;
        add_char_params.write_access = SEC_OPEN;
    
        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.rd_auth = 1;      
        attr_md.wr_auth = 1;
        attr_md.vlen = 0;
        attr_md.vloc = BLE_GATTS_VLOC_STACK;
    
       err_code = characteristic_add(p_lbs->service_handle,
                                      &add_char_params,
                                      &p_lbs->led_char_handles);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
    }

    Thanks,

    Pooja

  • That should have been enough, not sure why it is not generated.

    What happened to the peer request when it send a read command? the values were read by the peer normally without this device generating BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST ? or the peer did not get the value?

Reply Children
Related