source: mainline/uspace/app/wifi_supplicant/wifi_supplicant.c@ 706b4de

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 706b4de was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[59fa7ab]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
[053fc2b]38#include <inet/inetcfg.h>
39#include <inet/dhcp.h>
[59fa7ab]40#include <errno.h>
41#include <stdio.h>
42#include <loc.h>
43
[8a64320e]44#define NAME "wifi_supplicant"
[59fa7ab]45
[8a64320e]46#define enum_name(name_arr, i) \
47 ((i < 0) ? "NA" : name_arr[i])
[1dcc0b9]48
49static const char* ieee80211_security_type_strs[] = {
50 "OPEN", "WEP", "WPA", "WPA2"
51};
52
53static const char* ieee80211_security_alg_strs[] = {
54 "WEP40", "WEP104", "CCMP", "TKIP"
55};
56
57static const char* ieee80211_security_auth_strs[] = {
58 "PSK", "8021X"
59};
60
[59fa7ab]61static void print_syntax(void)
62{
63 printf("syntax:\n");
64 printf("\t" NAME " [<cmd> [<args...>]]\n");
65 printf("\t<cmd> is:\n");
66 printf("\tlist - list wifi devices in <index>: <name> format\n");
[1dcc0b9]67 printf("\tscan <index> [-n] - output scan results (force scan "
[8a64320e]68 "immediately)\n");
[1dcc0b9]69 printf("\tconnect <index> <ssid_prefix> [<password>] - connect to "
[8a64320e]70 "network\n");
[1dcc0b9]71 printf("\tdisconnect <index> - disconnect from network\n");
[59fa7ab]72}
73
[8a64320e]74static char *nic_addr_format(nic_address_t *addr)
[59fa7ab]75{
[8a64320e]76 char *str;
77 int rc = asprintf(&str, "%02x:%02x:%02x:%02x:%02x:%02x",
78 addr->address[0], addr->address[1], addr->address[2],
79 addr->address[3], addr->address[4], addr->address[5]);
80
[59fa7ab]81 if (rc < 0)
82 return NULL;
[8a64320e]83
84 return str;
[59fa7ab]85}
86
87static int get_wifi_list(service_id_t **wifis, size_t *count)
88{
89 category_id_t wifi_cat;
90 int rc = loc_category_get_id("ieee80211", &wifi_cat, 0);
91 if (rc != EOK) {
92 printf("Error resolving category 'ieee80211'.\n");
93 return rc;
94 }
[8a64320e]95
[59fa7ab]96 rc = loc_category_get_svcs(wifi_cat, wifis, count);
97 if (rc != EOK) {
98 printf("Error getting list of WIFIs.\n");
99 return rc;
100 }
101
102 return EOK;
103}
104
105static async_sess_t *get_wifi_by_index(size_t i)
106{
107 service_id_t *wifis = NULL;
[8a64320e]108 size_t count;
109
110 int rc = get_wifi_list(&wifis, &count);
[59fa7ab]111 if (rc != EOK) {
112 printf("Error fetching wifi list.\n");
113 return NULL;
114 }
115
[8a64320e]116 if (i >= count) {
[59fa7ab]117 printf("Invalid wifi index.\n");
118 free(wifis);
119 return NULL;
120 }
[8a64320e]121
122 async_sess_t *sess =
[f9b2cb4c]123 loc_service_connect(wifis[i], INTERFACE_DDF, 0);
[59fa7ab]124 if (sess == NULL) {
125 printf("Error connecting to service.\n");
126 free(wifis);
127 return NULL;
128 }
[8a64320e]129
[59fa7ab]130 return sess;
131}
132
133static int wifi_list(void)
134{
135 service_id_t *wifis = NULL;
136 size_t count;
[8a64320e]137
138 int rc = get_wifi_list(&wifis, &count);
[59fa7ab]139 if (rc != EOK) {
140 printf("Error fetching wifi list.\n");
141 return EINVAL;
142 }
[8a64320e]143
[59fa7ab]144 printf("[Index]: [Service Name]\n");
145 for (size_t i = 0; i < count; i++) {
[8a64320e]146 char *svc_name;
[59fa7ab]147 rc = loc_service_get_name(wifis[i], &svc_name);
148 if (rc != EOK) {
149 printf("Error getting service name.\n");
150 free(wifis);
151 return rc;
152 }
153
154 printf("%zu: %s\n", i, svc_name);
[8a64320e]155
[59fa7ab]156 free(svc_name);
157 }
[8a64320e]158
[59fa7ab]159 return EOK;
160}
161
[1dcc0b9]162static int wifi_connect(uint32_t index, char *ssid_start, char *password)
[59fa7ab]163{
[1dcc0b9]164 assert(ssid_start);
[59fa7ab]165
166 async_sess_t *sess = get_wifi_by_index(index);
167 if (sess == NULL) {
[1dcc0b9]168 printf("Specified WIFI doesn't exist or cannot connect to "
[8a64320e]169 "it.\n");
[59fa7ab]170 return EINVAL;
171 }
172
[053fc2b]173 int rc = ieee80211_disconnect(sess);
174 if(rc != EOK) {
[8a64320e]175 if (rc == EREFUSED)
176 printf("Device is not ready yet.\n");
177 else
[053fc2b]178 printf("Error when disconnecting device. "
[8a64320e]179 "Error: %d\n", rc);
[053fc2b]180
181 return rc;
182 }
183
[a931b7b]184 rc = ieee80211_connect(sess, ssid_start, password);
[59fa7ab]185 if(rc != EOK) {
[8a64320e]186 if (rc == EREFUSED)
187 printf("Device is not ready yet.\n");
188 else if (rc == ETIMEOUT)
[1dcc0b9]189 printf("Timeout when authenticating to network.\n");
[8a64320e]190 else if (rc == ENOENT)
[053fc2b]191 printf("Given SSID not in scan results.\n");
[8a64320e]192 else
[1dcc0b9]193 printf("Error when connecting to network. "
[8a64320e]194 "Error: %d\n", rc);
[1dcc0b9]195
196 return rc;
197 }
198
[8a64320e]199 // TODO: Wait for DHCP address?
[053fc2b]200
[1dcc0b9]201 printf("Successfully connected to network!\n");
202
203 return EOK;
204}
205
206static int wifi_disconnect(uint32_t index)
207{
208 async_sess_t *sess = get_wifi_by_index(index);
209 if (sess == NULL) {
210 printf("Specified WIFI doesn't exist or cannot connect to "
[8a64320e]211 "it.\n");
[59fa7ab]212 return EINVAL;
213 }
214
[1dcc0b9]215 int rc = ieee80211_disconnect(sess);
[8a64320e]216 if (rc != EOK) {
217 if (rc == EREFUSED)
[1dcc0b9]218 printf("Device is not ready yet.\n");
[8a64320e]219 else if (rc == EINVAL)
[1dcc0b9]220 printf("Not connected to any WiFi network.\n");
[8a64320e]221 else
[1dcc0b9]222 printf("Error when disconnecting from network. "
[8a64320e]223 "Error: %d\n", rc);
224
[1dcc0b9]225 return rc;
226 }
227
228 printf("Successfully disconnected.\n");
[59fa7ab]229
230 return EOK;
231}
232
[1dcc0b9]233static int wifi_scan(uint32_t index, bool now)
[59fa7ab]234{
235 async_sess_t *sess = get_wifi_by_index(index);
236 if (sess == NULL) {
[1dcc0b9]237 printf("Specified WIFI doesn't exist or cannot connect to "
[8a64320e]238 "it.\n");
[59fa7ab]239 return EINVAL;
240 }
241
[8a64320e]242 ieee80211_scan_results_t scan_results;
[1dcc0b9]243 int rc = ieee80211_get_scan_results(sess, &scan_results, now);
[8a64320e]244 if (rc != EOK) {
245 if (rc == EREFUSED)
[1dcc0b9]246 printf("Device is not ready yet.\n");
[8a64320e]247 else
[1dcc0b9]248 printf("Failed to fetch scan results. Error: %d\n", rc);
249
250 return rc;
[59fa7ab]251 }
252
[8a64320e]253 if (scan_results.length == 0)
[1dcc0b9]254 return EOK;
255
[8a64320e]256 printf("%16.16s %17s %4s %5s %5s %7s %7s\n",
257 "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG");
[1dcc0b9]258
[8a64320e]259 for (uint8_t i = 0; i < scan_results.length; i++) {
[59fa7ab]260 ieee80211_scan_result_t result = scan_results.results[i];
261
[8a64320e]262 printf("%16.16s %17s %4d %5s %5s %7s %7s\n",
263 result.ssid, nic_addr_format(&result.bssid),
264 result.channel,
265 enum_name(ieee80211_security_type_strs, result.security.type),
266 enum_name(ieee80211_security_auth_strs, result.security.auth),
267 enum_name(ieee80211_security_alg_strs, result.security.pair_alg),
268 enum_name(ieee80211_security_alg_strs, result.security.group_alg));
[59fa7ab]269 }
270
271 return EOK;
272}
273
274int main(int argc, char *argv[])
275{
[8a64320e]276 int rc = inetcfg_init();
[053fc2b]277 if (rc != EOK) {
[8a64320e]278 printf("%s: Failed connecting to inetcfg service (%d).\n",
279 NAME, rc);
[053fc2b]280 return 1;
281 }
282
283 rc = dhcp_init();
284 if (rc != EOK) {
[8a64320e]285 printf("%s: Failed connecting to dhcp service (%d).\n",
286 NAME, rc);
[053fc2b]287 return 1;
288 }
289
[8a64320e]290 if (argc == 2) {
291 if (!str_cmp(argv[1], "list"))
[59fa7ab]292 return wifi_list();
[8a64320e]293 } else if (argc > 2) {
294 uint32_t index;
[59fa7ab]295 rc = str_uint32_t(argv[2], NULL, 10, false, &index);
[8a64320e]296 if (rc != EOK) {
297 printf("%s: Invalid argument.\n", NAME);
[59fa7ab]298 print_syntax();
299 return EINVAL;
300 }
[8a64320e]301
302 if (!str_cmp(argv[1], "scan")) {
[1dcc0b9]303 bool now = false;
[8a64320e]304 if (argc > 3)
305 if (!str_cmp(argv[3], "-n"))
[1dcc0b9]306 now = true;
[8a64320e]307
[1dcc0b9]308 return wifi_scan(index, now);
[8a64320e]309 } else if (!str_cmp(argv[1], "connect")) {
[59fa7ab]310 char *pass = NULL;
[8a64320e]311 if (argc > 3) {
312 if (argc > 4)
[59fa7ab]313 pass = argv[4];
[8a64320e]314
[59fa7ab]315 return wifi_connect(index, argv[3], pass);
[8a64320e]316 }
317 } else if (!str_cmp(argv[1], "disconnect"))
[1dcc0b9]318 return wifi_disconnect(index);
[59fa7ab]319 }
320
321 print_syntax();
322
323 return EOK;
324}
325
326/** @}
[8a64320e]327 */
Note: See TracBrowser for help on using the repository browser.