STM32WB0 Bluetooth® LE – Peer 2 Peer Applications

1. Peer to Peer Profile

The Peer to Peer Profile is a Generic Attribute Profile (GATT) based low-energy profile defined by STMicroelectronics with proprietary UUIDs(128 bits).
The Peer to Peer Profile, widely used for direct connection, defines the communication between a GATT-server of a Peer to Peer Server device, and a GATT-client Collector device, such as a smartphone, STM32WB0 Peer to Peer Client or Peer to Peer Router devices.

The Peer to Peer Server
  • Contains the P2P Service, which exposes two characteristics (Notification and Write) in order to create a bi-directional data communication.
  • Is the GATT server
The Collector
  • Accesses the information exposed by Peer to Peer Server, controls it with the Write characteristics, receives Notification from it.
  • Is the GATT client

This wiki page is the description of the Peer to Peer Server and Peer to Peer Client projects provided within the STM32CubeWB0 MCU Package[1]

Refer to the following page for the Peer to Peer Router project explanation.

Bluetooth® LE Peer to Peer Profile & STM32WB0
STM32WB0 P2P Profile


1.1. STM32WB0 Peer to Peer Server application

The table below describes the structure of Peer to Peer service:

Bluetooth® LE Peer to Peer Service specification
Service Characteristic Mode UUID size
P2P Service 0000FE40-cc7a-482a-984a-7f2ed5b3e58f
LED Write without Response/Read 0000FE41-8e22-4541-9d4c-21edae82ed19 2
SWITCH Notify 0000FE42-8e22-4541-9d4c-21edae82ed19 2

Peer To Peer Server is exported as a Service

  • LED characteristics:
    • Used to control LED1 (Blue) on STM32WB0 platform by remote device .
Peer to Peer Service - LED Characteristic
Byte Index 0 1
Name LED Selection LED Level
Value 0x01: Blue LED 0x00: LED off
0x01: LED on
  • SWITCH Characteristic:
    • Used to switch LED or Lamp on Collector device.
Peer to Peer Service - SWITCH Characteristic
Byte Index 0 1
Name Button Selection Status
Value 0x01: button 1 0x00 or 0x01


Example of flow diagram between STM32WB0 & ST BLE Toolbox
STM32WB0 P2P Flow Diagram


1.2. Advertising data

At startup, Peer To Peer Server application starts Advertising.
Data advertised are composed as follows:

P2P Server Advertising packet
Description Length AD Type Value
Device Name 8 0x09 p2pS_XX (XX: last byte of BD address)
Manufacturer Data 15 0xFF See table below
Flags 2 0x01 0x06
(GeneralDiscoverable, BrEdrNotSupported)


The manufacturer data are encoded following the STMicroelectronics BlueST SDK v2 as described below:

STMicroelectronics Manufacturer Advertising data
Byte Index 0 1 2-3 4 5 6 7 8 9 10-15
Function Length Manufacturer ID Company BlueST SDK Version Device ID Firmware ID Option 1 Option 2 Option 3 Device Address
Value 0x0F 0xFF 0x0030 STMicro 0x02 0x8D Nucleo-WB0 0x83 - P2P Server 0x00 0x00 0x00 0x08E12Axxxx


Info white.png Information
Advertising is stopped after 30s. You can press B1 to restart it.

1.3. STM32WB0 Peer to Peer Client application

It acts as a Central device with the support of GATT Client Layer.
At startup, by pressing B1 button, Peer To Peer Client application:

  • Starts scanning to detect Peer To Peer Server application by filtering the Firmware ID of the STMicroelectronics Manufacturer advertising data
  • Stops Scanning once P2P server detected
  • Connects to the P2P Server to establish the connection.
  • Discovers GATT Service & Characteristics of the P2P server
  • Enable all GATT server notification characteristics

Once connected, the Peer To Peer Client application:

  • By pressing B1 button, write a message to toggle the Blue LED of the P2P Server
  • Receive notification to toggle the on board Blue LED from remote P2P Server Application.
Example of flow diagram between STM32WB0 P2P Server & P2P Client
STM32WB0 P2P Flow Diagram


1.4. On board buttons configuration

Buttons configuration for Bluetooth® Low Energy P2P 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

Adv start

Notification

/ Clear Sec db

Adv start (Multi conn)

/ -

L2CAP CONN REQ

/
P2P Client Idle

Connected

Start Scan

Write Char

/ Clear Sec db

-

/ -

Change Conn Int

/

2. Requirements

2.1. Software and Hardware requirements

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

2.2. Collector applications compatible

The STM32CubeWB0 Peer To Peer Server project is compatible with the following collector:

3. STM32WB0 P2P Server & P2P Client examples description

3.1. Project directory

The "BLE_p2pServer" & "BLE_p2pClient"applications are available by downloading STM32CubeWB0 MCU Package[1].

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

3.2. Project description

3.2.1. Structure

Software project structure with the most important parts:

P2P Server / Client project structure
Connectivity WBA P2P server client Architecture.png
Connectivity yellow box.png
Main applicative part files
Connectivity dark blue box.png
Services 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 Server project initialization
Connectivity WB0 P2Ps 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

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

Manage the Peer 2 Peer Service notification

P2P_SERVER_Init(); /* p2p_server.c */
puce6.png
Register Service Handler

Update services and characteristics

P2P Client project initialization
Connectivity WB0 P2Pc 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
GATT_CLIENT_APP_Init(); 
puce5.png
Initialize the Application/Profile context (Services, Characteristics)
Register Service Handler

3.2.3. GAP and GATT initialization and interaction

P2P Server:

P2P Server software module interaction
Connectivity WB0 P2Ps module interaction.png


puce3.png

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

  • Initialize the Bluetooth® Low Energy stack - initialize the device as peripheral - configure and start advertising: ADV parameters, local name, UUID - APP_BLE_init()
  • Call the services controller initialization BLEEVT_Init() - ble_evt.c.
  • Manage the GAP event - BLEEVT_App_Notification() - ble_evt.c
    • HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE- provides information of the connection interval, slave latency, supervision timeout
    • HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE- provides the new information of the connection
    • HCI_DISCONNECTION_COMPLETE_EVT_CODE- informs the application about the link disconnection and the reason
puce5.png

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

  • Initialize the number of registered handler - BLEEVT_Init()
  • Manage events - BLE_STACK_Event()- from the Bluetooth® Low Energy Host Stack and redirect them to the gap event handler - BLEEVT_App_Notification()
puce6.png

The application level of the P2P Server is done with p2p_server_app.c:

  • Initialization of the services:
    • P2P Server Service - P2P_SERVER_Init() - p2p_server.c
  • Initialization of the context of the application
  • Receive notification from the P2P Server Service - P2P_SERVER_Notification()
  • When P2P_SERVER_LED_C_WRITE_NO_RESP_EVT event is received from the P2P Client, the blue LED is toggled.
  • P2P_SERVER_Switch_c_SendNotification() function is called on Button B1 press, to notify the P2P Client:
    • P2P_SERVER_UpdateValue() is called with the notification data - p2p_server.c
puce7.png

The Custom P2P Server Service p2p_server.c manages the specification of the service:

  • Service Init - P2P_SERVER_Init()
    • Register P2P Server Event Handle to Service Controller - BLEEVT_RegisterGattEvtHandler();
    • Initialize Service UUID – add P2P Server service as Primary services
      • Initialize LED_C characteristic
      • Initialize SWITCH_C characteristic
  • Manage the GATT event from Bluetooth® Low Energy Stack - P2P_SERVER_EventHandler()
    • ACI_GATT_SRV_ATTRIBUTE_MODIFIED_VSEVT_CODE
      • Reception of an attribute modification - LED C Value : ON or OFF Indication
        • Notify application of the Measurement Notification - P2P_SERVER_Notification(P2P_SERVER_LED_C_WRITE_NO_RESP_EVT)
          notification.EvtOpcode = P2P_SERVER_LED_C_WRITE_NO_RESP_EVT;         
          P2P_SERVER_Notification(&notification);
          

P2P Client:

P2P Client software module interaction
Connectivity WB0 P2Pc module interaction.png
puce3.png

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

  • Initialize the Bluetooth® Low Energy stack - initialize the device as peripheral - configure and start advertising: ADV parameters, local name, UUID - APP_BLE_init()
  • Call the services controller initialization BLEEVT_Init() - ble_evt.c.
  • Manage the GAP event - BLEEVT_App_Notification().
    • HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE- provides information of the connection interval, slave latency, supervision timeout
    • HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE- provides the new information of the connection
    • HCI_DISCONNECTION_COMPLETE_EVT_CODE- informs the application about the link disconnection and the reason
puce5.png

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

  • Initialize the number of registered handler - BLEEVT_Init()
  • Manage events - BLE_STACK_Event()- from the Bluetooth® Low Energy Host Stack and redirect them to the gap event handler - BLEEVT_App_Notification()
puce6.png

The application level of the P2P Client is done with gatt_client_app.c:

  • Initialization of the application context
    • Register Event Handler to Bluetooth® Low Energy Controller - P2P_CLIENT_EventHandler();
    • Initialize LED data level and selection
  • Manage the GATT event from Bluetooth® Low Energy Stack - gatt_parse_notification()
    • ACI_GATT_CLT_NOTIFICATION_VSEVT_CODE
      • When the notification event is received from the P2P Server, the blue LED is toggled - gatt_Notification()
  • Write characteristic to P2P Server (LED toggling) - P2Pclient_write_char() - by pressing Button B1 while connected.

3.3. How to use the Bluetooth® Low Energy P2P Server and P2P Client applications

P2P Server VS Smartphone
Once the BLE P2P Server application is installed on the STM32WB0 platform, launch ST BLE ToolBox smartphone application.

Then, scan and connect the device called p2pS_XX (where XX is replaced by the last byte of the BD address) to the application.
Once the Bluetooth® LE connection is established and the notification enabled by the smartphone (by clicking on P2P Server icon):

  • LED_C value can be modified by clicking on the LED button on smartphone interface, the blue LED of the Nucleo board is switched On/Off accordingly.
  • Pressing Button B1 on the board while connected sends a SWITCH_C notification to the smartphone and the switch level is updated on the smartphone interface.

P2P Server VS P2P Client
At least two Nucleo platforms are required, one flashed with the BLE P2P Server application and one with the BLE P2P Client application.

  • On P2P Client side:
    • Pressing Button B1 on the P2P Client platform while not connected, starts a scan procedure, and a connection is done if a P2P Server platform is found.
    • Pressing Button B1 on the P2P Client platform while connected, sends a GATT event to the P2P Server platform in order to switch On/Off the server blue LED.
  • On P2P Server side:
    • Pressing Button B1 on the P2P Server side while connected sends a SWITCH_C notification to the P2P Client platform and toggles the blue LED on client side.
    • Pressing Button B2 on the P2P Server side while connected, starts a new advertising in order to illustrate multi link feature.
    • Pressing Button B2 on the P2P Server side while not connected, clears the security DataBase of the platform.

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 application
 */
#define CFG_DEBUG_APP_TRACE         (1)
P2P Server - Initialization phase P2P Server - Connected Phase and LED On notification
==>> 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: fa:97:57:3a:5b:50
  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: p2p_Server
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
==>> Success: Start Advertising
>>== hci_le_connection_complete_event - Connection handle: 0x0801
     - Connection established with @:61:7b:8d:4f:4a:29
     - Connection Interval:   45.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
  Success: aci_gatt_confirm_indication command
HCI_VENDOR_EVT: 0x0C0E
HCI_VENDOR_EVT: 0x0C17
>>== 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:   45.00 ms
     - Connection latency:    0
     - Supervision Timeout:   5000 ms
HCI_VENDOR_EVT: 0x0C01
HCI_VENDOR_EVT: 0x0C03
-- P2P APPLICATION SERVER : NOTIFICATION ENABLED
P2P Client - Initialization phase P2P Client - Connected Phase and LED On notification
==>> 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: c0:d5:34:ed:fe:95
  Success: Gap_profile_set_dev_name - Device Name
  Success: Gap_profile_set_appearance - Appearance
  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
==>> aci_gap_set_scan_configuration - Success
==>> aci_gap_start_procedure - Success
  P2P server, db addr 0xDE:84:76:49:53:4B
==>> aci_gap_terminate_gap_proc - Success
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
-- GAP_GENERAL_DISCOVERY_PROC completed
Create connection to P2Pserver
==>> 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 @:de:84:76:49:53:4b
     - Connection Interval:   50.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
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
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 = 3
    1/3 short UUID=0x2A05, Properties=0x0020, CharHandle [0x0002 - 0x0003], GATT SERVICE_CHANGED_CHARACTERISTIC_UUID charac found
    2/3 short UUID=0x2B29, Properties=0x000A, CharHandle [0x0005 - 0x0006]
    3/3 short UUID=0x2B2A, Properties=0x0002, CharHandle [0x0007 - 0x0008]
HCI_VENDOR_EVT: 0x0C06
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  ConnHdl=0x0801, number of value pair = 3
    1/3 short UUID=0x2B3A, Properties=0x0002, CharHandle [0x0009 - 0x000A]
    2/3 short UUID=0x2A00, Properties=0x0002, CharHandle [0x000C - 0x000D], GAP DEVICE_NAME charac found
    3/3 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 = 1
    1/1 short UUID=0xFE41, Properties=0x0006, CharHandle [0x0011 - 0x0012], ST_P2P_WRITE_CHAR_UUID charac found
HCI_VENDOR_EVT: 0x0C06
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  ConnHdl=0x0801, number of value pair = 1
    1/1 short UUID=0xFE42, Properties=0x0010, CharHandle [0x0013 - 0x0014], ST_P2P_NOTIFY_CHAR_UUID charac found
HCI_VENDOR_EVT: 0x0C06
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
HCI_VENDOR_EVT: 0x0C04
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
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
HCI_VENDOR_EVT: 0x0C04
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
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
HCI_VENDOR_EVT: 0x0C04
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
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

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

ACI_GATT_NOTIFICATION_VSEVT_CODE - ConnHdl=0x0801, Attribute_Handle=0x0014
  Incoming Nofification received P2P Server information
  P2P APPLICATION CLIENT : NOTIFICATION RECEIVED - LED ON
HCI_VENDOR_EVT: 0x0C0F

4. P2P Client - smartphone application

4.1. ST BLE ToolBox application

Once the BLE P2P Server application is installed on the STM32WB0 platform, launch the ST BLE ToolBox smartphone application. The application starts scanning and allows to see advertisement data or to connect to the device. Once the device is connected an interface allows to switch On/Off the blue LED of the platform, and a notification reflect the Switch_C characteristic status.

BLE P2P application on ST BLE ToolBox
Connectivity WBA P2P Toolbox scan.jpg
Connectivity WBA P2P Toolbox adv.jpg
Connectivity WBA P2P Toolbox connected.jpg


5. References