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