@@ -100,18 +100,19 @@ void MicroGear::initEndpoint(Client *client, char* endpoint) {
100100 char pstr[100 ];
101101 int port = this ->securemode ?GEARAUTHSECUREPORT:GEARAUTHPORT;
102102
103- client->connect (GEARAUTHHOST,port);
104- sprintf (pstr," GET /api/endpoint/%s HTTP/1.1\r\n\r\n " ,this ->gearkey );
105- client->write ((const uint8_t *)pstr,strlen (pstr));
106-
107- delay (1000 );
108- getHTTPReply (client,pstr,200 );
109-
110- if (strlen (pstr)>6 ) {
111- strcpy (endpoint,pstr+6 );
112- writeEEPROM (endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
113- }
114- client->stop ();
103+ if (client->connect (GEARAUTHHOST,port)){
104+ sprintf (pstr," GET /api/endpoint/%s HTTP/1.1\r\n\r\n " ,this ->gearkey );
105+ client->write ((const uint8_t *)pstr,strlen (pstr));
106+
107+ delay (1000 );
108+ getHTTPReply (client,pstr,200 );
109+
110+ if (strlen (pstr)>6 ) {
111+ strcpy (endpoint,pstr+6 );
112+ writeEEPROM (endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
113+ }
114+ client->stop ();
115+ }
115116 }
116117}
117118
@@ -122,62 +123,63 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
122123 int port = (this ->securemode )?GEARAUTHSECUREPORT:GEARAUTHPORT;
123124
124125 *bts = 0 ;
125- client->connect (GEARAUTHHOST,port);
126-
127- if (this ->securemode ) {
128- WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
129-
130- // verify a certificate fingerprint against a fingerprint saved in eeprom
131- readEEPROM (tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
132- #ifdef DEBUG_H
133- Serial.print (" fingerprint loaded from eeprom : " );
134- Serial.println (tstr);
135- #endif
136- if (clientsecure->verify (tstr, GEARAUTHHOST)) {
137- #ifdef DEBUG_H
138- Serial.println (" fingerprint matched" );
139- #endif
140- }
141- else {
142- #ifdef DEBUG_H
143- Serial.println (" fingerprint mismatched, going to update" );
144- #endif
145- AuthClient::randomString (nonce,8 );
146- sprintf (tstr," GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n " ,this ->gearkey ,nonce);
147- clientsecure->write ((const uint8_t *)tstr,strlen (tstr));
148- delay (800 );
149- getHTTPReply (clientsecure,tstr,200 );
150- tstr[FINGERPRINTSIZE-1 ] = ' \0 ' ; // split fingerprint and signature
151- sprintf (hashkey," %s&%s&%s" ,this ->gearkey ,this ->gearsecret ,nonce);
152- Sha1.initHmac ((uint8_t *)hashkey,strlen (hashkey));
153- Sha1.HmacBase64 (hash, tstr);
154- for (int i=0 ;i<HMACSIZE;i++)
155- if (hash[i]==' /' ) hash[i] = ' _' ;
156-
157- if (strcmp (hash,tstr+FINGERPRINTSIZE)==0 ) {
158- #ifdef DEBUG_H
159- Serial.println (" new fingerprint updated" );
160- #endif
161- writeEEPROM (tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
162- }
163- else {
164- #ifdef DEBUG_H
165- Serial.println (" fingerprint verification failed, abort" );
166- #endif
167- clientsecure->stop ();
168- delay (5000 );
169- return ;
170- }
171- }
172- }
173-
174- strcpy (tstr," GET /api/time HTTP/1.1\r\n\r\n " );
175- client->write ((const uint8_t *)tstr,strlen (tstr));
176-
177- delay (1000 );
178- getHTTPReply (client,tstr,200 );
179- *bts = atol (tstr) - millis ()/1000 ;
180- client->stop ();
126+ if (client->connect (GEARAUTHHOST,port)){
127+
128+ if (this ->securemode ) {
129+ WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
130+
131+ // verify a certificate fingerprint against a fingerprint saved in eeprom
132+ readEEPROM (tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
133+ #ifdef DEBUG_H
134+ Serial.print (" fingerprint loaded from eeprom : " );
135+ Serial.println (tstr);
136+ #endif
137+ if (clientsecure->verify (tstr, GEARAUTHHOST)) {
138+ #ifdef DEBUG_H
139+ Serial.println (" fingerprint matched" );
140+ #endif
141+ }
142+ else {
143+ #ifdef DEBUG_H
144+ Serial.println (" fingerprint mismatched, going to update" );
145+ #endif
146+ AuthClient::randomString (nonce,8 );
147+ sprintf (tstr," GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n " ,this ->gearkey ,nonce);
148+ clientsecure->write ((const uint8_t *)tstr,strlen (tstr));
149+ delay (800 );
150+ getHTTPReply (clientsecure,tstr,200 );
151+ tstr[FINGERPRINTSIZE-1 ] = ' \0 ' ; // split fingerprint and signature
152+ sprintf (hashkey," %s&%s&%s" ,this ->gearkey ,this ->gearsecret ,nonce);
153+ Sha1.initHmac ((uint8_t *)hashkey,strlen (hashkey));
154+ Sha1.HmacBase64 (hash, tstr);
155+ for (int i=0 ;i<HMACSIZE;i++)
156+ if (hash[i]==' /' ) hash[i] = ' _' ;
157+
158+ if (strcmp (hash,tstr+FINGERPRINTSIZE)==0 ) {
159+ #ifdef DEBUG_H
160+ Serial.println (" new fingerprint updated" );
161+ #endif
162+ writeEEPROM (tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
163+ }
164+ else {
165+ #ifdef DEBUG_H
166+ Serial.println (" fingerprint verification failed, abort" );
167+ #endif
168+ clientsecure->stop ();
169+ delay (5000 );
170+ return ;
171+ }
172+ }
173+ }
174+
175+ strcpy (tstr," GET /api/time HTTP/1.1\r\n\r\n " );
176+ client->write ((const uint8_t *)tstr,strlen (tstr));
177+
178+ delay (1000 );
179+ getHTTPReply (client,tstr,200 );
180+ *bts = atol (tstr) - millis ()/1000 ;
181+ client->stop ();
182+ }
181183}
182184
183185MicroGear::MicroGear (Client& netclient ) {
@@ -257,20 +259,21 @@ void MicroGear::resetToken() {
257259 char revokecode[REVOKECODESIZE+1 ];
258260 int port = this ->securemode ?GEARAUTHSECUREPORT:GEARAUTHPORT;
259261
260- sockclient->connect (GEARAUTHHOST,port);
261- readEEPROM (token,EEPROM_TOKENOFFSET,TOKENSIZE);
262- readEEPROM (revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE);
263- sprintf (pstr," GET /api/revoke/%s/%s HTTP/1.1\r\n\r\n " ,token,revokecode);
264- sockclient->write ((const uint8_t *)pstr,strlen (pstr));
265-
266- delay (1000 );
267- getHTTPReply (sockclient,pstr,200 );
268-
269- if (strcmp (pstr," FAILED" )!=0 ) {
270- *state = EEPROM_STATE_NUL;
271- writeEEPROM (state,EEPROM_STATEOFFSET,1 );
272- }
273- sockclient->stop ();
262+ if (sockclient->connect (GEARAUTHHOST,port)){
263+ readEEPROM (token,EEPROM_TOKENOFFSET,TOKENSIZE);
264+ readEEPROM (revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE);
265+ sprintf (pstr," GET /api/revoke/%s/%s HTTP/1.1\r\n\r\n " ,token,revokecode);
266+ sockclient->write ((const uint8_t *)pstr,strlen (pstr));
267+
268+ delay (1000 );
269+ getHTTPReply (sockclient,pstr,200 );
270+
271+ if (strcmp (pstr," FAILED" )!=0 ) {
272+ *state = EEPROM_STATE_NUL;
273+ writeEEPROM (state,EEPROM_STATEOFFSET,1 );
274+ }
275+ sockclient->stop ();
276+ }
274277 }
275278 else {
276279 *state = EEPROM_STATE_NUL;
@@ -374,7 +377,8 @@ bool MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
374377 Serial.println (" authclient is disconnected" );
375378 #endif
376379 authclient->stop ();
377- delay (1000 );
380+ delay (1000 );
381+ break ;
378382 }
379383 }
380384 #ifdef DEBUG_H
@@ -466,9 +470,7 @@ int MicroGear::connectBroker(char* appid) {
466470 int gbport;
467471 bool tokenOK;
468472
469- do {
470- syncTime (sockclient, &bootts);
471- } while (bootts == 0 );
473+ syncTime (sockclient, &bootts);
472474
473475 #ifdef DEBUG_H
474476 Serial.print (" Time stamp : " );
@@ -637,6 +639,47 @@ bool MicroGear::publish(char* topic, String message, bool retained) {
637639 return publish (topic, buff, retained);
638640}
639641
642+ bool MicroGear::publish (char * topic, String message, String apikey) {
643+ char buff[MAXBUFFSIZE];
644+ message.toCharArray (buff,MAXBUFFSIZE);
645+
646+ char top[MAXTOPICSIZE] = " " ;
647+ strcat (top,topic);
648+ if (apikey!=" " ){
649+ strcat (top," /" );
650+ char buffapikey[MAXBUFFSIZE];
651+ apikey.toCharArray (buffapikey,MAXBUFFSIZE);
652+ strcat (top,buffapikey);
653+ }
654+ return publish (top, buff);
655+ }
656+
657+ bool MicroGear::publish (char * topic, String message, char * apikey) {
658+ char buff[MAXBUFFSIZE];
659+ message.toCharArray (buff,MAXBUFFSIZE);
660+
661+ char top[MAXTOPICSIZE] = " " ;
662+ strcat (top,topic);
663+ if (apikey!=" " ){
664+ strcat (top," /" );
665+ strcat (top,apikey);
666+ }
667+ return publish (top, buff);
668+ }
669+
670+ bool MicroGear::writeFeed (char * feedname, String message, char * apikey) {
671+ char buff[MAXBUFFSIZE];
672+ message.toCharArray (buff,MAXBUFFSIZE);
673+
674+ char top[MAXTOPICSIZE] = " /@writefeed/" ;
675+ strcat (top,feedname);
676+ if (apikey){
677+ strcat (top," /" );
678+ strcat (top,apikey);
679+ }
680+ return publish (top, buff);
681+ }
682+
640683/*
641684 setName() is deprecated
642685*/
0 commit comments