1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #import <Cocoa/Cocoa.h>
6 #import <CoreWLAN/CoreWLAN.h>
8 #include <mach-o/dyld.h>
12 #include <objc/objc.h>
13 #include <objc/objc-runtime.h>
15 #include "nsObjCExceptions.h"
16 #include "nsAutoPtr.h"
17 #include "nsCOMArray.h"
18 #include "nsWifiMonitor.h"
19 #include "nsWifiAccessPoint.h"
21 BOOL UsingSnowLeopard() {
22 static int32_t gOSXVersion = 0x0;
23 if (gOSXVersion == 0x0) {
24 Gestalt(gestaltSystemVersion, (SInt32*)&gOSXVersion);
26 return (gOSXVersion >= 0x00001060);
30 GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
32 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
34 if (!UsingSnowLeopard())
35 return NS_ERROR_NOT_AVAILABLE;
39 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
42 NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
45 return NS_ERROR_NOT_AVAILABLE;
48 Class CWI_class = [bundle classNamed:@"CWInterface"];
51 return NS_ERROR_NOT_AVAILABLE;
54 id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
57 return NS_ERROR_NOT_AVAILABLE;
60 NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
61 NSEnumerator *enumerator = [scan objectEnumerator];
63 while (id anObject = [enumerator nextObject]) {
64 nsWifiAccessPoint* ap = new nsWifiAccessPoint();
67 return NS_ERROR_OUT_OF_MEMORY;
70 // [CWInterface bssidData] is deprecated on OS X 10.7 and up. Which is
71 // is a pain, so we'll use it for as long as it's available.
72 unsigned char macData[6] = {0};
73 if ([anObject respondsToSelector:@selector(bssidData)]) {
74 NSData* data = [anObject bssidData];
76 memcpy(macData, [data bytes], 6);
79 // [CWInterface bssid] returns a string formatted "00:00:00:00:00:00".
80 NSString* macString = [anObject bssid];
81 if (macString && ([macString length] == 17)) {
82 for (NSUInteger i = 0; i < 6; ++i) {
83 NSString* part = [macString substringWithRange:NSMakeRange(i * 3, 2)];
84 NSScanner* scanner = [NSScanner scannerWithString:part];
85 unsigned int data = 0;
86 if (![scanner scanHexInt:&data]) {
89 macData[i] = (unsigned char) data;
94 // [CWInterface rssiValue] is available on OS X 10.7 and up (and
95 // [CWInterface rssi] is deprecated).
97 if ([anObject respondsToSelector:@selector(rssiValue)]) {
98 signal = (int) ((NSInteger) [anObject rssiValue]);
100 signal = [[anObject rssi] intValue];
104 ap->setSignal(signal);
105 ap->setSSID([[anObject ssid] UTF8String], 32);
107 accessPoints.AppendObject(ap);
110 @catch(NSException *_exn) {
112 return NS_ERROR_NOT_AVAILABLE;
119 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);