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.
27 #include "shared/log.h"
28 #include "dbus-system.h"
31 #define DBUS_REPLY_TIMEOUT (10000)
33 int check_systemd_active(void)
40 _I("%s %s", "org.freedesktop.systemd1.Unit", "ActiveState");
42 msg = dbus_handle_method_sync_with_reply_var("org.freedesktop.systemd1",
43 "/org/freedesktop/systemd1/unit/default_2etarget",
44 "org.freedesktop.DBus.Properties",
46 g_variant_new("(ss)", "org.freedesktop.systemd1.Unit", "ActiveState"));
50 if (!dh_get_param_from_var(msg, "(v)", &var)) {
51 _E("reply is not variant type");
55 if (!dh_get_param_from_var(var, "(s)", &state)) {
56 _E("variant doesn't have string (%s)", g_variant_get_type_string(var));
61 if (strncmp(state, "active", 6) == 0)
74 static GBusType g_default_bus_type = G_BUS_TYPE_SYSTEM;
75 pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
77 void dbus_handle_set_default_bus_type(GBusType bus_type)
79 if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION)
82 pthread_mutex_lock(&g_mutex);
83 g_default_bus_type = bus_type;
84 pthread_mutex_unlock(&g_mutex);
87 GBusType dbus_handle_get_default_bus_type(void)
91 pthread_mutex_lock(&g_mutex);
92 type = g_default_bus_type;
93 pthread_mutex_unlock(&g_mutex);
103 /* basic information */
105 GDBusConnection *conn;
108 GList *list_names; /* dbus_name */
109 GList *list_object; /* dbus_object_handle_s */
110 pthread_mutex_t mutex;
113 /* path + interfaces */
115 dbus_handle_s *dh; /* dbus handle */
116 const char *path; /* object path */
117 GList *list_ifaces; /* dbus_interface_s */
118 } dbus_object_handle_s;
121 dbus_object_handle_s *oh; /* object handle */
122 const char *name; /* interface name */
123 GList *list_methods; /* const dbus_method_s */
128 #define get_dh_from_oh(oh) ((dbus_object_handle_s*)oh)->dh
130 /* global shared bus : system, session */
131 static dbus_handle_s g_dh[2];
133 static dbus_handle_s *_dbus_handle_get_connection(GBusType bus_type);
135 dbus_handle_s * _dbus_handle_get_default_connection(void)
137 return _dbus_handle_get_connection(dbus_handle_get_default_bus_type());
140 #define dbus_handle_lock(handle) do {\
142 pthread_mutex_lock(&((handle)->mutex));\
145 #define dbus_handle_unlock(handle) do {\
147 pthread_mutex_unlock(&(handle)->mutex);\
150 #define dcl_dbus_handle() dbus_handle_s *dh = (dbus_handle_s *)handle;
151 #define dcl_dbus_handle_null_check() dbus_handle_s *dh = (dbus_handle_s *)handle;\
153 _E("dbus handle is null\n");\
157 dbus_object_handle_s * _dbus_handle_lookup_object(GList *list_obj, const char *obj_path);
158 dbus_interface_s * _dbus_handle_lookup_interface(GList *list_iface, const char *iface_name);
159 dbus_method_s * _dbus_handle_lookup_method(GList *list_methods, const char *method_name);
160 static GVariant* _append_variant(const char *signature, const char *param[]);
162 dbus_interface_s *_iface_u_to_s(const dbus_interface_u *iface_u)
164 dbus_interface_s *iface = NULL;
166 if (!iface_u || !iface_u->methods) {
171 iface = (dbus_interface_s *)calloc(1, sizeof(dbus_interface_s));
173 _E("failed to calloc");
177 iface->name = iface_u->name;
178 iface->modified = TRUE;
180 for (int i = 0 ; i < iface_u->nr_methods; ++i) {
181 //_D("attached %s:%p", iface_u->methods[i].member, iface_u->methods[i].func);
182 iface->list_methods = g_list_prepend(iface->list_methods, (void*)(iface_u->methods + i));
188 static GDBusConnection * _get_bus(GBusType bus_type)
190 GDBusConnection *conn = NULL;
193 if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION) {
194 _E("Wrong bus_type %d", bus_type);
198 conn = g_bus_get_sync(bus_type, NULL, &err);
200 _E("failed to get bus:type:%d, %s\n", bus_type, err->message);
208 static GDBusConnection * _get_bus_private(GBusType bus_type)
211 GDBusConnection *conn = NULL;
212 const char * address;
214 if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION) {
215 _E("Wrong bus_type %d", bus_type);
219 address = g_dbus_address_get_for_bus_sync(bus_type, NULL, &err);
220 if (!address || err) {
221 _E("failed to get bus address\n");
226 conn = g_dbus_connection_new_for_address_sync(address,
227 (GDBusConnectionFlags) (G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
228 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION),
229 NULL, /* GDBusAuthObserver */
233 _E("failed to get private bus\n");
242 static dbus_handle_s *_dbus_handle_get_connection(GBusType bus_type)
244 int ibus = bus_type - 1;
245 dbus_handle_s *dh = NULL;
247 if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION) {
248 _E("Unknown bus type %d", bus_type);
253 dbus_handle_lock(dh);
256 dh->conn = _get_bus(bus_type);
258 dh->bus_type = bus_type;
263 dbus_handle_unlock(dh);
269 static dbus_handle_s *_dbus_handle_get_connection_private(GBusType bus_type)
273 if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION) {
274 _E("Unknown bus type %d", bus_type);
278 dh = (dbus_handle_s *)calloc(1, sizeof(dbus_handle_s));
280 _E("failed to allocate memory for dbus handle");
284 dbus_handle_lock(dh);
287 dh->conn = _get_bus_private(bus_type);
288 dh->bus_type = bus_type;
293 dbus_handle_unlock(dh);
298 dbus_handle_unlock(dh);
304 dbus_handle_h dbus_handle_get_connection(GBusType bus_type, gboolean priv)
306 dbus_handle_s *dh = NULL;
308 if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION) {
309 _E("Wrong bus_type %d\n", bus_type);
315 dh = _dbus_handle_get_connection_private(bus_type);
318 dh = _dbus_handle_get_connection(bus_type);
323 static void _dbus_handle_add_bus_name(dbus_handle_s *handle, const char *name, guint id)
325 dbus_name *dn = NULL;
328 if (!handle || !name || !id)
331 dn = (dbus_name*)calloc(1, sizeof(dbus_name));
333 _E("failed to calloc");
339 // todo : delete lock ?
340 locked = pthread_mutex_trylock(&handle->mutex);
341 if (locked != 0 && locked != EBUSY) {
342 _E("failed to lock %d\n", locked);
346 handle->list_names = g_list_prepend(handle->list_names, dn);
348 // todo : delete lock ?
350 dbus_handle_unlock(handle);
353 static gint _compare_dbus_name(gconstpointer a, gconstpointer b)
355 const char *bus_name = ((dbus_name *)a)->bus_name;
358 return strcmp(bus_name, (const char *)b);
361 dbus_name * _dbus_handle_lookup_dbus_name(GList *list_name, const char *bus_name)
363 if (!list_name || !bus_name)
366 GList *item = g_list_find_custom(list_name, bus_name, _compare_dbus_name);
370 return (dbus_name *)item->data;
373 #define dh_to_ds(x) ((dbus_handle_s*)x)
375 /* remove dbus_name from dbus handle */
376 static void _dbus_handle_remove_bus_name(dbus_handle_s *handle, const char *bus_name)
379 dbus_name *dn = NULL;
382 _E("wrong bus_name %s", bus_name);
386 dh = _dbus_handle_get_default_connection();
388 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
393 dbus_handle_lock(dh);
394 dn = _dbus_handle_lookup_dbus_name(dh->list_names, bus_name);
396 _E("failed to find dbus name %s", bus_name);
399 dh->list_names = g_list_remove(dh->list_names, dn);
402 dbus_handle_unlock(dh);
405 //extern void booting_done(void);
407 /* default handler */
408 static void _name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
410 dbus_handle_s *dh = (dbus_handle_s *)user_data;
415 _E("%s:%d:dbus handle is null\n", __func__, __LINE__);
419 // todo: add bus name?
420 //dh->bus_name = name;
423 /* default handler */
424 static void _name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data)
426 _E("%s:%d:%s\n", __func__, __LINE__, name);
427 dbus_handle_s *dh = (dbus_handle_s *)user_data;
429 _E("%s:%d:dbus handle is null\n", __func__, __LINE__);
432 _dbus_handle_remove_bus_name(dh, name);
435 int dbus_handle_request_bus_name(dbus_handle_h handle,
436 const char *bus_name,
437 GBusNameAcquiredCallback acquired_handler,
438 GBusNameLostCallback lost_handler)
445 _E("bus_name is NULL");
449 /* get shared connection */
451 dh = _dbus_handle_get_default_connection();
453 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
458 dbus_handle_lock(dh);
460 _E("failed to register name: connection is null\n");
464 /* todo : search name on connection */
465 item = g_list_find_custom(dh->list_names, bus_name, _compare_dbus_name);
467 id = ((dbus_name*)(item->data))->id;
468 _E("name already exist:%u", id);
472 id = g_bus_own_name_on_connection(dh->conn,
474 G_BUS_NAME_OWNER_FLAGS_NONE,
475 acquired_handler ? acquired_handler : _name_acquired,
476 lost_handler ? lost_handler : _name_lost,
480 _E("failed to own name:%s\n", bus_name);
484 _dbus_handle_add_bus_name(dh, bus_name, id);
487 dbus_handle_unlock(dh);
491 /* !! _name_lost handler callback is disabled by g_bus_unown_name : ubuntu */
492 int dbus_handle_release_bus_name(dbus_handle_h handle, const char *bus_name)
495 dbus_name *dn = NULL;
498 _E("Wrong bus name");
503 dh = _dbus_handle_get_default_connection();
505 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
510 dn = _dbus_handle_lookup_dbus_name(dh->list_names, bus_name);
512 _E("failed to find bus_name %s on dbus handle", bus_name);
516 _E("unown name %d", dn->id);
517 /* _name_lost handler is disabled by g_bus_unown_name : ubuntu */
518 g_bus_unown_name(dn->id);
520 dbus_handle_lock(dh);
521 dh->list_names = g_list_remove(dh->list_names, dn);
523 dbus_handle_unlock(dh);
528 int dbus_handle_free_connection(dbus_handle_h handle)
531 dbus_handle_s *pdh = NULL;
536 dh = _dbus_handle_get_default_connection();
538 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
544 _E("connection is NULL");
550 /* disable dbus handler */
551 dbus_handle_lock(dh);
558 /* flush everything */
559 if (!g_dbus_connection_flush_sync(pdh->conn, NULL, &err)) {
560 _E("failed to flush %s\n", err->message);
565 _D("list_names %u", g_list_length(pdh->list_names));
567 /* unown every well-knwon name */
568 if (pdh->list_names) {
569 dbus_name *dn = NULL;
570 for (item = g_list_first(pdh->list_names); item != NULL; item = g_list_next(item)) {
571 dn = (dbus_name *)item->data;
575 /* _name_lost handler is disabled by g_bus_unown_name : ubuntu */
576 _D("unown name id : %u", dn->id);
577 g_bus_unown_name(dn->id);
580 g_list_free(pdh->list_names);
581 pdh->list_names = NULL;
584 _D("list_object %u", g_list_length(pdh->list_object));
586 /* unregister every object */
587 if (pdh->list_object) {
588 dbus_object_handle_s * oh = NULL;
589 //g_list_foreach(pdh->list_object, [] (gpointer data, gpointer user_data) {}, NULL);
590 for (item = g_list_first(pdh->list_object); item != NULL; item = g_list_next(item)) {
591 oh = (dbus_object_handle_s *)item->data;
592 if (!oh || !oh->list_ifaces)
595 _D("delete object path %s", oh->path);
597 /* unregister every interface, method handles */
598 for (GList *iface = g_list_first(oh->list_ifaces); iface != NULL; iface = g_list_next(iface)) {
599 dbus_interface_s *ih = (dbus_interface_s *)iface->data;
603 _D("delete object iface %s", ih->name);
606 g_dbus_connection_unregister_object(pdh->conn, ih->reg_id);
611 /* close connection */
613 _E("close private connection\n");
615 if (!g_dbus_connection_close_sync(pdh->conn, NULL, &err)) {
616 _E("Error closing connection %s\n", err->message);
622 /* _free_func_object callback free the data */
623 //assert(g_list_length(pdh->list_names) == 0);
624 //assert(g_list_length(pdh->list_object) == 0);
626 g_object_unref(pdh->conn);
628 dbus_handle_unlock(dh);
638 #define buf_cal_free_space(size, nwrite) ((size - nwrite - 1) > 0 ? (size - nwrite - 1) : 0)
639 #define buf_block_size 8192
641 #define buf_check_space_realloc(buf, nwrite, buf_len) do {\
642 if ((nwrite >= buf_len - 1024)) {\
643 if (buf_len >= buf_block_size * 10) {\
644 _E("buf is too big to allocate. %d", buf_len);\
646 _E("buf_check_space_realloc");\
648 buf_len += buf_block_size;\
649 tmp = (char *)realloc(buf, buf_len);\
651 _E("failed to realloc");\
658 /* cal index of end of brace */
659 static int _check_brace(const char * expr)
670 if (expr[0] != '(' && expr[0] != '{')
673 for (int i = 0 ; i < len; ++i) {
675 if (expr[i] == '(' || expr[i] == '{') {
676 qu[qucnt++] = expr[i];
677 if (qucnt >= sizeof(qu)) {
678 _E("queue is too large. %s", expr);
684 if (expr[i] == ')' || expr[i] == '}') {
692 if (expr[i] == ')') {
697 } else if (expr[i] == '}') {
718 static int _get_xml_from_interfaces(char **xml, const dbus_interface_s *interfaces)
723 const dbus_method_s *pmethod;
724 int buf_len = buf_block_size;
727 _E("interfaces is null");
731 // todo : check dbus naming rule for interface name. ?
732 if (!interfaces->name) {
733 _E("wrong interface name");
736 if (!interfaces->list_methods) {
741 buf = (char *)malloc(buf_len);
743 _E("buf is null. not enough memory\n");
747 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "<node>""\n""\t""<interface name='%s'>""\n", interfaces->name);
750 for (GList *item = g_list_first(interfaces->list_methods); item != NULL; item = g_list_next(item)) {
751 pmethod = (const dbus_method_s *)item->data;
755 /* check free space of buf */
756 buf_check_space_realloc(buf, nwrite, buf_len);
758 if (pmethod->signature_in == NULL && pmethod->signature_out == NULL) {
759 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t""<method name='%s'/>""\n", pmethod->member);
763 /* <method name='###'> */
764 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t""<method name='%s'>""\n", pmethod->member);
767 len_args = pmethod->signature_in ? strlen(pmethod->signature_in) : 0;
768 for (int m = 0; m < len_args; ++m) {
770 // array a(), as, ay ?
771 if (pmethod->signature_in[m] == 'a') {
773 ei = _check_brace(pmethod->signature_in + m + 1);
775 char tmp[128] = {0,};
776 strncpy(tmp, pmethod->signature_in + m, ei + 1);
777 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t\t""<arg type='%s' name='arg%d' direction='in'/>""\n", tmp, m);
781 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t\t""<arg type='%c%c' name='arg%d' direction='in'/>""\n", pmethod->signature_in[m], pmethod->signature_in[m+1], m);
786 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t\t""<arg type='%c' name='arg%d' direction='in'/>""\n", pmethod->signature_in[m], m);
790 len_args = pmethod->signature_out ? strlen(pmethod->signature_out) : 0;
791 for (int m = 0; m < len_args; ++m) {
793 // todo: container type
794 if (pmethod->signature_out[m] == 'a') {
796 ei = _check_brace(pmethod->signature_out + m + 1);
798 char tmp[128] = {0,};
799 strncpy(tmp, pmethod->signature_out + m, ei + 1);
800 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t\t""<arg type='%s' name='arg%d' direction='out'/>""\n", tmp, m);
804 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t\t""<arg type='%c%c' name='arg%d' direction='out'/>""\n", pmethod->signature_out[m], pmethod->signature_out[m+1], m);
809 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t\t""<arg type='%c' name='arg%d' direction='out'/>""\n", pmethod->signature_out[m], m);
813 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t\t""</method>""\n");
816 nwrite += snprintf(buf + nwrite, buf_cal_free_space(buf_len, nwrite), "\t""</interface>""\n""</node>""");
820 /* todo: delete log */
825 _E("%s", buf + nwrite - 512);
830 static gint _compare_dbus_object(gconstpointer a, gconstpointer b)
832 dbus_object_handle_s * pa = (dbus_object_handle_s *)a;
833 if (!pa->path || !((const char*)b))
835 return strcmp(pa->path, (const char*)b);
838 static gint _compare_dbus_interface(gconstpointer a, gconstpointer b)
840 dbus_interface_s * pa = (dbus_interface_s *)a;
841 if (!pa->name || !((const char*)b))
843 return strcmp(pa->name, (const char*)b);
846 static gint _compare_dbus_interface_by_id(gconstpointer a, gconstpointer b)
848 dbus_interface_s * pa = (dbus_interface_s *)a;
849 if (!pa->reg_id || !((guint*)b))
851 return !(pa->reg_id == *((guint*)b));
854 static gint _compare_dbus_method(gconstpointer a, gconstpointer b)
856 dbus_method_s *pa = (dbus_method_s*)a;
857 if (!pa->member || !((const char*)b))
859 return strcmp(pa->member, (const char*)b);
862 dbus_object_handle_s * _dbus_handle_lookup_object(GList *list_obj, const char *obj_path)
864 if (!list_obj || !obj_path)
867 GList *item = g_list_find_custom(list_obj, obj_path, _compare_dbus_object);
871 return (dbus_object_handle_s *)item->data;
874 dbus_interface_s * _dbus_handle_lookup_interface(GList *list_iface, const char *iface_name)
876 if (!list_iface || !iface_name)
879 GList *item = g_list_find_custom(list_iface, iface_name, _compare_dbus_interface);
883 return (dbus_interface_s *)item->data;
886 dbus_interface_s * _dbus_handle_lookup_interface_by_id(GList *list_iface, guint id)
888 if (!list_iface || !id)
891 GList *item = g_list_find_custom(list_iface, &id, _compare_dbus_interface_by_id);
895 return (dbus_interface_s *)item->data;
898 dbus_method_s * _dbus_handle_lookup_method(GList *list_methods, const char *method_name)
900 if (!list_methods || !method_name)
903 GList *item = g_list_find_custom(list_methods, method_name, _compare_dbus_method);
907 return (dbus_method_s *)item->data;
910 static void _free_func_object(gpointer data)
912 dbus_interface_s *ih = (dbus_interface_s *)data;
913 dbus_object_handle_s *oh = NULL;
916 _E("interface handle is null");
917 assert(0); // something wrong
921 _E("unregister interface %s", ih->name);
923 /* just free list, not data(static dbus_method_s) */
924 g_list_free(ih->list_methods);
928 _E("object handle is null");
929 assert(0); // something wrong
933 /* remove ih from list_ifaces */
934 oh->list_ifaces = g_list_remove(oh->list_ifaces, ih);
936 /* interface_s is copy of interface_u */
939 /* remove oh from list_object */
940 if (!oh->list_ifaces) {
941 oh->dh->list_object = g_list_remove(oh->dh->list_object, oh);
946 static int _dbus_handle_attach_object(dbus_handle_s *dh, const char *obj_path, dbus_interface_s *iface)
948 dbus_object_handle_s *oh = NULL;
950 if (!dh || !obj_path || !iface) {
951 _E("failed to attache object. wrong parameter");
955 /* find object handle */
957 oh = _dbus_handle_lookup_object(dh->list_object, obj_path);
960 oh = (dbus_object_handle_s*)calloc(1, sizeof(dbus_object_handle_s));
962 _E("failed to calloc");
969 dh->list_object = g_list_prepend(dh->list_object, oh);
973 /* attach interface */
974 oh->list_ifaces = g_list_prepend(oh->list_ifaces, iface);
982 libgio verify path and interface of incoming message.
983 --> just check method name.
985 # parameters - member of invocation struct
986 every parameters of this function are member of GDBusMethodInvocation *invocation.
987 There are no reason to using g_dbus_method_invocation_get_* apis to get inforamtion from message.
988 just use params - sender, path, iface, name and param.
991 # user defined handler #
993 1. synchronous handling
996 return g_variant_new("(i)", ret);
999 2) without return value
1001 return dbus_handle_new_g_variant_tuple(); // g_variant_new_tuple(NULL, 0)
1004 2. asynchronous handling
1005 handler MUST call 'g_dbus_method_invocation_return_value' itself. otherwise, LEAK !!
1011 # if handler return NULL, assume asynchronous handling. do nothing.
1016 1) with return value
1017 g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", ret));
1019 2) without return value
1020 g_dbus_method_invocation_return_value(invocation, NULL);
1025 static void _method_call_handler(GDBusConnection *conn,
1026 const gchar *sender,
1031 GDBusMethodInvocation *invocation,
1034 dbus_interface_s *iface_s = (dbus_interface_s *)user_data;
1035 const dbus_method_s *methods;
1036 GVariant *result = NULL;
1039 methods = _dbus_handle_lookup_method(iface_s->list_methods, name);
1041 result = methods->func(conn, sender, path, iface, name, param, invocation, get_dh_from_oh(iface_s->oh));
1043 /* async, maybe they will reply...maybe.. */
1050 g_dbus_method_invocation_return_value(invocation, result);
1053 static GDBusInterfaceVTable path_vtable = {_method_call_handler};
1057 before register object, attach object into dbus handle
1058 _dbus_handle_attach_object()
1060 static int _dbus_handle_register_dbus_object(dbus_handle_h handle, const char *obj_path, dbus_interface_s *iface)
1066 GDBusNodeInfo * nodeinfo = NULL;
1067 GDBusInterfaceInfo *ifaceinfo = NULL;
1069 if (!obj_path || !iface) {
1070 _E("wrong parameter\n");
1074 dh = _dbus_handle_get_default_connection();
1076 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1081 _E("connection is null\n");
1085 ret = _get_xml_from_interfaces(&buf, iface);
1087 _E("failed to make xml format");
1091 /* todo: delete this */
1093 if (strlen(buf) <= 512) {
1096 _E("%s", buf + strlen(buf) - 512);
1100 nodeinfo = g_dbus_node_info_new_for_xml(buf, &err);
1101 if (!nodeinfo || err) {
1102 _E("failed to make introspection data:err:%s:xml:%s\n", err->message, buf);
1107 ifaceinfo = g_dbus_node_info_lookup_interface(nodeinfo, iface->name);
1109 _E("failed to g_dbus_node_info_lookup_interface");
1115 path own single interface
1116 if interface is already registered, then failed.
1117 g_dbus_connection_register_object ref(ifaceinfo) now, unref if object is unregistered
1119 ret = g_dbus_connection_register_object(dh->conn,
1127 _E("failed to register object:err:%s:\n", err->message);
1132 iface->reg_id = ret;
1133 iface->modified = FALSE;
1136 /* todo: detach object */
1137 //_dbus_handle_detach_object(dh, obj_path, iface);
1138 /* attach interface before register object */
1139 /*ret = _dbus_handle_detach_object(dh, obj_path, iface);
1141 _E("failed to attach object");
1146 g_dbus_node_info_unref(nodeinfo);
1156 register same interface at once
1158 if interface is constructed by multiple methods,
1159 also it is not possible to make methods struct at once,
1161 use dbus_handle_add_dbus_object(), dbus_handle_register_dbus_object_all().
1165 int dbus_handle_register_dbus_object(dbus_handle_h handle, const char *obj_path, const dbus_interface_u *iface_u)
1169 dbus_interface_s *iface = NULL;
1171 if (!obj_path || !iface_u) {
1172 _E("wrong parameter\n");
1176 dh = _dbus_handle_get_default_connection();
1178 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1183 _E("connection is null\n");
1187 /* check registered interface */
1188 if (dh->list_object) {
1189 dbus_object_handle_s *oh = _dbus_handle_lookup_object(dh->list_object, obj_path);
1191 dbus_interface_s *ih = _dbus_handle_lookup_interface(oh->list_ifaces, iface_u->name);
1193 _E("path %s, interface %s already registered", obj_path, iface_u->name);
1199 iface = _iface_u_to_s(iface_u);
1201 _E("failed to _iface_u_to_s");
1205 /* attach interface before register object */
1206 ret = _dbus_handle_attach_object(dh, obj_path, iface);
1208 _E("failed to attach object");
1212 ret = _dbus_handle_register_dbus_object(dh, obj_path, iface);
1214 _E("failed to register dbus object%d", ret);
1221 int dbus_handle_unregister_dbus_object(dbus_handle_h handle, const char *obj_path)
1224 dbus_object_handle_s *oh = NULL;
1231 dh = _dbus_handle_get_default_connection();
1233 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1237 if (!dh->list_object) {
1238 _E("list_object is empty");
1242 oh = _dbus_handle_lookup_object(dh->list_object, obj_path);
1244 _E("no object with name %s", obj_path);
1248 /* unregister every interface of object*/
1249 for (GList *item = g_list_first(oh->list_ifaces); item != NULL; item = g_list_next(item)) {
1250 dbus_interface_s *ih = item->data;
1252 _E("this is error");
1256 /* remove ih from list_ifaces */
1258 item = g_list_previous(item);
1260 /* remove and free link */
1261 oh->list_ifaces = g_list_remove(oh->list_ifaces, ih);
1263 /* free list_methods */
1264 g_list_free(ih->list_methods);
1271 /* unregister object by id */
1272 ret = g_dbus_connection_unregister_object(dh->conn, ih->reg_id);
1274 _E("failed to unregister object %s, interface %s, regid %d", oh->path, ih->name, ih->reg_id);
1281 add object temporarily.
1282 dbus_handle_register_dbus_object_all register every objects on connection.
1284 return registered method count
1286 int dbus_handle_add_dbus_object(dbus_handle_h handle, const char *obj_path, const dbus_interface_u *iface_u)
1289 dbus_object_handle_s *oh = NULL;
1290 dbus_interface_s *ih = NULL;
1293 if (!obj_path || !iface_u) {
1294 _E("wrong parameter path %s, iface_u %p\n", obj_path, iface_u);
1297 if (iface_u && (!iface_u->name || !iface_u->methods)) {
1298 _E("wrong parameter path %s, iface_u %p\n", obj_path, iface_u);
1302 cnt = iface_u->nr_methods;
1305 dh = _dbus_handle_get_default_connection();
1307 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1313 _E("failed to register method. connection is null\n");
1317 /* if there are no object list, just add */
1318 if (!dh->list_object) {
1319 if (_dbus_handle_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) {
1320 _E("failed to attach object");
1326 oh = _dbus_handle_lookup_object(dh->list_object, obj_path);
1327 /* if there are no matched object, just add */
1329 if (_dbus_handle_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) {
1330 _E("failed to attach object");
1336 /* this is an error, interface must have one or more item ? */
1337 if (!oh->list_ifaces) {
1338 _E("error. list_ifaces is null\n");
1343 ih = _dbus_handle_lookup_interface(oh->list_ifaces, iface_u->name);
1344 /* if there are no matched interface, just add */
1346 if (_dbus_handle_attach_object(dh, obj_path, _iface_u_to_s(iface_u))) {
1347 _E("failed to attach object");
1354 1. unregister interface
1355 2. update interface and methods
1356 3. register interface
1359 _E("interface already registered, ignore new interface");
1363 /* attach new methods */
1365 for (int i = 0; i < iface_u->nr_methods; ++i) {
1366 GList *item = g_list_find_custom(g_list_first(ih->list_methods), iface_u->methods[i].member, _compare_dbus_method);
1368 //_D("attached %s", iface_u->methods[i].member);
1369 ih->list_methods = g_list_prepend(ih->list_methods, (void*)(iface_u->methods + i));
1375 ih->modified = TRUE;
1378 /*todo: delete debugging log */
1379 //if (dh && dh->list_object)
1380 // _D("obj list len %d", g_list_length(dh->list_object));
1381 //if (oh && oh->list_ifaces)
1382 // _D("iface list len %d", g_list_length(oh->list_ifaces));
1383 //if (ih && ih->list_methods)
1384 // _D("method list len %d", g_list_length(ih->list_methods));
1389 int dbus_handle_register_dbus_object_all(dbus_handle_h handle)
1392 dbus_object_handle_s *oh = NULL;
1393 dbus_interface_s *ih = NULL;
1397 dh = _dbus_handle_get_default_connection();
1399 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1404 _E("connection is null\n");
1408 if (!dh->list_object) {
1409 _E("obj list is empty");
1413 /*if (dh && dh->list_object)
1414 _D("obj list len %d", g_list_length(dh->list_object));*/
1416 for (GList *item = g_list_first(dh->list_object); item != NULL; item = g_list_next(item)) {
1417 oh = (dbus_object_handle_s *)item->data;
1420 _E("something wrong");
1423 if (!oh->list_ifaces) {
1424 _E("path %s: list_ifaces are null", oh->path);
1428 //_D("iface list len %d", g_list_length(oh->list_ifaces));
1430 for (GList *li = g_list_first(oh->list_ifaces); li != NULL; li = g_list_next(li)) {
1431 ih = (dbus_interface_s *)li->data;
1433 /* if there are no modification, goto next */
1437 /* todo: if already registered interface, unregister first */
1439 /*_E("interface %s:", ih->name);
1440 if (ih && ih->list_methods)
1441 _D("method list len %d", g_list_length(ih->list_methods));*/
1443 ret = _dbus_handle_register_dbus_object(dh, oh->path, ih);
1445 _E("failed to register dbus object%d", ret);
1452 // todo: delete all updates
1457 static void _free_func_signal(gpointer data)
1459 //_D("free signal subscribe");
1462 guint subscribe_dbus_signal(dbus_handle_h handle, const char *path,
1463 const char *iface, const char *name,
1464 GDBusSignalCallback cb, void *data,
1465 destroy_notified free_func)
1470 dh = _dbus_handle_get_default_connection();
1472 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1478 _E("connection is null. check bus status");
1481 return g_dbus_connection_signal_subscribe(dh->conn, NULL, iface, name, path, NULL, G_DBUS_SIGNAL_FLAGS_NONE, cb, data, _free_func_signal);
1484 void unsubscribe_dbus_signal(dbus_handle_h handle, guint id)
1488 dh = _dbus_handle_get_default_connection();
1490 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1496 _E("connection is null. check bus status");
1500 g_dbus_connection_signal_unsubscribe(dh->conn, id);
1503 int _check_type_string_is_container(const char *signature)
1508 switch (signature[0]) {
1523 GVariant *dbus_handle_make_simple_array(const char *sig, int *param)
1525 GVariantBuilder *builder = NULL;
1526 GVariant *var = NULL;
1530 builder = g_variant_builder_new(G_VARIANT_TYPE(sig));
1532 _E("failed to g_variant_builder_new");
1537 g_variant_builder_add(builder, "i", param[i++]);
1539 snprintf(format, sizeof(format) - 1, "(%s)", sig);
1540 var = g_variant_new(format, builder);
1541 g_variant_builder_unref(builder);
1545 /* todo: looks like garbage... */
1546 static GVariant* _append_variant(const char *signature, const char *param[])
1553 unsigned long long int64_type;
1556 char container[255];// The maximum length of a signature is 255.
1557 const char *sig = signature;
1558 GVariantBuilder *builder = NULL;
1560 if (!signature || !param)
1563 /* workaround for user fault "(i) != i" but we treat this as same signature */
1564 /* G_VARIANT_TYPE("si") return NULL */
1565 /* todo: actually user have to use correct signature */
1566 if (!_check_type_string_is_container(signature)) {
1567 snprintf(container, sizeof(container) - 1, "(%s)", signature);
1570 if (!g_variant_type_is_container(G_VARIANT_TYPE(sig))) {
1571 _E("signature (%s) is not container type", signature);
1574 builder = g_variant_builder_new(G_VARIANT_TYPE(sig));
1577 for (ch = (char *)sig, i = 0; i < len; ++i, ++ch) {
1583 bool_type = (atoi(param[pi++]) == 0 ? FALSE : TRUE);
1584 g_variant_builder_add(builder, "b", bool_type);
1587 int_type = atoi(param[pi++]);
1588 g_variant_builder_add(builder, "i", int_type);
1591 int_type = strtoul(param[pi++], NULL, 10);
1592 g_variant_builder_add(builder, "u", int_type);
1595 int64_type = atoll(param[pi++]);
1596 g_variant_builder_add(builder, "t", int64_type);
1599 g_variant_builder_add(builder, "s", param[pi++]);
1605 g_variant_builder_add(builder, "^ay", param[pi++]);
1616 ret = g_variant_builder_end(builder);
1617 g_variant_builder_clear(builder);
1618 g_variant_builder_unref(builder);
1623 int dbus_handle_broadcast_dbus_signal(const char *path,
1626 const char *signature,
1627 const char *param[])
1629 dbus_handle_s *dh = NULL;
1632 GVariant *var = NULL;
1634 dh = _dbus_handle_get_default_connection();
1636 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1640 if (signature && param)
1641 var = _append_variant(signature, param);
1642 ret = g_dbus_connection_emit_signal(dh->conn, NULL, path, iface, name, var, &err);
1644 _E("%d %s\n", ret, err ? err->message : NULL);
1651 int dbus_handle_broadcast_dbus_signal_var(const char *path,
1656 dbus_handle_s *dh = NULL;
1660 dh = _dbus_handle_get_default_connection();
1662 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1666 ret = g_dbus_connection_emit_signal(dh->conn, NULL, path, iface, name, param, &err);
1668 _E("%d %s\n", ret, err ? err->message : NULL);
1675 GVariant *dbus_handle_method_sync_with_reply_var(const char *dest,
1682 GVariant * ret = NULL;
1683 dbus_handle_s *dh = NULL;
1685 if (!dest || !path || !iface || !method) {
1686 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
1688 g_variant_unref(var);
1692 dh = _dbus_handle_get_default_connection();
1694 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1696 g_variant_unref(var);
1700 ret = g_dbus_connection_call_sync(dh->conn,
1701 dest, path, iface, method,
1702 var, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &err);
1705 _E("failed to g_dbus_connection_call_sync:%s", err->message);
1708 _E("failed to g_dbus_connection_call_sync");
1716 GVariant *dbus_handle_method_sync_with_reply_var_timeout(const char *dest, const char *path,
1717 const char *iface, const char *method, GVariant *var, int timeout)
1720 GVariant * ret = NULL;
1721 dbus_handle_s *dh = NULL;
1723 if (!dest || !path || !iface || !method) {
1724 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
1726 g_variant_unref(var);
1730 dh = _dbus_handle_get_default_connection();
1732 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1734 g_variant_unref(var);
1738 ret = g_dbus_connection_call_sync(dh->conn,
1739 dest, path, iface, method,
1740 var, NULL, G_DBUS_CALL_FLAGS_NONE, timeout, NULL, &err);
1743 _E("failed to g_dbus_connection_call_sync:%s", err->message);
1746 _E("failed to g_dbus_connection_call_sync");
1754 gint* dbus_handle_get_unix_fd_list(GDBusMethodInvocation *invocation, int *size)
1756 GUnixFDList *fd_list = NULL;
1759 fd_list = g_dbus_message_get_unix_fd_list(g_dbus_method_invocation_get_message(invocation));
1762 _E("failed to g_unix_fd_list_get_length: fd_list is null");
1766 length = g_unix_fd_list_get_length(fd_list);
1768 _E("failed to g_unix_fd_list_get_length: list size is 0");
1774 return g_unix_fd_list_steal_fds(fd_list, NULL);
1777 GVariant *dbus_handle_method_with_unix_fd_list_sync_with_reply(const char *dest,
1781 const char *signature,
1782 const char *param[],
1789 GVariant * var = NULL;
1790 GVariant * ret = NULL;
1791 dbus_handle_s *dh = NULL;
1792 GDBusProxy *proxy = NULL;
1793 GUnixFDList *g_infdlist = NULL;
1794 GUnixFDList *g_outfdlist = NULL;
1796 if (!dest || !path || !iface || !method) {
1797 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
1800 if (in_fdlist && in_size == 0) {
1801 _E("wrong in_fdlist is not null but in_size is 0");
1805 dh = _dbus_handle_get_default_connection();
1807 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1811 if (signature && param)
1812 var = _append_variant(signature, param);
1814 proxy = g_dbus_proxy_new_sync(dh->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, dest, path, iface, NULL, &err);
1817 _E("failed to proxy_new_sync(%s)\n", err->message);
1820 _E("failed to proxy_new_sync\n");
1822 g_variant_unref(var);
1828 g_infdlist = g_unix_fd_list_new_from_array(in_fdlist, in_size);
1830 _E("failed to g_unix_fd_list_new_from_array\n");
1833 //g_infdlist = g_unix_fd_list_new();
1834 //if (g_unix_fd_list_append(g_infdlist, in_fdlist[0], &err) < 0) {
1837 ret = g_dbus_proxy_call_with_unix_fd_list_sync(proxy, method, var, G_DBUS_CALL_FLAGS_NONE, -1,
1838 g_infdlist, &g_outfdlist, NULL, &err);
1841 _E("failed to g_dbus_proxy_call_with_unix_fd_list_sync:%s", err->message);
1844 _E("failed to g_dbus_proxy_call_with_unix_fd_list_sync:");
1846 g_variant_unref(var);
1848 g_object_unref(g_infdlist);
1853 /* fds to out array */
1855 *out_size = g_unix_fd_list_get_length(g_outfdlist);
1859 *out_fdlist = g_unix_fd_list_steal_fds(g_outfdlist, NULL);
1864 g_object_unref(g_outfdlist);
1866 g_object_unref(proxy);
1871 GVariant *dbus_handle_method_with_unix_fd_list_sync_with_reply_var(const char *dest,
1882 GVariant * ret = NULL;
1883 dbus_handle_s *dh = NULL;
1884 GDBusProxy *proxy = NULL;
1885 GUnixFDList *g_infdlist = NULL;
1886 GUnixFDList *g_outfdlist = NULL;
1888 if (!dest || !path || !iface || !method) {
1889 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
1892 if (in_fdlist && in_size == 0) {
1893 _E("wrong in_fdlist is not null but in_size is 0");
1897 dh = _dbus_handle_get_default_connection();
1899 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1903 proxy = g_dbus_proxy_new_sync(dh->conn, G_DBUS_PROXY_FLAGS_NONE, NULL, dest, path, iface, NULL, &err);
1906 _E("failed to proxy_new_sync(%s)\n", err->message);
1909 _E("failed to proxy_new_sync\n");
1911 g_variant_unref(param);
1918 g_infdlist = g_unix_fd_list_new_from_array(in_fdlist, in_size);
1920 _E("failed to g_unix_fd_list_new_from_array\n");
1923 //g_infdlist = g_unix_fd_list_new();
1924 //if (g_unix_fd_list_append(g_infdlist, in_fdlist[0], &err) < 0) {
1928 ret = g_dbus_proxy_call_with_unix_fd_list_sync(proxy, method, param, G_DBUS_CALL_FLAGS_NONE, -1,
1929 g_infdlist, &g_outfdlist, NULL, &err);
1932 _E("failed to g_dbus_proxy_call_with_unix_fd_list_sync:%s", err->message);
1935 _E("failed to g_dbus_proxy_call_with_unix_fd_list_sync:");
1937 g_variant_unref(param);
1939 g_object_unref(g_infdlist);
1944 /* copy fds to out array */
1946 *out_size = g_unix_fd_list_get_length(g_outfdlist);
1949 *out_fdlist = g_unix_fd_list_steal_fds(g_outfdlist, NULL);
1953 g_object_unref(g_outfdlist);
1955 g_object_unref(proxy);
1959 int dbus_handle_method_sync(const char *dest,
1963 const char *signature,
1964 const char *param[])
1967 gboolean result_bool;
1968 GVariant *reply = NULL;
1969 GVariant * var = NULL;
1971 dbus_handle_s *dh = NULL;
1973 if (!dest || !path || !iface || !method) {
1974 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
1978 dh = _dbus_handle_get_default_connection();
1980 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
1984 if (signature && param)
1985 var = _append_variant(signature, param);
1987 reply = g_dbus_connection_call_sync(dh->conn,
1988 dest, path, iface, method,
1989 var, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &err);
1990 if (!reply || err) {
1992 _E("failed to g_dbus_connection_call_sync:%s", err->message);
1995 _E("failed to g_dbus_connection_call_sync");
1996 g_variant_unref(var);
2001 if (g_strcmp0("(i)", g_variant_get_type_string(reply)) == 0) {
2002 g_variant_get(reply, "(i)", &result);
2003 } else if (g_strcmp0("(b)", g_variant_get_type_string(reply)) == 0) {
2004 g_variant_get(reply, "(b)", &result_bool);
2005 result = (int)result_bool;
2010 g_variant_unref(reply);
2015 int dbus_handle_method_sync_var(const char *dest,
2022 gboolean result_bool;
2023 GVariant *reply = NULL;
2025 if (!dest || !path || !iface || !method) {
2026 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
2030 reply = dbus_handle_method_sync_with_reply_var(dest, path, iface, method, param);
2034 if (g_strcmp0("(i)", g_variant_get_type_string(reply)) == 0) {
2035 g_variant_get(reply, "(i)", &result);
2036 } else if (g_strcmp0("(b)", g_variant_get_type_string(reply)) == 0) {
2037 g_variant_get(reply, "(b)", &result_bool);
2038 result = (int)result_bool;
2043 g_variant_unref(reply);
2048 int dbus_handle_method_sync_timeout(const char *dest,
2052 const char *signature,
2053 const char *param[],
2056 dbus_handle_s *dh = NULL;
2058 GVariant * var = NULL;
2059 GVariant * reply = NULL;
2062 if (!dest || !path || !iface || !method) {
2063 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
2067 dh = _dbus_handle_get_default_connection();
2069 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2073 if (signature && param)
2074 var = _append_variant(signature, param);
2076 reply = g_dbus_connection_call_sync(dh->conn,
2077 dest, path, iface, method,
2078 var, NULL, G_DBUS_CALL_FLAGS_NONE, timeout, NULL, &err);
2079 if (!reply || err) {
2080 _E("failed to g_dbus_connection_call_sync:%s", err->message);
2085 if (g_strcmp0("(i)", g_variant_get_type_string(reply)) == 0)
2086 g_variant_get(reply, "(i)", &result);
2090 g_variant_unref(reply);
2095 int dbus_handle_method_sync_pairs(const char *dest,
2103 GVariant * reply = NULL;
2107 GVariantBuilder *builder;
2108 dbus_handle_s *dh = NULL;
2110 if (!dest || !path || !iface || !method) {
2111 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
2115 dh = _dbus_handle_get_default_connection();
2117 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2121 builder = g_variant_builder_new(G_VARIANT_TYPE("a{ss}"));
2123 for (int i = 0 ; i < num ; i = i + 2) {
2124 key = va_arg(args, char *);
2125 value = va_arg(args, char *);
2126 _I("key(%s), value(%s)", key, value);
2127 g_variant_builder_add(builder, "{ss}", key, value);
2130 var = g_variant_new("(a{ss})", builder);
2131 g_variant_builder_unref(builder);
2133 reply = g_dbus_connection_call_sync(dh->conn,
2134 dest, path, iface, method,
2135 var, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, &err);
2136 if (!reply || err) {
2137 _E("failed to g_dbus_connection_call_sync");
2141 if (g_strcmp0("(i)", g_variant_get_type_string(reply)) == 0)
2142 g_variant_get(reply, "(i)", &ret);
2146 g_variant_unref(reply);
2151 int dbus_handle_method_async_pairs(const char *dest,
2160 GVariantBuilder *builder;
2161 dbus_handle_s *dh = NULL;
2163 if (!dest || !path || !iface || !method) {
2164 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
2168 dh = _dbus_handle_get_default_connection();
2170 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2175 builder = g_variant_builder_new(G_VARIANT_TYPE("a{ss}"));
2177 for (int i = 0 ; i < num ; i = i + 2) {
2178 key = va_arg(args, char *);
2179 value = va_arg(args, char *);
2180 _I("key(%s), value(%s)", key, value);
2181 g_variant_builder_add(builder, "{ss}", key, value);
2184 var = g_variant_new("(a{ss})", builder);
2185 g_variant_builder_unref(builder);
2187 g_dbus_connection_call(dh->conn, dest, path, iface, method,
2188 var, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,
2195 int dbus_handle_method_async(const char *dest,
2199 const char *signature,
2200 const char *param[])
2202 GVariant * var = NULL;
2203 dbus_handle_s *dh = NULL;
2205 if (!dest || !path || !iface || !method) {
2206 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
2210 dh = _dbus_handle_get_default_connection();
2212 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2216 if (signature && param)
2217 var = _append_variant(signature, param);
2219 g_dbus_connection_call(dh->conn, dest, path, iface, method,
2220 var, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,
2227 int dbus_handle_method_async_var(const char *dest,
2233 dbus_handle_s *dh = NULL;
2235 if (!dest || !path || !iface || !method) {
2236 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
2240 dh = _dbus_handle_get_default_connection();
2242 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2246 g_dbus_connection_call(dh->conn, dest, path, iface, method,
2247 param, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,
2254 /* callback should free gvariant */
2255 static void _cb_pending(GDBusConnection *conn,
2259 GVariant *reply = NULL;
2261 pending_call_data *data = (pending_call_data *)user_data;
2263 reply = g_dbus_connection_call_finish(conn, res, &err);
2264 if (!reply || err) {
2266 _E("no message : [%s]", err->message);
2272 if (data && data->func)
2273 data->func(NULL, data->data, err);
2277 if (data && data->func)
2278 data->func(reply, data->data, err);
2284 int dbus_handle_method_async_with_reply(const char *dest,
2288 const char *signature,
2289 const char *param[],
2294 dbus_handle_s *dh = NULL;
2295 pending_call_data *pdata = NULL;
2296 GVariant * var = NULL;
2299 if (!dest || !path || !iface || !method) {
2300 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
2304 if (timeout_msec < -1) {
2305 _E("wrong timeout %d", timeout_msec);
2309 dh = _dbus_handle_get_default_connection();
2311 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2315 if (signature && param)
2316 var = _append_variant(signature, param);
2319 pdata = (pending_call_data*)malloc(sizeof(pending_call_data));
2328 g_dbus_connection_call(dh->conn, dest, path, iface, method,
2329 var, NULL, G_DBUS_CALL_FLAGS_NONE, timeout_msec, NULL,
2330 (GAsyncReadyCallback)_cb_pending,
2336 g_variant_unref(var);
2340 int dbus_handle_method_async_with_reply_var(const char *dest,
2349 dbus_handle_s *dh = NULL;
2350 pending_call_data *pdata = NULL;
2353 if (!dest || !path || !iface || !method) {
2354 _E("wrong parameters dest(%s) path(%s) iface(%s) method(%s)", dest, path, iface, method);
2358 if (timeout_msec < -1) {
2359 _E("wrong timeout %d", timeout_msec);
2363 dh = _dbus_handle_get_default_connection();
2365 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2370 pdata = (pending_call_data*)malloc(sizeof(pending_call_data));
2379 g_dbus_connection_call(dh->conn, dest, path, iface, method,
2380 param, NULL, G_DBUS_CALL_FLAGS_NONE, timeout_msec, NULL,
2381 (GAsyncReadyCallback)_cb_pending,
2387 g_variant_unref(param);
2391 int dbus_connection_get_sender_pid(GDBusConnection *conn, const char * sender)
2394 GVariant *vret = NULL;
2398 _E("connection is null");
2402 _E("sender is null");
2406 vret = g_dbus_connection_call_sync(conn,
2407 "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetConnectionUnixProcessID",
2408 g_variant_new("(s)", sender),
2410 G_DBUS_CALL_FLAGS_NONE,
2415 _E("failed to g_dbus_connection_call_sync:%s", err->message);
2420 g_variant_get(vret, "(u)", &pid);
2421 g_variant_unref(vret);
2426 int dbus_handle_get_sender_pid(dbus_handle_h handle, const char * sender)
2431 dh = _dbus_handle_get_default_connection();
2433 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2438 _E("wrong dbus handle. connection is null");
2443 return dbus_connection_get_sender_pid(dh->conn, sender);
2446 int dbus_handle_get_sender_credentials(dbus_handle_h handle, const char *name, GDBusCredentials *creds)
2449 GVariant *vret = NULL;
2451 GVariantIter *iter = NULL;
2456 dh = _dbus_handle_get_default_connection();
2458 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2462 vret = g_dbus_connection_call_sync(dh->conn,
2465 DBUS_INTERFACE_NAME,
2466 "GetConnectionCredentials",
2467 g_variant_new("(s)", name),
2469 G_DBUS_CALL_FLAGS_NONE,
2474 _E("failed to g_dbus_connection_call_sync:%s", err->message);
2478 g_variant_get(vret, "(a{sv})", &iter);
2480 while (g_variant_iter_loop(iter, "{sv}", &item, &sub)) {
2481 if (!g_strcmp0(item, "UnixUserID")) {
2482 g_variant_get(sub, "u", &creds->uid);
2483 //_D("UnixUserID %u", creds->uid);
2484 } else if (!g_strcmp0(item, "ProcessID")) {
2485 g_variant_get(sub, "u", &creds->pid);
2486 //_D("ProcessID %u", creds->pid);
2487 } else if (!g_strcmp0(item, "LinuxSecurityLabel")) {
2488 g_variant_get(sub, "^ay", &creds->sec_label);
2489 //_D("%s", creds->sec_label);
2494 g_variant_iter_free(iter);
2496 g_variant_unref(vret);
2501 void _destroy_notify_watch_name(gpointer data)
2507 int dbus_handle_watch_name(const char *name,
2508 GBusNameAppearedCallback name_appeared_handler,
2509 GBusNameVanishedCallback name_vanished_handler,
2511 GDestroyNotify user_data_free_func)
2516 _E("wrong name name %s", name);
2519 if (!name_appeared_handler && !name_vanished_handler) {
2520 _E("both function pointers are null");
2524 id = g_bus_watch_name(dbus_handle_get_default_bus_type(),
2526 G_BUS_NAME_WATCHER_FLAGS_NONE,
2527 name_appeared_handler,
2528 name_vanished_handler,
2530 user_data_free_func ? user_data_free_func : _destroy_notify_watch_name);
2532 _E("failed to g_bus_watch_name");
2539 void dbus_handle_unwatch_name(guint id)
2542 _E("wrong id %d", id);
2545 g_bus_unwatch_name(id);
2548 int _get_cmdline_name(pid_t pid, char *cmdline, size_t cmdline_size)
2551 char buf[PATH_MAX + 1];
2554 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
2555 fd = open(buf, O_RDONLY);
2561 ret = read(fd, buf, PATH_MAX);
2566 buf[PATH_MAX] = '\0';
2568 filename = strrchr(buf, '/');
2569 if (filename == NULL)
2572 filename = filename + 1;
2574 if (cmdline_size < strlen(filename) + 1) {
2579 strncpy(cmdline, filename, cmdline_size - 1);
2580 cmdline[cmdline_size - 1] = '\0';
2585 char **dbus_handle_get_owner_list(dbus_handle_h handle, const char *bus_name)
2589 GVariant *vret = NULL;
2590 GVariantIter *iter = NULL;
2591 gchar **strv = NULL;
2596 _E("wrong parameter bus_name(%s)", bus_name);
2601 dh = _dbus_handle_get_default_connection();
2603 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2608 vret = g_dbus_connection_call_sync(dh->conn,
2609 "org.freedesktop.DBus",
2611 "org.freedesktop.DBus",
2613 g_variant_new("(s)", bus_name),
2615 G_DBUS_CALL_FLAGS_NONE,
2620 _E("failed to g_dbus_connection_call_sync:%s", err->message);
2625 g_variant_get(vret, "(as)", &iter);
2626 strv = g_new(gchar *, g_variant_iter_n_children(iter) + 1);
2629 while (g_variant_iter_loop(iter, "s", &str))
2630 strv[i++] = g_strdup(str);
2633 g_variant_iter_free(iter);
2634 g_variant_unref(vret);
2639 void dbush_handle_check_owner_name(dbus_handle_h handle, const char *owner_name)
2642 char exe_name[PATH_MAX];
2648 dh = _dbus_handle_get_default_connection();
2650 _E("failed to get default connection, bustype:%d", (int)dbus_handle_get_default_bus_type());
2655 strv = dbus_handle_get_owner_list(dh, owner_name);
2657 _E("failed to get owner list of %s", owner_name);
2661 for (i = 0; strv[i] != NULL; ++i) {
2662 pid = dbus_handle_get_sender_pid(dh, strv[i]);
2663 if (_get_cmdline_name(pid, exe_name, PATH_MAX) != 0)
2665 _I("%s(%d)", exe_name, pid);
2671 dbus_handle_h dbus_handle_init(GBusType type,
2672 const char* bus_name,
2673 GBusNameAcquiredCallback acquired_handler,
2674 GBusNameLostCallback lost_handler)
2676 dbus_handle_h handle = NULL;
2680 _E("Wrong bus name");
2684 // todo: do we need retry ? - booting time
2685 for (i = 0 ; i < 3; ++i) {
2686 handle = dbus_handle_get_connection(type, FALSE);
2691 ret = dbus_handle_request_bus_name(handle, bus_name, acquired_handler, lost_handler);
2695 dbush_handle_check_owner_name(NULL, bus_name);