-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharduinoLoRaWAN.cpp
4839 lines (4214 loc) · 122 KB
/
arduinoLoRaWAN.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
/*
* ERTIS - University of Malaga
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Current version: Version modified by UMA from the Libelium original version
* Original version: http://www.cooking-hacks.com/media/cooking/images/documentation/tutorial_kit_lorawan/LoRaWAN_library_arduino_v1_3.zip
*/
#ifndef __WPROGRAM_H__
#include "arduinoClasses.h"
#include "arduinoUART.h"
#endif
#include "arduinoLoRaWAN.h"
/******************************************************************************
* FLASH DEFINITIONS COMMANDS
******************************************************************************/
const char command_00[] PROGMEM = "sys reset\r\n";
const char command_01[] PROGMEM = "sys factoryRESET\r\n";
const char command_02[] PROGMEM = "sys get hweui\r\n";
const char command_03[] PROGMEM = "sys get vdd\r\n";
const char command_04[] PROGMEM = "mac reset %s\r\n";
const char command_05[] PROGMEM = "mac tx cnf %u %s\r\n";
const char command_06[] PROGMEM = "mac tx uncnf %u %s\r\n";
const char command_07[] PROGMEM = "mac join abp\r\n";
const char command_08[] PROGMEM = "mac save\r\n";
const char command_09[] PROGMEM = "mac pause\r\n";
const char command_10[] PROGMEM = "mac resume\r\n";
const char command_11[] PROGMEM = "mac set devaddr %s\r\n";
const char command_12[] PROGMEM = "mac set deveui %s\r\n";
const char command_13[] PROGMEM = "mac set appeui %s\r\n";
const char command_14[] PROGMEM = "mac set nwkskey %s\r\n";
const char command_15[] PROGMEM = "mac set appskey %s\r\n";
const char command_16[] PROGMEM = "mac set appkey %s\r\n";
const char command_17[] PROGMEM = "mac set pwridx %u\r\n";
const char command_18[] PROGMEM = "mac set dr %u\r\n";
const char command_19[] PROGMEM = "mac set adr %s\r\n";
const char command_20[] PROGMEM = "mac set ch freq %u %lu\r\n";
const char command_21[] PROGMEM = "mac set ch dcycle %u %u\r\n";
const char command_22[] PROGMEM = "mac set ch drrange %u %u %u\r\n";
const char command_23[] PROGMEM = "mac set ch status %u %s\r\n";
const char command_24[] PROGMEM = "mac get devaddr\r\n";
const char command_25[] PROGMEM = "mac get deveui\r\n";
const char command_26[] PROGMEM = "mac get appeui\r\n";
const char command_27[] PROGMEM = "mac get dr\r\n";
const char command_28[] PROGMEM = "mac get band\r\n";
const char command_29[] PROGMEM = "mac get pwridx\r\n";
const char command_30[] PROGMEM = "mac get adr\r\n";
const char command_31[] PROGMEM = "mac get dcycleps\r\n";
const char command_32[] PROGMEM = "mac get mrgn\r\n";
const char command_33[] PROGMEM = "mac get gwnb\r\n";
const char command_34[] PROGMEM = "mac get status\r\n";
const char command_35[] PROGMEM = "mac get ch freq %u\r\n";
const char command_36[] PROGMEM = "mac get ch dcycle %u\r\n";
const char command_37[] PROGMEM = "mac get ch drrange %u\r\n";
const char command_38[] PROGMEM = "mac get ch status %u\r\n";
const char command_39[] PROGMEM = "radio rx 0\r\n";
const char command_40[] PROGMEM = "radio tx %s\r\n";
const char command_41[] PROGMEM = "radio cw on\r\n";
const char command_42[] PROGMEM = "radio cw off\r\n";
const char command_43[] PROGMEM = "radio set mod %s\r\n";
const char command_44[] PROGMEM = "radio set freq %lu\r\n";
const char command_45[] PROGMEM = "radio set pwr %i\r\n";
const char command_46[] PROGMEM = "radio set sf %s\r\n";
const char command_47[] PROGMEM = "radio set rxbw %s\r\n";
const char command_48[] PROGMEM = "radio set rxbw %s.%s\r\n";
const char command_49[] PROGMEM = "radio set bitrate %u\r\n";
const char command_50[] PROGMEM = "radio set fdev %u\r\n";
const char command_51[] PROGMEM = "radio set prlen %u\r\n";
const char command_52[] PROGMEM = "radio set crc %s\r\n";
const char command_53[] PROGMEM = "radio set cr %s\r\n";
const char command_54[] PROGMEM = "radio set wdt %lu\r\n";
const char command_55[] PROGMEM = "radio set bw %u\r\n";
const char command_56[] PROGMEM = "radio get mod\r\n";
const char command_57[] PROGMEM = "radio get freq\r\n";
const char command_58[] PROGMEM = "radio get pwr\r\n";
const char command_59[] PROGMEM = "radio get sf\r\n";
const char command_60[] PROGMEM = "radio get rxbw\r\n";
const char command_61[] PROGMEM = "radio get bitrate\r\n";
const char command_62[] PROGMEM = "radio get cr\r\n";
const char command_63[] PROGMEM = "radio get wdt\r\n";
const char command_64[] PROGMEM = "radio get bw\r\n";
const char command_65[] PROGMEM = "radio get snr\r\n";
const char command_66[] PROGMEM = "radio get crc\r\n";
const char command_67[] PROGMEM = "radio get prlen\r\n";
const char command_68[] PROGMEM = "sys get ver\r\n";
const char command_69[] PROGMEM = "mac set retx %u\r\n";
const char command_70[] PROGMEM = "mac get retx\r\n";
const char command_71[] PROGMEM = "radio get fdev\r\n";
const char command_72[] PROGMEM = "mac set upctr %lu\r\n";
const char command_73[] PROGMEM = "mac get upctr\r\n";
const char command_74[] PROGMEM = "mac set dnctr %lu\r\n";
const char command_75[] PROGMEM = "mac get dnctr\r\n";
const char command_76[] PROGMEM = "mac join otaa\r\n";
const char command_77[] PROGMEM = "mac set linkchk %u\r\n";
const char command_78[] PROGMEM = "mac set rx2 %u %lu\r\n";
const char command_79[] PROGMEM = "mac set rxdelay1 %u\r\n";
const char command_80[] PROGMEM = "mac reset\r\n";
const char* const table_LoRaWAN_COMMANDS[] PROGMEM=
{
command_00,
command_01,
command_02,
command_03,
command_04,
command_05,
command_06,
command_07,
command_08,
command_09,
command_10,
command_11,
command_12,
command_13,
command_14,
command_15,
command_16,
command_17,
command_18,
command_19,
command_20,
command_21,
command_22,
command_23,
command_24,
command_25,
command_26,
command_27,
command_28,
command_29,
command_30,
command_31,
command_32,
command_33,
command_34,
command_35,
command_36,
command_37,
command_38,
command_39,
command_40,
command_41,
command_42,
command_43,
command_44,
command_45,
command_46,
command_47,
command_48,
command_49,
command_50,
command_51,
command_52,
command_53,
command_54,
command_55,
command_56,
command_57,
command_58,
command_59,
command_60,
command_61,
command_62,
command_63,
command_64,
command_65,
command_66,
command_67,
command_68,
command_69,
command_70,
command_71,
command_72,
command_73,
command_74,
command_75,
command_76,
command_77,
command_78,
command_79,
command_80,
};
/******************************************************************************
* FLASH DEFINITIONS ANSWERS
******************************************************************************/
const char answer_00[] PROGMEM = "ok";
const char answer_01[] PROGMEM = "invalid_param";
const char answer_02[] PROGMEM = "no_free_ch";
const char answer_03[] PROGMEM = "mac_rx";
const char answer_04[] PROGMEM = "radio_tx_ok";
const char answer_05[] PROGMEM = "RN2483";
const char answer_06[] PROGMEM = "accepted";
const char answer_07[] PROGMEM = "radio_rx ";
const char answer_08[] PROGMEM = "mac_tx_ok";
const char answer_09[] PROGMEM = "on";
const char answer_10[] PROGMEM = "off";
const char answer_11[] PROGMEM = "mac_paused";
const char answer_12[] PROGMEM = "radio_err";
const char answer_13[] PROGMEM = "\r\n";
const char answer_14[] PROGMEM = "4294967245";
const char answer_15[] PROGMEM = "mac_err";
const char answer_16[] PROGMEM = "invalid_data_len";
const char answer_17[] PROGMEM = "keys_not_init";
const char answer_18[] PROGMEM = "not_joined";
const char answer_19[] PROGMEM = "denied";
const char answer_20[] PROGMEM = "RN2903";
const char answer_21[] PROGMEM = "invalid_param";
const char answer_22[] PROGMEM = "ok";
const char* const table_LoRaWAN_ANSWERS[] PROGMEM =
{
answer_00,
answer_01,
answer_02,
answer_03,
answer_04,
answer_05,
answer_06,
answer_07,
answer_08,
answer_09,
answer_10,
answer_11,
answer_12,
answer_13,
answer_14,
answer_15,
answer_16,
answer_17,
answer_18,
answer_19,
answer_20,
answer_21,
answer_22
};
void flushLoraReceiveSerial(){
while( serialRead(UART) != -1);
}
/******************************************************************************
* User API
******************************************************************************/
////////////////////////////////////////////////////////////////////////////////
// System functions
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief This function powers on the module
*/
uint8_t arduinoLoRaWAN::ON()
{
uint8_t error;
_baudrate = 57600;
_uart = UART;
beginUART();
delay(500);
// the module shows a message when is turned on
// we have to flush this data in order to avoid misunderstandings with other commands
flushLoraReceiveSerial();
error = check();
return error;
}
/*!
* @brief This function powers down the module
* @return
* @arg '0' if OK
*/
uint8_t arduinoLoRaWAN::OFF()
{
closeUART();
return LORAWAN_ANSWER_OK;
}
/*!
* @brief This function resets and restart the stored internal configurations
* will be loaded upon reboot and saves modules version.
*
* @return
* @arg '0' if OK
* @arg '2' if no answer
*
*/
uint8_t arduinoLoRaWAN::reset()
{
uint8_t status;
char ans1[15];
char ans2[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
memset(ans2,0x00,sizeof(ans2));
// create "sys reset" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[0])));
// create "RN2483" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[5])));
// create "RN2903" answer
sprintf_P(ans2,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[20])));
//send command and wait for ans
status = sendCommand(_command,ans1,ans2,600);
if (status == 1)
{
_version = RN2483_MODULE;
return LORAWAN_ANSWER_OK;
}
else if (status == 2)
{
_version = RN2903_MODULE;
return LORAWAN_ANSWER_OK;
}
else
{
_version = 0;
return LORAWAN_NO_ANSWER;
}
}
/*!
* @brief This function resets the module's configuration data and user
* EEPROM to factory default values, restarts the module and saves
* modules version.
*
* @return
* @arg '0' if OK
* @arg '2' if no answer
*
*/
uint8_t arduinoLoRaWAN::factoryReset()
{
uint8_t status;
char ans1[15];
char ans2[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
memset(ans2,0x00,sizeof(ans2));
// create "sys factoryRESET" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[1])));
// create "RN2483" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[5])));
// create "RN2903" answer
sprintf_P(ans2,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[20])));
//send command and wait for ans
status = sendCommand(_command,ans1,ans2,5000);
if (status == 1)
{
waitFor((char *)"\r\n",1000);
_version = RN2483_MODULE;
return LORAWAN_ANSWER_OK;
}
else if (status == 2)
{
waitFor((char *)"\r\n",1000);
_version = RN2903_MODULE;
return LORAWAN_ANSWER_OK;
}
else
{
return LORAWAN_NO_ANSWER;
}
}
/*!
* @brief This function gets hardware EUI from the module
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
*/
uint8_t arduinoLoRaWAN::getEUI()
{
uint8_t status;
char ans1[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
// create "sys get hweui" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[2])));
// create "invalid_param" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[1])));
//send command and wait for ans
status = sendCommand(_command,(char *)"\r\n",ans1,500);
if (status == 0)
{
return LORAWAN_NO_ANSWER;
}
else if (status == 1)
{
char* pch = strtok((char*)_buffer,"\r\n");
if (pch != NULL)
{
memset(_eui,0x00,sizeof(_eui));
strncpy(_eui, pch, sizeof(_eui));
return LORAWAN_ANSWER_OK;
}
else
{
return LORAWAN_ANSWER_ERROR;
}
}
else
{
return LORAWAN_ANSWER_ERROR;
}
}
/*!
* @brief This function gets hardware EUI from the module nad stores
* last 4 byte as devAddres
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
*/
uint8_t arduinoLoRaWAN::getAddr()
{
uint8_t status;
char ans1[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
// create "sys get hweui" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[2])));
// create "invalid_param" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[1])));
//send command and wait for ans
status = sendCommand(_command,(char *)"\r\n",ans1,500);
if (status == 0)
{
return LORAWAN_NO_ANSWER;
}
else if (status == 1)
{
char* pch = strtok((char*)_buffer,"\r\n");
if (pch != NULL)
{
memset(_devAddr,0x00,sizeof(_devAddr));
strncpy(_devAddr, pch+8, sizeof(_devAddr));
return LORAWAN_ANSWER_OK;
}
else
{
return LORAWAN_ANSWER_ERROR;
}
}
else
{
return LORAWAN_ANSWER_ERROR;
}
}
/*!
* @brief This functions gets supply power from the module
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
*/
uint8_t arduinoLoRaWAN::getSupplyPower()
{
uint8_t status;
char ans1[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
// create "sys get vdd" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[3])));
// create "invalid_param" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[1])));
//send command and wait for ans
status = sendCommand(_command,(char *)"\r\n",ans1,500);
if (status == 1)
{
_supplyPower = parseIntValue();
if (_supplyPower == 0)
{
return LORAWAN_ANSWER_ERROR;
}
return LORAWAN_ANSWER_OK;
}
else if (status == 2)
{
return LORAWAN_ANSWER_ERROR;
}
else
{
return LORAWAN_NO_ANSWER;
}
}
/*!
* @brief Checks if module is ready to use and saves which kind of
* module has been plugged to Arduino, either RN2483 or RN2903
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
*
*/
uint8_t arduinoLoRaWAN::check()
{
uint8_t status;
char ans1[15];
char ans2[15];
char ans3[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
memset(ans2,0x00,sizeof(ans2));
memset(ans3,0x00,sizeof(ans3));
// create "sys get ver" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[68])));
// create "RN2483" command
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[5])));
// create "RN2903" answer
sprintf_P(ans2,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[20])));
// create "invalid_param" answer
sprintf_P(ans3,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[21])));
//send command and wait for ans
status = sendCommand(_command,ans1,ans2,ans3,1000);
if (status == 1)
{
_version = RN2483_MODULE;
return LORAWAN_ANSWER_OK;
}
if (status == 2)
{
_version = RN2903_MODULE;
return LORAWAN_ANSWER_OK;
}
else if (status == 3)
{
return LORAWAN_ANSWER_ERROR;
}
else
{
return LORAWAN_NO_ANSWER;
}
}
////////////////////////////////////////////////////////////////////////////////
// LoRaWAN functions
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief This function is used to reset LoRaWAN configuration and set working band.
*
* @param char* band: working LoRaWAN band: "433","868" or "900"
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
* @arg '7' if input parameter error
*/
uint8_t arduinoLoRaWAN::resetMacConfig(char* band)
{
uint8_t status;
char ans1[15];
char ans2[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
memset(ans2,0x00,sizeof(ans2));
if ((strcmp(band, "433")) && (strcmp(band, "868")) && (strcmp(band, "900")))
{
return LORAWAN_INPUT_ERROR;
}
if (_version == RN2903_MODULE)
{
// create "mac reset" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[80])));
// create "ok" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[0])));
// create "invalid_param" answer
sprintf_P(ans2,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[1])));
//send command and wait for ans
status = sendCommand(_command,ans1,ans2,1000);
if (status == 1)
{
return LORAWAN_ANSWER_OK;
}
else if (status == 2)
{
return LORAWAN_ANSWER_ERROR;
}
else
{
return LORAWAN_NO_ANSWER;
}
}
// create "mac reset" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[4])), band);
// create "ok" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[0])));
// create "invalid_param" answer
sprintf_P(ans2,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[1])));
//send command and wait for ans
status = sendCommand(_command,ans1,ans2,1000);
if (status == 1)
{
return LORAWAN_ANSWER_OK;
}
else if (status == 2)
{
return LORAWAN_ANSWER_ERROR;
}
else
{
return LORAWAN_NO_ANSWER;
}
}
/*!
* @brief This function gets hardware EUI and sets MAC devEUI
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
*/
uint8_t arduinoLoRaWAN::setDeviceEUI()
{
uint8_t error = getEUI();
if (error != 0)
{
return error;
}
return setDeviceEUI(_eui);
}
/*!
* @brief This function sets MAC devEUI
*
* @param char* EUI: EUI to be set
*
* @remarks EUI is a sequence of digit representing the value of devEUI
* expressed in hexadecimal value (i.e. EUI = 0004A30B001A836D
* – address is composed by the following byte stream:
* 0x00, 0x04, 0xA3, 0x0B, 0x00, 0x1A, 0x83, 0x6D - 16 digit
* converted in 8 bytes).
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
* @arg '7' if input parameter error
*/
uint8_t arduinoLoRaWAN::setDeviceEUI(char* eui)
{
uint8_t status;
char ans1[15];
char ans2[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
memset(ans2,0x00,sizeof(ans2));
// check eui length
if (strlen(eui)!=16) return LORAWAN_INPUT_ERROR;
// check if eui is a hexadecimal string
for (uint8_t i=0;i<16;i++)
{
if (((eui[i] < 0x30) || (eui[i] > 0x39)) && ((eui[i] < 0x41) || (eui[i] > 0x46)))
{
return LORAWAN_INPUT_ERROR;
}
}
// create "mac set deveui" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[12])), eui);
// create "ok" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[0])));
// create "invalid_param" answer
sprintf_P(ans2,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[1])));
//send command and wait for ans
status = sendCommand(_command,ans1,ans2,100);
if (status == 1)
{
memset(_devEUI,0x00,sizeof(_devEUI));
strncpy(_devEUI,eui,sizeof(_devEUI));
return LORAWAN_ANSWER_OK;
}
else if (status == 2)
{
return LORAWAN_ANSWER_ERROR;
}
else
{
return LORAWAN_NO_ANSWER;
}
}
/*!
* @brief This function gets the MAC device EUI from module
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
*/
uint8_t arduinoLoRaWAN::getDeviceEUI()
{
uint8_t status;
char ans1[20];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
// create "mac get deveui" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[25])));
// create "invalid_param" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[21])));
//send command and wait for ans
status = sendCommand(_command,(char *)"\r\n",ans1,300);
if (status == 0)
{
return LORAWAN_NO_ANSWER;
}
else if (status == 1)
{
char* pch = strtok((char*)_buffer,"\r\n");
if (pch != NULL)
{
memset(_devEUI,0x00,sizeof(_devEUI));
strncpy(_devEUI, pch, sizeof(_devEUI));
return LORAWAN_ANSWER_OK;
}
else
{
return LORAWAN_ANSWER_ERROR;
}
}
else
{
return LORAWAN_ANSWER_ERROR;
}
}
/*!
* @brief This function gets hardware EUI and sets MAC devAddress
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
*/
uint8_t arduinoLoRaWAN::setDeviceAddr()
{
getAddr();
return setDeviceAddr(_devAddr);
}
/*!
* @brief This function sets MAC devAddress
*
* @param char* addr: addr to be set
*
* @remarks addr is a sequence of digit representing the value of addres
* expressed in hexadecimal value (i.e. addr = 001A836D – address
* is composed by the following byte stream: 0x00, 0x1A, 0x83, 0x6D
* – 8 digit converted in 4 bytes).
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
* @arg '7' if input parameter error
*/
uint8_t arduinoLoRaWAN::setDeviceAddr(char* addr)
{
uint8_t status;
char ans1[15];
char ans2[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
memset(ans2,0x00,sizeof(ans2));
// check addr length
if (strlen(addr)!=8) return LORAWAN_INPUT_ERROR;
// check if addr is a hexadecimal string
for (uint8_t i=0;i<8;i++)
{
if (((addr[i] < 0x30) || (addr[i] > 0x39)) && ((addr[i] < 0x41) || (addr[i] > 0x46)))
{
return LORAWAN_INPUT_ERROR;
}
}
// create "mac set devaddr" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[11])), addr);
// create "ok" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[0])));
// create "invalid_param" answer
sprintf_P(ans2,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[1])));
//send command and wait for ans
status = sendCommand(_command,ans1,ans2,100);
if (status == 1)
{
memset(_devAddr,0x00,sizeof(_devAddr));
strncpy(_devAddr,addr,sizeof(_devAddr));
return LORAWAN_ANSWER_OK;
}
else if (status == 2)
{
return LORAWAN_ANSWER_ERROR;
}
else
{
return LORAWAN_NO_ANSWER;
}
}
/*!
* @brief This function gets the MAC device Address from module
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
*/
uint8_t arduinoLoRaWAN::getDeviceAddr()
{
uint8_t status;
char ans1[50];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
// create "mac get devaddr" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[24])));
// create "invalid_param" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[21])));
//send command and wait for ans
status = sendCommand(_command,(char *)"\r\n",ans1,300);
if (status == 0)
{
return LORAWAN_NO_ANSWER;
}
else if (status == 1)
{
char* pch = strtok((char*)_buffer,"\r\n");
if (pch != NULL)
{
memset(_devAddr,0x00,sizeof(_devAddr));
strncpy(_devAddr, pch, sizeof(_devAddr));
return LORAWAN_ANSWER_OK;
}
else
{
return LORAWAN_ANSWER_ERROR;
}
}
else
{
return LORAWAN_ANSWER_ERROR;
}
}
/*!
* @brief This function sets MAC Network Session Key
*
* @param char* key: key to be set
*
* @remarks key is a sequence of digit representing the value of NwkSKey
* expressed in hexadecimal value (i.e. key = 000102030405060708091011121314
* 32 digit converted in 16 bytes).
*
* @return
* @arg '0' if OK
* @arg '1' if error
* @arg '2' if no answer
* @arg '7' if input parameter error
*/
uint8_t arduinoLoRaWAN::setNwkSessionKey(char* key)
{
uint8_t status;
char ans1[15];
char ans2[15];
memset(_command,0x00,sizeof(_command));
memset(ans1,0x00,sizeof(ans1));
memset(ans2,0x00,sizeof(ans2));
// check key length
if (strlen(key)!=32) return LORAWAN_INPUT_ERROR;
// check if key is a hexadecimal string
for (uint8_t i=0;i<32;i++)
{
if (((key[i] < 0x30) || (key[i] > 0x39)) && ((key[i] < 0x41) || (key[i] > 0x46)))
{
return LORAWAN_INPUT_ERROR;
}
}
// create "mac set nwkskey" command
sprintf_P(_command,(char*)pgm_read_word(&(table_LoRaWAN_COMMANDS[14])), key);
// create "ok" answer
sprintf_P(ans1,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[0])));
// create "invalid_param" answer
sprintf_P(ans2,(char*)pgm_read_word(&(table_LoRaWAN_ANSWERS[1])));
//send command and wait for ans
status = sendCommand(_command,ans1,ans2,100);
if (status == 1)
{
memset(_nwkSKey,0x00,sizeof(_nwkSKey));
strncpy(_nwkSKey,key,sizeof(_nwkSKey));
return LORAWAN_ANSWER_OK;
}
else if (status == 2)
{
return LORAWAN_ANSWER_ERROR;
}