-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apio.cpp
1037 lines (893 loc) · 25.1 KB
/
Apio.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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************\
* Apio Library *
* https://github.com/Apio/library-Apio *
* Copyright (c) 2012-2014, Apio Inc. All rights reserved. *
* ------------------------------------------------------------------------ *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the BSD License as described in license.txt. *
\**************************************************************************/
#include <Arduino.h>
#include "Apio.h"
#include <avr/eeprom.h>
#include "atmegarfr2.h"
#include "StringBuffer.h"
#include "String.h"
ApioClass Apio;
void divide_string(String stringToSplit) {
int strlen=stringToSplit.length();
//Serial1.println(stringToSplit); //debug
int i; //counter
Apio.deviceAddr="";
Apio.property="";
Apio.value="";
i=0;
for(i; stringToSplit.charAt(i)!=':' && i<strlen ;i++)
{
Apio.deviceAddr += String(stringToSplit.charAt(i));
}
//-----------property----------------
for(i++; stringToSplit.charAt(i)!=':' && i<strlen ;i++)
{
Apio.property += String(stringToSplit.charAt(i));
}
//-----------value----------------
for(i++; stringToSplit.charAt(i)!='-' && i<strlen ;i++)
{
Apio.value += String(stringToSplit.charAt(i));
}
int newProperty = 1;
for(int j = 0; j<Apio.indexReceived; j++)
{
if(Apio.propertyReceived[j]==Apio.property)
{
Apio.valueReceived[j] = Apio.value;
newProperty = 0;
}
}
if(newProperty)
{
Apio.propertyReceived[Apio.indexReceived]=Apio.property;
Apio.valueReceived[Apio.indexReceived]= Apio.value;
Apio.indexReceived++;
}
//Serial.println(Apio.property+":"+Apio.value);
}
static int fieldAnswerTo = 0;
static char *fieldAnswerChunks;
static int fieldAnswerChunksAt;
static int fieldAnswerRetries;
static StringBuffer fieldCommand(0, 16);
StringBuffer a;
static NWK_DataReq_t toSend;
// mesh callback for handling incoming commands
static bool receive(NWK_DataInd_t *ind);
static bool receive(NWK_DataInd_t *ind) {
if(Apio.isDongle)
{
int message_size=ind->size;
char bufferL[62];
for(int i=0; i<message_size; i++)
{
//Buffer[i] = ind->data[i];
bufferL[i] = ind->data[i];
}
//Serial.println(String(bufferL));
return true;
} else {
//Serial.println("Ciao ho ricevuto questo");
int message_size=ind->size;
int i;
char Buffer[110];
String receivedL="";
for(i=0; i<message_size; i++)
{
Buffer[i] = ind->data[i];
//delay(10);
//Serial.write(ind->data[i]);
}
//Serial.println();
NWK_SetAckControl(abs(ind->rssi));
//Serial.println(String(Buffer));
divide_string(String(Buffer));
for(int i=0; i<100; i++)
{
Buffer[i]=NULL;
}
return true;
}
/*
numvar ret;
if (Scout.handler.isVerbose) {
Serial.print(F("Received command"));
Serial.print(F("lqi: "));
Serial.print(ind->lqi);
Serial.print(F(" "));
Serial.print(F("rssi: "));
Serial.println(ind->rssi);
}
if (fieldAnswerTo) {
if (Scout.handler.isVerbose) {
Serial.println(F("can't receive command while sending answer"));
}
return false;
}
if (!fieldCommand.concat((const char*)ind->data, ind->size)) {
return false; // TODO we need to restart or something, no memory
}
// ack w/ our rssi as control
NWK_SetAckControl(abs(ind->rssi));
// when null terminated, do the message
if (fieldCommand[fieldCommand.length() - 1] != '\0') {
if (Scout.handler.isVerbose) {
Serial.println(F("waiting for more"));
}
return true;
}
if (Scout.handler.isVerbose) {
Serial.print(F("running command "));
Serial.println(fieldCommand.c_str());
}
Shell.lastMeshRssi = abs(ind->rssi);
Shell.lastMeshLqi = ind->lqi;
Shell.lastMeshFrom = ind->srcAddr;
fieldCommandOutput = "";
ret = Shell.eval(PrintToString(fieldCommandOutput), fieldCommand);
fieldCommand = (char*)NULL;
if (Scout.handler.isVerbose) {
Serial.print(F("got result "));
Serial.println(ret);
Serial.println(fieldCommandOutput.c_str());
}
// a command from the command endpoint doesn't get a response
if(ind->srcEndpoint == 2)
{
fieldCommandOutput = (char*)NULL;
return true;
}
// send data back in chunks
fieldAnswerTo = ind->srcAddr;
fieldAnswerChunksAt = 0;
fieldAnswerRetries = 0;
fieldAnswerChunk();
return true;
*/
}
void appDataConf(NWK_DataReq_t *req)
{
//Serial.print("ACK: "); //debug
switch(req->status)
{
case NWK_SUCCESS_STATUS:
//if(Apio.isDongle) Serial.println("1:"+String(req->dstAddr));
//Serial.println(String(req->control));
Apio.ack = 1;
break;
case NWK_ERROR_STATUS:
//if(Apio.isDongle) Serial.println("2:"+String(req->dstAddr));
Apio.ack = 2;
break;
case NWK_OUT_OF_MEMORY_STATUS:
//if(Apio.isDongle) Serial.println("3:"+String(req->dstAddr));
Apio.ack = 3;
break;
case NWK_NO_ACK_STATUS:
//if(Apio.isDongle) Serial.println("4:"+String(req->dstAddr));
//if(Apio.isDongle) Serial.println(req->control);
Apio.ack = 4;
break;
case NWK_NO_ROUTE_STATUS:
//if(Apio.isDongle) Serial.println("5:"+String(req->dstAddr));
Apio.ack = 5;
break;
case NWK_PHY_CHANNEL_ACCESS_FAILURE_STATUS:
//if(Apio.isDongle) Serial.println("6:"+String(req->dstAddr));
Apio.ack = 6;
break;
case NWK_PHY_NO_ACK_STATUS:
//if(Apio.isDongle) Serial.println("7:"+String(req->dstAddr));
Apio.ack = 7;
break;
default:
//if(Apio.isDongle) Serial.println("no correspondence in ack");
break;
}
Apio.nwkDataReqB = 0;
//Apio.loop();
}
//Apio.send();
//static void fieldAnswerChunk();
/*
// chunk packet confirmation callback by mesh
static void fieldAnswerChunkConfirm(NWK_DataReq_t *req);
// send the first/next chunk of the answer back and confirm
static void fieldAnswerChunk();
// mesh callback whenever another scout announces something on a channel
static bool fieldAnnouncements(NWK_DataInd_t *ind);
*/
ApioClass::ApioClass() {
// this has to be called as early as possible before other code uses the register
lastResetCause = GPIOR0;
}
ApioClass::~ApioClass() { }
/*Apio.setup(parametri sketchName, sketchRevision )
* Questa funzione mette in due variabile lo sketchName e lo sketchRevision.
* Poi fa SYS_Init(); la funzione che serve all'ATmega256rfr2
* Attiva PHY_RandomReq(); che genera numeri random (potrebbe tornare utile)
* Serial.begin(115200) questo va bene anche per noi, sia per la General che per il Dongle
*
*/
void ApioClass::setup(const char *sketchName, const char *sketchRevision, const uint16_t theAddress, const uint16_t thePanId) {
this->appId = sketchName;
this->sketchRevision = sketchRevision;
this->hi = 0;
this->flagDeleted = 0;
SYS_Init();
PHY_RandomReq();
NWK_Init();
Serial.begin(115200);
theAdd= theAddress;
//Serial.println(theAdd);
//pinMode(20,OUTPUT);
//pinMode(21,OUTPUT);
if(theAddress==0)
{
this->isDongle=1;
Serial.println("COORDINATOR ACTIVE");
//meshSetRadio(theAddress, thePanId, 0x1a);
loadSettingsFromEeprom();
//Serial.println("E sono gay");
} else {
this->isDongle=0;
Serial.println("Sono una General");
if(theAddress==9999){
eraseEeprom();
}
else if(theAddress==9998){
loadSettingsFromEeprom();
} else {
meshSetRadio(theAddress, thePanId, 0x1a);
}
}
//Canale di ascolto di default messaggi Apio
meshListen(1, receive);
currMillis = millis();
preMillis = currMillis;
//loadSettingsFromEeprom();
}
void ApioClass::loop() {
SYS_TaskHandler();
if(this->isDongle){
if(!this->hi){
if (this->isVerbose)
{
Serial.println("Invio l'hi a tutti");
}
this->sendTo="65535";
this->mex = "65535:hi:m-";
this->hi=1;
send();
}
//Serial.println(ack);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
Serial.println("c");
}
if(ack!=1 && contatoreInvii<6 && flagNoLoop != 1){
if(isVerbose) Serial.println("Non ho potuto inviare "+codaInvio+" riprovo");
send(codaInvio);
contatoreInvii++;
} else if (contatoreInvii>=6){
if(isVerbose) Serial.println("Invio avvenuto correttamente o impossibile inviare");
contatoreInvii = 0;
flagNoLoop =1;
}
if(Serial.available()){
char c = readFromSerial();
if(this->isVerbose) Serial.println(c+this->sendTo+":"+this->mex);
if(c=='l') {
send();
}
else if(c=='0'){
//0:panId:1:dataRate:0:radioPower:1-
int strlen=this->mex.length();
String panId;
String dataRate;
String radioPower;
int j=0;
String command="";
String value = "";
int i=0;
//Serial.println(this->mex);
while(j<strlen){
for (i; this->mex[i]!=':' ; i++){
command+=this->mex[i];
j++;
}
if(command!=""){
j++;
for (i++; this->mex[i]!=':' && this->mex[i]!='-' ; i++){
value+=this->mex[i];
j++;
}
}
i++;
if(command=="panId"){
panId = value;
}
if(command=="dataRate"){
dataRate = value;
}
if(command=="radioPower"){
radioPower = value;
}
j++;
command="";
value = "";
}
saveEepromAddress(0, panId.toInt(), 0x1a );
meshSetRadio(0, panId.toInt(), 0x1a);
}
}
}
if(!this->isDongle)
{
//fade(delFade, fadeOn); /////ruiztech tolto per problemi
//This function backup the state of the object when the network goes down
if(this->property=="hi"){
for(int j = 0; j<indexReceived; j++)
{
if(this->isVerbose) Serial.println(theAdd+":update:"+propertyReceived[j]+":"+valueReceived[j]+"-");
send(theAdd+":update:"+propertyReceived[j]+":"+valueReceived[j]+"-");
}
this->property = "";
}
if(this->property=="setmesh"){
Serial.println("Ciao");
String add = "";
String pan = "";
int i=0;
while(i<6){
if(i<4){
add+= this->value[i];
}
else {
pan+=this->value[i];
}
i++;
}
//Serial.println(add);
//Serial.println(pan);
if(add=="9998"){
this->flagDeleted = 1;
eraseEeprom();
} else {
saveEepromAddress(add.toInt(), pan.toInt(), 0x1a);
meshSetRadio(add.toInt(), pan.toInt(), 0x1a);
}
this->property="";
this->value="";
}
if(ack!=1 && flagSend==1){
if(isVerbose) Serial.println("Smetto di inviare come un matto");
ack=1;
flagSend=0;
}
else if(ack!=1 && contatoreInvii<6 && flagSend==0){
if(isVerbose) Serial.println("Non ho potuto inviare "+codaInvio+" riprovo");
send(codaInvio);
contatoreInvii++;
} else if (contatoreInvii>=6 && flagSend==0){
if(isVerbose) Serial.println("Invio avvenuto correttamente o impossibile inviare");
contatoreInvii = 0;
ack=1;
flagSend=1;
}
}
//Qui settiamo la tabella di routing
//Controllo della rete
bool showStatus = (indicate && lastIndicate < now && (now % indicate == 0));
if(showStatus){
NWK_RouteTableEntry_t *table = NWK_RouteTable();
bool meshed = 0;
for (int i=0; i < NWK_ROUTE_TABLE_SIZE; i++)
{
//Serial.println(String(table[i].dstAddr));
if (table[i].dstAddr != NWK_ROUTE_UNKNOWN) meshed = 1;
}
if(meshed)
{
lastIndicate = now;
}
}
}
void ApioClass::fade(int del, int flagFade){
if(flagFade){
//Serial.println("I'm HERE");
currMillis = millis();
if(currMillis - preMillis>del){
analogWrite(20, brightness);
analogWrite(21, brightnessNeg);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
brightnessNeg=255-brightness;
// wait for 30 milliseconds to see the dimming effect
preMillis = currMillis;
}
}
else {
analogWrite(20, 0);
analogWrite(21, 0);
currMillis = millis();
preMillis = currMillis;
}
}
void ApioClass::goToSleep(uint32_t sleepForMs) {
// TODO http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=136036
// - put radio to sleep
// - set all GPIO pins to inputs
// - turn off ADC
// - turn off backpack power
// - put MCU to sleep
}
void ApioClass::enableExternalAref() {
isExternalAref = true;
analogReference(EXTERNAL);
}
void ApioClass::disableExternalAref() {
isExternalAref = false;
ADMUX = (1 << REFS1) | (1 << REFS0); // 1.6V internal voltage ref.
}
bool ApioClass::getExternalAref() {
return isExternalAref;
}
char ApioClass::readFromSerial() {
//l16:onoff:1-rosso:255-verde:255-blu:255-funzioni:0-
//Serial1.println("readfrom");
this->sendTo="";
this->mex = "";
int flagAddr=0;
char interface=NULL;
delayMicroseconds(100); // it is needed, otherwise every now and then you lose the first character to be read!!!
interface = Serial.read();
delayMicroseconds(100);
while (Serial.available())
{
char buf = Serial.read();
delayMicroseconds(100);
if(buf==':'){
flagAddr=1;
}
if(flagAddr==0)
{
this->sendTo+=buf;
}
this->mex += buf;
}
//Serial.println(content);
delayMicroseconds(100);
//divide_string(content);
return interface;
}
const char* ApioClass::getLastResetCause() {
switch (lastResetCause) {
case 1:
return PSTR("Power-on");
break;
case 2:
return PSTR("External");
break;
case 4:
return PSTR("Brown-out");
break;
case 8:
return PSTR("Watchdog");
break;
case 16:
return PSTR("JTAG");
break;
default:
return PSTR("Unknown Cause Reset");
}
}
int8_t ApioClass::getTemperature() {
if (isExternalAref == false) {
return HAL_MeasureTemperature() + tempOffset;
} else {
return -127;
}
}
int8_t ApioClass::getTemperatureOffset(void) {
return tempOffset;
}
void ApioClass::setTemperatureOffset(int8_t offset) {
eeprom_update_byte((uint8_t *)8124, (uint8_t)offset);
tempOffset = offset;
}
void ApioClass::setHQToken(const char *token) {
for (int i=0; i<32; i++) {
eeprom_update_byte((uint8_t *)8130+i, token[i]);
}
}
void ApioClass::getHQToken(char *token) {
for (int i=0; i<32; i++) {
token[i] = eeprom_read_byte((uint8_t *)8130+i);
}
}
void ApioClass::resetHQToken() {
for (int i=0; i<32; i++) {
eeprom_update_byte((uint8_t *)8130+i, 0xFF);
}
}
void ApioClass::setOTAFlag() {
eeprom_update_byte((uint8_t *)8125, 0x00);
}
uint32_t ApioClass::getHwSerial() {
return eeprom_read_dword((uint32_t *)8184);
}
uint16_t ApioClass::getHwFamily() {
return eeprom_read_word((uint16_t *)8188);
}
uint8_t ApioClass::getHwVersion() {
return eeprom_read_byte((uint8_t *)8190);
}
uint8_t ApioClass::getEEPROMVersion() {
return eeprom_read_byte((uint8_t *)8191);
}
void ApioClass::send(String message) {
if(this->isDongle == 1){
if(!nwkDataReqB)
{
int len;
int flagReinvio = 0;
if(message != ""){
len = message.length(); //if i use toSend.toCharArray() the lwm packet do not get good
//Serial.print("Lunghezza:");
//Serial.println(len);
flagReinvio = 1;
codaInvio = message;
char sendThis[len];
//sendTo="0";
for(int g=0; g<len ;g++)
{
sendThis[g]=message.charAt(g);
//Serial.write(sendThis[g]);
}
a = message;
sendTo = exSend;
} else {
flagNoLoop = 0;
//Serial.println("Voglio inviare e sono un dongle");
len = mex.length(); //if i use toSend.toCharArray() the lwm packet do not get good
//Serial.print("Lunghezza:");
//Serial.println(len);
char sendThis[len];
for(int g=0; g<len ;g++)
{
sendThis[g]=mex.charAt(g);
//Serial.write(sendThis[g]);
}
a = mex;
}
//Serial.println(mex);
this->nwkDataReqB = 1;
if (!flagReinvio) exSend = sendTo;
toSend.dstAddr = this->sendTo.toInt();
//Serial.println(toSend.dstAddr);
toSend.dstEndpoint = 1;
toSend.srcEndpoint = 1;
//toSend.options = NWK_OPT_ACK_REQUEST;
toSend.data = (uint8_t*)a.c_str();
//Serial.println(toSend.data);
toSend.size = len;
toSend.confirm = appDataConf;
NWK_DataReq(&toSend);
mex = "";
sendTo="";
}
} else {
if(!nwkDataReqB)
{
//Serial.println("Voglio inviare e sono un dongle");
int len = message.length(); //if i use toSend.toCharArray() the lwm packet do not get good
//Serial.print("Lunghezza:");
//Serial.println(len);
codaInvio = message;
char sendThis[len];
sendTo="0";
for(int g=0; g<len ;g++)
{
sendThis[g]=message.charAt(g);
//Serial.write(sendThis[g]);
}
a = message;
//Serial.println(mex);
this->nwkDataReqB = 1;
toSend.dstAddr = this->sendTo.toInt();
//Serial.println(toSend.dstAddr);
toSend.dstEndpoint = 1;
toSend.srcEndpoint = 1;
//toSend.options = NWK_OPT_ACK_REQUEST;
toSend.data = (uint8_t*)a.c_str();
//Serial.println(toSend.data);
toSend.size = len;
toSend.confirm = appDataConf;
NWK_DataReq(&toSend);
mex = "";
}
//Serial.println("Voglio inviare e sono una general");
}
// TODO - Send state to HQ, and set pin values and pinmodes from response
}
void ApioClass::eraseEeprom(){
if(eeprom_read_word((uint16_t *)8182) != 0x9999){
Serial.println("NONE");
eeprom_update_word((uint16_t *)8182, 0x9999);
eeprom_update_word((uint16_t *)8180, 0x01);
eeprom_update_byte((uint8_t *)8179, 0x1a);
}
meshSetRadio(9999, 0x01, 0x1a);
}
void ApioClass::saveEepromAddress(const uint16_t theAddress, const uint16_t thePanId, const uint8_t theChannel){
eeprom_update_word((uint16_t *)8182, theAddress);
eeprom_update_word((uint16_t *)8180, thePanId);
eeprom_update_byte((uint8_t *)8179, theChannel);
}
void ApioClass::loadSettingsFromEeprom() {
// Address 8124 - 1 byte - Temperature offset
// Address 8125 - 1 byte - Initiate OTA flag
// Address 8126 - 1 byte - Data rate
// Address 8127 - 3 bytes - Torch color (R,G,B)
// Address 8130 - 32 bytes - HQ Token
// Address 8162 - 16 bytes - Security Key
// Address 8178 - 1 byte - Transmitter Power
// Address 8179 - 1 byte - Frequency Channel
// Address 8180 - 2 bytes - Network Identifier/Troop ID
// Address 8182 - 2 bytes - Network Address/Scout ID
// Address 8184 - 4 bytes - Unique ID
// Address 8188 - 2 bytes - HW family
// Address 8190 - 1 byte - HW Version
// Address 8191 - 1 byte - EEPROM Version
byte buffer[32];
//Questo l'HQToken non so cosa fa
/*for (int i=0; i<32; i++) {
buffer[i] = eeprom_read_byte((uint8_t *)8130+i);
}
setHQToken((char *)buffer);
memset(buffer, 0x00, 32);
//Non lo implementiamo
for (int i=0; i<16; i++) {
buffer[i] = eeprom_read_byte((uint8_t *)8162+i);
}
meshSetSecurityKey((uint8_t *)buffer);
memset(buffer, 0x00, 16);
*/
if(this->isDongle){
if (eeprom_read_word((uint16_t *)8182) != 0x0000 ||
eeprom_read_word((uint16_t *)8180) != 0x01 ||
eeprom_read_byte((uint8_t *)8179) != 0x1a) {
//Serial.println(eeprom_read_word((uint16_t *)8180));
meshSetRadio(eeprom_read_word((uint16_t *)8182), eeprom_read_word((uint16_t *)8180), eeprom_read_byte((uint8_t *)8179));
}
else {
meshSetRadio(0x0000, 0x01, 0x1a);
}
}
else {
if (eeprom_read_word((uint16_t *)8182) != 0x9999 ||
eeprom_read_word((uint16_t *)8180) != 0x01 ||
eeprom_read_byte((uint8_t *)8179) != 0x1a) {
meshSetRadio(eeprom_read_word((uint16_t *)8182), eeprom_read_word((uint16_t *)8180), eeprom_read_byte((uint8_t *)8179));
}
else {
meshSetRadio(9999, 0x01, 0x1a);
}
}
/*if (eeprom_read_byte((uint8_t *)8178) != 0xFF) {
meshSetPower(eeprom_read_byte((uint8_t *)8178));
}
if (eeprom_read_byte((uint8_t *)8126) != 0xFF) {
meshSetDataRate(eeprom_read_byte((uint8_t *)8126));
}*/
/*
if (eeprom_read_byte((uint8_t *)8124) != 0xFF) {
tempOffset = (int8_t)eeprom_read_byte((uint8_t *)8124);
}*/
}
void ApioClass::meshSetRadio(const uint16_t theAddress, const uint16_t thePanId, const uint8_t theChannel) {
if(theAddress==9999){
// fadeOn=1; ///ruiztech tolto per problemi
} else {
// fadeOn=0; ///ruiztech tolto per problemi
}
NWK_SetAddr(theAddress);
address = theAddress;
NWK_SetPanId(thePanId);
PanId = thePanId;
meshSetChannel(theChannel);
PHY_SetRxState(true);
//Serial.println(address);
//Serial.println(thePanId);
//eeprom_update_word((uint16_t *)8182, address);
//eeprom_update_word((uint16_t *)8180, panId);
//if (eeprom_read_byte((uint8_t *)8178) != 0xFF) {
meshSetPower(0);
meshSetDataRate(0);
if(!this->isDongle && !this->flagDeleted){
send(String(theAddress)+":hi:appId:"+appId+"-");
this->flagDeleted = 0;
}
}
void ApioClass::meshSetChannel(const uint8_t theChannel) {
PHY_SetChannel(theChannel);
channel = theChannel;
//eeprom_update_byte((uint8_t *)8179, channel);
}
void ApioClass::meshSetPower(const uint8_t theTxPower) {
/* Page 116 of the 256RFR2 datasheet
0 3.5 dBm
1 3.3 dBm
2 2.8 dBm
3 2.3 dBm
4 1.8 dBm
5 1.2 dBm
6 0.5 dBm
7 -0.5 dBm
8 -1.5 dBm
9 -2.5 dBm
10 -3.5 dBm
11 -4.5 dBm
12 -6.5 dBm
13 -8.5 dBm
14 -11.5 dBm
15 -16.5 dBm
*/
PHY_SetTxPower(theTxPower);
txPower = theTxPower;
//eeprom_update_byte((uint8_t *)8178, theTxPower);
}
void ApioClass::meshSetDataRate(const uint8_t theRate) {
/* Page 123 of the 256RFR2 datasheet
0 250 kb/s | -100 dBm
1 500 kb/s | -96 dBm
2 1000 kb/s | -94 dBm
3 2000 kb/s | -86 dBm
*/
TRX_CTRL_2_REG_s.oqpskDataRate = theRate;
dataRate = theRate;
//eeprom_update_byte((uint8_t *)8126, theRate);
}
void ApioClass::meshSetSecurityKey(const uint8_t *key) {
NWK_SetSecurityKey((uint8_t *)key);
/*for (int i=0; i<16; i++) {
eeprom_update_byte((uint8_t *)8162+i, key[i]);
}*/
}
void ApioClass::meshGetSecurityKey(char *key) {
for (int i=0; i<16; i++) {
key[i] = eeprom_read_byte((uint8_t *)8162+i);
}
}
void ApioClass::meshResetSecurityKey(void) {
const uint8_t buf[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
meshSetSecurityKey(buf);
}
void ApioClass::meshListen(uint8_t endpoint, bool (*handler)(NWK_DataInd_t *ind)) {
NWK_OpenEndpoint(endpoint, handler);
}
void ApioClass::meshJoinGroup(uint16_t groupAddress) {
if (!NWK_GroupIsMember(groupAddress)) {
NWK_GroupAdd(groupAddress);
}
}
void ApioClass::meshLeaveGroup(uint16_t groupAddress) {
if (NWK_GroupIsMember(groupAddress)) {
NWK_GroupRemove(groupAddress);
}
}
bool ApioClass::meshIsInGroup(uint16_t groupAddress) {
return NWK_GroupIsMember(groupAddress);
}
uint16_t ApioClass::getAddress() {
return address;
}
uint16_t ApioClass::getPanId() {
return panId;
}
uint8_t ApioClass::getChannel() {
return channel;
}
uint8_t ApioClass::getTxPower() {
return txPower;
}
const char* ApioClass::getSketchName() {
return sketchName;
}
const char* ApioClass::getSketchRevision() {
return sketchRevision;
}
int32_t ApioClass::getSketchBuild() {
return sketchBuild;
}
const char* ApioClass::getTxPowerDb() {
switch (txPower) {
case 0:
return PSTR("3.5 dBm");
break;
case 1:
return PSTR("3.3 dBm");
break;
case 2:
return PSTR("2.8 dBm");
break;
case 3:
return PSTR("2.3 dBm");
break;
case 4:
return PSTR("1.8 dBm");
break;
case 5:
return PSTR("1.2 dBm");
break;
case 6:
return PSTR("0.5 dBm");
break;
case 7:
return PSTR("-0.5 dBm");
break;
case 8:
return PSTR("-1.5 dBm");
break;
case 9:
return PSTR("-2.5 dBm");
break;
case 10:
return PSTR("-3.5 dBm");
break;
case 11:
return PSTR("-4.5 dBm");
break;
case 12:
return PSTR("-6.5 dBm");
break;
case 13: