Debian Codegraph @

network/net_darwin.m

raw
1 #import <Foundation/Foundation.h>
2 #import <CoreWLAN/CoreWLAN.h>
3
4 // The go side of things expects frequencies.
5 int chan2freq(int channel) {
6 if(channel <= 13){
7 return ((channel - 1) * 5) + 2412;
8 } else if(channel == 14) {
9 return 2484;
10 } else if(channel <= 173) {
11 return ((channel - 7) * 5) + 5035;
12 } else if(channel == 177) {
13 return 5885;
14 }
15 return 0;
16 }
17
18 const char *GetSupportedFrequencies(const char *iface) {
19 @autoreleasepool {
20 NSString *interfaceName = [NSString stringWithUTF8String:iface];
21 CWInterface *interface = [CWInterface interfaceWithName:interfaceName];
22 if (!interface) {
23 return NULL;
24 }
25
26 NSSet *supportedChannels = [interface supportedWLANChannels];
27 NSMutableArray *frequencies = [NSMutableArray arrayWithCapacity:[supportedChannels count]];
28
29 for (CWChannel *channel in supportedChannels) {
30 // The go side of things expects frequencies.
31 [frequencies addObject:@(chan2freq(channel.channelNumber))];
32 }
33
34 NSError *error = nil;
35 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:frequencies options:0 error:&error];
36 if (!jsonData) {
37 NSLog(@"Failed to serialize frequencies: %@", error);
38 return NULL;
39 }
40
41 NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
42 return strdup([jsonString UTF8String]);
43 }
44 }
45
46 bool SetInterfaceChannel(const char *iface, int channel) {
47 @autoreleasepool {
48 NSString *interfaceName = [NSString stringWithUTF8String:iface];
49 CWInterface *interface = [CWInterface interfaceWithName:interfaceName];
50 if (!interface) {
51 return false;
52 }
53
54 NSError *error = nil;
55 NSSet *supportedChannels = [interface supportedWLANChannels];
56 for (CWChannel * channelObj in supportedChannels) {
57 // it looks like we can't directly build a CWChannel object anymore
58 if ([channelObj channelNumber] == channel) {
59 [interface setWLANChannel:channelObj error:nil];
60 if (error) {
61 NSLog(@"Failed to set channel: %@", error);
62 return false;
63 }
64 return true;
65 }
66 }
67
68 NSLog(@"channel %d not supported", channel);
69
70 return false;
71 }
72 }