blob: a5f1c706c1db68837ba3a667e52f6e4ac1dfad5b [file] [log] [blame]
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
20 */
21
22#include "adhoc.h"
23#include "config.h"
24
25#if (__APPLE__ && HAVE_COREWLAN)
26
27#include "logging.h"
28#include <sstream>
29
30using namespace std;
31
32INIT_LOGGER ("Adhoc.OSX");
33
34#import <CoreWLAN/CoreWLAN.h>
35#import <CoreWLAN/CoreWLANConstants.h>
36#import <CoreWLAN/CWInterface.h>
37#import <CoreWLAN/CoreWLANTypes.h>
38
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080039const NSUInteger g_channel = 11;
40static NSString * g_priorNetwork = 0;
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080041
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080042bool
43Adhoc::CreateAdhoc ()
44{
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080045 NSString *networkName = [[NSString alloc] initWithCString:"NDNdirect" encoding:NSASCIIStringEncoding];
46 NSString *passphrase = [[NSString alloc] initWithCString:"NDNhello" encoding:NSASCIIStringEncoding];
47 NSString *securityMode = [[NSString alloc] initWithCString:"Open" encoding:NSASCIIStringEncoding];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080048
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080049 NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080050
51 //Choose the desired interface . the first one will be enought for this example
52 NSString *interfaceName =[airportInterfaces objectAtIndex:0];
53
54 CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
55
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080056 g_priorNetwork = airport.ssid;
Alexander Afanasyev3905e862013-03-04 12:28:38 -080057 _LOG_DEBUG ("Prior network: " << [g_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]);
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080058
59 _LOG_DEBUG ("Starting adhoc connection");
60
61 NSError *error = nil;
62 NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding];
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080063 BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:g_channel password:passphrase error:&error];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080064
65 if (!created)
66 {
67 return false;
68 }
69
70 _LOG_DEBUG ("Creating face for the adhoc connection");
71
72 // should do a better job later, when Ccnx::Control will be implemented
73
74 ostringstream cmd;
75 cmd << CCNX_PATH << "/bin/ccndc add / udp 169.254.255.255";
76 int ret = system (cmd.str ().c_str ());
77 if (ret == 0)
78 {
79 return true;
80 }
81 else
82 {
83 DestroyAdhoc ();
84 return false;
85 }
86}
87
88void
89Adhoc::DestroyAdhoc ()
90{
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080091 NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080092
93 //Choose the desired interface . the first one will be enought for this example
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080094 NSString *interfaceName = [airportInterfaces objectAtIndex:0];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080095
96 CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
97
98 [airport disassociate];
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080099
100 NSError *err;
Alexander Afanasyevb40fd852013-03-04 11:22:40 -0800101
102 if (g_priorNetwork != 0)
103 {
104 NSSet *scanResults = [airport scanForNetworksWithName:g_priorNetwork error:&err];
105
106 if([scanResults count] > 0)
107 {
108 CWNetwork *previousNetwork = [[scanResults allObjects] objectAtIndex:0];
109
110 [airport associateToNetwork:previousNetwork password:nil error:&err];
111
112 g_priorNetwork = 0;
113 return;
114 }
115
116 g_priorNetwork = 0;
117 }
118
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800119 [airport setPower:NO error:&err];
120 [airport setPower:YES error:&err];
121
122 // ok. this trick works. if just disassociate, then it will stay OFF
123 // setting power OFF/ON trick the system to reconnect to default WiFi
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -0800124}
125
126#endif // ADHOC_SUPPORTED