1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Geolocation.
16 * The Initial Developer of the Original Code is Mozilla Foundation
17 * Portions created by the Initial Developer are Copyright (C) 2008
18 * the Initial Developer. All Rights Reserved.
20 * This is a derivative of work done by Google under a BSD style License.
21 * See: http://gears.googlecode.com/svn/trunk/gears/geolocation/
24 * Doug Turner <dougt@meer.net> (Original Author)
25 * Nino D'Aversa <ninodaversa@gmail.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
49 #include "nsAutoPtr.h"
50 #include "nsWifiMonitor.h"
51 #include "nsWifiAccessPoint.h"
53 #include "nsIProxyObjectManager.h"
54 #include "nsServiceManagerUtils.h"
55 #include "nsComponentManagerUtils.h"
56 #include "nsIMutableArray.h"
59 nsWifiMonitor::DoScan()
61 HINSTANCE wlan_library
= LoadLibrary("Wlanapi.dll");
63 return NS_ERROR_NOT_AVAILABLE
;
65 WlanOpenHandleFunction WlanOpenHandle
= (WlanOpenHandleFunction
) GetProcAddress(wlan_library
, "WlanOpenHandle");
66 WlanEnumInterfacesFunction WlanEnumInterfaces
= (WlanEnumInterfacesFunction
) GetProcAddress(wlan_library
, "WlanEnumInterfaces");
67 WlanGetNetworkBssListFunction WlanGetNetworkBssList
= (WlanGetNetworkBssListFunction
) GetProcAddress(wlan_library
, "WlanGetNetworkBssList");
68 WlanFreeMemoryFunction WlanFreeMemory
= (WlanFreeMemoryFunction
) GetProcAddress(wlan_library
, "WlanFreeMemory");
69 WlanCloseHandleFunction WlanCloseHandle
= (WlanCloseHandleFunction
) GetProcAddress(wlan_library
, "WlanCloseHandle");
71 if (!WlanOpenHandle
||
72 !WlanEnumInterfaces
||
73 !WlanGetNetworkBssList
||
76 return NS_ERROR_FAILURE
;
78 // Regularly get the access point data.
80 nsCOMArray
<nsWifiAccessPoint
> lastAccessPoints
;
81 nsCOMArray
<nsWifiAccessPoint
> accessPoints
;
86 // Get the handle to the WLAN API.
87 DWORD negotiated_version
;
88 HANDLE wlan_handle
= NULL
;
89 // We could be executing on either Windows XP or Windows Vista, so use the
90 // lower version of the client WLAN API. It seems that the negotiated version
91 // is the Vista version irrespective of what we pass!
92 static const int kXpWlanClientVersion
= 1;
93 if ((*WlanOpenHandle
)(kXpWlanClientVersion
,
96 &wlan_handle
) != ERROR_SUCCESS
) {
97 return NS_ERROR_NOT_AVAILABLE
;
102 return NS_ERROR_FAILURE
;
104 // Get the list of interfaces. WlanEnumInterfaces allocates interface_list.
105 WLAN_INTERFACE_INFO_LIST
*interface_list
= NULL
;
106 if ((*WlanEnumInterfaces
)(wlan_handle
, NULL
, &interface_list
) != ERROR_SUCCESS
) {
108 (*WlanCloseHandle
)(wlan_handle
, NULL
);
109 return NS_ERROR_FAILURE
;
112 // Go through the list of interfaces and get the data for each.
113 for (int i
= 0; i
< static_cast<int>(interface_list
->dwNumberOfItems
); ++i
) {
115 WLAN_BSS_LIST
*bss_list
;
116 HRESULT rv
= (*WlanGetNetworkBssList
)(wlan_handle
,
117 &interface_list
->InterfaceInfo
[i
].InterfaceGuid
,
118 NULL
, // Use all SSIDs.
119 DOT11_BSS_TYPE_UNUSED
,
120 false, // bSecurityEnabled - unused
123 if (rv
!= ERROR_SUCCESS
) {
127 for (int j
= 0; j
< static_cast<int>(bss_list
->dwNumberOfItems
); ++j
) {
129 nsWifiAccessPoint
* ap
= new nsWifiAccessPoint();
133 const WLAN_BSS_ENTRY bss_entry
= bss_list
->wlanBssEntries
[j
];
135 ap
->setMac(bss_entry
.dot11Bssid
);
136 ap
->setSignal(bss_entry
.lRssi
);
137 ap
->setSSID((char*) bss_entry
.dot11Ssid
.ucSSID
,
138 bss_entry
.dot11Ssid
.uSSIDLength
);
140 accessPoints
.AppendObject(ap
);
142 (*WlanFreeMemory
)(bss_list
);
145 // Free interface_list.
146 (*WlanFreeMemory
)(interface_list
);
149 (*WlanCloseHandle
)(wlan_handle
, NULL
);
152 PRBool accessPointsChanged
= !AccessPointsEqual(accessPoints
, lastAccessPoints
);
153 nsCOMArray
<nsIWifiListener
> currentListeners
;
156 nsAutoMonitor
mon(mMonitor
);
158 for (PRUint32 i
= 0; i
< mListeners
.Length(); i
++) {
159 if (!mListeners
[i
].mHasSentData
|| accessPointsChanged
) {
160 mListeners
[i
].mHasSentData
= PR_TRUE
;
161 currentListeners
.AppendObject(mListeners
[i
].mListener
);
166 ReplaceArray(lastAccessPoints
, accessPoints
);
168 if (currentListeners
.Count() > 0) {
169 PRUint32 resultCount
= lastAccessPoints
.Count();
170 nsIWifiAccessPoint
** result
= static_cast<nsIWifiAccessPoint
**> (nsMemory::Alloc(sizeof(nsIWifiAccessPoint
*) * resultCount
));
172 return NS_ERROR_OUT_OF_MEMORY
;
174 for (PRUint32 i
= 0; i
< resultCount
; i
++)
175 result
[i
] = lastAccessPoints
[i
];
177 for (PRInt32 i
= 0; i
< currentListeners
.Count(); i
++) {
179 LOG(("About to send data to the wifi listeners\n"));
181 nsCOMPtr
<nsIWifiListener
> proxy
;
182 nsCOMPtr
<nsIProxyObjectManager
> proxyObjMgr
= do_GetService("@mozilla.org/xpcomproxy;1");
183 proxyObjMgr
->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD
,
184 NS_GET_IID(nsIWifiListener
),
186 NS_PROXY_SYNC
| NS_PROXY_ALWAYS
,
187 getter_AddRefs(proxy
));
189 LOG(("There is no proxy available. this should never happen\n"));
193 nsresult rv
= proxy
->OnChange(result
, resultCount
);
194 LOG( ("... sent %d\n", rv
));
198 nsMemory::Free(result
);
201 // wait for some reasonable amount of time. pref?
202 LOG(("waiting on monitor\n"));
204 nsAutoMonitor
mon(mMonitor
);
205 mon
.Wait(PR_SecondsToInterval(60));