@@ -7,6 +7,8 @@ void (* cb_message)(char*, uint8_t*,unsigned int);
77void (* cb_present)(char *, uint8_t *,unsigned int );
88void (* cb_absent)(char *, uint8_t *,unsigned int );
99void (* cb_connected)(char *, uint8_t *,unsigned int );
10+ void (* cb_error)(char *, uint8_t *,unsigned int );
11+ void (* cb_info)(char *, uint8_t *,unsigned int );
1012
1113void msgCallback (char * topic, uint8_t * payload, unsigned int length) {
1214 /* remove /appid/ */
@@ -31,6 +33,18 @@ void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
3133 if (mg) mg->resetEndpoint ();
3234 }
3335 }
36+ else if (*topic == ' @' ) {
37+ if (strcmp (topic," @error" )==0 ) {
38+ if (cb_error) {
39+ cb_error (" error" ,payload,length);
40+ }
41+ }
42+ else if (strcmp (topic," @info" )==0 ) {
43+ if (cb_info) {
44+ cb_info (" info" ,payload,length);
45+ }
46+ }
47+ }
3448 else if (cb_message) {
3549 cb_message (topic,payload,length);
3650 }
@@ -100,18 +114,19 @@ void MicroGear::initEndpoint(Client *client, char* endpoint) {
100114 char pstr[100 ];
101115 int port = this ->securemode ?GEARAUTHSECUREPORT:GEARAUTHPORT;
102116
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));
117+ if ( client->connect (GEARAUTHHOST,port)){
118+ sprintf (pstr," GET /api/endpoint/%s HTTP/1.1\r\n\r\n " ,this ->gearkey );
119+ client->write ((const uint8_t *)pstr,strlen (pstr));
106120
107- delay (1000 );
108- getHTTPReply (client,pstr,200 );
121+ delay (1000 );
122+ getHTTPReply (client,pstr,200 );
109123
110- if (strlen (pstr)>6 ) {
111- strcpy (endpoint,pstr+6 );
112- writeEEPROM (endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
113- }
114- client->stop ();
124+ if (strlen (pstr)>6 ) {
125+ strcpy (endpoint,pstr+6 );
126+ writeEEPROM (endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
127+ }
128+ client->stop ();
129+ }
115130 }
116131}
117132
@@ -122,62 +137,63 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
122137 int port = (this ->securemode )?GEARAUTHSECUREPORT:GEARAUTHPORT;
123138
124139 *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 ();
140+ if (client->connect (GEARAUTHHOST,port)){
141+
142+ if (this ->securemode ) {
143+ WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
144+
145+ // verify a certificate fingerprint against a fingerprint saved in eeprom
146+ readEEPROM (tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
147+ #ifdef DEBUG_H
148+ Serial.print (" fingerprint loaded from eeprom : " );
149+ Serial.println (tstr);
150+ #endif
151+ if (clientsecure->verify (tstr, GEARAUTHHOST)) {
152+ #ifdef DEBUG_H
153+ Serial.println (" fingerprint matched" );
154+ #endif
155+ }
156+ else {
157+ #ifdef DEBUG_H
158+ Serial.println (" fingerprint mismatched, going to update" );
159+ #endif
160+ AuthClient::randomString (nonce,8 );
161+ sprintf (tstr," GET /api/fingerprint/%s/%s HTTP/1.1\r\n\r\n " ,this ->gearkey ,nonce);
162+ clientsecure->write ((const uint8_t *)tstr,strlen (tstr));
163+ delay (800 );
164+ getHTTPReply (clientsecure,tstr,200 );
165+ tstr[FINGERPRINTSIZE-1 ] = ' \0 ' ; // split fingerprint and signature
166+ sprintf (hashkey," %s&%s&%s" ,this ->gearkey ,this ->gearsecret ,nonce);
167+ Sha1.initHmac ((uint8_t *)hashkey,strlen (hashkey));
168+ Sha1.HmacBase64 (hash, tstr);
169+ for (int i=0 ;i<HMACSIZE;i++)
170+ if (hash[i]==' /' ) hash[i] = ' _' ;
171+
172+ if (strcmp (hash,tstr+FINGERPRINTSIZE)==0 ) {
173+ #ifdef DEBUG_H
174+ Serial.println (" new fingerprint updated" );
175+ #endif
176+ writeEEPROM (tstr, EEPROM_CERTFINGERPRINT, FINGERPRINTSIZE);
177+ }
178+ else {
179+ #ifdef DEBUG_H
180+ Serial.println (" fingerprint verification failed, abort" );
181+ #endif
182+ clientsecure->stop ();
183+ delay (5000 );
184+ return ;
185+ }
186+ }
187+ }
188+
189+ strcpy (tstr," GET /api/time HTTP/1.1\r\n\r\n " );
190+ client->write ((const uint8_t *)tstr,strlen (tstr));
191+
192+ delay (1000 );
193+ getHTTPReply (client,tstr,200 );
194+ *bts = atol (tstr) - millis ()/1000 ;
195+ client->stop ();
196+ }
181197}
182198
183199MicroGear::MicroGear (Client& netclient ) {
@@ -198,6 +214,9 @@ MicroGear::MicroGear(Client& netclient ) {
198214 cb_connected = NULL ;
199215 cb_absent = NULL ;
200216 cb_present = NULL ;
217+ cb_error = NULL ;
218+ cb_info = NULL ;
219+
201220 eepromready = false ;
202221
203222 mg = (MicroGear *)this ;
@@ -220,6 +239,13 @@ void MicroGear::on(unsigned char event, void (* callback)(char*, uint8_t*,unsign
220239 break ;
221240 case CONNECTED :
222241 if (callback) cb_connected = callback;
242+ break ;
243+ case ERROR :
244+ if (callback) cb_error = callback;
245+ break ;
246+ case INFO :
247+ if (callback) cb_info = callback;
248+ break ;
223249 }
224250}
225251
@@ -257,20 +283,21 @@ void MicroGear::resetToken() {
257283 char revokecode[REVOKECODESIZE+1 ];
258284 int port = this ->securemode ?GEARAUTHSECUREPORT:GEARAUTHPORT;
259285
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 ();
286+ if (sockclient->connect (GEARAUTHHOST,port)){
287+ readEEPROM (token,EEPROM_TOKENOFFSET,TOKENSIZE);
288+ readEEPROM (revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE);
289+ sprintf (pstr," GET /api/revoke/%s/%s HTTP/1.1\r\n\r\n " ,token,revokecode);
290+ sockclient->write ((const uint8_t *)pstr,strlen (pstr));
291+
292+ delay (1000 );
293+ getHTTPReply (sockclient,pstr,200 );
294+
295+ if (strcmp (pstr," FAILED" )!=0 ) {
296+ *state = EEPROM_STATE_NUL;
297+ writeEEPROM (state,EEPROM_STATEOFFSET,1 );
298+ }
299+ sockclient->stop ();
300+ }
274301 }
275302 else {
276303 *state = EEPROM_STATE_NUL;
@@ -375,6 +402,7 @@ bool MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
375402 #endif
376403 authclient->stop ();
377404 delay (1000 );
405+ break ;
378406 }
379407 }
380408 #ifdef DEBUG_H
@@ -466,9 +494,7 @@ int MicroGear::connectBroker(char* appid) {
466494 int gbport;
467495 bool tokenOK;
468496
469- do {
470- syncTime (sockclient, &bootts);
471- } while (bootts == 0 );
497+ syncTime (sockclient, &bootts);
472498
473499 #ifdef DEBUG_H
474500 Serial.print (" Time stamp : " );
@@ -633,10 +659,64 @@ bool MicroGear::publish(char* topic, String message) {
633659
634660bool MicroGear::publish (char * topic, String message, bool retained) {
635661 char buff[MAXBUFFSIZE];
636- message.toCharArray (buff,MAXBUFFSIZE);
662+ message.toCharArray (buff,MAXBUFFSIZE- 1 );
637663 return publish (topic, buff, retained);
638664}
639665
666+ bool MicroGear::publish (char * topic, String message, String apikey) {
667+ char buff[MAXBUFFSIZE];
668+ message.toCharArray (buff,MAXBUFFSIZE-1 );
669+
670+ char top[MAXTOPICSIZE] = " " ;
671+ strcat (top,topic);
672+ if (apikey!=" " ){
673+ strcat (top," /" );
674+ char buffapikey[MAXBUFFSIZE];
675+ apikey.toCharArray (buffapikey,MAXBUFFSIZE);
676+ strcat (top,buffapikey);
677+ }
678+ return publish (top, buff);
679+ }
680+
681+ bool MicroGear::publish (char * topic, String message, char * apikey) {
682+ char buff[MAXBUFFSIZE];
683+ message.toCharArray (buff,MAXBUFFSIZE-1 );
684+
685+ char top[MAXTOPICSIZE] = " " ;
686+ strcat (top,topic);
687+ if (apikey!=" " ){
688+ strcat (top," /" );
689+ strcat (top,apikey);
690+ }
691+ return publish (top, buff);
692+ }
693+
694+ bool MicroGear::writeFeed (char * feedname, char *data, char * apikey) {
695+ char buff[MAXBUFFSIZE] = " /@writefeed/" ;
696+
697+ strcat (buff,feedname);
698+ if (apikey!=NULL && strlen (apikey)>0 ){
699+ strcat (buff," /" );
700+ strcat (buff,apikey);
701+ }
702+ return publish (buff, data);
703+ }
704+
705+ bool MicroGear::writeFeed (char * feedname, char *data) {
706+ return writeFeed (feedname, data, NULL );
707+ }
708+
709+ bool MicroGear::writeFeed (char * feedname, String data, char * apikey) {
710+ char buff[MAXBUFFSIZE];
711+ data.toCharArray (buff,MAXBUFFSIZE-1 );
712+
713+ return writeFeed (feedname, data,apikey);
714+ }
715+
716+ bool MicroGear::writeFeed (char * feedname, String data) {
717+ return writeFeed (feedname, data,NULL );
718+ }
719+
640720/*
641721 setName() is deprecated
642722*/
0 commit comments