Remove the sim feature and deprecated vconf key
[apps/native/ug-mobile-ap.git] / src / mh_func_onoff.c
1 /*
2 * ug-mobile-ap
3 *
4 * Copyright 2012-2013  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://floralicense.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 <wifi-direct.h>
22
23 #include "mh_func_onoff.h"
24
25 static bool __get_vconf_prev_wifi_state()
26 {
27         int value = 0;
28
29         if (vconf_get_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, &value) < 0) {
30                 ERR("vconf_get_bool is failed\n");
31                 return false;
32         }
33
34         DBG("%s : %d\n", VCONF_MOBILE_AP_PREV_WIFI_STATUS, value);
35
36         return value ? true : false;
37 }
38
39 int _get_vconf_usb_state()
40 {
41         int value = VCONFKEY_SYSMAN_USB_DISCONNECTED;
42
43         if (vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &value) < 0) {
44                 ERR("vconf_get_int is failed\n");
45                 return 0;
46         }
47
48         DBG("%s : %d\n", VCONFKEY_SYSMAN_USB_STATUS, value);
49
50         return value;
51 }
52
53 static bool __is_connected_wifi_net(mh_appdata_t *ad)
54 {
55         connection_wifi_state_e wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;
56         int ret;
57
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");
61                 return false;
62         }
63
64         if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED) {
65                 ERR("Wi-Fi network is not connected : %d\n", wifi_state);
66                 return false;
67         }
68
69         DBG("Wi-Fi network is connected\n");
70         return true;
71 }
72
73 static bool __is_connected_ethernet_net(mh_appdata_t *ad)
74 {
75         connection_ethernet_state_e ethernet_state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
76         int ret;
77
78         ret = connection_get_ethernet_state(ad->conn_handle, &ethernet_state);
79         if (ret != CONNECTION_ERROR_NONE) {
80                 ERR("connection_get_ethernet_state() is failed : %d\n");
81                 return false;
82         }
83
84         if (ethernet_state != CONNECTION_ETHERNET_STATE_CONNECTED) {
85                 ERR("Ethernet network is not connected : %d\n", ethernet_state);
86                 return false;
87         }
88
89         DBG("Ethernet network is connected\n");
90         return true;
91 }
92
93 static bool __is_connected_cellular_net(mh_appdata_t *ad)
94 {
95         connection_cellular_state_e cellular_state = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
96         int ret;
97
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");
101                 return false;
102         }
103
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"));
107                 _create_popup(ad);
108                 ERR("Cellular network is not connected\n");
109                 return false;
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"));
114                 _create_popup(ad);
115                 ERR("Cellular network is not connected : %d\n", cellular_state);
116                 return false;
117         }
118
119         DBG("Cellular network is connected\n");
120         return true;
121 }
122
123 static int __create_wifi_hotspot_on_popup(mh_appdata_t *ad)
124 {
125         char *fmt;
126         char *str;
127         bool wifi_state = false;
128
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");
132         else
133                 fmt = _("IDS_ST_BODY_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE_THE_MAXIMUM_NUMBER_OF_TETHERED_DEVICES_ALLOWED_IS_PD");
134
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);
138         free(str);
139
140         _create_popup(ad);
141
142         return 0;
143 }
144
145 static int __create_bt_tethering_on_popup(mh_appdata_t *ad)
146 {
147         char *str;
148
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);
154         free(str);
155
156         _create_popup(ad);
157
158         return 0;
159 }
160
161 static int __create_usb_tethering_on_popup(mh_appdata_t *ad)
162 {
163         char *str;
164
165         str = _("IDS_MOBILEAP_POP_TETHERING_CONSUMES_MORE_BATTERY_POWER_AND_INCREASES_YOUR_DATA_USAGE");
166         _prepare_popup(MH_POP_USB_ON_CONF, str);
167         _create_popup(ad);
168
169         return 0;
170 }
171
172 static void __disable_tethering_by_ind(mh_appdata_t *ad, tethering_disabled_cause_e cause)
173 {
174         if (ad == NULL) {
175                 ERR("Param is NULL\n");
176                 return;
177         }
178
179         DBG("cause : %d\n", cause);
180         switch (cause) {
181         case TETHERING_DISABLED_BY_WIFI_ON:
182                 DBG("TETHERING_DISABLED_IND by WIFI\n");
183                 break;
184
185         case TETHERING_DISABLED_BY_BT_OFF:
186                 DBG("TETHERING_DISABLED_BY_BT_DEACTIVATION\n");
187                 break;
188
189         case TETHERING_DISABLED_BY_USB_DISCONNECTION:
190                 DBG("TETHERING_DISABLED_IND by USB DISCONNECT\n");
191                 break;
192
193         case TETHERING_DISABLED_BY_FLIGHT_MODE:
194                 DBG("TETHERING_DISABLED_IND by FLIGHT_MODE\n");
195                 break;
196
197         case TETHERING_DISABLED_BY_TIMEOUT:
198                 DBG("TETHERING_DISABLED_BY_TIMEOUT\n");
199                 break;
200
201         case TETHERING_DISABLED_BY_OTHERS:
202                 DBG("TETHERING_DISABLED_IND by OTHERS\n");
203                 break;
204
205         case TETHERING_DISABLED_BY_LOW_BATTERY:
206                 DBG("TETHERING_DISABLED_IND by LOW_BATTERY\n");
207                 break;
208
209         default:
210                 DBG("TETHERING_DISABLED_IND Default\n");
211                 break;
212         }
213
214         return;
215 }
216
217 /* Wi-Fi Direct callback */
218 static void _wifi_direct_state_cb(int error_code, wifi_direct_device_state_e state, void *user_data)
219 {
220         DBG("+\n");
221
222         if (user_data == NULL) {
223                 ERR("The param is NULL\n");
224                 return;
225         }
226
227         mh_appdata_t *ad = (mh_appdata_t *)user_data;
228         int ret = 0;
229
230         wifi_direct_unset_device_state_changed_cb();
231         wifi_direct_deinitialize();
232         DBG("-\n");
233
234         if (error_code != 0) {
235                 ERR("wifi_direct_deactivate fail in cb : %d\n", error_code);
236                 _update_main_view(ad);
237                 return;
238         }
239
240         if (state != WIFI_DIRECT_DEVICE_STATE_DEACTIVATED) {
241                 ERR("Unknown state : %d\n", state);
242                 return;
243         }
244
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);
249                 return;
250         }
251
252         DBG("-\n");
253         return;
254 }
255
256 /* Wi-Fi callbacks */
257 static void __wifi_activated_cb(wifi_error_e result, void *user_data)
258 {
259         __MOBILE_AP_FUNC_ENTER__;
260
261         if (user_data == NULL) {
262                 ERR("The param is NULL\n");
263                 return;
264         }
265
266         mh_appdata_t *ad = (mh_appdata_t *)user_data;
267         int ret = 0;
268
269         if (result != WIFI_ERROR_NONE) {
270                 ERR("__wifi_activated_cb error : %d\n", result);
271                 _update_main_view(ad);
272                 return;
273         }
274
275         DBG("Wi-Fi on is done\n");
276
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);
280                 return;
281         }
282         _update_main_view(ad);
283
284         __MOBILE_AP_FUNC_EXIT__;
285         return;
286 }
287
288 static void __wifi_deactivated_cb(wifi_error_e result, void *user_data)
289 {
290         __MOBILE_AP_FUNC_ENTER__;
291
292         if (user_data == NULL) {
293                 ERR("The param is NULL\n");
294                 return;
295         }
296
297         mh_appdata_t *ad = (mh_appdata_t *)user_data;
298         int ret;
299
300         if (result != WIFI_ERROR_NONE) {
301                 ERR("__wifi_deactivated_cb error : %d\n", result);
302                 _update_main_view(ad);
303                 return;
304         }
305
306         DBG("Wi-Fi is turned off\n");
307
308         ret = vconf_set_bool(VCONF_MOBILE_AP_PREV_WIFI_STATUS, 1);
309         if (ret < 0)
310                 ERR("vconf_set_bool() is failed : %d\n", ret);
311
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);
316                 return;
317         }
318
319         __MOBILE_AP_FUNC_EXIT__;
320         return;
321 }
322
323 /* Tethering callbacks */
324 void _enabled_cb(tethering_error_e result, tethering_type_e type, bool is_requested, void *user_data)
325 {
326         if (user_data == NULL) {
327                 ERR("user_data is NULL\n");
328                 return;
329         }
330
331         mh_appdata_t *ad = (mh_appdata_t *)user_data;
332
333         ad->main.need_recover_wifi_tethering = false;
334
335         if (!is_requested) {
336                 if (NULL != ad->popup) {
337                         evas_object_del(ad->popup);
338                         ad->popup = NULL;
339                 }
340                 _update_main_view(ad);
341
342                 return;
343         }
344
345         if (result != TETHERING_ERROR_NONE) {
346                 _prepare_popup(MH_POP_INFORMATION,
347                                 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING"));
348                 _create_popup(ad);
349         }
350
351         _update_main_view(ad);
352
353         return;
354 }
355
356 void _disabled_cb(tethering_error_e result, tethering_type_e type, tethering_disabled_cause_e cause, void *user_data)
357 {
358         if (user_data == NULL) {
359                 ERR("user_data is NULL\n");
360                 return;
361         }
362
363         if (result == TETHERING_ERROR_NONE) {
364                 int ret = vconf_set_int(
365                                 VCONFKEY_MOBILE_HOTSPOT_WIFI_STATE,
366                                 VCONFKEY_MOBILE_HOTSPOT_WIFI_OFF);
367                 if (ret < 0)
368                         ERR("vconf_set_int() is failed : %d\n", ret);
369         }
370
371         mh_appdata_t *ad = (mh_appdata_t *)user_data;
372
373         if (ad->main.need_recover_wifi_tethering == true) {
374                 DBG("Wi-Fi tethering will be turned on again\n");
375                 return;
376         }
377
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);
382                         ad->popup = NULL;
383                 }
384                 _update_main_view(ad);
385                 __disable_tethering_by_ind(ad, cause);
386                 return;
387         }
388
389         if (result != TETHERING_ERROR_NONE) {
390                 _prepare_popup(MH_POP_INFORMATION,
391                                 _("IDS_MOBILEAP_POP_UNABLE_TO_USE_TETHERING"));
392                 _create_popup(ad);
393                 _update_main_view(ad);
394                 return;
395         }
396
397         DBG("Tethering [%d] is disabled by reqeust\n", type);
398
399         _update_main_view(ad);
400
401         return;
402 }
403
404 void _connection_changed_cb(tethering_client_h client, bool is_opened, void *user_data)
405 {
406         if (user_data == NULL) {
407                 ERR("user_data is NULL\n");
408                 return;
409         }
410
411         mh_appdata_t *ad = (mh_appdata_t *)user_data;
412         char *name = NULL;
413
414         tethering_client_get_name(client, &name);
415         DBG("Client %s is %s\n", name, is_opened ?  "connected" : "disconnected");
416         if (name)
417                 free(name);
418
419         ap_update_data_device(ad);
420
421         return;
422 }
423
424 void _data_usage_cb(tethering_error_e result, unsigned long long received_data, unsigned long long sent_data, void *user_data)
425 {
426         if (user_data == NULL) {
427                 ERR("user_data is NULL\n");
428                 return;
429         }
430
431         mh_appdata_t *ad = (mh_appdata_t *)user_data;
432
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);
438         }
439
440         ad->data_statistics.is_updated = true;
441
442         return;
443 }
444 /* End of Tethering callbacks */
445
446 int _handle_wifi_onoff_change(mh_appdata_t *ad)
447 {
448         __MOBILE_AP_FUNC_ENTER__;
449
450         int ret;
451         int connected_wifi_clients = 0;
452
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");
458                 }
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"));
462                         _create_popup(ad);
463                 } else {
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);
467                                 return -1;
468                         }
469                 }
470                 return 0;
471         }
472
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");
476                 return -1;
477         }
478
479         if (__create_wifi_hotspot_on_popup(ad) < 0) {
480                 ERR("__create_wifi_hotspot_on_popup fail\n");
481                 return -1;
482         }
483
484         __MOBILE_AP_FUNC_EXIT__;
485
486         return 0;
487 }
488
489 int _handle_bt_onoff_change(mh_appdata_t *ad)
490 {
491         __MOBILE_AP_FUNC_ENTER__;
492
493         int ret;
494
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);
498                 if (ret) {
499                         ERR("Error disable bt tethering [%d]\n", ret);
500                         return -1;
501                 }
502                 return 0;
503         }
504
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");
509                 return -1;
510         }
511
512         if (__create_bt_tethering_on_popup(ad) < 0) {
513                 ERR("__create_bt_tethering_on_popup fail\n");
514                 return -1;
515         }
516
517         __MOBILE_AP_FUNC_EXIT__;
518
519         return 0;
520 }
521
522 int _handle_usb_onoff_change(mh_appdata_t *ad)
523 {
524         __MOBILE_AP_FUNC_ENTER__;
525
526         int ret;
527
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);
531                 if (ret) {
532                         DBG("Error disable usb tethering : %d\n", ret);
533                         return -1;
534                 }
535                 return 0;
536         }
537
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");
542                 return -1;
543         }
544
545         if (__create_usb_tethering_on_popup(ad) < 0) {
546                 ERR("__create_usb_tethering_on_popup fail\n");
547                 return -1;
548         }
549
550         __MOBILE_AP_FUNC_EXIT__;
551
552         return 0;
553 }
554
555 int _turn_off_wifi(mh_appdata_t *ad)
556 {
557         int ret;
558
559         ret = wifi_deactivate(__wifi_deactivated_cb, (void *)ad);
560         if (ret != WIFI_ERROR_NONE) {
561                 ERR("wifi_deactivate() is failed : %d\n", ret);
562                 return -1;
563         }
564
565         return 0;
566 }
567
568 int _turn_on_wifi(mh_appdata_t *ad)
569 {
570         int ret;
571
572         ret = wifi_activate(__wifi_activated_cb, (void *)ad);
573         if (ret != WIFI_ERROR_NONE) {
574                 ERR("wifi_activate() is failed : %d\n", ret);
575                 return -1;
576         }
577
578         return 0;
579 }
580
581 bool _is_wifi_direct_on(void)
582 {
583         int wifi_direct_state = 0;
584         int ret;
585
586         ret = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
587         if (ret < 0) {
588                 ERR("vconf_get_int() is failed : %d\n", ret);
589                 return false;
590         }
591
592         return wifi_direct_state != 0 ? true : false;
593 }
594
595 int _turn_off_wifi_direct(mh_appdata_t *ad)
596 {
597         int ret;
598
599         ret = wifi_direct_initialize();
600         if (ret < 0) {
601                 ERR("wifi_direct_initialize() is failed : %d\n", ret);
602                 return -1;
603         }
604
605         ret = wifi_direct_set_device_state_changed_cb(_wifi_direct_state_cb, (void *)ad);
606         if (ret < 0) {
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);
610                 return -1;
611         }
612
613         ret = wifi_direct_deactivate();
614         if (ret < 0) {
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);
620                 return -1;
621         }
622
623         return 0;
624 }