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

event handler for characteristic is zero

Hi,

I try to implement custom characteristic. But I notice when I use read auth or normal read event, the handle value is zero. I have linked the handl to the add characteristic.

        case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
            printf("%s: BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST\n", __FUNCTION__);

                if((p_ble_evt->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_READ)&&
                (p_ble_evt->evt.gatts_evt.params.authorize_request.request.read.handle == p_mwds->event_data_count_handles.value_handle))
               {
                    printf("1:%d\r\n",p_ble_evt->evt.gatts_evt.params.authorize_request.request.read.handle);
                    printf("2:%d\r\n",p_mwds->event_data_count_handles.value_handle);
                    printf("%s: BLE_GATTS_EVT_READ_AUTHORIZE_REQUEST\n", __FUNCTION__);
                    ble_gatts_rw_authorize_reply_params_t auth_reply;
                    auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
                    auth_reply.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
                    auth_reply.params.read.update = 0;
                    auth_reply.params.read.offset = 0;
                    auth_reply.params.read.len = 1;
                    //auth_reply.params.read.len = sizeof(p_my_data->my_char);
                    auth_reply.params.read.p_data = (uint8_t*)&my_char;
                    //auth_reply.params.read.p_data = (uint8_t*)&p_my_data->my_char;
                    sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle, &auth_reply);
                    //on_read_auth(p_ble_evt);

                  //case BLE_GATTS_AUTHORIZE_TYPE_WRITE:
                    //printf("%s: BLE_GATTS_EVT_WRITE_AUTHORIZE_REQUEST\n", __FUNCTION__);
                    //on_write_auth(p_ble_evt);
                }
            break;

static uint32_t read_only_char_add(ble_mwds_t * p_mwds, const ble_mwds_init_t * p_mwds_init, ble_gatts_char_handles_t * p_char_handle, uint16_t uuid)
{
    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;

    // Add Custom Value characteristic
    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);
    
    cccd_md.write_perm = p_mwds_init->custom_value_char_attr_md.cccd_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.write  = 0;
    char_md.char_props.notify = 0; 
    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_mwds->uuid_type;
    ble_uuid.uuid = uuid; /* MWDS: JUST A PLACEHOLDER FOR NOW! */

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

    attr_md.read_perm  = p_mwds_init->custom_value_char_attr_md.read_perm;
    attr_md.write_perm = p_mwds_init->custom_value_char_attr_md.write_perm;
    attr_md.vloc       = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth    = 1;
    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   = sizeof(uint8_t);

  return sd_ble_gatts_characteristic_add(p_mwds->service_handle, &char_md,
                                               &attr_char_value,
                                               p_char_handle);
}

read_only_char_add(p_mwds, p_mwds_init, MWDS_EVENT_COUNT_CHAR_UUID, &p_mwds->event_data_count_handles);

struct ble_mwds_s
{
    ble_mwds_evt_handler_t        evt_handler;             
    uint16_t                      service_handle;          
    ble_gatts_char_handles_t      custom_value_handles;
    ble_gatts_char_handles_t      event_data_count_handles;
    ble_gatts_char_handles_t      event_data_handles;      
    ble_gatts_char_handles_t      debug_data_count_handles;
    ble_gatts_char_handles_t      debug_data_handles;      

    ble_gatts_char_handles_t      index_handler;           
    ble_gatts_char_handles_t      value_handler;           
    ble_gatts_char_handles_t      device_table_handler;		
    uint16_t                      conn_handle;             
    uint8_t                       uuid_type; 
 };

Parents Reply Children
No Data
Related