Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | a4e94e4 | 2013-04-16 17:43:26 -0700 | [diff] [blame] | 19 | * Ilya Moiseenko <iliamo@ucla.edu> |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 20 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 21 | */ |
| 22 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame^] | 23 | #include "adhoc.hpp" |
Alexander Afanasyev | e83c056 | 2016-12-24 10:20:41 -0800 | [diff] [blame] | 24 | #include "core/chronoshare-config.hpp" |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 25 | |
| 26 | #if (__APPLE__ && HAVE_COREWLAN) |
| 27 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame^] | 28 | #include "logging.hpp" |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 29 | #include <sstream> |
| 30 | |
| 31 | using namespace std; |
| 32 | |
| 33 | INIT_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 Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 40 | const NSUInteger g_channel = 11; |
| 41 | static NSString * g_priorNetwork = 0; |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 43 | bool |
| 44 | Adhoc::CreateAdhoc () |
| 45 | { |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 46 | 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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 50 | NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 51 | |
| 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 Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 57 | g_priorNetwork = airport.ssid; |
Alexander Afanasyev | 3905e86 | 2013-03-04 12:28:38 -0800 | [diff] [blame] | 58 | _LOG_DEBUG ("Prior network: " << [g_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]); |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 59 | |
| 60 | _LOG_DEBUG ("Starting adhoc connection"); |
| 61 | |
| 62 | NSError *error = nil; |
| 63 | NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding]; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 64 | BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:g_channel password:passphrase error:&error]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 65 | |
| 66 | if (!created) |
| 67 | { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | _LOG_DEBUG ("Creating face for the adhoc connection"); |
| 72 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 73 | // should do a better job later, when Ndnx::Control will be implemented |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 74 | |
| 75 | ostringstream cmd; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 76 | cmd << NDNX_PATH << "/bin/ndndc add / udp 169.254.255.255"; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 77 | 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 | |
| 89 | void |
| 90 | Adhoc::DestroyAdhoc () |
| 91 | { |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 92 | NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 93 | |
| 94 | //Choose the desired interface . the first one will be enought for this example |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 95 | NSString *interfaceName = [airportInterfaces objectAtIndex:0]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 96 | |
| 97 | CWInterface *airport = [CWInterface interfaceWithName:interfaceName]; |
| 98 | |
| 99 | [airport disassociate]; |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 100 | |
| 101 | NSError *err; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 102 | |
| 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 Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 120 | [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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | #endif // ADHOC_SUPPORTED |