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.
23 #include "wifi-eap-config.h"
27 static char *__get_ssid(const char *name)
30 char buf_tmp[32] = {0,};
39 buf = g_try_malloc0(len * 2 + 1);
43 for (i = 0; i < len; i++) {
44 snprintf(buf_tmp, 3, "%02x", name[i]);
48 DBG("SSID - [%s]\n", buf);
53 static gboolean __config_save(GKeyFile *keyfile, char *file_name)
60 data = g_key_file_to_data(keyfile, &length, NULL);
61 DBG("Data lenght-[%d]", length);
63 file = fopen(file_name, "w");
65 DBG("fopen() fails!");
70 DBG("Wrote data successfully to [%s] file!", file_name);
78 static gboolean __config_delete(const char *ssid)
80 gchar *config_file = NULL;
83 config_file = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR,
85 if(config_file == NULL)
88 if (g_file_test(config_file, G_FILE_TEST_EXISTS) == FALSE) {
90 } else if (g_file_test(config_file, G_FILE_TEST_IS_REGULAR) == TRUE) {
100 gboolean netconfig_iface_wifi_create_config(NetconfigWifi *wifi,
101 GHashTable *fields, GError **error)
103 DBG("netconfig_iface_wifi_create_config");
104 g_return_val_if_fail(wifi != NULL, FALSE);
107 GKeyFile *keyfile = NULL;
109 gpointer field, value;
110 gchar *file_name = NULL;
111 gchar *ssid_hex = NULL;
112 gchar *grp_name = NULL;
114 g_hash_table_iter_init(&iter, fields);
115 while (g_hash_table_iter_next(&iter, &field, &value)) {
117 if (!strcmp(field, CONNMAN_CONFIG_FIELD_NAME)) {
118 ssid_hex = __get_ssid(value);
120 } else if (!strcmp(field, CONNMAN_CONFIG_FIELD_SSID)) {
121 ssid_hex = g_strdup_printf("%s",
128 if (NULL == ssid_hex) {
129 DBG("Fail! Could not fetch the ssid");
133 /* Create unique service group name */
134 grp_name = g_strdup_printf("service_%s", ssid_hex);
135 if(NULL == grp_name) {
136 DBG("Fail! Could not create the service group name");
141 keyfile = g_key_file_new();
142 if (NULL == keyfile) {
143 DBG("g_key_file_new() fails!");
149 g_hash_table_iter_init(&iter, fields);
150 while (g_hash_table_iter_next(&iter, &field, &value)) {
151 DBG("Field - [%s] Value - [%s]", field, value);
154 g_key_file_set_string(keyfile, grp_name, field, value);
157 file_name = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR,
159 if(NULL == file_name) {
160 DBG("g_strdup_printf() fails. Could not save config!");
161 g_key_file_free(keyfile);
167 ret = __config_save(keyfile, file_name);
169 DBG("Could not save config!");
171 DBG("Saved config in [%s] successfully", file_name);
173 g_key_file_free(keyfile);
181 gboolean netconfig_iface_wifi_delete_config(NetconfigWifi *wifi,
182 gchar *profile, GError **error)
184 DBG("netconfig_iface_wifi_delete_config");
185 g_return_val_if_fail(wifi != NULL, FALSE);
194 str1 = strstr(profile, "wifi_");
196 str2 = strchr(str1 + 5, '_');
198 str3 = strchr(str2 + 1, '_');
199 ssid_len = str3 - str2 - 1;
200 strncpy(ssid, str2 + 1, ssid_len);
201 DBG("ssid_len - [%d] SSID - [%s]", ssid_len, ssid);
203 ret = __config_delete(ssid);
205 DBG("Deleted the config file successfully");
207 DBG("Deletion of config file failed");
209 DBG("Fetching of SSID fails");
212 DBG("Fetching of SSID fails");