Introduce wrapper to restore callback function to its original state
[wifihood] / wifiscand / wifiscand.c
index abc067c..57a5fa9 100644 (file)
@@ -6,20 +6,6 @@
 #else
 #include <glib.h>
 #include <dbus/dbus.h>
-#define OSSO_OK 0
-#define OSSO_ERROR 1
-#endif
-
-#ifndef HAVE_LIBOSSO
-#define osso_context_t DBusConnection
-typedef struct osso_rpc_t_values {
-  int i;
-  char *s;
-  } osso_rpc_t_values;
-typedef struct osso_rpc_t {
-  int type;
-  osso_rpc_t_values value;
-  } osso_rpc_t;
 #endif
 
 #define WIFISCAND_VERSION_STRING  "1.1"
@@ -235,70 +221,11 @@ static GMainLoop *event_loop = NULL;
 #endif
 static short int start_flags = 0;
 
-#ifndef HAVE_LIBOSSO
-int submit_reply_message(DBusConnection *connection, DBusMessage *message, char *string) {
-  DBusMessage *reply = dbus_message_new_method_return (message);
-  if (reply == NULL)
-    exit(0);
-  if (!dbus_message_append_args (reply, DBUS_TYPE_STRING, &string, DBUS_TYPE_INVALID))
-    exit(0);
-  if (!dbus_connection_send (connection, reply, NULL))
-    exit(0);
-  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 */
-#ifdef HAVE_LIBOSSO
 gint dbus_req_handler(const gchar * interface, const gchar * method,
                       GArray * arguments, gpointer data,
                       osso_rpc_t * retval)
-#else
-static DBusHandlerResult dbus_req_handler (DBusConnection  *connection,
-                   DBusMessage     *message,
-                   void            *data)
-#endif
 {
-#ifndef HAVE_LIBOSSO
-    osso_rpc_t *retval = malloc(sizeof(osso_rpc_t));
-#endif
-
     AppData *appdata;
     appdata = (AppData *) data;
 
@@ -306,34 +233,18 @@ static DBusHandlerResult dbus_req_handler (DBusConnection  *connection,
     retval->value.s = (gchar*) malloc( sizeof(gchar *) );
     retval->value.s[0] = '\0';
 
-#ifdef HAVE_LIBOSSO
     if ( strcmp(method,"wakeup")==0 ) {
-#else
-    if (dbus_message_is_method_call(message, OSSO_IFACE, "wakeup")) {
-#endif
         retval->value.s = (gchar *) realloc(retval->value.s,16*sizeof(gchar *));
         snprintf(retval->value.s,16,"WifiScand ready");
-#ifdef HAVE_LIBOSSO
         return OSSO_OK;
-#else
-        return submit_reply_message(connection, message, retval->value.s);
-#endif
     }
 
-#ifdef HAVE_LIBOSSO
     if ( strcmp(method,"start")==0 ) {
-#else
-    if (dbus_message_is_method_call(message, OSSO_IFACE, "start")) {
-#endif
 
         if( (appdata->iface.sock=iw_sockets_open()) < 0) {
             retval->value.s = (gchar *) realloc(retval->value.s,33*sizeof(gchar *));
             snprintf(retval->value.s,33,"Failure in socket initialization");
-#ifdef HAVE_LIBOSSO
             return OSSO_ERROR;
-#else
-            return submit_reply_message(connection, message, retval->value.s);
-#endif
         }
 
         struct ifreq frq;
@@ -341,11 +252,7 @@ static DBusHandlerResult dbus_req_handler (DBusConnection  *connection,
         if(ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
             retval->value.s = (gchar *) realloc(retval->value.s,28*sizeof(gchar *));
             snprintf(retval->value.s,28,"Cannot get interface status");
-#ifdef HAVE_LIBOSSO
             return OSSO_ERROR;
-#else
-            return submit_reply_message(connection, message, retval->value.s);
-#endif
         }
 
         start_flags = frq.ifr_flags;
@@ -354,28 +261,16 @@ static DBusHandlerResult dbus_req_handler (DBusConnection  *connection,
         if(ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq)) {
             retval->value.s = (gchar *) realloc(retval->value.s,27*sizeof(gchar *));
             snprintf(retval->value.s,27,"Cannot set interface state");
-#ifdef HAVE_LIBOSSO
             return OSSO_ERROR;
-#else
-            return submit_reply_message(connection, message, retval->value.s);
-#endif
         }
 
         refresh(&appdata->iface);
         retval->value.s = (gchar *) realloc(retval->value.s,22*sizeof(gchar *));
         snprintf(retval->value.s,22,"Interface initialized");
-#ifdef HAVE_LIBOSSO
         return OSSO_OK;
-#else
-        return submit_reply_message(connection, message, retval->value.s);
-#endif
     }
 
-#ifdef HAVE_LIBOSSO
     if ( strcmp(method,"stop")==0 ) {
-#else
-    if (dbus_message_is_method_call(message, OSSO_IFACE, "stop")) {
-#endif
        struct ifreq frq;
        strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
        if(!ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
@@ -397,21 +292,13 @@ static DBusHandlerResult dbus_req_handler (DBusConnection  *connection,
         exit(0);
     }
 
-#ifdef HAVE_LIBOSSO
     if ( strcmp(method,"scan")==0 ) {
-#else
-    if (dbus_message_is_method_call(message, OSSO_IFACE, "scan")) {
-#endif
 
         ScanInfo **scan = (ScanInfo **) makeScan(&appdata->iface,retval->value.s);
         if(scan == NULL) {
             retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
             snprintf(retval->value.s,64,"ERROR");
-#ifdef HAVE_LIBOSSO
             return OSSO_ERROR;
-#else
-            return submit_reply_message(connection, message, retval->value.s);
-#endif
         }
 
         int i;
@@ -420,32 +307,100 @@ static DBusHandlerResult dbus_req_handler (DBusConnection  *connection,
             retval->value.s = (gchar *) realloc(retval->value.s,23*(i+1)*sizeof(gchar *));
            if ( retval->value.s == NULL ) {
                 retval->value.s = "Error allocating memory";
-#ifdef HAVE_LIBOSSO
                 return OSSO_ERROR;
-#else
-                return submit_reply_message(connection, message, retval->value.s);
-#endif
            }
             sprintf(retval->value.s+strlen(retval->value.s),"%s:%d ",sc->mac,sc->rssi);
         }
 
         retval->value.s[strlen(retval->value.s)-1]  = '\0';
-#ifdef HAVE_LIBOSSO
         return OSSO_OK;
-#else
-        return submit_reply_message(connection, message, retval->value.s);
-#endif
     }
 
     retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
     snprintf(retval->value.s,64,"Unknown method");
-#ifdef HAVE_LIBOSSO
     return OSSO_ERROR;
-#else
-    return submit_reply_message(connection, message, retval->value.s);
-#endif
 }
 
+#ifndef HAVE_LIBOSSO
+
+#define OSSO_OK 0
+#define OSSO_ERROR 1
+
+#define osso_context_t DBusConnection
+typedef struct osso_rpc_t_values {
+  int i;
+  char *s;
+  } osso_rpc_t_values;
+typedef struct osso_rpc_t {
+  int type;
+  osso_rpc_t_values value;
+  } osso_rpc_t;
+
+static DBusObjectPathVTable *vtable = NULL;
+
+int dbus_set_cb_f( DBusConnection *context,
+               const char *service, const char *object,
+               const char *interface, void *handler, // FIXME : Set proper type for 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; // FIXME : Aqui va el nuevo handler/wrapper
+
+       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();
+}
+
+static DBusHandlerResult handler_wrapper (DBusConnection  *connection,
+                   DBusMessage     *message,
+                   void            *data) {
+
+       gint retcode;
+       osso_rpc_t *retval = malloc(sizeof(osso_rpc_t));
+
+       if ( dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ) {
+               retcode = dbus_req_handler(dbus_message_get_interface(message),
+                               dbus_message_get_member(message),
+                               NULL, data, retval);
+
+               if ( retval->value.s != NULL ) {
+                       DBusMessage *reply = dbus_message_new_method_return (message);
+                       if (reply == NULL)
+                               exit(0);
+                       if (!dbus_message_append_args (reply, DBUS_TYPE_STRING, &retval->value.s, DBUS_TYPE_INVALID))
+                               exit(0);
+                       if (!dbus_connection_send (connection, reply, NULL))
+                               exit(0);
+                       dbus_message_unref (reply);
+                       }
+
+               }
+
+       return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+#endif
+
 int main( void ) {
 
        osso_context_t *osso_context;
@@ -483,7 +438,7 @@ 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_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, dbus_req_handler, appdata);
+       int result = dbus_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, handler_wrapper, appdata);
 #endif
 
        if (result != OSSO_OK) {