From d78abdaf6dd5d16123424103bf29be4e17a9c04b Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 11 Feb 2024 18:12:19 -0600 Subject: [PATCH] Small clean-up and formatting of comments --- src/HAP.cpp | 77 +++++++++++++++++++++++------------------------------ src/src.ino | 11 +++++--- 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 571bbcc..fad74f0 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -122,7 +122,7 @@ void HAPClient::processRequest(){ messageSize=client.available(); - if(messageSize>MAX_HTTP){ // exceeded maximum number of bytes allowed + if(messageSize>MAX_HTTP){ // exceeded maximum number of bytes allowed badRequestError(); LOG0("\n*** ERROR: HTTP message of %d bytes exceeds maximum allowed (%d)\n\n",messageSize,MAX_HTTP); return; @@ -130,24 +130,24 @@ void HAPClient::processRequest(){ TempBuffer httpBuf(messageSize+1); // leave room for null character added below - if(cPair){ // expecting encrypted message + if(cPair){ // expecting encrypted message LOG2("<<<< #### "); LOG2(client.remoteIP()); LOG2(" #### <<<<\n"); - nBytes=receiveEncrypted(httpBuf,messageSize); // decrypt and return number of bytes read + nBytes=receiveEncrypted(httpBuf,messageSize); // decrypt and return number of bytes read - if(!nBytes){ // decryption failed (error message already printed in function) + if(!nBytes){ // decryption failed (error message already printed in function) badRequestError(); return; } - } else { // expecting plaintext message + } else { // expecting plaintext message LOG2("<<<<<<<<< "); LOG2(client.remoteIP()); LOG2(" <<<<<<<<<\n"); - nBytes=client.read(httpBuf,messageSize); // read expected number of bytes + nBytes=client.read(httpBuf,messageSize); // read expected number of bytes if(nBytes!=messageSize || client.available()!=0){ badRequestError(); @@ -183,7 +183,7 @@ void HAPClient::processRequest(){ LOG2(body); LOG2("\n------------ END BODY! ------------\n"); - if(!strncmp(body,"POST ",5)){ // this is a POST request + if(!strncmp(body,"POST ",5)){ // this is a POST request if(cLen==0){ badRequestError(); @@ -207,61 +207,48 @@ void HAPClient::processRequest(){ return; } // POST request - if(!strncmp(body,"PUT ",4)){ // this is a PUT request + if(!strncmp(body,"PUT ",4)){ // this is a PUT request if(cLen==0){ badRequestError(); LOG0("\n*** ERROR: HTTP PUT request contains no Content\n\n"); - return; + return; } + + LOG2((char *)content); + LOG2("\n------------ END JSON! ------------\n"); - if(!strncmp(body,"PUT /characteristics ",21) && // PUT CHARACTERISTICS - strstr(body,"Content-Type: application/hap+json")){ // check that content is JSON + if(!strncmp(body,"PUT /characteristics ",21) && strstr(body,"Content-Type: application/hap+json")) // PUT CHARACTERISTICS + putCharacteristicsURL((char *)content); - content[cLen]='\0'; // add a trailing null on end of JSON - LOG2((char *)content); // print JSON - LOG2("\n------------ END JSON! ------------\n"); - - putCharacteristicsURL((char *)content); // process URL - return; + else if(!strncmp(body,"PUT /prepare ",13) && strstr(body,"Content-Type: application/hap+json")) // PUT PREPARE + putPrepareURL((char *)content); + + else { + notFoundError(); + LOG0("\n*** ERROR: Bad PUT request - URL not found\n\n"); } - - if(!strncmp(body,"PUT /prepare ",13) && // PUT PREPARE - strstr(body,"Content-Type: application/hap+json")){ // check that content is JSON - - content[cLen]='\0'; // add a trailing null on end of JSON - LOG2((char *)content); // print JSON - LOG2("\n------------ END JSON! ------------\n"); - - putPrepareURL((char *)content); // process URL - return; - } - - notFoundError(); - LOG0("\n*** ERROR: Bad PUT request - URL not found\n\n"); + return; } // PUT request - if(!strncmp(body,"GET ",4)){ // this is a GET request + if(!strncmp(body,"GET ",4)){ // this is a GET request - if(!strncmp(body,"GET /accessories ",17)){ // GET ACCESSORIES + if(!strncmp(body,"GET /accessories ",17)) // GET ACCESSORIES getAccessoriesURL(); - return; - } - if(!strncmp(body,"GET /characteristics?",21)){ // GET CHARACTERISTICS + else if(!strncmp(body,"GET /characteristics?",21)) // GET CHARACTERISTICS getCharacteristicsURL(body+21); - return; - } - if(homeSpan.webLog.isEnabled && !strncmp(body,homeSpan.webLog.statusURL.c_str(),homeSpan.webLog.statusURL.length())){ // GET STATUS - AN OPTIONAL, NON-HAP-R2 FEATURE + else if(homeSpan.webLog.isEnabled && !strncmp(body,homeSpan.webLog.statusURL.c_str(),homeSpan.webLog.statusURL.length())) // GET STATUS - AN OPTIONAL, NON-HAP-R2 FEATURE getStatusURL(this,NULL,NULL); - return; - } - notFoundError(); - LOG0("\n*** ERROR: Bad GET request - URL not found\n\n"); + else { + notFoundError(); + LOG0("\n*** ERROR: Bad GET request - URL not found\n\n"); + } + return; } // GET request @@ -1121,7 +1108,9 @@ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char * hapOut.setHapClient(hapClient).setLogLevel(2).setCallback(callBack).setCallbackUserData(user_data); - if(!callBack) hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n"; + if(!callBack) + hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n"; + hapOut << "" << homeSpan.displayName << "\n"; hapOut << "\n"; hapOut << "

" << homeSpan.displayName << "

\n"; diff --git a/src/src.ino b/src/src.ino index bc8e539..43de092 100644 --- a/src/src.ino +++ b/src/src.ino @@ -34,7 +34,7 @@ void setup() { Serial.begin(115200); homeSpan.setLogLevel(1); -// homeSpan.enableWebLog(50,"pool.ntp.org","UTC",NULL); + homeSpan.enableWebLog(50,"pool.ntp.org","UTC",NULL); // homeSpan.enableWebLog(50,"pool.ntp.org","UTC","myStatus"); // homeSpan.enableWebLog(50,NULL,NULL,NULL); @@ -72,12 +72,15 @@ void loop(){ void webLogTest(const char *dummy){ Serial.printf("\n*** In Web Log Test. Starting Custom Web Log Handler\n"); // here is where you would perform any HTTPS initializations - homeSpan.getWebLog(webLogHandler); // this starts the normal weblog with output redirected to the specified handler (below) + homeSpan.getWebLog(webLogHandler,NULL); // this starts the normal weblog with output redirected to the specified handler (below) } -void webLogHandler(const char *buf){ - if(buf!=NULL) +void webLogHandler(const char *buf, void *args){ + if(buf!=NULL){ + Serial.printf("--------\n"); Serial.printf("%s",buf); // here is where you would transmit data to the HTTPS connection + Serial.printf("********\n"); + } else Serial.print("*** DONE!\n\n"); // here is where you would close the HTTPS connection }