STM32WB Bluetooth® LE – AT command server Project

Revision as of 13:36, 21 April 2022 by Registered User (→‎How to adapt the application)

1. STM32WB - Extended advertising configuration for applications

M0: stm32wb5x_BLE_Satck_full_extended_fw.bin @0x080c7000 (CubeWB package v1.14.0)

M4: M4: BLE_p2pServer_Ext and BLE_p2pClient_Ext v1.14.0

The BLE_p2pServer_Ext and BLE_p2pClient_Ext application are available by downloading STM32CubeWB[1] release.

Application is compiled with following options in app_conf.h:  CFG_BLE_OPTIONS: default flags + SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV SHCI_C2_BLE_INIT_OPTIONS_CS_ALGO2 (mandatory in Extended Advertising PDUs) New parameters are defined in v1.14.0: CFG_BLE_MAX_ADV_SET_NBR and CFG_BLE_MAX_ADV_DATA_LEN. They are transmitted to the stack through ble_init_cmd_packet. 


New Scatter file to use (provided in M4 projects): Shared RAM for M4 has been reduced Part of SRAM2B is now used for M4, mailbox buffers


2. Applications description


3. Auxiliary packet introduction

The PDU payload of primary advertising channels can vary in length from 6 to 37 bytes. (6 bytes for addresses, remaining 31 bytes for data)
In Extended Advertising, secondary advertising channels are used to offload data

  • Up to 1650 bytes
  • Multiple and agnostic adverting sets

The advertising packets on primary channels contain the information in which secondary advertising channel and the offset to the start time the auxiliary data will be transmitted

Aux packet


4. Adv Extended NoConn_NoScan

Primary and auxiliary segments-NO SCANNABLE/NO CONNECTABLE
Segments timing implementation:

  • Both primary/auxiliary segments belong to the same scheduled slot for all fragmentations policy (length depends on data size)
  • Channel of AUX_ADV_IND (+request and response) are randomly selected when set is ACTIVATED
  • Chained channels follow a hopping of 1 increment
Example of no scannable/no connectable set


5. Adv Extended Scannable

Primary and auxiliary segments-SCANNABLE
The scannable undirected event type using the ADV_EXT_IND PDU allows any scanner to respond with a scan request to receive scan response data on the secondary advertising physical channel

  • Only Auxiliary segment could be scanned, and Data could only be contained in the scan response.
  • A scanner may send a scan request using the AUX_SCAN_REQ PDU on the same secondary advertising channel index as the received AUX_ADV_IND
Example of scannable set


6. Adv Extended Connectable

Primary and auxiliary segments- CONNECTABLE
The connectable directed advertising event type using ADV_EXT_IND allows an initiator to respond with a connect request on the secondary advertising physical channel to establish an ACL connection

  • After every AUX_ADV_IND PDU related to this event it sends, the advertiser shall listen for AUX_CONNECT_REQ PDUs on the same secondary advertising channel index.
Example of connectable set


7. ACI-HCI commands

Command Comment Use in application
aci_gap_adv_set_configuration Set the extended advertising configuration for one advertising set BLE_p2pServer_Ext
aci_gap_adv_set_adv_data Set the data used in extended advertising PDUs that have a data field BLE_p2pServer_Ext
aci_gap_adv_set_scan_resp_data Provide scan response data used during extended advertising BLE_p2pServer_Ext
aci_gap_adv_set_enable Enable or disable one or more extended advertising sets BLE_p2pServer_Ext
hci_le_read_maximum_advertising_data_length read the maximum length of data supported by the Controller for use as advertisement data or scan response data in an extended advertising event BLE_p2pServer_Ext
hci_le_read_number_of_supported_advertising_sets read the maximum number of advertising sets supported by the Controller at the same time during extended advertising BLE_p2pServer_Ext
aci_gap_adv_set_random_address Set the random device address of an advertising set configured to use specific random address BLE_p2pServer_Ext
aci_gap_adv_remove_set Remove an advertising set from the controller Not used
aci_gap_adv_clear_sets Aci_gap_adv_clear_sets Not used
hci_le_set_extended_scan_parameters set the extended scan parameters to be used on the advertising physical channels BLE_p2pClient_Ext
hci_le_set_extended_scan_enable enable or disable extended scanning BLE_p2pClient_Ext


8. HCI events

Command Comment Use in applications
hci_le_extended_advertising_report_event Indicate that a Bluetooth device has responded to an active scan or has broadcast advertisements that were received during a passive scan BLE_p2pClient_Ext
hci_le_advertising_set_terminated_event Indicate that the Controller has terminated advertising in the advertising sets specified by the Advertising_Handle parameter Not used
hci_le_scan_request_received_event Indicate that a SCAN_REQ PDU or an AUX_SCAN_REQ PDU has been received by the advertiser BLE_p2pClient_Ext
hci_le_scan_timeout_event Indicates that scanning has ended because the duration has expired Not used


9. Implementation in BLE_p2pServer_Ext application

4 types of extended advertising are available in app_ble.c

  • Extended scannable
  • Extended connectable
  • Extended no connectable, no scannable
  • Legacy (extended API)

9.1. Advertising set configuration

A structure holds parameters to create an advertising set, following parameters are available:

  • Adv_Set_t
    • uint8_t Advertising_Handle Handle to identify the set
    • uint16_t Duration Duration in 10ms unit, 0 is no duration
    • uint8_t Max_Extended_Advertising_Events Number of events max, 0 is no maximun
  • uint8_t* data Pointer to the data set in the payload
  • uint16_t data_len Number of data to use
  • uint8_t sid ID of the advertising set
  • uint16_t property Mask to define the property of the set, connectable / scannable / legacy
  • uint16_t interval_min Low limit of adv interval
  • uint16_t interval_max High limit of adv interval
  • uint8_t username[22] Complete local name

9.2. Advertising set management

Creation of an advertising set is composed of 3 main steps

  • Configuration using aci_gap_set_configuration() function
  • Data filling with Set_ext_data() application function composed of following commands
    • An operation parameter must be identified  (HCI_SET_ADV_DATA_OPERATION_FIRST / INTERMEDIATE / LAST or COMPLETE)
    • Compute data pointer and data length
    • Finally use commands aci_gap_adv_set_scan_resp_data() for scannable else use aci_gap_adv_set_adv_data() 
  • Start of the advertising set using aci_gap_adv_set_enable() function

9.3. Commands sequence

Commands sequence

9.4. How to adapt the application

All parameters of the adv_set_param structure can be modified in APP_BLE_Init function
4 configurations are predefined and can be modified

  • Extended SCANNABLE
  • Extended NO SCANNABLE-NO CONNECTABLE
  • Extended CONNECTABLE
  • Extended LEGACY

10. Events generation

An event is an information received either from the remote device or from the local device to inform the application about the status change or the data received.

Events description
Connectivity Events generation.png

11. Security

The BLE security model includes five security features:

  • Pairing: process for creating one or more shared secret keys.
  • Bonding: act of storing the keys created during pairing for use in subsequent connections in order to form a trusted device pair.
  • Device authentication: verification that the two devices have the same keys.
  • Encryption: provides message confidentiality.
  • Message integrity: protects against fake messages.(4 bytes message integrity check MIC)

BLE uses two security levels:

  • Legacy pairing – short temporary key (STK). STK is created to encrypt connection. Then, if bonding, LTK is used for subsequent connections.
  • Secure connection – long term key (LTK). LTK is created to encrypt connection.

11.1. Legacy pairing and no fixed pin (initiated by the central)

Default initialization values can be changed when no link is established with:

BLE_IO_CAPA=4
BLE_SET_AUTHEN_REQ=0,1,0,1,111111 (fixed pin is not used)
Legacy pairing with no fixed pin exchange sequence
Connectivity LP nofixedpin1.png
Connectivity LP nofixedpin2.png

11.2. Legacy pairing and fixed pin (111111) (initiated by the central)

Default initialization values can be changed when no link is established with:

BLE_IO_CAPA=0
BLE_SET_AUTHEN_REQ=0,0,0,0,111111 (fixed pin = 111111 is used)
Legacy pairing with fixed pin exchange sequence
Connectivity LP fixedpin1.png
Connectivity LP fixedpin2.png

11.3. Secure connection (initiated by the central)

Use of default values when BLE_INIT is sent:

io_capability = 4
bonding_mode = 0
mitm = 1
sc_support = 1
use_fixed_pin = 1
fixed_pin = 111111
identity_address_type = 0
Secure connection (initiated by the central) exchange sequence
Connectivity SecureConnection1.png
Connectivity SecureConnection2.png

12. Flow sequences for examples

12.1. First demonstration using predefined applications

12.1.1. BLE_SVC=1 P2P_server

Steps to use P2P_server application:

  • Power on your board running BLE_AT_Server application
  • Open a terminal emulator to send data to STM32WB LPUART1 interface.
  • Send AT+BLE_SVC=1 command : creates P2P_server service and starts advertising. OK is received
  • Then, open ST BLE Sensor smartphone application and connect to P2PSRV1 device
  • Events are received over LPUART:
    • BLE_EVT_CONN=1
    • BLE_EVT_UPD_CONN=1
    • BLE_EVT_UPD_CONN=1
  • Play with the light on the smartphone, events are received over LPUART:
    • BLE_EVT_WRITE=1,2,x with x = 0 led off, x = 1 led on
  • Send over the LPUART AT+BLE_NOTIF_VAL=1,2,1 command – toggles button characteristic status, (AT+BLE_NOTIF_VAL=1,2,0 has no effect), on the smartphone "Button pressed: hh:mm:ss {status}" is displayed. OK is received


P2P Server exchange sequence
Connectivity P2P server.png
Connectivity P2P sequence debug trace.png

12.1.2. BLE_SVC=2 heart rate

Steps to use the heart rate application:

  • Power on your board running BLE_AT_Server application
  • Open a terminal emulator to send data to STM32WB LPUART1 interface
  • Send AT+BLE_SVC=2 command : creates Heart Rate service and starts advertising. OK is received
  • Then, open ST BLE Sensor smartphone application and connect to HRSTM device
  • Events are received over LPUART:
    • BLE_EVT_CONN=1
    • BLE_EVT_UPD_CONN=1
    • BLE_EVT_UPD_CONN=1
  • Send over the LPUART AT+BLE_NOTIF_VAL=2,1,70 command – sets heart rate measurement characteristic value to 70. OK is received
Heart Rate exchange sequence
Connectivity HeartRate.png
Connectivity HR sequence debug trace.png

12.2. Second demonstration: creating a custom service

12.2.1. BLE_SVC=3 Custom service

Steps to create custom service:

  • Power on your board running BLE_AT_Server application
  • Open a terminal emulator to send data to STM32WB LPUART1 interface
  • Send AT+BLE_INIT=1 command, initialization. OK is received
  • Send AT+BLE_SVC=3,2,0xaabb,10 command, creation of a custom service. OK is received
  • Send AT+BLE_CHAR_ADD=3,1,1,0x3344,2,26,0,1 command, creation of a characteristic with properties: read, write, notify. OK is received
  • Send AT+BLE_CHAR_ADD=3,2,1,0x5566,2,40,0,1 command, creation of a second characteristic with properties: write, indicate. OK is received
  • Send AT+BLE_ADV=1, starts advertising. OK is received
  • Then, open ST BLE ToolBox smartphone application and connect to AT_SERVER device, discover service and characteristics.
  • Events are received over LPUART:
    • <BLE_EVT_CONN=1
    • <BLE_EVT_UPD_CONN=1
    • <BLE_EVT_UPD_CONN=1
Creation of custom service exchange sequence
Connectivity newcustom.png
ST BLE ToolBox Custom service
Connectivity ST BLE ToolBox Custom Service.jpg

Write characteristics, enable notification and indication:

  • On the first writable characteristic, click on the write button, enter the value to write for example 0x1122 and click on send. Value is written on service 3, characteristic 1.
  • Event is received over LPUART:
    • <BLE_EVT_WRITE=3,1,0x1122
  • Same thing on the second writable characteristic, write 0x3344 value.
  • Event is received over LPUART:
    • <BLE_EVT_WRITE=3,2,0x3344
  • On notifiable characteristics, click on Notify for characteristic 1 to enable notification and click on Indicate for characteristic 2 to enable indication.
  • Send AT+BLE_NOTIF_VAL=3,1,1 command, OK is received. Notification received on the smartphone.
  • Send AT+ BLE_INDIC_VAL=3,2,1 command, OK is received. Indication received on the smartphone.
Characteristic management exchange sequence
Connectivity writechar1.png
Connectivity writechar2.png

Initialization sequence with specific parameters, change of connection interval:

  • Send AT+BLE_PUB_ADDR=0x112233445566 command, configuration of public address. OK is received
  • Send AT+BLE_INIT=1 command, initialization. OK is received
  • Send AT+BLE_ADV_PARAM=0,0,20,25 command, configuration of advertising parameters. OK is received
  • Send AT+BLE_ADV_PARAM? command to check advertising parameters.
    • +BLE_ADV_PARAM:0,0,20.0,25.0,0 is received
    • OK is received
  • Send AT+BLE_ADV=1 command, starts advertising. OK is received
  • Then, open ST BLE ToolBox smartphone application and connect to AT_SERVER device, discover service and characteristics.
  • Events are received over LPUART:
    • <BLE_EVT_CONN=1
    • <BLE_EVT_UPD_CONN=1
    • <BLE_EVT_UPD_CONN=1
  • Send AT+BLE_CONN_INT=125,150 command, change connection interval. OK is received
  • Events are received over LPUART:
    • <BLE_EVT_UPD_CONN=1
  • Send AT+BLE_CONN_INT? command to check connection interval.
    • +BLE_CONN_INT:150.00,150.00 is received
    • OK is received
Initialization sequence with specific parameters, change of connection interval
Connectivity conninterval1.png
Connectivity conninterval2.png

Initialization sequence with random address defined by the user:

  • Send AT+BLE_RAND_ADDR=0xee2233445566 command, configuration of random address. OK is received
  • Send AT+BLE_RAND_ADDR? command to check random address.
    • +BLE_RAND_ADDR:0xee2233445566 is received
    • OK is received
  • Send AT+BLE_INIT=1 command, initialization. OK is received
  • Send AT+BLE_ADV_PARAM=0,0,20,25 command, configuration of advertising parameters. OK is received
  • Send AT+BLE_ADV_PARAM? command to check advertising parameters.
    • +BLE_ADV_PARAM:0,0,20.0,25.0,0 is received
    • OK is received
  • Send AT+BLE_ADV=1 command, starts advertising. OK is received
  • Then, open ST BLE ToolBox smartphone application and connect to AT_SERVER device, discover service and characteristics.
  • Events are received over LPUART :
    • <BLE_EVT_CONN=1
    • <BLE_EVT_UPD_CONN=1
    • <BLE_EVT_UPD_CONN=1
Initialization sequence with random address defined by the user
Connectivity random addr1 1.png
Connectivity random addr1 2.png

Initialization sequence with default random address (CFG_STATIC_RANDOM_ADDRESS defined in app_conf.h):

  • Send AT+BLE_CFG_RAND_ADDR=1 command, configuration of random address. OK is received
  • Send AT+BLE_RAND_ADDR? command to check random address.
    • +BLE_RAND_ADDR:0x000000000000 is received
    • OK is received
  • Send AT+BLE_INIT=1 command, initialization. OK is received
  • Send AT+BLE_RAND_ADDR? command to check random address.
    • +BLE_RAND_ADDR:0xc122aabbccdd is received
    • OK is received
  • Send AT+BLE_ADV_PARAM? command to check advertising parameters.
    • +BLE_ADV_PARAM:0,0,20.0,30.0,0 is received
    • OK is received
  • Send AT+BLE_ADV=1 command, starts advertising. OK is received
  • Then, open ST BLE ToolBox smartphone application and connect to AT_SERVER device, discover service and characteristics.
  • Events are received over LPUART:
    • <BLE_EVT_CONN=1
    • <BLE_EVT_UPD_CONN=1
    • <BLE_EVT_UPD_CONN=1
Initialization sequence with default random address
Connectivity random addr2 1.png
Connectivity random addr2 2.png

Initialization sequence with default random address generated with random generator (CFG_STATIC_RANDOM_ADDRESS not defined in app_conf.h):

  • Send AT+BLE_CFG_RAND_ADDR=1 command, configuration of random address. OK is received
  • Send AT+BLE_RAND_ADDR? command to check random address.
    • +BLE_RAND_ADDR:0x000000000000 is received
    • OK is received
  • Send AT+BLE_INIT=1 command, initialization. OK is received
  • Send AT+BLE_RAND_ADDR? command to check random address.
    • +BLE_RAND_ADDR:0xe7257d5fd350 is received
    • OK is received
  • Send AT+BLE_ADV=1 command, starts advertising. OK is received
  • Send AT+BLE_ADV_PARAM? command to check advertising parameters.
    • +BLE_ADV_PARAM:0,0,20.0,30.0,1 is received
    • OK is received
  • Then, open ST BLE ToolBox smartphone application and connect to AT_SERVER device, discover service and characteristics.
  • Events are received over LPUART :
    • <BLE_EVT_CONN=1
    • <BLE_EVT_UPD_CONN=1
    • <BLE_EVT_UPD_CONN=1
Initialization sequence with default random address
Connectivity random addr3 1.png
Connectivity random addr3 2.png

Initialization sequence with a new name:

  • Send AT+BLE_NAME=WB_ATCMD command, configuration of device name. OK is received
  • Send AT+BLE_NAME? command to check random address.
    • +BLE_NAME:WB_ATCMD is received
    • OK is received
  • Send AT+BLE_INIT=1 command, initialization. OK is received
  • Send AT+BLE_ADV=1 command, starts advertising. OK is received
  • Then, open ST BLE ToolBox smartphone application and connect to WB_ATCMD device, discover service and characteristics.
  • Events are received over LPUART :
    • <BLE_EVT_CONN=1
    • <BLE_EVT_UPD_CONN=1
    • <BLE_EVT_UPD_CONN=1
Initialization sequence with a new name
Connectivity name1.png
Connectivity name2.png

13. References