blob: 9132ad97db7e292428774a4bfd47c3c2bccf6ac0 [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 Afanasyeve8496a32013-03-03 16:10:43 -080039const NSUInteger channel = 11;
40
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080041bool
42Adhoc::CreateAdhoc ()
43{
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080044 NSString *networkName = [[NSString alloc] initWithCString:"NDNdirect" encoding:NSASCIIStringEncoding];
45 NSString *passphrase = [[NSString alloc] initWithCString:"NDNhello" encoding:NSASCIIStringEncoding];
46 NSString *securityMode = [[NSString alloc] initWithCString:"Open" encoding:NSASCIIStringEncoding];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080047
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080048 NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080049
50 //Choose the desired interface . the first one will be enought for this example
51 NSString *interfaceName =[airportInterfaces objectAtIndex:0];
52
53 CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
54
55 NSString * _priorNetwork = airport.ssid;
56 _LOG_DEBUG ("Prior network: " << [_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]);
57
58 _LOG_DEBUG ("Starting adhoc connection");
59
60 NSError *error = nil;
61 NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding];
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080062 BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:channel password:passphrase error:&error];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080063
64 if (!created)
65 {
66 return false;
67 }
68
69 _LOG_DEBUG ("Creating face for the adhoc connection");
70
71 // should do a better job later, when Ccnx::Control will be implemented
72
73 ostringstream cmd;
74 cmd << CCNX_PATH << "/bin/ccndc add / udp 169.254.255.255";
75 int ret = system (cmd.str ().c_str ());
76 if (ret == 0)
77 {
78 return true;
79 }
80 else
81 {
82 DestroyAdhoc ();
83 return false;
84 }
85}
86
87void
88Adhoc::DestroyAdhoc ()
89{
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080090 NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080091
92 //Choose the desired interface . the first one will be enought for this example
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080093 NSString *interfaceName = [airportInterfaces objectAtIndex:0];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080094
95 CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
96
97 [airport disassociate];
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080098
99 NSError *err;
100 [airport setPower:NO error:&err];
101 [airport setPower:YES error:&err];
102
103 // ok. this trick works. if just disassociate, then it will stay OFF
104 // setting power OFF/ON trick the system to reconnect to default WiFi
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -0800105}
106
107#endif // ADHOC_SUPPORTED