Qi Zhao | 3ec3331 | 2017-02-12 02:51:05 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017, Regents of the University of California, |
| 4 | * |
| 5 | * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD |
| 6 | * authors and contributors. |
| 7 | * |
| 8 | * NFD Control Center is free software: you can redistribute it and/or modify it under the |
| 9 | * terms of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 14 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with NFD |
| 17 | * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "osx-adhoc.hpp" |
| 21 | |
| 22 | #include <ndn-cxx/name.hpp> |
| 23 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
| 24 | |
| 25 | #ifdef OSX_BUILD |
| 26 | |
| 27 | #import <CoreWLAN/CoreWLAN.h> |
| 28 | #import <CoreWLAN/CoreWLANConstants.h> |
| 29 | #import <CoreWLAN/CWInterface.h> |
| 30 | #import <CoreWLAN/CWWiFiClient.h> |
| 31 | #import <CoreWLAN/CoreWLANTypes.h> |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace ncc { |
| 35 | |
| 36 | const std::string adhocUri = "udp4://224.0.23.170:56363"; |
| 37 | const std::set<Name> adhocPrefixes = {"/", "/adhoc"}; |
| 38 | const NSUInteger g_channel = 11; |
| 39 | const uint16_t ROUTE_ORIGIN_NCC = 67; |
| 40 | |
| 41 | Adhoc::Adhoc(Face& face, KeyChain& keychain) |
| 42 | : m_face(face) |
| 43 | , m_keyChain(keychain) |
| 44 | , m_controller(new nfd::Controller(m_face, m_keyChain)) |
| 45 | , m_faceMonitor(new nfd::FaceMonitor(m_face)) |
| 46 | { |
| 47 | m_faceMonitor->onNotification.connect(bind(&Adhoc::onNotification, this, _1)); |
| 48 | } |
| 49 | |
| 50 | Adhoc::~Adhoc() |
| 51 | { |
| 52 | delete m_faceMonitor; |
| 53 | delete m_controller; |
| 54 | } |
| 55 | |
| 56 | bool |
| 57 | Adhoc::createAdhoc() |
| 58 | { |
| 59 | m_faceMonitor->start(); |
| 60 | |
| 61 | NSString* networkName = |
| 62 | [[NSString alloc] initWithCString:"NDNdirect" encoding:NSASCIIStringEncoding]; |
| 63 | NSString* passphrase = |
| 64 | [[NSString alloc] initWithCString:"NDNhello" encoding:NSASCIIStringEncoding]; |
| 65 | NSArray* airportInterfaces = [CWWiFiClient interfaceNames]; |
| 66 | NSString* interfaceName = [airportInterfaces objectAtIndex:0]; |
| 67 | CWWiFiClient* wifiInterfaces = [[CWWiFiClient alloc] init]; |
| 68 | CWInterface* airport = [wifiInterfaces interfaceWithName:interfaceName]; |
| 69 | |
| 70 | NSError* error = nil; |
| 71 | NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding]; |
| 72 | BOOL created = [airport startIBSSModeWithSSID:data |
| 73 | security:kCWIBSSModeSecurityNone |
| 74 | channel:g_channel |
| 75 | password:passphrase |
| 76 | error:&error]; |
| 77 | |
| 78 | if (!created) { |
| 79 | return false; |
| 80 | } |
| 81 | else { |
| 82 | unregisterRoutes(ndn::nfd::ROUTE_ORIGIN_AUTOCONF); |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | Adhoc::destroyAdhoc() |
| 89 | { |
| 90 | unregisterRoutes(ROUTE_ORIGIN_NCC); |
| 91 | m_faceMonitor->stop(); |
| 92 | |
| 93 | NSArray* airportInterfaces = [CWWiFiClient interfaceNames]; |
| 94 | NSString* interfaceName = [airportInterfaces objectAtIndex:0]; |
| 95 | CWWiFiClient* wifiInterfaces = [[CWWiFiClient alloc] init]; |
| 96 | CWInterface* airport = [wifiInterfaces interfaceWithName:interfaceName]; |
| 97 | [airport disassociate]; |
| 98 | |
| 99 | NSError* err = nil; |
| 100 | |
| 101 | [airport setPower:NO error:&err]; |
| 102 | [NSThread sleepForTimeInterval: 3.0]; |
| 103 | [airport setPower:YES error:&err]; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | Adhoc::onNotification(const ndn::nfd::FaceEventNotification& notification) |
| 108 | { |
| 109 | if (notification.getKind() == ndn::nfd::FACE_EVENT_CREATED && |
| 110 | notification.getRemoteUri() == adhocUri && |
| 111 | notification.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) { |
| 112 | for (const auto& prefix : adhocPrefixes) { |
| 113 | ndn::nfd::ControlParameters params; |
| 114 | params |
| 115 | .setName(prefix) |
| 116 | .setFaceId(notification.getFaceId()) |
| 117 | .setOrigin(ROUTE_ORIGIN_NCC); |
| 118 | m_controller->start<ndn::nfd::RibRegisterCommand>(params, nullptr, nullptr); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | Adhoc::unregisterRoutes(uint16_t origin) |
| 125 | { |
| 126 | m_controller->fetch<ndn::nfd::RibDataset>([this, origin] (const std::vector<nfd::RibEntry>& ribs) { |
| 127 | for (const auto& rib : ribs) { |
| 128 | for (const auto& route : rib.getRoutes()) { |
| 129 | if (route.getOrigin() == origin) { |
| 130 | ndn::nfd::ControlParameters params; |
| 131 | params.setFaceId(route.getFaceId()); |
| 132 | m_controller->start<ndn::nfd::FaceDestroyCommand>(params, nullptr, nullptr); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | }, nullptr); |
| 137 | } |
| 138 | |
| 139 | } // namespace ncc |
| 140 | } // namespace ndn |
| 141 | |
| 142 | #endif // OSX_BUILD |