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