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