diff --git a/AuthClient.cpp b/AuthClient.cpp index abdc085..ef74006 100644 --- a/AuthClient.cpp +++ b/AuthClient.cpp @@ -15,7 +15,8 @@ AuthClient::~AuthClient() { } -void AuthClient::init(char* appid, char* scope, unsigned long bts) { +void AuthClient::init(char *authendpoint, char* appid, char* scope, unsigned long bts) { + this->authendpoint = authendpoint; this->appid = appid; this->scope = scope; this->bootts = bts; @@ -24,7 +25,7 @@ void AuthClient::init(char* appid, char* scope, unsigned long bts) { bool AuthClient::connect(bool issecuremode) { int port = issecuremode?GEARAUTHSECUREPORT:GEARAUTHPORT; this->securemode = issecuremode; - if (client->connect(GEARAUTHHOST,port)) { + if (client->connect(authendpoint,port)) { return true; } else { @@ -173,7 +174,7 @@ int AuthClient::getGearToken(char mode, char* token, char* tokensecret, char* en *flag = '\0'; strcpy(signbase,this->securemode?"POST&https%3A%2F%2F":"POST&http%3A%2F%2F"); - sprintf(strtail(signbase),"%s%%3A%d",GEARAUTHHOST,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT); + sprintf(strtail(signbase),"%s%%3A%d",authendpoint,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT); if (mode == _REQUESTTOKEN) { writeln("POST /api/rtoken HTTP/1.1"); @@ -183,7 +184,7 @@ int AuthClient::getGearToken(char mode, char* token, char* tokensecret, char* en writeln("POST /api/atoken HTTP/1.1"); strcat(signbase,"%2Fapi%2Fatoken&"); } - sprintf(buff,"Host: %s:%d",GEARAUTHHOST,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT); + sprintf(buff,"Host: %s:%d",authendpoint,this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT); writeln(buff); write("Authorization: OAuth "); diff --git a/AuthClient.h b/AuthClient.h index 2bacf32..3970814 100644 --- a/AuthClient.h +++ b/AuthClient.h @@ -12,12 +12,12 @@ #include "SHA1.h" //#include "debug.h" -#define GEARAUTHHOST "ga.netpie.io" #define GEARAUTHPORT 8080 #define GEARAUTHSECUREPORT 8081 #define MGREV "E8A1b" #define MAXVERIFIERSIZE 32 +#define MAXGEARAUTHSIZE 32 #define TOKENSIZE 16 #define TOKENSECRETSIZE 32 #define MAXHEADERLINESIZE 350 @@ -33,8 +33,9 @@ class AuthClient { virtual ~AuthClient(); static void randomString(char* ,int); - void init(char*, char*,unsigned long); + void init(char*, char*, char*,unsigned long); bool connect(bool); + char *authendpoint; void stop(); void write_P(const char[]); void writeln_P(const char[]); @@ -42,7 +43,6 @@ class AuthClient { void writeln(char*); bool readln(char*, size_t); int getGearToken(char, char*, char*, char*, char*, char*, char*, char *, char*, char*, char*); - protected: private: Client* client; char* appid; @@ -57,6 +57,6 @@ class AuthClient { char* strtail(char*); void strcat(char*, char*); void addParam(char*, char*, char*, bool); - unsigned long bootts; + unsigned long bootts; }; #endif \ No newline at end of file diff --git a/MicroGear.cpp b/MicroGear.cpp index fa2bd9a..eca1db7 100644 --- a/MicroGear.cpp +++ b/MicroGear.cpp @@ -114,7 +114,7 @@ void MicroGear::initEndpoint(Client *client, char* endpoint) { char pstr[100]; int port = this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT; - if(client->connect(GEARAUTHHOST,port)){ + if(client->connect(gearauth,port)){ sprintf(pstr,"GET /api/endpoint/%s HTTP/1.1\r\n\r\n",this->gearkey); client->write((const uint8_t *)pstr,strlen(pstr)); @@ -137,63 +137,69 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) { int port = (this->securemode)?GEARAUTHSECUREPORT:GEARAUTHPORT; *bts = 0; - if(client->connect(GEARAUTHHOST,port)){ - - if (this->securemode) { - WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client); - - // verify a certificate fingerprint against a fingerprint saved in eeprom - readEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE); - #ifdef DEBUG_H - Serial.print("fingerprint loaded from eeprom : "); - Serial.println(tstr); - #endif - if (clientsecure->verify(tstr, GEARAUTHHOST)) { - #ifdef DEBUG_H - Serial.println("fingerprint matched"); - #endif - } - else { - #ifdef DEBUG_H - Serial.println("fingerprint mismatched, going to update"); - #endif - AuthClient::randomString(nonce,8); - sprintf(tstr,"GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n",this->gearkey,nonce); - clientsecure->write((const uint8_t *)tstr,strlen(tstr)); - delay(800); - getHTTPReply(clientsecure,tstr,200); - tstr[FINGERPRINTSIZE-1] = '\0'; // split fingerprint and signature - sprintf(hashkey,"%s&%s&%s",this->gearkey,this->gearsecret,nonce); - Sha1.initHmac((uint8_t*)hashkey,strlen(hashkey)); - Sha1.HmacBase64(hash, tstr); - for (int i=0;istop(); - delay(5000); - return; - } - } - } - - strcpy(tstr,"GET /api/time HTTP/1.1\r\n\r\n"); - client->write((const uint8_t *)tstr,strlen(tstr)); + if (this->securemode) { + WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client); + // verify a certificate fingerprint against a fingerprint saved in eeprom + readEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE); + #ifdef DEBUG_H + Serial.print("fingerprint loaded from eeprom : "); + Serial.println(tstr); + Serial.print("Host : "); + Serial.println(gearauth); + #endif + clientsecure->setFingerprint(tstr); + if(clientsecure->connect(gearauth,port)){ + if (clientsecure->verify(tstr, gearauth)) { + #ifdef DEBUG_H + Serial.println("fingerprint matched"); + #endif + } + } + else { + clientsecure->setInsecure(); + if(clientsecure->connect(gearauth,port)){ + #ifdef DEBUG_H + Serial.println("fingerprint mismatched, going to update"); + #endif + AuthClient::randomString(nonce,8); + sprintf(tstr,"GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n",this->gearkey,nonce); + clientsecure->write((const uint8_t *)tstr,strlen(tstr)); + delay(800); + getHTTPReply(clientsecure,tstr,200); + tstr[FINGERPRINTSIZE-1] = '\0'; // split fingerprint and signature + sprintf(hashkey,"%s&%s&%s",this->gearkey,this->gearsecret,nonce); + Sha1.initHmac((uint8_t*)hashkey,strlen(hashkey)); + Sha1.HmacBase64(hash, tstr); + for (int i=0;istop(); + delay(5000); + return; + } + } + } + } - delay(1000); - getHTTPReply(client,tstr,200); - *bts = atol(tstr) - millis()/1000; - client->stop(); - } + if(client->connect(gearauth,port)){ + strcpy(tstr,"GET /api/time HTTP/1.1\r\n\r\n"); + client->write((const uint8_t *)tstr,strlen(tstr)); + delay(1000); + getHTTPReply(client,tstr,200); + *bts = atol(tstr) - millis()/1000; + client->stop(); + } } MicroGear::MicroGear(Client& netclient ) { @@ -209,6 +215,9 @@ MicroGear::MicroGear(Client& netclient ) { this->backoff = 10; this->retry = RETRY; + strcpy(this->gearauth,GEARAUTHHOST); + this->gearauth[MAXGEARAUTHSIZE] = '\0'; + this->eepromoffset = 0; cb_message = NULL; cb_connected = NULL; @@ -283,7 +292,7 @@ void MicroGear::resetToken() { char revokecode[REVOKECODESIZE+1]; int port = this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT; - if(sockclient->connect(GEARAUTHHOST,port)){ + if(sockclient->connect(gearauth,port)){ readEEPROM(token,EEPROM_TOKENOFFSET,TOKENSIZE); readEEPROM(revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE); sprintf(pstr,"GET /api/revoke/%s/%s HTTP/1.1\r\n\r\n",token,revokecode); @@ -514,7 +523,7 @@ int MicroGear::connectBroker(char* appid) { if (authclient) delete(authclient); authclient = new AuthClient(*sockclient); - authclient->init(appid,scope,bootts); + authclient->init(gearauth,appid,scope,bootts); tokenOK = getToken(this->gearkey,this->gearalias,token,tokensecret,endpoint); delete(authclient); @@ -710,11 +719,11 @@ bool MicroGear::writeFeed(char* feedname, String data, char* apikey) { char buff[MAXBUFFSIZE]; data.toCharArray(buff,MAXBUFFSIZE-1); - return writeFeed(feedname, data,apikey); + return writeFeed(feedname, buff, apikey); } bool MicroGear::writeFeed(char* feedname, String data) { - return writeFeed(feedname, data,NULL); + return writeFeed(feedname, data, NULL); } /* @@ -821,3 +830,13 @@ int MicroGear::state() { if (!mqttclient) return -9; else return this->mqttclient->state(); } + +int MicroGear::setConfig(char* key, char* value) { + if (strcmp(key,"GEARAUTH")==0) { + strncpy(gearauth,value,MAXGEARAUTHSIZE); + return 1; + } + else { + return 0; + } +} diff --git a/MicroGear.h b/MicroGear.h index 4731869..366adb3 100644 --- a/MicroGear.h +++ b/MicroGear.h @@ -17,6 +17,7 @@ #include "AuthClient.h" //#include "debug.h" +#define GEARAUTHHOST "ga.netpie.io" #define GBPORT 1883 #define GBSECUREPORT 8883 #define DEFAULTSECUREMODE false @@ -86,6 +87,7 @@ class MicroGear { int eepromoffset; bool eepromready; int backoff, retry; + char gearauth[MAXGEARAUTHSIZE+1]; void* self; AuthClient* authclient; @@ -98,7 +100,6 @@ class MicroGear { void syncTime(Client*, unsigned long*); void initEndpoint(Client*, char*); bool getToken(char*, char*, char*, char*, char*); - public: int constate; char* endpoint; @@ -150,6 +151,7 @@ class MicroGear { void setEEPROMOffset(int); void readEEPROM(char*,int, int); void writeEEPROM(char*,int, int); + int setConfig(char*, char*); }; #endif diff --git a/README.md b/README.md index e39abe2..e04c6cf 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) { void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { Serial.println("Connected to NETPIE..."); /* Set the alias of this microgear ALIAS */ - microgear.setName(ALIAS); + microgear.setAlias(ALIAS); } @@ -160,6 +160,17 @@ microgear.init("sXfqDcXHzbFXiLk", "DNonzg2ivwS8ceksykGntrfQjxbL98", "myplant"); --- +**void MicroGear:: setEEPROMOffset(int offset)** + +Shift the offset of an EEPROM address where a microgear token is stored. This command will be useful if your application wants to store some other data in an EEPROM as well. The default offset value is 0. + +![EEPROM-token](docs/images/EEPROM-token-diagram.png) + +**arguments** +* *offset* - The EEPROM address offset vale. The default is 0. + +--- + **void MicroGear::on(unsigned char event, void (* callback)(char*, uint8_t*,unsigned int))** Add a callback listener to the event. diff --git a/README.th.md b/README.th.md index 7166b10..fc1e77f 100644 --- a/README.th.md +++ b/README.th.md @@ -67,7 +67,7 @@ void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) { void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { Serial.println("Connected to NETPIE..."); /* Set the alias of this microgear ALIAS */ - microgear.setName(ALIAS); + microgear.setAlias(ALIAS); } @@ -153,6 +153,17 @@ microgear.init("sXfqDcXHzbFXiLk", "DNonzg2ivwS8ceksykGntrfQjxbL98", "myplant"); --- +**void MicroGear:: setEEPROMOffset(int *offset*)** + +ตั้งค่าตำแหน่งแรกของ EEPROM ที่จะให้ microgear เก็บบันทึก token คำสั่งนี้จะมีประโยชน์ในกรณีที่ผู้ใช้ ต้องการใช้ EEPROM ในการเก็บบันทึกข้อมูลอย่างขึ้นด้วย ข้อมูลจะได้ไม่บันทึกทับซ้อนกัน โดยปกติหากไม่ได้เรียกคำสั่งนี้ microgear library จะใช้ EEPROM เริ่มต้นที่ตำแหน่งที่ 0 ในการเก็บ token + +![EEPROM-token](docs/images/EEPROM-token-diagram.png) + +**arguments** +* *offset* - ค่า offset ของตำแหน่ง EEPROM ที่ microgaer ใช้บันทึกข้อมูล ค่า default เป็น 0 + +--- + **void MicroGear::on(unsigned char event, void (* callback)(char*, uint8_t*,unsigned int))** เพิ่มฟังก์ชั่นที่ตอบสนองต่อ event diff --git a/docs/images/EEPROM-token-diagram.png b/docs/images/EEPROM-token-diagram.png new file mode 100644 index 0000000..f05d79c Binary files /dev/null and b/docs/images/EEPROM-token-diagram.png differ diff --git a/library.properties b/library.properties index bcecf48..4f463d0 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP8266 Microgear -version=1.2.1 +version=1.2.4 author=Chavee Issariyapat maintainer=Chavee Issariyapat sentence=A client library for ESP8266 to connect to NETPIE IOT Platform.