Converted StatusCode to Class

Converted "statusCode Enum" to "StatusCode Class Enum" and moved from HomeSpan.h to Settings.h
This commit is contained in:
Gregg 2020-07-24 21:54:09 -05:00
parent f3f8996c8a
commit 47e7f7b6b4
5 changed files with 51 additions and 47 deletions

View File

@ -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
};

View File

@ -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;i<n;i++)
if(pObj[i].status)
if(pObj[i].status!=StatusCode::OK)
multiCast=1;
if(!multiCast){ // JSON object has no content
@ -1156,7 +1156,7 @@ void HAPClient::checkNotifications(){
while(pb){ // PASS 2: loop through all characteristics registered as Push Buttons
if(pb->trigger){ // 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

View File

@ -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;i<nObj;i++){ // PASS 2: loop again over all objects
if(pObj[i].status==SC_TBD){ // if object status still TBD
if(pObj[i].status==StatusCode::TBD){ // if object status still TBD
SpanService *svc=pObj[i].characteristic->service; // 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;j<nObj;j++){ // loop over this object plus any remaining objects to update values and save status for any other characteristics in this service
if(pObj[j].characteristic->service==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;i<nObj;i++){ // loop over all objects
if(pObj[i].status==SC_OK && pObj[i].val){ // characteristic was successfully updated with a new value (i.e. not just an EV request)
if(pObj[i].status==StatusCode::OK && pObj[i].val){ // characteristic was successfully updated with a new value (i.e. not just an EV request)
if(pObj[i].characteristic->ev[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;i<numIDs;i++){ // PASS 1: loop over all ids requested to check status codes, which are only included if there is at least one error
for(int i=0;i<numIDs;i++){ // PASS 1: loop over all ids requested to check status codes - only errors are if characteristic not found, or not readable
sscanf(ids[i],"%d.%d",&aid,&iid); // parse aid and iid
Characteristics[i]=find(aid,iid); // find matching chararacteristic
if(Characteristics[i]){ // if found
if(Characteristics[i]->perms&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);
}
///////////////////////////////

View File

@ -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)
};

View File

@ -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
};