Update for tizen_2.1
[framework/connectivity/mobileap-agent.git] / src / mobileap_handler.c
1 /*
2  *  mobileap-agent
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 <glib.h>
21 #include <dbus/dbus.h>
22 #include <dbus/dbus-glib.h>
23 #include <dbus/dbus-glib-lowlevel.h>
24
25 #include "mobileap_agent.h"
26 #include "mobileap_common.h"
27 #include "mobileap_bluetooth.h"
28 #include "mobileap_wifi.h"
29 #include "mobileap_usb.h"
30 #include "mobileap_notification.h"
31
32 /* Need translation */
33 #define MH_NOTI_TIMEOUT_STR     "Tap for setting"
34 #define MH_NOTI_TIMEOUT_TITLE   "Disable tethering by timeout"
35
36 typedef struct {
37         guint src_id;
38         GSourceFunc func;
39         void *user_data;
40 } sp_timeout_handler_t;
41
42 static gboolean __wifi_timeout_cb(gpointer user_data);
43 static gboolean __bt_timeout_cb(gpointer user_data);
44
45 static sp_timeout_handler_t sp_timeout_handler[MOBILE_AP_TYPE_MAX] = {
46         {0, __wifi_timeout_cb, NULL},
47         {0, NULL, NULL},
48         {0, __bt_timeout_cb, NULL}};
49
50
51
52 static void __handle_flight_mode_changed_cb(keynode_t *key, void *data)
53 {
54         if (key == NULL) {
55                 ERR("Parameter is NULL\n");
56                 return;
57         }
58
59         TetheringObject *obj = (TetheringObject *)data;
60         int vconf_key = 0;
61
62         if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI)) {
63                 DBG("Wi-Fi tethering is not enabled\n");
64                 return;
65         }
66
67         if (vconf_keynode_get_type(key) != VCONF_TYPE_BOOL) {
68                 ERR("Invalid vconf key type\n");
69                 return;
70         }
71
72         vconf_key = vconf_keynode_get_bool(key);
73         DBG("key = %s, value = %d(bool)\n",
74                         vconf_keynode_get_name(key), vconf_key);
75
76         if (vconf_key == FALSE)
77                 return;
78
79         DBG("Flight mode\n");
80         _disable_wifi_tethering(obj);
81         _emit_mobileap_dbus_signal(obj, E_SIGNAL_FLIGHT_MODE, NULL);
82
83         return;
84 }
85
86 static void __handle_device_name_changed_cb(keynode_t *key, void *data)
87 {
88         if (key == NULL || data == NULL) {
89                 ERR("Parameter is NULL\n");
90                 return;
91         }
92
93         TetheringObject *obj = (TetheringObject *)data;
94         char *vconf_key = NULL;
95
96         if (!_mobileap_is_enabled(MOBILE_AP_STATE_WIFI)) {
97                 DBG("Wi-Fi hotspot is not enabled\n");
98                 return;
99         }
100
101         if (vconf_keynode_get_type(key) != VCONF_TYPE_STRING) {
102                 ERR("Invalid vconf key type\n");
103                 return;
104         }
105
106         vconf_key = vconf_keynode_get_str(key);
107         DBG("key = %s, value = %s(str)\n",
108                         vconf_keynode_get_name(key), vconf_key);
109
110         if (g_strcmp0(vconf_key, obj->ssid) == 0) {
111                 DBG("ssid is not changed\n");
112         } else {
113                 DBG("ssid is changed\n");
114                 _disable_wifi_tethering(obj);
115         }
116
117         return;
118 }
119
120 void _register_vconf_cb(void *user_data)
121 {
122         if (user_data == NULL) {
123                 ERR("Invalid param\n");
124                 return;
125         }
126
127         vconf_reg_t vconf_reg[] = {
128                 {VCONFKEY_TELEPHONY_FLIGHT_MODE,
129                         __handle_flight_mode_changed_cb, NULL},
130                 {VCONFKEY_SETAPPL_DEVICE_NAME_STR,
131                         __handle_device_name_changed_cb, NULL},
132                 {NULL, NULL, NULL}
133         };
134
135         int i = 0;
136         int ret = 0;
137
138         while (vconf_reg[i].key != NULL && vconf_reg[i].cb != NULL) {
139                 DBG("Register [%d] : %s\n", i, vconf_reg[i].key);
140                 ret = vconf_notify_key_changed(vconf_reg[i].key,
141                                         vconf_reg[i].cb, user_data);
142                 if (ret != 0) {
143                         ERR("vconf_notify_key_changed is failed : %d\n", ret);
144                 }
145
146                 if (vconf_reg[i].value) {
147                         ret = vconf_get_int(vconf_reg[i].key,
148                                         vconf_reg[i].value);
149                         if (ret != 0) {
150                                 ERR("vconf_get_int is failed : %d\n", ret);
151                         }
152                 }
153
154                 i++;
155         }
156
157         return;
158 }
159
160 void _unregister_vconf_cb(void *user_data)
161 {
162         if (user_data == NULL) {
163                 ERR("Invalid param\n");
164                 return;
165         }
166
167         vconf_reg_t vconf_reg[] = {
168                 {VCONFKEY_TELEPHONY_FLIGHT_MODE,
169                         __handle_flight_mode_changed_cb, NULL},
170                 {VCONFKEY_SETAPPL_DEVICE_NAME_STR,
171                         __handle_device_name_changed_cb, NULL},
172                 {NULL, NULL, NULL}
173         };
174
175         int i = 0;
176         int ret = 0;
177
178         while (vconf_reg[i].key != NULL && vconf_reg[i].cb != NULL) {
179                 DBG("Register [%d] : %s\n", i, vconf_reg[i].key);
180                 ret = vconf_ignore_key_changed(vconf_reg[i].key,
181                                 vconf_reg[i].cb);
182                 if (ret != 0) {
183                         ERR("vconf_notify_key_changed is failed : %d\n", ret);
184                 }
185
186                 i++;
187         }
188
189         return;
190 }
191
192 static gboolean __wifi_timeout_cb(gpointer data)
193 {
194         DBG("+\n");
195         if (data == NULL) {
196                 ERR("data is NULL\n");
197                 return FALSE;
198         }
199
200         TetheringObject *obj = (TetheringObject *)data;
201
202         if (_mobileap_is_enabled(MOBILE_AP_STATE_WIFI) == FALSE) {
203                 ERR("There is no conn. via Wi-Fi tethernig. But nothing to do\n");
204                 return FALSE;
205         }
206
207         _disable_wifi_tethering(obj);
208         _emit_mobileap_dbus_signal(obj,
209                         E_SIGNAL_WIFI_TETHER_OFF, SIGNAL_MSG_TIMEOUT);
210
211         _create_timeout_noti(MH_NOTI_TIMEOUT_STR, MH_NOTI_TIMEOUT_TITLE,
212                         MH_NOTI_ICON_PATH);
213
214         DBG("-\n");
215         return FALSE;
216 }
217
218 static gboolean __bt_timeout_cb(gpointer data)
219 {
220         DBG("+\n");
221         if (data == NULL) {
222                 ERR("data is NULL\n");
223                 return FALSE;
224         }
225
226         TetheringObject *obj = (TetheringObject *)data;
227
228         if (_mobileap_is_enabled(MOBILE_AP_STATE_BT) == FALSE) {
229                 ERR("There is no conn. via BT tethering. But nothing to do\n");
230                 return FALSE;
231         }
232
233         _disable_bt_tethering(obj);
234         _emit_mobileap_dbus_signal(obj,
235                         E_SIGNAL_BT_TETHER_OFF, SIGNAL_MSG_TIMEOUT);
236
237         _create_timeout_noti(MH_NOTI_TIMEOUT_STR, MH_NOTI_TIMEOUT_TITLE,
238                         MH_NOTI_ICON_PATH);
239         DBG("-\n");
240         return FALSE;
241 }
242
243 void _init_timeout_cb(mobile_ap_type_e type, void *user_data)
244 {
245         DBG("+\n");
246         if (sp_timeout_handler[type].func == NULL) {
247                 DBG("Not supported timeout : type[%d]\n", type);
248                 return;
249         }
250
251         if (user_data == NULL) {
252                 ERR("Invalid param\n");
253                 return;
254         }
255
256         if (sp_timeout_handler[type].src_id > 0) {
257                 DBG("There is already registered timeout source\n");
258                 g_source_remove(sp_timeout_handler[type].src_id);
259                 sp_timeout_handler[type].src_id = 0;
260         }
261
262         sp_timeout_handler[type].user_data = user_data;
263
264         DBG("-\n");
265         return;
266 }
267
268 void _start_timeout_cb(mobile_ap_type_e type)
269 {
270         DBG("+\n");
271         if (sp_timeout_handler[type].func == NULL) {
272                 DBG("Not supported timeout : type[%d]\n", type);
273                 return;
274         }
275
276         if (sp_timeout_handler[type].src_id > 0) {
277                 ERR("It is not registered or stopped\n");
278                 return;
279         }
280
281         sp_timeout_handler[type].src_id = g_timeout_add(TETHERING_CONN_TIMEOUT,
282                         sp_timeout_handler[type].func,
283                         sp_timeout_handler[type].user_data);
284
285         DBG("-\n");
286         return;
287 }
288
289 void _stop_timeout_cb(mobile_ap_type_e type)
290 {
291         DBG("+\n");
292         if (sp_timeout_handler[type].func == NULL) {
293                 DBG("Not supported timeout : type[%d]\n", type);
294                 return;
295         }
296
297         if (sp_timeout_handler[type].src_id == 0) {
298                 ERR("It is not started yet\n");
299                 return;
300         }
301
302         g_source_remove(sp_timeout_handler[type].src_id);
303         sp_timeout_handler[type].src_id = 0;
304
305         DBG("-\n");
306         return;
307 }
308
309 void _deinit_timeout_cb(mobile_ap_type_e type) {
310         DBG("+\n");
311         if (sp_timeout_handler[type].func == NULL) {
312                 DBG("Not supported timeout : type[%d]\n", type);
313                 return;
314         }
315
316         if (sp_timeout_handler[type].src_id > 0) {
317                 g_source_remove(sp_timeout_handler[type].src_id);
318                 sp_timeout_handler[type].src_id = 0;
319         }
320
321         sp_timeout_handler[type].user_data = NULL;
322
323         DBG("-\n");
324         return;
325 }
326