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.
20 #include <wifi-direct.h>
22 #include "mh_func_onoff.h"
24 static bool __get_vconf_prev_wifi_state()
28 if (vconf_get_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, &value) < 0) {
29 ERR("vconf_get_bool is failed\n");
33 DBG("%s : %d\n", VCONF_MOBILE_AP_PREV_WIFI_STATUS, value);
35 return value ? true : false;
38 int _get_vconf_usb_state()
40 int value = VCONFKEY_SYSMAN_USB_DISCONNECTED;
42 if (vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &value) < 0) {
43 ERR("vconf_get_int is failed\n");
47 DBG("%s : %d\n", VCONFKEY_SYSMAN_USB_STATUS, value);
52 static bool __is_connected_wifi_net(mh_appdata_t *ad)
54 connection_wifi_state_e wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;
57 ret = connection_get_wifi_state(ad->conn_handle, &wifi_state);
58 if (ret != CONNECTION_ERROR_NONE) {
59 ERR("connection_get_wifi_state() is failed : %d\n");
63 if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED) {
64 ERR("Wi-Fi network is not connected : %d\n", wifi_state);
68 DBG("Wi-Fi network is connected\n");
72 static bool __is_connected_ethernet_net(mh_appdata_t *ad)
74 connection_ethernet_state_e ethernet_state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
77 ret = connection_get_ethernet_state(ad->conn_handle, ðernet_state);
78 if (ret != CONNECTION_ERROR_NONE) {
79 ERR("connection_get_ethernet_state() is failed : %d\n");
83 if (ethernet_state != CONNECTION_ETHERNET_STATE_CONNECTED) {
84 ERR("Ethernet network is not connected : %d\n", ethernet_state);
88 DBG("Ethernet network is connected\n");
92 static bool __is_connected_cellular_net(mh_appdata_t *ad)
94 connection_cellular_state_e cellular_state = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
95 sim_state_e sim_state = SIM_STATE_UNAVAILABLE;
99 ret = sim_get_state(&sim_state);
100 if (ret != SIM_ERROR_NONE) {
101 ERR("sim_get_state() is failed : %d\n", ret);
102 _prepare_popup(MH_POP_INFORMATION,
103 _("IDS_MOBILEAP_POP_INSERT_SIM_CARD_AND_RESTART_DEVICE_TO_USE_TETHERING"));
107 DBG("SIM State : %d\n", sim_state);
108 if (sim_state != SIM_STATE_AVAILABLE) {
109 _prepare_popup(MH_POP_INFORMATION,
110 _("IDS_MOBILEAP_POP_INSERT_SIM_CARD_AND_RESTART_DEVICE_TO_USE_TETHERING"));
115 ret = connection_get_cellular_state(ad->conn_handle, &cellular_state);
116 if (ret != CONNECTION_ERROR_NONE) {
117 ERR("connection_get_cellular_state() is failed : %d\n");
121 if (cellular_state == CONNECTION_CELLULAR_STATE_FLIGHT_MODE) {
122 _prepare_popup(MH_POP_INFORMATION_WO_BUTTON,
123 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING_IN_FLIGHT_MODE_TO_USE_TETHERING_DISABLE_FLIGHT_MODE"));
125 ERR("Cellular network is not connected\n");
127 } else if (cellular_state != CONNECTION_CELLULAR_STATE_CONNECTED &&
128 cellular_state != CONNECTION_CELLULAR_STATE_AVAILABLE) {
129 _prepare_popup(MH_POP_INFORMATION,
130 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_PACKET_DATA_SERVICE_OUT_OF_COVERAGE"));
132 ERR("Cellular network is not connected : %d\n", cellular_state);
136 DBG("Cellular network is connected\n");
140 static int __create_wifi_hotspot_on_popup(mh_appdata_t *ad)
143 bool wifi_state = false;
145 wifi_is_activated(&wifi_state);
146 if (wifi_state == true || _is_wifi_direct_on() == true)
147 str = _("IDS_MOBILEAP_POP_WI_FI_NETWORK_WILL_BE_DISCONNECTED_WI_FI_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE_CONTINUE_Q");
149 str = _("IDS_MOBILEAP_POP_WI_FI_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE_CONTINUE_Q");
150 _prepare_popup(MH_POP_WIFI_ON_CONF, str);
156 static int __create_bt_tethering_on_popup(mh_appdata_t *ad)
160 str = _("IDS_MOBILEAP_POP_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE");
161 _prepare_popup(MH_POP_BT_ON_CONF, str);
167 static int __create_usb_tethering_on_popup(mh_appdata_t *ad)
171 str = _("IDS_MOBILEAP_POP_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE");
172 _prepare_popup(MH_POP_USB_ON_CONF, str);
178 static void __disable_tethering_by_ind(mh_appdata_t *ad, tethering_disabled_cause_e cause)
181 ERR("Param is NULL\n");
185 DBG("cause : %d\n", cause);
187 case TETHERING_DISABLED_BY_WIFI_ON:
188 DBG("TETHERING_DISABLED_IND by WIFI\n");
191 case TETHERING_DISABLED_BY_BT_OFF:
192 DBG("TETHERING_DISABLED_BY_BT_DEACTIVATION\n");
195 case TETHERING_DISABLED_BY_USB_DISCONNECTION:
196 DBG("TETHERING_DISABLED_IND by USB DISCONNECT\n");
199 case TETHERING_DISABLED_BY_FLIGHT_MODE:
200 DBG("TETHERING_DISABLED_IND by FLIGHT_MODE\n");
203 case TETHERING_DISABLED_BY_TIMEOUT:
204 DBG("TETHERING_DISABLED_BY_TIMEOUT\n");
207 case TETHERING_DISABLED_BY_OTHERS:
208 DBG("TETHERING_DISABLED_IND by OTHERS\n");
211 case TETHERING_DISABLED_BY_LOW_BATTERY:
212 DBG("TETHERING_DISABLED_IND by LOW_BATTERY\n");
215 case TETHERING_DISABLED_BY_MDM_ON:
216 DBG("TETHERING_DISABLED_IND by MDM\n");
220 DBG("TETHERING_DISABLED_IND Default\n");
227 static void __recover_wifi_station_mode(void)
231 if (__get_vconf_prev_wifi_state() == false) {
232 DBG("No need to recover wifi station mode\n");
236 if (_turn_on_wifi() != 0)
237 ERR("_turn_on_wifi is failed\n");
238 if (vconf_set_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, 0) < 0)
239 ERR("vconf_set_bool failed\n");
244 /* Wi-Fi Direct callback */
245 static void _wifi_direct_state_cb(int error_code, wifi_direct_device_state_e state, void *user_data)
249 if (user_data == NULL) {
250 ERR("The param is NULL\n");
254 mh_appdata_t *ad = (mh_appdata_t *)user_data;
257 wifi_direct_unset_device_state_changed_cb();
258 wifi_direct_deinitialize();
261 if (error_code != 0) {
262 ERR("wifi_direct_deactivate fail in cb : %d\n", error_code);
263 _update_main_view(ad);
267 if (state != WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
268 ERR("Unknown state : %d\n", state);
272 ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
273 if (ret != TETHERING_ERROR_NONE) {
274 ERR("wifi tethering on is failed : %d\n", ret);
275 _update_main_view(ad);
283 /* Wi-Fi callbacks */
284 static void __wifi_activated_cb(wifi_error_e result, void *user_data)
286 __MOBILE_AP_FUNC_ENTER__;
288 DBG("Wi-Fi on is done\n");
290 __MOBILE_AP_FUNC_EXIT__;
294 static void __wifi_deactivated_cb(wifi_error_e result, void *user_data)
296 __MOBILE_AP_FUNC_ENTER__;
298 if (user_data == NULL) {
299 ERR("The param is NULL\n");
303 mh_appdata_t *ad = (mh_appdata_t *)user_data;
306 if (result != WIFI_ERROR_NONE) {
307 ERR("__wifi_deactivated_cb error : %d\n", result);
308 _update_main_view(ad);
312 DBG("Wi-Fi is turned off\n");
314 ret = vconf_set_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, 1);
316 ERR("vconf_set_bool() is failed : %d\n", ret);
318 ret = tethering_enable(ad->handle, TETHERING_TYPE_WIFI);
319 if (ret != TETHERING_ERROR_NONE) {
320 ERR("wifi tethering on is failed : %d\n", ret);
321 _update_main_view(ad);
325 __MOBILE_AP_FUNC_EXIT__;
329 /* Tethering callbacks */
330 void _enabled_cb(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
332 if (user_data == NULL) {
333 ERR("user_data is NULL\n");
337 mh_appdata_t *ad = (mh_appdata_t *)user_data;
339 ad->main.need_recover_wifi_tethering = false;
342 if (NULL != ad->popup) {
343 evas_object_del(ad->popup);
346 _update_main_view(ad);
351 if (result != TETHERING_ERROR_NONE) {
352 _prepare_popup(MH_POP_INFORMATION,
353 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING"));
357 _update_main_view(ad);
362 void _disabled_cb(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
364 if (user_data == NULL) {
365 ERR("user_data is NULL\n");
369 mh_appdata_t *ad = (mh_appdata_t *)user_data;
371 if (ad->main.need_recover_wifi_tethering == true) {
372 DBG("Wi-Fi tethering will be turned on again\n");
376 if (cause != TETHERING_DISABLED_BY_REQUEST) {
377 DBG("Tethering [%d] is disabled because of [%d]\n", type, cause);
378 if (NULL != ad->popup) {
379 evas_object_del(ad->popup);
382 _update_main_view(ad);
383 __disable_tethering_by_ind(ad, cause);
387 if (result != TETHERING_ERROR_NONE) {
388 _prepare_popup(MH_POP_INFORMATION,
389 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING"));
391 _update_main_view(ad);
395 DBG("Tethering [%d] is disabled by reqeust\n", type);
396 if (type == TETHERING_TYPE_WIFI) {
397 __recover_wifi_station_mode();
400 _update_main_view(ad);
405 void _connection_changed_cb(tethering_client_h client, bool is_opened, void *user_data)
407 if (user_data == NULL) {
408 ERR("user_data is NULL\n");
412 mh_appdata_t *ad = (mh_appdata_t *)user_data;
415 tethering_client_get_name(client, &name);
416 DBG("Client %s is %s\n", name, is_opened ? "connected" : "disconnected");
420 ap_update_data_device(ad);
425 void _data_usage_cb(tethering_error_e result, unsigned long long received_data, unsigned long long sent_data, void *user_data)
427 if (user_data == NULL) {
428 ERR("user_data is NULL\n");
432 mh_appdata_t *ad = (mh_appdata_t *)user_data;
434 if (ad->data_statistics.pdp_total_sent != sent_data ||
435 ad->data_statistics.pdp_total_receive != received_data) {
436 ad->data_statistics.pdp_total_sent = sent_data;
437 ad->data_statistics.pdp_total_receive = received_data;
438 ap_update_data_packet_usage(ad);
441 ad->data_statistics.is_updated = true;
445 /* End of Tethering callbacks */
447 int _handle_wifi_onoff_change(mh_appdata_t *ad)
449 __MOBILE_AP_FUNC_ENTER__;
452 int connected_wifi_clients = 0;
454 /* Turn off WiFi hotspot */
455 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI) {
456 if (_get_no_of_connected_device(ad->handle, &connected_wifi_clients,
457 TETHERING_TYPE_WIFI) == FALSE) {
458 ERR("Getting the number of connected device is failed\n");
460 if (connected_wifi_clients > 0) {
461 _prepare_popup(MH_POP_WIFI_OFF_CONF,
462 _("IDS_MOBILEAP_POP_DISABLING_TETHERING_WILL_PREVENT_LINKED_DEVICES_FROM_ACCESSING_THE_INTERNET_CONTINUE_Q"));
465 ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
466 if (ret != TETHERING_ERROR_NONE) {
467 ERR("wifi tethering off is failed : %d\n", ret);
474 /* Turn on WiFi hotspot */
475 if (!__is_connected_ethernet_net(ad) && !__is_connected_cellular_net(ad)) {
476 ERR("There is no connected network\n");
480 if (__create_wifi_hotspot_on_popup(ad) < 0) {
481 ERR("__create_wifi_hotspot_on_popup fail\n");
485 __MOBILE_AP_FUNC_EXIT__;
490 int _handle_bt_onoff_change(mh_appdata_t *ad)
492 __MOBILE_AP_FUNC_ENTER__;
496 /* Turn off Bluetooth tethering */
497 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_BT) {
498 ret = tethering_disable(ad->handle, TETHERING_TYPE_BT);
500 ERR("Error disable bt tethering [%d]\n", ret);
506 /* Turn on Bluetooth tethering */
507 if (!__is_connected_ethernet_net(ad) && !__is_connected_wifi_net(ad) &&
508 !__is_connected_cellular_net(ad)) {
509 ERR("There is no connected network\n");
513 if (__create_bt_tethering_on_popup(ad) < 0) {
514 ERR("__create_bt_tethering_on_popup fail\n");
518 __MOBILE_AP_FUNC_EXIT__;
523 int _handle_usb_onoff_change(mh_appdata_t *ad)
525 __MOBILE_AP_FUNC_ENTER__;
529 /* Turn off USB tethering */
530 if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_USB) {
531 ret = tethering_disable(ad->handle, TETHERING_TYPE_USB);
533 DBG("Error disable usb tethering : %d\n", ret);
539 /* Turn on USB tethering */
540 if (!__is_connected_ethernet_net(ad) && !__is_connected_wifi_net(ad) &&
541 !__is_connected_cellular_net(ad)) {
542 ERR("There is no connected network\n");
546 if (__create_usb_tethering_on_popup(ad) < 0) {
547 ERR("__create_usb_tethering_on_popup fail\n");
551 __MOBILE_AP_FUNC_EXIT__;
556 int _turn_off_wifi(mh_appdata_t *ad)
560 ret = wifi_deactivate(__wifi_deactivated_cb, (void *)ad);
561 if (ret != WIFI_ERROR_NONE) {
562 ERR("wifi_deactivate() is failed : %d\n", ret);
569 int _turn_on_wifi(void)
573 ret = wifi_activate(__wifi_activated_cb, NULL);
574 if (ret != WIFI_ERROR_NONE) {
575 ERR("wifi_activate() is failed : %d\n", ret);
582 bool _is_wifi_direct_on(void)
584 int wifi_direct_state = 0;
587 ret = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
589 ERR("vconf_get_int() is failed : %d\n", ret);
593 return wifi_direct_state != 0 ? true : false;
596 int _turn_off_wifi_direct(mh_appdata_t *ad)
600 ret = wifi_direct_initialize();
602 ERR("wifi_direct_initialize() is failed : %d\n", ret);
606 ret = wifi_direct_set_device_state_changed_cb(_wifi_direct_state_cb, (void *)ad);
608 ERR("wifi_direct_set_device_state_changed_cb() is failed : %d\n", ret);
609 ret = wifi_direct_deinitialize();
610 DBG("wifi_direct_deinitialize() ret : %d\n", ret);
614 ret = wifi_direct_deactivate();
616 ERR("wifi_direct_deactivate() is failed : %d\n", ret);
617 ret = wifi_direct_unset_device_state_changed_cb();
618 DBG("wifi_direct_unset_device_state_changed_cb() ret : %d\n", ret);
619 ret = wifi_direct_deinitialize();
620 DBG("wifi_direct_deinitialize() ret : %d\n", ret);