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>
34 #include "wifi-state.h"
36 #define WIFI_MAC_INFO_FILE "/opt/etc/.mac.info"
37 #define WIFI_MAC_INFO_LENGTH 17
38 #define WIFI_DEV_NAME "wlan0"
40 GKeyFile *netconfig_keyfile_load(const char *pathname)
42 GKeyFile *keyfile = NULL;
45 keyfile = g_key_file_new();
46 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
47 DBG("Unable to open %s, error %s", pathname, error->message);
50 g_key_file_free(keyfile);
57 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
61 gchar *keydata = NULL;
62 gchar *needle = NULL, *directory = NULL;
64 directory = g_strdup(pathname);
65 needle = g_strrstr(directory, "/");
70 if (directory == NULL || (*directory) == '\0') {
75 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
76 if (g_mkdir_with_parents(directory,
77 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
84 keydata = g_key_file_to_data(keyfile, &size, &error);
85 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
86 DBG("Unable to save %s, error %s", pathname, error->message);
90 if (chmod(pathname, S_IRUSR | S_IWUSR) != 0)
91 DBG("Unable to change permission of %s", pathname);
95 g_key_file_free(keyfile);
98 void netconfig_start_timer_seconds(guint secs,
99 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
103 if (callback == NULL) {
104 ERR("callback function is NULL");
108 if ((timer_id != NULL && *timer_id != 0)) {
109 ERR("timer already is registered");
113 t_id = g_timeout_add_seconds(secs, callback, user_data);
116 ERR("Can't add timer");
120 if (timer_id != NULL)
124 void netconfig_start_timer(guint msecs,
125 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
129 INFO("Register timer with callback pointer (%p)", callback);
131 if (callback == NULL) {
132 ERR("callback function is NULL");
136 if ((timer_id != NULL && *timer_id != 0)) {
137 ERR("timer already is registered");
141 t_id = g_timeout_add(msecs, callback, user_data);
144 ERR("Can't add timer");
148 if (timer_id != NULL)
152 void netconfig_stop_timer(guint *timer_id)
154 if (timer_id == NULL) {
155 ERR("timer is NULL");
159 if (*timer_id != 0) {
160 g_source_remove(*timer_id);
165 static gboolean __netconfig_test_device_picker()
167 char *favorite_wifi_service = NULL;
169 favorite_wifi_service = netconfig_wifi_get_favorite_service();
170 if (favorite_wifi_service != NULL) {
171 g_free(favorite_wifi_service);
178 static void __netconfig_pop_device_picker(void)
181 int wifi_ug_state = 0;
183 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
184 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
187 DBG("Launch Wi-Fi device picker");
188 // TODO : display a popup
192 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
194 if (__netconfig_test_device_picker() == TRUE)
195 __netconfig_pop_device_picker();
200 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method,
203 static guint netconfig_wifi_device_picker_service_timer = 0;
205 if (is_set_method != TRUE)
206 return netconfig_wifi_device_picker_service_timer;
208 if (netconfig_wifi_device_picker_service_timer != timer_id)
209 netconfig_wifi_device_picker_service_timer = timer_id;
211 return netconfig_wifi_device_picker_service_timer;
214 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
216 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
219 static guint __netconfig_wifi_device_picker_get_timer_id(void)
221 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
224 void netconfig_wifi_device_picker_service_start(void)
227 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
228 int hotspot_wifi_state = VCONFKEY_MOBILE_HOTSPOT_WIFI_OFF;
231 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
232 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
235 /* If Wi-Fi tethering is pending on, don't show device picker UI*/
236 vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_WIFI_STATE, &hotspot_wifi_state);
237 if (hotspot_wifi_state == VCONFKEY_MOBILE_HOTSPOT_WIFI_PENDING_ON) {
238 DBG("hotspot_wifi_state %d", hotspot_wifi_state);
242 DBG("Register device picker timer with %d milliseconds",
243 NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
245 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL,
246 __netconfig_wifi_try_device_picker, NULL, &timer_id);
248 __netconfig_wifi_device_picker_set_timer_id(timer_id);
251 void netconfig_wifi_device_picker_service_stop(void)
255 timer_id = __netconfig_wifi_device_picker_get_timer_id();
259 DBG("Clear device picker timer with timer_id %d", timer_id);
261 netconfig_stop_timer(&timer_id);
263 __netconfig_wifi_device_picker_set_timer_id(timer_id);
266 gboolean netconfig_is_wifi_direct_on(void)
268 int wifi_direct_state = 0;
270 vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
272 DBG("Wi-Fi direct mode %d", wifi_direct_state);
273 return (wifi_direct_state != 0) ? TRUE : FALSE;
276 gboolean netconfig_is_wifi_tethering_on(void)
278 int wifi_tethering_state = 0;
280 vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
282 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
283 if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
289 /* args[] and env[] should be terminated with NULL pointer */
290 gboolean netconfig_execute_file(const char *file_path,
291 char *const args[], char *const env[])
297 if (!(pid = fork())) {
298 register unsigned int index = 0;
299 INFO("pid(%d), ppid (%d)", getpid(), getppid());
300 INFO("Inside child, exec (%s) command", file_path);
303 while (args[index] != NULL) {
304 INFO(" %s", args[index]);
309 if (execve(file_path, args, env) == -1) {
310 DBG("Fail to execute command...(%s)",
314 } else if (pid > 0) {
315 if (waitpid(pid, &rv, 0) == -1) {
316 DBG("wait pid (%u) rv (%d)", pid, rv);
319 DBG("exited, rv=%d", WEXITSTATUS(rv));
320 } else if (WIFSIGNALED(rv)) {
321 DBG("killed by signal %d", WTERMSIG(rv));
322 } else if (WIFSTOPPED(rv)) {
323 DBG("stopped by signal %d", WSTOPSIG(rv));
324 } else if (WIFCONTINUED(rv)) {
331 DBG("failed to fork()...(%s)", strerror(errno));
335 gboolean netconfig_iface_wifi_launch_direct(NetconfigWifi *wifi, GError **error)
339 DBG("Launch Wi-Fi direct daemon");
341 const char *path = "/usr/bin/wifi-direct-server.sh";
342 char *const args[] = { "wifi-direct-server.sh", "start", NULL};
343 char *const env[] = { NULL };
345 ret = netconfig_execute_file(path, args, env);
348 INFO("Failed to launch Wi-Fi direct daemon");
350 netconfig_error_wifi_direct_failed(error);
356 void netconfig_add_wifi_found_notification(void)
358 INFO("Add wifi found notification");
361 void netconfig_del_wifi_found_notification(void)
363 INFO("Delete wifi found notification");
367 void netconfig_set_wifi_mac_address(void)
372 char buf[WIFI_MAC_INFO_LENGTH + 1];
375 mac_info = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
376 if (mac_info == NULL) {
377 ERR("Failed to open vconf key %s", VCONFKEY_WIFI_BSSID_ADDRESS);
380 INFO("%s : %s", VCONFKEY_WIFI_BSSID_ADDRESS, mac_info);
382 fp = fopen(WIFI_MAC_INFO_FILE, "r");
384 if (fgets(buf, sizeof(buf), fp) == NULL) {
385 ERR("Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
389 INFO("%s : %s", WIFI_MAC_INFO_FILE, buf);
391 if (strlen(buf) < WIFI_MAC_INFO_LENGTH) {
392 ERR("Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
396 buf[WIFI_MAC_INFO_LENGTH] = '\0';
398 // not MAC into file use ioctl to get MAC
399 ctl_sk = socket(PF_INET,SOCK_DGRAM,0);
401 ERR("Failed to open socket");
405 memset(&ifr, 0, sizeof(struct ifreq));
406 strncpy(ifr.ifr_name, WIFI_DEV_NAME, sizeof(ifr.ifr_name) - 1);
407 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = 0;
409 if (ioctl(ctl_sk, SIOCGIFHWADDR, &ifr) != 0) {
410 ERR("Failed to SIOCGIFHWADDR ioctl");
414 snprintf(buf, WIFI_MAC_INFO_LENGTH + 1,
415 "%02x:%02x:%02x:%02x:%02x:%02x",
416 (unsigned char)ifr.ifr_hwaddr.sa_data[0],
417 (unsigned char)ifr.ifr_hwaddr.sa_data[1],
418 (unsigned char)ifr.ifr_hwaddr.sa_data[2],
419 (unsigned char)ifr.ifr_hwaddr.sa_data[3],
420 (unsigned char)ifr.ifr_hwaddr.sa_data[4],
421 (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
423 INFO("%s MAC address: %s", WIFI_DEV_NAME, buf);
426 if (mac_info && (g_str_equal(mac_info, buf) == TRUE))
429 if (vconf_set_str(VCONFKEY_WIFI_BSSID_ADDRESS, buf) != 0)
430 ERR("Failed to set MAC info to %s", VCONFKEY_WIFI_BSSID_ADDRESS);