2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/socket.h>
27 #include <sys/ioctl.h>
32 #include <tizen_error.h>
35 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data);
38 static void __test_device_state_callback(wifi_error_e error_code, wifi_device_state_e state, bool is_requested, void* user_data)
40 printf("Device state changed callback, error code : %d, requested : %d", error_code, is_requested);
42 if (state == WIFI_DEVICE_STATE_ACTIVATED)
43 printf(", state : Activated\n");
45 printf(", state : Deactivated\n");
48 static void __test_bg_scan_completed_callback(wifi_error_e error_code, void* user_data)
50 printf("Background Scan Completed, error code : %d\n", error_code);
53 static void __test_scan_request_callback(wifi_error_e error_code, void* user_data)
55 printf("Scan Completed from scan request, error code : %d\n", error_code);
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)
63 printf("Connection state changed callback, error code : %d, requested : %d", error_code, is_requested);
66 case WIFI_CONNECTION_STATE_CONNECTING:
67 printf(", state : Connecting");
69 case WIFI_CONNECTION_STATE_CONNECTED:
70 printf(", state : Connected");
72 case WIFI_CONNECTION_STATE_DISCONNECTING:
73 printf(", state : Disconnecting");
75 case WIFI_CONNECTION_STATE_DISCONNECTED:
76 printf(", state : Disconnected");
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);
84 printf(", AP name : %s\n", ap_name);
89 static void __test_rssi_level_callback(wifi_rssi_level_e rssi_level, void* user_data)
91 printf("RSSI level changed callback, level = %d\n", rssi_level);
94 static const char* __test_print_state(wifi_connection_state_e state)
97 case WIFI_CONNECTION_STATE_DISCONNECTED:
98 return "Disconnected";
99 case WIFI_CONNECTION_STATE_CONNECTING:
101 case WIFI_CONNECTION_STATE_CONNECTED:
103 case WIFI_CONNECTION_STATE_DISCONNECTING:
104 return "Disconnecting";
110 static bool __test_found_ap_callback(wifi_ap_h ap, void *user_data)
113 char *ap_name = NULL;
114 wifi_connection_state_e state;
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);
122 rv = wifi_ap_get_connection_state(ap, &state);
123 if (rv != WIFI_ERROR_NONE) {
124 printf("Fail to get State [%d]\n", rv);
129 printf("AP name : %s, state : %s\n", ap_name, __test_print_state(state));
135 static bool __test_found_connect_ap_callback(wifi_ap_h ap, void *user_data)
138 char *ap_name = NULL;
139 char *ap_name_part = (char*)user_data;
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);
147 if (strstr(ap_name, ap_name_part) != NULL) {
148 bool required = false;
149 wifi_ap_is_passphrase_required(ap, &required);
152 char passphrase[100];
153 printf("Input passphrase for %s : ", ap_name);
154 rv = scanf("%100s", passphrase);
156 rv = wifi_ap_set_passphrase(ap, passphrase);
157 if (rv != WIFI_ERROR_NONE) {
158 printf("Fail to set passphrase : %d\n", rv);
164 rv = wifi_connect(ap);
165 if (rv != WIFI_ERROR_NONE)
166 printf("Fail to connect [%s] : %d\n", ap_name, rv);
168 printf("Success to connect [%s]\n", ap_name);
178 static bool __test_found_disconnect_ap_callback(wifi_ap_h ap, void *user_data)
181 char *ap_name = NULL;
182 char *ap_name_part = (char*)user_data;
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);
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);
195 printf("Success to disconnect %s\n", ap_name);
205 int test_wifi_init(void)
207 int rv = wifi_initialize();
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);
215 printf("Wifi init failed [%d]\n", rv);
219 printf("Wifi init succeeded\n");
223 int test_wifi_deinit(void)
227 rv = wifi_deinitialize();
229 if (rv != WIFI_ERROR_NONE){
230 printf("Wifi deinit failed [%d]\n", rv);
234 printf("Wifi deinit succeeded\n");
238 int test_wifi_activate(void)
242 rv = wifi_activate();
244 if (rv != WIFI_ERROR_NONE) {
245 printf("Fail to activate Wi-Fi device [%d]\n", rv);
249 printf("Success to activate Wi-Fi device\n");
254 int test_wifi_deactivate(void)
258 rv = wifi_deactivate();
260 if (rv != WIFI_ERROR_NONE) {
261 printf("Fail to deactivate Wi-Fi device [%d]\n", rv);
265 printf("Success to deactivate Wi-Fi device\n");
270 int test_is_activated(void)
275 rv = wifi_is_activated(&state);
277 if (rv != WIFI_ERROR_NONE) {
278 printf("Fail to get Wi-Fi device state [%d]\n", rv);
282 printf("Success to get Wi-Fi device state : %s\n", (state) ? "TRUE" : "FALSE");
287 int test_get_connection_state(void)
290 wifi_connection_state_e connection_state;
292 rv = wifi_get_connection_state(&connection_state);
294 if (rv != WIFI_ERROR_NONE) {
295 printf("Fail to get connection state [%d]\n", rv);
299 printf("Success to get connection state : ");
300 switch (connection_state) {
301 case WIFI_CONNECTION_STATE_CONNECTING:
302 printf("Connecting\n");
304 case WIFI_CONNECTION_STATE_CONNECTED:
305 printf("Connected\n");
307 case WIFI_CONNECTION_STATE_DISCONNECTING:
308 printf("Disconnecting\n");
310 case WIFI_CONNECTION_STATE_DISCONNECTED:
311 printf("Disconnected\n");
320 int test_get_mac_address(void)
323 char *mac_addr = NULL;
325 rv = wifi_get_mac_address(&mac_addr);
327 if (rv != WIFI_ERROR_NONE) {
328 printf("Fail to get MAC address [%d]\n", rv);
332 printf("MAC address : %s\n", mac_addr);
338 int test_get_interface_name(void)
341 char *if_name = NULL;
343 rv = wifi_get_network_interface_name(&if_name);
345 if (rv != WIFI_ERROR_NONE) {
346 printf("Fail to get Interface name [%d]\n", rv);
350 printf("Interface name : %s\n", if_name);
356 int test_scan_request(void)
360 rv = wifi_scan(__test_scan_request_callback, NULL);
362 if (rv != WIFI_ERROR_NONE) {
363 printf("Scan request failed [%d]\n", rv);
367 printf("Scan request succeeded\n");
372 int test_get_connected_ap(void)
375 char *ap_name = NULL;
378 rv = wifi_get_connected_ap(&ap_h);
379 if (rv != WIFI_ERROR_NONE) {
380 printf("Fail to get connected AP [%d]\n", rv);
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);
391 printf("Connected AP : %s\n", ap_name);
393 wifi_ap_destroy(ap_h);
398 int test_foreach_found_aps(void)
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);
408 printf("Get AP list finished\n");
413 int test_connect_ap(void)
418 printf("Input a part of AP name to connect : ");
419 rv = scanf("%32s", ap_name);
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);
427 printf("Connection step finished\n");
431 int test_disconnect_ap(void)
436 printf("Input a part of AP name to disconnect : ");
437 rv = scanf("%32s", ap_name);
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);
445 printf("Disconnection step finished\n");
449 int main(int argc, char **argv)
452 mainloop = g_main_loop_new (NULL, FALSE);
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 );
457 printf("Test Thread created...\n");
459 g_main_loop_run (mainloop);
464 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
469 memset(a, '\0', 100);
470 printf("Event received from stdin\n");
472 rv = read(0, a, 100);
474 if (rv < 0 || a[0] == '0') exit(1);
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");
494 printf("ENTER - Show options menu.......\n");
499 rv = test_wifi_init();
502 rv = test_wifi_deinit();
505 rv = test_wifi_activate();
508 rv = test_wifi_deactivate();
511 rv = test_is_activated();
514 rv = test_get_connection_state();
517 rv = test_get_mac_address();
520 rv = test_get_interface_name();
523 rv = test_scan_request();
526 rv = test_get_connected_ap();
529 rv = test_foreach_found_aps();
532 rv = test_connect_ap();
535 rv = test_disconnect_ap();