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.
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 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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 | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
- SWITCH Characteristic:
- Used to switch LED or Lamp on Collector device.
Peer to Peer Service - SWITCH Characteristic | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
1.2. Advertising data
At startup, Peer To Peer Server application starts Advertising.
Data advertised are composed as follows:
P2P Server Advertising packet | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
The manufacturer data are encoded following the STMicroelectronics BlueST SDK v2 as described below:
STMicroelectronics Manufacturer Advertising data | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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.
1.4. On board buttons configuration
Buttons configuration for Bluetooth® Low Energy P2P Application on STM32WB0 Serie Nucleo boards | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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:
3.2.2. Application initialization
The different steps of the application initialization are described below:
3.2.3. GAP and GATT initialization and interaction
P2P Server:
and 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
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()
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
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(¬ification);
- Notify application of the Measurement Notification - P2P_SERVER_Notification(P2P_SERVER_LED_C_WRITE_NO_RESP_EVT)
- Reception of an attribute modification - LED C Value : ON or OFF Indication
- ACI_GATT_SRV_ATTRIBUTE_MODIFIED_VSEVT_CODE
P2P Client:
and 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
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()
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()
- ACI_GATT_CLT_NOTIFICATION_VSEVT_CODE
- 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.
5. References