blob: 7d2b1df3373c6cca7789e40b6f24f20bc16a47ca [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>
Alexander Afanasyeva4e94e42013-04-16 17:43:26 -070019 * Ilya Moiseenko <iliamo@ucla.edu>
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080020 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
21 */
22
23#include "adhoc.h"
24#include "config.h"
25
26#if (__APPLE__ && HAVE_COREWLAN)
27
28#include "logging.h"
29#include <sstream>
30
31using namespace std;
32
33INIT_LOGGER ("Adhoc.OSX");
34
35#import <CoreWLAN/CoreWLAN.h>
36#import <CoreWLAN/CoreWLANConstants.h>
37#import <CoreWLAN/CWInterface.h>
38#import <CoreWLAN/CoreWLANTypes.h>
39
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080040const NSUInteger g_channel = 11;
41static NSString * g_priorNetwork = 0;
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080042
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080043bool
44Adhoc::CreateAdhoc ()
45{
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080046 NSString *networkName = [[NSString alloc] initWithCString:"NDNdirect" encoding:NSASCIIStringEncoding];
47 NSString *passphrase = [[NSString alloc] initWithCString:"NDNhello" encoding:NSASCIIStringEncoding];
48 NSString *securityMode = [[NSString alloc] initWithCString:"Open" encoding:NSASCIIStringEncoding];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080049
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080050 NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080051
52 //Choose the desired interface . the first one will be enought for this example
53 NSString *interfaceName =[airportInterfaces objectAtIndex:0];
54
55 CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
56
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080057 g_priorNetwork = airport.ssid;
Alexander Afanasyev3905e862013-03-04 12:28:38 -080058 _LOG_DEBUG ("Prior network: " << [g_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]);
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080059
60 _LOG_DEBUG ("Starting adhoc connection");
61
62 NSError *error = nil;
63 NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding];
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080064 BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:g_channel password:passphrase error:&error];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080065
66 if (!created)
67 {
68 return false;
69 }
70
71 _LOG_DEBUG ("Creating face for the adhoc connection");
72
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070073 // should do a better job later, when Ndnx::Control will be implemented
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080074
75 ostringstream cmd;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070076 cmd << NDNX_PATH << "/bin/ndndc add / udp 169.254.255.255";
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080077 int ret = system (cmd.str ().c_str ());
78 if (ret == 0)
79 {
80 return true;
81 }
82 else
83 {
84 DestroyAdhoc ();
85 return false;
86 }
87}
88
89void
90Adhoc::DestroyAdhoc ()
91{
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080092 NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080093
94 //Choose the desired interface . the first one will be enought for this example
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080095 NSString *interfaceName = [airportInterfaces objectAtIndex:0];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080096
97 CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
98
99 [airport disassociate];
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800100
101 NSError *err;
Alexander Afanasyevb40fd852013-03-04 11:22:40 -0800102
103 if (g_priorNetwork != 0)
104 {
105 NSSet *scanResults = [airport scanForNetworksWithName:g_priorNetwork error:&err];
106
107 if([scanResults count] > 0)
108 {
109 CWNetwork *previousNetwork = [[scanResults allObjects] objectAtIndex:0];
110
111 [airport associateToNetwork:previousNetwork password:nil error:&err];
112
113 g_priorNetwork = 0;
114 return;
115 }
116
117 g_priorNetwork = 0;
118 }
119
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800120 [airport setPower:NO error:&err];
121 [airport setPower:YES error:&err];
122
123 // ok. this trick works. if just disassociate, then it will stay OFF
124 // setting power OFF/ON trick the system to reconnect to default WiFi
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -0800125}
126
127#endif // ADHOC_SUPPORTED