adhoc: Attempting to remember and restore previous connection, toggle wifi device on/off as a fallback

Change-Id: Iddbef8c496216519802264428ccae2a25aefc6a4
diff --git a/adhoc/adhoc-osx.mm b/adhoc/adhoc-osx.mm
index 9132ad9..b58bdd4 100644
--- a/adhoc/adhoc-osx.mm
+++ b/adhoc/adhoc-osx.mm
@@ -36,7 +36,8 @@
 #import <CoreWLAN/CWInterface.h>
 #import <CoreWLAN/CoreWLANTypes.h>
 
-const NSUInteger channel = 11;
+const NSUInteger g_channel = 11;
+static NSString * g_priorNetwork = 0;
 
 bool
 Adhoc::CreateAdhoc ()
@@ -52,14 +53,14 @@
 
   CWInterface *airport = [CWInterface interfaceWithName:interfaceName];
 
-  NSString * _priorNetwork = airport.ssid;
+  g_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:channel password:passphrase error:&error];
+  BOOL created = [airport startIBSSModeWithSSID:data security:kCWIBSSModeSecurityNone channel:g_channel password:passphrase error:&error];
 
   if (!created)
     {
@@ -97,6 +98,24 @@
   [airport disassociate];
 
   NSError *err;
+
+  if (g_priorNetwork != 0)
+    {
+      NSSet *scanResults = [airport scanForNetworksWithName:g_priorNetwork error:&err];
+
+      if([scanResults count] > 0)
+        {
+          CWNetwork *previousNetwork = [[scanResults allObjects] objectAtIndex:0];
+
+          [airport associateToNetwork:previousNetwork password:nil error:&err];
+
+          g_priorNetwork = 0;
+          return;
+        }
+
+      g_priorNetwork = 0;
+    }
+
   [airport setPower:NO error:&err];
   [airport setPower:YES error:&err];