STM32WBA 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, STM32WBA 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 STM32CubeWBA MCU Package[1]

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

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


1.1. STM32WBA 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 characteristic:
    • Used to control LED1 (Blue) on STM32WBA 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 STM32WBA & ST BLE Toolbox
STM32WBA 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)



Manufacturer data are encoded following 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 0x8B Nucleo-WBA 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. STM32WBA 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 Sever 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 STM32WBA P2P Server & P2P Client
STM32WBA P2P Flow Diagram


1.4. On board buttons configuration

Buttons configuration for Bluetooth® Low Energy P2P Application on Nucleo-WBA52CG 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 system requirements

The following list contains 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 WBA Firmware 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 WBA Firmware 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

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

Hardware platform illustration
Nucleo-WBA5


2.3. Collector applications compatible

The STM32CubeWBA Health Thermometer Sensor project is compatible with the following collector:

3. STM32WBA P2P Server & P2P Client examples description

3.1. Project directory

The "BLE_p2pServer" & "BLE_p2pClient"applications are available by downloading STM32CubeWBA 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 P2Ps c Archi.png
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:

P2P Server project initialization
Connectivity WBA P2Ps Initialization.png
puce1.png
  • Initialize the system (HAL, clocks, peripherals)
  • Infinite loop for run mode
puce2.png
  • Initialize the BSP, Power Mode, trace, memory manager, NVM
  • Wait for initialization done
puce3.png
  • Initialize the Bluetooth® LE Host Stack
puce4.png
  • Initialize the Bluetooth® LE GATT level
  • Initialize the Bluetooth® LE GAP level
puce5.png
  • Reset the number of registered handler
puce6.png
  • Initialize the context
  • Manage Heart Rate Service notification
puce7.png
  • Register Service Handler
  • Update services and characteristics
P2P Client project initialization
Connectivity WBA P2Pc Initialization.png
puce1.png
  • Initialize the system (HAL, clocks, peripherals)
  • Infinite loop for run mode
puce2.png
  • Initialize the BSP, Power Mode, trace, memory manager, NVM
  • Wait for initialization done
puce3.png
  • Initialize the Bluetooth® LE Host Stack
puce4.png
  • Initialize the Bluetooth® LE GATT level
  • Initialize the Bluetooth® LE GAP level
puce5.png
  • Reset the number of registered handler
puce6.png
  • Initialize the context
  • Register Client Handler

3.2.3. GAP and GATT initialization and interaction

P2P Server:

P2P Server software module interaction
Connectivity WBA 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 SVCCTL_Init() - svc_ctl.c
  • Manage the GAP event - SVCCTL_App_Notification() - svc_ctl.c
    • HCI_LE_CONNECTION_COMPLETE - provides information of the connection interval, slave latency, supervision timeout
    • HCI_LE_CONNECTION_UPDATE_COMPLETE- provides the new information of the 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 Bluetooth® Low Energy Host Stack and redirect them to the gap event handler - SVCCTL_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 - SVCCTL_RegisterSvcHandler();
    • 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_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)

P2P Client:

P2P Client software module interaction
Connectivity WBA 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 SVCCTL_Init() - svc_ctl.c
  • Manage the GAP event - SVCCTL_App_Notification() - svc_ctl.c
    • HCI_LE_CONNECTION_COMPLETE - provides information of the connection interval, slave latency, supervision timeout
    • HCI_LE_CONNECTION_UPDATE_COMPLETE- provides the new information of the 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 Bluetooth® Low Energy Host Stack and redirect them to the gap event handler - SVCCTL_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 - SVCCTL_RegisterCltHandler();
    • Initialize LED data level and selection
  • Manage the GATT event from Bluetooth® Low Energy Stack - gatt_parse_notification()
    • ACI_GATT_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 STM32WBA 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 send 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 P2P Client platform while not connected, start a scan procedure, and connection is done if a P2P Server platform is found.
    • Pressing Button B1 on P2P Client platform while connected, send a GATT event to the P2P Server platform in order to switch On/Off server blue LED.
  • On P2P Server side:
    • Pressing Button B1 on P2P Server side while connected send a SWITCH_C notification to the P2P Client platform and toggle the blue LED on client side.
    • Pressing Button B2 on P2P Server side while connected, start a new advertising in order to illustrate multi link feature.
    • Pressing Button B2 on P2P Server side while not connected, clear 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_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:7c:ea
  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: P2P_Server
  Success: aci_gatt_add_char command   : LED_C
  Success: aci_gatt_add_char command   : SWITCH_C
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 @:7d:64:80:ff:2e:4f
     - 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
-- P2P APPLICATION SERVER : NOTIFICATION ENABLED

-- GATT : LED CONFIGURATION RECEIVED
-- P2P APPLICATION SERVER : LED1 ON
P2P Client - Initialization phase P2P Client - Connected Phase and LED On notification
==>> 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:7d:0c
  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 - Appearance
  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

==>> aci_gap_start_general_discovery_proc - Success
  P2P server, db addr 0x00:80:E1:2A:7C:EA
==>> aci_gap_terminate_gap_proc - Success
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
-- GAP_GENERAL_DISCOVERY_PROC completed
Create connection to P2Pserver
  wait for event HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
>>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - 
     Connection handle: 0x0001
     - Connection established with @:00:80:e1:2a:7c:ea
     - Connection Interval:   50.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
Discover services, characteristics and descriptors for 
 P2Pserver
GATT services discovery
GATT services discovery
PROC_GATT_DISC_ALL_PRIMARY_SERVICES 
 aci_gatt_disc_all_primary_services cmd NOK status =0x0C

DISCOVER_ALL_CHARS ConnHdl=0x0001 
 ALLServiceHandle[0x0000 - 0x0000]
All characteristics discovery Failed, status =0x0C

DISCOVER_ALL_CHAR_DESCS [0x0000 - 0x0000]
All characteristic descriptors discovery Failed, status =0x0C

All notifications enabled Successfully

ACI_ATT_READ_BY_GROUP_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0001
  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=0x0001
  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=0x0001 
 ALLServiceHandle[0x0001 - 0x0011]
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0001
  ConnHdl=0x0001, 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=0x2A00, Properties=0x000A, 
    CharHandle [0x0006 - 0x0007], 
GAP DEVICE_NAME charac found
    3/3 short UUID=0x2A01, Properties=0x0002, 
    CharHandle [0x0008 - 0x0009], 
GAP APPEARANCE charac found
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0001
  ConnHdl=0x0001, number of value pair = 1
    1/1 short UUID=0x2A04, Properties=0x0002, 
    CharHandle [0x000A - 0x000B]
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0001
  ConnHdl=0x0001, number of value pair = 1
    1/1 short UUID=0xFE41, Properties=0x0006, 
    CharHandle [0x000D - 0x000E],
ST_P2P_WRITE_CHAR_UUID charac found
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0001
  ConnHdl=0x0001, number of value pair = 1
    1/1 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=0x0001
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
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0001
  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
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0001
PRIMARY_SERVICE_UUID=0x2800 handle=0x000C
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 
  CharStartHandle=0x000D
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0001
  UUID=0xFE41, handle=0x000E, found ST_P2P_WRITE_CHAR_UUID
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0001
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 
  CharStartHandle=0x000F
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0001
  UUID=0xFE42, handle=0x0010, found ST_P2P_NOTIFY_CHAR_UUID
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0001
Descriptor UUID=0x2902, handle=0x000F-0x0010-0x0011
 P2P Notification found : 
Desc UUID=0x2902 handle=0x000F-0x0010-0x0011
All characteristic descriptors discovered Successfully

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

ACI_GATT_NOTIFICATION_VSEVT_CODE - ConnHdl=0x0001, 
  Attribute_Handle=0x0010
  Incoming Nofification received P2P Server information
  P2P APPLICATION CLIENT : NOTIFICATION RECEIVED - LED ON

4. P2P Client - smartphone application

4.1. ST BLE ToolBox application

Once the BLE P2P Server application is installed on the STM32WBA 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