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.
22 #include "netsupplicant.h"
24 #define NETCONFIG_DBUS_REPLY_TIMEOUT (10 * 1000)
26 static void setup_dbus_args(gpointer data, gpointer user_data)
28 struct dbus_input_arguments *args;
29 DBusMessageIter *iter;
31 if (data != NULL && user_data != NULL) {
32 args = (struct dbus_input_arguments *)data;
33 iter = (DBusMessageIter *) user_data;
35 dbus_message_iter_append_basic(iter, args->type,
40 static GList *setup_input_args(GList *list,
41 struct dbus_input_arguments *items)
43 struct dbus_input_arguments *iter = items;
49 list = g_list_append(list, iter);
56 gboolean netconfig_wifi_get_ifname(char **ifname)
58 DBusConnection *connection = NULL;
59 DBusMessage *message = NULL;
62 char *ptr = (char *)*ifname;
63 const char *temp = NULL;
65 char object_path[DBUS_PATH_MAX_BUFLEN] = { 0, };
66 char *path_ptr = &object_path[0];
68 GList *input_args = NULL;
69 struct dbus_input_arguments args[] = {
70 {DBUS_TYPE_STRING, SUPPLICANT_INTERFACE ".Interface"},
71 {DBUS_TYPE_STRING, "Ifname"},
78 if (netconfig_wifi_get_supplicant_interface(&path_ptr) != TRUE) {
79 DBG("Fail to get wpa_supplicant DBus path");
83 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
84 if (connection == NULL) {
85 ERR("Error!!! Fail to get system DBus");
89 input_args = setup_input_args(input_args, args);
91 message = netconfig_supplicant_invoke_dbus_method(
92 SUPPLICANT_SERVICE, connection,
94 SUPPLICANT_GLOBAL_INTERFACE, "Get",
97 g_list_free(input_args);
99 if (message == NULL) {
100 ERR("Error!!! Failed to get service properties");
104 MessageType = dbus_message_get_type(message);
106 if (MessageType == DBUS_MESSAGE_TYPE_ERROR) {
107 const char *err_ptr = dbus_message_get_error_name(message);
108 ERR("Error!!! Error message received %s", err_ptr);
112 dbus_message_iter_init(message, &iter);
114 if ((MessageType = dbus_message_iter_get_arg_type(&iter))
115 == DBUS_TYPE_VARIANT) {
116 DBusMessageIter string_type;
117 dbus_message_iter_recurse(&iter, &string_type);
119 if ((MessageType = dbus_message_iter_get_arg_type(&string_type))
120 == DBUS_TYPE_STRING) {
121 dbus_message_iter_get_basic(&string_type, &temp);
127 g_strlcpy(ptr, temp, 16);
129 dbus_message_unref(message);
130 dbus_connection_unref(connection);
136 dbus_message_unref(message);
138 if (connection != NULL)
139 dbus_connection_unref(connection);
144 gboolean netconfig_wifi_get_supplicant_interface(char **path)
146 DBusConnection *connection = NULL;
147 DBusMessage *message = NULL;
148 DBusMessageIter iter;
150 char *ptr = (char *)*path;
151 const char *temp = NULL;
153 GList *input_args = NULL;
154 struct dbus_input_arguments args[] = {
155 {DBUS_TYPE_STRING, SUPPLICANT_INTERFACE},
156 {DBUS_TYPE_STRING, "Interfaces"},
163 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
164 if (connection == NULL) {
165 ERR("Error!!! Fail to get system DBus");
169 input_args = setup_input_args(input_args, args);
171 message = netconfig_supplicant_invoke_dbus_method(
172 SUPPLICANT_SERVICE, connection,
174 SUPPLICANT_GLOBAL_INTERFACE, "Get",
177 g_list_free(input_args);
179 if (message == NULL) {
180 ERR("Error!!! Failed to get service properties");
184 MessageType = dbus_message_get_type(message);
186 if (MessageType == DBUS_MESSAGE_TYPE_ERROR) {
187 const char *err_msg = dbus_message_get_error_name(message);
188 ERR("Error!!! Error message received %s", err_msg);
192 dbus_message_iter_init(message, &iter);
193 if ((MessageType = dbus_message_iter_get_arg_type(&iter))
194 == DBUS_TYPE_VARIANT) {
195 DBusMessageIter array;
196 dbus_message_iter_recurse(&iter, &array);
198 if ((MessageType = dbus_message_iter_get_arg_type(&array))
199 == DBUS_TYPE_ARRAY) {
200 DBusMessageIter object_path;
201 dbus_message_iter_recurse(&array, &object_path);
203 if ((MessageType = dbus_message_iter_get_arg_type(&object_path))
204 == DBUS_TYPE_OBJECT_PATH)
205 dbus_message_iter_get_basic(&object_path, &temp);
213 g_strlcpy(ptr, temp, DBUS_PATH_MAX_BUFLEN);
215 dbus_message_unref(message);
216 dbus_connection_unref(connection);
222 dbus_message_unref(message);
224 if (connection != NULL)
225 dbus_connection_unref(connection);
230 DBusMessage *netconfig_supplicant_invoke_dbus_method(const char *dest,
231 DBusConnection *connection,
232 const char *path, const char *interface_name,
233 const char *method, GList *args)
236 DBusMessageIter iter;
237 DBusMessage *reply = NULL;
238 DBusMessage *message = NULL;
240 message = dbus_message_new_method_call(dest, path, interface_name, method);
241 if (message == NULL) {
242 ERR("Error!!! DBus method call fail");
246 dbus_message_iter_init_append(message, &iter);
249 g_list_foreach(args, setup_dbus_args, (gpointer) &iter);
251 dbus_error_init(&error);
253 reply = dbus_connection_send_with_reply_and_block(connection, message,
254 NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
257 if (dbus_error_is_set(&error) == TRUE) {
258 ERR("Error!!! dbus_connection_send_with_reply_and_block() failed. DBus error [%s: %s]",
259 error.name, error.message);
261 dbus_error_free(&error);
263 ERR("Error!!! Failed to get properties");
265 dbus_message_unref(message);
270 dbus_message_unref(message);