From 117c348708e4fb2c52133317b0f4606a4ef92b83 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 30 Dec 2023 16:22:35 -0600 Subject: [PATCH] Moved StreamBuf parameter calls into HapOut --- src/HAP.cpp | 19 ++++++++++++++----- src/HAP.h | 29 +++++++++++++++++++++++------ src/HomeSpan.cpp | 2 +- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index bfb8313..98a9f4e 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1152,7 +1152,8 @@ int HAPClient::getStatusURL(){ LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",client.remoteIP().toString().c_str()); - hapStream.setHapClient(this); +// hapStream.setHapClient(this).setLogLevel(2); + hapOut.setHapClient(this).setLogLevel(2); hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n"; hapOut << "" << homeSpan.displayName << "\n"; @@ -1250,7 +1251,8 @@ int HAPClient::getStatusURL(){ hapOut << "\n"; } - hapOut << "" << std::endl; + hapOut << "\n"; + hapOut.flush(); LOG2("------------ SENT! --------------\n"); @@ -1637,7 +1639,7 @@ void Nonce::inc(){ StreamBuffer::StreamBuffer(){ - buffer=(char *)HS_MALLOC(bufSize); + buffer=(char *)HS_MALLOC(bufSize+1); // add 1 for optional null terminator when printing text setp(buffer, buffer+bufSize-1); } @@ -1655,7 +1657,8 @@ int StreamBuffer::flushBuffer(){ int num=pptr()-pbase(); if(logLevel<=homeSpan.getLogLevel()){ - write(1,buffer,num); + buffer[num]='\0'; + Serial.print(buffer); } if(hapClient!=NULL){ @@ -1685,8 +1688,14 @@ StreamBuffer::int_type StreamBuffer::overflow(StreamBuffer::int_type c){ ////////////////////////////////////// int StreamBuffer::sync(){ + + int_type c=flushBuffer(); - if(flushBuffer()==EOF) + enablePrettyPrint=false; + logLevel=0; + hapClient=NULL; + + if(c==EOF) return(-1); return(0); } diff --git a/src/HAP.h b/src/HAP.h index db43189..fa7448f 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -180,13 +180,17 @@ struct HAPClient { }; ///////////////////////////////////////////////// -// StreamBuffer Structure +// StreamBuffer and HapOut Structures + +class HapOut; class StreamBuffer : public std::streambuf { + + friend HapOut; private: - const size_t bufSize=1024; + const size_t bufSize=1024; // max allowed for HAP encrypted records char *buffer; HAPClient *hapClient=NULL; int logLevel=0; @@ -201,14 +205,27 @@ class StreamBuffer : public std::streambuf { StreamBuffer(); ~StreamBuffer(); - StreamBuffer& setHapClient(HAPClient *hapClient){this->hapClient=hapClient;return(*this);} - StreamBuffer& setLogLevel(int logLevel){this->logLevel=logLevel;return(*this);} - StreamBuffer& prettyPrint(){enablePrettyPrint=true;return(*this);} +}; + +class HapOut : public std::ostream { + + private: + + StreamBuffer *sBuf; + + public: + + HapOut(StreamBuffer *sBuf) : std::ostream(sBuf) {this->sBuf=sBuf;} + + HapOut& setHapClient(HAPClient *hapClient){sBuf->hapClient=hapClient;return(*this);} + HapOut& setLogLevel(int logLevel){sBuf->logLevel=logLevel;return(*this);} + HapOut& prettyPrint(){sBuf->enablePrettyPrint=true;return(*this);} }; ///////////////////////////////////////////////// // Extern Variables extern HAPClient **hap; -extern std::ostream hapOut; +//extern std::ostream hapOut; +extern HapOut hapOut; extern StreamBuffer hapStream; diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 0be3256..218f8a9 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -46,7 +46,7 @@ const __attribute__((section(".rodata_custom_desc"))) SpanPartition spanPartitio using namespace Utils; StreamBuffer hapStream; -std::ostream hapOut(&hapStream); +HapOut hapOut(&hapStream); HAPClient **hap; // HAP Client structure containing HTTP client connections, parsing routines, and state variables (global-scoped variable) Span homeSpan; // HAP Attributes database and all related control functions for this Accessory (global-scoped variable)