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
  • Hello,

    Please make sure to have DEBUG defined in your preprocessor defines, like shown in the included image:

    This will make your logger output a detailed error message whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.
    Please do this, and let me know what the error message reads, along with which function that returned the error code pointed to in the error message.

    Best regards,
    Karl

  • Hello,

    mr Karl i am very happy to communicate with you and thank you so much for your reply, i tried as you said  and i got this error,

    thank you,

  • sagarnayakm said:
    mr Karl i am very happy to communicate with you and thank you so much for your reply

    No problem at all, I am happy to help!

    sagarnayakm said:
    i tried as you said  and i got this error,

    Which function returns this error code? Please check the end of the error message to see where the APP_ERROR_CHECK was triggered.

    Best regards,
    Karl

  • Hello,

    yes, i got where exactly its the function service_init()  in that ble_cus_init() it returns err_code 7 please

    have a look at this ,

    thank you,

  • 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

Reply
  • 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

Children
  • 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.

  • Hello,

    sagarnayakm said:
    i think first error is resolved like now its advertising Device name

    I am happy to hear that the initial issue is resolved!
    How did you resolve it?

    Could you also tell me which step of the tutorial you are currently working on?

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

    Could you confirm that you pass all the returned error codes to an APP_ERROR_CHECK?
    Furthermore, I see that you use a lot of breakpoints in your code - please be advised that you may not use breakpoints while the SoftDevice is enabled, since they will halt the CPU and cause the SoftDevice to assert immediately upon resuming the program. This is because the SoftDevice keeps track of upcoming BLE events using a timer instances, and so it will assert if it for some reason is unable to process them.

    Best regards,
    Karl

  • How did you resolve it?

    Hello, i adding step by step peice of code to ble_cus .c and .h  files i don't exactly the issue.

    Could you also tell me which step of the tutorial you are currently working on?

    Yes, now i am in step 7 like when i write 01 value the led4 should on and again if i write 01 led should off .

    so its working fine. for my application its enough right.??

    or should i need to continue the propagating and remaining steps also.

    now i can see in my nrf_connect mobile app like this 

      

    so, please tell me that for sending some data from app to device is it enough.?? or i need to follow remaining steps also.

    thank you.

  • I am glad to hear that it is working after following the tutorial, great!

    I would recommend that you follow the tutorial to the end because it contains important information about how to forward service specific events to your application, along with the notify property, which comes in handy in a multitude of applications.
    If it turns out that the parts are not directly relevant to your application now it is still good to know for future reference, and to better understand how the BLE protocol and communication works.

    Best regards,
    Karl

Related