Add temp buffer statistic

This commit is contained in:
Michael Geramb 2023-11-11 19:48:11 +01:00
parent ae60a84855
commit 3d955be629
6 changed files with 53 additions and 19 deletions

View File

@ -96,7 +96,7 @@ void HAPClient::init(){
} }
if(!nvs_get_blob(hapNVS,"CONTROLLERS",NULL,&len)){ // if found long-term Controller Pairings data from NVS if(!nvs_get_blob(hapNVS,"CONTROLLERS",NULL,&len)){ // if found long-term Controller Pairings data from NVS
TempBuffer <Controller> tBuf(len/sizeof(Controller)); TempBuffer <Controller> tBuf(len/sizeof(Controller), "HAPClient::init");
nvs_get_blob(hapNVS,"CONTROLLERS",tBuf.get(),&len); // retrieve data nvs_get_blob(hapNVS,"CONTROLLERS",tBuf.get(),&len); // retrieve data
for(int i=0;i<tBuf.size();i++){ for(int i=0;i<tBuf.size();i++){
if(tBuf.get()[i].allocated) if(tBuf.get()[i].allocated)
@ -157,7 +157,7 @@ void HAPClient::processRequest(){
return; return;
} }
TempBuffer <uint8_t> httpBuf(messageSize+1); // leave room for null character added below TempBuffer <uint8_t> httpBuf(messageSize+1, "HAPClient::processRequest"); // leave room for null character added below
if(cPair){ // expecting encrypted message if(cPair){ // expecting encrypted message
LOG2("<<<< #### "); LOG2("<<<< #### ");
@ -859,7 +859,7 @@ int HAPClient::getAccessoriesURL(){
LOG1(")...\n"); LOG1(")...\n");
int nBytes = homeSpan.sprintfAttributes(NULL); // get size of HAP attributes JSON int nBytes = homeSpan.sprintfAttributes(NULL); // get size of HAP attributes JSON
TempBuffer <char> jBuf(nBytes+1); TempBuffer <char> jBuf(nBytes+1,"HAPClient::getAccessoriesURL");
homeSpan.sprintfAttributes(jBuf.get()); // create JSON database (will need to re-cast to uint8_t* below) homeSpan.sprintfAttributes(jBuf.get()); // create JSON database (will need to re-cast to uint8_t* below)
char *body; char *body;
@ -958,7 +958,7 @@ int HAPClient::postPairingsURL(){
case 5: { case 5: {
LOG1("List...\n"); LOG1("List...\n");
TempBuffer <uint8_t> tBuf(listControllers(NULL)); TempBuffer <uint8_t> tBuf(listControllers(NULL), "HAPClient::postPairingsURL listControllers");
char *body; char *body;
asprintf(&body,"HTTP/1.1 200 OK\r\nContent-Type: application/pairing+tlv8\r\nContent-Length: %d\r\n\r\n",tBuf.len()); // create Body with Content Length = size of TLV data asprintf(&body,"HTTP/1.1 200 OK\r\nContent-Type: application/pairing+tlv8\r\nContent-Length: %d\r\n\r\n",tBuf.len()); // create Body with Content Length = size of TLV data
@ -1393,7 +1393,7 @@ void HAPClient::eventNotify(SpanBuf *pObj, int nObj, int ignoreClient){
void HAPClient::tlvRespond(){ void HAPClient::tlvRespond(){
TempBuffer <uint8_t> tBuf(tlv8.pack(NULL)); // create buffer to hold TLV data TempBuffer <uint8_t> tBuf(tlv8.pack(NULL), "HAPClient::tlvRespond"); // create buffer to hold TLV data
tlv8.pack(tBuf.get()); // pack TLV records into buffer tlv8.pack(tBuf.get()); // pack TLV records into buffer
char *body; char *body;
@ -1433,7 +1433,7 @@ int HAPClient::receiveEncrypted(uint8_t *httpBuf, int messageSize){
return(0); return(0);
} }
TempBuffer <uint8_t> tBuf(n+16); // expected number of total bytes = n bytes in encoded message + 16 bytes for appended authentication tag TempBuffer <uint8_t> tBuf(n+16, "HAPClient::receiveEncrypted"); // expected number of total bytes = n bytes in encoded message + 16 bytes for appended authentication tag
if(client.read(tBuf.get(),tBuf.len())!=tBuf.len()){ if(client.read(tBuf.get(),tBuf.len())!=tBuf.len()){
LOG0("\n\n*** ERROR: Malformed encrypted message frame\n\n"); LOG0("\n\n*** ERROR: Malformed encrypted message frame\n\n");
@ -1469,7 +1469,7 @@ void HAPClient::sendEncrypted(char *body, uint8_t *dataBuf, int dataLen){
if(maxFrameSize>FRAME_SIZE) // cap maxFrameSize by FRAME_SIZE (HAP restriction) if(maxFrameSize>FRAME_SIZE) // cap maxFrameSize by FRAME_SIZE (HAP restriction)
maxFrameSize=FRAME_SIZE; maxFrameSize=FRAME_SIZE;
TempBuffer <uint8_t> tBuf(2+maxFrameSize+16); // 2-byte AAD + encrytped data + 16-byte authentication tag TempBuffer <uint8_t> tBuf(2+maxFrameSize+16, "HAPClient::sendEncrypted"); // 2-byte AAD + encrytped data + 16-byte authentication tag
tBuf.get()[0]=bodyLen%256; // store number of bytes in first frame that encrypts the Body (AAD bytes) tBuf.get()[0]=bodyLen%256; // store number of bytes in first frame that encrypts the Body (AAD bytes)
tBuf.get()[1]=bodyLen/256; tBuf.get()[1]=bodyLen/256;
@ -1696,7 +1696,7 @@ void HAPClient::saveControllers(){
return; return;
} }
TempBuffer <Controller> tBuf(controllerList.size()); // create temporary buffer to hold Controller data TempBuffer <Controller> tBuf(controllerList.size(), "HAPClient::saveControllers"); // create temporary buffer to hold Controller data
std::copy(controllerList.begin(),controllerList.end(),tBuf.get()); // copy data from linked list to buffer std::copy(controllerList.begin(),controllerList.end(),tBuf.get()); // copy data from linked list to buffer
nvs_set_blob(hapNVS,"CONTROLLERS",tBuf.get(),tBuf.len()); // update data nvs_set_blob(hapNVS,"CONTROLLERS",tBuf.get(),tBuf.len()); // update data

View File

@ -580,7 +580,7 @@ void Span::processSerialCommand(const char *c){
case 'Z': { case 'Z': {
HAPClient::saveControllers(); HAPClient::saveControllers();
break; break;
TempBuffer <uint8_t> tBuf(HAPClient::listControllers(NULL)); TempBuffer <uint8_t> tBuf(HAPClient::listControllers(NULL), "Span::processSerialCommand Z");
HAPClient::listControllers(tBuf.get()); HAPClient::listControllers(tBuf.get());
Serial.printf("SIZE = %d\n",tBuf.len()); Serial.printf("SIZE = %d\n",tBuf.len());
HAPClient::hexPrintRow(tBuf.get(),tBuf.len()); HAPClient::hexPrintRow(tBuf.get(),tBuf.len());
@ -628,7 +628,7 @@ void Span::processSerialCommand(const char *c){
case 'd': { case 'd': {
TempBuffer <char> qBuf(sprintfAttributes(NULL)+1); TempBuffer <char> qBuf(sprintfAttributes(NULL)+1, "Span::processSerialCommand d");
sprintfAttributes(qBuf.get()); sprintfAttributes(qBuf.get());
LOG0("\n*** Attributes Database: size=%d configuration=%d ***\n\n",qBuf.len()-1,hapConfig.configNumber); LOG0("\n*** Attributes Database: size=%d configuration=%d ***\n\n",qBuf.len()-1,hapConfig.configNumber);
@ -854,6 +854,7 @@ void Span::processSerialCommand(const char *c){
case 'm': { case 'm': {
LOG0("Free Heap=%d bytes (low=%d)\n",heap_caps_get_free_size(MALLOC_CAP_DEFAULT),heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT)); LOG0("Free Heap=%d bytes (low=%d)\n",heap_caps_get_free_size(MALLOC_CAP_DEFAULT),heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT));
LOG0("Max used TempBuffer size=%d bytes from %s\n", TempBufferBase::getMaxUsedTempBufferSize(), TempBufferBase::getNameOfBufferWithLargestBufferSize());
} }
break; break;
@ -1023,7 +1024,7 @@ void Span::processSerialCommand(const char *c){
LOG0("\n*** Pairing Data used for Cloning another Device\n\n"); LOG0("\n*** Pairing Data used for Cloning another Device\n\n");
size_t olen; size_t olen;
TempBuffer<char> tBuf(256); TempBuffer<char> tBuf(256, "Span::processSerialCommand P");
mbedtls_base64_encode((uint8_t *)tBuf.get(),256,&olen,(uint8_t *)&HAPClient::accessory,sizeof(struct Accessory)); mbedtls_base64_encode((uint8_t *)tBuf.get(),256,&olen,(uint8_t *)&HAPClient::accessory,sizeof(struct Accessory));
LOG0("Accessory data: %s\n",tBuf.get()); LOG0("Accessory data: %s\n",tBuf.get());
for(const auto &cont : HAPClient::controllerList){ for(const auto &cont : HAPClient::controllerList){
@ -1037,7 +1038,7 @@ void Span::processSerialCommand(const char *c){
case 'C': { case 'C': {
LOG0("\n*** Clone Pairing Data from another Device\n\n"); LOG0("\n*** Clone Pairing Data from another Device\n\n");
TempBuffer<char> tBuf(200); TempBuffer<char> tBuf(200, "Span::processSerialCommand C");
size_t olen; size_t olen;
tBuf.get()[0]='\0'; tBuf.get()[0]='\0';
@ -1591,7 +1592,7 @@ int Span::sprintfAttributes(char **ids, int numIDs, int flags, char *cBuf){
boolean Span::updateDatabase(boolean updateMDNS){ boolean Span::updateDatabase(boolean updateMDNS){
uint8_t tHash[48]; uint8_t tHash[48];
TempBuffer <char> tBuf(sprintfAttributes(NULL,GET_META|GET_PERMS|GET_TYPE|GET_DESC)+1); TempBuffer <char> tBuf(sprintfAttributes(NULL,GET_META|GET_PERMS|GET_TYPE|GET_DESC)+1, "Span::updateDatabase");
sprintfAttributes(tBuf.get(),GET_META|GET_PERMS|GET_TYPE|GET_DESC); sprintfAttributes(tBuf.get(),GET_META|GET_PERMS|GET_TYPE|GET_DESC);
mbedtls_sha512_ret((uint8_t *)tBuf.get(),tBuf.len(),tHash,1); // create SHA-384 hash of JSON (can be any hash - just looking for a unique key) mbedtls_sha512_ret((uint8_t *)tBuf.get(),tBuf.len(),tHash,1); // create SHA-384 hash of JSON (can be any hash - just looking for a unique key)

View File

@ -730,7 +730,7 @@ class SpanCharacteristic{
size_t olen; size_t olen;
mbedtls_base64_encode(NULL,0,&olen,data,len); // get length of string buffer needed (mbedtls includes the trailing null in this size) mbedtls_base64_encode(NULL,0,&olen,data,len); // get length of string buffer needed (mbedtls includes the trailing null in this size)
TempBuffer<char> tBuf(olen); // create temporary string buffer, with room for trailing null TempBuffer<char> tBuf(olen, "SpanCharacteristic::setData"); // create temporary string buffer, with room for trailing null
mbedtls_base64_encode((uint8_t*)tBuf.get(),olen,&olen,data,len ); // encode data into string buf mbedtls_base64_encode((uint8_t*)tBuf.get(),olen,&olen,data,len ); // encode data into string buf
setString(tBuf.get()); // call setString to continue processing as if characteristic was a string setString(tBuf.get()); // call setString to continue processing as if characteristic was a string
} }

View File

@ -184,7 +184,7 @@ void Network::apConfigure(){
continue; continue;
} }
TempBuffer <uint8_t> httpBuf(messageSize+1); // leave room for null character added below TempBuffer <uint8_t> httpBuf(messageSize+1, "Network::apConfigure"); // leave room for null character added below
int nBytes=client.read(httpBuf.get(),messageSize); // read all available bytes up to maximum allowed+1 int nBytes=client.read(httpBuf.get(),messageSize); // read all available bytes up to maximum allowed+1

View File

@ -279,3 +279,23 @@ void PushButton::reset(){
////////////////////////////////////// //////////////////////////////////////
touch_value_t PushButton::threshold=0; touch_value_t PushButton::threshold=0;
//////////////////////////////////////
int TempBufferBase::maxUsedTempBufferSize = 0;
//////////////////////////////////////
int TempBufferBase::getMaxUsedTempBufferSize() {
return maxUsedTempBufferSize;
}
//////////////////////////////////////
const char* TempBufferBase::nameOfBufferWithLargestBufferSize = "";
//////////////////////////////////////
const char* TempBufferBase::getNameOfBufferWithLargestBufferSize() {
return nameOfBufferWithLargestBufferSize;
}

View File

@ -41,19 +41,32 @@ String mask(char *c, int n); // simply utility that creates a String fr
// Creates a temporary buffer that is freed after // Creates a temporary buffer that is freed after
// going out of scope // going out of scope
class TempBufferBase
{
protected:
static int maxUsedTempBufferSize;
static const char* nameOfBufferWithLargestBufferSize;
public:
static int getMaxUsedTempBufferSize();
static const char* getNameOfBufferWithLargestBufferSize();
};
template <class bufType> template <class bufType>
class TempBuffer { class TempBuffer : TempBufferBase {
private: private:
bufType *buf; bufType *buf;
int nBytes; int nBytes;
int nElements; int nElements;
public: public:
TempBuffer(int _nElements, const char* nameOfBuffer) : nElements(_nElements) {
TempBuffer(int _nElements) : nElements(_nElements) {
nBytes=nElements*sizeof(bufType); nBytes=nElements*sizeof(bufType);
if (maxUsedTempBufferSize < nBytes)
{
maxUsedTempBufferSize = nBytes;
nameOfBufferWithLargestBufferSize = nameOfBuffer;
}
buf=(bufType *)malloc(nBytes); buf=(bufType *)malloc(nBytes);
if(buf==NULL){ if(buf==NULL){
Serial.print("\n\n*** FATAL ERROR: Requested allocation of "); Serial.print("\n\n*** FATAL ERROR: Requested allocation of ");