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