source: mainline/uspace/app/wifi_supplicant/wifi_supplicant.c@ 1569a9b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1569a9b was c1694b6b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Add str_error() in numerous places.

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*
2 * Copyright (c) 2015 Jan Kolarik
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup nic
30 * @{
31 */
32/** @file WiFi device configuration utility.
33 *
34 */
35
36#include <ieee80211_iface.h>
37
38#include <inet/inetcfg.h>
39#include <inet/dhcp.h>
40#include <errno.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <str_error.h>
44#include <loc.h>
45
46#define NAME "wifi_supplicant"
47
48#define enum_name(name_arr, i) \
49 ((i < 0) ? "NA" : name_arr[i])
50
51static const char* ieee80211_security_type_strs[] = {
52 "OPEN", "WEP", "WPA", "WPA2"
53};
54
55static const char* ieee80211_security_alg_strs[] = {
56 "WEP40", "WEP104", "CCMP", "TKIP"
57};
58
59static const char* ieee80211_security_auth_strs[] = {
60 "PSK", "8021X"
61};
62
63static void print_syntax(void)
64{
65 printf("syntax:\n");
66 printf("\t" NAME " [<cmd> [<args...>]]\n");
67 printf("\t<cmd> is:\n");
68 printf("\tlist - list wifi devices in <index>: <name> format\n");
69 printf("\tscan <index> [-n] - output scan results (force scan "
70 "immediately)\n");
71 printf("\tconnect <index> <ssid_prefix> [<password>] - connect to "
72 "network\n");
73 printf("\tdisconnect <index> - disconnect from network\n");
74}
75
76static char *nic_addr_format(nic_address_t *addr)
77{
78 char *str;
79 int rc = asprintf(&str, "%02x:%02x:%02x:%02x:%02x:%02x",
80 addr->address[0], addr->address[1], addr->address[2],
81 addr->address[3], addr->address[4], addr->address[5]);
82
83 if (rc < 0)
84 return NULL;
85
86 return str;
87}
88
89static int get_wifi_list(service_id_t **wifis, size_t *count)
90{
91 category_id_t wifi_cat;
92 int rc = loc_category_get_id("ieee80211", &wifi_cat, 0);
93 if (rc != EOK) {
94 printf("Error resolving category 'ieee80211'.\n");
95 return rc;
96 }
97
98 rc = loc_category_get_svcs(wifi_cat, wifis, count);
99 if (rc != EOK) {
100 printf("Error getting list of WIFIs.\n");
101 return rc;
102 }
103
104 return EOK;
105}
106
107static async_sess_t *get_wifi_by_index(size_t i)
108{
109 service_id_t *wifis = NULL;
110 size_t count;
111
112 int rc = get_wifi_list(&wifis, &count);
113 if (rc != EOK) {
114 printf("Error fetching wifi list.\n");
115 return NULL;
116 }
117
118 if (i >= count) {
119 printf("Invalid wifi index.\n");
120 free(wifis);
121 return NULL;
122 }
123
124 async_sess_t *sess =
125 loc_service_connect(wifis[i], INTERFACE_DDF, 0);
126 if (sess == NULL) {
127 printf("Error connecting to service.\n");
128 free(wifis);
129 return NULL;
130 }
131
132 return sess;
133}
134
135static int wifi_list(void)
136{
137 service_id_t *wifis = NULL;
138 size_t count;
139
140 int rc = get_wifi_list(&wifis, &count);
141 if (rc != EOK) {
142 printf("Error fetching wifi list: %s\n", str_error(rc));
143 return EINVAL;
144 }
145
146 printf("[Index]: [Service Name]\n");
147 for (size_t i = 0; i < count; i++) {
148 char *svc_name;
149 rc = loc_service_get_name(wifis[i], &svc_name);
150 if (rc != EOK) {
151 printf("Error getting service name: %s\n", str_error(rc));
152 free(wifis);
153 return rc;
154 }
155
156 printf("%zu: %s\n", i, svc_name);
157
158 free(svc_name);
159 }
160
161 return EOK;
162}
163
164static int wifi_connect(uint32_t index, char *ssid_start, char *password)
165{
166 assert(ssid_start);
167
168 async_sess_t *sess = get_wifi_by_index(index);
169 if (sess == NULL) {
170 printf("Specified WIFI doesn't exist or cannot connect to "
171 "it.\n");
172 return EINVAL;
173 }
174
175 int rc = ieee80211_disconnect(sess);
176 if(rc != EOK) {
177 if (rc == EREFUSED)
178 printf("Device is not ready yet.\n");
179 else
180 printf("Error when disconnecting device: %s\n",
181 str_error(rc));
182
183 return rc;
184 }
185
186 rc = ieee80211_connect(sess, ssid_start, password);
187 if(rc != EOK) {
188 if (rc == EREFUSED)
189 printf("Device is not ready yet.\n");
190 else if (rc == ETIMEOUT)
191 printf("Timeout when authenticating to network.\n");
192 else if (rc == ENOENT)
193 printf("Given SSID not in scan results.\n");
194 else
195 printf("Error when connecting to network: %s\n",
196 str_error(rc));
197
198 return rc;
199 }
200
201 // TODO: Wait for DHCP address?
202
203 printf("Successfully connected to network!\n");
204
205 return EOK;
206}
207
208static int wifi_disconnect(uint32_t index)
209{
210 async_sess_t *sess = get_wifi_by_index(index);
211 if (sess == NULL) {
212 printf("Specified WIFI doesn't exist or cannot connect to "
213 "it.\n");
214 return EINVAL;
215 }
216
217 int rc = ieee80211_disconnect(sess);
218 if (rc != EOK) {
219 if (rc == EREFUSED)
220 printf("Device is not ready yet.\n");
221 else if (rc == EINVAL)
222 printf("Not connected to any WiFi network.\n");
223 else
224 printf("Error when disconnecting from network: %s\n",
225 str_error(rc));
226
227 return rc;
228 }
229
230 printf("Successfully disconnected.\n");
231
232 return EOK;
233}
234
235static int wifi_scan(uint32_t index, bool now)
236{
237 async_sess_t *sess = get_wifi_by_index(index);
238 if (sess == NULL) {
239 printf("Specified WIFI doesn't exist or cannot connect to "
240 "it.\n");
241 return EINVAL;
242 }
243
244 ieee80211_scan_results_t scan_results;
245 int rc = ieee80211_get_scan_results(sess, &scan_results, now);
246 if (rc != EOK) {
247 if (rc == EREFUSED)
248 printf("Device is not ready yet.\n");
249 else
250 printf("Failed to fetch scan results: %s\n",
251 str_error(rc));
252
253 return rc;
254 }
255
256 if (scan_results.length == 0)
257 return EOK;
258
259 printf("%16.16s %17s %4s %5s %5s %7s %7s\n",
260 "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG");
261
262 for (uint8_t i = 0; i < scan_results.length; i++) {
263 ieee80211_scan_result_t result = scan_results.results[i];
264
265 printf("%16.16s %17s %4d %5s %5s %7s %7s\n",
266 result.ssid, nic_addr_format(&result.bssid),
267 result.channel,
268 enum_name(ieee80211_security_type_strs, result.security.type),
269 enum_name(ieee80211_security_auth_strs, result.security.auth),
270 enum_name(ieee80211_security_alg_strs, result.security.pair_alg),
271 enum_name(ieee80211_security_alg_strs, result.security.group_alg));
272 }
273
274 return EOK;
275}
276
277int main(int argc, char *argv[])
278{
279 int rc = inetcfg_init();
280 if (rc != EOK) {
281 printf("%s: Failed connecting to inetcfg service: %s.\n",
282 NAME, str_error(rc));
283 return 1;
284 }
285
286 rc = dhcp_init();
287 if (rc != EOK) {
288 printf("%s: Failed connecting to dhcp service: %s.\n",
289 NAME, str_error(rc));
290 return 1;
291 }
292
293 if (argc == 2) {
294 if (!str_cmp(argv[1], "list"))
295 return wifi_list();
296 } else if (argc > 2) {
297 uint32_t index;
298 rc = str_uint32_t(argv[2], NULL, 10, false, &index);
299 if (rc != EOK) {
300 printf("%s: Invalid argument.\n", NAME);
301 print_syntax();
302 return EINVAL;
303 }
304
305 if (!str_cmp(argv[1], "scan")) {
306 bool now = false;
307 if (argc > 3)
308 if (!str_cmp(argv[3], "-n"))
309 now = true;
310
311 return wifi_scan(index, now);
312 } else if (!str_cmp(argv[1], "connect")) {
313 char *pass = NULL;
314 if (argc > 3) {
315 if (argc > 4)
316 pass = argv[4];
317
318 return wifi_connect(index, argv[3], pass);
319 }
320 } else if (!str_cmp(argv[1], "disconnect"))
321 return wifi_disconnect(index);
322 }
323
324 print_syntax();
325
326 return EOK;
327}
328
329/** @}
330 */
Note: See TracBrowser for help on using the repository browser.