Skip to content

Merge changes from master #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions AuthClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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");
Expand All @@ -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 ");
Expand Down
8 changes: 4 additions & 4 deletions AuthClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,16 +33,16 @@ 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[]);
void write(char*);
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;
Expand All @@ -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
141 changes: 80 additions & 61 deletions MicroGear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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;i<HMACSIZE;i++)
if (hash[i]=='/') hash[i] = '_';

if(strcmp(hash,tstr+FINGERPRINTSIZE)==0) {
#ifdef DEBUG_H
Serial.println("new fingerprint updated");
#endif
writeEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
}
else {
#ifdef DEBUG_H
Serial.println("fingerprint verification failed, abort");
#endif
clientsecure->stop();
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;i<HMACSIZE;i++)
if (hash[i]=='/') hash[i] = '_';
if(strcmp(hash,tstr+FINGERPRINTSIZE)==0) {
#ifdef DEBUG_H
Serial.println("new fingerprint updated");
Serial.print("fingerprint : ");
Serial.println(tstr);
#endif
writeEEPROM(tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
}
else {
#ifdef DEBUG_H
Serial.println("fingerprint verification failed, abort");
#endif
clientsecure->stop();
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 ) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion MicroGear.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "AuthClient.h"
//#include "debug.h"

#define GEARAUTHHOST "ga.netpie.io"
#define GBPORT 1883
#define GBSECUREPORT 8883
#define DEFAULTSECUREMODE false
Expand Down Expand Up @@ -86,6 +87,7 @@ class MicroGear {
int eepromoffset;
bool eepromready;
int backoff, retry;
char gearauth[MAXGEARAUTHSIZE+1];

void* self;
AuthClient* authclient;
Expand All @@ -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;
Expand Down Expand Up @@ -150,6 +151,7 @@ class MicroGear {
void setEEPROMOffset(int);
void readEEPROM(char*,int, int);
void writeEEPROM(char*,int, int);
int setConfig(char*, char*);
};

#endif
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion README.th.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -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
Expand Down
Binary file added docs/images/EEPROM-token-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP8266 Microgear
version=1.2.1
version=1.2.4
author=Chavee Issariyapat <[email protected]>
maintainer=Chavee Issariyapat <[email protected]>
sentence=A client library for ESP8266 to connect to NETPIE IOT Platform.
Expand Down