STM32WBA Bluetooth® LE – P2P Router

Revision as of 17:00, 23 February 2023 by Registered User

1. Peer to Peer Router profile

The Peer to Peer Router application is intended to demonstrate multi capabilities of the STM32WBA Bluetooth® LE solution. It acts as:

  • GAP Central & GATT Server (Router Service) device to be connected and controlled by Smartphone
  • 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 is the description of the Peer to Peer Router project provided within the STM32CubeWBA MCU Package[1]

Overall application diagram
STM32WBA P2P Profile


The table below describes the structure of 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

Router service is composed of three characteristics:

  • Write forward characteristic :
    • to be used by the smartphone or web Bluetooth® application to control one of the end device through the P2P router device.
Router service - Write forward characteristic
Byte Index 0 1
Name Index of end device LED level
Value 0 to 14
0xFF - All
0x00: LEDoff
0x01: LEDon
  • 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 web Bluetooth® application
Router service - Notify forward characteristic
Byte Index 0 1
Name Index of end device Switch level
Value 0 to 14 0x00: Off
0x01: On
  • Device info characteristic:
    • notifies end device information (BD address, Name, ..) to smartphone or web Bluetooth® 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 adv name
Value 0 to 14 0x00: None
0x01: Found
0x02: Connecting
0x03: Discovering
0x05: Connected
0x06: 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 STM32WBAs & ST BLE Toolbox smartphone application.
STM32WBA P2P Router flow diagram


1.1. Advertising data

At start up, P2P Router application starts advertising with an interval of ??ms. Data advertised are composed of:

  • An advertising element containing device name.
  • Another advertising element containing manufacturer data encoded following STMicroelectronics BlueST v2 as described below:
P2P Router advertising manufacturer data STMicroelectronics BlueST SDK v2
0 1 2-3 4 5 6 7 8 9 10-15
Length Manufacturer ID Company BlueST Version Device ID Firmware ID Option 1 Option 2 Option 3 Device DB address
P2P Router 0x0F 0xFF 0x0030 STMicro 0x02 0x8B Nucleo-WBA 0x85 - P2P Router 0x00 0x00 0x00 0x00:80:E1:xx:xx:xx


1.2. On board buttons configuration

P2P Router Application Nucleo-WBA buttons configuration
Application Condition B1 Click B1 Long Press B2 Click B2 Long Press B3 Click B3 Long Press
P2P Router Any Starts scanning Clear security
database

2. Requirements

2.1. Software and system requirements

The software required are the following (minimum IDEs version):

  • IAR Embedded Workbench for ARM (EWARM) toolchain V9.20.1
  • RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.37
  • STM32CubeIDE toolchain V1.12.0 [2].


Programmer:

  • STM32CubeProgrammer[3] : To flash the board with an already generated binary

2.2. Hardware requirements

NUCLEO-WBA5 [4] is necessary to install the application.

Hardware platform illustration
Nucleo-WBA5


2.3. Remote interfaces compatible

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 STM32CubeWBA MCU Package[5].

P2P Router project directory
Connectivity:Connectivity WBA P2PR dir.PNG

3.2. Project description

3.2.1. Structure

Software project structure with the most important 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
Services 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

puce3.png

and puce4.png The Bluetooth® LE P2P Router application initialization is done within app_ble.c

  • Initialize the BLE stack - initialize the device as peripheral and central - configure and start advertising - APP_BLE_init()
  • Call the services controller initialization SVCCTL_Init() - svc_ctl.c
  • Manage the GAP event - SVCCTL_App_Notification()
    • HCI_LE_CONNECTION_COMPLETE - provides information of the connection interval, slave latency, supervision timeout
    • HCI_LE_CONNECTION_UPDATE_COMPLETE- provides information of new connection
    • HCI_DISCONNECTION_COMPLETE - informs the application about the link disconnection and the reason
puce5.png

The Services management is done by the service controller, svc_ctl.c

  • Initialize the number of registered handler - SVCCTL_Init()
  • Manage events - SVCCTL_UserEvtRx()- from the BLE Host Stack and redirect them to the gap event handler - SVCCTL_App_Notification


puce6.png

The application level of P2P Router is done with p2pr_app.c:

  • Initialization of P2P Router service by calling P2PR_Init() - p2pr.c
  • Initialization context of application applicative part:
    • Creation of some tasks
    • Setup data tables holding links status
  • Receive notification from the P2P Router service - P2PR_Notification()


puce7.png

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

  • Service applicative part initialization - P2PR_Init()
    • Register P2P Router Event Handler to Service Controller - SVCCTL_RegisterSvcHandler(P2PR_EventHandler)
    • Initialize service, add P2P Router service as primary service
      • Initialize write forward (WRITEFWD) characteristic
      • Initialize notif forward (NOTIFFWD) characteristic
      • Initialize device info (DEVINFO) characteristic
  • Manage the GATT event from BLE Stack - P2PR_EventHandler()
    • ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE
      • Reception of an attribute modification packet
        • Update notification state for characteristics supporting it
        • Transfer packet to applicative part by calling P2PR_Notification() function


puce8.png

Client links are managed into file gatt_client_app.c

  • Initialize the number of registered handler - SVCCTL_Init()
  • Manage events - SVCCTL_UserEvtRx()- from the BLE Host Stack and redirect them to the gap event handler - SVCCTL_App_Notification
Heart Rate Sensor software module interaction
Connectivity HeartRate-Project.png


3.3. Build and install

Follow the steps described in Bluetooth® LE Build and Install Application page, applying them for STM32WBA BLE_p2pRouter project.

3.4. How to use

Once the BLE P2P Router application is installed on STM32WBA platform, launch ST BLE ToolBox smartphone or web Bluetooth® application.
Then, scan and connect to device called p2pR_XX (where XX is replaced by the last byte of the BD address)
Once the Bluetooth® LE connection is established and the notification enabled by the smartphone:

  • Pressing button 1 will allows to add an additional end device to P2P Router network by following steps:
    • Scan is started (blue LED turned on), reports are analysed, looking for a P2P Server application running around
    • Scan is stopped once an end device if found of scan timeout appends (blue LED turned off).
    • If a

3.5. 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 application
 */
#define CFG_DEBUG_APP_TRACE     1
Heart Rate Sensor - Initialization phase Connected Phase
Wireless Firmware version 1.13.0
Wireless Firmware build 5
FUS version 1.2.0
>>== SHCI_SUB_EVT_CODE_READY

>>== WIRELESS_FW_RUNNING 
>>== DBGMCU_GetRevisionID= 2001 

==>> 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:26:ff:eb
  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: 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

Device Information Service (DIS) is added Successfully 000C
Manufacturer Name Characteristic Added Successfully  000D 
Model Number String Characteristic Added Successfully  000F 
Heart Rate Service (HRS) is added Successfully 0011
Heart Rate Measurement Characteristic Added Successfully  0012 
Sensor Location Characteristic Added Successfully  0015 
Control Point Characteristic Added Successfully  0017 
==>> aci_gap_set_discoverable - Success
==>> Success: Start Fast Advertising
>>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - Connection handle: 0x801
     - Connection established with Central: @:72:16:9d:9e:d5:8e
     - Connection Interval:   48.75 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:   48.75 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms

ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE HRS_NOTIFICATION_ENABLED
==>> hci_le_read_phy - Success
==>> PHY Param  TX= 1, RX= 1 
==>> hci_le_set_phy PHY Param  TX= 2, RX= 2 - Success

==>> HCI_LE_PHY_UPDATE_COMPLETE_SUBEVT_CODE - status ok 
==>> hci_le_read_phy - Success 
==>> PHY Param  TX= 2, RX= 2

4. References