1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/wifi/osx_corewlan.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#import <Cocoa/Cocoa.h> 1.9 +#import <CoreWLAN/CoreWLAN.h> 1.10 + 1.11 +#include <dlfcn.h> 1.12 +#include <unistd.h> 1.13 + 1.14 +#include <objc/objc.h> 1.15 +#include <objc/objc-runtime.h> 1.16 + 1.17 +#include "nsObjCExceptions.h" 1.18 +#include "nsAutoPtr.h" 1.19 +#include "nsCOMArray.h" 1.20 +#include "nsWifiMonitor.h" 1.21 +#include "nsWifiAccessPoint.h" 1.22 + 1.23 +nsresult 1.24 +GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints) 1.25 +{ 1.26 + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; 1.27 + 1.28 + accessPoints.Clear(); 1.29 + 1.30 + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 1.31 + 1.32 + @try { 1.33 + NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease]; 1.34 + if (!bundle) { 1.35 + [pool release]; 1.36 + return NS_ERROR_NOT_AVAILABLE; 1.37 + } 1.38 + 1.39 + Class CWI_class = [bundle classNamed:@"CWInterface"]; 1.40 + if (!CWI_class) { 1.41 + [pool release]; 1.42 + return NS_ERROR_NOT_AVAILABLE; 1.43 + } 1.44 + 1.45 + id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil]; 1.46 + if (!scanResult) { 1.47 + [pool release]; 1.48 + return NS_ERROR_NOT_AVAILABLE; 1.49 + } 1.50 + 1.51 + NSArray* scan = [NSMutableArray arrayWithArray:scanResult]; 1.52 + NSEnumerator *enumerator = [scan objectEnumerator]; 1.53 + 1.54 + while (id anObject = [enumerator nextObject]) { 1.55 + nsWifiAccessPoint* ap = new nsWifiAccessPoint(); 1.56 + if (!ap) { 1.57 + [pool release]; 1.58 + return NS_ERROR_OUT_OF_MEMORY; 1.59 + } 1.60 + 1.61 + // [CWInterface bssidData] is deprecated on OS X 10.7 and up. Which is 1.62 + // is a pain, so we'll use it for as long as it's available. 1.63 + unsigned char macData[6] = {0}; 1.64 + if ([anObject respondsToSelector:@selector(bssidData)]) { 1.65 + NSData* data = [anObject bssidData]; 1.66 + if (data) { 1.67 + memcpy(macData, [data bytes], 6); 1.68 + } 1.69 + } else { 1.70 + // [CWInterface bssid] returns a string formatted "00:00:00:00:00:00". 1.71 + NSString* macString = [anObject bssid]; 1.72 + if (macString && ([macString length] == 17)) { 1.73 + for (NSUInteger i = 0; i < 6; ++i) { 1.74 + NSString* part = [macString substringWithRange:NSMakeRange(i * 3, 2)]; 1.75 + NSScanner* scanner = [NSScanner scannerWithString:part]; 1.76 + unsigned int data = 0; 1.77 + if (![scanner scanHexInt:&data]) { 1.78 + data = 0; 1.79 + } 1.80 + macData[i] = (unsigned char) data; 1.81 + } 1.82 + } 1.83 + } 1.84 + 1.85 + // [CWInterface rssiValue] is available on OS X 10.7 and up (and 1.86 + // [CWInterface rssi] is deprecated). 1.87 + int signal = 0; 1.88 + if ([anObject respondsToSelector:@selector(rssiValue)]) { 1.89 + signal = (int) ((NSInteger) [anObject rssiValue]); 1.90 + } else { 1.91 + signal = [[anObject rssi] intValue]; 1.92 + } 1.93 + 1.94 + ap->setMac(macData); 1.95 + ap->setSignal(signal); 1.96 + ap->setSSID([[anObject ssid] UTF8String], 32); 1.97 + 1.98 + accessPoints.AppendObject(ap); 1.99 + } 1.100 + } 1.101 + @catch(NSException *_exn) { 1.102 + [pool release]; 1.103 + return NS_ERROR_NOT_AVAILABLE; 1.104 + } 1.105 + 1.106 + [pool release]; 1.107 + 1.108 + return NS_OK; 1.109 + 1.110 + NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE); 1.111 +}