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