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;
96 sim_state_e sim_state = SIM_STATE_UNAVAILABLE;
100 ret = sim_get_state(&sim_state);
101 if (ret != SIM_ERROR_NONE) {
102 ERR("sim_get_state() is failed : %d\n", ret);
103 _prepare_popup(MH_POP_INFORMATION,
104 _("IDS_MOBILEAP_POP_INSERT_SIM_CARD_AND_RESTART_DEVICE_TO_USE_TETHERING"));
108 DBG("SIM State : %d\n", sim_state);
109 if (sim_state != SIM_STATE_AVAILABLE) {
110 _prepare_popup(MH_POP_INFORMATION,
111 _("IDS_MOBILEAP_POP_INSERT_SIM_CARD_AND_RESTART_DEVICE_TO_USE_TETHERING"));
116 ret = connection_get_cellular_state(ad->conn_handle, &cellular_state);
117 if (ret != CONNECTION_ERROR_NONE) {
118 ERR("connection_get_cellular_state() is failed : %d\n");
122 if (cellular_state == CONNECTION_CELLULAR_STATE_FLIGHT_MODE) {
123 _prepare_popup(MH_POP_INFORMATION_WO_BUTTON,
124 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING_IN_FLIGHT_MODE_TO_USE_TETHERING_DISABLE_FLIGHT_MODE"));
126 ERR("Cellular network is not connected\n");
128 } else if (cellular_state != CONNECTION_CELLULAR_STATE_CONNECTED &&
129 cellular_state != CONNECTION_CELLULAR_STATE_AVAILABLE) {
130 _prepare_popup(MH_POP_INFORMATION,
131 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_PACKET_DATA_SERVICE_OUT_OF_COVERAGE"));
133 ERR("Cellular network is not connected : %d\n", cellular_state);
137 DBG("Cellular network is connected\n");
141 static int __create_wifi_hotspot_on_popup(mh_appdata_t *ad)
145 bool wifi_state = false;
147 wifi_is_activated(&wifi_state);
148 if (wifi_state == true || _is_wifi_direct_on() == true)
149 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");
151 fmt = _("IDS_ST_BODY_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE_THE_MAXIMUM_NUMBER_OF_TETHERED_DEVICES_ALLOWED_IS_PD");
153 str = malloc(MH_LABEL_LENGTH_MAX);
154 snprintf(str, MH_LABEL_LENGTH_MAX, fmt, TETHERING_WIFI_MAX_CONNECTED_STA);
155 _prepare_popup(MH_POP_WIFI_ON_CONF, str);
163 static int __create_bt_tethering_on_popup(mh_appdata_t *ad)
167 str = malloc(MH_LABEL_LENGTH_MAX);
168 snprintf(str, MH_LABEL_LENGTH_MAX,
169 _("IDS_ST_BODY_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE_THE_MAXIMUM_NUMBER_OF_TETHERED_DEVICES_ALLOWED_IS_PD"),
170 TETHERING_BT_MAX_CONNECTED_STA);
171 _prepare_popup(MH_POP_BT_ON_CONF, str);
179 static int __create_usb_tethering_on_popup(mh_appdata_t *ad)
183 str = _("IDS_MOBILEAP_POP_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE");
184 _prepare_popup(MH_POP_USB_ON_CONF, str);
190 static void __disable_tethering_by_ind(mh_appdata_t *ad, tethering_disabled_cause_e cause)
193 ERR("Param is NULL\n");
197 DBG("cause : %d\n", cause);
199 case TETHERING_DISABLED_BY_WIFI_ON:
200 DBG("TETHERING_DISABLED_IND by WIFI\n");
203 case TETHERING_DISABLED_BY_BT_OFF:
204 DBG("TETHERING_DISABLED_BY_BT_DEACTIVATION\n");
207 case TETHERING_DISABLED_BY_USB_DISCONNECTION:
208 DBG("TETHERING_DISABLED_IND by USB DISCONNECT\n");
211 case TETHERING_DISABLED_BY_FLIGHT_MODE:
212 DBG("TETHERING_DISABLED_IND by FLIGHT_MODE\n");
215 case TETHERING_DISABLED_BY_TIMEOUT:
216 DBG("TETHERING_DISABLED_BY_TIMEOUT\n");
219 case TETHERING_DISABLED_BY_OTHERS:
220 DBG("TETHERING_DISABLED_IND by OTHERS\n");
223 case TETHERING_DISABLED_BY_LOW_BATTERY:
224 DBG("TETHERING_DISABLED_IND by LOW_BATTERY\n");
227 case TETHERING_DISABLED_BY_MDM_ON:
228 DBG("TETHERING_DISABLED_IND by MDM\n");
232 DBG("TETHERING_DISABLED_IND Default\n");
239 /* Wi-Fi Direct callback */
240 static void _wifi_direct_state_cb(int error_code, wifi_direct_device_state_e state, void *user_data)
244 if (user_data == NULL) {
245 ERR("The param is NULL\n");
249 mh_appdata_t *ad = (mh_appdata_t *)user_data;
252 wifi_direct_unset_device_state_changed_cb();
253 wifi_direct_deinitialize();
256 if (error_code != 0) {
257 ERR("wifi_direct_deactivate fail in cb : %d\n", error_code);
258 _update_main_view(ad);
262 if (state != WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
263 ERR("Unknown state : %d\n", state);
267 ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
268 if (ret != TETHERING_ERROR_NONE) {
269 ERR("wifi tethering on is failed : %d\n", ret);
270 _update_main_view(ad);
278 /* Wi-Fi callbacks */
279 static void __wifi_activated_cb(wifi_error_e result, void *user_data)
281 __MOBILE_AP_FUNC_ENTER__;
283 if (user_data == NULL) {
284 ERR("The param is NULL\n");
288 mh_appdata_t *ad = (mh_appdata_t *)user_data;
291 if (result != WIFI_ERROR_NONE) {
292 ERR("__wifi_activated_cb error : %d\n", result);
293 _update_main_view(ad);
297 DBG("Wi-Fi on is done\n");
299 ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
300 if (ret != TETHERING_ERROR_NONE) {
301 ERR("wifi tethering on is failed : %d\n", ret);
304 _update_main_view(ad);
306 __MOBILE_AP_FUNC_EXIT__;
310 static void __wifi_deactivated_cb(wifi_error_e result, void *user_data)
312 __MOBILE_AP_FUNC_ENTER__;
314 if (user_data == NULL) {
315 ERR("The param is NULL\n");
319 mh_appdata_t *ad = (mh_appdata_t *)user_data;
322 if (result != WIFI_ERROR_NONE) {
323 ERR("__wifi_deactivated_cb error : %d\n", result);
324 _update_main_view(ad);
328 DBG("Wi-Fi is turned off\n");
330 ret = vconf_set_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, 1);
332 ERR("vconf_set_bool() is failed : %d\n", ret);
334 ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
335 if (ret != TETHERING_ERROR_NONE) {
336 ERR("wifi tethering on is failed : %d\n", ret);
337 _update_main_view(ad);
341 __MOBILE_AP_FUNC_EXIT__;
345 /* Tethering callbacks */
346 void _enabled_cb(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
348 if (user_data == NULL) {
349 ERR("user_data is NULL\n");
353 if (result == TETHERING_ERROR_NONE) {
354 int ret = vconf_set_int(
355 VCONFKEY_MOBILE_HOTSPOT_WIFI_STATE,
356 VCONFKEY_MOBILE_HOTSPOT_WIFI_ON);
358 ERR("vconf_set_int() is failed : %d\n", ret);
361 mh_appdata_t *ad = (mh_appdata_t *)user_data;
363 ad->main.need_recover_wifi_tethering = false;
366 if (NULL != ad->popup) {
367 evas_object_del(ad->popup);
370 _update_main_view(ad);
375 if (result != TETHERING_ERROR_NONE) {
376 _prepare_popup(MH_POP_INFORMATION,
377 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING"));
381 _update_main_view(ad);
386 void _disabled_cb(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
388 if (user_data == NULL) {
389 ERR("user_data is NULL\n");
393 if (result == TETHERING_ERROR_NONE) {
394 int ret = vconf_set_int(
395 VCONFKEY_MOBILE_HOTSPOT_WIFI_STATE,
396 VCONFKEY_MOBILE_HOTSPOT_WIFI_OFF);
398 ERR("vconf_set_int() is failed : %d\n", ret);
401 mh_appdata_t *ad = (mh_appdata_t *)user_data;
403 if (ad->main.need_recover_wifi_tethering == true) {
404 DBG("Wi-Fi tethering will be turned on again\n");
408 if (cause != TETHERING_DISABLED_BY_REQUEST) {
409 DBG("Tethering [%d] is disabled because of [%d]\n", type, cause);
410 if (NULL != ad->popup) {
411 evas_object_del(ad->popup);
414 _update_main_view(ad);
415 __disable_tethering_by_ind(ad, cause);
419 if (result != TETHERING_ERROR_NONE) {
420 _prepare_popup(MH_POP_INFORMATION,
421 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING"));
423 _update_main_view(ad);
427 DBG("Tethering [%d] is disabled by reqeust\n", type);
429 _update_main_view(ad);
434 void _connection_changed_cb(tethering_client_h client, bool is_opened, void *user_data)
436 if (user_data == NULL) {
437 ERR("user_data is NULL\n");
441 mh_appdata_t *ad = (mh_appdata_t *)user_data;
444 tethering_client_get_name(client, &name);
445 DBG("Client %s is %s\n", name, is_opened ? "connected" : "disconnected");
449 ap_update_data_device(ad);
454 void _data_usage_cb(tethering_error_e result, unsigned long long received_data, unsigned long long sent_data, void *user_data)
456 if (user_data == NULL) {
457 ERR("user_data is NULL\n");
461 mh_appdata_t *ad = (mh_appdata_t *)user_data;
463 if (ad->data_statistics.pdp_total_sent != sent_data ||
464 ad->data_statistics.pdp_total_receive != received_data) {
465 ad->data_statistics.pdp_total_sent = sent_data;
466 ad->data_statistics.pdp_total_receive = received_data;
467 ap_update_data_packet_usage(ad);
470 ad->data_statistics.is_updated = true;
474 /* End of Tethering callbacks */
476 int _handle_wifi_onoff_change(mh_appdata_t *ad)
478 __MOBILE_AP_FUNC_ENTER__;
481 int connected_wifi_clients = 0;
483 /* Turn off WiFi hotspot */
484 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI) {
485 if (_get_no_of_connected_device(ad->handle, &connected_wifi_clients,
486 TETHERING_TYPE_WIFI) == FALSE) {
487 ERR("Getting the number of connected device is failed\n");
489 if (connected_wifi_clients > 0) {
490 _prepare_popup(MH_POP_WIFI_OFF_CONF,
491 _("IDS_MOBILEAP_POP_DISABLING_TETHERING_WILL_PREVENT_LINKED_DEVICES_FROM_ACCESSING_THE_INTERNET_CONTINUE_Q"));
495 VCONFKEY_MOBILE_HOTSPOT_WIFI_STATE,
496 VCONFKEY_MOBILE_HOTSPOT_WIFI_PENDING_OFF);
498 ERR("vconf_set_int() is failed : %d\n", ret);
500 ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
501 if (ret != TETHERING_ERROR_NONE) {
502 ERR("wifi tethering off is failed : %d\n", ret);
509 /* Turn on WiFi hotspot */
510 if (!__is_connected_ethernet_net(ad) && !__is_connected_cellular_net(ad)) {
511 ERR("There is no connected network\n");
515 if (__create_wifi_hotspot_on_popup(ad) < 0) {
516 ERR("__create_wifi_hotspot_on_popup fail\n");
520 __MOBILE_AP_FUNC_EXIT__;
525 int _handle_bt_onoff_change(mh_appdata_t *ad)
527 __MOBILE_AP_FUNC_ENTER__;
531 /* Turn off Bluetooth tethering */
532 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_BT) {
533 ret = tethering_disable(ad->handle, TETHERING_TYPE_BT);
535 ERR("Error disable bt tethering [%d]\n", ret);
541 /* Turn on Bluetooth tethering */
542 if (!__is_connected_ethernet_net(ad) && !__is_connected_wifi_net(ad) &&
543 !__is_connected_cellular_net(ad)) {
544 ERR("There is no connected network\n");
548 if (__create_bt_tethering_on_popup(ad) < 0) {
549 ERR("__create_bt_tethering_on_popup fail\n");
553 __MOBILE_AP_FUNC_EXIT__;
558 int _handle_usb_onoff_change(mh_appdata_t *ad)
560 __MOBILE_AP_FUNC_ENTER__;
564 /* Turn off USB tethering */
565 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_USB) {
566 ret = tethering_disable(ad->handle, TETHERING_TYPE_USB);
568 DBG("Error disable usb tethering : %d\n", ret);
574 /* Turn on USB tethering */
575 if (!__is_connected_ethernet_net(ad) && !__is_connected_wifi_net(ad) &&
576 !__is_connected_cellular_net(ad)) {
577 ERR("There is no connected network\n");
581 if (__create_usb_tethering_on_popup(ad) < 0) {
582 ERR("__create_usb_tethering_on_popup fail\n");
586 __MOBILE_AP_FUNC_EXIT__;
591 int _turn_off_wifi(mh_appdata_t *ad)
595 ret = wifi_deactivate(__wifi_deactivated_cb, (void *)ad);
596 if (ret != WIFI_ERROR_NONE) {
597 ERR("wifi_deactivate() is failed : %d\n", ret);
604 int _turn_on_wifi(mh_appdata_t *ad)
608 ret = wifi_activate(__wifi_activated_cb, (void *)ad);
609 if (ret != WIFI_ERROR_NONE) {
610 ERR("wifi_activate() is failed : %d\n", ret);
617 bool _is_wifi_direct_on(void)
619 int wifi_direct_state = 0;
622 ret = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
624 ERR("vconf_get_int() is failed : %d\n", ret);
628 return wifi_direct_state != 0 ? true : false;
631 int _turn_off_wifi_direct(mh_appdata_t *ad)
635 ret = wifi_direct_initialize();
637 ERR("wifi_direct_initialize() is failed : %d\n", ret);
641 ret = wifi_direct_set_device_state_changed_cb(_wifi_direct_state_cb, (void *)ad);
643 ERR("wifi_direct_set_device_state_changed_cb() is failed : %d\n", ret);
644 ret = wifi_direct_deinitialize();
645 DBG("wifi_direct_deinitialize() ret : %d\n", ret);
649 ret = wifi_direct_deactivate();
651 ERR("wifi_direct_deactivate() is failed : %d\n", ret);
652 ret = wifi_direct_unset_device_state_changed_cb();
653 DBG("wifi_direct_unset_device_state_changed_cb() ret : %d\n", ret);
654 ret = wifi_direct_deinitialize();
655 DBG("wifi_direct_deinitialize() ret : %d\n", ret);