Tizen 2.1 base
[platform/core/connectivity/net-config.git] / src / utils / util.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 <unistd.h>
21 #include <string.h>
22 #include <sys/wait.h>
23 #include <sys/stat.h>
24 #include <vconf.h>
25 #include <vconf-keys.h>
26 #include <wifi-direct.h>
27 #include <syspopup_caller.h>
28 #include <aul.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "neterror.h"
33 #include "wifi-state.h"
34
35 #define WIFI_MAC_INFO_FILE      "/opt/etc/.mac.info"
36 #define WIFI_MAC_INFO_LENGTH    17
37
38 GKeyFile *netconfig_keyfile_load(const char *pathname)
39 {
40         GKeyFile *keyfile = NULL;
41         GError *error = NULL;
42
43         keyfile = g_key_file_new();
44         if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
45                 DBG("Unable to open %s, error %s", pathname, error->message);
46                 g_error_free(error);
47
48                 g_key_file_free(keyfile);
49                 keyfile = NULL;
50         }
51
52         return keyfile;
53 }
54
55 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
56 {
57         gsize size = 0;
58         GError *error = NULL;
59         gchar *keydata = NULL;
60         gchar *needle = NULL, *directory = NULL;
61
62         directory = g_strdup(pathname);
63         needle = g_strrstr(directory, "/");
64
65         if (needle != NULL)
66                 *needle = '\0';
67
68         if (directory == NULL || (*directory) == '\0') {
69                 g_free(directory);
70                 return;
71         }
72
73         if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
74                 if (g_mkdir_with_parents(directory,
75                                 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
76                         g_free(directory);
77                         return;
78                 }
79         }
80         g_free(directory);
81
82         keydata = g_key_file_to_data(keyfile, &size, &error);
83         if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
84                 DBG("Unable to save %s, error %s", pathname, error->message);
85                 g_error_free(error);
86         }
87
88         if (chmod(pathname, S_IRUSR | S_IWUSR) != 0)
89                 DBG("Unable to change permission of %s", pathname);
90
91         g_free(keydata);
92
93         g_key_file_free(keyfile);
94 }
95
96 void netconfig_start_timer_seconds(guint secs,
97                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
98 {
99         guint t_id = 0;
100
101         if (callback == NULL) {
102                 ERR("callback function is NULL");
103                 return;
104         }
105
106         if ((timer_id != NULL && *timer_id != 0)) {
107                 ERR("timer already is registered");
108                 return;
109         }
110
111         t_id = g_timeout_add_seconds(secs, callback, user_data);
112
113         if (t_id == 0) {
114                 ERR("Can't add timer");
115                 return;
116         }
117
118         if (timer_id != NULL)
119                 *timer_id = t_id;
120 }
121
122 void netconfig_start_timer(guint msecs,
123                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
124 {
125         guint t_id = 0;
126
127         INFO("Register timer with callback pointer (%p)", callback);
128
129         if (callback == NULL) {
130                 ERR("callback function is NULL");
131                 return;
132         }
133
134         if ((timer_id != NULL && *timer_id != 0)) {
135                 ERR("timer already is registered");
136                 return;
137         }
138
139         t_id = g_timeout_add(msecs, callback, user_data);
140
141         if (t_id == 0) {
142                 ERR("Can't add timer");
143                 return;
144         }
145
146         if (timer_id != NULL)
147                 *timer_id = t_id;
148 }
149
150 void netconfig_stop_timer(guint *timer_id)
151 {
152         if (timer_id == NULL) {
153                 ERR("timer is NULL");
154                 return;
155         }
156
157         if (*timer_id != 0) {
158                 g_source_remove(*timer_id);
159                 *timer_id = 0;
160         }
161 }
162
163 static gboolean __netconfig_test_device_picker()
164 {
165         char *favorite_wifi_service = NULL;
166
167         favorite_wifi_service = netconfig_wifi_get_favorite_service();
168         if (favorite_wifi_service != NULL) {
169                 g_free(favorite_wifi_service);
170                 return FALSE;
171         }
172
173         return TRUE;
174 }
175
176 static void __netconfig_pop_device_picker(void)
177 {
178         int rv = 0;
179         bundle *b = NULL;
180         int wifi_ug_state = 0;
181
182         vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
183         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
184                 return;
185
186         b = bundle_create();
187
188         DBG("Launch Wi-Fi device picker");
189         rv = syspopup_launch("wifi-qs", b);
190
191         bundle_free(b);
192 }
193
194 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
195 {
196         if (__netconfig_test_device_picker() == TRUE)
197                 __netconfig_pop_device_picker();
198
199         return FALSE;
200 }
201
202 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method,
203                 guint timer_id)
204 {
205         static guint netconfig_wifi_device_picker_service_timer = 0;
206
207         if (is_set_method != TRUE)
208                 return netconfig_wifi_device_picker_service_timer;
209
210         if (netconfig_wifi_device_picker_service_timer != timer_id)
211                 netconfig_wifi_device_picker_service_timer = timer_id;
212
213         return netconfig_wifi_device_picker_service_timer;
214 }
215
216 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
217 {
218         __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
219 }
220
221 static guint __netconfig_wifi_device_picker_get_timer_id(void)
222 {
223         return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
224 }
225
226 void netconfig_wifi_device_picker_service_start(void)
227 {
228         int wifi_ug_state;
229         const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
230         guint timer_id = 0;
231
232         vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
233         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
234                 return;
235
236         DBG("Register device picker timer with %d milliseconds",
237                         NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
238
239         netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL,
240                         __netconfig_wifi_try_device_picker, NULL, &timer_id);
241
242         __netconfig_wifi_device_picker_set_timer_id(timer_id);
243 }
244
245 void netconfig_wifi_device_picker_service_stop(void)
246 {
247         guint timer_id = 0;
248
249         timer_id = __netconfig_wifi_device_picker_get_timer_id();
250         if (timer_id == 0)
251                 return;
252
253         DBG("Clear device picker timer with timer_id %d", timer_id);
254
255         netconfig_stop_timer(&timer_id);
256
257         __netconfig_wifi_device_picker_set_timer_id(timer_id);
258 }
259
260 gboolean netconfig_is_wifi_direct_on(void)
261 {
262         int wifi_direct_state = 0;
263
264         vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
265
266         DBG("Wi-Fi direct mode %d", wifi_direct_state);
267         return (wifi_direct_state != 0) ? TRUE : FALSE;
268 }
269
270 gboolean netconfig_is_wifi_tethering_on(void)
271 {
272         int wifi_tethering_state = 0;
273
274         vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
275
276         DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
277         if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
278                 return TRUE;
279
280         return FALSE;
281 }
282
283 /* args[] and env[] should be terminated with NULL pointer */
284 gboolean netconfig_execute_file(const char *file_path,
285                 char *const args[], char *const env[])
286 {
287         pid_t pid = 0;
288         int rv = 0;
289         errno = 0;
290
291         if (!(pid = fork())) {
292                 register unsigned int index = 0;
293                 INFO("pid(%d), ppid (%d)", getpid(), getppid());
294                 INFO("Inside child, exec (%s) command", file_path);
295
296                 index = 0;
297                 while (args[index] != NULL) {
298                         INFO(" %s", args[index]);
299                         index++;
300                 }
301
302                 errno = 0;
303                 if (execve(file_path, args, env) == -1) {
304                         DBG("Fail to execute command...(%s)",
305                                         strerror(errno));
306                         return FALSE;
307                 }
308         } else if (pid > 0) {
309                 if (waitpid(pid, &rv, 0) == -1) {
310                         DBG("wait pid (%u) rv (%d)", pid, rv);
311
312                         if (WIFEXITED(rv)) {
313                                 DBG("exited, rv=%d", WEXITSTATUS(rv));
314                         } else if (WIFSIGNALED(rv)) {
315                                 DBG("killed by signal %d", WTERMSIG(rv));
316                         } else if (WIFSTOPPED(rv)) {
317                                 DBG("stopped by signal %d", WSTOPSIG(rv));
318                         } else if (WIFCONTINUED(rv)) {
319                                 DBG("continued");
320                         }
321                 }
322                 return TRUE;
323         }
324
325         DBG("failed to fork()...(%s)", strerror(errno));
326         return FALSE;
327 }
328
329 gboolean netconfig_iface_wifi_launch_direct(NetconfigWifi *wifi, GError **error)
330 {
331         gboolean ret = TRUE;
332
333         DBG("Launch Wi-Fi direct daemon");
334
335         const char *path = "/usr/bin/wifi-direct-server.sh";
336         char *const args[] = { "wifi-direct-server.sh", "start", NULL};
337         char *const env[] = { NULL };
338
339         ret = netconfig_execute_file(path, args, env);
340
341         if (ret != TRUE) {
342                 INFO("Failed to launch Wi-Fi direct daemon");
343
344                 netconfig_error_wifi_direct_failed(error);
345         }
346
347         return ret;
348 }
349
350 void netconfig_add_wifi_found_notification(void)
351 {
352         int ret;
353         bundle *b = bundle_create();
354
355         bundle_add(b, "_SYSPOPUP_TYPE_", "add_found_ap_noti");
356
357         ret = aul_launch_app("org.tizen.net-popup", b);
358
359         bundle_free(b);
360
361         if (ret >= 0)
362                 DBG("Successfully added notification");
363         else
364                 ERR("Unable to launch noti-popup. Err = %d", ret);
365 }
366
367 void netconfig_del_wifi_found_notification(void)
368 {
369         int ret;
370         bundle *b = bundle_create();
371
372         bundle_add(b, "_SYSPOPUP_TYPE_", "del_found_ap_noti");
373
374         ret = aul_launch_app("org.tizen.net-popup", b);
375
376         bundle_free(b);
377
378         if (ret >= 0)
379                 DBG("Successfully deleted notification");
380         else
381                 ERR("Unable to launch noti-popup. Err = %d", ret);
382 }
383
384
385 void netconfig_set_wifi_mac_address(void)
386 {
387         FILE *fp;
388         char buf[WIFI_MAC_INFO_LENGTH + 1];
389         char *mac_info;
390
391         mac_info = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
392         if (mac_info == NULL) {
393                 ERR("Failed to open vconf key %s", VCONFKEY_WIFI_BSSID_ADDRESS);
394                 return;
395         }
396
397         INFO("%s : %s", VCONFKEY_WIFI_BSSID_ADDRESS, mac_info);
398
399         fp = fopen(WIFI_MAC_INFO_FILE, "r");
400         if (fp == NULL) {
401                 ERR("Failed to open file %s", WIFI_MAC_INFO_FILE);
402                 g_free(mac_info);
403                 return;
404         }
405
406         if (fgets(buf, sizeof(buf), fp) == NULL) {
407                 ERR("Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
408                 goto done;
409         }
410
411         INFO("%s : %s", WIFI_MAC_INFO_FILE, buf);
412
413         if (strlen(buf) < WIFI_MAC_INFO_LENGTH) {
414                 ERR("Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
415                 goto done;
416         }
417
418         buf[WIFI_MAC_INFO_LENGTH] = '\0';
419
420         if (g_str_equal(mac_info, buf) == TRUE)
421                 goto done;
422
423         if (vconf_set_str(VCONFKEY_WIFI_BSSID_ADDRESS, buf) != 0)
424                 ERR("Failed to set MAC info to %s", VCONFKEY_WIFI_BSSID_ADDRESS);
425
426 done:
427         g_free(mac_info);
428         fclose(fp);
429 }