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> |
| 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 | |
| 30 | using namespace std; |
| 31 | |
| 32 | INIT_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 Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 39 | const NSUInteger g_channel = 11; |
| 40 | static NSString * g_priorNetwork = 0; |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 42 | bool |
| 43 | Adhoc::CreateAdhoc () |
| 44 | { |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 45 | NSString *networkName = [[NSString alloc] initWithCString:"NDNdirect" encoding:NSASCIIStringEncoding]; |
| 46 | NSString *passphrase = [[NSString alloc] initWithCString:"NDNhello" encoding:NSASCIIStringEncoding]; |
| 47 | NSString *securityMode = [[NSString alloc] initWithCString:"Open" encoding:NSASCIIStringEncoding]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 49 | NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 50 | |
| 51 | //Choose the desired interface . the first one will be enought for this example |
| 52 | NSString *interfaceName =[airportInterfaces objectAtIndex:0]; |
| 53 | |
| 54 | CWInterface *airport = [CWInterface interfaceWithName:interfaceName]; |
| 55 | |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 56 | g_priorNetwork = airport.ssid; |
Alexander Afanasyev | 3905e86 | 2013-03-04 12:28:38 -0800 | [diff] [blame^] | 57 | _LOG_DEBUG ("Prior network: " << [g_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]); |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 58 | |
| 59 | _LOG_DEBUG ("Starting adhoc connection"); |
| 60 | |
| 61 | NSError *error = nil; |
| 62 | NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding]; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 63 | 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] | 64 | |
| 65 | if (!created) |
| 66 | { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | _LOG_DEBUG ("Creating face for the adhoc connection"); |
| 71 | |
| 72 | // should do a better job later, when Ccnx::Control will be implemented |
| 73 | |
| 74 | ostringstream cmd; |
| 75 | cmd << CCNX_PATH << "/bin/ccndc add / udp 169.254.255.255"; |
| 76 | int ret = system (cmd.str ().c_str ()); |
| 77 | if (ret == 0) |
| 78 | { |
| 79 | return true; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | DestroyAdhoc (); |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | Adhoc::DestroyAdhoc () |
| 90 | { |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 91 | NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 92 | |
| 93 | //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] | 94 | NSString *interfaceName = [airportInterfaces objectAtIndex:0]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 95 | |
| 96 | CWInterface *airport = [CWInterface interfaceWithName:interfaceName]; |
| 97 | |
| 98 | [airport disassociate]; |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 99 | |
| 100 | NSError *err; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 101 | |
| 102 | if (g_priorNetwork != 0) |
| 103 | { |
| 104 | NSSet *scanResults = [airport scanForNetworksWithName:g_priorNetwork error:&err]; |
| 105 | |
| 106 | if([scanResults count] > 0) |
| 107 | { |
| 108 | CWNetwork *previousNetwork = [[scanResults allObjects] objectAtIndex:0]; |
| 109 | |
| 110 | [airport associateToNetwork:previousNetwork password:nil error:&err]; |
| 111 | |
| 112 | g_priorNetwork = 0; |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | g_priorNetwork = 0; |
| 117 | } |
| 118 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 119 | [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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | #endif // ADHOC_SUPPORTED |