2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 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.
22 #include "netsupplicant.h"
24 #define DBUS_OBJECT_PATH_MAX 150
25 #define NETCONFIG_DBUS_REPLY_TIMEOUT (10 * 1000)
27 static void setup_dbus_args(gpointer data, gpointer user_data)
29 DBusMessageIter *iter;
30 struct dbus_input_arguments *args;
32 if (data == NULL || user_data == NULL)
35 iter = (DBusMessageIter *) user_data;
36 args = (struct dbus_input_arguments *)data;
37 if (args->data == NULL)
41 case DBUS_TYPE_STRING:
42 case DBUS_TYPE_OBJECT_PATH:
43 DBG("parameter [%s]", args->data);
44 dbus_message_iter_append_basic(iter, args->type, &(args->data));
46 case DBUS_TYPE_BOOLEAN:
47 case DBUS_TYPE_UINT32:
49 DBG("parameter [%d]", args->data);
50 dbus_message_iter_append_basic(iter, args->type, args->data);
52 case DBUS_TYPE_INVALID:
58 GList *setup_input_args(GList *list, struct dbus_input_arguments *items)
60 struct dbus_input_arguments *iter = items;
66 list = g_list_append(list, iter);
73 const char *netconfig_wifi_get_supplicant_interface(void)
75 GList *input_args = NULL;
76 DBusMessage *message = NULL;
77 struct dbus_input_arguments args[] = {
78 {DBUS_TYPE_STRING, WIFI_IFNAME},
79 {DBUS_TYPE_INVALID, NULL}
82 static char obj_path[DBUS_OBJECT_PATH_MAX] = { '\0', };
84 if (obj_path[0] != '\0')
85 return (const char *)obj_path;
87 input_args = setup_input_args(input_args, args);
89 message = netconfig_supplicant_invoke_dbus_method(
90 SUPPLICANT_SERVICE, SUPPLICANT_PATH,
91 SUPPLICANT_INTERFACE, "GetInterface", input_args);
93 g_list_free(input_args);
98 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR) {
99 const char *err_msg = dbus_message_get_error_name(message);
100 ERR("Error!!! Error message received %s", err_msg);
104 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
107 g_strlcpy(obj_path, path, DBUS_OBJECT_PATH_MAX);
109 dbus_message_unref(message);
111 return (const char *)obj_path;
115 dbus_message_unref(message);
120 DBusMessage *netconfig_supplicant_invoke_dbus_method(const char *dest,
121 const char *path, const char *interface_name,
122 const char *method, GList *args)
125 DBusMessageIter iter;
126 DBusMessage *reply = NULL;
127 DBusMessage *message = NULL;
128 DBusConnection *connection = NULL;
130 // DBG("[DBUS Sync] %s %s %s", interface_name, method, path);
132 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
133 if (connection == NULL) {
134 ERR("Failed to get system bus");
138 message = dbus_message_new_method_call(dest, path, interface_name, method);
139 if (message == NULL) {
140 ERR("Failed DBus method call");
141 dbus_connection_unref(connection);
145 dbus_message_iter_init_append(message, &iter);
148 g_list_foreach(args, setup_dbus_args, (gpointer)&iter);
150 dbus_error_init(&error);
152 reply = dbus_connection_send_with_reply_and_block(connection, message,
153 NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
156 if (dbus_error_is_set(&error) == TRUE) {
157 ERR("dbus_connection_send_with_reply_and_block() failed. "
158 "DBus error [%s: %s]", error.name, error.message);
160 dbus_error_free(&error);
162 ERR("Failed to get properties");
164 dbus_message_unref(message);
165 dbus_connection_unref(connection);
170 dbus_message_unref(message);
171 dbus_connection_unref(connection);
176 dbus_bool_t netconfig_supplicant_invoke_dbus_method_nonblock(const char *dest,
177 const char *path, const char *interface_name,
178 const char *method, GList *args,
179 DBusPendingCallNotifyFunction notify_func)
181 dbus_bool_t result = FALSE;
182 DBusMessageIter iter;
183 DBusPendingCall *call;
184 DBusMessage *message = NULL;
185 DBusConnection *connection = NULL;
187 DBG("[DBUS Async] %s %s %s", interface_name, method, path);
189 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
190 if (connection == NULL) {
191 ERR("Failed to get system bus");
195 message = dbus_message_new_method_call(dest, path, interface_name, method);
196 if (message == NULL) {
197 ERR("Failed DBus method call");
198 dbus_connection_unref(connection);
202 dbus_message_iter_init_append(message, &iter);
205 g_list_foreach(args, setup_dbus_args, (gpointer)&iter);
207 result = dbus_connection_send_with_reply(connection, message, &call,
208 NETCONFIG_DBUS_REPLY_TIMEOUT);
209 if (result == FALSE || call == NULL) {
210 ERR("dbus_connection_send_with_reply() failed");
212 dbus_message_unref(message);
213 dbus_connection_unref(connection);
218 if (notify_func == NULL)
219 dbus_pending_call_cancel(call);
221 dbus_pending_call_set_notify(call, notify_func, NULL, NULL);
223 dbus_message_unref(message);
224 dbus_connection_unref(connection);
229 DBusMessage *netconfig_supplicant_invoke_dbus_interface_property_get(const char *interface,
233 DBusMessage *reply = NULL;
234 DBusMessage *message = NULL;
235 DBusConnection *connection = NULL;
238 ERR("[DBUS] property_get : %s", key);
240 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
241 if (connection == NULL) {
242 ERR("Failed to get system bus");
246 path = netconfig_wifi_get_supplicant_interface();
248 DBG("Failed to get wpa_supplicant DBus path");
249 dbus_connection_unref(connection);
253 message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
254 DBUS_INTERFACE_PROPERTIES, "Get");
255 if (message == NULL) {
256 ERR("Failed DBus method call");
257 dbus_connection_unref(connection);
261 dbus_message_append_args(message, DBUS_TYPE_STRING, &interface,
262 DBUS_TYPE_STRING, &key, NULL);
264 dbus_error_init(&error);
266 reply = dbus_connection_send_with_reply_and_block(connection, message,
267 NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
269 if (dbus_error_is_set(&error) == TRUE) {
270 ERR("dbus_connection_send_with_reply_and_block() failed. "
271 "DBus error [%s: %s]", error.name, error.message);
273 dbus_error_free(&error);
275 ERR("Failed to get properties");
277 dbus_message_unref(message);
278 dbus_connection_unref(connection);
283 dbus_message_unref(message);
284 dbus_connection_unref(connection);
289 dbus_bool_t netconfig_supplicant_invoke_dbus_interface_property_set(const char *interface,
290 const char *key, const char *type, GList *args,
291 DBusPendingCallNotifyFunction notify_func)
293 dbus_bool_t result = FALSE;
294 DBusPendingCall *call;
295 DBusMessage *message = NULL;
296 DBusConnection *connection = NULL;
297 DBusMessageIter iter, value;
300 DBG("[DBUS] property_set : %s", key);
302 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
303 if (connection == NULL) {
304 ERR("Failed to get system bus");
308 path = netconfig_wifi_get_supplicant_interface();
310 ERR("Failed to get wpa_supplicant DBus path");
311 dbus_connection_unref(connection);
315 message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
316 DBUS_INTERFACE_PROPERTIES, "Set");
317 if (message == NULL) {
318 ERR("Failed DBus method call");
319 dbus_connection_unref(connection);
323 dbus_message_iter_init_append(message, &iter);
324 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface);
325 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
327 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
331 g_list_foreach(args, setup_dbus_args, (gpointer)&value);
333 dbus_message_iter_close_container(&iter, &value);
335 result = dbus_connection_send_with_reply(connection, message, &call,
336 NETCONFIG_DBUS_REPLY_TIMEOUT);
337 if (result == FALSE || call == NULL) {
338 ERR("dbus_connection_send_with_reply() failed");
340 dbus_message_unref(message);
341 dbus_connection_unref(connection);
346 if (notify_func == NULL)
347 dbus_pending_call_cancel(call);
349 dbus_pending_call_set_notify(call, notify_func, NULL, NULL);
351 dbus_message_unref(message);
352 dbus_connection_unref(connection);