-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlora.c
1125 lines (986 loc) · 24.8 KB
/
lora.c
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
#include <sys/types.h>
#include <stdio.h>
#include <getopt.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <pthread.h>
#include <event.h> /* libevent */
/*
* -b serial baudrate
* -a lora airspeed
* -s lora hat tty
* -f lora frequence in Mhz
* -n lora net id
* -p tx power
* -z lora packet size
* -k crypt key
* -v verbose, print rssi & other infos
*/
int bps = 115200, lora_airspeed = 38400, lora_freq = 433, lora_netid = 0, lora_power = 22, lora_buffersize = 240, verbose_mode = 0, lora_addr = 0;
uint16_t lora_key = 0;
char lora_tty[50] = "/dev/ttyS0";
int lora_fd = -1, tx_blocksize = 200;
struct event_base *ev_base = NULL;
struct event lora_event;
uint8_t lora_rbuf[512];
int lora_rpos = 0, lora_rsize = 512;
#define M0 "22"
#define M1 "27"
#define AUX "4"
#define LORA_TEXT 0
#define LORA_BINARY 1
#define LORA_FILE 2
#define LORA_MAGIC 0xEB90
#define SX126X_UART_BAUDRATE_1200 0x00
#define SX126X_UART_BAUDRATE_2400 0x20
#define SX126X_UART_BAUDRATE_4800 0x40
#define SX126X_UART_BAUDRATE_9600 0x60
#define SX126X_UART_BAUDRATE_19200 0x80
#define SX126X_UART_BAUDRATE_38400 0xA0
#define SX126X_UART_BAUDRATE_57600 0xC0
#define SX126X_UART_BAUDRATE_115200 0xE0
#define SX126X_PACKAGE_SIZE_240_BYTE 0x00
#define SX126X_PACKAGE_SIZE_128_BYTE 0x40
#define SX126X_PACKAGE_SIZE_64_BYTE 0x80
#define SX126X_PACKAGE_SIZE_32_BYTE 0xC0
#define SX126X_POWER_22DBM 0x00
#define SX126X_POWER_17DBM 0x01
#define SX126X_POWER_13DBM 0x02
#define SX126X_POWER_10DBM 0x03
#define SX126X_AIRSPEED_1200 0x01
#define SX126X_AIRSPEED_2400 0x02
#define SX126X_AIRSPEED_4800 0x03
#define SX126X_AIRSPEED_9600 0x04
#define SX126X_AIRSPEED_19200 0x05
#define SX126X_AIRSPEED_38400 0x06
#define SX126X_AIRSPEED_62500 0x07
void
print_help(void)
{
printf("usage: loraip [optargs]\r\n"
" -a airspeed, set lora airspeed, default is 38400\r\n"
" -A lora address, set lora address, default is 0\r\n"
" -b baudrate, set tty baudrate, default is 115200\r\n"
" -s tty, set lorahat serial tty, default is /dev/ttyS0\r\n"
" -f freq, set lora freq in Mhz, default is 433MHz\r\n"
" -n netid, set lora netid, default is 0\r\n"
" -p power, set lora tx power in dBm, default is 22dBm\r\n"
" -z size, set lorahat packet size, default is 240 Bytes, 128/64/32\r\n"
" -k key, set lora crypt key, default is 0, don't crypt\r\n"
" -B blocksize, set lora file transfer block size, default is 200\r\n"
" -v, verbose mode\r\n"
" -h, print this message\r\n"
);
exit(0);
}
/**
* Dumps a chunk of memory to the screen
*/
void
hex_dump(void *p, int len, int with_header)
{
int i, j = 0, k;
char text[17], *src;
if (p == NULL || len <= 0)
return;
src = (char *)p;
if (with_header)
printf("%p : ", src);
for (i = 0; i < len; i ++) {
text[j] = src[i];
j ++;
printf("%02X ", src[i]);
if (j == 8)
printf(" ");
if ((j == 16) || (i == (len - 1))) {
/* last line or full of 16 bytes */
text[j] = '\0';
for (k = 0; k < j; k ++) {
if ((text[k] < 32) || (text[k] > 126)) {
text[k] = '.';
}
}
/* fill necessary space */
if (j < 8)
printf(" ");
for (; j < 16; j ++) {
printf(" ");
}
printf(" %s\r\n", text);
if (with_header && (i < (len - 1))) {
printf("%p : ", src + i + 1);
}
j = 0;
}
}
if (i % 16)
printf("\r\n");
}
/* return 0 if OK
* return 1 if FAILED
*/
int
set_nonblock(int fd)
{
int flag = 1;
struct linger ling = {0, 0};
int r = 0;
if (fd < 0) return 1;
r = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
r = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(flag));
r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
r = setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
flag = fcntl(fd, F_GETFL, 0);
if (flag >= 0)
fcntl(fd, F_SETFL, flag | O_NONBLOCK);
return r;
}
/* return 0 if found.
* return 1 if not found
*/
int
find_magic(const uint8_t *src, int len, uint16_t match, int *r)
{
int i;
uint8_t key[2];
if (src == NULL || len < 2 || r == NULL) return 1;
key[0] = (uint8_t)((match >> 8) & 0xff);
key[1] = (uint8_t)(match & 0xff);
for (i = 0; i < len - 1; i ++) {
if (src[i] == key[0] && src[i + 1] == key[1]) {
*r = i;
return 0;
}
}
return 1;
}
#define OUTPUT "out"
#define INPUT "in"
#define LOW "0"
#define HIGH "1"
/* return 0 if OK
* return -1 if FAILED
*/
int
setup_pin(char *pin, char *mode)
{
FILE *fp;
char path[65] = "/sys/class/gpio/export";
int k, r = 0;
if (pin == NULL || pin[0] == '\0' || mode == NULL || mode[0] == '\0')
return -1;
fp = fopen(path, "w");
if (fp == NULL) {
fprintf(stderr, "can't write %s: %s\r\n", path, strerror(errno));
return -1;
}
k = strlen(pin);
if (k != fwrite(pin, 1, k, fp)) {
fprintf(stderr, "write %s to file %s failed: %s\r\n", pin, path, strerror(errno));
fclose(fp);
return -1;
}
fclose(fp);
usleep(100000); /* sleep 0.1s before next operation */
snprintf(path, 64, "/sys/class/gpio/gpio%s/direction", pin);
fp = fopen(path, "w");
if (fp == NULL) {
fprintf(stderr, "can't write %s: %s\r\n", path, strerror(errno));
return -1;
}
k = strlen(mode);
if (k != fwrite(mode, 1, k, fp)) {
fprintf(stderr, "write %s to file %s failed: %s\r\n", mode, path, strerror(errno));
r = -1;
}
fclose(fp);
return r;
}
/* return 0 if OK
* return -1 if FAILED
*/
int
write_pin(char *pin, char *value)
{
char path[65];
FILE *fp;
int k, r = 0;
if (pin == NULL || pin[0] == '\0' || value == NULL || value[0] == '\0')
return -1;
snprintf(path, 64, "/sys/class/gpio/gpio%s/value", pin);
fp = fopen(path, "w");
if (fp == NULL) {
fprintf(stderr, "can't write %s: %s\r\n", path, strerror(errno));
return -1;
}
k = strlen(value);
if (k != fwrite(value, 1, k, fp)) {
fprintf(stderr, "write %s to file %s failed: %s\r\n", value, path, strerror(errno));
r = -1;
}
fclose(fp);
return r;
}
/* return 0 or 1 if OK
* return -1 if read FAILED
*/
int
read_pin(char *pin)
{
char path[65], c;
FILE *fp;
if (pin == NULL || pin[0] == '\0')
return -1;
snprintf(path, 64, "/sys/class/gpio/gpio%s/value", pin);
fp = fopen(path, "r");
if (fp == NULL) {
fprintf(stderr, "can't read %s: %s\r\n", path, strerror(errno));
return -1;
}
if (1 != fread(&c, 1, 1, fp)) {
fprintf(stderr, "read from %s failed: %s\r\n", path, strerror(errno));
c = '0' - 1;
}
fclose(fp);
return (c - '0');
}
/* return 0 if OK
* return -1 if FAILED
*/
int
release_pin(char *pin)
{
FILE *fp;
char path[65] = "/sys/class/gpio/unexport";
int k, r = 0;
if (pin == NULL || pin[0] == '\0')
return -1;
fp = fopen(path, "w");
if (fp == NULL) {
fprintf(stderr, "can't write %s: %s\r\n", path, strerror(errno));
return -1;
}
k = strlen(pin);
if (k != fwrite(pin, 1, k, fp)) {
fprintf(stderr, "write %s to file %s failed: %s\r\n", pin, path, strerror(errno));
r = -1;
}
fclose(fp);
return r;
}
void
reset_lorahat(void)
{
/* put lorahat in deepsleep mode */
write_pin(M0, HIGH);
write_pin(M1, HIGH);
usleep(100000); /* sleep 0.1s */
/* to reset lorahat settings */
write_pin(M0, LOW);
write_pin(M1, LOW);
usleep(200000); /* sleep 0.2s */
}
/* return fd of opened ttys of baudrate with 8N1
* return -1 if failed.
*/
int
open_tty(char *dev, int speed, int non_block)
{
struct termios options;
int fd, rate, flag;
if (dev == NULL || dev[0] == '\0')
return -1;
fd = open(dev, O_RDWR | O_NOCTTY /* | O_NONBLOCK */);
if (fd < 0) {
fprintf(stderr, "can't open %s: %s\r\n", dev, strerror(errno));
return -1;
}
switch(speed) {
case 2400:
rate = B2400;
break;
case 4800:
rate = B4800;
break;
case 9600:
rate = B9600;
break;
case 19200:
rate = B19200;
break;
case 38400:
rate = B38400;
break;
case 57600:
rate = B57600;
break;
case 115200:
rate = B115200;
break;
case 460800:
rate = B460800;
break;
case 500000:
rate = B500000;
break;
case 921600:
rate = B921600;
break;
case 1000000:
rate = B1000000;
break;
case 1500000:
rate = B1500000;
break;
case 2000000:
rate = B2000000;
break;
case 2500000:
rate = B2500000;
break;
case 3000000:
rate = B3000000;
break;
default:
rate = 9600;
break;
}
if (-1 == tcgetattr(fd, &options)) {
fprintf(stderr, "tcgetattr(#%d) failed, %s\r\n", fd, strerror(errno));
close(fd);
return -1;
}
cfsetispeed(&options, rate);
cfsetospeed(&options, rate);
options.c_cflag |= (CLOCAL | CREAD);
/* 8N1 */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
options.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
options.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OPOST);
/* non-blocking read */
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 0;
if (-1 == tcsetattr(fd, TCSANOW, &options)) {
fprintf(stderr, "tcsetattr(#%d) failed, %s\r\n", fd, strerror(errno));
close(fd);
return -1;
}
if (non_block) {
/* set non-blocking tty fd */
flag = fcntl(fd, F_GETFL, 0);
if (flag >= 0)
fcntl(fd, F_SETFL, flag | O_NONBLOCK);
}
/* Flush old transmissions */
if (tcflush(fd, TCIOFLUSH) == -1)
fprintf(stderr, "flushing serial port %s failed, %s\r\n", dev, strerror(errno));
return fd;
}
/* return 0 if init OK
* return -1 if init failed
*/
int
init_lorahat(int fd, int baud, int rx_freq, int rx_addr, int rx_netid, int tx_power, int airspeed, int buffersize, uint16_t cryptkey)
{
uint8_t cfg_reg[] = {0xC2, 0x00, 0x09, 0x00, 0x00, 0x00, 0x62, 0x00, 0x12, 0x43, 0x00, 0x00}, buf[13];
uint8_t rxfreq_off, baud_key, airspeed_key, power_key, buffersize_key;
int r;
if (fd < 0)
return -1;
if ((rx_freq <= 930) && (rx_freq >= 850)) {
rxfreq_off = rx_freq - 850;
} else if ((rx_freq <= 493) && (rx_freq >= 410)) {
rxfreq_off = rx_freq - 410;
} else {
fprintf(stderr, "bad lora rx_freq %d, should be 410-493 or 850-930\r\n", rx_freq);
return -1;
}
/* address is 0 - 65535
* under the same frequence, if set 65535, the node can receive
* messages from another node of address is 0 to 65534 and similarly,
* the address 0 to 65534 of node can receive messages while
* the another note of address is 65535 sends.
* otherwise two node must be same the address and frequence
*/
cfg_reg[3] = (rx_addr >> 8) & 0xff;
cfg_reg[4] = rx_addr & 0xff;
/* netid, 0 - 255*/
cfg_reg[5] = rx_netid & 0xff;
switch (baud) {
case 1200:
baud_key = SX126X_UART_BAUDRATE_1200;
break;
case 2400:
baud_key = SX126X_UART_BAUDRATE_2400;
break;
case 4800:
baud_key = SX126X_UART_BAUDRATE_4800;
break;
case 19200:
baud_key = SX126X_UART_BAUDRATE_19200;
break;
case 38400:
baud_key = SX126X_UART_BAUDRATE_38400;
break;
case 57600:
baud_key = SX126X_UART_BAUDRATE_57600;
break;
case 115200:
baud_key = SX126X_UART_BAUDRATE_115200;
break;
case 9600:
default:
baud_key = SX126X_UART_BAUDRATE_9600;
break;
}
switch (airspeed) {
case 1200:
airspeed_key = SX126X_AIRSPEED_1200;
break;
case 2400:
airspeed_key = SX126X_AIRSPEED_2400;
break;
case 4800:
airspeed_key = SX126X_AIRSPEED_4800;
break;
case 19200:
airspeed_key = SX126X_AIRSPEED_19200;
break;
case 38400:
airspeed_key = SX126X_AIRSPEED_38400;
break;
case 62500:
airspeed_key = SX126X_AIRSPEED_62500;
break;
case 9600:
default:
airspeed_key = SX126X_AIRSPEED_9600;
break;
}
cfg_reg[6] = baud_key + airspeed_key;
switch (buffersize) {
case 128:
buffersize_key = SX126X_PACKAGE_SIZE_128_BYTE;
break;
case 64:
buffersize_key = SX126X_PACKAGE_SIZE_64_BYTE;
break;
case 32:
buffersize_key = SX126X_PACKAGE_SIZE_32_BYTE;
break;
case 240:
default:
buffersize_key = SX126X_PACKAGE_SIZE_240_BYTE;
break;
}
switch (tx_power) {
case 17:
power_key = SX126X_POWER_17DBM;
break;
case 13:
power_key = SX126X_POWER_13DBM;
break;
case 10:
power_key = SX126X_POWER_10DBM;
break;
case 22:
default:
power_key = SX126X_POWER_22DBM;
break;
}
cfg_reg[7] = buffersize_key + power_key + 0x20;
cfg_reg[8] = rxfreq_off;
cfg_reg[9] = 0x43; /* disable rssi and don't relay */
// cfg_reg[9] = 0x43 + 0x80; /* enable rssi and don't relay */
cfg_reg[10] = (cryptkey >> 8) & 0xff;
cfg_reg[11] = cryptkey & 0xff;
/* let lorahat enter config mode */
write_pin(M0, LOW);
write_pin(M1, HIGH);
usleep(100000); /* sleep 0.1s */
/* write to lorahat */
if (verbose_mode) {
printf("write 12 bytes to lorahat:\r\n");
hex_dump(cfg_reg, 12, 0);
}
r = write(fd, cfg_reg, 12);
if (r != 12) {
/* write failed */
write_pin(M1, LOW);
fprintf(stderr, "write #12 cfg register to fd #%d failed, return %d\r\n", fd, r);
return -1;
}
usleep(200000); /* sleep 0.2s */
r = read(fd, buf, 12);
write_pin(M0, LOW);
write_pin(M1, LOW);
if (r < 12) {
fprintf(stderr, "read #12 bytes from fd #%d failed, return %d\r\n", fd, r);
return -1;
}
if (verbose_mode) {
printf("read 12 bytes from lorahat:\r\n");
hex_dump(buf, 12, 0);
}
return 0;
}
uint8_t
sumofbuffer(uint8_t *src, int len)
{
uint8_t c = 0;
int i;
if (src == NULL || len <= 0)
return 0;
for (i = 0; i < len; i ++)
c += src[i];
return c;
}
/* return lora tty fd if OK
* return -1 if failed
*/
int
config_lorahat(char *tty, int tty_bps, int rxfreq, int rxaddr, int netid, int tx_power, int airspeed, int packetsize, uint16_t key)
{
int c, fd, flag;
if (tty == NULL || tty[0] == '\0')
return -1;
/* open lorahat's tty */
fd = open_tty(tty, 9600, 0);
if (fd < 0) {
fprintf(stderr, "fail to open lorahat serial device %s at 9600 bps\r\n", tty);
return -1;
}
printf("open %s at default 9600 bps -> fd #%d\r\n", tty, fd);
c = init_lorahat(fd, tty_bps, rxfreq, rxaddr, netid, tx_power, airspeed, packetsize, key);
printf("config lorahat at tty %dbps, airspeed %dbps, rx %d MHz, rx addr #%d, netid #%d, tx pwr %ddBm, packet size #%d -> %s\r\n",
tty_bps, airspeed, rxfreq, rxaddr, netid, tx_power, packetsize, c == 0?"OK":"FAILED");
if (c == -1) {
close(fd);
return -1;
}
if (tty_bps != 9600) {
/* we need to reconnect lorahat's tty */
close(fd);
fd = open_tty(tty, tty_bps, 1);
if (fd < 0) {
fprintf(stderr, "fail to re-open lorahat serial device %s/%dbps\r\n", tty, tty_bps);
return -1;
}
printf("reopen %s - #%d bps -> fd #%d\r\n", tty, tty_bps, fd);
} else {
/* set non-blocking tty fd */
flag = fcntl(fd, F_GETFL, 0);
if (flag >= 0)
fcntl(fd, F_SETFL, flag | O_NONBLOCK);
}
return fd;
}
/* return -2 if EOF
* return -1 if failed
* return 0 if there has no data to read at this time
* return > 0 if success
*/
int
read_tty(int fd, uint8_t *b, int len)
{
int r;
if (fd <= 0 || b == NULL || len < 0) return -1;
if (len == 0) return 0;
if (len > 8)
len = 8;
r = read(fd, b, len);
if ((r == -1 && (errno != EINTR && errno != EAGAIN)) || r == 0) {
/* connection closed or reset */
if (r == 0) return -2; /* EOF */
return -1;
}
return r;
}
/* return 0 if OK
* return -1 if FAILED
*/
int
write_lorahat(int fd, int rx_freq, int rx_addr, int tx_freq, int tx_addr, uint8_t *payload, int len, uint8_t type)
{
uint8_t tx_buf[512];
int r, pos = 0;
if (fd < 0 || payload == NULL || len <= 0 || len > 250)
return -1;
tx_buf[0] = (tx_addr >> 8) & 0xff;
tx_buf[1] = tx_addr & 0xff;
if ((tx_freq <= 930) && (tx_freq >= 850)) {
tx_buf[2] = tx_freq - 850;
} else if ((tx_freq <= 493) && (tx_freq >= 410)) {
tx_buf[2] = tx_freq - 410;
} else {
tx_buf[2] = 0;
}
tx_buf[3] = (rx_addr >> 8) & 0xff;
tx_buf[4] = rx_addr & 0xff;
if ((rx_freq <= 930) && (rx_freq >= 850)) {
tx_buf[5] = rx_freq - 850;
} else if ((rx_freq <= 493) && (rx_freq >= 410)) {
tx_buf[5] = rx_freq - 410;
} else {
tx_buf[5] = 0;
}
/* add LORA MAGIC HERE */
tx_buf[6] = (LORA_MAGIC >> 8) & 0xff;
tx_buf[7] = LORA_MAGIC & 0xff;
tx_buf[8] = (len & 0xff) + 2; /* prepend length before payload message */
tx_buf[9] = type;
memcpy(tx_buf + 10, payload, len);
/* append sum of buffer */
tx_buf[10 + len] = sumofbuffer(payload, len) + type;
tx_buf[11 + len] = '\0';
len += 11;
/* we need to write all data to lorahat here */
while (pos < len) {
r = write(fd, tx_buf + pos, len - pos);
if (r > 0) {
pos += r;
} else if ((r == -1 ) && (errno != EINTR) && (errno != EAGAIN)) {
fprintf(stderr, "write #%d data to fd %d failed, return %d, %s\r\n", len - pos, fd, r, strerror(errno));
return -1;
}
}
return 0;
}
struct filecmd {
char name[8]; /* 8 chars filename, just for test */
int pos;
};
/* return 0 if OK
* return -1 if FAILED
*/
int
rxfile(uint8_t *payload, int len)
{
char name[9];
int pos;
FILE *fp;
if (payload == NULL || len <= sizeof(struct filecmd))
return -1;
memcpy(name, payload, 8);
name[8] = '\0';
memcpy(&pos, payload + 8, sizeof(pos));
if (pos == 0)
fp = fopen(name, "wb"); /* overwrite file */
else
fp = fopen(name, "ab"); /* append file, don't overwrite file */
if (fp == NULL) {
fprintf(stderr, "can't write to file %s, %s\r\n", name, strerror(errno));
return -1;
}
fseek(fp, pos, SEEK_SET);
len -= sizeof(struct filecmd);
if (len != fwrite(payload + sizeof(struct filecmd), 1, len, fp)) {
fprintf(stderr, "write #%d data at pos %d to file %s failed, %s\r\n", len, pos, name, strerror(errno));
fclose(fp);
return -1;
}
fflush(fp);
fclose(fp);
printf("write #%d data at pos %d to file %s -> OK\r\n", len, pos, name);
return 0;
}
/* return 0 if SEND OK
* return -1 if SEND FAILED
*/
int
sendfile_lorahat(int fd, int rx_freq, int rx_addr, int tx_freq, int tx_addr, char *file)
{
FILE *fp;
uint8_t payload[512];
int pos = 0, size, r = 0, len, bps, k;
char name[9];
time_t start, end;
struct filecmd c;
int blocksize = 0;
if (file == NULL || file[0] == '\0')
return -1;
fp = fopen(file, "rb");
if (fp == NULL) {
fprintf(stderr, "can't open %s, %s\r\n", file, strerror(errno));
return -1;
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
start = time(NULL);
snprintf(name, 9, "%d", (int) random());
if (tx_blocksize > lora_buffersize)
blocksize = lora_buffersize - 25; /* smaller lora packet size */
else
blocksize = tx_blocksize;
while (pos < size) {
if ((size - pos) > blocksize)
len = blocksize;
else
len = size - pos;
if (len != fread(payload + sizeof(c), 1, len, fp)) {
fprintf(stderr, "read #%d from %s at pos %d failed, %s\r\n", len, file, pos, strerror(errno));
r = -1;
break;
}
c.pos = pos;
memcpy(c.name, name, 8);
memcpy(payload, &c, sizeof(c));
while (read_pin(AUX) == 0) {
usleep(100); /* sleep 0.1ms before AUX goes to high */
}
if (-1 == write_lorahat(fd, rx_freq, rx_addr, tx_freq, tx_addr, payload, len + sizeof(c), LORA_FILE)) {
fprintf(stderr, "write to lorahat at #%d failed\r\n", pos);
r = -1;
break;
}
k = 0;
/* AUX -> LOW indicates beginning of LORA TX
* ... LORA TXing ...
* AUX -> HIGH indicated end of LORA TX
* wait for 1ms make sure TX is stopped
*/
while (read_pin(AUX) == 1) {
usleep(1000); /* sleep 1ms before AUX goes to low */
k += 1;
}
while (read_pin(AUX) == 0) {
usleep(1000); /* sleep 1ms before AUX goes to HIGH */
k += 1;
}
usleep(1000);
printf("send #%d at pos %d data to lorahat -> OK, %dms\r\n", len, pos, k);
pos += len;
}
end = time(NULL);
if (end == start) {
bps = pos * 8;
} else {
bps = pos * 8 / (end - start);
}
printf("sending %d/%d data to lora takes %d seconds, %dbps\r\n", pos, size, (int) (end - start), bps);
fclose(fp);
return r;
}
void
handle_lora(const int fd, short which, void *arg)
{
int r, last, start;
uint8_t calc_sum, sum, type, len;
if (which & EV_READ) {
r = read_tty(fd, lora_rbuf + lora_rpos, lora_rsize - lora_rpos);
if (r < 0)
return;
lora_rpos += r;
if (lora_rpos > 7) {
/* to find LORAMAGIC 0xeb90 */
lora_rbuf[lora_rpos] = '\0';
r = find_magic(lora_rbuf, lora_rpos, LORA_MAGIC, &start);
if (r == 1) {
/* NO LORA_MAGIC FOUND */
if (lora_rpos == lora_rsize) {
/* can't find LORA_MAGIC in all buffer, clear it now */
if (lora_rbuf[lora_rsize - 1] == (((LORA_MAGIC >> 8) & 0xff))) {
/* keep last high byte of LORA_MAGIC */
lora_rbuf[0] = (LORA_MAGIC >> 8) & 0xff;
lora_rpos = 1;
} else {
lora_rpos = 0;
}
}
return;
}
if (start < 3) {
/* not enough prefix for full message */
/* skip this magic word */
memmove (lora_rbuf, lora_rbuf + start + 2, lora_rpos - start - 2);
lora_rpos -= start + 2;
return;
}
if (start > 3) {
/* move data to pos 0 */
memmove (lora_rbuf, lora_rbuf + start - 3, lora_rpos - start + 3);
lora_rpos -= start - 3;
}
if ((lora_rpos < 8) || (lora_rpos < (lora_rbuf[5] + 6))) {
/* not full message */
return;
}
/* [ADDR HIGH][ADDR LOW][NETID][0xEB][0x90][LEN][TYPE][PAYLOAD][SUM]
* LEN = SIZEOF(PAYLOAD) + 1 + 1
*/
if (verbose_mode)
hex_dump(lora_rbuf, lora_rpos, 1);
len = lora_rbuf[5];
type = lora_rbuf[6];
calc_sum = sumofbuffer(lora_rbuf + 6, len - 1);
last = len + 6 - 1;
sum = lora_rbuf[last];
lora_rbuf[last] = '\0'; /* clear sum of buffer */
if (calc_sum != sum) {
lora_rbuf[last] = sum;
hex_dump(lora_rbuf, lora_rsize, 1);
printf("lora msg mismatched CALC 0x%x != DATA 0x%x\r\n", calc_sum, sum);
} else if (type == LORA_TEXT) {
printf("rx txt msg: from address %d / %dMHz, len %d, \"%s\"\r\n",
((lora_rbuf[0] << 8) + lora_rbuf[1]), 410 + lora_rbuf[2], len - 2,
(char *)(lora_rbuf + 7));
} else if (type == LORA_BINARY) {
printf("rx binary msg:\r\n");
hex_dump(lora_rbuf + 7, len - 2, 0);
} else if (type == LORA_FILE) {
rxfile(lora_rbuf + 7, len - 2);
}
lora_rpos -= 6 + len;
if (lora_rpos > 0) {
/* keep unprocessed data */
memmove (lora_rbuf, lora_rbuf + 6 + len, lora_rpos);
}
}
}
}
void *
send_handler(void *arg)
{
char msg[512];
char *p, *q;
int tx_freq, tx_addr, r;
while (1) {
printf("enter '[s|t]0,433,xxxx' to send message\r\n");
if (NULL != fgets(msg, 512, stdin)) {
/* parse address,freq string first */
r = strlen(msg) - 1;
if (r >= 0 && msg[r] == '\n')
msg[r] = '\0';
p = strchr(msg + 1, ',');
if (p == NULL)