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) 2009
18 * the Initial Developer. All Rights Reserved.
21 * Doug Turner <dougt@meer.net> (Original Author)
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #import <Cocoa/Cocoa.h>
39 #include <mach-o/dyld.h>
43 #include <objc/objc.h>
44 #include <objc/objc-runtime.h>
46 #include "nsObjCExceptions.h"
47 #include "nsAutoPtr.h"
48 #include "nsCOMArray.h"
49 #include "nsWifiMonitor.h"
50 #include "nsWifiAccessPoint.h"
52 BOOL UsingSnowLeopard() {
53 static PRInt32 gOSXVersion = 0x0;
54 if (gOSXVersion == 0x0) {
55 Gestalt(gestaltSystemVersion, (SInt32*)&gOSXVersion);
57 return (gOSXVersion >= 0x00001060);
61 GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
63 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
65 if (!UsingSnowLeopard())
66 return NS_ERROR_NOT_AVAILABLE;
70 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
73 NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
76 return NS_ERROR_NOT_AVAILABLE;
79 Class CWI_class = [bundle classNamed:@"CWInterface"];
82 return NS_ERROR_NOT_AVAILABLE;
85 id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
88 return NS_ERROR_NOT_AVAILABLE;
91 NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
92 NSEnumerator *enumerator = [scan objectEnumerator];
94 while (id anObject = [enumerator nextObject]) {
95 nsWifiAccessPoint* ap = new nsWifiAccessPoint();
98 return NS_ERROR_OUT_OF_MEMORY;
100 NSData* data = [anObject bssidData];
101 ap->setMac((unsigned char*)[data bytes]);
102 ap->setSignal([[anObject rssi] intValue]);
103 ap->setSSID([[anObject ssid] UTF8String], 32);
105 accessPoints.AppendObject(ap);
108 @catch(NSException *_exn) {
110 return NS_ERROR_NOT_AVAILABLE;
117 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);