From d849595bfe22b3b5955970522d602efa15c22d80 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 5 Dec 2020 13:50:24 -0600 Subject: [PATCH] More updates to Accessory ID * if Accessory ID is NOT specified, the default is aid=1+aid of last Accessory, with aid of very first Accessory always set to 1. * aid of first Accessory must ALWAYS be set to 1 - if user over-rides with another value, error will be thrown * validation now includes checking for duplicate aids, as well as ensuring the first Accessory is always aid=1 --- src/HomeSpan.cpp | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index ed3d9f0..5869e3d 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1053,21 +1053,39 @@ int Span::sprintfAttributes(char **ids, int numIDs, int flags, char *cBuf){ SpanAccessory::SpanAccessory(uint32_t aid){ if(!homeSpan.Accessories.empty()){ + this->aid=homeSpan.Accessories.back()->aid+1; if(!homeSpan.Accessories.back()->Services.empty()) homeSpan.Accessories.back()->Services.back()->validate(); homeSpan.Accessories.back()->validate(); + } else { + this->aid=1; } homeSpan.Accessories.push_back(this); - - if(aid==0) - this->aid=homeSpan.Accessories.size(); - else - this->aid=aid; - homeSpan.configLog+="+Accessory-" + String(this->aid) + "\n"; + if(aid>0){ // override with user-specified aid + this->aid=aid; + } + + homeSpan.configLog+="+Accessory-" + String(this->aid); + + for(int i=0;iaid==homeSpan.Accessories[i]->aid){ + homeSpan.configLog+=" *** ERROR! ID already in use for another Accessory. ***"; + homeSpan.nFatalErrors++; + break; + } + } + + if(homeSpan.Accessories.size()==1 && this->aid!=1){ + homeSpan.configLog+=" *** ERROR! ID of first Accessory must always be 1. ***"; + homeSpan.nFatalErrors++; + } + + homeSpan.configLog+="\n"; + } /////////////////////////////// @@ -1082,8 +1100,8 @@ void SpanAccessory::validate(){ foundInfo=true; else if(!strcmp(Services[i]->type,"A2")) foundProtocol=true; - else if(aid==1) // this is the first Accessory and it has more than just AccessoryInfo and HAPProtocolInformation - homeSpan.isBridge=false; // this is not a bridge device + else if(aid==1) // this is an Accessory with aid=1, but it has more than just AccessoryInfo and HAPProtocolInformation. So... + homeSpan.isBridge=false; // ...this is not a bridge device } if(!foundInfo){ @@ -1092,7 +1110,7 @@ void SpanAccessory::validate(){ homeSpan.nFatalErrors++; } - if(!foundProtocol && (aid==1 || !homeSpan.isBridge)){ // HAPProtocolInformation must always be present in first Accessory, and any other Accessory is the device is not a bridge) + if(!foundProtocol && (aid==1 || !homeSpan.isBridge)){ // HAPProtocolInformation must always be present in Accessory if aid=1, and any other Accessory if the device is not a bridge) homeSpan.configLog+=" !Service HAPProtocolInformation"; homeSpan.configLog+=" *** ERROR! Required Service for this Accessory not found. ***\n"; homeSpan.nFatalErrors++;