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_2 {
85 namespace implementation {
86 namespace iface_config_utils {
setWpsDeviceName(struct wpa_supplicant * wpa_s,const std::string & name)87 SupplicantStatus setWpsDeviceName(
88     struct wpa_supplicant* wpa_s, const std::string& name)
89 {
90 	WPA_ASSERT(wpa_s);
91 	if (freeAndSetStringConfigParam(
92 		wpa_s, name, kMaxWpsDeviceNameSize, CFG_CHANGED_DEVICE_NAME,
93 		&wpa_s->conf->device_name)) {
94 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
95 	}
96 	return {SupplicantStatusCode::SUCCESS, ""};
97 }
98 
setWpsDeviceType(struct wpa_supplicant * wpa_s,const std::array<uint8_t,8> & type)99 SupplicantStatus setWpsDeviceType(
100     struct wpa_supplicant* wpa_s, const std::array<uint8_t, 8>& type)
101 {
102 	WPA_ASSERT(wpa_s);
103 	WPA_ASSERT(type.size() == WPS_DEV_TYPE_LEN);
104 	os_memcpy(wpa_s->conf->device_type, type.data(), WPS_DEV_TYPE_LEN);
105 	processConfigUpdate(wpa_s, CFG_CHANGED_DEVICE_TYPE);
106 	return {SupplicantStatusCode::SUCCESS, ""};
107 }
108 
setWpsManufacturer(struct wpa_supplicant * wpa_s,const std::string & manufacturer)109 SupplicantStatus setWpsManufacturer(
110     struct wpa_supplicant* wpa_s, const std::string& manufacturer)
111 {
112 	WPA_ASSERT(wpa_s);
113 	if (freeAndSetStringConfigParam(
114 		wpa_s, manufacturer, kMaxWpsManufacturerSize,
115 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->manufacturer)) {
116 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
117 	}
118 	return {SupplicantStatusCode::SUCCESS, ""};
119 }
120 
setWpsModelName(struct wpa_supplicant * wpa_s,const std::string & model_name)121 SupplicantStatus setWpsModelName(
122     struct wpa_supplicant* wpa_s, const std::string& model_name)
123 {
124 	WPA_ASSERT(wpa_s);
125 	if (freeAndSetStringConfigParam(
126 		wpa_s, model_name, kMaxWpsModelNameSize, CFG_CHANGED_WPS_STRING,
127 		&wpa_s->conf->model_name)) {
128 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
129 	}
130 	return {SupplicantStatusCode::SUCCESS, ""};
131 }
132 
setWpsModelNumber(struct wpa_supplicant * wpa_s,const std::string & model_number)133 SupplicantStatus setWpsModelNumber(
134     struct wpa_supplicant* wpa_s, const std::string& model_number)
135 {
136 	WPA_ASSERT(wpa_s);
137 	if (freeAndSetStringConfigParam(
138 		wpa_s, model_number, kMaxWpsModelNumberSize,
139 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->model_number)) {
140 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
141 	}
142 	return {SupplicantStatusCode::SUCCESS, ""};
143 }
144 
setWpsSerialNumber(struct wpa_supplicant * wpa_s,const std::string & serial_number)145 SupplicantStatus setWpsSerialNumber(
146     struct wpa_supplicant* wpa_s, const std::string& serial_number)
147 {
148 	WPA_ASSERT(wpa_s);
149 	if (freeAndSetStringConfigParam(
150 		wpa_s, serial_number, kMaxWpsSerialNumberSize,
151 		CFG_CHANGED_WPS_STRING, &wpa_s->conf->serial_number)) {
152 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
153 	}
154 	return {SupplicantStatusCode::SUCCESS, ""};
155 }
156 
setWpsConfigMethods(struct wpa_supplicant * wpa_s,uint16_t config_methods)157 SupplicantStatus setWpsConfigMethods(
158     struct wpa_supplicant* wpa_s, uint16_t config_methods)
159 {
160 	WPA_ASSERT(wpa_s);
161 	if (freeAndSetStringConfigParam(
162 		wpa_s, convertWpsConfigMethodsMaskToString(config_methods),
163 		UINT32_MAX, CFG_CHANGED_CONFIG_METHODS,
164 		&wpa_s->conf->config_methods)) {
165 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
166 	}
167 	return {SupplicantStatusCode::SUCCESS, ""};
168 }
169 
setExternalSim(struct wpa_supplicant * wpa_s,bool useExternalSim)170 SupplicantStatus setExternalSim(
171     struct wpa_supplicant* wpa_s, bool useExternalSim)
172 {
173 	WPA_ASSERT(wpa_s);
174 	wpa_s->conf->external_sim = useExternalSim ? 1 : 0;
175 	return {SupplicantStatusCode::SUCCESS, ""};
176 }
177 }  // namespace iface_config_utils
178 }  // namespace implementation
179 }  // namespace V1_2
180 }  // namespace supplicant
181 }  // namespace wifi
182 }  // namespace hardware
183 }  // namespace android
184