blob: 3b7da801a1b4ff145f21d8c7d19addca448a8a3f [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
39bool
40Adhoc::CreateAdhoc ()
41{
42 NSString *networkName = @"NDNdirect";
43 NSNumber *channel = @11;
44 NSString *passphrase = @"NDNhello";
45 NSString *securityMode = @"Open";
46
47 NSArray *airportInterfaces = [CWInterface supportedInterfaces];
48
49 //Choose the desired interface . the first one will be enought for this example
50 NSString *interfaceName =[airportInterfaces objectAtIndex:0];
51
52 CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
53
54 NSString * _priorNetwork = airport.ssid;
55 _LOG_DEBUG ("Prior network: " << [_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]);
56
57 _LOG_DEBUG ("Starting adhoc connection");
58
59 NSError *error = nil;
60 NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding];
61 BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:(NSUInteger)@11 password:passphrase error:&error];
62
63 if (!created)
64 {
65 return false;
66 }
67
68 _LOG_DEBUG ("Creating face for the adhoc connection");
69
70 // should do a better job later, when Ccnx::Control will be implemented
71
72 ostringstream cmd;
73 cmd << CCNX_PATH << "/bin/ccndc add / udp 169.254.255.255";
74 int ret = system (cmd.str ().c_str ());
75 if (ret == 0)
76 {
77 return true;
78 }
79 else
80 {
81 DestroyAdhoc ();
82 return false;
83 }
84}
85
86void
87Adhoc::DestroyAdhoc ()
88{
89 NSString *networkName = @"NDNdirect";
90 NSNumber *channel = @11;
91 NSString *passphrase = @"NDNhello";
92 NSString *securityMode = @"Open";
93
94 NSArray *airportInterfaces = [CWInterface supportedInterfaces];
95
96 //Choose the desired interface . the first one will be enought for this example
97 NSString *interfaceName =[airportInterfaces objectAtIndex:0];
98
99 CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
100
101 [airport disassociate];
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800102
103 NSError *err;
104 [airport setPower:NO error:&err];
105 [airport setPower:YES error:&err];
106
107 // ok. this trick works. if just disassociate, then it will stay OFF
108 // setting power OFF/ON trick the system to reconnect to default WiFi
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -0800109}
110
111#endif // ADHOC_SUPPORTED