Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gatt : Dynamic service addition #1485

Open
RoshanESP opened this issue Apr 13, 2023 · 1 comment
Open

Gatt : Dynamic service addition #1485

RoshanESP opened this issue Apr 13, 2023 · 1 comment

Comments

@RoshanESP
Copy link
Contributor

Hi,
Currently, in gatts, services can only be added at the time of stack init.

int
ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs)  
{
    struct ble_gatt_resources res = { 0 };
    int rc;

    rc = ble_gatts_count_resources(defs, &res);
    if (rc != 0) {
        return rc;
    }

    ble_hs_max_services += res.svcs;
    ble_hs_max_attrs += res.attrs;

    /* Reserve an extra CCCD for the cache. */
    ble_hs_max_client_configs +=
        res.cccds * (MYNEWT_VAL(BLE_MAX_CONNECTIONS) + 1);

    return 0;
}

All the memory needed for services and attributes is pre-decided and cannot be changed later.

I found the approach to implement dynamic addition/deletion of the services below.

image

In the above diagram, the current scheme(old scheme) :

  1. ble_gatts_clt_cfg_pool allocates the entire ble_gatts_clt_cfg array in one go. This restricts the further addition of any clt_cfg if needed in case if the service containing subscribable characteristics is added.
  2. The ble_gatts_clt_cfgs static global array present in ble_gatts.c file cannot be extended further if needed due to its fix size.
  3. This same data structure also reflects in the ble_gatts_conn present in ble_gatt_priv.h file. This structure is stored for each connection.
  4. Beyond the memory allocated in mempool no memory can be allocated at the runtime from mempool.

In the new scheme, the following changes solve the problems with the current scheme :

  1. ble_gatts_clt_cfg_pool allocates only one clt_cfg block at a time. This allows the further addition of clt_cfg blocks in case of new service containing a subscribable characteristic is added.
  2. ble_gatts_clt_cfgs will be converted to a list instead of an array. This solves the problem of adding the clt_cfg at runtime.
  3. ble_gatts_conn will also be converted to list.
  4. Whenever the dynamic service is added, the memory will be allocated directly using malloc. At the time of deinit, the mempool memory can be freed using the check os_memblock_from(). If the check fails the memory is freed using normal free().
@RoshanESP
Copy link
Contributor Author

Raised an mr for this: #1498

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant