Bug 482613 - Support for WiFi Scanning on Windows Mobile r=dougt, sr=jst
author Nino DAversa <ninodaversa@gmail.com>
Tue, 21 Apr 2009 07:30:25 -0700
changeset 27568 7b213ee861164549b4d099423afd3d9d436d38c7
parent 27567 fcaf50dc12d282046bf0138f107440c7cc7c695a
child 27569 54a5878ac36828732bdb5af4497afccd6ebef376
push id6621
push userdougt@mozilla.com
push dateTue, 21 Apr 2009 14:30:39 +0000
treeherdermozilla-central@7b213ee86116 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdougt, jst
bugs482613
milestone1.9.2a1pre
Bug 482613 - Support for WiFi Scanning on Windows Mobile r=dougt, sr=jst
configure.in
netwerk/wifi/src/nsWifiScannerWin.cpp
--- a/configure.in
+++ b/configure.in
@@ -7881,16 +7881,18 @@ if test "$NECKO_WIFI"; then
     AC_CHECK_HEADER([iwlib.h])
     if test "$ac_cv_header_iwlib_h" = "yes"; then
       NECKO_WIFI=1
     fi
   elif test "$OS_ARCH" = "Darwin"; then
     NECKO_WIFI=1
   elif test "$OS_ARCH" = "WINNT"; then
     NECKO_WIFI=1
+  elif test "$OS_ARCH" = "WINCE"; then
+    NECKO_WIFI=1    
   else
     AC_MSG_WARN([Necko WiFi scanning not supported on your platform.])
   fi
   AC_SUBST(NECKO_WIFI)
   
   if test "$NECKO_WIFI"; then
     AC_DEFINE(NECKO_WIFI)
   fi
--- a/netwerk/wifi/src/nsWifiScannerWin.cpp
+++ b/netwerk/wifi/src/nsWifiScannerWin.cpp
@@ -17,16 +17,17 @@
  * Portions created by the Initial Developer are Copyright (C) 2008
  * the Initial Developer. All Rights Reserved.
  *
  * This is a derivative of work done by Google under a BSD style License.
  * See: http://gears.googlecode.com/svn/trunk/gears/geolocation/
  *
  * Contributor(s):
  *  Doug Turner <dougt@meer.net>  (Original Author)
+ *  Nino D'Aversa <ninodaversa@gmail.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -49,16 +50,23 @@
 #include "nsWifiMonitor.h"
 #include "nsWifiAccessPoint.h"
 
 #include "nsIProxyObjectManager.h"
 #include "nsServiceManagerUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsIMutableArray.h"
 
+#ifdef WINCE
+#include <Iphlpapi.h>  // For GetAdaptersInfo()
+#include <nuiouser.h>  // For NDISUIO stuff
+// GetAdaptersInfo
+typedef DWORD (*GETADAPTERSINFO)(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen);
+#endif
+
 
 // Taken from ndis.h
 #define NDIS_STATUS_INVALID_LENGTH   ((NDIS_STATUS)0xC0010014L)
 #define NDIS_STATUS_BUFFER_TOO_SHORT ((NDIS_STATUS)0xC0010016L)
 
 static const int kStringLength = 512;
 
 // The limits on the size of the buffer used for the OID query.
@@ -70,29 +78,114 @@ static int oid_buffer_size_ = kStringLen
 PRBool ResizeBuffer(int requested_size, BYTE **buffer)
 {
   if (requested_size > kMaximumBufferSize) {
     free(*buffer);
     *buffer = NULL;
     return false;
   }
 
-  BYTE *new_buffer = reinterpret_cast<BYTE*>(realloc(*buffer, requested_size));
+  BYTE *new_buffer = (BYTE*)realloc(*buffer, requested_size);
   if (new_buffer == NULL) {
     free(*buffer);
     *buffer = NULL;
     return false;
   }
 
   *buffer = new_buffer;
   return true;
 }
 
 
+#ifdef WINCE
+int PerformQuery(HANDLE &ndis_handle,
+                 TCHAR *device_name,
+                 BYTE *buffer,
+                 DWORD buffer_size,
+                 BYTE *&data,
+                 DWORD *bytes_out) {
+  // Form the query parameters.
+  NDISUIO_QUERY_OID *query = (NDISUIO_QUERY_OID*)(buffer);
+  query->ptcDeviceName = device_name;
+  query->Oid = OID_802_11_BSSID_LIST;
+  
+  if (!DeviceIoControl(ndis_handle,
+                       IOCTL_NDISUIO_QUERY_OID_VALUE,
+                       query,
+                       sizeof(NDISUIO_QUERY_OID),
+                       query,
+                       buffer_size,
+                       bytes_out,
+                       NULL)) {
+    return GetLastError();
+  }
+  // The start of the NDIS_802_11_BSSID_LIST is at Data[0]
+  data = &query->Data[0];
+  return ERROR_SUCCESS;
+}
 
+ 	
+
+void GetNetworkInterfaces(GETADAPTERSINFO pGetAdaptersInfo, nsStringArray& interfaces){
+  // Get the list of adapters. First determine the buffer size.
+  ULONG buffer_size = 0;
+  // since buffer_size is zero before this, we should get ERROR_BUFFER_OVERFLOW
+  // after this error the value of buffer_size will reflect the size needed
+  if (pGetAdaptersInfo(NULL, &buffer_size) != ERROR_BUFFER_OVERFLOW)      
+    return;
+  
+  // Allocate adapter_info with correct size.
+  IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO*)malloc(buffer_size);
+  if (adapter_info == NULL){
+    free (adapter_info);
+    return;
+  }
+  
+  if (pGetAdaptersInfo(adapter_info, &buffer_size) != ERROR_SUCCESS){
+    free (adapter_info);
+    return;
+  }
+  
+  // Walk through the list of adapters.
+  while (adapter_info) {      
+    // AdapterName e.g. TNETW12511
+    nsString adapterName;
+    // AdapterName is in ASCII
+    adapterName.AppendWithConversion(adapter_info->AdapterName);      
+    interfaces.AppendString(adapterName);
+    adapter_info = adapter_info->Next;
+  }
+  free (adapter_info);
+}
+
+nsresult SetupWince(HANDLE& ndis_handle, GETADAPTERSINFO& pGetAdaptersInfo){
+  // Get the Network Driver Interface Specification (NDIS) handle for wireless
+  // devices. NDIS defines a standard API for Network Interface Cards (NICs).
+  // NDIS User Mode I/O (NDISUIO) is an NDIS protocol driver which offers
+  // support for wireless devices.
+
+  ndis_handle = CreateFile(NDISUIO_DEVICE_NAME, GENERIC_READ,
+			   FILE_SHARE_READ, NULL, OPEN_EXISTING,
+			   FILE_ATTRIBUTE_READONLY, NULL);
+  if (INVALID_HANDLE_VALUE == ndis_handle)
+    return NS_ERROR_FAILURE;
+  
+  HINSTANCE hIpDLL = LoadLibraryW(L"Iphlpapi.dll");
+  if (!hIpDLL)
+    return NS_ERROR_NOT_AVAILABLE;
+  
+  pGetAdaptersInfo = (GETADAPTERSINFO) GetProcAddress(hIpDLL, "GetAdaptersInfo");
+  
+  if (!pGetAdaptersInfo)
+    return NS_ERROR_FAILURE;
+
+  return NS_OK;
+}
+
+#else
 int PerformQuery(HANDLE adapter_handle,
                  BYTE *buffer,
                  DWORD buffer_size,
                  DWORD *bytes_out) {
 
   DWORD oid = OID_802_11_BSSID_LIST;
   
   if (!DeviceIoControl(adapter_handle,
@@ -161,17 +254,17 @@ bool DefineDosDeviceIfNotExists(unsigned
                         target_path.get())) {
     return false;
   }
   // Check that the device is really there. 
   return QueryDosDeviceW(device_name, target, kStringLength) > 0 &&
     target_path.Equals(target);
 } 
 
-void GetNetworkInterfacesFromRegistry(nsStringArray& interfaces)
+void GetNetworkInterfaces(nsStringArray& interfaces)
 {
   HKEY network_cards_key = NULL;
   if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
                     L"Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards",
                     0,
                     KEY_READ,
                     &network_cards_key) != ERROR_SUCCESS)
     {
@@ -208,17 +301,17 @@ void GetNetworkInterfacesFromRegistry(ns
     unsigned short service_name[kStringLength];
     DWORD service_name_size = kStringLength;
     DWORD type = 0;
     
     if (RegQueryValueExW(hardware_key,
                          L"ServiceName",
                          NULL,
                          &type,
-                         reinterpret_cast<LPBYTE>(service_name),
+                         (LPBYTE)service_name,
                          &service_name_size) == ERROR_SUCCESS) {
  
       interfaces.AppendString(nsString(service_name));  // is this allowed?
     } 
     RegCloseKey(hardware_key);
   }
   RegCloseKey(network_cards_key);
 }
@@ -233,57 +326,78 @@ PRBool IsRunningOnVista() {
     os_version.dwOSVersionInfoSize = sizeof(os_version);
     GetVersionEx(&os_version);
     os_major_version = os_version.dwMajorVersion;
     platform_id = os_version.dwPlatformId;
   }
 
   return (6 == os_major_version) && (VER_PLATFORM_WIN32_NT == platform_id);
 }
+#endif
 
 nsresult
 nsWifiMonitor::DoScan()
 {
+#ifndef WINCE
   if (!IsRunningOnVista()) {
+#else
+    HANDLE ndis_handle;
+    GETADAPTERSINFO pGetAdaptersInfo;
+    nsresult rc = SetupWince(ndis_handle, pGetAdaptersInfo);
+    if (rc != NS_OK)
+      return rc;
+#endif
 
     nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
     nsCOMArray<nsWifiAccessPoint> accessPoints;
     
     do {
       accessPoints.Clear();
 
       nsStringArray interfaces;
-      GetNetworkInterfacesFromRegistry(interfaces);
+#ifdef WINCE
+      GetNetworkInterfaces(pGetAdaptersInfo, interfaces);
+#else
+      GetNetworkInterfaces(interfaces);
+#endif
       
       for (int i = 0; i < interfaces.Count(); i++) {
         nsString *s = interfaces.StringAt(i);
         unsigned short *service_name = (PRUnichar*) s->get();
         
+#ifndef WINCE        
         if (!DefineDosDeviceIfNotExists(service_name))
           continue;
 
         // Get the handle to the device. This will fail if the named device is not
         // valid.
         
         HANDLE adapter_handle = GetFileHandle(service_name);
         if (adapter_handle == INVALID_HANDLE_VALUE)
           continue;
+#else
+        BYTE *data; // will store address of  NDIS_802_11_BSSID_LIST data
+#endif
 
         // Get the data.
         
-        BYTE *buffer = reinterpret_cast<BYTE*>(malloc(oid_buffer_size_));
+        BYTE *buffer = (BYTE*)malloc(oid_buffer_size_);
         if (buffer == NULL)
           return NS_ERROR_OUT_OF_MEMORY;
           
         DWORD bytes_out;
         int result;
         
         while (true) {     
           bytes_out = 0; 
+#ifdef WINCE
+          result = PerformQuery(ndis_handle, service_name, buffer, oid_buffer_size_, data, &bytes_out);
+#else
           result = PerformQuery(adapter_handle, buffer, oid_buffer_size_, &bytes_out);
+#endif
           
           if (result == ERROR_GEN_FAILURE ||  // Returned by some Intel cards.
               result == ERROR_INSUFFICIENT_BUFFER ||
               result == ERROR_MORE_DATA ||
               result == NDIS_STATUS_INVALID_LENGTH ||
               result == NDIS_STATUS_BUFFER_TOO_SHORT) {
             
             // The buffer we supplied is too small, so increase it. bytes_out should 
@@ -301,25 +415,29 @@ nsWifiMonitor::DoScan()
             }
           } else {
             // The buffer is not too small.
             break;
           }
         }
           
         if (result == ERROR_SUCCESS) {
-          NDIS_802_11_BSSID_LIST* bssid_list = reinterpret_cast<NDIS_802_11_BSSID_LIST*>(buffer);
+#ifdef WINCE
+          NDIS_802_11_BSSID_LIST* bssid_list = (NDIS_802_11_BSSID_LIST*)data;
+#else
+          NDIS_802_11_BSSID_LIST* bssid_list = (NDIS_802_11_BSSID_LIST*)buffer;
+#endif
           
           // Walk through the BSS IDs.
           int found = 0;
-          const uint8 *iterator = reinterpret_cast<const uint8*>(&bssid_list->Bssid[0]);
-          const uint8 *end_of_buffer = reinterpret_cast<const uint8*>(&bssid_list) + oid_buffer_size_;
+          const uint8 *iterator = (const uint8*)&bssid_list->Bssid[0];
+          const uint8 *end_of_buffer = (const uint8*)buffer + oid_buffer_size_;
           for (int i = 0; i < static_cast<int>(bssid_list->NumberOfItems); ++i) {
             
-            const NDIS_WLAN_BSSID *bss_id = reinterpret_cast<const NDIS_WLAN_BSSID*>(iterator);
+            const NDIS_WLAN_BSSID *bss_id = (const NDIS_WLAN_BSSID*)iterator;
             
             // Check that the length of this BSS ID is reasonable.
             if (bss_id->Length < sizeof(NDIS_WLAN_BSSID) ||
                 iterator + bss_id->Length > end_of_buffer) {
               break;
             }
             
             nsWifiAccessPoint* ap = new nsWifiAccessPoint();
@@ -334,20 +452,24 @@ nsWifiMonitor::DoScan()
             
             // Move to the next BSS ID.
             iterator += bss_id->Length;
           }
 
           free(buffer); 
 
           // Clean up.
+#ifndef WINCE
           CloseHandle(adapter_handle);
+#endif
         }
         
+#ifndef WINCE
         UndefineDosDevice(service_name);
+#endif
       }
      
  
     
     PRBool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
     nsCOMArray<nsIWifiListener> currentListeners;
     
     {
@@ -402,16 +524,20 @@ nsWifiMonitor::DoScan()
     
     nsAutoMonitor mon(mMonitor);
     mon.Wait(PR_SecondsToInterval(60));
   }
   while (mKeepGoing == PR_TRUE);
 
 
 
+#ifdef WINCE
+    //Clean up
+    CloseHandle(ndis_handle);
+#else
   }
   else {
 
     HINSTANCE wlan_library = LoadLibrary("Wlanapi.dll");
     if (!wlan_library)
       return NS_ERROR_NOT_AVAILABLE;
     
     WlanOpenHandleFunction WlanOpenHandle = (WlanOpenHandleFunction) GetProcAddress(wlan_library, "WlanOpenHandle");
@@ -553,10 +679,11 @@ nsWifiMonitor::DoScan()
       // wait for some reasonable amount of time.  pref?
       LOG(("waiting on monitor\n"));
       
       nsAutoMonitor mon(mMonitor);
       mon.Wait(PR_SecondsToInterval(60));
     }
     while (mKeepGoing == PR_TRUE);
   }
+#endif
   return NS_OK;
 }