From 6ccca3ef9ca79b23a87a3637eb1f787b5f1db304 Mon Sep 17 00:00:00 2001
From: Thorsten von Eicken <tve@rightscale.com>
Date: Sun, 19 Jun 2016 12:09:28 -0700
Subject: [PATCH] Fix auth mode for AP, fixes #145

---
 README.adoc           | 10 ++++++++
 esp-link/cgiwifi.c    | 16 ++++++++-----
 html/wifi/wifiAp.html | 56 +++++++++++++------------------------------
 html/wifi/wifiAp.js   | 12 ----------
 4 files changed, 37 insertions(+), 57 deletions(-)

diff --git a/README.adoc b/README.adoc
index 737979c..ca6a309 100644
--- a/README.adoc
+++ b/README.adoc
@@ -171,6 +171,16 @@ to join its network to configure it. The short version is:
     you reconnect your laptop/phone to your normal network and access esp-link via its hostname
     or IP address
 
+#### Notes on using AP (access point) mode
+
+Esp-link does not support STA+AP mode, however it does support STA mode and AP mode. What happens
+is that STA+AP mode is used at boot and when making STA changes to allow for recovery: the AP
+mode stays on for a while so you can connect to it and fix the STA mode. Once STA has connected,
+esp-link switches to STA-only mode. There is no setting to stay in STA+AP mode. So... if you want
+to use AP ensure you set esp-link to AP-only mode. If you want STA+AP mode you're gonna have to
+modify the source for yourself. (This stuff is painful to test and rather tricky, so don't expect
+the way it works to change.)
+
 ### LED indicators
 
 Assuming appropriate hardware attached to GPIO pins, the green "conn" LED will show the wifi
diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c
index 9dc694a..afb6223 100644
--- a/esp-link/cgiwifi.c
+++ b/esp-link/cgiwifi.c
@@ -551,30 +551,34 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) {
     if (checkString(buff) && len>7 && len<=64) {
         // String preprocessing done in client side, wifiap.js line 31
         os_memcpy(apconf.password, buff, len);
+        os_printf("Setting AP password len=%d\n", len);
     } else if (len != 0) {
         jsonHeader(connData, 400);
         httpdSend(connData, "PASSWORD not valid or out of range", -1);
         return HTTPD_CGI_DONE;
     }
     // Set auth mode
-    if(len != 0){
+    if (len != 0) {
         // Set authentication mode, before password to check open settings
         len=httpdFindArg(connData->getArgs, "ap_authmode", buff, sizeof(buff));
-        if(len>0){
+        if (len > 0) {
             int value = atoi(buff);
-            if(value >= 0  && value <= 4){
+            if (value > 0  && value <= 4) {
                 apconf.authmode = value;
-            }else{
+            } else {
                 // If out of range set by default
+                os_printf("Forcing AP authmode to WPA_WPA2_PSK\n");
                 apconf.authmode = 4;
             }
-        }else{
+        } else {
             // Valid password but wrong auth mode, default 4
+            os_printf("Forcing AP authmode to WPA_WPA2_PSK\n");
             apconf.authmode = 4;
         }
-    }else{
+    } else {
         apconf.authmode = 0;
     }
+    os_printf("Setting AP authmode=%d\n", apconf.authmode);
     // Set max connection number
     len=httpdFindArg(connData->getArgs, "ap_maxconn", buff, sizeof(buff));
     if(len>0){
diff --git a/html/wifi/wifiAp.html b/html/wifi/wifiAp.html
index e93e54d..41b3697 100644
--- a/html/wifi/wifiAp.html
+++ b/html/wifi/wifiAp.html
@@ -31,7 +31,6 @@
             <div id="AP_Settings-spinner" class="spinner spinner-small"></div>
             
             <form action="#" id="AP_Settings-form" class="pure-form" hidden>
-	           <!-- <input type="text" id="conn_check" name="ap_connex" value="0" hidden>-->
               <legend>Soft-AP main settings, use with care!</legend>
               
               <div class="pure-form-stacked">
@@ -46,34 +45,6 @@
                 <div class="popup">Password must be at least 8 chars long!</div>
               </div>
               
-             <div class="pure-form-stacked">
-             <legend>Soft-AP Advanced Settings</legend> 
-             </div>
-             
-            <div class="form-horizontal">
-              <label for="AP_Settings-ron" style="margin-right:1em">
-                <input type="radio" name="ap" value="on" id="AP_Settings-ron"/>
-                Show </label>
-                <label for="AP_Settings-roff" style="margin-right:1em">
-                <input type="radio" name="ap" value="off" id="AP_Settings-roff"/>
-                Hide </label>
-            </div>
-            
-            <div id="AP_Settings-off" class="pure-form-stacked"></div> 
-            <div id="AP_Settings-on" class="pure-form-stacked">             
-              
-              <div class="pure-form-stacked">
-                <label>Soft-AP Max Connections</label>
-                <input type="text" name="ap_maxconn" />
-                <div class="popup">Max 4 ( default 4 )</div>
-              </div>
-              
-              <div class="pure-form-stacked">
-                <label>Soft-AP Beacon Interval</label>
-                <input type="text" name="ap_beacon" />
-                <div class="popup">Between 100 - 60000 ms ( default 100ms )</div>
-              </div>
-              
               <div class="pure-form-stacked">   
                 <label>Soft-AP Auth Mode</label>
                 <select name="ap_authmode" href="#">
@@ -85,14 +56,24 @@
                 </select>
                 <div class="popup">Default WPA_WPA2_PSK</div>
               </div>
-              
-               <div class="form-horizontal">
-                <label><input type="checkbox" name="ap_hidden" />Soft-AP SSID hidden</label>
-                <div class="popup">Check this box to hide you Soft-AP SSID ( default Not Hidden )</div>
+               
+              <div class="pure-form-stacked">
+                <label>Soft-AP Max Connections</label>
+                <input type="text" name="ap_maxconn" />
+                <div class="popup">Max 4 ( default 4 )</div>
+              </div>
+               
+              <div class="pure-form-stacked">
+                <label>Soft-AP Beacon Interval</label>
+                <input type="text" name="ap_beacon" />
+                <div class="popup">Between 100 - 60000 ms ( default 100ms )</div>
+              </div>
+               
+              <div class="form-horizontal">
+               <label><input type="checkbox" name="ap_hidden" />Soft-AP SSID hidden</label>
+               <div class="popup">Check this box to hide you Soft-AP SSID ( default Not Hidden )</div>
               </div>
               
-            </div>
-            
               <button id="AP_Settings-button" type="submit" class="pure-button button-primary">
                 Change Soft-AP settings!
               </button>
@@ -114,10 +95,7 @@ onLoad(function() {
   getWifiInfo();
   // Fetch actual settings
   fetchApSettings();
-  // Hide advanced settings
-  undoApAdvanced();
-  bnd($("#AP_Settings-ron"), "click", doApAdvanced);
-  bnd($("#AP_Settings-roff"), "click", undoApAdvanced); 
+  // Wire-up form
   bnd($("#AP_Settings-form"), "submit", changeApSettings);
 });
 </script>
diff --git a/html/wifi/wifiAp.js b/html/wifi/wifiAp.js
index efa61f8..59dd6ce 100644
--- a/html/wifi/wifiAp.js
+++ b/html/wifi/wifiAp.js
@@ -83,15 +83,3 @@ function fetchApSettings() {
     window.setTimeout(fetchApSettings, 1000);
   });
 }
-
-function doApAdvanced() {
-  $('#AP_Settings-on').removeAttribute('hidden');
-  $("#AP_Settings-off").setAttribute("hidden", "");
-  $("#AP_Settings-roff").removeAttribute("checked");
-}
-
-function undoApAdvanced(){
-  $("#AP_Settings-on").setAttribute("hidden", "");
-  $("#AP_Settings-off").removeAttribute("hidden");
-  $("#AP_Settings-roff").setAttribute("checked", "");
-}