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>
20 #include <dbus/dbus-glib.h>
21 #include <dbus/dbus-glib-lowlevel.h>
23 #include "mobileap_agent.h"
24 #include "mobileap_common.h"
25 #include "mobileap_bluetooth.h"
26 #include "mobileap_wifi.h"
27 #include "mobileap_usb.h"
28 #include "mobileap_notification.h"
30 /* Need translation */
31 #define MH_NOTI_TIMEOUT_STR "Tap for setting"
32 #define MH_NOTI_TIMEOUT_TITLE "Disable tethering by timeout"
38 } sp_timeout_handler_t;
40 static gboolean __wifi_timeout_cb(gpointer user_data);
41 static gboolean __bt_timeout_cb(gpointer user_data);
43 static sp_timeout_handler_t sp_timeout_handler[MOBILE_AP_TYPE_MAX] = {
44 {0, __wifi_timeout_cb, NULL},
46 {0, __bt_timeout_cb, NULL}};
48 static void __handle_flight_mode_changed_cb(keynode_t *key, void *data)
51 ERR("Parameter is NULL\n");
55 MobileAPObject *obj = (MobileAPObject *)data;
58 if (_mobileap_is_disabled()) {
59 DBG("Tethering is not enabled\n");
63 if (vconf_keynode_get_type(key) != VCONF_TYPE_BOOL) {
64 ERR("Invalid vconf key type\n");
68 vconf_key = vconf_keynode_get_bool(key);
69 DBG("key = %s, value = %d(bool)\n",
70 vconf_keynode_get_name(key), vconf_key);
72 if (vconf_key == FALSE) {
73 DBG("Flight mode is turned off\n");
78 _disable_wifi_tethering(obj);
79 _emit_mobileap_dbus_signal(obj, E_SIGNAL_FLIGHT_MODE, NULL);
85 static void __handle_device_name_changed_cb(keynode_t *key, void *data)
87 if (key == NULL || data == NULL) {
88 ERR("Parameter is NULL\n");
92 MobileAPObject *obj = (MobileAPObject *)data;
93 char *vconf_key = NULL;
95 if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI)) {
96 DBG("Wi-Fi hotspot is not enabled\n");
100 if (vconf_keynode_get_type(key) != VCONF_TYPE_STRING) {
101 ERR("Invalid vconf key type\n");
105 vconf_key = vconf_keynode_get_str(key);
106 DBG("key = %s, value = %s(str)\n",
107 vconf_keynode_get_name(key), vconf_key);
109 if (g_strcmp0(vconf_key, obj->ssid) == 0) {
110 DBG("ssid is not changed\n");
112 DBG("ssid is changed\n");
113 _disable_wifi_tethering(obj);
122 void _register_vconf_cb(void *user_data)
124 if (user_data == NULL) {
125 ERR("Invalid param\n");
129 vconf_reg_t vconf_reg[] = {
130 {VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL,
131 __handle_flight_mode_changed_cb, NULL},
132 {VCONFKEY_SETAPPL_DEVICE_NAME_STR,
133 __handle_device_name_changed_cb, NULL},
140 while (vconf_reg[i].key != NULL && vconf_reg[i].cb != NULL) {
141 DBG("Register [%d] : %s\n", i, vconf_reg[i].key);
142 ret = vconf_notify_key_changed(vconf_reg[i].key,
143 vconf_reg[i].cb, user_data);
145 ERR("vconf_notify_key_changed is failed : %d\n", ret);
148 if (vconf_reg[i].value) {
149 ret = vconf_get_int(vconf_reg[i].key,
152 ERR("vconf_get_int is failed : %d\n", ret);
162 void _unregister_vconf_cb(void *user_data)
164 if (user_data == NULL) {
165 ERR("Invalid param\n");
169 vconf_reg_t vconf_reg[] = {
170 {VCONFKEY_SETAPPL_FLIGHT_MODE_BOOL,
171 __handle_flight_mode_changed_cb, NULL},
172 {VCONFKEY_SETAPPL_DEVICE_NAME_STR,
173 __handle_device_name_changed_cb, NULL},
180 while (vconf_reg[i].key != NULL && vconf_reg[i].cb != NULL) {
181 DBG("Register [%d] : %s\n", i, vconf_reg[i].key);
182 ret = vconf_ignore_key_changed(vconf_reg[i].key,
185 ERR("vconf_notify_key_changed is failed : %d\n", ret);
194 static gboolean __wifi_timeout_cb(gpointer data)
198 ERR("data is NULL\n");
202 MobileAPObject *obj = (MobileAPObject *)data;
204 if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI) == FALSE) {
205 ERR("There is no conn. via Wi-Fi tethernig. But nothing to do\n");
209 _disable_wifi_tethering(obj);
210 _emit_mobileap_dbus_signal(obj,
211 E_SIGNAL_WIFI_TETHER_OFF, SIGNAL_MSG_TIMEOUT);
213 _create_timeout_noti(MH_NOTI_TIMEOUT_STR, MH_NOTI_TIMEOUT_TITLE,
220 static gboolean __bt_timeout_cb(gpointer data)
224 ERR("data is NULL\n");
228 MobileAPObject *obj = (MobileAPObject *)data;
230 if (_mobileap_is_enabled(MOBILE_AP_STATE_BT) == FALSE) {
231 ERR("There is no conn. via BT tethering. But nothing to do\n");
235 _disable_bt_tethering(obj);
236 _emit_mobileap_dbus_signal(obj,
237 E_SIGNAL_BT_TETHER_OFF, SIGNAL_MSG_TIMEOUT);
239 _create_timeout_noti(MH_NOTI_TIMEOUT_STR, MH_NOTI_TIMEOUT_TITLE,
245 void _init_timeout_cb(mobile_ap_type_e type, void *user_data)
248 if (sp_timeout_handler[type].func == NULL) {
249 DBG("Not supported timeout : type[%d]\n", type);
253 if (user_data == NULL) {
254 ERR("Invalid param\n");
258 if (sp_timeout_handler[type].src_id > 0) {
259 DBG("There is already registered timeout source\n");
260 g_source_remove(sp_timeout_handler[type].src_id);
261 sp_timeout_handler[type].src_id = 0;
264 sp_timeout_handler[type].user_data = user_data;
270 void _start_timeout_cb(mobile_ap_type_e type)
273 if (sp_timeout_handler[type].func == NULL) {
274 DBG("Not supported timeout : type[%d]\n", type);
278 if (sp_timeout_handler[type].src_id > 0) {
279 ERR("It is not registered or stopped\n");
283 sp_timeout_handler[type].src_id = g_timeout_add(TETHERING_CONN_TIMEOUT,
284 sp_timeout_handler[type].func,
285 sp_timeout_handler[type].user_data);
291 void _stop_timeout_cb(mobile_ap_type_e type)
294 if (sp_timeout_handler[type].func == NULL) {
295 DBG("Not supported timeout : type[%d]\n", type);
299 if (sp_timeout_handler[type].src_id == 0) {
300 ERR("It is not started yet\n");
304 g_source_remove(sp_timeout_handler[type].src_id);
305 sp_timeout_handler[type].src_id = 0;
311 void _deinit_timeout_cb(mobile_ap_type_e type) {
313 if (sp_timeout_handler[type].func == NULL) {
314 DBG("Not supported timeout : type[%d]\n", type);
318 if (sp_timeout_handler[type].src_id > 0) {
319 g_source_remove(sp_timeout_handler[type].src_id);
320 sp_timeout_handler[type].src_id = 0;
323 sp_timeout_handler[type].user_data = NULL;