@@ -38,32 +38,30 @@ void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
3838
3939bool MicroGear::clientReadln (Client* client, char *buffer, size_t buflen) {
4040 size_t pos = 0 ;
41+
4142 while (true ) {
42- while (true ) {
43- uint8_t byte = client->read ();
44- if (byte == ' \n ' ) {
45- // EOF found.
46- if (pos < buflen) {
47- if (pos > 0 && buffer[pos - 1 ] == ' \r ' )
48- pos--;
49- buffer[pos] = ' \0 ' ;
50- }
51- else {
52- buffer[buflen - 1 ] = ' \0 ' ;
53- }
54- return true ;
43+ uint8_t byte = client->read ();
44+ if (byte == ' \n ' ) {
45+ // EOF found.
46+ if (pos < buflen) {
47+ if (pos > 0 && buffer[pos - 1 ] == ' \r ' )
48+ pos--;
49+ buffer[pos] = ' \0 ' ;
5550 }
56-
57- if (byte != 255 ) {
58- if (pos < buflen) buffer[pos++] = byte;
51+ else {
52+ buffer[buflen - 1 ] = ' \0 ' ;
5953 }
60- else {
61- buffer[pos++] = ' \0 ' ;
62- return true ;
63- }
54+ return true ;
6455 }
56+
57+ if (byte != 255 ) {
58+ if (pos < buflen) buffer[pos++] = byte;
59+ }
60+ else {
61+ buffer[pos++] = ' \0 ' ;
62+ return false ;
63+ }
6564 }
66- return false ;
6765}
6866
6967int MicroGear::getHTTPReply (Client *client, char *buff, size_t buffsize) {
@@ -102,7 +100,7 @@ void MicroGear::initEndpoint(Client *client, char* endpoint) {
102100 char pstr[100 ];
103101 int port = this ->securemode ?GEARAUTHSECUREPORT:GEARAUTHPORT;
104102
105- if (client->connect (GEARAUTHHOST,port)) {
103+ if (client->connect (GEARAUTHHOST,port)){
106104 sprintf (pstr," GET /api/endpoint/%s HTTP/1.1\r\n\r\n " ,this ->gearkey );
107105 client->write ((const uint8_t *)pstr,strlen (pstr));
108106
@@ -113,8 +111,8 @@ void MicroGear::initEndpoint(Client *client, char* endpoint) {
113111 strcpy (endpoint,pstr+6 );
114112 writeEEPROM (endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
115113 }
114+ client->stop ();
116115 }
117- client->stop ();
118116 }
119117}
120118
@@ -124,11 +122,9 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
124122 char hash[32 ], hashkey[60 ];
125123 int port = (this ->securemode )?GEARAUTHSECUREPORT:GEARAUTHPORT;
126124
127- *bts = 0 ;
128- if (client->connect (GEARAUTHHOST,port)) {
129- #ifdef DEBUG_H
130- Serial.println (" connect gearauth" );
131- #endif
125+ *bts = 0 ;
126+ if (client->connect (GEARAUTHHOST,port)){
127+
132128 if (this ->securemode ) {
133129 WiFiClientSecure *clientsecure = (WiFiClientSecure *)(client);
134130
@@ -175,22 +171,20 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
175171 }
176172 }
177173 }
174+
178175 strcpy (tstr," GET /api/time HTTP/1.1\r\n\r\n " );
179176 client->write ((const uint8_t *)tstr,strlen (tstr));
180177
181178 delay (1000 );
182179 getHTTPReply (client,tstr,200 );
183180 *bts = atol (tstr) - millis ()/1000 ;
184181 client->stop ();
185- }
186- #ifdef DEBUG_H
187- Serial.println (" synctime done" );
188- #endif
182+ }
189183}
190184
191185MicroGear::MicroGear (Client& netclient ) {
192186 sockclient = &netclient;
193- constate = CLIENT_NOTCONNECT ;
187+ constate = MQTTCLIENT_NOTCONNECTED ;
194188 authclient = NULL ;
195189 mqttclient = NULL ;
196190
@@ -265,7 +259,7 @@ void MicroGear::resetToken() {
265259 char revokecode[REVOKECODESIZE+1 ];
266260 int port = this ->securemode ?GEARAUTHSECUREPORT:GEARAUTHPORT;
267261
268- if (sockclient->connect (GEARAUTHHOST,port)) {
262+ if (sockclient->connect (GEARAUTHHOST,port)){
269263 readEEPROM (token,EEPROM_TOKENOFFSET,TOKENSIZE);
270264 readEEPROM (revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE);
271265 sprintf (pstr," GET /api/revoke/%s/%s HTTP/1.1\r\n\r\n " ,token,revokecode);
@@ -278,8 +272,8 @@ void MicroGear::resetToken() {
278272 *state = EEPROM_STATE_NUL;
279273 writeEEPROM (state,EEPROM_STATEOFFSET,1 );
280274 }
275+ sockclient->stop ();
281276 }
282- sockclient->stop ();
283277 }
284278 else {
285279 *state = EEPROM_STATE_NUL;
@@ -289,7 +283,7 @@ void MicroGear::resetToken() {
289283}
290284
291285
292- void MicroGear::getToken (char *gkey, char *galias, char * token, char * tokensecret, char *endpoint) {
286+ bool MicroGear::getToken (char *gkey, char *galias, char * token, char * tokensecret, char *endpoint) {
293287 char state[2 ], tstate[2 ];
294288 int authstatus = 0 ;
295289
@@ -353,7 +347,7 @@ void MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
353347 #ifdef DEBUG_H
354348 Serial.println (" authclient is disconnected" );
355349 #endif
356- delay (2000 );
350+ // delay(2000);
357351 return false ;
358352 }
359353 } while (!authstatus);
@@ -440,7 +434,7 @@ void MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
440434 }
441435 }
442436 }while (*state == EEPROM_STATE_REQ);
443- // reset accesstoken retry counter
437+ // reset accesstoken retry counter
444438 retry = RETRY;
445439 #ifdef DEBUG_H
446440 Serial.println (authstatus); Serial.println (token); Serial.println (tokensecret); Serial.println (endpoint);
@@ -453,24 +447,28 @@ void MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
453447 readEEPROM (tokensecret,EEPROM_TOKENSECRETOFFSET,TOKENSECRETSIZE);
454448 readEEPROM (endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
455449 }
450+
451+ authclient->stop ();
456452
457453 if (*state != EEPROM_STATE_ACC) {
458454 #ifdef DEBUG_H
459455 Serial.println (" Fail to get a token." );
460456 #endif
461- delay (2000 );
457+ // delay(2000);
458+ return false ;
462459 }
463- authclient-> stop () ;
460+ return true ;
464461}
465462
466- bool MicroGear::connectBroker (char * appid) {
463+ int MicroGear::connectBroker (char * appid) {
467464 char username[USERNAMESIZE+1 ];
468465 char password[PASSWORDSIZE+1 ];
469466 char buff[2 *TOKENSECRETSIZE+2 ];
470467 char token[TOKENSIZE+1 ];
471468 char tokensecret[TOKENSECRETSIZE+1 ];
472469 char endpoint[MAXENDPOINTLENGTH+1 ];
473470 int gbport;
471+ bool tokenOK;
474472
475473 syncTime (sockclient, &bootts);
476474
@@ -479,96 +477,96 @@ bool MicroGear::connectBroker(char* appid) {
479477 Serial.println (bootts);
480478 #endif
481479
482- if (bootts!=0 ){
483- this ->appid = appid;
484- topicprefixlen = strlen (appid)+1 ;
480+ this ->appid = appid;
481+ topicprefixlen = strlen (appid)+1 ;
485482
486- if (mqttclient) {
487- // recently disconnected with code 4
488- if (mqttclient->state () == 4 ) {
489- resetToken ();
490- }
491- delete (mqttclient);
492- }
483+ if (mqttclient) {
484+ // recently disconnected with code 4
485+ if (mqttclient->state () == 4 ) {
486+ resetToken ();
487+ }
488+ delete (mqttclient);
489+ }
493490
494- if (authclient) delete (authclient);
495- authclient = new AuthClient (*sockclient);
496- authclient->init (appid,scope,bootts);
497- getToken (this ->gearkey ,this ->gearalias ,token,tokensecret,endpoint);
498- delete (authclient);
499- authclient = NULL ;
500-
501- /* if endpoint is empty, request a new one */
502- initEndpoint (sockclient, endpoint);
503-
504- /* generate one-time user/password */
505- sprintf (username," %s%%%s%%%lu" ,token,gearkey,bootts+millis ()/1000 );
506- sprintf (buff," %s&%s" ,tokensecret,gearsecret);
507- Sha1.initHmac ((uint8_t *)buff,strlen (buff));
508- Sha1.HmacBase64 (password, username);
509-
510- if (*token && *tokensecret) {
511- #ifdef DEBUG_H
512- Serial.println (" Going to connect to MQTT broker" );
513- Serial.println (token);
514- Serial.println (username);
515- Serial.println (password);
516- Serial.println (endpoint);
517- #endif
491+ if (authclient) delete (authclient);
492+ authclient = new AuthClient (*sockclient);
493+ authclient->init (appid,scope,bootts);
494+
495+ tokenOK = getToken (this ->gearkey ,this ->gearalias ,token,tokensecret,endpoint);
496+ delete (authclient);
497+ authclient = NULL ;
498+
499+ if (tokenOK && *token && *tokensecret) {
518500
519- char *p = endpoint;
520- while (*p!=' \0 ' ) {
521- if (*p == ' :' ) {
522- *p = ' \0 ' ;
523- p++;
524- break ;
525- }
526- p++;
527- }
501+ /* if endpoint is empty, request a new one */
502+ initEndpoint (sockclient, endpoint);
528503
529- gbport = this ->securemode ?GBSECUREPORT:GBPORT;
530- mqttclient = new PubSubClient (endpoint, gbport, msgCallback, *sockclient);
531- #ifdef DEBUG_H
532- Serial.print (" Connecting to : " );
533- Serial.print (endpoint);
534- Serial.print (" :" );
535- Serial.println (gbport);
536- #endif
537- delay (500 );
538-
539- constate = this ->mqttclient ->connect (token,username+TOKENSIZE+1 ,password);
540- switch (constate) {
541- case CLIENT_CONNECTED :
542- backoff = MINBACKOFFTIME;
543- if (cb_present)
544- subscribe (" /&present" );
545- if (cb_absent)
546- subscribe (" /&absent" );
547-
548- sprintf (buff," /&id/%s/#" ,token);
549- subscribe (buff);
550-
551- if (cb_connected)
552- cb_connected (NULL ,NULL ,0 );
553- break ;
554- case CLIENT_NOTCONNECT :
555- if (backoff < MAXBACKOFFTIME) backoff = 2 *backoff;
556- delay (backoff);
557- break ;
558- }
559- return constate;
560- }
561- else return false ;
562- }
563- else return false ;
504+ /* generate one-time user/password */
505+ sprintf (username," %s%%%s%%%lu" ,token,gearkey,bootts+millis ()/1000 );
506+ sprintf (buff," %s&%s" ,tokensecret,gearsecret);
507+ Sha1.initHmac ((uint8_t *)buff,strlen (buff));
508+ Sha1.HmacBase64 (password, username);
509+
510+ #ifdef DEBUG_H
511+ Serial.println (" Going to connect to MQTT broker" );
512+ Serial.println (token);
513+ Serial.println (username);
514+ Serial.println (password);
515+ Serial.println (endpoint);
516+ #endif
517+
518+ char *p = endpoint;
519+ while (*p!=' \0 ' ) {
520+ if (*p == ' :' ) {
521+ *p = ' \0 ' ;
522+ p++;
523+ break ;
524+ }
525+ p++;
526+ }
527+
528+ gbport = this ->securemode ?GBSECUREPORT:GBPORT;
529+ mqttclient = new PubSubClient (endpoint, gbport, msgCallback, *sockclient);
530+ #ifdef DEBUG_H
531+ Serial.print (" Connecting to : " );
532+ Serial.print (endpoint);
533+ Serial.print (" :" );
534+ Serial.println (gbport);
535+ #endif
536+ delay (500 );
537+
538+ constate = this ->mqttclient ->connect (token,username+TOKENSIZE+1 ,password);
539+ switch (constate) {
540+ case MQTTCLIENT_CONNECTED :
541+ backoff = MINBACKOFFTIME;
542+ if (cb_present)
543+ subscribe (" /&present" );
544+ if (cb_absent)
545+ subscribe (" /&absent" );
546+
547+ sprintf (buff," /&id/%s/#" ,token);
548+ subscribe (buff);
549+
550+ if (cb_connected)
551+ cb_connected (NULL ,NULL ,0 );
552+
553+ return NETPIECLIENT_CONNECTED;
554+
555+ case MQTTCLIENT_NOTCONNECTED :
556+ if (backoff < MAXBACKOFFTIME) backoff = 2 *backoff;
557+ delay (backoff);
558+ return NETPIECLIENT_NOTCONNECTED;
559+ }
560+ }
561+ else return NETPIECLIENT_TOKENERROR;
564562}
565563
566564void MicroGear::useTLS (bool usetls) {
567565 this ->securemode = usetls;
568566}
569567
570- bool MicroGear::connect (char * appid) {
571- connectBroker (appid);
568+ int MicroGear::connect (char * appid) {
569+ return connectBroker (appid);
572570}
573571
574572bool MicroGear::connected () {
0 commit comments