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 constexpr uint32_t kMaxWpsDeviceNameSize = WPS_DEV_NAME_MAX_LEN;
18 constexpr uint32_t kMaxWpsManufacturerSize = WPS_MANUFACTURER_MAX_LEN;
19 constexpr uint32_t kMaxWpsModelNameSize = WPS_MODEL_NAME_MAX_LEN;
20 constexpr uint32_t kMaxWpsModelNumberSize = WPS_MODEL_NUMBER_MAX_LEN;
21 constexpr uint32_t kMaxWpsSerialNumberSize = WPS_SERIAL_NUMBER_MAX_LEN;
22 
processConfigUpdate(struct wpa_supplicant * wpa_s,uint32_t changed_param)23 void processConfigUpdate(struct wpa_supplicant* wpa_s, uint32_t changed_param)
24 {
25 	wpa_s->conf->changed_parameters |= changed_param;
26 	wpa_supplicant_update_config(wpa_s);
27 }
28 
29 // Free any existing pointer stored in |dst| and store the provided string value
30 // there.
freeAndSetStringConfigParam(struct wpa_supplicant * wpa_s,const std::string & value,uint32_t max_size,uint32_t changed_param,char ** dst)31 int freeAndSetStringConfigParam(
32     struct wpa_supplicant* wpa_s, const std::string& value, uint32_t max_size,
33     uint32_t changed_param, char** dst)
34 {
35 	if (value.size() > max_size) {
36 		return -1;
37 	}
38 	WPA_ASSERT(dst);
39 	os_free(static_cast<void*>(*dst));
40 	*dst = os_strdup(value.c_str());
41 	processConfigUpdate(wpa_s, changed_param);
42 	return 0;
43 }
44 
convertWpsConfigMethodsMaskToString(uint16_t config_methods)45 std::string convertWpsConfigMethodsMaskToString(uint16_t config_methods)
46 {
47 	using WpsConfigMethods =
48 	    android::hardware::wifi::supplicant::V1_0::WpsConfigMethods;
49 	std::string config_methods_str;
50 	for (const auto& flag_and_name :
51 	     {std::make_pair(WpsConfigMethods::USBA, "usba"),
52 	      {WpsConfigMethods::ETHERNET, "ethernet"},
53 	      {WpsConfigMethods::LABEL, "label"},
54 	      {WpsConfigMethods::DISPLAY, "display"},
55 	      {WpsConfigMethods::INT_NFC_TOKEN, "int_nfc_token"},
56 	      {WpsConfigMethods::EXT_NFC_TOKEN, "ext_nfc_token"},
57 	      {WpsConfigMethods::NFC_INTERFACE, "nfc_interface"},
58 	      {WpsConfigMethods::PUSHBUTTON, "push_button"},
59 	      {WpsConfigMethods::KEYPAD, "keypad"},
60 	      {WpsConfigMethods::VIRT_PUSHBUTTON, "virtual_push_button"},
61 	      {WpsConfigMethods::PHY_PUSHBUTTON, "physical_push_button"},
62 	      {WpsConfigMethods::P2PS, "p2ps"},
63 	      {WpsConfigMethods::VIRT_DISPLAY, "virtual_display"},
64 	      {WpsConfigMethods::PHY_DISPLAY, "physical_display"}}) {
65 		const auto flag =
66 		    static_cast<std::underlying_type<WpsConfigMethods>::type>(
67 			flag_and_name.first);
68 		if ((config_methods & flag) == flag) {
69 			config_methods_str += flag_and_name.second;
70 			config_methods_str += " ";
71 		}
72 	}
73 	return config_methods_str;
74 }
75 }  // namespace
76 
77 namespace android {
78 namespace hardware {
79 namespace wifi {
80 namespace supplicant {
81 namespace V1_0 {
82 namespace implementation {
83 namespace iface_config_utils {
setWpsDeviceName(struct wpa_supplicant * wpa_s,const std::string & name)84 SupplicantStatus 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 {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
92 	}
93 	return {SupplicantStatusCode::SUCCESS, ""};
94 }
95 
setWpsDeviceType(struct wpa_supplicant * wpa_s,const std::array<uint8_t,8> & type)96 SupplicantStatus setWpsDeviceType(
97     struct wpa_supplicant* wpa_s, const std::array<uint8_t, 8>& 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 {SupplicantStatusCode::SUCCESS, ""};
104 }
105 
setWpsManufacturer(struct wpa_supplicant * wpa_s,const std::string & manufacturer)106 SupplicantStatus 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 {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
114 	}
115 	return {SupplicantStatusCode::SUCCESS, ""};
116 }
117 
setWpsModelName(struct wpa_supplicant * wpa_s,const std::string & model_name)118 SupplicantStatus 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 {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
126 	}
127 	return {SupplicantStatusCode::SUCCESS, ""};
128 }
129 
setWpsModelNumber(struct wpa_supplicant * wpa_s,const std::string & model_number)130 SupplicantStatus 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 {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
138 	}
139 	return {SupplicantStatusCode::SUCCESS, ""};
140 }
141 
setWpsSerialNumber(struct wpa_supplicant * wpa_s,const std::string & serial_number)142 SupplicantStatus 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 {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
150 	}
151 	return {SupplicantStatusCode::SUCCESS, ""};
152 }
153 
setWpsConfigMethods(struct wpa_supplicant * wpa_s,uint16_t config_methods)154 SupplicantStatus 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 {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
163 	}
164 	return {SupplicantStatusCode::SUCCESS, ""};
165 }
166 
setExternalSim(struct wpa_supplicant * wpa_s,bool useExternalSim)167 SupplicantStatus 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 {SupplicantStatusCode::SUCCESS, ""};
173 }
174 }  // namespace iface_config_utils
175 }  // namespace implementation
176 }  // namespace V1_0
177 }  // namespace wifi
178 }  // namespace supplicant
179 }  // namespace hardware
180 }  // namespace android
181