Release TIZEN 2.0 beta
[framework/api/wifi.git] / test / wifi_test.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <glib.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <netdb.h>
21 #include <sys/socket.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/un.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <sys/ioctl.h>
28 #include <signal.h>
29 #include "assert.h"
30 #include "glib.h"
31 #include <wifi.h>
32 #include <tizen_error.h>
33
34
35 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
36
37
38 static void __test_device_state_callback(wifi_error_e error_code, wifi_device_state_e state, bool is_requested, void* user_data)
39 {
40         printf("Device state changed callback, error code : %d, requested : %d", error_code, is_requested);
41
42         if (state == WIFI_DEVICE_STATE_ACTIVATED)
43                 printf(", state : Activated\n");
44         else
45                 printf(", state : Deactivated\n");
46 }
47
48 static void __test_bg_scan_completed_callback(wifi_error_e error_code, void* user_data)
49 {
50         printf("Background Scan Completed, error code : %d\n", error_code);
51 }
52
53 static void __test_scan_request_callback(wifi_error_e error_code, void* user_data)
54 {
55         printf("Scan Completed from scan request, error code : %d\n", error_code);
56 }
57
58 static void __test_connection_state_callback(wifi_error_e error_code, wifi_connection_state_e state, wifi_ap_h ap, bool is_requested, void* user_data)
59 {
60         int rv = 0;
61         char *ap_name = NULL;
62
63         printf("Connection state changed callback, error code : %d, requested : %d", error_code, is_requested);
64
65         switch (state) {
66         case WIFI_CONNECTION_STATE_CONNECTING:
67                 printf(", state : Connecting");
68                 break;
69         case WIFI_CONNECTION_STATE_CONNECTED:
70                 printf(", state : Connected");
71                 break;
72         case WIFI_CONNECTION_STATE_DISCONNECTING:
73                 printf(", state : Disconnecting");
74                 break;
75         case WIFI_CONNECTION_STATE_DISCONNECTED:
76                 printf(", state : Disconnected");
77                 break;
78         }
79
80         rv = wifi_ap_get_essid(ap, &ap_name);
81         if (rv != WIFI_ERROR_NONE)
82                 printf(", Fail to get AP name [%d]\n", rv);
83         else {
84                 printf(", AP name : %s\n", ap_name);
85                 g_free(ap_name);
86         }
87 }
88
89 static void __test_rssi_level_callback(wifi_rssi_level_e rssi_level, void* user_data)
90 {
91         printf("RSSI level changed callback, level = %d\n", rssi_level);
92 }
93
94 static const char* __test_print_state(wifi_connection_state_e state)
95 {
96         switch (state) {
97         case WIFI_CONNECTION_STATE_DISCONNECTED:
98                 return "Disconnected";
99         case WIFI_CONNECTION_STATE_CONNECTING:
100                 return "Connecting";
101         case WIFI_CONNECTION_STATE_CONNECTED:
102                 return "Connected";
103         case WIFI_CONNECTION_STATE_DISCONNECTING:
104                 return "Disconnecting";
105         }
106
107         return "Unknown";
108 }
109
110 static bool __test_found_ap_callback(wifi_ap_h ap, void *user_data)
111 {
112         int rv = 0;
113         char *ap_name = NULL;
114         wifi_connection_state_e state;
115
116         rv = wifi_ap_get_essid(ap, &ap_name);
117         if (rv != WIFI_ERROR_NONE) {
118                 printf("Fail to get AP name [%d]\n", rv);
119                 return false;
120         }
121
122         rv = wifi_ap_get_connection_state(ap, &state);
123         if (rv != WIFI_ERROR_NONE) {
124                 printf("Fail to get State [%d]\n", rv);
125                 g_free(ap_name);
126                 return false;
127         }
128
129         printf("AP name : %s, state : %s\n", ap_name, __test_print_state(state));
130         g_free(ap_name);
131
132         return true;
133 }
134
135 static bool __test_found_connect_ap_callback(wifi_ap_h ap, void *user_data)
136 {
137         int rv = 0;
138         char *ap_name = NULL;
139         char *ap_name_part = (char*)user_data;
140
141         rv = wifi_ap_get_essid(ap, &ap_name);
142         if (rv != WIFI_ERROR_NONE) {
143                 printf("Fail to get AP name [%d]\n", rv);
144                 return false;
145         }
146
147         if (strstr(ap_name, ap_name_part) != NULL) {
148                 bool required = false;
149                 wifi_ap_is_passphrase_required(ap, &required);
150
151                 if (required) {
152                         char passphrase[100];
153                         printf("Input passphrase for %s : ", ap_name);
154                         rv = scanf("%100s", passphrase);
155
156                         rv = wifi_ap_set_passphrase(ap, passphrase);
157                         if (rv != WIFI_ERROR_NONE) {
158                                 printf("Fail to set passphrase : %d\n", rv);
159                                 g_free(ap_name);
160                                 return false;
161                         }
162                 }
163
164                 rv = wifi_connect(ap);
165                 if (rv != WIFI_ERROR_NONE)
166                         printf("Fail to connect [%s] : %d\n", ap_name, rv);
167                 else
168                         printf("Success to connect [%s]\n", ap_name);
169
170                 g_free(ap_name);
171                 return false;
172         }
173
174         g_free(ap_name);
175         return true;
176 }
177
178 static bool __test_found_disconnect_ap_callback(wifi_ap_h ap, void *user_data)
179 {
180         int rv = 0;
181         char *ap_name = NULL;
182         char *ap_name_part = (char*)user_data;
183
184         rv = wifi_ap_get_essid(ap, &ap_name);
185         if (rv != WIFI_ERROR_NONE) {
186                 printf("Fail to get AP name [%d]\n", rv);
187                 return false;
188         }
189
190         if (strstr(ap_name, ap_name_part) != NULL) {
191                 rv = wifi_disconnect(ap);
192                 if (rv != WIFI_ERROR_NONE)
193                         printf("Fail to disconnect %s : [%d]\n", ap_name, rv);
194                 else
195                         printf("Success to disconnect %s\n", ap_name);
196
197                 g_free(ap_name);
198                 return false;
199         }
200
201         g_free(ap_name);
202         return true;
203 }
204
205 int test_wifi_init(void)
206 {
207         int rv = wifi_initialize();
208
209         if (rv == WIFI_ERROR_NONE) {
210                 wifi_set_device_state_changed_cb(__test_device_state_callback, NULL);
211                 wifi_set_background_scan_cb(__test_bg_scan_completed_callback, NULL);
212                 wifi_set_connection_state_changed_cb(__test_connection_state_callback, NULL);
213                 wifi_set_rssi_level_changed_cb(__test_rssi_level_callback, NULL);
214         } else {
215                 printf("Wifi init failed [%d]\n", rv);
216                 return -1;
217         }
218
219         printf("Wifi init succeeded\n");
220         return 1;
221 }
222  
223 int  test_wifi_deinit(void)
224 {
225         int rv = 0;
226
227         rv = wifi_deinitialize();
228
229         if (rv != WIFI_ERROR_NONE){
230                 printf("Wifi deinit failed [%d]\n", rv);
231                 return -1;
232         }
233
234         printf("Wifi deinit succeeded\n");
235         return 1;
236 }
237
238 int test_wifi_activate(void)
239 {
240         int rv = 0;
241
242         rv = wifi_activate();
243
244         if (rv != WIFI_ERROR_NONE) {
245                 printf("Fail to activate Wi-Fi device [%d]\n", rv);
246                 return -1;
247         }
248
249         printf("Success to activate Wi-Fi device\n");
250
251         return 1;
252 }
253
254 int test_wifi_deactivate(void)
255 {
256         int rv = 0;
257
258         rv = wifi_deactivate();
259
260         if (rv != WIFI_ERROR_NONE) {
261                 printf("Fail to deactivate Wi-Fi device [%d]\n", rv);
262                 return -1;
263         }
264
265         printf("Success to deactivate Wi-Fi device\n");
266
267         return 1;
268 }
269
270 int test_is_activated(void)
271 {
272         int rv = 0;
273         bool state;
274
275         rv = wifi_is_activated(&state);
276
277         if (rv != WIFI_ERROR_NONE) {
278                 printf("Fail to get Wi-Fi device state [%d]\n", rv);
279                 return -1;
280         }
281
282         printf("Success to get Wi-Fi device state : %s\n", (state) ? "TRUE" : "FALSE");
283
284         return 1;
285 }
286
287 int test_get_connection_state(void)
288 {
289         int rv = 0;
290         wifi_connection_state_e connection_state;
291
292         rv = wifi_get_connection_state(&connection_state);
293
294         if (rv != WIFI_ERROR_NONE) {
295                 printf("Fail to get connection state [%d]\n", rv);
296                 return -1;
297         }
298
299         printf("Success to get connection state : ");
300         switch (connection_state) {
301         case WIFI_CONNECTION_STATE_CONNECTING:
302                 printf("Connecting\n");
303                 break;
304         case WIFI_CONNECTION_STATE_CONNECTED:
305                 printf("Connected\n");
306                 break;
307         case WIFI_CONNECTION_STATE_DISCONNECTING:
308                 printf("Disconnecting\n");
309                 break;
310         case WIFI_CONNECTION_STATE_DISCONNECTED:
311                 printf("Disconnected\n");
312                 break;
313         default:
314                 printf("Unknown\n");
315         }
316
317         return 1;
318 }
319
320 int test_get_mac_address(void)
321 {
322         int rv = 0;
323         char *mac_addr = NULL;
324
325         rv = wifi_get_mac_address(&mac_addr);
326
327         if (rv != WIFI_ERROR_NONE) {
328                 printf("Fail to get MAC address [%d]\n", rv);
329                 return -1;
330         }
331
332         printf("MAC address : %s\n", mac_addr);
333         g_free(mac_addr);
334
335         return 1;
336 }
337
338 int test_get_interface_name(void)
339 {
340         int rv = 0;
341         char *if_name = NULL;
342
343         rv = wifi_get_network_interface_name(&if_name);
344
345         if (rv != WIFI_ERROR_NONE) {
346                 printf("Fail to get Interface name [%d]\n", rv);
347                 return -1;
348         }
349
350         printf("Interface name : %s\n", if_name);
351         g_free(if_name);
352
353         return 1;
354 }
355
356 int test_scan_request(void)
357 {
358         int rv = 0;
359
360         rv = wifi_scan(__test_scan_request_callback, NULL);
361
362         if (rv != WIFI_ERROR_NONE) {
363                 printf("Scan request failed [%d]\n", rv);
364                 return -1;
365         }
366
367         printf("Scan request succeeded\n");
368
369         return 1;
370 }
371
372 int test_get_connected_ap(void)
373 {
374         int rv = 0;
375         char *ap_name = NULL;
376         wifi_ap_h ap_h;
377
378         rv = wifi_get_connected_ap(&ap_h);
379         if (rv != WIFI_ERROR_NONE) {
380                 printf("Fail to get connected AP [%d]\n", rv);
381                 return -1;
382         }
383
384         rv = wifi_ap_get_essid(ap_h, &ap_name);
385         if (rv != WIFI_ERROR_NONE) {
386                 printf("Fail to get connected AP [%d]\n", rv);
387                 wifi_ap_destroy(ap_h);
388                 return -1;
389         }
390
391         printf("Connected AP : %s\n", ap_name);
392         g_free(ap_name);
393         wifi_ap_destroy(ap_h);
394
395         return 1;
396 }
397
398 int test_foreach_found_aps(void)
399 {
400         int rv = 0;
401
402         rv = wifi_foreach_found_aps(__test_found_ap_callback, NULL);
403         if (rv != WIFI_ERROR_NONE) {
404                 printf("Fail to get AP list [%d]\n", rv);
405                 return -1;
406         }
407
408         printf("Get AP list finished\n");
409
410         return 1;
411 }
412
413 int test_connect_ap(void)
414 {
415         int rv = 0;
416         char ap_name[32];
417
418         printf("Input a part of AP name to connect : ");
419         rv = scanf("%32s", ap_name);
420
421         rv = wifi_foreach_found_aps(__test_found_connect_ap_callback, ap_name);
422         if (rv != WIFI_ERROR_NONE) {
423                 printf("Fail to connect (can't get AP list) [%d]\n", rv);
424                 return -1;
425         }
426
427         printf("Connection step finished\n");
428         return 1;
429 }
430
431 int test_disconnect_ap(void)
432 {
433         int rv = 0;
434         char ap_name[32];
435
436         printf("Input a part of AP name to disconnect : ");
437         rv = scanf("%32s", ap_name);
438
439         rv = wifi_foreach_found_aps(__test_found_disconnect_ap_callback, ap_name);
440         if (rv != WIFI_ERROR_NONE) {
441                 printf("Fail to disconnect (can't get AP list) [%d]\n", rv);
442                 return -1;
443         }
444
445         printf("Disconnection step finished\n");
446         return 1;
447 }
448
449 int main(int argc, char **argv)
450 {
451         GMainLoop *mainloop;
452         mainloop = g_main_loop_new (NULL, FALSE);
453
454         GIOChannel *channel = g_io_channel_unix_new(0);
455         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread,NULL );
456
457         printf("Test Thread created...\n");
458
459         g_main_loop_run (mainloop);
460
461         return 0;
462 }
463
464 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
465 {
466         int rv = 0;
467         char a[100];
468
469         memset(a, '\0', 100);
470         printf("Event received from stdin\n");
471
472         rv = read(0, a, 100);
473
474         if (rv < 0 || a[0] == '0') exit(1);
475
476         if (*a == '\n' || *a == '\r'){
477                 printf("\n\n Network Connection API Test App\n\n");
478                 printf("Options..\n");
479                 printf("1       - Wi-Fi init and set callbacks\n");
480                 printf("2       - Wi-Fi deinit(unset callbacks automatically)\n");
481                 printf("3       - Activate Wi-Fi device\n");
482                 printf("4       - Deactivate Wi-Fi device\n");
483                 printf("5       - Is Wi-Fi activated?\n");
484                 printf("6       - Get connection state\n");
485                 printf("7       - Get MAC address\n");
486                 printf("8       - Get Wi-Fi interface name\n");
487                 printf("9       - Scan request\n");
488                 printf("a       - Get Connected AP\n");
489                 printf("b       - Get AP list\n");
490                 printf("c       - Connect\n");
491                 printf("d       - Disconnect\n");
492                 printf("0       - Exit \n");
493
494                 printf("ENTER  - Show options menu.......\n");
495         }
496
497         switch (a[0]) {
498                 case '1': {
499                         rv = test_wifi_init();
500                 } break;
501                 case '2': {
502                         rv = test_wifi_deinit();
503                 } break;
504                 case '3': {
505                         rv = test_wifi_activate();
506                 } break;
507                 case '4': {
508                         rv = test_wifi_deactivate();
509                 } break;
510                 case '5': {
511                         rv = test_is_activated();
512                 } break;
513                 case '6': {
514                         rv = test_get_connection_state();
515                 } break;
516                 case '7': {
517                         rv = test_get_mac_address();
518                 } break;
519                 case '8': {
520                         rv = test_get_interface_name();
521                 } break;
522                 case '9': {
523                         rv = test_scan_request();
524                 } break;
525                 case 'a': {
526                         rv = test_get_connected_ap();
527                 } break;
528                 case 'b': {
529                         rv = test_foreach_found_aps();
530                 } break;
531                 case 'c': {
532                         rv = test_connect_ap();
533                 } break;
534                 case 'd': {
535                         rv = test_disconnect_ap();
536                 } break;
537         }
538         return TRUE;
539 }
540