-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmodem.c
1101 lines (982 loc) · 27.1 KB
/
zmodem.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
/* zmodem.c */
#if HOST
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#else
#include "chip.h"
#include "uart.h"
#include "lfs.h"
#include "speaker.h"
#endif
#include "crc.h"
#include "zmodem.h"
#include <string.h>
#include <stdlib.h>
#define ZPAD 0x2a
#define ZDLE 0x18
#define XON 0x11
#define XOFF 0x13
#define ZCRCE 0x68
#define ZCRCG 0x69
#define ZCRCQ 0x6a
#define ZCRCW 0x6b
#define ZRUB0 0x6c
#define ZRUB1 0x6d
#define ZBIN 0x41
#define ZHEX 0x42
#define ZBIN32 0x43
#define ZBINR32 0x44
#define ZRQINIT 0x00
#define ZRINIT 0x01
#define ZSINIT 0x02
#define ZACK 0x03
#define ZFILE 0x04
#define ZSKIP 0x05
#define ZNAK 0x06
#define ZABORT 0x07
#define ZFIN 0x08
#define ZRPOS 0x09
#define ZDATA 0x0a
#define ZEOF 0x0b
#define ZFERR 0x0c
#define ZCRC 0x0d
#define ZCHALLENGE 0x0e
#define ZCOMPL 0x0f
#define ZCAN 0x10
#define ZFREECNT 0x11
#define ZCOMMAND 0x12
#define ZSTDERR 0x13
#define ZF0_CANFDX 0x01
#define ZF0_CANOVIO 0x02
#define ZF0_CANBRK 0x04
#define ZF0_CANCRY 0x08
#define ZF0_CANLZW 0x10
#define ZF0_CANFC32 0x20
#define ZF0_ESCCTL 0x40
#define ZF0_ESC8 0x80
#define ZF1_CANVHDR 0x01
#define ZF0_TESCCTL 0x40
#define ZF0_TESC8 0x80
#define BUFFER_SIZE ZMODEM_BUFFER_SIZE
struct {
bool active;
int zdlecount;
int count;
int state;
int protostate;
int datalen;
uint8_t frametype;
bool escape_ctrl;
bool escape_bit8;
bool ok;
uint32_t fileoffset;
uint32_t transferred;
uint32_t total;
int timeouts;
lfs_t *lfs;
lfs_file_t *lfs_file;
bool fileopen;
int debug;
void (*process_data)(void);
uint8_t header[5];
uint8_t crc[4];
uint8_t buffer[BUFFER_SIZE];
} zstate;
#define STATE_IDLE 0
#define STATE_HEADER 1
#define STATE_HEADER_ZHEX 2
#define STATE_HEADER_ZBIN 3
#define STATE_HEADER_ZBIN32 4
#define STATE_HEADER_ZHEX_CRC 5
#define STATE_HEADER_ZBIN_CRC 6
#define STATE_HEADER_ZBIN32_CRC 7
#define STATE_HEADER_ZHEX_CRLF 8
#define STATE_DATA_ZBIN 9
#define STATE_DATA_ZBIN32 10
#define STATE_DATA_ZBIN_ZCRCE 11
#define STATE_DATA_ZBIN_ZCRCG 12
#define STATE_DATA_ZBIN_ZCRCQ 13
#define STATE_DATA_ZBIN_ZCRCW 14
#define STATE_DATA_ZBIN32_ZCRCE 15
#define STATE_DATA_ZBIN32_ZCRCG 16
#define STATE_DATA_ZBIN32_ZCRCQ 17
#define STATE_DATA_ZBIN32_ZCRCW 18
#define STATE_FINISH 19
#define PROTOSTATE_RX_IDLE 0
#define PROTOSTATE_RX_WAIT 1
#define PROTOSTATE_RX_TRANSFER 2
#ifdef HOST
#define ZDEBUG(...) fprintf(stderr, __VA_ARGS__)
void ztx_byte(uint8_t byte);
#else
#define ZDEBUG(...)
#define ztx_byte(x) uart_transmit(x)
#endif
#define isxdigit(x) ((((x) >= '0') && ((x) <= '9')) || (((x) >= 'a') && ((x) <= 'f')))
#define hexdecode(x) (((x) < 'a') ? ((x) - '0') : ((x) + 10 - 'a'))
#define hexencode(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
bool zmodem_open_file(char *filename);
bool zmodem_close_file(void);
void ztx_escbyte(uint8_t byte)
{
bool escape = false;
if (zstate.escape_ctrl && (byte < 32))
escape = true;
if (zstate.escape_bit8 && (byte >= 0x80))
escape = true;
if ((byte & 0x7f) == XON)
escape = true;
if ((byte & 0x7f) == XOFF)
escape = true;
if (byte == ZDLE)
escape = true;
if (escape) {
ztx_byte(ZDLE);
byte ^= 0x40;
}
ztx_byte(byte);
}
void ztx_hexbyte(uint8_t byte)
{
uint8_t b1 = byte >> 4;
uint8_t b2 = byte & 0x0f;
ztx_byte(hexencode(b1));
ztx_byte(hexencode(b2));
}
void zmodem_enter_state(int state)
{
zstate.count = 0;
zstate.state = state;
ZDEBUG("In state %d\n", state);
}
void zmodem_enter_protostate(int state)
{
zstate.protostate = state;
ZDEBUG("In protocol state %d\n", state);
}
void zmodem_reinit(void)
{
zstate.active = false;
zstate.zdlecount = 0;
zstate.count = 0;
zstate.state = STATE_IDLE;
zstate.protostate = PROTOSTATE_RX_IDLE;
zstate.escape_ctrl = false;
zstate.escape_bit8 = false;
zstate.process_data = NULL;
zstate.timeouts = 0;
zstate.transferred = 0;
zstate.total = 0;
if (zstate.fileopen)
zmodem_close_file();
}
void *zmodem_init(lfs_t *lfs, lfs_file_t *lfs_file)
{
zstate.lfs = lfs;
zstate.lfs_file = lfs_file;
zstate.fileopen = false;
zstate.debug = 0;
zmodem_reinit();
return zstate.buffer;
}
int zmodem_progress(void)
{
if (zstate.total == 0)
return -1;
if (zstate.transferred > zstate.total)
return -1;
return zstate.transferred * 100 / zstate.total;
}
bool zmodem_active(void)
{
return zstate.active;
}
bool zmodem_waiting(void)
{
return zstate.timeouts != 0;
}
void zmodem_setactive(void)
{
zstate.active = true;
}
int zmodem_debug(void)
{
return zstate.debug;
}
void zmodem_send_hex_header(int type, void *data);
void zmodem_send_zrinit(void)
{
uint8_t header[] = {
BUFFER_SIZE & 0xff,
BUFFER_SIZE >> 8,
0,
ZF0_CANFC32
};
zstate.active = true;
zmodem_send_hex_header(ZRINIT, header);
zmodem_enter_state(STATE_IDLE);
zmodem_enter_protostate(PROTOSTATE_RX_WAIT);
}
void zmodem_send_abort(void)
{
for (int i = 0; i < 8; i++)
ztx_byte(ZDLE);
for (int i = 0; i < 10; i++)
ztx_byte(8);
}
void zmodem_process_zrqinit(void)
{
if (zstate.protostate != PROTOSTATE_RX_IDLE)
return;
zmodem_send_zrinit();
}
void zmodem_process_zrinit(void)
{
/* XXX we're getting echoes back, something's gone wrong */
zmodem_enter_state(STATE_IDLE);
zmodem_enter_protostate(PROTOSTATE_RX_IDLE);
}
void zmodem_process_zsinit_data(void)
{
uint8_t header[] = {
0,
0,
0,
0
};
if (zstate.protostate != PROTOSTATE_RX_WAIT)
return;
ZDEBUG("zsinit_data: %d\n", zstate.state);
/* We don't care about the Attn sequence for now */
switch (zstate.frametype) {
case ZCRCQ:
case ZCRCW:
zmodem_send_hex_header(ZACK, header);
break;
default:
break;
}
}
void zmodem_enter_data_state(void)
{
switch (zstate.state) {
case STATE_HEADER_ZHEX_CRLF:
zmodem_enter_state(STATE_DATA_ZBIN);
break;
case STATE_HEADER_ZBIN_CRC:
zmodem_enter_state(STATE_DATA_ZBIN);
break;
case STATE_HEADER_ZBIN32_CRC:
zmodem_enter_state(STATE_DATA_ZBIN32);
break;
}
}
void zmodem_process_zsinit(void)
{
ZDEBUG("zsinit\n");
if (zstate.protostate != PROTOSTATE_RX_WAIT)
return;
ZDEBUG("zsinit working\n");
zstate.escape_ctrl = ((zstate.header[4] & ZF0_TESCCTL) != 0);
zstate.escape_bit8 = ((zstate.header[4] & ZF0_TESC8) != 0);
zstate.process_data = zmodem_process_zsinit_data;
zmodem_enter_data_state();
}
void zmodem_process_zack(void)
{
;
}
void zmodem_send_rpos(void)
{
uint8_t header[] = {
zstate.fileoffset & 0xff,
(zstate.fileoffset >> 8) & 0xff,
(zstate.fileoffset >> 16) & 0xff,
(zstate.fileoffset >> 24) & 0xff
};
zmodem_send_hex_header(ZRPOS, header);
}
void zmodem_process_zfile_data(void)
{
if (zstate.datalen > BUFFER_SIZE)
return;
if (zstate.buffer[zstate.datalen-1] != '\0')
return;
if (!zstate.ok) {
/* XXX what can we do here? Send ZRINIT again? */
return;
}
char *filenamep = (char *)zstate.buffer;
int filenamelen = strlen(filenamep);
char *lengthp = filenamep + filenamelen+1;
/* Check that we have at least one more null terminator */
if (filenamelen+1 >= zstate.datalen)
return;
char *pch = strtok(lengthp, " ");
char *mdatep = NULL;
char *modep = NULL;
char *serialp = NULL;
char *rfilesp = NULL;
char *rdatap = NULL;
if (pch != NULL) {
/* lengthp */
pch = strtok(NULL, " ");
}
if (pch != NULL) {
mdatep = pch;
pch = strtok(NULL, " ");
}
if (pch != NULL) {
modep = pch;
pch = strtok(NULL, " ");
}
if (pch != NULL) {
serialp = pch;
pch = strtok(NULL, " ");
}
if (pch != NULL) {
rfilesp = pch;
pch = strtok(NULL, " ");
}
if (pch != NULL) {
rdatap = pch;
pch = strtok(NULL, " ");
}
(void) mdatep;
(void) modep;
(void) serialp;
(void) rfilesp;
if (rdatap) {
zstate.total = zstate.transferred + atoi(rdatap);
}
/* Optional - send a ZRCRC to determine where to start from */
ZDEBUG("Got filename: %s\n", zstate.buffer);
zstate.fileoffset = 0;
if (zmodem_open_file((char *)zstate.buffer)) {
zmodem_enter_protostate(PROTOSTATE_RX_TRANSFER);
zmodem_send_rpos();
} else {
uint8_t header[] = {0, 0, 0, 0};
/* ZSKIP or ZFERR? */
zmodem_send_hex_header(ZSKIP, header);
}
}
void zmodem_process_zfile(void)
{
if (zstate.protostate != PROTOSTATE_RX_WAIT)
return;
/* Ignore the conversion options
* We're not going to do any conversion
* But maybe later we can take note of ZCRECOV
* Also ignore the management options
* We don't have timestamps
* And we can save some complexity here
*/
zstate.process_data = zmodem_process_zfile_data;
zmodem_enter_data_state();
}
void zmodem_process_zskip(void)
{
/* We shouldn't be getting this as a receiver */
;
}
void zmodem_process_znak(void)
{
/* Last header was garbled */
zmodem_send_zrinit();
}
void zmodem_process_zabort(void)
{
/* In theory, we shouldn't get this as a receiver */
;
}
void zmodem_process_zfin(void)
{
if (zstate.protostate != PROTOSTATE_RX_WAIT)
return;
uint8_t header[] = {
0,
0,
0,
0
};
zmodem_send_hex_header(ZFIN, header);
zmodem_enter_state(STATE_FINISH);
}
void zmodem_process_zrpos(void)
{
/* We shouldn't get this as a receiver */
}
bool zmodem_file_write(void)
{
if (!zstate.fileopen)
return false;
/* Write zstate.buffer */
ZDEBUG("Writing %d bytes to file\n", zstate.datalen);
int err = lfs_file_write(zstate.lfs, zstate.lfs_file, zstate.buffer, zstate.datalen);
if (err < 0)
return false;
return true;
}
bool zmodem_open_file(char *filename)
{
int err = lfs_file_open(zstate.lfs, zstate.lfs_file, filename, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC);
if (!err)
zstate.fileopen = true;
return !err;
}
bool zmodem_close_file(void)
{
int err = lfs_file_close(zstate.lfs, zstate.lfs_file);
if (!err)
zstate.fileopen = false;
return !err;
}
void zmodem_process_zdata_data(void)
{
uint32_t offset = (zstate.header[1]) +
(zstate.header[2] << 8) +
(zstate.header[3] << 16) +
(zstate.header[4] << 24);
if (zstate.ok && zmodem_file_write()) {
zstate.fileoffset += zstate.datalen;
/* XXX we may need to revisit this if we ever support resuming */
zstate.transferred += zstate.datalen;
ZDEBUG("offset: %d, %d\n", offset, zstate.fileoffset);
uint8_t header[] = {
offset & 0xff,
(offset >> 8) & 0xff,
(offset >> 16) & 0xff,
(offset >> 24) & 0xff
};
switch (zstate.state) {
case STATE_DATA_ZBIN_ZCRCQ:
case STATE_DATA_ZBIN_ZCRCW:
case STATE_DATA_ZBIN32_ZCRCQ:
case STATE_DATA_ZBIN32_ZCRCW:
zmodem_send_hex_header(ZACK | 0x80, header);
}
} else {
zmodem_send_rpos();
zmodem_enter_state(STATE_IDLE);
}
}
void zmodem_process_zdata(void)
{
uint32_t offset = (zstate.header[1]) +
(zstate.header[2] << 8) +
(zstate.header[3] << 16) +
(zstate.header[4] << 24);
if (zstate.protostate != PROTOSTATE_RX_TRANSFER)
return;
if (offset == zstate.fileoffset) {
zmodem_enter_data_state();
zstate.process_data = zmodem_process_zdata_data;
} else {
zmodem_send_rpos();
/* XXX do we maybe have to receive the ZDATA data first? */
zmodem_enter_state(STATE_IDLE);
}
}
void zmodem_process_zeof(void)
{
uint32_t offset = (zstate.header[1]) +
(zstate.header[2] << 8) +
(zstate.header[3] << 16) +
(zstate.header[4] << 24);
if (zstate.protostate != PROTOSTATE_RX_TRANSFER)
return;
ZDEBUG("EOF: %d, %d\n", offset, zstate.fileoffset);
if (offset != zstate.fileoffset) {
zmodem_enter_state(STATE_IDLE);
return;
}
zmodem_close_file();
zmodem_send_zrinit();
}
void zmodem_process_zferr(void)
{
/* We shouldn't get this as a receiver */
;
}
void zmodem_process_zcrc(void)
{
/* We're not sending ZRCRC yet, so we shouldn't get it as a response yet */
;
}
void zmodem_process_zchallenge(void)
{
/* We shouldn't get this as a receiver */
}
void zmodem_process_zcompl(void)
{
/* We shouldn't get this as a receiver */
}
void zmodem_process_zcan(void)
{
ZDEBUG("Abort sequence received\n");
zstate.zdlecount = 0;
zmodem_send_abort();
zmodem_enter_state(STATE_IDLE);
}
void zmodem_process_zfreecnt(void)
{
/* unimplemented */
;
}
void zmodem_process_zcommand(void)
{
/* unimplemented */
;
}
void zmodem_process_zstderr(void)
{
/* unimplemented */
;
}
void zmodem_process_header(void)
{
ZDEBUG("Header received\n");
ZDEBUG("Type: %x\n", zstate.header[0]);
ZDEBUG("Contents: %x %x %x %x\n", zstate.header[1], zstate.header[2], zstate.header[3], zstate.header[4]);
zstate.timeouts = 0;
switch (zstate.header[0]) {
case ZRQINIT:
zmodem_process_zrqinit();
break;
case ZRINIT:
zmodem_process_zrinit();
break;
case ZSINIT:
zmodem_process_zsinit();
break;
case ZACK:
zmodem_process_zack();
break;
case ZFILE:
zmodem_process_zfile();
break;
case ZSKIP:
zmodem_process_zskip();
break;
case ZNAK:
zmodem_process_znak();
break;
case ZABORT:
zmodem_process_zabort();
break;
case ZFIN:
zmodem_process_zfin();
break;
case ZRPOS:
zmodem_process_zrpos();
break;
case ZDATA:
zmodem_process_zdata();
break;
case ZEOF:
zmodem_process_zeof();
break;
case ZFERR:
zmodem_process_zferr();
break;
case ZCRC:
zmodem_process_zcrc();
break;
case ZCHALLENGE:
zmodem_process_zchallenge();
break;
case ZCOMPL:
zmodem_process_zcompl();
break;
case ZCAN:
zmodem_process_zcan();
break;
case ZFREECNT:
zmodem_process_zfreecnt();
break;
case ZCOMMAND:
zmodem_process_zcommand();
break;
case ZSTDERR:
zmodem_process_zstderr();
break;
}
}
void zmodem_timeout(void)
{
if (zstate.protostate == PROTOSTATE_RX_IDLE)
return;
if (zstate.state == STATE_FINISH) {
zmodem_reinit();
return;
}
zstate.timeouts++;
if (zstate.timeouts > 4) {
zmodem_reinit();
return;
}
if (zstate.protostate == PROTOSTATE_RX_TRANSFER && zstate.timeouts == 1) {
zmodem_send_rpos();
return;
}
/* If we're in the middle of a data transfer, we can reset the counter
* here. We'll end up back at the start anyway, and the counter can count
* up from there until we abort the whole session.
*/
if (zstate.protostate == PROTOSTATE_RX_TRANSFER)
zstate.timeouts = 0;
zmodem_send_zrinit();
}
void zmodem_send_hex_header(int type, void *data)
{
uint8_t buffer[5];
uint16_t crc;
buffer[0] = type & 0x7f;
for (int i = 0; i < 4; i++)
buffer[i+1] = ((uint8_t *)data)[i];
ZDEBUG("Send hex header: %x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
crc = crc16(buffer, 5, 0);
ztx_byte(ZPAD);
ztx_byte(ZPAD);
ztx_byte(ZDLE);
ztx_byte(ZHEX);
for (int i = 0; i < 5; i++)
ztx_hexbyte(buffer[i]);
ztx_hexbyte(crc >> 8);
ztx_hexbyte(crc & 0xff);
ztx_byte('\r');
ztx_byte('\n');
if (type == ZACK || type == ZFIN)
return;
ztx_byte(XON);
}
void zmodem_send_bin_header(int type, void *data)
{
uint8_t buffer[5];
uint16_t crc;
buffer[0] = type;
for (int i = 0; i < 4; i++)
buffer[i+1] = ((uint8_t *)data)[i];
ZDEBUG("Send bin header: %x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
crc = crc16(buffer, 5, 0);
ztx_byte(ZPAD);
ztx_byte(ZDLE);
ztx_byte(ZHEX);
for (int i = 0; i < 5; i++)
ztx_escbyte(buffer[i]);
ztx_escbyte(crc >> 8);
ztx_escbyte(crc & 0xff);
}
void zmodem_send_bin32_header(int type, void *data)
{
uint8_t buffer[5];
uint32_t crc;
buffer[0] = type;
for (int i = 0; i < 4; i++)
buffer[i+1] = ((uint8_t *)data)[i];
ZDEBUG("Send bin32 header: %x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
crc = crc32(buffer, 5, 0);
ztx_byte(ZPAD);
ztx_byte(ZDLE);
ztx_byte(ZHEX);
for (int i = 0; i < 5; i++)
ztx_escbyte(buffer[i]);
ztx_escbyte((crc >> 24) & 0xff);
ztx_escbyte((crc >> 16) & 0xff);
ztx_escbyte((crc >> 8) & 0xff);
ztx_escbyte(crc & 0xff);
}
void zmodem_send_znak(void)
{
uint8_t zero[4] = {0, 0, 0, 0};
zmodem_send_hex_header(ZNAK, zero);
}
void zmodem_process_data(bool ok)
{
ZDEBUG("Data received\n");
ZDEBUG("CRC %s\n", ok?"ok":"not ok");
for (int i = 0; i < zstate.datalen; i++) {
ZDEBUG("%x ", zstate.buffer[i]);
if ((i % 16) == 15) {
ZDEBUG("\n");
}
}
if (zstate.count % 16)
ZDEBUG("\n");
zstate.ok = ok;
switch (zstate.state) {
case STATE_DATA_ZBIN_ZCRCE:
case STATE_DATA_ZBIN32_ZCRCE:
zmodem_enter_state(STATE_IDLE);
break;
case STATE_DATA_ZBIN_ZCRCG:
zmodem_enter_state(STATE_DATA_ZBIN);
break;
case STATE_DATA_ZBIN32_ZCRCG:
zmodem_enter_state(STATE_DATA_ZBIN32);
break;
case STATE_DATA_ZBIN_ZCRCQ:
case STATE_DATA_ZBIN32_ZCRCQ:
zmodem_enter_state(STATE_IDLE);
break;
case STATE_DATA_ZBIN_ZCRCW:
case STATE_DATA_ZBIN32_ZCRCW:
zmodem_enter_state(STATE_IDLE);
break;
default:
zmodem_enter_state(STATE_IDLE);
break;
}
if (zstate.process_data != NULL)
zstate.process_data();
}
void zrx_byte(uint8_t byte)
{
//ZDEBUG("Received byte %x\n", byte);
if ((byte & 0x7f) == XON || (byte & 0x7f) == XOFF)
return;
if (byte == ZDLE) {
zstate.zdlecount++;
if (zstate.zdlecount >= 5) {
zstate.header[0] = ZCAN;
zmodem_process_header();
}
if (zstate.count && zstate.state == STATE_IDLE) {
zstate.zdlecount = 0;
zmodem_enter_state(STATE_HEADER);
}
} else {
if (zstate.zdlecount) {
switch (byte) {
case ZRUB0:
byte = 0x7f;
break;
case ZRUB1:
byte = 0xff;
break;
default:
if ((byte & 0x60) == 0x40)
byte ^= 0x40;
break;
}
}
switch (zstate.state) {
case STATE_IDLE:
if (byte == ZPAD)
zstate.count++;
else
zstate.count = 0;
break;
case STATE_HEADER:
switch (byte) {
case ZHEX:
zmodem_enter_state(STATE_HEADER_ZHEX);
break;
case ZBIN:
zmodem_enter_state(STATE_HEADER_ZBIN);
break;
case ZBIN32:
zmodem_enter_state(STATE_HEADER_ZBIN32);
break;
default:
zmodem_send_znak();
zmodem_enter_state(STATE_IDLE);
break;
}
break;
case STATE_HEADER_ZHEX:
if (!isxdigit(byte)) {
zmodem_send_znak();
zmodem_enter_state(STATE_IDLE);
}
byte = hexdecode(byte);
if (zstate.count & 1)
zstate.header[zstate.count/2] += byte;
else
zstate.header[zstate.count/2] = byte << 4;
zstate.count++;
if (zstate.count >= 10)
zmodem_enter_state(STATE_HEADER_ZHEX_CRC);
break;
case STATE_HEADER_ZHEX_CRC:
if (!isxdigit(byte)) {
zmodem_send_znak();
zmodem_enter_state(STATE_IDLE);
}
byte = hexdecode(byte);
if (zstate.count & 1)
zstate.crc[zstate.count/2] += byte;
else
zstate.crc[zstate.count/2] = byte << 4;
zstate.count++;
if (zstate.count >= 4)
zmodem_enter_state(STATE_HEADER_ZHEX_CRLF);
break;
case STATE_HEADER_ZHEX_CRLF:
if (zstate.count == 0 && (byte & 0x7f) == '\r') {
zstate.count++;
break;
}
if (zstate.count == 1 && (byte & 0x7f) == '\n') {
uint16_t crc = crc16(zstate.header, 5, 0);
uint16_t rxcrc = zstate.crc[1] | (zstate.crc[0] << 8);
ZDEBUG("CRC calculated: %x, received %x\n", crc, rxcrc);
if (crc == rxcrc) {
zmodem_process_header();
} else {
zmodem_send_znak();
zmodem_enter_state(STATE_IDLE);
}
break;
}
zmodem_send_znak();
zmodem_enter_state(STATE_IDLE);
break;
case STATE_HEADER_ZBIN:
case STATE_HEADER_ZBIN32:
zstate.header[zstate.count++] = byte;
if (zstate.count >= 5)
zmodem_enter_state((zstate.state == STATE_HEADER_ZBIN32)?STATE_HEADER_ZBIN32_CRC:STATE_HEADER_ZBIN_CRC);
break;
case STATE_HEADER_ZBIN_CRC:
zstate.crc[zstate.count++] = byte;
if (zstate.count >= 2) {
uint16_t crc = crc16(zstate.header, 5, 0);
uint16_t rxcrc = zstate.crc[1] | (zstate.crc[0] << 8);
ZDEBUG("CRC calculated: %x, received %x\n", crc, rxcrc);
if (crc == rxcrc) {
zmodem_process_header();
} else {
zmodem_send_znak();
zmodem_enter_state(STATE_IDLE);
}
}
break;
case STATE_HEADER_ZBIN32_CRC:
zstate.crc[zstate.count++] = byte;
if (zstate.count >= 4) {
uint32_t crc = crc32(zstate.header, 5, 0);
uint32_t rxcrc = zstate.crc[0] | (zstate.crc[1] << 8) |
(zstate.crc[2] << 16) | (zstate.crc[3] << 24);
ZDEBUG("CRC calculated: %x, received %x\n", crc, rxcrc);
if (crc == rxcrc) {
zmodem_process_header();
} else {
zmodem_send_znak();
zmodem_enter_state(STATE_IDLE);
}
}
break;
case STATE_DATA_ZBIN:
case STATE_DATA_ZBIN32:
if (zstate.count < BUFFER_SIZE)
zstate.buffer[zstate.count] = byte;
zstate.count++;
if (zstate.zdlecount) {
switch (byte) {
case ZCRCE:
zstate.datalen = zstate.count - 1;
zstate.frametype = byte;
zmodem_enter_state((zstate.state == STATE_DATA_ZBIN32)?STATE_DATA_ZBIN32_ZCRCE:STATE_DATA_ZBIN_ZCRCE);
break;
case ZCRCG:
zstate.datalen = zstate.count - 1;
zstate.frametype = byte;