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;
48 static guint network_connected_popup_timer_id = 0;
49 static gboolean block_network_connected_popup = FALSE;
51 static void __netconfig_pop_wifi_connected_poppup(const char *ssid)
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);
65 DBG("Launch Wi-Fi connected alert network popup");
66 syspopup_launch("net-popup", b);
71 static gboolean _block_network_connection_popup(gpointer data)
73 block_network_connected_popup = FALSE;
74 netconfig_stop_timer(&network_connected_popup_timer_id);
78 static void __set_wifi_connected_essid(void)
80 const char *essid_name = NULL;
81 const char *wifi_profile = netconfig_get_default_profile();
83 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
86 if (wifi_profile == NULL ||
87 netconfig_is_wifi_profile(wifi_profile) != TRUE) {
88 ERR("Can't get Wi-Fi profile");
92 essid_name = netconfig_wifi_get_connected_essid(wifi_profile);
93 if (essid_name == NULL) {
94 ERR("Can't get Wi-Fi name");
98 netconfig_set_vconf_str(VCONFKEY_WIFI_CONNECTED_AP_NAME, essid_name);
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);
110 static void __unset_wifi_connected_essid(void)
112 netconfig_set_vconf_str(VCONFKEY_WIFI_CONNECTED_AP_NAME, "");
115 static const char *__get_wifi_connected_essid(void)
117 const char *essid_name = NULL;
118 const char *wifi_profile = NULL;
120 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
123 wifi_profile = netconfig_get_default_profile();
125 if (wifi_profile == NULL || netconfig_is_wifi_profile(wifi_profile) != TRUE) {
126 ERR("Can't get Wi-Fi profile");
130 essid_name = netconfig_wifi_get_connected_essid(wifi_profile);
131 if (essid_name == NULL) {
132 ERR("Can't get Wi-Fi name");
139 static gboolean __is_wifi_profile_available(void)
141 GVariant *message = NULL;
142 GVariantIter *iter, *next;
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");
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)
158 g_variant_iter_free(next);
163 g_variant_unref(message);
165 g_variant_iter_free(iter);
170 static gboolean __is_favorited(GVariantIter *array)
172 gboolean is_favorite = FALSE;
176 while (g_variant_iter_loop(array, "{sv}", &key, &var)) {
179 if (g_str_equal(key, "Favorite") != TRUE)
182 value = g_variant_get_boolean(var);
186 g_variant_unref(var);
193 static void _wifi_state_connected_activation(void)
195 /* Add activation of services when Wi-Fi is connected */
198 static void _wifi_state_changed(wifi_service_state_e state)
202 for (list = notifier_list; list; list = list->next) {
203 wifi_state_notifier *notifier = list->data;
205 if (notifier->wifi_state_changed != NULL)
206 notifier->wifi_state_changed(state, notifier->user_data);
210 static void _set_bss_found(gboolean found)
212 if (found != new_bss_found)
213 new_bss_found = found;
216 static gboolean _check_network_notification(gpointer data)
218 int qs_enable = 0, ug_state = 0;
219 static gboolean check_again = FALSE;
221 wifi_tech_state_e tech_state;
222 wifi_service_state_e service_state;
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);
230 service_state = wifi_state_get_service_state();
231 if (service_state == NETCONFIG_WIFI_CONNECTED) {
232 DBG("Service state is connected");
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);
244 if (__is_wifi_profile_available() == FALSE) {
245 netconfig_send_notification_to_net_popup(
246 NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
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");
256 netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &ug_state);
257 if (ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
260 netconfig_send_notification_to_net_popup(NETCONFIG_ADD_FOUND_AP_NOTI, NULL);
262 _set_bss_found(FALSE);
265 netconfig_stop_timer(&network_noti_timer_id);
269 static char *_get_connman_favorite_service(void)
271 char *favorite_service = NULL;
272 GVariant *message = NULL;
274 GVariantIter *iter, *next;
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");
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)
289 if (__is_favorited(next) == TRUE) {
290 favorite_service = g_strdup(obj);
292 g_variant_iter_free(next);
297 g_variant_iter_free(iter);
298 g_variant_unref(message);
300 return favorite_service;
303 static void __notification_value_changed_cb(keynode_t *node, void *user_data)
307 if (netconfig_vconf_get_int(VCONFKEY_WIFI_ENABLE_QS, &value) < 0)
310 if (value == VCONFKEY_WIFI_QS_DISABLE)
311 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
314 static void _register_network_notification(void)
316 #if defined TIZEN_WEARABLE
319 vconf_notify_key_changed(VCONFKEY_WIFI_ENABLE_QS, __notification_value_changed_cb, NULL);
322 static void _deregister_network_notification(void)
324 #if defined TIZEN_WEARABLE
327 vconf_ignore_key_changed(VCONFKEY_WIFI_ENABLE_QS, __notification_value_changed_cb);
330 static void _set_power_save(gboolean power_save)
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)
341 if_path = netconfig_wifi_get_supplicant_interface();
342 if (if_path == NULL) {
343 ERR("Fail to get wpa_supplicant DBus path");
348 input_args = g_variant_new_string(args_enable);
350 input_args = g_variant_new_string(args_disable);
352 result = netconfig_supplicant_invoke_dbus_method_nonblock(
355 SUPPLICANT_INTERFACE ".Interface",
360 ERR("Fail to set power save mode POWERMODE %d", power_save);
362 old_state = power_save;
364 g_variant_unref(input_args);
368 static void _set_power_lock(gboolean power_lock)
373 char state[] = "lcdoff";
374 char flag[] = "staycurstate";
375 char standby[] = "NULL";
377 char sleepmargin[] = "sleepmargin";
379 const char *lockstate = "lockstate";
380 const char *unlockstate = "unlockstate";
381 static gboolean old_state = FALSE;
382 const char *lock_method;
384 if (old_state == power_lock)
387 if (power_lock == TRUE) {
388 /* deviced power lock enable */
389 params = g_variant_new("(sssi)", state, flag, standby, timeout);
391 lock_method = lockstate;
393 /* deviced power lock disable */
394 params = g_variant_new("(ss)", state, sleepmargin);
396 lock_method = unlockstate;
399 reply = netconfig_invoke_dbus_method(
400 "org.tizen.system.deviced",
401 "/Org/Tizen/System/DeviceD/Display",
402 "org.tizen.system.deviced.display",
406 ERR("Failed to set_power_lock");
410 ret = g_variant_get_int32(reply);
412 ERR("Failed to set power lock %s with ret %d",
413 power_lock == TRUE ? "enable" : "disable", ret);
415 old_state = power_lock;
417 g_variant_unref(reply);
422 void wifi_state_emit_power_completed(gboolean power_on)
425 wifi_emit_power_on_completed((Wifi *)get_wifi_object());
427 wifi_emit_power_off_completed((Wifi *)get_wifi_object());
429 DBG("Successfully sent signal [%s]", (power_on) ? "powerOn" : "powerOff");
432 void wifi_state_emit_power_failed(void)
434 wifi_emit_power_operation_failed((Wifi *)get_wifi_object());
436 DBG("Successfully sent signal [PowerOperationFailed]");
439 void wifi_state_update_power_state(gboolean powered)
441 wifi_tech_state_e tech_state;
443 /* It's automatically updated by signal-handler
444 * DO NOT update manually
445 * It includes Wi-Fi state configuration
447 tech_state = wifi_state_get_technology_state();
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");
453 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_POWERED);
455 wifi_state_emit_power_completed(TRUE);
457 netconfig_wifi_device_picker_service_start();
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);
463 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_ON);
465 netconfig_wifi_bgscan_stop();
466 netconfig_wifi_bgscan_start(TRUE);
468 /* Add callback to track change in notification setting */
469 _register_network_notification();
471 } else if (tech_state > NETCONFIG_WIFI_TECH_OFF) {
472 DBG("Wi-Fi turned off or in power-save mode");
474 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
476 netconfig_wifi_device_picker_service_stop();
478 wifi_power_disable_technology_state_by_only_connman_signal();
479 wifi_power_driver_and_supplicant(FALSE);
481 wifi_state_emit_power_completed(FALSE);
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);
487 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_OFF);
489 netconfig_wifi_set_bgscan_pause(FALSE);
490 netconfig_wifi_bgscan_stop();
492 _set_bss_found(FALSE);
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);
498 _deregister_network_notification();
502 char *wifi_get_favorite_service(void)
504 return _get_connman_favorite_service();
507 void wifi_start_timer_network_notification(void)
509 #if defined TIZEN_WEARABLE
510 /* In case of wearable device, no need to notify available Wi-Fi APs */
513 netconfig_start_timer(NETCONFIG_NETWORK_NOTIFICATION_TIMEOUT, _check_network_notification, NULL, &network_noti_timer_id);
516 void wifi_state_notifier_register(wifi_state_notifier *notifier)
518 DBG("register notifier");
520 notifier_list = g_slist_append(notifier_list, notifier);
523 void wifi_state_notifier_unregister(wifi_state_notifier *notifier)
525 DBG("un-register notifier");
527 notifier_list = g_slist_remove_all(notifier_list, notifier);
530 void wifi_state_notifier_cleanup(void)
532 g_slist_free_full(notifier_list, NULL);
535 void wifi_state_set_bss_found(gboolean found)
537 _set_bss_found(found);
540 gboolean wifi_state_is_bss_found(void)
542 return new_bss_found;
545 void wifi_state_set_service_state(wifi_service_state_e new_state)
547 static gboolean dhcp_stage = FALSE;
548 wifi_service_state_e old_state = g_service_state;
550 if (old_state == new_state)
553 g_service_state = new_state;
554 DBG("Wi-Fi state %d ==> %d", old_state, new_state);
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);
561 } else if (dhcp_stage == TRUE) {
562 _set_power_lock(FALSE);
563 _set_power_save(TRUE);
567 if (new_state == NETCONFIG_WIFI_CONNECTED) {
568 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
570 netconfig_set_vconf_int(VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_CONNECTED);
571 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_CONNECTED);
573 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_CONNECTED);
575 __set_wifi_connected_essid();
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);
581 __unset_wifi_connected_essid();
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);
586 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_ON);
588 netconfig_wifi_indicator_stop();
590 netconfig_wifi_set_bgscan_pause(FALSE);
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);
599 netconfig_wifi_bgscan_stop();
600 netconfig_wifi_bgscan_start(TRUE);
603 _wifi_state_changed(new_state);
605 if (new_state == NETCONFIG_WIFI_CONNECTED) {
606 _wifi_state_connected_activation();
610 wifi_service_state_e wifi_state_get_service_state(void)
612 return g_service_state;
615 void wifi_state_set_tech_state(wifi_tech_state_e new_state)
617 wifi_tech_state_e old_state = g_tech_state;
619 if (old_state == new_state)
622 g_tech_state = new_state;
624 DBG("Wi-Fi technology state %d ==> %d", old_state, new_state);
627 wifi_tech_state_e wifi_state_get_technology_state(void)
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;
637 if (g_tech_state > NETCONFIG_WIFI_TECH_UNKNOWN)
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;
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)
653 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
654 const gchar *sdata = NULL;
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");
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;
666 else if (strcmp(key, "Tethering") == 0 && data) {
668 } else if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
669 sdata = g_variant_get_string(variant, NULL);
673 g_variant_iter_free(next);
676 g_variant_unref(message);
678 g_variant_iter_free(iter);
680 if (wifi_tech_powered == TRUE)
681 ret = NETCONFIG_WIFI_TECH_POWERED;
683 if (wifi_tech_connected == TRUE)
684 ret = NETCONFIG_WIFI_TECH_CONNECTED;
691 void wifi_state_set_connected_essid(void)
693 __set_wifi_connected_essid();
696 void wifi_state_get_connected_essid(gchar **essid)
698 *essid = g_strdup(__get_wifi_connected_essid());
701 /* wifi_connection_state_e in CAPI
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
709 /* connection_wifi_state_e in CAPI
711 * CONNECTION_WIFI_STATE_DEACTIVATED = 0
712 * CONNECTION_WIFI_STATE_DISCONNECTED = 1
713 * CONNECTION_WIFI_STATE_CONNECTED = 2
715 gboolean handle_get_wifi_state(Wifi *wifi, GDBusMethodInvocation *context)
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();
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");
732 switch (service_state) {
733 case NETCONFIG_WIFI_FAILURE:
734 param = g_variant_new("(s)", "failure");
736 case NETCONFIG_WIFI_ASSOCIATION:
737 param = g_variant_new("(s)", "association");
739 case NETCONFIG_WIFI_CONFIGURATION:
740 param = g_variant_new("(s)", "configuration");
742 case NETCONFIG_WIFI_CONNECTED:
743 param = g_variant_new("(s)", "connected");
745 case NETCONFIG_WIFI_UNKNOWN:
746 case NETCONFIG_WIFI_IDLE:
748 param = g_variant_new("(s)", "disconnected");
752 g_dbus_method_invocation_return_value(context, param);