2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <vconf-keys.h>
23 #include <bundle_internal.h>
24 #include <eventsystem.h>
25 #include <syspopup_caller.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"
38 #define NETCONFIG_NETWORK_NOTIFICATION_TIMEOUT 15 * 1000
40 static gboolean new_bss_found = FALSE;
41 static guint network_noti_timer_id = 0;
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;
46 static GSList *notifier_list = NULL;
49 static void __netconfig_pop_wifi_connected_poppup(const char *ssid)
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);
63 DBG("Launch Wi-Fi connected alert network popup");
64 syspopup_launch("net.netpopup", b);
69 static void __set_wifi_connected_essid(void)
71 const char *essid_name = NULL;
72 const char *wifi_profile = netconfig_get_default_profile();
74 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
77 if (wifi_profile == NULL ||
78 netconfig_is_wifi_profile(wifi_profile) != TRUE) {
79 ERR("Can't get Wi-Fi profile");
83 essid_name = netconfig_wifi_get_connected_essid(wifi_profile);
84 if (essid_name == NULL) {
85 ERR("Can't get Wi-Fi name");
89 netconfig_set_vconf_str(VCONFKEY_WIFI_CONNECTED_AP_NAME, essid_name);
90 __netconfig_pop_wifi_connected_poppup(essid_name);
93 static void __unset_wifi_connected_essid(void)
95 netconfig_set_vconf_str(VCONFKEY_WIFI_CONNECTED_AP_NAME, "");
98 static const char *__get_wifi_connected_essid(void)
100 const char *essid_name = NULL;
101 const char *wifi_profile = NULL;
103 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
106 wifi_profile = netconfig_get_default_profile();
108 if (wifi_profile == NULL || netconfig_is_wifi_profile(wifi_profile) != TRUE) {
109 ERR("Can't get Wi-Fi profile");
113 essid_name = netconfig_wifi_get_connected_essid(wifi_profile);
114 if (essid_name == NULL) {
115 ERR("Can't get Wi-Fi name");
122 static gboolean __is_wifi_profile_available(void)
124 GVariant *message = NULL;
125 GVariantIter *iter, *next;
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");
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) {
142 g_variant_iter_free(next);
147 g_variant_unref(message);
149 g_variant_iter_free(iter);
154 static gboolean __is_favorited(GVariantIter *array)
156 gboolean is_favorite = FALSE;
160 while (g_variant_iter_loop(array, "{sv}", &key, &var)) {
163 if (g_str_equal(key, "Favorite") != TRUE) {
167 value = g_variant_get_boolean(var);
171 g_variant_unref(var);
178 static void _wifi_state_connected_activation(void)
180 /* Add activation of services when Wi-Fi is connected */
183 static void _wifi_state_changed(wifi_service_state_e state)
187 for (list = notifier_list; list; list = list->next) {
188 wifi_state_notifier *notifier = list->data;
190 if (notifier->wifi_state_changed != NULL)
191 notifier->wifi_state_changed(state, notifier->user_data);
195 static void _set_bss_found(gboolean found)
197 if (found != new_bss_found)
198 new_bss_found = found;
201 static gboolean _check_network_notification(gpointer data)
203 int qs_enable = 0, ug_state = 0;
204 static gboolean check_again = FALSE;
206 wifi_tech_state_e tech_state;
207 wifi_service_state_e service_state;
209 tech_state = wifi_state_get_technology_state();
210 if (tech_state < NETCONFIG_WIFI_TECH_POWERED) {
211 DBG("Wi-Fi off or WPS only supported[%d]", tech_state);
215 service_state = wifi_state_get_service_state();
216 if (service_state == NETCONFIG_WIFI_CONNECTED) {
217 DBG("Service state is connected");
219 } else if (service_state == NETCONFIG_WIFI_ASSOCIATION ||
220 service_state == NETCONFIG_WIFI_CONFIGURATION) {
221 DBG("Service state is connecting (check again : %d)", check_again);
229 if (__is_wifi_profile_available() == FALSE) {
230 netconfig_send_notification_to_net_popup(
231 NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
235 vconf_get_int(VCONFKEY_WIFI_ENABLE_QS, &qs_enable);
236 if (qs_enable != VCONFKEY_WIFI_QS_ENABLE) {
237 DBG("qs_enable != VCONFKEY_WIFI_QS_ENABLE");
241 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &ug_state);
242 if (ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND) {
246 netconfig_send_notification_to_net_popup(NETCONFIG_ADD_FOUND_AP_NOTI, NULL);
248 _set_bss_found(FALSE);
251 netconfig_stop_timer(&network_noti_timer_id);
255 static char *_get_connman_favorite_service(void)
257 char *favorite_service = NULL;
258 GVariant *message = NULL;
260 GVariantIter *iter, *next;
262 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
263 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
264 "GetServices", NULL);
265 if (message == NULL) {
266 ERR("Failed to get service list");
270 g_variant_get(message, "(a(oa{sv}))", &iter);
271 while (g_variant_iter_loop(iter, "(oa{sv})", &obj, &next)) {
272 if (obj == NULL || netconfig_is_wifi_profile(obj) == FALSE) {
276 if (__is_favorited(next) == TRUE) {
277 favorite_service = g_strdup(obj);
279 g_variant_iter_free(next);
284 g_variant_iter_free(iter);
285 g_variant_unref(message);
287 return favorite_service;
290 static void __notification_value_changed_cb(keynode_t *node, void *user_data)
294 if (vconf_get_int(VCONFKEY_WIFI_ENABLE_QS, &value) < 0) {
298 if (value == VCONFKEY_WIFI_QS_DISABLE) {
299 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
303 static void _register_network_notification(void)
305 #if defined TIZEN_WEARABLE
308 vconf_notify_key_changed(VCONFKEY_WIFI_ENABLE_QS, __notification_value_changed_cb, NULL);
311 static void _deregister_network_notification(void)
313 #if defined TIZEN_WEARABLE
316 vconf_ignore_key_changed(VCONFKEY_WIFI_ENABLE_QS, __notification_value_changed_cb);
319 static void _set_power_save(gboolean power_save)
323 GVariant *input_args = NULL;
324 static gboolean old_state = TRUE;
325 const gchar *args_disable = "POWERMODE 1";
326 const gchar *args_enable = "POWERMODE 0";
327 if (old_state == power_save)
330 if_path = netconfig_wifi_get_supplicant_interface();
331 if (if_path == NULL) {
332 ERR("Fail to get wpa_supplicant DBus path");
337 input_args = g_variant_new_string(args_enable);
339 input_args = g_variant_new_string(args_disable);
341 result = netconfig_supplicant_invoke_dbus_method_nonblock(
344 SUPPLICANT_INTERFACE ".Interface",
349 ERR("Fail to set power save mode POWERMODE %d", power_save);
351 old_state = power_save;
356 static void _set_power_lock(gboolean power_lock)
361 char state[] = "lcdoff";
362 char flag[] = "staycurstate";
363 char standby[] = "NULL";
365 char sleepmargin[] = "sleepmargin";
367 const char *lockstate = "lockstate";
368 const char *unlockstate = "unlockstate";
369 static gboolean old_state = FALSE;
370 const char *lock_method;
372 if (old_state == power_lock)
375 if (power_lock == TRUE) {
376 /* deviced power lock enable */
377 params = g_variant_new("(sssi)", state, flag, standby, timeout);
379 lock_method = lockstate;
381 /* deviced power lock disable */
382 params = g_variant_new("(ss)", state, sleepmargin);
384 lock_method = unlockstate;
387 reply = netconfig_invoke_dbus_method(
388 "org.tizen.system.deviced",
389 "/Org/Tizen/System/DeviceD/Display",
390 "org.tizen.system.deviced.display",
394 ERR("Failed to set_power_lock");
398 ret = g_variant_get_int32(reply);
400 ERR("Failed to set power lock %s with ret %d",
401 power_lock == TRUE ? "enable" : "disable", ret);
403 old_state = power_lock;
405 g_variant_unref(reply);
410 void wifi_state_emit_power_completed(gboolean power_on)
413 wifi_emit_power_on_completed((Wifi *)get_wifi_object());
415 wifi_emit_power_off_completed((Wifi *)get_wifi_object());
417 DBG("Successfully sent signal [%s]",(power_on)?"powerOn":"powerOff");
420 void wifi_state_emit_power_failed(void)
422 wifi_emit_power_operation_failed((Wifi *)get_wifi_object());
424 DBG("Successfully sent signal [PowerOperationFailed]");
427 void wifi_state_update_power_state(gboolean powered)
429 wifi_tech_state_e tech_state;
431 /* It's automatically updated by signal-handler
432 * DO NOT update manually
433 * It includes Wi-Fi state configuration
435 tech_state = wifi_state_get_technology_state();
437 if (powered == TRUE) {
438 if (tech_state < NETCONFIG_WIFI_TECH_POWERED && netconfig_is_wifi_tethering_on() != TRUE) {
439 DBG("Wi-Fi turned on or waken up from power-save mode");
441 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_POWERED);
443 wifi_state_emit_power_completed(TRUE);
445 netconfig_wifi_device_picker_service_start();
447 netconfig_set_vconf_int(VCONF_WIFI_LAST_POWER_STATE, VCONFKEY_WIFI_UNCONNECTED);
448 netconfig_set_vconf_int(VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_UNCONNECTED);
449 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_NOT_CONNECTED);
451 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_ON);
453 netconfig_wifi_bgscan_stop();
454 netconfig_wifi_bgscan_start(TRUE);
456 /* Add callback to track change in notification setting */
457 _register_network_notification();
459 } else if (tech_state > NETCONFIG_WIFI_TECH_OFF) {
460 DBG("Wi-Fi turned off or in power-save mode");
462 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
464 netconfig_wifi_device_picker_service_stop();
466 wifi_power_disable_technology_state_by_only_connman_signal();
467 wifi_power_driver_and_supplicant(FALSE);
469 wifi_state_emit_power_completed(FALSE);
471 netconfig_set_vconf_int(VCONF_WIFI_LAST_POWER_STATE, VCONFKEY_WIFI_OFF);
472 netconfig_set_vconf_int(VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_OFF);
473 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_OFF);
475 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_OFF);
477 netconfig_wifi_set_bgscan_pause(FALSE);
478 netconfig_wifi_bgscan_stop();
480 _set_bss_found(FALSE);
482 /* Inform net-popup to remove the wifi found notification */
483 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
484 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_PORTAL_NOTI, NULL);
486 _deregister_network_notification();
490 char *wifi_get_favorite_service(void)
492 return _get_connman_favorite_service();
495 void wifi_start_timer_network_notification(void)
497 #if defined TIZEN_WEARABLE
498 /* In case of wearable device, no need to notify available Wi-Fi APs */
501 netconfig_start_timer(NETCONFIG_NETWORK_NOTIFICATION_TIMEOUT, _check_network_notification, NULL, &network_noti_timer_id);
504 void wifi_state_notifier_register(wifi_state_notifier *notifier)
506 DBG("register notifier");
508 notifier_list = g_slist_append(notifier_list, notifier);
511 void wifi_state_notifier_unregister(wifi_state_notifier *notifier)
513 DBG("un-register notifier");
515 notifier_list = g_slist_remove_all(notifier_list, notifier);
518 void wifi_state_notifier_cleanup(void)
520 g_slist_free_full(notifier_list, NULL);
523 void wifi_state_set_bss_found(gboolean found)
525 _set_bss_found(found);
528 gboolean wifi_state_is_bss_found(void)
530 return new_bss_found;
533 void wifi_state_set_service_state(wifi_service_state_e new_state)
535 static gboolean dhcp_stage = FALSE;
536 wifi_service_state_e old_state = g_service_state;
538 if (old_state == new_state)
541 g_service_state = new_state;
542 DBG("Wi-Fi state %d ==> %d", old_state, new_state);
544 /* During DHCP, temporarily disable Wi-Fi power saving */
545 if ((old_state < NETCONFIG_WIFI_ASSOCIATION || old_state == NETCONFIG_WIFI_FAILURE) && new_state == NETCONFIG_WIFI_CONFIGURATION) {
546 _set_power_lock(TRUE);
547 _set_power_save(FALSE);
549 } else if (dhcp_stage == TRUE) {
550 _set_power_lock(FALSE);
551 _set_power_save(TRUE);
555 if (new_state == NETCONFIG_WIFI_CONNECTED) {
556 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
558 netconfig_set_vconf_int(VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_CONNECTED);
559 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_CONNECTED);
561 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_CONNECTED);
563 __set_wifi_connected_essid();
565 netconfig_wifi_indicator_start();
566 } else if (old_state == NETCONFIG_WIFI_CONNECTED) {
567 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_PORTAL_NOTI, NULL);
569 __unset_wifi_connected_essid();
571 netconfig_set_vconf_int (VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_UNCONNECTED);
572 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_NOT_CONNECTED);
574 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_ON);
576 netconfig_wifi_indicator_stop();
578 netconfig_wifi_set_bgscan_pause(FALSE);
580 netconfig_wifi_bgscan_stop();
581 netconfig_wifi_bgscan_start(TRUE);
582 } else if ((old_state > NETCONFIG_WIFI_IDLE && old_state < NETCONFIG_WIFI_CONNECTED) && new_state == NETCONFIG_WIFI_IDLE){
583 //in ipv6 case disconnect/association -> association
584 DBG("reset the bg scan period");
585 netconfig_wifi_set_bgscan_pause(FALSE);
587 netconfig_wifi_bgscan_stop();
588 netconfig_wifi_bgscan_start(TRUE);
591 _wifi_state_changed(new_state);
593 if (new_state == NETCONFIG_WIFI_CONNECTED){
594 _wifi_state_connected_activation();
595 #if defined TIZEN_WEARABLE
596 wc_launch_syspopup(WC_POPUP_TYPE_WIFI_CONNECTED);
601 wifi_service_state_e wifi_state_get_service_state(void)
603 return g_service_state;
606 void wifi_state_set_tech_state(wifi_tech_state_e new_state)
608 wifi_tech_state_e old_state = g_tech_state;
610 if (old_state == new_state)
613 g_tech_state = new_state;
615 DBG("Wi-Fi technology state %d ==> %d", old_state, new_state);
618 wifi_tech_state_e wifi_state_get_technology_state(void)
620 GVariant *message = NULL, *variant;
621 GVariantIter *iter, *next;
622 wifi_tech_state_e ret = NETCONFIG_WIFI_TECH_OFF;
623 gboolean wifi_tech_powered = FALSE;
624 gboolean wifi_tech_connected = FALSE;
628 if (g_tech_state > NETCONFIG_WIFI_TECH_UNKNOWN)
631 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
632 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
633 "GetTechnologies", NULL);
634 if (message == NULL) {
635 ERR("Failed to get_technology_state");
636 return NETCONFIG_WIFI_TECH_UNKNOWN;
639 g_variant_get(message, "(a(oa{sv}))", &iter);
640 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
641 if (path == NULL || g_strcmp0(path, CONNMAN_WIFI_TECHNOLOGY_PREFIX) != 0) {
645 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
646 const gchar *sdata = NULL;
649 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_BOOLEAN)) {
650 data = g_variant_get_boolean(variant);
651 DBG("key-[%s] - %s", key, data ? "True" : "False");
653 if (strcmp(key, "Powered") == 0 && data) {
654 wifi_tech_powered = TRUE;
655 } else if (strcmp(key, "Connected") == 0 && data) {
656 wifi_tech_connected = TRUE;
657 } else if (strcmp(key, "Tethering") == 0 && data) {
660 } else if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
661 sdata = g_variant_get_string(variant, NULL);
665 g_variant_iter_free (next);
668 g_variant_unref(message);
670 g_variant_iter_free (iter);
672 if (wifi_tech_powered == TRUE)
673 ret = NETCONFIG_WIFI_TECH_POWERED;
675 if (wifi_tech_connected == TRUE)
676 ret = NETCONFIG_WIFI_TECH_CONNECTED;
683 void wifi_state_set_connected_essid(void)
685 __set_wifi_connected_essid();
686 #if defined TIZEN_WEARABLE
687 wc_launch_syspopup(WC_POPUP_TYPE_WIFI_CONNECTED);
691 void wifi_state_get_connected_essid(gchar **essid)
693 *essid = g_strdup(__get_wifi_connected_essid());
696 /* wifi_connection_state_e in CAPI
698 * WIFI_CONNECTION_STATE_FAILURE = -1
699 * WIFI_CONNECTION_STATE_DISCONNECTED = 0
700 * WIFI_CONNECTION_STATE_ASSOCIATION = 1
701 * WIFI_CONNECTION_STATE_CONFIGURATION = 2
702 * WIFI_CONNECTION_STATE_CONNECTED = 3
704 /* connection_wifi_state_e in CAPI
706 * CONNECTION_WIFI_STATE_DEACTIVATED = 0
707 * CONNECTION_WIFI_STATE_DISCONNECTED = 1
708 * CONNECTION_WIFI_STATE_CONNECTED = 2
710 gboolean handle_get_wifi_state(Wifi *wifi, GDBusMethodInvocation *context)
712 g_return_val_if_fail(wifi != NULL, FALSE);
713 GVariant *param = NULL;
714 wifi_tech_state_e tech_state = NETCONFIG_WIFI_TECH_UNKNOWN;
715 wifi_service_state_e service_state = NETCONFIG_WIFI_UNKNOWN;
716 tech_state = wifi_state_get_technology_state();
717 service_state = wifi_state_get_service_state();
719 if(tech_state == NETCONFIG_WIFI_TECH_UNKNOWN) {
720 param = g_variant_new("(s)", "unknown");
721 } else if(tech_state == NETCONFIG_WIFI_TECH_OFF ||
722 tech_state == NETCONFIG_WIFI_TECH_WPS_ONLY) {
723 param = g_variant_new("(s)", "deactivated");
725 switch (service_state) {
726 case NETCONFIG_WIFI_FAILURE:
727 param = g_variant_new("(s)", "failure");
729 case NETCONFIG_WIFI_ASSOCIATION:
730 param = g_variant_new("(s)", "association");
732 case NETCONFIG_WIFI_CONFIGURATION:
733 param = g_variant_new("(s)", "configuration");
735 case NETCONFIG_WIFI_CONNECTED:
736 param = g_variant_new("(s)", "connected");
738 case NETCONFIG_WIFI_UNKNOWN:
739 case NETCONFIG_WIFI_IDLE:
741 param = g_variant_new("(s)", "disconnected");
745 g_dbus_method_invocation_return_value(context, param);