STM32WBA Bluetooth® LE – P2P router

Revision as of 15:32, 22 March 2023 by Registered User (How to use the Bluetooth® Low Energy P2P router application)

1 Peer-to-peer router profile

The Peer-to-peer router application (P2P) is intended to demonstrate the multilink capabilities of the STM32WBA Bluetooth® LE solution. It acts as:

  • GAP Central & GATT server (router service) device to be connected and controlled by a smartphone or a web page interface
  • GAP Peripheral & GATT client device to control up to seven end-devices (P2P server)

The router service is a generic attribute profile (GATT) based low-energy profile defined by STMicroelectronics with proprietary UUIDs (128 bits) including three characteristics.

This wiki page describes the Peer-to-peer router project provided within the STM32CubeWBA MCU package[1].

Application overview
Connectivity WBA P2P router Topology.png


The table below describes the structure of the router service:

Bluetooth® LE router service specification
Service Characteristic Mode UUID Size
Router service 0000FEB0-cc7a-482a-984a-7f2ed5b3e58f
Write forward Write without response / Read 0000FEB1-8e22-4541-9d4c-21edae82ed19 2
Notif forward Notify 0000FEB2-8e22-4541-9d4c-21edae82ed19 2
Device info Notify 0000FEB3-8e22-4541-9d4c-21edae82ed19 32

The router service is composed of three characteristics:

  • Write forward characteristic:
    • to be used by the smartphone or Bluetooth® web application to control one of the end-devices through the P2P router device.
Router service - Write forward characteristic
Byte Index 0 1
Name Index of end-device LED level
Value 0 to 13
0xFF: All
0x00: LED off
0x01: LED on
  • Notify forward characteristic:
    • to be used by the P2P router application to relay notification from end-devices (button pushed) in order to inform smartphone or Bluetooth® web application
Router service - Notify forward characteristic
Byte Index 0 1
Name Index of end-device Switch level
Value 0 to 13 0x00: Off
0x01: On
  • Device info characteristic:
    • notifies end-device information (such as BD address or Name) to smartphone or Bluetooth® web application
Router Service - Notify forward characteristic
Byte Index 0 1 [2:7] 8 9 [10:up to 31]
Name Index of
end-device
End-device
status
End-device
BD address
End-device actual
LED Level
End-device actual
switch level
End-device
advertising name
Value 0 to 13 0x00: None
0x01: Found
0x02: Connecting
0x03: Discovering
0x04: Connected
0x05: Link lost
BD address
MSB to LSB
0x00: Off
0x01: On
0x00: Off
0x01: On
ASCII string
terminated by \0


Example of flow diagram between STM32WBA microcontrollers and ST BLE toolbox smartphone application
STM32WBA P2P Router flow diagram


1.1 Advertising data

At startup, the P2P router application starts advertising with an interval of approximatively 80 to 100 ms.
The advertised data are composed as follows:

Advertising packet
Description Length AD type Value
Device name 0x06 0x09 p2pR_XX
as a string
Manufacturer Data 0x0F 0xFF See table below
Flags 0x02 0x01 0x06
(General discoverable, BrEdr not supported)


Manufacturer data are encoded following STMicroelectronics BlueST SDK v2 as described below:

Advertising manufacturer data STMicroelectronics BlueST SDK v2
Byte index 0 1 2-3 4 5 6 7 8 9 10-15
Function Length Manufacturer ID Company BlueST version Board ID Firmware ID Option 1 Option 2 Option 3 Bluetooth® device
address
Value 0x0F 0xFF 0x0030
STMicro
0x02 0x8B
NUCLEO-WBA52CG
0x85
P2P router
0x00 0x00 0x00 0x00:80:E1:XX:XX:XX


1.2 On-board button configuration

Button configuration for Bluetooth® Low Energy P2P router application on NUCLEO-WBA52CG board
Application Condition B1 Click B1 Long Press B2 Click B2 Long Press B3 Click B3 Long Press
P2P Server Idle

Connected
Starts scanning

Starts scanning
/ / / / /

2 Requirements

2.1 Software and system requirements

Below the required software as well as the required minimum IDE version:

  • IAR Embedded Workbench for ARM (EWARM) toolchain V9.20.1, plus a patch available in STM32CubeWBA MCU package: STM32Cube_FW_WBA_Vx.x.x/Utilities/PC_Software/EWARMv8_STM32WBAx_V1.2.zip
  • RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.37, plus a patch available in STM32CubeWBA MCU package: STM32Cube_FW_WBA_Vx.x.x/Utilities/PC_Software/Keil.STM32WBAx_DFP.1.0.0.zip
  • STM32CubeIDE toolchain V1.12.0 [2].

The following programmer software is required to flash the board with an already generated binary:

  • STM32CubeProgrammer[3]

2.2 Hardware requirements

The application must be installed on the NUCLEO-WBA52CG [4].

Hardware platform illustration
Nucleo-WBA5


2.3 Compatible remote interfaces

The STM32CubeWBA P2P router project is compatible with the following remote interfaces:

3 STM32WBA P2P router description

3.1 Project directory

The "BLE_p2pRouter" application is available by downloading the STM32CubeWBA MCU package[5].

Refer to the How to Build a Bluetooth® LE project wiki page for information on the project directory.

3.2 Project description

3.2.1 Software project structure

Below the software project structure with its main parts:

P2P router project structure
Connectivity WBA P2P router Archi.jpg
Connectivity yellow box.png
Main applicative part files
Connectivity blue box.png
Bluetooth® LE stack API interface
Connectivity dark blue box.png
Service management
Connectivity green box.png
System commands
Connectivity pink box.png
Transport layer interface

WARNING: Do not modify the files in Middlewares folder


3.2.2 Application initialization

The different steps of the application initialization are described below:

Heart Rate project initialization
Connectivity WBA P2P router Initialization.jpg
puce1.png
  • Initialize the system (HAL, Clock, peripherals)
  • Infinite loop for run mode
puce2.png
  • Initialize the BSP, power mode, trace, memory manager, NVM
  • Wait for initialization done
puce3.png
  • Initialize Bluetooth® LE host stack
puce4.png
  • Initialize Bluetooth® LE GATT level
  • Initialize Bluetooth® LE GAP level
puce5.png
  • Reset registered handler count
puce6.png
  • Initialize the context
  • ManageP2P router service notification
puce7.png
  • Register service handler
  • Update services and characteristics
puce8.png
  • Initialize the context
  • Register client event handler

3.2.3 GAP and GATT initialization and interaction

Software module interaction
Connectivity WBA P2PR module interaction.png


puce3.png

and puce4.png: the Bluetooth® LE P2P router application initialization is done within app_ble.c:

  • Initialization of the Bluetooth® Low Energy stack: initialization of the device as peripheral and central, and configuration and start of the advertising - APP_BLE_init()
  • Call of the service controller initialization - SVCCTL_Init() located in svc_ctl.c
  • Management of the GAP event - SVCCTL_App_Notification()
    • HCI_LE_CONNECTION_COMPLETE provides information on the connection interval, slave latency, and supervision timeout
    • HCI_LE_CONNECTION_UPDATE_COMPLETE provides information on the new connection
    • HCI_DISCONNECTION_COMPLETE informs the application about the link disconnection and the reason
puce5.png

The service management is done by the service controller svc_ctl.c:

  • Initialization of the number of registered handler - SVCCTL_Init()
  • Management of the events from the Bluetooth® Low Energy host stack by calling SVCCTL_UserEvtRx(), and redirection to the gap event handler using SVCCTL_App_Notification
puce6.png

The application level of the P2P router is handled by p2pr_app.c:

  • Initialization of the P2P router service - P2PR_Init() located in p2pr.c
  • Initialization of the context of the application applicative part:
    • Creation of some tasks
    • Setup data tables holding links status
  • Reception of notifications from the P2P router service by calling P2PR_Notification()
puce7.png

The P2P router service p2pr.c manages the specification of the service:

  • Service applicative part initialization - P2PR_Init()
    • Handling of the register P2P router event to service controller - SVCCTL_RegisterSvcHandler(P2PR_EventHandler)
    • Initialization of the service and addition of P2P router service as primary service
      • Initialization of the write forward (WRITEFWD) characteristic
      • Initialization of the notif forward (NOTIFFWD) characteristic
      • Initialization of the device info (DEVINFO) characteristic
  • Management of the GATT event from Bluetooth® Low Energy stack - P2PR_EventHandler()
    • ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE
      • Reception of an attribute modification packet
        • Update of the notification state for the characteristics that support it
        • Transfer of the packet to the applicative part by calling the P2PR_Notification() function
puce8.png

Client links are managed into the gatt_client_app.c file:

  • Initialization of the number of registered handler - SVCCTL_Init()
  • Management of events from the Bluetooth® Low Energy host stack by calling SVCCTL_UserEvtRx(), and redirection to the GATT client event handler using the Event_Handler

3.3 How to use the Bluetooth® Low Energy P2P router application

Once the BLE P2P router application is installed on the STM32WBA platform, launch the ST BLE ToolBox smartphone or the Bluetooth® web application.
Then, scan and connect to device called p2pR_XX (where XX is replaced by the last byte of the BD address)
On release v1.0.0, the total link count is limited to 8. Once the Bluetooth® LE connection is established, and notifications are enabled by the smartphone:

  • Pressing button 1 allows an new end-device to be added the P2P router network. This is done by executing the following steps:
    • Scan is started (blue LED turned on), reports are analyzed looking for a running P2P Server application
    • Scan is stopped once a valid end-device is found or scan timeout happens (blue LED turned off).
    • A valid end-device is detected thanks to FW_ID present in the advertising manufacturer data.
    • A connection is initiated and once it is done:
      • The MTU configuration is exchanged - aci_gatt_exchange_config().
      • The service discovery starts - aci_gatt_disc_all_primary_services().
      • Characteristics are discovered - aci_gatt_disc_all_char_of_service().
      • Descriptors are discovered - aci_gatt_disc_all_char_desc().
      • Notifications of enabled for the characteristics that support them.

The P2P router application maintain tables containing information on the connected end-device, referenced by a device index.
This device index ranges from 0 to 13. See details below:

Table name
a_P2PR_device_...
status[ ] connHdl[ ] bd_addr[ ] char_write_level[ ] char_notif_level[ ] name[ ] name_len[ ]
description 0x00: None
0x01: Found
0x02: Connecting
0x03: Discovering
0x04: Connected
0x05: Link lost
Connection
handle
Bluetooth® Device
Address
Level of write
characteristic
Level of notification
characteristic
Advertising name
as a string
Advertising name
string length

Updates of this table are sent to the remote interface by sending notifications on device info characteristic (format described in chapter 1).

3.4 UART debug trace

Thanks to the debug log via UART interface, it is possible to trace the application project.

To enable the traces within the project, enable them within app_ble.c as described below:

/**
 * When set to 1, the traces are enabled in the BLE services
 */
#define CFG_DEBUG_BLE_TRACE     1
/**
 * Enable or disable traces in the application
 */
#define CFG_DEBUG_APP_TRACE     1
Initialization and remote interface connection phase
==>> Start Ble_Hci_Gap_Gatt_Init function
  Success: hci_reset command
  Success: aci_hal_write_config_data command - CONFIG_DATA_PUBADDR_OFFSET
  Public Bluetooth Address: 00:80:e1:2a:7e:05
  Success: aci_hal_write_config_data command - CONFIG_DATA_IR_OFFSET
  Success: aci_hal_write_config_data command - CONFIG_DATA_ER_OFFSET
  Success: aci_hal_set_tx_power_level command
  Success: aci_gatt_init command
  Success: aci_gap_init command
  Success: aci_gatt_update_char_value - Device Name
  Success: aci_gatt_update_char_value - Appearance
  Success: hci_le_set_default_phy command
  Success: aci_gap_set_io_capability command
  Success: aci_gap_set_authentication_requirement command
  Success: aci_gap_configure_whitelist command
==>> End Ble_Hci_Gap_Gatt_Init function

Services and Characteristics creation
  Success: aci_gatt_add_service command: p2pR 

  Success: aci_gatt_add_char command   : WRITEFWD
  Success: aci_gatt_add_char command   : NOTIFFWD
  Success: aci_gatt_add_char command   : DEVINFO
End of Services and Characteristics creation

  Success: aci_hal_set_radio_activity_mask command

==>> aci_gap_set_discoverable - Success
==>> Success: Start advertising
>>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - Connection handle: 0x0001
     - Connection established with @:4f:5d:ff:57:3b:1e
     - Connection Interval:   30.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms

>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
     - Connection Interval:   7.50 ms
     - Connection latency:    0
     - Supervision Timeout:   5000 ms
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
     - Connection Interval:   30.00 ms
     - Connection latency:    0
     - Supervision Timeout:   5000 ms
Scan and connection to an end-device
==>> aci_gap_start_general_discovery_proc - Success
  p2pServer name: p2pSext_F5, db addr: 0x00:80:E1:2A:7B:F5
  -> stored into p2pRouter table at index 0
  Success: aci_gatt_update_char_value DEVINFO command
==>> aci_gap_terminate_gap_proc - Success
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
-- GAP_GENERAL_DISCOVERY_PROC completed
Create connection to p2pServer stored in table at index 0
  wait for event HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
>>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - Connection handle: 0x0002
     - Connection established with @:00:80:e1:2a:7b:f5
     - Connection Interval:   50.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
  Success: MTU exchange
  MTU exchanged size = 247
  MTU exchanged size = 247
Discover services, characteristics and descriptors for table index 0
GATT services discovery
ACI_ATT_READ_BY_GROUP_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0002
  1/2 short UUID=0x1801, handle [0x0001 - 0x0004], GENERIC_ATTRIBUTE_SERVICE_UUID found
  2/2 short UUID=0x1800, handle [0x0005 - 0x000B], GAP_SERVICE_UUID found
ACI_ATT_READ_BY_GROUP_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0002
  1/1 short UUID=0xFE40, handle [0x000C - 0x0011], P2P_SERVICE_UUID found
PROC_GATT_DISC_ALL_PRIMARY_SERVICES services discovered Successfully

DISCOVER_ALL_CHARS ConnHdl=0x0002 ALLServiceHandle[0x0001 - 0x0011]
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0002
  ConnHdl=0x0002, number of value pair = 4
    1/4 short UUID=0x2A05, Properties=0x0020, CharHandle [0x0002 - 0x0003], 
    GATT SERVICE_CHANGED_CHARACTERISTIC_UUID charac found
    2/4 short UUID=0x2A00, Properties=0x000A, CharHandle [0x0006 - 0x0007], 
    GAP DEVICE_NAME charac found
    3/4 short UUID=0x2A01, Properties=0x0002, CharHandle [0x0008 - 0x0009], 
   GAP APPEARANCE charac found
    4/4 short UUID=0x2A04, Properties=0x0002, CharHandle [0x000A - 0x000B]
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0002
  ConnHdl=0x0002, number of value pair = 2
    1/2 short UUID=0xFE41, Properties=0x0006, CharHandle [0x000D - 0x000E], 
   ST_P2P_WRITE_CHAR_UUID charac found
    2/2 short UUID=0xFE42, Properties=0x0010, CharHandle [0x000F - 0x0010], 
   ST_P2P_NOTIFY_CHAR_UUID charac found
All characteristics discovered Successfully

DISCOVER_ALL_CHAR_DESCS [0x0001 - 0x0011]
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0002
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0002
  UUID=0x2A05, handle=0x0003, found GATT SERVICE_CHANGED_CHARACTERISTIC_UUID
Descriptor UUID=0x2902, handle=0x0002-0x0003-0x0004, Service Changed found
PRIMARY_SERVICE_UUID=0x2800 handle=0x0005
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0006
  UUID=0x2A00, handle=0x0007, found GAP DEVICE_NAME_UUID
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0008
  UUID=0x2A01, handle=0x0009, found GAP APPEARANCE_UUID
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x000A
  UUID=0x2A04, handle=0x000B
PRIMARY_SERVICE_UUID=0x2800 handle=0x000C
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x000D
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0002
  UUID=0xFE41, handle=0x000E, found ST_P2P_WRITE_CHAR_UUID
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0002
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x000F
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0002
  UUID=0xFE42, handle=0x0010, found ST_P2P_NOTIFY_CHAR_UUID
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0002
Descriptor UUID=0x2902, handle=0x000F-0x0010-0x0011P2PNotification found : 
 Desc UUID=0x2902 handle=0x000F-0x0010-0x0011
All characteristic descriptors discovered Successfully

 ServiceChangedCharDescHdl =0x0004
 P2PNotificationDescHdl =0x0011
All notifications enabled Successfully

  Success: aci_gatt_update_char_value DEVINFO command

4 References