Moved prettyPrint into HapOut

This completes re-work of all JSON buffer memory management to use HAPOut.

Next up: Stress test with Max Accessories to see how many Accessories we can now handle.
This commit is contained in:
Gregg 2024-01-01 09:39:54 -06:00
parent bef151cdb1
commit cc7ffb4c07
5 changed files with 47 additions and 134 deletions

View File

@ -1633,7 +1633,10 @@ void HapOut::HapStreamBuffer::flushBuffer(){
if(logLevel<=homeSpan.getLogLevel()){
buffer[num]='\0'; // add null terminator but DO NOT increment num (we don't want terminator considered as part of buffer)
Serial.print(buffer);
if(enablePrettyPrint) // if pretty print needed, use formatted method
printFormatted(buffer,num,2);
else // if not, just print
Serial.print(buffer);
}
if(hapClient!=NULL){
@ -1680,12 +1683,49 @@ int HapOut::HapStreamBuffer::sync(){
hapClient=NULL;
enablePrettyPrint=false;
byteCount=0;
indent=0;
mbedtls_sha512_finish_ret(ctx,hash); // finish SHA-384 and store hash
mbedtls_sha512_starts_ret(ctx,1); // re-start hash for next time
return(0);
}
//////////////////////////////////////
void HapOut::HapStreamBuffer::printFormatted(char *buf, size_t nChars, size_t nsp){
for(int i=0;i<nChars;i++){
switch(buf[i]){
case '{':
case '[':
Serial.printf("%c\n",buf[i]);
indent+=nsp;
for(int j=0;j<indent;j++)
Serial.printf(" ");
break;
case '}':
case ']':
Serial.printf("\n");
indent-=nsp;
for(int j=0;j<indent;j++)
Serial.printf(" ");
Serial.printf("%c",buf[i]);
break;
case ',':
Serial.printf("%c\n",buf[i]);
for(int j=0;j<indent;j++)
Serial.printf(" ");
break;
default:
Serial.printf("%c",buf[i]);
}
}
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

View File

@ -195,6 +195,7 @@ class HapOut : public std::ostream {
int logLevel=255; // default is NOT to print anything
boolean enablePrettyPrint=false;
size_t byteCount=0;
size_t indent=0;
uint8_t *hash;
mbedtls_sha512_context *ctx;
@ -202,6 +203,7 @@ class HapOut : public std::ostream {
int_type overflow(int_type c) override;
int sync() override;
size_t getSize(){return(byteCount+pptr()-pbase());}
void printFormatted(char *buf, size_t nChars, size_t nsp);
HapStreamBuffer();
~HapStreamBuffer();

View File

@ -620,14 +620,12 @@ void Span::processSerialCommand(const char *c){
case 'd': {
LOG0("\n*** Attributes Database ***\n\n");
hapOut.prettyPrint();
printfAttributes();
LOG0("\n*** Attributes Database: size=%d configuration=%d ***\n\n",hapOut.getSize(),hapConfig.configNumber);
size_t nBytes=hapOut.getSize();
hapOut.flush();
hapOut.setLogLevel(0);
printfAttributes();
hapOut.flush();
LOG0("\n\n*** End Database ***\n\n");
LOG0("\n\n*** End Database: size=%d configuration=%d ***\n\n",nBytes,hapConfig.configNumber);
}
break;
@ -1250,52 +1248,6 @@ void Span::printfAttributes(int flags){
///////////////////////////////
void Span::prettyPrint(char *buf, int nsp, int minLogLevel){
if(logLevel<minLogLevel)
return;
int s=strlen(buf);
int indent=0;
for(int i=0;i<s;i++){
switch(buf[i]){
case '{':
case '[':
Serial.printf("%c\n",buf[i]);
indent+=nsp;
for(int j=0;j<indent;j++)
Serial.printf(" ");
break;
case '}':
case ']':
Serial.printf("\n");
indent-=nsp;
for(int j=0;j<indent;j++)
Serial.printf(" ");
Serial.printf("%c",buf[i]);
break;
case ',':
Serial.printf("%c\n",buf[i]);
for(int j=0;j<indent;j++)
Serial.printf(" ");
break;
default:
Serial.printf("%c",buf[i]);
} // switch
} // loop over all characters
Serial.printf("\n");
}
///////////////////////////
boolean Span::deleteAccessory(uint32_t n){
auto it=homeSpan.Accessories.begin();

View File

@ -288,7 +288,6 @@ class Span{
void printfAttributes(int flags=GET_VALUE|GET_META|GET_PERMS|GET_TYPE|GET_DESC); // writes Attributes JSON database to hapOut stream
void prettyPrint(char *buf, int nsp=2, int minLogLevel=0); // print arbitrary JSON from buf to serial monitor, formatted with indentions of 'nsp' spaces, subject to specified minimum log level
SpanCharacteristic *find(uint32_t aid, int iid); // return Characteristic with matching aid and iid (else NULL if not found)
int countCharacteristics(char *buf); // return number of characteristic objects referenced in PUT /characteristics JSON request
int updateCharacteristics(char *buf, SpanBuf *pObj); // parses PUT /characteristics JSON request 'buf into 'pObj' and updates referenced characteristics; returns 1 on success, 0 on fail

View File

@ -1,80 +0,0 @@
/*********************************************************************************
* MIT License
*
* Copyright (c) 2020-2023 Gregg E. Berman
*
* https://github.com/HomeSpan/HomeSpan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
********************************************************************************/
//#include "HAP.h"
//
//StreamBuffer::StreamBuffer(){
//
// buffer=(char *)HS_MALLOC(bufSize);
// setp(buffer, buffer+bufSize-1);
//}
//
////////////////////////////////////////
//
//StreamBuffer::~StreamBuffer(){
//
// free(buffer);
// sync();
//}
//
////////////////////////////////////////
//
//int StreamBuffer::flushBuffer(){
// int num=pptr()-pbase();
//
// write(1,buffer,num);
// client->write(buffer,num);
// delay(1);
//
// pbump(-num);
// return num;
//}
//
////////////////////////////////////////
//
//StreamBuffer::int_type StreamBuffer::overflow(StreamBuffer::int_type c){
//
// if(c!=EOF){
// *pptr() = c;
// pbump(1);
// }
//
// if(flushBuffer()==EOF)
// return EOF;
// return c;
//}
//
////////////////////////////////////////
//
//int StreamBuffer::sync(){
//
// if(flushBuffer()==EOF)
// return -1;
// return 0;
//}
//
////////////////////////////////////////