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

Characteristic permissions are not being enforced correctly

Hi, 

I encountered a weird behavior. i defined characteristics with specific read/write permission however all the characteristics are writable for some reason altough the permissions are sent correctly. 

I have noticed it on a BT dongle from Texas Instruments. in which i can see the right permissions but even if a characteristic is read only i can still write to it. 

you can see the permissions in the following snippet:

permissions

the value is changed with a write process 

the packets sent are:

in other devices i have worked on a characteristic would deny a write sequence if no write permissions are set.

please let me know what is required in order to solve this. 

  • Hi,

    Please make sure you configure your characteristic correctly. As you probably know this is done using sd_ble_gatts_characteristic_add. Make sure you do configure both char_md and attr_md, and that you memset these before you configure them.

    Example for Hear rate characteristic:

    /**@brief Function for adding the Heart Rate Measurement characteristic.
     *
     * @param[in]   p_hrs        Heart Rate Service structure.
     * @param[in]   p_hrs_init   Information needed to initialize the service.
     *
     * @return      NRF_SUCCESS on success, otherwise an error code.
     */
    static uint32_t heart_rate_measurement_char_add(ble_hrs_t            * p_hrs,
                                                    const ble_hrs_init_t * p_hrs_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;
        uint8_t             encoded_initial_hrm[MAX_HRM_LEN];
    
        memset(&cccd_md, 0, sizeof(cccd_md));
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
        cccd_md.write_perm = p_hrs_init->hrs_hrm_attr_md.cccd_write_perm;
        cccd_md.vloc       = BLE_GATTS_VLOC_STACK;
    
        memset(&char_md, 0, sizeof(char_md));
    
        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_BLE_ASSIGN(ble_uuid, BLE_UUID_HEART_RATE_MEASUREMENT_CHAR);
    
        memset(&attr_md, 0, sizeof(attr_md));
    
        attr_md.read_perm  = p_hrs_init->hrs_hrm_attr_md.read_perm;
        attr_md.write_perm = p_hrs_init->hrs_hrm_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  = hrm_encode(p_hrs, INITIAL_VALUE_HRM, encoded_initial_hrm);
        attr_char_value.init_offs = 0;
        attr_char_value.max_len   = MAX_HRM_LEN;
        attr_char_value.p_value   = encoded_initial_hrm;
    
        return sd_ble_gatts_characteristic_add(p_hrs->service_handle,
                                               &char_md,
                                               &attr_char_value,
                                               &p_hrs->hrm_handles);
    }

     

     

Related