STM32WB-WBA GATT Data Base and bonded devices information storage

Under construction.png Coming soon

1. STM32WB - BLE GATT DB SRAM computation

Maximum size of SRAM allocation for the GATT database cannot be changed by the user's FW and varies for different stacks.

Calculation of the Max size allocated for GATT Data Base in SRAM is done in M0.

Max size table


The limitation for number of services and number of attributes follows the rule:
(number of services) x 48 + (number of attributes) x 40 + CFG_BLE_ATT_VALUE_ARRAY_SIZE < Max size

To apply the limitation, it is necessary to calculate the number of services, the number of attributes and the attribute value array size.

1.1. How to calculate CFG_BLE_NUM_GATT_SERVICES ?

CFG_BLE_NUM_GATT_SERVICES defines the number of services to be stored in the GATT database.
Note that the default GAP and GATT services are automatically added at initialization.
The parameter CFG_BLE_NUM_GATT_SERVICES must be the number of user services increased by 2 (number of default GAP and GATT services).
CFG_BLE_NUM_GATT_SERVICES = 2 + number of user's services

1.2. How to calculate CFG_BLE_NUM_GATT_ATTRIBUTES ?

CFG_BLE_NUM_GATT_ATTRIBUTES defines the number of attribute records related to all the characteristics for the specific BLE user application.

For each characteristic:
the number of attribute records goes from two to four depending on the characteristic properties:
Minimum of two (one for declaration and one for the value)
Add one more record for each additional property: Notify or Indicate, Broadcast.

Notify and Indicate properties correspond to CCCD (Client Characteristic Configuration Descriptor)
Broadcast property corresponds to SCCD (Server Characteristic Configuration Descriptor)

CFG_BLE_NUM_GATT_ATTRIBUTES = <number of SCCD attributes> + 9 + number of user's attributes

Example of DIS service - Number of GATT attributes

Example of service including characteristic with notification - Number of GATT attributes

Example of service including characteristic with notification and user description descriptor - Number of GATT attributes

1.3. How to calculate CFG_BLE_ATT_VALUE_ARRAY_SIZE ?

CFG_BLE_ATT_VALUE_ARRAY_SIZE defines the size of the storage area for the attribute values.

Each characteristic contributes to the Attribute Value Array Size as follow:
Characteristic value length plus:
– 5 bytes if characteristic UUID is 16 bits
– 19 bytes if characteristic UUID is 128 bits
– 2 bytes if characteristic has a server configuration descriptor
– 2 bytes * CFG_BLE_NUM_LINK if the characteristic has a client configuration descriptor
– 2 bytes if the characteristic has extended properties
Each descriptor contributes to the Attribute Value Array Size as follow:
Descriptor length

Example of DIS service - Attribute Value Array Size

Example of service including characteristic with notification - Attribute Value Array Size

Example of service including characteristic with notification and user description descriptor - Attribute Value Array Size

Moreover, it is necessary to add characteristics related to the default GATT services. For example:
Service change characteristic (if not removed): 9 (5+2+2), 2 for indicate, 2 for number of link Device Name: 13 (5+2+6), 2 for broadcast, 6 for name length Appearance: 7 (5+2), 2 for attribute length Peripheral Preferred Connection Parameters: 13 (5+8), 8 for attribute length

For this case

default value array size: 9 + 31 =  42 bytes

2. STM32WB - BLE GATT DB and security record in NVM

2.1. BLE GATT DB record in NVM - FULL_GATTDB_NVM flag

The GATT DB record in NVM is composed:

per service:
2 bytes for handle
3 or 17 bytes for UUID

per characteristic attribute:
2 bytes for handle
3 or 17 bytes for UUID
2 bytes for CCCD value (only if the attribute is a CCCD)
This is size_of_gatt record

Example of DIS service - GATT DB stored in NVM

Example of service including characteristic with notification - GATT DB stored in NVM

Example of service including characteristic with notification and user description descriptor - GATT DB stored in NVM

2.2. BLE GATT DB record in NVM - REDUC_GATTDB_NVM and NO_SVC_CHANGE_DESC flags

The GATT record in NVM is composed:

per characteristic attribute:
2 bytes for CCCD value (only if the attribute is a CCCD)

This is size_of_gatt record

2.3. BLE GATT DB record in NVM - REDUC_GATTDB_NVM and WITH_SVC_CHANGE_DESC flags

The GATT record in NVM is composed:

per characteristic attribute: 2 bytes for CCCD value (only if the attribute is a CCCD)

+ 16 bytes (The DB hash characteristic stores a 128 bits value calculated from the DB structure.
Any change in the DB structure results in a different hash value.)

This is size_of_gatt record

2.4. BLE security record

The security record in NVM is fixed and equal to 80 bytes
This is size_of_sec_record

3. STM32WB - BLE number of bonded devices in NVM

Regarding the size of NVM, the computation of number n of bonded devices that can be stored in NVM is the following:

n = (total_size_of_nvm-1) / [ (size_of_sec_record+1) + (size_of_gatt record+1) ]

total_size_of_nvm = 2028 bytes (fixed in M0)
size_of_sec_record =  80 bytes (this is a constant from the BLE stack)
size_of_gatt_record is dependent of the flags chosen in the application in app_conf.h file
See the description in previous point.

3.1. Example 1

Default GATT configuration without adding services:

GATT record = 42 bytes

3.2. Example 2

Default GATT configuration + 1 empty service with UUID_16

GATT record = 
42 bytes for default + 5 bytes for the service

3.3. Example 3

Default GATT configuration + 1 service with UUID_16 + 1 characteristic with UUID_16

GATT record = 
42 bytes for default + 5 bytes for the service + 5 bytes for the 1st characteristic attribute + 5 bytes for the 2nd characteristic attribute + 7 bytes for the 3rd characteristic attribute (CCCD)

If we consider the example used in section 1 and 2

Example - GATT DB stored in SRAM and in NVM

Formula to calculate Total Buffer Gatt Size

(number of services) x 48 + (number of attributes) x 40 + CFG_BLE_ATT_VALUE_ARRAY_SIZE
In previous example, 
size_of_gatt record is equal to 614 bytes

Formula to calculate the number of bonded devices that can be stored in NVM

n = (total_size_of_nvm-1) / [ (size_of_sec_record+1) + (size_of_gatt record+1) ]

3.4. General information concerning NVM

We don’t store values of attributes, only the CCCD value.

it is applicable for GATT server only.

On server side it is used to: - check if services have changed - keep CCCD in memory

3.5. References