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>
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"
37 #define NETCONFIG_NETWORK_NOTIFICATION_TIMEOUT 15 * 1000
39 static gboolean new_bss_found = FALSE;
40 static guint network_noti_timer_id = 0;
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;
45 static GSList *notifier_list = NULL;
48 static void __netconfig_pop_wifi_connected_poppup(const char *ssid)
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);
62 DBG("Launch Wi-Fi connected alert network popup");
63 syspopup_launch("net.netpopup", b);
68 static void __set_wifi_connected_essid(void)
70 const char *essid_name = NULL;
71 const char *wifi_profile = netconfig_get_default_profile();
73 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
76 if (wifi_profile == NULL ||
77 netconfig_is_wifi_profile(wifi_profile) != TRUE) {
78 ERR("Can't get Wi-Fi profile");
82 essid_name = netconfig_wifi_get_connected_essid(wifi_profile);
83 if (essid_name == NULL) {
84 ERR("Can't get Wi-Fi name");
88 netconfig_set_vconf_str(VCONFKEY_WIFI_CONNECTED_AP_NAME, essid_name);
89 __netconfig_pop_wifi_connected_poppup(essid_name);
92 static void __unset_wifi_connected_essid(void)
94 netconfig_set_vconf_str(VCONFKEY_WIFI_CONNECTED_AP_NAME, "");
97 static const char *__get_wifi_connected_essid(void)
99 const char *essid_name = NULL;
100 const char *wifi_profile = NULL;
102 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED)
105 wifi_profile = netconfig_get_default_profile();
107 if (wifi_profile == NULL || netconfig_is_wifi_profile(wifi_profile) != TRUE) {
108 ERR("Can't get Wi-Fi profile");
112 essid_name = netconfig_wifi_get_connected_essid(wifi_profile);
113 if (essid_name == NULL) {
114 ERR("Can't get Wi-Fi name");
121 static gboolean __is_wifi_profile_available(void)
123 GVariant *message = NULL;
124 GVariantIter *iter, *next;
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");
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) {
141 g_variant_iter_free(next);
146 g_variant_unref(message);
148 g_variant_iter_free(iter);
153 static gboolean __is_favorited(GVariantIter *array)
155 gboolean is_favorite = FALSE;
159 while (g_variant_iter_loop(array, "{sv}", &key, &var)) {
162 if (g_str_equal(key, "Favorite") != TRUE) {
166 value = g_variant_get_boolean(var);
170 g_variant_unref(var);
177 static void _wifi_state_connected_activation(void)
179 /* Add activation of services when Wi-Fi is connected */
182 static void _wifi_state_changed(wifi_service_state_e state)
186 for (list = notifier_list; list; list = list->next) {
187 wifi_state_notifier *notifier = list->data;
189 if (notifier->wifi_state_changed != NULL)
190 notifier->wifi_state_changed(state, notifier->user_data);
194 static void _set_bss_found(gboolean found)
196 if (found != new_bss_found)
197 new_bss_found = found;
200 static gboolean _check_network_notification(gpointer data)
202 int qs_enable = 0, ug_state = 0;
203 static gboolean check_again = FALSE;
205 wifi_tech_state_e tech_state;
206 wifi_service_state_e service_state;
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);
214 service_state = wifi_state_get_service_state();
215 if (service_state == NETCONFIG_WIFI_CONNECTED) {
216 DBG("Service state is connected");
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);
228 if (__is_wifi_profile_available() == FALSE) {
229 netconfig_send_notification_to_net_popup(
230 NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
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");
240 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &ug_state);
241 if (ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND) {
245 netconfig_send_notification_to_net_popup(NETCONFIG_ADD_FOUND_AP_NOTI, NULL);
247 _set_bss_found(FALSE);
250 netconfig_stop_timer(&network_noti_timer_id);
254 static char *_get_connman_favorite_service(void)
256 char *favorite_service = NULL;
257 GVariant *message = NULL;
259 GVariantIter *iter, *next;
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");
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) {
275 if (__is_favorited(next) == TRUE) {
276 favorite_service = g_strdup(obj);
278 g_variant_iter_free(next);
283 g_variant_iter_free(iter);
284 g_variant_unref(message);
286 return favorite_service;
289 static void __notification_value_changed_cb(keynode_t *node, void *user_data)
293 if (vconf_get_int(VCONFKEY_WIFI_ENABLE_QS, &value) < 0) {
297 if (value == VCONFKEY_WIFI_QS_DISABLE) {
298 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
302 static void _register_network_notification(void)
304 #if defined TIZEN_WEARABLE
307 vconf_notify_key_changed(VCONFKEY_WIFI_ENABLE_QS, __notification_value_changed_cb, NULL);
310 static void _deregister_network_notification(void)
312 #if defined TIZEN_WEARABLE
315 vconf_ignore_key_changed(VCONFKEY_WIFI_ENABLE_QS, __notification_value_changed_cb);
318 static void _set_power_save(gboolean power_save)
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)
329 if_path = netconfig_wifi_get_supplicant_interface();
330 if (if_path == NULL) {
331 ERR("Fail to get wpa_supplicant DBus path");
336 input_args = g_variant_new_string(args_enable);
338 input_args = g_variant_new_string(args_disable);
340 result = netconfig_supplicant_invoke_dbus_method_nonblock(
343 SUPPLICANT_INTERFACE ".Interface",
348 ERR("Fail to set power save mode POWERMODE %d", power_save);
350 old_state = power_save;
355 static void _set_power_lock(gboolean power_lock)
360 char state[] = "lcdoff";
361 char flag[] = "staycurstate";
362 char standby[] = "NULL";
364 char sleepmargin[] = "sleepmargin";
366 const char *lockstate = "lockstate";
367 const char *unlockstate = "unlockstate";
368 static gboolean old_state = FALSE;
369 const char *lock_method;
371 if (old_state == power_lock)
374 if (power_lock == TRUE) {
375 /* deviced power lock enable */
376 params = g_variant_new("(sssi)", state, flag, standby, timeout);
378 lock_method = lockstate;
380 /* deviced power lock disable */
381 params = g_variant_new("(ss)", state, sleepmargin);
383 lock_method = unlockstate;
386 reply = netconfig_invoke_dbus_method(
387 "org.tizen.system.deviced",
388 "/Org/Tizen/System/DeviceD/Display",
389 "org.tizen.system.deviced.display",
393 ERR("Failed to set_power_lock");
397 ret = g_variant_get_int32(reply);
399 ERR("Failed to set power lock %s with ret %d",
400 power_lock == TRUE ? "enable" : "disable", ret);
402 old_state = power_lock;
404 g_variant_unref(reply);
409 void wifi_state_emit_power_completed(gboolean power_on)
412 wifi_emit_power_on_completed((Wifi *)get_wifi_object());
414 wifi_emit_power_off_completed((Wifi *)get_wifi_object());
416 DBG("Successfully sent signal [%s]",(power_on)?"powerOn":"powerOff");
419 void wifi_state_emit_power_failed(void)
421 wifi_emit_power_operation_failed((Wifi *)get_wifi_object());
423 DBG("Successfully sent signal [PowerOperationFailed]");
426 void wifi_state_update_power_state(gboolean powered)
428 wifi_tech_state_e tech_state;
430 /* It's automatically updated by signal-handler
431 * DO NOT update manually
432 * It includes Wi-Fi state configuration
434 tech_state = wifi_state_get_technology_state();
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");
440 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_POWERED);
442 wifi_state_emit_power_completed(TRUE);
444 netconfig_wifi_device_picker_service_start();
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);
450 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_ON);
452 netconfig_wifi_bgscan_stop();
453 netconfig_wifi_bgscan_start(TRUE);
455 /* Add callback to track change in notification setting */
456 _register_network_notification();
458 } else if (tech_state > NETCONFIG_WIFI_TECH_OFF) {
459 DBG("Wi-Fi turned off or in power-save mode");
461 wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_WPS_ONLY);
463 netconfig_wifi_device_picker_service_stop();
465 wifi_power_disable_technology_state_by_only_connman_signal();
466 wifi_power_driver_and_supplicant(FALSE);
468 wifi_state_emit_power_completed(FALSE);
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);
474 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_OFF);
476 netconfig_wifi_set_bgscan_pause(FALSE);
477 netconfig_wifi_bgscan_stop();
479 _set_bss_found(FALSE);
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);
485 _deregister_network_notification();
489 char *wifi_get_favorite_service(void)
491 return _get_connman_favorite_service();
494 void wifi_start_timer_network_notification(void)
496 #if defined TIZEN_WEARABLE
497 /* In case of wearable device, no need to notify available Wi-Fi APs */
500 netconfig_start_timer(NETCONFIG_NETWORK_NOTIFICATION_TIMEOUT, _check_network_notification, NULL, &network_noti_timer_id);
503 void wifi_state_notifier_register(wifi_state_notifier *notifier)
505 DBG("register notifier");
507 notifier_list = g_slist_append(notifier_list, notifier);
510 void wifi_state_notifier_unregister(wifi_state_notifier *notifier)
512 DBG("un-register notifier");
514 notifier_list = g_slist_remove_all(notifier_list, notifier);
517 void wifi_state_notifier_cleanup(void)
519 g_slist_free_full(notifier_list, NULL);
522 void wifi_state_set_bss_found(gboolean found)
524 _set_bss_found(found);
527 gboolean wifi_state_is_bss_found(void)
529 return new_bss_found;
532 void wifi_state_set_service_state(wifi_service_state_e new_state)
534 static gboolean dhcp_stage = FALSE;
535 wifi_service_state_e old_state = g_service_state;
537 if (old_state == new_state)
540 g_service_state = new_state;
541 DBG("Wi-Fi state %d ==> %d", old_state, new_state);
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);
548 } else if (dhcp_stage == TRUE) {
549 _set_power_lock(FALSE);
550 _set_power_save(TRUE);
554 if (new_state == NETCONFIG_WIFI_CONNECTED) {
555 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_FOUND_AP_NOTI, NULL);
557 netconfig_set_vconf_int(VCONFKEY_WIFI_STATE, VCONFKEY_WIFI_CONNECTED);
558 netconfig_set_vconf_int(VCONFKEY_NETWORK_WIFI_STATE, VCONFKEY_NETWORK_WIFI_CONNECTED);
560 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_CONNECTED);
562 __set_wifi_connected_essid();
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);
568 __unset_wifi_connected_essid();
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);
573 netconfig_set_system_event(SYS_EVENT_WIFI_STATE, EVT_KEY_WIFI_STATE, EVT_VAL_WIFI_ON);
575 netconfig_wifi_indicator_stop();
577 netconfig_wifi_set_bgscan_pause(FALSE);
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);
586 netconfig_wifi_bgscan_stop();
587 netconfig_wifi_bgscan_start(TRUE);
590 _wifi_state_changed(new_state);
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);
600 wifi_service_state_e wifi_state_get_service_state(void)
602 return g_service_state;
605 void wifi_state_set_tech_state(wifi_tech_state_e new_state)
607 wifi_tech_state_e old_state = g_tech_state;
609 if (old_state == new_state)
612 g_tech_state = new_state;
614 DBG("Wi-Fi technology state %d ==> %d", old_state, new_state);
617 wifi_tech_state_e wifi_state_get_technology_state(void)
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;
627 if (g_tech_state > NETCONFIG_WIFI_TECH_UNKNOWN)
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;
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) {
644 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
645 const gchar *sdata = NULL;
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");
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) {
659 } else if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
660 sdata = g_variant_get_string(variant, NULL);
664 g_variant_iter_free (next);
667 g_variant_unref(message);
669 g_variant_iter_free (iter);
671 if (wifi_tech_powered == TRUE)
672 ret = NETCONFIG_WIFI_TECH_POWERED;
674 if (wifi_tech_connected == TRUE)
675 ret = NETCONFIG_WIFI_TECH_CONNECTED;
682 void wifi_state_set_connected_essid(void)
684 __set_wifi_connected_essid();
685 #if defined TIZEN_WEARABLE
686 wc_launch_syspopup(WC_POPUP_TYPE_WIFI_CONNECTED);
690 void wifi_state_get_connected_essid(gchar **essid)
692 *essid = g_strdup(__get_wifi_connected_essid());
695 /* wifi_connection_state_e in CAPI
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
703 /* connection_wifi_state_e in CAPI
705 * CONNECTION_WIFI_STATE_DEACTIVATED = 0
706 * CONNECTION_WIFI_STATE_DISCONNECTED = 1
707 * CONNECTION_WIFI_STATE_CONNECTED = 2
709 gboolean handle_get_wifi_state(Wifi *wifi, GDBusMethodInvocation *context)
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();
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");
724 switch (service_state) {
725 case NETCONFIG_WIFI_FAILURE:
726 param = g_variant_new("(s)", "failure");
728 case NETCONFIG_WIFI_ASSOCIATION:
729 param = g_variant_new("(s)", "association");
731 case NETCONFIG_WIFI_CONFIGURATION:
732 param = g_variant_new("(s)", "configuration");
734 case NETCONFIG_WIFI_CONNECTED:
735 param = g_variant_new("(s)", "connected");
737 case NETCONFIG_WIFI_UNKNOWN:
738 case NETCONFIG_WIFI_IDLE:
740 param = g_variant_new("(s)", "disconnected");
744 g_dbus_method_invocation_return_value(context, param);