Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / netwerk / wifi / nsWifiScannerMac.cpp
blobebe16ba8f44aba1131e041d0be53b260f2048614
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
12 * License.
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/
23 * Contributor(s):
24 * Doug Turner <dougt@meer.net> (Original Author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include <mach-o/dyld.h>
41 #include <dlfcn.h>
42 #include <unistd.h>
44 #include "osx_wifi.h"
46 #include "nsAutoPtr.h"
47 #include "nsCOMArray.h"
48 #include "nsWifiMonitor.h"
49 #include "nsWifiAccessPoint.h"
51 #include "nsIProxyObjectManager.h"
52 #include "nsServiceManagerUtils.h"
53 #include "nsComponentManagerUtils.h"
54 #include "nsIMutableArray.h"
56 // defined in osx_corewlan.mm
57 // basically relaces accesspoints in the passed reference
58 // it lives in a separate file so that we can use objective c.
59 extern nsresult GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints);
61 nsresult
62 nsWifiMonitor::DoScanWithCoreWLAN()
64 // Regularly get the access point data.
66 nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
67 nsCOMArray<nsWifiAccessPoint> accessPoints;
69 do {
70 nsresult rv = GetAccessPointsFromWLAN(accessPoints);
71 if (NS_FAILED(rv))
72 return rv;
74 PRBool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
75 nsCOMArray<nsIWifiListener> currentListeners;
78 nsAutoMonitor mon(mMonitor);
80 for (PRUint32 i = 0; i < mListeners.Length(); i++) {
81 if (!mListeners[i].mHasSentData || accessPointsChanged) {
82 mListeners[i].mHasSentData = PR_TRUE;
83 currentListeners.AppendObject(mListeners[i].mListener);
88 ReplaceArray(lastAccessPoints, accessPoints);
90 if (currentListeners.Count() > 0)
92 PRUint32 resultCount = lastAccessPoints.Count();
93 nsIWifiAccessPoint** result = static_cast<nsIWifiAccessPoint**> (nsMemory::Alloc(sizeof(nsIWifiAccessPoint*) * resultCount));
94 if (!result) {
95 return NS_ERROR_OUT_OF_MEMORY;
98 for (PRUint32 i = 0; i < resultCount; i++)
99 result[i] = lastAccessPoints[i];
101 for (PRInt32 i = 0; i < currentListeners.Count(); i++) {
103 LOG(("About to send data to the wifi listeners\n"));
105 nsCOMPtr<nsIWifiListener> proxy;
106 nsCOMPtr<nsIProxyObjectManager> proxyObjMgr = do_GetService("@mozilla.org/xpcomproxy;1");
107 proxyObjMgr->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
108 NS_GET_IID(nsIWifiListener),
109 currentListeners[i],
110 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
111 getter_AddRefs(proxy));
112 if (!proxy) {
113 LOG(("There is no proxy available. this should never happen\n"));
115 else
117 nsresult rv = proxy->OnChange(result, resultCount);
118 LOG( ("... sent %d\n", rv));
122 nsMemory::Free(result);
125 // wait for some reasonable amount of time. pref?
126 LOG(("waiting on monitor\n"));
128 nsAutoMonitor mon(mMonitor);
129 mon.Wait(PR_SecondsToInterval(60));
131 while (mKeepGoing);
133 return NS_OK;
136 nsresult
137 nsWifiMonitor::DoScanOld()
139 void *apple_80211_library = dlopen(
140 "/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211",
141 RTLD_LAZY);
142 if (!apple_80211_library)
143 return NS_ERROR_NOT_AVAILABLE;
145 WirelessContextPtr wifi_context_;
147 WirelessAttachFunction WirelessAttach_function_ = reinterpret_cast<WirelessAttachFunction>(dlsym(apple_80211_library, "WirelessAttach"));
148 WirelessScanSplitFunction WirelessScanSplit_function_ = reinterpret_cast<WirelessScanSplitFunction>(dlsym(apple_80211_library, "WirelessScanSplit"));
149 WirelessDetachFunction WirelessDetach_function_ = reinterpret_cast<WirelessDetachFunction>(dlsym(apple_80211_library, "WirelessDetach"));
151 if (!WirelessAttach_function_ || !WirelessScanSplit_function_ || !WirelessDetach_function_) {
152 dlclose(apple_80211_library);
153 return NS_ERROR_NOT_AVAILABLE;
156 WIErr err = 0;
158 err = (*WirelessAttach_function_)(&wifi_context_, 0);
159 if (err != noErr) {
160 printf("Error: WirelessAttach: %d\n", (int) err);
161 dlclose(apple_80211_library);
162 return NS_ERROR_FAILURE;
165 // Regularly get the access point data.
167 nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
168 nsCOMArray<nsWifiAccessPoint> accessPoints;
170 do {
171 accessPoints.Clear();
173 CFArrayRef managed_access_points = NULL;
174 CFArrayRef adhoc_access_points = NULL;
176 if ((*WirelessScanSplit_function_)(wifi_context_,
177 &managed_access_points,
178 &adhoc_access_points,
179 0) != noErr) {
180 return NS_ERROR_FAILURE;
183 if (managed_access_points == NULL) {
184 return NS_ERROR_FAILURE;
187 int accessPointsCount = CFArrayGetCount(managed_access_points);
189 for (int i = 0; i < accessPointsCount; ++i) {
191 nsWifiAccessPoint* ap = new nsWifiAccessPoint();
192 if (!ap)
193 continue;
195 const WirelessNetworkInfo *access_point_info =
196 reinterpret_cast<const WirelessNetworkInfo*>(CFDataGetBytePtr(reinterpret_cast<const CFDataRef>(CFArrayGetValueAtIndex(managed_access_points, i))));
198 ap->setMac(access_point_info->macAddress);
200 // WirelessNetworkInfo::signal appears to be signal strength in dBm.
201 ap->setSignal(access_point_info->signal);
203 ap->setSSID(reinterpret_cast<const char*>(access_point_info->name),
204 access_point_info->nameLen);
206 accessPoints.AppendObject(ap);
209 PRBool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
210 nsCOMArray<nsIWifiListener> currentListeners;
213 nsAutoMonitor mon(mMonitor);
215 for (PRUint32 i = 0; i < mListeners.Length(); i++) {
216 if (!mListeners[i].mHasSentData || accessPointsChanged) {
217 mListeners[i].mHasSentData = PR_TRUE;
218 currentListeners.AppendObject(mListeners[i].mListener);
223 ReplaceArray(lastAccessPoints, accessPoints);
225 if (currentListeners.Count() > 0)
227 PRUint32 resultCount = lastAccessPoints.Count();
228 nsIWifiAccessPoint** result = static_cast<nsIWifiAccessPoint**> (nsMemory::Alloc(sizeof(nsIWifiAccessPoint*) * resultCount));
229 if (!result) {
230 dlclose(apple_80211_library);
231 return NS_ERROR_OUT_OF_MEMORY;
234 for (PRUint32 i = 0; i < resultCount; i++)
235 result[i] = lastAccessPoints[i];
237 for (PRInt32 i = 0; i < currentListeners.Count(); i++) {
239 LOG(("About to send data to the wifi listeners\n"));
241 nsCOMPtr<nsIWifiListener> proxy;
242 nsCOMPtr<nsIProxyObjectManager> proxyObjMgr = do_GetService("@mozilla.org/xpcomproxy;1");
243 proxyObjMgr->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
244 NS_GET_IID(nsIWifiListener),
245 currentListeners[i],
246 NS_PROXY_SYNC | NS_PROXY_ALWAYS,
247 getter_AddRefs(proxy));
248 if (!proxy) {
249 LOG(("There is no proxy available. this should never happen\n"));
251 else
253 nsresult rv = proxy->OnChange(result, resultCount);
254 LOG( ("... sent %d\n", rv));
258 nsMemory::Free(result);
261 // wait for some reasonable amount of time. pref?
262 LOG(("waiting on monitor\n"));
264 nsAutoMonitor mon(mMonitor);
265 mon.Wait(PR_SecondsToInterval(60));
267 while (mKeepGoing);
269 (*WirelessDetach_function_)(wifi_context_);
271 dlclose(apple_80211_library);
273 return NS_OK;
276 nsresult
277 nsWifiMonitor::DoScan()
279 nsresult rv = DoScanWithCoreWLAN();
281 // we can remove this once we stop caring about 10.5.
282 if (NS_FAILED(rv))
283 rv = DoScanOld();
285 return rv;