From 18c74e6f175639a80dde56ea7a231c0ef571b162 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 30 Dec 2023 17:07:12 -0600 Subject: [PATCH] Moved StreamBuf into HapOut as private nested class --- src/HAP.cpp | 11 +++++----- src/HAP.h | 55 +++++++++++++++++++----------------------------- src/HomeSpan.cpp | 4 +--- 3 files changed, 28 insertions(+), 42 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 98a9f4e..6b7e38e 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1152,7 +1152,6 @@ int HAPClient::getStatusURL(){ LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",client.remoteIP().toString().c_str()); -// 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"; @@ -1637,7 +1636,7 @@ void Nonce::inc(){ ////////////////////////////////////// ////////////////////////////////////// -StreamBuffer::StreamBuffer(){ +HapOut::HapStreamBuffer::HapStreamBuffer(){ buffer=(char *)HS_MALLOC(bufSize+1); // add 1 for optional null terminator when printing text setp(buffer, buffer+bufSize-1); @@ -1645,7 +1644,7 @@ StreamBuffer::StreamBuffer(){ ////////////////////////////////////// -StreamBuffer::~StreamBuffer(){ +HapOut::HapStreamBuffer::~HapStreamBuffer(){ sync(); free(buffer); @@ -1653,7 +1652,7 @@ StreamBuffer::~StreamBuffer(){ ////////////////////////////////////// -int StreamBuffer::flushBuffer(){ +int HapOut::HapStreamBuffer::flushBuffer(){ int num=pptr()-pbase(); if(logLevel<=homeSpan.getLogLevel()){ @@ -1673,7 +1672,7 @@ int StreamBuffer::flushBuffer(){ ////////////////////////////////////// -StreamBuffer::int_type StreamBuffer::overflow(StreamBuffer::int_type c){ +std::streambuf::int_type HapOut::HapStreamBuffer::overflow(std::streambuf::int_type c){ if(c!=EOF){ *pptr() = c; @@ -1687,7 +1686,7 @@ StreamBuffer::int_type StreamBuffer::overflow(StreamBuffer::int_type c){ ////////////////////////////////////// -int StreamBuffer::sync(){ +int HapOut::HapStreamBuffer::sync(){ int_type c=flushBuffer(); diff --git a/src/HAP.h b/src/HAP.h index fa7448f..e690cc0 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -180,52 +180,41 @@ struct HAPClient { }; ///////////////////////////////////////////////// -// StreamBuffer and HapOut Structures - -class HapOut; - -class StreamBuffer : public std::streambuf { - - friend HapOut; - - private: - - const size_t bufSize=1024; // max allowed for HAP encrypted records - char *buffer; - HAPClient *hapClient=NULL; - int logLevel=0; - boolean enablePrettyPrint=false; - - int flushBuffer() ; - int_type overflow(int_type c) override; - int sync() override; - - public: - - StreamBuffer(); - ~StreamBuffer(); - -}; +// HapOut Structure class HapOut : public std::ostream { private: + + struct HapStreamBuffer : public std::streambuf { + + const size_t bufSize=1024; // max allowed for HAP encrypted records + char *buffer; + HAPClient *hapClient=NULL; + int logLevel=0; + boolean enablePrettyPrint=false; - StreamBuffer *sBuf; + int flushBuffer() ; + int_type overflow(int_type c) override; + int sync() override; + + HapStreamBuffer(); + ~HapStreamBuffer(); + }; + + HapStreamBuffer hapBuffer; public: - HapOut(StreamBuffer *sBuf) : std::ostream(sBuf) {this->sBuf=sBuf;} + HapOut() : std::ostream(&hapBuffer){} - 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);} + HapOut& setHapClient(HAPClient *hapClient){hapBuffer.hapClient=hapClient;return(*this);} + HapOut& setLogLevel(int logLevel){hapBuffer.logLevel=logLevel;return(*this);} + HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;return(*this);} }; ///////////////////////////////////////////////// // Extern Variables extern HAPClient **hap; -//extern std::ostream hapOut; extern HapOut hapOut; -extern StreamBuffer hapStream; diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 218f8a9..73f1593 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -45,9 +45,7 @@ const __attribute__((section(".rodata_custom_desc"))) SpanPartition spanPartitio using namespace Utils; -StreamBuffer hapStream; -HapOut hapOut(&hapStream); - +HapOut hapOut; // Specialized output stream that can both print to serial monitor and encrypt/transmit to HAP Clients with minimal memory usage (global-scope) 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) HapCharacteristics hapChars; // Instantiation of all HAP Characteristics (used to create SpanCharacteristics)