From bd474778e545374327dbeb9990ed0082b802b501 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 6 Jan 2024 16:55:17 -0600 Subject: [PATCH] fix for PSRAM with new hapOut Force hapOut to use internal memory only. Tested on ESP32-S2 with/without PSRAM. However, crashes on ESP32-S3 when using PSRAM (?!) --- .../MaxAccessories/MaxAccessories.ino | 2 +- src/HAP.cpp | 18 ++++++----- src/HAP.h | 3 +- src/HomeSpan.cpp | 30 ++++++++++++------- src/src.ino | 2 +- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/examples/Other Examples/MaxAccessories/MaxAccessories.ino b/examples/Other Examples/MaxAccessories/MaxAccessories.ino index aa72f2d..d485a06 100644 --- a/examples/Other Examples/MaxAccessories/MaxAccessories.ino +++ b/examples/Other Examples/MaxAccessories/MaxAccessories.ino @@ -73,7 +73,7 @@ void setup() { Serial.begin(115200); - homeSpan.setLogLevel(1); + homeSpan.setLogLevel(2); homeSpan.enableWebLog(500); homeSpan.begin(Category::Lighting,"HomeSpan Max"); diff --git a/src/HAP.cpp b/src/HAP.cpp index 3fc126f..5cfa6c0 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1604,15 +1604,19 @@ void Nonce::inc(){ HapOut::HapStreamBuffer::HapStreamBuffer(){ - buffer=(char *)HS_MALLOC(bufSize+1); // add 1 for adding null terminator when printing text - encBuf=(uint8_t *)HS_MALLOC(bufSize+18); // 2-byte AAD + encrypted data + 16-byte authentication tag + // note - must require all memory allocation to be pulled from INTERNAL heap only + + const uint32_t caps=MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL; + + buffer=(char *)heap_caps_malloc(bufSize+1,caps); // add 1 for adding null terminator when printing text + encBuf=(uint8_t *)heap_caps_malloc(bufSize+18,caps); // 2-byte AAD + encrypted data + 16-byte authentication tag + hash=(uint8_t *)heap_caps_malloc(48,caps); // space for SHA-384 hash output + ctx = (mbedtls_sha512_context *)heap_caps_malloc(sizeof(mbedtls_sha512_context),caps); // space for hash context - hash=(uint8_t *)HS_MALLOC(48); // space for SHA-384 hash output - ctx = (mbedtls_sha512_context *)HS_MALLOC(sizeof(mbedtls_sha512_context)); // space for hash context - mbedtls_sha512_init(ctx); // initialize context - mbedtls_sha512_starts_ret(ctx,1); // start SHA-384 hash (note second argument=1) + mbedtls_sha512_init(ctx); // initialize context + mbedtls_sha512_starts_ret(ctx,1); // start SHA-384 hash (note second argument=1) - setp(buffer, buffer+bufSize-1); + setp(buffer, buffer+bufSize-1); // assign buffer pointers } ////////////////////////////////////// diff --git a/src/HAP.h b/src/HAP.h index cd79b57..579ca04 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -207,6 +207,7 @@ class HapOut : public std::ostream { HapStreamBuffer(); ~HapStreamBuffer(); + }; HapStreamBuffer hapBuffer; @@ -214,7 +215,7 @@ class HapOut : public std::ostream { public: HapOut() : std::ostream(&hapBuffer){} - + HapOut& setHapClient(HAPClient *hapClient){hapBuffer.hapClient=hapClient;return(*this);} HapOut& setLogLevel(int logLevel){hapBuffer.logLevel=logLevel;return(*this);} HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;hapBuffer.logLevel=0;return(*this);} diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 9e1fd96..2d746cd 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -185,10 +185,10 @@ void Span::pollTask() { processSerialCommand("i"); // print homeSpan configuration info HAPClient::init(); // read NVS and load HAP settings - - if(heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL)