4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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.
23 #include "mh_func_onoff.h"
25 #include "mh_string.h"
27 static bool is_wifi_tethering_checkbox_popup_active = false;
29 void _update_tethering_enabling_item(mh_appdata_t *ad, tethering_type_e type, mh_state_e state)
32 case TETHERING_TYPE_WIFI:
33 _update_wifi_item(ad, MH_STATE_NONE);
34 ad->is_wifi_teth_enabling = false;
37 case TETHERING_TYPE_BT:
38 _update_bt_item(ad, MH_STATE_NONE);
39 ad->is_bt_teth_enabling = false;
42 case TETHERING_TYPE_USB:
43 _update_usb_item(ad, MH_STATE_NONE);
44 ad->is_usb_teth_enabling = false;
48 ERR("invalid type \n");
52 void _wifi_tethering_checkbox_popup_status_set(bool value)
54 is_wifi_tethering_checkbox_popup_active = value;
57 bool _wifi_tethering_checkbox_popup_status_get(void)
59 return is_wifi_tethering_checkbox_popup_active;
62 int _get_vconf_usb_state()
64 int value = VCONFKEY_SYSMAN_USB_DISCONNECTED;
66 if (vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &value) < 0) {
67 ERR("vconf_get_int is failed\n");
70 DBG("%s : %d\n", VCONFKEY_SYSMAN_USB_STATUS, value);
75 void _update_tethering_item(mh_appdata_t * ad, mh_state_e state)
77 ERR("type : %d state : %d\n", ad->type, state);
79 case TETHERING_TYPE_WIFI:
80 _update_wifi_item(ad, state);
82 case TETHERING_TYPE_BT:
83 _update_bt_item(ad, state);
85 case TETHERING_TYPE_USB:
86 _update_usb_item(ad, state);
93 gboolean _ps_recheck_timeout_cb(gpointer data)
95 connection_cellular_state_e cellular_state = _get_cellular_state();
96 mh_appdata_t *ad = (mh_appdata_t *)data;
97 static int recheck_count = 0;
99 DBG("Re-Check cellular state (%d)\n", recheck_count);
101 if(cellular_state == CONNECTION_CELLULAR_STATE_FLIGHT_MODE) {
102 _update_tethering_item(ad, MH_STATE_NONE);
107 if (cellular_state == CONNECTION_CELLULAR_STATE_CONNECTED ||
108 cellular_state == CONNECTION_CELLULAR_STATE_AVAILABLE) {
109 if (ad->type == TETHERING_TYPE_WIFI) {
110 if (_create_wifi_hotspot_on_popup(ad) < 0) {
111 ERR("__create_wifi_hotspot_on_popup fail\n");
112 _update_tethering_item(ad, MH_STATE_NONE);
116 _send_signal_qp(QP_SIGNAL_PROGRESS_ON);
117 } else if (ad->type == TETHERING_TYPE_BT && _create_bt_tethering_on_popup(ad) < 0) {
118 ERR("_create_bt_tethering_on_popup fail\n");
119 _update_tethering_item(ad, MH_STATE_NONE);
122 } else if (ad->type == TETHERING_TYPE_USB && _create_usb_tethering_on_popup(ad) < 0) {
123 ERR("__create_usb_hotspot_on_popup fail\n");
124 _update_tethering_item(ad, MH_STATE_NONE);
128 ERR("Unknown tethering type \n");
133 if(++recheck_count >= PS_RECHECK_COUNT_MAX) {
134 DBG("Cellular network is not connected : %d\n", cellular_state);
135 _update_tethering_item(ad, MH_STATE_NONE);
136 _prepare_popup(MH_POPUP_NETWORK_OUT_OF_RANGE, STR_NO_DATA_SERVICE);
147 static bool __is_connected_wifi_net(mh_appdata_t *ad)
149 connection_wifi_state_e wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;
152 ret = connection_get_wifi_state(ad->conn_handle, &wifi_state);
153 if (ret != CONNECTION_ERROR_NONE) {
154 ERR("connection_get_wifi_state() is failed : %d\n");
158 if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED) {
159 ERR("Wi-Fi network is not connected : %d\n", wifi_state);
163 DBG("Wi-Fi network is connected\n");
167 static bool __is_connected_ethernet_net(mh_appdata_t *ad)
169 connection_ethernet_state_e ethernet_state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
172 ret = connection_get_ethernet_state(ad->conn_handle, ðernet_state);
173 if (ret != CONNECTION_ERROR_NONE) {
174 ERR("connection_get_ethernet_state() is failed : %d\n");
178 if (ethernet_state != CONNECTION_ETHERNET_STATE_CONNECTED) {
179 ERR("Ethernet network is not connected : %d\n", ethernet_state);
183 DBG("Ethernet network is connected\n");
187 static int __is_preconditions_handled(mh_appdata_t *ad)
191 connection_cellular_state_e cellular_state = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
194 /* check ethernet connection */
195 if (__is_connected_ethernet_net(ad)) {
196 DBG("ethernet network is connected\n");
200 /* check wifi connection */
201 if (ad->type != TETHERING_TYPE_WIFI) {
202 if (__is_connected_wifi_net(ad)) {
203 DBG("wifi network is connected\n");
208 /* Check SIM state */
209 if (_get_sim_state() != VCONFKEY_TELEPHONY_SIM_INSERTED) {
210 if(ad->type == TETHERING_TYPE_WIFI)
211 _prepare_popup(MH_POPUP_NO_SIM, STR_INSERT_SIM_TO_USE_TETH);
213 _prepare_popup(MH_POPUP_NO_SIM, STR_CONN_MOBILE_DATA_TO_USE_TETH);
219 /* Check cellular state */
220 cellular_state = _get_cellular_state();
221 DBG("cellular state is : %d \n", cellular_state);
222 if (cellular_state == CONNECTION_CELLULAR_STATE_FLIGHT_MODE) {
223 _prepare_popup(MH_POPUP_FLIGHT_MODE, STR_DISABLE_FLIGHT_MODE_MSG);
225 ERR("Flight mode is ON\n");
228 if (vconf_get_bool(VCONFKEY_3G_ENABLE , &dnet_state) < 0) {
229 ERR("vconf_get_bool is failed\n");
230 } else if(dnet_state == 0) {
231 DBG("Data Network is not connected");
232 _prepare_popup(MH_POPUP_MOBILE_DATA_OFF, STR_NO_NET_CONN_MSG);
236 if (cellular_state != CONNECTION_CELLULAR_STATE_CONNECTED &&
237 cellular_state != CONNECTION_CELLULAR_STATE_AVAILABLE) {
238 if(ad->ps_recheck_timer_id > 0) {
239 g_source_remove(ad->ps_recheck_timer_id);
240 ad->ps_recheck_timer_id = 0;
241 if (ad->is_wifi_teth_enabling == true && ad->type != TETHERING_TYPE_WIFI) {
242 _update_tethering_enabling_item(ad, TETHERING_TYPE_WIFI, MH_STATE_NONE);
244 if (ad->is_bt_teth_enabling == true && ad->type != TETHERING_TYPE_BT) {
245 _update_tethering_enabling_item(ad, TETHERING_TYPE_BT, MH_STATE_NONE);
247 if (ad->is_usb_teth_enabling == true && ad->type != TETHERING_TYPE_USB) {
248 _update_tethering_enabling_item(ad, TETHERING_TYPE_USB, MH_STATE_NONE);
251 ad->ps_recheck_timer_id = g_timeout_add(PS_RECHECK_INTERVAL, _ps_recheck_timeout_cb, (void*)ad);
256 DBG("Cellular network is connected\n");
261 int _create_wifi_hotspot_on_popup(mh_appdata_t *ad)
265 bool wifi_state = false;
268 wifi_is_activated(&wifi_state);
269 _set_vconf_prev_wifi_state(wifi_state);
270 value = _get_checkbox_status(TETHERING_TYPE_WIFI);
272 if (wifi_state == true || _is_wifi_direct_on() == true) {
273 fmt = STR_TETH_ON_DESC_1;
275 fmt = STR_TETH_ON_DESC_2;
277 str = g_malloc0(MH_LABEL_LENGTH_MAX);
279 ERR("memory allocation is failed\n");
282 snprintf(str, MH_LABEL_LENGTH_MAX, fmt, TETHERING_WIFI_MAX_CONNECTED_STA);
283 _wifi_tethering_checkbox_popup_status_set(true);
284 _prepare_popup(MH_POPUP_WIFI_ON_CHECKBOX, str);
288 _prepare_popup_type(MH_POPUP_WIFI_ON_CHECKBOX);
294 int _create_bt_tethering_on_popup(mh_appdata_t *ad)
296 char *fmt = STR_TETH_ON_DESC_2;
299 value = _get_checkbox_status(TETHERING_TYPE_BT);
301 str = g_malloc0(MH_LABEL_LENGTH_MAX);
303 ERR("memory allocation is failed\n");
306 snprintf(str, MH_LABEL_LENGTH_MAX, fmt,
307 TETHERING_BT_MAX_CONNECTED_STA);
308 _prepare_popup(MH_POPUP_BT_ON_CHECKBOX, str);
312 _prepare_popup_type(MH_POPUP_BT_ON_CHECKBOX);
318 int _create_usb_tethering_on_popup(mh_appdata_t *ad)
322 value = _get_checkbox_status(TETHERING_TYPE_USB);
323 DBG("%s : %d\n", VCONF_MOBILE_AP_USB_POPUP_CHECKBOX_STATUS, value);
325 str = g_malloc0(MH_LABEL_LENGTH_MAX);
327 ERR("memory allocation is failed\n");
330 snprintf(str, MH_LABEL_LENGTH_MAX, "%s",
332 _prepare_popup(MH_POPUP_USB_ON_CHECKBOX, str);
336 _prepare_popup_type(MH_POPUP_USB_ON_CHECKBOX);
342 static void __disable_tethering_by_ind(mh_appdata_t *ad, tethering_disabled_cause_e cause)
345 ERR("Param is NULL\n");
349 DBG("cause : %d\n", cause);
351 case TETHERING_DISABLED_BY_WIFI_ON:
354 case TETHERING_DISABLED_BY_BT_OFF:
357 case TETHERING_DISABLED_BY_USB_DISCONNECTION:
358 if (ad->is_bt_teth_enabling && ad->main.bt_item) {
359 _update_bt_item(ad, MH_STATE_NONE);
360 ad->is_bt_teth_enabling = false;
362 if (ad->is_wifi_teth_enabling && ad->main.wifi_item) {
363 _update_wifi_item(ad, MH_STATE_NONE);
364 ad->is_wifi_teth_enabling = false;
368 case TETHERING_DISABLED_BY_FLIGHT_MODE:
371 case TETHERING_DISABLED_BY_TIMEOUT:
374 case TETHERING_DISABLED_BY_OTHERS:
375 if (ad->main.wifi_item && _get_vconf_prev_wifi_state() == true) {
376 elm_object_item_disabled_set(ad->main.wifi_item, EINA_TRUE);
380 case TETHERING_DISABLED_BY_LOW_BATTERY:
390 /* Wi-Fi callbacks */
391 static void __wifi_activated_cb(wifi_error_e result, void *user_data)
398 /* Tethering callbacks */
399 void _enabled_cb(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
402 DBG("result : %d, type : %d, is_requested : %d\n", result, type, is_requested);
404 if (user_data == NULL) {
405 ERR("user_data is NULL\n");
409 mh_appdata_t *ad = (mh_appdata_t *)user_data;
412 if (ad->type == type && ad->popup) {
413 DBG("This tethering type is already enabled\n");
414 evas_object_del(ad->popup);
417 _update_main_view(ad, type);
418 if (type == TETHERING_TYPE_RESERVED) {
419 DBG("MobileAP is turned on\n");
420 _prepare_popup(MH_POPUP_WIFI_AP_OFF, STR_WIFI_AP_CONTROLLED_ANOTHER_APP);
426 if (result != TETHERING_ERROR_NONE) {
428 _update_main_view(ad, type);
433 void _disabled_cb(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
436 DBG("result : %d, type : %d, cause : %d\n", result, type, cause);
438 if (user_data == NULL) {
439 ERR("user_data is NULL\n");
443 mh_appdata_t *ad = (mh_appdata_t *)user_data;
445 if (cause != TETHERING_DISABLED_BY_REQUEST) {
446 if (ad->type == type && ad->popup) {
447 DBG("This tethering type is already disabled\n");
448 evas_object_del(ad->popup);
451 __disable_tethering_by_ind(ad, cause);
452 _update_main_view(ad, type);
456 if (result != TETHERING_ERROR_NONE) {
457 _prepare_popup(MH_POPUP_TETH_ENABLING_FAILED, STR_UNABLE_TO_USE_TETH);
459 _update_main_view(ad, type);
463 if (ad->main.wifi_item && type == TETHERING_TYPE_WIFI && _get_vconf_prev_wifi_state() == true) {
464 elm_object_item_disabled_set(ad->main.wifi_item, EINA_TRUE);
467 _update_main_view(ad, type);
472 void _connection_changed_cb(tethering_client_h client, bool is_opened, void *user_data)
476 if (user_data == NULL) {
477 ERR("user_data is NULL\n");
481 mh_appdata_t *ad = (mh_appdata_t *)user_data;
482 char *mac_addr = NULL;
485 _append_list_client_handle(ad, client);
487 #ifdef TETHERING_DATA_USAGE_SUPPORT
488 if (ad->is_foreground && _get_list_clients_count(ad) == 1) {
489 _start_update_data_packet_usage(ad);
493 tethering_client_get_mac_address(client, &mac_addr);
495 _delete_list_client_handle(ad, mac_addr);
498 #ifdef TETHERING_DATA_USAGE_SUPPORT
499 if (ad->is_foreground && _get_list_clients_count(ad) == 0)
500 _stop_update_data_packet_usage(ad);
504 ap_update_data_device(ad);
509 void _wifi_state_changed_cb(wifi_device_state_e state, void *user_data)
511 if (user_data == NULL) {
512 ERR("user_data is NULL\n");
518 mh_appdata_t *ad= (mh_appdata_t *)user_data;
521 if (state == WIFI_DEVICE_STATE_ACTIVATED) {
522 if (ad->main.wifi_item && elm_object_item_disabled_get(ad->main.wifi_item))
523 elm_object_item_disabled_set(ad->main.wifi_item, EINA_FALSE);
524 _set_vconf_prev_wifi_state(false);
525 } else if (state == WIFI_DEVICE_STATE_DEACTIVATED){
526 _set_vconf_prev_wifi_state(true);
529 if (ad->type == TETHERING_TYPE_WIFI && ad->popup && ad->popup_checkbox &&
530 _wifi_tethering_checkbox_popup_status_get()) {
531 evas_object_del(ad->popup_checkbox);
532 ad->popup_checkbox = NULL;
533 evas_object_del(ad->popup);
535 if (state == WIFI_DEVICE_STATE_ACTIVATED) {
536 fmt = STR_TETH_ON_DESC_1;
538 fmt = STR_TETH_ON_DESC_2;
540 str = g_malloc0(MH_LABEL_LENGTH_MAX);
542 ERR("memory allocation is failed\n");
545 snprintf(str, MH_LABEL_LENGTH_MAX, fmt, TETHERING_WIFI_MAX_CONNECTED_STA);
546 _prepare_popup(MH_POPUP_WIFI_ON_CHECKBOX, str);
553 void _visibility_changed_cb(bool is_visible, void *user_data)
557 if (user_data == NULL) {
558 ERR("user_data is NULL\n");
562 mh_appdata_t *ad = (mh_appdata_t *)user_data;
564 if (ad->setup.visibility == is_visible)
567 ad->setup.visibility = is_visible;
568 ad->setup.visibility_new = is_visible;
569 if (ad->setup.hide_item)
570 elm_genlist_item_update(ad->setup.hide_item);
577 void _security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
581 if (user_data == NULL) {
582 ERR("user_data is NULL\n");
586 mh_appdata_t *ad = (mh_appdata_t *)user_data;
588 if (ad->setup.security_type == changed_type)
591 ad->setup.security_type = changed_type;
592 ad->setup.security_type_new = changed_type;
594 if (ad->setup.security_item)
595 elm_genlist_item_update(ad->setup.security_item);
602 void _passphrase_changed_cb(void *user_data)
606 if (user_data == NULL) {
607 ERR("user_data is NULL\n");
611 mh_appdata_t *ad = (mh_appdata_t *)user_data;
612 char *passphrase = NULL;
615 ret = tethering_wifi_get_passphrase(ad->handle, &passphrase);
616 if (ret != TETHERING_ERROR_NONE) {
617 ERR("tethering_wifi_get_passphrase failed ret = [0x%x]\n", ret);
621 if (!g_strcmp0(passphrase, ad->setup.wifi_passphrase)) {
625 g_strlcpy(ad->setup.wifi_passphrase, passphrase,
626 sizeof(ad->setup.wifi_passphrase));
628 g_strlcpy(ad->setup.wifi_passphrase_new, passphrase,
629 sizeof(ad->setup.wifi_passphrase_new));
631 if (ad->setup.pw_item)
632 elm_genlist_item_update(ad->setup.pw_item);
640 #ifdef TETHERING_DATA_USAGE_SUPPORT
641 void _data_usage_cb(tethering_error_e result, unsigned long long received_data, unsigned long long sent_data, void *user_data)
643 if (user_data == NULL) {
644 ERR("user_data is NULL\n");
648 mh_appdata_t *ad = (mh_appdata_t *)user_data;
650 if (ad->data_statistics.pdp_total_sent != sent_data ||
651 ad->data_statistics.pdp_total_receive != received_data) {
652 ad->data_statistics.pdp_total_sent = sent_data;
653 ad->data_statistics.pdp_total_receive = received_data;
654 ap_update_data_packet_usage(ad);
657 ad->data_statistics.is_updated = true;
662 /* End of Tethering callbacks */
664 int _handle_wifi_onoff_change(mh_appdata_t *ad)
666 __MOBILE_AP_FUNC_ENTER__;
669 int connected_wifi_clients = 0;
671 /* Turn off WiFi hotspot */
672 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI) {
673 if (_get_no_of_connected_device(ad, &connected_wifi_clients,
674 TETHERING_TYPE_WIFI) == EINA_FALSE) {
675 ERR("Getting the number of connected device is failed\n");
677 if (connected_wifi_clients > 0) {
678 _prepare_popup(MH_POPUP_WIFI_OFF, STR_CLOSE_INTERNET_Q);
681 ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
682 if (ret != TETHERING_ERROR_NONE) {
683 ERR("wifi tethering off is failed : %d\n", ret);
686 _send_signal_qp(QP_SIGNAL_PROGRESS_OFF);
691 /* Turn on WiFi hotspot */
692 ret = __is_preconditions_handled(ad);
698 if (_create_wifi_hotspot_on_popup(ad) < 0) {
699 ERR("__create_wifi_hotspot_on_popup fail\n");
703 __MOBILE_AP_FUNC_EXIT__;
708 int _handle_bt_onoff_change(mh_appdata_t *ad)
710 __MOBILE_AP_FUNC_ENTER__;
714 /* Turn off Bluetooth tethering */
715 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_BT) {
716 ret = tethering_disable(ad->handle, TETHERING_TYPE_BT);
718 ERR("Error disable bt tethering [%d]\n", ret);
724 /* Turn on Bluetooth tethering */
725 ret = __is_preconditions_handled(ad);
731 if (_create_bt_tethering_on_popup(ad) < 0) {
732 ERR("_create_bt_tethering_on_popup fail\n");
736 __MOBILE_AP_FUNC_EXIT__;
741 int _handle_usb_onoff_change(mh_appdata_t *ad)
743 __MOBILE_AP_FUNC_ENTER__;
747 /* Turn off USB tethering */
748 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_USB) {
749 ret = tethering_disable(ad->handle, TETHERING_TYPE_USB);
751 ERR("Error disable usb tethering : %d\n", ret);
757 /* Turn on USB tethering */
758 ret = __is_preconditions_handled(ad);
764 if (_create_usb_tethering_on_popup(ad) < 0) {
765 ERR("_create_usb_tethering_on_popup fail\n");
769 __MOBILE_AP_FUNC_EXIT__;
774 int _turn_on_wifi(void)
778 ret = wifi_activate(__wifi_activated_cb, NULL);
779 if (ret != WIFI_ERROR_NONE) {
780 ERR("wifi_activate() is failed : %d\n", ret);
787 bool _is_wifi_direct_on(void)
789 int wifi_direct_state = 0;
792 ret = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
794 ERR("vconf_get_int() is failed : %d\n", ret);
798 return wifi_direct_state != 0 ? true : false;