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