use PSRAM in AsyncJsonResponse

This commit is contained in:
proddy
2026-05-17 16:19:54 +02:00
parent 144d6589ef
commit 90894898c2
18 changed files with 179 additions and 21 deletions

View File

@@ -1336,4 +1336,8 @@ handshaked
startm startm
netifs netifs
testemail testemail
sendmail sendmail
serialises
SPIRAM
optimisations
IILE

View File

@@ -10,7 +10,7 @@ APStatus::APStatus(AsyncWebServer * server, SecurityManager * securityManager, A
} }
void APStatus::apStatus(AsyncWebServerRequest * request) { void APStatus::apStatus(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
root["status"] = _apSettingsService->getAPNetworkStatus(); root["status"] = _apSettingsService->getAPNetworkStatus();

View File

@@ -1,5 +1,7 @@
#include "AuthenticationService.h" #include "AuthenticationService.h"
#include "../core/psram_async_json_response.h"
AuthenticationService::AuthenticationService(AsyncWebServer * server, SecurityManager * securityManager) AuthenticationService::AuthenticationService(AsyncWebServer * server, SecurityManager * securityManager)
: _securityManager(securityManager) { : _securityManager(securityManager) {
// none of these need authentication // none of these need authentication
@@ -23,7 +25,7 @@ void AuthenticationService::signIn(AsyncWebServerRequest * request, JsonVariant
Authentication authentication = _securityManager->authenticate(username, password); Authentication authentication = _securityManager->authenticate(username, password);
if (authentication.authenticated) { if (authentication.authenticated) {
User * user = authentication.user; User * user = authentication.user;
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject jsonObject = response->getRoot(); JsonObject jsonObject = response->getRoot();
jsonObject["access_token"] = _securityManager->generateJWT(user); jsonObject["access_token"] = _securityManager->generateJWT(user);
response->setLength(); response->setLength();

View File

@@ -7,6 +7,7 @@
#include "SecurityManager.h" #include "SecurityManager.h"
#include "StatefulService.h" #include "StatefulService.h"
#include "../core/psram_async_json_response.h"
#define HTTP_ENDPOINT_ORIGIN_ID "http" #define HTTP_ENDPOINT_ORIGIN_ID "http"
@@ -58,7 +59,7 @@ class HttpEndpoint {
} }
} }
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject jsonObject = response->getRoot().to<JsonObject>(); JsonObject jsonObject = response->getRoot().to<JsonObject>();
_statefulService->read(jsonObject, _stateReader); _statefulService->read(jsonObject, _stateReader);
response->setLength(); response->setLength();

View File

@@ -10,7 +10,7 @@ MqttStatus::MqttStatus(AsyncWebServer * server, MqttSettingsService * mqttSettin
} }
void MqttStatus::mqttStatus(AsyncWebServerRequest * request) { void MqttStatus::mqttStatus(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
root["enabled"] = _mqttSettingsService->isEnabled(); root["enabled"] = _mqttSettingsService->isEnabled();

View File

@@ -30,7 +30,7 @@ String toLocalTimeString(tm * time) {
} }
void NTPStatus::ntpStatus(AsyncWebServerRequest * request) { void NTPStatus::ntpStatus(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
// grab the current instant in unix seconds // grab the current instant in unix seconds

View File

@@ -13,7 +13,7 @@ NetworkStatus::NetworkStatus(AsyncWebServer * server, SecurityManager * security
} }
void NetworkStatus::networkStatus(AsyncWebServerRequest * request) { void NetworkStatus::networkStatus(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
wl_status_t wifi_status = WiFi.status(); wl_status_t wifi_status = WiFi.status();

View File

@@ -1,5 +1,7 @@
#include "SecuritySettingsService.h" #include "SecuritySettingsService.h"
#include "../core/psram_async_json_response.h"
SecuritySettingsService::SecuritySettingsService(AsyncWebServer * server, FS * fs) SecuritySettingsService::SecuritySettingsService(AsyncWebServer * server, FS * fs)
: _httpEndpoint(SecuritySettings::read, SecuritySettings::update, this, server, SECURITY_SETTINGS_PATH, this) : _httpEndpoint(SecuritySettings::read, SecuritySettings::update, this, server, SECURITY_SETTINGS_PATH, this)
, _fsPersistence(SecuritySettings::read, SecuritySettings::update, this, fs, SECURITY_SETTINGS_FILE) , _fsPersistence(SecuritySettings::read, SecuritySettings::update, this, fs, SECURITY_SETTINGS_FILE)
@@ -112,7 +114,7 @@ void SecuritySettingsService::generateToken(AsyncWebServerRequest * request) {
auto usernameParam = request->getParam("username"); auto usernameParam = request->getParam("username");
for (const User & _user : _state.users) { for (const User & _user : _state.users) {
if (_user.username == usernameParam->value()) { if (_user.username == usernameParam->value()) {
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
root["token"] = generateJWT(&_user); root["token"] = generateJWT(&_user);
response->setLength(); response->setLength();

View File

@@ -189,7 +189,7 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) {
// add MD5 to the response // add MD5 to the response
if (strlen(_md5.data()) == _md5.size() - 1) { if (strlen(_md5.data()) == _md5.size() - 1) {
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
root["md5"] = _md5.data(); root["md5"] = _md5.data();
response->setLength(); response->setLength();

View File

@@ -1,5 +1,7 @@
#include "WiFiScanner.h" #include "WiFiScanner.h"
#include "../core/psram_async_json_response.h"
WiFiScanner::WiFiScanner(AsyncWebServer * server, SecurityManager * securityManager) { WiFiScanner::WiFiScanner(AsyncWebServer * server, SecurityManager * securityManager) {
securityManager->addEndpoint(server, SCAN_NETWORKS_SERVICE_PATH, AuthenticationPredicates::IS_ADMIN, [this](AsyncWebServerRequest * request) { securityManager->addEndpoint(server, SCAN_NETWORKS_SERVICE_PATH, AuthenticationPredicates::IS_ADMIN, [this](AsyncWebServerRequest * request) {
scanNetworks(request); scanNetworks(request);
@@ -22,7 +24,7 @@ void WiFiScanner::scanNetworks(AsyncWebServerRequest * request) {
void WiFiScanner::listNetworks(AsyncWebServerRequest * request) { void WiFiScanner::listNetworks(AsyncWebServerRequest * request) {
const int numNetworks = WiFi.scanComplete(); const int numNetworks = WiFi.scanComplete();
if (numNetworks > -1) { if (numNetworks > -1) {
auto * response = new AsyncJsonResponse(false); auto * response = new emsesp::PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
JsonArray networks = root["networks"].to<JsonArray>(); JsonArray networks = root["networks"].to<JsonArray>();
for (uint8_t i = 0; i < numNetworks; i++) { for (uint8_t i = 0; i < numNetworks; i++) {

View File

@@ -57,6 +57,7 @@
#include "../web/WebModulesService.h" #include "../web/WebModulesService.h"
#include "psram_json_allocator.h" #include "psram_json_allocator.h"
#include "psram_async_json_response.h"
#include "emsdevicevalue.h" #include "emsdevicevalue.h"
#include "emsdevice.h" #include "emsdevice.h"
#include "emsfactory.h" #include "emsfactory.h"

View File

@@ -0,0 +1,143 @@
/*
* EMS-ESP - https://github.com/emsesp/EMS-ESP
* Copyright 2020-2025 emsesp.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EMSESP_PSRAM_ASYNC_JSON_RESPONSE_H
#define EMSESP_PSRAM_ASYNC_JSON_RESPONSE_H
#include "psram_json_allocator.h"
#ifndef EMSESP_STANDALONE
#include <AsyncJson.h>
#include <ChunkPrint.h>
#else
#include <AsyncJson.h>
#endif
namespace emsesp {
// AsyncJsonResponse subclass whose JsonDocument lives in PSRAM instead of
// internal SRAM.
//
// Why: every web API response goes through AsyncJsonResponse. The library's
// base class declares `JsonDocument _jsonBuffer;` with the *default*
// allocator, which on ESP32 means malloc() → internal heap. For large
// payloads (Dashboard, /rest/coreData, /rest/sensorData, full settings,
// customizations, etc.) this transiently consumes many KB of the same
// internal heap that LwIP / AsyncTCP / mbedTLS also need. Each concurrent
// browser tab compounds the cost.
//
// We can't change the base class's _jsonBuffer allocator (the upstream
// constructor doesn't take one), but we can route around it: keep our own
// PSRAM-backed document, override the virtual setLength()/_fillBuffer() so
// the framework serialises *our* document, and name-hide getRoot() so
// callers populate *our* document. The base's _jsonBuffer stays empty
// (just one root slot, <~32 bytes).
//
// Callers must use the derived type (or `auto`) when calling getRoot(),
// because getRoot() is non-virtual in the base. `request->send(response)`
// works as-is because setLength()/_fillBuffer() ARE virtual in the
// AsyncAbstractResponse grandparent.
//
// On standalone the lib_standalone AsyncJsonResponse stub never actually
// serves responses, so this whole class still compiles and behaves
// identically (allocator falls back to malloc anyway).
class PsramAsyncJsonResponse : public ::AsyncJsonResponse {
public:
explicit PsramAsyncJsonResponse(bool isArray = false)
: ::AsyncJsonResponse(isArray)
, psram_doc_(PsramJsonAllocator::instance()) {
if (isArray) {
psram_root_ = psram_doc_.add<JsonArray>();
} else {
psram_root_ = psram_doc_.add<JsonObject>();
}
}
// Hides AsyncJsonResponse::getRoot(). Must be called through a
// derived-type pointer/reference (the framework's base pointer keeps
// pointing at the empty base _jsonBuffer, which is intentional).
JsonVariant getRoot() {
return psram_root_;
}
#ifndef EMSESP_STANDALONE
size_t setLength() override {
_contentLength = measureJson(psram_root_);
if (_contentLength) {
_isValid = true;
}
return _contentLength;
}
size_t _fillBuffer(uint8_t * data, size_t len) override {
ChunkPrint dest(data, _sentLength, len);
serializeJson(psram_root_, dest);
return dest.written();
}
#endif
private:
JsonDocument psram_doc_;
JsonVariant psram_root_;
};
#if !defined(EMSESP_STANDALONE) && defined(ASYNC_MSG_PACK_SUPPORT) && ASYNC_MSG_PACK_SUPPORT == 1
// MessagePack equivalent — same routing trick but serialises with MsgPack.
class PsramAsyncMessagePackResponse : public ::AsyncMessagePackResponse {
public:
explicit PsramAsyncMessagePackResponse(bool isArray = false)
: ::AsyncMessagePackResponse(isArray)
, psram_doc_(PsramJsonAllocator::instance()) {
if (isArray) {
psram_root_ = psram_doc_.add<JsonArray>();
} else {
psram_root_ = psram_doc_.add<JsonObject>();
}
}
JsonVariant getRoot() {
return psram_root_;
}
size_t setLength() override {
_contentLength = measureMsgPack(psram_root_);
if (_contentLength) {
_isValid = true;
}
return _contentLength;
}
size_t _fillBuffer(uint8_t * data, size_t len) override {
ChunkPrint dest(data, _sentLength, len);
serializeMsgPack(psram_root_, dest);
return dest.written();
}
private:
JsonDocument psram_doc_;
JsonVariant psram_root_;
};
#else
// Standalone or no msgpack support: alias to plain JSON response so the
// codebase compiles unchanged.
using PsramAsyncMessagePackResponse = PsramAsyncJsonResponse;
#endif
} // namespace emsesp
#endif

View File

@@ -108,7 +108,7 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
EMSESP::system_.refreshHeapMem(); EMSESP::system_.refreshHeapMem();
// output json buffer // output json buffer
auto response = new AsyncJsonResponse(); auto response = new PsramAsyncJsonResponse();
// add more mem if needed - won't be needed in ArduinoJson 7 // add more mem if needed - won't be needed in ArduinoJson 7
// while (!response->getSize()) { // while (!response->getSize()) {

View File

@@ -27,7 +27,7 @@ WebActivityService::WebActivityService(AsyncWebServer * server, SecurityManager
} }
void WebActivityService::webActivityService(AsyncWebServerRequest * request) { void WebActivityService::webActivityService(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false); auto * response = new PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
JsonArray statsJson = root["stats"].to<JsonArray>(); JsonArray statsJson = root["stats"].to<JsonArray>();

View File

@@ -58,7 +58,7 @@ WebDataService::WebDataService(AsyncWebServer * server, SecurityManager * securi
// this is used in the Devices page and contains all EMS device information // this is used in the Devices page and contains all EMS device information
// /coreData endpoint // /coreData endpoint
void WebDataService::core_data(AsyncWebServerRequest * request) { void WebDataService::core_data(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false); auto * response = new PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
// list is already sorted by device type // list is already sorted by device type
@@ -70,7 +70,7 @@ void WebDataService::core_data(AsyncWebServerRequest * request) {
obj["id"] = emsdevice->unique_id(); // a unique id obj["id"] = emsdevice->unique_id(); // a unique id
obj["tn"] = emsdevice->device_type_2_device_name_translated(); // translated device type name obj["tn"] = emsdevice->device_type_2_device_name_translated(); // translated device type name
obj["t"] = emsdevice->device_type(); // device type number obj["t"] = emsdevice->device_type(); // device type number
obj["b"] = emsdevice->brand_to_cstr(); // brand obj["b"] = emsdevice->brand_to_char(); // brand (std::string → copied into doc, safe across async serialize)
obj["n"] = emsdevice->name(); // custom name obj["n"] = emsdevice->name(); // custom name
obj["d"] = emsdevice->device_id(); // deviceid obj["d"] = emsdevice->device_id(); // deviceid
obj["p"] = emsdevice->product_id(); // productid obj["p"] = emsdevice->product_id(); // productid
@@ -104,7 +104,7 @@ void WebDataService::core_data(AsyncWebServerRequest * request) {
// sensor data - sends back to web // sensor data - sends back to web
// /sensorData endpoint // /sensorData endpoint
void WebDataService::sensor_data(AsyncWebServerRequest * request) { void WebDataService::sensor_data(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false); auto * response = new PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
// temperature sensors // temperature sensors
@@ -176,7 +176,7 @@ void WebDataService::device_data(AsyncWebServerRequest * request) {
if (request->hasParam(F_(id))) { if (request->hasParam(F_(id))) {
id = Helpers::atoint(request->getParam(F_(id))->value().c_str()); // get id from url id = Helpers::atoint(request->getParam(F_(id))->value().c_str()); // get id from url
auto * response = new AsyncMessagePackResponse(); auto * response = new PsramAsyncMessagePackResponse();
// check size // check size
// while (!response) { // while (!response) {
@@ -217,6 +217,9 @@ void WebDataService::device_data(AsyncWebServerRequest * request) {
return; return;
} }
#endif #endif
// no matching device and not CUSTOM_UID: we never called request->send(response),
// so AsyncWebServer never took ownership. Delete it ourselves to avoid leaking.
delete response;
} }
// invalid // invalid
@@ -269,7 +272,7 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
return; return;
} }
// create JSON for output // create JSON for output
auto * response = new AsyncJsonResponse(false); auto * response = new PsramAsyncJsonResponse(false);
JsonObject output = response->getRoot(); JsonObject output = response->getRoot();
// the data could be in any format, but we need string // the data could be in any format, but we need string
// authenticated is always true // authenticated is always true

View File

@@ -193,7 +193,7 @@ void WebLogService::transmit(const QueuedLogMessage & message) {
void WebLogService::getSetValues(AsyncWebServerRequest * request, JsonVariant json) { void WebLogService::getSetValues(AsyncWebServerRequest * request, JsonVariant json) {
if ((request->method() == HTTP_GET) || (!json.is<JsonObject>())) { if ((request->method() == HTTP_GET) || (!json.is<JsonObject>())) {
// GET - return the values // GET - return the values
auto * response = new AsyncJsonResponse(false); auto * response = new PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
root["level"] = level_; root["level"] = level_;
root["max_messages"] = maximum_log_messages_; root["max_messages"] = maximum_log_messages_;

View File

@@ -414,7 +414,7 @@ void WebSettingsService::board_profile(AsyncWebServerRequest * request) {
if (request->hasParam("boardProfile")) { if (request->hasParam("boardProfile")) {
std::string board_profile = request->getParam("boardProfile")->value().c_str(); std::string board_profile = request->getParam("boardProfile")->value().c_str();
auto * response = new AsyncJsonResponse(false); auto * response = new PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
// 0=led, 1=dallas, 2=rx, 3=tx, 4=button, 5=phy_type, 6=eth_power, 7=eth_phy_addr, 8=eth_clock_mode, 9=led_type // 0=led, 1=dallas, 2=rx, 3=tx, 4=button, 5=phy_type, 6=eth_power, 7=eth_phy_addr, 8=eth_clock_mode, 9=led_type

View File

@@ -47,7 +47,7 @@ WebStatusService::WebStatusService(AsyncWebServer * server, SecurityManager * se
void WebStatusService::systemStatus(AsyncWebServerRequest * request) { void WebStatusService::systemStatus(AsyncWebServerRequest * request) {
EMSESP::system_.refreshHeapMem(); // refresh free heap and max alloc heap EMSESP::system_.refreshHeapMem(); // refresh free heap and max alloc heap
auto * response = new AsyncJsonResponse(false); auto * response = new PsramAsyncJsonResponse(false);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
// //
@@ -187,7 +187,7 @@ void WebStatusService::systemStatus(AsyncWebServerRequest * request) {
// generic action handler - as a POST // generic action handler - as a POST
void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json) { void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json) {
auto * response = new AsyncJsonResponse(); auto * response = new PsramAsyncJsonResponse();
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
// param is optional - https://arduinojson.org/news/2024/09/18/arduinojson-7-2/ // param is optional - https://arduinojson.org/news/2024/09/18/arduinojson-7-2/