Include a configuration setting to specify the interval among wireless scans
[wifihood] / wifiscand / wifiscand.c
index 7351306..d0df690 100644 (file)
@@ -7,6 +7,25 @@
 #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
 
@@ -45,162 +64,63 @@ typedef struct {
 
 /* ------------------------------------------------------------------ WirelessInterface_ScanItem */
 
-static int WirelessInterface_ScanItem(struct iw_event* event, ScanInfo **scaninfo) {
-
-       switch(event->cmd) {
-                                 
-               case SIOCGIWAP: {
+void WirelessInterface_ScanItem(struct wireless_scan* ap, ScanInfo **scaninfo) {
 
                        *scaninfo = (ScanInfo*) malloc( sizeof(ScanInfo) );
                        memset(*scaninfo, 0, sizeof(ScanInfo) );
 
-                       iw_ether_ntop( (const struct ether_addr*)(event->u.ap_addr.sa_data), (*scaninfo)->mac);
-                       return 1;
-               }
+                       iw_sawap_ntop( &ap->ap_addr, (*scaninfo)->mac);
 
-               case IWEVQUAL: {
-                       (*scaninfo)->rssi = event->u.qual.level - 0x100;
-                       return 0;
-               }
+                       (*scaninfo)->rssi = ap->stats.qual.level - 0x100;
+                       (*scaninfo)->noise = ap->stats.qual.level - 0x100;
 
-               default: {
-                        return 0;
-                }
-       }
 }
 
 /* ---------------------------------------------------------------------- */
 
-void* makeScan(WirelessInterface* coso, gchar *message) {
-       struct iwreq          wrq;
-       unsigned char* buffer     = NULL;
-       int            buflen     = IW_SCAN_MAX_DATA;
+void* makeScan(WirelessInterface* wlan, gchar *message) {
+       wireless_scan_head context;
        iwrange        range;
-       int            has_range;
-       struct timeval        tv;
-       int            timeout    = 10000000;
+       int delay = 1;
 
        ScanInfo        **scan = NULL;
 
-       has_range = (iw_get_range_info(coso->sock, coso->ifname, &range) >= 0);
-
-       tv.tv_sec          = 0;
-       tv.tv_usec         = 250000;
-       wrq.u.data.pointer = NULL;
-       wrq.u.data.flags   = 0;
-       wrq.u.data.length  = 0;
-
-       if(iw_set_ext(coso->sock, coso->ifname, SIOCSIWSCAN, &wrq) < 0) {
-               if(errno != EPERM) {
-                       message = "Interface doesn't support scanning";
-                       return NULL;
-               }
-               tv.tv_usec = 0;
-       }
-       timeout -= tv.tv_usec;
-
-       while(1) {
-               fd_set rfds;
-               int    last_fd;
-               int    ret;
-
-               FD_ZERO(&rfds);
-               last_fd = -1;
+       iw_get_range_info(wlan->sock, wlan->ifname, &range);
 
-               ret = select(last_fd + 1, &rfds, NULL, NULL, &tv);
+       /* We don't call iw_scan to allow fine control of delays and timeouts */
+       context.result = NULL;
+       context.retry = 0;
 
-               if(ret < 0) {
-                       if(errno == EAGAIN || errno == EINTR) continue;
-                       
-                       else {
-                               message = "Unknown scanning error";
-                               return NULL;
-                       }
+       while(delay>0) {
+               delay = iw_process_scan(wlan->sock, wlan->ifname, range.we_version_compiled, &context);
+               if(delay < 0) break;
+               usleep(delay * 1000);
                }
 
-               if(!ret) {
-                       unsigned char* newbuf;
+       if ( delay == 0 ) {
 
-                       realloc:
-                       newbuf = (unsigned char*) realloc(buffer, buflen);
-                       
-                       if(!newbuf) {
-                               if(buffer) free(buffer);
-                               message = "Memory allocation failure in scan";
-                               return NULL;
-                       }
-                       
-                       buffer = newbuf;
-
-                       wrq.u.data.pointer = buffer;
-                       wrq.u.data.flags   = 0;
-                       wrq.u.data.length  = buflen;
-                       
-                       if(iw_get_ext(coso->sock, coso->ifname, SIOCGIWSCAN, &wrq) < 0) {
-                               if((errno == E2BIG)) {
-                                       if(wrq.u.data.length > buflen) buflen = wrq.u.data.length;
-                                       else buflen *= 2;
-
-                                       goto realloc;
-                               }
-
-                               if(errno == EAGAIN) {
-                                       tv.tv_sec   = 0;
-                                       tv.tv_usec  = 100000;
-                                       timeout    -= tv.tv_usec;
-                                       
-                                       if(timeout > 0) continue;
-                               }
-
-                               free(buffer);
-                               
-                               message = "Unable to read scan data";
-
-                               return NULL;
+               int i = 1; // To acount for the final NULL
+               struct wireless_scan *ap = context.result;
+               while ( ap != NULL ) {
+                                       ap = ap->next;
+                                       i++;
                        }
-                       
-                       else break;
-               }
-       }
+               scan = (ScanInfo**) malloc( i * sizeof(ScanInfo *) );
+               memset(scan, 0, i * sizeof(ScanInfo *) );
 
+               ScanInfo **sc = scan;
 
-       if(wrq.u.data.length)   {
-               struct iw_event      iwe;
-               stream_descr stream;
-               int          ret;
-               void*    scan_dict = NULL;
-               
-               iw_init_event_stream(&stream, (char*)(buffer), wrq.u.data.length);
-               
-               scan = (ScanInfo**) malloc( 25 * sizeof(ScanInfo *) );
-               memset(scan, 0, 25 * sizeof(ScanInfo *) );
-               *scan = NULL;
-               int i;
-               for (i=0; i<25; i++) *(scan+i) = NULL;
-
-               i = 0;
-               ScanInfo *sc = NULL;
-               do {
-                       ret = iw_extract_event_stream(&stream, &iwe, range.we_version_compiled);
-               
-                       if(ret > 0) {
-                               int sr = WirelessInterface_ScanItem(&iwe, &sc);
-                               if(sr && i<25) {
-                                       *(scan+i) = sc;
-                                       i++;
-                               }
+               /* Extract values */
+               ap = context.result;
+               while ( ap != NULL ) {
+                               WirelessInterface_ScanItem(ap, sc++);
+                                       ap = ap->next;
                        }
-               }
-               
-               while(ret > 0);
 
        } else {
                message = "Unknown error";
-               free(buffer);
-               return NULL;
        }
 
-       free(buffer);
        return scan;
 }
 
@@ -246,6 +166,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");
@@ -260,6 +188,17 @@ gint dbus_req_handler(const gchar * interface, const gchar * method,
             return OSSO_ERROR;
         }
 
+        /* Get range stuff */
+       iwrange        range;
+        int has_range = (iw_get_range_info(appdata->iface.sock, appdata->iface.ifname, &range) >= 0);
+
+        /* Check if the interface could support scanning. */
+        if((!has_range) || (range.we_version_compiled < 14)) {
+            retval->value.s = (gchar *) realloc(retval->value.s,35*sizeof(gchar *));
+            snprintf(retval->value.s,35,"Interface doesn't support scanning");
+            return OSSO_ERROR;
+        }
+
         struct ifreq frq;
         strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
         if(ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
@@ -314,15 +253,19 @@ gint dbus_req_handler(const gchar * interface, const gchar * method,
             return OSSO_ERROR;
         }
 
-        int i;
-        for (i=0; i<25&&*(scan+i)!=NULL; i++) {
-            ScanInfo* sc = *(scan+i);
-            retval->value.s = (gchar *) realloc(retval->value.s,23*(i+1)*sizeof(gchar *));
+        int i = 0;
+        while ( *(scan+i) != NULL ) i++;
+        retval->value.s = (gchar *) malloc( (22*i+1) * sizeof(gchar *) );
            if ( retval->value.s == NULL ) {
                 retval->value.s = "Error allocating memory";
                 return OSSO_ERROR;
-           }
-            sprintf(retval->value.s+strlen(retval->value.s),"%s:%d ",sc->mac,sc->rssi);
+               }
+        memset(retval->value.s, '\0', (22*i+1) * sizeof(gchar *) );
+        i = 0;
+        while ( *scan != NULL ) {
+            sprintf(retval->value.s+(22*i),"%s:%d ",(*scan)->mac,(*scan)->rssi);
+            scan++;
+            i++;
         }
 
         retval->value.s[strlen(retval->value.s)-1]  = '\0';
@@ -418,7 +361,7 @@ int main( void ) {
        osso_context = osso_initialize(OSSO_NAME, WIFISCAND_VERSION_STRING, TRUE, NULL);
 #else
        dbus_error_init(&error);
-       osso_context = dbus_bus_get(DBUS_BUS_STARTER, &error);
+       osso_context = dbus_bus_get(DBUS_BUS_SESSION, &error);
 #endif
 
        /* Check that initialization was ok */