Initialize Tizen 2.3
[framework/connectivity/net-config.git] / wearable / src / dbus / netsupplicant.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "log.h"
21 #include "netdbus.h"
22 #include "netsupplicant.h"
23
24 #define DBUS_OBJECT_PATH_MAX                    150
25 #define NETCONFIG_DBUS_REPLY_TIMEOUT    (10 * 1000)
26
27 static void setup_dbus_args(gpointer data, gpointer user_data)
28 {
29         DBusMessageIter *iter;
30         struct dbus_input_arguments *args;
31
32         if (data == NULL || user_data == NULL)
33                 return;
34
35         iter = (DBusMessageIter *) user_data;
36         args = (struct dbus_input_arguments *)data;
37         if (args->data == NULL)
38                 return;
39
40         switch (args->type) {
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));
45                 break;
46         case DBUS_TYPE_BOOLEAN:
47         case DBUS_TYPE_UINT32:
48         case DBUS_TYPE_INT32:
49                 DBG("parameter [%d]", args->data);
50                 dbus_message_iter_append_basic(iter, args->type, args->data);
51                 break;
52         case DBUS_TYPE_INVALID:
53         default:
54                 return;
55         }
56 }
57
58 GList *setup_input_args(GList *list, struct dbus_input_arguments *items)
59 {
60         struct dbus_input_arguments *iter = items;
61
62         if (iter == NULL)
63                 return NULL;
64
65         while (iter->data) {
66                 list = g_list_append(list, iter);
67                 iter++;
68         }
69
70         return list;
71 }
72
73 const char *netconfig_wifi_get_supplicant_interface(void)
74 {
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}
80         };
81         const char *path;
82         static char obj_path[DBUS_OBJECT_PATH_MAX] = { '\0', };
83
84         if (obj_path[0] != '\0')
85                 return (const char *)obj_path;
86
87         input_args = setup_input_args(input_args, args);
88
89         message = netconfig_supplicant_invoke_dbus_method(
90                         SUPPLICANT_SERVICE, SUPPLICANT_PATH,
91                         SUPPLICANT_INTERFACE, "GetInterface", input_args);
92
93         g_list_free(input_args);
94
95         if (message == NULL)
96                 return NULL;
97
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);
101                 goto error;
102         }
103
104         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
105                                                         DBUS_TYPE_INVALID);
106
107         g_strlcpy(obj_path, path, DBUS_OBJECT_PATH_MAX);
108
109         dbus_message_unref(message);
110
111         return (const char *)obj_path;
112
113 error:
114         if (message != NULL)
115                 dbus_message_unref(message);
116
117         return NULL;
118 }
119
120 DBusMessage *netconfig_supplicant_invoke_dbus_method(const char *dest,
121                 const char *path, const char *interface_name,
122                 const char *method, GList *args)
123 {
124         DBusError error;
125         DBusMessageIter iter;
126         DBusMessage *reply = NULL;
127         DBusMessage *message = NULL;
128         DBusConnection *connection = NULL;
129
130 //      DBG("[DBUS Sync] %s %s %s", interface_name, method, path);
131
132         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
133         if (connection == NULL) {
134                 ERR("Failed to get system bus");
135                 return NULL;
136         }
137
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);
142                 return NULL;
143         }
144
145         dbus_message_iter_init_append(message, &iter);
146
147         if (args != NULL)
148                 g_list_foreach(args, setup_dbus_args, (gpointer)&iter);
149
150         dbus_error_init(&error);
151
152         reply = dbus_connection_send_with_reply_and_block(connection, message,
153                         NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
154
155         if (reply == NULL) {
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);
159
160                         dbus_error_free(&error);
161                 } else
162                         ERR("Failed to get properties");
163
164                 dbus_message_unref(message);
165                 dbus_connection_unref(connection);
166
167                 return NULL;
168         }
169
170         dbus_message_unref(message);
171         dbus_connection_unref(connection);
172
173         return reply;
174 }
175
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)
180 {
181         dbus_bool_t result = FALSE;
182         DBusMessageIter iter;
183         DBusPendingCall *call;
184         DBusMessage *message = NULL;
185         DBusConnection *connection = NULL;
186
187         DBG("[DBUS Async] %s %s %s", interface_name, method, path);
188
189         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
190         if (connection == NULL) {
191                 ERR("Failed to get system bus");
192                 return result;
193         }
194
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);
199                 return result;
200         }
201
202         dbus_message_iter_init_append(message, &iter);
203
204         if (args != NULL)
205                 g_list_foreach(args, setup_dbus_args, (gpointer)&iter);
206
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");
211
212                 dbus_message_unref(message);
213                 dbus_connection_unref(connection);
214
215                 return result;
216         }
217
218         if (notify_func == NULL)
219                 dbus_pending_call_cancel(call);
220         else
221                 dbus_pending_call_set_notify(call, notify_func, NULL, NULL);
222
223         dbus_message_unref(message);
224         dbus_connection_unref(connection);
225
226         return result;
227 }
228
229 DBusMessage *netconfig_supplicant_invoke_dbus_interface_property_get(const char *interface,
230                         const char *key)
231 {
232         DBusError error;
233         DBusMessage *reply = NULL;
234         DBusMessage *message = NULL;
235         DBusConnection *connection = NULL;
236         const char *path;
237
238         ERR("[DBUS] property_get : %s", key);
239
240         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
241         if (connection == NULL) {
242                 ERR("Failed to get system bus");
243                 return NULL;
244         }
245
246         path = netconfig_wifi_get_supplicant_interface();
247         if (path == NULL) {
248                 DBG("Failed to get wpa_supplicant DBus path");
249                 dbus_connection_unref(connection);
250                 return NULL;
251         }
252
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);
258                 return NULL;
259         }
260
261         dbus_message_append_args(message, DBUS_TYPE_STRING, &interface,
262                                         DBUS_TYPE_STRING, &key, NULL);
263
264         dbus_error_init(&error);
265
266         reply = dbus_connection_send_with_reply_and_block(connection, message,
267                                 NETCONFIG_DBUS_REPLY_TIMEOUT, &error);
268         if (reply == NULL) {
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);
272
273                         dbus_error_free(&error);
274                 } else
275                         ERR("Failed to get properties");
276
277                 dbus_message_unref(message);
278                 dbus_connection_unref(connection);
279
280                 return NULL;
281         }
282
283         dbus_message_unref(message);
284         dbus_connection_unref(connection);
285
286         return reply;
287 }
288
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)
292 {
293         dbus_bool_t result = FALSE;
294         DBusPendingCall *call;
295         DBusMessage *message = NULL;
296         DBusConnection *connection = NULL;
297         DBusMessageIter iter, value;
298         const char *path;
299
300         DBG("[DBUS] property_set : %s", key);
301
302         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
303         if (connection == NULL) {
304                 ERR("Failed to get system bus");
305                 return result;
306         }
307
308         path = netconfig_wifi_get_supplicant_interface();
309         if (path == NULL) {
310                 ERR("Failed to get wpa_supplicant DBus path");
311                 dbus_connection_unref(connection);
312                 return result;
313         }
314
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);
320                 return result;
321         }
322
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);
326
327         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
328                                         type, &value);
329
330         if (args != NULL)
331                 g_list_foreach(args, setup_dbus_args, (gpointer)&value);
332
333         dbus_message_iter_close_container(&iter, &value);
334
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");
339
340                 dbus_message_unref(message);
341                 dbus_connection_unref(connection);
342
343                 return result;
344         }
345
346         if (notify_func == NULL)
347                 dbus_pending_call_cancel(call);
348         else
349                 dbus_pending_call_set_notify(call, notify_func, NULL, NULL);
350
351         dbus_message_unref(message);
352         dbus_connection_unref(connection);
353
354         return result;
355 }