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