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-agent.h"
28 #define NETCONFIG_AGENT_FIELD_PASSPHRASE "Passphrase"
29 #define NETCONFIG_AGENT_FIELD_WPS "WPS"
30 #define NETCONFIG_AGENT_FIELD_WPS_PBC "WPS_PBC"
31 #define NETCONFIG_AGENT_FIELD_WPS_PIN "WPS_PIN"
33 struct netconfig_wifi_agent {
39 static struct netconfig_wifi_agent agent;
41 static void __netconfig_agent_clear_fields(void)
43 DBG("__netconfig_agent_clear_fields");
45 g_free(agent.passphrase);
46 g_free(agent.wps_pin);
48 agent.passphrase = NULL;
50 agent.wps_pbc = FALSE;
53 gboolean netconfig_agent_register(void)
55 DBG("netconfig_agent_register");
57 DBusMessage *reply = NULL;
59 char *param_array[] = {NULL, NULL};
61 snprintf(param1, 64, "objpath:%s", NETCONFIG_WIFI_PATH);
62 param_array[0] = param1;
64 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
65 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
66 "RegisterAgent", param_array);
69 ERR("Error! Request failed");
73 dbus_message_unref(reply);
78 gboolean netconfig_agent_unregister(void)
80 DBG("netconfig_agent_unregister");
82 DBusMessage *reply = NULL;
84 char *param_array[] = {NULL, NULL};
86 snprintf(param1, 64, "objpath:%s", NETCONFIG_WIFI_PATH);
87 param_array[0] = param1;
89 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
90 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
91 "UnregisterAgent", param_array);
94 ERR("Error! Request failed");
98 dbus_message_unref(reply);
100 /* Clearing the agent fields */
101 __netconfig_agent_clear_fields();
106 gboolean netconfig_iface_wifi_set_field(NetconfigWifi *wifi,
107 GHashTable *fields, GError **error)
110 gpointer field, value;
112 DBG("Set agent fields");
114 g_return_val_if_fail(wifi != NULL, FALSE);
116 __netconfig_agent_clear_fields();
118 g_hash_table_iter_init(&iter, fields);
120 while (g_hash_table_iter_next(&iter, &field, &value)) {
121 DBG("Field - [%s]", field);
122 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0) {
123 g_free(agent.passphrase);
124 agent.passphrase = g_strdup(value);
126 DBG("Field [%s] - []", field);
127 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PBC) == 0) {
128 agent.wps_pbc = FALSE;
129 if (g_strcmp0(value, "enable") == 0)
130 agent.wps_pbc = TRUE;
132 DBG("Field [%s] - [%d]", field, agent.wps_pbc);
133 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PIN) == 0) {
134 g_free(agent.wps_pin);
135 agent.wps_pbc = FALSE;
136 agent.wps_pin = g_strdup(value);
138 DBG("Field [%s] - []", field);
145 gboolean netconfig_iface_wifi_request_input(NetconfigWifi *wifi,
146 gchar *service, GHashTable *fields,
147 DBusGMethodInvocation *context)
150 gpointer field, value;
151 GHashTable *out_table = NULL;
152 GValue *ret_value = NULL;
154 g_return_val_if_fail(wifi != NULL, FALSE);
159 DBG("Agent fields requested for service: %s", service);
161 out_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
162 if (NULL == out_table)
165 g_hash_table_iter_init(&iter, fields);
167 while (g_hash_table_iter_next(&iter, &field, &value)) {
168 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0 &&
169 agent.passphrase != NULL) {
170 ret_value = g_slice_new0(GValue);
172 g_value_init(ret_value, G_TYPE_STRING);
173 g_value_set_string(ret_value, agent.passphrase);
174 g_hash_table_insert(out_table, g_strdup(field), ret_value);
176 DBG("Setting [%s] - []", field);
177 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS) == 0 &&
178 (agent.wps_pbc == TRUE || agent.wps_pin != NULL)) {
179 ret_value = g_slice_new0(GValue);
181 g_value_init(ret_value, G_TYPE_STRING);
183 if (agent.wps_pbc == TRUE) {
184 /* Sending empty string for WPS push button method */
185 g_value_set_string(ret_value, "");
187 DBG("Setting empty string for [%s]", field);
188 } else if (agent.wps_pin != NULL) {
189 g_value_set_string(ret_value, agent.wps_pin);
191 DBG("Setting string [%s] - []", field);
194 g_hash_table_insert(out_table, g_strdup(field), ret_value);
198 dbus_g_method_return(context, out_table);
200 __netconfig_agent_clear_fields();