Skip to content

Commit b43b1ca

Browse files
authored
v1.9
1 parent 3f7a9c4 commit b43b1ca

File tree

6 files changed

+52
-40
lines changed

6 files changed

+52
-40
lines changed

appGlobals.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
#define DEBUG_MEM false // leave as false
2323
#define FLUSH_DELAY 0 // for debugging crashes
2424
#define DBG_ON false // esp debug output
25+
#define DBG_LVL ESP_LOG_ERROR // level if DBG_ON true: ESP_LOG_ERROR, ESP_LOG_WARN, ESP_LOG_INFO, ESP_LOG_DEBUG, ESP_LOG_VERBOSE
2526
#define DOT_MAX 50
2627
#define HOSTNAME_GRP 0
2728
#define USE_IP6 false
2829

2930
#define APP_NAME "VoiceChanger" // max 15 chars
30-
#define APP_VER "1.8"
31+
#define APP_VER "1.9"
3132

3233
#define HTTP_CLIENTS 2 // http, ws
3334
#define MAX_STREAMS 0
@@ -55,7 +56,7 @@
5556
#define ISVC // VC specific code in generics
5657

5758
// to determine if newer data files need to be loaded
58-
#define CFG_VER 5
59+
#define CFG_VER 6
5960

6061
#ifdef CONFIG_IDF_TARGET_ESP32S3
6162
#define SERVER_STACK_SIZE (1024 * 8)

globals.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ void initStatus(int cfgGroup, int delayVal);
151151
void killSocket(int skt = -99);
152152
void listBuff(const uint8_t* b, size_t len);
153153
bool listDir(const char* fname, char* jsonBuff, size_t jsonBuffLen, const char* extension);
154+
void loadCerts();
154155
bool loadConfig();
155156
void logLine();
156157
void logPrint(const char *fmtStr, ...);
@@ -254,6 +255,7 @@ extern bool wsLog;
254255
extern uint16_t sustainId;
255256
extern bool heartBeatDone;
256257
extern TaskHandle_t heartBeatHandle;
258+
extern char portFwd[];
257259

258260
// remote file server
259261
extern char fsServer[];
@@ -300,8 +302,7 @@ extern const char* smtp_rootCACertificate;
300302
extern const char* mqtt_rootCACertificate;
301303
extern const char* telegram_rootCACertificate;
302304
extern const char* hfs_rootCACertificate;
303-
extern const char* prvtkey_pem; // app https server private key
304-
extern const char* cacert_pem; // app https server public certificate
305+
extern char* serverCerts[];
305306

306307
// app status
307308
extern char timezone[];

prefs.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,10 @@ void updateStatus(const char* variable, const char* _value, bool fromUser) {
378378
} else {
379379
res = updateAppStatus(variable, value, fromUser);
380380
if (!res) {
381-
if (fromUser) LOG_WRN("Unable to config %s as required cpp file not included", variable);
381+
if (fromUser) {
382+
updateConfigVect(variable, value); // in case value previously set in different compilation state
383+
LOG_WRN("Unable to use %s as required cpp file not included", variable);
384+
}
382385
else LOG_VRB("Unrecognised config: %s", variable);
383386
}
384387
}
@@ -536,6 +539,14 @@ bool loadConfig() {
536539
loadPrefs(); // overwrites any corresponding entries in config
537540
// load variables from stored config vector
538541
reloadConfigs();
542+
#if INCLUDE_CERTS
543+
loadCerts();
544+
#else
545+
if (useHttps) {
546+
LOG_WRN("Need to compile with INCLUDE_CERTS true to use HTTPS");
547+
useHttps = false;
548+
}
549+
#endif
539550
debugMemory("loadConfig");
540551
return true;
541552
}

utils.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static void initBrownout(void);
2626
int wakePin; // if wakeUse is true
2727
bool wakeUse = false; // true to allow app to sleep and wake
2828
char* jsonBuff = NULL;
29+
char portFwd[6] = "";
2930

3031
/************************** Wifi **************************/
3132

@@ -72,11 +73,10 @@ static void setupMdnsHost() {
7273
snprintf(mdnsName, MAX_IP_LEN, "%.*s", MAX_IP_LEN - 1, hostName);
7374
if (MDNS.begin(mdnsName)) {
7475
// Add service to MDNS
75-
MDNS.addService("http", "tcp", HTTP_PORT);
76-
MDNS.addService("https", "tcp", HTTPS_PORT);
77-
//MDNS.addService("ws", "udp", 83);
78-
//MDNS.addService("ftp", "tcp", 21);
79-
LOG_INF("mDNS service: http://%s.local", mdnsName);
76+
useHttps ? MDNS.addService("https", "tcp", HTTPS_PORT) : MDNS.addService("http", "tcp", HTTP_PORT);
77+
MDNS.addService("ws", "udp", 83);
78+
MDNS.addService("ftp", "tcp", 21);
79+
LOG_INF("mDNS service: http%s://%s.local", useHttps ? "s" : "", mdnsName);
8080
} else LOG_WRN("mDNS host: %s Failed", mdnsName);
8181
debugMemory("setupMdnsHost");
8282
}
@@ -118,7 +118,7 @@ static void onWiFiEvent(WiFiEvent_t event) {
118118
case ARDUINO_EVENT_WIFI_STA_STOP: LOG_INF("Wifi Station stopped %s", ST_SSID); break;
119119
case ARDUINO_EVENT_WIFI_AP_START: {
120120
if (strlen(AP_SSID) && !strcmp(WiFi.softAPSSID().c_str(), AP_SSID)) {
121-
LOG_INF("Wifi AP SSID: %s started, use '%s://%s' to connect", WiFi.softAPSSID().c_str(), useHttps ? "https" : "http", WiFi.softAPIP().toString().c_str());
121+
LOG_INF("Wifi AP SSID: %s started, use 'http%s://%s' to connect", WiFi.softAPSSID().c_str(), useHttps ? "s" : "", WiFi.softAPIP().toString().c_str());
122122
APstarted = true;
123123
}
124124
break;
@@ -866,13 +866,23 @@ void logLine() {
866866
logPrint(" \n");
867867
}
868868

869+
int vprintfRedirect(const char* format, va_list args) {
870+
// format esp_log() output for logPrint()
871+
char buffer[256];
872+
int len = vsnprintf(buffer, sizeof(buffer), format, args);
873+
logPrint("%s", buffer);
874+
return len;
875+
}
876+
869877
void logSetup() {
870878
// prep logging environment
871879
Serial.begin(115200);
872880
Serial.setDebugOutput(DBG_ON);
873881
printf("\n\n");
874882
if (DEBUG_MEM) printf("init > Free: heap %lu\n", ESP.getFreeHeap());
875-
if (!DBG_ON) esp_log_level_set("*", ESP_LOG_NONE); // suppress ESP_LOG_ERROR messages
883+
if (DBG_ON) esp_log_level_set("*", DBG_LVL);
884+
else esp_log_level_set("*", ESP_LOG_NONE); // suppress esp log messages
885+
esp_log_set_vprintf(vprintfRedirect); // redirect esp_log output to app log
876886
if (crashLoop == MAGIC_NUM) snprintf(startupFailure, SF_LEN, STARTUP_FAIL "Crash loop detected, check log %s", (brownoutStatus == 'B' || brownoutStatus == 'R') ? "(brownout)" : " ");
877887
crashLoop = MAGIC_NUM;
878888
logSemaphore = xSemaphoreCreateBinary(); // flag that log message formatted
@@ -903,6 +913,7 @@ void formatHex(const char* inData, size_t inLen) {
903913

904914
const char* espErrMsg(esp_err_t errCode) {
905915
// convert esp error code to text
916+
// https://github.com/espressif/esp-idf/blob/master/components/esp_common/include/esp_err.h
906917
static char errText[100];
907918
esp_err_to_name_r(errCode, errText, 100);
908919
return errText;

utilsFS.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ static bool prepSD_MMC() {
7878
if (res) {
7979
fp.mkdir(DATA_DIR);
8080
infoSD();
81-
res = true;
82-
} else {
83-
LOG_WRN("SD card mount failed");
84-
res = false;
85-
}
81+
} else LOG_WRN("SD card mount failed");
8682
#endif
8783
return res;
8884
}

webServer.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static char value[FILE_NAME_LEN];
1212
static char retainAction[2];
1313
int refreshVal = 5000; // msecs
1414

15-
static httpd_handle_t httpServer = NULL; // web server port
15+
static httpd_handle_t httpServer = NULL; // web server port
1616
static int fdWs = -1; // websocket sockfd
1717
bool useHttps = false;
1818
bool useSecure = false;
@@ -501,74 +501,66 @@ static esp_err_t customOrNotFoundHandler(httpd_req_t *req, httpd_err_code_t err)
501501

502502
void startWebServer() {
503503
esp_err_t res = ESP_FAIL;
504-
chunk = psramFound() ? (byte*)ps_malloc(CHUNKSIZE) : (byte*)malloc(CHUNKSIZE);
504+
chunk = psramFound() ? (byte*)ps_malloc(CHUNKSIZE) : (byte*)malloc(CHUNKSIZE);
505505
#if INCLUDE_CERTS
506-
size_t prvtkey_len = strlen(prvtkey_pem);
507-
size_t cacert_len = strlen(cacert_pem);
508-
if (useHttps && (!cacert_len || !prvtkey_len)) {
509-
useHttps = false;
510-
LOG_ALT("HTTPS not available as server keys not defined, using HTTP");
511-
}
512506
if (useHttps) {
513507
// HTTPS server
514508
httpd_ssl_config_t config = HTTPD_SSL_CONFIG_DEFAULT();
515509
#if CONFIG_IDF_TARGET_ESP32S3
516510
config.httpd.stack_size = SERVER_STACK_SIZE;
517511
#endif
518-
config.cacert_pem = (const uint8_t*)cacert_pem;
519-
config.cacert_len = cacert_len + 1;
520-
config.prvtkey_pem = (const uint8_t*)prvtkey_pem;
521-
config.prvtkey_len = prvtkey_len + 1;
512+
config.prvtkey_pem = (const uint8_t*)serverCerts[0];
513+
config.prvtkey_len = strlen(serverCerts[0]) + 1;
514+
config.servercert = (const uint8_t*)serverCerts[1];
515+
config.servercert_len = strlen(serverCerts[1]) + 1;
516+
522517
//config.user_cb = https_server_user_callback;
523518
config.httpd.server_port = HTTPS_PORT;
524-
config.httpd.ctrl_port = HTTPS_PORT;
525519
config.httpd.lru_purge_enable = true; // close least used socket
526520
config.httpd.max_uri_handlers = MAX_HANDLERS;
527521
config.httpd.max_open_sockets = HTTP_CLIENTS + MAX_STREAMS;
528522
config.httpd.task_priority = HTTP_PRI;
529523
//config.httpd.uri_match_fn = httpd_uri_match_wildcard;
530524
res = httpd_ssl_start(&httpServer, &config);
531525
} else {
526+
#else
527+
if (!useHttps) {
532528
#endif
533529
// HTTP server
534530
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
535531
#if CONFIG_IDF_TARGET_ESP32S3
536532
config.stack_size = SERVER_STACK_SIZE;
537-
#endif
533+
#endif
538534
config.server_port = HTTP_PORT;
539-
config.ctrl_port = HTTP_PORT;
540-
config.lru_purge_enable = true;
535+
config.lru_purge_enable = true;
541536
config.max_uri_handlers = MAX_HANDLERS;
542537
config.max_open_sockets = HTTP_CLIENTS + MAX_STREAMS;
543538
config.task_priority = HTTP_PRI;
544539
//config.uri_match_fn = httpd_uri_match_wildcard;
545540
res = httpd_start(&httpServer, &config);
546-
#if INCLUDE_CERTS
547541
}
548-
#endif
549-
550542
httpd_uri_t indexUri = {.uri = "/", .method = HTTP_GET, .handler = indexHandler, .user_ctx = NULL};
551543
httpd_uri_t webUri = {.uri = "/web", .method = HTTP_GET, .handler = webHandler, .user_ctx = NULL};
552544
httpd_uri_t controlUri = {.uri = "/control", .method = HTTP_GET, .handler = controlHandler, .user_ctx = NULL};
553545
httpd_uri_t updateUri = {.uri = "/update", .method = HTTP_POST, .handler = updateHandler, .user_ctx = NULL};
554546
httpd_uri_t statusUri = {.uri = "/status", .method = HTTP_GET, .handler = statusHandler, .user_ctx = NULL};
555-
httpd_uri_t wsUri = {.uri = "/ws", .method = HTTP_GET, .handler = wsHandler, .user_ctx = NULL, .is_websocket = true};
556547
httpd_uri_t uploadUri = {.uri = "/upload", .method = HTTP_POST, .handler = uploadHandler, .user_ctx = NULL};
548+
httpd_uri_t wifiUri = {.uri = "/wifi", .method = HTTP_GET, .handler = setupHandler, .user_ctx = NULL};
549+
httpd_uri_t wsUri = {.uri = "/ws", .method = HTTP_GET, .handler = wsHandler, .user_ctx = NULL, .is_websocket = true};
557550
httpd_uri_t sustainUri = {.uri = "/sustain", .method = HTTP_GET, .handler = appSpecificSustainHandler, .user_ctx = NULL};
558551
httpd_uri_t checkUri = {.uri = "/sustain", .method = HTTP_HEAD, .handler = appSpecificSustainHandler, .user_ctx = NULL};
559-
httpd_uri_t wifiUri = {.uri = "/wifi", .method = HTTP_GET, .handler = setupHandler, .user_ctx = NULL};
560552

561553
if (res == ESP_OK) {
562554
httpd_register_uri_handler(httpServer, &indexUri);
563555
httpd_register_uri_handler(httpServer, &webUri);
564556
httpd_register_uri_handler(httpServer, &controlUri);
565557
httpd_register_uri_handler(httpServer, &updateUri);
566558
httpd_register_uri_handler(httpServer, &statusUri);
567-
httpd_register_uri_handler(httpServer, &wsUri);
568559
httpd_register_uri_handler(httpServer, &uploadUri);
560+
httpd_register_uri_handler(httpServer, &wifiUri);
561+
httpd_register_uri_handler(httpServer, &wsUri);
569562
httpd_register_uri_handler(httpServer, &sustainUri);
570563
httpd_register_uri_handler(httpServer, &checkUri);
571-
httpd_register_uri_handler(httpServer, &wifiUri);
572564
httpd_register_err_handler(httpServer, HTTPD_404_NOT_FOUND, customOrNotFoundHandler);
573565

574566
LOG_INF("Starting web server on port: %u", useHttps ? HTTPS_PORT : HTTP_PORT);

0 commit comments

Comments
 (0)