Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Custom service characteristic not initialized

Hi,

I have created a custom service correctly and now I want to add a characteristic to it that has notifications enabled so that I can be notified in value changes.
For some after service is created without errors, the characteristic is not created and I get a message "CHARACTERISTIC ADD ERROR 7" in the very last
check of the following code.

The code shows the custom service and characteristic initialization. Any ideas? Thank you!

#define CUSTOM_UUID_BASE {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xEF, 0xD6, 0x31, 0xBE, 0x00, 0x00, 0x15, 0x16}
#define CUSTOM_SERVICE_UUID 0x0001
#define CUSTOM_CHARACT_UUID 0x0002    

static ble_uuid_t m_adv_uuids[]          =  
{
   {BLE_UUID_BATTERY_SERVICE, BLE_UUID_TYPE_BLE},       // 1
   {BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE},       // 2
   {CUSTOM_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN+1}  // 3
};

static void services_init(void)
{
    uint32_t           err_code;
    ble_nus_init_t     nus_init;
    nrf_ble_qwr_init_t qwr_init = {0};
    
    // Initialize Queued Write Module.
    qwr_init.error_handler = nrf_qwr_error_handler;

    err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
    APP_ERROR_CHECK(err_code);

    //================ Initialize NUS-=============================
    memset(&nus_init, 0, sizeof(nus_init));

    nus_init.data_handler = nus_data_handler;

    err_code = ble_nus_init(m_nus_gen, &nus_init);
    APP_ERROR_CHECK(err_code);

    //================Custom Service - Advertising===============
    ble_uuid_t ble_uuid;
    uint16_t service_handle;
    uint8_t  uuid_type = 0;

    ble_uuid128_t base_uuid = CUSTOM_UUID_BASE;
    err_code = sd_ble_uuid_vs_add(&base_uuid, &uuid_type);
    if (err_code != NRF_SUCCESS) {
      NRF_LOG_INFO("CUSTOM SRVC ERROR %X| UUID TYPE: %X",err_code ,uuid_type);
    }
    NRF_LOG_INFO(" UUID TYPE: %X",uuid_type);  // returns 3
    
    //=================Custom Service - Connected==================
    ble_uuid.type = uuid_type;
    ble_uuid.uuid = CUSTOM_SERVICE_UUID;   //0x1523

    // Add the service.
    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                        &ble_uuid,
                                        &service_handle);
   
    if (err_code != NRF_SUCCESS) {
        NRF_LOG_INFO("CUSTOM SERVICE ERROR %X| SERVICE HANDLE: %X",err_code ,service_handle);
    }
    NRF_LOG_INFO("SERVICE HANDLE: %X",service_handle);
    
    //------------Custom service Characteristic-----------
    ble_gatts_char_md_t char_md;
    memset(&char_md, 0, sizeof(char_md));
    char_md.char_props.notify = 1;  // Enable notifications
    char_md.char_props.read = 1;

    ble_gatts_attr_md_t attr_md;
    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);

    ble_gatts_attr_t    attr_char_value;
    ble_uuid.type = uuid_type;
    ble_uuid.uuid = CUSTOM_CHARACT_UUID;  // Set your characteristic UUID

    uint8_t initial_value[] = {0}; // just a value
    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.max_len   = BLE_GATTS_VAR_ATTR_LEN_MAX;
    attr_char_value.p_value   = initial_value;

    ble_gatts_char_handles_t  handles;
    err_code = sd_ble_gatts_characteristic_add(service_handle,
                                               &char_md,
                                               &attr_char_value,
                                               &handles);
    if (err_code != NRF_SUCCESS) {
        NRF_LOG_INFO("CHARACTERISTIC ADD ERROR %X", err_code);
    }
    //----------------------------------------------------
}

Parents
  • Hi,

    Return value 7 is NRF_ERROR_INVALID_PARAM. One thing I notice is that you have not set attr_md VLOCK, so that needs to be set. As you use a value on the stack, you need to set this to BLE_GATTS_VLOC_STACK.

  • Hi, I set the vloc as follow and now I get an Error 12.

    Then, I increased NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE
    from 1408 to 1600 with the corresponding changes in
    RAM_START and SIZE but the same error occurred.

    Then, I increased attr_char_value.init_len to a larger number (i.e. 10) but the same happened

    ble_gatts_char_md_t char_md;
    memset(&char_md, 0, sizeof(char_md));
    char_md.char_props.notify = 1;  // Enable notifications
    char_md.char_props.read = 1;
    
    ble_gatts_attr_md_t attr_md;
    memset(&attr_md, 0, sizeof(attr_md));
    
    attr_md.vloc = BLE_GATTS_VLOC_STACK;
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);

Reply
  • Hi, I set the vloc as follow and now I get an Error 12.

    Then, I increased NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE
    from 1408 to 1600 with the corresponding changes in
    RAM_START and SIZE but the same error occurred.

    Then, I increased attr_char_value.init_len to a larger number (i.e. 10) but the same happened

    ble_gatts_char_md_t char_md;
    memset(&char_md, 0, sizeof(char_md));
    char_md.char_props.notify = 1;  // Enable notifications
    char_md.char_props.read = 1;
    
    ble_gatts_attr_md_t attr_md;
    memset(&attr_md, 0, sizeof(attr_md));
    
    attr_md.vloc = BLE_GATTS_VLOC_STACK;
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);

Children
No Data
Related