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>
11 #include <objc/objc.h>
12 #include <objc/objc-runtime.h>
14 #include "nsObjCExceptions.h"
15 #include "nsAutoPtr.h"
16 #include "nsCOMArray.h"
17 #include "nsWifiMonitor.h"
18 #include "nsWifiAccessPoint.h"
21 GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
23 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
27 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
30 NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
33 return NS_ERROR_NOT_AVAILABLE;
36 Class CWI_class = [bundle classNamed:@"CWInterface"];
39 return NS_ERROR_NOT_AVAILABLE;
42 id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
45 return NS_ERROR_NOT_AVAILABLE;
48 NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
49 NSEnumerator *enumerator = [scan objectEnumerator];
51 while (id anObject = [enumerator nextObject]) {
52 nsWifiAccessPoint* ap = new nsWifiAccessPoint();
55 return NS_ERROR_OUT_OF_MEMORY;
58 // [CWInterface bssidData] is deprecated on OS X 10.7 and up. Which is
59 // is a pain, so we'll use it for as long as it's available.
60 unsigned char macData[6] = {0};
61 if ([anObject respondsToSelector:@selector(bssidData)]) {
62 NSData* data = [anObject bssidData];
64 memcpy(macData, [data bytes], 6);
67 // [CWInterface bssid] returns a string formatted "00:00:00:00:00:00".
68 NSString* macString = [anObject bssid];
69 if (macString && ([macString length] == 17)) {
70 for (NSUInteger i = 0; i < 6; ++i) {
71 NSString* part = [macString substringWithRange:NSMakeRange(i * 3, 2)];
72 NSScanner* scanner = [NSScanner scannerWithString:part];
73 unsigned int data = 0;
74 if (![scanner scanHexInt:&data]) {
77 macData[i] = (unsigned char) data;
82 // [CWInterface rssiValue] is available on OS X 10.7 and up (and
83 // [CWInterface rssi] is deprecated).
85 if ([anObject respondsToSelector:@selector(rssiValue)]) {
86 signal = (int) ((NSInteger) [anObject rssiValue]);
88 signal = [[anObject rssi] intValue];
92 ap->setSignal(signal);
93 ap->setSSID([[anObject ssid] UTF8String], 32);
95 accessPoints.AppendObject(ap);
98 @catch(NSException *_exn) {
100 return NS_ERROR_NOT_AVAILABLE;
107 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);