Initial embedding of Adhoc network creating code into ChronoShare

(gui extension in the next commit)

Change-Id: I9c873dfc0316c03beb75bb395c21b976faddd228
diff --git a/adhoc/adhoc-osx.mm b/adhoc/adhoc-osx.mm
new file mode 100644
index 0000000..107e230
--- /dev/null
+++ b/adhoc/adhoc-osx.mm
@@ -0,0 +1,104 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#include "adhoc.h"
+#include "config.h"
+
+#if (__APPLE__ && HAVE_COREWLAN)
+
+#include "logging.h"
+#include <sstream>
+
+using namespace std;
+
+INIT_LOGGER ("Adhoc.OSX");
+
+#import <CoreWLAN/CoreWLAN.h>
+#import <CoreWLAN/CoreWLANConstants.h>
+#import <CoreWLAN/CWInterface.h>
+#import <CoreWLAN/CoreWLANTypes.h>
+
+bool
+Adhoc::CreateAdhoc ()
+{
+  NSString *networkName = @"NDNdirect";
+  NSNumber *channel = @11;
+  NSString *passphrase = @"NDNhello";
+  NSString *securityMode = @"Open";
+
+  NSArray *airportInterfaces = [CWInterface supportedInterfaces];
+
+  //Choose the desired interface . the first one will be enought for this example
+  NSString *interfaceName =[airportInterfaces objectAtIndex:0];
+
+  CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
+
+  NSString * _priorNetwork = airport.ssid;
+  _LOG_DEBUG ("Prior network: " << [_priorNetwork cStringUsingEncoding:NSASCIIStringEncoding]);
+
+  _LOG_DEBUG ("Starting adhoc connection");
+
+  NSError *error = nil;
+  NSData* data = [networkName dataUsingEncoding:NSUTF8StringEncoding];
+  BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:(NSUInteger)@11 password:passphrase error:&error];
+
+  if (!created)
+    {
+      return false;
+    }
+
+  _LOG_DEBUG ("Creating face for the adhoc connection");
+
+  // should do a better job later, when Ccnx::Control will be implemented
+
+  ostringstream cmd;
+  cmd << CCNX_PATH << "/bin/ccndc add / udp 169.254.255.255";
+  int ret = system (cmd.str ().c_str ());
+  if (ret == 0)
+    {
+      return true;
+    }
+  else
+    {
+      DestroyAdhoc ();
+      return false;
+    }
+}
+
+void
+Adhoc::DestroyAdhoc ()
+{
+  NSString *networkName = @"NDNdirect";
+  NSNumber *channel = @11;
+  NSString *passphrase = @"NDNhello";
+  NSString *securityMode = @"Open";
+
+  NSArray *airportInterfaces = [CWInterface supportedInterfaces];
+
+  //Choose the desired interface . the first one will be enought for this example
+  NSString *interfaceName =[airportInterfaces objectAtIndex:0];
+
+  CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
+
+  [airport disassociate];
+}
+
+#endif // ADHOC_SUPPORTED
diff --git a/adhoc/adhoc.h b/adhoc/adhoc.h
new file mode 100644
index 0000000..6181ecb
--- /dev/null
+++ b/adhoc/adhoc.h
@@ -0,0 +1,45 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#ifndef CHRONOSHARE_ADHOC_H
+#define CHRONOSHARE_ADHOC_H
+
+#include "config.h"
+
+#if (__APPLE__ && HAVE_COREWLAN)
+    #define ADHOC_SUPPORTED 1
+#endif
+
+#ifdef ADHOC_SUPPORTED
+
+class Adhoc
+{
+public:
+  static bool
+  CreateAdhoc ();
+
+  static void
+  DestroyAdhoc ();
+};
+
+#endif
+
+#endif // CHRONOSHARE_ADHOC_H
diff --git a/waf b/waf
index a6859ac..4ae5a18 100755
--- a/waf
+++ b/waf
Binary files differ
diff --git a/wscript b/wscript
index 084d764..8a0bede 100644
--- a/wscript
+++ b/wscript
@@ -3,7 +3,7 @@
 VERSION='0.1'
 APPNAME='chronoshare'
 
-from waflib import Build, Logs
+from waflib import Build, Logs, Utils
 
 def options(opt):
     opt.add_option('--debug',action='store_true',default=False,dest='debug',help='''debugging mode''')
@@ -11,10 +11,10 @@
     opt.add_option('--yes',action='store_true',default=False) # for autoconf/automake/make compatibility
     opt.add_option('--log4cxx', action='store_true',default=False,dest='log4cxx',help='''Compile with log4cxx logging support''')
 
-    opt.load('compiler_cxx boost ccnx protoc qt4')
+    opt.load('compiler_c compiler_cxx boost ccnx protoc qt4')
 
 def configure(conf):
-    conf.load("compiler_cxx")
+    conf.load("compiler_c compiler_cxx")
 
     conf.define ("CHRONOSHARE_VERSION", VERSION)
 
@@ -22,6 +22,10 @@
     conf.check_cfg(package='libevent', args=['--cflags', '--libs'], uselib_store='LIBEVENT', mandatory=True)
     conf.check_cfg(package='libevent_pthreads', args=['--cflags', '--libs'], uselib_store='LIBEVENT_PTHREADS', mandatory=True)
 
+    if Utils.unversioned_sys_platform () == "darwin":
+        conf.check_cxx(framework_name='Foundation', uselib_store='OSX_FOUNDATION', cxxflags="-ObjC++", mandatory=False)
+        conf.check_cxx(framework_name='CoreWLAN',   uselib_store='OSX_COREWLAN',   cxxflags="-ObjC++", define_name='HAVE_COREWLAN', mandatory=False)
+
     if not conf.check_cfg(package='openssl', args=['--cflags', '--libs'], uselib_store='SSL', mandatory=False):
         libcrypto = conf.check_cc(lib='crypto',
                                   header_name='openssl/crypto.h',
@@ -52,6 +56,7 @@
         return
 
     conf.check_ccnx (path=conf.options.ccnx_dir)
+    conf.define ('CCNX_PATH', conf.env.CCNX_ROOT)
 
     if conf.options.debug:
         conf.define ('_DEBUG', 1)
@@ -93,6 +98,16 @@
         includes = "ccnx src scheduler executor",
         )
 
+    adhoc = bld (
+        target = "adhoc",
+        features=['cxx'],
+        includes = "ccnx src",
+    )
+    if Utils.unversioned_sys_platform () == "darwin":
+        adhoc.mac_app = True
+        adhoc.source = 'adhoc/adhoc-osx.mm'
+        adhoc.use = "OSX_FOUNDATION OSX_COREWLAN"
+
     chornoshare = bld (
         target="chronoshare",
         features=['cxx'],
@@ -122,7 +137,17 @@
           install_prefix = None,
           )
 
-    app_plist = '''<?xml version="1.0" encoding="UTF-8"?>
+    qt = bld (
+        target = "ChronoShare",
+        features = "qt4 cxx cxxprogram",
+        defines = "WAF",
+        source = bld.path.ant_glob(['gui/*.cpp', 'gui/*.cc', 'gui/*.qrc']),
+        includes = "ccnx scheduler executor fs-watcher gui src . ",
+        use = "BOOST BOOST_FILESYSTEM SQLITE3 QTCORE QTGUI LOG4CXX fs_watcher ccnx database chronoshare"
+        )
+
+    if Utils.unversioned_sys_platform () == "darwin":
+        app_plist = '''<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
 <plist version="0.9">
 <dict>
@@ -140,17 +165,11 @@
     <string>1</string>
 </dict>
 </plist>'''
+        qt.mac_app = "ChronoShare.app"
+        qt.mac_plist = app_plist % "ChronoShare"
+        qt.use += " OSX_FOUNDATION OSX_COREWLAN adhoc"
+        # qt.use += " OSX_FOUNDATION OSX_COREWLAN adhoc"
 
-    qt = bld (
-	target = "ChronoShare",
-        mac_app = "ChronoShare.app",
-        mac_plist = app_plist % "ChronoShare",
-	features = "qt4 cxx cxxprogram",
-	defines = "WAF",
-	source = bld.path.ant_glob(['gui/*.cpp', 'gui/*.cc', 'gui/*.qrc']),
-	includes = "ccnx scheduler executor fs-watcher gui src . ",
-	use = "BOOST BOOST_FILESYSTEM SQLITE3 QTCORE QTGUI LOG4CXX fs_watcher ccnx database chronoshare"
-	)
 
     cmdline = bld (
         target = "csd",
@@ -169,10 +188,8 @@
 	use = "BOOST BOOST_FILESYSTEM SQLITE3 QTCORE LOG4CXX fs_watcher ccnx database chronoshare"
         )
 
-
-
-
-
-
-
-
+from waflib import TaskGen
+@TaskGen.extension('.mm')
+def m_hook(self, node):
+    """Alias .mm files to be compiled the same as .cc files, gcc/clang will do the right thing."""
+    return self.create_compiled_task('cxx', node)