source: mainline/uspace/app/wifi_supplicant/wifi_supplicant.c@ 1433ecda

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1433ecda was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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