forked from netpie2015/microgear-esp8266-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicroGear.cpp
836 lines (709 loc) · 23.5 KB
/
MicroGear.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
#include "MicroGear.h"
unsigned char topicprefixlen;
MicroGear* mg = NULL;
void (* cb_message)(char*, uint8_t*,unsigned int);
void (* cb_present)(char*, uint8_t*,unsigned int);
void (* cb_absent)(char*, uint8_t*,unsigned int);
void (* cb_connected)(char*, uint8_t*,unsigned int);
void (* cb_error)(char*, uint8_t*,unsigned int);
void (* cb_info)(char*, uint8_t*,unsigned int);
void msgCallback(char* topic, uint8_t* payload, unsigned int length) {
/* remove /appid/ */
char* rtopic = topic+topicprefixlen+1;
/* if a control message */
if (*rtopic == '&') {
if (strcmp(rtopic,"&present") == 0) {
if (cb_present) {
cb_present("present",payload,length);
}
}
else if (strcmp(rtopic,"&absent") == 0) {
if (cb_absent) {
cb_absent("absent",payload,length);
}
}
else if (strcmp(rtopic,"&resetendpoint") == 0) {
#ifdef DEBUG_H
Serial.println("to reset endpoint");
#endif
if (mg) mg->resetEndpoint();
}
}
else if (*topic == '@') {
if (strcmp(topic,"@error")==0) {
if (cb_error) {
cb_error("error",payload,length);
}
}
else if (strcmp(topic,"@info")==0) {
if (cb_info) {
cb_info("info",payload,length);
}
}
}
else if (cb_message) {
cb_message(topic,payload,length);
}
}
bool MicroGear::clientReadln(Client* client, char *buffer, size_t buflen) {
size_t pos = 0;
while (true) {
uint8_t byte = client->read();
if (byte == '\n') {
// EOF found.
if (pos < buflen) {
if (pos > 0 && buffer[pos - 1] == '\r')
pos--;
buffer[pos] = '\0';
}
else {
buffer[buflen - 1] = '\0';
}
return true;
}
if (byte != 255) {
if (pos < buflen) buffer[pos++] = byte;
}
else{
buffer[pos++] = '\0';
return false;
}
}
}
int MicroGear::getHTTPReply(Client *client, char *buff, size_t buffsize) {
int httpstatus = 0;
char pline = 0;
buff[0] = '\0';
while (true) {
clientReadln(client, buff, buffsize);
if (httpstatus==0) {
if (strncmp(buff,"HTTP",4)==0) {
buff[12] = '\0';
httpstatus = atoi(buff+9);
}
}
else {
if (pline > 0) {
return httpstatus;
}
if (strlen(buff)<1) pline++;
}
}
}
void MicroGear::resetEndpoint() {
writeEEPROM("",EEPROM_ENDPOINTSOFFSET,1);
}
void MicroGear::initEndpoint(Client *client, char* endpoint) {
if (*endpoint == '\0') {
#ifdef DEBUG_H
Serial.println("resync endpoint..");
#endif
char pstr[100];
int port = this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT;
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));
delay(1000);
getHTTPReply(client,pstr,200);
if (strlen(pstr)>6) {
strcpy(endpoint,pstr+6);
writeEEPROM(endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
}
client->stop();
}
}
}
void MicroGear::syncTime(Client *client, unsigned long *bts) {
char tstr[200];
char nonce[10];
char hash[32], hashkey[60];
int port = (this->securemode)?GEARAUTHSECUREPORT:GEARAUTHPORT;
*bts = 0;
if(client->connect(gearauth,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, gearauth)) {
#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));
delay(1000);
getHTTPReply(client,tstr,200);
*bts = atol(tstr) - millis()/1000;
client->stop();
}
}
MicroGear::MicroGear(Client& netclient ) {
sockclient = &netclient;
constate = MQTTCLIENT_NOTCONNECTED;
authclient = NULL;
mqttclient = NULL;
this->securemode = DEFAULTSECUREMODE;
this->token = NULL;
this->tokensecret = NULL;
this->backoff = 10;
this->retry = RETRY;
strcpy(this->gearauth,GEARAUTHHOST);
this->gearauth[MAXGEARAUTHSIZE] = '\0';
this->eepromoffset = 0;
cb_message = NULL;
cb_connected = NULL;
cb_absent = NULL;
cb_present = NULL;
cb_error = NULL;
cb_info = NULL;
eepromready = false;
mg = (MicroGear *)this;
}
void MicroGear::on(unsigned char event, void (* callback)(char*, uint8_t*,unsigned int)) {
switch (event) {
case MESSAGE :
if (callback) cb_message = callback;
break;
case PRESENT :
if (callback) cb_present = callback;
if (connected())
subscribe("/&present");
break;
case ABSENT :
if (callback) cb_absent = callback;
if (connected())
subscribe("/&absent");
break;
case CONNECTED :
if (callback) cb_connected = callback;
break;
case ERROR :
if (callback) cb_error = callback;
break;
case INFO :
if (callback) cb_info = callback;
break;
}
}
void MicroGear::setEEPROMOffset(int val) {
this->eepromoffset = val;
}
void MicroGear::readEEPROM(char* buff,int offset,int len) {
int i;
for (i=0;i<len;i++) {
buff[i] = (char)EEPROM.read(this->eepromoffset+offset+i);
}
buff[len] = '\0';
}
void MicroGear::writeEEPROM(char* buff,int offset,int len) {
int i;
for (i=0;i<len;i++) {
EEPROM.write(this->eepromoffset+offset+i,buff[i]);
}
EEPROM.commit();
}
void MicroGear::resetToken() {
char state[2];
if (!eepromready) {
EEPROM.begin(this->eepromoffset+256);
eepromready = true;
}
if (eepromready) {
readEEPROM(state,EEPROM_STATEOFFSET,1);
if (state[0] == EEPROM_STATE_ACC) {
char pstr[200];
char token[TOKENSIZE+1];
char revokecode[REVOKECODESIZE+1];
int port = this->securemode?GEARAUTHSECUREPORT:GEARAUTHPORT;
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);
sockclient->write((const uint8_t *)pstr,strlen(pstr));
delay(1000);
getHTTPReply(sockclient,pstr,200);
if (strcmp(pstr,"FAILED")!=0) {
*state = EEPROM_STATE_NUL;
writeEEPROM(state,EEPROM_STATEOFFSET,1);
}
sockclient->stop();
}
}
else {
*state = EEPROM_STATE_NUL;
writeEEPROM(state,EEPROM_STATEOFFSET,1);
}
}
}
bool MicroGear::getToken(char *gkey, char *galias, char* token, char* tokensecret, char *endpoint) {
char state[2], tstate[2];
int authstatus = 0;
char ekey[KEYSIZE+1];
char rtoken[TOKENSIZE+1];
char rtokensecret[TOKENSECRETSIZE+1];
char flag[FLAGSIZE+1];
char buff[2*TOKENSECRETSIZE+2];
char revokecode[REVOKECODESIZE+1];
token[0] = '\0';
tokensecret[0] = '\0';
readEEPROM(ekey,EEPROM_KEYOFFSET,KEYSIZE);
if (strncmp(gkey,ekey,KEYSIZE)!=0) resetToken();
readEEPROM(state,EEPROM_STATEOFFSET,1);
#ifdef DEBUG_H
Serial.print("Token rom state == ");
Serial.print(state);
Serial.println(";");
#endif
delay(500);
// if token is null --> get a requesttoken
if (*state != EEPROM_STATE_REQ && *state != EEPROM_STATE_ACC) {
#ifdef DEBUG_H
Serial.println("need a req token.");
#endif
do {
delay(2000);
if (authclient->connect(this->securemode)) {
#ifdef DEBUG_H
Serial.println("authclient is connected");
#endif
authstatus = authclient->getGearToken(_REQUESTTOKEN,rtoken,rtokensecret,endpoint,flag,gearkey,gearsecret,galias,scope,NULL,NULL);
delay(1000);
#ifdef DEBUG_H
Serial.println(authstatus); Serial.println(rtoken); Serial.println(rtokensecret); Serial.println(endpoint);
#endif
authclient->stop();
if (authstatus == 200) {
*state = EEPROM_STATE_REQ;
writeEEPROM(state,EEPROM_STATEOFFSET,1);
writeEEPROM(gearkey,EEPROM_KEYOFFSET,KEYSIZE);
writeEEPROM(rtoken,EEPROM_TOKENOFFSET,TOKENSIZE);
writeEEPROM(rtokensecret,EEPROM_TOKENSECRETOFFSET,TOKENSECRETSIZE);
#ifdef DEBUG_H
Serial.println("@ Write Request Token");
Serial.println(state);
Serial.println(rtoken);
Serial.println(rtokensecret);
#endif
}
}
else {
#ifdef DEBUG_H
Serial.println("authclient is disconnected");
#endif
//delay(2000);
return false;
}
} while (!authstatus);
}
//if token is a requesttoken --> get an accesstoken
if (*state == EEPROM_STATE_REQ) {
readEEPROM(rtoken,EEPROM_TOKENOFFSET,TOKENSIZE);
readEEPROM(rtokensecret,EEPROM_TOKENSECRETOFFSET,TOKENSECRETSIZE);
do {
delay(1000);
authstatus = 0;
while(authstatus==0) {
if (authclient->connect(this->securemode)) {
#ifdef DEBUG_H
Serial.println("authclient is connected, get access token");
#endif
authstatus = authclient->getGearToken(_ACCESSTOKEN,token,tokensecret,endpoint,flag,gearkey,gearsecret,galias,"",rtoken,rtokensecret);
delay(1000);
authclient->stop();
delay(1000);
}
else {
#ifdef DEBUG_H
Serial.println("authclient is disconnected");
#endif
authclient->stop();
delay(1000);
break;
}
}
#ifdef DEBUG_H
Serial.println(authstatus);
Serial.print("flag = ");
Serial.println(flag[0]);
#endif
// still write an EEPROM ic case of flag S
if (authstatus == 200) {
*state = EEPROM_STATE_ACC;
// if return with session flag mark eeprom token as null
if (flag[0] != 'S') *tstate = EEPROM_STATE_ACC;
else *tstate = EEPROM_STATE_NUL;
writeEEPROM(tstate,EEPROM_STATEOFFSET,1);
writeEEPROM(gearkey,EEPROM_KEYOFFSET,KEYSIZE);
writeEEPROM(token,EEPROM_TOKENOFFSET,TOKENSIZE);
writeEEPROM(tokensecret,EEPROM_TOKENSECRETOFFSET,TOKENSECRETSIZE);
//Calculate revokecode
sprintf(buff,"%s&%s",tokensecret,gearsecret);
Sha1.initHmac((uint8_t*)buff,strlen(buff));
Sha1.HmacBase64(revokecode, token);
for (int i=0;i<REVOKECODESIZE;i++)
if (revokecode[i]=='/') revokecode[i] = '_';
writeEEPROM(revokecode,EEPROM_REVOKECODEOFFSET,REVOKECODESIZE);
char *p = endpoint+12;
while (*p != '\0') {
if (*p == '%') {
*p = *(p+1)>='A'?16*(*(p+1)-'A'+10):16*(*(p+1)-'0');
*p += *(p+2)>='A'?(*(p+2)-'A'+10):(*(p+2)-'0');
strcpy(p+1,p+3);
}
p++;
}
writeEEPROM(endpoint+12,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
}
else {
if (authstatus != 401) {
#ifdef DEBUG_H
Serial.println("Return code is not 401");
Serial.println(authstatus);
#endif
/* request token error e.g. revoked */
*state = EEPROM_STATE_NUL;
writeEEPROM(state,EEPROM_STATEOFFSET,1);
break;
}
}
}while (*state == EEPROM_STATE_REQ);
// reset accesstoken retry counter
retry = RETRY;
#ifdef DEBUG_H
Serial.println(authstatus); Serial.println(token); Serial.println(tokensecret); Serial.println(endpoint);
#endif
}
//if token is a requesttoken --> get an accesstoken
if (*state == EEPROM_STATE_ACC) {
readEEPROM(token,EEPROM_TOKENOFFSET,TOKENSIZE);
readEEPROM(tokensecret,EEPROM_TOKENSECRETOFFSET,TOKENSECRETSIZE);
readEEPROM(endpoint,EEPROM_ENDPOINTSOFFSET,MAXENDPOINTLENGTH);
}
authclient->stop();
if (*state != EEPROM_STATE_ACC) {
#ifdef DEBUG_H
Serial.println("Fail to get a token.");
#endif
//delay(2000);
return false;
}
return true;
}
int MicroGear::connectBroker(char* appid) {
char username[USERNAMESIZE+1];
char password[PASSWORDSIZE+1];
char buff[2*TOKENSECRETSIZE+2];
char token[TOKENSIZE+1];
char tokensecret[TOKENSECRETSIZE+1];
char endpoint[MAXENDPOINTLENGTH+1];
int gbport;
bool tokenOK;
syncTime(sockclient, &bootts);
#ifdef DEBUG_H
Serial.print("Time stamp : ");
Serial.println(bootts);
#endif
this->appid = appid;
topicprefixlen = strlen(appid)+1;
if (mqttclient) {
// recently disconnected with code 4
if (mqttclient->state() == 4) {
resetToken();
}
delete(mqttclient);
}
if (authclient) delete(authclient);
authclient = new AuthClient(*sockclient);
authclient->init(gearauth,appid,scope,bootts);
tokenOK = getToken(this->gearkey,this->gearalias,token,tokensecret,endpoint);
delete(authclient);
authclient = NULL;
if (tokenOK && *token && *tokensecret) {
/* if endpoint is empty, request a new one */
initEndpoint(sockclient, endpoint);
/* generate one-time user/password */
sprintf(username,"%s%%%s%%%lu",token,gearkey,bootts+millis()/1000);
sprintf(buff,"%s&%s",tokensecret,gearsecret);
Sha1.initHmac((uint8_t*)buff,strlen(buff));
Sha1.HmacBase64(password, username);
#ifdef DEBUG_H
Serial.println("Going to connect to MQTT broker");
Serial.println(token);
Serial.println(username);
Serial.println(password);
Serial.println(endpoint);
#endif
char *p = endpoint;
while (*p!='\0') {
if (*p == ':') {
*p = '\0';
p++;
break;
}
p++;
}
gbport = this->securemode?GBSECUREPORT:GBPORT;
mqttclient = new PubSubClient(endpoint, gbport, msgCallback, *sockclient);
#ifdef DEBUG_H
Serial.print("Connecting to : ");
Serial.print(endpoint);
Serial.print(":");
Serial.println(gbport);
#endif
delay(500);
constate = this->mqttclient->connect(token,username+TOKENSIZE+1,password);
switch (constate) {
case MQTTCLIENT_CONNECTED :
backoff = MINBACKOFFTIME;
if (cb_present)
subscribe("/&present");
if (cb_absent)
subscribe("/&absent");
sprintf(buff,"/&id/%s/#",token);
subscribe(buff);
if (cb_connected)
cb_connected(NULL,NULL,0);
return NETPIECLIENT_CONNECTED;
case MQTTCLIENT_NOTCONNECTED :
if (backoff < MAXBACKOFFTIME) backoff = 2*backoff;
delay(backoff);
return NETPIECLIENT_NOTCONNECTED;
}
}
else return NETPIECLIENT_TOKENERROR;
}
void MicroGear::useTLS(bool usetls) {
this->securemode = usetls;
}
int MicroGear::connect(char* appid) {
return connectBroker(appid);
}
bool MicroGear::connected() {
//if (constate == CLIENT_NOTCONNECT) return CLIENT_NOTCONNECT;
//else return this->mqttclient->connected();
return this->sockclient->connected();
}
void MicroGear::subscribe(char* topic) {
char top[MAXTOPICSIZE] = "/";
strcat(top,appid);
strcat(top,topic);
mqttclient->subscribe(top);
}
void MicroGear::unsubscribe(char* topic) {
char top[MAXTOPICSIZE] = "/";
strcat(top,appid);
strcat(top,topic);
mqttclient->unsubscribe(top);
}
bool MicroGear::publish(char* topic, char* message, bool retained) {
char top[MAXTOPICSIZE] = "/";
strcat(top,appid);
strcat(top,topic);
return mqttclient->publish(top, message, retained);
}
bool MicroGear::publish(char* topic, char* message) {
return publish(topic, message, false);
}
bool MicroGear::publish(char* topic, double message, int n) {
return publish(topic, message, n, false);
}
bool MicroGear::publish(char* topic, double message, int n, bool retained) {
char mstr[16];
dtostrf(message,0,n,mstr);
return publish(topic, mstr, retained);
}
bool MicroGear::publish(char* topic, double message) {
return publish(topic, message, 8, false);
}
bool MicroGear::publish(char* topic, double message, bool retained) {
return publish(topic, message, 8, retained);
}
bool MicroGear::publish(char* topic, int message) {
return publish(topic, message, 0, false);
}
bool MicroGear::publish(char* topic, int message, bool retained) {
return publish(topic, message, 0, retained);
}
bool MicroGear::publish(char* topic, String message) {
return publish(topic, message, false);
}
bool MicroGear::publish(char* topic, String message, bool retained) {
char buff[MAXBUFFSIZE];
message.toCharArray(buff,MAXBUFFSIZE-1);
return publish(topic, buff, retained);
}
bool MicroGear::publish(char* topic, String message, String apikey) {
char buff[MAXBUFFSIZE];
message.toCharArray(buff,MAXBUFFSIZE-1);
char top[MAXTOPICSIZE] = "";
strcat(top,topic);
if(apikey!=""){
strcat(top,"/");
char buffapikey[MAXBUFFSIZE];
apikey.toCharArray(buffapikey,MAXBUFFSIZE);
strcat(top,buffapikey);
}
return publish(top, buff);
}
bool MicroGear::publish(char* topic, String message, char* apikey) {
char buff[MAXBUFFSIZE];
message.toCharArray(buff,MAXBUFFSIZE-1);
char top[MAXTOPICSIZE] = "";
strcat(top,topic);
if(apikey!=""){
strcat(top,"/");
strcat(top,apikey);
}
return publish(top, buff);
}
bool MicroGear::writeFeed(char* feedname, char *data, char* apikey) {
char buff[MAXBUFFSIZE] = "/@writefeed/";
strcat(buff,feedname);
if(apikey!=NULL && strlen(apikey)>0){
strcat(buff,"/");
strcat(buff,apikey);
}
return publish(buff, data);
}
bool MicroGear::writeFeed(char* feedname, char *data) {
return writeFeed(feedname, data, NULL);
}
bool MicroGear::writeFeed(char* feedname, String data, char* apikey) {
char buff[MAXBUFFSIZE];
data.toCharArray(buff,MAXBUFFSIZE-1);
return writeFeed(feedname, buff, apikey);
}
bool MicroGear::writeFeed(char* feedname, String data) {
return writeFeed(feedname, data, NULL);
}
/*
setName() is deprecated
*/
void MicroGear::setName(char* gearname) {
char top[MAXTOPICSIZE];
if (this->gearname) {
strcpy(top,"/gearname/");
strcat(top,this->gearname);
unsubscribe(top);
}
strcpy(top,"/gearname/");
strcat(top,gearname);
this->gearname = gearname;
subscribe(top);
}
void MicroGear::setAlias(char* gearalias) {
char top[MAXTOPICSIZE];
strcpy(top,"/@setalias/");
strcat(top,gearalias);
this->gearalias = gearalias;
publish(top,"");
}
bool MicroGear::chat(char* targetgear, char* message) {
bool result;
char top[MAXTOPICSIZE];
sprintf(top,"/%s/gearname/%s",appid,targetgear);
result = mqttclient->publish(top, message);
mqttclient->loop();
return result;
}
bool MicroGear::chat(char* topic, double message, int n) {
bool result;
char mstr[16];
dtostrf(message,0,n,mstr);
result = chat(topic, mstr);
mqttclient->loop();
return result;
}
bool MicroGear::chat(char* topic, double message) {
return chat(topic, message, 8);
}
bool MicroGear::chat(char* topic, int message) {
return chat(topic, message, 0);
}
bool MicroGear::chat(char* topic, String message) {
char buff[MAXBUFFSIZE];
message.toCharArray(buff,MAXBUFFSIZE);
return chat(topic, buff);
}
int MicroGear::init(char* gearkey,char* gearsecret) {
init(gearkey,gearsecret,"","");
}
int MicroGear::init(char* gearkey,char* gearsecret,char* gearalias) {
init(gearkey,gearsecret,gearalias,"");
}
int MicroGear::init(char* gearkey,char* gearsecret,char* gearalias, char* scope) {
this->gearkey = gearkey;
this->gearsecret = gearsecret;
this->gearalias = gearalias;
this->scope = scope;
if (!eepromready) {
EEPROM.begin(this->eepromoffset+256);
eepromready = true;
}
}
void MicroGear::loop() {
this->mqttclient->loop();
}
void MicroGear::setToken(char *key, char* token,char* tokensecret) {
char state[2];
this->token = token;
this->tokensecret = tokensecret;
*state = EEPROM_STATE_ACC;
writeEEPROM(state,EEPROM_STATEOFFSET,1);
writeEEPROM(key,EEPROM_KEYOFFSET,KEYSIZE);
writeEEPROM(token,EEPROM_TOKENOFFSET,TOKENSIZE);
writeEEPROM(tokensecret,EEPROM_TOKENSECRETOFFSET,TOKENSECRETSIZE);
}
void MicroGear::strcat(char* a, char* b) {
char *p;
p = a + strlen(a);
strcpy(p,b);
}
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;
}
}