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