STM32WBA Bluetooth® LE – Peer 2 Peer Applications

Revision as of 16:12, 14 February 2023 by Registered User (→‎GAP and GATT initialization and interaction)

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 expose two characteristics (Notification & Write) in order to create a bi-direction 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]

Follow this "link" 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: LEDoff
0x01: LEDon
  • 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: button1 0x00 or 0x01


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


At start up, Peer To Peer Server application starts Advertising including the Manufacturer Advertising Elements as described below:

0 1 2-3 4 5 6 7 8 9 10-15
Length Manufacturer ID Company BlueSTSDK Version Device ID Firmware ID Option 1 Option 2 Option 3 Device Address
P2P Server 0x0F 0xFF 0x0030 STMicro 0x02 0x8B Nucleo-WBA 0x83 0x00 0x00 0x00 0x08E12Axxxx

Advertising is stopped after 30s, pressing B1 restart it.

1.2. STM32WBA Peer to Peer Client application

It acts as a Central device with the support of GATT Client Layer.
At start up, 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.3. On board buttons configuration

P2P Applications Nucleo-WBA buttons configuration
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 software required are the following (minimum IDEs version):

  • IAR Embedded Workbench for ARM (EWARM) toolchain V??
  • RealView Microcontroller Development Kit (MDK-ARM) toolchain V??
  • STM32CubeIDE toolchain V?? [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. 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[5]. LINK TO UPDATE

P2P Server & Client projects directory

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


puce3.png

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

  • Initialize the BLE 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 BLE 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 1 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 BLE 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 Server software module interaction
Connectivity WBA P2Ps module interaction.png



puce3.png

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

  • Initialize the BLE 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 BLE 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 BLE Controller - SVCCTL_RegisterCltHandler();
    • Initialize LED data level and selection
  • Manage the GATT event from BLE 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 1 while connected.


P2P Client software module interaction
Connectivity WBA P2Pc module interaction.png

3.3. Build and install

Follow the steps described in Bluetooth® LE Build and Install Application page, applying them for STM32WBA BLE_p2pServer and BLE_p2pClient projects

3.4. How to use


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 SW1 button on the board while connected send a SWITCH_C notification to the smartphone and the switch level is updated on the smartphone interface.


3.5. UART debug trace

Thanks to the debug log via UART interface LINK TO UPDATE?, 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 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

4. References