blob: 52589e6112bf16ed71ef69d36c2569c412a494b2 [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
31INIT_LOGGER ("Adhoc.OSX");
32
33#import <CoreWLAN/CoreWLAN.h>
34#import <CoreWLAN/CoreWLANConstants.h>
35#import <CoreWLAN/CWInterface.h>
36#import <CoreWLAN/CoreWLANTypes.h>
37
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080038const NSUInteger g_channel = 11;
39static NSString * g_priorNetwork = 0;
Alexander Afanasyeve8496a32013-03-03 16:10:43 -080040
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
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080055 g_priorNetwork = airport.ssid;
Alexander Afanasyev3905e862013-03-04 12:28:38 -080056 _LOG_DEBUG ("Prior network: " << [g_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]);
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080057
58 _LOG_DEBUG ("Starting adhoc connection");
59
60 NSError *error = nil;
61 NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding];
Alexander Afanasyevb40fd852013-03-04 11:22:40 -080062 BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:g_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
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070071 // should do a better job later, when Ndnx::Control will be implemented
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080072
73 ostringstream cmd;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070074 cmd << NDNX_PATH << "/bin/ndndc add / udp 169.254.255.255";
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -080075 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;
Alexander Afanasyevb40fd852013-03-04 11:22:40 -0800100
101 if (g_priorNetwork != 0)
102 {
103 NSSet *scanResults = [airport scanForNetworksWithName:g_priorNetwork error:&err];
104
105 if([scanResults count] > 0)
106 {
107 CWNetwork *previousNetwork = [[scanResults allObjects] objectAtIndex:0];
108
109 [airport associateToNetwork:previousNetwork password:nil error:&err];
110
111 g_priorNetwork = 0;
112 return;
113 }
114
115 g_priorNetwork = 0;
116 }
117
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800118 [airport setPower:NO error:&err];
119 [airport setPower:YES error:&err];
120
121 // ok. this trick works. if just disassociate, then it will stay OFF
122 // setting power OFF/ON trick the system to reconnect to default WiFi
Alexander Afanasyeva98e69c2013-02-24 15:42:45 -0800123}
124
125#endif // ADHOC_SUPPORTED