nexus: Add TiwlanEventListener for reading driver events directly

Signed-off-by: San Mehat <san@google.com>
diff --git a/nexus/TiwlanWifiController.cpp b/nexus/TiwlanWifiController.cpp
index 6945e3e..61535c3 100644
--- a/nexus/TiwlanWifiController.cpp
+++ b/nexus/TiwlanWifiController.cpp
@@ -18,6 +18,9 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
 
 #include <cutils/properties.h>
 #define LOG_TAG "TiwlanWifiController"
@@ -25,6 +28,7 @@
 
 #include "PropertyManager.h"
 #include "TiwlanWifiController.h"
+#include "TiwlanEventListener.h"
 
 #define DRIVER_PROP_NAME "wlan.driver.status"
 
@@ -36,6 +40,8 @@
                                            char *modargs) :
                       WifiController(propmngr, handlers, modpath, modname,
                                      modargs) {
+    mEventListener = NULL;
+    mListenerSock = -1;
 }
 
 int TiwlanWifiController::powerUp() {
@@ -43,6 +49,13 @@
 }
 
 int TiwlanWifiController::powerDown() {
+    if (mEventListener) {
+        delete mEventListener;
+        close(mListenerSock);
+        mListenerSock = -1;
+        mEventListener = NULL;
+    }
+   
     return 0; // Powerdown is currently done when the driver is unloaded
 }
 
@@ -60,17 +73,56 @@
     // Wait for driver to be ready
     while (count-- > 0) {
         if (property_get(DRIVER_PROP_NAME, driver_status, NULL)) {
-            if (strcmp(driver_status, "ok") == 0)
+            if (!strcmp(driver_status, "ok")) {
+                LOGD("Firmware loaded OK");
+
+                if (startDriverEventListener()) {
+                    LOGW("Failed to start driver event listener");
+                }
+
                 return 0;
-            else if (strcmp(DRIVER_PROP_NAME, "failed") == 0)
+            } else if (!strcmp(DRIVER_PROP_NAME, "failed")) {
+                LOGE("Firmware load failed");
                 return -1;
+            }
         }
         usleep(200000);
     }
     property_set(DRIVER_PROP_NAME, "timeout");
+    LOGE("Firmware load timed out");
     return -1;
 }
 
+int TiwlanWifiController::startDriverEventListener() {
+    struct sockaddr_in addr;
+    int s;
+
+    if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
+        return -1;
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = htonl(INADDR_ANY);
+    addr.sin_port = htons(TI_DRIVER_MSG_PORT);
+
+    if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+        close(s);
+        return -1;
+    }
+
+    mEventListener = new TiwlanEventListener(s);
+
+    if (mEventListener->startListener()) {
+        LOGE("Error starting driver listener (%s)", strerror(errno));
+        delete mEventListener;
+        mEventListener = NULL;
+        close(s);
+        return -1;
+    }
+    mListenerSock = s;
+    return 0;
+}
+
 bool TiwlanWifiController::isFirmwareLoaded() {
     // Always load the firmware
     return false;