From b50db3f0783ebcfeec3653b5e195409a43102463 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 27 Dec 2020 09:11:18 -0600 Subject: [PATCH] Cleaned up almost all compiler warning messages and cleaned up wifi connect message 1) Used `const char *` instead of `char *` where appropriate, including the need to create a dummy blank string for us in certain places. 2) Set initialization of WiFiClient to 0 instead of NULL, since WiFiClient is not a pointer (probably don't need to set it to anything since WiFiClient overrides the boolean operator anyway). 3) Cleaned up some of the messaging and logic when WiFi tries to connect so that users know to wait a bit. 4) Only remaining warning messages are for casting SpanService to (void *), which I think i unavailable (and is not forbidden). To Do: Go through examples and check for warnings (will likely need to convert `char *` to `const char *` in many places. --- src/HAP.cpp | 2 +- src/HAP.h | 2 +- src/HAPConstants.h | 4 +-- src/HKDF.cpp | 2 +- src/HKDF.h | 2 +- src/HomeSpan.cpp | 63 +++++++++++++++++++++++++++------------------- src/HomeSpan.h | 36 +++++++++++++------------- src/Network.cpp | 4 +-- src/Network.h | 6 ++--- src/Services.h | 14 +++++------ src/TLV.h | 6 ++--- 11 files changed, 76 insertions(+), 65 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index e44384a..b351467 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -701,7 +701,7 @@ int HAPClient::postPairVerifyURL(){ memcpy(iosCurveKey,tlv8.buf(kTLVType_PublicKey),32); // save iosCurveKey (will persist until end of verification process) - crypto_scalarmult_curve25519(sharedCurveKey,secretCurveKey,iosCurveKey); // generate (and persist) Pair Verify SharedSecret CurveKey from Accessory's Curve25519 secret key and Controller's Curve25519 public key (32 bytes) + int rVal=crypto_scalarmult_curve25519(sharedCurveKey,secretCurveKey,iosCurveKey); // generate (and persist) Pair Verify SharedSecret CurveKey from Accessory's Curve25519 secret key and Controller's Curve25519 public key (32 bytes) uint8_t *accessoryPairingID = accessory.ID; // set accessoryPairingID size_t accessoryPairingIDLen = 17; diff --git a/src/HAP.h b/src/HAP.h index d239bb1..b74413a 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -91,7 +91,7 @@ struct HAPClient { // individual structures and data defined for each Hap Client connection - WiFiClient client=NULL; // handle to client + WiFiClient client=0; // handle to client Controller *cPair; // pointer to info on current, session-verified Paired Controller (NULL=un-verified, and therefore un-encrypted, connection) // These keys are generated in the first call to pair-verify and used in the second call to pair-verify so must persist for a short period diff --git a/src/HAPConstants.h b/src/HAPConstants.h index f30f042..a332ff8 100644 --- a/src/HAPConstants.h +++ b/src/HAPConstants.h @@ -90,8 +90,8 @@ enum class StatusCode { /////////////////////////////// struct HapCharType { - char *id; - char *name; + const char *id; + const char *name; uint8_t perms; }; diff --git a/src/HKDF.cpp b/src/HKDF.cpp index ea0afa4..1ed8fe2 100644 --- a/src/HKDF.cpp +++ b/src/HKDF.cpp @@ -35,7 +35,7 @@ // Wrapper function to call mbedtls_hkdf, below, with // HAP-specific parameters and assumptions -int HKDF::create(uint8_t *outputKey, uint8_t *inputKey, int inputLen, char *salt, char *info){ +int HKDF::create(uint8_t *outputKey, uint8_t *inputKey, int inputLen, const char *salt, const char *info){ return(mbedtls_hkdf( mbedtls_md_info_from_type(MBEDTLS_MD_SHA512), (uint8_t *) salt, (size_t) strlen(salt), diff --git a/src/HKDF.h b/src/HKDF.h index cb57970..20eb713 100644 --- a/src/HKDF.h +++ b/src/HKDF.h @@ -39,5 +39,5 @@ // use SHA-512 with 32 bytes of output as required by HAP. struct HKDF { - int create(uint8_t *outputKey, uint8_t *inputKey, int inputLen, char *salt, char *info); // output of HKDF is always a 32-byte key derived from an input key, a salt string, and an info string + int create(uint8_t *outputKey, uint8_t *inputKey, int inputLen, const char *salt, const char *info); // output of HKDF is always a 32-byte key derived from an input key, a salt string, and an info string }; diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 7835d09..804eb53 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -44,7 +44,7 @@ Span homeSpan; // HAP Attributes database and all related c // Span // /////////////////////////////// -void Span::begin(Category catID, char *displayName, char *hostNameBase, char *modelName){ +void Span::begin(Category catID, const char *displayName, const char *hostNameBase, const char *modelName){ this->displayName=displayName; this->hostNameBase=hostNameBase; @@ -319,24 +319,34 @@ void Span::initWifi(){ int nChars=snprintf(NULL,0,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15); 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); - - int nTries=0; statusLED.start(LED_WIFI_CONNECTING); controlButton.reset(); + + int nTries=0; + + Serial.print("Attempting connection to: "); + Serial.print(network.wifiData.ssid); + Serial.print(". Type 'X ' or press Control Button for 3 seconds at any time to terminate search and delete WiFi credentials."); while(WiFi.status()!=WL_CONNECTED){ - Serial.print("Connecting to: "); - Serial.print(network.wifiData.ssid); - Serial.print("... "); - nTries++; - + + if(nTries++ == 0) + Serial.print("\nConnecting.."); + if(WiFi.begin(network.wifiData.ssid,network.wifiData.pwd)!=WL_CONNECTED){ - int delayTime=nTries%6?5000:60000; + int delayTime; char buf[8]=""; - Serial.print("Can't connect. Re-trying in "); - Serial.print(delayTime/1000); - Serial.print(" seconds. Type 'X ' or press Control Button for 3 seconds to terminate search and delete WiFi credentials..."); + if(nTries<=10){ + delayTime=2000; + Serial.print("."); + } else { + nTries=0; + delayTime=60000; + Serial.print(" Can't connect! Will re-try in "); + Serial.print(delayTime/1000); + Serial.print(" seconds..."); + } long sTime=millis(); while(millis()-sTimetype=type; this->perms=perms; this->hapName=hapName; @@ -1274,56 +1283,56 @@ SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, char *hapName) /////////////////////////////// -SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, boolean value, char *hapName) : SpanCharacteristic(type, perms, hapName) { +SpanCharacteristic::SpanCharacteristic(const char *type, uint8_t perms, boolean value, const char *hapName) : SpanCharacteristic(type, perms, hapName) { this->format=BOOL; this->value.BOOL=value; } /////////////////////////////// -SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, int32_t value, char *hapName) : SpanCharacteristic(type, perms, hapName) { +SpanCharacteristic::SpanCharacteristic(const char *type, uint8_t perms, int32_t value, const char *hapName) : SpanCharacteristic(type, perms, hapName) { this->format=INT; this->value.INT=value; } /////////////////////////////// -SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, uint8_t value, char *hapName) : SpanCharacteristic(type, perms, hapName) { +SpanCharacteristic::SpanCharacteristic(const char *type, uint8_t perms, uint8_t value, const char *hapName) : SpanCharacteristic(type, perms, hapName) { this->format=UINT8; this->value.UINT8=value; } /////////////////////////////// -SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, uint16_t value, char *hapName) : SpanCharacteristic(type, perms, hapName) { +SpanCharacteristic::SpanCharacteristic(const char *type, uint8_t perms, uint16_t value, const char *hapName) : SpanCharacteristic(type, perms, hapName) { this->format=UINT16; this->value.UINT16=value; } /////////////////////////////// -SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, uint32_t value, char *hapName) : SpanCharacteristic(type, perms, hapName) { +SpanCharacteristic::SpanCharacteristic(const char *type, uint8_t perms, uint32_t value, const char *hapName) : SpanCharacteristic(type, perms, hapName) { this->format=UINT32; this->value.UINT32=value; } /////////////////////////////// -SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, uint64_t value, char *hapName) : SpanCharacteristic(type, perms, hapName) { +SpanCharacteristic::SpanCharacteristic(const char *type, uint8_t perms, uint64_t value, const char *hapName) : SpanCharacteristic(type, perms, hapName) { this->format=UINT64; this->value.UINT64=value; } /////////////////////////////// -SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, double value, char *hapName) : SpanCharacteristic(type, perms, hapName) { +SpanCharacteristic::SpanCharacteristic(const char *type, uint8_t perms, double value, const char *hapName) : SpanCharacteristic(type, perms, hapName) { this->format=FLOAT; this->value.FLOAT=value; } /////////////////////////////// -SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, const char* value, char *hapName) : SpanCharacteristic(type, perms, hapName) { +SpanCharacteristic::SpanCharacteristic(const char *type, uint8_t perms, const char* value, const char *hapName) : SpanCharacteristic(type, perms, hapName) { this->format=STRING; this->value.STRING=value; } @@ -1542,7 +1551,8 @@ void SpanCharacteristic::setVal(int val){ SpanBuf sb; // create SpanBuf object sb.characteristic=this; // set characteristic sb.status=StatusCode::OK; // set status - sb.val=""; // set dummy "val" so that sprintfNotify knows to consider this "update" + char dummy[]=""; + sb.val=dummy; // set dummy "val" so that sprintfNotify knows to consider this "update" homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector } @@ -1557,7 +1567,8 @@ void SpanCharacteristic::setVal(double val){ SpanBuf sb; // create SpanBuf object sb.characteristic=this; // set characteristic sb.status=StatusCode::OK; // set status - sb.val=""; // set dummy "val" so that sprintfNotify knows to consider this "update" + char dummy[]=""; + sb.val=dummy; // set dummy "val" so that sprintfNotify knows to consider this "update" homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector } diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 861f90f..25836d9 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -74,10 +74,10 @@ struct SpanConfig { struct Span{ - char *displayName; // display name for this device - broadcast as part of Bonjour MDNS - 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 *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 char *hostName; // full host name of this device - constructed from hostNameBase and 6-byte AccessoryID - 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) unsigned long snapTime; // current time (in millis) snapped before entering Service loops() or updates() boolean isInitialized=false; // flag indicating HomeSpan has been initialized @@ -85,7 +85,7 @@ struct Span{ String configLog; // log of configuration process, including any errors boolean isBridge=true; // flag indicating whether device is configured as a bridge (i.e. first Accessory contains nothing but AccessoryInformation and HAPProtocolInformation) - char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing + const char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing uint8_t statusPin=DEFAULT_STATUS_PIN; // pin for status LED uint8_t controlPin=DEFAULT_CONTROL_PIN; // pin for Control Pushbutton uint8_t logLevel=DEFAULT_LOG_LEVEL; // level for writing out log messages to serial monitor @@ -106,15 +106,15 @@ struct Span{ HapCharList chr; // list of all HAP Characteristics void begin(Category catID=DEFAULT_CATEGORY, - char *displayName=DEFAULT_DISPLAY_NAME, - char *hostNameBase=DEFAULT_HOST_NAME, - char *modelName=DEFAULT_MODEL_NAME); + const char *displayName=DEFAULT_DISPLAY_NAME, + const char *hostNameBase=DEFAULT_HOST_NAME, + const char *modelName=DEFAULT_MODEL_NAME); void poll(); // poll HAP Clients and process any new HAP requests int getFreeSlot(); // returns free HAPClient slot number. HAPClients slot keep track of each active HAPClient connection void initWifi(); // initialize and connect to WiFi network void commandMode(); // allows user to control and reset HomeSpan settings with the control button - void processSerialCommand(char *c); // process command 'c' (typically from readSerial, though can be called with any 'c') + void processSerialCommand(const char *c); // process command 'c' (typically from readSerial, though can be called with any 'c') int sprintfAttributes(char *cBuf); // prints Attributes JSON database into buf, unless buf=NULL; return number of characters printed, excluding null terminator, even if buf=NULL void prettyPrint(char *buf, int nsp=2); // print arbitrary JSON from buf to serial monitor, formatted with indentions of 'nsp' spaces @@ -216,7 +216,7 @@ struct SpanCharacteristic{ }; int iid=0; // Instance ID (HAP Table 6-3) - char *type; // Characteristic Type + const char *type; // Characteristic Type const char *hapName; // HAP Name UVal value; // Characteristic Value uint8_t perms; // Characteristic Permissions @@ -231,15 +231,15 @@ struct SpanCharacteristic{ UVal newValue; // the updated value requested by PUT /characteristic SpanService *service=NULL; // pointer to Service containing this Characteristic - SpanCharacteristic(char *type, uint8_t perms, char *hapName); - SpanCharacteristic(char *type, uint8_t perms, boolean value, char *hapName); - SpanCharacteristic(char *type, uint8_t perms, uint8_t value, char *hapName); - SpanCharacteristic(char *type, uint8_t perms, uint16_t value, char *hapName); - SpanCharacteristic(char *type, uint8_t perms, uint32_t value, char *hapName); - SpanCharacteristic(char *type, uint8_t perms, uint64_t value, char *hapName); - SpanCharacteristic(char *type, uint8_t perms, int32_t value, char *hapName); - SpanCharacteristic(char *type, uint8_t perms, double value, char *hapName); - SpanCharacteristic(char *type, uint8_t perms, const char* value, char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, const char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, boolean value, const char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, uint8_t value, const char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, uint16_t value, const char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, uint32_t value, const char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, uint64_t value, const char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, int32_t value, const char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, double value, const char *hapName); + SpanCharacteristic(const char *type, uint8_t perms, const char* value, const char *hapName); 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.) diff --git a/src/Network.cpp b/src/Network.cpp index e963a2b..32f2332 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -131,7 +131,7 @@ void Network::apConfigure(){ } WiFiServer apServer(80); - client=NULL; + client=0; TempBuffer tempBuffer(MAX_HTTP+1); uint8_t *httpBuf=tempBuffer.buf; @@ -375,7 +375,7 @@ void Network::processRequest(char *body, char *formData){ ////////////////////////////////////// -int Network::getFormValue(char *formData, char *tag, char *value, int maxSize){ +int Network::getFormValue(char *formData, const char *tag, char *value, int maxSize){ char *s=strstr(formData,tag); // find start of tag diff --git a/src/Network.h b/src/Network.h index faa9cb9..09ea642 100644 --- a/src/Network.h +++ b/src/Network.h @@ -42,8 +42,8 @@ struct Network { const int MAX_HTTP=4095; // max number of bytes in HTTP message - char *apSSID=DEFAULT_AP_SSID; // Access Point SSID - char *apPassword=DEFAULT_AP_PASSWORD; // Access Point password (does not need to be secret - only used to ensure excrypted WiFi connection) + const char *apSSID=DEFAULT_AP_SSID; // Access Point SSID + const char *apPassword=DEFAULT_AP_PASSWORD; // Access Point password (does not need to be secret - only used to ensure excrypted WiFi connection) unsigned long lifetime=DEFAULT_AP_TIMEOUT*1000; // length of time (in milliseconds) to keep Access Point alive before shutting down and re-starting char **ssidList=NULL; @@ -66,7 +66,7 @@ struct Network { boolean allowedCode(char *s); // checks if Setup Code is allowed (HAP defines a list of disallowed codes) void apConfigure(); // configure homeSpan WiFi and Setup Code using temporary Captive Access Point; only returns if sucessful, else ESP restarts void processRequest(char *body, char *formData); // process the HTTP request - int getFormValue(char *formData, char *tag, char *value, int maxSize); // search for 'tag' in 'formData' and copy result into 'value' up to 'maxSize' characters; returns number of characters, else -1 if 'tag' not found + int getFormValue(char *formData, const char *tag, char *value, int maxSize); // search for 'tag' in 'formData' and copy result into 'value' up to 'maxSize' characters; returns number of characters, else -1 if 'tag' not found int badRequestError(); // return 400 error }; diff --git a/src/Services.h b/src/Services.h index 54f0110..664e034 100644 --- a/src/Services.h +++ b/src/Services.h @@ -417,8 +417,8 @@ namespace Characteristic { CREATE_CHAR(CurrentTiltAngle,int,0); CREATE_CHAR(FilterLifeLevel,double,0); CREATE_CHAR(FilterChangeIndication,uint8_t,0); - CREATE_CHAR(FirmwareRevision,char *,"1.0.0"); - CREATE_CHAR(HardwareRevision,char *,"1.0.0"); + CREATE_CHAR(FirmwareRevision,const char *,"1.0.0"); + CREATE_CHAR(HardwareRevision,const char *,"1.0.0"); CREATE_CHAR(HeatingThresholdTemperature,double,16); CREATE_CHAR(HoldPosition,boolean,false); CREATE_CHAR(Hue,double,0); @@ -429,11 +429,11 @@ namespace Characteristic { CREATE_CHAR(LockCurrentState,uint8_t,0); CREATE_CHAR(LockPhysicalControls,uint8_t,0); CREATE_CHAR(LockTargetState,uint8_t,0); - CREATE_CHAR(Manufacturer,char *,"HomeSpan"); - CREATE_CHAR(Model,char *,"HomeSpan-ESP32"); + CREATE_CHAR(Manufacturer,const char *,"HomeSpan"); + CREATE_CHAR(Model,const char *,"HomeSpan-ESP32"); CREATE_CHAR(MotionDetected,boolean,false); CREATE_CHAR(Mute,boolean,false); - CREATE_CHAR(Name,char *,"unnamed"); + CREATE_CHAR(Name,const char *,"unnamed"); CREATE_CHAR(NitrogenDioxideDensity,double,0); CREATE_CHAR(ObstructionDetected,boolean,false); CREATE_CHAR(PM25Density,double,0); @@ -455,7 +455,7 @@ namespace Characteristic { CREATE_CHAR(SecuritySystemAlarmType,uint8_t,0); CREATE_CHAR(SecuritySystemCurrentState,uint8_t,3); CREATE_CHAR(SecuritySystemTargetState,uint8_t,3); - CREATE_CHAR(SerialNumber,char *,"HS-12345"); + CREATE_CHAR(SerialNumber,const char *,"HS-12345"); CREATE_CHAR(ServiceLabelIndex,uint8_t,1); CREATE_CHAR(ServiceLabelNamespace,uint8_t,1); CREATE_CHAR(SlatType,uint8_t,0); @@ -481,7 +481,7 @@ namespace Characteristic { CREATE_CHAR(TemperatureDisplayUnits,uint8_t,0); CREATE_CHAR(TargetVerticalTiltAngle,int,0); CREATE_CHAR(ValveType,uint8_t,0); - CREATE_CHAR(Version,char *,"1.0.0"); + CREATE_CHAR(Version,const char *,"1.0.0"); CREATE_CHAR(VOCDensity,double,0); CREATE_CHAR(Volume,uint8_t,0); CREATE_CHAR(WaterLevel,double,0); diff --git a/src/TLV.h b/src/TLV.h index b4c696c..077c895 100644 --- a/src/TLV.h +++ b/src/TLV.h @@ -38,7 +38,7 @@ class TLV { int len; // LENGTH uint8_t *val; // VALUE buffer int maxLen; // maximum length of VALUE buffer - char *name; // abbreviated name of this TAG + const char *name; // abbreviated name of this TAG }; tlv_t tlv[maxTags]; // pointer to array of TLV record structures @@ -48,7 +48,7 @@ public: TLV(); - int create(tagType tag, int maxLen, char *name); // creates a new TLV record of type 'tag' with 'maxLen' bytes and display 'name' + int create(tagType tag, int maxLen, const char *name); // creates a new TLV record of type 'tag' with 'maxLen' bytes and display 'name' void clear(); // clear all TLV structures int val(tagType tag); // returns VAL for TLV with matching TAG (or -1 if no match) @@ -75,7 +75,7 @@ TLV::TLV(){ // TLV create(tag, maxLen, name) template -int TLV::create(tagType tag, int maxLen, char *name){ +int TLV::create(tagType tag, int maxLen, const char *name){ if(numTags==maxTags){ Serial.print("\n*** ERROR: Can't create new TLC tag type with name='");