From 392eac38b9d2d2462f1ca389809c5d4d7ad38026 Mon Sep 17 00:00:00 2001 From: Gregg Date: Mon, 8 Jan 2024 18:19:38 -0600 Subject: [PATCH] Added homeSpan.getWebLog() --- src/HAP.cpp | 30 ++++++++++++++++++++---------- src/HAP.h | 5 ++++- src/HomeSpan.cpp | 17 ++++++++++------- src/HomeSpan.h | 4 +++- src/src.ino | 22 +++++++++++++++++----- 5 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 4bc4231..3ff4f3f 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -256,7 +256,7 @@ void HAPClient::processRequest(){ } if(homeSpan.webLog.isEnabled && !strncmp(body,homeSpan.webLog.statusURL.c_str(),homeSpan.webLog.statusURL.length())){ // GET STATUS - AN OPTIONAL, NON-HAP-R2 FEATURE - getStatusURL(); + getStatusURL(this,NULL); return; } @@ -1095,7 +1095,7 @@ int HAPClient::putPrepareURL(char *json){ ////////////////////////////////////// -int HAPClient::getStatusURL(){ +void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *)){ char clocktime[33]; @@ -1116,9 +1116,10 @@ int HAPClient::getStatusURL(){ sprintf(uptime,"%d:%02d:%02d:%02d",days,hours,mins,secs); - LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",client.remoteIP().toString().c_str()); - - hapOut.setHapClient(this).setLogLevel(2); + if(hapClient) + LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",hapClient->client.remoteIP().toString().c_str()); + + hapOut.setHapClient(hapClient).setLogLevel(2).setCallback(callBack); hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n"; hapOut << "" << homeSpan.displayName << "\n"; @@ -1225,10 +1226,10 @@ int HAPClient::getStatusURL(){ hapOut << "\n"; hapOut.flush(); - LOG2("------------ SENT! --------------\n"); - - client.stop(); - return(1); + if(hapClient){ + hapClient->client.stop(); + LOG2("------------ SENT! --------------\n"); + } } ////////////////////////////////////// @@ -1635,8 +1636,12 @@ void HapOut::HapStreamBuffer::flushBuffer(){ byteCount+=num; + buffer[num]='\0'; // add null terminator but DO NOT increment num (we don't want terminator considered as part of buffer) + + if(callBack) + callBack(buffer); + 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) if(enablePrettyPrint) // if pretty print needed, use formatted method printFormatted(buffer,num,2); else // if not, just print @@ -1688,6 +1693,11 @@ int HapOut::HapStreamBuffer::sync(){ enablePrettyPrint=false; byteCount=0; indent=0; + + if(callBack){ + callBack(NULL); + callBack=NULL; + } mbedtls_sha512_finish_ret(ctx,hash); // finish SHA-384 and store hash mbedtls_sha512_starts_ret(ctx,1); // re-start hash for next time diff --git a/src/HAP.h b/src/HAP.h index 579ca04..0f292d5 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -143,7 +143,6 @@ struct HAPClient { int getCharacteristicsURL(char *urlBuf); // GET /characteristics (HAP Section 6.7.4) int putCharacteristicsURL(char *json); // PUT /characteristics (HAP Section 6.7.2) int putPrepareURL(char *json); // PUT /prepare (HAP Section 6.7.2.4) - int getStatusURL(); // GET / status (an optional, non-HAP feature) void tlvRespond(TLV8 &tlv8); // respond to client with HTTP OK header and all defined TLV data records void sendEncrypted(char *body, uint8_t *dataBuf, int dataLen); // send client complete ChaCha20-Poly1305 encrypted HTTP mesage comprising a null-terminated 'body' and 'dataBuf' with 'dataLen' bytes @@ -172,6 +171,8 @@ struct HAPClient { static void checkTimedWrites(); // checks for expired Timed Write PIDs, and clears any found (HAP Section 6.7.2.4) static void eventNotify(SpanBuf *pObj, int nObj, int ignoreClient=-1); // transmits EVENT Notifications for nObj SpanBuf objects, pObj, with optional flag to ignore a specific client + static void getStatusURL(HAPClient *, void (*)(const char *)); // GET / status (an optional, non-HAP feature) + class HAPTLV : public TLV8 { // dedicated class for HAP TLV8 records public: HAPTLV() : TLV8(HAP_Names,11){} @@ -198,6 +199,7 @@ class HapOut : public std::ostream { size_t indent=0; uint8_t *hash; mbedtls_sha512_context *ctx; + void (*callBack)(const char *)=NULL; void flushBuffer(); int_type overflow(int_type c) override; @@ -219,6 +221,7 @@ class HapOut : public std::ostream { HapOut& setHapClient(HAPClient *hapClient){hapBuffer.hapClient=hapClient;return(*this);} HapOut& setLogLevel(int logLevel){hapBuffer.logLevel=logLevel;return(*this);} HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;hapBuffer.logLevel=0;return(*this);} + HapOut& setCallback(void(*f)(const char *)){hapBuffer.callBack=f;return(*this);} uint8_t *getHash(){return(hapBuffer.hash);} size_t getSize(){return(hapBuffer.getSize());} diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 2d746cd..5efb6fd 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -451,8 +451,6 @@ void Span::checkConnect(){ id[17]='\0'; // add terminating null // create broadcast name from server base name plus accessory ID (without ':') - - char *hostName; if(!hostNameSuffix) asprintf(&hostName,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15); @@ -553,9 +551,6 @@ void Span::checkConnect(){ if(wifiCallbackAll) wifiCallbackAll((connected+1)/2); - - - free(hostName); } // initWiFi @@ -583,8 +578,10 @@ void Span::processSerialCommand(const char *c){ LOG0("\n*** HomeSpan Status ***\n\n"); - LOG0("IP Address: %s\n\n",WiFi.localIP().toString().c_str()); - LOG0("Accessory ID: "); + LOG0("IP Address: %s\n",WiFi.localIP().toString().c_str()); + if(webLog.isEnabled && hostName!=NULL) + LOG0("Web Logging: http://%s.local:%d%s\n",hostName,tcpPortNum,webLog.statusURL.c_str()+4); + LOG0("\nAccessory ID: "); HAPClient::charPrintRow(HAPClient::accessory.ID,17); LOG0(" LTPK: "); HAPClient::hexPrintRow(HAPClient::accessory.LTPK,32); @@ -1146,6 +1143,12 @@ void Span::processSerialCommand(const char *c){ /////////////////////////////// +void Span::getWebLog(void (*f)(const char *)){ + HAPClient::getStatusURL(NULL,f); +} + +/////////////////////////////// + void Span::resetStatus(){ if(strlen(network.wifiData.ssid)==0) STATUS_UPDATE(start(LED_WIFI_NEEDED),HS_WIFI_NEEDED) diff --git a/src/HomeSpan.h b/src/HomeSpan.h index edf367c..c08ba0f 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -222,6 +222,7 @@ class Span{ const char *displayName; // display name for this device - broadcast as part of Bonjour MDNS const char *hostNameBase; // base of hostName of this device - full host name broadcast by Bonjour MDNS will have 6-byte accessoryID as well as '.local' automatically appended const char *hostNameSuffix=NULL; // optional "suffix" of hostName of this device. If specified, will be used as the hostName suffix instead of the 6-byte accessoryID + char *hostName=NULL; // derived full hostname const char *modelName; // model name of this device - broadcast as Bonjour field "md" char category[3]=""; // category ID of primary accessory - broadcast as Bonjour field "ci" (HAP Section 13) unsigned long snapTime; // current time (in millis) snapped before entering Service loops() or updates() @@ -371,7 +372,8 @@ class Span{ } Span& setWebLogCSS(const char *css){webLog.css="\n" + String(css) + "\n";return(*this);} - Span& setWebLogCallback(void (*f)(String &)){weblogCallback=f;return(*this);} + Span& setWebLogCallback(void (*f)(String &)){weblogCallback=f;return(*this);} + void getWebLog(void (*f)(const char *)); Span& setVerboseWifiReconnect(bool verbose=true){verboseWifiReconnect=verbose;return(*this);} diff --git a/src/src.ino b/src/src.ino index 08c4290..9105778 100644 --- a/src/src.ino +++ b/src/src.ino @@ -27,14 +27,14 @@ #include "HomeSpan.h" -#define MAX_LIGHTS 4 +#define MAX_LIGHTS 2 void setup() { Serial.begin(115200); homeSpan.setLogLevel(2); - homeSpan.enableWebLog(500); + homeSpan.enableWebLog(50); homeSpan.begin(Category::Lighting,"HomeSpan Max"); @@ -51,11 +51,13 @@ void setup() { new Characteristic::Name(c); new Service::LightBulb(); new Characteristic::On(0,false); - new Characteristic::Brightness(50,false); - new Characteristic::Hue(120,false); - new Characteristic::Saturation(100,false); +// new Characteristic::Brightness(50,false); +// new Characteristic::Hue(120,false); +// new Characteristic::Saturation(100,false); } + new SpanUserCommand('w', " - get web log test",webLogTest); + } ////////////////////////////////////// @@ -67,3 +69,13 @@ void loop(){ } ////////////////////////////////////// + +void webLogTest(const char *dummy){ + Serial.printf("\n*** In Web Log Test. Starting Custom Web Log Handler\n"); + homeSpan.getWebLog(webLogHandler); + Serial.printf("\n**** Done!"); +} + +void webLogHandler(const char *buf){ + Serial.print("Here I am\n"); +}