Wi-Fi: P2P
The P2P sample demonstrates how to establish a Wi-Fi® Peer-to-Peer (P2P) connection between devices using the Wi-Fi Direct® functionality.
Requirements
The sample supports the following development kits:
Hardware platforms |
PCA |
Board name |
Shields |
|
|---|---|---|---|---|
PCA10217 |
|
|||
PCA10143 |
|
|||
PCA10184 |
|
|
||
PCA10095 |
|
|
Overview
The sample demonstrates Wi-Fi P2P functionality using the following operating modes:
Client (CLI) mode - The device discovers a peer and joins or negotiates a P2P connection.
Group Owner (GO) mode - The device forms and manages an autonomous P2P group.
Persistent Group Owner mode - The device forms an autonomous persistent group and verifies, in code, that the stored group is reused (re-invoked by ID with the same credentials) across group-add cycles.
The Group Owner (GO) mode can optionally run a TCP echo server, and the client can run a matching TCP echo client, to exercise the data path over the P2P link.
Wi-Fi P2P enables direct communication between devices without requiring a traditional access point.
For more information, see Wi-Fi Direct (P2P) mode.
Configuration
See Configuring and building for information about how to permanently or temporarily change the configuration.
Configuration options
The following sample-specific Kconfig options are used in this sample (located in samples/wifi/p2p/Kconfig):
- CONFIG_SAMPLE_P2P_CLI_MODE
(bool) CLI mode (Client: Find peer and connect)
This configuration option enables the device to act as a P2P client.
- CONFIG_SAMPLE_P2P_GO_MODE
(bool) GO mode (Group owner: Group add/remove)
This configuration option enables the device to act as a P2P Group Owner. AP mode is enabled per board in the sample’s boards/ configuration files.
- CONFIG_SAMPLE_P2P_GO_PERSISTENT_MODE
(bool) GO mode (Persistent: autonomous persistent group)
This configuration option enables an autonomous persistent Group Owner (P2P_GROUP_ADD with persistent_set) that verifies in code, across cycles, that the stored group is reused. GO-only; persistence is RAM-only here.
- CONFIG_SAMPLE_P2P_DISCOVERY_FULL
(bool) Full discovery
This configuration option enables the device to scan all Wi‑Fi channels while searching for P2P peers.
- CONFIG_SAMPLE_P2P_DISCOVERY_SOCIAL
(bool) Social discovery
This configuration option enables the device to scan only the Wi-Fi Direct social channels (1, 6, 11).
- CONFIG_SAMPLE_P2P_DISCOVERY_PROGRESSIVE
(bool) Progressive discovery
This configuration option enables alternating between social channel scans and full scans.
- CONFIG_SAMPLE_P2P_FIND_TIMEOUT_S
(int) P2P discovery timeout (seconds)
This configuration option sets the timeout, in seconds, during which the device performs P2P peer discovery.
- CONFIG_SAMPLE_P2P_GO_INTENT_VALUE
(int) GO intent value
This configuration option sets the Group Owner intent value used during P2P negotiation.
- CONFIG_SAMPLE_P2P_FREQUENCY
(int) P2P connection frequency (MHz)
This configuration option sets the frequency used when initiating the P2P connection.
- CONFIG_SAMPLE_P2P_JOIN
(bool) Join an existing P2P group
This configuration option enables the device to join an existing P2P group instead of creating a new one.
- CONFIG_SAMPLE_P2P_PEER_ADDRESS
(string) P2P peer MAC address
This configuration option sets the MAC address of the peer P2P device.
- CONFIG_SAMPLE_P2P_DHCP_TIMEOUT_S
(int) DHCP timeout in seconds
- CONFIG_SAMPLE_P2P_ECHO_CLI_MODE
(bool) Echo client mode
This configuration options helps to start a TCP echo client. Connect to Go IP address and send test strings and validates the echoed response.
- CONFIG_SAMPLE_P2P_ECHO_SERVER_ADDR
(string) Echo server (P2P GO) IPv4 address
This configuration option sets the IPv4 address of the P2P Group Owner running the echo server. It must match CONFIG_NET_CONFIG_MY_IPV4_ADDR used when building the GO image, since the two are set independently.
- CONFIG_SAMPLE_P2P_ECHO_CLI_PORT
(int) P2P server port for echo client
This configuration option sets the operating port used when creating the P2P Echo Client. It must match CONFIG_SAMPLE_P2P_ECHO_SERVER_PORT on the GO image, since the two are set independently.
- CONFIG_SAMPLE_P2P_ECHO_CLI_COUNT
(int) Number of echo requests to send
This configuration option sets how many echo request/response iterations the client performs before returning.
- CONFIG_SAMPLE_P2P_ECHO_CLI_INTERVAL_MS
(int) Delay between echo requests (ms)
This configuration option sets the delay, in milliseconds, between successive echo requests sent by the client.
- CONFIG_SAMPLE_P2P_ECHO_CLI_REPLY_TIMEOUT_MS
(int) Echo reply timeout (ms)
This configuration option sets how long, in milliseconds, the client waits for an echo reply before aborting the echo test.
- CONFIG_SAMPLE_P2P_ECHO_MSG_BUF_SIZE
(int) Echo message buffer size (bytes)
Size, in bytes, of the TCP buffer used by the echo client (to hold the reply) and the echo server (to receive and echo back). Data larger than this is handled in chunks of this size.
- CONFIG_SAMPLE_P2P_CONNECTION_METHOD_PIN
(bool) PIN method
This configuration option enables establishing the P2P connection using a WPS PIN.
- CONFIG_SAMPLE_P2P_CONNECTION_METHOD_PBC
(bool) Push Button Configuration (PBC)
This configuration option enables the device to establish the P2P connection using WPS Push Button Configuration (PBC).
- CONFIG_SAMPLE_P2P_METHOD_DISPLAY
(bool) Display PIN method
This configuration option enables the device to generate and display a PIN which must be entered on the peer device.
- CONFIG_SAMPLE_P2P_METHOD_KEYPAD
(bool) Keypad PIN method
This configuration option enables the device to use the PIN provided by the peer device.
- CONFIG_SAMPLE_P2P_CONNECTION_METHOD_PIN_VALUE
(string) PIN value
This configuration option sets the PIN value to be used when the keypad method is selected.
- CONFIG_SAMPLE_P2P_DHCPV4_SERVER
(bool) DHCPv4 server
This configuration option enables DHCPV4 server settings.
- CONFIG_SAMPLE_P2P_DHCPV4_POOL_START
(string) Start address of DHCPv4 pool
This configuration option sets the start IP address of DHCPv4 pool.
- CONFIG_SAMPLE_P2P_GO_FREQ
(int) P2P GO operating frequency (MHz)
This configuration option sets the operating frequency used when creating the P2P Group Owner group.
- CONFIG_SAMPLE_P2P_GO_VHT
(bool) VHT support
This configuration option enables the device to allow Very High Throughput (VHT) capabilities when creating the P2P group.
- CONFIG_SAMPLE_P2P_GO_BSSID
(string) GO BSSID
This configuration option sets the BSSID to be used when creating the P2P group. If left empty, the BSSID is selected automatically.
- CONFIG_SAMPLE_P2P_IFACE_NAME
(string) Interface name
This configuration option sets the interface name used when removing the P2P group.
- CONFIG_SAMPLE_P2P_PEER_DISCONN_TIMEOUT_S
(int) Timeout waiting for peer disconnect (seconds)
This configuration option sets the timeout for waiting for a peer to disconnect.
- CONFIG_SAMPLE_P2P_ECHO_SERVER_MODE
(bool) Echo Server mode
This configuration option helps to start a TCP echo server in GO mode. Listen on a fixed port and echo back whatever data is received. Only available in (non-persistent) GO mode, which runs the echo server.
- CONFIG_SAMPLE_P2P_ECHO_SERVER_PORT
(int) P2P server port for echo server
This configuration option sets the operating port used when creating the P2P Echo Server. It must match CONFIG_SAMPLE_P2P_ECHO_CLI_PORT on the CLI image, since the two are set independently.
- CONFIG_SAMPLE_P2P_ECHO_POLL_INTERVAL_MS
(int) Echo server poll interval (ms)
This configuration option sets how often, in milliseconds, the echo server re-checks the P2P peer connection state while waiting for a client connection or data. It bounds how long P2P group teardown can be delayed after the peer disconnects.
- CONFIG_SAMPLE_P2P_GO_PERSISTENT_CYCLES
(int) Number of persistence-verification cycles to run in this boot
Number of verification cycles. Cycle 1 creates the persistent group and records its id/SSID; later cycles re-invoke it by id and check the SSID is unchanged. At least 2 are needed (create + verify).
The sample provides configuration files that select a mode together with its common options:
echo-cli.conf- Client mode with the TCP echo client.echo-go.conf- Autonomous Group Owner mode with the TCP echo server.go-persistent.conf- Persistent Group Owner mode with persistence verification.
In CLI mode, you must set the CONFIG_SAMPLE_P2P_PEER_ADDRESS Kconfig option to the Group Owner’s MAC address to connect to it.
Building and running
This sample can be found under samples/wifi/p2p in the nRF Connect SDK folder structure.
For more security, it is recommended to use the */ns variant of the board target (see the Requirements section above.)
When built for this variant, the sample is configured to compile and run as a non-secure application using security by separation.
Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.
To build the sample, follow the instructions in Building an application for your preferred building environment. See also Programming an application for programming steps and Testing and optimization for general information about testing and debugging in the nRF Connect SDK.
Note
When building repository applications in the SDK repositories, building with sysbuild is enabled by default.
If you work with out-of-tree freestanding applications, you need to manually pass the --sysbuild parameter to every build command or configure west to always use it.
To build for the nRF7002 DK, use the nrf7002dk/nrf5340/cpuapp board target.
Select a mode by applying its overlay with the EXTRA_CONF_FILE option.
The following examples build the application for the nRF7002 DK:
# Autonomous Group Owner with the echo server
west build -b nrf7002dk/nrf5340/cpuapp -- -DEXTRA_CONF_FILE=echo-go.conf
# Client with the echo client (set CONFIG_SAMPLE_P2P_PEER_ADDRESS to the GO MAC)
west build -b nrf7002dk/nrf5340/cpuapp -- -DEXTRA_CONF_FILE=echo-cli.conf
# Persistent Group Owner with persistence verification
west build -b nrf7002dk/nrf5340/cpuapp -- -DEXTRA_CONF_FILE=go-persistent.conf
Refer to the sample.yaml file for a complete list of supported boards and their corresponding build command options.
Testing
After programming the sample to your development kit, complete the following steps to test it:
Connect the kit to the computer using a USB cable. The kit is assigned a serial port. Serial ports are referred to as COM ports on Windows, /dev/ttyACM devices on Linux, and /dev/tty devices on macOS. To list Nordic Semiconductor devices connected to your computer together with their serial ports, open a terminal and run the
nrfutil device listcommand. Alternatively, check your operating system’s device manager or its equivalent.Connect to the kit with a terminal emulator (for example, the Serial Terminal app). See Testing and optimization for the required settings and steps.
The sample shows the following output:
CLI mode:
*** Booting nRF Connect SDK v3.2.99-aa13127d2ec2 *** *** Using Zephyr OS v4.3.99-1d6a9d7577c0 *** [00:00:00.328,369] <inf> net_config: Initializing network [00:00:00.328,369] <inf> net_config: Waiting interface 1 (0x20001318) to be up... [00:00:00.328,582] <inf> net_config: IPv4 address: 192.168.1.99 [00:00:00.328,613] <inf> net_config: Running dhcpv4 client... [00:00:00.328,887] <inf> p2p_cli: Starting nrf7002dk with CPU frequency: 64 MHz [00:00:00.329,589] <inf> wifi_supplicant: wpa_supplicant initialized [00:00:01.331,634] <inf> p2p_cli: P2P find started [00:00:05.950,012] <inf> wpa_supp: P2P-DEVICE-FOUND 2a:94:01:b5:19:ee p2p_dev_addr=28:94:01:b5:19:ee pri_dev_type=10-0050F204-5 name='P2P_DEVICE' config_methods=0x88 dev_capab=0x25 gro1 [00:00:05.951,568] <inf> wpa_supp: P2P-DEVICE-FOUND d2:39:fa:43:23:c1 p2p_dev_addr=d2:39:fa:43:23:c1 pri_dev_type=10-0050F204-5 name='Galaxy S22' config_methods=0x188 dev_capab=0x25 gr1 [00:00:05.952,941] <inf> p2p_cli: Peer found, initializing connection [00:00:05.956,359] <inf> wpa_supp: P2P-FIND-STOPPED [00:00:05.958,068] <inf> p2p_cli: PIN: 74834488 [00:00:07.994,812] <err> wpa_supp: Failed to allocated for event: 19 [00:00:15.589,416] <inf> wpa_supp: P2P-GO-NEG-SUCCESS role=client freq=2462 ht40=0 peer_dev=d2:39:fa:43:23:c1 peer_iface=d2:39:fa:43:a3:c1 wps_method=Display [00:00:15.589,996] <inf> wpa_supp: wlan0: WPS-PIN-ACTIVE [00:00:16.631,103] <inf> wpa_supp: wlan0: SME: Trying to authenticate with d2:39:fa:43:a3:c1 (SSID='DIRECT-V2-Galaxy S22' freq=2462 MHz) [00:00:17.001,617] <inf> wpa_supp: wlan0: Trying to associate with d2:39:fa:43:a3:c1 (SSID='DIRECT-V2-Galaxy S22' freq=2462 MHz) [00:00:17.122,283] <wrn> net_if: iface 1 pkt 0x2002bb0c send failure status -1 [00:00:17.126,800] <inf> wpa_supp: wlan0: Associated with d2:39:fa:43:a3:c1 [00:00:17.127,929] <inf> wpa_supp: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0 [00:00:17.136,962] <inf> wpa_supp: wlan0: CTRL-EVENT-EAP-STARTED EAP authentication started [00:00:17.148,529] <inf> wpa_supp: wlan0: CTRL-EVENT-EAP-PROPOSED-METHOD vendor=14122 method=1 [00:00:17.149,169] <inf> wpa_supp: wlan0: CTRL-EVENT-EAP-METHOD EAP vendor 14122 method 1 (WSC) selected [00:00:17.149,597] <inf> wpa_supp: wlan0: CTRL-EVENT-EAP-FAILURE EAP authentication failed [00:00:17.152,038] <inf> wpa_supp: wlan0: Added BSSID d2:39:fa:43:a3:c1 into ignore list, ignoring for 10 seconds [00:00:17.153,289] <inf> wpa_supp: wlan0: CTRL-EVENT-DISCONNECTED bssid=d2:39:fa:43:a3:c1 reason=3 locally_generated=1 [00:00:17.153,472] <inf> p2p_cli: Connected [00:00:17.153,869] <inf> wpa_supp: wlan0: BSSID d2:39:fa:43:a3:c1 ignore list count incremented to 2, ignoring for 10 seconds [00:00:17.221,466] <err> wpa_supp: wpa_drv_zep_event_proc_scan_res: Failed to alloc scan results(525 bytes) [00:00:17.530,761] <inf> wpa_supp: wlan0: Removed BSSID d2:39:fa:43:a3:c1 from ignore list (clear) [00:00:17.532,318] <inf> wpa_supp: wlan0: SME: Trying to authenticate with d2:39:fa:43:a3:c1 (SSID='DIRECT-V2-Galaxy S22' freq=2462 MHz) [00:00:17.546,752] <inf> wpa_supp: wlan0: Trying to associate with d2:39:fa:43:a3:c1 (SSID='DIRECT-V2-Galaxy S22' freq=2462 MHz) [00:00:17.615,051] <inf> wpa_supp: wlan0: Associated with d2:39:fa:43:a3:c1
GO mode:
*** Booting nRF Connect SDK v3.2.99-aa13127d2ec2 *** *** Using Zephyr OS v4.3.99-c6863fca5073 *** [00:00:00.161,072] <inf> net_config: Initializing network [00:00:00.161,079] <inf> net_config: Waiting interface 1 (0x20001590) to be up... [00:00:00.161,204] <inf> net_config: IPv4 address: 192.168.1.99 [00:00:00.161,223] <inf> net_config: Running dhcpv4 client... [00:00:00.161,620] <inf> wifi_supplicant: wpa_supplicant initialized [00:00:00.163,200] <inf> p2p_go: DHCPv4 server started and pool address starts from 192.168.1.2 [00:00:00.163,234] <inf> p2p_go: Starting nrf54lm20dk with CPU frequency: 128 MHz [00:00:03.331,783] <inf> p2p_go: P2P group add initiated (freq=2462 persistent=-1) 25777451 [00:00:04.111,941] <inf> wpa_supp: wlan0: interface state UNINITIALIZED->ENABLED [00:00:04.112,007] <inf> wpa_supp: wlan0: AP-ENABLED [00:00:04.112,550] <inf> wpa_supp: wlan0: CTRL-EVENT-CONNECTED - Connection to f4:ce:36:00:d2:4c completed [id=0 id_str=] [00:00:04.114,047] <inf> wpa_supp: P2P-GROUP-STARTED wlan0 GO ssid="DIRECT-TE" freq=2462 go_dev_addr=f4:ce:36:00:d2:4c [00:00:04.120,574] <inf> p2p_go: WPS PIN is: 25777451 [00:00:04.120,580] <inf> p2p_go: Waiting for Peer to connect.... [00:00:12.080,084] <inf> wpa_supp: P2P-DEVICE-FOUND d2:39:fa:43:23:c1 p2p_dev_addr=d2:39:fa:43:23:c1 pri_dev_type=10-0050F204-5 name='Galaxy S22' config_methods=0x188 dev_capab=0x25 gr1 [00:00:12.080,794] <inf> wpa_supp: P2P-PROV-DISC-SHOW-PIN d2:39:fa:43:23:c1 95612096 p2p_dev_addr=d2:39:fa:43:23:c1 pri_dev_type=10-0050F204-5 name='Galaxy S22' config_methods=0x188 de0 [00:00:20.125,672] <inf> wpa_supp: wlan0: CTRL-EVENT-EAP-STARTED d2:39:fa:43:a3:c1 [00:00:20.125,850] <inf> wpa_supp: wlan0: CTRL-EVENT-EAP-PROPOSED-METHOD vendor=0 method=1 [00:00:20.155,190] <inf> wpa_supp: wlan0: CTRL-EVENT-EAP-PROPOSED-METHOD vendor=14122 method=254 [00:00:21.788,629] <inf> wpa_supp: wlan0: WPS-REG-SUCCESS d2:39:fa:43:a3:c1 9c76ef98-4ad4-5c04-8101-2582fc0a75e2 [00:00:21.793,220] <inf> wpa_supp: wlan0: WPS-SUCCESS [00:00:21.793,721] <inf> wpa_supp: wlan0: CTRL-EVENT-EAP-FAILURE d2:39:fa:43:a3:c1 [00:00:22.381,428] <inf> wpa_supp: wlan0: AP-STA-CONNECTED d2:39:fa:43:a3:c1 p2p_dev_addr=d2:39:fa:43:23:c1 [00:00:22.381,841] <inf> wpa_supp: wlan0: EAPOL-4WAY-HS-COMPLETED d2:39:fa:43:a3:c1 [00:00:22.404,754] <inf> p2p_go: Peer connected [00:00:24.404,812] <inf> p2p_go: Waiting for peer to disconnect... [00:00:29.374,086] <inf> wpa_supp: wlan0: AP-STA-DISCONNECTED d2:39:fa:43:a3:c1 p2p_dev_addr=d2:39:fa:43:23:c1 [00:00:29.374,628] <inf> p2p_go: Peer disconnected, removing group [00:00:29.399,271] <inf> wpa_supp: P2P-GROUP-REMOVED wlan0 GO reason=REQUESTED [00:00:29.400,572] <inf> wpa_supp: wlan0: interface state ENABLED->DISABLED [00:00:29.475,793] <inf> wpa_supp: wlan0: AP-DISABLED [00:00:29.505,446] <inf> wpa_supp: wlan0: CTRL-EVENT-DISCONNECTED bssid=f4:ce:36:00:d2:4c reason=3 locally_generated=1 [00:00:29.505,648] <inf> p2p_go: P2P group remove initiated: wlan0 [00:00:29.505,654] <inf> p2p_go: P2P group removed successfully
Persistent GO mode:
*** Booting nRF Connect SDK v3.2.99-aa13127d2ec2 *** *** Using Zephyr OS v4.3.99-1d6a9d7577c0 *** [00:00:01.359,344] <inf> p2p_go_persistent: Persistence cycle 1/3 (create) [00:00:01.360,000] <inf> p2p_go_persistent: P2P group add initiated (new persistent group) [00:00:03.360,000] <inf> p2p_go_persistent: Stored persistent network: id=1 ssid="DIRECT-Gb" [00:00:03.400,000] <inf> p2p_go_persistent: P2P group remove initiated: wlan0 [00:00:05.400,000] <inf> p2p_go_persistent: Persistence cycle 2/3 (re-invoke by id) [00:00:05.401,000] <inf> p2p_go_persistent: P2P group add initiated (re-invoke persistent id=1) [00:00:07.401,000] <inf> p2p_go_persistent: PERSISTENCE VERIFIED: re-invoked id=1 reused ssid="DIRECT-Gb" [00:00:07.440,000] <inf> p2p_go_persistent: P2P group remove initiated: wlan0 [00:00:09.440,000] <inf> p2p_go_persistent: Persistence cycle 3/3 (re-invoke by id) [00:00:11.440,000] <inf> p2p_go_persistent: PERSISTENCE VERIFIED: re-invoked id=1 reused ssid="DIRECT-Gb" [00:00:11.480,000] <inf> p2p_go_persistent: Persistence verification done: 2 verified, 0 mismatched