1 /**************************************************************************
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
7 Contact: Boram Park <boram1288.park@samsung.com>
8 Sangjin LEE <lsj119@samsung.com>
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
36 #include <sys/types.h>
39 #include "xdbg_types.h"
40 #include "xdbg_dbus_client.h"
42 #define REPLY_TIME 1000
45 struct _XDbgDBusClientInfo
48 char reqname[STR_LEN];
53 static DBusHandlerResult
54 _xDbgDBusClinetMsgFilter (DBusConnection *conn, DBusMessage *msg, void *data)
56 XDbgDBusClientInfo *info = (XDbgDBusClientInfo*)data;
58 /* If we get disconnected, then take everything down, and attempt to
59 * reconnect immediately (assuming it's just a restart). The
60 * connection isn't valid at this point, so throw it out immediately. */
61 if (dbus_message_is_signal (msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
63 XDBG_LOG ("[CLIENT:%s] disconnected by signal\n", info->client);
66 return DBUS_HANDLER_RESULT_HANDLED;
69 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
73 _xDbgDBusClinetInit (XDbgDBusClientInfo *info)
78 dbus_error_init (&err);
79 RETURN_VAL_IF_FAIL (info->conn == NULL, FALSE);
81 info->conn = dbus_bus_get (DBUS_BUS_SYSTEM, &err);
82 if (dbus_error_is_set (&err))
84 XDBG_LOG ("[CLIENT:%s] failed: connection (%s)\n", info->client, err.message);
89 XDBG_LOG ("[CLIENT:%s] failed: connection NULL\n", info->client);
93 dbus_connection_set_exit_on_disconnect (info->conn, FALSE);
95 if (!dbus_connection_add_filter (info->conn, _xDbgDBusClinetMsgFilter, info, NULL))
97 XDBG_LOG ("[CLIENT:%s] failed: add filter (%s)\n", info->client, err.message);
101 ret = dbus_bus_request_name (info->conn, info->reqname,
102 DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
103 if (dbus_error_is_set (&err))
105 XDBG_LOG ("[CLIENT:%s] failed: request name (%s)\n", info->client, err.message);
108 if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
110 XDBG_LOG ("[CLIENT:%s] failed: Not Primary Owner (%d)\n", info->client, ret);
114 dbus_error_free (&err);
116 // XDBG_LOG ("[CLIENT:%s] connected\n", info->client);
121 dbus_connection_remove_filter (info->conn, _xDbgDBusClinetMsgFilter, info);
126 dbus_connection_unref (info->conn);
130 dbus_error_free (&err);
137 _xDbgDBusClinetDeinit (XDbgDBusClientInfo *info)
144 dbus_error_init (&err);
145 dbus_bus_release_name (info->conn, info->reqname, &err);
146 if (dbus_error_is_set (&err))
147 XDBG_LOG ("[CLIENT:%s] failed: release name (%s)\n", info->client, err.message);
148 dbus_error_free (&err);
150 dbus_connection_remove_filter (info->conn, _xDbgDBusClinetMsgFilter, info);
151 dbus_connection_unref (info->conn);
154 // XDBG_LOG ("[CLIENT:%s] disconnected\n", info->client);
158 xDbgDBusClientConnect (void)
160 XDbgDBusClientInfo *info = NULL;
162 info = calloc (1, sizeof (XDbgDBusClientInfo));
163 GOTO_IF_FAIL (info != NULL, err_conn);
165 snprintf (info->client, STR_LEN, "%d", getpid());
166 snprintf (info->reqname, STR_LEN, "%s%d", XDBG_DBUS_CLIENT, getpid());
168 if (!_xDbgDBusClinetInit (info))
181 xDbgDBusClientDisconnect (XDbgDBusClientInfo* info)
186 _xDbgDBusClinetDeinit (info);
192 xDbugDBusClientSendMessage (XDbgDBusClientInfo *info, int argc, char **argv)
194 DBusMessage *msg = NULL;
195 DBusMessage *reply_msg = NULL;
196 DBusMessageIter iter;
201 RETURN_IF_FAIL (info != NULL);
202 RETURN_IF_FAIL (info->conn != NULL);
203 RETURN_IF_FAIL (argc > 0);
204 RETURN_IF_FAIL (argv[0] != '\0');
206 dbus_error_init (&err);
208 msg = dbus_message_new_method_call (XDBG_DBUS_SERVER, XDBG_DBUS_PATH,
209 XDBG_DBUS_INTERFACE, XDBG_DBUS_METHOD);
210 GOTO_IF_FAIL (msg != NULL, err_send);
212 dbus_message_iter_init_append (msg, &iter);
213 for (i = 0; i < argc; i++)
214 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &argv[i]))
216 XDBG_LOG ("[CLIENT:%s] failed: append\n", info->client);
220 reply_msg = dbus_connection_send_with_reply_and_block (info->conn, msg,
222 if (dbus_error_is_set (&err))
224 XDBG_LOG ("[CLIENT:%s] failed: send (%s)\n", info->client, err.message);
227 GOTO_IF_FAIL (reply_msg != NULL, err_send);
229 if (!dbus_message_iter_init (reply_msg, &iter))
231 XDBG_LOG ("[CLIENT:%s] Message has no arguments\n", info->client);
239 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
241 XDBG_LOG ("[CLIENT:%s] Argument is not string!\n", info->client);
245 dbus_message_iter_get_basic (&iter, &arg);
248 XDBG_LOG ("[CLIENT:%s] arg is NULL\n", info->client);
252 XDBG_LOG ("%s\n", arg);
253 } while (dbus_message_iter_has_next (&iter) &&
254 dbus_message_iter_next (&iter));
258 dbus_message_unref(msg);
260 dbus_message_unref(reply_msg);