Introduce functions similar to those provided by libosso
authorjaviplx <javiplx@gmail.com>
Mon, 4 Oct 2010 16:04:48 +0000 (16:04 +0000)
committerjaviplx <javiplx@gmail.com>
Mon, 4 Oct 2010 16:04:48 +0000 (16:04 +0000)
git-svn-id: file:///svnroot/wifihood/trunk@17 c51dfc6a-5949-4919-9c8e-f207a149c383

wifiscand/wifiscand.c

index 09d6176..abc067c 100644 (file)
@@ -4,8 +4,8 @@
 #ifdef HAVE_LIBOSSO
 #include <libosso.h>
 #else
-#include "glib/gtypes.h"
-#include "dbus/dbus.h"
+#include <glib.h>
+#include <dbus/dbus.h>
 #define OSSO_OK 0
 #define OSSO_ERROR 1
 #endif
@@ -247,6 +247,41 @@ int submit_reply_message(DBusConnection *connection, DBusMessage *message, char
   dbus_message_unref (reply);
   return DBUS_HANDLER_RESULT_HANDLED;
 }
+
+static DBusObjectPathVTable *vtable = NULL;
+
+int dbus_set_cb_f( DBusConnection *context,
+               const char *service, const char *object,
+               const char *interface, void *handler,
+               void *user_data) {
+
+       /* First, we prepare the complex dbus handler structures */
+       DBusObjectPathVTable *vtable = malloc( sizeof(DBusObjectPathVTable) );
+       memset(vtable , '\0', sizeof(DBusObjectPathVTable) );
+       vtable->message_function = (DBusObjectPathMessageFunction) handler;
+
+       if (!dbus_connection_register_fallback(context, object, vtable, user_data)) {
+               return OSSO_ERROR;
+               }
+
+       DBusError error;
+       dbus_error_init(&error);
+
+       dbus_bus_request_name (context, service, 0, &error);
+       if ( dbus_error_is_set(&error) ) {
+               dbus_error_free (&error);
+               return OSSO_ERROR;
+               }
+
+       dbus_error_free (&error);
+       return OSSO_OK;
+}
+
+void dbus_deinitialize( DBusConnection *context ) {
+       free(vtable); vtable = NULL;
+       dbus_connection_unref(context);
+       dbus_shutdown();
+}
 #endif
 
 /* Callback for normal D-BUS messages */
@@ -353,8 +388,7 @@ static DBusHandlerResult dbus_req_handler (DBusConnection  *connection,
 #ifdef HAVE_LIBOSSO
         osso_deinitialize(appdata->osso_context);
 #else
-       dbus_connection_unref(appdata->osso_context);
-       dbus_shutdown();
+       dbus_deinitialize(appdata->osso_context);
 #endif
         /* Instead of exiting, signaling finish to main loop could be better
         retval->value.s = (gchar *) realloc(retval->value.s,34*sizeof(gchar *));
@@ -412,15 +446,6 @@ static DBusHandlerResult dbus_req_handler (DBusConnection  *connection,
 #endif
 }
 
-#ifndef HAVE_LIBOSSO
-static DBusObjectPathVTable
-dbus_req_handler_vtable = {
-  NULL,
-  dbus_req_handler,
-  NULL,
-};
-#endif
-
 int main( void ) {
 
        osso_context_t *osso_context;
@@ -447,11 +472,7 @@ int main( void ) {
 
        /* Create AppData */
        AppData *appdata;
-#ifdef HAVE_LIBOSSO
        appdata = g_new0(AppData, 1);
-#else
-       appdata = malloc(sizeof(AppData));
-#endif
        appdata->osso_context = osso_context;
 
        memset(&(appdata->iface.info), 0, sizeof(wireless_info));
@@ -462,29 +483,16 @@ int main( void ) {
 #ifdef HAVE_LIBOSSO
        osso_return_t result = osso_rpc_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, dbus_req_handler, appdata);
 #else
-       int result = dbus_connection_register_fallback(osso_context, OSSO_OBJECT, &dbus_req_handler_vtable, appdata);
+       int result = dbus_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, dbus_req_handler, appdata);
 #endif
-// NOTE : valid return codes do not match from both functions
-#ifdef HAVE_LIBOSSO
+
        if (result != OSSO_OK) {
-#else
-       if (!result) {
-#endif
 #ifdef HAVE_LIBOSSO
                osso_system_note_infoprint(appdata->osso_context, "Failure while setting OSSO callback", NULL);
 #endif
                return OSSO_ERROR;
        }
 
-#ifndef HAVE_LIBOSSO
-       result = dbus_bus_request_name (osso_context, OSSO_SERVICE, 0, &error);
-       if (dbus_error_is_set (&error)) {
-               fprintf (stderr, "Error %s\n", error.message);
-               dbus_error_free (&error);
-               exit (1);
-               }
-#endif
-
        /* INITIALIZATION FINISH */
 
 #ifdef HAVE_LIBOSSO
@@ -498,8 +506,7 @@ int main( void ) {
 #ifdef HAVE_LIBOSSO
        osso_deinitialize(osso_context);
 #else
-       dbus_connection_unref(osso_context);
-       dbus_shutdown();
+       dbus_deinitialize(osso_context);
 #endif
 
        exit(0);