xref: /aosp_15_r20/external/wpa_supplicant_8/wpa_supplicant/aidl/vendor/iface_config_utils.cpp (revision 03f9172ca588f91df233974f4258bab95191f931)
1 /*
2  * WPA Supplicant - Iface configuration methods
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 
14 namespace {
15 using aidl::android::hardware::wifi::supplicant::SupplicantStatusCode;
16 using aidl::android::hardware::wifi::supplicant::WpsConfigMethods;
17 
18 constexpr uint32_t kMaxWpsDeviceNameSize = WPS_DEV_NAME_MAX_LEN;
19 constexpr uint32_t kMaxWpsManufacturerSize = WPS_MANUFACTURER_MAX_LEN;
20 constexpr uint32_t kMaxWpsModelNameSize = WPS_MODEL_NAME_MAX_LEN;
21 constexpr uint32_t kMaxWpsModelNumberSize = WPS_MODEL_NUMBER_MAX_LEN;
22 constexpr uint32_t kMaxWpsSerialNumberSize = WPS_SERIAL_NUMBER_MAX_LEN;
23 
processConfigUpdate(struct wpa_supplicant * wpa_s,uint32_t changed_param)24 void processConfigUpdate(struct wpa_supplicant* wpa_s, uint32_t changed_param)
25 {
26 	wpa_s->conf->changed_parameters |= changed_param;
27 	wpa_supplicant_update_config(wpa_s);
28 }
29 
30 // Free any existing pointer stored in |dst| and store the provided string value
31 // there.
freeAndSetStringConfigParam(struct wpa_supplicant * wpa_s,const std::string & value,uint32_t max_size,uint32_t changed_param,char ** dst)32 int freeAndSetStringConfigParam(
33 	struct wpa_supplicant* wpa_s, const std::string& value, uint32_t max_size,
34 	uint32_t changed_param, char** dst)
35 {
36 	if (value.size() > max_size) {
37 		return -1;
38 	}
39 	WPA_ASSERT(dst);
40 	os_free(static_cast<void*>(*dst));
41 	*dst = os_strdup(value.c_str());
42 	processConfigUpdate(wpa_s, changed_param);
43 	return 0;
44 }
45 
convertWpsConfigMethodsMaskToString(uint16_t config_methods)46 std::string convertWpsConfigMethodsMaskToString(uint16_t config_methods)
47 {
48 	std::string config_methods_str;
49 	for (const auto& flag_and_name :
50 		 {std::make_pair(WpsConfigMethods::USBA, "usba"),
51 		  {WpsConfigMethods::ETHERNET, "ethernet"},
52 		  {WpsConfigMethods::LABEL, "label"},
53 		  {WpsConfigMethods::DISPLAY, "display"},
54 		  {WpsConfigMethods::INT_NFC_TOKEN, "int_nfc_token"},
55 		  {WpsConfigMethods::EXT_NFC_TOKEN, "ext_nfc_token"},
56 		  {WpsConfigMethods::NFC_INTERFACE, "nfc_interface"},
57 		  {WpsConfigMethods::PUSHBUTTON, "push_button"},
58 		  {WpsConfigMethods::KEYPAD, "keypad"},
59 		  {WpsConfigMethods::VIRT_PUSHBUTTON, "virtual_push_button"},
60 		  {WpsConfigMethods::PHY_PUSHBUTTON, "physical_push_button"},
61 		  {WpsConfigMethods::P2PS, "p2ps"},
62 		  {WpsConfigMethods::VIRT_DISPLAY, "virtual_display"},
63 		  {WpsConfigMethods::PHY_DISPLAY, "physical_display"}}) {
64 		const auto flag =
65 			static_cast<std::underlying_type<WpsConfigMethods>::type>(
66 			flag_and_name.first);
67 		if ((config_methods & flag) == flag) {
68 			config_methods_str += flag_and_name.second;
69 			config_methods_str += " ";
70 		}
71 	}
72 	return config_methods_str;
73 }
74 }  // namespace
75 
76 namespace aidl {
77 namespace android {
78 namespace hardware {
79 namespace wifi {
80 namespace supplicant {
81 namespace iface_config_utils {
82 using misc_utils::createStatus;
83 
setWpsDeviceName(struct wpa_supplicant * wpa_s,const std::string & name)84 ndk::ScopedAStatus setWpsDeviceName(
85 	struct wpa_supplicant* wpa_s, const std::string& name)
86 {
87 	WPA_ASSERT(wpa_s);
88 	if (freeAndSetStringConfigParam(
89 		wpa_s, name, kMaxWpsDeviceNameSize, CFG_CHANGED_DEVICE_NAME,
90 		&wpa_s->conf->device_name)) {
91 		return createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID);
92 	}
93 	return ndk::ScopedAStatus::ok();
94 }
95 
setWpsDeviceType(struct wpa_supplicant * wpa_s,const std::array<uint8_t,WPS_DEV_TYPE_LEN> & type)96 ndk::ScopedAStatus setWpsDeviceType(
97 	struct wpa_supplicant* wpa_s, const std::array<uint8_t, WPS_DEV_TYPE_LEN>& type)
98 {
99 	WPA_ASSERT(wpa_s);
100 	WPA_ASSERT(type.size() == WPS_DEV_TYPE_LEN);
101 	os_memcpy(wpa_s->conf->device_type, type.data(), WPS_DEV_TYPE_LEN);
102 	processConfigUpdate(wpa_s, CFG_CHANGED_DEVICE_TYPE);
103 	return ndk::ScopedAStatus::ok();
104 }
105 
setWpsManufacturer(struct wpa_supplicant * wpa_s,const std::string & manufacturer)106 ndk::ScopedAStatus setWpsManufacturer(
107 	struct wpa_supplicant* wpa_s, const std::string& manufacturer)
108 {
109 	WPA_ASSERT(wpa_s);
110 	if (freeAndSetStringConfigParam(
111 		wpa_s, manufacturer, kMaxWpsManufacturerSize,
112 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->manufacturer)) {
113 		return createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID);
114 	}
115 	return ndk::ScopedAStatus::ok();
116 }
117 
setWpsModelName(struct wpa_supplicant * wpa_s,const std::string & model_name)118 ndk::ScopedAStatus setWpsModelName(
119 	struct wpa_supplicant* wpa_s, const std::string& model_name)
120 {
121 	WPA_ASSERT(wpa_s);
122 	if (freeAndSetStringConfigParam(
123 		wpa_s, model_name, kMaxWpsModelNameSize, CFG_CHANGED_WPS_STRING,
124 		&wpa_s->conf->model_name)) {
125 		return createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID);
126 	}
127 	return ndk::ScopedAStatus::ok();
128 }
129 
setWpsModelNumber(struct wpa_supplicant * wpa_s,const std::string & model_number)130 ndk::ScopedAStatus setWpsModelNumber(
131 	struct wpa_supplicant* wpa_s, const std::string& model_number)
132 {
133 	WPA_ASSERT(wpa_s);
134 	if (freeAndSetStringConfigParam(
135 		wpa_s, model_number, kMaxWpsModelNumberSize,
136 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->model_number)) {
137 		return createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID);
138 	}
139 	return ndk::ScopedAStatus::ok();
140 }
141 
setWpsSerialNumber(struct wpa_supplicant * wpa_s,const std::string & serial_number)142 ndk::ScopedAStatus setWpsSerialNumber(
143 	struct wpa_supplicant* wpa_s, const std::string& serial_number)
144 {
145 	WPA_ASSERT(wpa_s);
146 	if (freeAndSetStringConfigParam(
147 		wpa_s, serial_number, kMaxWpsSerialNumberSize,
148 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->serial_number)) {
149 		return createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID);
150 	}
151 	return ndk::ScopedAStatus::ok();
152 }
153 
setWpsConfigMethods(struct wpa_supplicant * wpa_s,uint16_t config_methods)154 ndk::ScopedAStatus setWpsConfigMethods(
155 	struct wpa_supplicant* wpa_s, uint16_t config_methods)
156 {
157 	WPA_ASSERT(wpa_s);
158 	if (freeAndSetStringConfigParam(
159 		wpa_s, convertWpsConfigMethodsMaskToString(config_methods),
160 		UINT32_MAX, CFG_CHANGED_CONFIG_METHODS,
161 		&wpa_s->conf->config_methods)) {
162 		return createStatus(SupplicantStatusCode::FAILURE_ARGS_INVALID);
163 	}
164 	return ndk::ScopedAStatus::ok();
165 }
166 
setExternalSim(struct wpa_supplicant * wpa_s,bool useExternalSim)167 ndk::ScopedAStatus setExternalSim(
168 	struct wpa_supplicant* wpa_s, bool useExternalSim)
169 {
170 	WPA_ASSERT(wpa_s);
171 	wpa_s->conf->external_sim = useExternalSim ? 1 : 0;
172 	return ndk::ScopedAStatus::ok();
173 }
174 }  // namespace iface_config_utils
175 }  // namespace supplicant
176 }  // namespace wifi
177 }  // namespace hardware
178 }  // namespace android
179 }  // namespace aidl
180