• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant - P2P Iface Aidl interface
3  * Copyright (c) 2021, Google Inc. All rights reserved.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "aidl_manager.h"
10 #include "aidl_return_util.h"
11 #include "iface_config_utils.h"
12 #include "misc_utils.h"
13 #include "p2p_iface.h"
14 #include "sta_network.h"
15 
16 extern "C"
17 {
18 #include "ap.h"
19 #include "wps_supplicant.h"
20 #include "wifi_display.h"
21 #include "utils/eloop.h"
22 #include "wpa_supplicant_i.h"
23 #include "driver_i.h"
24 }
25 
26 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 3
27 // Wait time before triggering the single channel scan to discover Auto GO.
28 // Use a shorter wait time when the given frequency is GO operating frequency.
29 // The idea is to quickly finish scans and return the status to application.
30 #define P2P_JOIN_SINGLE_CHANNEL_SCAN_INTERVAL_USECS 200000
31 // Wait time before triggering the multiple channel scan to discover Auto GO.
32 #define P2P_JOIN_MULTIPLE_CHANNEL_SCAN_INTERVAL_USECS 1000000
33 
34 namespace {
35 const char kConfigMethodStrPbc[] = "pbc";
36 const char kConfigMethodStrDisplay[] = "display";
37 const char kConfigMethodStrKeypad[] = "keypad";
38 constexpr char kSetMiracastMode[] = "MIRACAST ";
39 constexpr uint8_t kWfdDeviceInfoSubelemId = 0;
40 constexpr uint8_t kWfdR2DeviceInfoSubelemId = 11;
41 constexpr char kWfdDeviceInfoSubelemLenHexStr[] = "0006";
42 
43 std::function<void()> pending_join_scan_callback = NULL;
44 std::function<void()> pending_scan_res_join_callback = NULL;
45 
46 using aidl::android::hardware::wifi::supplicant::ISupplicantP2pIface;
47 using aidl::android::hardware::wifi::supplicant::ISupplicantStaNetwork;
48 using aidl::android::hardware::wifi::supplicant::MiracastMode;
49 using aidl::android::hardware::wifi::supplicant::P2pFrameTypeMask;
50 
convertAidlMiracastModeToInternal(MiracastMode mode)51 uint8_t convertAidlMiracastModeToInternal(
52 	MiracastMode mode)
53 {
54 	switch (mode) {
55 	case MiracastMode::DISABLED:
56 		return 0;
57 	case MiracastMode::SOURCE:
58 		return 1;
59 	case MiracastMode::SINK:
60 		return 2;
61 	};
62 	WPA_ASSERT(false);
63 }
64 
65 /**
66  * Check if the provided ssid is valid or not.
67  *
68  * Returns 1 if valid, 0 otherwise.
69  */
isSsidValid(const std::vector<uint8_t> & ssid)70 int isSsidValid(const std::vector<uint8_t>& ssid)
71 {
72 	if (ssid.size() == 0 ||
73 		ssid.size() >
74 		static_cast<uint32_t>(ISupplicantStaNetwork::
75 					  SSID_MAX_LEN_IN_BYTES)) {
76 		return 0;
77 	}
78 	return 1;
79 }
80 
81 /**
82  * Check if the provided psk passhrase is valid or not.
83  *
84  * Returns 1 if valid, 0 otherwise.
85  */
isPskPassphraseValid(const std::string & psk)86 int isPskPassphraseValid(const std::string &psk)
87 {
88 	if (psk.size() <
89 		static_cast<uint32_t>(ISupplicantStaNetwork::
90 					  PSK_PASSPHRASE_MIN_LEN_IN_BYTES) ||
91 		psk.size() >
92 		static_cast<uint32_t>(ISupplicantStaNetwork::
93 					  PSK_PASSPHRASE_MAX_LEN_IN_BYTES)) {
94 		return 0;
95 	}
96 	if (has_ctrl_char((u8 *)psk.c_str(), psk.size())) {
97 		return 0;
98 	}
99 	return 1;
100 }
101 
setBandScanFreqsList(struct wpa_supplicant * wpa_s,enum hostapd_hw_mode hw_mode,bool exclude_dfs,struct wpa_driver_scan_params * params)102 static int setBandScanFreqsList(
103 	struct wpa_supplicant *wpa_s,
104 	enum hostapd_hw_mode hw_mode,
105 	bool exclude_dfs,
106 	struct wpa_driver_scan_params *params)
107 {
108 	struct hostapd_hw_modes *mode;
109 	int count, i;
110 
111 	mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, hw_mode, 0);
112 	if (mode == NULL || !mode->num_channels) {
113 		wpa_printf(MSG_ERROR,
114 			"P2P: No channels supported in this hw_mode: %d", hw_mode);
115 		return -1;
116 	}
117 
118 	/*
119 	 * Allocate memory for frequency array, allocate one extra
120 	 * slot for the zero-terminator.
121 	 */
122 	params->freqs = (int *) os_calloc(mode->num_channels + 1, sizeof(int));
123 	if (params->freqs == NULL) {
124 		return -ENOMEM;
125 	}
126 	for (count = 0, i = 0; i < mode->num_channels; i++) {
127 		if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED) {
128 			continue;
129 		}
130 		if (exclude_dfs && (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)) {
131 			continue;
132 		}
133 		params->freqs[count++] = mode->channels[i].freq;
134 	}
135 	if (!count && params->freqs) {
136 		wpa_printf(MSG_ERROR,
137 			"P2P: All channels(exclude_dfs: %d) are disabled in this hw_mode: %d",
138 			exclude_dfs, hw_mode);
139 		os_free(params->freqs);
140 		return -1;
141 	}
142 	return 0;
143 }
144 
setScanFreq(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,int freq,int operating_freq)145 static int setScanFreq(struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params,
146 	int freq, int operating_freq)
147 {
148 	int frequency = operating_freq ? operating_freq : freq;
149 	if (disabled_freq(wpa_s, frequency)) {
150 		wpa_printf(MSG_ERROR,
151 				"P2P: freq %d is not supported for a client.", frequency);
152 		return -1;
153 	}
154 	/*
155 	 * Allocate memory for frequency array, with one extra
156 	 * slot for the zero-terminator.
157 	 */
158 	params->freqs = new int[2] {frequency, 0};
159 	return 0;
160 }
161 
162 /**
163  * setP2pCliOptimizedScanFreqsList - Fill the frequencies to scan in Scan
164  * parameters.
165  * @wpa_s: Pointer to wpa_supplicant data
166  * @params: Pointer to Scan parameters.
167  * @freq: Frequency/Band requested to scan by the application, possible values are,
168  *		0 - All the frequencies - full scan
169  *		2 - Frequencies in 2.4GHz
170  *		5 - Frequencies in 5GHz
171  *		- Valid frequency
172  * @operating_freq: Frequency of BSS if found in scan cache
173  * Returns: Pointer to the BSS entry or %NULL if not found
174  */
setP2pCliOptimizedScanFreqsList(struct wpa_supplicant * wpa_s,struct wpa_driver_scan_params * params,int freq,int operating_freq)175 static int setP2pCliOptimizedScanFreqsList(struct wpa_supplicant *wpa_s,
176 	struct wpa_driver_scan_params *params, int freq, int operating_freq)
177 {
178 	int ret;
179 	/* If BSS is found in scan cache, first scan its operating frequency */
180 	if (!wpa_s->p2p_join_scan_count && operating_freq) {
181 		ret = setScanFreq(wpa_s, params, freq, operating_freq);
182 		if (!ret) {
183 			return ret;
184 		}
185 	}
186 
187 	/* Empty freq params means scan all the frequencies */
188 	if (freq == 0) {
189 		return 0;
190 	}
191 	else if (freq == 2 || freq == 5) {
192 		/* Scan the frequencies in the band */
193 		enum hostapd_hw_mode mode;
194 		int ret;
195 		if (wpa_s->hw.modes == NULL) {
196 			wpa_printf(MSG_DEBUG,
197 				   "P2P: Unknown what %dG channels the driver supports.", freq);
198 			return 0;
199 		}
200 		mode = freq == 5 ? HOSTAPD_MODE_IEEE80211A : HOSTAPD_MODE_IEEE80211G;
201 		if (wpa_s->p2p_join_scan_count < 2) {
202 			// scan all non DFS channels in the first two attempts
203 			ret = setBandScanFreqsList(wpa_s, mode, true, params);
204 			if (ret < 0 && (-ENOMEM != ret)) {
205 				// try to scan all channels before returning error
206 				ret = setBandScanFreqsList(wpa_s, mode, false, params);
207 			}
208 		} else {
209 			// scan all channels
210 			ret = setBandScanFreqsList(wpa_s, mode, false, params);
211 		}
212 		return ret;
213 	} else {
214 		/* Scan the frequency requested by the application */
215 		ret = setScanFreq(wpa_s, params, freq, 0);
216 		return ret;
217 	}
218 	return 0;
219 }
220 
221 /**
222  * getP2pJoinScanInterval - Get the delay in triggering the scan to discover
223  * Auto GO.
224  */
getP2pJoinScanIntervalUsecs(int freq)225 static int getP2pJoinScanIntervalUsecs(int freq)
226 {
227 	if (freq == 5 || freq == 2 || freq == 0) {
228 		return P2P_JOIN_MULTIPLE_CHANNEL_SCAN_INTERVAL_USECS;
229 	} else {
230 		return P2P_JOIN_SINGLE_CHANNEL_SCAN_INTERVAL_USECS;
231 	}
232 }
233 
234 /*
235  * isAnyEtherAddr - match any ether address
236  *
237  */
isAnyEtherAddr(const u8 * a)238 int isAnyEtherAddr(const u8 *a)
239 {
240 	// 02:00:00:00:00:00
241 	return (a[0] == 2) && !(a[1] | a[2] | a[3] | a[4] | a[5]);
242 }
243 
244 /**
245  * findBssBySsid - Fetch a BSS table entry based on SSID and optional BSSID.
246  * @wpa_s: Pointer to wpa_supplicant data
247  * @bssid: BSSID, 02:00:00:00:00:00 matches any bssid
248  * @ssid: SSID
249  * @ssid_len: Length of @ssid
250  * Returns: Pointer to the BSS entry or %NULL if not found
251  */
findBssBySsid(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * ssid,size_t ssid_len)252 struct wpa_bss* findBssBySsid(
253 	struct wpa_supplicant *wpa_s, const u8 *bssid,
254 	const u8 *ssid, size_t ssid_len)
255 {
256 	struct wpa_bss *bss;
257 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
258 		if ((isAnyEtherAddr(bssid) ||
259 			os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) &&
260 			bss->ssid_len == ssid_len &&
261 			os_memcmp(bss->ssid, ssid, ssid_len) == 0)
262 			return bss;
263 	}
264 	return NULL;
265 }
266 
267 /**
268  * findBssBySsidFromAnyInterface - Fetch a BSS table entry based on SSID and optional BSSID
269  * by iterating through all the interfaces.
270  * @head: Head of Pointer to wpa_supplicant data
271  * @bssid: BSSID, 02:00:00:00:00:00 matches any bssid
272  * @ssid: SSID
273  * @ssid_len: Length of @ssid
274  * Returns: Pointer to the BSS entry or %NULL if not found
275  */
findBssBySsidFromAnyInterface(struct wpa_supplicant * head,const u8 * bssid,const u8 * ssid,size_t ssid_len)276 struct wpa_bss* findBssBySsidFromAnyInterface(
277 	struct wpa_supplicant *head, const u8 *bssid,
278 	const u8 *ssid, size_t ssid_len)
279 {
280 	struct wpa_supplicant *wpa_s;
281 	struct wpa_bss *bss = NULL;
282 	for (wpa_s = head; wpa_s; wpa_s = wpa_s->next) {
283 		bss = findBssBySsid(wpa_s, bssid, ssid, ssid_len);
284 		if (bss != NULL) {
285 			return bss;
286 		}
287 	}
288 	return bss;
289 }
290 
addGroupClientNetwork(struct wpa_supplicant * wpa_s,uint8_t * group_owner_bssid,const std::vector<uint8_t> & ssid,const std::string & passphrase)291 struct wpa_ssid* addGroupClientNetwork(
292 	struct wpa_supplicant* wpa_s,
293 	uint8_t *group_owner_bssid,
294 	const std::vector<uint8_t>& ssid,
295 	const std::string& passphrase)
296 {
297 	struct wpa_ssid* wpa_network = wpa_config_add_network(wpa_s->conf);
298 	if (!wpa_network) {
299 		return NULL;
300 	}
301 	// set general network defaults
302 	wpa_config_set_network_defaults(wpa_network);
303 
304 	// set P2p network defaults
305 	wpa_network->p2p_group = 1;
306 	wpa_network->mode = wpas_mode::WPAS_MODE_INFRA;
307 
308 	wpa_network->auth_alg = WPA_AUTH_ALG_OPEN;
309 	wpa_network->key_mgmt = WPA_KEY_MGMT_PSK;
310 	wpa_network->proto = WPA_PROTO_RSN;
311 	wpa_network->pairwise_cipher = WPA_CIPHER_CCMP;
312 	wpa_network->group_cipher = WPA_CIPHER_CCMP;
313 	wpa_network->disabled = 2;
314 
315 	// set necessary fields
316 	os_memcpy(wpa_network->bssid, group_owner_bssid, ETH_ALEN);
317 	wpa_network->bssid_set = 1;
318 
319 	wpa_network->ssid = (uint8_t *)os_malloc(ssid.size());
320 	if (wpa_network->ssid == NULL) {
321 		wpa_config_remove_network(wpa_s->conf, wpa_network->id);
322 		return  NULL;
323 	}
324 	memcpy(wpa_network->ssid, ssid.data(), ssid.size());
325 	wpa_network->ssid_len = ssid.size();
326 
327 	wpa_network->psk_set = 0;
328 	wpa_network->passphrase = dup_binstr(passphrase.c_str(), passphrase.length());
329 	if (wpa_network->passphrase == NULL) {
330 		wpa_config_remove_network(wpa_s->conf, wpa_network->id);
331 		return  NULL;
332 	}
333 	wpa_config_update_psk(wpa_network);
334 
335 	return wpa_network;
336 
337 }
338 
joinScanWrapper(void * eloop_ctx,void * timeout_ctx)339 void joinScanWrapper(void *eloop_ctx, void *timeout_ctx)
340 {
341 	struct wpa_supplicant *wpa_s = (struct wpa_supplicant *) eloop_ctx;
342 
343 	if (pending_join_scan_callback != NULL) {
344 		pending_join_scan_callback();
345 	}
346 }
347 
scanResJoinWrapper(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)348 void scanResJoinWrapper(
349 	struct wpa_supplicant *wpa_s,
350 	struct wpa_scan_results *scan_res)
351 {
352 	if (wpa_s->p2p_scan_work) {
353 		struct wpa_radio_work *work = wpa_s->p2p_scan_work;
354 		wpa_s->p2p_scan_work = NULL;
355 		radio_work_done(work);
356 	}
357 
358 	if (pending_scan_res_join_callback) {
359 		pending_scan_res_join_callback();
360 	}
361 }
362 
joinScanReq(struct wpa_supplicant * wpa_s,const std::vector<uint8_t> & ssid,int freq,int operating_freq)363 int joinScanReq(
364 	struct wpa_supplicant* wpa_s,
365 	const std::vector<uint8_t>& ssid,
366 	int freq, int operating_freq)
367 {
368 	int ret;
369 	struct wpa_driver_scan_params params;
370 	struct wpabuf *ies;
371 	size_t ielen;
372 	unsigned int bands;
373 
374 	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
375 		wpa_printf(MSG_ERROR,
376 			"P2P: P2P interface is gone, cancel join scan");
377 		return -ENXIO;
378 	}
379 
380 	os_memset(&params, 0, sizeof(params));
381 	if (ssid.size() > 0) {
382 		params.ssids[0].ssid = ssid.data();
383 		params.ssids[0].ssid_len = ssid.size();
384 	} else {
385 		params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
386 		params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
387 	}
388 	wpa_printf(MSG_DEBUG, "Scan SSID %s for join with frequency %d"
389 		"BSS operating_freq from scan cache %d",
390 		wpa_ssid_txt(params.ssids[0].ssid, params.ssids[0].ssid_len), freq, operating_freq);
391 
392 	/* Construct an optimized p2p scan channel list */
393 	ret = setP2pCliOptimizedScanFreqsList(wpa_s, &params, freq, operating_freq);
394 	if (ret < 0) {
395 		wpa_printf(MSG_ERROR,
396 				   "Failed to set frequency in p2p scan params, error = %d", ret);
397 		return -1;
398 	}
399 
400 	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
401 	ies = wpabuf_alloc(ielen);
402 	if (ies == NULL) {
403 		if (params.freqs) {
404 			os_free(params.freqs);
405 		}
406 		return -1;
407 	}
408 
409 	bands = wpas_get_bands(wpa_s, params.freqs);
410 	p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
411 
412 	params.p2p_probe = 1;
413 	params.extra_ies = (u8 *) wpabuf_head(ies);
414 	params.extra_ies_len = wpabuf_len(ies);
415 	if (wpa_s->clear_driver_scan_cache) {
416 		wpa_printf(MSG_DEBUG,
417 			"Request driver to clear scan cache due to local BSS flush");
418 		params.only_new_results = 1;
419 	}
420 
421 	ret = wpa_drv_scan(wpa_s, &params);
422 	if (!ret) {
423 		os_get_reltime(&wpa_s->scan_trigger_time);
424 		if (wpa_s->scan_res_handler) {
425 			wpa_printf(MSG_DEBUG, "Replace current running scan result handler");
426 		}
427 		wpa_s->p2p_join_scan_count++;
428 		wpa_s->scan_res_handler = scanResJoinWrapper;
429 		wpa_s->own_scan_requested = 1;
430 		wpa_s->clear_driver_scan_cache = 0;
431 	}
432 
433 	if (params.freqs) {
434 		os_free(params.freqs);
435 	}
436 
437 	wpabuf_free(ies);
438 
439 	return ret;
440 }
441 
is6GhzAllowed(struct wpa_supplicant * wpa_s)442 static bool is6GhzAllowed(struct wpa_supplicant *wpa_s) {
443 	if (!wpa_s->global->p2p) return false;
444 	return wpa_s->global->p2p->allow_6ghz;
445 }
446 
joinGroup(struct wpa_supplicant * wpa_s,uint8_t * group_owner_bssid,const std::vector<uint8_t> & ssid,const std::string & passphrase)447 int joinGroup(
448 	struct wpa_supplicant* wpa_s,
449 	uint8_t *group_owner_bssid,
450 	const std::vector<uint8_t>& ssid,
451 	const std::string& passphrase)
452 {
453 	int ret = 0;
454 	int he = wpa_s->conf->p2p_go_he;
455 	int vht = wpa_s->conf->p2p_go_vht;
456 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
457 
458 	// Construct a network for adding group.
459 	// Group client follows the persistent attribute of Group Owner.
460 	// If joined group is persistent, it adds a persistent network on GroupStarted.
461 	struct wpa_ssid *wpa_network = addGroupClientNetwork(
462 		wpa_s, group_owner_bssid, ssid, passphrase);
463 	if (wpa_network == NULL) {
464 		wpa_printf(MSG_ERROR, "P2P: Cannot construct a network for group join.");
465 		return -1;
466 	}
467 
468 	// this is temporary network only for establishing the connection.
469 	wpa_network->temporary = 1;
470 
471 	if (wpas_p2p_group_add_persistent(
472 		wpa_s, wpa_network, 0, 0, 0, 0, ht40, vht,
473 		CHANWIDTH_USE_HT, he, 0, NULL, 0, 0, is6GhzAllowed(wpa_s))) {
474 		ret = -1;
475 	}
476 
477 	// Always remove this temporary network at the end.
478 	wpa_config_remove_network(wpa_s->conf, wpa_network->id);
479 	return ret;
480 }
481 
notifyGroupJoinFailure(struct wpa_supplicant * wpa_s)482 void notifyGroupJoinFailure(
483 	struct wpa_supplicant* wpa_s)
484 {
485 	u8 zero_addr[ETH_ALEN] = {0};
486 	std::vector<uint8_t> ssid = {'D', 'I', 'R', 'E','C', 'T', '-'};
487 	std::string passphrase = "";
488 	struct wpa_ssid *wpa_network = addGroupClientNetwork(
489 		wpa_s, zero_addr, ssid, passphrase);
490 	if (wpa_network) {
491 		wpa_network->temporary = 1;
492 		wpas_notify_p2p_group_formation_failure(wpa_s, "Failed to find the group.");
493 		wpas_notify_p2p_group_removed(
494 			wpa_s, wpa_network, "client");
495 		wpa_config_remove_network(
496 			wpa_s->conf, wpa_network->id);
497 	} else {
498 		wpa_printf(MSG_ERROR,
499 			"P2P: Cannot construct a network.");
500 	}
501 }
502 
scanResJoinIgnore(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)503 void scanResJoinIgnore(struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res) {
504 	wpa_printf(MSG_DEBUG, "P2P: Ignore group join scan results.");
505 
506 	if (wpa_s->p2p_scan_work) {
507 		struct wpa_radio_work *work = wpa_s->p2p_scan_work;
508 		wpa_s->p2p_scan_work = NULL;
509 		radio_work_done(work);
510 	}
511 
512 }
513 
updateP2pVendorElem(struct wpa_supplicant * wpa_s,enum wpa_vendor_elem_frame frameType,const std::vector<uint8_t> & vendorElemBytes)514 static void updateP2pVendorElem(struct wpa_supplicant* wpa_s, enum wpa_vendor_elem_frame frameType,
515 	const std::vector<uint8_t>& vendorElemBytes) {
516 
517 	wpa_printf(MSG_INFO, "Set vendor elements to frames %d", frameType);
518 	struct wpa_supplicant* vendor_elem_wpa_s = wpas_vendor_elem(wpa_s, frameType);
519 	if (vendor_elem_wpa_s->vendor_elem[frameType]) {
520 		wpabuf_free(vendor_elem_wpa_s->vendor_elem[frameType]);
521 		vendor_elem_wpa_s->vendor_elem[frameType] = NULL;
522 	}
523 	if (vendorElemBytes.size() > 0) {
524 		vendor_elem_wpa_s->vendor_elem[frameType] =
525 			wpabuf_alloc_copy(vendorElemBytes.data(), vendorElemBytes.size());
526 	}
527 	wpas_vendor_elem_update(vendor_elem_wpa_s);
528 }
529 
convertWpaP2pFrameTypeToHalP2pFrameTypeBit(int frameType)530 uint32_t convertWpaP2pFrameTypeToHalP2pFrameTypeBit(int frameType) {
531 	switch (frameType) {
532 	case VENDOR_ELEM_PROBE_REQ_P2P:
533 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_PROBE_REQ_P2P);
534 	case VENDOR_ELEM_PROBE_RESP_P2P:
535 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_PROBE_RESP_P2P);
536 	case VENDOR_ELEM_PROBE_RESP_P2P_GO:
537 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_PROBE_RESP_P2P_GO);
538 	case VENDOR_ELEM_BEACON_P2P_GO:
539 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_BEACON_P2P_GO);
540 	case VENDOR_ELEM_P2P_PD_REQ:
541 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_PD_REQ);
542 	case VENDOR_ELEM_P2P_PD_RESP:
543 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_PD_RESP);
544 	case VENDOR_ELEM_P2P_GO_NEG_REQ:
545 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_GO_NEG_REQ);
546 	case VENDOR_ELEM_P2P_GO_NEG_RESP:
547 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_GO_NEG_RESP);
548 	case VENDOR_ELEM_P2P_GO_NEG_CONF:
549 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_GO_NEG_CONF);
550 	case VENDOR_ELEM_P2P_INV_REQ:
551 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_INV_REQ);
552 	case VENDOR_ELEM_P2P_INV_RESP:
553 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_INV_RESP);
554 	case VENDOR_ELEM_P2P_ASSOC_REQ:
555 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_ASSOC_REQ);
556 	case VENDOR_ELEM_P2P_ASSOC_RESP:
557 		return static_cast<uint32_t>(P2pFrameTypeMask::P2P_FRAME_P2P_ASSOC_RESP);
558 	}
559 	return 0;
560 }
561 }  // namespace
562 
563 namespace aidl {
564 namespace android {
565 namespace hardware {
566 namespace wifi {
567 namespace supplicant {
568 using aidl_return_util::validateAndCall;
569 using misc_utils::createStatus;
570 using misc_utils::createStatusWithMsg;
571 
P2pIface(struct wpa_global * wpa_global,const char ifname[])572 P2pIface::P2pIface(struct wpa_global* wpa_global, const char ifname[])
573 	: wpa_global_(wpa_global), ifname_(ifname), is_valid_(true)
574 {}
575 
invalidate()576 void P2pIface::invalidate() { is_valid_ = false; }
isValid()577 bool P2pIface::isValid()
578 {
579 	return (is_valid_ && (retrieveIfacePtr() != nullptr));
580 }
581 
getName(std::string * _aidl_return)582 ::ndk::ScopedAStatus P2pIface::getName(
583 	std::string* _aidl_return)
584 {
585 	return validateAndCall(
586 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
587 		&P2pIface::getNameInternal, _aidl_return);
588 }
589 
getType(IfaceType * _aidl_return)590 ::ndk::ScopedAStatus P2pIface::getType(
591 	IfaceType* _aidl_return)
592 {
593 	return validateAndCall(
594 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
595 		&P2pIface::getTypeInternal, _aidl_return);
596 }
597 
addNetwork(std::shared_ptr<ISupplicantP2pNetwork> * _aidl_return)598 ::ndk::ScopedAStatus P2pIface::addNetwork(
599 	std::shared_ptr<ISupplicantP2pNetwork>* _aidl_return)
600 {
601 	return validateAndCall(
602 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
603 		&P2pIface::addNetworkInternal, _aidl_return);
604 }
605 
removeNetwork(int32_t in_id)606 ::ndk::ScopedAStatus P2pIface::removeNetwork(
607 	int32_t in_id)
608 {
609 	return validateAndCall(
610 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
611 		&P2pIface::removeNetworkInternal, in_id);
612 }
613 
getNetwork(int32_t in_id,std::shared_ptr<ISupplicantP2pNetwork> * _aidl_return)614 ::ndk::ScopedAStatus P2pIface::getNetwork(
615 	int32_t in_id, std::shared_ptr<ISupplicantP2pNetwork>* _aidl_return)
616 {
617 	return validateAndCall(
618 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
619 		&P2pIface::getNetworkInternal, _aidl_return, in_id);
620 }
621 
listNetworks(std::vector<int32_t> * _aidl_return)622 ::ndk::ScopedAStatus P2pIface::listNetworks(
623 	std::vector<int32_t>* _aidl_return)
624 {
625 	return validateAndCall(
626 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
627 		&P2pIface::listNetworksInternal, _aidl_return);
628 }
629 
registerCallback(const std::shared_ptr<ISupplicantP2pIfaceCallback> & in_callback)630 ::ndk::ScopedAStatus P2pIface::registerCallback(
631 	const std::shared_ptr<ISupplicantP2pIfaceCallback>& in_callback)
632 {
633 	return validateAndCall(
634 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
635 		&P2pIface::registerCallbackInternal, in_callback);
636 }
637 
getDeviceAddress(std::vector<uint8_t> * _aidl_return)638 ::ndk::ScopedAStatus P2pIface::getDeviceAddress(
639 	std::vector<uint8_t>* _aidl_return)
640 {
641 	return validateAndCall(
642 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
643 		&P2pIface::getDeviceAddressInternal, _aidl_return);
644 }
645 
setSsidPostfix(const std::vector<uint8_t> & in_postfix)646 ::ndk::ScopedAStatus P2pIface::setSsidPostfix(
647 	const std::vector<uint8_t>& in_postfix)
648 {
649 	return validateAndCall(
650 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
651 		&P2pIface::setSsidPostfixInternal, in_postfix);
652 }
653 
setGroupIdle(const std::string & in_groupIfName,int32_t in_timeoutInSec)654 ::ndk::ScopedAStatus P2pIface::setGroupIdle(
655 	const std::string& in_groupIfName, int32_t in_timeoutInSec)
656 {
657 	return validateAndCall(
658 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
659 		&P2pIface::setGroupIdleInternal, in_groupIfName,
660 		in_timeoutInSec);
661 }
662 
setPowerSave(const std::string & in_groupIfName,bool in_enable)663 ::ndk::ScopedAStatus P2pIface::setPowerSave(
664 	const std::string& in_groupIfName, bool in_enable)
665 {
666 	return validateAndCall(
667 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
668 		&P2pIface::setPowerSaveInternal, in_groupIfName, in_enable);
669 }
670 
find(int32_t in_timeoutInSec)671 ::ndk::ScopedAStatus P2pIface::find(
672 	int32_t in_timeoutInSec)
673 {
674 	return validateAndCall(
675 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
676 		&P2pIface::findInternal, in_timeoutInSec);
677 }
678 
stopFind()679 ::ndk::ScopedAStatus P2pIface::stopFind()
680 {
681 	return validateAndCall(
682 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
683 		&P2pIface::stopFindInternal);
684 }
685 
flush()686 ::ndk::ScopedAStatus P2pIface::flush()
687 {
688 	return validateAndCall(
689 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
690 		&P2pIface::flushInternal);
691 }
692 
connect(const std::vector<uint8_t> & in_peerAddress,WpsProvisionMethod in_provisionMethod,const std::string & in_preSelectedPin,bool in_joinExistingGroup,bool in_persistent,int32_t in_goIntent,std::string * _aidl_return)693 ::ndk::ScopedAStatus P2pIface::connect(
694 	const std::vector<uint8_t>& in_peerAddress,
695 	WpsProvisionMethod in_provisionMethod,
696 	const std::string& in_preSelectedPin, bool in_joinExistingGroup,
697 	bool in_persistent, int32_t in_goIntent, std::string* _aidl_return)
698 {
699 	return validateAndCall(
700 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
701 		&P2pIface::connectInternal, _aidl_return, in_peerAddress,
702 		in_provisionMethod, in_preSelectedPin, in_joinExistingGroup,
703 		in_persistent, in_goIntent);
704 }
705 
cancelConnect()706 ::ndk::ScopedAStatus P2pIface::cancelConnect()
707 {
708 	return validateAndCall(
709 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
710 		&P2pIface::cancelConnectInternal);
711 }
712 
provisionDiscovery(const std::vector<uint8_t> & in_peerAddress,WpsProvisionMethod in_provisionMethod)713 ::ndk::ScopedAStatus P2pIface::provisionDiscovery(
714 	const std::vector<uint8_t>& in_peerAddress,
715 	WpsProvisionMethod in_provisionMethod)
716 {
717 	return validateAndCall(
718 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
719 		&P2pIface::provisionDiscoveryInternal, in_peerAddress,
720 		in_provisionMethod);
721 }
722 
addGroup(bool in_persistent,int32_t in_persistentNetworkId)723 ndk::ScopedAStatus P2pIface::addGroup(
724 	bool in_persistent, int32_t in_persistentNetworkId)
725 {
726 	return validateAndCall(
727 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
728 		&P2pIface::addGroupInternal, in_persistent,
729 		in_persistentNetworkId);
730 }
731 
addGroupWithConfig(const std::vector<uint8_t> & in_ssid,const std::string & in_pskPassphrase,bool in_persistent,int32_t in_freq,const std::vector<uint8_t> & in_peerAddress,bool in_joinExistingGroup)732 ::ndk::ScopedAStatus P2pIface::addGroupWithConfig(
733 	const std::vector<uint8_t>& in_ssid,
734 	const std::string& in_pskPassphrase, bool in_persistent,
735 	int32_t in_freq, const std::vector<uint8_t>& in_peerAddress,
736 	bool in_joinExistingGroup)
737 {
738 	return validateAndCall(
739 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
740 		&P2pIface::addGroupWithConfigInternal, in_ssid,
741 		in_pskPassphrase, in_persistent, in_freq,
742 		in_peerAddress, in_joinExistingGroup);
743 }
744 
removeGroup(const std::string & in_groupIfName)745 ::ndk::ScopedAStatus P2pIface::removeGroup(
746 	const std::string& in_groupIfName)
747 {
748 	return validateAndCall(
749 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
750 		&P2pIface::removeGroupInternal, in_groupIfName);
751 }
752 
reject(const std::vector<uint8_t> & in_peerAddress)753 ::ndk::ScopedAStatus P2pIface::reject(
754 	const std::vector<uint8_t>& in_peerAddress)
755 {
756 	return validateAndCall(
757 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
758 		&P2pIface::rejectInternal, in_peerAddress);
759 }
760 
invite(const std::string & in_groupIfName,const std::vector<uint8_t> & in_goDeviceAddress,const std::vector<uint8_t> & in_peerAddress)761 ::ndk::ScopedAStatus P2pIface::invite(
762 	const std::string& in_groupIfName,
763 	const std::vector<uint8_t>& in_goDeviceAddress,
764 	const std::vector<uint8_t>& in_peerAddress)
765 {
766 	return validateAndCall(
767 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
768 		&P2pIface::inviteInternal, in_groupIfName,
769 		in_goDeviceAddress, in_peerAddress);
770 }
771 
reinvoke(int32_t in_persistentNetworkId,const std::vector<uint8_t> & in_peerAddress)772 ::ndk::ScopedAStatus P2pIface::reinvoke(
773 	int32_t in_persistentNetworkId,
774 	const std::vector<uint8_t>& in_peerAddress)
775 {
776 	return validateAndCall(
777 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
778 		&P2pIface::reinvokeInternal, in_persistentNetworkId,
779 		in_peerAddress);
780 }
781 
configureExtListen(int32_t in_periodInMillis,int32_t in_intervalInMillis)782 ::ndk::ScopedAStatus P2pIface::configureExtListen(
783 	int32_t in_periodInMillis, int32_t in_intervalInMillis)
784 {
785 	return validateAndCall(
786 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
787 		&P2pIface::configureExtListenInternal, in_periodInMillis,
788 		in_intervalInMillis);
789 }
790 
setListenChannel(int32_t in_channel,int32_t in_operatingClass)791 ::ndk::ScopedAStatus P2pIface::setListenChannel(
792 	int32_t in_channel, int32_t in_operatingClass)
793 {
794 	return validateAndCall(
795 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
796 		&P2pIface::setListenChannelInternal, in_channel,
797 		in_operatingClass);
798 }
799 
setDisallowedFrequencies(const std::vector<FreqRange> & in_ranges)800 ::ndk::ScopedAStatus P2pIface::setDisallowedFrequencies(
801 	const std::vector<FreqRange>& in_ranges)
802 {
803 	return validateAndCall(
804 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
805 		&P2pIface::setDisallowedFrequenciesInternal, in_ranges);
806 }
807 
getSsid(const std::vector<uint8_t> & in_peerAddress,std::vector<uint8_t> * _aidl_return)808 ::ndk::ScopedAStatus P2pIface::getSsid(
809 	const std::vector<uint8_t>& in_peerAddress,
810 	std::vector<uint8_t>* _aidl_return)
811 {
812 	return validateAndCall(
813 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
814 		&P2pIface::getSsidInternal, _aidl_return, in_peerAddress);
815 }
816 
getGroupCapability(const std::vector<uint8_t> & in_peerAddress,P2pGroupCapabilityMask * _aidl_return)817 ::ndk::ScopedAStatus P2pIface::getGroupCapability(
818 	const std::vector<uint8_t>& in_peerAddress,
819 	P2pGroupCapabilityMask* _aidl_return)
820 {
821 	return validateAndCall(
822 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
823 		&P2pIface::getGroupCapabilityInternal, _aidl_return, in_peerAddress);
824 }
825 
addBonjourService(const std::vector<uint8_t> & in_query,const std::vector<uint8_t> & in_response)826 ::ndk::ScopedAStatus P2pIface::addBonjourService(
827 	const std::vector<uint8_t>& in_query,
828 	const std::vector<uint8_t>& in_response)
829 {
830 	return validateAndCall(
831 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
832 		&P2pIface::addBonjourServiceInternal, in_query, in_response);
833 }
834 
removeBonjourService(const std::vector<uint8_t> & in_query)835 ::ndk::ScopedAStatus P2pIface::removeBonjourService(
836 	const std::vector<uint8_t>& in_query)
837 {
838 	return validateAndCall(
839 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
840 		&P2pIface::removeBonjourServiceInternal, in_query);
841 }
842 
addUpnpService(int32_t in_version,const std::string & in_serviceName)843 ::ndk::ScopedAStatus P2pIface::addUpnpService(
844 	int32_t in_version, const std::string& in_serviceName)
845 {
846 	return validateAndCall(
847 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
848 		&P2pIface::addUpnpServiceInternal, in_version, in_serviceName);
849 }
850 
removeUpnpService(int32_t in_version,const std::string & in_serviceName)851 ::ndk::ScopedAStatus P2pIface::removeUpnpService(
852 	int32_t in_version, const std::string& in_serviceName)
853 {
854 	return validateAndCall(
855 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
856 		&P2pIface::removeUpnpServiceInternal, in_version,
857 		in_serviceName);
858 }
859 
flushServices()860 ::ndk::ScopedAStatus P2pIface::flushServices()
861 {
862 	return validateAndCall(
863 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
864 		&P2pIface::flushServicesInternal);
865 }
866 
requestServiceDiscovery(const std::vector<uint8_t> & in_peerAddress,const std::vector<uint8_t> & in_query,int64_t * _aidl_return)867 ::ndk::ScopedAStatus P2pIface::requestServiceDiscovery(
868 	const std::vector<uint8_t>& in_peerAddress,
869 	const std::vector<uint8_t>& in_query,
870 	int64_t* _aidl_return)
871 {
872 	return validateAndCall(
873 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
874 		&P2pIface::requestServiceDiscoveryInternal, _aidl_return,
875 		in_peerAddress, in_query);
876 }
877 
cancelServiceDiscovery(int64_t in_identifier)878 ::ndk::ScopedAStatus P2pIface::cancelServiceDiscovery(
879 	int64_t in_identifier)
880 {
881 	return validateAndCall(
882 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
883 		&P2pIface::cancelServiceDiscoveryInternal, in_identifier);
884 }
885 
setMiracastMode(MiracastMode in_mode)886 ::ndk::ScopedAStatus P2pIface::setMiracastMode(
887 	MiracastMode in_mode)
888 {
889 	return validateAndCall(
890 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
891 		&P2pIface::setMiracastModeInternal, in_mode);
892 }
893 
startWpsPbc(const std::string & in_groupIfName,const std::vector<uint8_t> & in_bssid)894 ::ndk::ScopedAStatus P2pIface::startWpsPbc(
895 	const std::string& in_groupIfName,
896 	const std::vector<uint8_t>& in_bssid)
897 {
898 	return validateAndCall(
899 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
900 		&P2pIface::startWpsPbcInternal, in_groupIfName, in_bssid);
901 }
902 
startWpsPinKeypad(const std::string & in_groupIfName,const std::string & in_pin)903 ::ndk::ScopedAStatus P2pIface::startWpsPinKeypad(
904 	const std::string& in_groupIfName,
905 	const std::string& in_pin)
906 {
907 	return validateAndCall(
908 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
909 		&P2pIface::startWpsPinKeypadInternal, in_groupIfName, in_pin);
910 }
911 
startWpsPinDisplay(const std::string & in_groupIfName,const std::vector<uint8_t> & in_bssid,std::string * _aidl_return)912 ::ndk::ScopedAStatus P2pIface::startWpsPinDisplay(
913 	const std::string& in_groupIfName,
914 	const std::vector<uint8_t>& in_bssid,
915 	std::string* _aidl_return)
916 {
917 	return validateAndCall(
918 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
919 		&P2pIface::startWpsPinDisplayInternal, _aidl_return,
920 		in_groupIfName, in_bssid);
921 }
922 
cancelWps(const std::string & in_groupIfName)923 ::ndk::ScopedAStatus P2pIface::cancelWps(
924 	const std::string& in_groupIfName)
925 {
926 	return validateAndCall(
927 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
928 		&P2pIface::cancelWpsInternal, in_groupIfName);
929 }
930 
setWpsDeviceName(const std::string & in_name)931 ::ndk::ScopedAStatus P2pIface::setWpsDeviceName(
932 	const std::string& in_name)
933 {
934 	return validateAndCall(
935 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
936 		&P2pIface::setWpsDeviceNameInternal, in_name);
937 }
938 
setWpsDeviceType(const std::vector<uint8_t> & in_type)939 ::ndk::ScopedAStatus P2pIface::setWpsDeviceType(
940 	const std::vector<uint8_t>& in_type)
941 {
942 	return validateAndCall(
943 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
944 		&P2pIface::setWpsDeviceTypeInternal, in_type);
945 }
946 
setWpsManufacturer(const std::string & in_manufacturer)947 ::ndk::ScopedAStatus P2pIface::setWpsManufacturer(
948 	const std::string& in_manufacturer)
949 {
950 	return validateAndCall(
951 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
952 		&P2pIface::setWpsManufacturerInternal, in_manufacturer);
953 }
954 
setWpsModelName(const std::string & in_modelName)955 ::ndk::ScopedAStatus P2pIface::setWpsModelName(
956 	const std::string& in_modelName)
957 {
958 	return validateAndCall(
959 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
960 		&P2pIface::setWpsModelNameInternal, in_modelName);
961 }
962 
setWpsModelNumber(const std::string & in_modelNumber)963 ::ndk::ScopedAStatus P2pIface::setWpsModelNumber(
964 	const std::string& in_modelNumber)
965 {
966 	return validateAndCall(
967 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
968 		&P2pIface::setWpsModelNumberInternal, in_modelNumber);
969 }
970 
setWpsSerialNumber(const std::string & in_serialNumber)971 ::ndk::ScopedAStatus P2pIface::setWpsSerialNumber(
972 	const std::string& in_serialNumber)
973 {
974 	return validateAndCall(
975 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
976 		&P2pIface::setWpsSerialNumberInternal, in_serialNumber);
977 }
978 
setWpsConfigMethods(WpsConfigMethods in_configMethods)979 ::ndk::ScopedAStatus P2pIface::setWpsConfigMethods(
980 	WpsConfigMethods in_configMethods)
981 {
982 	return validateAndCall(
983 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
984 		&P2pIface::setWpsConfigMethodsInternal, in_configMethods);
985 }
986 
enableWfd(bool in_enable)987 ::ndk::ScopedAStatus P2pIface::enableWfd(
988 	bool in_enable)
989 {
990 	return validateAndCall(
991 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
992 		&P2pIface::enableWfdInternal, in_enable);
993 }
994 
setWfdDeviceInfo(const std::vector<uint8_t> & in_info)995 ::ndk::ScopedAStatus P2pIface::setWfdDeviceInfo(
996 	const std::vector<uint8_t>& in_info)
997 {
998 	return validateAndCall(
999 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1000 		&P2pIface::setWfdDeviceInfoInternal, in_info);
1001 }
1002 
createNfcHandoverRequestMessage(std::vector<uint8_t> * _aidl_return)1003 ::ndk::ScopedAStatus P2pIface::createNfcHandoverRequestMessage(
1004 	std::vector<uint8_t>* _aidl_return)
1005 {
1006 	return validateAndCall(
1007 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1008 		&P2pIface::createNfcHandoverRequestMessageInternal, _aidl_return);
1009 }
1010 
createNfcHandoverSelectMessage(std::vector<uint8_t> * _aidl_return)1011 ::ndk::ScopedAStatus P2pIface::createNfcHandoverSelectMessage(
1012 	std::vector<uint8_t>* _aidl_return)
1013 {
1014 	return validateAndCall(
1015 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1016 		&P2pIface::createNfcHandoverSelectMessageInternal, _aidl_return);
1017 }
1018 
reportNfcHandoverResponse(const std::vector<uint8_t> & in_request)1019 ::ndk::ScopedAStatus P2pIface::reportNfcHandoverResponse(
1020 	const std::vector<uint8_t>& in_request)
1021 {
1022 	return validateAndCall(
1023 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1024 		&P2pIface::reportNfcHandoverResponseInternal, in_request);
1025 }
1026 
reportNfcHandoverInitiation(const std::vector<uint8_t> & in_select)1027 ::ndk::ScopedAStatus P2pIface::reportNfcHandoverInitiation(
1028 	const std::vector<uint8_t>& in_select)
1029 {
1030 	return validateAndCall(
1031 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1032 		&P2pIface::reportNfcHandoverInitiationInternal, in_select);
1033 }
1034 
saveConfig()1035 ::ndk::ScopedAStatus P2pIface::saveConfig()
1036 {
1037 	return validateAndCall(
1038 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1039 		&P2pIface::saveConfigInternal);
1040 }
1041 
setMacRandomization(bool in_enable)1042 ::ndk::ScopedAStatus P2pIface::setMacRandomization(
1043 	bool in_enable)
1044 {
1045 	return validateAndCall(
1046 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1047 		&P2pIface::setMacRandomizationInternal, in_enable);
1048 }
1049 
setEdmg(bool in_enable)1050 ::ndk::ScopedAStatus P2pIface::setEdmg(
1051 	bool in_enable)
1052 {
1053 	return validateAndCall(
1054 		this, SupplicantStatusCode::FAILURE_NETWORK_INVALID,
1055 		&P2pIface::setEdmgInternal, in_enable);
1056 }
1057 
getEdmg(bool * _aidl_return)1058 ::ndk::ScopedAStatus P2pIface::getEdmg(
1059 	bool* _aidl_return)
1060 {
1061 	return validateAndCall(
1062 		this, SupplicantStatusCode::FAILURE_NETWORK_INVALID,
1063 		&P2pIface::getEdmgInternal, _aidl_return);
1064 }
1065 
setWfdR2DeviceInfo(const std::vector<uint8_t> & in_info)1066 ::ndk::ScopedAStatus P2pIface::setWfdR2DeviceInfo(
1067 	const std::vector<uint8_t>& in_info)
1068 {
1069 	return validateAndCall(
1070 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1071 		&P2pIface::setWfdR2DeviceInfoInternal, in_info);
1072 }
1073 
removeClient(const std::vector<uint8_t> & peer_address,bool isLegacyClient)1074 ::ndk::ScopedAStatus P2pIface::removeClient(
1075         const std::vector<uint8_t>& peer_address, bool isLegacyClient)
1076 {
1077 	return validateAndCall(
1078 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1079 		&P2pIface::removeClientInternal, peer_address, isLegacyClient);
1080 }
1081 
findOnSocialChannels(int32_t in_timeoutInSec)1082 ::ndk::ScopedAStatus P2pIface::findOnSocialChannels(
1083 	int32_t in_timeoutInSec)
1084 {
1085 	return validateAndCall(
1086 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1087 		&P2pIface::findOnSocialChannelsInternal, in_timeoutInSec);
1088 }
1089 
findOnSpecificFrequency(int32_t in_freq,int32_t in_timeoutInSec)1090 ::ndk::ScopedAStatus P2pIface::findOnSpecificFrequency(
1091 	int32_t in_freq,
1092 	int32_t in_timeoutInSec)
1093 {
1094 	return validateAndCall(
1095 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1096 		&P2pIface::findOnSpecificFrequencyInternal,
1097 		in_freq, in_timeoutInSec);
1098 }
1099 
setVendorElements(P2pFrameTypeMask in_frameTypeMask,const std::vector<uint8_t> & in_vendorElemBytes)1100 ::ndk::ScopedAStatus P2pIface::setVendorElements(
1101 	P2pFrameTypeMask in_frameTypeMask,
1102 	const std::vector<uint8_t>& in_vendorElemBytes)
1103 {
1104 	return validateAndCall(
1105 		this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
1106 		&P2pIface::setVendorElementsInternal, in_frameTypeMask, in_vendorElemBytes);
1107 }
1108 
getNameInternal()1109 std::pair<std::string, ndk::ScopedAStatus> P2pIface::getNameInternal()
1110 {
1111 	return {ifname_, ndk::ScopedAStatus::ok()};
1112 }
1113 
getTypeInternal()1114 std::pair<IfaceType, ndk::ScopedAStatus> P2pIface::getTypeInternal()
1115 {
1116 	return {IfaceType::P2P, ndk::ScopedAStatus::ok()};
1117 }
1118 
1119 std::pair<std::shared_ptr<ISupplicantP2pNetwork>, ndk::ScopedAStatus>
addNetworkInternal()1120 P2pIface::addNetworkInternal()
1121 {
1122 	std::shared_ptr<ISupplicantP2pNetwork> network;
1123 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1124 	struct wpa_ssid* ssid = wpa_supplicant_add_network(wpa_s);
1125 	if (!ssid) {
1126 		return {network, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1127 	}
1128 	AidlManager* aidl_manager = AidlManager::getInstance();
1129 	if (!aidl_manager ||
1130 		aidl_manager->getP2pNetworkAidlObjectByIfnameAndNetworkId(
1131 		wpa_s->ifname, ssid->id, &network)) {
1132 		return {network, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1133 	}
1134 	return {network, ndk::ScopedAStatus::ok()};
1135 }
1136 
removeNetworkInternal(int32_t id)1137 ndk::ScopedAStatus P2pIface::removeNetworkInternal(int32_t id)
1138 {
1139 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1140 	int result = wpa_supplicant_remove_network(wpa_s, id);
1141 	if (result == -1) {
1142 		return createStatus(SupplicantStatusCode::FAILURE_NETWORK_UNKNOWN);
1143 	} else if (result != 0) {
1144 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1145 	}
1146 	return ndk::ScopedAStatus::ok();
1147 }
1148 
1149 std::pair<std::shared_ptr<ISupplicantP2pNetwork>, ndk::ScopedAStatus>
getNetworkInternal(int32_t id)1150 P2pIface::getNetworkInternal(int32_t id)
1151 {
1152 	std::shared_ptr<ISupplicantP2pNetwork> network;
1153 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1154 	struct wpa_ssid* ssid = wpa_config_get_network(wpa_s->conf, id);
1155 	if (!ssid) {
1156 		return {network, createStatus(SupplicantStatusCode::FAILURE_NETWORK_UNKNOWN)};
1157 	}
1158 	AidlManager* aidl_manager = AidlManager::getInstance();
1159 	if (!aidl_manager ||
1160 		aidl_manager->getP2pNetworkAidlObjectByIfnameAndNetworkId(
1161 		wpa_s->ifname, ssid->id, &network)) {
1162 		return {network, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1163 	}
1164 	return {network, ndk::ScopedAStatus::ok()};
1165 }
1166 
1167 std::pair<std::vector<int32_t>, ndk::ScopedAStatus>
listNetworksInternal()1168 P2pIface::listNetworksInternal()
1169 {
1170 	std::vector<int32_t> network_ids;
1171 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1172 	for (struct wpa_ssid* wpa_ssid = wpa_s->conf->ssid; wpa_ssid;
1173 		 wpa_ssid = wpa_ssid->next) {
1174 		network_ids.emplace_back(wpa_ssid->id);
1175 	}
1176 	return {std::move(network_ids), ndk::ScopedAStatus::ok()};
1177 }
1178 
registerCallbackInternal(const std::shared_ptr<ISupplicantP2pIfaceCallback> & callback)1179 ndk::ScopedAStatus P2pIface::registerCallbackInternal(
1180 	const std::shared_ptr<ISupplicantP2pIfaceCallback>& callback)
1181 {
1182 	AidlManager* aidl_manager = AidlManager::getInstance();
1183 	if (!aidl_manager ||
1184 		aidl_manager->addP2pIfaceCallbackAidlObject(ifname_, callback)) {
1185 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1186 	}
1187 	return ndk::ScopedAStatus::ok();
1188 }
1189 
1190 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus>
getDeviceAddressInternal()1191 P2pIface::getDeviceAddressInternal()
1192 {
1193 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1194 	std::vector<uint8_t> addr(
1195 		wpa_s->global->p2p_dev_addr,
1196 		wpa_s->global->p2p_dev_addr + ETH_ALEN);
1197 	return {addr, ndk::ScopedAStatus::ok()};
1198 }
1199 
setSsidPostfixInternal(const std::vector<uint8_t> & postfix)1200 ndk::ScopedAStatus P2pIface::setSsidPostfixInternal(
1201 	const std::vector<uint8_t>& postfix)
1202 {
1203 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1204 	if (p2p_set_ssid_postfix(
1205 		wpa_s->global->p2p, postfix.data(), postfix.size())) {
1206 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1207 	}
1208 	return ndk::ScopedAStatus::ok();
1209 }
1210 
setGroupIdleInternal(const std::string & group_ifname,uint32_t timeout_in_sec)1211 ndk::ScopedAStatus P2pIface::setGroupIdleInternal(
1212 	const std::string& group_ifname, uint32_t timeout_in_sec)
1213 {
1214 	struct wpa_supplicant* wpa_group_s =
1215 		retrieveGroupIfacePtr(group_ifname);
1216 	if (!wpa_group_s) {
1217 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
1218 	}
1219 	wpa_group_s->conf->p2p_group_idle = timeout_in_sec;
1220 	return ndk::ScopedAStatus::ok();
1221 }
1222 
setPowerSaveInternal(const std::string & group_ifname,bool enable)1223 ndk::ScopedAStatus P2pIface::setPowerSaveInternal(
1224 	const std::string& group_ifname, bool enable)
1225 {
1226 	struct wpa_supplicant* wpa_group_s =
1227 		retrieveGroupIfacePtr(group_ifname);
1228 	if (!wpa_group_s) {
1229 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
1230 	}
1231 	if (wpa_drv_set_p2p_powersave(wpa_group_s, enable, -1, -1)) {
1232 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1233 	}
1234 	return ndk::ScopedAStatus::ok();
1235 }
1236 
findInternal(uint32_t timeout_in_sec)1237 ndk::ScopedAStatus P2pIface::findInternal(uint32_t timeout_in_sec)
1238 {
1239 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1240 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1241 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_DISABLED);
1242 	}
1243 	uint32_t search_delay = wpas_p2p_search_delay(wpa_s);
1244 	if (wpas_p2p_find(
1245 		wpa_s, timeout_in_sec, P2P_FIND_START_WITH_FULL, 0, nullptr,
1246 		nullptr, search_delay, 0, nullptr, 0, is6GhzAllowed(wpa_s))) {
1247 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1248 	}
1249 	return ndk::ScopedAStatus::ok();
1250 }
1251 
stopFindInternal()1252 ndk::ScopedAStatus P2pIface::stopFindInternal()
1253 {
1254 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1255 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1256 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_DISABLED);
1257 	}
1258 	if (wpa_s->scan_res_handler == scanResJoinWrapper) {
1259 		wpa_printf(MSG_DEBUG, "P2P: Stop pending group scan for stopping find).");
1260 		pending_scan_res_join_callback = NULL;
1261 		wpa_s->scan_res_handler = scanResJoinIgnore;
1262 	}
1263 	wpas_p2p_stop_find(wpa_s);
1264 	return ndk::ScopedAStatus::ok();
1265 }
1266 
flushInternal()1267 ndk::ScopedAStatus P2pIface::flushInternal()
1268 {
1269 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1270 	os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
1271 	wpa_s->force_long_sd = 0;
1272 	wpas_p2p_stop_find(wpa_s);
1273 	wpa_s->parent->p2ps_method_config_any = 0;
1274 	wpa_bss_flush(wpa_s);
1275 	if (wpa_s->global->p2p)
1276 		p2p_flush(wpa_s->global->p2p);
1277 	return ndk::ScopedAStatus::ok();
1278 }
1279 
1280 // This method only implements support for subset (needed by Android framework)
1281 // of parameters that can be specified for connect.
connectInternal(const std::vector<uint8_t> & peer_address,WpsProvisionMethod provision_method,const std::string & pre_selected_pin,bool join_existing_group,bool persistent,uint32_t go_intent)1282 std::pair<std::string, ndk::ScopedAStatus> P2pIface::connectInternal(
1283 	const std::vector<uint8_t>& peer_address,
1284 	WpsProvisionMethod provision_method,
1285 	const std::string& pre_selected_pin, bool join_existing_group,
1286 	bool persistent, uint32_t go_intent)
1287 {
1288 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1289 	if (go_intent > 15) {
1290 		return {"", createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID)};
1291 	}
1292 	if (peer_address.size() != ETH_ALEN) {
1293 		return {"", createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID)};
1294 	}
1295 	int go_intent_signed = join_existing_group ? -1 : go_intent;
1296 	p2p_wps_method wps_method = {};
1297 	switch (provision_method) {
1298 	case WpsProvisionMethod::PBC:
1299 		wps_method = WPS_PBC;
1300 		break;
1301 	case WpsProvisionMethod::DISPLAY:
1302 		wps_method = WPS_PIN_DISPLAY;
1303 		break;
1304 	case WpsProvisionMethod::KEYPAD:
1305 		wps_method = WPS_PIN_KEYPAD;
1306 		break;
1307 	}
1308 	int he = wpa_s->conf->p2p_go_he;
1309 	int vht = wpa_s->conf->p2p_go_vht;
1310 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
1311 	const char* pin =
1312 		pre_selected_pin.length() > 0 ? pre_selected_pin.data() : nullptr;
1313 	bool auto_join = !join_existing_group;
1314 	int new_pin = wpas_p2p_connect(
1315 		wpa_s, peer_address.data(), pin, wps_method, persistent, auto_join,
1316 		join_existing_group, false, go_intent_signed, 0, 0, -1, false, ht40,
1317 		vht, CHANWIDTH_USE_HT, he, 0, nullptr, 0, is6GhzAllowed(wpa_s));
1318 	if (new_pin < 0) {
1319 		return {"", createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1320 	}
1321 	std::string pin_ret;
1322 	if (provision_method == WpsProvisionMethod::DISPLAY &&
1323 		pre_selected_pin.empty()) {
1324 		pin_ret = misc_utils::convertWpsPinToString(new_pin);
1325 	}
1326 	return {pin_ret, ndk::ScopedAStatus::ok()};
1327 }
1328 
cancelConnectInternal()1329 ndk::ScopedAStatus P2pIface::cancelConnectInternal()
1330 {
1331 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1332 	if (wpa_s->scan_res_handler == scanResJoinWrapper) {
1333 		wpa_printf(MSG_DEBUG, "P2P: Stop pending group scan for canceling connect");
1334 		pending_scan_res_join_callback = NULL;
1335 		wpa_s->scan_res_handler = scanResJoinIgnore;
1336 	}
1337 	if (wpas_p2p_cancel(wpa_s)) {
1338 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1339 	}
1340 	return ndk::ScopedAStatus::ok();
1341 }
1342 
provisionDiscoveryInternal(const std::vector<uint8_t> & peer_address,WpsProvisionMethod provision_method)1343 ndk::ScopedAStatus P2pIface::provisionDiscoveryInternal(
1344 	const std::vector<uint8_t>& peer_address,
1345 	WpsProvisionMethod provision_method)
1346 {
1347 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1348 	p2ps_provision* prov_param;
1349 	const char* config_method_str = nullptr;
1350 	if (peer_address.size() != ETH_ALEN) {
1351 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1352 	}
1353 	switch (provision_method) {
1354 	case WpsProvisionMethod::PBC:
1355 		config_method_str = kConfigMethodStrPbc;
1356 		break;
1357 	case WpsProvisionMethod::DISPLAY:
1358 		config_method_str = kConfigMethodStrDisplay;
1359 		break;
1360 	case WpsProvisionMethod::KEYPAD:
1361 		config_method_str = kConfigMethodStrKeypad;
1362 		break;
1363 	}
1364 	if (wpas_p2p_prov_disc(
1365 		wpa_s, peer_address.data(), config_method_str,
1366 		WPAS_P2P_PD_FOR_GO_NEG, nullptr)) {
1367 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1368 	}
1369 	return ndk::ScopedAStatus::ok();
1370 }
1371 
removeGroupInternal(const std::string & group_ifname)1372 ndk::ScopedAStatus P2pIface::removeGroupInternal(const std::string& group_ifname)
1373 {
1374 	struct wpa_supplicant* wpa_group_s =
1375 		retrieveGroupIfacePtr(group_ifname);
1376 	if (!wpa_group_s) {
1377 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
1378 	}
1379 	wpa_group_s->global->p2p_go_found_external_scan = 0;
1380 	if (wpas_p2p_group_remove(wpa_group_s, group_ifname.c_str())) {
1381 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1382 	}
1383 	return ndk::ScopedAStatus::ok();
1384 }
1385 
rejectInternal(const std::vector<uint8_t> & peer_address)1386 ndk::ScopedAStatus P2pIface::rejectInternal(
1387 	const std::vector<uint8_t>& peer_address)
1388 {
1389 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1390 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
1391 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_DISABLED);
1392 	}
1393 	if (peer_address.size() != ETH_ALEN) {
1394 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1395 	}
1396 	if (wpas_p2p_reject(wpa_s, peer_address.data())) {
1397 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1398 	}
1399 	return ndk::ScopedAStatus::ok();
1400 }
1401 
inviteInternal(const std::string & group_ifname,const std::vector<uint8_t> & go_device_address,const std::vector<uint8_t> & peer_address)1402 ndk::ScopedAStatus P2pIface::inviteInternal(
1403 	const std::string& group_ifname,
1404 	const std::vector<uint8_t>& go_device_address,
1405 	const std::vector<uint8_t>& peer_address)
1406 {
1407 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1408 	if (go_device_address.size() != ETH_ALEN || peer_address.size() != ETH_ALEN) {
1409 		return {createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1410 	}
1411 	if (wpas_p2p_invite_group(
1412 		wpa_s, group_ifname.c_str(), peer_address.data(),
1413 		go_device_address.data(), is6GhzAllowed(wpa_s))) {
1414 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1415 	}
1416 	return ndk::ScopedAStatus::ok();
1417 }
1418 
reinvokeInternal(int32_t persistent_network_id,const std::vector<uint8_t> & peer_address)1419 ndk::ScopedAStatus P2pIface::reinvokeInternal(
1420 	int32_t persistent_network_id,
1421 	const std::vector<uint8_t>& peer_address)
1422 {
1423 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1424 	int he = wpa_s->conf->p2p_go_he;
1425 	int vht = wpa_s->conf->p2p_go_vht;
1426 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
1427 	struct wpa_ssid* ssid =
1428 		wpa_config_get_network(wpa_s->conf, persistent_network_id);
1429 	if (ssid == NULL || ssid->disabled != 2) {
1430 		return createStatus(SupplicantStatusCode::FAILURE_NETWORK_UNKNOWN);
1431 	}
1432 	if (peer_address.size() != ETH_ALEN) {
1433 		return {createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1434 	}
1435 	if (wpas_p2p_invite(
1436 		wpa_s, peer_address.data(), ssid, NULL, 0, 0, ht40, vht,
1437 		CHANWIDTH_USE_HT, 0, he, 0, is6GhzAllowed(wpa_s))) {
1438 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1439 	}
1440 	return ndk::ScopedAStatus::ok();
1441 }
1442 
configureExtListenInternal(uint32_t period_in_millis,uint32_t interval_in_millis)1443 ndk::ScopedAStatus P2pIface::configureExtListenInternal(
1444 	uint32_t period_in_millis, uint32_t interval_in_millis)
1445 {
1446 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1447 	if (wpas_p2p_ext_listen(wpa_s, period_in_millis, interval_in_millis)) {
1448 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1449 	}
1450 	return ndk::ScopedAStatus::ok();
1451 }
1452 
setListenChannelInternal(uint32_t channel,uint32_t operating_class)1453 ndk::ScopedAStatus P2pIface::setListenChannelInternal(
1454 	uint32_t channel, uint32_t operating_class)
1455 {
1456 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1457 	if (p2p_set_listen_channel(
1458 		wpa_s->global->p2p, operating_class, channel, 1)) {
1459 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1460 	}
1461 	return ndk::ScopedAStatus::ok();
1462 }
1463 
setDisallowedFrequenciesInternal(const std::vector<FreqRange> & ranges)1464 ndk::ScopedAStatus P2pIface::setDisallowedFrequenciesInternal(
1465 	const std::vector<FreqRange>& ranges)
1466 {
1467 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1468 	using DestT = struct wpa_freq_range_list::wpa_freq_range;
1469 	DestT* freq_ranges = nullptr;
1470 	// Empty ranges is used to enable all frequencies.
1471 	if (ranges.size() != 0) {
1472 		freq_ranges = static_cast<DestT*>(
1473 			os_malloc(sizeof(DestT) * ranges.size()));
1474 		if (!freq_ranges) {
1475 			return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1476 		}
1477 		uint32_t i = 0;
1478 		for (const auto& range : ranges) {
1479 			freq_ranges[i].min = range.min;
1480 			freq_ranges[i].max = range.max;
1481 			i++;
1482 		}
1483 	}
1484 
1485 	os_free(wpa_s->global->p2p_disallow_freq.range);
1486 	wpa_s->global->p2p_disallow_freq.range = freq_ranges;
1487 	wpa_s->global->p2p_disallow_freq.num = ranges.size();
1488 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
1489 	return ndk::ScopedAStatus::ok();
1490 }
1491 
getSsidInternal(const std::vector<uint8_t> & peer_address)1492 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> P2pIface::getSsidInternal(
1493 	const std::vector<uint8_t>& peer_address)
1494 {
1495 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1496 	if (peer_address.size() != ETH_ALEN) {
1497 		return {std::vector<uint8_t>(),
1498 			createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1499 	}
1500 	const struct p2p_peer_info* info =
1501 		p2p_get_peer_info(wpa_s->global->p2p, peer_address.data(), 0);
1502 	if (!info) {
1503 		return {std::vector<uint8_t>(),
1504 			createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1505 	}
1506 	const struct p2p_device* dev =
1507 		reinterpret_cast<const struct p2p_device*>(
1508 		(reinterpret_cast<const uint8_t*>(info)) -
1509 		offsetof(struct p2p_device, info));
1510 	std::vector<uint8_t> ssid;
1511 	if (dev && dev->oper_ssid_len) {
1512 		ssid.assign(
1513 			dev->oper_ssid, dev->oper_ssid + dev->oper_ssid_len);
1514 	}
1515 	return {ssid, ndk::ScopedAStatus::ok()};
1516 }
1517 
getGroupCapabilityInternal(const std::vector<uint8_t> & peer_address)1518 std::pair<P2pGroupCapabilityMask, ndk::ScopedAStatus> P2pIface::getGroupCapabilityInternal(
1519 	const std::vector<uint8_t>& peer_address)
1520 {
1521 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1522 	if (peer_address.size() != ETH_ALEN) {
1523 		return {static_cast<P2pGroupCapabilityMask>(0),
1524 			createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1525 	}
1526 	const struct p2p_peer_info* info =
1527 		p2p_get_peer_info(wpa_s->global->p2p, peer_address.data(), 0);
1528 	if (!info) {
1529 		return {static_cast<P2pGroupCapabilityMask>(0),
1530 			createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1531 	}
1532 	return {static_cast<P2pGroupCapabilityMask>(info->group_capab),
1533 		ndk::ScopedAStatus::ok()};
1534 }
1535 
addBonjourServiceInternal(const std::vector<uint8_t> & query,const std::vector<uint8_t> & response)1536 ndk::ScopedAStatus P2pIface::addBonjourServiceInternal(
1537 	const std::vector<uint8_t>& query, const std::vector<uint8_t>& response)
1538 {
1539 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1540 	auto query_buf = misc_utils::convertVectorToWpaBuf(query);
1541 	auto response_buf = misc_utils::convertVectorToWpaBuf(response);
1542 	if (!query_buf || !response_buf) {
1543 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1544 	}
1545 	if (wpas_p2p_service_add_bonjour(
1546 		wpa_s, query_buf.get(), response_buf.get())) {
1547 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1548 	}
1549 	// If successful, the wpabuf is referenced internally and hence should
1550 	// not be freed.
1551 	query_buf.release();
1552 	response_buf.release();
1553 	return ndk::ScopedAStatus::ok();
1554 }
1555 
removeBonjourServiceInternal(const std::vector<uint8_t> & query)1556 ndk::ScopedAStatus P2pIface::removeBonjourServiceInternal(
1557 	const std::vector<uint8_t>& query)
1558 {
1559 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1560 	auto query_buf = misc_utils::convertVectorToWpaBuf(query);
1561 	if (!query_buf) {
1562 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1563 	}
1564 	if (wpas_p2p_service_del_bonjour(wpa_s, query_buf.get())) {
1565 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1566 	}
1567 	return ndk::ScopedAStatus::ok();
1568 }
1569 
addUpnpServiceInternal(uint32_t version,const std::string & service_name)1570 ndk::ScopedAStatus P2pIface::addUpnpServiceInternal(
1571 	uint32_t version, const std::string& service_name)
1572 {
1573 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1574 	if (wpas_p2p_service_add_upnp(wpa_s, version, service_name.c_str())) {
1575 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1576 	}
1577 	return ndk::ScopedAStatus::ok();
1578 }
1579 
removeUpnpServiceInternal(uint32_t version,const std::string & service_name)1580 ndk::ScopedAStatus P2pIface::removeUpnpServiceInternal(
1581 	uint32_t version, const std::string& service_name)
1582 {
1583 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1584 	if (wpas_p2p_service_del_upnp(wpa_s, version, service_name.c_str())) {
1585 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1586 	}
1587 	return ndk::ScopedAStatus::ok();
1588 }
1589 
flushServicesInternal()1590 ndk::ScopedAStatus P2pIface::flushServicesInternal()
1591 {
1592 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1593 	wpas_p2p_service_flush(wpa_s);
1594 	return ndk::ScopedAStatus::ok();
1595 }
1596 
requestServiceDiscoveryInternal(const std::vector<uint8_t> & peer_address,const std::vector<uint8_t> & query)1597 std::pair<uint64_t, ndk::ScopedAStatus> P2pIface::requestServiceDiscoveryInternal(
1598 	const std::vector<uint8_t>& peer_address,
1599 	const std::vector<uint8_t>& query)
1600 {
1601 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1602 	auto query_buf = misc_utils::convertVectorToWpaBuf(query);
1603 	if (!query_buf) {
1604 		return {0, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1605 	}
1606 	if (peer_address.size() != ETH_ALEN) {
1607 		return {0, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1608 	}
1609 	const uint8_t* dst_addr = is_zero_ether_addr(peer_address.data())
1610 					  ? nullptr
1611 					  : peer_address.data();
1612 	uint64_t identifier =
1613 		wpas_p2p_sd_request(wpa_s, dst_addr, query_buf.get());
1614 	if (identifier == 0) {
1615 		return {0, createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1616 	}
1617 	return {identifier, ndk::ScopedAStatus::ok()};
1618 }
1619 
cancelServiceDiscoveryInternal(uint64_t identifier)1620 ndk::ScopedAStatus P2pIface::cancelServiceDiscoveryInternal(uint64_t identifier)
1621 {
1622 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1623 	if (wpas_p2p_sd_cancel_request(wpa_s, identifier)) {
1624 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1625 	}
1626 	return ndk::ScopedAStatus::ok();
1627 }
1628 
setMiracastModeInternal(MiracastMode mode)1629 ndk::ScopedAStatus P2pIface::setMiracastModeInternal(
1630 	MiracastMode mode)
1631 {
1632 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1633 	uint8_t mode_internal = convertAidlMiracastModeToInternal(mode);
1634 	const std::string cmd_str =
1635 		kSetMiracastMode + std::to_string(mode_internal);
1636 	std::vector<char> cmd(
1637 		cmd_str.c_str(), cmd_str.c_str() + cmd_str.size() + 1);
1638 	char driver_cmd_reply_buf[4096] = {};
1639 	if (wpa_drv_driver_cmd(
1640 		wpa_s, cmd.data(), driver_cmd_reply_buf,
1641 		sizeof(driver_cmd_reply_buf))) {
1642 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1643 	}
1644 	return ndk::ScopedAStatus::ok();
1645 }
1646 
startWpsPbcInternal(const std::string & group_ifname,const std::vector<uint8_t> & bssid)1647 ndk::ScopedAStatus P2pIface::startWpsPbcInternal(
1648 	const std::string& group_ifname, const std::vector<uint8_t>& bssid)
1649 {
1650 	struct wpa_supplicant* wpa_group_s =
1651 		retrieveGroupIfacePtr(group_ifname);
1652 	if (!wpa_group_s) {
1653 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
1654 	}
1655 	if (bssid.size() != ETH_ALEN) {
1656 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1657 	}
1658 	const uint8_t* bssid_addr =
1659 		is_zero_ether_addr(bssid.data()) ? nullptr : bssid.data();
1660 #ifdef CONFIG_AP
1661 	if (wpa_group_s->ap_iface) {
1662 		if (wpa_supplicant_ap_wps_pbc(wpa_group_s, bssid_addr, NULL)) {
1663 			return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1664 		}
1665 		return ndk::ScopedAStatus::ok();
1666 	}
1667 #endif /* CONFIG_AP */
1668 	if (wpas_wps_start_pbc(wpa_group_s, bssid_addr, 0, 0)) {
1669 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1670 	}
1671 	return ndk::ScopedAStatus::ok();
1672 }
1673 
startWpsPinKeypadInternal(const std::string & group_ifname,const std::string & pin)1674 ndk::ScopedAStatus P2pIface::startWpsPinKeypadInternal(
1675 	const std::string& group_ifname, const std::string& pin)
1676 {
1677 	struct wpa_supplicant* wpa_group_s =
1678 		retrieveGroupIfacePtr(group_ifname);
1679 	if (!wpa_group_s) {
1680 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
1681 	}
1682 #ifdef CONFIG_AP
1683 	if (wpa_group_s->ap_iface) {
1684 		if (wpa_supplicant_ap_wps_pin(
1685 			wpa_group_s, nullptr, pin.c_str(), nullptr, 0, 0) < 0) {
1686 			return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1687 		}
1688 		return ndk::ScopedAStatus::ok();
1689 	}
1690 #endif /* CONFIG_AP */
1691 	if (wpas_wps_start_pin(
1692 		wpa_group_s, nullptr, pin.c_str(), 0, DEV_PW_DEFAULT)) {
1693 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1694 	}
1695 	return ndk::ScopedAStatus::ok();
1696 }
1697 
startWpsPinDisplayInternal(const std::string & group_ifname,const std::vector<uint8_t> & bssid)1698 std::pair<std::string, ndk::ScopedAStatus> P2pIface::startWpsPinDisplayInternal(
1699 	const std::string& group_ifname, const std::vector<uint8_t>& bssid)
1700 {
1701 	struct wpa_supplicant* wpa_group_s =
1702 		retrieveGroupIfacePtr(group_ifname);
1703 	if (!wpa_group_s) {
1704 		return {"", createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN)};
1705 	}
1706 	if (bssid.size() != ETH_ALEN) {
1707 		return {"", createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1708 	}
1709 	const uint8_t* bssid_addr =
1710 		is_zero_ether_addr(bssid.data()) ? nullptr : bssid.data();
1711 	int pin = wpas_wps_start_pin(
1712 		wpa_group_s, bssid_addr, nullptr, 0, DEV_PW_DEFAULT);
1713 	if (pin < 0) {
1714 		return {"", createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1715 	}
1716 	return {misc_utils::convertWpsPinToString(pin), ndk::ScopedAStatus::ok()};
1717 }
1718 
cancelWpsInternal(const std::string & group_ifname)1719 ndk::ScopedAStatus P2pIface::cancelWpsInternal(const std::string& group_ifname)
1720 {
1721 	struct wpa_supplicant* wpa_group_s =
1722 		retrieveGroupIfacePtr(group_ifname);
1723 	if (!wpa_group_s) {
1724 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_UNKNOWN);
1725 	}
1726 	if (wpas_wps_cancel(wpa_group_s)) {
1727 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1728 	}
1729 	return ndk::ScopedAStatus::ok();
1730 }
1731 
setWpsDeviceNameInternal(const std::string & name)1732 ndk::ScopedAStatus P2pIface::setWpsDeviceNameInternal(const std::string& name)
1733 {
1734 	return iface_config_utils::setWpsDeviceName(retrieveIfacePtr(), name);
1735 }
1736 
setWpsDeviceTypeInternal(const std::vector<uint8_t> & type)1737 ndk::ScopedAStatus P2pIface::setWpsDeviceTypeInternal(
1738 	const std::vector<uint8_t>& type)
1739 {
1740 	std::array<uint8_t, 8> type_arr;
1741 	if (type.size() != 8) {
1742 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1743 	}
1744 	std::copy_n(type.begin(), 8, type_arr.begin());
1745 	return iface_config_utils::setWpsDeviceType(retrieveIfacePtr(), type_arr);
1746 }
1747 
setWpsManufacturerInternal(const std::string & manufacturer)1748 ndk::ScopedAStatus P2pIface::setWpsManufacturerInternal(
1749 	const std::string& manufacturer)
1750 {
1751 	return iface_config_utils::setWpsManufacturer(
1752 		retrieveIfacePtr(), manufacturer);
1753 }
1754 
setWpsModelNameInternal(const std::string & model_name)1755 ndk::ScopedAStatus P2pIface::setWpsModelNameInternal(
1756 	const std::string& model_name)
1757 {
1758 	return iface_config_utils::setWpsModelName(
1759 		retrieveIfacePtr(), model_name);
1760 }
1761 
setWpsModelNumberInternal(const std::string & model_number)1762 ndk::ScopedAStatus P2pIface::setWpsModelNumberInternal(
1763 	const std::string& model_number)
1764 {
1765 	return iface_config_utils::setWpsModelNumber(
1766 		retrieveIfacePtr(), model_number);
1767 }
1768 
setWpsSerialNumberInternal(const std::string & serial_number)1769 ndk::ScopedAStatus P2pIface::setWpsSerialNumberInternal(
1770 	const std::string& serial_number)
1771 {
1772 	return iface_config_utils::setWpsSerialNumber(
1773 		retrieveIfacePtr(), serial_number);
1774 }
1775 
setWpsConfigMethodsInternal(WpsConfigMethods config_methods)1776 ndk::ScopedAStatus P2pIface::setWpsConfigMethodsInternal(WpsConfigMethods config_methods)
1777 {
1778 
1779 	return iface_config_utils::setWpsConfigMethods(
1780 		retrieveIfacePtr(), static_cast<uint16_t>(config_methods));
1781 }
1782 
enableWfdInternal(bool enable)1783 ndk::ScopedAStatus P2pIface::enableWfdInternal(bool enable)
1784 {
1785 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1786 	wifi_display_enable(wpa_s->global, enable);
1787 	return ndk::ScopedAStatus::ok();
1788 }
1789 
setWfdDeviceInfoInternal(const std::vector<uint8_t> & info)1790 ndk::ScopedAStatus P2pIface::setWfdDeviceInfoInternal(
1791 	const std::vector<uint8_t>& info)
1792 {
1793 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1794 	std::vector<char> wfd_device_info_hex(info.size() * 2 + 1);
1795 	wpa_snprintf_hex(
1796 		wfd_device_info_hex.data(), wfd_device_info_hex.size(), info.data(),
1797 		info.size());
1798 	// |wifi_display_subelem_set| expects the first 2 bytes
1799 	// to hold the lenght of the subelement. In this case it's
1800 	// fixed to 6, so prepend that.
1801 	std::string wfd_device_info_set_cmd_str =
1802 		std::to_string(kWfdDeviceInfoSubelemId) + " " +
1803 		kWfdDeviceInfoSubelemLenHexStr + wfd_device_info_hex.data();
1804 	std::vector<char> wfd_device_info_set_cmd(
1805 		wfd_device_info_set_cmd_str.c_str(),
1806 		wfd_device_info_set_cmd_str.c_str() +
1807 		wfd_device_info_set_cmd_str.size() + 1);
1808 	if (wifi_display_subelem_set(
1809 		wpa_s->global, wfd_device_info_set_cmd.data())) {
1810 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1811 	}
1812 	return ndk::ScopedAStatus::ok();
1813 }
1814 
1815 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus>
createNfcHandoverRequestMessageInternal()1816 P2pIface::createNfcHandoverRequestMessageInternal()
1817 {
1818 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1819 	auto buf = misc_utils::createWpaBufUniquePtr(
1820 		wpas_p2p_nfc_handover_req(wpa_s, 1));
1821 	if (!buf) {
1822 		return {std::vector<uint8_t>(),
1823 			createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1824 	}
1825 	return {misc_utils::convertWpaBufToVector(buf.get()),
1826 		ndk::ScopedAStatus::ok()};
1827 }
1828 
1829 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus>
createNfcHandoverSelectMessageInternal()1830 P2pIface::createNfcHandoverSelectMessageInternal()
1831 {
1832 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1833 	auto buf = misc_utils::createWpaBufUniquePtr(
1834 		wpas_p2p_nfc_handover_sel(wpa_s, 1, 0));
1835 	if (!buf) {
1836 		return {std::vector<uint8_t>(),
1837 			createStatus(SupplicantStatusCode::FAILURE_UNKNOWN)};
1838 	}
1839 	return {misc_utils::convertWpaBufToVector(buf.get()),
1840 		ndk::ScopedAStatus::ok()};
1841 }
1842 
reportNfcHandoverResponseInternal(const std::vector<uint8_t> & request)1843 ndk::ScopedAStatus P2pIface::reportNfcHandoverResponseInternal(
1844 	const std::vector<uint8_t>& request)
1845 {
1846 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1847 	auto req = misc_utils::convertVectorToWpaBuf(request);
1848 	auto sel = misc_utils::convertVectorToWpaBuf(std::vector<uint8_t>{0});
1849 	if (!req || !sel) {
1850 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1851 	}
1852 
1853 	if (wpas_p2p_nfc_report_handover(wpa_s, 0, req.get(), sel.get(), 0)) {
1854 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1855 	}
1856 	return ndk::ScopedAStatus::ok();
1857 }
1858 
reportNfcHandoverInitiationInternal(const std::vector<uint8_t> & select)1859 ndk::ScopedAStatus P2pIface::reportNfcHandoverInitiationInternal(
1860 	const std::vector<uint8_t>& select)
1861 {
1862 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1863 	auto req = misc_utils::convertVectorToWpaBuf(std::vector<uint8_t>{0});
1864 	auto sel = misc_utils::convertVectorToWpaBuf(select);
1865 	if (!req || !sel) {
1866 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1867 	}
1868 
1869 	if (wpas_p2p_nfc_report_handover(wpa_s, 1, req.get(), sel.get(), 0)) {
1870 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1871 	}
1872 	return ndk::ScopedAStatus::ok();
1873 }
1874 
saveConfigInternal()1875 ndk::ScopedAStatus P2pIface::saveConfigInternal()
1876 {
1877 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1878 	if (!wpa_s->conf->update_config) {
1879 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1880 	}
1881 	if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1882 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1883 	}
1884 	return ndk::ScopedAStatus::ok();
1885 }
1886 
addGroupInternal(bool persistent,int32_t persistent_network_id)1887 ndk::ScopedAStatus P2pIface::addGroupInternal(
1888 	bool persistent, int32_t persistent_network_id)
1889 {
1890 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1891 	int he = wpa_s->conf->p2p_go_he;
1892 	int vht = wpa_s->conf->p2p_go_vht;
1893 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
1894 	struct wpa_ssid* ssid =
1895 		wpa_config_get_network(wpa_s->conf, persistent_network_id);
1896 	if (ssid == NULL) {
1897 		if (wpas_p2p_group_add(
1898 			wpa_s, persistent, 0, 0, ht40, vht,
1899 			CHANWIDTH_USE_HT, he, 0, is6GhzAllowed(wpa_s))) {
1900 			return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1901 		} else {
1902 			return ndk::ScopedAStatus::ok();
1903 		}
1904 	} else if (ssid->disabled == 2) {
1905 		if (wpas_p2p_group_add_persistent(
1906 			wpa_s, ssid, 0, 0, 0, 0, ht40, vht,
1907 			CHANWIDTH_USE_HT, he, 0, NULL, 0, 0, is6GhzAllowed(wpa_s))) {
1908 			return createStatus(SupplicantStatusCode::FAILURE_NETWORK_UNKNOWN);
1909 		} else {
1910 			return ndk::ScopedAStatus::ok();
1911 		}
1912 	}
1913 	return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1914 }
1915 
addGroupWithConfigInternal(const std::vector<uint8_t> & ssid,const std::string & passphrase,bool persistent,uint32_t freq,const std::vector<uint8_t> & peer_address,bool joinExistingGroup)1916 ndk::ScopedAStatus P2pIface::addGroupWithConfigInternal(
1917 	const std::vector<uint8_t>& ssid, const std::string& passphrase,
1918 	bool persistent, uint32_t freq, const std::vector<uint8_t>& peer_address,
1919 	bool joinExistingGroup)
1920 {
1921 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
1922 	int he = wpa_s->conf->p2p_go_he;
1923 	int vht = wpa_s->conf->p2p_go_vht;
1924 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
1925 
1926 	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
1927 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_DISABLED);
1928 	}
1929 
1930 	if (!isSsidValid(ssid)) {
1931 		return createStatusWithMsg(SupplicantStatusCode::FAILURE_ARGS_INVALID,
1932 			"SSID is invalid.");
1933 	}
1934 
1935 	if (!isPskPassphraseValid(passphrase)) {
1936 		return createStatusWithMsg(SupplicantStatusCode::FAILURE_ARGS_INVALID,
1937 			"Passphrase is invalid.");
1938 	}
1939 
1940 	if (!joinExistingGroup) {
1941 		struct p2p_data *p2p = wpa_s->global->p2p;
1942 		os_memcpy(p2p->ssid, ssid.data(), ssid.size());
1943 		p2p->ssid_len = ssid.size();
1944 		p2p->ssid_set = 1;
1945 
1946 		os_memset(p2p->passphrase, 0, sizeof(p2p->passphrase));
1947 		os_memcpy(p2p->passphrase, passphrase.c_str(), passphrase.length());
1948 		p2p->passphrase_set = 1;
1949 
1950 		if (wpas_p2p_group_add(
1951 			wpa_s, persistent, freq, 0, ht40, vht,
1952 			CHANWIDTH_USE_HT, he, 0, is6GhzAllowed(wpa_s))) {
1953 			return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
1954 		}
1955 		return ndk::ScopedAStatus::ok();
1956 	}
1957 
1958 	// The rest is for group join.
1959 	wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND before group join.");
1960 	wpas_p2p_stop_find(wpa_s);
1961 
1962 	if (peer_address.size() != ETH_ALEN) {
1963 		return createStatusWithMsg(SupplicantStatusCode::FAILURE_ARGS_INVALID,
1964 			"Peer address is invalid.");
1965 	}
1966 
1967 	if (pending_scan_res_join_callback != NULL) {
1968 		wpa_printf(MSG_WARNING, "P2P: Renew scan result callback with new request.");
1969 	}
1970 
1971 	pending_join_scan_callback =
1972 		[wpa_s, ssid, peer_address, freq]() {
1973 		if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
1974 			return;
1975 		}
1976 		int operating_freq = 0;
1977 		struct wpa_bss *bss = findBssBySsidFromAnyInterface(
1978 			wpa_s->global->ifaces, peer_address.data(), ssid.data(), ssid.size());
1979 		if (bss != NULL) {
1980 			wpa_printf(MSG_DEBUG, "P2P: Found Group owner " MACSTR "in scan cache",
1981 				MAC2STR(bss->bssid));
1982 			operating_freq = bss->freq;
1983 		}
1984 
1985 		int ret = joinScanReq(wpa_s, ssid, freq, operating_freq);
1986 		// for BUSY case, the scan might be occupied by WiFi.
1987 		// Do not give up immediately, but try again later.
1988 		if (-EBUSY == ret) {
1989 			// re-schedule this join scan
1990 			eloop_cancel_timeout(joinScanWrapper, wpa_s, NULL);
1991 			eloop_register_timeout(0, P2P_JOIN_SINGLE_CHANNEL_SCAN_INTERVAL_USECS,
1992 					joinScanWrapper, wpa_s, NULL);
1993 		} else if (0 != ret) {
1994 			notifyGroupJoinFailure(wpa_s);
1995 			pending_scan_res_join_callback = NULL;
1996 		}
1997 	};
1998 
1999 	pending_scan_res_join_callback = [wpa_s, ssid, passphrase, peer_address, freq, this]() {
2000 		if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
2001 			return;
2002 		}
2003 
2004 		wpa_printf(MSG_DEBUG, "P2P: Scan results received for join (reinvoke).");
2005 
2006 		struct wpa_bss *bss = findBssBySsid(
2007 			wpa_s, peer_address.data(), ssid.data(), ssid.size());
2008 		if (bss) {
2009 			wpa_s->global->p2p_go_found_external_scan = 1;
2010 			if (0 != joinGroup(wpa_s, bss->bssid, ssid, passphrase)) {
2011 				wpa_printf(MSG_ERROR, "P2P: Failed to join a group.");
2012 				wpa_s->global->p2p_go_found_external_scan = 0;
2013 			}
2014 			// no need to notify group join failure here,
2015 			// it will be handled by wpas_p2p_group_add_persistent
2016 			// called in joinGroup.
2017 			pending_scan_res_join_callback = NULL;
2018 			return;
2019 		}
2020 		wpa_printf(MSG_DEBUG, "P2P: Join scan count %d.", wpa_s->p2p_join_scan_count);
2021 		eloop_cancel_timeout(joinScanWrapper, wpa_s, NULL);
2022 		if (wpa_s->p2p_join_scan_count < P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2023 			wpa_printf(MSG_DEBUG, "P2P: Try join again later.");
2024 			eloop_register_timeout(0, getP2pJoinScanIntervalUsecs(freq),
2025 				joinScanWrapper, wpa_s, this);
2026 			return;
2027 		}
2028 
2029 		wpa_printf(MSG_ERROR, "P2P: Failed to find the group with "
2030 			"network name %s - stop join attempt",
2031 			wpa_ssid_txt(ssid.data(), ssid.size()));
2032 		notifyGroupJoinFailure(wpa_s);
2033 		pending_scan_res_join_callback = NULL;
2034 	};
2035 
2036 	wpa_s->p2p_join_scan_count = 0;
2037 	pending_join_scan_callback();
2038 	if (pending_scan_res_join_callback == NULL) {
2039 		return createStatusWithMsg(SupplicantStatusCode::FAILURE_UNKNOWN,
2040 			"Failed to start scan.");
2041 	}
2042 	return ndk::ScopedAStatus::ok();
2043 }
2044 
setMacRandomizationInternal(bool enable)2045 ndk::ScopedAStatus P2pIface::setMacRandomizationInternal(bool enable)
2046 {
2047 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
2048 	bool currentEnabledState = !!wpa_s->conf->p2p_device_random_mac_addr;
2049 	u8 *addr = NULL;
2050 
2051 	// The same state, no change is needed.
2052 	if (currentEnabledState == enable) {
2053 		wpa_printf(MSG_DEBUG, "The random MAC is %s already.",
2054 			(enable) ? "enabled" : "disabled");
2055 		return ndk::ScopedAStatus::ok();
2056 	}
2057 
2058 	if (enable) {
2059 		wpa_s->conf->p2p_device_random_mac_addr = 1;
2060 		wpa_s->conf->p2p_interface_random_mac_addr = 1;
2061 
2062 		// restore config if it failed to set up MAC address.
2063 		if (wpas_p2p_mac_setup(wpa_s) < 0) {
2064 			wpa_s->conf->p2p_device_random_mac_addr = 0;
2065 			wpa_s->conf->p2p_interface_random_mac_addr = 0;
2066 			return createStatusWithMsg(SupplicantStatusCode::FAILURE_UNKNOWN,
2067 				"Failed to set up MAC address.");
2068 		}
2069 	} else {
2070 		// disable random MAC will use original MAC address
2071 		// regardless of any saved persistent groups.
2072 		if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
2073 			wpa_printf(MSG_ERROR, "Failed to restore MAC address");
2074 			return createStatusWithMsg(SupplicantStatusCode::FAILURE_UNKNOWN,
2075 				"Failed to restore MAC address.");
2076 		}
2077 
2078 		if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
2079 			wpa_printf(MSG_INFO, "Could not update MAC address information");
2080 			return createStatusWithMsg(SupplicantStatusCode::FAILURE_UNKNOWN,
2081 				"Failed to update MAC address.");
2082 		}
2083 		wpa_s->conf->p2p_device_random_mac_addr = 0;
2084 		wpa_s->conf->p2p_interface_random_mac_addr = 0;
2085 	}
2086 
2087 	// update internal data to send out correct device address in action frame.
2088 	os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2089 	os_memcpy(wpa_s->global->p2p->cfg->dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2090 
2091 	return ndk::ScopedAStatus::ok();
2092 }
2093 
setEdmgInternal(bool enable)2094 ndk::ScopedAStatus P2pIface::setEdmgInternal(bool enable)
2095 {
2096 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
2097 	wpa_printf(MSG_DEBUG, "set p2p_go_edmg to %d", enable);
2098 	wpa_s->conf->p2p_go_edmg = enable ? 1 : 0;
2099 	wpa_s->p2p_go_edmg = enable ? 1 : 0;
2100 	return ndk::ScopedAStatus::ok();
2101 }
2102 
getEdmgInternal()2103 std::pair<bool, ndk::ScopedAStatus> P2pIface::getEdmgInternal()
2104 {
2105 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
2106 	return {(wpa_s->p2p_go_edmg == 1), ndk::ScopedAStatus::ok()};
2107 }
2108 
setWfdR2DeviceInfoInternal(const std::vector<uint8_t> & info)2109 ndk::ScopedAStatus P2pIface::setWfdR2DeviceInfoInternal(
2110 	const std::vector<uint8_t>& info)
2111 {
2112 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
2113 	uint32_t wfd_r2_device_info_hex_len = info.size() * 2 + 1;
2114 	std::vector<char> wfd_r2_device_info_hex(wfd_r2_device_info_hex_len);
2115 	wpa_snprintf_hex(
2116 		wfd_r2_device_info_hex.data(), wfd_r2_device_info_hex.size(),
2117 		info.data(),info.size());
2118 	std::string wfd_r2_device_info_set_cmd_str =
2119 		 std::to_string(kWfdR2DeviceInfoSubelemId) + " " +
2120 		 wfd_r2_device_info_hex.data();
2121 	std::vector<char> wfd_r2_device_info_set_cmd(
2122 		 wfd_r2_device_info_set_cmd_str.c_str(),
2123 		 wfd_r2_device_info_set_cmd_str.c_str() +
2124 		 wfd_r2_device_info_set_cmd_str.size() + 1);
2125 	if (wifi_display_subelem_set(
2126 		wpa_s->global, wfd_r2_device_info_set_cmd.data())) {
2127 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
2128 	}
2129 	return ndk::ScopedAStatus::ok();
2130 }
2131 
removeClientInternal(const std::vector<uint8_t> & peer_address,bool isLegacyClient)2132 ndk::ScopedAStatus P2pIface::removeClientInternal(
2133     const std::vector<uint8_t>& peer_address, bool isLegacyClient)
2134 {
2135 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
2136 	if (peer_address.size() != ETH_ALEN) {
2137 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
2138 	}
2139 	wpas_p2p_remove_client(wpa_s, peer_address.data(), isLegacyClient? 1 : 0);
2140 	return ndk::ScopedAStatus::ok();
2141 }
2142 
findOnSocialChannelsInternal(uint32_t timeout_in_sec)2143 ndk::ScopedAStatus P2pIface::findOnSocialChannelsInternal(uint32_t timeout_in_sec)
2144 {
2145 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
2146 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2147 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_DISABLED);
2148 	}
2149 	uint32_t search_delay = wpas_p2p_search_delay(wpa_s);
2150 	if (wpas_p2p_find(
2151 		wpa_s, timeout_in_sec, P2P_FIND_ONLY_SOCIAL, 0, nullptr,
2152 		nullptr, search_delay, 0, nullptr, 0, is6GhzAllowed(wpa_s))) {
2153 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
2154 	}
2155 	return ndk::ScopedAStatus::ok();
2156 }
2157 
findOnSpecificFrequencyInternal(uint32_t freq,uint32_t timeout_in_sec)2158 ndk::ScopedAStatus P2pIface::findOnSpecificFrequencyInternal(
2159 	uint32_t freq, uint32_t timeout_in_sec)
2160 {
2161 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
2162 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2163 		return createStatus(SupplicantStatusCode::FAILURE_IFACE_DISABLED);
2164 	}
2165 	uint32_t search_delay = wpas_p2p_search_delay(wpa_s);
2166 	if (wpas_p2p_find(
2167 		wpa_s, timeout_in_sec, P2P_FIND_START_WITH_FULL, 0, nullptr,
2168 		nullptr, search_delay, 0, nullptr, freq, is6GhzAllowed(wpa_s))) {
2169 		return createStatus(SupplicantStatusCode::FAILURE_UNKNOWN);
2170 	}
2171 	return ndk::ScopedAStatus::ok();
2172 }
2173 
setVendorElementsInternal(P2pFrameTypeMask frameTypeMask,const std::vector<uint8_t> & vendorElemBytes)2174 ndk::ScopedAStatus P2pIface::setVendorElementsInternal(
2175 	P2pFrameTypeMask frameTypeMask,
2176 	const std::vector<uint8_t>& vendorElemBytes)
2177 {
2178 	struct wpa_supplicant* wpa_s = retrieveIfacePtr();
2179 	for (int i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
2180 		uint32_t bit = convertWpaP2pFrameTypeToHalP2pFrameTypeBit(i);
2181 		if (0 == bit) continue;
2182 
2183 		if (static_cast<uint32_t>(frameTypeMask) & bit) {
2184 			updateP2pVendorElem(wpa_s, (enum wpa_vendor_elem_frame) i, vendorElemBytes);
2185 		}
2186 	}
2187 	return ndk::ScopedAStatus::ok();
2188 }
2189 
2190 /**
2191  * Retrieve the underlying |wpa_supplicant| struct
2192  * pointer for this iface.
2193  * If the underlying iface is removed, then all RPC method calls on this object
2194  * will return failure.
2195  */
retrieveIfacePtr()2196 wpa_supplicant* P2pIface::retrieveIfacePtr()
2197 {
2198 	return wpa_supplicant_get_iface(wpa_global_, ifname_.c_str());
2199 }
2200 
2201 /**
2202  * Retrieve the underlying |wpa_supplicant| struct
2203  * pointer for this group iface.
2204  */
retrieveGroupIfacePtr(const std::string & group_ifname)2205 wpa_supplicant* P2pIface::retrieveGroupIfacePtr(const std::string& group_ifname)
2206 {
2207 	return wpa_supplicant_get_iface(wpa_global_, group_ifname.c_str());
2208 }
2209 
2210 }  // namespace supplicant
2211 }  // namespace wifi
2212 }  // namespace hardware
2213 }  // namespace android
2214 }  // namespace aidl
2215