Added new method homeSpan.setHostNameSuffix()

Allows user to replace the 6-byte AccessoryID "suffix" in the hostName with a custom suffix, included a blank string.  To do: Add to documentation.
This commit is contained in:
Gregg 2021-01-21 08:07:57 -06:00
parent b4df9578ca
commit b87a3d1dc3
3 changed files with 18 additions and 4 deletions

View File

@ -362,10 +362,20 @@ void Span::checkConnect(){
id[17]='\0'; // add terminating null id[17]='\0'; // add terminating null
// create broadcaset name from server base name plus accessory ID (without ':') // create broadcaset name from server base name plus accessory ID (without ':')
int nChars=snprintf(NULL,0,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15); int nChars;
if(!hostNameSuffix)
nChars=snprintf(NULL,0,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15);
else
nChars=snprintf(NULL,0,"%s%s",hostNameBase,hostNameSuffix);
char hostName[nChars+1]; char hostName[nChars+1];
sprintf(hostName,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15);
if(!hostNameSuffix)
sprintf(hostName,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15);
else
sprintf(hostName,"%s%s",hostNameBase,hostNameSuffix);
Serial.print("\nStarting MDNS...\n"); Serial.print("\nStarting MDNS...\n");
Serial.print("Broadcasting as: "); Serial.print("Broadcasting as: ");

View File

@ -75,7 +75,8 @@ struct SpanConfig {
struct Span{ struct Span{
const char *displayName; // display name for this device - broadcast as part of Bonjour MDNS const char *displayName; // display name for this device - broadcast as part of Bonjour MDNS
const char *hostNameBase; // base of host name of this device - full host name broadcast by Bonjour MDNS will have 6-byte accessoryID as well as '.local' automatically appended const char *hostNameBase; // base of hostName of this device - full host name broadcast by Bonjour MDNS will have 6-byte accessoryID as well as '.local' automatically appended
const char *hostNameSuffix=NULL; // optional "suffix" of hostName of this device. If specified, will be used as the hostName suffix instead of the 6-byte accessoryID
char *hostName; // full host name of this device - constructed from hostNameBase and 6-byte AccessoryID char *hostName; // full host name of this device - constructed from hostNameBase and 6-byte AccessoryID
const char *modelName; // model name of this device - broadcast as Bonjour field "md" const char *modelName; // model name of this device - broadcast as Bonjour field "md"
char category[3]=""; // category ID of primary accessory - broadcast as Bonjour field "ci" (HAP Section 13) char category[3]=""; // category ID of primary accessory - broadcast as Bonjour field "ci" (HAP Section 13)
@ -141,6 +142,7 @@ struct Span{
void setCommandTimeout(uint16_t nSec){comModeLife=nSec*1000;} // sets Command Mode Timeout (seconds) void setCommandTimeout(uint16_t nSec){comModeLife=nSec*1000;} // sets Command Mode Timeout (seconds)
void setLogLevel(uint8_t level){logLevel=level;} // sets Log Level for log messages (0=baseline, 1=intermediate, 2=all) void setLogLevel(uint8_t level){logLevel=level;} // sets Log Level for log messages (0=baseline, 1=intermediate, 2=all)
void setMaxConnections(uint8_t nCon){maxConnections=nCon;} // sets maximum number of simultaneous HAP connections (HAP requires devices support at least 8) void setMaxConnections(uint8_t nCon){maxConnections=nCon;} // sets maximum number of simultaneous HAP connections (HAP requires devices support at least 8)
void setHostNameSuffix(const char *suffix){hostNameSuffix=suffix;} // sets the hostName suffix to be used instead of the 6-byte AccessoryID
}; };
/////////////////////////////// ///////////////////////////////

View File

@ -10,6 +10,8 @@ void setup() {
homeSpan.setLogLevel(2); homeSpan.setLogLevel(2);
homeSpan.setHostNameSuffix("");
homeSpan.begin(Category::Lighting,"HomeSpanTest"); homeSpan.begin(Category::Lighting,"HomeSpanTest");
new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments