如何使用wpa_supplicant及wpa_cli 创建p2p

最近在搞linux 系统的p2p,特此简单总结下。

1. p2p_connect 方式协商P2P

使用p2p_connect的方式,其完整命令如下:

wpa_supplicant -B -i wlan0 -c /data/cfg/wpa_supplicant.conf
wpa_cli
p2p_find
p2p_connect  52:7c:85:b2:de:34   pbc go_intent=1    (当go_intent=15时,强制当前设备为GO角色)
p2p_group_remove p2p-wlan0-0

完整示例如下:

[root:/]# wpa_supplicant -B -i wlan0 -c /data/cfg/wpa_supplicant.conf
Successfully initialized wpa_supplicant
[root:/]# wpa_cli
Selected interface 'p2p-dev-wlan0'
// 1. 使能p2p 且允许被其他设备搜索到
> p2p_find
OK
<3>P2P-DEVICE-FOUND 7e:76:68:0e:d7:d9 p2p_dev_addr=7e:76:68:0e:57:d9 pri_dev_type=10-0050F204-5 name='HUAWEI Mate 20 Pro (UD)' config_methods=0x188 dev_capab=0x25 group_capab=0x2b vendor_elems=1 new=1

// 2. 当其他设备搜索到当前设备时,会发起连接请求
<3>P2P-GO-NEG-REQUEST 52:7c:85:b2:de:34 dev_passwd_id=4 go_intent=14

// 3. 收到连接请求后通过 p2p_connect 连接其mac, 角包为两者协商
> p2p_connect  52:7c:85:b2:de:34   pbc go_intent=1
OK
<3>P2P-FIND-STOPPED

// 4. 协商结果,当前设备为 client角色,其他设备为 GO角色
<3>P2P-GO-NEG-SUCCESS role=client freq=5180 ht40=1 peer_dev=52:7c:85:b2:de:34 peer_iface=72:ec:4b:ac:5a:53 wps_method=PBC
<3>P2P-GROUP-FORMATION-SUCCESS
<3>P2P-GROUP-STARTED p2p-wlan0-0 client ssid="DIRECT-UC-HUAWEI Mate 30 Pro" freq=5180 psk=96ffd94980aa6e6d103d01af20a293d5d566bd49e68b29f110e10df537ad7306 go_dev_addr=52:7c:85:b2:de:34 [PERSISTENT]

// 5. 断开连接
> p2p_group_remove p2p-wlan0-0
OK
<3>P2P-GROUP-REMOVED p2p-wlan0-0 client reason=REQUESTED

注意,当 go_intent=15 时,说明强制当前设备为Go角色。

> p2p_find
OK
<3>P2P-DEVICE-FOUND 52:7c:85:b2:de:34 p2p_dev_addr=52:7c:85:b2:de:34 pri_dev_type=10-0050F204-5 name='HUAWEI Mate 30 Pro' config_methods=0x188 dev_capab=0x25 group_capab=0x0 vendor_elems=1 new=0

<3>P2P-INVITATION-RECEIVED sa=52:7c:85:b2:de:34 go_dev_addr=82:c5:f2:a9:32:1d unknown-network
<3>P2P-GO-NEG-REQUEST 52:7c:85:b2:de:34 dev_passwd_id=4 go_intent=14

> p2p_connect  52:7c:85:b2:de:34   pbc go_intent=15
OK
<3>P2P-FIND-STOPPED
<3>P2P-GO-NEG-SUCCESS role=GO freq=5200 ht40=1 peer_dev=52:7c:85:b2:de:34 peer_iface=82:ba:1c:59:35:ba wps_method=PBC
<3>P2P-GROUP-FORMATION-SUCCESS
<3>P2P-GROUP-STARTED p2p-wlan0-2 GO ssid="DIRECT-VZ" freq=5200 passphrase="LRVoCUqG" go_dev_addr=82:c5:f2:a9:32:1d
<3>AP-STA-CONNECTED 82:ba:1c:59:35:ba p2p_dev_addr=52:7c:85:b2:de:34

1.1 PIN码方式加入

Steps设备1 (Client)设备2(Go)
1wpa_cliwpa_cli
2p2p_find
3p2p_find
4p2p_connect 82:ba:1c:59:35:ba pin go_intent=1
示例:10632994
5p2p_connect 52:7c:85:b2: de:34 10632994 go_intent=15
6ifconfig p2p-wlan0-0 169.254.108.49 netmask 255.255.0.0ifconfig p2p-wlan0-0 169.254.108.1 netmask 255.255.0.0
7ping 169.254.108.1ping 169.254.108.49

1.2 PBC方式加入

Steps设备1 (Client)设备2(Go)
1wpa_cliwpa_cli
2p2p_find
3p2p_find
4p2p_connect 82:ba:1c:59:35:ba pbc go_intent=1
5p2p_connect 52:7c:85:b2: de:34 pbc go_intent=14
6ifconfig p2p-wlan0-0 169.254.108.49 netmask 255.255.0.0ifconfig p2p-wlan0-0 169.254.108.1 netmask 255.255.0.0
7ping 169.254.108.1ping 169.254.108.49

2. p2p_group_add 方式提前创建Group 组

2.1 PBC方式加入

Steps设备1 (GO)设备2(Client)
1wpa_cliwpa_cli
2p2p_group_add
3Ctrl-c to exit
4ifconfig p2p-wlan0-0 169.254.108.1 netmask 255.255.0.0
5dnsmasq -C /data/bin/dnsmasq.conf --interface=p2p-wlan0-0 --no-dhcp-interface=usb0
6p2p_find
7p2p_connect 82:ba:1c:59:35:ba pbc join
8wpa_cli -i p2p-wlan0-0 wps_pbcCtrl-c to exit
9ping 169.254.108.49ping 169.254.108.1

2.2 PIN码方式加入(from GO)

Steps设备1 (GO)设备2(Client)
1wpa_cliwpa_cli
2p2p_group_add
3Ctrl-c to exit
4wpa_cli -i p2p-wlan0-0 wps_pin any
(shows <pin>,如: 73077947)
5ifconfig p2p-wlan0-0 169.254.108.1 netmask 255.255.0.0
6dnsmasq -C /data/bin/dnsmasq.conf --interface=p2p-wlan0-0 --no-dhcp-interface=usb0
7p2p_find
8p2p_connect 82:ba:1c:59:35:ba 73077947 join
9wpa_cli -i p2p-wlan0-0 wps_pbcCtrl-c to exit
10ping 169.254.108.49ping 169.254.108.1

2.3 PIN码方式加入(from Client)

Steps设备1 (GO)设备2(Client)
1wpa_cliwpa_cli
2p2p_group_add
3Ctrl-c to exit
4ifconfig p2p-wlan0-0 169.254.108.1 netmask 255.255.0.0
5dnsmasq -C /data/bin/dnsmasq.conf --interface=p2p-wlan0-0 --no-dhcp-interface=usb0
6p2p_find
7p2p_connect 82:ba:1c:59:35:ba pin join
示例:10632994
8wpa_cli -i p2p-wlan0-0 wps_pin any 10632994
9ping 169.254.108.49ping 169.254.108.1

2.4 Invite方式加入

Steps设备1 (GO)设备2(Client)
1wpa_cliwpa_cli
2p2p_group_add
3Ctrl-c to exit
4ifconfig p2p-wlan0-0 169.254.108.1 netmask 255.255.0.0
5dnsmasq -C /data/bin/dnsmasq.conf --interface=p2p-wlan0-0 --no-dhcp-interface=usb0
6p2p_find
7p2p_invite group=p2p-p2p0-0 peer=82:ba:1c:59:35:ba
8p2p_connect 52:7c:85:b2: de:34 pbc join
9wpa_cli -i p2p-wlan0-0 wps_pbcCtrl-c to exit
9ping 169.254.108.49ping 169.254.108.1

3. dnsmasq.conf 及 wpa_supplicant.conf 内容

3.1 dnsmasq.conf

user=root
interface=wlan0 p2p-dev-wlan0
#listen-address=127.0.0.1
dhcp-range=169.254.108.2,169.254.108.51
#server=8.8.8.8

3.2 wpa_supplicant.conf

ctrl_interface=/var/run/wpa_supplicant
#update_config=1
device_name=p2p-5G-test
device_type=10-0050F204-5
config_methods=display push_button keypad virtual_push_button physical_display
p2p_add_cli_chan=1
p2p_go_ht40=1
pmf=1

network={
	ssid="SSID"
	psk="PASSWORD"
	key_mgmt=WPA-PSK
	priority=1
}

network={
	ssid="DIRECT-p2p-5G-test"
	bssid=82:c5:f2:a9:32:1d
	psk="123456789"
	proto=RSN
	key_mgmt=WPA-PSK
	pairwise=CCMP
	auth_alg=OPEN
	mode=3
	disabled=2
}

ap_scan=1
network={
	ssid="TP-LINK_aaaa"
	psk="135246789"
	key_mgmt=WPA-PSK
	priority=1
}




详细可参考:《Wi-Fi direct


版权声明

本文转自: https://blog.csdn.net/Ciellee/article/details/117529523

Logo

更多推荐