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.
This commit is contained in:
Gregg 2020-08-09 09:25:10 -05:00
parent d8a8eedad4
commit 471d62f3c2
4 changed files with 35 additions and 24 deletions

View File

@ -2,7 +2,7 @@
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include <sodium.h> #include <sodium.h>
#include "HomeSpan.h" //#include "HomeSpan.h"
#include "HAP.h" #include "HAP.h"
////////////////////////////////////// //////////////////////////////////////
@ -1084,16 +1084,17 @@ int HAPClient::putCharacteristicsURL(char *json){
// Create and send Event Notifications if needed // 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;i<MAX_CONNECTIONS;i++){ // loop over all connection slots for(int i=0;i<MAX_CONNECTIONS;i++){ // loop over all connection slots
if(hap[i].client && i!=HAPClient::conNum){ // if there is a client connected to this slot and it is NOT the current client requesting this update if(hap[i].client && i!=HAPClient::conNum){ // if there is a client connected to this slot and it is NOT the current client requesting this update
int numNotify=0; int nBytes=homeSpan.sprintfNotify(pObj,n,NULL,i); // get JSON response - includes terminating null (will be recast to uint8_t* below)
int nBytes=homeSpan.sprintfNotify(pObj,n,NULL,i,numNotify); // get JSON response - includes terminating null (will be recast to uint8_t* below)
if(numNotify){ if(nBytes>0){
numNotify=0;
char jsonBuf[nBytes+1]; 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 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]; char body[nChars+1];
@ -1111,6 +1112,7 @@ int HAPClient::putCharacteristicsURL(char *json){
} // if there are characteristic updates to notify } // if there are characteristic updates to notify
} // if client exists } // if client exists
} }
*/
return(1); return(1);
} }
@ -1163,31 +1165,37 @@ void HAPClient::checkNotifications(){
} }
} }
for(int i=0;i<MAX_CONNECTIONS;i++){ // loop over all connection slots eventNotify(pObj,n); // transmit EVENT Notification for "n" pObj objects
if(hap[i].client){ // if there is a client connected to this slot
int numNotify=0; }
int nBytes=homeSpan.sprintfNotify(pObj,n,NULL,i,numNotify); // get JSON response - includes terminating null (will be recast to uint8_t* below)
if(numNotify){ //////////////////////////////////////
numNotify=0;
void HAPClient::eventNotify(SpanPut *pObj, int nObj, int ignoreClient){
for(int cNum=0;cNum<MAX_CONNECTIONS;cNum++){ // loop over all connection slots
if(hap[cNum].client && cNum!=ignoreClient){ // if there is a client connected to this slot and it is NOT flagged to be ignored (in cases where it is the client making a PUT request
int nBytes=homeSpan.sprintfNotify(pObj,nObj,NULL,cNum); // get JSON response for notifications to client cNum - includes terminating null (will be recast to uint8_t* below)
if(nBytes>0){ // if there are notifications to send to client cNum
char jsonBuf[nBytes+1]; 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 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]; 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); 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("\n>>>>>>>>>> ");
LOG2(hap[i].client.remoteIP()); LOG2(hap[cNum].client.remoteIP());
LOG2(" >>>>>>>>>>\n"); LOG2(" >>>>>>>>>>\n");
LOG2(body); LOG2(body);
LOG2(jsonBuf); LOG2(jsonBuf);
LOG2("\n"); 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 } // if client exists
} }

View File

@ -6,6 +6,7 @@
#include "HAPConstants.h" #include "HAPConstants.h"
#include "HKDF.h" #include "HKDF.h"
#include "SRP.h" #include "SRP.h"
#include "HomeSpan.h"
///////////////////////////////////////////////// /////////////////////////////////////////////////
// NONCE Structure (HAP used last 64 of 96 bits) // 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 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 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 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
}; };

View File

@ -5,7 +5,7 @@
#include "Utils.h" #include "Utils.h"
#include "HAP.h" #include "HAP.h"
#include "HomeSpan.h" //#include "HomeSpan.h"
using namespace Utils; 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; int nChars=0;
boolean notifyFlag=false;
nChars+=snprintf(cBuf,cBuf?64:0,"{\"characteristics\":["); 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(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 if(notifyFlag) // already printed at least one other characteristic
nChars+=snprintf(cBuf?(cBuf+nChars):NULL,cBuf?64:0,","); 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 nChars+=pObj[i].characteristic->sprintfAttributes(cBuf?(cBuf+nChars):NULL,GET_AID); // get JSON attributes for characteristic
numNotify++; notifyFlag=true;
} // notification requested } // notification requested
} // characteristic updated } // 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,"]}"); 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
} }
/////////////////////////////// ///////////////////////////////

View File

@ -67,7 +67,7 @@ struct Span{
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 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 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 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 void setResetPin(int pin){resetPin=pin;} // sets new pin to be used for factory reset