1 /*
2  * hidl interface for wpa_supplicant daemon
3  * Copyright (struct wpa_supplicant* wpa_s, c) 2004-2016, Jouni Malinen
4  * <j@w1.fi>
5  * Copyright (struct wpa_supplicant* wpa_s, c) 2004-2016, Roshan Pius
6  * <rpius@google.com>
7  *
8  * This software may be distributed under the terms of the BSD license.
9  * See README for more details.
10  */
11 
12 #include "hidl_manager.h"
13 #include "hidl_return_util.h"
14 #include "iface_config_utils.h"
15 
16 namespace {
17 using android::hardware::wifi::supplicant::V1_0::SupplicantStatus;
18 using android::hardware::wifi::supplicant::V1_0::SupplicantStatusCode;
19 
20 constexpr uint32_t kMaxWpsDeviceNameSize = WPS_DEV_NAME_MAX_LEN;
21 constexpr uint32_t kMaxWpsManufacturerSize = WPS_MANUFACTURER_MAX_LEN;
22 constexpr uint32_t kMaxWpsModelNameSize = WPS_MODEL_NAME_MAX_LEN;
23 constexpr uint32_t kMaxWpsModelNumberSize = WPS_MODEL_NUMBER_MAX_LEN;
24 constexpr uint32_t kMaxWpsSerialNumberSize = WPS_SERIAL_NUMBER_MAX_LEN;
25 
processConfigUpdate(struct wpa_supplicant * wpa_s,uint32_t changed_param)26 void processConfigUpdate(struct wpa_supplicant* wpa_s, uint32_t changed_param)
27 {
28 	wpa_s->conf->changed_parameters |= changed_param;
29 	wpa_supplicant_update_config(wpa_s);
30 }
31 
32 // Free any existing pointer stored in |dst| and store the provided string value
33 // there.
freeAndSetStringConfigParam(struct wpa_supplicant * wpa_s,const std::string & value,uint32_t max_size,uint32_t changed_param,char ** dst)34 int freeAndSetStringConfigParam(
35     struct wpa_supplicant* wpa_s, const std::string& value, uint32_t max_size,
36     uint32_t changed_param, char** dst)
37 {
38 	if (value.size() > max_size) {
39 		return -1;
40 	}
41 	WPA_ASSERT(dst);
42 	os_free(static_cast<void*>(*dst));
43 	*dst = os_strdup(value.c_str());
44 	processConfigUpdate(wpa_s, changed_param);
45 	return 0;
46 }
47 
convertWpsConfigMethodsMaskToString(uint16_t config_methods)48 std::string convertWpsConfigMethodsMaskToString(uint16_t config_methods)
49 {
50 	using WpsConfigMethods =
51 	    android::hardware::wifi::supplicant::V1_0::WpsConfigMethods;
52 	std::string config_methods_str;
53 	for (const auto& flag_and_name :
54 	     {std::make_pair(WpsConfigMethods::USBA, "usba"),
55 	      {WpsConfigMethods::ETHERNET, "ethernet"},
56 	      {WpsConfigMethods::LABEL, "label"},
57 	      {WpsConfigMethods::DISPLAY, "display"},
58 	      {WpsConfigMethods::INT_NFC_TOKEN, "int_nfc_token"},
59 	      {WpsConfigMethods::EXT_NFC_TOKEN, "ext_nfc_token"},
60 	      {WpsConfigMethods::NFC_INTERFACE, "nfc_interface"},
61 	      {WpsConfigMethods::PUSHBUTTON, "push_button"},
62 	      {WpsConfigMethods::KEYPAD, "keypad"},
63 	      {WpsConfigMethods::VIRT_PUSHBUTTON, "virtual_push_button"},
64 	      {WpsConfigMethods::PHY_PUSHBUTTON, "physical_push_button"},
65 	      {WpsConfigMethods::P2PS, "p2ps"},
66 	      {WpsConfigMethods::VIRT_DISPLAY, "virtual_display"},
67 	      {WpsConfigMethods::PHY_DISPLAY, "physical_display"}}) {
68 		const auto flag =
69 		    static_cast<std::underlying_type<WpsConfigMethods>::type>(
70 			flag_and_name.first);
71 		if ((config_methods & flag) == flag) {
72 			config_methods_str += flag_and_name.second;
73 			config_methods_str += " ";
74 		}
75 	}
76 	return config_methods_str;
77 }
78 }  // namespace
79 
80 namespace android {
81 namespace hardware {
82 namespace wifi {
83 namespace supplicant {
84 namespace V1_4 {
85 namespace implementation {
86 namespace iface_config_utils {
87 using V1_0::SupplicantStatusCode;
88 
setWpsDeviceName(struct wpa_supplicant * wpa_s,const std::string & name)89 SupplicantStatus setWpsDeviceName(
90     struct wpa_supplicant* wpa_s, const std::string& name)
91 {
92 	WPA_ASSERT(wpa_s);
93 	if (freeAndSetStringConfigParam(
94 		wpa_s, name, kMaxWpsDeviceNameSize, CFG_CHANGED_DEVICE_NAME,
95 		&wpa_s->conf->device_name)) {
96 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
97 	}
98 	return {SupplicantStatusCode::SUCCESS, ""};
99 }
100 
setWpsDeviceType(struct wpa_supplicant * wpa_s,const std::array<uint8_t,8> & type)101 SupplicantStatus setWpsDeviceType(
102     struct wpa_supplicant* wpa_s, const std::array<uint8_t, 8>& type)
103 {
104 	WPA_ASSERT(wpa_s);
105 	WPA_ASSERT(type.size() == WPS_DEV_TYPE_LEN);
106 	os_memcpy(wpa_s->conf->device_type, type.data(), WPS_DEV_TYPE_LEN);
107 	processConfigUpdate(wpa_s, CFG_CHANGED_DEVICE_TYPE);
108 	return {SupplicantStatusCode::SUCCESS, ""};
109 }
110 
setWpsManufacturer(struct wpa_supplicant * wpa_s,const std::string & manufacturer)111 SupplicantStatus setWpsManufacturer(
112     struct wpa_supplicant* wpa_s, const std::string& manufacturer)
113 {
114 	WPA_ASSERT(wpa_s);
115 	if (freeAndSetStringConfigParam(
116 		wpa_s, manufacturer, kMaxWpsManufacturerSize,
117 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->manufacturer)) {
118 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
119 	}
120 	return {SupplicantStatusCode::SUCCESS, ""};
121 }
122 
setWpsModelName(struct wpa_supplicant * wpa_s,const std::string & model_name)123 SupplicantStatus setWpsModelName(
124     struct wpa_supplicant* wpa_s, const std::string& model_name)
125 {
126 	WPA_ASSERT(wpa_s);
127 	if (freeAndSetStringConfigParam(
128 		wpa_s, model_name, kMaxWpsModelNameSize, CFG_CHANGED_WPS_STRING,
129 		&wpa_s->conf->model_name)) {
130 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
131 	}
132 	return {SupplicantStatusCode::SUCCESS, ""};
133 }
134 
setWpsModelNumber(struct wpa_supplicant * wpa_s,const std::string & model_number)135 SupplicantStatus setWpsModelNumber(
136     struct wpa_supplicant* wpa_s, const std::string& model_number)
137 {
138 	WPA_ASSERT(wpa_s);
139 	if (freeAndSetStringConfigParam(
140 		wpa_s, model_number, kMaxWpsModelNumberSize,
141 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->model_number)) {
142 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
143 	}
144 	return {SupplicantStatusCode::SUCCESS, ""};
145 }
146 
setWpsSerialNumber(struct wpa_supplicant * wpa_s,const std::string & serial_number)147 SupplicantStatus setWpsSerialNumber(
148     struct wpa_supplicant* wpa_s, const std::string& serial_number)
149 {
150 	WPA_ASSERT(wpa_s);
151 	if (freeAndSetStringConfigParam(
152 		wpa_s, serial_number, kMaxWpsSerialNumberSize,
153 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->serial_number)) {
154 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
155 	}
156 	return {SupplicantStatusCode::SUCCESS, ""};
157 }
158 
setWpsConfigMethods(struct wpa_supplicant * wpa_s,uint16_t config_methods)159 SupplicantStatus setWpsConfigMethods(
160     struct wpa_supplicant* wpa_s, uint16_t config_methods)
161 {
162 	WPA_ASSERT(wpa_s);
163 	if (freeAndSetStringConfigParam(
164 		wpa_s, convertWpsConfigMethodsMaskToString(config_methods),
165 		UINT32_MAX, CFG_CHANGED_CONFIG_METHODS,
166 		&wpa_s->conf->config_methods)) {
167 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
168 	}
169 	return {SupplicantStatusCode::SUCCESS, ""};
170 }
171 
setExternalSim(struct wpa_supplicant * wpa_s,bool useExternalSim)172 SupplicantStatus setExternalSim(
173     struct wpa_supplicant* wpa_s, bool useExternalSim)
174 {
175 	WPA_ASSERT(wpa_s);
176 	wpa_s->conf->external_sim = useExternalSim ? 1 : 0;
177 	return {SupplicantStatusCode::SUCCESS, ""};
178 }
179 }  // namespace iface_config_utils
180 }  // namespace implementation
181 }  // namespace V1_4
182 }  // namespace supplicant
183 }  // namespace wifi
184 }  // namespace hardware
185 }  // namespace android
186