2

So I have a fresh pi zero w up and running raspbian stretch lite.

How can I enable Wifi direct/p2p where the Pi is GO and where it has a static ip i can connect to, and install/configure a DHCP server on the pi that when a client device connects to the pi over WiFi direct it get assigned an ip address?

Any idea how to do it? has anybody managed to get a similar scenario working? where to start or what resources I could use, its not my first attempt I have been scratching my head around this a lot for the past couple of weeks, and its driving me crazy, it's definitely not an easy task, I messed a lot with the previous installed OS trying to get it working so I had to re-flash a new image.

/etc/wpa_supplicant.conf:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1
country=LB
ap_scan=1
device_name=raspberry
device_type=1-0050F204-1
driver_param=use_p2p_group_interface=1
p2p_go_intent=15
p2p_go_ht40=1
p2p_listen_reg_class=81
p2p_listen_channel=1
p2p_oper_reg_class=81
p2p_oper_channel=1

Ta!

motash
  • 161
  • 1
  • 2
  • 5
  • As stated in my answer to your similar but less specific question [dhcpcd.conf setting static ip not working!](https://raspberrypi.stackexchange.com/a/94169/79866) I'm just looking at WiFi Direct connections. So could you please paste your `/etc/wpa_supplicant/wpa_supplicant.conf` into your question? – Ingo Feb 13 '19 at 01:01
  • I added the contents of my wpa_supplicant.conf, as I have messed up my previous attempt by running lots of commands, so I wanted to start on a clean installation, also the pi had already other stuff that aren't needed anymore so i thought it's better to start from scratch anyways. – motash Feb 13 '19 at 11:33

1 Answers1

3

UPDATE:
I have made a more detailed answer at Connect Android smartphone with Wi-Fi Direct to a Raspberry Pi. Please look there. This answer here isn't updated anymore.


I have tested this with a smart phone using android 6.0 and with Raspberry Pi OS (32-bit) Lite 2020-08-20 based on Debian Buster, updated with sudo bash -c 'apt update && apt full-upgrade && reboot'. WiFi Direct uses Wi-Fi Protected Setup (WPS) for authentication that knows mainly two modes: Push Button Control (PBC) and Pin Code. For this example I will only use PBC because it is a bit simpler without fiddling with a pin code.

We want to have a DHCP server running. WiFi Direct is organized in groups and every group has one group owner (GO). Only the group owner is allowed to run a DHCP server because we have to ensure that only one DHCP server is present in the group. The group owner can change and it seems that there are protocol datagrams to negotiate DHCP. I have seen a hint about this at Wi-Fi Direct vs Ad-hoc mode, section 6) Frame exchange sequence:

After that we can exchange data frames in the group, but usually the DHCP protocol will be used first to provide the client with an IP address.

But I haven't found further information how it works so for this setup I will set the RasPi to the group owner. To ensure that a device is always negotiated to a group owner we use the option p2p_go_intent=[0..15]. 0 means the RasPi becomes a client, 15 means the RasPi becomes a group owner. 7 means a chance of 50 % to become a group owner.

I prefer to use systemd-networkd because it has all in one and is able to manage dynamically changing interfaces. WiFi Direct groups are represented by virtual wifi interfaces with increasing numbers, e.g. p2p-wlan0-0, p2p-wlan0-1 and so on.

Just follow to Use systemd-networkd for general networking. You can use section "♦ Quick Step". Then come back here.

To configure wpa_supplicant create this file with your settings for country= and device_name=. By specification, the device name should always start with DIRECT-. You can just copy and paste this in one block to your command line beginning with cat and including EOF (delimiter EOF will not get part of the file):

rpi ~# cat > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=DE
device_name=DIRECT-RasPi1
# In order to support 802.11n for the p2p Group Owner
p2p_go_ht40=1
EOF

rpi ~# chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
rpi ~# systemctl disable wpa_supplicant.service
rpi ~# systemctl enable wpa_supplicant@wlan0.service
rpi ~# rfkill unblock wlan

To give the group interface a static ip address and enable a DHCP server on it, create this file:

rpi ~# cat > /etc/systemd/network/12-p2p-wlan0.network <<EOF
[Match]
Name=p2p-wlan0-*
[Network]
Address=192.168.4.1/24
DHCPServer=yes
EOF

Reboot.


To manage devices with wpa_cli we have to specify the control interface wpa_cli -ip2p-dev-wlan0. In wpa_supplicant.conf we have specified where to find the names of the control interfaces:

rpi ~$ ls -1 /var/run/wpa_supplicant/
p2p-dev-wlan0
wlan0

Then on the RasPi I start finding devices:

rpi ~$ wpa_cli -ip2p-dev-wlan0 p2p_find

Now I go to the WiFi Direct page on my smartphone
System settings -> Networks -> WiFi -> tick bar in upper right corner -> Advanced WiFi -> Wi-Fi Direct

Wi-Fi Direct page

Now I could look with wpa_cli -ip2p-dev-wlan0 p2p_peers what mac addresses of found devices are available. Then I have to show the details for every mac address with wpa_cli -ip2p-dev-wlan0 p2p_peer <MAC-ADDR> what name it has to find my smartphone. I do it with this one liner, for example:

rpi ~$ for i in $( wpa_cli -ip2p-dev-wlan0 p2p_peers ); do echo -n "$i "; wpa_cli -ip2p-dev-wlan0 p2p_peer $i | grep device_name=; done
a2:91:69:b2:91:a9 device_name=LG Leon 4G LTE
4e:ef:c0:a7:05:82 device_name=Fire TV stick
9a:0c:82:ba:7a:aa device_name=Android_58eb
32:cd:a7:f2:ee:5c device_name=DIRECT-1KC48x Series

and find a2:91:69:b2:91:a9 for my LG Leon 4G LTE. Now I only work with this mac address (p2p_dev_addr) and connect to it:

rpi ~$ wpa_cli -ip2p-dev-wlan0 p2p_connect a2:91:69:b2:91:a9 pbc go_intent=15

Note that we use pbc (push button control) and go_intent=15 to make the RasPi the group owner (see above).

On the smartphone tick SEARCH

In the popped up window I just confirm the connection by ticking ACCEPT (push button).

Wi-Fi Direct invitation

Under Advanced I find the ip address of the device, e.g. 192.168.4.102:

Wi-Fi Direct Advanced

and just ping it from the RasPi to confirm that the smartphone is connected.

rpi ~$ ping -c3 192.168.4.102
PING 192.168.4.102 (192.168.4.102) 56(84) bytes of data.
64 bytes from 192.168.4.102: icmp_seq=1 ttl=64 time=6.90 ms
64 bytes from 192.168.4.102: icmp_seq=2 ttl=64 time=3.46 ms
64 bytes from 192.168.4.102: icmp_seq=3 ttl=64 time=3.14 ms

--- 192.168.4.102 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 3.149/4.505/6.904/1.701 ms
rpi ~$

To finish the connection we need the group identifier. You can find it with:

rpi ~$ ip -br link | grep -Po 'p2p-wlan0-\d+'
p2p-wlan0-12

And with this we finish the connection with p2p_group_remove:

rpi ~$ wpa_cli -ip2p-dev-wlan0 p2p_group_remove p2p-wlan0-12

Or with a one-liner:

rpi ~$ wpa_cli -ip2p-dev-wlan0 P2p_group_remove $(ip -br link | grep -Po 'p2p-wlan0-\d+')

That's it.


references:
(1) OMAP Wireless Connectivity NLCP WiFi Direct Configuration Scripts
(2) White Paper - Wi-Fi Direct
(3) Draft WiFi P2P Technical Specification.pdf
(4) wpa_supplicant and Wi-Fi P2P
(5) wpa_supplicant and Wi-Fi Protected Setup (WPS)

Ingo
  • 40,606
  • 15
  • 76
  • 189
  • Great! this is really helpful, thanks for sharing this, I haven't tested it yet, but I can't seem to find where you have enabled the DHCP server on the Group owner device? are you assuming that it's already enabled and configured or does system-networkd include it by default? if not can you please suggest what you used and preferably share how to do it in your answer to make it even better for anyone who wants to work with wifi direct from scratch? I will try too to update when I get my parts working hopefully soon! – motash Feb 17 '19 at 16:14
  • @Mohyddin I'm still working on this, also just at the moment. Please notice the last sentence: *Work in progress, will be continued soon with setting up a second device to connect to.* ;-) I'm just sitting on [OMAP Wireless Connectivity NLCP WiFi Direct Configuration Scripts](http://processors.wiki.ti.com/index.php/OMAP_Wireless_Connectivity_NLCP_WiFi_Direct_Configuration_Scripts) to translate it to two RasPis. Then I will update soon. I think setting up a DHCP server will be the last step when we have stable connections of the two devices. – Ingo Feb 17 '19 at 16:27
  • looking forward to that! Also I would like to point out that you should probably add "persistent" keyword after creating the group like so "p2p_group_add persistent" so that the group doesn't get destroyed after some time, hope that helps! I am asking about DHCP server is that I'm kinda stuck at the point where I need to have my DHCP sever running on the GO device (pi) so that the client device (android phone in my case) broadcasts "WIFI_P2P_CONNECTION_CHANGED_ACTION" and I'm not sure how to do that exactly without messing up the networking on the pi like I did before. – motash Feb 17 '19 at 17:03
  • @Mohyddin I have rewritten the answer to show how it works with a DHCP server. – Ingo Feb 23 '19 at 00:49
  • @Ingo I tried your steps, and it is showing raspberry pi device in the list but when i accepted the invitation to connect in my android device, it is not connecting and in the wpa_cli console I am getting this,`P2P-GO-NEG-SUCCESS role=GO freq=2412 ht40=1 peer_dev=2a:3f:69:1d:ed:c5 peer_iface=2a:3f:69:1d:ed:c5 wps_method=PBC <3>P2P-GROUP-FORMATION-FAILURE` Do you know what could be the reason? – Sony Apr 19 '19 at 14:38
  • @Sony Seems to be a problem with group owner (`P2P-GO-NEG-SUCCESS`). Maybe it's an issue with your android device? Try the invitation from the RasPi (`wpa_cli -ip2p-dev-wlan0 p2p_connect a2:91:69:b2:91:a9 pbc go_intent=15`) with other `go_intent=` as described in paragraph 4 in the answer. Try first 7. – Ingo Apr 20 '19 at 09:12
  • @Ingo i have tried with go_intent=1, 7 and 15, but the result is same, now I am flashing the sd card again and starting from scratch. – Sony Apr 20 '19 at 09:18
  • @Ingo after starting from scratch and ran every command and rebooting, I am not able to access Pi device over ssh – Sony Apr 20 '19 at 09:28
  • @Sony Do you can ping it? – Ingo Apr 20 '19 at 09:45
  • @Ingo, no. It is not showing in the active clients list of my router – Sony Apr 20 '19 at 09:51
  • @Sony I mean to ping as described in the answer. Your router has nothing to do with WiFi Direct. It is a **managed** access point and I don't believe that it will show WiFi Direct clients because it scans for managed stations. – Ingo Apr 20 '19 at 09:57
  • @Ingo, when i tried to ping to the PI device, it is showing `Destination Host Unreachable` – Sony Apr 20 '19 at 09:58
  • @Sony Please read the end of the answer. There is described how to find the ip address of the android device and how to ping it from the RasPi. Does it work this way? What ip address does your android device have? Where does this checking fails? – Ingo Apr 20 '19 at 10:04
  • @Ingo have completed up to setting wpa_supplicant and dhcp server and after rebooting I am not able to connect or ping the Pi device to run this `wpa_cli -ip2p-dev-wlan0 p2p_find command to start discovery ` – Sony Apr 20 '19 at 10:12
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/92669/discussion-between-ingo-and-sony). – Ingo Apr 20 '19 at 10:15
  • Finally, I managed to connect mixing the configuration from the `switch over to systemd-networkd` section – Sony Apr 20 '19 at 11:10
  • Will update or post another answer with the changes i made – Sony Apr 22 '19 at 09:11