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

C Issue when adding a new characteristic

Hi there, 

I am having a hard time with some C pointers to add a new BLE Characteristic. 

I would like to have a function that returns a simple characteristic metadata and attribute value, given some parameters:

static void getUnsecureCharacteristic(ble_gatts_char_md_t *char_md, ble_gatts_attr_t *char_value, uint8_t read, uint8_t write, uint8_t notify, uint32_t uuid) {

  uint32_t err_code;
  ble_gatts_attr_md_t cccd_md;
  ble_uuid_t ble_uuid;
  ble_gatts_attr_md_t attr_md;

  //Memset needs  pointer, no need to dereference it here
  memset(char_md, 0, sizeof(*char_md));

  char_md->char_props.read = read;
  char_md->char_props.write = write;
  char_md->char_props.notify = notify;
  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 = NULL;
  char_md->p_sccd_md = NULL;

  memset(&attr_md, 0, sizeof(attr_md));
  ble_srv_cccd_security_mode_t secMode;
  attr_md.read_perm = secMode.read_perm;
  attr_md.write_perm = secMode.write_perm;
  attr_md.vloc = BLE_GATTS_VLOC_STACK;
  attr_md.rd_auth = 0;
  attr_md.wr_auth = 0;
  attr_md.vlen = 0;

  ble_uuid.type = 0x02;
  ble_uuid.uuid = uuid;

  memset(char_value, 0, sizeof(*char_value));

  char_value->p_uuid = &ble_uuid;
  char_value->p_attr_md = &attr_md;
  char_value->init_len = sizeof(uint8_t);
  char_value->init_offs = 0;
  char_value->max_len = sizeof(uint8_t);
}

Then I can call my function from outside:

static uint32_t custom_value_char_add(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
{
    uint32_t            err_code;
    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;

  getUnsecureCharacteristic(&char_md, &attr_char_value, 1, 1, 1, CUSTOM_VALUE_CHAR_UUID);
  
  err_code = sd_ble_gatts_characteristic_add(p_cus->service_handle, &char_md,
                                               &attr_char_value,
                                               &p_cus->custom_value_handles);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    return NRF_SUCCESS;
}

Unfortunately the  sd_ble_gatts_characteristic_add functions returns a NRF_ERROR_NOT_SUPPORTED.

If I don't use the external function, the code works properly. This probably means I am doing something wrong with the memory allocation or setting the memory for those structures. 

Does something looks suspicious? Thank you!

Related