From 30195eb79d54f99e0f7b0743522330a221661c4c Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Mon, 3 Apr 2017 11:56:33 +0800
Subject: [PATCH] esp32: dont require factory button if no ssl certs yet

---
 lib/libwebsockets.h               |  2 +-
 lib/lws-plat-esp32.c              | 24 ++++++++++++++++++++++++
 plugins/protocol_esp32_lws_scan.c | 11 ++++++++++-
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index 2a9fb3a4d..8be0054a8 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -537,7 +537,6 @@ static inline void uv_close(uv_handle_t *h, void *v)
 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
 #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
 
-static inline uint32_t lws_esp32_get_reboot_type(void) { uint32_t *p = (uint32_t *)LWS_MAGIC_REBOOT_TYPE_ADS; return *p; }
 
 /* user code provides these */
 
@@ -592,6 +591,7 @@ extern const esp_partition_t *
 lws_esp_ota_get_boot_partition(void);
 extern int
 lws_esp32_get_image_info(const esp_partition_t *part, struct lws_esp32_image *i, char *json, int json_len);
+extern uint32_t lws_esp32_get_reboot_type(void);
 #else
 typedef int lws_sockfd_type;
 typedef int lws_filefd_type;
diff --git a/lib/lws-plat-esp32.c b/lib/lws-plat-esp32.c
index c8b83f3ac..9261f8c9f 100644
--- a/lib/lws-plat-esp32.c
+++ b/lib/lws-plat-esp32.c
@@ -585,6 +585,30 @@ struct esp32_file {
 	const struct inode *i;
 };
 
+uint32_t lws_esp32_get_reboot_type(void)
+{
+	uint32_t *p = (uint32_t *)LWS_MAGIC_REBOOT_TYPE_ADS, val = *p;
+	nvs_handle nvh;
+	size_t s = 0;
+	int n = 0;
+
+	ESP_ERROR_CHECK(nvs_open("lws-station", NVS_READWRITE, &nvh));
+	if (nvs_get_blob(nvh, "ssl-pub.der", NULL, &s) == ESP_OK)
+		n = 1;
+	if (nvs_get_blob(nvh, "ssl-pri.der", NULL, &s) == ESP_OK)
+		n |= 2;
+	nvs_close(nvh);
+
+	/*
+	 * in the case the SSL certs are not there, don't require
+	 * the button to be down to access all features.
+	 */
+	if (n != 3)
+		val = LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON;
+
+	return val;
+}
+
 static void render_ip(char *dest, int len, uint8_t *ip)
 {
 	snprintf(dest, len, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
diff --git a/plugins/protocol_esp32_lws_scan.c b/plugins/protocol_esp32_lws_scan.c
index 1ef407b86..d463040db 100644
--- a/plugins/protocol_esp32_lws_scan.c
+++ b/plugins/protocol_esp32_lws_scan.c
@@ -363,6 +363,7 @@ callback_esplws_scan(struct lws *wsi, enum lws_callback_reasons reason,
 			uint8_t mac[6];
 			struct lws_esp32_image i;
 			char img_factory[512], img_ota[512];
+			int grt;
 
 		case SCAN_STATE_INITIAL:
 
@@ -378,6 +379,14 @@ callback_esplws_scan(struct lws *wsi, enum lws_callback_reasons reason,
 
 			nvs_close(nvh);
 
+			/*
+			 * this value in the JSON is just
+			 * used for UI indication.  Each conditional feature confirms
+			 * it itself before it allows itself to be used.
+			 */
+
+			grt = lws_esp32_get_reboot_type();
+
 			esp_efuse_read_mac(mac);
 			strcpy(img_factory, " { \"date\": \"Empty\" }");
 			strcpy(img_ota, " { \"date\": \"Empty\" }");
@@ -406,7 +415,7 @@ callback_esplws_scan(struct lws *wsi, enum lws_callback_reasons reason,
 				      " \"img_factory\": %s,\n"
 				      " \"img_ota\": %s,\n",
 				      lws_esp32.model,
-				      lws_esp32_get_reboot_type() == LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON, 
+				      grt == LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON, 
 				      lws_esp32.serial,
 				      lws_esp32.opts,
 				      lws_esp32.model, lws_esp32.serial,