Moved StreamBuf parameter calls into HapOut

This commit is contained in:
Gregg 2023-12-30 16:22:35 -06:00
parent fb5c9e1e29
commit 117c348708
3 changed files with 38 additions and 12 deletions

View File

@ -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 << "<html><head><title>" << homeSpan.displayName << "</title>\n";
@ -1250,7 +1251,8 @@ int HAPClient::getStatusURL(){
hapOut << "</table>\n";
}
hapOut << "</body></html>" << std::endl;
hapOut << "</body></html>\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);
}

View File

@ -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;

View File

@ -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)