Skip to content

Commit ec57e37

Browse files
committed
microgear.connect() returns connection status
1 parent 1d491a5 commit ec57e37

File tree

3 files changed

+63
-52
lines changed

3 files changed

+63
-52
lines changed

AuthClient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bool AuthClient::connect(bool issecuremode) {
3535
void AuthClient::stop() {
3636
client->stop();
3737
}
38+
3839
/**
3940
* [AuthClient::writeout write string to client
4041
* @param str text

MicroGear.cpp

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,30 @@ void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
3838

3939
bool 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;
55+
}
56+
57+
if (byte != 255) {
58+
if (pos < buflen) buffer[pos++] = byte;
6459
}
60+
else{
61+
buffer[pos++] = '\0';
62+
return false;
63+
}
6564
}
66-
return false;
6765
}
6866

6967
int MicroGear::getHTTPReply(Client *client, char *buff, size_t buffsize) {
@@ -184,7 +182,7 @@ void MicroGear::syncTime(Client *client, unsigned long *bts) {
184182

185183
MicroGear::MicroGear(Client& netclient ) {
186184
sockclient = &netclient;
187-
constate = CLIENT_NOTCONNECT;
185+
constate = MQTTCLIENT_NOTCONNECTED;
188186
authclient = NULL;
189187
mqttclient = NULL;
190188

@@ -282,7 +280,7 @@ void MicroGear::resetToken() {
282280
}
283281

284282

285-
void MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecret, char *endpoint) {
283+
bool MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecret, char *endpoint) {
286284
char state[2], tstate[2];
287285
int authstatus = 0;
288286

@@ -346,7 +344,8 @@ void MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
346344
#ifdef DEBUG_H
347345
Serial.println("authclient is disconnected");
348346
#endif
349-
delay(2000);
347+
//delay(2000);
348+
return false;
350349
}
351350
} while (!authstatus);
352351
}
@@ -431,7 +430,7 @@ void MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
431430
}
432431
}
433432
}while (*state == EEPROM_STATE_REQ);
434-
// reset accesstoken retry counter
433+
// reset accesstoken retry counter
435434
retry = RETRY;
436435
#ifdef DEBUG_H
437436
Serial.println(authstatus); Serial.println(token); Serial.println(tokensecret); Serial.println(endpoint);
@@ -444,24 +443,28 @@ void MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecre
444443
readEEPROM(tokensecret,EEPROM_TOKENSECRETOFFSET,TOKENSECRETSIZE);
445444
readEEPROM(endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
446445
}
446+
447+
authclient->stop();
447448

448449
if (*state != EEPROM_STATE_ACC) {
449450
#ifdef DEBUG_H
450451
Serial.println("Fail to get a token.");
451452
#endif
452-
delay(2000);
453+
//delay(2000);
454+
return false;
453455
}
454-
authclient->stop();
456+
return true;
455457
}
456458

457-
bool MicroGear::connectBroker(char* appid) {
459+
int MicroGear::connectBroker(char* appid) {
458460
char username[USERNAMESIZE+1];
459461
char password[PASSWORDSIZE+1];
460462
char buff[2*TOKENSECRETSIZE+2];
461463
char token[TOKENSIZE+1];
462464
char tokensecret[TOKENSECRETSIZE+1];
463465
char endpoint[MAXENDPOINTLENGTH+1];
464466
int gbport;
467+
bool tokenOK;
465468

466469
do {
467470
syncTime(sockclient, &bootts);
@@ -486,20 +489,22 @@ bool MicroGear::connectBroker(char* appid) {
486489
if (authclient) delete(authclient);
487490
authclient = new AuthClient(*sockclient);
488491
authclient->init(appid,scope,bootts);
489-
getToken(this->gearkey,this->gearalias,token,tokensecret,endpoint);
492+
493+
tokenOK = getToken(this->gearkey,this->gearalias,token,tokensecret,endpoint);
490494
delete(authclient);
491495
authclient = NULL;
496+
497+
if (tokenOK && *token && *tokensecret) {
492498

493-
/* if endpoint is empty, request a new one */
494-
initEndpoint(sockclient, endpoint);
499+
/* if endpoint is empty, request a new one */
500+
initEndpoint(sockclient, endpoint);
501+
502+
/* generate one-time user/password */
503+
sprintf(username,"%s%%%s%%%lu",token,gearkey,bootts+millis()/1000);
504+
sprintf(buff,"%s&%s",tokensecret,gearsecret);
505+
Sha1.initHmac((uint8_t*)buff,strlen(buff));
506+
Sha1.HmacBase64(password, username);
495507

496-
/* generate one-time user/password */
497-
sprintf(username,"%s%%%s%%%lu",token,gearkey,bootts+millis()/1000);
498-
sprintf(buff,"%s&%s",tokensecret,gearsecret);
499-
Sha1.initHmac((uint8_t*)buff,strlen(buff));
500-
Sha1.HmacBase64(password, username);
501-
502-
if (*token && *tokensecret) {
503508
#ifdef DEBUG_H
504509
Serial.println("Going to connect to MQTT broker");
505510
Serial.println(token);
@@ -530,7 +535,7 @@ bool MicroGear::connectBroker(char* appid) {
530535

531536
constate = this->mqttclient->connect(token,username+TOKENSIZE+1,password);
532537
switch (constate) {
533-
case CLIENT_CONNECTED :
538+
case MQTTCLIENT_CONNECTED :
534539
backoff = MINBACKOFFTIME;
535540
if (cb_present)
536541
subscribe("/&present");
@@ -542,23 +547,24 @@ bool MicroGear::connectBroker(char* appid) {
542547

543548
if (cb_connected)
544549
cb_connected(NULL,NULL,0);
545-
break;
546-
case CLIENT_NOTCONNECT :
550+
551+
return NETPIECLIENT_CONNECTED;
552+
553+
case MQTTCLIENT_NOTCONNECTED :
547554
if (backoff < MAXBACKOFFTIME) backoff = 2*backoff;
548555
delay(backoff);
549-
break;
556+
return NETPIECLIENT_NOTCONNECTED;
550557
}
551-
return constate;
552558
}
553-
else return false;
559+
else return NETPIECLIENT_TOKENERROR;
554560
}
555561

556562
void MicroGear::useTLS(bool usetls) {
557563
this->securemode = usetls;
558564
}
559565

560-
bool MicroGear::connect(char* appid) {
561-
connectBroker(appid);
566+
int MicroGear::connect(char* appid) {
567+
return connectBroker(appid);
562568
}
563569

564570
bool MicroGear::connected() {

MicroGear.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@
5454
#define MICROGEAR_REJECTED 2
5555
#define RETRY 3
5656

57-
#define CLIENT_NOTCONNECT 0
58-
#define CLIENT_CONNECTED 1
57+
#define MQTTCLIENT_NOTCONNECTED 0
58+
#define MQTTCLIENT_CONNECTED 1
59+
60+
#define NETPIECLIENT_CONNECTED 0
61+
#define NETPIECLIENT_NOTCONNECTED 1
62+
#define NETPIECLIENT_TOKENERROR 2
5963

6064
/* Event Type */
6165
#define MESSAGE 1
@@ -69,7 +73,7 @@ class MicroGear {
6973
private:
7074
char* appid;
7175
char* gearname;
72-
char* gearkey;
76+
char* gearkey;
7377
char* gearsecret;
7478
char* gearalias;
7579
char* scope;
@@ -86,12 +90,12 @@ class MicroGear {
8690
MQTTClient *mqttclient;
8791
Client *sockclient;
8892

89-
bool connectBroker(char*);
93+
int connectBroker(char*);
9094
int getHTTPReply(Client*, char*, size_t);
9195
bool clientReadln(Client*, char*, size_t);
9296
void syncTime(Client*, unsigned long*);
9397
void initEndpoint(Client*, char*);
94-
void getToken(char*, char*, char*, char*, char*);
98+
bool getToken(char*, char*, char*, char*, char*);
9599

96100
public:
97101
int constate;
@@ -101,7 +105,7 @@ class MicroGear {
101105
void setName(char*);
102106
void setAlias(char*);
103107
void useTLS(bool);
104-
bool connect(char*);
108+
int connect(char*);
105109
bool connected();
106110

107111
bool publish(char*, char*);

0 commit comments

Comments
 (0)