blob: c8cf0e159c3d3a43f71053a386274cfa434eb2c6 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "adhoc.hpp"
Alexander Afanasyeve83c0562016-12-24 10:20:41 -080022#include "core/chronoshare-config.hpp"
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080023
24#if (__APPLE__ && HAVE_COREWLAN)
25
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080026#include "logging.hpp"
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080027#include <sstream>
28
29using namespace std;
30
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080031INIT_LOGGER("Adhoc.OSX");
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080032
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080033#import <CoreWLAN/CWInterface.h>
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080034#import <CoreWLAN/CoreWLAN.h>
35#import <CoreWLAN/CoreWLANConstants.h>
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080036#import <CoreWLAN/CoreWLANTypes.h>
37
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080038const NSUInteger g_channel = 11;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039static NSString* g_priorNetwork = 0;
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080040
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080041bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080042Adhoc::CreateAdhoc()
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080043{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080044 NSString* networkName =
45 [[NSString alloc] initWithCString:"NDNdirect" encoding:NSASCIIStringEncoding];
46 NSString* passphrase =
47 [[NSString alloc] initWithCString:"NDNhello" encoding:NSASCIIStringEncoding];
48 NSString* securityMode = [[NSString alloc] initWithCString:"Open" encoding:NSASCIIStringEncoding];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080049
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -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
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080053 NSString* interfaceName = [airportInterfaces objectAtIndex:0];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080054
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080055 CWInterface* airport = [CWInterface interfaceWithName:interfaceName];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080056
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080057 g_priorNetwork = airport.ssid;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 _LOG_DEBUG("Prior network: " << [g_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]);
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080059
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080060 _LOG_DEBUG("Starting adhoc connection");
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080061
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062 NSError* error = nil;
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080063 NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding];
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080064 BOOL created = [airport startIBSSModeWithSSID:data
65 security:kCWIBSSModeSecurityNone
66 channel:g_channel
67 password:passphrase
68 error:&error];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080069
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080070 if (!created) {
71 return false;
72 }
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080073
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080074 _LOG_DEBUG("Creating face for the adhoc connection");
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080075
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070076 // should do a better job later, when Ndnx::Control will be implemented
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080077
78 ostringstream cmd;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080079 cmd << CCNX_PATH << "/bin/ccndc add / udp 169.254.255.255";
80 int ret = system(cmd.str().c_str());
81 if (ret == 0) {
82 return true;
83 }
84 else {
85 DestroyAdhoc();
86 return false;
87 }
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080088}
89
90void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091Adhoc::DestroyAdhoc()
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080092{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093 NSArray* airportInterfaces = [[CWInterface interfaceNames] allObjects];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080094
95 //Choose the desired interface . the first one will be enought for this example
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080096 NSString* interfaceName = [airportInterfaces objectAtIndex:0];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080097
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080098 CWInterface* airport = [CWInterface interfaceWithName:interfaceName];
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080099
100 [airport disassociate];
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800101
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800102 NSError* err;
Alexander Afanasyevb40fd852013-03-04 11:22:40 -0800103
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800104 if (g_priorNetwork != 0) {
105 NSSet* scanResults = [airport scanForNetworksWithName:g_priorNetwork error:&err];
Alexander Afanasyevb40fd852013-03-04 11:22:40 -0800106
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 if ([scanResults count] > 0) {
108 CWNetwork* previousNetwork = [[scanResults allObjects] objectAtIndex:0];
Alexander Afanasyevb40fd852013-03-04 11:22:40 -0800109
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 [airport associateToNetwork:previousNetwork password:nil error:&err];
Alexander Afanasyevb40fd852013-03-04 11:22:40 -0800111
112 g_priorNetwork = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800113 return;
Alexander Afanasyevb40fd852013-03-04 11:22:40 -0800114 }
115
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800116 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