4 * Copyright 2012-2013 Samsung Electronics Co., Ltd
6 * Licensed under the Flora License, Version 1.1 (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://floralicense.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.
21 #include <wifi-direct.h>
23 #include "mh_func_onoff.h"
25 static bool __get_vconf_prev_wifi_state()
29 if (vconf_get_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, &value) < 0) {
30 ERR("vconf_get_bool is failed\n");
34 DBG("%s : %d\n", VCONF_MOBILE_AP_PREV_WIFI_STATUS, value);
36 return value ? true : false;
39 int _get_vconf_usb_state()
41 int value = VCONFKEY_SYSMAN_USB_DISCONNECTED;
43 if (vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &value) < 0) {
44 ERR("vconf_get_int is failed\n");
48 DBG("%s : %d\n", VCONFKEY_SYSMAN_USB_STATUS, value);
53 static bool __is_connected_wifi_net(mh_appdata_t *ad)
55 connection_wifi_state_e wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;
58 ret = connection_get_wifi_state(ad->conn_handle, &wifi_state);
59 if (ret != CONNECTION_ERROR_NONE) {
60 ERR("connection_get_wifi_state() is failed : %d\n");
64 if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED) {
65 ERR("Wi-Fi network is not connected : %d\n", wifi_state);
69 DBG("Wi-Fi network is connected\n");
73 static bool __is_connected_ethernet_net(mh_appdata_t *ad)
75 connection_ethernet_state_e ethernet_state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
78 ret = connection_get_ethernet_state(ad->conn_handle, ðernet_state);
79 if (ret != CONNECTION_ERROR_NONE) {
80 ERR("connection_get_ethernet_state() is failed : %d\n");
84 if (ethernet_state != CONNECTION_ETHERNET_STATE_CONNECTED) {
85 ERR("Ethernet network is not connected : %d\n", ethernet_state);
89 DBG("Ethernet network is connected\n");
93 static bool __is_connected_cellular_net(mh_appdata_t *ad)
95 connection_cellular_state_e cellular_state = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
98 ret = connection_get_cellular_state(ad->conn_handle, &cellular_state);
99 if (ret != CONNECTION_ERROR_NONE) {
100 ERR("connection_get_cellular_state() is failed : %d\n");
104 if (cellular_state == CONNECTION_CELLULAR_STATE_FLIGHT_MODE) {
105 _prepare_popup(MH_POP_INFORMATION_WO_BUTTON,
106 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING_IN_FLIGHT_MODE_TO_USE_TETHERING_DISABLE_FLIGHT_MODE"));
108 ERR("Cellular network is not connected\n");
110 } else if (cellular_state != CONNECTION_CELLULAR_STATE_CONNECTED &&
111 cellular_state != CONNECTION_CELLULAR_STATE_AVAILABLE) {
112 _prepare_popup(MH_POP_INFORMATION,
113 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_PACKET_DATA_SERVICE_OUT_OF_COVERAGE"));
115 ERR("Cellular network is not connected : %d\n", cellular_state);
119 DBG("Cellular network is connected\n");
123 static int __create_wifi_hotspot_on_popup(mh_appdata_t *ad)
127 bool wifi_state = false;
129 wifi_is_activated(&wifi_state);
130 if (wifi_state == true || _is_wifi_direct_on() == true)
131 fmt = _("IDS_ST_BODY_WI_FI_NETWORK_WILL_BE_DISCONNECTED_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE_THE_MAXIMUM_NUMBER_OF_TETHERED_DEVICES_ALLOWED_IS_PD");
133 fmt = _("IDS_ST_BODY_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE_THE_MAXIMUM_NUMBER_OF_TETHERED_DEVICES_ALLOWED_IS_PD");
135 str = malloc(MH_LABEL_LENGTH_MAX);
136 snprintf(str, MH_LABEL_LENGTH_MAX, fmt, TETHERING_WIFI_MAX_CONNECTED_STA);
137 _prepare_popup(MH_POP_WIFI_ON_CONF, str);
145 static int __create_bt_tethering_on_popup(mh_appdata_t *ad)
149 str = malloc(MH_LABEL_LENGTH_MAX);
150 snprintf(str, MH_LABEL_LENGTH_MAX,
151 _("IDS_ST_BODY_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE_THE_MAXIMUM_NUMBER_OF_TETHERED_DEVICES_ALLOWED_IS_PD"),
152 TETHERING_BT_MAX_CONNECTED_STA);
153 _prepare_popup(MH_POP_BT_ON_CONF, str);
161 static int __create_usb_tethering_on_popup(mh_appdata_t *ad)
165 str = _("IDS_MOBILEAP_POP_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE");
166 _prepare_popup(MH_POP_USB_ON_CONF, str);
172 static void __disable_tethering_by_ind(mh_appdata_t *ad, tethering_disabled_cause_e cause)
175 ERR("Param is NULL\n");
179 DBG("cause : %d\n", cause);
181 case TETHERING_DISABLED_BY_WIFI_ON:
182 DBG("TETHERING_DISABLED_IND by WIFI\n");
185 case TETHERING_DISABLED_BY_BT_OFF:
186 DBG("TETHERING_DISABLED_BY_BT_DEACTIVATION\n");
189 case TETHERING_DISABLED_BY_USB_DISCONNECTION:
190 DBG("TETHERING_DISABLED_IND by USB DISCONNECT\n");
193 case TETHERING_DISABLED_BY_FLIGHT_MODE:
194 DBG("TETHERING_DISABLED_IND by FLIGHT_MODE\n");
197 case TETHERING_DISABLED_BY_TIMEOUT:
198 DBG("TETHERING_DISABLED_BY_TIMEOUT\n");
201 case TETHERING_DISABLED_BY_OTHERS:
202 DBG("TETHERING_DISABLED_IND by OTHERS\n");
205 case TETHERING_DISABLED_BY_LOW_BATTERY:
206 DBG("TETHERING_DISABLED_IND by LOW_BATTERY\n");
210 DBG("TETHERING_DISABLED_IND Default\n");
217 /* Wi-Fi Direct callback */
218 static void _wifi_direct_state_cb(int error_code, wifi_direct_device_state_e state, void *user_data)
222 if (user_data == NULL) {
223 ERR("The param is NULL\n");
227 mh_appdata_t *ad = (mh_appdata_t *)user_data;
230 wifi_direct_unset_device_state_changed_cb();
231 wifi_direct_deinitialize();
234 if (error_code != 0) {
235 ERR("wifi_direct_deactivate fail in cb : %d\n", error_code);
236 _update_main_view(ad);
240 if (state != WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
241 ERR("Unknown state : %d\n", state);
245 ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
246 if (ret != TETHERING_ERROR_NONE) {
247 ERR("wifi tethering on is failed : %d\n", ret);
248 _update_main_view(ad);
256 /* Wi-Fi callbacks */
257 static void __wifi_activated_cb(wifi_error_e result, void *user_data)
259 __MOBILE_AP_FUNC_ENTER__;
261 if (user_data == NULL) {
262 ERR("The param is NULL\n");
266 mh_appdata_t *ad = (mh_appdata_t *)user_data;
269 if (result != WIFI_ERROR_NONE) {
270 ERR("__wifi_activated_cb error : %d\n", result);
271 _update_main_view(ad);
275 DBG("Wi-Fi on is done\n");
277 ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
278 if (ret != TETHERING_ERROR_NONE) {
279 ERR("wifi tethering on is failed : %d\n", ret);
282 _update_main_view(ad);
284 __MOBILE_AP_FUNC_EXIT__;
288 static void __wifi_deactivated_cb(wifi_error_e result, void *user_data)
290 __MOBILE_AP_FUNC_ENTER__;
292 if (user_data == NULL) {
293 ERR("The param is NULL\n");
297 mh_appdata_t *ad = (mh_appdata_t *)user_data;
300 if (result != WIFI_ERROR_NONE) {
301 ERR("__wifi_deactivated_cb error : %d\n", result);
302 _update_main_view(ad);
306 DBG("Wi-Fi is turned off\n");
308 ret = vconf_set_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, 1);
310 ERR("vconf_set_bool() is failed : %d\n", ret);
312 ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
313 if (ret != TETHERING_ERROR_NONE) {
314 ERR("wifi tethering on is failed : %d\n", ret);
315 _update_main_view(ad);
319 __MOBILE_AP_FUNC_EXIT__;
323 /* Tethering callbacks */
324 void _enabled_cb(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
326 if (user_data == NULL) {
327 ERR("user_data is NULL\n");
331 mh_appdata_t *ad = (mh_appdata_t *)user_data;
333 ad->main.need_recover_wifi_tethering = false;
336 if (NULL != ad->popup) {
337 evas_object_del(ad->popup);
340 _update_main_view(ad);
345 if (result != TETHERING_ERROR_NONE) {
346 _prepare_popup(MH_POP_INFORMATION,
347 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING"));
351 _update_main_view(ad);
356 void _disabled_cb(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
358 if (user_data == NULL) {
359 ERR("user_data is NULL\n");
363 if (result == TETHERING_ERROR_NONE) {
364 int ret = vconf_set_int(
365 VCONFKEY_MOBILE_HOTSPOT_WIFI_STATE,
366 VCONFKEY_MOBILE_HOTSPOT_WIFI_OFF);
368 ERR("vconf_set_int() is failed : %d\n", ret);
371 mh_appdata_t *ad = (mh_appdata_t *)user_data;
373 if (ad->main.need_recover_wifi_tethering == true) {
374 DBG("Wi-Fi tethering will be turned on again\n");
378 if (cause != TETHERING_DISABLED_BY_REQUEST) {
379 DBG("Tethering [%d] is disabled because of [%d]\n", type, cause);
380 if (NULL != ad->popup) {
381 evas_object_del(ad->popup);
384 _update_main_view(ad);
385 __disable_tethering_by_ind(ad, cause);
389 if (result != TETHERING_ERROR_NONE) {
390 _prepare_popup(MH_POP_INFORMATION,
391 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING"));
393 _update_main_view(ad);
397 DBG("Tethering [%d] is disabled by reqeust\n", type);
399 _update_main_view(ad);
404 void _connection_changed_cb(tethering_client_h client, bool is_opened, void *user_data)
406 if (user_data == NULL) {
407 ERR("user_data is NULL\n");
411 mh_appdata_t *ad = (mh_appdata_t *)user_data;
414 tethering_client_get_name(client, &name);
415 DBG("Client %s is %s\n", name, is_opened ? "connected" : "disconnected");
419 ap_update_data_device(ad);
424 void _data_usage_cb(tethering_error_e result, unsigned long long received_data, unsigned long long sent_data, void *user_data)
426 if (user_data == NULL) {
427 ERR("user_data is NULL\n");
431 mh_appdata_t *ad = (mh_appdata_t *)user_data;
433 if (ad->data_statistics.pdp_total_sent != sent_data ||
434 ad->data_statistics.pdp_total_receive != received_data) {
435 ad->data_statistics.pdp_total_sent = sent_data;
436 ad->data_statistics.pdp_total_receive = received_data;
437 ap_update_data_packet_usage(ad);
440 ad->data_statistics.is_updated = true;
444 /* End of Tethering callbacks */
446 int _handle_wifi_onoff_change(mh_appdata_t *ad)
448 __MOBILE_AP_FUNC_ENTER__;
451 int connected_wifi_clients = 0;
453 /* Turn off WiFi hotspot */
454 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI) {
455 if (_get_no_of_connected_device(ad->handle, &connected_wifi_clients,
456 TETHERING_TYPE_WIFI) == FALSE) {
457 ERR("Getting the number of connected device is failed\n");
459 if (connected_wifi_clients > 0) {
460 _prepare_popup(MH_POP_WIFI_OFF_CONF,
461 _("IDS_MOBILEAP_POP_DISABLING_TETHERING_WILL_PREVENT_LINKED_DEVICES_FROM_ACCESSING_THE_INTERNET_CONTINUE_Q"));
464 ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
465 if (ret != TETHERING_ERROR_NONE) {
466 ERR("wifi tethering off is failed : %d\n", ret);
473 /* Turn on WiFi hotspot */
474 if (!__is_connected_ethernet_net(ad) && !__is_connected_cellular_net(ad)) {
475 ERR("There is no connected network\n");
479 if (__create_wifi_hotspot_on_popup(ad) < 0) {
480 ERR("__create_wifi_hotspot_on_popup fail\n");
484 __MOBILE_AP_FUNC_EXIT__;
489 int _handle_bt_onoff_change(mh_appdata_t *ad)
491 __MOBILE_AP_FUNC_ENTER__;
495 /* Turn off Bluetooth tethering */
496 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_BT) {
497 ret = tethering_disable(ad->handle, TETHERING_TYPE_BT);
499 ERR("Error disable bt tethering [%d]\n", ret);
505 /* Turn on Bluetooth tethering */
506 if (!__is_connected_ethernet_net(ad) && !__is_connected_wifi_net(ad) &&
507 !__is_connected_cellular_net(ad)) {
508 ERR("There is no connected network\n");
512 if (__create_bt_tethering_on_popup(ad) < 0) {
513 ERR("__create_bt_tethering_on_popup fail\n");
517 __MOBILE_AP_FUNC_EXIT__;
522 int _handle_usb_onoff_change(mh_appdata_t *ad)
524 __MOBILE_AP_FUNC_ENTER__;
528 /* Turn off USB tethering */
529 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_USB) {
530 ret = tethering_disable(ad->handle, TETHERING_TYPE_USB);
532 DBG("Error disable usb tethering : %d\n", ret);
538 /* Turn on USB tethering */
539 if (!__is_connected_ethernet_net(ad) && !__is_connected_wifi_net(ad) &&
540 !__is_connected_cellular_net(ad)) {
541 ERR("There is no connected network\n");
545 if (__create_usb_tethering_on_popup(ad) < 0) {
546 ERR("__create_usb_tethering_on_popup fail\n");
550 __MOBILE_AP_FUNC_EXIT__;
555 int _turn_off_wifi(mh_appdata_t *ad)
559 ret = wifi_deactivate(__wifi_deactivated_cb, (void *)ad);
560 if (ret != WIFI_ERROR_NONE) {
561 ERR("wifi_deactivate() is failed : %d\n", ret);
568 int _turn_on_wifi(mh_appdata_t *ad)
572 ret = wifi_activate(__wifi_activated_cb, (void *)ad);
573 if (ret != WIFI_ERROR_NONE) {
574 ERR("wifi_activate() is failed : %d\n", ret);
581 bool _is_wifi_direct_on(void)
583 int wifi_direct_state = 0;
586 ret = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
588 ERR("vconf_get_int() is failed : %d\n", ret);
592 return wifi_direct_state != 0 ? true : false;
595 int _turn_off_wifi_direct(mh_appdata_t *ad)
599 ret = wifi_direct_initialize();
601 ERR("wifi_direct_initialize() is failed : %d\n", ret);
605 ret = wifi_direct_set_device_state_changed_cb(_wifi_direct_state_cb, (void *)ad);
607 ERR("wifi_direct_set_device_state_changed_cb() is failed : %d\n", ret);
608 ret = wifi_direct_deinitialize();
609 DBG("wifi_direct_deinitialize() ret : %d\n", ret);
613 ret = wifi_direct_deactivate();
615 ERR("wifi_direct_deactivate() is failed : %d\n", ret);
616 ret = wifi_direct_unset_device_state_changed_cb();
617 DBG("wifi_direct_unset_device_state_changed_cb() ret : %d\n", ret);
618 ret = wifi_direct_deinitialize();
619 DBG("wifi_direct_deinitialize() ret : %d\n", ret);