STM32WB0 Bluetooth® LE – P2P router

1. Peer-to-peer router profile

The Peer-to-peer router application (P2P) is intended to demonstrate the multilink capabilities of the STM32WB0 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 article describes the Peer-to-peer router project provided within the STM32CubeWB0 MCU package[1].

Application overview
Connectivity WB0 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 - Device info 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 STM32WB0 microcontrollers and ST Bluetooth® LE toolbox smartphone application
STM32WB0 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 0x8D
NUCLEO-WB0
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 STM32WB0 Serie Nucleo boards
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 hardware requirements

For software and hardware requirements, refer to STM32WB0 Build BLE Project wiki article.

2.2. Compatible remote interfaces

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

3. STM32WB0 P2P router description

3.1. Project directory

The "BLE_p2pRouter" application is available by downloading the STM32CubeWB0 MCU package[1].


Refer to the How to Build a Bluetooth® LE project wiki article 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 WB0 P2P router Archi.png
Connectivity yellow box.png
Main applicative part files
Connectivity dark blue box.png
Service management
Connectivity green box.png
Bluetooth® Low Energy libraries
Connectivity pink box.png
Bluetooth® Low Energy stack modular configuration options and event dispatcher files

WARNING: Do not modify the files in Middlewares folder


3.2.2. Application initialization

The different steps of the application initialization are described below:

P2P Router project initialization
Connectivity WB0 P2P router Initialization.png
puce1.png
Initialize the system (HAL, clocks, peripherals).
HAL_Init();
SystemClock_Config();
PeriphCommonClock_Config();
MX_GPIO_Init();
MX_RADIO_Init();
MX_RADIO_TIMER_Init();
MX_PKA_Init();
MX_APPE_Init();
puce2.png
Initialize the RNG, AES block, PKA:
HW_RNG_Init();
HW_AES_Init();
HW_PKA_Init();
APP_BLE_Init();
puce3.png
Initialize the Bluetooth® LE host stack
BLE_Init();
puce4.png
Initialize the Bluetooth® LE GATT level

Initialize the Bluetooth® LE GAP level

P2PR_APP_Init();
puce5.png
Initialize the Application/Profile context (Services, Characteristics)

Manage the Peer 2 Peer Service notification

P2PR_Init();
puce6.png
Register Service Handler

Update services and characteristics

puce7.png
Initialize the Application/Profile context

Register Client Handler

3.2.3. GAP and GATT initialization and interaction

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 BLEEVT_Init() - ble_evt.c.
  • Management of the GAP event - BLEEVT_App_Notification() - ble_evt.c
    • HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE provides information on the connection interval, slave latency, and supervision timeout.
    • HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE provides information on the new connection.
    • HCI_DISCONNECTION_COMPLETE_EVT_CODEinforms the application about the link disconnection and the reason.

Event management is done by the Bluetoooth® Low Energy Event Dispatcher, ble_evt.c

  • Initialization of the number of registered handler - BLEEVT_Init().
  • Management of the events from the Bluetooth® Low Energy host stack by calling P2PR_APP_EvtRx(), and redirection to the gap event handler using BLEEVT_App_Notification.


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

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 - BLEEVT_RegisterGattEvtHandler(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_SRV_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.

Client links are managed into the gatt_client_app.c file:

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

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

Once the BLE P2P router application is installed on the STM32WB0 platform, launch the ST BLE ToolBox smartphone or the Bluetooth® web application.

Follow the following steps to make the program functions:

  • Connect to STM32WB0 device, select p2pR_xx device in the list.
  • Power up to seven p2pServer devices next to the BLE_p2pRouter device.
  • With a click on B1, BLE_p2pRouter scan and then connect to a p2pServer device surrounding.
  • Devices should appear on the smartphone interface.
  • On p2pServer device, a click on B1 sends a notification to BLE_p2pRouter.
    • This GATT notification message is forwarded to the smartphone and displayed on the interface.
  • On the smartphone interface, a GATT write command can be sent to selected p2pServer devices, changing the led status.
    • This write message is sent first to BLE_p2pRouter and then routed to its destination. The blue led toggle on selected p2pServer devices.


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_conf.h as described below:

/**
 * Enable or disable traces in the application
 */
#define CFG_DEBUG_APP_TRACE     1
Log
==>> Start BLE_Init function
  Success: aci_hal_set_tx_power_level command
  Success: aci_gatt_srv_profile_init command
  Success: aci_gap_init command
  Static Random Bluetooth Address: de:84:76:49:53:4b
  Success: Gap_profile_set_dev_name - Device Name
  Success: Gap_profile_set_appearance - Appearance
  Success: hci_le_set_default_phy command
  Success: aci_gap_set_io_capability command
  Success: aci_gap_set_security_requirements command
  Success: aci_gap_configure_filter_accept_and_resolving_list command
==>> End BLE_Init function

Services and Characteristics creation
  Success: aci_gatt_srv_add_service command: p2pr
End of Services and Characteristics creation

  Success: aci_hal_set_radio_activity_mask command
==>> Success: aci_gap_set_advertising_configuration
==>> Success: aci_gap_set_advertising_data
==>> Success: aci_gap_set_advertising_enable
Button 1 pressed
==>> aci_gap_set_scan_configuration - Success
==>> aci_gap_start_procedure - Success
  p2pServer name: p2pS_D7, db addr: 0xD9:BA:9C:28:8F:D7
  -> stored into p2pRouter table at index 0
==>> 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
==>> aci_gap_set_connection_configuration Success , result: 0x00
  wait for event HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE
HCI_LE_META_EVT: 0x14
>>== hci_le_connection_complete_event - Connection handle: 0x0801
     - Connection established with @:d9:ba:9c:28:8f:d7
     - Connection Interval:   100.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
  Success: MTU exchange
  MTU exchanged size = 247
  MTU exchanged size = 247
HCI_VENDOR_EVT: 0x0C03
HCI_VENDOR_EVT: 0x0C10
Discover services, characteristics and descriptors for table index 0
GATT services discovery
ACI_ATT_READ_BY_GROUP_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  1/2 short UUID=0x1801, handle [0x0001 - 0x000A], GENERIC_ATTRIBUTE_SERVICE_UUID found
  2/2 short UUID=0x1800, handle [0x000B - 0x000F], GAP_SERVICE_UUID found
HCI_VENDOR_EVT: 0x0C0A
ACI_ATT_READ_BY_GROUP_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  1/1 short UUID=0xFE40, handle [0x0010 - 0x0015], P2P_SERVICE_UUID found
HCI_VENDOR_EVT: 0x0C0A
EVENT OPCODE: 0x0C11
HCI_VENDOR_EVT: 0x0C11
HCI_VENDOR_EVT: 0x0C10
PROC_GATT_CTL_DISC_ALL_PRIMARY_SERVICES services discovered Successfully

DISCOVER_ALL_CHARS ConnHdl=0x0801 ALLServiceHandle[0x0001 - 0x0015]
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  ConnHdl=0x0801, number of value pair = 6
    1/6 short UUID=0x2A05, Properties=0x0020, CharHandle [0x0002 - 0x0003], GATT SERVICE_CHANGED_CHARACTERISTIC_UUID charac found
    2/6 short UUID=0x2B29, Properties=0x000A, CharHandle [0x0005 - 0x0006]
    3/6 short UUID=0x2B2A, Properties=0x0002, CharHandle [0x0007 - 0x0008]
    4/6 short UUID=0x2B3A, Properties=0x0002, CharHandle [0x0009 - 0x000A]
    5/6 short UUID=0x2A00, Properties=0x0002, CharHandle [0x000C - 0x000D], GAP DEVICE_NAME charac found
    6/6 short UUID=0x2A01, Properties=0x0002, CharHandle [0x000E - 0x000F], GAP APPEARANCE charac found
HCI_VENDOR_EVT: 0x0C06
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  ConnHdl=0x0801, number of value pair = 2
    1/2 short UUID=0xFE41, Properties=0x0006, CharHandle [0x0011 - 0x0012], ST_P2P_WRITE_CHAR_UUID charac found
    2/2 short UUID=0xFE42, Properties=0x0010, CharHandle [0x0013 - 0x0014], ST_P2P_NOTIFY_CHAR_UUID charac found
HCI_VENDOR_EVT: 0x0C06
EVENT OPCODE: 0x0C11
HCI_VENDOR_EVT: 0x0C11
HCI_VENDOR_EVT: 0x0C10
All characteristics discovered Successfully

DISCOVER_ALL_CHAR_DESCS [0x0001 - 0x0015]
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
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
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0005
  UUID=0x2B29, handle=0x0006
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0007
  UUID=0x2B2A, handle=0x0008
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0009
  UUID=0x2B3A, handle=0x000A
PRIMARY_SERVICE_UUID=0x2800 handle=0x000B
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x000C
  UUID=0x2A00, handle=0x000D, found GAP DEVICE_NAME_UUID
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x000E
  UUID=0x2A01, handle=0x000F, found GAP APPEARANCE_UUID
PRIMARY_SERVICE_UUID=0x2800 handle=0x0010
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0011
HCI_VENDOR_EVT: 0x0C04
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
  UUID=0xFE41, handle=0x0012, found ST_P2P_WRITE_CHAR_UUID
HCI_VENDOR_EVT: 0x0C04
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0013
HCI_VENDOR_EVT: 0x0C04
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
  UUID=0xFE42, handle=0x0014, found ST_P2P_NOTIFY_CHAR_UUID
HCI_VENDOR_EVT: 0x0C04
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
Descriptor UUID=0x2902, handle=0x0013-0x0014-0x0015P2PNotification found : Desc UUID=0x2902 handle=0x0013-0x0014-0x0015
HCI_VENDOR_EVT: 0x0C04
HCI_VENDOR_EVT: 0x0C10
All characteristic descriptors discovered Successfully

EVENT OPCODE: 0x0C11
HCI_VENDOR_EVT: 0x0C11
HCI_VENDOR_EVT: 0x0C10
 ServiceChangedCharDescHdl =0x0004
HCI_VENDOR_EVT: 0x0C10
 P2PNotificationDescHdl =0x0015
All notifications enabled Successfully

4. References