Adding many Customs Services in RSL10 BLE

Hi,

I’m developing a BLE application on the RSL10. I’m using with 3.4.2 SDK version.

In my application I want to have two custom services + generic ones.

To do that, I created two functions like below, based from peripheral_server_sleep example :

void CustomService_ServiceAdd(void)
{
    struct gattm_add_svc_req *req = KE_MSG_ALLOC_DYN(GATTM_ADD_SVC_REQ,
                                                     TASK_GATTM, TASK_APP,
                                                     gattm_add_svc_req,
                                                     CS_IDX_NB * sizeof(struct
                                                                        gattm_att_desc));

    uint8_t i;
    const uint8_t svc_uuid[ATT_UUID_128_LEN] = CS_SVC_UUID;

    const struct gattm_att_desc att[CS_IDX_NB] =
    {
        /* Attribute Index  = Attribute properties: UUID,
         *                                          Permissions,
         *                                          Max size,
         *                                          Extra permissions */

        /* TX Characteristic */
        [CS_IDX_TX_VALUE_CHAR]     = ATT_DECL_CHAR(),
        [CS_IDX_TX_VALUE_VAL]      = ATT_DECL_CHAR_UUID_128(CS_CHARACTERISTIC_TX_UUID,
                                                            PERM(RD, ENABLE) | PERM(NTF, ENABLE),
                                                            CS_TX_VALUE_MAX_LENGTH),
        [CS_IDX_TX_VALUE_CCC]      = ATT_DECL_CHAR_CCC(),
        [CS_IDX_TX_VALUE_USR_DSCP] = ATT_DECL_CHAR_USER_DESC(CS_USER_DESCRIPTION_MAX_LENGTH),

        /* RX Characteristic */
        [CS_IDX_RX_VALUE_CHAR]     = ATT_DECL_CHAR(),
        [CS_IDX_RX_VALUE_VAL]      = ATT_DECL_CHAR_UUID_128(CS_CHARACTERISTIC_RX_UUID,
                                                            PERM(RD, ENABLE)
                                                            | PERM(WRITE_REQ, ENABLE) | PERM(WRITE_COMMAND, ENABLE),
                                                            CS_RX_VALUE_MAX_LENGTH),
        [CS_IDX_RX_VALUE_CCC]      = ATT_DECL_CHAR_CCC(),
        [CS_IDX_RX_VALUE_USR_DSCP] = ATT_DECL_CHAR_USER_DESC(CS_USER_DESCRIPTION_MAX_LENGTH),
    };

    /* Fill the add custom service message */
    req->svc_desc.start_hdl = 0;
    req->svc_desc.task_id = TASK_APP;
    req->svc_desc.perm = PERM(SVC_UUID_LEN, UUID_128);
    req->svc_desc.nb_att = CS_IDX_NB;

    memcpy(&req->svc_desc.uuid[0], &svc_uuid[0], ATT_UUID_128_LEN);

    for (i = 0; i < CS_IDX_NB; i++)
    {
        memcpy(&req->svc_desc.atts[i], &att[i], sizeof(struct gattm_att_desc));
    }

    /* Send the message */
    ke_msg_send(req);
}

Then, to Read and Write data of each Custom service, I defined functions based on the event needed. For example, to read data, I do that:

  • DEFINE_MESSAGE_HANDLER(GATTC_READ_REQ_IND, Service0_ReadReqInd) : to read data from service 0

  • DEFINE_MESSAGE_HANDLER(GATTC_READ_REQ_IND, Service1_ReadReqInd): to read data from service 1

My custom services are added as expected because I can see them with a BLE client. But when reading the attribute of the custom service 0, I get the one from the custom service 1. There is a conflict and I know why, given that the event is the same (GATTC_READ_REQ_IND) for both services.

I wonder in this case; how can we do to distinguish between event operations?

Best regards,

Hi,

It would be best for you to implement Custom Service functionality similar to what is done in our new BLE Abstraction samples.

The new Abstraction will keep track of Start_Handle and End_Handle variables for each Characteristics in the Custom Services.

Then, when a Write or Read Request is received, both Custom Service Handlers will be triggered, but you can then compare the Handle being Written/Read and only reply from the Custom Service that contains the Handle you are trying to interact with.

For a detailed implementation, you can look into the ‘ ble_gatt.c ’ & ‘ ble_gatt.c ’ abstraction files to observe how they manage the multiple Custom Services.

Thank you for using our Community Forum.

Thank you Rastislav for your answer.

That mean that using the same way as peripheral_server_sleep_ext example is not a good idea ?
In this example BLE abstraction is not used in this case.

Best regards,

Hi,

You can refer to samples starting with BLE_... prefix. Those use BLE Abstraction.

You can still use the peripheral_server_sleep_ext especially considering it is the only sample that implements specific this functionality, but you will need to add the multiple Custom Service support as outlined in required steps above.

Thank you for using our Community Forum.