Source code

Revision control

Other Tools

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/. */
4
5
#import <Cocoa/Cocoa.h>
6
#import <CoreWLAN/CoreWLAN.h>
7
8
#include <dlfcn.h>
9
#include <unistd.h>
10
11
#include <objc/objc.h>
12
#include <objc/objc-runtime.h>
13
14
#include "nsObjCExceptions.h"
15
#include "nsAutoPtr.h"
16
#include "nsCOMArray.h"
17
#include "nsWifiMonitor.h"
18
#include "nsWifiAccessPoint.h"
19
20
nsresult GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint>& accessPoints) {
21
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
22
23
accessPoints.Clear();
24
25
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
26
27
@try {
28
NSBundle* bundle = [[[NSBundle alloc]
29
initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
30
if (!bundle) {
31
[pool release];
32
return NS_ERROR_NOT_AVAILABLE;
33
}
34
35
Class CWI_class = [bundle classNamed:@"CWInterface"];
36
if (!CWI_class) {
37
[pool release];
38
return NS_ERROR_NOT_AVAILABLE;
39
}
40
41
id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
42
if (!scanResult) {
43
[pool release];
44
return NS_ERROR_NOT_AVAILABLE;
45
}
46
47
NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
48
NSEnumerator* enumerator = [scan objectEnumerator];
49
50
while (id anObject = [enumerator nextObject]) {
51
auto* ap = new nsWifiAccessPoint();
52
if (!ap) {
53
[pool release];
54
return NS_ERROR_OUT_OF_MEMORY;
55
}
56
57
// [CWInterface bssidData] is deprecated on OS X 10.7 and up. Which is
58
// is a pain, so we'll use it for as long as it's available.
59
unsigned char macData[6] = {0};
60
if ([anObject respondsToSelector:@selector(bssidData)]) {
61
NSData* data = [anObject bssidData];
62
if (data) {
63
memcpy(macData, [data bytes], 6);
64
}
65
} else {
66
// [CWInterface bssid] returns a string formatted "00:00:00:00:00:00".
67
NSString* macString = [anObject bssid];
68
if (macString && ([macString length] == 17)) {
69
for (NSUInteger i = 0; i < 6; ++i) {
70
NSString* part = [macString substringWithRange:NSMakeRange(i * 3, 2)];
71
NSScanner* scanner = [NSScanner scannerWithString:part];
72
unsigned int data = 0;
73
if (![scanner scanHexInt:&data]) {
74
data = 0;
75
}
76
macData[i] = (unsigned char)data;
77
}
78
}
79
}
80
81
// [CWInterface rssiValue] is available on OS X 10.7 and up (and
82
// [CWInterface rssi] is deprecated).
83
int signal = 0;
84
if ([anObject respondsToSelector:@selector(rssiValue)]) {
85
signal = (int)((NSInteger)[anObject rssiValue]);
86
} else {
87
signal = [[anObject rssi] intValue];
88
}
89
90
ap->setMac(macData);
91
ap->setSignal(signal);
92
ap->setSSID([[anObject ssid] UTF8String], 32);
93
94
accessPoints.AppendObject(ap);
95
}
96
} @catch (NSException*) {
97
[pool release];
98
return NS_ERROR_NOT_AVAILABLE;
99
}
100
101
[pool release];
102
103
return NS_OK;
104
105
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);
106
}