Modify log format to store raw output from dbus scan
[wifihood] / wifiscand / wifiscand.c
index 36780c9..4c588f5 100644 (file)
@@ -1,9 +1,46 @@
 
 #include <iwlib.h>
 
+#ifdef HAVE_LIBOSSO
 #include <libosso.h>
-
-#define WIFISCAND_VERSION_STRING  "1.0"
+#else
+#include <glib.h>
+#include <dbus/dbus.h>
+
+#define WIFISCAN_INTROSPECTION "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n\
+         \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n\
+<node name=\"/org/javiplx/wifiscan\">\n\
+  <interface name=\"org.javiplx.wifiscan\">\n\
+    <method name=\"wakeup\">\n\
+      <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
+    </method>\n\
+    <method name=\"start\">\n\
+      <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
+    </method>\n\
+    <method name=\"scan\">\n\
+      <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
+    </method>\n\
+    <method name=\"stop\">\n\
+      <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
+    </method>\n\
+  </interface>\n\
+</node>\n"
+
+#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;
+#endif
+
+#define WIFISCAND_VERSION_STRING  "1.1"
 
 #define OSSO_NAME    "wifiscan"
 #define OSSO_SERVICE "org.javiplx."OSSO_NAME
@@ -211,7 +248,9 @@ struct _AppData {
     osso_context_t *osso_context;
 };
 
+#ifdef HAVE_LIBOSSO
 static GMainLoop *event_loop = NULL;
+#endif
 static short int start_flags = 0;
 
 /* Callback for normal D-BUS messages */
@@ -226,6 +265,14 @@ gint dbus_req_handler(const gchar * interface, const gchar * method,
     retval->value.s = (gchar*) malloc( sizeof(gchar *) );
     retval->value.s[0] = '\0';
 
+#ifndef HAVE_LIBOSSO
+    if ( strcmp(method,"Introspect")==0 ) {
+        retval->value.s = (gchar *) realloc(retval->value.s,630*sizeof(gchar *));
+        snprintf(retval->value.s,strlen(WIFISCAN_INTROSPECTION),WIFISCAN_INTROSPECTION);
+        return OSSO_OK;
+    }
+#endif
+
     if ( strcmp(method,"wakeup")==0 ) {
         retval->value.s = (gchar *) realloc(retval->value.s,16*sizeof(gchar *));
         snprintf(retval->value.s,16,"WifiScand ready");
@@ -273,12 +320,16 @@ gint dbus_req_handler(const gchar * interface, const gchar * method,
        }
         iw_sockets_close(appdata->iface.sock);
        appdata->iface.sock = 0;
+#ifdef HAVE_LIBOSSO
         osso_deinitialize(appdata->osso_context);
         /* Instead of exiting, signaling finish to main loop could be better
         retval->value.s = (gchar *) realloc(retval->value.s,34*sizeof(gchar *));
         snprintf(retval->value.s,34,"Interface moved to original state");
         */
         exit(0);
+#else
+        return OSSO_OK;
+#endif
     }
 
     if ( strcmp(method,"scan")==0 ) {
@@ -310,16 +361,99 @@ gint dbus_req_handler(const gchar * interface, const gchar * method,
     return OSSO_ERROR;
 }
 
+#ifndef HAVE_LIBOSSO
+
+dbus_bool_t stopped_service = FALSE;
+
+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 ( strcmp(dbus_message_get_member(message),"stop")==0 )
+                       stopped_service = TRUE;
+
+               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;
+#ifndef HAVE_LIBOSSO
+       DBusError error;
+#endif
 
        /* Initialize maemo application */
+#ifdef HAVE_LIBOSSO
        osso_context = osso_initialize(OSSO_NAME, WIFISCAND_VERSION_STRING, TRUE, NULL);
+#else
+       dbus_error_init(&error);
+       osso_context = dbus_bus_get(DBUS_BUS_SESSION, &error);
+#endif
 
        /* Check that initialization was ok */
        if (osso_context == NULL) {
+#ifndef HAVE_LIBOSSO
+               fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", error.message);
+               dbus_error_free (&error);
+#endif
                exit(OSSO_ERROR);
                }
 
@@ -333,19 +467,34 @@ int main( void ) {
        appdata->iface.sock   = 0;
 
        /* Add handler for hello D-BUS messages */
+#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, handler_wrapper, appdata);
+#endif
+
        if (result != OSSO_OK) {
+#ifdef HAVE_LIBOSSO
                osso_system_note_infoprint(appdata->osso_context, "Failure while setting OSSO callback", NULL);
+#endif
                return OSSO_ERROR;
        }
 
        /* INITIALIZATION FINISH */
 
+#ifdef HAVE_LIBOSSO
        event_loop = g_main_loop_new(NULL, FALSE);
        g_main_loop_run(event_loop);
+#else
+       while (dbus_connection_read_write_dispatch(osso_context, -1) && stopped_service==FALSE) {}
+#endif
 
        /* Deinitialize OSSO */
+#ifdef HAVE_LIBOSSO
        osso_deinitialize(osso_context);
+#else
+       dbus_deinitialize(osso_context);
+#endif
 
        exit(0);
 }