Get interface name from fi.w1.wpa_supplicant1.Interface
[platform/core/connectivity/net-config.git] / src / utils / util.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 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 <dlfcn.h>
21 #include <errno.h>
22 #include <vconf.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <net/if.h>
28 #include <net/route.h>
29 #include <arpa/inet.h>
30 #include <sys/wait.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <linux/limits.h>
34 #include <ctype.h>
35 #include <vconf-keys.h>
36 #include <tzplatform_config.h>
37 #include <system_info.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <dirent.h>
41 #include <time.h>
42
43 #include "log.h"
44 #include "util.h"
45 #include "neterror.h"
46 #include "wifi-state.h"
47 #include "netdbus.h"
48
49 #define DBUS_SERVICE_DBUS               "org.freedesktop.DBus"
50 #define DBUS_INTERFACE_DBUS             "org.freedesktop.DBus"
51 #define MAC_INFO_FILEPATH               tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
52 #define MAC_ADDRESS_FILEPATH    "/sys/class/net/wlan0/address"
53 #define HEADED_PLUGIN_FILEPATH          "/usr/lib/net-config-plugin-headed.so"
54 #define TELEPHONY_PLUGIN_FILEPATH       "/usr/lib/net-config-plugin-telephony.so"
55 #define STC_PLUGIN_FILEPATH             "/usr/lib/net-config-plugin-stc.so"
56 #define BATTERY_PLUGIN_FILEPATH     "/usr/lib/net-config-plugin-battery.so"
57 #define CONNMAN_MAINFILE                        "/etc/connman/main.conf"
58 #define CONNMAN_WIFI_DEF_IFNAME         "DefaultWifiInterface"
59
60 static gboolean netconfig_device_picker_test = FALSE;
61 typedef struct {
62         char *conn_name;
63         int conn_id;
64 } dnssd_conn_destroy_data;
65
66 static gboolean netconfig_plugin_headed_enabled = FALSE;
67 static gboolean netconfig_plugin_telephony_enabled = FALSE;
68 static gboolean netconfig_plugin_stc_enabled = FALSE;
69 static gboolean netconfig_plugin_battery_enabled = FALSE;
70 static void *handle_headed;
71 static void *handle_telephony;
72 static void *handle_stc;
73 static void *handle_battery;
74 static struct netconfig_headed_plugin_t *headed_plugin;
75 static struct netconfig_telephony_plugin_t *telephony_plugin;
76 static struct netconfig_stc_plugin_t *stc_plugin;
77 static struct netconfig_battery_plugin_t *battery_plugin;
78
79 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
80 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
81
82 gboolean netconfig_check_mac_address(const char *service, const char *mac_address)
83 {
84         int service_index = 0;
85         int mac_index = 0;
86         const char *org = NULL;
87         const char *dst = NULL;
88         int i = 0;
89
90         if (g_str_has_prefix(service, "wifi_") == FALSE)
91                 return FALSE;
92
93         service_index = strlen("wifi_");
94         for (i = 0; i < 6; i++) {
95                 org = &service[service_index];
96                 dst = &mac_address[mac_index];
97
98                 if (g_ascii_strncasecmp(org, dst, 2) != 0)
99                         return FALSE;
100
101                 service_index += 2;
102                 mac_index += 3;
103         }
104
105         return TRUE;
106 }
107
108 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
109 {
110         gsize length;
111
112         if (!passphrase)
113                 return FALSE;
114
115         length = strlen(passphrase);
116
117         if (g_str_has_suffix(service, "psk") == TRUE) {
118                 if (length == 64) {
119                         for (int i = 0; i < 64; i++)
120                                 if (!isxdigit((unsigned char)passphrase[i]))
121                                         return FALSE;
122                 } else if (length < 8 || length > 63)
123                         return FALSE;
124         } else if (g_str_has_suffix(service, "wep") == TRUE) {
125                 if (length == 10 || length == 26) {
126                         for (int i = 0; i < length; i++)
127                                 if (!isxdigit((unsigned char)passphrase[i]))
128                                         return FALSE;
129                 } else if (length != 5 && length != 13)
130                         return FALSE;
131         }
132
133         return TRUE;
134 }
135
136 GKeyFile *netconfig_keyfile_load(const char *pathname)
137 {
138         GKeyFile *keyfile = NULL;
139         GError *error = NULL;
140
141         keyfile = g_key_file_new();
142         if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
143                 DBG("Unable to open %s, error %s", pathname, error->message);
144                 g_error_free(error);
145
146                 g_key_file_free(keyfile);
147                 keyfile = NULL;
148         }
149
150         DBG("loaded keyfile %s", pathname);
151         return keyfile;
152 }
153
154 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
155 {
156         gsize size = 0;
157         GError *error = NULL;
158         gchar *keydata = NULL;
159         gchar *needle = NULL, *directory = NULL;
160
161         directory = g_strdup(pathname);
162         if (directory == NULL) {
163                 ERR("directory is NULL");
164                 return;
165         }
166
167         needle = g_strrstr(directory, "/");
168         if (needle != NULL)
169                 *needle = '\0';
170
171         if ((*directory) == '\0') {
172                 g_free(directory);
173                 ERR("directory is NULL");
174                 return;
175         }
176
177         if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
178                 if (g_mkdir_with_parents(directory,
179                                 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
180                         g_free(directory);
181                         ERR("failed to make directory");
182                         return;
183                 }
184         }
185         g_free(directory);
186
187         keydata = g_key_file_to_data(keyfile, &size, &error);
188         if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
189                 ERR("Unable to save %s, error %s", pathname, error->message);
190                 g_error_free(error);
191         }
192
193         if (chmod(pathname, S_IRUSR | S_IWUSR) < 0)
194                 DBG("Failed to change mode");
195         else
196                 DBG("Successfully saved keyfile %s", pathname);
197
198         g_free(keydata);
199 }
200
201 void netconfig_start_timer_seconds(guint secs,
202                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
203 {
204         guint t_id = 0;
205
206         if (callback == NULL) {
207                 ERR("callback function is NULL");
208                 return;
209         }
210
211         if ((timer_id != NULL && *timer_id != 0)) {
212                 ERR("timer already is registered");
213                 return;
214         }
215
216         t_id = g_timeout_add_seconds(secs, callback, user_data);
217
218         if (t_id == 0) {
219                 ERR("Can't add timer");
220                 return;
221         }
222
223         if (timer_id != NULL)
224                 *timer_id = t_id;
225 }
226
227 void netconfig_start_timer(guint msecs,
228                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
229 {
230         guint t_id = 0;
231
232         INFO("Register timer with callback pointer (%p)", callback);
233
234         if (callback == NULL) {
235                 ERR("callback function is NULL");
236                 return;
237         }
238
239         if ((timer_id != NULL && *timer_id != 0)) {
240                 ERR("timer already is registered");
241                 return;
242         }
243
244         t_id = g_timeout_add(msecs, callback, user_data);
245
246         if (t_id == 0) {
247                 ERR("Can't add timer");
248                 return;
249         }
250
251         if (timer_id != NULL)
252                 *timer_id = t_id;
253 }
254
255 void netconfig_stop_timer(guint *timer_id)
256 {
257         if (timer_id == NULL) {
258                 ERR("timer is NULL");
259                 return;
260         }
261
262         if (*timer_id != 0) {
263                 g_source_remove(*timer_id);
264                 *timer_id = 0;
265         }
266 }
267
268 static gboolean __netconfig_test_device_picker()
269 {
270         char *favorite_wifi_service = NULL;
271
272         favorite_wifi_service = wifi_get_favorite_service();
273         if (favorite_wifi_service != NULL) {
274                 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
275                 g_free(favorite_wifi_service);
276                 return FALSE;
277         }
278
279         return TRUE;
280 }
281
282 static void __netconfig_pop_device_picker(void)
283 {
284         if (!netconfig_plugin_headed_enabled)
285                 return;
286
287         if (!headed_plugin)
288                 return;
289
290         headed_plugin->pop_device_picker();
291 }
292
293 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
294 {
295         if (__netconfig_test_device_picker() == TRUE)
296                 __netconfig_pop_device_picker();
297
298         return FALSE;
299 }
300
301 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
302 {
303         static guint netconfig_wifi_device_picker_service_timer = 0;
304
305         if (is_set_method != TRUE)
306                 return netconfig_wifi_device_picker_service_timer;
307
308         if (netconfig_wifi_device_picker_service_timer != timer_id)
309                 netconfig_wifi_device_picker_service_timer = timer_id;
310
311         return netconfig_wifi_device_picker_service_timer;
312 }
313
314 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
315 {
316         __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
317 }
318
319 static guint __netconfig_wifi_device_picker_get_timer_id(void)
320 {
321         return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
322 }
323
324 void netconfig_wifi_enable_device_picker_test(void)
325 {
326         netconfig_device_picker_test = TRUE;
327 }
328
329 void netconfig_wifi_device_picker_service_start(void)
330 {
331         const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
332         guint timer_id = 0;
333
334         if (netconfig_device_picker_test == TRUE)
335                 netconfig_device_picker_test = FALSE;
336         else
337                 return;
338
339         int wifi_ug_state;
340
341         netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
342         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
343                 return;
344
345         DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
346         netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
347
348         __netconfig_wifi_device_picker_set_timer_id(timer_id);
349 }
350
351 void netconfig_wifi_device_picker_service_stop(void)
352 {
353         guint timer_id = 0;
354
355         timer_id = __netconfig_wifi_device_picker_get_timer_id();
356         if (timer_id == 0)
357                 return;
358
359         DBG("Clear device picker timer with timer_id %d", timer_id);
360
361         netconfig_stop_timer(&timer_id);
362
363         __netconfig_wifi_device_picker_set_timer_id(timer_id);
364 }
365
366 gboolean netconfig_is_wifi_direct_on(void)
367 {
368         if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
369                 return FALSE;
370
371         int wifi_direct_state = 0;
372
373         netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
374
375         DBG("Wi-Fi direct mode %d", wifi_direct_state);
376         return (wifi_direct_state != 0) ? TRUE : FALSE;
377 }
378
379 gboolean netconfig_is_wifi_tethering_on(void)
380 {
381         if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
382                 int wifi_tethering_state = 0;
383
384                 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
385                 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
386                 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
387                                 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
388                         DBG("Mobile AP is on");
389                         return TRUE;
390                 }
391         }
392
393         DBG("Mobile AP is off");
394         return FALSE;
395 }
396
397 gboolean netconfig_interface_up(const char *ifname)
398 {
399         int fd;
400         struct ifreq ifr;
401
402         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
403         if (fd < 0) {
404                 ERR("socket failed %d", errno);
405                 return FALSE;
406         }
407
408         memset(&ifr, 0, sizeof(ifr));
409         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
410
411         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
412                 ERR("Fail to get IFFLAGS %d", errno);
413                 close(fd);
414                 return FALSE;
415         }
416
417         DBG("IFFLAGS: %x", ifr.ifr_flags);
418         ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
419         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
420                 ERR("Fail to set IFFLAGS %d", errno);
421                 close(fd);
422                 return FALSE;
423         }
424
425         close(fd);
426
427         DBG("Successfully activated wireless interface %s", ifname);
428         return TRUE;
429 }
430
431 gboolean netconfig_interface_down(const char *ifname)
432 {
433         int fd;
434         struct ifreq ifr;
435
436         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
437         if (fd < 0) {
438                 ERR("socket failed %d", errno);
439                 return FALSE;
440         }
441
442         memset(&ifr, 0, sizeof(ifr));
443         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
444
445         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
446                 ERR("Fail to get IFFLAGS %d", errno);
447                 close(fd);
448                 return FALSE;
449         }
450
451         DBG("IFFLAGS: %x", ifr.ifr_flags);
452         ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
453         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
454                 ERR("Fail to set IFFLAGS %d", errno);
455                 close(fd);
456                 return FALSE;
457         }
458
459         close(fd);
460
461         DBG("Successfully de-activated wireless interface %s", ifname);
462         return TRUE;
463 }
464
465 int netconfig_execute_file(const char *file_path,
466                 char *const args[], char *const envs[])
467 {
468         pid_t pid = 0;
469         int status = 0;
470         int rv = 0;
471         errno = 0;
472         register unsigned int index = 0;
473         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
474
475         while (args[index] != NULL) {
476                 DBG("%s", args[index]);
477                 index++;
478         }
479
480         if (!(pid = fork())) {
481                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
482                 DBG("Inside child, exec (%s) command", file_path);
483
484                 errno = 0;
485                 if (execve(file_path, args, envs) == -1) {
486                         DBG("Fail to execute command (%s)",
487                                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
488                         exit(1);
489                 }
490         } else if (pid > 0) {
491                 if (waitpid(pid, &status, 0) == -1)
492                         DBG("wait pid (%u) status (%d)", pid, status);
493
494                 if (WIFEXITED(status)) {
495                         rv = WEXITSTATUS(status);
496                         DBG("exited, status=%d", rv);
497                 } else if (WIFSIGNALED(status)) {
498                         DBG("killed by signal %d", WTERMSIG(status));
499                 } else if (WIFSTOPPED(status)) {
500                         DBG("stopped by signal %d", WSTOPSIG(status));
501                 } else if (WIFCONTINUED(status)) {
502                         DBG("continued");
503                 }
504
505                 return rv;
506         }
507
508         DBG("failed to fork(%s)",
509                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
510         return -EIO;
511 }
512
513 int netconfig_execute_cmd(const char *cmd)
514 {
515         if (cmd == NULL)
516                 return -EIO;
517
518         pid_t pid = 0;
519         int status = 0;
520         int rv = 0;
521         errno = 0;
522         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
523         gchar **args = NULL;
524
525         DBG("command: %s", cmd);
526
527         args = g_strsplit_set(cmd, " ", -1);
528
529         if (!(pid = fork())) {
530                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
531
532                 errno = 0;
533                 if (execv(args[0], args) == -1) {
534                         DBG("Fail to execute command (%s)",
535                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
536                         g_strfreev(args);
537                         exit(1);
538                 }
539         } else if (pid > 0) {
540                 if (waitpid(pid, &status, 0) == -1)
541                         DBG("wait pid (%u) status (%d)", pid, status);
542
543                 if (WIFEXITED(status)) {
544                         rv = WEXITSTATUS(status);
545                         DBG("exited, status=%d", rv);
546                 } else if (WIFSIGNALED(status)) {
547                         DBG("killed by signal %d", WTERMSIG(status));
548                 } else if (WIFSTOPPED(status)) {
549                         DBG("stopped by signal %d", WSTOPSIG(status));
550                 } else if (WIFCONTINUED(status)) {
551                         DBG("continued");
552                 }
553
554                 g_strfreev(args);
555                 return rv;
556         }
557
558         DBG("failed to fork(%s)",
559                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
560         g_strfreev(args);
561
562         return -EIO;
563 }
564
565 static void on_clat_handler()
566 {
567         pid_t clat_pid = 0;
568         int state = 0;
569
570         clat_pid = waitpid(-1, &state, WNOHANG);
571
572         DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
573 }
574
575 int netconfig_execute_clatd(const char *file_path, char *const args[])
576 {
577         pid_t pid = 0;
578         int rv = 0;
579         errno = 0;
580         register unsigned int index = 0;
581         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
582
583         struct sigaction act;
584         int state = 0;
585
586         act.sa_handler = on_clat_handler;
587         sigemptyset(&act.sa_mask);
588         act.sa_flags = 0;
589
590         state = sigaction(SIGCHLD, &act, 0);
591         if (state != 0) {
592                 DBG("sigaction() : %d", state);
593                 return -1;
594         }
595
596         while (args[index] != NULL) {
597                 DBG("%s", args[index]);
598                 index++;
599         }
600
601         if (!(pid = fork())) {
602                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
603                 DBG("Inside child, exec (%s) command", file_path);
604
605                 errno = 0;
606                 if (execvp(file_path, args) == -1) {
607                         ERR("Fail to execute command (%s)",
608                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
609                         return -1;
610                 }
611         } else if (pid > 0) {
612                 ERR("Success to launch clatd");
613                 return rv;
614         }
615
616         DBG("failed to fork(%s)",
617                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
618         return -EIO;
619 }
620
621 static void no_wait_signal_handler()
622 {
623         pid_t child_pid = 0;
624         int state = 0;
625
626         child_pid = waitpid(-1, &state, WNOHANG);
627
628         DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
629 }
630
631 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
632 {
633         pid_t pid = 0;
634         int rv = 0;
635         errno = 0;
636         register unsigned int index = 0;
637
638         struct sigaction act;
639         int state = 0;
640         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
641
642         act.sa_handler = no_wait_signal_handler;
643         sigemptyset(&act.sa_mask);
644         act.sa_flags = 0;
645
646         state = sigaction(SIGCHLD, &act, 0);
647         if (state != 0) {
648                 DBG("sigaction() : %d", state);
649                 return -1;
650         }
651
652         while (args[index] != NULL) {
653                 DBG("%s", args[index]);
654                 index++;
655         }
656
657         if (!(pid = fork())) {
658                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
659                 DBG("Inside child, exec (%s) command", file_path);
660
661                 errno = 0;
662                 if (execvp(file_path, args) == -1) {
663                         ERR("Fail to execute command (%s)",
664                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
665                         return -1;
666                 }
667         } else if (pid > 0) {
668                 ERR("Successfully launched child process");
669                 return rv;
670         }
671
672         DBG("failed to fork(%s)",
673                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
674         return -EIO;
675 }
676
677 int __netconfig_get_interface_index(const char *interface_name)
678 {
679         struct ifreq ifr;
680         int sock = 0;
681         int result = 0;
682         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
683
684         if (interface_name == NULL) {
685                 DBG("Inteface name is NULL");
686                 return -1;
687         }
688
689         errno = 0;
690         sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
691         if (sock < 0) {
692                 DBG("Failed to create socket : %s",
693                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
694                 return -1;
695         }
696
697         memset(&ifr, 0, sizeof(ifr));
698         strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
699         result = ioctl(sock, SIOCGIFINDEX, &ifr);
700         close(sock);
701
702         if (result < 0) {
703                 DBG("Failed to get ifr index: %s",
704                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
705                 return -1;
706         }
707
708         return ifr.ifr_ifindex;
709 }
710
711 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
712 {
713         struct ifreq ifr;
714         struct rtentry rt;
715         struct sockaddr_in addr_in;
716         int sock;
717         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
718
719         memset(&ifr, 0, sizeof(ifr));
720
721         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
722
723         if (ifr.ifr_ifindex < 0)
724                 return -1;
725
726         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
727
728         memset(&rt, 0, sizeof(rt));
729
730         rt.rt_flags = RTF_UP | RTF_HOST;
731         memset(&addr_in, 0, sizeof(struct sockaddr_in));
732         addr_in.sin_family = address_family;
733         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
734         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
735
736         memset(&addr_in, 0, sizeof(struct sockaddr_in));
737         addr_in.sin_family = address_family;
738         addr_in.sin_addr.s_addr = INADDR_ANY;
739         memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
740
741         memset(&addr_in, 0, sizeof(struct sockaddr_in));
742         addr_in.sin_family = AF_INET;
743         addr_in.sin_addr.s_addr = inet_addr(subnet);
744         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
745
746         rt.rt_dev = ifr.ifr_name;
747
748         errno = 0;
749         sock = socket(PF_INET, SOCK_DGRAM, 0);
750
751         if (sock < 0) {
752                 DBG("Failed to create socket : %s",
753                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
754                 return -1;
755         }
756
757         if (ioctl(sock, SIOCADDRT, &rt) < 0) {
758                 DBG("Failed to set route address : %s",
759                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
760                 close(sock);
761                 return -1;
762         }
763
764         close(sock);
765
766         return 1;
767 }
768
769 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
770 {
771         struct ifreq ifr;
772         struct rtentry rt;
773         struct sockaddr_in addr_in;
774         int sock;
775         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
776
777         memset(&ifr, 0, sizeof(ifr));
778         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
779
780         if (ifr.ifr_ifindex < 0)
781                 return -1;
782
783         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
784
785         memset(&rt, 0, sizeof(rt));
786
787         rt.rt_flags = RTF_UP;
788         memset(&addr_in, 0, sizeof(struct sockaddr_in));
789         addr_in.sin_family = address_family;
790         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
791         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
792
793         memset(&addr_in, 0, sizeof(struct sockaddr_in));
794         addr_in.sin_family = address_family;
795         addr_in.sin_addr.s_addr = inet_addr(subnet);
796         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
797         rt.rt_dev = ifr.ifr_name;
798
799         errno = 0;
800         sock = socket(PF_INET, SOCK_DGRAM, 0);
801
802         if (sock < 0) {
803                 DBG("Failed to create socket : %s",
804                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
805                 return -1;
806         }
807
808         if (ioctl(sock, SIOCDELRT, &rt) < 0) {
809                 DBG("Failed to set route address : %s",
810                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
811                 close(sock);
812                 return -1;
813         }
814
815         close(sock);
816
817         return 1;
818 }
819
820 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
821 {
822         struct in6_rtmsg rt;
823         int fd = 0;
824         int err = 0;
825         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
826
827         memset(&rt, 0, sizeof(rt));
828
829         rt.rtmsg_dst_len = prefix_len;
830
831         rt.rtmsg_flags = RTF_UP | RTF_HOST;
832
833         errno = 0;
834         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
835                 DBG("inet_pton failed : %s",
836                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
837                 return -1;
838         }
839
840         if (gateway != NULL) {
841                 rt.rtmsg_flags |= RTF_GATEWAY;
842                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
843                         DBG("inet_pton failed : %s",
844                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
845                         return -1;
846                 }
847         }
848
849         rt.rtmsg_metric = 1;
850
851         fd = socket(AF_INET6, SOCK_DGRAM, 0);
852         if (fd < 0) {
853                 DBG("Failed to create socket : %s",
854                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
855                 return -1;
856         }
857
858         rt.rtmsg_ifindex = 0;
859
860         if (interface) {
861                 struct ifreq ifr;
862                 memset(&ifr, 0, sizeof(ifr));
863                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
864                 ioctl(fd, SIOCGIFINDEX, &ifr);
865                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
866         }
867
868         if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
869                 DBG("Failed to add route: %s",
870                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
871                 close(fd);
872                 return -1;
873         }
874
875         close(fd);
876
877         return 1;
878 }
879
880 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
881 {
882         struct in6_rtmsg rt;
883         int fd = 0;
884         int err = 0;
885
886         memset(&rt, 0, sizeof(rt));
887
888         rt.rtmsg_dst_len = prefix_len;
889
890         rt.rtmsg_flags = RTF_UP | RTF_HOST;
891
892         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
893                 err = -errno;
894                 return err;
895         }
896
897         if (gateway != NULL) {
898                 rt.rtmsg_flags |= RTF_GATEWAY;
899                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
900                         err = -errno;
901                         return err;
902                 }
903         }
904
905         rt.rtmsg_metric = 1;
906
907         fd = socket(AF_INET6, SOCK_DGRAM, 0);
908         if (fd < 0)
909                 return -1;
910
911         rt.rtmsg_ifindex = 0;
912
913         if (interface) {
914                 struct ifreq ifr;
915                 memset(&ifr, 0, sizeof(ifr));
916                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
917                 ioctl(fd, SIOCGIFINDEX, &ifr);
918                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
919         }
920
921         if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
922                 DBG("Failed to del route: %d\n", err);
923                 close(fd);
924                 return -1;
925         }
926
927         close(fd);
928
929         return 1;
930 }
931
932 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
933 {
934         if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
935                 wifi_complete_launch_direct(wifi, context);
936                 return TRUE;
937         }
938
939         int ret = 0;
940         DBG("Launch Wi-Fi direct daemon");
941
942         const char *path = "/usr/bin/wifi-direct-server.sh";
943         char *const args[] = { "wifi-direct-server.sh", "start", NULL };
944         char *const envs[] = { NULL };
945
946         ret = netconfig_execute_file(path, args, envs);
947         if (ret < 0) {
948                 ERR("Failed to launch Wi-Fi direct daemon");
949                 netconfig_error_wifi_direct_failed(context);
950                 return TRUE;
951         }
952
953         wifi_complete_launch_direct(wifi, context);
954         return TRUE;
955 }
956
957 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
958 {
959         if (!netconfig_plugin_headed_enabled)
960                 return FALSE;
961
962         if (!headed_plugin)
963                 return FALSE;
964
965         return headed_plugin->send_notification_to_net_popup(noti, ssid);
966 }
967
968 int netconfig_send_message_to_net_popup(const char *title,
969                 const char *content, const char *type, const char *ssid)
970 {
971         if (!netconfig_plugin_headed_enabled)
972                 return 0;
973
974         if (!headed_plugin)
975                 return 0;
976
977         return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
978 }
979
980 int netconfig_send_restriction_to_net_popup(const char *title,
981                 const char *type, const char *restriction)
982 {
983         if (!netconfig_plugin_headed_enabled)
984                 return 0;
985
986         if (!headed_plugin)
987                 return 0;
988
989         return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
990 }
991
992 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
993 {
994         if (!netconfig_plugin_headed_enabled)
995                 return;
996
997         if (!headed_plugin)
998                 return;
999
1000         headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1001 }
1002
1003 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1004 {
1005         if (!netconfig_plugin_headed_enabled)
1006                 return;
1007
1008         if (!headed_plugin)
1009                 return;
1010
1011         headed_plugin->pop_wifi_connected_poppup(ssid);
1012 }
1013
1014 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1015 {
1016         if (!netconfig_plugin_telephony_enabled)
1017                 return;
1018
1019         if (!telephony_plugin)
1020                 return;
1021
1022         telephony_plugin->get_telephony_network_type(svctype, pstype);
1023 }
1024
1025 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1026 {
1027         if (!netconfig_plugin_telephony_enabled)
1028                 return FALSE;
1029
1030         if (!telephony_plugin)
1031                 return FALSE;
1032
1033         return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1034 }
1035
1036 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1037                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1038 {
1039         if (!netconfig_plugin_telephony_enabled)
1040                 return NETCONFIG_ERROR_INTERNAL;
1041
1042         if (!telephony_plugin)
1043                 return NETCONFIG_ERROR_INTERNAL;
1044
1045         return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1046 }
1047
1048 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1049                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1050 {
1051         if (!netconfig_plugin_telephony_enabled)
1052                 return FALSE;
1053
1054         if (!telephony_plugin)
1055                 return FALSE;
1056
1057         return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1058 }
1059
1060 gboolean netconfig_tapi_check_sim_state(void)
1061 {
1062         if (!netconfig_plugin_telephony_enabled)
1063                 return FALSE;
1064
1065         if (!telephony_plugin)
1066                 return FALSE;
1067
1068         return telephony_plugin->tapi_check_sim_state();
1069 }
1070
1071 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1072                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1073 {
1074         if (!netconfig_plugin_telephony_enabled)
1075                 return FALSE;
1076
1077         if (!telephony_plugin)
1078                 return FALSE;
1079
1080         return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1081 }
1082
1083 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1084                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1085 {
1086         if (!netconfig_plugin_telephony_enabled)
1087                 return FALSE;
1088
1089         if (!telephony_plugin)
1090                 return FALSE;
1091
1092         return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1093 }
1094
1095 static void __netconfig_stc_get_dn_stats(time_t from, time_t to, GSList **list)
1096 {
1097         if (!netconfig_plugin_stc_enabled)
1098                 return;
1099
1100         if (!stc_plugin)
1101                 return;
1102
1103         return stc_plugin->get_stc_dn_stats(from, to, list);
1104 }
1105
1106 static void __netconfig_stc_get_wifi_stats(time_t from, time_t to, GSList **list)
1107 {
1108         if (!netconfig_plugin_stc_enabled)
1109                 return;
1110
1111         if (!stc_plugin)
1112                 return;
1113
1114         return stc_plugin->get_stc_wifi_stats(from, to, list);
1115 }
1116
1117 void netconfig_battery_start_dn(void)
1118 {
1119         if (!netconfig_plugin_battery_enabled)
1120                 return;
1121
1122         if (!battery_plugin)
1123                 return;
1124
1125         return battery_plugin->start_dn_data();
1126 }
1127
1128 void netconfig_battery_end_dn(void)
1129 {
1130         if (!netconfig_plugin_battery_enabled)
1131                 return;
1132
1133         if (!battery_plugin)
1134                 return;
1135
1136         return battery_plugin->end_dn_data(0, __netconfig_stc_get_dn_stats);
1137 }
1138
1139 void netconfig_battery_update_dn_rssi(int rssi)
1140 {
1141         if (!netconfig_plugin_battery_enabled)
1142                 return;
1143
1144         if (!battery_plugin)
1145                 return;
1146
1147         return battery_plugin->update_dn_rssi(rssi);
1148 }
1149
1150 void netconfig_battery_start_wifi(void)
1151 {
1152         if (!netconfig_plugin_battery_enabled)
1153                 return;
1154
1155         if (!battery_plugin)
1156                 return;
1157
1158         return battery_plugin->start_wifi_data();
1159 }
1160
1161 void netconfig_battery_end_wifi(void)
1162 {
1163         if (!netconfig_plugin_battery_enabled)
1164                 return;
1165
1166         if (!battery_plugin)
1167                 return;
1168
1169         return battery_plugin->end_wifi_data(0, __netconfig_stc_get_wifi_stats);
1170 }
1171
1172 void netconfig_battery_update_wifi_scan(int state)
1173 {
1174         if (!netconfig_plugin_battery_enabled)
1175                 return;
1176
1177         if (!battery_plugin)
1178                 return;
1179
1180         return battery_plugin->update_wifi_scan(state);
1181 }
1182
1183 void netconfig_battery_update_wifi_rssi(int rssi)
1184 {
1185         if (!netconfig_plugin_battery_enabled)
1186                 return;
1187
1188         if (!battery_plugin)
1189                 return;
1190
1191         return battery_plugin->update_wifi_rssi(rssi);
1192 }
1193
1194 void netconfig_battery_get_dn_list(void *data)
1195 {
1196         if (!netconfig_plugin_battery_enabled)
1197                 return;
1198
1199         if (!battery_plugin)
1200                 return;
1201
1202         return battery_plugin->get_battery_dn_list(data, __netconfig_stc_get_dn_stats);
1203 }
1204
1205 void netconfig_battery_get_wifi_list(void *data)
1206 {
1207         if (!netconfig_plugin_battery_enabled)
1208                 return;
1209
1210         if (!battery_plugin)
1211                 return;
1212
1213         return battery_plugin->get_battery_wifi_list(data, __netconfig_stc_get_wifi_stats);
1214 }
1215
1216 void netconfig_set_vconf_int(const char * key, int value, gboolean log)
1217 {
1218         int ret = 0;
1219
1220         if (log)
1221                 DBG("[%s: %d]", key, value);
1222
1223         ret = vconf_set_int(key, value);
1224         if (ret != VCONF_OK)
1225                 ERR("Failed to set");
1226 }
1227
1228 void netconfig_set_vconf_str(const char * key, const char * value, gboolean log)
1229 {
1230         int ret = 0;
1231
1232         if (log)
1233                 DBG("[%s: %s]", key, value);
1234
1235         ret = vconf_set_str(key, value);
1236         if (ret != VCONF_OK)
1237                 ERR("Failed to set");
1238 }
1239
1240 int netconfig_vconf_get_int(const char * key, int *value)
1241 {
1242         int ret = 0;
1243
1244         ret = vconf_get_int(key, value);
1245         if (ret != VCONF_OK) {
1246                 ERR("Failed to get vconfkey [%s] value", key);
1247                 return -1;
1248         }
1249
1250         return 0;
1251 }
1252
1253 int netconfig_vconf_get_bool(const char * key, int *value)
1254 {
1255         int ret = 0;
1256
1257         ret = vconf_get_bool(key, value);
1258         if (ret != VCONF_OK) {
1259                 ERR("Failed to get vconfkey [%s] value", key);
1260                 return -1;
1261         }
1262
1263         return 0;
1264 }
1265
1266 char* netconfig_get_env(const char *key)
1267 {
1268         FILE *fp;
1269         char buf[256], *entry = NULL, *value = NULL, *last;
1270         int len = 0;
1271
1272         if (!key)
1273                 return NULL;
1274
1275         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1276         if (!fp)
1277                 return NULL;
1278
1279         while (fgets(buf, sizeof(buf), fp)) {
1280                 entry = buf;
1281                 entry = strtok_r(entry, "=", &last);
1282                 if (entry) {
1283                         if (strstr(entry, key)) {
1284                                 entry = strtok_r(NULL, "\n", &last);
1285                                 if (entry) {
1286                                         len = strlen(entry);
1287                                         value = (char*)malloc(len+1);
1288                                         g_strlcpy(value, entry, len+1);
1289                                 } else {
1290                                         value = (char*)malloc(sizeof(char));
1291                                         g_strlcpy(value, "\n", sizeof(char));
1292                                 }
1293                                 break;
1294                         }
1295                 }
1296         }
1297
1298         fclose(fp);
1299         return value;
1300 }
1301
1302 void netconfig_set_mac_address_to_vconf(const char *def_mac)
1303 {
1304         int mac_len = 0;
1305
1306         mac_len = strlen(def_mac);
1307         if (mac_len < 17) {
1308                 ERR("def_mac is empty");
1309                 return;
1310         }
1311
1312         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, def_mac, TRUE);
1313 }
1314
1315 void netconfig_set_mac_address_from_file(void)
1316 {
1317         FILE *file = NULL;
1318         char mac_str[MAC_ADDRESS_MAX_LEN];
1319         gchar *mac_lower_str = NULL;
1320         int mac_len = 0;
1321
1322         file = fopen(MAC_INFO_FILEPATH, "r");
1323         if (file == NULL) {
1324                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1325                 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1326                 if (file == NULL) {
1327                         ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1328                         return;
1329                 }
1330         }
1331         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1332                 ERR("Fail to read mac address");
1333                 fclose(file);
1334                 return;
1335         }
1336
1337         mac_len = strlen(mac_str);
1338         if (mac_len < 17) {
1339                 ERR("mac.info is empty");
1340                 fclose(file);
1341                 return;
1342         }
1343
1344         mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1345         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str, TRUE);
1346
1347         g_free(mac_lower_str);
1348         fclose(file);
1349 }
1350
1351 char *netconfig_get_mac_address_from_file(const char *ifname)
1352 {
1353         FILE *file = NULL;
1354         char file_path[PATH_MAX] = {0, };
1355         char mac_str[MAC_ADDRESS_MAX_LEN];
1356         int mac_len = 0;
1357
1358         if (!ifname)
1359                 return NULL;
1360
1361         g_snprintf(file_path, sizeof(file_path),
1362                         "/sys/class/net/%s/address", ifname);
1363
1364         DBG("ifname: %s", ifname);
1365         DBG("file_path: %s", file_path);
1366         file = fopen(file_path, "r");
1367         if (file == NULL) {
1368                 ERR("Fail to open %s", file_path);
1369                 return NULL;
1370         }
1371         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1372                 ERR("Fail to read mac address");
1373                 fclose(file);
1374                 return NULL;
1375         }
1376
1377         mac_len = strlen(mac_str);
1378         if (mac_len < 17) {
1379                 ERR("mac is empty");
1380                 fclose(file);
1381                 return NULL;
1382         }
1383         DBG("address: %s", mac_str);
1384
1385         fclose(file);
1386         return g_strdup(mac_str);
1387 }
1388
1389 char *netconfig_get_default_ifname_from_file(void)
1390 {
1391         GKeyFile *keyfile = NULL;
1392
1393         keyfile = netconfig_keyfile_load(CONNMAN_MAINFILE);
1394         if (keyfile == NULL) {
1395                 ERR("keyfile[%s] is NULL", CONNMAN_MAINFILE);
1396                 return NULL;
1397         }
1398
1399         char *str = g_key_file_get_string(keyfile, "General",
1400                         CONNMAN_WIFI_DEF_IFNAME, NULL);
1401         g_key_file_free(keyfile);
1402
1403         if (!str)
1404                 return NULL;
1405
1406         if (*str == '\0') {
1407                 g_free(str);
1408                 return NULL;
1409         }
1410
1411         g_strchomp(str);
1412
1413         if (strlen(str) >= IFNAMSIZ) {
1414                 g_free(str);
1415                 return NULL;
1416         }
1417
1418         DBG("ifname[%s]", str);
1419         return str;
1420 }
1421
1422 int netconfig_freq_to_channel(int freq)
1423 {
1424         if (freq < 2412 || freq > 5825 ||
1425                 (freq > 2484 && freq < 5180)) {
1426                 ERR("Invalid Frequence Range");
1427                 return 0;
1428         }
1429         if (freq >= 5180)
1430                 return 36 + (freq - 5180)/5;
1431         else if (freq <= 2472)
1432                 return 1 + (freq - 2412)/5;
1433         else if (freq == 2484)
1434                 return 14;
1435         else
1436                 return 0;
1437 }
1438
1439 int netconfig_get_operating_class(int freq)
1440 {
1441         int channel = 0;
1442         int oper_class = 0;
1443
1444         channel = netconfig_freq_to_channel(freq);
1445
1446         if (channel) {
1447                 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1448                 if (channel >= 1 && channel <= 13)
1449                         oper_class = 81;
1450                 /* Operating class 115 - 5 GHz, channels 36-48 */
1451                 else if (channel >= 36 && channel <= 48)
1452                         oper_class = 115;
1453                 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1454                 else
1455                         oper_class = 124;
1456
1457                 INFO("Operating Class  is [%d]", oper_class);
1458                 return oper_class;
1459         }
1460         return 0;
1461 }
1462
1463 tizen_profile_t _get_tizen_profile()
1464 {
1465         static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1466         if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1467                 return profile;
1468
1469         char *profileName;
1470         system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1471         switch (*profileName) {
1472         case 'm':
1473         case 'M':
1474                 profile = TIZEN_PROFILE_MOBILE;
1475                 break;
1476         case 'w':
1477         case 'W':
1478                 profile = TIZEN_PROFILE_WEARABLE;
1479                 break;
1480         case 't':
1481         case 'T':
1482                 profile = TIZEN_PROFILE_TV;
1483                 break;
1484         case 'i':
1485         case 'I':
1486                 profile = TIZEN_PROFILE_IVI;
1487                 break;
1488         default: // common or unknown ==> ALL ARE COMMON.
1489                 profile = TIZEN_PROFILE_COMMON;
1490         }
1491         free(profileName);
1492
1493         return profile;
1494 }
1495
1496 void netconfig_plugin_init()
1497 {
1498         handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1499         if (!handle_headed) {
1500                 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1501         } else {
1502                 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1503                 if (!headed_plugin) {
1504                         ERR("Can't load symbol: %s", dlerror());
1505                         dlclose(handle_headed);
1506                 } else {
1507                         netconfig_plugin_headed_enabled = TRUE;
1508                 }
1509         }
1510
1511         handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1512         if (!handle_telephony) {
1513                 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1514         } else {
1515                 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1516                 if (!telephony_plugin) {
1517                         ERR("Can't load symbol: %s", dlerror());
1518                         dlclose(handle_telephony);
1519                 } else {
1520                         netconfig_plugin_telephony_enabled = TRUE;
1521                 }
1522         }
1523
1524         handle_stc = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
1525         if (!handle_stc) {
1526                 ERR("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
1527         } else {
1528                 stc_plugin = dlsym(handle_stc, "netconfig_stc_plugin");
1529                 if (!stc_plugin) {
1530                         ERR("Can't load symbol: %s", dlerror());
1531                         dlclose(handle_stc);
1532                 } else {
1533                         netconfig_plugin_stc_enabled = TRUE;
1534                 }
1535         }
1536
1537         handle_battery = dlopen(BATTERY_PLUGIN_FILEPATH, RTLD_NOW);
1538         if (!handle_battery) {
1539                 ERR("Can't load %s: %s", BATTERY_PLUGIN_FILEPATH, dlerror());
1540         } else {
1541                 battery_plugin = dlsym(handle_battery, "netconfig_battery_plugin");
1542                 if (!battery_plugin) {
1543                         ERR("Can't load symbol: %s", dlerror());
1544                         dlclose(handle_battery);
1545                 } else {
1546                         netconfig_plugin_battery_enabled = TRUE;
1547                 }
1548         }
1549
1550 }
1551
1552 void netconfig_plugin_deinit()
1553 {
1554         if (netconfig_plugin_headed_enabled) {
1555                 netconfig_plugin_headed_enabled = FALSE;
1556                 dlclose(handle_headed);
1557         }
1558
1559         if (netconfig_plugin_telephony_enabled) {
1560                 netconfig_plugin_telephony_enabled = FALSE;
1561                 dlclose(handle_telephony);
1562         }
1563
1564         if (netconfig_plugin_stc_enabled) {
1565                 netconfig_plugin_stc_enabled = FALSE;
1566                 dlclose(handle_stc);
1567         }
1568
1569         if (netconfig_plugin_battery_enabled) {
1570                 netconfig_plugin_battery_enabled = FALSE;
1571                 dlclose(handle_battery);
1572         }
1573
1574 }
1575
1576 gboolean netconfig_get_headed_plugin_flag()
1577 {
1578         return netconfig_plugin_headed_enabled;
1579 }
1580
1581 gboolean netconfig_get_telephony_plugin_flag()
1582 {
1583         return netconfig_plugin_telephony_enabled;
1584 }
1585
1586 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1587 {
1588         const char *key = NULL;
1589
1590         if (!is_feature_checked[feature]) {
1591                 switch (feature) {
1592                 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1593                         key = ETHERNET_FEATURE;
1594                         break;
1595                 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1596                         key = TETHERING_FEATURE;
1597                         break;
1598                 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1599                         key = WIFI_DIRECT_FEATURE;
1600                         break;
1601                 case NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP:
1602                         key = WIFI_SOFTAP_FEATURE;
1603                         break;
1604                 default:
1605                         ERR("Uknown feature");
1606                         return false;
1607                 }
1608
1609                 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1610                         ERR("Get feature is failed");
1611                         return false;
1612                 }
1613                 is_feature_checked[feature] = true;
1614         }
1615         return feature_supported[feature];
1616 }
1617
1618 void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr)
1619 {
1620         char t;
1621
1622         while (blen) {
1623                 t = (*bin >> 4) & 0x0f;
1624
1625                 if (t <= 9)
1626                         *hexstr = t + '0';
1627                 else if (t >= 10 && t <= 16)
1628                         *hexstr = (t - 10) + 'a';
1629
1630                 hexstr++;
1631
1632                 t = *bin & 0x0f;
1633
1634                 if (t <= 9)
1635                         *hexstr = t + '0';
1636                 else if (t >= 10 && t <= 16)
1637                         *hexstr = (t - 10) + 'a';
1638
1639                 hexstr++;
1640                 bin++;
1641                 blen--;
1642         }
1643
1644         *hexstr = '\0';
1645 }
1646
1647 bool __is_hidden_file(const  char *file)
1648 {
1649         /* exclude "." ,  "..", "settings" and hidden files */
1650         if( g_strcmp0( file, "." ) == 0 ||
1651                         g_strcmp0( file, ".." ) == 0 || file[0]=='.' || g_strcmp0(file, "settings")==0 )
1652         {
1653                 return true;
1654         }
1655
1656         return false;
1657 }
1658
1659 int get_files_count(const char *path)
1660 {
1661         DIR *dfd = NULL;
1662         struct dirent *dir = NULL;
1663         int file_count = 0;
1664         dfd = opendir(path);
1665         if (dfd) {
1666                 while ((dir = readdir(dfd)) != NULL) {
1667                         if (__is_hidden_file(dir->d_name))
1668                                 continue;
1669
1670                         if (strncmp(dir->d_name, "wifi_", 5) != 0)
1671                                 continue;
1672
1673                         ++file_count;
1674                 }
1675                 closedir(dfd);
1676         }
1677
1678         return file_count;
1679 }
1680
1681 char * get_least_recently_profile(const char *path)
1682 {
1683         DIR *dfd = NULL;
1684         struct dirent *dir = NULL;
1685         unsigned long lastModified =  (unsigned long)~0;
1686         char *file = NULL;
1687         dfd = opendir(path);
1688         if (dfd) {
1689                 while ((dir = readdir(dfd)) != NULL) {
1690                         if (__is_hidden_file(dir->d_name))
1691                                 continue;
1692
1693                         if (strncmp(dir->d_name, "wifi_", 5) != 0)
1694                                 continue;
1695
1696                         struct stat attr;
1697                         gchar *full_path = g_strdup_printf("%s/%s", path, dir->d_name);
1698                         if (stat(full_path, &attr)== 0) {
1699                                 if(lastModified > attr.st_mtime)
1700                                 {
1701                                         lastModified = attr.st_mtime;
1702                                         file = dir->d_name;
1703                                 }
1704                         } else {
1705                                 ERR("stat failed");
1706                         }
1707                         g_free(full_path);
1708                 }
1709                 closedir(dfd);
1710         }
1711
1712         DBG("least recently path: [%s]",  file);
1713
1714         return file;
1715 }