From 47e7f7b6b41a9c092dc61ef71853e24e4f1999b9 Mon Sep 17 00:00:00 2001 From: Gregg Date: Fri, 24 Jul 2020 21:54:09 -0500 Subject: [PATCH] Converted StatusCode to Class Converted "statusCode Enum" to "StatusCode Class Enum" and moved from HomeSpan.h to Settings.h --- examples/Intermediate/5-WorkingLED/DEV_LED.h | 6 +-- src/HAP.cpp | 4 +- src/HomeSpan.cpp | 54 ++++++++++---------- src/HomeSpan.h | 18 ++----- src/Settings.h | 16 ++++++ 5 files changed, 51 insertions(+), 47 deletions(-) diff --git a/examples/Intermediate/5-WorkingLED/DEV_LED.h b/examples/Intermediate/5-WorkingLED/DEV_LED.h index b45f1d4..66d0420 100644 --- a/examples/Intermediate/5-WorkingLED/DEV_LED.h +++ b/examples/Intermediate/5-WorkingLED/DEV_LED.h @@ -21,13 +21,13 @@ struct DEV_LED : Service::LightBulb { // First we create a derived } // end constructor - // Finally, we over-ride the default update() method with instructions that actually turn on/off the LED. Note update() returns type "statusCode" + // Finally, we over-ride the default update() method with instructions that actually turn on/off the LED. Note update() returns type "StatusCode" - statusCode update(){ + StatusCode update(){ digitalWrite(ledPin,power->newValue.BOOL); // use a standard Arduino function to turn on/off ledPin based on the boolean variable power->newValue.BOOL - return(SC_OK); // return OKAY status code. There are other possibilties we will explore in later examples. + return(StatusCode::OK); // return OK status code. There are other possibilties we will explore in later examples. } // update }; diff --git a/src/HAP.cpp b/src/HAP.cpp index 225ff51..a78f39c 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1051,7 +1051,7 @@ int HAPClient::putCharacteristicsURL(char *json){ int multiCast=0; // check if all status is OK, or if multicast response is request for(int i=0;itrigger){ // characteristic is triggered pb->characteristic->value.BOOL=false; // turn off characteristic - pObj[n].status=SC_OK; // populate pObj + pObj[n].status=StatusCode::OK; // populate pObj pObj[n].characteristic=pb->characteristic; pObj[n].val=""; // dummy object needed to ensure sprintfNotify knows to consider this "update" n++; // increment number of Push Buttons found that need to be turned off diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index a0486df..7372140 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -555,17 +555,17 @@ int Span::updateCharacteristics(char *buf, SpanPut *pObj){ pObj[i].characteristic = find(pObj[i].aid,pObj[i].iid); // find characteristic with matching aid/iid and store pointer if(pObj[i].characteristic) // if found, initialize characterstic update with new val/ev - pObj[i].status=pObj[i].characteristic->loadUpdate(pObj[i].val,pObj[i].ev); // save status code, which is either an error, or SC_TBD (in which case isUpdated for the characteristic has been set to true) + pObj[i].status=pObj[i].characteristic->loadUpdate(pObj[i].val,pObj[i].ev); // save status code, which is either an error, or TBD (in which case isUpdated for the characteristic has been set to true) else - pObj[i].status=SC_UnknownResource; // if not found, set HAP error + pObj[i].status=StatusCode::UnknownResource; // if not found, set HAP error } // first pass for(int i=0;iservice; // set service containing the characteristic underlying the object - statusCode status=svc->update(); // update service and save returned statusCode + StatusCode status=svc->update(); // update service and save returned statusCode for(int j=i;jservice==svc){ // if service matches @@ -574,7 +574,7 @@ int Span::updateCharacteristics(char *buf, SpanPut *pObj){ LOG1(svc->Characteristics[j]->aid); LOG1(" iid="); LOG1(svc->Characteristics[j]->iid); - if(status==SC_OK){ // if status is okay + if(status==StatusCode::OK){ // if status is okay pObj[j].characteristic->value =pObj[j].characteristic->newValue; // update characteristic value with new value LOG1(" (okay)\n"); @@ -587,7 +587,7 @@ int Span::updateCharacteristics(char *buf, SpanPut *pObj){ } } - } // object had SC_TBD status + } // object had TBD status } // loop over all objects return(1); @@ -616,7 +616,7 @@ int Span::sprintfNotify(SpanPut *pObj, int nObj, char *cBuf, int conNum, int &nu for(int i=0;iev[conNum]){ // if notifications requested for this characteristic by specified connection number @@ -662,24 +662,24 @@ int Span::sprintfAttributes(char **ids, int numIDs, int flags, char *cBuf){ int aid, iid; SpanCharacteristic *Characteristics[numIDs]; - int status[numIDs]; + StatusCode status[numIDs]; boolean sFlag=false; - for(int i=0;iperms&SpanCharacteristic::PR){ // if permissions allow reading - status[i]=0; + status[i]=StatusCode::OK; // always set status to OK (since no actual reading of device is needed) } else { Characteristics[i]=NULL; - status[i]=SC_WriteOnly; - sFlag=true; + status[i]=StatusCode::WriteOnly; + sFlag=true; // set flag indicating there was an error } } else { - status[i]=SC_UnknownResource; - sFlag=true; + status[i]=StatusCode::UnknownResource; + sFlag=true; // set flag indicating there was an error } } @@ -963,7 +963,7 @@ int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){ /////////////////////////////// -statusCode SpanCharacteristic::loadUpdate(char *val, char *ev){ +StatusCode SpanCharacteristic::loadUpdate(char *val, char *ev){ if(ev){ // request for notification boolean evFlag; @@ -973,10 +973,10 @@ statusCode SpanCharacteristic::loadUpdate(char *val, char *ev){ else if(!strcmp(ev,"1") || !strcmp(ev,"true")) evFlag=true; else - return(SC_InvalidValue); + return(StatusCode::InvalidValue); if(evFlag && !(perms&EV)) // notification is not supported for characteristic - return(SC_NotifyNotAllowed); + return(StatusCode::NotifyNotAllowed); LOG1("Notification Request for aid="); LOG1(aid); @@ -989,10 +989,10 @@ statusCode SpanCharacteristic::loadUpdate(char *val, char *ev){ } if(!val) // no request to update value - return(SC_OK); + return(StatusCode::OK); if(!(perms&PW)) // cannot write to read only characteristic - return(SC_ReadOnly); + return(StatusCode::ReadOnly); switch(format){ @@ -1002,43 +1002,43 @@ statusCode SpanCharacteristic::loadUpdate(char *val, char *ev){ else if(!strcmp(val,"1") || !strcmp(val,"true")) newValue.BOOL=true; else - return(SC_InvalidValue); + return(StatusCode::InvalidValue); break; case INT: if(!sscanf(val,"%d",&newValue.INT)) - return(SC_InvalidValue); + return(StatusCode::InvalidValue); break; case UINT8: if(!sscanf(val,"%u",&newValue.UINT8)) - return(SC_InvalidValue); + return(StatusCode::InvalidValue); break; case UINT16: if(!sscanf(val,"%u",&newValue.UINT16)) - return(SC_InvalidValue); + return(StatusCode::InvalidValue); break; case UINT32: if(!sscanf(val,"%llu",&newValue.UINT32)) - return(SC_InvalidValue); + return(StatusCode::InvalidValue); break; case UINT64: if(!sscanf(val,"%llu",&newValue.UINT64)) - return(SC_InvalidValue); + return(StatusCode::InvalidValue); break; case FLOAT: if(!sscanf(val,"%lg",&newValue.FLOAT)) - return(SC_InvalidValue); + return(StatusCode::InvalidValue); break; } // switch isUpdated=true; - return(SC_TBD); + return(StatusCode::TBD); } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index d960cc3..62f677d 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -5,18 +5,6 @@ using std::vector; -enum statusCode { // HAP Table 6-11 - SC_OK=0, - SC_Unable=-70402, - SC_Busy=-70403, - SC_ReadOnly=-70404, - SC_WriteOnly=-70405, - SC_NotifyNotAllowed=-70406, - SC_UnknownResource=-70409, - SC_InvalidValue=-70410, - SC_TBD=-1 // status To-Be-Determined (TBD) once service.update() called -}; - enum { GET_AID=1, GET_META=2, @@ -111,7 +99,7 @@ struct SpanService{ SpanService(const char *type, ServiceType mod=ServiceType::Regular); int sprintfAttributes(char *cBuf); // prints Service JSON records into buf; return number of characters printed, excluding null terminator - virtual statusCode update() {return(SC_OK);} // update Service and return final statusCode based on updated Characteristics - should be overridden by DEVICE-SPECIFIC Services + virtual StatusCode update() {return(StatusCode::OK);} // update Service and return final statusCode based on updated Characteristics - should be overridden by DEVICE-SPECIFIC Services }; /////////////////////////////// @@ -175,7 +163,7 @@ struct SpanCharacteristic{ SpanCharacteristic(char *type, uint8_t perms, const char* value); int sprintfAttributes(char *cBuf, int flags); // prints Characteristic JSON records into buf, according to flags mask; return number of characters printed, excluding null terminator - statusCode loadUpdate(char *val, char *ev); // load updated val/ev from PUT /characteristic JSON request. Return intiial HAP status code (checks to see if characteristic is found, is writable, etc.) + StatusCode loadUpdate(char *val, char *ev); // load updated val/ev from PUT /characteristic JSON request. Return intiial HAP status code (checks to see if characteristic is found, is writable, etc.) void autoOff(int waitTime=250); // turns Characteristic off (false) automatically after waitTime milliseconds; only applicable to BOOL characteristics }; @@ -196,7 +184,7 @@ struct SpanPut{ // storage to process PUT /charact int iid; // iid to update char *val=NULL; // updated value (optional, though either at least 'val' or 'ev' must be specified) char *ev=NULL; // updated event notification flag (optional, though either at least 'val' or 'ev' must be specified) - statusCode status; // return status (HAP Table 6-11) + StatusCode status; // return status (HAP Table 6-11) SpanCharacteristic *characteristic=NULL; // Characteristic to update (NULL if not found) }; diff --git a/src/Settings.h b/src/Settings.h index f576ee6..80e164a 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -70,3 +70,19 @@ enum class Category { Faucets=29, ShowerSystems=30 }; + +////////////////////////////////////////////////////// +// Types of Status Codes for use with update() // +// Reference: HAP Table 6-11 // + +enum class StatusCode { + OK=0, + Unable=-70402, + Busy=-70403, + ReadOnly=-70404, + WriteOnly=-70405, + NotifyNotAllowed=-70406, + UnknownResource=-70409, + InvalidValue=-70410, + TBD=-1 // status To-Be-Determined (TBD) once service.update() called +};