2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <net/route.h>
29 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
33 #include <linux/limits.h>
35 #include <vconf-keys.h>
36 #include <tzplatform_config.h>
37 #include <system_info.h>
39 #include <sys/types.h>
46 #include "wifi-state.h"
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"
60 static gboolean netconfig_device_picker_test = FALSE;
64 } dnssd_conn_destroy_data;
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;
79 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
80 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
82 gboolean netconfig_check_mac_address(const char *service, const char *mac_address)
84 int service_index = 0;
86 const char *org = NULL;
87 const char *dst = NULL;
90 if (g_str_has_prefix(service, "wifi_") == FALSE)
93 service_index = strlen("wifi_");
94 for (i = 0; i < 6; i++) {
95 org = &service[service_index];
96 dst = &mac_address[mac_index];
98 if (g_ascii_strncasecmp(org, dst, 2) != 0)
108 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
115 length = strlen(passphrase);
117 if (g_str_has_suffix(service, "psk") == TRUE) {
119 for (int i = 0; i < 64; i++)
120 if (!isxdigit((unsigned char)passphrase[i]))
122 } else if (length < 8 || length > 63)
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]))
129 } else if (length != 5 && length != 13)
136 GKeyFile *netconfig_keyfile_load(const char *pathname)
138 GKeyFile *keyfile = NULL;
139 GError *error = NULL;
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);
146 g_key_file_free(keyfile);
150 DBG("loaded keyfile %s", pathname);
154 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
157 GError *error = NULL;
158 gchar *keydata = NULL;
159 gchar *needle = NULL, *directory = NULL;
161 directory = g_strdup(pathname);
162 if (directory == NULL) {
163 ERR("directory is NULL");
167 needle = g_strrstr(directory, "/");
171 if ((*directory) == '\0') {
173 ERR("directory is NULL");
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) {
181 ERR("failed to make directory");
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);
193 if (chmod(pathname, S_IRUSR | S_IWUSR) < 0)
194 DBG("Failed to change mode");
196 DBG("Successfully saved keyfile %s", pathname);
201 void netconfig_start_timer_seconds(guint secs,
202 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
206 if (callback == NULL) {
207 ERR("callback function is NULL");
211 if ((timer_id != NULL && *timer_id != 0)) {
212 ERR("timer already is registered");
216 t_id = g_timeout_add_seconds(secs, callback, user_data);
219 ERR("Can't add timer");
223 if (timer_id != NULL)
227 void netconfig_start_timer(guint msecs,
228 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
232 INFO("Register timer with callback pointer (%p)", callback);
234 if (callback == NULL) {
235 ERR("callback function is NULL");
239 if ((timer_id != NULL && *timer_id != 0)) {
240 ERR("timer already is registered");
244 t_id = g_timeout_add(msecs, callback, user_data);
247 ERR("Can't add timer");
251 if (timer_id != NULL)
255 void netconfig_stop_timer(guint *timer_id)
257 if (timer_id == NULL) {
258 ERR("timer is NULL");
262 if (*timer_id != 0) {
263 g_source_remove(*timer_id);
268 static gboolean __netconfig_test_device_picker()
270 char *favorite_wifi_service = NULL;
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);
282 static void __netconfig_pop_device_picker(void)
284 if (!netconfig_plugin_headed_enabled)
290 headed_plugin->pop_device_picker();
293 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
295 if (__netconfig_test_device_picker() == TRUE)
296 __netconfig_pop_device_picker();
301 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
303 static guint netconfig_wifi_device_picker_service_timer = 0;
305 if (is_set_method != TRUE)
306 return netconfig_wifi_device_picker_service_timer;
308 if (netconfig_wifi_device_picker_service_timer != timer_id)
309 netconfig_wifi_device_picker_service_timer = timer_id;
311 return netconfig_wifi_device_picker_service_timer;
314 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
316 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
319 static guint __netconfig_wifi_device_picker_get_timer_id(void)
321 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
324 void netconfig_wifi_enable_device_picker_test(void)
326 netconfig_device_picker_test = TRUE;
329 void netconfig_wifi_device_picker_service_start(void)
331 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
334 if (netconfig_device_picker_test == TRUE)
335 netconfig_device_picker_test = FALSE;
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)
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);
348 __netconfig_wifi_device_picker_set_timer_id(timer_id);
351 void netconfig_wifi_device_picker_service_stop(void)
355 timer_id = __netconfig_wifi_device_picker_get_timer_id();
359 DBG("Clear device picker timer with timer_id %d", timer_id);
361 netconfig_stop_timer(&timer_id);
363 __netconfig_wifi_device_picker_set_timer_id(timer_id);
366 gboolean netconfig_is_wifi_direct_on(void)
368 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
371 int wifi_direct_state = 0;
373 netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
375 DBG("Wi-Fi direct mode %d", wifi_direct_state);
376 return (wifi_direct_state != 0) ? TRUE : FALSE;
379 gboolean netconfig_is_wifi_tethering_on(void)
381 if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
382 int wifi_tethering_state = 0;
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");
393 DBG("Mobile AP is off");
397 gboolean netconfig_interface_up(const char *ifname)
402 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
404 ERR("socket failed %d", errno);
408 memset(&ifr, 0, sizeof(ifr));
409 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
411 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
412 ERR("Fail to get IFFLAGS %d", errno);
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);
427 DBG("Successfully activated wireless interface %s", ifname);
431 gboolean netconfig_interface_down(const char *ifname)
436 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
438 ERR("socket failed %d", errno);
442 memset(&ifr, 0, sizeof(ifr));
443 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
445 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
446 ERR("Fail to get IFFLAGS %d", errno);
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);
461 DBG("Successfully de-activated wireless interface %s", ifname);
465 int netconfig_execute_file(const char *file_path,
466 char *const args[], char *const envs[])
472 register unsigned int index = 0;
473 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
475 while (args[index] != NULL) {
476 DBG("%s", args[index]);
480 if (!(pid = fork())) {
481 DBG("pid(%d), ppid (%d)", getpid(), getppid());
482 DBG("Inside child, exec (%s) command", file_path);
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));
490 } else if (pid > 0) {
491 if (waitpid(pid, &status, 0) == -1)
492 DBG("wait pid (%u) status (%d)", pid, status);
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)) {
508 DBG("failed to fork(%s)",
509 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
513 int netconfig_execute_cmd(const char *cmd)
522 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
525 DBG("command: %s", cmd);
527 args = g_strsplit_set(cmd, " ", -1);
529 if (!(pid = fork())) {
530 DBG("pid(%d), ppid (%d)", getpid(), getppid());
533 if (execv(args[0], args) == -1) {
534 DBG("Fail to execute command (%s)",
535 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
539 } else if (pid > 0) {
540 if (waitpid(pid, &status, 0) == -1)
541 DBG("wait pid (%u) status (%d)", pid, status);
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)) {
558 DBG("failed to fork(%s)",
559 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
565 static void on_clat_handler()
570 clat_pid = waitpid(-1, &state, WNOHANG);
572 DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
575 int netconfig_execute_clatd(const char *file_path, char *const args[])
580 register unsigned int index = 0;
581 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
583 struct sigaction act;
586 act.sa_handler = on_clat_handler;
587 sigemptyset(&act.sa_mask);
590 state = sigaction(SIGCHLD, &act, 0);
592 DBG("sigaction() : %d", state);
596 while (args[index] != NULL) {
597 DBG("%s", args[index]);
601 if (!(pid = fork())) {
602 DBG("pid(%d), ppid (%d)", getpid(), getppid());
603 DBG("Inside child, exec (%s) command", file_path);
606 if (execvp(file_path, args) == -1) {
607 ERR("Fail to execute command (%s)",
608 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
611 } else if (pid > 0) {
612 ERR("Success to launch clatd");
616 DBG("failed to fork(%s)",
617 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
621 static void no_wait_signal_handler()
626 child_pid = waitpid(-1, &state, WNOHANG);
628 DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
631 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
636 register unsigned int index = 0;
638 struct sigaction act;
640 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
642 act.sa_handler = no_wait_signal_handler;
643 sigemptyset(&act.sa_mask);
646 state = sigaction(SIGCHLD, &act, 0);
648 DBG("sigaction() : %d", state);
652 while (args[index] != NULL) {
653 DBG("%s", args[index]);
657 if (!(pid = fork())) {
658 DBG("pid(%d), ppid (%d)", getpid(), getppid());
659 DBG("Inside child, exec (%s) command", file_path);
662 if (execvp(file_path, args) == -1) {
663 ERR("Fail to execute command (%s)",
664 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
667 } else if (pid > 0) {
668 ERR("Successfully launched child process");
672 DBG("failed to fork(%s)",
673 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
677 int __netconfig_get_interface_index(const char *interface_name)
682 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
684 if (interface_name == NULL) {
685 DBG("Inteface name is NULL");
690 sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
692 DBG("Failed to create socket : %s",
693 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
697 memset(&ifr, 0, sizeof(ifr));
698 strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
699 result = ioctl(sock, SIOCGIFINDEX, &ifr);
703 DBG("Failed to get ifr index: %s",
704 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
708 return ifr.ifr_ifindex;
711 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
715 struct sockaddr_in addr_in;
717 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
719 memset(&ifr, 0, sizeof(ifr));
721 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
723 if (ifr.ifr_ifindex < 0)
726 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
728 memset(&rt, 0, sizeof(rt));
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));
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));
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));
746 rt.rt_dev = ifr.ifr_name;
749 sock = socket(PF_INET, SOCK_DGRAM, 0);
752 DBG("Failed to create socket : %s",
753 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
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));
769 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
773 struct sockaddr_in addr_in;
775 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
777 memset(&ifr, 0, sizeof(ifr));
778 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
780 if (ifr.ifr_ifindex < 0)
783 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
785 memset(&rt, 0, sizeof(rt));
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));
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;
800 sock = socket(PF_INET, SOCK_DGRAM, 0);
803 DBG("Failed to create socket : %s",
804 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
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));
820 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
825 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
827 memset(&rt, 0, sizeof(rt));
829 rt.rtmsg_dst_len = prefix_len;
831 rt.rtmsg_flags = RTF_UP | RTF_HOST;
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));
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));
851 fd = socket(AF_INET6, SOCK_DGRAM, 0);
853 DBG("Failed to create socket : %s",
854 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
858 rt.rtmsg_ifindex = 0;
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;
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));
880 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
886 memset(&rt, 0, sizeof(rt));
888 rt.rtmsg_dst_len = prefix_len;
890 rt.rtmsg_flags = RTF_UP | RTF_HOST;
892 if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
897 if (gateway != NULL) {
898 rt.rtmsg_flags |= RTF_GATEWAY;
899 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
907 fd = socket(AF_INET6, SOCK_DGRAM, 0);
911 rt.rtmsg_ifindex = 0;
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;
921 if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
922 DBG("Failed to del route: %d\n", err);
932 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
934 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
935 wifi_complete_launch_direct(wifi, context);
940 DBG("Launch Wi-Fi direct daemon");
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 };
946 ret = netconfig_execute_file(path, args, envs);
948 ERR("Failed to launch Wi-Fi direct daemon");
949 netconfig_error_wifi_direct_failed(context);
953 wifi_complete_launch_direct(wifi, context);
957 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
959 if (!netconfig_plugin_headed_enabled)
965 return headed_plugin->send_notification_to_net_popup(noti, ssid);
968 int netconfig_send_message_to_net_popup(const char *title,
969 const char *content, const char *type, const char *ssid)
971 if (!netconfig_plugin_headed_enabled)
977 return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
980 int netconfig_send_restriction_to_net_popup(const char *title,
981 const char *type, const char *restriction)
983 if (!netconfig_plugin_headed_enabled)
989 return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
992 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
994 if (!netconfig_plugin_headed_enabled)
1000 headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1003 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1005 if (!netconfig_plugin_headed_enabled)
1011 headed_plugin->pop_wifi_connected_poppup(ssid);
1014 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1016 if (!netconfig_plugin_telephony_enabled)
1019 if (!telephony_plugin)
1022 telephony_plugin->get_telephony_network_type(svctype, pstype);
1025 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1027 if (!netconfig_plugin_telephony_enabled)
1030 if (!telephony_plugin)
1033 return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1036 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1037 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1039 if (!netconfig_plugin_telephony_enabled)
1040 return NETCONFIG_ERROR_INTERNAL;
1042 if (!telephony_plugin)
1043 return NETCONFIG_ERROR_INTERNAL;
1045 return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1048 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1049 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1051 if (!netconfig_plugin_telephony_enabled)
1054 if (!telephony_plugin)
1057 return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1060 gboolean netconfig_tapi_check_sim_state(void)
1062 if (!netconfig_plugin_telephony_enabled)
1065 if (!telephony_plugin)
1068 return telephony_plugin->tapi_check_sim_state();
1071 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1072 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1074 if (!netconfig_plugin_telephony_enabled)
1077 if (!telephony_plugin)
1080 return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1083 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1084 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1086 if (!netconfig_plugin_telephony_enabled)
1089 if (!telephony_plugin)
1092 return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1095 static void __netconfig_stc_get_dn_stats(time_t from, time_t to, GSList **list)
1097 if (!netconfig_plugin_stc_enabled)
1103 return stc_plugin->get_stc_dn_stats(from, to, list);
1106 static void __netconfig_stc_get_wifi_stats(time_t from, time_t to, GSList **list)
1108 if (!netconfig_plugin_stc_enabled)
1114 return stc_plugin->get_stc_wifi_stats(from, to, list);
1117 void netconfig_battery_start_dn(void)
1119 if (!netconfig_plugin_battery_enabled)
1122 if (!battery_plugin)
1125 return battery_plugin->start_dn_data();
1128 void netconfig_battery_end_dn(void)
1130 if (!netconfig_plugin_battery_enabled)
1133 if (!battery_plugin)
1136 return battery_plugin->end_dn_data(0, __netconfig_stc_get_dn_stats);
1139 void netconfig_battery_update_dn_rssi(int rssi)
1141 if (!netconfig_plugin_battery_enabled)
1144 if (!battery_plugin)
1147 return battery_plugin->update_dn_rssi(rssi);
1150 void netconfig_battery_start_wifi(void)
1152 if (!netconfig_plugin_battery_enabled)
1155 if (!battery_plugin)
1158 return battery_plugin->start_wifi_data();
1161 void netconfig_battery_end_wifi(void)
1163 if (!netconfig_plugin_battery_enabled)
1166 if (!battery_plugin)
1169 return battery_plugin->end_wifi_data(0, __netconfig_stc_get_wifi_stats);
1172 void netconfig_battery_update_wifi_scan(int state)
1174 if (!netconfig_plugin_battery_enabled)
1177 if (!battery_plugin)
1180 return battery_plugin->update_wifi_scan(state);
1183 void netconfig_battery_update_wifi_rssi(int rssi)
1185 if (!netconfig_plugin_battery_enabled)
1188 if (!battery_plugin)
1191 return battery_plugin->update_wifi_rssi(rssi);
1194 void netconfig_battery_get_dn_list(void *data)
1196 if (!netconfig_plugin_battery_enabled)
1199 if (!battery_plugin)
1202 return battery_plugin->get_battery_dn_list(data, __netconfig_stc_get_dn_stats);
1205 void netconfig_battery_get_wifi_list(void *data)
1207 if (!netconfig_plugin_battery_enabled)
1210 if (!battery_plugin)
1213 return battery_plugin->get_battery_wifi_list(data, __netconfig_stc_get_wifi_stats);
1216 void netconfig_set_vconf_int(const char * key, int value, gboolean log)
1221 DBG("[%s: %d]", key, value);
1223 ret = vconf_set_int(key, value);
1224 if (ret != VCONF_OK)
1225 ERR("Failed to set");
1228 void netconfig_set_vconf_str(const char * key, const char * value, gboolean log)
1233 DBG("[%s: %s]", key, value);
1235 ret = vconf_set_str(key, value);
1236 if (ret != VCONF_OK)
1237 ERR("Failed to set");
1240 int netconfig_vconf_get_int(const char * key, int *value)
1244 ret = vconf_get_int(key, value);
1245 if (ret != VCONF_OK) {
1246 ERR("Failed to get vconfkey [%s] value", key);
1253 int netconfig_vconf_get_bool(const char * key, int *value)
1257 ret = vconf_get_bool(key, value);
1258 if (ret != VCONF_OK) {
1259 ERR("Failed to get vconfkey [%s] value", key);
1266 char* netconfig_get_env(const char *key)
1269 char buf[256], *entry = NULL, *value = NULL, *last;
1275 fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1279 while (fgets(buf, sizeof(buf), fp)) {
1281 entry = strtok_r(entry, "=", &last);
1283 if (strstr(entry, key)) {
1284 entry = strtok_r(NULL, "\n", &last);
1286 len = strlen(entry);
1287 value = (char*)malloc(len+1);
1288 g_strlcpy(value, entry, len+1);
1290 value = (char*)malloc(sizeof(char));
1291 g_strlcpy(value, "\n", sizeof(char));
1302 void netconfig_set_mac_address_to_vconf(const char *def_mac)
1306 mac_len = strlen(def_mac);
1308 ERR("def_mac is empty");
1312 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, def_mac, TRUE);
1315 void netconfig_set_mac_address_from_file(void)
1318 char mac_str[MAC_ADDRESS_MAX_LEN];
1319 gchar *mac_lower_str = NULL;
1322 file = fopen(MAC_INFO_FILEPATH, "r");
1324 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1325 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1327 ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1331 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1332 ERR("Fail to read mac address");
1337 mac_len = strlen(mac_str);
1339 ERR("mac.info is empty");
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);
1347 g_free(mac_lower_str);
1351 char *netconfig_get_mac_address_from_file(const char *ifname)
1354 char file_path[PATH_MAX] = {0, };
1355 char mac_str[MAC_ADDRESS_MAX_LEN];
1361 g_snprintf(file_path, sizeof(file_path),
1362 "/sys/class/net/%s/address", ifname);
1364 DBG("ifname: %s", ifname);
1365 DBG("file_path: %s", file_path);
1366 file = fopen(file_path, "r");
1368 ERR("Fail to open %s", file_path);
1371 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1372 ERR("Fail to read mac address");
1377 mac_len = strlen(mac_str);
1379 ERR("mac is empty");
1383 DBG("address: %s", mac_str);
1386 return g_strdup(mac_str);
1389 char *netconfig_get_default_ifname_from_file(void)
1391 GKeyFile *keyfile = NULL;
1393 keyfile = netconfig_keyfile_load(CONNMAN_MAINFILE);
1394 if (keyfile == NULL) {
1395 ERR("keyfile[%s] is NULL", CONNMAN_MAINFILE);
1399 char *str = g_key_file_get_string(keyfile, "General",
1400 CONNMAN_WIFI_DEF_IFNAME, NULL);
1401 g_key_file_free(keyfile);
1413 if (strlen(str) >= IFNAMSIZ) {
1418 DBG("ifname[%s]", str);
1422 int netconfig_freq_to_channel(int freq)
1424 if (freq < 2412 || freq > 5825 ||
1425 (freq > 2484 && freq < 5180)) {
1426 ERR("Invalid Frequence Range");
1430 return 36 + (freq - 5180)/5;
1431 else if (freq <= 2472)
1432 return 1 + (freq - 2412)/5;
1433 else if (freq == 2484)
1439 int netconfig_get_operating_class(int freq)
1444 channel = netconfig_freq_to_channel(freq);
1447 /* Operating class 81 - 2.4 GHz band channels 1..13 */
1448 if (channel >= 1 && channel <= 13)
1450 /* Operating class 115 - 5 GHz, channels 36-48 */
1451 else if (channel >= 36 && channel <= 48)
1453 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
1457 INFO("Operating Class is [%d]", oper_class);
1463 tizen_profile_t _get_tizen_profile()
1465 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1466 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1470 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1471 switch (*profileName) {
1474 profile = TIZEN_PROFILE_MOBILE;
1478 profile = TIZEN_PROFILE_WEARABLE;
1482 profile = TIZEN_PROFILE_TV;
1486 profile = TIZEN_PROFILE_IVI;
1488 default: // common or unknown ==> ALL ARE COMMON.
1489 profile = TIZEN_PROFILE_COMMON;
1496 void netconfig_plugin_init()
1498 handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1499 if (!handle_headed) {
1500 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
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);
1507 netconfig_plugin_headed_enabled = TRUE;
1511 handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1512 if (!handle_telephony) {
1513 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
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);
1520 netconfig_plugin_telephony_enabled = TRUE;
1524 handle_stc = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
1526 ERR("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
1528 stc_plugin = dlsym(handle_stc, "netconfig_stc_plugin");
1530 ERR("Can't load symbol: %s", dlerror());
1531 dlclose(handle_stc);
1533 netconfig_plugin_stc_enabled = TRUE;
1537 handle_battery = dlopen(BATTERY_PLUGIN_FILEPATH, RTLD_NOW);
1538 if (!handle_battery) {
1539 ERR("Can't load %s: %s", BATTERY_PLUGIN_FILEPATH, dlerror());
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);
1546 netconfig_plugin_battery_enabled = TRUE;
1552 void netconfig_plugin_deinit()
1554 if (netconfig_plugin_headed_enabled) {
1555 netconfig_plugin_headed_enabled = FALSE;
1556 dlclose(handle_headed);
1559 if (netconfig_plugin_telephony_enabled) {
1560 netconfig_plugin_telephony_enabled = FALSE;
1561 dlclose(handle_telephony);
1564 if (netconfig_plugin_stc_enabled) {
1565 netconfig_plugin_stc_enabled = FALSE;
1566 dlclose(handle_stc);
1569 if (netconfig_plugin_battery_enabled) {
1570 netconfig_plugin_battery_enabled = FALSE;
1571 dlclose(handle_battery);
1576 gboolean netconfig_get_headed_plugin_flag()
1578 return netconfig_plugin_headed_enabled;
1581 gboolean netconfig_get_telephony_plugin_flag()
1583 return netconfig_plugin_telephony_enabled;
1586 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1588 const char *key = NULL;
1590 if (!is_feature_checked[feature]) {
1592 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1593 key = ETHERNET_FEATURE;
1595 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1596 key = TETHERING_FEATURE;
1598 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1599 key = WIFI_DIRECT_FEATURE;
1601 case NETCONFIG_SUPPORTED_FEATURE_WIFI_SOFTAP:
1602 key = WIFI_SOFTAP_FEATURE;
1605 ERR("Uknown feature");
1609 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1610 ERR("Get feature is failed");
1613 is_feature_checked[feature] = true;
1615 return feature_supported[feature];
1618 void netconfig_convert_bytes_to_hexstr(const char *bin, int blen, gchar* hexstr)
1623 t = (*bin >> 4) & 0x0f;
1627 else if (t >= 10 && t <= 16)
1628 *hexstr = (t - 10) + 'a';
1636 else if (t >= 10 && t <= 16)
1637 *hexstr = (t - 10) + 'a';
1647 bool __is_hidden_file(const char *file)
1649 /* exclude "." , "..", "settings" and hidden files */
1650 if( g_strcmp0( file, "." ) == 0 ||
1651 g_strcmp0( file, ".." ) == 0 || file[0]=='.' || g_strcmp0(file, "settings")==0 )
1659 int get_files_count(const char *path)
1662 struct dirent *dir = NULL;
1664 dfd = opendir(path);
1666 while ((dir = readdir(dfd)) != NULL) {
1667 if (__is_hidden_file(dir->d_name))
1670 if (strncmp(dir->d_name, "wifi_", 5) != 0)
1681 char * get_least_recently_profile(const char *path)
1684 struct dirent *dir = NULL;
1685 unsigned long lastModified = (unsigned long)~0;
1687 dfd = opendir(path);
1689 while ((dir = readdir(dfd)) != NULL) {
1690 if (__is_hidden_file(dir->d_name))
1693 if (strncmp(dir->d_name, "wifi_", 5) != 0)
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)
1701 lastModified = attr.st_mtime;
1712 DBG("least recently path: [%s]", file);