Fix dlog format error
[apps/native/ug-mobile-ap.git] / src / mh_func_onoff.c
1 /*
2 * ug-mobile-ap
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
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
9
10 * http://www.tizenopensource.org/license
11
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.
17 *
18 */
19
20 #include <stdlib.h>
21 #include <glib.h>
22
23 #include "mh_func_onoff.h"
24 #include "mh_popup.h"
25 #include "mh_string.h"
26
27 static bool is_wifi_tethering_checkbox_popup_active = false;
28
29 void _update_tethering_enabling_item(mh_appdata_t *ad, tethering_type_e type, mh_state_e state)
30 {
31         switch (type) {
32         case TETHERING_TYPE_WIFI:
33                 _update_wifi_item(ad, MH_STATE_NONE);
34                 ad->is_wifi_teth_enabling = false;
35                 break;
36         case TETHERING_TYPE_BT:
37                 _update_bt_item(ad, MH_STATE_NONE);
38                 ad->is_bt_teth_enabling = false;
39                 break;
40         case TETHERING_TYPE_USB:
41                 _update_usb_item(ad, MH_STATE_NONE);
42                 ad->is_usb_teth_enabling = false;
43                 break;
44         default:
45                 ERR("invalid type \n");
46                 break;
47         }
48 }
49 void _wifi_tethering_checkbox_popup_status_set(bool value)
50 {
51         is_wifi_tethering_checkbox_popup_active = value;
52 }
53
54 bool _wifi_tethering_checkbox_popup_status_get(void)
55 {
56         return is_wifi_tethering_checkbox_popup_active;
57 }
58
59 int _get_vconf_usb_state()
60 {
61         int value = VCONFKEY_SYSMAN_USB_DISCONNECTED;
62
63         if (vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &value) < 0) {
64                 ERR("vconf_get_int is failed\n");
65                 return 0;
66         }
67         DBG("%s : %d\n", VCONFKEY_SYSMAN_USB_STATUS, value);
68
69         return value;
70 }
71
72 void _update_tethering_item(mh_appdata_t * ad, mh_state_e state)
73 {
74         ERR("type : %d state : %d\n", ad->type, state);
75         switch (ad->type) {
76         case TETHERING_TYPE_WIFI:
77                 _update_wifi_item(ad, state);
78                 break;
79         case TETHERING_TYPE_BT:
80                 _update_bt_item(ad, state);
81                 break;
82         case TETHERING_TYPE_USB:
83                 _update_usb_item(ad, state);
84                 break;
85         default:
86                 break;
87         }
88 }
89
90 gboolean _ps_recheck_timeout_cb(gpointer data)
91 {
92         connection_cellular_state_e cellular_state = _get_cellular_state();
93         mh_appdata_t *ad = (mh_appdata_t *)data;
94         static int recheck_count = 0;
95
96         DBG("Re-Check cellular state (%d)\n", recheck_count);
97
98         if (cellular_state == CONNECTION_CELLULAR_STATE_FLIGHT_MODE) {
99                 _update_tethering_item(ad, MH_STATE_NONE);
100                 recheck_count = 0;
101                 return FALSE;
102         }
103
104         if (cellular_state == CONNECTION_CELLULAR_STATE_CONNECTED ||
105                 cellular_state == CONNECTION_CELLULAR_STATE_AVAILABLE) {
106                 if (ad->type == TETHERING_TYPE_WIFI) {
107                         if (_create_wifi_hotspot_on_popup(ad) < 0) {
108                                 ERR("__create_wifi_hotspot_on_popup fail\n");
109                                 _update_tethering_item(ad, MH_STATE_NONE);
110                                 recheck_count = 0;
111                                 return FALSE;
112                         }
113                 } else if (ad->type == TETHERING_TYPE_BT && _create_bt_tethering_on_popup(ad) < 0) {
114                                 ERR("_create_bt_tethering_on_popup fail\n");
115                                 _update_tethering_item(ad, MH_STATE_NONE);
116                                 recheck_count = 0;
117                                 return FALSE;
118                 } else if (ad->type == TETHERING_TYPE_USB && _create_usb_tethering_on_popup(ad) < 0) {
119                                 ERR("__create_usb_hotspot_on_popup fail\n");
120                                 _update_tethering_item(ad, MH_STATE_NONE);
121                                 recheck_count = 0;
122                                 return FALSE;
123                 } else {
124                         ERR("Unknown tethering type \n");
125                         recheck_count = 0;
126                         return FALSE;
127                 }
128         } else {
129                 if (++recheck_count >= PS_RECHECK_COUNT_MAX) {
130                         DBG("Cellular network is not connected : %d\n", cellular_state);
131                         _update_tethering_item(ad, MH_STATE_NONE);
132                         _prepare_popup(MH_POPUP_NETWORK_OUT_OF_RANGE, STR_NO_DATA_SERVICE);
133                         _create_popup(ad);
134                         recheck_count = 0;
135                         return FALSE;
136                 }
137                 return TRUE;
138         }
139         recheck_count = 0;
140         return FALSE;
141 }
142
143 static bool __is_connected_wifi_net(mh_appdata_t *ad)
144 {
145         connection_wifi_state_e wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;
146         int ret;
147
148         ret = connection_get_wifi_state(ad->conn_handle, &wifi_state);
149         if (ret != CONNECTION_ERROR_NONE) {
150                 ERR("connection_get_wifi_state() is failed : %d\n", ret);
151                 return false;
152         }
153
154         if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED) {
155                 ERR("Wi-Fi network is not connected : %d\n", wifi_state);
156                 return false;
157         }
158
159         DBG("Wi-Fi network is connected\n");
160         return true;
161 }
162
163 static bool __is_connected_ethernet_net(mh_appdata_t *ad)
164 {
165         connection_ethernet_state_e ethernet_state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
166         int ret;
167
168         ret = connection_get_ethernet_state(ad->conn_handle, &ethernet_state);
169         if (ret != CONNECTION_ERROR_NONE) {
170                 ERR("connection_get_ethernet_state() is failed : %d\n", ret);
171                 return false;
172         }
173
174         if (ethernet_state != CONNECTION_ETHERNET_STATE_CONNECTED) {
175                 ERR("Ethernet network is not connected : %d\n", ethernet_state);
176                 return false;
177         }
178
179         DBG("Ethernet network is connected\n");
180         return true;
181 }
182
183 static int __is_preconditions_handled(mh_appdata_t *ad)
184 {
185         DBG("+\n");
186
187         connection_cellular_state_e cellular_state = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
188         int dnet_state = 0;
189
190         /* check ethernet connection */
191         if (__is_connected_ethernet_net(ad)) {
192                 DBG("ethernet network is connected\n");
193                 return 1;
194         }
195
196         /*  check wifi connection */
197         if (ad->type != TETHERING_TYPE_WIFI) {
198                 if (__is_connected_wifi_net(ad)) {
199                         DBG("wifi network is connected\n");
200                         return 1;
201                 }
202         }
203
204         /* Check SIM state */
205         if (_get_sim_state() != VCONFKEY_TELEPHONY_SIM_INSERTED) {
206                 if (ad->type == TETHERING_TYPE_WIFI)
207                         _prepare_popup(MH_POPUP_NO_SIM, STR_INSERT_SIM_TO_USE_TETH);
208                 else
209                         _prepare_popup(MH_POPUP_NO_SIM, STR_CONN_MOBILE_DATA_TO_USE_TETH);
210
211                 _create_popup(ad);
212                 return -1;
213         }
214
215         /* Check cellular state */
216         cellular_state = _get_cellular_state();
217         DBG("cellular state is :  %d \n", cellular_state);
218         if (cellular_state == CONNECTION_CELLULAR_STATE_FLIGHT_MODE) {
219                 _prepare_popup(MH_POPUP_FLIGHT_MODE, STR_DISABLE_FLIGHT_MODE_MSG);
220                 _create_popup(ad);
221                 ERR("Flight mode is ON\n");
222                 return -1;
223         } else {
224                 if (vconf_get_bool(VCONFKEY_3G_ENABLE , &dnet_state) < 0) {
225                         ERR("vconf_get_bool is failed\n");
226                 } else if (dnet_state == 0) {
227                         DBG("Data Network is not connected");
228                         _prepare_popup(MH_POPUP_MOBILE_DATA_OFF, STR_NO_NET_CONN_MSG);
229                         _create_popup(ad);
230                         return 0;
231                 }
232                 if (cellular_state != CONNECTION_CELLULAR_STATE_CONNECTED &&
233                         cellular_state != CONNECTION_CELLULAR_STATE_AVAILABLE) {
234                         if (ad->ps_recheck_timer_id > 0) {
235                                 g_source_remove(ad->ps_recheck_timer_id);
236                                 ad->ps_recheck_timer_id = 0;
237                                 if (ad->is_wifi_teth_enabling == true
238                                                 && ad->type != TETHERING_TYPE_WIFI)
239                                         _update_tethering_enabling_item(ad, TETHERING_TYPE_WIFI,
240                                                                                                         MH_STATE_NONE);
241                                 if (ad->is_bt_teth_enabling == true
242                                                 && ad->type != TETHERING_TYPE_BT)
243                                         _update_tethering_enabling_item(ad, TETHERING_TYPE_BT,
244                                                                                                         MH_STATE_NONE);
245                                 if (ad->is_usb_teth_enabling == true
246                                                 && ad->type != TETHERING_TYPE_USB)
247                                         _update_tethering_enabling_item(ad, TETHERING_TYPE_USB,
248                                                                                                         MH_STATE_NONE);
249                         }
250                         ad->ps_recheck_timer_id = g_timeout_add(PS_RECHECK_INTERVAL,
251                                                                                 _ps_recheck_timeout_cb, (void*)ad);
252                         return 0;
253                 }
254         }
255
256         DBG("-\n");
257         return 1;
258 }
259
260 int _create_wifi_hotspot_on_popup(mh_appdata_t *ad)
261 {
262         char *fmt = NULL;
263         char *str = NULL;
264         bool wifi_state = false;
265         int value = 0;
266
267         wifi_manager_is_activated(ad->wifi_handle, &wifi_state);
268         _set_vconf_prev_wifi_state(wifi_state);
269         value = _get_checkbox_status(TETHERING_TYPE_WIFI);
270         if (0 == value) {
271                 if (wifi_state == true || _is_wifi_direct_on() == true)
272                         fmt = STR_TETH_ON_DESC_1;
273                 else
274                         fmt = STR_TETH_ON_DESC_2;
275
276                 str = g_malloc0(MH_LABEL_LENGTH_MAX);
277                 if (str == NULL) {
278                         ERR("memory allocation is failed\n");
279                         return -1;
280                 }
281                 snprintf(str, MH_LABEL_LENGTH_MAX, fmt, TETHERING_WIFI_MAX_CONNECTED_STA);
282                 _wifi_tethering_checkbox_popup_status_set(true);
283                 _prepare_popup(MH_POPUP_WIFI_ON_CHECKBOX, str);
284                 g_free(str);
285                 _create_popup(ad);
286         } else {
287                 _prepare_popup_type(MH_POPUP_WIFI_ON_CHECKBOX);
288                 _teth_on(ad);
289         }
290         return 0;
291 }
292
293 int _create_bt_tethering_on_popup(mh_appdata_t *ad)
294 {
295         char *fmt = STR_TETH_ON_DESC_2;
296         char *str = NULL;
297         int value = 0;
298         value = _get_checkbox_status(TETHERING_TYPE_BT);
299         if (0 == value) {
300                 str = g_malloc0(MH_LABEL_LENGTH_MAX);
301                 if (str == NULL) {
302                         ERR("memory allocation is failed\n");
303                         return -1;
304                 }
305                 snprintf(str, MH_LABEL_LENGTH_MAX, fmt,
306                                 TETHERING_BT_MAX_CONNECTED_STA);
307                 _prepare_popup(MH_POPUP_BT_ON_CHECKBOX, str);
308                 g_free(str);
309                 _create_popup(ad);
310         } else {
311                 _prepare_popup_type(MH_POPUP_BT_ON_CHECKBOX);
312                 _teth_on(ad);
313         }
314         return 0;
315 }
316
317 int _create_usb_tethering_on_popup(mh_appdata_t *ad)
318 {
319         int value = 0;
320         char *str = NULL;
321         value = _get_checkbox_status(TETHERING_TYPE_USB);
322         DBG("%s : %d\n", VCONF_MOBILE_AP_USB_POPUP_CHECKBOX_STATUS, value);
323         if (0 == value) {
324                 str = g_malloc0(MH_LABEL_LENGTH_MAX);
325                 if (str == NULL) {
326                         ERR("memory allocation is failed\n");
327                         return -1;
328                 }
329                 snprintf(str, MH_LABEL_LENGTH_MAX, "%s",
330                                 STR_TETH_ON_DESC_3);
331                 _prepare_popup(MH_POPUP_USB_ON_CHECKBOX, str);
332                 g_free(str);
333                 _create_popup(ad);
334         } else {
335                 _prepare_popup_type(MH_POPUP_USB_ON_CHECKBOX);
336                 _teth_on(ad);
337         }
338         return 0;
339 }
340
341 static void __disable_tethering_by_ind(mh_appdata_t *ad, tethering_disabled_cause_e cause)
342 {
343         if (ad == NULL) {
344                 ERR("Param is NULL\n");
345                 return;
346         }
347
348         DBG("cause : %d\n", cause);
349         switch (cause) {
350         case TETHERING_DISABLED_BY_WIFI_ON:
351                 break;
352
353         case TETHERING_DISABLED_BY_BT_OFF:
354                 break;
355
356         case TETHERING_DISABLED_BY_USB_DISCONNECTION:
357                 if (ad->is_bt_teth_enabling && ad->main.bt_item) {
358                         _update_bt_item(ad, MH_STATE_NONE);
359                         ad->is_bt_teth_enabling = false;
360                 }
361                 if (ad->is_wifi_teth_enabling && ad->main.wifi_item) {
362                         _update_wifi_item(ad, MH_STATE_NONE);
363                         ad->is_wifi_teth_enabling = false;
364                 }
365                 break;
366
367         case TETHERING_DISABLED_BY_FLIGHT_MODE:
368                 break;
369
370         case TETHERING_DISABLED_BY_TIMEOUT:
371                 break;
372
373         case TETHERING_DISABLED_BY_OTHERS:
374                 if (ad->main.wifi_item && _get_vconf_prev_wifi_state() == true)
375                         elm_object_item_disabled_set(ad->main.wifi_item, EINA_TRUE);
376                 break;
377
378         case TETHERING_DISABLED_BY_LOW_BATTERY:
379                 break;
380
381         default:
382                 break;
383         }
384
385         return;
386 }
387
388 /* Tethering callbacks */
389 void _enabled_cb(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
390 {
391         DBG("+\n");
392         DBG("result : %d, type : %d, is_requested : %d\n", result, type, is_requested);
393
394         if (user_data == NULL) {
395                 ERR("user_data is NULL\n");
396                 return;
397         }
398
399         mh_appdata_t *ad = (mh_appdata_t *)user_data;
400
401         if (!is_requested) {
402                 if (ad->type == type && ad->popup) {
403                         DBG("This tethering type is already enabled\n");
404                         evas_object_del(ad->popup);
405                         ad->popup = NULL;
406                 }
407                 _update_main_view(ad, type);
408                 return;
409         }
410
411         _update_main_view(ad, type);
412
413         return;
414 }
415
416 void _disabled_cb(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
417 {
418         DBG("+\n");
419         DBG("result : %d, type : %d, cause : %d\n", result, type, cause);
420
421         if (user_data == NULL) {
422                 ERR("user_data is NULL\n");
423                 return;
424         }
425
426         mh_appdata_t *ad = (mh_appdata_t *)user_data;
427
428         if (cause != TETHERING_DISABLED_BY_REQUEST) {
429                 if (ad->type == type && ad->popup) {
430                         DBG("This tethering type is already disabled\n");
431                         evas_object_del(ad->popup);
432                         ad->popup = NULL;
433                 }
434                 __disable_tethering_by_ind(ad, cause);
435                 _update_main_view(ad, type);
436                 return;
437         }
438
439         if (result != TETHERING_ERROR_NONE) {
440                 _prepare_popup(MH_POPUP_TETH_ENABLING_FAILED, STR_UNABLE_TO_USE_TETH);
441                 _create_popup(ad);
442                 _update_main_view(ad, type);
443                 return;
444         }
445
446         if (ad->main.wifi_item && type == TETHERING_TYPE_WIFI && _get_vconf_prev_wifi_state() == true)
447                 elm_object_item_disabled_set(ad->main.wifi_item, EINA_TRUE);
448
449         _update_main_view(ad, type);
450
451         return;
452 }
453
454 void _connection_changed_cb(tethering_client_h client, bool is_opened, void *user_data)
455 {
456         DBG("+\n");
457
458         if (user_data == NULL) {
459                 ERR("user_data is NULL\n");
460                 return;
461         }
462
463         mh_appdata_t *ad = (mh_appdata_t *)user_data;
464         char *mac_addr = NULL;
465
466         if (is_opened) {
467                 _append_list_client_handle(ad, client);
468
469 #ifdef TETHERING_DATA_USAGE_SUPPORT
470                 if (ad->is_foreground && _get_list_clients_count(ad) == 1)
471                         _start_update_data_packet_usage(ad);
472 #endif
473         } else {
474                 tethering_client_get_mac_address(client, &mac_addr);
475                 if (mac_addr) {
476                         _delete_list_client_handle(ad, mac_addr);
477                         free(mac_addr);
478                 }
479 #ifdef TETHERING_DATA_USAGE_SUPPORT
480                 if (ad->is_foreground && _get_list_clients_count(ad) == 0)
481                         _stop_update_data_packet_usage(ad);
482 #endif
483         }
484
485         ap_update_data_device(ad);
486
487         return;
488 }
489
490 void _wifi_state_changed_cb(wifi_manager_device_state_e state, void *user_data)
491 {
492         if (user_data == NULL) {
493                 ERR("user_data is NULL\n");
494                 return;
495         }
496
497         DBG("+\n");
498
499         mh_appdata_t *ad = (mh_appdata_t *)user_data;
500         char *str = NULL;
501         char *fmt = NULL;
502         if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED) {
503                 if (ad->main.wifi_item && elm_object_item_disabled_get(ad->main.wifi_item))
504                         elm_object_item_disabled_set(ad->main.wifi_item, EINA_FALSE);
505                 _set_vconf_prev_wifi_state(false);
506         } else if (state == WIFI_MANAGER_DEVICE_STATE_DEACTIVATED) {
507                 _set_vconf_prev_wifi_state(true);
508         }
509
510         if (ad->type == TETHERING_TYPE_WIFI && ad->popup && ad->popup_checkbox &&
511                         _wifi_tethering_checkbox_popup_status_get()) {
512                 evas_object_del(ad->popup_checkbox);
513                 ad->popup_checkbox = NULL;
514                 evas_object_del(ad->popup);
515                 ad->popup = NULL;
516                 if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED)
517                         fmt = STR_TETH_ON_DESC_1;
518                 else
519                         fmt = STR_TETH_ON_DESC_2;
520
521                 str = g_malloc0(MH_LABEL_LENGTH_MAX);
522                 if (str == NULL) {
523                         ERR("memory allocation is failed\n");
524                         return;
525                 }
526                 snprintf(str, MH_LABEL_LENGTH_MAX, fmt, TETHERING_WIFI_MAX_CONNECTED_STA);
527                 _prepare_popup(MH_POPUP_WIFI_ON_CHECKBOX, str);
528                 g_free(str);
529                 _create_popup(ad);
530         }
531         DBG("-\n");
532 }
533
534 void _visibility_changed_cb(bool is_visible, void *user_data)
535 {
536         DBG("+\n");
537
538         if (user_data == NULL) {
539                 ERR("user_data is NULL\n");
540                 return;
541         }
542
543         mh_appdata_t *ad = (mh_appdata_t *)user_data;
544
545         if (ad->setup.visibility == is_visible)
546                 return;
547
548         ad->setup.visibility = is_visible;
549         ad->setup.visibility_new = is_visible;
550         if (ad->setup.hide_item)
551                 elm_genlist_item_update(ad->setup.hide_item);
552
553         DBG("-\n");
554
555         return;
556 }
557
558 void _security_type_changed_cb(tethering_wifi_security_type_e changed_type, void *user_data)
559 {
560         DBG("+\n");
561
562         if (user_data == NULL) {
563                 ERR("user_data is NULL\n");
564                 return;
565         }
566
567         mh_appdata_t *ad = (mh_appdata_t *)user_data;
568
569         if (ad->setup.security_type == changed_type)
570                 return;
571
572         ad->setup.security_type = changed_type;
573         ad->setup.security_type_new = changed_type;
574
575         if (ad->setup.security_item)
576                 elm_genlist_item_update(ad->setup.security_item);
577
578         DBG("-\n");
579
580         return;
581 }
582
583 void _passphrase_changed_cb(void *user_data)
584 {
585         DBG("+\n");
586
587         if (user_data == NULL) {
588                 ERR("user_data is NULL\n");
589                 return;
590         }
591
592         mh_appdata_t *ad = (mh_appdata_t *)user_data;
593         char *passphrase = NULL;
594         int ret;
595
596         ret = tethering_wifi_get_passphrase(ad->handle, &passphrase);
597         if (ret != TETHERING_ERROR_NONE) {
598                 ERR("tethering_wifi_get_passphrase failed ret = [0x%x]\n", ret);
599                 return;
600         }
601
602         if (!g_strcmp0(passphrase, ad->setup.wifi_passphrase))
603                 goto DONE;
604
605         g_strlcpy(ad->setup.wifi_passphrase, passphrase,
606                 sizeof(ad->setup.wifi_passphrase));
607
608         g_strlcpy(ad->setup.wifi_passphrase_new, passphrase,
609                 sizeof(ad->setup.wifi_passphrase_new));
610
611         if (ad->setup.pw_item)
612                 elm_genlist_item_update(ad->setup.pw_item);
613
614 DONE:
615         g_free(passphrase);
616         DBG("-\n");
617         return;
618 }
619
620 #ifdef TETHERING_DATA_USAGE_SUPPORT
621 void _data_usage_cb(tethering_error_e result, unsigned long long received_data, unsigned long long sent_data, void *user_data)
622 {
623         if (user_data == NULL) {
624                 ERR("user_data is NULL\n");
625                 return;
626         }
627
628         mh_appdata_t *ad = (mh_appdata_t *)user_data;
629
630         if (ad->data_statistics.pdp_total_sent != sent_data ||
631                         ad->data_statistics.pdp_total_receive != received_data) {
632                 ad->data_statistics.pdp_total_sent = sent_data;
633                 ad->data_statistics.pdp_total_receive = received_data;
634                 ap_update_data_packet_usage(ad);
635         }
636
637         ad->data_statistics.is_updated = true;
638
639         return;
640 }
641 #endif
642 /* End of Tethering callbacks */
643
644 int _handle_wifi_onoff_change(mh_appdata_t *ad)
645 {
646         __MOBILE_AP_FUNC_ENTER__;
647
648         int ret;
649         int connected_wifi_clients = 0;
650
651         /* Turn off WiFi hotspot */
652         if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI) {
653                 if (_get_no_of_connected_device(ad, &connected_wifi_clients,
654                                         TETHERING_TYPE_WIFI) == EINA_FALSE) {
655                         ERR("Getting the number of connected device is failed\n");
656                 }
657                 if (connected_wifi_clients > 0) {
658                         _prepare_popup(MH_POPUP_WIFI_OFF, STR_CLOSE_INTERNET_Q);
659                         _create_popup(ad);
660                 } else {
661 #ifndef TIZEN_FEATURE_EMULATOR
662                         ret = tethering_disable(ad->handle, TETHERING_TYPE_WIFI);
663                         if (ret != TETHERING_ERROR_NONE) {
664                                 ERR("wifi tethering off is failed : %d\n", ret);
665                                 return -1;
666                         }
667 #endif
668                 }
669                 return 0;
670         }
671
672         /* Turn on WiFi hotspot */
673         ret = __is_preconditions_handled(ad);
674         if (ret < 0)
675                 return -1;
676         else if (ret == 0)
677                 return 0;
678
679         if (_create_wifi_hotspot_on_popup(ad) < 0) {
680                 ERR("__create_wifi_hotspot_on_popup fail\n");
681                 return -1;
682         }
683
684         __MOBILE_AP_FUNC_EXIT__;
685
686         return 0;
687 }
688
689 int _handle_bt_onoff_change(mh_appdata_t *ad)
690 {
691         __MOBILE_AP_FUNC_ENTER__;
692
693         int ret;
694
695         /* Turn off Bluetooth tethering */
696         if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_BT) {
697 #ifndef TIZEN_FEATURE_EMULATOR
698                 ret = tethering_disable(ad->handle, TETHERING_TYPE_BT);
699                 if (ret) {
700                         ERR("Error disable bt tethering [%d]\n", ret);
701                         return -1;
702                 }
703 #endif
704                 return 0;
705         }
706
707         /* Turn on Bluetooth tethering */
708         ret = __is_preconditions_handled(ad);
709         if (ret < 0)
710                 return -1;
711         else if (ret == 0)
712                 return 0;
713
714         if (_create_bt_tethering_on_popup(ad) < 0) {
715                 ERR("_create_bt_tethering_on_popup fail\n");
716                 return -1;
717         }
718
719         __MOBILE_AP_FUNC_EXIT__;
720
721         return 0;
722 }
723
724 int _handle_usb_onoff_change(mh_appdata_t *ad)
725 {
726         __MOBILE_AP_FUNC_ENTER__;
727
728         int ret;
729
730         /* Turn off USB tethering */
731         if (ad->main.hotspot_mode & VCONFKEY_MOBILE_HOTSPOT_MODE_USB) {
732 #ifndef TIZEN_FEATURE_EMULATOR
733                 ret = tethering_disable(ad->handle, TETHERING_TYPE_USB);
734                 if (ret) {
735                         ERR("Error disable usb tethering : %d\n", ret);
736                         return -1;
737                 }
738 #endif
739                 return 0;
740         }
741
742         /* Turn on USB tethering */
743         ret = __is_preconditions_handled(ad);
744         if (ret < 0)
745                 return -1;
746         else if (ret == 0)
747                 return 0;
748
749         if (_create_usb_tethering_on_popup(ad) < 0) {
750                 ERR("_create_usb_tethering_on_popup fail\n");
751                 return -1;
752         }
753
754         __MOBILE_AP_FUNC_EXIT__;
755
756         return 0;
757 }
758
759 bool _is_wifi_direct_on(void)
760 {
761         int wifi_direct_state = 0;
762         int ret;
763
764         ret = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
765         if (ret < 0) {
766                 ERR("vconf_get_int() is failed : %d\n", ret);
767                 return false;
768         }
769
770         return wifi_direct_state != 0 ? true : false;
771 }