Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 7 | * 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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 11 | * 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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 15 | * 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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "adhoc.hpp" |
Alexander Afanasyev | e83c056 | 2016-12-24 10:20:41 -0800 | [diff] [blame] | 22 | #include "core/chronoshare-config.hpp" |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 23 | |
| 24 | #if (__APPLE__ && HAVE_COREWLAN) |
| 25 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 26 | #include "logging.hpp" |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 27 | #include <sstream> |
| 28 | |
| 29 | using namespace std; |
| 30 | |
| 31 | INIT_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 Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 38 | const NSUInteger g_channel = 11; |
| 39 | static NSString * g_priorNetwork = 0; |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 41 | bool |
| 42 | Adhoc::CreateAdhoc () |
| 43 | { |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 44 | 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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 48 | NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 49 | |
| 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 Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 55 | g_priorNetwork = airport.ssid; |
Alexander Afanasyev | 3905e86 | 2013-03-04 12:28:38 -0800 | [diff] [blame] | 56 | _LOG_DEBUG ("Prior network: " << [g_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]); |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 57 | |
| 58 | _LOG_DEBUG ("Starting adhoc connection"); |
| 59 | |
| 60 | NSError *error = nil; |
| 61 | NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding]; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 62 | 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] | 63 | |
| 64 | if (!created) |
| 65 | { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | _LOG_DEBUG ("Creating face for the adhoc connection"); |
| 70 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 71 | // should do a better job later, when Ndnx::Control will be implemented |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 72 | |
| 73 | ostringstream cmd; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 74 | cmd << NDNX_PATH << "/bin/ndndc add / udp 169.254.255.255"; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 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 | |
| 87 | void |
| 88 | Adhoc::DestroyAdhoc () |
| 89 | { |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 90 | NSArray *airportInterfaces = [[CWInterface interfaceNames] allObjects]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 91 | |
| 92 | //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] | 93 | NSString *interfaceName = [airportInterfaces objectAtIndex:0]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 94 | |
| 95 | CWInterface *airport = [CWInterface interfaceWithName:interfaceName]; |
| 96 | |
| 97 | [airport disassociate]; |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 98 | |
| 99 | NSError *err; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 100 | |
| 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 Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 118 | [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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | #endif // ADHOC_SUPPORTED |