2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-09-17 00:08:15 +04:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <objc/objc.h>
|
|
|
|
#include <objc/objc-runtime.h>
|
|
|
|
|
2010-02-26 23:18:12 +03:00
|
|
|
#include "nsObjCExceptions.h"
|
2009-09-17 00:08:15 +04:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsWifiMonitor.h"
|
|
|
|
#include "nsWifiAccessPoint.h"
|
|
|
|
|
|
|
|
BOOL UsingSnowLeopard() {
|
|
|
|
static PRInt32 gOSXVersion = 0x0;
|
|
|
|
if (gOSXVersion == 0x0) {
|
|
|
|
Gestalt(gestaltSystemVersion, (SInt32*)&gOSXVersion);
|
|
|
|
}
|
|
|
|
return (gOSXVersion >= 0x00001060);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
|
|
|
|
{
|
2010-02-26 23:18:12 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
if (!UsingSnowLeopard())
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2009-10-09 08:19:55 +04:00
|
|
|
accessPoints.Clear();
|
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2010-02-26 23:18:12 +03:00
|
|
|
@try {
|
|
|
|
NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
|
|
|
|
if (!bundle) {
|
|
|
|
[pool release];
|
2009-10-09 08:19:55 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2010-02-26 23:18:12 +03:00
|
|
|
}
|
2009-09-17 00:08:15 +04:00
|
|
|
|
2009-10-09 08:19:55 +04:00
|
|
|
Class CWI_class = [bundle classNamed:@"CWInterface"];
|
2010-02-26 23:18:12 +03:00
|
|
|
if (!CWI_class) {
|
|
|
|
[pool release];
|
2009-10-09 08:19:55 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2010-02-26 23:18:12 +03:00
|
|
|
}
|
2009-09-17 00:08:15 +04:00
|
|
|
|
2010-02-26 23:18:12 +03:00
|
|
|
id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
|
|
|
|
if (!scanResult) {
|
|
|
|
[pool release];
|
2009-10-09 08:19:55 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2010-02-26 23:18:12 +03:00
|
|
|
}
|
2009-09-17 00:08:15 +04:00
|
|
|
|
2009-10-09 08:19:55 +04:00
|
|
|
NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
|
|
|
|
NSEnumerator *enumerator = [scan objectEnumerator];
|
2009-09-17 00:08:15 +04:00
|
|
|
|
2009-10-09 08:19:55 +04:00
|
|
|
while (id anObject = [enumerator nextObject]) {
|
|
|
|
nsWifiAccessPoint* ap = new nsWifiAccessPoint();
|
2010-02-26 23:18:12 +03:00
|
|
|
if (!ap) {
|
|
|
|
[pool release];
|
2009-10-09 08:19:55 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2010-02-26 23:18:12 +03:00
|
|
|
}
|
2011-05-10 23:44:17 +04:00
|
|
|
|
|
|
|
// [CWInterface bssidData] is deprecated on OS X 10.7 and up. Which is
|
|
|
|
// is a pain, so we'll use it for as long as it's available.
|
|
|
|
unsigned char macData[6] = {0};
|
|
|
|
if ([anObject respondsToSelector:@selector(bssidData)]) {
|
|
|
|
NSData* data = [anObject bssidData];
|
|
|
|
if (data) {
|
|
|
|
memcpy(macData, [data bytes], 6);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// [CWInterface bssid] returns a string formatted "00:00:00:00:00:00".
|
|
|
|
NSString* macString = [anObject bssid];
|
|
|
|
if (macString && ([macString length] == 17)) {
|
|
|
|
for (NSUInteger i = 0; i < 6; ++i) {
|
|
|
|
NSString* part = [macString substringWithRange:NSMakeRange(i * 3, 2)];
|
2011-05-12 00:05:28 +04:00
|
|
|
NSScanner* scanner = [NSScanner scannerWithString:part];
|
|
|
|
unsigned int data = 0;
|
|
|
|
if (![scanner scanHexInt:&data]) {
|
|
|
|
data = 0;
|
|
|
|
}
|
|
|
|
macData[i] = (unsigned char) data;
|
2011-05-10 23:44:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// [CWInterface rssiValue] is available on OS X 10.7 and up (and
|
|
|
|
// [CWInterface rssi] is deprecated).
|
|
|
|
int signal = 0;
|
|
|
|
if ([anObject respondsToSelector:@selector(rssiValue)]) {
|
|
|
|
signal = (int) ((NSInteger) [anObject rssiValue]);
|
|
|
|
} else {
|
|
|
|
signal = [[anObject rssi] intValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
ap->setMac(macData);
|
|
|
|
ap->setSignal(signal);
|
2009-10-09 08:19:55 +04:00
|
|
|
ap->setSSID([[anObject ssid] UTF8String], 32);
|
2009-09-17 00:08:15 +04:00
|
|
|
|
2009-10-09 08:19:55 +04:00
|
|
|
accessPoints.AppendObject(ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@catch(NSException *_exn) {
|
2010-02-26 23:18:12 +03:00
|
|
|
[pool release];
|
2009-10-09 08:19:55 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2009-09-17 00:08:15 +04:00
|
|
|
}
|
2010-02-26 23:18:12 +03:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
2009-09-17 00:08:15 +04:00
|
|
|
return NS_OK;
|
2010-02-26 23:18:12 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);
|
2009-09-17 00:08:15 +04:00
|
|
|
}
|