From 471d62f3c2b8c30aa701e1bbd2aafbc877f7ba73 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 9 Aug 2020 09:25:10 -0500 Subject: [PATCH] created standalone eventNotify function putCharacteristics and checkNotifications now both call the same eventNotify routing. This is being done to prepare for services that will stream notifications back to client (like a temperature sensor, or pushbutton). TO DO: rename checkNotifications to check TimedResets, and rename SpanPut to something more generic. --- src/HAP.cpp | 40 ++++++++++++++++++++++++---------------- src/HAP.h | 2 ++ src/HomeSpan.cpp | 13 +++++++------ src/HomeSpan.h | 4 ++-- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 80a898c..ead932d 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -2,7 +2,7 @@ #include #include -#include "HomeSpan.h" +//#include "HomeSpan.h" #include "HAP.h" ////////////////////////////////////// @@ -1084,16 +1084,17 @@ int HAPClient::putCharacteristicsURL(char *json){ // Create and send Event Notifications if needed + eventNotify(pObj,n,HAPClient::conNum); // transmit EVENT Notification for "n" pObj objects, except DO NOT notify client making request + +/* for(int i=0;i0){ char jsonBuf[nBytes+1]; - homeSpan.sprintfNotify(pObj,n,jsonBuf,i,numNotify); + homeSpan.sprintfNotify(pObj,n,jsonBuf,i); int nChars=snprintf(NULL,0,"EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",nBytes); // create Body with Content Length = size of JSON Buf char body[nChars+1]; @@ -1111,6 +1112,7 @@ int HAPClient::putCharacteristicsURL(char *json){ } // if there are characteristic updates to notify } // if client exists } +*/ return(1); } @@ -1163,31 +1165,37 @@ void HAPClient::checkNotifications(){ } } - for(int i=0;i0){ // if there are notifications to send to client cNum char jsonBuf[nBytes+1]; - homeSpan.sprintfNotify(pObj,n,jsonBuf,i,numNotify); + homeSpan.sprintfNotify(pObj,nObj,jsonBuf,cNum); int nChars=snprintf(NULL,0,"EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",nBytes); // create Body with Content Length = size of JSON Buf char body[nChars+1]; sprintf(body,"EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",nBytes); LOG2("\n>>>>>>>>>> "); - LOG2(hap[i].client.remoteIP()); + LOG2(hap[cNum].client.remoteIP()); LOG2(" >>>>>>>>>>\n"); LOG2(body); LOG2(jsonBuf); LOG2("\n"); - hap[i].sendEncrypted(body,(uint8_t *)jsonBuf,nBytes); // note recasting of jsonBuf into uint8_t* + hap[cNum].sendEncrypted(body,(uint8_t *)jsonBuf,nBytes); // note recasting of jsonBuf into uint8_t* - } // if there are characteristic updates to notify + } // if there are characteristic updates to notify client cNum } // if client exists } diff --git a/src/HAP.h b/src/HAP.h index 7c3d6d9..f98e7cd 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -6,6 +6,7 @@ #include "HAPConstants.h" #include "HKDF.h" #include "SRP.h" +#include "HomeSpan.h" ///////////////////////////////////////////////// // NONCE Structure (HAP used last 64 of 96 bits) @@ -111,6 +112,7 @@ struct HAPClient { static void removeController(uint8_t *id); // removes specific Controller. If no remaining admin Controllers, remove all others (if any) as per HAP requirements. static void printControllers(); // prints IDs of all allocated (paired) Controller static void checkNotifications(); // checks for notifications and reports to controllers as needed (HAP Section 6.8) + static void eventNotify(SpanPut *pObj, int nObj, int ignoreClient=-1); // transmits EVENT Notifications for nObj SpanPut objects, pObj, with optional flag to ignore a specific client }; diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 9437b07..d57cac2 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -5,7 +5,7 @@ #include "Utils.h" #include "HAP.h" -#include "HomeSpan.h" +//#include "HomeSpan.h" using namespace Utils; @@ -620,9 +620,10 @@ void Span::clearNotify(int slotNum){ /////////////////////////////// -int Span::sprintfNotify(SpanPut *pObj, int nObj, char *cBuf, int conNum, int &numNotify){ +int Span::sprintfNotify(SpanPut *pObj, int nObj, char *cBuf, int conNum){ int nChars=0; + boolean notifyFlag=false; nChars+=snprintf(cBuf,cBuf?64:0,"{\"characteristics\":["); @@ -632,11 +633,11 @@ int Span::sprintfNotify(SpanPut *pObj, int nObj, char *cBuf, int conNum, int &nu if(pObj[i].characteristic->ev[conNum]){ // if notifications requested for this characteristic by specified connection number - if(numNotify>0) // already printed at least one other characteristic - nChars+=snprintf(cBuf?(cBuf+nChars):NULL,cBuf?64:0,","); + if(notifyFlag) // already printed at least one other characteristic + nChars+=snprintf(cBuf?(cBuf+nChars):NULL,cBuf?64:0,","); // add preceeding comma before printing next characteristic nChars+=pObj[i].characteristic->sprintfAttributes(cBuf?(cBuf+nChars):NULL,GET_AID); // get JSON attributes for characteristic - numNotify++; + notifyFlag=true; } // notification requested } // characteristic updated @@ -644,7 +645,7 @@ int Span::sprintfNotify(SpanPut *pObj, int nObj, char *cBuf, int conNum, int &nu nChars+=snprintf(cBuf?(cBuf+nChars):NULL,cBuf?64:0,"]}"); - return(nChars); + return(notifyFlag?nChars:0); // if notifyFlag is not set, return 0, else return number of characters printed to cBuf } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 680cdf6..1e48912 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -66,8 +66,8 @@ struct Span{ int sprintfAttributes(SpanPut *pObj, int nObj, char *cBuf); // prints SpanPut object into buf, unless buf=NULL; return number of characters printed, excluding null terminator, even if buf=NULL int sprintfAttributes(char **ids, int numIDs, int flags, char *cBuf); // prints accessory.characteristic ids into buf, unless buf=NULL; return number of characters printed, excluding null terminator, even if buf=NULL - void clearNotify(int slotNum); // set ev notification flags for connection 'slotNum' to false across all characteristics - int sprintfNotify(SpanPut *pObj, int nObj, char *cBuf, int conNum, int &numNotify); // prints notification JSON into buf based on SpanPut objects and specified connection number + void clearNotify(int slotNum); // set ev notification flags for connection 'slotNum' to false across all characteristics + int sprintfNotify(SpanPut *pObj, int nObj, char *cBuf, int conNum); // prints notification JSON into buf based on SpanPut objects and specified connection number void setResetPin(int pin){resetPin=pin;} // sets new pin to be used for factory reset