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