4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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.
25 #include "core/common.h"
29 /* -1 is a default timeout value, it's converted to 25*1000 internally. */
30 #define DBUS_REPLY_TIMEOUT (-1)
32 struct pending_call_data {
37 int append_variant(DBusMessageIter *iter, const char *sig, char *param[])
44 struct dbus_byte *byte;
49 for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
52 int_type = atoi(param[i]);
53 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int_type);
56 int_type = strtoul(param[i], NULL, 10);
57 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &int_type);
60 int64_type = atoll(param[i]);
61 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &int64_type);
64 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, ¶m[i]);
70 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &arr);
71 byte = (struct dbus_byte*)param[i];
72 dbus_message_iter_append_fixed_array(&arr, DBUS_TYPE_BYTE, &(byte->data), byte->size);
73 dbus_message_iter_close_container(iter, &arr);
87 DBusMessage *dbus_method_sync_with_reply(const char *dest, const char *path,
88 const char *interface, const char *method,
89 const char *sig, char *param[])
98 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
100 _E("dbus_bus_get error");
104 msg = dbus_message_new_method_call(dest, path, interface, method);
106 _E("dbus_message_new_method_call(%s:%s-%s)",
107 path, interface, method);
111 dbus_message_iter_init_append(msg, &iter);
112 r = append_variant(&iter, sig, param);
114 _E("append_variant error(%d) %s %s:%s-%s",
115 r, dest, path, interface, method);
116 dbus_message_unref(msg);
120 dbus_error_init(&err);
122 reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
124 _E("dbus_connection_send error(No reply) %s %s:%s-%s",
125 dest, path, interface, method);
128 if (dbus_error_is_set(&err)) {
129 _E("dbus_connection_send error(%s:%s) %s %s:%s-%s",
130 err.name, err.message, dest, path, interface, method);
131 dbus_error_free(&err);
135 dbus_message_unref(msg);
139 int dbus_method_sync(const char *dest, const char *path,
140 const char *interface, const char *method,
141 const char *sig, char *param[])
143 DBusConnection *conn;
145 DBusMessageIter iter;
150 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
152 _E("dbus_bus_get error");
156 msg = dbus_message_new_method_call(dest, path, interface, method);
158 _E("dbus_message_new_method_call(%s:%s-%s)",
159 path, interface, method);
163 dbus_message_iter_init_append(msg, &iter);
164 ret = append_variant(&iter, sig, param);
166 _E("append_variant error(%d) %s %s:%s-%s",
167 ret, dest, path, interface, method);
168 dbus_message_unref(msg);
172 dbus_error_init(&err);
174 reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
175 dbus_message_unref(msg);
177 _E("dbus_connection_send error(%s:%s) %s %s:%s-%s",
178 err.name, err.message, dest, path, interface, method);
179 dbus_error_free(&err);
183 ret = dbus_message_get_args(reply, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
184 dbus_message_unref(reply);
186 _E("no message : [%s:%s] %s %s:%s-%s",
187 err.name, err.message, dest, path, interface, method);
188 dbus_error_free(&err);
195 int dbus_method_async(const char *dest, const char *path,
196 const char *interface, const char *method,
197 const char *sig, char *param[])
199 DBusConnection *conn;
201 DBusMessageIter iter;
204 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
206 _E("dbus_bus_get error");
210 msg = dbus_message_new_method_call(dest, path, interface, method);
212 _E("dbus_message_new_method_call(%s:%s-%s)",
213 path, interface, method);
217 dbus_message_iter_init_append(msg, &iter);
218 ret = append_variant(&iter, sig, param);
220 _E("append_variant error(%d) %s %s:%s-%s",
221 ret, dest, path, interface, method);
222 dbus_message_unref(msg);
226 ret = dbus_connection_send(conn, msg, NULL);
227 dbus_message_unref(msg);
229 _E("dbus_connection_send error(%s %s:%s-%s)",
230 dest, path, interface, method);
237 static void cb_pending(DBusPendingCall *pending, void *user_data)
241 struct pending_call_data *data = user_data;
244 ret = dbus_pending_call_get_completed(pending);
246 _I("dbus_pending_call_get_completed() fail");
248 dbus_pending_call_unref(pending);
252 dbus_error_init(&err);
253 msg = dbus_pending_call_steal_reply(pending);
256 dbus_set_error(&err, "org.tizen.system.deviced.NoReply",
257 "There was no reply to this method call");
258 data->func(data->data, NULL, &err);
259 dbus_error_free(&err);
264 ret = dbus_set_error_from_message(&err, msg);
267 data->func(data->data, NULL, &err);
268 dbus_error_free(&err);
271 data->func(data->data, msg, &err);
274 dbus_message_unref(msg);
275 dbus_pending_call_unref(pending);
278 int dbus_method_async_with_reply(const char *dest, const char *path,
279 const char *interface, const char *method,
280 const char *sig, char *param[], dbus_pending_cb cb, int timeout, void *data)
282 DBusConnection *conn;
284 DBusMessageIter iter;
285 DBusPendingCall *pending = NULL;
286 struct pending_call_data *pdata;
289 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
291 _E("dbus_bus_get error");
295 msg = dbus_message_new_method_call(dest, path, interface, method);
297 _E("dbus_message_new_method_call(%s:%s-%s)",
298 path, interface, method);
302 dbus_message_iter_init_append(msg, &iter);
303 ret = append_variant(&iter, sig, param);
305 _E("append_variant error(%d)%s %s:%s-%s",
306 ret, dest, path, interface, method);
307 dbus_message_unref(msg);
311 ret = dbus_connection_send_with_reply(conn, msg, &pending, timeout);
313 dbus_message_unref(msg);
314 _E("dbus_connection_send error(%s %s:%s-%s)",
315 dest, path, interface, method);
320 pdata = malloc(sizeof(struct pending_call_data));
327 ret = dbus_pending_call_set_notify(pending, cb_pending, pdata, free);
330 dbus_message_unref(msg);
331 dbus_pending_call_cancel(pending);
339 static void __CONSTRUCTOR__ dbus_init(void)
341 dbus_threads_init_default();