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