-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathADB922S.cpp
977 lines (854 loc) · 23.4 KB
/
ADB922S.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
/*
* ADB922S.cpp
*
*
* The MIT License
*
* Copyright (c) 2017-2018 Tomoaki Yamaguchi tomoaki@tomy-tech.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <Application.h>
#include <SoftwareSerial.h>
#include <string.h>
#include <stdarg.h>
#include <ADB922S.h>
#include <Payload.h>
#include <AppDefine.h>
using namespace tomyApplication;
extern int getFreeMemory(void);
extern PortList_t thePortList[];
const char* loraTxUnconfirmCmd = "lorawan tx ucnf";
const char* loraTxConfirmCmd = "lorawan tx cnf";
#define LoRa_Rx_PIN 11
#define LoRa_Tx_PIN 12
#define LoRa_WAKEUP_PIN 7
//
//
// Class ADB922S
//
//
ADB922S::ADB922S(void):
_baudrate{9600}, _joinStatus{not_joined}, _txTimeoutValue{LoRa_RECEIVE_DELAY2}, _stat{0}, _txOn{false}, _minDR{DR2},
_minDROn{false}, _adrAckCnt{0}, _adrReqBitOn{false}, _adrAckDelay{ADR_ACK_DELAY}, _adrAckLimit{ADR_ACK_LIMIT}, _noFreeChCnt{0},
_nbGw{0}, _margin{0}, _chStat{0}, _joinRetryCount{0}
{
_serialPort = new SoftwareSerial(LoRa_Rx_PIN, LoRa_Tx_PIN);
_txRetryCount = 1;
_maxPayloadSize = LoRa_DEFAULT_PAYLOAD_SIZE;
}
ADB922S::~ADB922S(void)
{
}
bool ADB922S::begin(uint32_t baudrate, LoRaDR dr, uint8_t txRetry, uint8_t joinRetry )
{
pinMode(LoRa_Rx_PIN, INPUT);
pinMode(LoRa_WAKEUP_PIN, OUTPUT);
_baudrate = baudrate;
_txTimeoutValue = LoRa_RECEIVE_DELAY2;
_serialPort->setTimeout(LoRa_SERIAL_WAIT_TIME);
_minDR = dr;
_txRetryCount = txRetry;
_joinRetryCount = joinRetry;
for ( uint8_t retry = 0; retry < 3; retry++ )
{
if ( checkBaudrate(_baudrate) < 0 )
{
char cmd[24];
uint32_t br[] = { 9600, 19200, 57600, 115200};
for ( uint8_t i = 0; i < 4; i++ )
{
if ( checkBaudrate(br[i]) == 0 )
{
LoRaDebug( F("Set Baudrate to %ld\n"), baudrate);
String fmt = F("mod set_baudrate %ld");
sprintf(cmd, fmt.c_str(), baudrate);
send(cmd, F(""),F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME);
_serialPort->begin(_baudrate);
if (send(F("mod set_echo off"), F("Ok"),F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) != LORA_RC_SUCCESS )
{
return false;
}
else
{
goto success_exit;
}
}
}
}
else
{
goto success_exit;
}
}
return false;
success_exit:
setDr(dr);
setTxRetryCount(_txRetryCount);
if ( send(F("mod save"), F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) == LORA_RC_SUCCESS )
{
return true;
}
else
{
return false;
}
}
void ADB922S::clearCmd(void)
{
_serialPort->listen();
send(F("\n"), F(""),F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME);
}
int ADB922S::checkBaudrate(uint32_t baudrate)
{
LoRaDebug( F("Baudrate = %ld\n"), baudrate);
_serialPort->begin(baudrate);
clearCmd();
return reset();
}
bool ADB922S::setTxRetryCount(uint8_t retry)
{
char cmd[26];
String fmt = F("lorawan set_txretry %d");
sprintf(cmd, fmt.c_str(), retry);
if ( send(cmd, F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) == 0 )
{
_txRetryCount = retry;
_txTimeoutValue = (uint32_t)(_txRetryCount + 1) * LoRa_RECEIVE_DELAY2;
return true;
}
return false;
}
bool ADB922S::checkADR(bool confirm)
{
if ( confirm || _adrReqBitOn )
{
return true;
}
else
{
return false;
}
}
uint8_t ADB922S::getTxRetryCount(void)
{
char count[4];
send( F("lorawan get_txretry"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, count, 3);
return (uint8_t) atoi(count);
}
void ADB922S::sleep(void)
{
digitalWrite(LoRa_WAKEUP_PIN, LOW);
send( F("mod sleep 1 1 0"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME);
}
void ADB922S::wakeup(void)
{
digitalWrite(LoRa_WAKEUP_PIN, HIGH);
digitalWrite(LoRa_WAKEUP_PIN, LOW);
clearCmd();
}
bool ADB922S::isJoin(void)
{
return connect(false);
}
bool ADB922S::join(void)
{
for ( uint8_t i = 0; i < _joinRetryCount; i++ )
{
DebugPrint(F("try to join... "));
if ( connect(true) )
{
_chStat = 0;
for ( uint8_t j = 0; j < 16; j++ )
{
_chStat = _chStat << 1;
if ( getChStat(j) )
{
_chStat |= 0x00001;
}
}
DebugPrint(F("accepted.\n"));
return true;
}
for ( uint8_t j = 0; j < 40 && i == _joinRetryCount; j++ )
{
delay(1000); // Duty 1%
}
}
DebugPrint(F("fail to join.\n"));
return false;
}
bool ADB922S::connect(bool reconnect)
{
String cmd;
int rc = 0;
clearCmd();
if(!reconnect)
{
rc = send(F("lorawan get_join_status"), F("joined"),F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME);
if ( rc == LORA_RC_SUCCESS )
{
_joinStatus = joined;
return true;
}
_joinStatus = not_joined;
}
//
// LoRa module join to Network Server by OTAA
//
if ( send(F("lorawan join otaa"), F("accepted"), F(""), ECHOFLAG, JOIN__WAIT_TIME ) != LORA_RC_SUCCESS )
{
_joinStatus = not_joined;
return false;
}
_joinStatus = joined;
return true;
}
//
//
// Getters
//
//
void ADB922S::getHwModel(char* val, uint8_t valLen)
{
send(F("mod get_hw_model"), F(""),F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, val, valLen);
}
void ADB922S::getVersion(char* val, uint8_t valLen)
{
send(F("mod get_ver"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, val, valLen);
}
void ADB922S::getEUI(char* val, uint8_t valLen)
{
send(F("lorawan get_deveui"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, val, valLen);
}
uint8_t ADB922S::getMaxPayloadSize(void)
{
return _maxPayloadSize;
}
//
//
// sendCmd( )
// Return value: LoRa_RV_SUCCESS, LoRa_RV_DATA_TOO_LONG, LoRa_RV_NOT_JOINED, LoRa_RV_ERROR
//
//
int ADB922S::send(String cmd, String resp1, String resp2 , bool echo, uint32_t timeout, char* returnVal, uint8_t len)
{
uint32_t tim = 0;
uint32_t time = (timeout == 0 ? _txTimeoutValue : timeout );
String resp;
int rc = 0;
String endCmd;
_serialPort->listen();
if ( echo )
{
ConsolePrint(F("\nSend =>%s<=\n"), cmd.c_str());
}
if (cmd.indexOf(F("mod reset")) >= 0 )
{
endCmd =F("\n\r\n\r> ");
}
else
{
endCmd = F("\r> ");
}
_serialPort->print(cmd);
_serialPort->print(F("\r"));
_serialPort->flush();
delay(200);
// Recive a response from the module.
tim = millis() + time + 100;
while (millis() < tim)
{
if (_serialPort->available() > 0)
{
char ch = _serialPort->read();
resp += String(ch);
LoRaDebug(F("%02x "), ch);
if ( resp.indexOf(endCmd) >= 0 )
{
break;
}
}
}
if ( echo )
{
ConsolePrint(F("\nRecv =>%s<=\n"), resp.c_str());
}
// set the response to a specified area if required.
if ( returnVal && len )
{
int idx1 = 0;
int idx2 = 0;
int idx3 = 0;
if ( ( idx1 = resp.indexOf(F(">>"))) > 0 )
{
// check LinkCheck Response because it's longer than payloadLen.
if ( (idx2 = resp.indexOf(F("DemodMargin") )) > 0 )
{
if ( (idx3 = resp.indexOf(F("NbGateways"))) > 0 )
{
_margin = (uint8_t)resp.substring( idx2 + 13, idx3 - 3).toInt();
if ( (idx2 = resp.indexOf(F(">>"), idx3)) > idx3 )
{
_nbGw = (uint8_t)resp.substring( idx3 + 12, idx2 - 2).toInt();
}
}
// set ADR_ACK_CNT (-1)
_adrAckCnt = 65535; // (uint16_t ) 65535 = (int ) -1
_minDROn = false;
return LORA_RC_SUCCESS;
}
if ( (idx2 = resp.indexOf(F(">>"), idx1 + 2)) > idx1 )
{
idx1 = idx2;
}
if ( (idx2 = resp.indexOf(F("\r"), idx1)) > idx1 )
{
memset(returnVal, 0, len);
strncpy(returnVal, resp.substring(idx1 + 3, idx2 - 1).c_str(), len - 1);
LoRaDebug(F("Recv =>%s<=\n"), returnVal);
}
}
}
// Check the response with specified response string.
if ( resp1 == "" && resp2 == "" )
{
rc = LORA_RC_SUCCESS;
}
else if (resp.indexOf(resp1) >= 0 || ( resp2 != "" && resp.indexOf(resp2) >= 0 ))
{
_noFreeChCnt = 0;
rc = LORA_RC_SUCCESS;
}
else if (resp.indexOf(F("not_joined")) >= 0 )
{
_joinStatus = not_joined;
rc = LORA_RC_NOT_JOINED;
DebugPrint(F("Data too long\n"));
}
else if (resp.indexOf(F("no_free_ch")) >= 0 )
{
DebugPrint(F("not joined\n"));
if ( _noFreeChCnt > MAX_NO_FREE_CH_CNT)
{
reset();
LoRaDebug(F(" Reset\n"));
_joinStatus = not_joined;
_noFreeChCnt = 0;
rc = LORA_RC_NOT_JOINED;
}
else
{
_noFreeChCnt++;
rc = LORA_RC_NO_FREE_CH;
DebugPrint(F("no free ch.\n"));
}
}
else if (resp.indexOf(F("invalid_data_length")) >= 0 )
{
rc = LORA_RC_DATA_TOO_LONG;
DebugPrint(F("Data too long\n"));
}
else if (resp.indexOf(F("busy")) >= 0 )
{
rc = LORA_RC_BUSY;
DebugPrint(F("busy\n"));
}
else
{
rc = LORA_RC_ERROR;
DebugPrint(F("no resp\n"));
}
#ifdef FREE_MEMORY_CHECK
ConsolePrint(F("Free RAM is %d bytes\n"), getFreeMemory());
#endif
return rc;
}
int ADB922S::send(const __FlashStringHelper* cmd, const __FlashStringHelper* resp1, const __FlashStringHelper* resp2, bool echo, uint32_t timeout, char* returnVal, uint8_t len)
{
String scmd = String(cmd);
String sresp1 = String(resp1);
String sresp2 = String(resp2);
int rc = send(scmd, sresp1, sresp2, echo, timeout, returnVal, len);
return rc;
}
//
//
// sendData( )
// Return value: LoRa_RV_SUCCESS, LoRa_RV_DATA_TOO_LONG, LoRa_RV_NOT_JOINED, LoRa_RV_ERROR
//
//
//#define LORA_BUFF_LEN (20 + _maxPayloadSize+1 +1 ) // last 1 is for checking data length
//#define LORA_BINARY_BUFF_LEN (20 + _maxPayloadSize*2+1 +1 )
int ADB922S::sendData(uint8_t port, bool echo, const __FlashStringHelper* format, ...)
{
va_list args;
va_start(args, format);
int rc = transmitString(port, echo, false, format, args);
va_end(args);
return rc;
}
int ADB922S::sendDataConfirm(uint8_t port, bool echo, const __FlashStringHelper* format, ...)
{
va_list args;
va_start(args, format);
int rc = transmitString(port, echo, true, format, args);
va_end(args);
return rc;
}
int ADB922S::sendPayload(uint8_t port, bool echo, Payload* payload)
{
return transmitBinaryData(port, echo, false, payload->getRowData(), payload->getLen());
}
int ADB922S::sendPayloadConfirm(uint8_t port, bool echo, Payload* payload)
{
return transmitBinaryData(port, echo, true, payload->getRowData(), payload->getLen());
}
int ADB922S::transmitString(uint8_t port, bool echo, bool confirm, const __FlashStringHelper* format, va_list args)
{
char data[(22 + _maxPayloadSize * 2 ) ];
memset(data, 0, (22 + _maxPayloadSize * 2) );
char buffer[(22 + _maxPayloadSize ) ];
char* pos = 0;
int len = 0;
if ( _joinStatus == not_joined )
{
LoRaDebug(F("Module is not joined. try to join.\n"));
if ( !join() )
{
_stat = LORA_RC_NOT_JOINED;
goto exit;
}
}
// ADR control
controlADR();
confirm = checkADR(confirm);
if (confirm)
{
strcpy(data, loraTxConfirmCmd);
pos = data + strlen(loraTxConfirmCmd);
}
else
{
strcpy(data, loraTxUnconfirmCmd);
pos = data +strlen(loraTxUnconfirmCmd);
}
sprintf(pos, " %d ", port);
len = strlen(data);
pos = data + len;
vsnprintf_P(pos, sizeof(data) - len, (const char*)format, args);
_stat = send(data, "tx_ok", "rx", echo, _txTimeoutValue, buffer, (20 + _maxPayloadSize+1 +1 ) );
checkRecvData(buffer, confirm);
exit:
return _stat;
}
int ADB922S::transmitBinaryData(uint8_t port, bool echo, bool confirm, uint8_t* binaryData, uint8_t dataLen)
{
uint8_t* bd = binaryData;
size_t len = 0;
char* pos = 0;
char data[(22 + _maxPayloadSize*2 )];
char buffer[(22 + _maxPayloadSize*2 )];
if ( _joinStatus == not_joined )
{
LoRaDebug(F("Module is not joined. try to join.\n"));
if ( !join() )
{
_stat = LORA_RC_NOT_JOINED;
goto exit;
}
}
// ADR control
controlADR();
confirm = checkADR(confirm);
memset(data, 0, (22 + _maxPayloadSize*2 ));
if (confirm)
{
strcpy(data, loraTxConfirmCmd);
len = strlen(loraTxConfirmCmd);
pos = data + len;
}
else
{
strcpy(data, loraTxUnconfirmCmd);
len = strlen(loraTxUnconfirmCmd);
pos = data + len;
}
sprintf(pos, " %d ", port);
len = strlen(data);
pos = data + len;
for ( uint8_t i = 0; i < dataLen; i++, bd++ )
{
sprintf(pos, "%02x", *bd);
pos = data + strlen(data);
}
_stat = send(data, F("tx_ok"), F("rx"), echo, _txTimeoutValue, buffer, (20 + _maxPayloadSize*2+1 +1 ) );
checkRecvData(buffer, confirm);
exit:
return _stat;
}
void ADB922S::checkRecvData(char* buff, bool conf)
{
if ( _stat == LORA_RC_SUCCESS )
{
if ( conf )
{
_adrAckCnt = 0;
_adrReqBitOn = false;
_minDROn = false;
}
else
{
if ( strncmp(buff, "DemodMargin =", 13 ) == 0)
{
_adrAckCnt = 0;
_minDROn = false;
}
else
{
_adrAckCnt++;
}
}
if ( strncmp(buff, "rx", 2) == 0 )
{
_downLinkData = String(buff + 3);
_txOn= true;
return;
}
}
else if ( _stat == LORA_RC_ERROR )
{
_adrAckCnt++;
}
_downLinkData = F("");
_txOn = false;
}
void ADB922S::controlADR(void)
{
ADRDebug(F(" \n-----ADR Info-----\nADR_ACK_CNT=%d\n"), _adrAckCnt);
if ( _adrOn && _adrAckCnt >= _adrAckLimit )
{
_adrReqBitOn = true;
if ( _adrAckCnt < _adrAckLimit + _adrAckDelay )
{
_maxPwrOn = false;
}
if ( _adrAckCnt >= _adrAckLimit )
{
_adrReqBitOn = true;
}
set_DR_Lower:
if ( (_adrAckCnt == _adrAckLimit + 5 * _adrAckDelay && !_minDROn) ||
(_adrAckCnt == _adrAckLimit + 4 * _adrAckDelay && !_minDROn) ||
(_adrAckCnt == _adrAckLimit + 3 * _adrAckDelay && !_minDROn) ||
(_adrAckCnt == _adrAckLimit + 2 * _adrAckDelay && !_minDROn) )
{
_minDROn = setLowerDr();
if ( _minDROn )
{
_finalDelay = 0;
}
}
if ( _minDROn )
{
if ( _finalDelay < _adrAckDelay )
{
_finalDelay++;
}
else
{
ADRDebug(F("Enable Channels\n"));
if ( _adrReqBitOn && _finalDelay == _adrAckDelay )
{
_finalDelay++;
uint16_t stats = _chStat;
for ( uint8_t ch = 0; ch < 16; ch++ )
{
bool chs = stats & 0x0001;
stats = stats >> 1;
ADRDebug(F("Ch%02d:%s\n"), 15 - ch, (chs ? "ON": "OFF"));
setChStat(15 - ch, chs); // ToDo: uncomment
}
}
_adrReqBitOn = false;
}
}
if ( _adrAckCnt == _adrAckLimit + _adrAckDelay && !_maxPwrOn)
{
if ( getPwrIndex() == 0 )
{
ADRDebug(F("Power is max\n"));
_adrAckCnt += _adrAckDelay;
_maxPwrOn = true;
goto set_DR_Lower;
}
else
{
_maxPwrOn = setMaxPower();
}
}
if ( _adrReqBitOn )
{
ADRDebug(F("Set ADRReqBit ON\n"));
}
}
ADRDebug(F("-------------------\n"));
}
Payload* ADB922S::getDownLinkPayload(void)
{
_payload.create(_maxPayloadSize);
getDownLinkBinaryData(_payload.getRowData());
return &_payload;
}
uint8_t ADB922S::getDownLinkPort( void)
{
int epos = _downLinkData.lastIndexOf(F(" "));
if ( epos >= 0 )
{
String port = _downLinkData.substring(0, epos);
return atoi(port.c_str());
}
return 0;
}
uint8_t ADB922S::getDownLinkBinaryData(uint8_t* data)
{
int bpos = _downLinkData.indexOf(F(" "));
if ( bpos >= 0 )
{
uint8_t i = 0;
for (bpos++ ; bpos < (uint8_t)_downLinkData.length(); bpos += 2 )
{
data[i] = ctoh(_downLinkData[bpos]);
data[i] = data[i] << 4;
data[i++] |= ctoh(_downLinkData[bpos + 1]);
}
return i;
}
return 0;
}
String ADB922S::getDownLinkData(void)
{
int bpos = _downLinkData.indexOf(F(" "));
if ( bpos >= 0 )
{
return _downLinkData.substring(bpos + 1);
}
return F("");
}
uint8_t ADB922S::ctoh(uint8_t ch)
{
if ( ch >= 0x30 && ch <=0x39 )
{
return ch - 0x30;
}
else if ( ch >= 0x41 && ch <= 0x46 )
{
return ch - 0x37;
}
return 0;
}
int ADB922S::setDr(LoRaDR dr)
{
char cmd[18];
String fmt = F("lorawan set_dr %d");
sprintf(cmd, fmt.c_str(), dr);
if ( send(cmd, F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) == 0 )
{
uint8_t pll[] = { 0, 0, 11, 53, 125, 242 };
_maxPayloadSize = pll[dr];
return _maxPayloadSize;
}
return -1;
}
bool ADB922S::setLowerDr(void )
{
uint8_t dr = getDr();
if (dr <= (uint8_t) _minDR )
{
ADRDebug(F("DR is min %d\n"),dr);
return true;
}
else
{
setDr((LoRaDR)--dr);
ADRDebug(F("Set DR=%d\n"), dr);
return false;
}
}
bool ADB922S::setMaxPower(void)
{
char cmd[20];
ADRDebug(F("Set Power Max\n"));
String cdata = F("lorawan set_power 5");
sprintf(cmd, cdata.c_str());
if ( send(cmd, F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) == 0 )
{
return true;
}
_maxPwrOn = false;
}
bool ADB922S::setADR(bool onOff)
{
char cmd[20];
const char* stat = (onOff ? "on" : "off");
String fmt = F("lorawan set_adr %s");
sprintf(cmd, fmt.c_str(), stat);
if ( send(cmd, F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) == 0 )
{
_adrOn = true;
_adrAckCnt = 0;
_finalDelay = 0;
return true;
}
_adrOn = false;
_adrAckCnt = 0;
return false;
}
bool ADB922S::setADRParams(uint8_t limit, uint8_t delay)
{
_adrAckLimit = limit;
_adrAckDelay = delay;
setADR(true);
}
bool ADB922S::setChStat(uint8_t ch, bool onOff)
{
char cmd[30];
const char* stat = (onOff ? "on" : "off");
String fmt = F("lorawan set_ch_status %d %s");
sprintf(cmd, fmt.c_str(), ch, stat);
if ( send(cmd, F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) == 0 )
{
return true;
}
return false;
}
bool ADB922S::setLinkCheck(void)
{
_margin = _nbGw = 0;
if ( send(F("lorawan set_linkchk"), F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) == 0 )
{
return true;
}
return false;
}
bool ADB922S::saveConfig(void)
{
if ( send(F("lorawan save"), F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME) == 0 )
{
return true;
}
return false;
}
uint8_t ADB922S::getDr(void)
{
char dr[3];
send( F("lorawan get_dr"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, dr, 2);
return (uint8_t) atoi(dr);
}
uint8_t ADB922S::getPwrIndex(void)
{
char pwr[3];
send( F("lorawan get_pwr"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, pwr, 2);
return (uint8_t) atoi(pwr);
}
uint8_t ADB922S::getMargin(void)
{
return _margin;
}
uint8_t ADB922S::getNbGw(void)
{
return _nbGw;
}
bool ADB922S::isAdrOn(void)
{
char stat[5];
send( F("lorawan get_adr"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, stat, 4);
if ( strcmp(stat, "on") == 0 )
{
_adrOn = true;
return true;
}
_adrOn = false;
return false;
}
int ADB922S::getChPara(CHID chId)
{
char cmd[24];
String fmt = F("lorawan get_ch_para %d");
sprintf(cmd, fmt.c_str(), chId);
send(cmd, F("Ok"), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME);
return 1;
}
bool ADB922S::getChStat(CHID chId)
{
char stat[5];
char cmd[26];
String fmt = F("lorawan get_ch_status %d");
sprintf(cmd, fmt.c_str(), chId);
send( cmd, F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, stat, 4);
if ( strcmp(stat, "on") == 0 )
{
return true;
}
return false;
}
int ADB922S::getDcBand(CHID bandId)
{
char cmd[24];
String fmt = F("lorawan get_dc_band %d");
sprintf(cmd, fmt.c_str(), bandId);
send( cmd, F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME);
return 0;
}
/* get a uplink counter that will be used at next transmission.*/
uint16_t ADB922S::getUpcnt(void)
{
char cnt[6];
send( F("lorawan get_upcnt"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, cnt, 5);
return (uint8_t) atoi(cnt);
}
uint16_t ADB922S::getDowncnt(void)
{
char cnt[6];
send( F("lorawan get_downcnt"), F(""), F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME, cnt, 5);
return (uint8_t) atoi(cnt);
}
void ADB922S::checkDownLink(void)
{
if ( _stat == LORA_RC_SUCCESS && _txOn == true )
{
uint8_t port = getDownLinkPort();
if ( port )
{
for ( uint8_t i = 0; thePortList[i].port != 0; i++ )
{
if ( port == thePortList[i].port )
{
thePortList[i].callback();
break;
}
}
}
}
_txOn = false;
}
int ADB922S::reset(void)
{
return send(F("mod reset"), F("TLM922S"),F(""), ECHOFLAG, LoRa_INIT_WAIT_TIME);
}