Merge "Change timeout value for dbus reply" into tizen
[platform/core/connectivity/net-config.git] / src / wifi-state.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <vconf.h>
21 #include <vconf-keys.h>
22 #include <bundle.h>
23 #include <bundle_internal.h>
24 #include <eventsystem.h>
25 #include <syspopup_caller.h>
26
27 #include "log.h"
28 #include "util.h"
29 #include "netdbus.h"
30 #include "wifi-state.h"
31 #include "wifi-power.h"
32 #include "netsupplicant.h"
33 #include "network-state.h"
34 #include "wifi-indicator.h"
35 #include "network-statistics.h"
36 #include "wifi-background-scan.h"
37
38 #define NETCONFIG_NETWORK_NOTIFICATION_TIMEOUT  15 * 1000
39
40 static gboolean new_bss_found = FALSE;
41 static guint network_noti_timer_id = 0;
42
43 static wifi_service_state_e g_service_state = NETCONFIG_WIFI_UNKNOWN;
44 static wifi_tech_state_e g_tech_state = NETCONFIG_WIFI_TECH_UNKNOWN;
45
46 static GSList *notifier_list = NULL;
47
48
49 static void __netconfig_pop_wifi_connected_poppup(const char *ssid)
50 {
51         bundle *b = NULL;
52
53         if (ssid == NULL)
54                 return;
55
56         b = bundle_create();
57
58         bundle_add(b, "_SYSPOPUP_TITLE_", "Network connection popup");
59         bundle_add(b, "_SYSPOPUP_TYPE_", "notification");
60         bundle_add(b, "_SYSPOPUP_CONTENT_", "wifi connected");
61         bundle_add(b, "_AP_NAME_", ssid);
62
63         DBG("Launch Wi-Fi connected alert network popup");
64         syspopup_launch("net-popup", b);
65
66         bundle_free(b);
67 }
68
69 static void __set_wifi_connected_essid(void)
70 {
71         const char *essid_name = NULL;
72         const char *wifi_profile = netconfig_get_default_profile();
73
74         if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
75                 return;
76
77         if (wifi_profile == NULL ||
78                         netconfig_is_wifi_profile(wifi_profile) != TRUE) {
79                 ERR("Can't get Wi-Fi profile");
80                 return;
81         }
82
83         essid_name = netconfig_wifi_get_connected_essid(wifi_profile);
84         if (essid_name == NULL) {
85                 ERR("Can't get Wi-Fi name");
86                 return;
87         }
88
89         netconfig_set_vconf_str(VCONFKEY_WIFI_CONNECTED_AP_NAME, essid_name);
90         __netconfig_pop_wifi_connected_poppup(essid_name);
91 }
92
93 static void __unset_wifi_connected_essid(void)
94 {
95         netconfig_set_vconf_str(VCONFKEY_WIFI_CONNECTED_AP_NAME, "");
96 }
97
98 static const char *__get_wifi_connected_essid(void)
99 {
100         const char *essid_name = NULL;
101         const char *wifi_profile = NULL;
102
103         if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
104                 return NULL;
105
106         wifi_profile = netconfig_get_default_profile();
107
108         if (wifi_profile == NULL || netconfig_is_wifi_profile(wifi_profile) != TRUE) {
109                 ERR("Can't get Wi-Fi profile");
110                 return NULL;
111         }
112
113         essid_name = netconfig_wifi_get_connected_essid(wifi_profile);
114         if (essid_name == NULL) {
115                 ERR("Can't get Wi-Fi name");
116                 return NULL;
117         }
118
119         return essid_name;
120 }
121
122 static gboolean __is_wifi_profile_available(void)
123 {
124         GVariant *message = NULL;
125         GVariantIter *iter, *next;
126         gchar *obj;
127
128         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
129                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
130                         "GetServices", NULL);
131         if (message == NULL) {
132                 ERR("Failed to get service list");
133                 return FALSE;
134         }
135
136         g_variant_get(message, "(a(oa{sv}))", &iter);
137         while (g_variant_iter_loop(iter, "(oa{sv})", &obj, &next)) {
138                 if (obj == NULL || netconfig_is_wifi_profile((const gchar*)obj) == FALSE)
139                         continue;
140
141                 g_variant_iter_free(next);
142                 g_free(obj);
143                 break;
144         }
145
146         g_variant_unref(message);
147
148         g_variant_iter_free(iter);
149
150         return TRUE;
151 }
152
153 static gboolean __is_favorited(GVariantIter *array)
154 {
155         gboolean is_favorite = FALSE;
156         gchar *key;
157         GVariant *var;
158
159         while (g_variant_iter_loop(array, "{sv}", &key, &var)) {
160                 gboolean value;
161
162                 if (g_str_equal(key, "Favorite") != TRUE)
163                         continue;
164
165                 value = g_variant_get_boolean(var);
166                 if (value)
167                         is_favorite = TRUE;
168                 g_free(key);
169                 g_variant_unref(var);
170                 break;
171         }
172
173         return is_favorite;
174 }
175
176 static void _wifi_state_connected_activation(void)
177 {
178         /* Add activation of services when Wi-Fi is connected */
179 }
180
181 static void _wifi_state_changed(wifi_service_state_e state)
182 {
183         GSList *list;
184
185         for (list = notifier_list; list; list = list->next) {
186                 wifi_state_notifier *notifier = list->data;
187
188                 if (notifier->wifi_state_changed != NULL)
189                         notifier->wifi_state_changed(state, notifier->user_data);
190         }
191 }
192
193 static void _set_bss_found(gboolean found)
194 {
195         if (found != new_bss_found)
196                 new_bss_found = found;
197 }
198
199 static gboolean _check_network_notification(gpointer data)
200 {
201         int qs_enable = 0, ug_state = 0;
202         static gboolean check_again = FALSE;
203
204         wifi_tech_state_e tech_state;
205         wifi_service_state_e service_state;
206
207         tech_state = wifi_state_get_technology_state();
208         if (tech_state < NETCONFIG_WIFI_TECH_POWERED) {
209                 DBG("Wi-Fi off or WPS only supported[%d]", tech_state);
210                 goto cleanup;
211         }
212
213         service_state = wifi_state_get_service_state();
214         if (service_state == NETCONFIG_WIFI_CONNECTED) {
215                 DBG("Service state is connected");
216                 goto cleanup;
217         } else if (service_state == NETCONFIG_WIFI_ASSOCIATION ||
218                 service_state == NETCONFIG_WIFI_CONFIGURATION) {
219                 DBG("Service state is connecting (check again : %d)", check_again);
220                 if (!check_again) {
221                         check_again = TRUE;
222                         return TRUE;
223                 } else
224                         check_again = FALSE;
225         }
226
227         if (__is_wifi_profile_available() == FALSE) {
228                 netconfig_send_notification_to_net_popup(
229                 NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
230                 goto cleanup;
231         }
232
233         vconf_get_int(VCONFKEY_WIFI_ENABLE_QS, &qs_enable);
234         if (qs_enable != VCONFKEY_WIFI_QS_ENABLE) {
235                 DBG("qs_enable != VCONFKEY_WIFI_QS_ENABLE");
236                 goto cleanup;
237         }
238
239         vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &ug_state);
240         if (ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
241                 goto cleanup;
242
243         netconfig_send_notification_to_net_popup(NETCONFIG_ADD_FOUND_AP_NOTI, NULL);
244
245         _set_bss_found(FALSE);
246
247 cleanup:
248         netconfig_stop_timer(&network_noti_timer_id);
249         return FALSE;
250 }
251
252 static char *_get_connman_favorite_service(void)
253 {
254         char *favorite_service = NULL;
255         GVariant *message = NULL;
256         gchar *obj;
257         GVariantIter *iter, *next;
258
259         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
260                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
261                         "GetServices", NULL);
262         if (message == NULL) {
263                 ERR("Failed to get service list");
264                 return NULL;
265         }
266
267         g_variant_get(message, "(a(oa{sv}))", &iter);
268         while (g_variant_iter_loop(iter, "(oa{sv})", &obj, &next)) {
269                 if (obj == NULL || netconfig_is_wifi_profile(obj) == FALSE)
270                         continue;
271
272                 if (__is_favorited(next) == TRUE) {
273                         favorite_service = g_strdup(obj);
274                         g_free(obj);
275                         g_variant_iter_free(next);
276                         break;
277                 }
278         }
279
280         g_variant_iter_free(iter);
281         g_variant_unref(message);
282
283         return favorite_service;
284 }
285
286 static void __notification_value_changed_cb(keynode_t *node, void *user_data)
287 {
288         int value = -1;
289
290         if (vconf_get_int(VCONFKEY_WIFI_ENABLE_QS, &value) < 0)
291                 return;
292
293         if (value == VCONFKEY_WIFI_QS_DISABLE)
294                 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
295 }
296
297 static void _register_network_notification(void)
298 {
299 #if defined TIZEN_WEARABLE
300         return;
301 #endif
302         vconf_notify_key_changed(VCONFKEY_WIFI_ENABLE_QS, __notification_value_changed_cb, NULL);
303 }
304
305 static void _deregister_network_notification(void)
306 {
307 #if defined TIZEN_WEARABLE
308                 return;
309 #endif
310         vconf_ignore_key_changed(VCONFKEY_WIFI_ENABLE_QS, __notification_value_changed_cb);
311 }
312
313 static void _set_power_save(gboolean power_save)
314 {
315         gboolean result;
316         const char *if_path;
317         GVariant *input_args = NULL;
318         static gboolean old_state = TRUE;
319         const gchar *args_disable = "POWERMODE 1";
320         const gchar *args_enable = "POWERMODE 0";
321         if (old_state == power_save)
322                 return;
323
324         if_path = netconfig_wifi_get_supplicant_interface();
325         if (if_path == NULL) {
326                 ERR("Fail to get wpa_supplicant DBus path");
327                 return;
328         }
329
330         if (power_save)
331                 input_args = g_variant_new_string(args_enable);
332         else
333                 input_args = g_variant_new_string(args_disable);
334
335         result = netconfig_supplicant_invoke_dbus_method_nonblock(
336                         SUPPLICANT_SERVICE,
337                         if_path,
338                         SUPPLICANT_INTERFACE ".Interface",
339                         "Driver",
340                         input_args,
341                         NULL);
342         if (result == FALSE)
343                 ERR("Fail to set power save mode POWERMODE %d", power_save);
344         else
345                 old_state = power_save;
346
347         return;
348 }
349
350 static void _set_power_lock(gboolean power_lock)
351 {
352         gint32 ret = 0;
353         GVariant *reply;
354         GVariant *params;
355         char state[] = "lcdoff";
356         char flag[] = "staycurstate";
357         char standby[] = "NULL";
358         int timeout = 0;
359         char sleepmargin[] = "sleepmargin";
360
361         const char *lockstate = "lockstate";
362         const char *unlockstate = "unlockstate";
363         static gboolean old_state = FALSE;
364         const char *lock_method;
365
366         if (old_state == power_lock)
367                 return;
368
369         if (power_lock == TRUE) {
370                 /* deviced power lock enable */
371                 params = g_variant_new("(sssi)", state, flag, standby, timeout);
372
373                 lock_method = lockstate;
374         } else {
375                 /* deviced power lock disable */
376                 params = g_variant_new("(ss)", state, sleepmargin);
377
378                 lock_method = unlockstate;
379         }
380
381         reply = netconfig_invoke_dbus_method(
382                         "org.tizen.system.deviced",
383                         "/Org/Tizen/System/DeviceD/Display",
384                         "org.tizen.system.deviced.display",
385                         lock_method,
386                         params);
387         if (reply == NULL) {
388                 ERR("Failed to set_power_lock");
389                 return;
390         }
391
392         ret = g_variant_get_int32(reply);
393         if (ret < 0)
394                 ERR("Failed to set power lock %s with ret %d",
395                                 power_lock == TRUE ? "enable" : "disable", ret);
396         else
397                 old_state = power_lock;
398
399         g_variant_unref(reply);
400
401         return;
402 }
403
404 void wifi_state_emit_power_completed(gboolean power_on)
405 {
406         if (power_on)
407                 wifi_emit_power_on_completed((Wifi *)get_wifi_object());
408         else
409                 wifi_emit_power_off_completed((Wifi *)get_wifi_object());
410
411         DBG("Successfully sent signal [%s]", (power_on) ? "powerOn" : "powerOff");
412 }
413
414 void wifi_state_emit_power_failed(void)
415 {
416         wifi_emit_power_operation_failed((Wifi *)get_wifi_object());
417
418         DBG("Successfully sent signal [PowerOperationFailed]");
419 }
420
421 void wifi_state_update_power_state(gboolean powered)
422 {
423         wifi_tech_state_e tech_state;
424
425         /* It's automatically updated by signal-handler
426          * DO NOT update manually
427          * It includes Wi-Fi state configuration
428          */
429         tech_state = wifi_state_get_technology_state();
430
431         if (powered == TRUE) {
432                 if (tech_state < NETCONFIG_WIFI_TECH_POWERED && netconfig_is_wifi_tethering_on() != TRUE) {
433                         DBG("Wi-Fi turned on or waken up from power-save mode");
434
435                         wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_POWERED);
436
437                         wifi_state_emit_power_completed(TRUE);
438
439                         netconfig_wifi_device_picker_service_start();
440
441                         netconfig_set_vconf_int(VCONF_WIFI_LAST_POWER_STATE, VCONFKEY_WIFI_UNCONNECTED);
442                         netconfig_set_vconf_int(VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_UNCONNECTED);
443                         netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_NOT_CONNECTED);
444
445                         netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_ON);
446
447                         netconfig_wifi_bgscan_stop();
448                         netconfig_wifi_bgscan_start(TRUE);
449
450                         /* Add callback to track change in notification setting */
451                         _register_network_notification();
452                 }
453         } else if (tech_state > NETCONFIG_WIFI_TECH_OFF) {
454                 DBG("Wi-Fi turned off or in power-save mode");
455
456                 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
457
458                 netconfig_wifi_device_picker_service_stop();
459
460                 wifi_power_disable_technology_state_by_only_connman_signal();
461                 wifi_power_driver_and_supplicant(FALSE);
462
463                 wifi_state_emit_power_completed(FALSE);
464
465                 netconfig_set_vconf_int(VCONF_WIFI_LAST_POWER_STATE, VCONFKEY_WIFI_OFF);
466                 netconfig_set_vconf_int(VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_OFF);
467                 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_OFF);
468
469                 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_OFF);
470
471                 netconfig_wifi_set_bgscan_pause(FALSE);
472                 netconfig_wifi_bgscan_stop();
473
474                 _set_bss_found(FALSE);
475
476                 /* Inform net-popup to remove the wifi found notification */
477                 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
478                 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_PORTAL_NOTI, NULL);
479
480                 _deregister_network_notification();
481         }
482 }
483
484 char *wifi_get_favorite_service(void)
485 {
486         return _get_connman_favorite_service();
487 }
488
489 void wifi_start_timer_network_notification(void)
490 {
491 #if defined TIZEN_WEARABLE
492                 /* In case of wearable device, no need to notify available Wi-Fi APs */
493                 return ;
494 #endif
495         netconfig_start_timer(NETCONFIG_NETWORK_NOTIFICATION_TIMEOUT, _check_network_notification, NULL, &network_noti_timer_id);
496 }
497
498 void wifi_state_notifier_register(wifi_state_notifier *notifier)
499 {
500         DBG("register notifier");
501
502         notifier_list = g_slist_append(notifier_list, notifier);
503 }
504
505 void wifi_state_notifier_unregister(wifi_state_notifier *notifier)
506 {
507         DBG("un-register notifier");
508
509         notifier_list = g_slist_remove_all(notifier_list, notifier);
510 }
511
512 void wifi_state_notifier_cleanup(void)
513 {
514         g_slist_free_full(notifier_list, NULL);
515 }
516
517 void wifi_state_set_bss_found(gboolean found)
518 {
519         _set_bss_found(found);
520 }
521
522 gboolean wifi_state_is_bss_found(void)
523 {
524         return new_bss_found;
525 }
526
527 void wifi_state_set_service_state(wifi_service_state_e new_state)
528 {
529         static gboolean dhcp_stage = FALSE;
530         wifi_service_state_e old_state = g_service_state;
531
532         if (old_state == new_state)
533                 return;
534
535         g_service_state = new_state;
536         DBG("Wi-Fi state %d ==> %d", old_state, new_state);
537
538         /* During DHCP, temporarily disable Wi-Fi power saving */
539         if ((old_state < NETCONFIG_WIFI_ASSOCIATION || old_state == NETCONFIG_WIFI_FAILURE) && new_state == NETCONFIG_WIFI_CONFIGURATION) {
540                 _set_power_lock(TRUE);
541                 _set_power_save(FALSE);
542                 dhcp_stage = TRUE;
543         } else if (dhcp_stage == TRUE) {
544                 _set_power_lock(FALSE);
545                 _set_power_save(TRUE);
546                 dhcp_stage = FALSE;
547         }
548
549         if (new_state == NETCONFIG_WIFI_CONNECTED) {
550                 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
551
552                 netconfig_set_vconf_int(VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_CONNECTED);
553                 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_CONNECTED);
554
555                 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_CONNECTED);
556
557                 __set_wifi_connected_essid();
558
559                 netconfig_wifi_indicator_start();
560         } else if (old_state == NETCONFIG_WIFI_CONNECTED) {
561                 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_PORTAL_NOTI, NULL);
562
563                 __unset_wifi_connected_essid();
564
565                 netconfig_set_vconf_int (VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_UNCONNECTED);
566                 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_NOT_CONNECTED);
567
568                 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_ON);
569
570                 netconfig_wifi_indicator_stop();
571
572                 netconfig_wifi_set_bgscan_pause(FALSE);
573
574                 netconfig_wifi_bgscan_stop();
575                 netconfig_wifi_bgscan_start(TRUE);
576         } else if ((old_state > NETCONFIG_WIFI_IDLE && old_state < NETCONFIG_WIFI_CONNECTED) && new_state == NETCONFIG_WIFI_IDLE) {
577                 /* in ipv6 case disconnect/association -> association */
578                 DBG("reset the bg scan period");
579                 netconfig_wifi_set_bgscan_pause(FALSE);
580
581                 netconfig_wifi_bgscan_stop();
582                 netconfig_wifi_bgscan_start(TRUE);
583         }
584
585         _wifi_state_changed(new_state);
586
587         if (new_state == NETCONFIG_WIFI_CONNECTED) {
588                 _wifi_state_connected_activation();
589 #if defined TIZEN_WEARABLE
590                 wc_launch_syspopup(WC_POPUP_TYPE_WIFI_CONNECTED);
591 #endif
592         }
593 }
594
595 wifi_service_state_e wifi_state_get_service_state(void)
596 {
597         return g_service_state;
598 }
599
600 void wifi_state_set_tech_state(wifi_tech_state_e new_state)
601 {
602         wifi_tech_state_e old_state = g_tech_state;
603
604         if (old_state == new_state)
605                 return;
606
607         g_tech_state = new_state;
608
609         DBG("Wi-Fi technology state %d ==> %d", old_state, new_state);
610 }
611
612 wifi_tech_state_e wifi_state_get_technology_state(void)
613 {
614         GVariant *message = NULL, *variant;
615         GVariantIter *iter, *next;
616         wifi_tech_state_e ret = NETCONFIG_WIFI_TECH_OFF;
617         gboolean wifi_tech_powered = FALSE;
618         gboolean wifi_tech_connected = FALSE;
619         const char *path;
620         gchar *key;
621
622         if (g_tech_state > NETCONFIG_WIFI_TECH_UNKNOWN)
623                 return g_tech_state;
624
625         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
626                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
627                         "GetTechnologies", NULL);
628         if (message == NULL) {
629                 ERR("Failed to get_technology_state");
630                 return NETCONFIG_WIFI_TECH_UNKNOWN;
631         }
632
633         g_variant_get(message, "(a(oa{sv}))", &iter);
634         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
635                 if (path == NULL || g_strcmp0(path, CONNMAN_WIFI_TECHNOLOGY_PREFIX) != 0)
636                         continue;
637
638                 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
639                         const gchar *sdata = NULL;
640                         gboolean data;
641
642                         if (g_variant_is_of_type(variant, G_VARIANT_TYPE_BOOLEAN)) {
643                                 data = g_variant_get_boolean(variant);
644                                 DBG("key-[%s] - %s", key, data ? "True" : "False");
645
646                                 if (strcmp(key, "Powered") == 0 && data)
647                                         wifi_tech_powered = TRUE;
648                                 else if (strcmp(key, "Connected") == 0 && data)
649                                         wifi_tech_connected = TRUE;
650                                 /* For further use
651                                 else if (strcmp(key, "Tethering") == 0 && data) {
652                                 } */
653                         } else if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
654                                 sdata = g_variant_get_string(variant, NULL);
655                                 DBG("%s", sdata);
656                         }
657                 }
658                 g_variant_iter_free(next);
659         }
660
661         g_variant_unref(message);
662
663         g_variant_iter_free(iter);
664
665         if (wifi_tech_powered == TRUE)
666                 ret = NETCONFIG_WIFI_TECH_POWERED;
667
668         if (wifi_tech_connected == TRUE)
669                 ret = NETCONFIG_WIFI_TECH_CONNECTED;
670
671         g_tech_state = ret;
672
673         return g_tech_state;
674 }
675
676 void wifi_state_set_connected_essid(void)
677 {
678         __set_wifi_connected_essid();
679 #if defined TIZEN_WEARABLE
680         wc_launch_syspopup(WC_POPUP_TYPE_WIFI_CONNECTED);
681 #endif
682 }
683
684 void wifi_state_get_connected_essid(gchar **essid)
685 {
686         *essid = g_strdup(__get_wifi_connected_essid());
687 }
688
689 /*      wifi_connection_state_e in CAPI
690  *
691  *      WIFI_CONNECTION_STATE_FAILURE           = -1
692  *      WIFI_CONNECTION_STATE_DISCONNECTED      = 0
693  *      WIFI_CONNECTION_STATE_ASSOCIATION       = 1
694  *      WIFI_CONNECTION_STATE_CONFIGURATION     = 2
695  *      WIFI_CONNECTION_STATE_CONNECTED         = 3
696  */
697 /*      connection_wifi_state_e in CAPI
698  *
699  *      CONNECTION_WIFI_STATE_DEACTIVATED       = 0
700  *      CONNECTION_WIFI_STATE_DISCONNECTED      = 1
701  *      CONNECTION_WIFI_STATE_CONNECTED         = 2
702  */
703 gboolean handle_get_wifi_state(Wifi *wifi, GDBusMethodInvocation *context)
704 {
705         g_return_val_if_fail(wifi != NULL, FALSE);
706         GVariant *param = NULL;
707         wifi_tech_state_e tech_state = NETCONFIG_WIFI_TECH_UNKNOWN;
708         wifi_service_state_e service_state = NETCONFIG_WIFI_UNKNOWN;
709         tech_state = wifi_state_get_technology_state();
710         service_state = wifi_state_get_service_state();
711
712         if (tech_state == NETCONFIG_WIFI_TECH_UNKNOWN)
713                 param = g_variant_new("(s)", "unknown");
714         else if (tech_state == NETCONFIG_WIFI_TECH_OFF ||
715                 tech_state == NETCONFIG_WIFI_TECH_WPS_ONLY)
716                 param = g_variant_new("(s)", "deactivated");
717         else if (tech_state == NETCONFIG_WIFI_TECH_CONNECTED)
718                 param = g_variant_new("(s)", "connected");
719         else {
720                 switch (service_state) {
721                 case NETCONFIG_WIFI_FAILURE:
722                         param = g_variant_new("(s)", "failure");
723                         break;
724                 case NETCONFIG_WIFI_ASSOCIATION:
725                         param = g_variant_new("(s)", "association");
726                         break;
727                 case NETCONFIG_WIFI_CONFIGURATION:
728                         param = g_variant_new("(s)", "configuration");
729                         break;
730                 case NETCONFIG_WIFI_CONNECTED:
731                         param = g_variant_new("(s)", "connected");
732                         break;
733                 case NETCONFIG_WIFI_UNKNOWN:
734                 case NETCONFIG_WIFI_IDLE:
735                 default:
736                         param = g_variant_new("(s)", "disconnected");
737                 }
738         }
739
740         g_dbus_method_invocation_return_value(context, param);
741
742         return TRUE;
743 }