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:
parent
bef151cdb1
commit
cc7ffb4c07
40
src/HAP.cpp
40
src/HAP.cpp
|
|
@ -1633,6 +1633,9 @@ void HapOut::HapStreamBuffer::flushBuffer(){
|
||||||
|
|
||||||
if(logLevel<=homeSpan.getLogLevel()){
|
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)
|
buffer[num]='\0'; // add null terminator but DO NOT increment num (we don't want terminator considered as part of buffer)
|
||||||
|
if(enablePrettyPrint) // if pretty print needed, use formatted method
|
||||||
|
printFormatted(buffer,num,2);
|
||||||
|
else // if not, just print
|
||||||
Serial.print(buffer);
|
Serial.print(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1680,6 +1683,7 @@ int HapOut::HapStreamBuffer::sync(){
|
||||||
hapClient=NULL;
|
hapClient=NULL;
|
||||||
enablePrettyPrint=false;
|
enablePrettyPrint=false;
|
||||||
byteCount=0;
|
byteCount=0;
|
||||||
|
indent=0;
|
||||||
|
|
||||||
mbedtls_sha512_finish_ret(ctx,hash); // finish SHA-384 and store hash
|
mbedtls_sha512_finish_ret(ctx,hash); // finish SHA-384 and store hash
|
||||||
mbedtls_sha512_starts_ret(ctx,1); // re-start hash for next time
|
mbedtls_sha512_starts_ret(ctx,1); // re-start hash for next time
|
||||||
|
|
@ -1687,6 +1691,42 @@ int HapOut::HapStreamBuffer::sync(){
|
||||||
return(0);
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -195,6 +195,7 @@ class HapOut : public std::ostream {
|
||||||
int logLevel=255; // default is NOT to print anything
|
int logLevel=255; // default is NOT to print anything
|
||||||
boolean enablePrettyPrint=false;
|
boolean enablePrettyPrint=false;
|
||||||
size_t byteCount=0;
|
size_t byteCount=0;
|
||||||
|
size_t indent=0;
|
||||||
uint8_t *hash;
|
uint8_t *hash;
|
||||||
mbedtls_sha512_context *ctx;
|
mbedtls_sha512_context *ctx;
|
||||||
|
|
||||||
|
|
@ -202,6 +203,7 @@ class HapOut : public std::ostream {
|
||||||
int_type overflow(int_type c) override;
|
int_type overflow(int_type c) override;
|
||||||
int sync() override;
|
int sync() override;
|
||||||
size_t getSize(){return(byteCount+pptr()-pbase());}
|
size_t getSize(){return(byteCount+pptr()-pbase());}
|
||||||
|
void printFormatted(char *buf, size_t nChars, size_t nsp);
|
||||||
|
|
||||||
HapStreamBuffer();
|
HapStreamBuffer();
|
||||||
~HapStreamBuffer();
|
~HapStreamBuffer();
|
||||||
|
|
|
||||||
|
|
@ -620,14 +620,12 @@ void Span::processSerialCommand(const char *c){
|
||||||
|
|
||||||
case 'd': {
|
case 'd': {
|
||||||
|
|
||||||
|
LOG0("\n*** Attributes Database ***\n\n");
|
||||||
|
hapOut.prettyPrint();
|
||||||
printfAttributes();
|
printfAttributes();
|
||||||
LOG0("\n*** Attributes Database: size=%d configuration=%d ***\n\n",hapOut.getSize(),hapConfig.configNumber);
|
size_t nBytes=hapOut.getSize();
|
||||||
hapOut.flush();
|
hapOut.flush();
|
||||||
|
LOG0("\n\n*** End Database: size=%d configuration=%d ***\n\n",nBytes,hapConfig.configNumber);
|
||||||
hapOut.setLogLevel(0);
|
|
||||||
printfAttributes();
|
|
||||||
hapOut.flush();
|
|
||||||
LOG0("\n\n*** End Database ***\n\n");
|
|
||||||
}
|
}
|
||||||
break;
|
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){
|
boolean Span::deleteAccessory(uint32_t n){
|
||||||
|
|
||||||
auto it=homeSpan.Accessories.begin();
|
auto it=homeSpan.Accessories.begin();
|
||||||
|
|
|
||||||
|
|
@ -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 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)
|
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 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
|
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
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
////////////////////////////////////////
|
|
||||||
Loading…
Reference in New Issue