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 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 31 | INIT_LOGGER("Adhoc.OSX"); |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 33 | #import <CoreWLAN/CWInterface.h> |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 34 | #import <CoreWLAN/CoreWLAN.h> |
| 35 | #import <CoreWLAN/CoreWLANConstants.h> |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 36 | #import <CoreWLAN/CoreWLANTypes.h> |
| 37 | |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 38 | const NSUInteger g_channel = 11; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 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 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 42 | Adhoc::CreateAdhoc() |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 43 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 44 | 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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -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 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 53 | NSString* interfaceName = [airportInterfaces objectAtIndex:0]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 55 | CWInterface* airport = [CWInterface interfaceWithName:interfaceName]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 57 | g_priorNetwork = airport.ssid; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -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 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 60 | _LOG_DEBUG("Starting adhoc connection"); |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 62 | NSError* error = nil; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 63 | NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding]; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 64 | BOOL created = [airport startIBSSModeWithSSID:data |
| 65 | security:kCWIBSSModeSecurityNone |
| 66 | channel:g_channel |
| 67 | password:passphrase |
| 68 | error:&error]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 70 | if (!created) { |
| 71 | return false; |
| 72 | } |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 74 | _LOG_DEBUG("Creating face for the adhoc connection"); |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 76 | // should do a better job later, when Ndnx::Control will be implemented |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 77 | |
| 78 | ostringstream cmd; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 79 | 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 Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 91 | Adhoc::DestroyAdhoc() |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 92 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 93 | NSArray* airportInterfaces = [[CWInterface interfaceNames] allObjects]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 94 | |
| 95 | //Choose the desired interface . the first one will be enought for this example |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 96 | NSString* interfaceName = [airportInterfaces objectAtIndex:0]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 98 | CWInterface* airport = [CWInterface interfaceWithName:interfaceName]; |
Alexander Afanasyev | a98e69c | 2013-02-24 15:42:45 -0800 | [diff] [blame] | 99 | |
| 100 | [airport disassociate]; |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 102 | NSError* err; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 104 | if (g_priorNetwork != 0) { |
| 105 | NSSet* scanResults = [airport scanForNetworksWithName:g_priorNetwork error:&err]; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 107 | if ([scanResults count] > 0) { |
| 108 | CWNetwork* previousNetwork = [[scanResults allObjects] objectAtIndex:0]; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 110 | [airport associateToNetwork:previousNetwork password:nil error:&err]; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 111 | |
| 112 | g_priorNetwork = 0; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 113 | return; |
Alexander Afanasyev | b40fd85 | 2013-03-04 11:22:40 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame^] | 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 |