Changeset 8a64320e in mainline for uspace/app/wifi_supplicant/wifi_supplicant.c
- Timestamp:
- 2015-04-23T23:40:14Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- dcba819
- Parents:
- 09044cb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/wifi_supplicant/wifi_supplicant.c
r09044cb r8a64320e 42 42 #include <loc.h> 43 43 44 #define NAME "wifi_supplicant" 45 46 #define enum_name(name_arr, i) ((i < 0) ? "NA" : name_arr[i]) 44 #define NAME "wifi_supplicant" 45 46 #define enum_name(name_arr, i) \ 47 ((i < 0) ? "NA" : name_arr[i]) 47 48 48 49 static const char* ieee80211_security_type_strs[] = { … … 65 66 printf("\tlist - list wifi devices in <index>: <name> format\n"); 66 67 printf("\tscan <index> [-n] - output scan results (force scan " 67 68 "immediately)\n"); 68 69 printf("\tconnect <index> <ssid_prefix> [<password>] - connect to " 69 70 "network\n"); 70 71 printf("\tdisconnect <index> - disconnect from network\n"); 71 72 } 72 73 73 static char *nic_addr_format(nic_address_t *a) 74 { 75 int rc; 76 char *s; 77 78 rc = asprintf(&s, "%02x:%02x:%02x:%02x:%02x:%02x", 79 a->address[0], a->address[1], a->address[2], 80 a->address[3], a->address[4], a->address[5]); 81 74 static char *nic_addr_format(nic_address_t *addr) 75 { 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 82 81 if (rc < 0) 83 82 return NULL; 84 85 return s ;83 84 return str; 86 85 } 87 86 … … 89 88 { 90 89 category_id_t wifi_cat; 91 92 90 int rc = loc_category_get_id("ieee80211", &wifi_cat, 0); 93 91 if (rc != EOK) { … … 95 93 return rc; 96 94 } 97 95 98 96 rc = loc_category_get_svcs(wifi_cat, wifis, count); 99 97 if (rc != EOK) { … … 107 105 static async_sess_t *get_wifi_by_index(size_t i) 108 106 { 109 int rc;107 service_id_t *wifis = NULL; 110 108 size_t count; 111 service_id_t *wifis = NULL; 112 113 rc = get_wifi_list(&wifis, &count); 109 110 int rc = get_wifi_list(&wifis, &count); 114 111 if (rc != EOK) { 115 112 printf("Error fetching wifi list.\n"); … … 117 114 } 118 115 119 if (i >= count) {116 if (i >= count) { 120 117 printf("Invalid wifi index.\n"); 121 118 free(wifis); 122 119 return NULL; 123 120 } 124 125 async_sess_t *sess = 126 121 122 async_sess_t *sess = 123 loc_service_connect(EXCHANGE_SERIALIZE, wifis[i], 0); 127 124 if (sess == NULL) { 128 125 printf("Error connecting to service.\n"); … … 130 127 return NULL; 131 128 } 132 129 133 130 return sess; 134 131 } … … 138 135 service_id_t *wifis = NULL; 139 136 size_t count; 140 char *svc_name; 141 int rc; 142 143 rc = get_wifi_list(&wifis, &count); 137 138 int rc = get_wifi_list(&wifis, &count); 144 139 if (rc != EOK) { 145 140 printf("Error fetching wifi list.\n"); 146 141 return EINVAL; 147 142 } 148 143 149 144 printf("[Index]: [Service Name]\n"); 150 145 for (size_t i = 0; i < count; i++) { 146 char *svc_name; 151 147 rc = loc_service_get_name(wifis[i], &svc_name); 152 148 if (rc != EOK) { … … 157 153 158 154 printf("%zu: %s\n", i, svc_name); 159 155 160 156 free(svc_name); 161 157 } 162 158 163 159 return EOK; 164 160 } … … 171 167 if (sess == NULL) { 172 168 printf("Specified WIFI doesn't exist or cannot connect to " 173 169 "it.\n"); 174 170 return EINVAL; 175 171 } … … 177 173 int rc = ieee80211_disconnect(sess); 178 174 if(rc != EOK) { 179 if (rc == EREFUSED) {180 printf("Device is not ready yet.\n"); 181 } else {175 if (rc == EREFUSED) 176 printf("Device is not ready yet.\n"); 177 else 182 178 printf("Error when disconnecting device. " 183 "Error: %d\n", rc); 184 } 179 "Error: %d\n", rc); 185 180 186 181 return rc; … … 189 184 rc = ieee80211_connect(sess, ssid_start, password); 190 185 if(rc != EOK) { 191 if (rc == EREFUSED) {192 printf("Device is not ready yet.\n"); 193 } else if(rc == ETIMEOUT) {186 if (rc == EREFUSED) 187 printf("Device is not ready yet.\n"); 188 else if (rc == ETIMEOUT) 194 189 printf("Timeout when authenticating to network.\n"); 195 } else if(rc == ENOENT) {190 else if (rc == ENOENT) 196 191 printf("Given SSID not in scan results.\n"); 197 } else {192 else 198 193 printf("Error when connecting to network. " 199 "Error: %d\n", rc); 200 } 201 202 return rc; 203 } 204 205 // TODO: Wait for DHCP address ? 194 "Error: %d\n", rc); 195 196 return rc; 197 } 198 199 // TODO: Wait for DHCP address? 206 200 207 201 printf("Successfully connected to network!\n"); … … 215 209 if (sess == NULL) { 216 210 printf("Specified WIFI doesn't exist or cannot connect to " 217 211 "it.\n"); 218 212 return EINVAL; 219 213 } 220 214 221 215 int rc = ieee80211_disconnect(sess); 222 if (rc != EOK) {223 if (rc == EREFUSED) {216 if (rc != EOK) { 217 if (rc == EREFUSED) 224 218 printf("Device is not ready yet.\n"); 225 } else if(rc == EINVAL) {219 else if (rc == EINVAL) 226 220 printf("Not connected to any WiFi network.\n"); 227 } else {221 else 228 222 printf("Error when disconnecting from network. " 229 230 }223 "Error: %d\n", rc); 224 231 225 return rc; 232 226 } … … 239 233 static int wifi_scan(uint32_t index, bool now) 240 234 { 241 ieee80211_scan_results_t scan_results;242 243 235 async_sess_t *sess = get_wifi_by_index(index); 244 236 if (sess == NULL) { 245 237 printf("Specified WIFI doesn't exist or cannot connect to " 246 238 "it.\n"); 247 239 return EINVAL; 248 240 } 249 241 242 ieee80211_scan_results_t scan_results; 250 243 int rc = ieee80211_get_scan_results(sess, &scan_results, now); 251 if (rc != EOK) {252 if (rc == EREFUSED) {244 if (rc != EOK) { 245 if (rc == EREFUSED) 253 246 printf("Device is not ready yet.\n"); 254 } else {247 else 255 248 printf("Failed to fetch scan results. Error: %d\n", rc); 256 } 257 258 return rc; 259 } 260 261 if(scan_results.length == 0) 249 250 return rc; 251 } 252 253 if (scan_results.length == 0) 262 254 return EOK; 263 255 264 printf("%16.16s %17s %4s %5s %5s %7s %7s\n", 265 266 267 for (int i = 0; i < scan_results.length; i++) {256 printf("%16.16s %17s %4s %5s %5s %7s %7s\n", 257 "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG"); 258 259 for (uint8_t i = 0; i < scan_results.length; i++) { 268 260 ieee80211_scan_result_t result = scan_results.results[i]; 269 261 270 printf("%16.16s %17s %4d %5s %5s %7s %7s\n", 271 result.ssid, 272 nic_addr_format(&result.bssid), 273 result.channel, 274 enum_name(ieee80211_security_type_strs, 275 result.security.type), 276 enum_name(ieee80211_security_auth_strs, 277 result.security.auth), 278 enum_name(ieee80211_security_alg_strs, 279 result.security.pair_alg), 280 enum_name(ieee80211_security_alg_strs, 281 result.security.group_alg) 282 ); 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)); 283 269 } 284 270 … … 288 274 int main(int argc, char *argv[]) 289 275 { 290 int rc; 291 uint32_t index; 292 293 rc = inetcfg_init(); 294 if (rc != EOK) { 295 printf(NAME ": Failed connecting to inetcfg service (%d).\n", 296 rc); 276 int rc = inetcfg_init(); 277 if (rc != EOK) { 278 printf("%s: Failed connecting to inetcfg service (%d).\n", 279 NAME, rc); 297 280 return 1; 298 281 } … … 300 283 rc = dhcp_init(); 301 284 if (rc != EOK) { 302 printf(NAME ": Failed connecting to dhcp service (%d).\n", rc); 285 printf("%s: Failed connecting to dhcp service (%d).\n", 286 NAME, rc); 303 287 return 1; 304 288 } 305 289 306 if (argc == 2) {307 if (!str_cmp(argv[1], "list")) {290 if (argc == 2) { 291 if (!str_cmp(argv[1], "list")) 308 292 return wifi_list(); 309 }310 } else if(argc > 2) {293 } else if (argc > 2) { 294 uint32_t index; 311 295 rc = str_uint32_t(argv[2], NULL, 10, false, &index); 312 if (rc != EOK) {313 printf( NAME ": Invalid argument.\n");296 if (rc != EOK) { 297 printf("%s: Invalid argument.\n", NAME); 314 298 print_syntax(); 315 299 return EINVAL; 316 300 } 317 if(!str_cmp(argv[1], "scan")) { 301 302 if (!str_cmp(argv[1], "scan")) { 318 303 bool now = false; 319 if (argc > 3)320 if (!str_cmp(argv[3], "-n"))304 if (argc > 3) 305 if (!str_cmp(argv[3], "-n")) 321 306 now = true; 307 322 308 return wifi_scan(index, now); 323 } else if (!str_cmp(argv[1], "connect")) {309 } else if (!str_cmp(argv[1], "connect")) { 324 310 char *pass = NULL; 325 if (argc > 3) {326 if (argc > 4)311 if (argc > 3) { 312 if (argc > 4) 327 313 pass = argv[4]; 314 328 315 return wifi_connect(index, argv[3], pass); 329 } 330 } else if (!str_cmp(argv[1], "disconnect")) {316 } 317 } else if (!str_cmp(argv[1], "disconnect")) 331 318 return wifi_disconnect(index); 332 }333 319 } 334 320
Note:
See TracChangeset
for help on using the changeset viewer.