SSB-8693 slsi hal library code for GSCAN
authorj.sangaru <j.sangaru@samsung.com>
Fri, 30 Oct 2015 10:58:57 +0000 (16:28 +0530)
committerTarun Karela <t.karela@samsung.com>
Mon, 4 Jun 2018 09:40:47 +0000 (10:40 +0100)
slsi hal library code
see http://12.36.211.73:8089/#/c/108073/

SCSC-Bug-Id: SSB-8693

Change-Id: Ib1826f3f6f6da7e0b961b0f02130780fc31865fc
Signed-off-by: j.sangaru <j.sangaru@samsung.com>
Android.mk [new file with mode: 0755]
common.cpp [new file with mode: 0755]
common.h [new file with mode: 0755]
cpp_bindings.cpp [new file with mode: 0755]
cpp_bindings.h [new file with mode: 0755]
gscan.cpp [new file with mode: 0755]
link_layer_stats.cpp [new file with mode: 0755]
rtt.cpp [new file with mode: 0755]
sync.h [new file with mode: 0755]
wifi_hal.cpp [new file with mode: 0755]

diff --git a/Android.mk b/Android.mk
new file mode 100755 (executable)
index 0000000..4bd17fa
--- /dev/null
@@ -0,0 +1,29 @@
+#############################################################################
+#
+# Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd
+#
+#############################################################################
+LOCAL_PATH := $(call my-dir)
+
+# Make the HAL library
+# ============================================================
+include $(CLEAR_VARS)
+
+LOCAL_CFLAGS := -Wno-unused-parameter
+
+LOCAL_C_INCLUDES += \
+        external/libnl/include \
+        $(call include-path-for, libhardware_legacy)/hardware_legacy \
+        external/wpa_supplicant_8/src/drivers
+
+LOCAL_SRC_FILES := \
+       wifi_hal.cpp \
+       rtt.cpp \
+       common.cpp \
+       cpp_bindings.cpp \
+       gscan.cpp \
+       link_layer_stats.cpp
+
+LOCAL_MODULE := libwifi-hal-slsi
+
+include $(BUILD_STATIC_LIBRARY)
diff --git a/common.cpp b/common.cpp
new file mode 100755 (executable)
index 0000000..c35f3fe
--- /dev/null
@@ -0,0 +1,214 @@
+\r
+#include <stdlib.h>\r
+#include <linux/pkt_sched.h>\r
+#include <netlink/object-api.h>\r
+#include <netlink/handlers.h>\r
+#include <linux/types.h>\r
+\r
+#include "wifi_hal.h"\r
+#include "common.h"\r
+\r
+interface_info *getIfaceInfo(wifi_interface_handle handle)\r
+{\r
+    return (interface_info *)handle;\r
+}\r
+\r
+wifi_handle getWifiHandle(wifi_interface_handle handle)\r
+{\r
+    return getIfaceInfo(handle)->handle;\r
+}\r
+\r
+hal_info *getHalInfo(wifi_handle handle)\r
+{\r
+    return (hal_info *)handle;\r
+}\r
+\r
+hal_info *getHalInfo(wifi_interface_handle handle)\r
+{\r
+    return getHalInfo(getWifiHandle(handle));\r
+}\r
+\r
+wifi_handle getWifiHandle(hal_info *info)\r
+{\r
+    return (wifi_handle)info;\r
+}\r
+\r
+wifi_interface_handle getIfaceHandle(interface_info *info)\r
+{\r
+    return (wifi_interface_handle)info;\r
+}\r
+\r
+wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+    /* TODO: check for multiple handlers? */\r
+    pthread_mutex_lock(&info->cb_lock);\r
+\r
+    wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;\r
+\r
+    if (info->num_event_cb < info->alloc_event_cb) {\r
+        info->event_cb[info->num_event_cb].nl_cmd  = cmd;\r
+        info->event_cb[info->num_event_cb].vendor_id  = 0;\r
+        info->event_cb[info->num_event_cb].vendor_subcmd  = 0;\r
+        info->event_cb[info->num_event_cb].cb_func = func;\r
+        info->event_cb[info->num_event_cb].cb_arg  = arg;\r
+        ALOGI("Successfully added event handler %p:%p for command %d at %d",\r
+                arg, func, cmd, info->num_event_cb);\r
+        info->num_event_cb++;\r
+        result = WIFI_SUCCESS;\r
+    }\r
+\r
+    pthread_mutex_unlock(&info->cb_lock);\r
+    return result;\r
+}\r
+\r
+wifi_error wifi_register_vendor_handler(wifi_handle handle,\r
+        uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+//ALOGD("GSCAN register handle wifi_register_vendor_handler %p", handle);\r
+    /* TODO: check for multiple handlers? */\r
+    pthread_mutex_lock(&info->cb_lock);\r
+        ALOGI("Added event handler %p", info);\r
+\r
+    wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;\r
+\r
+       //    ALOGD("register_vendor_handler: handle = %p", handle);\r
+    if (info->num_event_cb < info->alloc_event_cb) {\r
+        info->event_cb[info->num_event_cb].nl_cmd  = NL80211_CMD_VENDOR;\r
+        info->event_cb[info->num_event_cb].vendor_id  = id;\r
+        info->event_cb[info->num_event_cb].vendor_subcmd  = subcmd;\r
+        info->event_cb[info->num_event_cb].cb_func = func;\r
+        info->event_cb[info->num_event_cb].cb_arg  = arg;\r
+        ALOGI("Added event handler %p:%p for vendor 0x%0x and subcmd 0x%0x at %d",\r
+                arg, func, id, subcmd, info->num_event_cb);\r
+        info->num_event_cb++;\r
+        result = WIFI_SUCCESS;\r
+    }\r
+\r
+    pthread_mutex_unlock(&info->cb_lock);\r
+    return result;\r
+}\r
+\r
+void wifi_unregister_handler(wifi_handle handle, int cmd)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+    if (cmd == NL80211_CMD_VENDOR) {\r
+        ALOGE("Must use wifi_unregister_vendor_handler to remove vendor handlers");\r
+        return;\r
+    }\r
+\r
+    pthread_mutex_lock(&info->cb_lock);\r
+\r
+    for (int i = 0; i < info->num_event_cb; i++) {\r
+        if (info->event_cb[i].nl_cmd == cmd) {\r
+            ALOGI("Successfully removed event handler %p:%p for cmd = 0x%0x from %d",\r
+                    info->event_cb[i].cb_arg, info->event_cb[i].cb_func, cmd, i);\r
+\r
+            memmove(&info->event_cb[i], &info->event_cb[i+1],\r
+                (info->num_event_cb - i - 1) * sizeof(cb_info));\r
+            info->num_event_cb--;\r
+            break;\r
+        }\r
+    }\r
+\r
+    pthread_mutex_unlock(&info->cb_lock);\r
+}\r
+\r
+void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+    pthread_mutex_lock(&info->cb_lock);\r
+\r
+    for (int i = 0; i < info->num_event_cb; i++) {\r
+\r
+        if (info->event_cb[i].nl_cmd == NL80211_CMD_VENDOR\r
+                && info->event_cb[i].vendor_id == id\r
+                && info->event_cb[i].vendor_subcmd == subcmd) {\r
+            ALOGI("Successfully removed event handler %p:%p for vendor 0x%0x, subcmd 0x%0x from %d",\r
+                    info->event_cb[i].cb_arg, info->event_cb[i].cb_func, id, subcmd, i);\r
+            memmove(&info->event_cb[i], &info->event_cb[i+1],\r
+                (info->num_event_cb - i - 1) * sizeof(cb_info));\r
+            info->num_event_cb--;\r
+            break;\r
+        }\r
+    }\r
+\r
+    pthread_mutex_unlock(&info->cb_lock);\r
+}\r
+\r
+\r
+wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+    ALOGD("registering command %d", id);\r
+\r
+    wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;\r
+\r
+    if (info->num_cmd < info->alloc_cmd) {\r
+        info->cmd[info->num_cmd].id   = id;\r
+        info->cmd[info->num_cmd].cmd  = cmd;\r
+        ALOGI("Successfully added command %d: %p at %d", id, cmd, info->num_cmd);\r
+        info->num_cmd++;\r
+        result = WIFI_SUCCESS;\r
+    }\r
+\r
+    return result;\r
+}\r
+\r
+WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+    ALOGD("un-registering command %d", id);\r
+\r
+    WifiCommand *cmd = NULL;\r
+\r
+    for (int i = 0; i < info->num_cmd; i++) {\r
+        if (info->cmd[i].id == id) {\r
+            cmd = info->cmd[i].cmd;\r
+            memmove(&info->cmd[i], &info->cmd[i+1], (info->num_cmd - i) * sizeof(cmd_info));\r
+            info->num_cmd--;\r
+            ALOGI("Successfully removed command %d: %p from %d", id, cmd, i);\r
+            break;\r
+        }\r
+    }\r
+\r
+    return cmd;\r
+}\r
+\r
+WifiCommand *wifi_get_cmd(wifi_handle handle, int id)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+    WifiCommand *cmd = NULL;\r
+\r
+    for (int i = 0; i < info->num_cmd; i++) {\r
+        if (info->cmd[i].id == id) {\r
+            cmd = info->cmd[i].cmd;\r
+            break;\r
+        }\r
+    }\r
+\r
+    return cmd;\r
+}\r
+\r
+void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+    for (int i = 0; i < info->num_cmd; i++) {\r
+        if (info->cmd[i].cmd == cmd) {\r
+            int id = info->cmd[i].id;\r
+            memmove(&info->cmd[i], &info->cmd[i+1], (info->num_cmd - i) * sizeof(cmd_info));\r
+            info->num_cmd--;\r
+            ALOGI("Successfully removed command %d: %p from %d", id, cmd, i);\r
+            break;\r
+        }\r
+    }\r
+}\r
diff --git a/common.h b/common.h
new file mode 100755 (executable)
index 0000000..5cfa92a
--- /dev/null
+++ b/common.h
@@ -0,0 +1,163 @@
+\r
+#include "wifi_hal.h"\r
+\r
+#ifndef __WIFI_HAL_COMMON_H__\r
+#define __WIFI_HAL_COMMON_H__\r
+\r
+#define LOG_TAG  "WifiHAL"\r
+\r
+#include <utils/Log.h>\r
+#include "nl80211_copy.h"\r
+#include "sync.h"\r
+\r
+#define SOCKET_BUFFER_SIZE      (32768U)\r
+#define RECV_BUF_SIZE           (4096)\r
+#define DEFAULT_EVENT_CB_SIZE   (64)\r
+#define DEFAULT_CMD_SIZE        (64)\r
+#define DOT11_OUI_LEN             3\r
+\r
+/*\r
+ Vendor OUI - This is a unique identifier that identifies organization. Lets\r
+ code Android specific functions with Google OUI; although vendors can do more\r
+ with their own OUI's as well.\r
+ */\r
+\r
+const uint32_t GOOGLE_OUI = 0x001A11;\r
+/* TODO: define vendor OUI here */\r
+\r
+\r
+/*\r
+ This enum defines ranges for various commands; commands themselves\r
+ can be defined in respective feature headers; i.e. find gscan command\r
+ definitions in gscan.cpp\r
+ */\r
+\r
+typedef enum {\r
+    /* don't use 0 as a valid subcommand */\r
+    VENDOR_NL80211_SUBCMD_UNSPECIFIED,\r
+\r
+    /* define all vendor startup commands between 0x0 and 0x0FFF */\r
+    VENDOR_NL80211_SUBCMD_RANGE_START = 0x0001,\r
+    VENDOR_NL80211_SUBCMD_RANGE_END   = 0x0FFF,\r
+\r
+    /* define all GScan related commands between 0x1000 and 0x10FF */\r
+    ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START = 0x1000,\r
+    ANDROID_NL80211_SUBCMD_GSCAN_RANGE_END   = 0x10FF,\r
+\r
+    /* define all NearbyDiscovery related commands between 0x1100 and 0x11FF */\r
+    ANDROID_NL80211_SUBCMD_NBD_RANGE_START = 0x1100,\r
+    ANDROID_NL80211_SUBCMD_NBD_RANGE_END   = 0x11FF,\r
+\r
+    /* define all RTT related commands between 0x1100 and 0x11FF */\r
+    ANDROID_NL80211_SUBCMD_RTT_RANGE_START = 0x1100,\r
+    ANDROID_NL80211_SUBCMD_RTT_RANGE_END   = 0x11FF,\r
+\r
+    ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START = 0x1200,\r
+    ANDROID_NL80211_SUBCMD_LSTATS_RANGE_END   = 0x12FF,\r
+\r
+    /* This is reserved for future usage */\r
+\r
+} ANDROID_VENDOR_SUB_COMMAND;\r
+\r
+typedef enum {\r
+    SLSI_NL80211_VENDOR_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START,\r
+    SLSI_NL80211_VENDOR_SUBCMD_GET_VALID_CHANNELS,\r
+    SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN,\r
+    SLSI_NL80211_VENDOR_SUBCMD_DEL_GSCAN,\r
+    SLSI_NL80211_VENDOR_SUBCMD_GET_SCAN_RESULTS,\r
+    SLSI_NL80211_VENDOR_SUBCMD_SET_BSSID_HOTLIST,\r
+    SLSI_NL80211_VENDOR_SUBCMD_RESET_BSSID_HOTLIST,\r
+    SLSI_NL80211_VENDOR_SUBCMD_GET_HOTLIST_RESULTS,\r
+    SLSI_NL80211_VENDOR_SUBCMD_SET_SIGNIFICANT_CHANGE,\r
+    SLSI_NL80211_VENDOR_SUBCMD_RESET_SIGNIFICANT_CHANGE,\r
+    SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI,\r
+    SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS\r
+} WIFI_SUB_COMMAND;\r
+\r
+typedef enum {\r
+    GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS ,\r
+    GSCAN_EVENT_HOTLIST_RESULTS_FOUND,\r
+    GSCAN_EVENT_SCAN_RESULTS_AVAILABLE,\r
+    GSCAN_EVENT_FULL_SCAN_RESULTS,\r
+    GSCAN_EVENT_COMPLETE_SCAN,\r
+    GSCAN_EVENT_HOTLIST_RESULTS_LOST\r
+} WIFI_EVENT;\r
+\r
+typedef void (*wifi_internal_event_handler) (wifi_handle handle, int events);\r
+\r
+class WifiCommand;\r
+\r
+typedef struct {\r
+    int nl_cmd;\r
+    uint32_t vendor_id;\r
+    int vendor_subcmd;\r
+    nl_recvmsg_msg_cb_t cb_func;\r
+    void *cb_arg;\r
+} cb_info;\r
+\r
+typedef struct {\r
+    wifi_request_id id;\r
+    WifiCommand *cmd;\r
+} cmd_info;\r
+\r
+typedef struct {\r
+    wifi_handle handle;                             // handle to wifi data\r
+    char name[8+1];                                 // interface name + trailing null\r
+    int  id;                                        // id to use when talking to driver\r
+} interface_info;\r
+\r
+typedef struct {\r
+\r
+    struct nl_sock *cmd_sock;                       // command socket object\r
+    struct nl_sock *event_sock;                     // event socket object\r
+    int nl80211_family_id;                          // family id for 80211 driver\r
+\r
+    bool in_event_loop;                             // Indicates that event loop is active\r
+    bool clean_up;                                  // Indication to clean up the socket\r
+\r
+    wifi_internal_event_handler event_handler;      // default event handler\r
+    wifi_cleaned_up_handler cleaned_up_handler;     // socket cleaned up handler\r
+\r
+    cb_info *event_cb;                              // event callbacks\r
+    int num_event_cb;                               // number of event callbacks\r
+    int alloc_event_cb;                             // number of allocated callback objects\r
+    pthread_mutex_t cb_lock;                        // mutex for the event_cb access\r
+\r
+    cmd_info *cmd;                                  // Outstanding commands\r
+    int num_cmd;                                    // number of commands\r
+    int alloc_cmd;                                  // number of commands allocated\r
+\r
+    interface_info **interfaces;                    // array of interfaces\r
+    int num_interfaces;                             // number of interfaces\r
+\r
+\r
+    // add other details\r
+} hal_info;\r
+\r
+wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg);\r
+wifi_error wifi_register_vendor_handler(wifi_handle handle,\r
+            uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg);\r
+\r
+void wifi_unregister_handler(wifi_handle handle, int cmd);\r
+void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd);\r
+\r
+wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd);\r
+WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id);\r
+WifiCommand *wifi_get_cmd(wifi_handle handle, int id);\r
+void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd);\r
+\r
+interface_info *getIfaceInfo(wifi_interface_handle);\r
+wifi_handle getWifiHandle(wifi_interface_handle handle);\r
+hal_info *getHalInfo(wifi_handle handle);\r
+hal_info *getHalInfo(wifi_interface_handle handle);\r
+wifi_handle getWifiHandle(hal_info *info);\r
+wifi_interface_handle getIfaceHandle(interface_info *info);\r
+\r
+\r
+// some common macros\r
+\r
+#define min(x, y)       ((x) < (y) ? (x) : (y))\r
+#define max(x, y)       ((x) > (y) ? (x) : (y))\r
+\r
+#endif\r
+\r
diff --git a/cpp_bindings.cpp b/cpp_bindings.cpp
new file mode 100755 (executable)
index 0000000..bcfebca
--- /dev/null
@@ -0,0 +1,706 @@
+\r
+#include <stdint.h>\r
+#include <fcntl.h>\r
+#include <sys/socket.h>\r
+#include <netlink/genl/genl.h>\r
+#include <netlink/genl/family.h>\r
+#include <netlink/genl/ctrl.h>\r
+#include <linux/rtnetlink.h>\r
+#include <netpacket/packet.h>\r
+#include <linux/filter.h>\r
+#include <linux/errqueue.h>\r
+\r
+#include <linux/pkt_sched.h>\r
+#include <netlink/object-api.h>\r
+#include <netlink/netlink.h>\r
+#include <netlink/socket.h>\r
+#include <netlink/handlers.h>\r
+#include <stdarg.h>\r
+\r
+#include <ctype.h>\r
+\r
+#include "wifi_hal.h"\r
+#include "common.h"\r
+#include "cpp_bindings.h"\r
+\r
+void appendFmt(char *buf, int &offset, const char *fmt, ...)\r
+{\r
+    va_list params;\r
+    va_start(params, fmt);\r
+    offset += vsprintf(buf + offset, fmt, params);\r
+    va_end(params);\r
+}\r
+\r
+#define C2S(x)  case x: return #x;\r
+\r
+static const char *cmdToString(int cmd)\r
+{\r
+       switch (cmd) {\r
+       C2S(NL80211_CMD_UNSPEC)\r
+       C2S(NL80211_CMD_GET_WIPHY)\r
+       C2S(NL80211_CMD_SET_WIPHY)\r
+       C2S(NL80211_CMD_NEW_WIPHY)\r
+       C2S(NL80211_CMD_DEL_WIPHY)\r
+       C2S(NL80211_CMD_GET_INTERFACE)\r
+       C2S(NL80211_CMD_SET_INTERFACE)\r
+       C2S(NL80211_CMD_NEW_INTERFACE)\r
+       C2S(NL80211_CMD_DEL_INTERFACE)\r
+       C2S(NL80211_CMD_GET_KEY)\r
+       C2S(NL80211_CMD_SET_KEY)\r
+       C2S(NL80211_CMD_NEW_KEY)\r
+       C2S(NL80211_CMD_DEL_KEY)\r
+       C2S(NL80211_CMD_GET_BEACON)\r
+       C2S(NL80211_CMD_SET_BEACON)\r
+       C2S(NL80211_CMD_START_AP)\r
+       C2S(NL80211_CMD_STOP_AP)\r
+       C2S(NL80211_CMD_GET_STATION)\r
+       C2S(NL80211_CMD_SET_STATION)\r
+       C2S(NL80211_CMD_NEW_STATION)\r
+       C2S(NL80211_CMD_DEL_STATION)\r
+       C2S(NL80211_CMD_GET_MPATH)\r
+       C2S(NL80211_CMD_SET_MPATH)\r
+       C2S(NL80211_CMD_NEW_MPATH)\r
+       C2S(NL80211_CMD_DEL_MPATH)\r
+       C2S(NL80211_CMD_SET_BSS)\r
+       C2S(NL80211_CMD_SET_REG)\r
+       C2S(NL80211_CMD_REQ_SET_REG)\r
+       C2S(NL80211_CMD_GET_MESH_CONFIG)\r
+       C2S(NL80211_CMD_SET_MESH_CONFIG)\r
+       C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)\r
+       C2S(NL80211_CMD_GET_REG)\r
+       C2S(NL80211_CMD_GET_SCAN)\r
+       C2S(NL80211_CMD_TRIGGER_SCAN)\r
+       C2S(NL80211_CMD_NEW_SCAN_RESULTS)\r
+       C2S(NL80211_CMD_SCAN_ABORTED)\r
+       C2S(NL80211_CMD_REG_CHANGE)\r
+       C2S(NL80211_CMD_AUTHENTICATE)\r
+       C2S(NL80211_CMD_ASSOCIATE)\r
+       C2S(NL80211_CMD_DEAUTHENTICATE)\r
+       C2S(NL80211_CMD_DISASSOCIATE)\r
+       C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)\r
+       C2S(NL80211_CMD_REG_BEACON_HINT)\r
+       C2S(NL80211_CMD_JOIN_IBSS)\r
+       C2S(NL80211_CMD_LEAVE_IBSS)\r
+       C2S(NL80211_CMD_TESTMODE)\r
+       C2S(NL80211_CMD_CONNECT)\r
+       C2S(NL80211_CMD_ROAM)\r
+       C2S(NL80211_CMD_DISCONNECT)\r
+       C2S(NL80211_CMD_SET_WIPHY_NETNS)\r
+       C2S(NL80211_CMD_GET_SURVEY)\r
+       C2S(NL80211_CMD_NEW_SURVEY_RESULTS)\r
+       C2S(NL80211_CMD_SET_PMKSA)\r
+       C2S(NL80211_CMD_DEL_PMKSA)\r
+       C2S(NL80211_CMD_FLUSH_PMKSA)\r
+       C2S(NL80211_CMD_REMAIN_ON_CHANNEL)\r
+       C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)\r
+       C2S(NL80211_CMD_SET_TX_BITRATE_MASK)\r
+       C2S(NL80211_CMD_REGISTER_FRAME)\r
+       C2S(NL80211_CMD_FRAME)\r
+       C2S(NL80211_CMD_FRAME_TX_STATUS)\r
+       C2S(NL80211_CMD_SET_POWER_SAVE)\r
+       C2S(NL80211_CMD_GET_POWER_SAVE)\r
+       C2S(NL80211_CMD_SET_CQM)\r
+       C2S(NL80211_CMD_NOTIFY_CQM)\r
+       C2S(NL80211_CMD_SET_CHANNEL)\r
+       C2S(NL80211_CMD_SET_WDS_PEER)\r
+       C2S(NL80211_CMD_FRAME_WAIT_CANCEL)\r
+       C2S(NL80211_CMD_JOIN_MESH)\r
+       C2S(NL80211_CMD_LEAVE_MESH)\r
+       C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)\r
+       C2S(NL80211_CMD_UNPROT_DISASSOCIATE)\r
+       C2S(NL80211_CMD_NEW_PEER_CANDIDATE)\r
+       C2S(NL80211_CMD_GET_WOWLAN)\r
+       C2S(NL80211_CMD_SET_WOWLAN)\r
+       C2S(NL80211_CMD_START_SCHED_SCAN)\r
+       C2S(NL80211_CMD_STOP_SCHED_SCAN)\r
+       C2S(NL80211_CMD_SCHED_SCAN_RESULTS)\r
+       C2S(NL80211_CMD_SCHED_SCAN_STOPPED)\r
+       C2S(NL80211_CMD_SET_REKEY_OFFLOAD)\r
+       C2S(NL80211_CMD_PMKSA_CANDIDATE)\r
+       C2S(NL80211_CMD_TDLS_OPER)\r
+       C2S(NL80211_CMD_TDLS_MGMT)\r
+       C2S(NL80211_CMD_UNEXPECTED_FRAME)\r
+       C2S(NL80211_CMD_PROBE_CLIENT)\r
+       C2S(NL80211_CMD_REGISTER_BEACONS)\r
+       C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)\r
+       C2S(NL80211_CMD_SET_NOACK_MAP)\r
+       C2S(NL80211_CMD_CH_SWITCH_NOTIFY)\r
+       C2S(NL80211_CMD_START_P2P_DEVICE)\r
+       C2S(NL80211_CMD_STOP_P2P_DEVICE)\r
+       C2S(NL80211_CMD_CONN_FAILED)\r
+       C2S(NL80211_CMD_SET_MCAST_RATE)\r
+       C2S(NL80211_CMD_SET_MAC_ACL)\r
+       C2S(NL80211_CMD_RADAR_DETECT)\r
+       C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)\r
+       C2S(NL80211_CMD_UPDATE_FT_IES)\r
+       C2S(NL80211_CMD_FT_EVENT)\r
+       C2S(NL80211_CMD_CRIT_PROTOCOL_START)\r
+       C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)\r
+       C2S(NL80211_CMD_VENDOR)\r
+       default:\r
+               return "NL80211_CMD_UNKNOWN";\r
+       }\r
+}\r
+\r
+const char *attributeToString(int attribute)\r
+{\r
+    switch (attribute) {\r
+       C2S(NL80211_ATTR_UNSPEC)\r
+\r
+       C2S(NL80211_ATTR_WIPHY)\r
+       C2S(NL80211_ATTR_WIPHY_NAME)\r
+\r
+       C2S(NL80211_ATTR_IFINDEX)\r
+       C2S(NL80211_ATTR_IFNAME)\r
+       C2S(NL80211_ATTR_IFTYPE)\r
+\r
+       C2S(NL80211_ATTR_MAC)\r
+\r
+       C2S(NL80211_ATTR_KEY_DATA)\r
+       C2S(NL80211_ATTR_KEY_IDX)\r
+       C2S(NL80211_ATTR_KEY_CIPHER)\r
+       C2S(NL80211_ATTR_KEY_SEQ)\r
+       C2S(NL80211_ATTR_KEY_DEFAULT)\r
+\r
+       C2S(NL80211_ATTR_BEACON_INTERVAL)\r
+       C2S(NL80211_ATTR_DTIM_PERIOD)\r
+       C2S(NL80211_ATTR_BEACON_HEAD)\r
+       C2S(NL80211_ATTR_BEACON_TAIL)\r
+\r
+       C2S(NL80211_ATTR_STA_AID)\r
+       C2S(NL80211_ATTR_STA_FLAGS)\r
+       C2S(NL80211_ATTR_STA_LISTEN_INTERVAL)\r
+       C2S(NL80211_ATTR_STA_SUPPORTED_RATES)\r
+       C2S(NL80211_ATTR_STA_VLAN)\r
+       C2S(NL80211_ATTR_STA_INFO)\r
+\r
+       C2S(NL80211_ATTR_WIPHY_BANDS)\r
+\r
+       C2S(NL80211_ATTR_MNTR_FLAGS)\r
+\r
+       C2S(NL80211_ATTR_MESH_ID)\r
+       C2S(NL80211_ATTR_STA_PLINK_ACTION)\r
+       C2S(NL80211_ATTR_MPATH_NEXT_HOP)\r
+       C2S(NL80211_ATTR_MPATH_INFO)\r
+\r
+       C2S(NL80211_ATTR_BSS_CTS_PROT)\r
+       C2S(NL80211_ATTR_BSS_SHORT_PREAMBLE)\r
+       C2S(NL80211_ATTR_BSS_SHORT_SLOT_TIME)\r
+\r
+       C2S(NL80211_ATTR_HT_CAPABILITY)\r
+\r
+       C2S(NL80211_ATTR_SUPPORTED_IFTYPES)\r
+\r
+       C2S(NL80211_ATTR_REG_ALPHA2)\r
+       C2S(NL80211_ATTR_REG_RULES)\r
+\r
+       C2S(NL80211_ATTR_MESH_CONFIG)\r
+\r
+       C2S(NL80211_ATTR_BSS_BASIC_RATES)\r
+\r
+       C2S(NL80211_ATTR_WIPHY_TXQ_PARAMS)\r
+       C2S(NL80211_ATTR_WIPHY_FREQ)\r
+       C2S(NL80211_ATTR_WIPHY_CHANNEL_TYPE)\r
+\r
+       C2S(NL80211_ATTR_KEY_DEFAULT_MGMT)\r
+\r
+       C2S(NL80211_ATTR_MGMT_SUBTYPE)\r
+       C2S(NL80211_ATTR_IE)\r
+\r
+       C2S(NL80211_ATTR_MAX_NUM_SCAN_SSIDS)\r
+\r
+       C2S(NL80211_ATTR_SCAN_FREQUENCIES)\r
+       C2S(NL80211_ATTR_SCAN_SSIDS)\r
+       C2S(NL80211_ATTR_GENERATION) /* replaces old SCAN_GENERATION */\r
+       C2S(NL80211_ATTR_BSS)\r
+\r
+       C2S(NL80211_ATTR_REG_INITIATOR)\r
+       C2S(NL80211_ATTR_REG_TYPE)\r
+\r
+       C2S(NL80211_ATTR_SUPPORTED_COMMANDS)\r
+\r
+       C2S(NL80211_ATTR_FRAME)\r
+       C2S(NL80211_ATTR_SSID)\r
+       C2S(NL80211_ATTR_AUTH_TYPE)\r
+       C2S(NL80211_ATTR_REASON_CODE)\r
+\r
+       C2S(NL80211_ATTR_KEY_TYPE)\r
+\r
+       C2S(NL80211_ATTR_MAX_SCAN_IE_LEN)\r
+       C2S(NL80211_ATTR_CIPHER_SUITES)\r
+\r
+       C2S(NL80211_ATTR_FREQ_BEFORE)\r
+       C2S(NL80211_ATTR_FREQ_AFTER)\r
+\r
+       C2S(NL80211_ATTR_FREQ_FIXED)\r
+\r
+\r
+       C2S(NL80211_ATTR_WIPHY_RETRY_SHORT)\r
+       C2S(NL80211_ATTR_WIPHY_RETRY_LONG)\r
+       C2S(NL80211_ATTR_WIPHY_FRAG_THRESHOLD)\r
+       C2S(NL80211_ATTR_WIPHY_RTS_THRESHOLD)\r
+\r
+       C2S(NL80211_ATTR_TIMED_OUT)\r
+\r
+       C2S(NL80211_ATTR_USE_MFP)\r
+\r
+       C2S(NL80211_ATTR_STA_FLAGS2)\r
+\r
+       C2S(NL80211_ATTR_CONTROL_PORT)\r
+\r
+       C2S(NL80211_ATTR_TESTDATA)\r
+\r
+       C2S(NL80211_ATTR_PRIVACY)\r
+\r
+       C2S(NL80211_ATTR_DISCONNECTED_BY_AP)\r
+       C2S(NL80211_ATTR_STATUS_CODE)\r
+\r
+       C2S(NL80211_ATTR_CIPHER_SUITES_PAIRWISE)\r
+       C2S(NL80211_ATTR_CIPHER_SUITE_GROUP)\r
+       C2S(NL80211_ATTR_WPA_VERSIONS)\r
+       C2S(NL80211_ATTR_AKM_SUITES)\r
+\r
+       C2S(NL80211_ATTR_REQ_IE)\r
+       C2S(NL80211_ATTR_RESP_IE)\r
+\r
+       C2S(NL80211_ATTR_PREV_BSSID)\r
+\r
+       C2S(NL80211_ATTR_KEY)\r
+       C2S(NL80211_ATTR_KEYS)\r
+\r
+       C2S(NL80211_ATTR_PID)\r
+\r
+       C2S(NL80211_ATTR_4ADDR)\r
+\r
+       C2S(NL80211_ATTR_SURVEY_INFO)\r
+\r
+       C2S(NL80211_ATTR_PMKID)\r
+       C2S(NL80211_ATTR_MAX_NUM_PMKIDS)\r
+\r
+       C2S(NL80211_ATTR_DURATION)\r
+\r
+       C2S(NL80211_ATTR_COOKIE)\r
+\r
+       C2S(NL80211_ATTR_WIPHY_COVERAGE_CLASS)\r
+\r
+       C2S(NL80211_ATTR_TX_RATES)\r
+\r
+       C2S(NL80211_ATTR_FRAME_MATCH)\r
+\r
+       C2S(NL80211_ATTR_ACK)\r
+\r
+       C2S(NL80211_ATTR_PS_STATE)\r
+\r
+       C2S(NL80211_ATTR_CQM)\r
+\r
+       C2S(NL80211_ATTR_LOCAL_STATE_CHANGE)\r
+\r
+       C2S(NL80211_ATTR_AP_ISOLATE)\r
+\r
+       C2S(NL80211_ATTR_WIPHY_TX_POWER_SETTING)\r
+       C2S(NL80211_ATTR_WIPHY_TX_POWER_LEVEL)\r
+\r
+       C2S(NL80211_ATTR_TX_FRAME_TYPES)\r
+       C2S(NL80211_ATTR_RX_FRAME_TYPES)\r
+       C2S(NL80211_ATTR_FRAME_TYPE)\r
+\r
+       C2S(NL80211_ATTR_CONTROL_PORT_ETHERTYPE)\r
+       C2S(NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)\r
+\r
+       C2S(NL80211_ATTR_SUPPORT_IBSS_RSN)\r
+\r
+       C2S(NL80211_ATTR_WIPHY_ANTENNA_TX)\r
+       C2S(NL80211_ATTR_WIPHY_ANTENNA_RX)\r
+\r
+       C2S(NL80211_ATTR_MCAST_RATE)\r
+\r
+       C2S(NL80211_ATTR_OFFCHANNEL_TX_OK)\r
+\r
+       C2S(NL80211_ATTR_BSS_HT_OPMODE)\r
+\r
+       C2S(NL80211_ATTR_KEY_DEFAULT_TYPES)\r
+\r
+       C2S(NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION)\r
+\r
+       C2S(NL80211_ATTR_MESH_SETUP)\r
+\r
+       C2S(NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX)\r
+       C2S(NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX)\r
+\r
+       C2S(NL80211_ATTR_SUPPORT_MESH_AUTH)\r
+       C2S(NL80211_ATTR_STA_PLINK_STATE)\r
+\r
+       C2S(NL80211_ATTR_WOWLAN_TRIGGERS)\r
+       C2S(NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED)\r
+\r
+       C2S(NL80211_ATTR_SCHED_SCAN_INTERVAL)\r
+\r
+       C2S(NL80211_ATTR_INTERFACE_COMBINATIONS)\r
+       C2S(NL80211_ATTR_SOFTWARE_IFTYPES)\r
+\r
+       C2S(NL80211_ATTR_REKEY_DATA)\r
+\r
+       C2S(NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS)\r
+       C2S(NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN)\r
+\r
+       C2S(NL80211_ATTR_SCAN_SUPP_RATES)\r
+\r
+       C2S(NL80211_ATTR_HIDDEN_SSID)\r
+\r
+       C2S(NL80211_ATTR_IE_PROBE_RESP)\r
+       C2S(NL80211_ATTR_IE_ASSOC_RESP)\r
+\r
+       C2S(NL80211_ATTR_STA_WME)\r
+       C2S(NL80211_ATTR_SUPPORT_AP_UAPSD)\r
+\r
+       C2S(NL80211_ATTR_ROAM_SUPPORT)\r
+\r
+       C2S(NL80211_ATTR_SCHED_SCAN_MATCH)\r
+       C2S(NL80211_ATTR_MAX_MATCH_SETS)\r
+\r
+       C2S(NL80211_ATTR_PMKSA_CANDIDATE)\r
+\r
+       C2S(NL80211_ATTR_TX_NO_CCK_RATE)\r
+\r
+       C2S(NL80211_ATTR_TDLS_ACTION)\r
+       C2S(NL80211_ATTR_TDLS_DIALOG_TOKEN)\r
+       C2S(NL80211_ATTR_TDLS_OPERATION)\r
+       C2S(NL80211_ATTR_TDLS_SUPPORT)\r
+       C2S(NL80211_ATTR_TDLS_EXTERNAL_SETUP)\r
+\r
+       C2S(NL80211_ATTR_DEVICE_AP_SME)\r
+\r
+       C2S(NL80211_ATTR_DONT_WAIT_FOR_ACK)\r
+\r
+       C2S(NL80211_ATTR_FEATURE_FLAGS)\r
+\r
+       C2S(NL80211_ATTR_PROBE_RESP_OFFLOAD)\r
+\r
+       C2S(NL80211_ATTR_PROBE_RESP)\r
+\r
+       C2S(NL80211_ATTR_DFS_REGION)\r
+\r
+       C2S(NL80211_ATTR_DISABLE_HT)\r
+       C2S(NL80211_ATTR_HT_CAPABILITY_MASK)\r
+\r
+       C2S(NL80211_ATTR_NOACK_MAP)\r
+\r
+       C2S(NL80211_ATTR_INACTIVITY_TIMEOUT)\r
+\r
+       C2S(NL80211_ATTR_RX_SIGNAL_DBM)\r
+\r
+       C2S(NL80211_ATTR_BG_SCAN_PERIOD)\r
+\r
+       C2S(NL80211_ATTR_WDEV)\r
+\r
+       C2S(NL80211_ATTR_USER_REG_HINT_TYPE)\r
+\r
+       C2S(NL80211_ATTR_CONN_FAILED_REASON)\r
+\r
+       C2S(NL80211_ATTR_SAE_DATA)\r
+\r
+       C2S(NL80211_ATTR_VHT_CAPABILITY)\r
+\r
+       C2S(NL80211_ATTR_SCAN_FLAGS)\r
+\r
+       C2S(NL80211_ATTR_CHANNEL_WIDTH)\r
+       C2S(NL80211_ATTR_CENTER_FREQ1)\r
+       C2S(NL80211_ATTR_CENTER_FREQ2)\r
+\r
+       C2S(NL80211_ATTR_P2P_CTWINDOW)\r
+       C2S(NL80211_ATTR_P2P_OPPPS)\r
+\r
+       C2S(NL80211_ATTR_LOCAL_MESH_POWER_MODE)\r
+\r
+       C2S(NL80211_ATTR_ACL_POLICY)\r
+\r
+       C2S(NL80211_ATTR_MAC_ADDRS)\r
+\r
+       C2S(NL80211_ATTR_MAC_ACL_MAX)\r
+\r
+       C2S(NL80211_ATTR_RADAR_EVENT)\r
+\r
+       C2S(NL80211_ATTR_EXT_CAPA)\r
+       C2S(NL80211_ATTR_EXT_CAPA_MASK)\r
+\r
+       C2S(NL80211_ATTR_STA_CAPABILITY)\r
+       C2S(NL80211_ATTR_STA_EXT_CAPABILITY)\r
+\r
+       C2S(NL80211_ATTR_PROTOCOL_FEATURES)\r
+       C2S(NL80211_ATTR_SPLIT_WIPHY_DUMP)\r
+\r
+       C2S(NL80211_ATTR_DISABLE_VHT)\r
+       C2S(NL80211_ATTR_VHT_CAPABILITY_MASK)\r
+\r
+       C2S(NL80211_ATTR_MDID)\r
+       C2S(NL80211_ATTR_IE_RIC)\r
+\r
+       C2S(NL80211_ATTR_CRIT_PROT_ID)\r
+       C2S(NL80211_ATTR_MAX_CRIT_PROT_DURATION)\r
+\r
+       C2S(NL80211_ATTR_PEER_AID)\r
+\r
+\r
+       //S(NL80211_ATTR_VENDOR_ID)\r
+       C2S(NL80211_ATTR_VENDOR_SUBCMD)\r
+       C2S(NL80211_ATTR_VENDOR_DATA)\r
+       C2S(NL80211_ATTR_VENDOR_EVENTS)\r
+\r
+    default:\r
+        return "NL80211_ATTR_UNKNOWN";\r
+    }\r
+}\r
+\r
+void WifiEvent::log() {\r
+    parse();\r
+\r
+    byte *data = (byte *)genlmsg_attrdata(mHeader, 0);\r
+    int len = genlmsg_attrlen(mHeader, 0);\r
+    ALOGD("cmd = %s, len = %d", get_cmdString(), len);\r
+    ALOGD("vendor_id = %04x, vendor_subcmd = %d", get_vendor_id(), get_vendor_subcmd());\r
+\r
+    for (int i = 0; i < len; i += 16) {\r
+        char line[81];\r
+        int linelen = min(16, len - i);\r
+        int offset = 0;\r
+        appendFmt(line, offset, "%02x", data[i]);\r
+        for (int j = 1; j < linelen; j++) {\r
+            appendFmt(line, offset, " %02x", data[i+j]);\r
+        }\r
+\r
+        for (int j = linelen; j < 16; j++) {\r
+            appendFmt(line, offset, "   ");\r
+        }\r
+\r
+        line[23] = '-';\r
+\r
+        appendFmt(line, offset, "  ");\r
+\r
+        for (int j = 0; j < linelen; j++) {\r
+            if (isprint(data[i+j])) {\r
+                appendFmt(line, offset, "%c", data[i+j]);\r
+            } else {\r
+                appendFmt(line, offset, "-");\r
+            }\r
+        }\r
+\r
+        ALOGD("%s", line);\r
+    }\r
+\r
+    for (unsigned i = 0; i < NL80211_ATTR_MAX_INTERNAL; i++) {\r
+        if (mAttributes[i] != NULL) {\r
+            ALOGD("found attribute %s", attributeToString(i));\r
+        }\r
+    }\r
+\r
+    ALOGD("-- End of message --");\r
+}\r
+\r
+const char *WifiEvent::get_cmdString() {\r
+    return cmdToString(get_cmd());\r
+}\r
+\r
+\r
+int WifiEvent::parse() {\r
+    if (mHeader != NULL) {\r
+        return WIFI_SUCCESS;\r
+    }\r
+    mHeader = (genlmsghdr *)nlmsg_data(nlmsg_hdr(mMsg));\r
+    int result = nla_parse(mAttributes, NL80211_ATTR_MAX_INTERNAL, genlmsg_attrdata(mHeader, 0),\r
+          genlmsg_attrlen(mHeader, 0), NULL);\r
+\r
+    ALOGD("event len = %d", nlmsg_hdr(mMsg)->nlmsg_len);\r
+    return result;\r
+}\r
+\r
+int WifiRequest::create(int family, uint8_t cmd, int flags, int hdrlen) {\r
+    mMsg = nlmsg_alloc();\r
+    if (mMsg != NULL) {\r
+        genlmsg_put(mMsg, /* pid = */ 0, /* seq = */ 0, family,\r
+                hdrlen, flags, cmd, /* version = */ 0);\r
+        return WIFI_SUCCESS;\r
+    } else {\r
+        return WIFI_ERROR_OUT_OF_MEMORY;\r
+    }\r
+}\r
+\r
+int WifiRequest::create(uint32_t id, int subcmd) {\r
+    int res = create(NL80211_CMD_VENDOR);\r
+    if (res < 0) {\r
+        return res;\r
+    }\r
+\r
+    res = put_u32(NL80211_ATTR_VENDOR_ID, id);\r
+    if (res < 0) {\r
+        return res;\r
+    }\r
+\r
+    res = put_u32(NL80211_ATTR_VENDOR_SUBCMD, subcmd);\r
+    if (res < 0) {\r
+        return res;\r
+    }\r
+\r
+    if (mIface != -1) {\r
+        res = set_iface_id(mIface);\r
+    }\r
+\r
+    return res;\r
+}\r
+\r
+\r
+static int no_seq_check(struct nl_msg *msg, void *arg)\r
+{\r
+       return NL_OK;\r
+}\r
+\r
+int WifiCommand::requestResponse() {\r
+    int err = create();                 /* create the message */\r
+    if (err < 0) {\r
+        return err;\r
+    }\r
+\r
+    return requestResponse(mMsg);\r
+}\r
+\r
+int WifiCommand::requestResponse(WifiRequest& request) {\r
+    int err = 0;\r
+\r
+    struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);\r
+    if (!cb)\r
+        goto out;\r
+\r
+    err = nl_send_auto_complete(mInfo->cmd_sock, request.getMessage());    /* send message */\r
+    if (err < 0)\r
+        goto out;\r
+\r
+    err = 1;\r
+\r
+    nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);\r
+    nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);\r
+    nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);\r
+    nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);\r
+    nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, response_handler, this);\r
+\r
+    while (err > 0) {                   /* wait for reply */\r
+        int res = nl_recvmsgs(mInfo->cmd_sock, cb);\r
+        if (res) {\r
+            ALOGE("nl80211: %s->nl_recvmsgs failed: %d", __func__, res);\r
+        }\r
+    }\r
+out:\r
+    nl_cb_put(cb);\r
+    return err;\r
+}\r
+\r
+int WifiCommand::requestEvent(int cmd) {\r
+\r
+    ALOGD("requesting event %d", cmd);\r
+\r
+    int res = wifi_register_handler(wifiHandle(), cmd, event_handler, this);\r
+    if (res < 0) {\r
+        return res;\r
+    }\r
+\r
+    res = create();                                                 /* create the message */\r
+    if (res < 0)\r
+        goto out;\r
+\r
+    ALOGD("waiting for response %d", cmd);\r
+\r
+    res = nl_send_auto_complete(mInfo->cmd_sock, mMsg.getMessage());    /* send message */\r
+    if (res < 0)\r
+        goto out;\r
+\r
+    ALOGD("waiting for event %d", cmd);\r
+    res = mCondition.wait();\r
+    if (res < 0)\r
+        goto out;\r
+\r
+out:\r
+    wifi_unregister_handler(wifiHandle(), cmd);\r
+    return res;\r
+}\r
+\r
+int WifiCommand::requestVendorEvent(uint32_t id, int subcmd) {\r
+\r
+    int res = wifi_register_vendor_handler(wifiHandle(), id, subcmd, event_handler, this);\r
+    if (res < 0) {\r
+        return res;\r
+    }\r
+\r
+    res = create();                                                 /* create the message */\r
+    if (res < 0)\r
+        goto out;\r
+\r
+    res = nl_send_auto_complete(mInfo->cmd_sock, mMsg.getMessage());    /* send message */\r
+    if (res < 0)\r
+        goto out;\r
+\r
+    res = mCondition.wait();\r
+    if (res < 0)\r
+        goto out;\r
+\r
+out:\r
+    wifi_unregister_vendor_handler(wifiHandle(), id, subcmd);\r
+    return res;\r
+}\r
+\r
+/* Event handlers */\r
+int WifiCommand::response_handler(struct nl_msg *msg, void *arg) {\r
+    ALOGD("response_handler called");\r
+    WifiCommand *cmd = (WifiCommand *)arg;\r
+    WifiEvent reply(msg);\r
+    int res = reply.parse();\r
+    if (res < 0) {\r
+        ALOGE("Failed to parse reply message = %d", res);\r
+        return NL_SKIP;\r
+    } else {\r
+        // reply.log();\r
+        return cmd->handleResponse(reply);\r
+    }\r
+}\r
+\r
+int WifiCommand::event_handler(struct nl_msg *msg, void *arg) {\r
+    WifiCommand *cmd = (WifiCommand *)arg;\r
+    WifiEvent event(msg);\r
+    int res = event.parse();\r
+    if (res < 0) {\r
+        ALOGE("Failed to parse event = %d", res);\r
+        res = NL_SKIP;\r
+    } else {\r
+        res = cmd->handleEvent(event);\r
+    }\r
+\r
+    cmd->mCondition.signal();\r
+    return res;\r
+}\r
+\r
+/* Other event handlers */\r
+int WifiCommand::valid_handler(struct nl_msg *msg, void *arg) {\r
+    ALOGD("valid_handler called");\r
+    int *err = (int *)arg;\r
+    *err = 0;\r
+    return NL_SKIP;\r
+}\r
+\r
+int WifiCommand::ack_handler(struct nl_msg *msg, void *arg) {\r
+    ALOGD("ack_handler called");\r
+    int *err = (int *)arg;\r
+    *err = 0;\r
+    return NL_STOP;\r
+}\r
+\r
+int WifiCommand::finish_handler(struct nl_msg *msg, void *arg) {\r
+    ALOGD("finish_handler called");\r
+    int *ret = (int *)arg;\r
+    *ret = 0;\r
+    return NL_SKIP;\r
+}\r
+\r
+int WifiCommand::error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg) {\r
+    int *ret = (int *)arg;\r
+    *ret = err->error;\r
+\r
+    ALOGD("error_handler received : %d", err->error);\r
+    return NL_SKIP;\r
+}\r
diff --git a/cpp_bindings.h b/cpp_bindings.h
new file mode 100755 (executable)
index 0000000..65f0f4e
--- /dev/null
@@ -0,0 +1,346 @@
+\r
+#include "wifi_hal.h"\r
+#include "common.h"\r
+#include "sync.h"\r
+\r
+class WifiEvent\r
+{\r
+    /* TODO: remove this when nl headers are updated */\r
+    static const unsigned NL80211_ATTR_MAX_INTERNAL = 256;\r
+private:\r
+    struct nl_msg *mMsg;\r
+    struct genlmsghdr *mHeader;\r
+    struct nlattr *mAttributes[NL80211_ATTR_MAX_INTERNAL + 1];\r
+\r
+public:\r
+    WifiEvent(nl_msg *msg) {\r
+        mMsg = msg;\r
+        mHeader = NULL;\r
+        memset(mAttributes, 0, sizeof(mAttributes));\r
+    }\r
+    ~WifiEvent() {\r
+        /* don't destroy mMsg; it doesn't belong to us */\r
+    }\r
+\r
+    void log();\r
+\r
+    int parse();\r
+\r
+    genlmsghdr *header() {\r
+        return mHeader;\r
+    }\r
+\r
+    int get_cmd() {\r
+        return mHeader->cmd;\r
+    }\r
+\r
+    int get_vendor_id() {\r
+        return get_u32(NL80211_ATTR_VENDOR_ID);\r
+    }\r
+\r
+    int get_vendor_subcmd() {\r
+        return get_u32(NL80211_ATTR_VENDOR_SUBCMD);\r
+    }\r
+\r
+    void *get_vendor_data() {\r
+        return get_data(NL80211_ATTR_VENDOR_DATA);\r
+    }\r
+\r
+    int get_vendor_data_len() {\r
+        return get_len(NL80211_ATTR_VENDOR_DATA);\r
+    }\r
+\r
+    const char *get_cmdString();\r
+\r
+    nlattr ** attributes() {\r
+        return mAttributes;\r
+    }\r
+\r
+    nlattr *get_attribute(int attribute) {\r
+        return mAttributes[attribute];\r
+    }\r
+\r
+    uint8_t get_u8(int attribute) {\r
+        return mAttributes[attribute] ? nla_get_u8(mAttributes[attribute]) : 0;\r
+    }\r
+\r
+    uint16_t get_u16(int attribute) {\r
+        return mAttributes[attribute] ? nla_get_u16(mAttributes[attribute]) : 0;\r
+    }\r
+\r
+    uint32_t get_u32(int attribute) {\r
+        return mAttributes[attribute] ? nla_get_u32(mAttributes[attribute]) : 0;\r
+    }\r
+\r
+    uint64_t get_u64(int attribute) {\r
+        return mAttributes[attribute] ? nla_get_u64(mAttributes[attribute]) : 0;\r
+    }\r
+\r
+    int get_len(int attribute) {\r
+        return mAttributes[attribute] ? nla_len(mAttributes[attribute]) : 0;\r
+    }\r
+\r
+    void *get_data(int attribute) {\r
+        return mAttributes[attribute] ? nla_data(mAttributes[attribute]) : NULL;\r
+    }\r
+\r
+private:\r
+    WifiEvent(const WifiEvent&);        // hide copy constructor to prevent copies\r
+};\r
+\r
+class nl_iterator {\r
+    struct nlattr *pos;\r
+    int rem;\r
+public:\r
+    nl_iterator(struct nlattr *attr) {\r
+        pos = (struct nlattr *)nla_data(attr);\r
+        rem = nla_len(attr);\r
+    }\r
+    bool has_next() {\r
+        return nla_ok(pos, rem);\r
+    }\r
+    void next() {\r
+        pos = (struct nlattr *)nla_next(pos, &(rem));\r
+    }\r
+    struct nlattr *get() {\r
+        return pos;\r
+    }\r
+    uint16_t get_type() {\r
+        return pos->nla_type;\r
+    }\r
+    uint8_t get_u8() {\r
+        return nla_get_u8(pos);\r
+    }\r
+    uint16_t get_u16() {\r
+        return nla_get_u16(pos);\r
+    }\r
+    uint32_t get_u32() {\r
+        return nla_get_u32(pos);\r
+    }\r
+    uint64_t get_u64() {\r
+        return nla_get_u64(pos);\r
+    }\r
+    void* get_data() {\r
+        return nla_data(pos);\r
+    }\r
+    int get_len() {\r
+        return nla_len(pos);\r
+    }\r
+private:\r
+    nl_iterator(const nl_iterator&);    // hide copy constructor to prevent copies\r
+};\r
+\r
+class WifiRequest\r
+{\r
+private:\r
+    int mFamily;\r
+    int mIface;\r
+    struct nl_msg *mMsg;\r
+\r
+public:\r
+    WifiRequest(int family) {\r
+        mMsg = NULL;\r
+        mFamily = family;\r
+        mIface = -1;\r
+    }\r
+\r
+    WifiRequest(int family, int iface) {\r
+        mMsg = NULL;\r
+        mFamily = family;\r
+        mIface = iface;\r
+    }\r
+\r
+    ~WifiRequest() {\r
+        destroy();\r
+    }\r
+\r
+    void destroy() {\r
+        if (mMsg) {\r
+            nlmsg_free(mMsg);\r
+            mMsg = NULL;\r
+        }\r
+    }\r
+\r
+    nl_msg *getMessage() {\r
+        return mMsg;\r
+    }\r
+\r
+    /* Command assembly helpers */\r
+    int create(int family, uint8_t cmd, int flags, int hdrlen);\r
+    int create(uint8_t cmd) {\r
+        return create(mFamily, cmd, 0, 0);\r
+    }\r
+\r
+    int create(uint32_t id, int subcmd);\r
+\r
+    int put(int attribute, void *ptr, unsigned len) {\r
+        return nla_put(mMsg, attribute, len, ptr);\r
+    }\r
+    int put_u8(int attribute, uint8_t value) {\r
+        return nla_put(mMsg, attribute, sizeof(value), &value);\r
+    }\r
+    int put_u16(int attribute, uint16_t value) {\r
+        return nla_put(mMsg, attribute, sizeof(value), &value);\r
+    }\r
+    int put_u32(int attribute, uint32_t value) {\r
+        return nla_put(mMsg, attribute, sizeof(value), &value);\r
+    }\r
+    int put_u64(int attribute, uint64_t value) {\r
+        return nla_put(mMsg, attribute, sizeof(value), &value);\r
+    }\r
+    int put_string(int attribute, const char *value) {\r
+        return nla_put(mMsg, attribute, strlen(value) + 1, value);\r
+    }\r
+    int put_addr(int attribute, mac_addr value) {\r
+        return nla_put(mMsg, attribute, sizeof(mac_addr), value);\r
+    }\r
+\r
+    struct nlattr * attr_start(int attribute) {\r
+        return nla_nest_start(mMsg, attribute);\r
+    }\r
+    void attr_end(struct nlattr *attr) {\r
+        nla_nest_end(mMsg, attr);\r
+    }\r
+\r
+    int set_iface_id(int ifindex) {\r
+        return put_u32(NL80211_ATTR_IFINDEX, ifindex);\r
+    }\r
+private:\r
+    WifiRequest(const WifiRequest&);        // hide copy constructor to prevent copies\r
+\r
+};\r
+\r
+class WifiCommand\r
+{\r
+protected:\r
+    hal_info *mInfo;\r
+    WifiRequest mMsg;\r
+    Condition mCondition;\r
+    wifi_request_id mId;\r
+    interface_info *mIfaceInfo;\r
+    int mRefs;\r
+public:\r
+    WifiCommand(wifi_handle handle, wifi_request_id id)\r
+            : mMsg(getHalInfo(handle)->nl80211_family_id), mId(id), mRefs(1)\r
+    {\r
+        mIfaceInfo = NULL;\r
+        mInfo = getHalInfo(handle);\r
+        // ALOGD("WifiCommand %p created, mInfo = %p, mIfaceInfo = %p", this, mInfo, mIfaceInfo);\r
+    }\r
+\r
+    WifiCommand(wifi_interface_handle iface, wifi_request_id id)\r
+            : mMsg(getHalInfo(iface)->nl80211_family_id, getIfaceInfo(iface)->id), mId(id), mRefs(1)\r
+    {\r
+        mIfaceInfo = getIfaceInfo(iface);\r
+        mInfo = getHalInfo(iface);\r
+        // ALOGD("WifiCommand %p created, mInfo = %p, mIfaceInfo = %p", this, mInfo, mIfaceInfo);\r
+    }\r
+\r
+    virtual ~WifiCommand() {\r
+        // ALOGD("WifiCommand %p destroyed", this);\r
+    }\r
+\r
+    wifi_request_id id() {\r
+        return mId;\r
+    }\r
+\r
+    virtual void addRef() {\r
+        int refs = __sync_add_and_fetch(&mRefs, 1);\r
+        // ALOGD("addRef: WifiCommand %p has %d references", this, refs);\r
+    }\r
+\r
+    virtual void releaseRef() {\r
+        int refs = __sync_sub_and_fetch(&mRefs, 1);\r
+        if (refs == 0) {\r
+            delete this;\r
+        } else {\r
+            // ALOGD("releaseRef: WifiCommand %p has %d references", this, refs);\r
+        }\r
+    }\r
+\r
+    virtual int create() {\r
+        /* by default there is no way to cancel */\r
+        ALOGD("WifiCommand %p can't be created", this);\r
+        return WIFI_ERROR_NOT_SUPPORTED;\r
+    }\r
+\r
+    virtual int cancel() {\r
+        /* by default there is no way to cancel */\r
+        return WIFI_ERROR_NOT_SUPPORTED;\r
+    }\r
+\r
+    int requestResponse();\r
+    int requestEvent(int cmd);\r
+    int requestVendorEvent(uint32_t id, int subcmd);\r
+    int requestResponse(WifiRequest& request);\r
+\r
+protected:\r
+    wifi_handle wifiHandle() {\r
+        return getWifiHandle(mInfo);\r
+    }\r
+\r
+    wifi_interface_handle ifaceHandle() {\r
+        return getIfaceHandle(mIfaceInfo);\r
+    }\r
+\r
+    int familyId() {\r
+        return mInfo->nl80211_family_id;\r
+    }\r
+\r
+    int ifaceId() {\r
+        return mIfaceInfo->id;\r
+    }\r
+\r
+    /* Override this method to parse reply and dig out data; save it in the object */\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+        ALOGI("skipping a response");\r
+        return NL_SKIP;\r
+    }\r
+\r
+    /* Override this method to parse event and dig out data; save it in the object */\r
+    virtual int handleEvent(WifiEvent& event) {\r
+        ALOGI("skipping an event");\r
+        return NL_SKIP;\r
+    }\r
+\r
+    int registerHandler(int cmd) {\r
+        return wifi_register_handler(wifiHandle(), cmd, &event_handler, this);\r
+    }\r
+\r
+    void unregisterHandler(int cmd) {\r
+        wifi_unregister_handler(wifiHandle(), cmd);\r
+    }\r
+\r
+    int registerVendorHandler(uint32_t id, int subcmd) {\r
+        return wifi_register_vendor_handler(wifiHandle(), id, subcmd, &event_handler, this);\r
+    }\r
+\r
+    void unregisterVendorHandler(uint32_t id, int subcmd) {\r
+        wifi_unregister_vendor_handler(wifiHandle(), id, subcmd);\r
+    }\r
+\r
+private:\r
+    WifiCommand(const WifiCommand& );           // hide copy constructor to prevent copies\r
+\r
+    /* Event handling */\r
+    static int response_handler(struct nl_msg *msg, void *arg);\r
+\r
+    static int event_handler(struct nl_msg *msg, void *arg);\r
+\r
+    /* Other event handlers */\r
+    static int valid_handler(struct nl_msg *msg, void *arg);\r
+\r
+    static int ack_handler(struct nl_msg *msg, void *arg);\r
+\r
+    static int finish_handler(struct nl_msg *msg, void *arg);\r
+\r
+    static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg);\r
+};\r
+\r
+/* nl message processing macros (required to pass C++ type checks) */\r
+\r
+#define for_each_attr(pos, nla, rem) \\r
+    for (pos = (nlattr *)nla_data(nla), rem = nla_len(nla); \\r
+        nla_ok(pos, rem); \\r
+        pos = (nlattr *)nla_next(pos, &(rem)))\r
+\r
diff --git a/gscan.cpp b/gscan.cpp
new file mode 100755 (executable)
index 0000000..4c6eccc
--- /dev/null
+++ b/gscan.cpp
@@ -0,0 +1,1096 @@
+\r
+#include <stdint.h>\r
+#include <fcntl.h>\r
+#include <sys/socket.h>\r
+#include <netlink/genl/genl.h>\r
+#include <netlink/genl/family.h>\r
+#include <netlink/genl/ctrl.h>\r
+#include <linux/rtnetlink.h>\r
+#include <netpacket/packet.h>\r
+#include <linux/filter.h>\r
+#include <linux/errqueue.h>\r
+\r
+#include <linux/pkt_sched.h>\r
+#include <netlink/object-api.h>\r
+#include <netlink/netlink.h>\r
+#include <netlink/socket.h>\r
+#include <netlink/handlers.h>\r
+\r
+#include "sync.h"\r
+\r
+#define LOG_TAG  "WifiHAL"\r
+\r
+#include <utils/Log.h>\r
+\r
+#include "wifi_hal.h"\r
+#include "common.h"\r
+#include "cpp_bindings.h"\r
+\r
+typedef enum {\r
+\r
+    GSCAN_ATTRIBUTE_NUM_BUCKETS = 10,\r
+    GSCAN_ATTRIBUTE_BASE_PERIOD,\r
+    GSCAN_ATTRIBUTE_BUCKETS_BAND,\r
+    GSCAN_ATTRIBUTE_BUCKET_ID,\r
+    GSCAN_ATTRIBUTE_BUCKET_PERIOD,\r
+    GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS,\r
+    GSCAN_ATTRIBUTE_BUCKET_CHANNELS,\r
+    GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN,\r
+    GSCAN_ATTRIBUTE_REPORT_THRESHOLD,\r
+    GSCAN_ATTRIBUTE_NUM_SCANS_TO_CACHE,\r
+    GSCAN_ATTRIBUTE_DWELL_TIME,\r
+    GSCAN_ATTRIBUTE_SCAN_TYPE,\r
+    GSCAN_ATTRIBUTE_BAND = GSCAN_ATTRIBUTE_BUCKETS_BAND,\r
+\r
+    GSCAN_ATTRIBUTE_ENABLE_FEATURE = 20,\r
+    GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE,              /* indicates no more results */\r
+    GSCAN_ENABLE_FULL_SCAN_RESULTS,\r
+    GSCAN_ATTRIBUTE_REPORT_EVENTS,\r
+\r
+    /* remaining reserved for additional attributes */\r
+    GSCAN_ATTRIBUTE_NUM_OF_RESULTS = 30,\r
+    GSCAN_ATTRIBUTE_SCAN_RESULTS,                       /* flat array of wifi_scan_result */\r
+    GSCAN_ATTRIBUTE_NUM_CHANNELS,\r
+    GSCAN_ATTRIBUTE_CHANNEL_LIST,\r
+\r
+    /* remaining reserved for additional attributes */\r
+\r
+    GSCAN_ATTRIBUTE_SSID = 40,\r
+    GSCAN_ATTRIBUTE_BSSID,\r
+    GSCAN_ATTRIBUTE_CHANNEL,\r
+    GSCAN_ATTRIBUTE_RSSI,\r
+    GSCAN_ATTRIBUTE_TIMESTAMP,\r
+    GSCAN_ATTRIBUTE_RTT,\r
+    GSCAN_ATTRIBUTE_RTTSD,\r
+\r
+    /* remaining reserved for additional attributes */\r
+\r
+    GSCAN_ATTRIBUTE_HOTLIST_BSSIDS = 50,\r
+    GSCAN_ATTRIBUTE_RSSI_LOW,\r
+    GSCAN_ATTRIBUTE_RSSI_HIGH,\r
+    GSCAN_ATTRIBUTE_HOTLIST_ELEM,\r
+    GSCAN_ATTRIBUTE_HOTLIST_FLUSH,\r
+    GSCAN_ATTRIBUTE_CHANNEL_NUMBER,\r
+\r
+    /* remaining reserved for additional attributes */\r
+    GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE = 60,\r
+    GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE,\r
+    GSCAN_ATTRIBUTE_MIN_BREACHING,\r
+    GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS,\r
+\r
+    GSCAN_ATTRIBUTE_MAX\r
+\r
+} GSCAN_ATTRIBUTE;\r
+\r
+\r
+class GetCapabilitiesCommand : public WifiCommand\r
+{\r
+    wifi_gscan_capabilities *mCapabilities;\r
+public:\r
+    GetCapabilitiesCommand(wifi_interface_handle iface, wifi_gscan_capabilities *capabitlites)\r
+        : WifiCommand(iface, 0), mCapabilities(capabitlites)\r
+    {\r
+        memset(mCapabilities, 0, sizeof(*mCapabilities));\r
+    }\r
+\r
+    virtual int create() {\r
+        ALOGD("Creating message to get scan capablities; iface = %d", mIfaceInfo->id);\r
+\r
+        int ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_CAPABILITIES);\r
+        if (ret < 0) {\r
+           ALOGD("NL message creation failed");\r
+            return ret;\r
+        }\r
+\r
+        return ret;\r
+    }\r
+\r
+protected:\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+\r
+        ALOGD("In GetCapabilities::handleResponse");\r
+\r
+        if (reply.get_cmd() != NL80211_CMD_VENDOR) {\r
+            ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());\r
+            return NL_SKIP;\r
+        }\r
+\r
+        int id = reply.get_vendor_id();\r
+        int subcmd = reply.get_vendor_subcmd();\r
+\r
+        void *data = reply.get_vendor_data();\r
+        int len = reply.get_vendor_data_len();\r
+\r
+        ALOGD("Id = %0x, subcmd = %d, len = %d, expected len = %d", id, subcmd, len,\r
+                    sizeof(*mCapabilities));\r
+\r
+        memcpy(mCapabilities, data, min(len, (int) sizeof(*mCapabilities)));\r
+\r
+        return NL_OK;\r
+    }\r
+};\r
+\r
+\r
+wifi_error wifi_get_gscan_capabilities(wifi_interface_handle handle,\r
+        wifi_gscan_capabilities *capabilities)\r
+{\r
+    GetCapabilitiesCommand command(handle, capabilities);\r
+    return (wifi_error) command.requestResponse();\r
+}\r
+\r
+class GetChannelListCommand : public WifiCommand\r
+{\r
+    wifi_channel *channels;\r
+    int max_channels;\r
+    int *num_channels;\r
+    int band;\r
+public:\r
+    GetChannelListCommand(wifi_interface_handle iface, wifi_channel *channel_buf, int *ch_num,\r
+        int num_max_ch, int band)\r
+        : WifiCommand(iface, 0), channels(channel_buf), max_channels(num_max_ch), num_channels(ch_num),\r
+        band(band)\r
+    {\r
+        memset(channels, 0, sizeof(wifi_channel) * max_channels);\r
+    }\r
+    virtual int create() {\r
+        ALOGD("Creating message to get channel list; iface = %d", mIfaceInfo->id);\r
+\r
+        int ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_VALID_CHANNELS);\r
+        if (ret < 0) {\r
+            return ret;\r
+        }\r
+\r
+        nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);\r
+        ret = mMsg.put_u32(GSCAN_ATTRIBUTE_BAND, band);\r
+        if (ret < 0) {\r
+            return ret;\r
+        }\r
+\r
+        mMsg.attr_end(data);\r
+\r
+        return ret;\r
+    }\r
+\r
+protected:\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+\r
+        ALOGD("In GetChannelList::handleResponse");\r
+\r
+        if (reply.get_cmd() != NL80211_CMD_VENDOR) {\r
+            ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());\r
+            return NL_SKIP;\r
+        }\r
+\r
+        int id = reply.get_vendor_id();\r
+        int subcmd = reply.get_vendor_subcmd();\r
+        int num_channels_to_copy = 0;\r
+\r
+        nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);\r
+        int len = reply.get_vendor_data_len();\r
+\r
+        ALOGD("Id = %0x, subcmd = %d, len = %d", id, subcmd, len);\r
+        if (vendor_data == NULL || len == 0) {\r
+            ALOGE("no vendor data in GetChannelList response; ignoring it");\r
+            return NL_SKIP;\r
+        }\r
+\r
+        for (nl_iterator it(vendor_data); it.has_next(); it.next()) {\r
+            if (it.get_type() == GSCAN_ATTRIBUTE_NUM_CHANNELS) {\r
+                num_channels_to_copy = it.get_u32();\r
+                ALOGD("Got channel list with %d channels", num_channels_to_copy);\r
+                if(num_channels_to_copy > max_channels)\r
+                    num_channels_to_copy = max_channels;\r
+                *num_channels = num_channels_to_copy;\r
+            } else if (it.get_type() == GSCAN_ATTRIBUTE_CHANNEL_LIST && num_channels_to_copy) {\r
+                memcpy(channels, it.get_data(), sizeof(int) * num_channels_to_copy);\r
+            } else {\r
+                ALOGW("Ignoring invalid attribute type = %d, size = %d",\r
+                        it.get_type(), it.get_len());\r
+            }\r
+        }\r
+\r
+        return NL_OK;\r
+    }\r
+};\r
+\r
+wifi_error wifi_get_valid_channels(wifi_interface_handle handle,\r
+        int band, int max_channels, wifi_channel *channels, int *num_channels)\r
+{\r
+    GetChannelListCommand command(handle, channels, num_channels,\r
+                                        max_channels, band);\r
+    return (wifi_error) command.requestResponse();\r
+}\r
+/////////////////////////////////////////////////////////////////////////////\r
+\r
+/* helper functions */\r
+\r
+static int parseScanResults(wifi_scan_result *results, int num, nlattr *attr)\r
+{\r
+    memset(results, 0, sizeof(wifi_scan_result) * num);\r
+\r
+    int i = 0;\r
+    for (nl_iterator it(attr); it.has_next() && i < num; it.next(), i++) {\r
+\r
+        int index = it.get_type();\r
+        ALOGD("retrieved scan result %d", index);\r
+        nlattr *sc_data = (nlattr *) it.get_data();\r
+        wifi_scan_result *result = results + i;\r
+\r
+        for (nl_iterator it2(sc_data); it2.has_next(); it2.next()) {\r
+            int type = it2.get_type();\r
+            if (type == GSCAN_ATTRIBUTE_SSID) {\r
+                strncpy(result->ssid, (char *) it2.get_data(), it2.get_len());\r
+                result->ssid[it2.get_len()] = 0;\r
+            } else if (type == GSCAN_ATTRIBUTE_BSSID) {\r
+                memcpy(result->bssid, (byte *) it2.get_data(), sizeof(mac_addr));\r
+            } else if (type == GSCAN_ATTRIBUTE_TIMESTAMP) {\r
+                result->ts = it2.get_u64();\r
+            } else if (type == GSCAN_ATTRIBUTE_CHANNEL) {\r
+                result->ts = it2.get_u16();\r
+            } else if (type == GSCAN_ATTRIBUTE_RSSI) {\r
+                result->rssi = it2.get_u8();\r
+            } else if (type == GSCAN_ATTRIBUTE_RTT) {\r
+                result->rtt = it2.get_u64();\r
+            } else if (type == GSCAN_ATTRIBUTE_RTTSD) {\r
+                result->rtt_sd = it2.get_u64();\r
+            }\r
+        }\r
+\r
+    }\r
+\r
+    if (i >= num) {\r
+        ALOGE("Got too many results; skipping some");\r
+    }\r
+\r
+    return i;\r
+}\r
+\r
+int createFeatureRequest(WifiRequest& request, int subcmd) {\r
+\r
+    int result = request.create(GOOGLE_OUI, subcmd);\r
+    if (result < 0) {\r
+        return result;\r
+    }\r
+\r
+    return WIFI_SUCCESS;\r
+}\r
+\r
+class ScanCommand : public WifiCommand\r
+{\r
+    wifi_scan_cmd_params *mParams;\r
+    wifi_scan_result_handler mHandler;\r
+    static unsigned mGlobalFullScanBuckets;\r
+    bool mLocalFullScanBuckets;\r
+public:\r
+    ScanCommand(wifi_interface_handle iface, int id, wifi_scan_cmd_params *params,\r
+                wifi_scan_result_handler handler)\r
+        : WifiCommand(iface, id), mParams(params), mHandler(handler),\r
+          mLocalFullScanBuckets(0)\r
+    { }\r
+\r
+    int createSetupRequest(WifiRequest& request) {\r
+        int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);\r
+        result = request.put_u32(GSCAN_ATTRIBUTE_BASE_PERIOD, mParams->base_period);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+       result = request.put_u32(GSCAN_ATTRIBUTE_NUM_AP_PER_SCAN, mParams->max_ap_per_scan);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        result = request.put_u32(GSCAN_ATTRIBUTE_REPORT_THRESHOLD, mParams->report_threshold);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        result = request.put_u32(GSCAN_ATTRIBUTE_NUM_BUCKETS, mParams->num_buckets);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        for (int i = 0; i < mParams->num_buckets; i++) {\r
+            nlattr * bucket = request.attr_start(i);    // next bucket\r
+            result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_ID, mParams->buckets[i].bucket);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_PERIOD, mParams->buckets[i].period);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            result = request.put_u32(GSCAN_ATTRIBUTE_BUCKETS_BAND,\r
+                    mParams->buckets[i].band);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+\r
+            result = request.put_u32(GSCAN_ATTRIBUTE_REPORT_EVENTS,\r
+                    mParams->buckets[i].report_events);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+\r
+            result = request.put_u32(GSCAN_ATTRIBUTE_BUCKET_NUM_CHANNELS,\r
+                    mParams->buckets[i].num_channels);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+\r
+            if (mParams->buckets[i].num_channels) {\r
+                nlattr *channels = request.attr_start(GSCAN_ATTRIBUTE_BUCKET_CHANNELS);\r
+                for (int j = 0; j < mParams->buckets[i].num_channels; j++) {\r
+                    result = request.put_u32(j, mParams->buckets[i].channels[j].channel);\r
+                    if (result < 0) {\r
+                        return result;\r
+                    }\r
+                }\r
+                request.attr_end(channels);\r
+            }\r
+\r
+            request.attr_end(bucket);\r
+        }\r
+\r
+        request.attr_end(data);\r
+        return WIFI_SUCCESS;\r
+    }\r
+\r
+    int createStartRequest(WifiRequest& request) {\r
+        return createFeatureRequest(request, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN);\r
+    }\r
+\r
+    int createStopRequest(WifiRequest& request) {\r
+        return createFeatureRequest(request, SLSI_NL80211_VENDOR_SUBCMD_DEL_GSCAN);\r
+    }\r
+\r
+    int enableFullScanResultsIfRequired() {\r
+        /* temporary workaround till we have full support for per bucket scans */\r
+\r
+        ALOGI("enabling full scan results if needed");\r
+        int nBuckets = 0;\r
+        for (int i = 0; i < mParams->num_buckets; i++) {\r
+            if (mParams->buckets[i].report_events == 2) {\r
+                nBuckets++;\r
+            }\r
+        }\r
+\r
+        if (mGlobalFullScanBuckets == 0 && nBuckets != 0) {\r
+            \r
+           ALOGI("full scan results were requested ");\r
+           ALOGI("mGlobalFullScanBuckets = %d, nBuckets = %d", mGlobalFullScanBuckets, nBuckets);\r
+                   mLocalFullScanBuckets = nBuckets;\r
+        mGlobalFullScanBuckets += nBuckets;\r
+               registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);\r
+\r
+        } else {\r
+            ALOGI("mGlobalFullScanBuckets = %d, nBuckets = %d", mGlobalFullScanBuckets, nBuckets);\r
+\r
+        }\r
+\r
+        return WIFI_SUCCESS;       \r
+    }\r
+    int disableFullScanResultsIfRequired() {\r
+        /* temporary workaround till we have full support for per bucket scans */\r
+\r
+        if (mLocalFullScanBuckets == 0) {\r
+            return WIFI_SUCCESS;\r
+        }\r
+\r
+        mGlobalFullScanBuckets -= mLocalFullScanBuckets;\r
+        if (mGlobalFullScanBuckets == 0) {\r
+            \r
+            unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_FULL_SCAN_RESULTS);\r
+        }\r
+\r
+        return WIFI_SUCCESS;\r
+    }\r
+    int start() {\r
+        ALOGD(" sending scan req to driver");\r
+        WifiRequest request(familyId(), ifaceId());\r
+        int result = createSetupRequest(request);\r
+        if (result != WIFI_SUCCESS) {\r
+            ALOGE("failed to create setup request; result = %d", result);\r
+            return result;\r
+        }\r
+        ALOGD("Starting scan");\r
+\r
+        registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE);\r
+        registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_COMPLETE_SCAN);\r
+        result = enableFullScanResultsIfRequired();\r
+               if ( result == WIFI_SUCCESS)\r
+               {\r
+                  \r
+               }\r
+\r
+        result = requestResponse(request);\r
+        if (result != WIFI_SUCCESS) {\r
+            ALOGE("failed to start scan; result = %d", result);\r
+            unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_COMPLETE_SCAN);\r
+            unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE);\r
+            return result;\r
+        }\r
+\r
+      \r
+        return result;\r
+    }\r
+\r
+    virtual int cancel() {\r
+        ALOGD("Stopping scan");\r
+\r
+        WifiRequest request(familyId(), ifaceId());\r
+        int result = createStopRequest(request);\r
+        if (result != WIFI_SUCCESS) {\r
+            ALOGE("failed to create stop request; result = %d", result);\r
+        } else {\r
+            result = requestResponse(request);\r
+            if (result != WIFI_SUCCESS) {\r
+                ALOGE("failed to stop scan; result = %d", result);\r
+            }\r
+        }\r
+\r
+        unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_COMPLETE_SCAN);\r
+        unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SCAN_RESULTS_AVAILABLE);\r
+        disableFullScanResultsIfRequired();\r
+\r
+        return WIFI_SUCCESS;\r
+    }\r
+\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+        /* Nothing to do on response! */\r
+        return NL_SKIP;\r
+    }\r
+\r
+    virtual int handleEvent(WifiEvent& event) {\r
+        ALOGD("Got a scan results event");\r
+\r
+        event.log();\r
+\r
+        nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);\r
+        int len = event.get_vendor_data_len();\r
+        int event_id = event.get_vendor_subcmd();\r
+\r
+        if(event_id == GSCAN_EVENT_COMPLETE_SCAN) {\r
+            if (vendor_data == NULL || len != 4) {\r
+                ALOGD("Scan complete type not mentioned!");\r
+                return NL_SKIP;\r
+            }\r
+            wifi_scan_event evt_type;\r
+\r
+            evt_type = (wifi_scan_event) event.get_u32(NL80211_ATTR_VENDOR_DATA);\r
+            ALOGD("Scan complete: Received event type %d", evt_type);\r
+            if(*mHandler.on_scan_event)\r
+                (*mHandler.on_scan_event)(evt_type, evt_type);\r
+        } else if(event_id == GSCAN_EVENT_FULL_SCAN_RESULTS) {\r
+           if (vendor_data == NULL || len < sizeof(wifi_scan_result)) {\r
+               ALOGD("No scan results found");\r
+               return NL_SKIP;\r
+           }\r
+           wifi_scan_result *result = (wifi_scan_result *)event.get_vendor_data();\r
+\r
+           if(*mHandler.on_full_scan_result)\r
+               (*mHandler.on_full_scan_result)(id(), result);\r
+\r
+           ALOGD("%-32s\t", result->ssid);\r
+\r
+           ALOGD("%02x:%02x:%02x:%02x:%02x:%02x ", result->bssid[0], result->bssid[1],\r
+                   result->bssid[2], result->bssid[3], result->bssid[4], result->bssid[5]);\r
+\r
+           ALOGD("%d\t", result->rssi);\r
+           ALOGD("%d\t", result->channel);\r
+           ALOGD("%lld\t", result->ts);\r
+           ALOGD("%lld\t", result->rtt);\r
+           ALOGD("%lld\n", result->rtt_sd);\r
+        } else {\r
+\r
+            if (vendor_data == NULL || len != 4) {\r
+                ALOGD("No scan results found");\r
+                return NL_SKIP;\r
+            }\r
+\r
+            int num = event.get_u32(NL80211_ATTR_VENDOR_DATA);\r
+            ALOGD("Found %d scan results", num);\r
+            if(*mHandler.on_scan_results_available)\r
+                (*mHandler.on_scan_results_available)(id(), num);\r
+        }\r
+        return NL_SKIP;\r
+    }\r
+};\r
+\r
+unsigned ScanCommand::mGlobalFullScanBuckets = 0;\r
+\r
+wifi_error wifi_start_gscan(\r
+        wifi_request_id id,\r
+        wifi_interface_handle iface,\r
+        wifi_scan_cmd_params params,\r
+        wifi_scan_result_handler handler)\r
+{\r
+    wifi_handle handle = getWifiHandle(iface);\r
+\r
+    ALOGD("Starting GScan, halHandle = %p", handle);\r
+\r
+    ScanCommand *cmd = new ScanCommand(iface, id, &params, handler);\r
+    wifi_register_cmd(handle, id, cmd);\r
+    return (wifi_error)cmd->start();\r
+}\r
+\r
+wifi_error wifi_stop_gscan(wifi_request_id id, wifi_interface_handle iface)\r
+{\r
+    ALOGD("Stopping GScan");\r
+    wifi_handle handle = getWifiHandle(iface);\r
+\r
+    if(id == -1) {\r
+        wifi_scan_result_handler handler;\r
+        wifi_scan_cmd_params dummy_params;\r
+        wifi_handle handle = getWifiHandle(iface);\r
+        memset(&handler, 0, sizeof(handler));\r
+\r
+        ScanCommand *cmd = new ScanCommand(iface, id, &dummy_params, handler);\r
+        cmd->cancel();\r
+        cmd->releaseRef();\r
+        return WIFI_SUCCESS;\r
+    }\r
+\r
+\r
+    WifiCommand *cmd = wifi_unregister_cmd(handle, id);\r
+    if (cmd) {\r
+        cmd->cancel();\r
+        cmd->releaseRef();\r
+        return WIFI_SUCCESS;\r
+    }\r
+\r
+    return WIFI_ERROR_INVALID_ARGS;\r
+}\r
+\r
+class GetScanResultsCommand : public WifiCommand {\r
+    wifi_scan_result *mResults;\r
+    int mMax;\r
+    int *mNum;\r
+    int mRetrieved;\r
+    byte mFlush;\r
+    int mCompleted;\r
+public:\r
+    GetScanResultsCommand(wifi_interface_handle iface, byte flush,\r
+            wifi_scan_result *results, int max, int *num)\r
+        : WifiCommand(iface, -1), mResults(results), mMax(max), mNum(num),\r
+                mRetrieved(0), mFlush(flush), mCompleted(0)\r
+    { }\r
+\r
+    int createRequest(WifiRequest& request, int num, byte flush) {\r
+        int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_GET_SCAN_RESULTS);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);\r
+        result = request.put_u32(GSCAN_ATTRIBUTE_NUM_OF_RESULTS, num);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        request.attr_end(data);\r
+        return WIFI_SUCCESS;\r
+    }\r
+\r
+    int execute() {\r
+        WifiRequest request(familyId(), ifaceId());\r
+        ALOGD("retrieving %d scan results", mMax);\r
+\r
+        for (int i = 0; i < 10 && mRetrieved < mMax; i++) {\r
+            int result = createRequest(request, (mMax - mRetrieved), mFlush);\r
+            if (result < 0) {\r
+                ALOGE("failed to create request");\r
+                return result;\r
+            }\r
+\r
+            int prev_retrieved = mRetrieved;\r
+\r
+            result = requestResponse(request);\r
+\r
+            if (result != WIFI_SUCCESS) {\r
+                ALOGE("failed to retrieve scan results; result = %d", result);\r
+                return result;\r
+            }\r
+\r
+            if (mRetrieved == prev_retrieved || mCompleted) {\r
+                /* no more items left to retrieve */\r
+                break;\r
+            }\r
+\r
+            request.destroy();\r
+        }\r
+\r
+        ALOGE("GetScanResults read %d results", mRetrieved);\r
+        *mNum = mRetrieved;\r
+        return WIFI_SUCCESS;\r
+    }\r
+\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+        ALOGD("In GetScanResultsCommand::handleResponse");\r
+\r
+        if (reply.get_cmd() != NL80211_CMD_VENDOR) {\r
+            ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());\r
+            return NL_SKIP;\r
+        }\r
+\r
+        int id = reply.get_vendor_id();\r
+        int subcmd = reply.get_vendor_subcmd();\r
+\r
+        ALOGD("Id = %0x, subcmd = %d", id, subcmd);\r
+\r
+        /*\r
+        if (subcmd != GSCAN_SUBCMD_SCAN_RESULTS) {\r
+            ALOGE("Invalid response to GetScanResultsCommand; ignoring it");\r
+            return NL_SKIP;\r
+        }\r
+        */\r
+\r
+        nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);\r
+        int len = reply.get_vendor_data_len();\r
+\r
+        if (vendor_data == NULL || len == 0) {\r
+            ALOGE("no vendor data in GetScanResults response; ignoring it");\r
+            return NL_SKIP;\r
+        }\r
+\r
+        for (nl_iterator it(vendor_data); it.has_next(); it.next()) {\r
+            if (it.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS_COMPLETE) {\r
+                mCompleted = it.get_u8();\r
+                ALOGD("retrieved mCompleted flag : %d", mCompleted);\r
+            } else if (it.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS || it.get_type() == 0) {\r
+                for (nl_iterator it2(it.get()); it2.has_next(); it2.next()) {\r
+                    int num = 0;\r
+                    if (it2.get_type() == GSCAN_ATTRIBUTE_SCAN_RESULTS) {\r
+                        num = it2.get_len() / sizeof(wifi_scan_result);\r
+                        num = min(*mNum - mRetrieved, num);\r
+                        memcpy(mResults + mRetrieved, it2.get_data(),\r
+                                sizeof(wifi_scan_result) * num);\r
+                        ALOGD("Retrieved %d scan results", num);\r
+                        wifi_scan_result *results = (wifi_scan_result *)it2.get_data();\r
+                        for (int i = 0; i < num; i++) {\r
+                            wifi_scan_result *result = results + i;\r
+                            ALOGD("%02d  %-32s  %02x:%02x:%02x:%02x:%02x:%02x  %04d", i,\r
+                                result->ssid, result->bssid[0], result->bssid[1], result->bssid[2],\r
+                                result->bssid[3], result->bssid[4], result->bssid[5],\r
+                                result->rssi);\r
+                        }\r
+                        mRetrieved += num;\r
+                    } else {\r
+                        ALOGW("Ignoring invalid attribute type = %d, size = %d",\r
+                                it.get_type(), it.get_len());\r
+                    }\r
+                }\r
+            } else {\r
+                ALOGW("Ignoring invalid attribute type = %d, size = %d",\r
+                        it.get_type(), it.get_len());\r
+            }\r
+        }\r
+\r
+        return NL_OK;\r
+    }\r
+};\r
+\r
+wifi_error wifi_get_cached_gscan_results(wifi_interface_handle iface, byte flush,\r
+        int max, wifi_scan_result *results, int *num) {\r
+\r
+    ALOGD("Getting cached scan results, iface handle = %p, num = %d", iface, *num);\r
+\r
+    GetScanResultsCommand *cmd = new GetScanResultsCommand(iface, flush, results, max, num);\r
+    return (wifi_error)cmd->execute();\r
+}\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r
+\r
+class BssidHotlistCommand : public WifiCommand\r
+{\r
+private:\r
+    wifi_bssid_hotlist_params mParams;\r
+    wifi_hotlist_ap_found_handler mHandler;\r
+    static const int MAX_RESULTS = 64;\r
+    wifi_scan_result mResults[MAX_RESULTS];\r
+public:\r
+    BssidHotlistCommand(wifi_interface_handle handle, int id,\r
+            wifi_bssid_hotlist_params params, wifi_hotlist_ap_found_handler handler)\r
+        : WifiCommand(handle, id), mParams(params), mHandler(handler)\r
+    { }\r
+\r
+    int createSetupRequest(WifiRequest& request) {\r
+        int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_BSSID_HOTLIST);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);\r
+\r
+        result = request.put_u32(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE, mParams.lost_ap_sample_size);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        struct nlattr * attr = request.attr_start(GSCAN_ATTRIBUTE_HOTLIST_BSSIDS);\r
+        for (int i = 0; i < mParams.num_ap; i++) {\r
+            nlattr *attr2 = request.attr_start(GSCAN_ATTRIBUTE_HOTLIST_ELEM);\r
+            if (attr2 == NULL) {\r
+                return WIFI_ERROR_OUT_OF_MEMORY;\r
+            }\r
+            result = request.put_addr(GSCAN_ATTRIBUTE_BSSID, mParams.ap[i].bssid);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH, mParams.ap[i].high);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW, mParams.ap[i].low);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            result = request.put_u8(GSCAN_ATTRIBUTE_CHANNEL_NUMBER, mParams.ap[i].channel);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            request.attr_end(attr2);\r
+        }\r
+\r
+        request.attr_end(attr);\r
+        request.attr_end(data);\r
+        return result;\r
+    }\r
+\r
+    int createTeardownRequest(WifiRequest& request) {\r
+        int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_RESET_BSSID_HOTLIST);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    int start() {\r
+        ALOGD("Executing hotlist setup request, num = %d", mParams.num_ap);\r
+        WifiRequest request(familyId(), ifaceId());\r
+        int result = createSetupRequest(request);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        result = requestResponse(request);\r
+        if (result < 0) {\r
+            ALOGD("Failed to execute hotlist setup request, result = %d", result);\r
+            unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);\r
+            unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);\r
+            return result;\r
+        }\r
+\r
+        ALOGD("Successfully set %d APs in the hotlist", mParams.num_ap);\r
+        result = createFeatureRequest(request, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);\r
+        registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);\r
+\r
+        result = requestResponse(request);\r
+        if (result < 0) {\r
+            unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);\r
+            unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);\r
+            return result;\r
+        }\r
+\r
+        ALOGD("successfully restarted the scan");\r
+        return result;\r
+    }\r
+\r
+    virtual int cancel() {\r
+        /* unregister event handler */\r
+        unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_FOUND);\r
+        unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_HOTLIST_RESULTS_LOST);\r
+        /* create set hotlist message with empty hotlist */\r
+        WifiRequest request(familyId(), ifaceId());\r
+        int result = createTeardownRequest(request);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        result = requestResponse(request);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        ALOGD("Successfully reset APs in current hotlist");\r
+        return result;\r
+    }\r
+\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+        /* Nothing to do on response! */\r
+        return NL_SKIP;\r
+    }\r
+\r
+    virtual int handleEvent(WifiEvent& event) {\r
+        ALOGD("Hotlist AP event");\r
+        int event_id = event.get_vendor_subcmd();\r
+        event.log();\r
+\r
+        nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);\r
+        int len = event.get_vendor_data_len();\r
+\r
+        if (vendor_data == NULL || len == 0) {\r
+            ALOGD("No scan results found");\r
+            return NL_SKIP;\r
+        }\r
+\r
+        memset(mResults, 0, sizeof(wifi_scan_result) * MAX_RESULTS);\r
+\r
+        int num = len / sizeof(wifi_scan_result);\r
+        num = min(MAX_RESULTS, num);\r
+        memcpy(mResults, event.get_vendor_data(), num * sizeof(wifi_scan_result));\r
+\r
+        if (event_id == GSCAN_EVENT_HOTLIST_RESULTS_FOUND) {\r
+            ALOGD("FOUND %d hotlist APs", num);\r
+            if (*mHandler.on_hotlist_ap_found)\r
+                (*mHandler.on_hotlist_ap_found)(id(), num, mResults);\r
+        } else if (event_id == GSCAN_EVENT_HOTLIST_RESULTS_LOST) {\r
+            ALOGD("LOST %d hotlist APs", num);\r
+            if (*mHandler.on_hotlist_ap_lost)\r
+                (*mHandler.on_hotlist_ap_lost)(id(), num, mResults);\r
+        }\r
+        return NL_SKIP;\r
+    }\r
+};\r
+\r
+wifi_error wifi_set_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface,\r
+        wifi_bssid_hotlist_params params, wifi_hotlist_ap_found_handler handler)\r
+{\r
+    wifi_handle handle = getWifiHandle(iface);\r
+\r
+    BssidHotlistCommand *cmd = new BssidHotlistCommand(iface, id, params, handler);\r
+    wifi_register_cmd(handle, id, cmd);\r
+    return (wifi_error)cmd->start();\r
+}\r
+\r
+wifi_error wifi_reset_bssid_hotlist(wifi_request_id id, wifi_interface_handle iface)\r
+{\r
+    wifi_handle handle = getWifiHandle(iface);\r
+\r
+    WifiCommand *cmd = wifi_unregister_cmd(handle, id);\r
+    if (cmd) {\r
+        cmd->cancel();\r
+        cmd->releaseRef();\r
+        return WIFI_SUCCESS;\r
+    }\r
+\r
+    return WIFI_ERROR_INVALID_ARGS;\r
+}\r
+\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r
+\r
+class SignificantWifiChangeCommand : public WifiCommand\r
+{\r
+    typedef struct {\r
+        mac_addr bssid;                     // BSSID\r
+        wifi_channel channel;               // channel frequency in MHz\r
+        int num_rssi;                       // number of rssi samples\r
+        wifi_rssi rssi[8];                   // RSSI history in db\r
+    } wifi_significant_change_result_internal;\r
+\r
+private:\r
+    wifi_significant_change_params mParams;\r
+    wifi_significant_change_handler mHandler;\r
+    static const int MAX_RESULTS = 64;\r
+    wifi_significant_change_result_internal mResultsBuffer[MAX_RESULTS];\r
+    wifi_significant_change_result *mResults[MAX_RESULTS];\r
+public:\r
+    SignificantWifiChangeCommand(wifi_interface_handle handle, int id,\r
+            wifi_significant_change_params params, wifi_significant_change_handler handler)\r
+        : WifiCommand(handle, id), mParams(params), mHandler(handler)\r
+    { }\r
+\r
+    int createSetupRequest(WifiRequest& request) {\r
+        int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_SIGNIFICANT_CHANGE);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);\r
+\r
+        result = request.put_u16(GSCAN_ATTRIBUTE_RSSI_SAMPLE_SIZE, mParams.rssi_sample_size);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+        result = request.put_u16(GSCAN_ATTRIBUTE_LOST_AP_SAMPLE_SIZE, mParams.lost_ap_sample_size);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+        result = request.put_u16(GSCAN_ATTRIBUTE_MIN_BREACHING, mParams.min_breaching);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        struct nlattr * attr = request.attr_start(GSCAN_ATTRIBUTE_SIGNIFICANT_CHANGE_BSSIDS);\r
+\r
+        for (int i = 0; i < mParams.num_ap; i++) {\r
+\r
+            nlattr *attr2 = request.attr_start(i);\r
+            if (attr2 == NULL) {\r
+                return WIFI_ERROR_OUT_OF_MEMORY;\r
+            }\r
+            result = request.put_addr(GSCAN_ATTRIBUTE_BSSID, mParams.ap[i].bssid);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_HIGH, mParams.ap[i].high);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            result = request.put_u8(GSCAN_ATTRIBUTE_RSSI_LOW, mParams.ap[i].low);\r
+            if (result < 0) {\r
+                return result;\r
+            }\r
+            request.attr_end(attr2);\r
+        }\r
+\r
+        request.attr_end(attr);\r
+        request.attr_end(data);\r
+\r
+        return result;\r
+    }\r
+\r
+    int createTeardownRequest(WifiRequest& request) {\r
+        int result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_RESET_SIGNIFICANT_CHANGE);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        return result;\r
+    }\r
+\r
+    int start() {\r
+        ALOGD("Set significant wifi change");\r
+        WifiRequest request(familyId(), ifaceId());\r
+\r
+        int result = createSetupRequest(request);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        result = requestResponse(request);\r
+        if (result < 0) {\r
+            ALOGD("failed to set significant wifi change %d", result);\r
+            return result;\r
+        }\r
+\r
+        result = createFeatureRequest(request, SLSI_NL80211_VENDOR_SUBCMD_ADD_GSCAN);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        registerVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);\r
+\r
+        result = requestResponse(request);\r
+        if (result < 0) {\r
+            unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);\r
+            return result;\r
+        }\r
+\r
+        ALOGD("successfully restarted the scan");\r
+        return result;\r
+    }\r
+\r
+    virtual int cancel() {\r
+        /* unregister event handler */\r
+        unregisterVendorHandler(GOOGLE_OUI, GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS);\r
+\r
+        /* create set significant change monitor message with empty hotlist */\r
+        WifiRequest request(familyId(), ifaceId());\r
+\r
+        int result = createTeardownRequest(request);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        result = requestResponse(request);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        ALOGD("successfully reset significant wifi change");\r
+        return result;\r
+    }\r
+\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+        /* Nothing to do on response! */\r
+        return NL_SKIP;\r
+    }\r
+\r
+    virtual int handleEvent(WifiEvent& event) {\r
+        ALOGD("Got a significant wifi change event");\r
+\r
+        nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);\r
+        int len = event.get_vendor_data_len();\r
+\r
+        if (vendor_data == NULL || len == 0) {\r
+            ALOGD("No scan results found");\r
+            return NL_SKIP;\r
+        }\r
+\r
+        typedef struct {\r
+            uint16_t channel;\r
+            mac_addr bssid;\r
+            s16 rssi_history[8];\r
+        } ChangeInfo;\r
+\r
+        int num = min(len / sizeof(ChangeInfo), MAX_RESULTS);\r
+        ChangeInfo *ci = (ChangeInfo *)event.get_vendor_data();\r
+\r
+        for (int i = 0; i < num; i++) {\r
+            memcpy(mResultsBuffer[i].bssid, ci[i].bssid, sizeof(mac_addr));\r
+            mResultsBuffer[i].channel = ci[i].channel;\r
+            mResultsBuffer[i].num_rssi = 8;\r
+            for (int j = 0; j < mResultsBuffer[i].num_rssi; j++)\r
+                mResultsBuffer[i].rssi[j] = (int) ci[i].rssi_history[j];\r
+            mResults[i] = reinterpret_cast<wifi_significant_change_result *>(&(mResultsBuffer[i]));\r
+        }\r
+\r
+        ALOGD("Retrieved %d scan results", num);\r
+\r
+        if (num != 0) {\r
+            (*mHandler.on_significant_change)(id(), num, mResults);\r
+        } else {\r
+            ALOGW("No significant change reported");\r
+        }\r
+\r
+        return NL_SKIP;\r
+    }\r
+};\r
+\r
+wifi_error wifi_set_significant_change_handler(wifi_request_id id, wifi_interface_handle iface,\r
+        wifi_significant_change_params params, wifi_significant_change_handler handler)\r
+{\r
+    wifi_handle handle = getWifiHandle(iface);\r
+\r
+    SignificantWifiChangeCommand *cmd = new SignificantWifiChangeCommand(\r
+            iface, id, params, handler);\r
+    wifi_register_cmd(handle, id, cmd);\r
+    return (wifi_error)cmd->start();\r
+}\r
+\r
+wifi_error wifi_reset_significant_change_handler(wifi_request_id id, wifi_interface_handle iface)\r
+{\r
+    wifi_handle handle = getWifiHandle(iface);\r
+\r
+    WifiCommand *cmd = wifi_unregister_cmd(handle, id);\r
+    if (cmd) {\r
+        cmd->cancel();\r
+        cmd->releaseRef();\r
+        return WIFI_SUCCESS;\r
+    }\r
+\r
+    return WIFI_ERROR_INVALID_ARGS;\r
+}\r
diff --git a/link_layer_stats.cpp b/link_layer_stats.cpp
new file mode 100755 (executable)
index 0000000..87406a8
--- /dev/null
@@ -0,0 +1,31 @@
+#include <stdint.h>\r
+#include <fcntl.h>\r
+#include <sys/socket.h>\r
+#include <netlink/genl/genl.h>\r
+#include <netlink/genl/family.h>\r
+#include <netlink/genl/ctrl.h>\r
+#include <linux/rtnetlink.h>\r
+#include <netpacket/packet.h>\r
+#include <linux/filter.h>\r
+#include <linux/errqueue.h>\r
+\r
+#include <linux/pkt_sched.h>\r
+#include <netlink/object-api.h>\r
+#include <netlink/netlink.h>\r
+#include <netlink/socket.h>\r
+#include <netlink/handlers.h>\r
+\r
+#include "sync.h"\r
+\r
+#define LOG_TAG  "WifiHAL"\r
+\r
+#include <utils/Log.h>\r
+\r
+#include "wifi_hal.h"\r
+#include "common.h"\r
+#include "cpp_bindings.h"\r
+wifi_error wifi_get_link_stats(wifi_request_id id,\r
+        wifi_interface_handle iface, wifi_stats_result_handler handler)\r
+{\r
+    return WIFI_ERROR_NOT_SUPPORTED;\r
+}\r
diff --git a/rtt.cpp b/rtt.cpp
new file mode 100755 (executable)
index 0000000..ef92ae5
--- /dev/null
+++ b/rtt.cpp
@@ -0,0 +1,52 @@
+#include <stdint.h>\r
+#include <fcntl.h>\r
+#include <sys/socket.h>\r
+#include <netlink/genl/genl.h>\r
+#include <netlink/genl/family.h>\r
+#include <netlink/genl/ctrl.h>\r
+#include <linux/rtnetlink.h>\r
+#include <netpacket/packet.h>\r
+#include <linux/filter.h>\r
+#include <linux/errqueue.h>\r
+\r
+#include <linux/pkt_sched.h>\r
+#include <netlink/object-api.h>\r
+#include <netlink/netlink.h>\r
+#include <netlink/socket.h>\r
+#include <netlink-types.h>\r
+\r
+#include "nl80211_copy.h"\r
+\r
+#include "sync.h"\r
+\r
+#define LOG_TAG  "WifiHAL"\r
+\r
+#include <utils/Log.h>\r
+\r
+#include "wifi_hal.h"\r
+#include "common.h"\r
+#include "cpp_bindings.h"\r
+/* API to request RTT measurement */\r
+wifi_error wifi_rtt_range_request(wifi_request_id id, wifi_interface_handle iface,\r
+        unsigned num_rtt_config, wifi_rtt_config rtt_config[], wifi_rtt_event_handler handler)\r
+{\r
+    \r
+    return WIFI_ERROR_NOT_SUPPORTED;\r
+}\r
+\r
+/* API to cancel RTT measurements */\r
+wifi_error wifi_rtt_range_cancel(wifi_request_id id,  wifi_interface_handle iface,\r
+        unsigned num_devices, mac_addr addr[])\r
+{\r
+\r
+\r
+    return WIFI_ERROR_NOT_SUPPORTED;\r
+}\r
+\r
+/* API to get RTT capability */\r
+wifi_error wifi_get_rtt_capabilities(wifi_interface_handle iface,\r
+        wifi_rtt_capabilities *capabilities)\r
+{\r
+\r
+    return WIFI_ERROR_NOT_SUPPORTED;\r
+}\r
diff --git a/sync.h b/sync.h
new file mode 100755 (executable)
index 0000000..1dc0841
--- /dev/null
+++ b/sync.h
@@ -0,0 +1,54 @@
+\r
+#include <pthread.h>\r
+\r
+#ifndef __WIFI_HAL_SYNC_H__\r
+#define __WIFI_HAL_SYNC_H__\r
+\r
+class Mutex\r
+{\r
+private:\r
+    pthread_mutex_t mMutex;\r
+public:\r
+    Mutex() {\r
+        pthread_mutex_init(&mMutex, NULL);\r
+    }\r
+    ~Mutex() {\r
+        pthread_mutex_destroy(&mMutex);\r
+    }\r
+    int tryLock() {\r
+        return pthread_mutex_trylock(&mMutex);\r
+    }\r
+    int lock() {\r
+        return pthread_mutex_lock(&mMutex);\r
+    }\r
+    void unlock() {\r
+        pthread_mutex_unlock(&mMutex);\r
+    }\r
+};\r
+\r
+class Condition\r
+{\r
+private:\r
+    pthread_cond_t mCondition;\r
+    pthread_mutex_t mMutex;\r
+\r
+public:\r
+    Condition() {\r
+        pthread_mutex_init(&mMutex, NULL);\r
+        pthread_cond_init(&mCondition, NULL);\r
+    }\r
+    ~Condition() {\r
+        pthread_cond_destroy(&mCondition);\r
+        pthread_mutex_destroy(&mMutex);\r
+    }\r
+\r
+    int wait() {\r
+        return pthread_cond_wait(&mCondition, &mMutex);\r
+    }\r
+\r
+    void signal() {\r
+        pthread_cond_signal(&mCondition);\r
+    }\r
+};\r
+\r
+#endif
\ No newline at end of file
diff --git a/wifi_hal.cpp b/wifi_hal.cpp
new file mode 100755 (executable)
index 0000000..ad7cf79
--- /dev/null
@@ -0,0 +1,639 @@
+#include <errno.h>\r
+#include <stdint.h>\r
+#include <fcntl.h>\r
+#include <sys/socket.h>\r
+#include <netlink/genl/genl.h>\r
+#include <netlink/genl/family.h>\r
+#include <netlink/genl/ctrl.h>\r
+#include <linux/rtnetlink.h>\r
+#include <netpacket/packet.h>\r
+#include <linux/filter.h>\r
+#include <linux/errqueue.h>\r
+\r
+#include <linux/pkt_sched.h>\r
+#include <netlink/object-api.h>\r
+#include <netlink/netlink.h>\r
+#include <netlink/socket.h>\r
+#include <netlink/attr.h>\r
+#include <netlink/handlers.h>\r
+#include <netlink/msg.h>\r
+\r
+#include <dirent.h>\r
+#include <net/if.h>\r
+\r
+#include "sync.h"\r
+\r
+#define LOG_TAG  "WifiHAL"\r
+\r
+#include <utils/Log.h>\r
+\r
+#include "wifi_hal.h"\r
+#include "common.h"\r
+#include "cpp_bindings.h"\r
+\r
+\r
+#define WIFI_HAL_CMD_SOCK_PORT       644\r
+#define WIFI_HAL_EVENT_SOCK_PORT     645\r
+\r
+#define FEATURE_SET                  0\r
+#define FEATURE_SET_MATRIX           1\r
+#define ATTR_NODFS_VALUE             3\r
+\r
+static void internal_event_handler(wifi_handle handle, int events);\r
+static int internal_no_seq_check(nl_msg *msg, void *arg);\r
+static int internal_valid_message_handler(nl_msg *msg, void *arg);\r
+static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group);\r
+static int wifi_add_membership(wifi_handle handle, const char *group);\r
+static wifi_error wifi_init_interfaces(wifi_handle handle);\r
+\r
+typedef enum wifi_attr {\r
+    ANDR_WIFI_ATTRIBUTE_NUM_FEATURE_SET,\r
+    ANDR_WIFI_ATTRIBUTE_FEATURE_SET,\r
+    ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI\r
+} wifi_attr_t;\r
+\r
+/* Initialize/Cleanup */\r
+\r
+void wifi_socket_set_local_port(struct nl_sock *sock, uint32_t port)\r
+{\r
+    uint32_t pid = getpid() & 0x3FFFFF;\r
+    nl_socket_set_local_port(sock, pid + (port << 22));\r
+}\r
+\r
+static nl_sock * wifi_create_nl_socket(int port)\r
+{\r
+     ALOGI("Creating socket");\r
+    struct nl_sock *sock = nl_socket_alloc();\r
+    if (sock == NULL) {\r
+        ALOGE("Could not create handle");\r
+        return NULL;\r
+    }\r
+\r
+    wifi_socket_set_local_port(sock, port);\r
+\r
+    struct sockaddr *addr = NULL;\r
+    // ALOGI("sizeof(sockaddr) = %d, sizeof(sockaddr_nl) = %d", sizeof(*addr), sizeof(*addr_nl));\r
+\r
+     ALOGI("Connecting socket");\r
+    if (nl_connect(sock, NETLINK_GENERIC)) {\r
+        ALOGE("Could not connect handle");\r
+        nl_socket_free(sock);\r
+        return NULL;\r
+    }\r
+\r
+     ALOGI("Making socket nonblocking");\r
+    /*\r
+    if (nl_socket_set_nonblocking(sock)) {\r
+        ALOGE("Could make socket non-blocking");\r
+        nl_socket_free(sock);\r
+        return NULL;\r
+    }\r
+    */\r
+\r
+    return sock;\r
+}\r
+\r
+wifi_error wifi_initialize(wifi_handle *handle)\r
+{\r
+    srand(getpid());\r
+\r
+    ALOGI("Initializing wifi");\r
+    hal_info *info = (hal_info *)malloc(sizeof(hal_info));\r
+    if (info == NULL) {\r
+        ALOGE("Could not allocate hal_info");\r
+        return WIFI_ERROR_UNKNOWN;\r
+    }\r
+\r
+    memset(info, 0, sizeof(*info));\r
+\r
+    ALOGI("Creating socket");\r
+    struct nl_sock *cmd_sock = wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT);\r
+    if (cmd_sock == NULL) {\r
+        ALOGE("Could not create handle");\r
+        return WIFI_ERROR_UNKNOWN;\r
+    }\r
+\r
+    struct nl_sock *event_sock = wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT);\r
+    if (event_sock == NULL) {\r
+        ALOGE("Could not create handle");\r
+        nl_socket_free(cmd_sock);\r
+        return WIFI_ERROR_UNKNOWN;\r
+    }\r
+\r
+    struct nl_cb *cb = nl_socket_get_cb(event_sock);\r
+    if (cb == NULL) {\r
+        ALOGE("Could not create handle");\r
+        return WIFI_ERROR_UNKNOWN;\r
+    }\r
+\r
+//     ALOGI("cb->refcnt = %d", cb->cb_refcnt);\r
+    nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, internal_no_seq_check, info);\r
+    nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, internal_valid_message_handler, info);\r
+    nl_cb_put(cb);\r
+\r
+    info->cmd_sock = cmd_sock;\r
+    info->event_sock = event_sock;\r
+    info->clean_up = false;\r
+    info->in_event_loop = false;\r
+\r
+    info->event_cb = (cb_info *)malloc(sizeof(cb_info) * DEFAULT_EVENT_CB_SIZE);\r
+    info->alloc_event_cb = DEFAULT_EVENT_CB_SIZE;\r
+    info->num_event_cb = 0;\r
+\r
+    info->cmd = (cmd_info *)malloc(sizeof(cmd_info) * DEFAULT_CMD_SIZE);\r
+    info->alloc_cmd = DEFAULT_CMD_SIZE;\r
+    info->num_cmd = 0;\r
+\r
+    info->nl80211_family_id = genl_ctrl_resolve(cmd_sock, "nl80211");\r
+    if (info->nl80211_family_id < 0) {\r
+        ALOGE("Could not resolve nl80211 familty id");\r
+        nl_socket_free(cmd_sock);\r
+        nl_socket_free(event_sock);\r
+        free(info);\r
+        return WIFI_ERROR_UNKNOWN;\r
+    }\r
+\r
+    pthread_mutex_init(&info->cb_lock, NULL);\r
+\r
+    *handle = (wifi_handle) info;\r
+           ALOGD("wifi_initialize, handle = %p\n", handle);\r
+           ALOGD("wifi_initialize, *handle = %p\n", *handle);\r
+               ALOGD("wifi_initialize, info = %p\n", info);\r
+               ALOGD("wifi_initialize, *info = %pn", *info);\r
+    wifi_add_membership(*handle, "scan");\r
+    wifi_add_membership(*handle, "mlme");\r
+    wifi_add_membership(*handle, "regulatory");\r
+    wifi_add_membership(*handle, "vendor");\r
+\r
+    wifi_init_interfaces(*handle);\r
+     ALOGD("Found %d interfaces", info->num_interfaces);\r
+\r
+\r
+    ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR);\r
+    return WIFI_SUCCESS;\r
+}\r
+\r
+static int wifi_add_membership(wifi_handle handle, const char *group)\r
+{\r
+    hal_info *info = getHalInfo(handle);\r
+\r
+    int id = wifi_get_multicast_id(handle, "nl80211", group);\r
+    if (id < 0) {\r
+        ALOGE("Could not find group %s", group);\r
+        return id;\r
+    }\r
+\r
+    int ret = nl_socket_add_membership(info->event_sock, id);\r
+    if (ret < 0) {\r
+        ALOGE("Could not add membership to group %s", group);\r
+    }\r
+\r
+     ALOGI("Successfully added membership for group %s", group);\r
+    return ret;\r
+}\r
+\r
+static void internal_cleaned_up_handler(wifi_handle handle)\r
+{\r
+    hal_info *info = getHalInfo(handle);\r
+    wifi_cleaned_up_handler cleaned_up_handler = info->cleaned_up_handler;\r
+\r
+    if (info->cmd_sock != 0) {\r
+        nl_socket_free(info->cmd_sock);\r
+        nl_socket_free(info->event_sock);\r
+        info->cmd_sock = NULL;\r
+        info->event_sock = NULL;\r
+    }\r
+\r
+    (*cleaned_up_handler)(handle);\r
+    pthread_mutex_destroy(&info->cb_lock);\r
+    free(info);\r
+\r
+    ALOGI("Internal cleanup completed");\r
+}\r
+\r
+void wifi_cleanup(wifi_handle handle, wifi_cleaned_up_handler handler)\r
+{\r
+    hal_info *info = getHalInfo(handle);\r
+    info->cleaned_up_handler = handler;\r
+    info->clean_up = true;\r
+\r
+    ALOGI("Wifi cleanup completed");\r
+}\r
+\r
+static int internal_pollin_handler(wifi_handle handle)\r
+{\r
+    hal_info *info = getHalInfo(handle);\r
+               ALOGI("even_loop info = %p", info);\r
+    struct nl_cb *cb = nl_socket_get_cb(info->event_sock);\r
+    int res = nl_recvmsgs(info->event_sock, cb);\r
+     ALOGD("nl_recvmsgs returned %d", res);\r
+    nl_cb_put(cb);\r
+    return res;\r
+}\r
+\r
+/* Run event handler */\r
+void wifi_event_loop(wifi_handle handle)\r
+{\r
+    hal_info *info = getHalInfo(handle);\r
+       ALOGI("even_loop info = %p", info);\r
+       ALOGI("even_loop info = %p", handle);\r
+    if (info->in_event_loop) {\r
+        return;\r
+    } else {\r
+        info->in_event_loop = true;\r
+    }\r
+\r
+    pollfd pfd;\r
+    memset(&pfd, 0, sizeof(pfd));\r
+\r
+    pfd.fd = nl_socket_get_fd(info->event_sock);\r
+    pfd.events = POLLIN;\r
+\r
+    /* TODO: Add support for timeouts */\r
+\r
+    do {\r
+        int timeout = -1;                   /* Infinite timeout */\r
+        pfd.revents = 0;\r
+         ALOGI("Polling socket");\r
+        int result = poll(&pfd, 1, -1);\r
+        if (result < 0) {\r
+            ALOGE("Error polling socket");\r
+        } else if (pfd.revents & POLLERR) {\r
+            ALOGE("POLL Error; error no = %d", errno);\r
+            char buf[2048];\r
+            int result2 = read(pfd.fd, buf, sizeof(buf));\r
+            ALOGE("Read after POLL returned %d, error no = %d", result2, errno);\r
+        } else if (pfd.revents & POLLHUP) {\r
+            ALOGE("Remote side hung up");\r
+            break;\r
+        } else if (pfd.revents & POLLIN) {\r
+             ALOGI("Found some events!!!");\r
+            internal_pollin_handler(handle);\r
+        } else {\r
+            ALOGE("Unknown event - %0x", pfd.revents);\r
+        }\r
+    } while (!info->clean_up);\r
+\r
+\r
+    ALOGI("Cleaning up");\r
+    internal_cleaned_up_handler(handle);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////////////////\r
+\r
+static int internal_no_seq_check(struct nl_msg *msg, void *arg)\r
+{\r
+    return NL_OK;\r
+}\r
+\r
+static int internal_valid_message_handler(nl_msg *msg, void *arg)\r
+{\r
+    wifi_handle handle = (wifi_handle)arg;\r
+    hal_info *info = getHalInfo(handle);\r
+   ALOGI("even_loop info = %p", handle);\r
+   ALOGD("internal_valid_message_handler, info = %p", info);\r
+\r
+    WifiEvent event(msg);\r
+    int res = event.parse();\r
+    if (res < 0) {\r
+        ALOGE("Failed to parse event: %d", res);\r
+        return NL_SKIP;\r
+    }\r
+\r
+    int cmd = event.get_cmd();\r
+    uint32_t vendor_id = 0;\r
+    int subcmd = 0;\r
+\r
+    if (cmd == NL80211_CMD_VENDOR) {\r
+        vendor_id = event.get_u32(NL80211_ATTR_VENDOR_ID);\r
+        subcmd = event.get_u32(NL80211_ATTR_VENDOR_SUBCMD);\r
+        ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",\r
+                event.get_cmdString(), vendor_id, subcmd);\r
+    } else {\r
+         ALOGI("event received %s", event.get_cmdString());\r
+    }\r
+\r
+     //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);\r
+     //event.log();\r
+\r
+    bool dispatched = false;\r
+\r
+    pthread_mutex_lock(&info->cb_lock);\r
+    \r
+    ALOGI("Number of events %d", info->num_event_cb);\r
+\r
+    for (int i = 0; i < info->num_event_cb; i++) {\r
+        if (cmd == info->event_cb[i].nl_cmd) {\r
+            if (cmd == NL80211_CMD_VENDOR\r
+                && ((vendor_id != info->event_cb[i].vendor_id)\r
+                || (subcmd != info->event_cb[i].vendor_subcmd)))\r
+            {\r
+                /* event for a different vendor, ignore it */\r
+                continue;\r
+            }\r
+\r
+            cb_info *cbi = &(info->event_cb[i]);\r
+            nl_recvmsg_msg_cb_t cb_func = cbi->cb_func;\r
+            void *cb_arg = cbi->cb_arg;\r
+            WifiCommand *cmd = (WifiCommand *)cbi->cb_arg;\r
+            if (cmd != NULL) {\r
+                cmd->addRef();\r
+            }\r
+\r
+            pthread_mutex_unlock(&info->cb_lock);\r
+\r
+            (*cb_func)(msg, cb_arg);\r
+            if (cmd != NULL) {\r
+                cmd->releaseRef();\r
+            }\r
+\r
+            return NL_OK;\r
+        }\r
+    }\r
+\r
+    pthread_mutex_unlock(&info->cb_lock);\r
+    return NL_OK;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////////////////\r
+\r
+class GetMulticastIdCommand : public WifiCommand\r
+{\r
+private:\r
+    const char *mName;\r
+    const char *mGroup;\r
+    int   mId;\r
+public:\r
+    GetMulticastIdCommand(wifi_handle handle, const char *name, const char *group)\r
+        : WifiCommand(handle, 0)\r
+    {\r
+        mName = name;\r
+        mGroup = group;\r
+        mId = -1;\r
+    }\r
+\r
+    int getId() {\r
+        return mId;\r
+    }\r
+\r
+    virtual int create() {\r
+        int nlctrlFamily = genl_ctrl_resolve(mInfo->cmd_sock, "nlctrl");\r
+        ALOGI("ctrl family = %d", nlctrlFamily);\r
+        int ret = mMsg.create(nlctrlFamily, CTRL_CMD_GETFAMILY, 0, 0);\r
+        if (ret < 0) {\r
+            return ret;\r
+        }\r
+        ret = mMsg.put_string(CTRL_ATTR_FAMILY_NAME, mName);\r
+        return ret;\r
+    }\r
+\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+\r
+         ALOGE("handling reponse in %s", __func__);\r
+\r
+        struct nlattr **tb = reply.attributes();\r
+        struct genlmsghdr *gnlh = reply.header();\r
+        struct nlattr *mcgrp = NULL;\r
+        int i;\r
+\r
+        if (!tb[CTRL_ATTR_MCAST_GROUPS]) {\r
+            ALOGE("No multicast groups found");\r
+            return NL_SKIP;\r
+        } else {\r
+             ALOGE("Multicast groups attr size = %d", nla_len(tb[CTRL_ATTR_MCAST_GROUPS]));\r
+        }\r
+\r
+        for_each_attr(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {\r
+\r
+             ALOGE("Processing group");\r
+            struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];\r
+            nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, (nlattr *)nla_data(mcgrp),\r
+                nla_len(mcgrp), NULL);\r
+            if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || !tb2[CTRL_ATTR_MCAST_GRP_ID]) {\r
+                continue;\r
+            }\r
+\r
+            char *grpName = (char *)nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]);\r
+            int grpNameLen = nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME]);\r
+\r
+             ALOGE("Found group name %s", grpName);\r
+\r
+            if (strncmp(grpName, mGroup, grpNameLen) != 0)\r
+                continue;\r
+\r
+            mId = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);\r
+            break;\r
+        }\r
+\r
+        return NL_SKIP;\r
+    }\r
+\r
+};\r
+\r
+class SetPnoMacAddrOuiCommand : public WifiCommand {\r
+\r
+private:\r
+    byte *mOui;\r
+    feature_set *fset;\r
+    feature_set *feature_matrix;\r
+    int *fm_size;\r
+    int set_size_max;\r
+public:\r
+    SetPnoMacAddrOuiCommand(wifi_interface_handle handle, oui scan_oui)\r
+        : WifiCommand(handle, 0)\r
+    {\r
+        mOui = scan_oui;\r
+    }\r
+\r
+    int createRequest(WifiRequest& request, int subcmd, byte *scan_oui) {\r
+        int result = request.create(GOOGLE_OUI, subcmd);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);\r
+        result = request.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI, scan_oui, DOT11_OUI_LEN);\r
+        if (result < 0) {\r
+            return result;\r
+        }\r
+\r
+        request.attr_end(data);\r
+        return WIFI_SUCCESS;\r
+\r
+    }\r
+\r
+    int start() {\r
+        ALOGD("Sending mac address OUI");\r
+        WifiRequest request(familyId(), ifaceId());\r
+        int result = createRequest(request, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI, mOui);\r
+        if (result != WIFI_SUCCESS) {\r
+            ALOGE("failed to create request; result = %d", result);\r
+            return result;\r
+        }\r
+\r
+        result = requestResponse(request);\r
+        if (result != WIFI_SUCCESS) {\r
+            ALOGE("failed to set scanning mac OUI; result = %d", result);\r
+        }\r
+\r
+        return result;\r
+    }\r
+protected:\r
+    virtual int handleResponse(WifiEvent& reply) {\r
+         ALOGD("Request complete!");\r
+        /* Nothing to do on response! */\r
+        return NL_SKIP;\r
+    }\r
+};\r
+\r
+class SetNodfsCommand : public WifiCommand {\r
+\r
+private:\r
+    u32 mNoDfs;\r
+public:\r
+    SetNodfsCommand(wifi_interface_handle handle, u32 nodfs)\r
+        : WifiCommand(handle, 0) {\r
+        mNoDfs = nodfs;\r
+    }\r
+    virtual int create() {\r
+        int ret;\r
+\r
+        ret = mMsg.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS);\r
+        if (ret < 0) {\r
+            ALOGE("Can't create message to send to driver - %d", ret);\r
+            return ret;\r
+        }\r
+\r
+        nlattr *data = mMsg.attr_start(NL80211_ATTR_VENDOR_DATA);\r
+        ret = mMsg.put_u32(ATTR_NODFS_VALUE, mNoDfs);\r
+        if (ret < 0) {\r
+             return ret;\r
+        }\r
+\r
+        mMsg.attr_end(data);\r
+        return WIFI_SUCCESS;\r
+    }\r
+};\r
+\r
+static int wifi_get_multicast_id(wifi_handle handle, const char *name, const char *group)\r
+{\r
+    GetMulticastIdCommand cmd(handle, name, group);\r
+    int res = cmd.requestResponse();\r
+    if (res < 0)\r
+        return res;\r
+    else\r
+        return cmd.getId();\r
+}\r
+\r
+/////////////////////////////////////////////////////////////////////////\r
+\r
+static bool is_wifi_interface(const char *name)\r
+{\r
+    if (strncmp(name, "wlan", 4) != 0 && strncmp(name, "p2p", 3) != 0) {\r
+        /* not a wifi interface; ignore it */\r
+        return false;\r
+    } else {\r
+        return true;\r
+    }\r
+}\r
+\r
+static int get_interface(const char *name, interface_info *info)\r
+{\r
+    strcpy(info->name, name);\r
+    info->id = if_nametoindex(name);\r
+     ALOGI("found an interface : %s, id = %d", name, info->id);\r
+    return WIFI_SUCCESS;\r
+}\r
+\r
+wifi_error wifi_init_interfaces(wifi_handle handle)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+       ALOGD("wifi_init_interfaces, info = %p", info);\r
+\r
+    struct dirent *de;\r
+\r
+    DIR *d = opendir("/sys/class/net");\r
+    if (d == 0)\r
+        return WIFI_ERROR_UNKNOWN;\r
+\r
+    int n = 0;\r
+    while ((de = readdir(d))) {\r
+        if (de->d_name[0] == '.')\r
+            continue;\r
+        if (is_wifi_interface(de->d_name) ) {\r
+            n++;\r
+        }\r
+    }\r
+\r
+    closedir(d);\r
+\r
+    d = opendir("/sys/class/net");\r
+    if (d == 0)\r
+        return WIFI_ERROR_UNKNOWN;\r
+\r
+    info->interfaces = (interface_info **)malloc(sizeof(interface_info *) * n);\r
+\r
+    int i = 0;\r
+    while ((de = readdir(d))) {\r
+        if (de->d_name[0] == '.')\r
+            continue;\r
+        if (is_wifi_interface(de->d_name)) {\r
+            interface_info *ifinfo = (interface_info *)malloc(sizeof(interface_info));\r
+            if (get_interface(de->d_name, ifinfo) != WIFI_SUCCESS) {\r
+                free(ifinfo);\r
+                continue;\r
+            }\r
+            ifinfo->handle = handle;\r
+            info->interfaces[i] = ifinfo;\r
+            i++;\r
+        }\r
+    }\r
+\r
+    closedir(d);\r
+\r
+    info->num_interfaces = n;\r
+    return WIFI_SUCCESS;\r
+}\r
+\r
+wifi_error wifi_get_ifaces(wifi_handle handle, int *num, wifi_interface_handle **interfaces)\r
+{\r
+    hal_info *info = (hal_info *)handle;\r
+\r
+    *interfaces = (wifi_interface_handle *)info->interfaces;\r
+    *num = info->num_interfaces;\r
+\r
+    return WIFI_SUCCESS;\r
+}\r
+\r
+wifi_error wifi_get_iface_name(wifi_interface_handle handle, char *name, size_t size)\r
+{\r
+    interface_info *info = (interface_info *)handle;\r
+    strcpy(name, info->name);\r
+    return WIFI_SUCCESS;\r
+}\r
+\r
+wifi_error wifi_get_supported_feature_set(wifi_interface_handle handle, feature_set *set)\r
+{\r
+    return WIFI_ERROR_NOT_SUPPORTED;\r
+}\r
+\r
+wifi_error wifi_get_concurrency_matrix(wifi_interface_handle handle, int set_size_max,\r
+       feature_set set[], int *set_size)\r
+{\r
+    return WIFI_ERROR_NOT_SUPPORTED;\r
+}\r
+\r
+wifi_error wifi_set_scanning_mac_oui(wifi_interface_handle handle, oui scan_oui)\r
+{\r
+    SetPnoMacAddrOuiCommand command(handle, scan_oui);\r
+    return (wifi_error)command.start();\r
+\r
+}\r
+\r
+wifi_error wifi_set_nodfs_flag(wifi_interface_handle handle, u32 nodfs)\r
+{\r
+    SetNodfsCommand command(handle, nodfs);\r
+    return (wifi_error) command.requestResponse();\r
+}\r
+\r
+/////////////////////////////////////////////////////////////////////////////\r