Objec and service registration in dbus.
Hi
Iam new to dbus and i want to register object and service to dbus using c code. I tried with some example code but none of them is registering the object and hence i cant call those methods remotely. Can any one help me in this? My server side code is as follows: #include <dbus/dbus-glib.h> #include <dbus/dbus-glib-bindings.h> #include <time.h> #include <string.h> #include <stdio.h> int hello_alarm_alarm_example_callapi(int a); #include "hello_alarm.h" typedef struct { DBusGConnection *bus; } AppData; /* Define new GObject structure * HelloAlarm */ typedef struct { GObject parent; /* Define a pointer to store * appdata */ AppData *appdata; } HelloAlarm; /* Define HelloAlarm's class */ typedef struct { GObjectClass parent; /* HelloAlarm's say_hello-method. * This will be registered as a DBUS-method */ int (*alarm_example_callapi)(int a); } HelloAlarmClass; static void hello_alarm_class_init(HelloAlarmClass *klass) { /* Define implementation location of the * say_hello-method */ klass->alarm_example_callapi =hello_alarm_alarm_example_callapi; } GType hello_alarm_get_type() { static GType type = 0; /* If the type is already registered, * return it, otherwise register the * GTypeInfoStructure */ if(!type) { static const GTypeInfo info = { sizeof(HelloAlarmClass), NULL, /* base_init */ NULL, /* base_finalize */ (GClassInitFunc)hello_alarm_class_init, /* class_init */ NULL, /* class_finalize */ NULL, /* class_data */ sizeof(HelloAlarm), 0, /* n_preallocs */ NULL, /* instance_init */ }; type = g_type_register_static(G_TYPE_OBJECT, "HelloAlarmType", &info, 0); } return type; } static gboolean register_service(DBusGConnection *bus, GObject *obj, const DBusGObjectInfo *info, gchar *name, gchar *path, GError **error) { DBusGProxy *system; gboolean result; guint request_name_result; /* Install GObject's info so glib knows * what kind of interface to present on * D-BUS */ /* Register the object to D-BUS using * given object path */ dbus_g_object_type_install_info(G_TYPE_FROM_INSTANCE(obj),info); dbus_g_connection_register_g_object(bus, path, obj); /* Get proxy D-BUS-service */ system = dbus_g_proxy_new_for_name(bus, "org.maemo.alarm_example", "/org/maemo/alarm_example", "org.maemo.alarm_example"); /* system=dbus_g_proxy_new_for_name (bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus");*/ /* if (!dbus_g_proxy_call (system, "RequestName", &error, G_TYPE_STRING, "org.designfu.SampleService", G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_UINT, &request_name_result, G_TYPE_INVALID)) printf("Failed to acquire org.designfu.SampleService %s", error); */ /* Request a name for the service from D-BUS */ /*result = org_freedesktop_DBus_request_name(system, "org.designfu.SampleService", DBUS_NAME_FLAG_DO_NOT_QUEUE, NULL, error);*/ result = org_freedesktop_DBus_request_name(system, name, 0, NULL, error); dbus_g_connection_register_g_object(bus, "/", obj); /* Check that the name request went OK */ if(!result) { g_debug("result is false"); g_object_unref(system); return FALSE; } g_object_unref(system); return TRUE; } int hello_alarm_alarm_example_callapi(int a) { printf("HERE IS VALUE %d\n",a); return 1; } int main(int argc,char **argv) { GError *error = NULL; AppData appdata; HelloAlarm *server; g_type_init(); //conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error); appdata.bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); if (appdata.bus == NULL) { g_warning("Error %s\n", error->message); g_error_free(error); return 1; } else { g_debug("conn object is %d\n", appdata.bus); } server = g_object_new(hello_alarm_get_type(), NULL); server->appdata = &appdata; register_service(appdata.bus, G_OBJECT(server), &dbus_glib_hello_alarm_object_info, "org.maemo.alarm_example", "/org/maemo/alarm_example", &error); /* Check that the registration was successfull */ if(error) { g_error("Couldn't register service: %s", error->message); g_error_free(error); return 1; } dbus_g_connection_unref(appdata.bus); return 0; } |
All times are GMT -5. The time now is 12:45 AM. |