Created HapQR class in standalone HapQR.h file

Class used for the creation and storage of a pairing QR Code (just the text, not the actual graphic) from a HAP Setup Code, HAP Category, and HAP Setup ID.  The resulting QR Code text is output to Serial Monitor whenever the SetUp Code is generated or changed.  The user can type this text into any QR Generator to create a QR Code graphic for pairing the device to HomeKit (in lieu of creating a printed tag of the Setup Code formatted using the Scancardium font).

Though not needed for HomeSpan, this class implements all the settings and parameters (such as Version and Reserved) used to generate any Apple HomeKit QR Code.

This class is used internally by HomeSpan and is not intended for the end-user.
This commit is contained in:
Gregg 2021-01-26 19:13:07 -06:00
parent 77d436141a
commit b6b835ad42
4 changed files with 63 additions and 30 deletions

View File

@ -64,7 +64,7 @@ void HAPClient::init(){
nvs_set_blob(srpNVS,"VERIFYDATA",&verifyData,sizeof(verifyData)); // update data
nvs_commit(srpNVS); // commit to NVS
Serial.print("Optional QR Code: ");
Serial.print(homeSpan.getQRCode(homeSpan.defaultSetupCode));
Serial.print(homeSpan.qrCode.get(atoi(homeSpan.defaultSetupCode),homeSpan.qrID,atoi(homeSpan.category)));
Serial.print("\n\n");
}

59
src/HapQR.h Normal file
View File

@ -0,0 +1,59 @@
/*********************************************************************************
* MIT License
*
* Copyright (c) 2020-2021 Gregg E. Berman
*
* https://github.com/HomeSpan/HomeSpan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
********************************************************************************/
class HapQR {
private:
char qrCode[21];
public:
enum {
NFC=1,
IP=2,
BLTE=4
};
char *get(uint32_t setupCode, const char *setupID, uint8_t category, uint8_t protocols=IP, uint8_t qVersion=0, uint8_t qReserved=0){
setupCode&=0x07FFFFFF; // valid values: 0-99999999
qVersion&=0x7; // valid values: 0-7
qReserved&=0xF; // valid values: 0-15
protocols&=0x7; // valid values: 0-7
uint64_t n=((uint64_t) qVersion<<43) | ((uint64_t) qReserved<<39) | ((uint64_t) category<<31) | (protocols<<27) | setupCode;
sprintf(qrCode,"X-HM://");
for(int i=15;i>=7;i--){
qrCode[i]=n%36+48;
if(qrCode[i]>57)
qrCode[i]+=7;
n/=36;
}
sprintf(qrCode+16,"%-4.4s",setupID);
return(qrCode);
} // create
};

View File

@ -541,7 +541,7 @@ void Span::processSerialCommand(const char *c){
Serial.print("New Code Saved!\n");
Serial.print("Optional QR Code: ");
Serial.print(getQRCode(setupCode));
Serial.print(qrCode.get(atoi(setupCode),qrID,atoi(category)));
Serial.print("\n\n");
}
}
@ -753,32 +753,6 @@ void Span::processSerialCommand(const char *c){
///////////////////////////////
const char *Span::getQRCode(const char *setupCode){
uint64_t n;
uint64_t iSetupCode;
uint64_t iCategory;
sscanf(category,"%llu",&iCategory);
sscanf(setupCode,"%llu",&iSetupCode);
n=(iCategory << 31) | (0xA << 27) | iSetupCode;
qrCode="";
while(n>0){
char c=n%36+48;
if(c>57)
c+=7;
qrCode=c+qrCode;
n/=36;
}
qrCode="X-HM://00" + qrCode + qrID;
return(qrCode.c_str());
}
///////////////////////////////
int Span::sprintfAttributes(char *cBuf){
int nBytes=0;

View File

@ -38,6 +38,7 @@
#include "Utils.h"
#include "Network.h"
#include "HAPConstants.h"
#include "HapQR.h"
using std::vector;
using std::unordered_map;
@ -85,7 +86,7 @@ struct Span{
int nFatalErrors=0; // number of fatal errors in user-defined configuration
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)
String qrCode; // optional QR Code to use for pairing
HapQR qrCode; // optional QR Code to use for pairing
boolean connected=false; // WiFi connection status
unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts
@ -149,7 +150,6 @@ struct Span{
void setHostNameSuffix(const char *suffix){hostNameSuffix=suffix;} // sets the hostName suffix to be used instead of the 6-byte AccessoryID
void setPortNum(uint16_t port){tcpPortNum=port;} // sets the TCP port number to use for communications between HomeKit and HomeSpan
void setQRID(const char *id); // sets the Setup ID for optional pairing with a QR Code
const char *getQRCode(const char *setupCode); // gets an optional QR code from setupCode
};
///////////////////////////////