Changeset 1dcc0b9 in mainline for uspace/app/wifi_supplicant/wifi_supplicant.c
- Timestamp:
- 2015-04-06T10:47:51Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d7dadcb4
- Parents:
- 59fa7ab
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/wifi_supplicant/wifi_supplicant.c
r59fa7ab r1dcc0b9 36 36 #include <ieee80211_iface.h> 37 37 38 #include <malloc.h>39 38 #include <errno.h> 40 39 #include <stdio.h> … … 42 41 43 42 #define NAME "wifi_supplicant" 43 44 #define enum_name(name_arr, i) ((i < 0) ? "NA" : name_arr[i]) 45 46 static const char* ieee80211_security_type_strs[] = { 47 "OPEN", "WEP", "WPA", "WPA2" 48 }; 49 50 static const char* ieee80211_security_alg_strs[] = { 51 "WEP40", "WEP104", "CCMP", "TKIP" 52 }; 53 54 static const char* ieee80211_security_auth_strs[] = { 55 "PSK", "8021X" 56 }; 44 57 45 58 static void print_syntax(void) … … 49 62 printf("\t<cmd> is:\n"); 50 63 printf("\tlist - list wifi devices in <index>: <name> format\n"); 51 printf("\tscan <index> - output scan results for given device\n"); 64 printf("\tscan <index> [-n] - output scan results (force scan " 65 "immediately)\n"); 66 printf("\tconnect <index> <ssid_prefix> [<password>] - connect to " 67 "network\n"); 68 printf("\tdisconnect <index> - disconnect from network\n"); 52 69 } 53 70 … … 98 115 } 99 116 100 if(i > count - 1) {117 if(i >= count) { 101 118 printf("Invalid wifi index.\n"); 102 119 free(wifis); … … 104 121 } 105 122 106 async_sess_t *sess = loc_service_connect(EXCHANGE_SERIALIZE, wifis[i], 0); 123 async_sess_t *sess = 124 loc_service_connect(EXCHANGE_SERIALIZE, wifis[i], 0); 107 125 if (sess == NULL) { 108 126 printf("Error connecting to service.\n"); … … 144 162 } 145 163 146 static int wifi_connect(uint32_t index, char *ssid , char *password)147 { 148 assert(ssid );164 static int wifi_connect(uint32_t index, char *ssid_start, char *password) 165 { 166 assert(ssid_start); 149 167 150 168 async_sess_t *sess = get_wifi_by_index(index); 151 169 if (sess == NULL) { 152 printf("Specified WIFI doesn't exist or cannot connect to it.\n"); 170 printf("Specified WIFI doesn't exist or cannot connect to " 171 "it.\n"); 153 172 return EINVAL; 154 173 } 155 174 156 int rc = ieee80211_connect(sess, ssid , password);175 int rc = ieee80211_connect(sess, ssid_start, password); 157 176 if(rc != EOK) { 158 printf("Failed connecting to network. Error: %d\n", rc); 159 return EINVAL; 160 } 161 162 printf("Successfully connected to %s!\n", ssid); 163 164 return EOK; 165 } 166 167 static int wifi_scan(uint32_t index) 168 { 169 ieee80211_scan_results_t scan_results; 170 177 if(rc == EREFUSED) { 178 printf("Device is not ready yet.\n"); 179 } else if(rc == ETIMEOUT) { 180 printf("Timeout when authenticating to network.\n"); 181 } else if(rc == EPERM) { 182 printf("Bad password provided.\n"); 183 } else { 184 printf("Error when connecting to network. " 185 "Error: %d\n", rc); 186 } 187 188 return rc; 189 } 190 191 printf("Successfully connected to network!\n"); 192 193 return EOK; 194 } 195 196 static int wifi_disconnect(uint32_t index) 197 { 171 198 async_sess_t *sess = get_wifi_by_index(index); 172 199 if (sess == NULL) { 173 printf("Specified WIFI doesn't exist or cannot connect to it.\n"); 200 printf("Specified WIFI doesn't exist or cannot connect to " 201 "it.\n"); 174 202 return EINVAL; 175 203 } 176 204 177 int rc = ieee80211_ get_scan_results(sess, &scan_results);205 int rc = ieee80211_disconnect(sess); 178 206 if(rc != EOK) { 179 printf("Failed to fetch scan results. Error: %d\n", rc); 207 if(rc == EREFUSED) { 208 printf("Device is not ready yet.\n"); 209 } else if(rc == EINVAL) { 210 printf("Not connected to any WiFi network.\n"); 211 } else { 212 printf("Error when disconnecting from network. " 213 "Error: %d\n", rc); 214 } 215 return rc; 216 } 217 218 printf("Successfully disconnected.\n"); 219 220 return EOK; 221 } 222 223 static int wifi_scan(uint32_t index, bool now) 224 { 225 ieee80211_scan_results_t scan_results; 226 227 async_sess_t *sess = get_wifi_by_index(index); 228 if (sess == NULL) { 229 printf("Specified WIFI doesn't exist or cannot connect to " 230 "it.\n"); 180 231 return EINVAL; 181 232 } 233 234 int rc = ieee80211_get_scan_results(sess, &scan_results, now); 235 if(rc != EOK) { 236 if(rc == EREFUSED) { 237 printf("Device is not ready yet.\n"); 238 } else { 239 printf("Failed to fetch scan results. Error: %d\n", rc); 240 } 241 242 return rc; 243 } 244 245 if(scan_results.length == 0) 246 return EOK; 247 248 printf("%16.16s %17s %4s %5s %5s %7s %7s\n", 249 "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG"); 182 250 183 251 for(int i = 0; i < scan_results.length; i++) { 184 252 ieee80211_scan_result_t result = scan_results.results[i]; 185 253 186 printf("SSID: %s, MAC addr: %s\n", result.ssid, 187 nic_addr_format(&result.bssid)); 254 printf("%16.16s %17s %4d %5s %5s %7s %7s\n", 255 result.ssid, 256 nic_addr_format(&result.bssid), 257 result.channel, 258 enum_name(ieee80211_security_type_strs, 259 result.security.type), 260 enum_name(ieee80211_security_auth_strs, 261 result.security.auth), 262 enum_name(ieee80211_security_alg_strs, 263 result.security.pair_alg), 264 enum_name(ieee80211_security_alg_strs, 265 result.security.group_alg) 266 ); 188 267 } 189 268 … … 197 276 198 277 if(argc == 2) { 199 if 278 if(!str_cmp(argv[1], "list")) { 200 279 return wifi_list(); 201 280 } 202 281 } else if(argc > 2) { 203 282 rc = str_uint32_t(argv[2], NULL, 10, false, &index); 204 if 283 if(rc != EOK) { 205 284 printf(NAME ": Invalid argument.\n"); 206 285 print_syntax(); 207 286 return EINVAL; 208 287 } 209 if (!str_cmp(argv[1], "scan")) { 210 return wifi_scan(index); 211 } else if (!str_cmp(argv[1], "connect")) { 288 if(!str_cmp(argv[1], "scan")) { 289 bool now = false; 290 if(argc > 3) 291 if(!str_cmp(argv[3], "-n")) 292 now = true; 293 return wifi_scan(index, now); 294 } else if(!str_cmp(argv[1], "connect")) { 212 295 char *pass = NULL; 213 296 if(argc > 3) { … … 216 299 return wifi_connect(index, argv[3], pass); 217 300 } 301 } else if(!str_cmp(argv[1], "disconnect")) { 302 return wifi_disconnect(index); 218 303 } 219 304 }
Note:
See TracChangeset
for help on using the changeset viewer.