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.
25 #include <vconf-keys.h>
26 #include <wifi-direct.h>
27 #include <syspopup_caller.h>
33 #include "wifi-state.h"
35 #define WIFI_MAC_INFO_FILE "/opt/etc/.mac.info"
36 #define WIFI_MAC_INFO_LENGTH 17
38 GKeyFile *netconfig_keyfile_load(const char *pathname)
40 GKeyFile *keyfile = NULL;
43 keyfile = g_key_file_new();
44 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
45 DBG("Unable to open %s, error %s", pathname, error->message);
48 g_key_file_free(keyfile);
55 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
59 gchar *keydata = NULL;
60 gchar *needle = NULL, *directory = NULL;
62 directory = g_strdup(pathname);
63 needle = g_strrstr(directory, "/");
68 if (directory == NULL || (*directory) == '\0') {
73 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
74 if (g_mkdir_with_parents(directory,
75 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
82 keydata = g_key_file_to_data(keyfile, &size, &error);
83 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
84 DBG("Unable to save %s, error %s", pathname, error->message);
88 if (chmod(pathname, S_IRUSR | S_IWUSR) != 0)
89 DBG("Unable to change permission of %s", pathname);
93 g_key_file_free(keyfile);
96 void netconfig_start_timer_seconds(guint secs,
97 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
101 if (callback == NULL) {
102 ERR("callback function is NULL");
106 if ((timer_id != NULL && *timer_id != 0)) {
107 ERR("timer already is registered");
111 t_id = g_timeout_add_seconds(secs, callback, user_data);
114 ERR("Can't add timer");
118 if (timer_id != NULL)
122 void netconfig_start_timer(guint msecs,
123 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
127 INFO("Register timer with callback pointer (%p)", callback);
129 if (callback == NULL) {
130 ERR("callback function is NULL");
134 if ((timer_id != NULL && *timer_id != 0)) {
135 ERR("timer already is registered");
139 t_id = g_timeout_add(msecs, callback, user_data);
142 ERR("Can't add timer");
146 if (timer_id != NULL)
150 void netconfig_stop_timer(guint *timer_id)
152 if (timer_id == NULL) {
153 ERR("timer is NULL");
157 if (*timer_id != 0) {
158 g_source_remove(*timer_id);
163 static gboolean __netconfig_test_device_picker()
165 char *favorite_wifi_service = NULL;
167 favorite_wifi_service = netconfig_wifi_get_favorite_service();
168 if (favorite_wifi_service != NULL) {
169 g_free(favorite_wifi_service);
176 static void __netconfig_pop_device_picker(void)
180 int wifi_ug_state = 0;
182 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
183 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
188 DBG("Launch Wi-Fi device picker");
189 rv = syspopup_launch("wifi-qs", b);
194 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
196 if (__netconfig_test_device_picker() == TRUE)
197 __netconfig_pop_device_picker();
202 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method,
205 static guint netconfig_wifi_device_picker_service_timer = 0;
207 if (is_set_method != TRUE)
208 return netconfig_wifi_device_picker_service_timer;
210 if (netconfig_wifi_device_picker_service_timer != timer_id)
211 netconfig_wifi_device_picker_service_timer = timer_id;
213 return netconfig_wifi_device_picker_service_timer;
216 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
218 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
221 static guint __netconfig_wifi_device_picker_get_timer_id(void)
223 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
226 void netconfig_wifi_device_picker_service_start(void)
229 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
230 int hotspot_wifi_state = VCONFKEY_MOBILE_HOTSPOT_WIFI_OFF;
233 vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
234 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
237 /* If Wi-Fi tethering is pending on, don't show device picker UI*/
238 vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_WIFI_STATE, &hotspot_wifi_state);
239 if (hotspot_wifi_state == VCONFKEY_MOBILE_HOTSPOT_WIFI_PENDING_ON) {
240 DBG("hotspot_wifi_state %d", hotspot_wifi_state);
244 DBG("Register device picker timer with %d milliseconds",
245 NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
247 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL,
248 __netconfig_wifi_try_device_picker, NULL, &timer_id);
250 __netconfig_wifi_device_picker_set_timer_id(timer_id);
253 void netconfig_wifi_device_picker_service_stop(void)
257 timer_id = __netconfig_wifi_device_picker_get_timer_id();
261 DBG("Clear device picker timer with timer_id %d", timer_id);
263 netconfig_stop_timer(&timer_id);
265 __netconfig_wifi_device_picker_set_timer_id(timer_id);
268 gboolean netconfig_is_wifi_direct_on(void)
270 int wifi_direct_state = 0;
272 vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
274 DBG("Wi-Fi direct mode %d", wifi_direct_state);
275 return (wifi_direct_state != 0) ? TRUE : FALSE;
278 gboolean netconfig_is_wifi_tethering_on(void)
280 int wifi_tethering_state = 0;
282 vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
284 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
285 if (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
291 /* args[] and env[] should be terminated with NULL pointer */
292 gboolean netconfig_execute_file(const char *file_path,
293 char *const args[], char *const env[])
299 if (!(pid = fork())) {
300 register unsigned int index = 0;
301 INFO("pid(%d), ppid (%d)", getpid(), getppid());
302 INFO("Inside child, exec (%s) command", file_path);
305 while (args[index] != NULL) {
306 INFO(" %s", args[index]);
311 if (execve(file_path, args, env) == -1) {
312 DBG("Fail to execute command...(%s)",
316 } else if (pid > 0) {
317 if (waitpid(pid, &rv, 0) == -1) {
318 DBG("wait pid (%u) rv (%d)", pid, rv);
321 DBG("exited, rv=%d", WEXITSTATUS(rv));
322 } else if (WIFSIGNALED(rv)) {
323 DBG("killed by signal %d", WTERMSIG(rv));
324 } else if (WIFSTOPPED(rv)) {
325 DBG("stopped by signal %d", WSTOPSIG(rv));
326 } else if (WIFCONTINUED(rv)) {
333 DBG("failed to fork()...(%s)", strerror(errno));
337 gboolean netconfig_iface_wifi_launch_direct(NetconfigWifi *wifi, GError **error)
341 DBG("Launch Wi-Fi direct daemon");
343 const char *path = "/usr/bin/wifi-direct-server.sh";
344 char *const args[] = { "wifi-direct-server.sh", "start", NULL};
345 char *const env[] = { NULL };
347 ret = netconfig_execute_file(path, args, env);
350 INFO("Failed to launch Wi-Fi direct daemon");
352 netconfig_error_wifi_direct_failed(error);
358 void netconfig_add_wifi_found_notification(void)
361 bundle *b = bundle_create();
363 bundle_add(b, "_SYSPOPUP_TYPE_", "add_found_ap_noti");
365 ret = aul_launch_app("org.tizen.net-popup", b);
370 DBG("Successfully added notification");
372 ERR("Unable to launch noti-popup. Err = %d", ret);
375 void netconfig_del_wifi_found_notification(void)
378 bundle *b = bundle_create();
380 bundle_add(b, "_SYSPOPUP_TYPE_", "del_found_ap_noti");
382 ret = aul_launch_app("org.tizen.net-popup", b);
387 DBG("Successfully deleted notification");
389 ERR("Unable to launch noti-popup. Err = %d", ret);
393 void netconfig_set_wifi_mac_address(void)
396 char buf[WIFI_MAC_INFO_LENGTH + 1];
399 mac_info = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
400 if (mac_info == NULL) {
401 ERR("Failed to open vconf key %s", VCONFKEY_WIFI_BSSID_ADDRESS);
405 INFO("%s : %s", VCONFKEY_WIFI_BSSID_ADDRESS, mac_info);
407 fp = fopen(WIFI_MAC_INFO_FILE, "r");
409 ERR("Failed to open file %s", WIFI_MAC_INFO_FILE);
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 if (g_str_equal(mac_info, buf) == TRUE)
431 if (vconf_set_str(VCONFKEY_WIFI_BSSID_ADDRESS, buf) != 0)
432 ERR("Failed to set MAC info to %s", VCONFKEY_WIFI_BSSID_ADDRESS);