This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to add custom service to the Ble_app_template example

Hello i am using nrf52810 with SDK version 17.0.2 , i want to add custom service to the ble_app_template example and i followed this tutorial link https://github.com/edvinand/custom_ble_service_example so at first debug i got this error as expected 

next i have change the RAM_START AND RAM_SIZE as per the memory size mentioned in the error then i DEBUG it again so the i got 

so i don't know what's happening please help me to resolve this . 

thank you.

Parents Reply Children
  • So it seems your ble_cus_init function is returning the NRF_ERROR_INVALID_PARAM error.
    How does this function look? Under what conditions will it return this error code?
    Likely it is just forwarding the error code generated by a SDK function it itself is calling.

    Best regards,
    Karl

  • Hello,

    How does this function look?

    the service_init() function is look this.

    static void services_init(void)
    {
    ret_code_t err_code;
    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);

    /* YOUR_JOB: Add code to initialize the services used by the application.
    ble_xxs_init_t xxs_init;
    ble_yys_init_t yys_init;

    // Initialize XXX Service.
    memset(&xxs_init, 0, sizeof(xxs_init));

    xxs_init.evt_handler = NULL;
    xxs_init.is_xxx_notify_supported = true;
    xxs_init.ble_xx_initial_value.level = 100;

    err_code = ble_bas_init(&m_xxs, &xxs_init);
    APP_ERROR_CHECK(err_code);

    // Initialize YYY Service.
    memset(&yys_init, 0, sizeof(yys_init));
    yys_init.evt_handler = on_yys_evt;
    yys_init.ble_yy_initial_value.counter = 0;

    err_code = ble_yy_service_init(&yys_init, &yy_init);
    APP_ERROR_CHECK(err_code);
    */

    ble_cus_init_t cus_init;


    // Initialize CUS Service.
    memset(&cus_init, 0, sizeof(cus_init));

    err_code = ble_cus_init(&m_cus, &cus_init);
    printf("err_code:%d",err_code);
    APP_ERROR_CHECK(err_code);
    }

    next, like i want to send some data from

    aa-ble_app_template - Custom_service1.zip

    nrf_connect mobile app to the device. If i am not wrong i need to add custom_service to my code right.?? so for this i used this code please have look at the zip.file

    thank you.

  • sagarnayakm said:
    the service_init() function is look this.

    The error seems to be returned by the ble_cus_init not services_init, please show ble_cus_init instead so I may take a look.
    Please also try to find out for yourself why it would return the INVALID_PARAM by checking its API reference - or the API reference of any function which error code is forwarded - and let me know what you think.

    Best regards,
    Karl

  • Hello,

    please show ble_cus_init instead so I may take a look.

    uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
    {
    if (p_cus == NULL || p_cus_init == NULL)
    {
    return NRF_ERROR_NULL;
    }

    uint32_t err_code;
    ble_uuid_t ble_uuid;

    // Initialize service structure
    p_cus->evt_handler = p_cus_init->evt_handler;
    p_cus->conn_handle = BLE_CONN_HANDLE_INVALID;

    // Add Custom Service UUID
    ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE};
    err_code = sd_ble_uuid_vs_add(&base_uuid, &p_cus->uuid_type);
    VERIFY_SUCCESS(err_code);

    ble_uuid.type = p_cus->uuid_type;
    ble_uuid.uuid = CUSTOM_SERVICE_UUID;

    // Add the Custom Service
    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_cus->service_handle);
    if (err_code != NRF_SUCCESS)
    {
    return err_code;
    }

    // Add Custom Value characteristic
    return custom_value_char_add(p_cus, p_cus_init);
    }

    let me know what you think.

    i think the UUID which we are passing to the function is wrong.

  • Hello,

    Hi mr.Karl i think first error is resolved like now its advertising Device name and i can able to connect while debugging also its working fine without any errors with custom_service tutorial code not mine.

    then i have add read and write characteristics to the service but its not added. for more information please have look 

    here unknown service is added but read and write characteristics not added even when i debug there is no error as you can see in the first picture.

    the ble_cus_init() look like this 

    ret_code_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
    {
        if (p_cus == NULL || p_cus_init == NULL)
        {
            return NRF_ERROR_NULL;
        }
    
        ret_code_t  err_code;
        ble_uuid_t  ble_uuid;
    
        // Initialize service structure
        p_cus->conn_handle               = BLE_CONN_HANDLE_INVALID;
    
        // Add Custom Service UUID
        ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE};
        err_code =  sd_ble_uuid_vs_add(&base_uuid, &p_cus->uuid_type);
        VERIFY_SUCCESS(err_code);
        
        ble_uuid.type = p_cus->uuid_type;
        ble_uuid.uuid = CUSTOM_SERVICE_UUID;
    
        // Add the Custom Service
        err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_cus->service_handle);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;    // This seems redundant, but we'll add more later
        }
        return err_code;
    
    
         // Add the Custom Value Characteristic
        return custom_value_char_add(p_cus, p_cus_init);
        
    }
    

    next the characteristics add function is 

    static ret_code_t custom_value_char_add(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
    {
        ret_code_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;
    
        memset(&cccd_md, 0, sizeof(cccd_md));
    
        char_md.char_props.read   = 1;
        char_md.char_props.write  = 1;
        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.type             = p_cus->uuid_type;
        ble_uuid.uuid             = CUSTOM_VALUE_CHAR_UUID;
        
        memset(&attr_md, 0, sizeof(attr_md));
    
        attr_md.read_perm         = p_cus_init->custom_value_char_attr_md.read_perm;
        attr_md.write_perm        = p_cus_init->custom_value_char_attr_md.write_perm;
        attr_md.vloc              = BLE_GATTS_VLOC_STACK;
        attr_md.rd_auth           = 0;
        attr_md.wr_auth           = 0;
        attr_md.vlen              = 0;
    
        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);
    
        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;
    
    }

    thank you.

Related