-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzmodem_zm.cpp
More file actions
994 lines (898 loc) · 20.3 KB
/
zmodem_zm.cpp
File metadata and controls
994 lines (898 loc) · 20.3 KB
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
// See this page for all code: http://www.raspberryginger.com/jbailey/minix/html/dir_acf1a49c3b8ff2cb9205e4a19757c0d6.html
// From: http://www.raspberryginger.com/jbailey/minix/html/zm_8c-source.html
// docs at: http://www.raspberryginger.com/jbailey/minix/html/zm_8c.html
// Look at all the files:
// http://www.raspberryginger.com/jbailey/minix/html/files.html
#ifndef ZMODEM_ZM_CPP
#define ZMODEM_ZM_CPP
/*
* Z M . C
* ZMODEM protocol primitives
* 05-09-88 Chuck Forsberg Omen Technology Inc
*
* Entry point Functions:
* zsbhdr(type, hdr) send binary header
* zshhdr(type, hdr) send hex header
* zgethdr(hdr, eflag) receive header - binary or hex
* zsdata(buf, len, frameend) send data
* zrdata(buf, len) receive data
* stohdr(pos) store position data in Txhdr
* long rclhdr(hdr) recover position offset from header
*/
#ifdef ARDUINO
#include "zmodem_fixes.h"
#include "zmodem.h"
#include "zmodem_crc16.cpp"
#include "zmodem_zm.h"
#else
#ifndef CANFDX
#include "zmodem.h"
#endif
#endif
// Shared globals
long Bytesleft; // from rz - Shared with sz bytcnt
long rxbytes; // from rz - Shared with sz Lrxpos
int Blklen; // from rz - Shared with sz blklen
#define Rxtimeout 100 /* Tenths of seconds to wait for something */
#define Verbose 0
// This buffer blends Txb (from sz) and secbuf (from rz) into a single buffer, saving 1K
// of memory.
char oneKbuf[1025];
/* Globals used by ZMODEM functions */
uint8_t Rxframeind; /* ZBIN ZBIN32, or ZHEX type of frame received */
uint8_t Rxtype; /* Type of header received */
int Rxcount; /* Count of data bytes received */
char Rxhdr[4]; /* Received header */
char Txhdr[4]; /* Transmitted header */
long Rxpos; /* Received file position */
long Txpos; /* Transmitted file position */
int8_t Txfcs32; /* TRUE means send binary frames with 32 bit FCS */
int8_t Crc32t; /* Display flag indicating 32 bit CRC being sent */
int8_t Crc32; /* Display flag indicating 32 bit CRC being received */
//int Znulls; /* Number of nulls to send at beginning of ZDATA hdr */
char Attn[ZATTNLEN+1]; /* Attention string rx sends to tx on err */
char zconv; /* ZMODEM file conversion request */
char zmanag; /* ZMODEM file management request */
char ztrans; /* ZMODEM file transport request */
uint8_t Zctlesc; /* Encode control characters */
#define Zrwindow 1400 /* RX window size (controls garbage count) */
//int Nozmodem = 0; /* If invoked as "rb" */
int lastsent; /* Last char we sent */
uint8_t Not8bit; /* Seven bits seen on header */
//char Lzmanag; /* Local ZMODEM file management request */
//int Restricted = 0; /* restricted; no /.. or ../ in filenames */
//int Quiet=0; /* overrides logic that would otherwise set verbose */
uint8_t Eofseen; /* EOF seen on input set by zfilbuf */
uint8_t firstsec;
char Lastrx;
char Crcflg;
uint8_t errors;
// Dylan (monte_carlo_ecm, bitflipper, etc.) - Removing this for release to save
// memory, only used for debugging
/*
char *frametypes[] = {
(char *)"Carrier Lost", // -3
(char *)"TIMEOUT", // -2
(char *)"ERROR", // -1
#define FTOFFSET 3
(char *)"ZRQINIT",
(char *)"ZRINIT",
(char *)"ZSINIT",
(char *)"ZACK",
(char *)"ZFILE",
(char *)"ZSKIP",
(char *)"ZNAK",
(char *)"ZABORT",
(char *)"ZFIN",
(char *)"ZRPOS",
(char *)"ZDATA",
(char *)"ZEOF",
(char *)"ZFERR",
(char *)"ZCRC",
(char *)"ZCHALLENGE",
(char *)"ZCOMPL",
(char *)"ZCAN",
(char *)"ZFREECNT",
(char *)"ZCOMMAND",
(char *)"ZSTDERR",
(char *)"xxxxx"
#define FRTYPES 22 // Total number of frame types in this array
// not including psuedo negative entries
};
*/
#define badcrc F("Bad CRC");
/* Send ZMODEM binary header hdr of type type */
void zsbhdr(int type, char *hdr)
{
vfile(F("zsbhdr: %s %lx"), frametypes[type+FTOFFSET], rclhdr(hdr));
/* if (type == ZDATA)
for (n = Znulls; --n >=0; )
xsendline(0);
*/
xsendline(ZPAD);
xsendline(ZDLE);
//Pete (El Supremo) This looks wrong but it is correct - the code fails if == is used
if ((Crc32t = Txfcs32)) {
int n;
UNSL long crc;
xsendline(ZBIN32);
zsendline(type);
crc = 0xFFFFFFFFL;
crc = UPDC32(type, crc);
for (n=4; --n >= 0; ++hdr) {
crc = UPDC32((0377 & *hdr), crc);
zsendline(*hdr);
}
crc = ~crc;
for (n=4; --n >= 0;) {
zsendline((int)crc);
crc >>= 8;
}
} else {
int n;
unsigned short crc;
xsendline(ZBIN);
zsendline(type);
crc = updcrc(type, 0);
for (n=4; --n >= 0; ++hdr) {
zsendline(*hdr);
crc = updcrc((0377& *hdr), crc);
}
crc = updcrc(0,updcrc(0,crc));
zsendline(crc>>8);
zsendline(crc);
}
if (type != ZDATA)
flushmo();
}
/* Send ZMODEM HEX header hdr of type type */
void zshhdr(int type,char *hdr)
{
int n;
unsigned short crc;
vfile(F("zshhdr: %s %lx"), frametypes[type+FTOFFSET], rclhdr(hdr));
sendline(ZPAD);
sendline(ZPAD);
sendline(ZDLE);
sendline(ZHEX);
zputhex(type);
Crc32t = 0;
crc = updcrc(type, 0);
for (n=4; --n >= 0; ++hdr) {
zputhex(*hdr);
crc = updcrc((0377 & *hdr), crc);
}
crc = updcrc(0,updcrc(0,crc));
zputhex(crc>>8);
zputhex(crc);
/* Make it printable on remote machine */
sendline(015);
sendline(0212);
/*
* Uncork the remote in case a fake XOFF has stopped data flow
*/
if (type != ZFIN && type != ZACK)
sendline(021);
flushmo();
}
/*
* Send binary array buf of length length, with ending ZDLE sequence frameend
*/
/*
static char *Zendnames[] = {
(char *)"ZCRCE",
(char *)"ZCRCG",
(char *)"ZCRCQ",
(char *)"ZCRCW"
};
*/
void zsdata(char *buf,int length,int frameend)
{
vfile(F("zsdata: %d %s"), length, Zendnames[(frameend-ZCRCE)&3]);
if (Crc32t) {
int c;
UNSL long crc;
crc = 0xFFFFFFFFL;
for (;--length >= 0; ++buf) {
c = *buf & 0377;
if (c & 0140)
xsendline(lastsent = c);
else
zsendline(c);
crc = UPDC32(c, crc);
}
xsendline(ZDLE);
xsendline(frameend);
crc = UPDC32(frameend, crc);
crc = ~crc;
for (length=4; --length >= 0;) {
zsendline((int)crc);
crc >>= 8;
}
} else {
unsigned short crc;
crc = 0;
for (;--length >= 0; ++buf) {
zsendline(*buf);
crc = updcrc((0377 & *buf), crc);
}
xsendline(ZDLE);
xsendline(frameend);
crc = updcrc(frameend, crc);
crc = updcrc(0,updcrc(0,crc));
zsendline(crc>>8);
zsendline(crc);
}
if (frameend == ZCRCW) {
xsendline(XON);
flushmo();
}
}
/*
* Receive array buf of max length with ending ZDLE sequence
* and CRC. Returns the ending character or error code.
* NB: On errors may store length+1 bytes!
*/
int zrdata(char *buf,int length)
{
int c;
char *end;
int d;
if (Rxframeind == ZBIN32) {
UNSL long crc;
crc = 0xFFFFFFFFL;
Rxcount = 0;
end = buf + length;
while (buf <= end) {
if ((c = zdlread()) & ~0377) {
crcfoo32:
switch (c) {
case GOTCRCE:
case GOTCRCG:
case GOTCRCQ:
case GOTCRCW:
d = c;
c &= 0377;
crc = UPDC32(c, crc);
if ((c = zdlread()) & ~0377)
goto crcfoo32;
crc = UPDC32(c, crc);
if ((c = zdlread()) & ~0377)
goto crcfoo32;
crc = UPDC32(c, crc);
if ((c = zdlread()) & ~0377)
goto crcfoo32;
crc = UPDC32(c, crc);
if ((c = zdlread()) & ~0377)
goto crcfoo32;
crc = UPDC32(c, crc);
if (crc != 0xDEBB20E3) {
zperr(badcrc);
return ERROR;
}
Rxcount = length - (end - buf);
vfile(F("zrdat32: %d %s"), Rxcount,
Zendnames[(d-GOTCRCE)&3]);
return d;
case GOTCAN:
zperr("Sender Canceled");
return ZCAN;
case TIMEOUT:
zperr("TIMEOUT");
return c;
default:
zperr("Bad data subpacket");
return c;
}
}
*buf++ = c;
crc = UPDC32(c, crc);
}
zperr("Data subpacket too long");
return ERROR;
} else {
unsigned short crc;
crc = Rxcount = 0;
end = buf + length;
while (buf <= end) {
if ((c = zdlread()) & ~0377) {
crcfoo16:
switch (c) {
case GOTCRCE:
case GOTCRCG:
case GOTCRCQ:
case GOTCRCW:
crc = updcrc((d=c)&0377, crc);
if ((c = zdlread()) & ~0377)
goto crcfoo16;
crc = updcrc(c, crc);
if ((c = zdlread()) & ~0377)
goto crcfoo16;
crc = updcrc(c, crc);
if (crc & 0xFFFF) {
zperr(badcrc);
return ERROR;
}
Rxcount = length - (end - buf);
vfile(F("zrdata: %d %s"), Rxcount,
Zendnames[(d-GOTCRCE)&3]);
return d;
case GOTCAN:
zperr("Sender Canceled");
return ZCAN;
case TIMEOUT:
zperr("TIMEOUT");
return c;
default:
zperr("Bad data subpacket");
return c;
}
}
*buf++ = c;
crc = updcrc(c, crc);
}
zperr("Data subpacket too long");
return ERROR;
}
}
/*
* Read a ZMODEM header to hdr, either binary or hex.
* eflag controls local display of non zmodem characters:
* 0: no display
* 1: display printing characters only
* 2: display all non ZMODEM characters
* On success, set Zmodem to 1, set Rxpos and return type of header.
* Otherwise return negative on error.
* Return ERROR instantly if ZCRCW sequence, for fast error recovery.
*/
int zgethdr(char *hdr,int eflag)
{
int c, n, cancount;
n = Zrwindow; //+ Baudrate; /* Max bytes before start of frame */
Rxframeind = Rxtype = 0;
startover:
cancount = 5;
again:
/* Return immediate ERROR if ZCRCW sequence seen */
ZSERIAL.setTimeout(Rxtimeout * 100);
c = readline(Rxtimeout);
ZSERIAL.setTimeout(TYPICAL_SERIAL_TIMEOUT);
switch (c) {
case RCDO:
case TIMEOUT:
goto fifi;
case CAN:
gotcan:
if (--cancount <= 0) {
c = ZCAN;
goto fifi;
}
switch (c = readline(1)) {
case TIMEOUT:
goto again;
case ZCRCW:
c = ERROR;
/* **** FALL THRU TO **** */
case RCDO:
goto fifi;
default:
break;
case CAN:
if (--cancount <= 0) {
c = ZCAN;
goto fifi;
}
goto again;
}
/* **** FALL THRU TO **** */
default:
agn2:
if ( --n == 0) {
zperr("Garbage count exceeded");
return(ERROR);
}
if (eflag && ((c &= 0177) & 0140))
bttyout(c);
else if (eflag > 1)
bttyout(c);
#ifdef UNIX
fflush(stderr);
#endif
goto startover;
case ZPAD|0200: /* This is what we want. */
Not8bit = c;
case ZPAD: /* This is what we want. */
break;
}
cancount = 5;
splat:
switch (c = noxrd7()) {
case ZPAD:
goto splat;
case RCDO:
case TIMEOUT:
goto fifi;
default:
goto agn2;
case ZDLE: /* This is what we want. */
break;
}
switch (c = noxrd7()) {
case RCDO:
case TIMEOUT:
goto fifi;
case ZBIN:
Rxframeind = ZBIN;
Crc32 = FALSE;
c = zrbhdr(hdr);
break;
case ZBIN32:
Crc32 = Rxframeind = ZBIN32;
c = zrbhdr32(hdr);
break;
case ZHEX:
Rxframeind = ZHEX;
Crc32 = FALSE;
c = zrhhdr(hdr);
break;
case CAN:
goto gotcan;
default:
goto agn2;
}
Rxpos = hdr[ZP3] & 0377;
Rxpos = (Rxpos<<8) + (hdr[ZP2] & 0377);
Rxpos = (Rxpos<<8) + (hdr[ZP1] & 0377);
Rxpos = (Rxpos<<8) + (hdr[ZP0] & 0377);
fifi:
switch (c) {
case GOTCAN:
c = ZCAN;
/* **** FALL THRU TO **** */
case ZNAK:
case ZCAN:
case ERROR:
case TIMEOUT:
case RCDO:
// zperr("Got %s", frametypes[c+FTOFFSET]);
/* **** FALL THRU TO **** */
// default:
// if (c >= -3 && c <= FRTYPES)
// vfile(F("zgethdr: %s %lx"), frametypes[c+FTOFFSET], Rxpos);
// else
// vfile(F("zgethdr: %d %lx"), c, Rxpos);
break;
}
return c;
}
//#endif
/* Receive a binary style header (type and position) */
int zrbhdr(char *hdr)
{
int c, n;
unsigned short crc;
if ((c = zdlread()) & ~0377)
return c;
Rxtype = c;
crc = updcrc(c, 0);
for (n=4; --n >= 0; ++hdr) {
if ((c = zdlread()) & ~0377)
return c;
crc = updcrc(c, crc);
*hdr = c;
}
if ((c = zdlread()) & ~0377)
return c;
crc = updcrc(c, crc);
if ((c = zdlread()) & ~0377)
return c;
crc = updcrc(c, crc);
if (crc & 0xFFFF) {
zperr(badcrc);
return ERROR;
}
#ifdef ZMODEM
Protocol = ZMODEM;
#endif
// Zmodem = 1;
return Rxtype;
}
/* Receive a binary style header (type and position) with 32 bit FCS */
int zrbhdr32(char *hdr)
{
int c, n;
UNSL long crc;
if ((c = zdlread()) & ~0377)
return c;
Rxtype = c;
crc = 0xFFFFFFFFL;
crc = UPDC32(c, crc);
#ifdef DEBUGZ
vfile(F("zrbhdr32 c=%X crc=%lX"), c, crc);
#endif
for (n=4; --n >= 0; ++hdr) {
if ((c = zdlread()) & ~0377)
return c;
crc = UPDC32(c, crc);
*hdr = c;
#ifdef DEBUGZ
vfile(F("zrbhdr32 c=%X crc=%lX"), c, crc);
#endif
}
for (n=4; --n >= 0;) {
if ((c = zdlread()) & ~0377)
return c;
crc = UPDC32(c, crc);
#ifdef DEBUGZ
vfile(F("zrbhdr32 c=%X crc=%lX"), c, crc);
#endif
}
if (crc != 0xDEBB20E3) {
zperr(badcrc);
return ERROR;
}
#ifdef ZMODEM
Protocol = ZMODEM;
#endif
// Zmodem = 1;
return Rxtype;
}
/* Receive a hex style header (type and position) */
int zrhhdr(char *hdr)
{
int c;
unsigned short crc;
int n;
if ((c = zgethex()) < 0)
return c;
Rxtype = c;
crc = updcrc(c, 0);
for (n=4; --n >= 0; ++hdr) {
if ((c = zgethex()) < 0)
return c;
crc = updcrc(c, crc);
*hdr = c;
}
if ((c = zgethex()) < 0)
return c;
crc = updcrc(c, crc);
if ((c = zgethex()) < 0)
return c;
crc = updcrc(c, crc);
if (crc & 0xFFFF) {
zperr(badcrc);
return ERROR;
}
switch ( c = readline(1)) {
case 0215:
Not8bit = c;
/* **** FALL THRU TO **** */
case 015:
/* Throw away possible cr/lf */
switch (c = readline(1)) {
case 012:
Not8bit |= c;
}
}
#ifdef ZMODEM
Protocol = ZMODEM;
#endif
// Zmodem = 1;
return Rxtype;
}
/* Send a byte as two hex digits */
/*void zputhex(int c)
{
static char digits[] = "0123456789abcdef";
if (Verbose>8)
vfile(F("zputhex: %02X"), c);
sendline(digits[(c&0xF0)>>4]);
sendline(digits[(c)&0xF]);
} */
PROGMEM static const char digits[17] = "0123456789abcdef";
void zputhex(int c)
{
// static char digits[] = "0123456789abcdef";
if (Verbose>8)
vfile(F("zputhex: %02X"), c);
sendline(pgm_read_byte(digits+((c&0xF0)>>4)));
sendline(pgm_read_byte(digits+((c)&0xF)));
}
/*
* Send character c with ZMODEM escape sequence encoding.
* Escape XON, XOFF. Escape CR following @ (Telenet net escape)
*/
int zsendline2(int c)
{
/* Quick check for non control characters */
if (c & 0140)
xsendline(lastsent = c);
else {
switch (c &= 0377) {
case ZDLE:
xsendline(ZDLE);
xsendline (lastsent = (c ^= 0100));
break;
case 015:
case 0215:
if (!Zctlesc && (lastsent & 0177) != '@')
goto sendit;
/* **** FALL THRU TO **** */
case 020:
case 021:
case 023:
case 0220:
case 0221:
case 0223:
xsendline(ZDLE);
c ^= 0100;
sendit:
xsendline(lastsent = c);
break;
default:
if (Zctlesc && ! (c & 0140)) {
xsendline(ZDLE);
c ^= 0100;
}
xsendline(lastsent = c);
}
}
}
/* Decode two lower case hex digits into an 8 bit byte value */
int zgethex(void)
{
int c, n;
if ((c = noxrd7()) < 0)
return c;
n = c - '0';
if (n > 9)
n -= ('a' - ':');
if (n & ~0xF)
return ERROR;
if ((c = noxrd7()) < 0)
return c;
c -= '0';
if (c > 9)
c -= ('a' - ':');
if (c & ~0xF)
return ERROR;
c += (n<<4);
return c;
}
/*
* Read a byte, checking for ZMODEM escape encoding
* including CAN*5 which represents a quick abort
*/
/*
int zdlread(void)
{
int c;
again:
// Quick check for non control characters
if ((c = readline(Rxtimeout)) & 0140)
return c;
switch (c) {
case ZDLE:
break;
case 023:
case 0223:
case 021:
case 0221:
goto again;
default:
if (Zctlesc && !(c & 0140)) {
goto again;
}
return c;
}
again2:
if ((c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN && (c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN && (c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN && (c = readline(Rxtimeout)) < 0)
return c;
switch (c) {
case CAN:
return GOTCAN;
case ZCRCE:
case ZCRCG:
case ZCRCQ:
case ZCRCW:
return (c | GOTOR);
case ZRUB0:
return 0177;
case ZRUB1:
return 0377;
case 023:
case 0223:
case 021:
case 0221:
goto again2;
default:
if (Zctlesc && ! (c & 0140)) {
goto again2;
}
if ((c & 0140) == 0100)
return (c ^ 0100);
break;
}
if (Verbose>1)
zperr("Bad escape sequence %x", c);
return ERROR;
}
*/
int zdlread2(int c)
{
again:
// Quick check for non control characters
switch (c) {
case ZDLE:
break;
case 023:
case 0223:
case 021:
case 0221:
if ((c = readline(Rxtimeout)) & 0140)
return c;
goto again;
default:
if (Zctlesc && !(c & 0140)) {
if ((c = readline(Rxtimeout)) & 0140)
return c;
goto again;
}
return c;
}
again2:
if ((c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN && (c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN && (c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN && (c = readline(Rxtimeout)) < 0)
return c;
switch (c) {
case CAN:
return GOTCAN;
case ZCRCE:
case ZCRCG:
case ZCRCQ:
case ZCRCW:
return (c | GOTOR);
case ZRUB0:
return 0177;
case ZRUB1:
return 0377;
case 023:
case 0223:
case 021:
case 0221:
goto again2;
default:
if (Zctlesc && ! (c & 0140)) {
goto again2;
}
if ((c & 0140) == 0100)
return (c ^ 0100);
break;
}
if (Verbose>1)
zperr("Bad escape sequence %x", c);
return ERROR;
}
/*
* Read a character from the modem line with timeout.
* Eat parity, XON and XOFF characters.
*/
int noxrd7(void)
{
int c;
for (;;) {
if ((c = readline(Rxtimeout)) < 0)
return c;
switch (c &= 0177) {
case XON:
case XOFF:
continue;
default:
if (Zctlesc && !(c & 0140))
continue;
case '\r':
case '\n':
case ZDLE:
return c;
}
}
}
/* Store long integer pos in Txhdr */
void stohdr(long pos)
{
Txhdr[ZP0] = pos;
Txhdr[ZP1] = pos>>8;
Txhdr[ZP2] = pos>>16;
Txhdr[ZP3] = pos>>24;
}
#ifndef NOTDEF
/* Recover a long integer from a header */
long rclhdr(char *hdr)
{
long l;
l = (hdr[ZP3] & 0377);
l = (l << 8) | (hdr[ZP2] & 0377);
l = (l << 8) | (hdr[ZP1] & 0377);
l = (l << 8) | (hdr[ZP0] & 0377);
return l;
}
#endif
/*
* Send a character to modem. Small is beautiful.
*/
// Why was this called sendline ??
//void sendline(int c)
//{
// ZSERIAL.write(c & 0xFF);
// ZSERIAL.write(char(c));
// ZSERIAL.flush();
//DSERIAL.print("SEND: ");
//DSERIAL.print(c, HEX);
//DSERIAL.println();
//}
/*
//>>> Needs to be fixed up - see the original in rz
// like sendline, this does not read a line!
int readline(int timeout)
{
long then;
unsigned char c;
then = millis();
while(ZSERIAL.available() < 1) {
if(millis() - then > (unsigned int)timeout*100UL) {
DSERIAL.println("readline - TIMEOUT");
return(TIMEOUT);
}
}
c = ZSERIAL.read();
//DSERIAL.print("READ: ");
//DSERIAL.print(c, HEX);
//DSERIAL.println();
return(c);
}
*/
/*
* Purge the modem input queue of all characters
*/
void purgeline(void)
{
while(ZSERIAL.available())ZSERIAL.read();
}
/*
* Local console output simulation
*/
void bttyout(int c)
{
#ifndef ARDUINO
if (Verbose || Fromcu)
putc(c, stderr);
#endif
}
void flushmo(void)
{
ZSERIAL.flush();
}
/* send cancel string to get the other end to shut up */
void canit(void)
{
for (int i=0; i < 10; ++i) {
ZSERIAL.write(24);
}
for (int i=0; i < 10; ++i) {
ZSERIAL.write(8);
}
ZSERIAL.flush();
}
/* End of zm.c */
#endif