2 * Network Configuration Module
4 * Copyright (c) 2012-2013 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.
24 #include <sys/socket.h>
25 #include <sys/ioctl.h>
28 #include <vconf-keys.h>
29 #include <wifi-direct.h>
30 #include <syspopup_caller.h>
36 #include "wifi-state.h"
38 #define WIFI_MAC_INFO_FILE "/opt/etc/.mac.info"
39 #define WIFI_MAC_INFO_LENGTH 17
40 #define WIFI_DEV_NAME "wlan0"
42 GKeyFile *netconfig_keyfile_load(const char *pathname)
44 GKeyFile *keyfile = NULL;
47 keyfile = g_key_file_new();
48 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
49 DBG("Unable to open %s, error %s", pathname, error->message);
52 g_key_file_free(keyfile);
59 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
63 gchar *keydata = NULL;
64 gchar *needle = NULL, *directory = NULL;
66 directory = g_strdup(pathname);
67 needle = g_strrstr(directory, "/");
72 if (directory == NULL || (*directory) == '\0') {
77 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
78 if (g_mkdir_with_parents(directory,
79 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
86 keydata = g_key_file_to_data(keyfile, &size, &error);
87 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
88 DBG("Unable to save %s, error %s", pathname, error->message);
92 if (chmod(pathname, S_IRUSR | S_IWUSR) != 0)
93 DBG("Unable to change permission of %s", pathname);
97 g_key_file_free(keyfile);
100 void netconfig_start_timer_seconds(guint secs,
101 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
105 if (callback == NULL) {
106 ERR("callback function is NULL");
110 if ((timer_id != NULL && *timer_id != 0)) {
111 ERR("timer already is registered");
115 t_id = g_timeout_add_seconds(secs, callback, user_data);
118 ERR("Can't add timer");
122 if (timer_id != NULL)
126 void netconfig_start_timer(guint msecs,
127 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
131 INFO("Register timer with callback pointer (%p)", callback);
133 if (callback == NULL) {
134 ERR("callback function is NULL");
138 if ((timer_id != NULL && *timer_id != 0)) {
139 ERR("timer already is registered");
143 t_id = g_timeout_add(msecs, callback, user_data);
146 ERR("Can't add timer");
150 if (timer_id != NULL)
154 void netconfig_stop_timer(guint *timer_id)
156 if (timer_id == NULL) {
157 ERR("timer is NULL");
161 if (*timer_id != 0) {
162 g_source_remove(*timer_id);
167 static gboolean __netconfig_test_device_picker()
169 char *favorite_wifi_service = NULL;
171 favorite_wifi_service = netconfig_wifi_get_favorite_service();
172 if (favorite_wifi_service != NULL) {
173 g_free(favorite_wifi_service);
180 static void __netconfig_pop_device_picker(void)
184 int wifi_ug_state = 0;
186 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
187 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
192 DBG("Launch Wi-Fi device picker");
193 rv = syspopup_launch("wifi-qs", b);
198 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
200 if (__netconfig_test_device_picker() == TRUE)
201 __netconfig_pop_device_picker();
206 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method,
209 static guint netconfig_wifi_device_picker_service_timer = 0;
211 if (is_set_method != TRUE)
212 return netconfig_wifi_device_picker_service_timer;
214 if (netconfig_wifi_device_picker_service_timer != timer_id)
215 netconfig_wifi_device_picker_service_timer = timer_id;
217 return netconfig_wifi_device_picker_service_timer;
220 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
222 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
225 static guint __netconfig_wifi_device_picker_get_timer_id(void)
227 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
230 void netconfig_wifi_device_picker_service_start(void)
233 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
234 int hotspot_wifi_state = VCONFKEY_MOBILE_HOTSPOT_WIFI_OFF;
237 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
238 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
241 /* If Wi-Fi tethering is pending on, don't show device picker UI*/
242 vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_WIFI_STATE, &hotspot_wifi_state);
243 if (hotspot_wifi_state == VCONFKEY_MOBILE_HOTSPOT_WIFI_PENDING_ON) {
244 DBG("hotspot_wifi_state %d", hotspot_wifi_state);
248 DBG("Register device picker timer with %d milliseconds",
249 NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
251 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL,
252 __netconfig_wifi_try_device_picker, NULL, &timer_id);
254 __netconfig_wifi_device_picker_set_timer_id(timer_id);
257 void netconfig_wifi_device_picker_service_stop(void)
261 timer_id = __netconfig_wifi_device_picker_get_timer_id();
265 DBG("Clear device picker timer with timer_id %d", timer_id);
267 netconfig_stop_timer(&timer_id);
269 __netconfig_wifi_device_picker_set_timer_id(timer_id);
272 gboolean netconfig_is_wifi_direct_on(void)
274 int wifi_direct_state = 0;
276 vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
278 DBG("Wi-Fi direct mode %d", wifi_direct_state);
279 return (wifi_direct_state != 0) ? TRUE : FALSE;
282 gboolean netconfig_is_wifi_tethering_on(void)
284 int wifi_tethering_state = 0;
286 vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
288 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
289 if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
295 /* args[] and env[] should be terminated with NULL pointer */
296 gboolean netconfig_execute_file(const char *file_path,
297 char *const args[], char *const env[])
303 if (!(pid = fork())) {
304 register unsigned int index = 0;
305 INFO("pid(%d), ppid (%d)", getpid(), getppid());
306 INFO("Inside child, exec (%s) command", file_path);
309 while (args[index] != NULL) {
310 INFO(" %s", args[index]);
315 if (execve(file_path, args, env) == -1) {
316 DBG("Fail to execute command...(%s)",
320 } else if (pid > 0) {
321 if (waitpid(pid, &rv, 0) == -1) {
322 DBG("wait pid (%u) rv (%d)", pid, rv);
325 DBG("exited, rv=%d", WEXITSTATUS(rv));
326 } else if (WIFSIGNALED(rv)) {
327 DBG("killed by signal %d", WTERMSIG(rv));
328 } else if (WIFSTOPPED(rv)) {
329 DBG("stopped by signal %d", WSTOPSIG(rv));
330 } else if (WIFCONTINUED(rv)) {
337 DBG("failed to fork()...(%s)", strerror(errno));
341 gboolean netconfig_iface_wifi_launch_direct(NetconfigWifi *wifi, GError **error)
345 DBG("Launch Wi-Fi direct daemon");
347 const char *path = "/usr/bin/wifi-direct-server.sh";
348 char *const args[] = { "wifi-direct-server.sh", "start", NULL};
349 char *const env[] = { NULL };
351 ret = netconfig_execute_file(path, args, env);
354 INFO("Failed to launch Wi-Fi direct daemon");
356 netconfig_error_wifi_direct_failed(error);
362 void netconfig_add_wifi_found_notification(void)
365 bundle *b = bundle_create();
367 bundle_add(b, "_SYSPOPUP_TYPE_", "add_found_ap_noti");
369 ret = aul_launch_app("org.tizen.net-popup", b);
374 DBG("Successfully added notification");
376 ERR("Unable to launch noti-popup. Err = %d", ret);
379 void netconfig_del_wifi_found_notification(void)
382 bundle *b = bundle_create();
384 bundle_add(b, "_SYSPOPUP_TYPE_", "del_found_ap_noti");
386 ret = aul_launch_app("org.tizen.net-popup", b);
391 DBG("Successfully deleted notification");
393 ERR("Unable to launch noti-popup. Err = %d", ret);
397 void netconfig_set_wifi_mac_address(void)
402 char buf[WIFI_MAC_INFO_LENGTH + 1];
405 mac_info = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
406 if (mac_info == NULL) {
407 ERR("Failed to open vconf key %s", VCONFKEY_WIFI_BSSID_ADDRESS);
410 INFO("%s : %s", VCONFKEY_WIFI_BSSID_ADDRESS, mac_info);
412 fp = fopen(WIFI_MAC_INFO_FILE, "r");
414 if (fgets(buf, sizeof(buf), fp) == NULL) {
415 ERR("Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
419 INFO("%s : %s", WIFI_MAC_INFO_FILE, buf);
421 if (strlen(buf) < WIFI_MAC_INFO_LENGTH) {
422 ERR("Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
426 buf[WIFI_MAC_INFO_LENGTH] = '\0';
428 // not MAC into file use ioctl to get MAC
429 ctl_sk = socket(PF_INET,SOCK_DGRAM,0);
431 ERR("Failed to open socket");
435 memset(&ifr, 0, sizeof(struct ifreq));
436 strncpy(ifr.ifr_name, WIFI_DEV_NAME, sizeof(ifr.ifr_name) - 1);
437 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = 0;
439 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr) != 0) {
440 ERR("Failed to SIOCGIFHWADDR ioctl");
444 snprintf(buf, WIFI_MAC_INFO_LENGTH + 1,
445 "%02x:%02x:%02x:%02x:%02x:%02x",
446 (unsigned char)ifr.ifr_hwaddr.sa_data[0],
447 (unsigned char)ifr.ifr_hwaddr.sa_data[1],
448 (unsigned char)ifr.ifr_hwaddr.sa_data[2],
449 (unsigned char)ifr.ifr_hwaddr.sa_data[3],
450 (unsigned char)ifr.ifr_hwaddr.sa_data[4],
451 (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
453 INFO("%s MAC address: %s", WIFI_DEV_NAME, buf);
456 if (mac_info && (g_str_equal(mac_info, buf) == TRUE))
459 if (vconf_set_str(VCONFKEY_WIFI_BSSID_ADDRESS, buf) != 0)
460 ERR("Failed to set MAC info to %s", VCONFKEY_WIFI_BSSID_ADDRESS);