3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <dbus/dbus.h>
22 #include "mobileap_softap.h"
23 #include "mobileap_common.h"
24 #include "mobileap_bluetooth.h"
25 #include "mobileap_wifi.h"
26 #include "mobileap_usb.h"
27 #include "mobileap_notification.h"
28 #include "mobileap_handler.h"
30 #define VCONF_IS_DEVICE_RENAMED_IN_UG "file/private/libug-setting-mobileap-efl/is_device_rename_local"
37 } sp_timeout_handler_t;
39 static gboolean __wifi_timeout_cb(gpointer user_data);
40 static gboolean __bt_timeout_cb(gpointer user_data);
42 static sp_timeout_handler_t sp_timeout_handler[MOBILE_AP_TYPE_MAX] = {
43 {0, 0, __wifi_timeout_cb, NULL},
45 {0, 0, __bt_timeout_cb, NULL},
48 static void __handle_network_cellular_state_changed_cb(keynode_t *key, void *data)
51 ERR("Parameter is NULL\n");
55 Tethering *obj = (Tethering *)data;
58 if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI | MOBILE_AP_STATE_WIFI_AP)) {
62 if (vconf_keynode_get_type(key) != VCONF_TYPE_INT) {
63 ERR("Invalid vconf key type\n");
67 vconf_key = vconf_keynode_get_int(key);
68 SDBG("key = %s, value = %d(int)\n",
69 vconf_keynode_get_name(key), vconf_key);
71 if (vconf_key != VCONFKEY_NETWORK_CELLULAR_FLIGHT_MODE)
74 if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI))
75 _disable_wifi_tethering(obj);
76 else if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP))
77 _disable_wifi_ap(obj);
78 if (_mobileap_is_enabled(MOBILE_AP_STATE_USB))
79 _disable_usb_tethering(obj);
81 tethering_emit_flight_mode(obj);
86 static void __handle_device_name_changed_cb(keynode_t *key, void *data)
88 if (key == NULL || data == NULL) {
89 ERR("Parameter is NULL\n");
93 Tethering *obj = (Tethering *)data;
94 char *vconf_key = NULL;
95 softap_settings_t *new_settings = _get_softap_settings();
96 softap_security_type_e sec_type;
98 if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI | MOBILE_AP_STATE_WIFI_AP)) {
102 if (vconf_keynode_get_type(key) != VCONF_TYPE_STRING) {
103 ERR("Invalid vconf key type\n");
106 vconf_key = vconf_keynode_get_str(key);
108 if (g_strcmp0(vconf_key, new_settings->ssid) != 0) {
109 DBG("Device name is changed\n");
110 if (!g_strcmp0(new_settings->security_type, SOFTAP_SECURITY_TYPE_WPA2_PSK_STR)) {
111 sec_type = SOFTAP_SECURITY_TYPE_WPA2_PSK;
113 sec_type = SOFTAP_SECURITY_TYPE_OPEN;
115 if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI)) {
116 _reload_softap_settings(obj, vconf_key, new_settings->key,
117 new_settings->hide_mode, sec_type);
118 } else if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI_AP)) {
119 _reload_softap_settings_for_ap(obj, vconf_key, new_settings->key,
120 new_settings->hide_mode, sec_type);
126 static void __handle_language_changed_cb(keynode_t *key, void *data)
128 if (key == NULL || data == NULL) {
129 ERR("Parameter is NULL\n");
133 char *language = NULL;
135 if (vconf_keynode_get_type(key) != VCONF_TYPE_STRING) {
136 ERR("Invalid vconf key type\n");
140 language = vconf_get_str(VCONFKEY_LANGSET);
142 setenv("LANG", language, 1);
143 setenv("LC_MESSAGES", language, 1);
144 setlocale(LC_ALL, language);
151 gboolean _is_power_save_survival_mode(void)
156 ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &status);
158 ERR("vconf_get_int is failed : %d\n", ret);
162 DBG(" PS mode status : %d", status);
163 if (status == SETTING_PSMODE_SURVIVAL)
169 void _register_vconf_cb(void *user_data)
171 if (user_data == NULL) {
172 ERR("Invalid param\n");
176 vconf_reg_t vconf_reg[] = {
177 {VCONFKEY_NETWORK_CELLULAR_STATE,
178 __handle_network_cellular_state_changed_cb, NULL},
179 {VCONFKEY_SETAPPL_DEVICE_NAME_STR,
180 __handle_device_name_changed_cb, NULL},
182 __handle_language_changed_cb, NULL},
189 while (vconf_reg[i].key != NULL && vconf_reg[i].cb != NULL) {
190 ret = vconf_notify_key_changed(vconf_reg[i].key,
191 vconf_reg[i].cb, user_data);
193 ERR("vconf_notify_key_changed is failed : %d\n", ret);
196 if (vconf_reg[i].value) {
197 ret = vconf_get_int(vconf_reg[i].key,
200 ERR("vconf_get_int is failed : %d\n", ret);
210 void _unregister_vconf_cb(void)
212 vconf_reg_t vconf_reg[] = {
213 {VCONFKEY_NETWORK_CELLULAR_STATE,
214 __handle_network_cellular_state_changed_cb, NULL},
215 {VCONFKEY_SETAPPL_DEVICE_NAME_STR,
216 __handle_device_name_changed_cb, NULL},
218 __handle_language_changed_cb, NULL},
225 while (vconf_reg[i].key != NULL && vconf_reg[i].cb != NULL) {
226 ret = vconf_ignore_key_changed(vconf_reg[i].key,
229 ERR("vconf_notify_key_changed is failed : %d\n", ret);
238 static gboolean __wifi_timeout_cb(gpointer data)
242 ERR("data is NULL\n");
246 Tethering *obj = (Tethering *)data;
248 if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI) == FALSE) {
249 ERR("There is no conn. via Wi-Fi tethernig. But nothing to do\n");
253 _disable_wifi_tethering(obj);
254 tethering_emit_wifi_off(obj, SIGNAL_MSG_TIMEOUT);
255 //_launch_toast_popup(MOBILE_AP_TETHERING_TIMEOUT_TOAST_POPUP);
256 _create_timeout_noti(MH_NOTI_ICON_WIFI);
261 static gboolean __bt_timeout_cb(gpointer data)
265 ERR("data is NULL\n");
269 Tethering *obj = (Tethering *)data;
271 if (_mobileap_is_enabled(MOBILE_AP_STATE_BT) == FALSE) {
272 ERR("There is no conn. via BT tethering. But nothing to do\n");
276 _disable_bt_tethering(obj);
277 tethering_emit_bluetooth_off(obj, SIGNAL_MSG_TIMEOUT);
278 //_launch_toast_popup(MOBILE_AP_TETHERING_TIMEOUT_TOAST_POPUP);
279 _create_timeout_noti(MH_NOTI_ICON_BT);
284 static sp_timeout_handler_t *__find_next_timeout(void)
286 sp_timeout_handler_t *next_timeout = &sp_timeout_handler[MOBILE_AP_TYPE_WIFI];
289 for (i = MOBILE_AP_TYPE_USB; i < MOBILE_AP_TYPE_MAX; i++) {
290 if (sp_timeout_handler[i].end_time == 0)
293 if (sp_timeout_handler[i].end_time < next_timeout->end_time ||
294 next_timeout->end_time == 0)
295 next_timeout = &sp_timeout_handler[i];
301 static void __expire_timeout(sp_timeout_handler_t *sp)
303 if (sp->alarm_id > 0) {
304 alarmmgr_remove_alarm(sp->alarm_id);
311 sp->func(sp->user_data);
314 static void __reset_timeout(sp_timeout_handler_t *sp)
316 if (sp->alarm_id > 0) {
317 alarmmgr_remove_alarm(sp->alarm_id);
324 int _sp_timeout_handler(alarm_id_t alarm_id, void *user_param)
332 sp_timeout_handler_t *next_timeout;
335 for (i = MOBILE_AP_TYPE_WIFI; i < MOBILE_AP_TYPE_MAX; i++) {
336 if (sp_timeout_handler[i].end_time == 0)
339 if (sp_timeout_handler[i].alarm_id == alarm_id) {
340 sp_timeout_handler[i].alarm_id = 0;
341 __expire_timeout(&sp_timeout_handler[i]);
345 interval = (time_t)difftime(sp_timeout_handler[i].end_time, now);
349 __expire_timeout(&sp_timeout_handler[i]);
352 next_timeout = __find_next_timeout();
353 if (next_timeout->end_time == 0)
356 interval = (time_t)difftime(next_timeout->end_time, now);
357 ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, interval, 0, NULL,
358 &next_timeout->alarm_id);
359 if (ret != ALARMMGR_RESULT_SUCCESS) {
360 ERR("alarmmgr_add_alarm is failed. end_time : %d\n",
361 next_timeout->end_time);
369 void _init_timeout_cb(mobile_ap_type_e type, void *user_data)
373 if (sp_timeout_handler[type].func == NULL) {
377 if (user_data == NULL) {
378 ERR("Invalid param\n");
382 sp_timeout_handler[type].alarm_id = 0;
383 sp_timeout_handler[type].end_time = 0;
384 sp_timeout_handler[type].user_data = user_data;
390 void _start_timeout_cb(mobile_ap_type_e type, time_t end_time)
395 sp_timeout_handler_t *next_timeout;
397 if (sp_timeout_handler[type].func == NULL) {
401 __reset_timeout(&sp_timeout_handler[type]);
402 sp_timeout_handler[type].end_time = end_time;
404 next_timeout = __find_next_timeout();
405 if (next_timeout->alarm_id > 0) {
409 for (i = MOBILE_AP_TYPE_WIFI; i < MOBILE_AP_TYPE_MAX; i++) {
410 if (sp_timeout_handler[i].alarm_id == 0)
413 __reset_timeout(&sp_timeout_handler[i]);
416 interval = (time_t)difftime(next_timeout->end_time, time(NULL));
418 __expire_timeout(next_timeout);
422 ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, interval, 0, NULL,
423 &next_timeout->alarm_id);
424 if (ret != ALARMMGR_RESULT_SUCCESS) {
425 ERR("alarmmgr_add_alarm is failed. type : %d, end_time : %d\n",
433 void _stop_timeout_cb(mobile_ap_type_e type)
440 sp_timeout_handler_t *next_timeout;
442 if (sp_timeout_handler[type].func == NULL) {
446 if (sp_timeout_handler[type].alarm_id == 0) {
447 sp_timeout_handler[type].end_time = 0;
451 for (i = MOBILE_AP_TYPE_WIFI; i < MOBILE_AP_TYPE_MAX; i++) {
452 if (sp_timeout_handler[i].end_time != sp_timeout_handler[type].end_time ||
456 sp_timeout_handler[i].alarm_id = sp_timeout_handler[type].alarm_id;
457 sp_timeout_handler[type].alarm_id = 0;
458 sp_timeout_handler[type].end_time = 0;
461 __reset_timeout(&sp_timeout_handler[type]);
463 next_timeout = __find_next_timeout();
464 if (next_timeout->end_time == 0)
467 interval = (time_t)difftime(next_timeout->end_time, time(NULL));
469 __expire_timeout(next_timeout);
473 ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, interval, 0, NULL,
474 &next_timeout->alarm_id);
475 if (ret != ALARMMGR_RESULT_SUCCESS) {
476 ERR("alarmmgr_add_alarm is failed. type : %d, end_time : %d\n",
477 type, next_timeout->end_time);
484 void _deinit_timeout_cb(mobile_ap_type_e type) {
487 if (sp_timeout_handler[type].func == NULL) {
491 if (sp_timeout_handler[type].alarm_id > 0) {
492 _stop_timeout_cb(type);
495 sp_timeout_handler[type].user_data = NULL;
496 sp_timeout_handler[type].end_time = 0;