apache / apache/mynewt-nimble

Gatt : Dynamic service addition

Open
#1,485 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
893
Forks
512
Avg merge
13d 31m
Merged PRs (30d)
7

Description

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](https://user-images.githubusercontent.com/109731607/231661263-3a9a1f7e-a6f8-422d-916f-181721fa0193.png)

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().

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with ble_gatts_count_cfg and the existing allocation paths for ble_gatts_clt_cfg_pool in ble_gatts.c, then inspect ble_gatts_conn in ble_gatt_priv.h. Compare the current fixed-array and mempool assumptions with the proposed list-based design and runtime allocation approach. Done means services and their attributes, client configurations, and connection state can be added and deleted dynamically without exceeding initialization-time allocations.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.