Merge pull request #1005 from mc-hamster/master

Fix for "Creating SSL certificate" screen when there's no display
This commit is contained in:
Jm Casler 2021-12-18 14:49:16 -05:00 committed by GitHub
commit 4a053801ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 52 deletions

View File

@ -40,6 +40,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef NO_ESP32 #ifndef NO_ESP32
#include "mesh/http/WiFiAPClient.h" #include "mesh/http/WiFiAPClient.h"
#include "esp_task_wdt.h"
#endif #endif
using namespace meshtastic; /** @todo remove */ using namespace meshtastic; /** @todo remove */
@ -155,6 +156,11 @@ static void drawSSLScreen(OLEDDisplay *display, OLEDDisplayUiState *state, int16
display->setFont(FONT_SMALL); display->setFont(FONT_SMALL);
display->drawString(64 + x, y, "Creating SSL certificate"); display->drawString(64 + x, y, "Creating SSL certificate");
#ifndef NO_ESP32
yield();
esp_task_wdt_reset();
#endif
display->setFont(FONT_SMALL); display->setFont(FONT_SMALL);
if ((millis() / 1000) % 2) { if ((millis() / 1000) % 2) {
display->drawString(64 + x, FONT_HEIGHT_SMALL + y + 2, "Please wait . . ."); display->drawString(64 + x, FONT_HEIGHT_SMALL + y + 2, "Please wait . . .");
@ -697,6 +703,7 @@ void _screen_header()
Screen::Screen(uint8_t address, int sda, int scl) : OSThread("Screen"), cmdQueue(32), dispdev(address, sda, scl), ui(&dispdev) Screen::Screen(uint8_t address, int sda, int scl) : OSThread("Screen"), cmdQueue(32), dispdev(address, sda, scl), ui(&dispdev)
{ {
address_found = address;
cmdQueue.setReader(this); cmdQueue.setReader(this);
} }
@ -934,10 +941,12 @@ void Screen::drawDebugInfoWiFiTrampoline(OLEDDisplay *display, OLEDDisplayUiStat
* it is expected that this will be used during the boot phase */ * it is expected that this will be used during the boot phase */
void Screen::setSSLFrames() void Screen::setSSLFrames()
{ {
DEBUG_MSG("showing SSL frames\n"); if (address_found) {
static FrameCallback sslFrames[] = {drawSSLScreen}; // DEBUG_MSG("showing SSL frames\n");
ui.setFrames(sslFrames, 1); static FrameCallback sslFrames[] = {drawSSLScreen};
ui.update(); ui.setFrames(sslFrames, 1);
ui.update();
}
} }
// restore our regular frame list // restore our regular frame list

View File

@ -97,6 +97,8 @@ class Screen : public concurrency::OSThread
Screen(const Screen &) = delete; Screen(const Screen &) = delete;
Screen &operator=(const Screen &) = delete; Screen &operator=(const Screen &) = delete;
uint8_t address_found;
/// Initializes the UI, turns on the display, starts showing boot screen. /// Initializes the UI, turns on the display, starts showing boot screen.
// //
// Not thread safe - must be called before any other methods are called. // Not thread safe - must be called before any other methods are called.

View File

@ -6,6 +6,8 @@
#include <HTTPMultipartBodyParser.hpp> #include <HTTPMultipartBodyParser.hpp>
#include <HTTPURLEncodedBodyParser.hpp> #include <HTTPURLEncodedBodyParser.hpp>
#include "sleep.h" #include "sleep.h"
#include "graphics/Screen.h"
#include <WebServer.h> #include <WebServer.h>
#include <WiFi.h> #include <WiFi.h>
@ -79,13 +81,13 @@ static void taskCreateCert(void *parameter)
{ {
prefs.begin("MeshtasticHTTPS", false); prefs.begin("MeshtasticHTTPS", false);
// Delete the saved certs (used in debugging)
#if 0 #if 0
DEBUG_MSG("Deleting any saved SSL keys ...\n"); // Delete the saved certs (used in debugging)
// prefs.clear(); DEBUG_MSG("Deleting any saved SSL keys ...\n");
prefs.remove("PK"); // prefs.clear();
prefs.remove("cert"); prefs.remove("PK");
prefs.remove("cert");
#endif #endif
@ -156,6 +158,7 @@ static void taskCreateCert(void *parameter)
void createSSLCert() void createSSLCert()
{ {
bool runLoop = false;
if (isWifiAvailable() && !isCertReady) { if (isWifiAvailable() && !isCertReady) {
// Create a new process just to handle creating the cert. // Create a new process just to handle creating the cert.
@ -163,21 +166,28 @@ void createSSLCert()
// jm@casler.org (Oct 2020) // jm@casler.org (Oct 2020)
xTaskCreate(taskCreateCert, /* Task function. */ xTaskCreate(taskCreateCert, /* Task function. */
"createCert", /* String with name of task. */ "createCert", /* String with name of task. */
16384, /* Stack size in bytes. */ //16384, /* Stack size in bytes. */
8192, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */ NULL, /* Parameter passed as input of the task */
16, /* Priority of the task. */ 16, /* Priority of the task. */
NULL); /* Task handle. */ NULL); /* Task handle. */
DEBUG_MSG("Waiting for SSL Cert to be generated.\n"); DEBUG_MSG("Waiting for SSL Cert to be generated.\n");
int seconds = 0;
while (!isCertReady) { while (!isCertReady) {
DEBUG_MSG("."); if ((millis() / 500) % 2) {
delay(1000); if (runLoop) {
yield(); DEBUG_MSG(".");
esp_task_wdt_reset();
seconds++; yield();
if ((seconds == 3) && screen) { esp_task_wdt_reset();
screen->setSSLFrames();
if (millis() / 1000 >= 3) {
screen->setSSLFrames();
}
}
runLoop = false;
} else {
runLoop = true;
} }
} }
DEBUG_MSG("SSL Cert Ready!\n"); DEBUG_MSG("SSL Cert Ready!\n");
@ -201,40 +211,6 @@ void initWebServer()
{ {
DEBUG_MSG("Initializing Web Server ...\n"); DEBUG_MSG("Initializing Web Server ...\n");
#if 0
// this seems to be a copypaste dup of taskCreateCert
prefs.begin("MeshtasticHTTPS", false);
size_t pkLen = prefs.getBytesLength("PK");
size_t certLen = prefs.getBytesLength("cert");
DEBUG_MSG("Checking if we have a previously saved SSL Certificate.\n");
if (pkLen && certLen) {
uint8_t *pkBuffer = new uint8_t[pkLen];
prefs.getBytes("PK", pkBuffer, pkLen);
uint8_t *certBuffer = new uint8_t[certLen];
prefs.getBytes("cert", certBuffer, certLen);
cert = new SSLCert(certBuffer, certLen, pkBuffer, pkLen);
DEBUG_MSG("Retrieved Private Key: %d Bytes\n", cert->getPKLength());
// DEBUG_MSG("Retrieved Private Key: " + String(cert->getPKLength()) + " Bytes");
// for (int i = 0; i < cert->getPKLength(); i++)
// Serial.print(cert->getPKData()[i], HEX);
// Serial.println();
DEBUG_MSG("Retrieved Certificate: %d Bytes\n", cert->getCertLength());
// for (int i = 0; i < cert->getCertLength(); i++)
// Serial.print(cert->getCertData()[i], HEX);
// Serial.println();
} else {
DEBUG_MSG("Web Server started without SSL keys! How did this happen?\n");
}
#endif
// We can now use the new certificate to setup our server as usual. // We can now use the new certificate to setup our server as usual.
secureServer = new HTTPSServer(cert); secureServer = new HTTPSServer(cert);
insecureServer = new HTTPServer(); insecureServer = new HTTPServer();