-
Notifications
You must be signed in to change notification settings - Fork 3
/
yaesu_rw.c
2234 lines (1951 loc) · 49.3 KB
/
yaesu_rw.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
/*
* yaesu_rw - Interface to Yaesu radio clone interfaces
* Copyright (C) 2009 Corey Minyard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#ifdef __APPLE__
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <gensio/gensio.h>
#ifndef RADIO_CONFIGDIR
#define RADIO_CONFIGDIR "/etc/radioconf"
#endif
char *version = PACKAGE_VERSION;
static struct option long_options[] = {
{"help", 0, NULL, '?'},
{"hash", 0, NULL, 'h'},
{"verbose", 0, NULL, 'v'},
{"file", 1, NULL, 'f'},
{"ignerr", 0, NULL, 'I'},
{"read", 0, NULL, 'r'},
{"write", 0, NULL, 'w'},
{"rcv_echo", 0, NULL, 'e'},
{"norcv_echo",0, NULL, 'm'},
{"send_echo",0, NULL, 'y'},
{"checksum", 0, NULL, 'c'},
{"nochecksum",0, NULL, 'g'},
{"waitchecksum", 0, NULL, 'j'},
{"nowaitchecksum", 0, NULL, 'k'},
{"checkblock", 0, NULL, 'p'},
{"nocheckblock",0, NULL, 'q'},
{"delayack", 0, NULL, 'l'},
{"nodelayack",0, NULL, 'n'},
{"chunksize",1, NULL, 'a'},
{"waitchunk",1, NULL, 'b'},
{"configdir",0, NULL, 'F'},
{"prewritedelay", 1, NULL, 't'},
{"noendecho", 0, NULL, 'u'},
{"csumdelay", 1, NULL, 'x'},
{NULL, 0, NULL, 0}
};
char *progname = NULL;
int verbose = 0;
int hash = 0;
#define YAESU_TIMEOUT(d, s, u) \
do { \
d->timeout.secs = s; d->timeout.nsecs = u * 1000; \
} while(0)
#define YAESU_TEST_TIMEOUT(d) YAESU_TIMEOUT(d, 1, 0)
#define YAESU_CHAR_TIMEOUT(d) YAESU_TIMEOUT(d, 5, 0)
#define YAESU_TIMING_TIMEOUT(d) YAESU_TIMEOUT(d, 0, 200000)
#define YAESU_WAITWRITE_TIMEOUT(d) YAESU_TIMEOUT(d, 0, 5000)
#define YAESU_CSUMDELAY_TIMEOUT(d) YAESU_TIMEOUT(d, 0, d->csumdelay)
#define YAESU_WAITCHUNK_TIMEOUT(d) YAESU_TIMEOUT(d, 0, d->waitchunk)
#define YAESU_PREWRITE_TIMEOUT(d) YAESU_TIMEOUT(d, d->prewritedelay / 1000000,\
d->prewritedelay % 1000000)
#define YAESU_START_TIMEOUT(d) YAESU_TIMEOUT(d, 10, 0)
#define YAESU_MAX_RETRIES 5
enum yaesu_read_state {
YAESU_STATE_WAITFIRSTBLOCK,
YAESU_STATE_INFIRSTBLOCK,
YAESU_STATE_WAITBLOCK,
YAESU_STATE_INBLOCK,
YAESU_STATE_DELAYCSUM,
YAESU_STATE_DELAYACK,
YAESU_STATE_WAITCSUM,
YAESU_STATE_CSUM,
YAESU_STATE_CSUM_WAITACK,
YAESU_STATE_DONE
};
#define BUFF_ALLOC_INC 16
#define MAX_BLOCK_SIZE 256
struct yaesu_block {
unsigned int len;
unsigned int alloc_len;
unsigned char *buff;
struct yaesu_block *next, *prev;
};
struct yaesu_blocksizes {
/* Note: Last one of nblocks in the array must be 0, it will repeat. */
unsigned short nblocks;
unsigned short block_len;
};
struct yaesu_data {
int err;
int is_read;
enum yaesu_read_state state;
struct gensio *io;
struct gensio_os_funcs *o;
gensio_time timeout;
unsigned int pos;
unsigned int rpos;
unsigned int retries;
struct yaesu_block head;
struct yaesu_block *curr_block;
unsigned int data_count;
unsigned int block_count;
int send_echo;
int recv_echo;
int noendecho;
unsigned int expect_len;
int timeout_mode;
unsigned int csum;
unsigned int block_len;
struct yaesu_blocksizes *bsizes;
unsigned int bsize_left;
unsigned int curr_bsize;
unsigned char write_buf[65536];
unsigned int write_start;
unsigned int write_len;
int waitchunk;
int csumdelay;
int chunksize;
enum { CHUNK_NOWAIT, CHUNK_CHECK, CHUNK_DELAY } waiting_chunk_done;
int has_checksum;
int has_checkblock;
int waitchecksum;
int delayack;
bool in_prewritedelay;
int prewritedelay;
};
#define DEFAULT_CHUNKSIZE 0
#define DEFAULT_WAITCHUNK 0
#define DEFAULT_CSUMDELAY 0
const unsigned char testbuf[8] = { '0', '1', '2', '3', '4', '5', '6', '7' };
const unsigned char ack[1] = { 0x06 };
static void
do_vlog(struct gensio_os_funcs *f, enum gensio_log_levels level,
const char *log, va_list args)
{
fprintf(stderr, "gensio %s log: ", gensio_log_level_to_str(level));
vfprintf(stderr, log, args);
fprintf(stderr, "\n");
}
struct yaesu_data *
alloc_yaesu_data(struct gensio_os_funcs *o, struct gensio *io, int is_read,
int send_echo, int recv_echo, int noendecho, int has_checksum,
int has_checkblock, int waitchecksum, int chunksize,
int waitchunk, int delayack, int prewritedelay, int csumdelay)
{
struct yaesu_data *d;
d = malloc(sizeof(*d));
if (!d)
return NULL;
d->is_read = is_read;
d->state = YAESU_STATE_WAITFIRSTBLOCK;
if (is_read)
YAESU_START_TIMEOUT(d);
else
YAESU_CHAR_TIMEOUT(d);
d->retries = 0;
d->pos = 0;
d->data_count = 0;
d->block_count = 0;
d->head.next = &d->head;
d->head.prev = &d->head;
d->csum = 0;
d->io = io;
d->o = o;
d->write_start = 0;
d->write_len = 0;
d->chunksize = chunksize;
d->waitchunk = waitchunk;
d->csumdelay = csumdelay;
d->delayack = 1;
d->waiting_chunk_done = CHUNK_NOWAIT;
d->send_echo = send_echo;
d->recv_echo = recv_echo;
d->noendecho = noendecho;
d->bsizes = NULL;
d->timeout_mode = 0;
d->has_checksum = has_checksum;
d->has_checkblock = has_checkblock;
d->waitchecksum = waitchecksum;
d->prewritedelay = prewritedelay;
return d;
}
unsigned int
max_blocksize(struct yaesu_data *d)
{
struct yaesu_blocksizes *b = d->bsizes;
unsigned int max = 0;
if (!b)
return d->block_len;
for (;;) {
if (b->block_len > max)
max = b->block_len;
if (!b->nblocks)
break;
b++;
}
return max;
}
void
free_yaesu_data(struct yaesu_data *d)
{
struct yaesu_block *b, *bf;
b = d->head.next;
while (b != &d->head) {
bf = b;
b = b->next;
if (bf->buff)
free(bf->buff);
free(bf);
}
free(d);
}
void
yaesu_get_timeout(struct yaesu_data *d, gensio_time *time)
{
*time = d->timeout;
}
int
yaesu_is_done(struct yaesu_data *d)
{
return d->err || (d->state == YAESU_STATE_DONE && !d->in_prewritedelay);
}
void
yaesu_next_block_len(struct yaesu_data *d)
{
if (d->bsizes) {
if (d->bsize_left == 0) {
d->curr_bsize++;
d->bsize_left = d->bsizes[d->curr_bsize].nblocks;
d->block_len = d->bsizes[d->curr_bsize].block_len;
if (d->bsize_left == 0)
d->bsizes = NULL;
}
d->bsize_left--;
}
}
struct yaesu_block *
yaesu_new_block(struct yaesu_data *d)
{
struct yaesu_block *b;
yaesu_next_block_len(d);
b = malloc(sizeof(*b));
if (!b)
return NULL;
b->buff = malloc(BUFF_ALLOC_INC);
if (!b->buff) {
free(b);
return NULL;
}
d->block_count++;
b->len = 0;
b->alloc_len = BUFF_ALLOC_INC;
b->next = &d->head;
b->prev = d->head.prev;
b->next->prev = b;
b->prev->next = b;
return b;
}
struct yaesu_conf {
char *name;
unsigned char *header;
unsigned int header_cmp_len;
unsigned int header_len;
unsigned int data_len;
unsigned int block_size;
int recv_echo;
int send_echo;
int noendecho;
int has_checksum;
int has_checkblock;
int waitchecksum;
int chunksize;
int waitchunk;
int csumdelay;
int delayack;
int prewritedelay;
struct yaesu_blocksizes *bsizes;
struct yaesu_conf *next;
};
struct yaesu_conf *radios = NULL;
unsigned int
max_header_size(void)
{
struct yaesu_conf *r;
unsigned int max = 0;
for(r = radios; r; r = r->next) {
if (r->header_len > max)
max = r->header_len;
}
return max;
}
int
check_yaesu_type(struct yaesu_data *d, unsigned char *buff, unsigned int len,
int do_default)
{
struct yaesu_conf *r;
for(r = radios; r; r = r->next) {
if (len != r->header_len)
continue;
if (memcmp(r->header, buff, r->header_cmp_len) == 0) {
/* found it */
printf("Found a %s\n", r->name);
d->expect_len = r->data_len;
if (r->bsizes) {
d->bsizes = r->bsizes;
d->curr_bsize = 0;
d->bsize_left = r->bsizes[0].nblocks;
d->block_len = r->bsizes[0].block_len;
} else
d->block_len = r->block_size;
if (d->recv_echo < 0)
d->recv_echo = r->recv_echo;
if (d->send_echo < 0)
d->send_echo = r->send_echo;
if (d->noendecho < 0)
d->noendecho = r->noendecho;
if (d->has_checksum < 0)
d->has_checksum = r->has_checksum;
if (d->has_checkblock < 0)
d->has_checkblock = r->has_checkblock;
if (d->waitchecksum < 0)
d->waitchecksum = r->waitchecksum;
if (d->chunksize < 0)
d->chunksize = r->chunksize;
if (d->waitchunk < 0)
d->waitchunk = r->waitchunk;
if (d->csumdelay < 0)
d->csumdelay = r->csumdelay;
if (d->delayack < 0)
d->delayack = r->delayack;
if (d->prewritedelay < 0)
d->prewritedelay = r->prewritedelay;
return 1;
}
}
if (do_default) {
printf("Unable to find the device, going ahead, but it probably won't"
" work\n");
d->expect_len = 1000000;
d->block_len = 64;
if (d->recv_echo < 0)
d->recv_echo = 0;
if (d->send_echo < 0)
d->send_echo = 0;
if (d->noendecho < 0)
d->noendecho = 0;
if (d->has_checksum < 0)
d->has_checksum = 0;
if (d->has_checkblock < 0)
d->has_checkblock = 0;
if (d->waitchecksum < 0)
d->waitchecksum = 1;
if (d->waitchunk < 0)
d->waitchunk = DEFAULT_WAITCHUNK;
if (d->csumdelay < 0)
d->csumdelay = DEFAULT_CSUMDELAY;
if (d->chunksize < 0)
d->chunksize = DEFAULT_CHUNKSIZE;
if (d->delayack < 0)
d->delayack = 1;
d->timeout_mode = 1;
return 1;
}
return 0;
}
int
append_yaesu_data(struct yaesu_data *d, struct yaesu_block *b,
unsigned char *buf, unsigned int len)
{
unsigned int new_len = b->len + len;
if (new_len > b->alloc_len) {
unsigned int alloc_len;
unsigned char *nb;
alloc_len = b->alloc_len + BUFF_ALLOC_INC;
while (alloc_len < new_len)
alloc_len = alloc_len + BUFF_ALLOC_INC;
nb = malloc(alloc_len);
if (!nb)
return GE_NOMEM;
memcpy(nb, b->buff, b->len);
free(b->buff);
b->alloc_len = alloc_len;
b->buff = nb;
}
memcpy(b->buff + b->len, buf, len);
b->len += len;
return 0;
}
int
add_yaesu_block(struct yaesu_data *d, unsigned char *data, unsigned int size)
{
struct yaesu_block *b;
b = yaesu_new_block(d);
if (!b)
return GE_NOMEM;
b->buff = malloc(size);
if (!b->buff) {
free(b);
return GE_NOMEM;
}
memcpy(b->buff, data, size);
b->len = size;
d->data_count += size;
return 0;
}
int
yaesu_write(struct yaesu_data *d, const unsigned char *data, unsigned int len,
bool *written)
{
unsigned int end;
int rv;
unsigned int total_written = 0;
if (len + d->write_len > sizeof(d->write_buf)) {
*written = false;
return 0;
}
*written = true;
if (len > 0) {
if (verbose > 1) {
unsigned int i;
printf("Write:");
for (i = 0; i < len; i++)
printf(" %2.2x", data[i]);
printf("\n");
fflush(stdout);
}
end = (d->write_start + d->write_len) % sizeof(d->write_buf);
if ((end + len) < sizeof(d->write_buf))
memcpy(d->write_buf + end, data, len);
else {
unsigned int left = 256 - end;
memcpy(d->write_buf + end, data, left);
memcpy(d->write_buf, data + left, len - left);
}
d->write_len += len;
}
if (d->prewritedelay) {
if (d->in_prewritedelay) {
d->in_prewritedelay = false;
} else {
d->in_prewritedelay = true;
gensio_set_write_callback_enable(d->io, false);
YAESU_PREWRITE_TIMEOUT(d);
return 0;
}
}
end = (d->write_start + d->write_len) % sizeof(d->write_buf);
if (d->write_len > 0) {
int to_write;
gensiods written;
if (end > d->write_start) {
write_nowrap:
if ((d->chunksize > 0)
&& (total_written + d->write_len > (unsigned int) d->chunksize))
to_write = d->chunksize - total_written;
else
to_write = d->write_len;
rv = gensio_write(d->io, &written, d->write_buf + d->write_start,
to_write, NULL);
if (rv)
return rv;
total_written += written;
if (written > 0 && verbose > 2) {
gensiods i;
gensio_time time;
gensio_os_funcs_get_monotonic_time(d->o, &time);
printf("Write(2: %ld:%6.6ld):", time.secs, (long) time.nsecs);
for (i = 0; i < written; i++)
printf(" %2.2x", d->write_buf[d->write_start+i]);
printf("\n");
fflush(stdout);
}
d->write_len -= written;
d->write_start += written;
if ((d->chunksize > 0)
&& (total_written >= (unsigned int) d->chunksize))
goto out_delay;
if (d->write_len > 0)
goto not_all_written;
} else {
unsigned int wrc;
/*
* Note that this also covers a completely full buffer, where
* end == write_start.
*/
wrc = sizeof(d->write_buf) - d->write_start;
if ((d->chunksize > 0)
&& (total_written + wrc > (unsigned int) d->chunksize))
to_write = d->chunksize - total_written;
else
to_write = d->write_len;
rv = gensio_write(d->io, &written,
d->write_buf + d->write_start, to_write, NULL);
printf("B: %d\n", rv);
if (rv)
return rv;
total_written += written;
if (written > 0 && verbose > 2) {
gensiods i;
printf("Write(2):");
for (i = 0; i < written; i++)
printf(" %2.2x", d->write_buf[d->write_start+i]);
printf("\n");
fflush(stdout);
}
d->write_len -= written;
d->write_start += written;
if (d->write_start >= sizeof(d->write_buf)) {
/* We wrote it all, move to the beginning of the buffer. */
d->write_start = 0;
if (d->write_len > 0)
/* Finish up the write. */
goto write_nowrap;
} else if ((d->chunksize > 0)
&& (total_written >= (unsigned int) d->chunksize))
goto out_delay;
else
goto not_all_written;
}
}
gensio_set_write_callback_enable(d->io, false);
return 0;
out_delay:
gensio_set_write_callback_enable(d->io, false);
YAESU_WAITWRITE_TIMEOUT(d);
d->waiting_chunk_done = CHUNK_CHECK;
return 0;
not_all_written:
gensio_set_write_callback_enable(d->io, true);
return 0;
}
int
yaesu_check_block(struct yaesu_data *d, struct yaesu_block *b)
{
unsigned int i;
unsigned int sum;
if (!d->has_checkblock)
return 0;
if (b->len < 2)
return GE_INVAL;
if (b->buff[0] != (d->block_count - 1))
return GE_INVAL;
sum = 0;
for (i = 1; i < b->len - 1; i++) {
sum += b->buff[i];
b->buff[i-1] = b->buff[i];
}
b->len -= 2; /* We remove the block number and the checksum */
if (b->buff[b->len + 1] != (sum & 0xff))
return GE_INVAL;
return 0;
}
int
handle_yaesu_read_data(struct yaesu_data *d,
unsigned char *buf, gensiods buflen)
{
int rv, err;
unsigned int i;
struct yaesu_block *b;
bool written;
if (verbose > 1) {
gensio_time time;
gensio_os_funcs_get_monotonic_time(d->o, &time);
printf("Read (%ld:%6.6ld):", time.secs, (long) time.nsecs);
for (i = 0; i < buflen; i++)
printf(" %2.2x", buf[i]);
printf("\n");
fflush(stdout);
}
if (d->send_echo) {
/* Radio echos everything received. */
int err = yaesu_write(d, buf, buflen, &written);
if (err)
return err;
if (!written)
return GE_TOOBIG;
}
switch(d->state) {
case YAESU_STATE_WAITFIRSTBLOCK:
d->state = YAESU_STATE_INFIRSTBLOCK;
b = yaesu_new_block(d);
if (!b)
return GE_NOMEM;
YAESU_CHAR_TIMEOUT(d);
/* FALLTHROUGH */
case YAESU_STATE_INFIRSTBLOCK:
b = d->head.prev;
if ((buflen + b->len) > MAX_BLOCK_SIZE)
return GE_INVAL;
err = append_yaesu_data(d, b, buf, buflen);
if (err)
return err;
d->data_count += buflen;
for (i = 0; i < buflen; i++)
d->csum += buf[i];
if (check_yaesu_type(d, b->buff, b->len, 0)) {
if (hash) {
printf(".");
fflush(stdout);
}
d->state = YAESU_STATE_WAITBLOCK;
rv = yaesu_write(d, ack, 1, &written);
if (rv)
return rv;
if (!written)
return GE_TOOBIG;
}
break;
case YAESU_STATE_WAITBLOCK:
if (d->recv_echo) {
if (buf[0] != ack[0])
return GE_INVAL;
buf++;
buflen--;
}
d->state = YAESU_STATE_INBLOCK;
b = yaesu_new_block(d);
if (!b)
return GE_NOMEM;
if (buflen == 0)
break;
/* FALLTHROUGH */
case YAESU_STATE_INBLOCK:
b = d->head.prev;
if (!d->timeout_mode && ((buflen + b->len) > d->block_len))
return GE_INVAL;
err = append_yaesu_data(d, b, buf, buflen);
if (err)
return err;
d->data_count += buflen;
for (i = 0; i < buflen; i++)
d->csum += buf[i];
if (d->data_count > d->expect_len)
return GE_INVAL;
else if (d->data_count == d->expect_len) {
int doack = 1;
rv = yaesu_check_block(d, b);
if (rv)
return rv;
if (d->has_checksum)
d->state = YAESU_STATE_WAITCSUM;
else if (d->delayack) {
/* Some radios are picky and want a delay before sending the
last ack. */
YAESU_WAITWRITE_TIMEOUT(d);
d->state = YAESU_STATE_DELAYACK;
doack = 0;
} else
d->state = YAESU_STATE_DONE;
if (doack) {
rv = yaesu_write(d, ack, 1, &written);
if (rv)
return rv;
if (!written)
return GE_TOOBIG;
}
} else if (!d->timeout_mode && (b->len == d->block_len)) {
if (hash) {
printf(".");
fflush(stdout);
}
rv = yaesu_check_block(d, b);
if (rv)
return rv;
d->state = YAESU_STATE_WAITBLOCK;
rv = yaesu_write(d, ack, 1, &written);
if (rv)
return rv;
if (!written)
return GE_TOOBIG;
}
break;
case YAESU_STATE_WAITCSUM:
if (d->recv_echo && !d->noendecho) {
if (buf[0] != ack[0])
return GE_INVAL;
buf++;
buflen--;
}
d->state = YAESU_STATE_CSUM;
if (buflen == 0)
break;
/* FALLTHROUGH */
case YAESU_STATE_CSUM:
if ((d->csum & 0xff) != buf[0])
/* Checksum failure. */
return GE_INVAL;
if (d->delayack) {
/* Some radios are picky and want a delay before sending the
last ack. */
YAESU_WAITWRITE_TIMEOUT(d);
d->state = YAESU_STATE_DELAYACK;
} else {
rv = yaesu_write(d, ack, 1, &written);
if (rv)
return rv;
if (!written)
return GE_TOOBIG;
}
break;
case YAESU_STATE_CSUM_WAITACK:
case YAESU_STATE_DONE:
case YAESU_STATE_DELAYCSUM:
case YAESU_STATE_DELAYACK:
break;
}
return 0;
}
int
handle_yaesu_read_timeout(struct yaesu_data *d)
{
int rv;
unsigned int i;
struct yaesu_block *b;
bool written;
switch(d->state) {
case YAESU_STATE_INFIRSTBLOCK:
goto timeout2;
case YAESU_STATE_INBLOCK:
case YAESU_STATE_WAITBLOCK:
if (d->timeout_mode)
goto timeout2;
/* FALLTHRU */
case YAESU_STATE_WAITFIRSTBLOCK:
case YAESU_STATE_WAITCSUM:
case YAESU_STATE_CSUM:
return GE_TIMEDOUT;
case YAESU_STATE_DELAYACK:
d->state = YAESU_STATE_DONE;
rv = yaesu_write(d, ack, 1, &written);
if (rv)
return rv;
if (!written)
return GE_TOOBIG;
return 0;
case YAESU_STATE_CSUM_WAITACK:
case YAESU_STATE_DONE:
case YAESU_STATE_DELAYCSUM:
break;
}
return 0;
timeout2:
switch(d->state) {
case YAESU_STATE_INFIRSTBLOCK:
b = d->head.prev;
check_yaesu_type(d, b->buff, b->len, 1);
YAESU_TIMING_TIMEOUT(d);
printf("Going into timer mode, dumping block information\n");
printf("Header size = %d\n", b->len);
printf("Header data is:");
for (i = 0; i < b->len; i++)
printf(" %2.2x", b->buff[i]);
printf("\nBlock sizes:");
fflush(stdout);
if (hash) {
printf(".");
fflush(stdout);
}
d->state = YAESU_STATE_WAITBLOCK;
rv = yaesu_write(d, ack, 1, &written);
if (rv)
return rv;
if (!written)
return GE_TOOBIG;
break;
case YAESU_STATE_INBLOCK:
b = d->head.prev;
printf(" %d", b->len);
fflush(stdout);
if (hash) {
printf(".");
fflush(stdout);
}
d->state = YAESU_STATE_WAITBLOCK;
rv = yaesu_write(d, ack, 1, &written);
if (rv)
return rv;
if (!written)
return GE_TOOBIG;
break;
case YAESU_STATE_WAITBLOCK:
/* Must be end of data. The last block will be the checksum. */
b = d->head.prev;
if (b->len == 0) {
printf("Last data block was 0 bytes, not a checksum?\n");
return GE_INVAL;
}
b->len--;
d->csum -= b->buff[b->len];
if (d->has_checksum && (d->csum & 0xff) != b->buff[b->len])
/* Checksum failure. */
return GE_INVAL;
d->state = YAESU_STATE_DONE;
break;
default:
abort();
}
return 0;
}
int
yaesu_start_write(struct yaesu_data *d)
{
struct yaesu_block *b;
int rv;
unsigned int i;
unsigned int to_write;
bool written;
d->data_count = 0;
d->curr_block = d->head.next;
d->rpos = 0;
if (hash) {
printf(".");
fflush(stdout);
}
b = d->curr_block;
for (i = 0; i < b->len; i++)
d->csum += b->buff[i];
to_write = b->len - d->pos;
rv = yaesu_write(d, b->buff + d->pos, to_write, &written);
if (rv)
return rv;
if (written) {
d->pos = to_write;
d->data_count += to_write;
}
return 0;
}
int
handle_yaesu_write_ready(struct yaesu_data *d)
{
struct yaesu_block *b;
int rv;
unsigned int to_write;
bool written;
if (d->is_read) {
rv = yaesu_write(d, NULL, 0, &written);
if (rv)
return rv;
if (!written)
return GE_TOOBIG;
}
if (d->state == YAESU_STATE_WAITCSUM) {
unsigned char cs[1];
cs[0] = d->csum;
rv = yaesu_write(d, cs, 1, &written);
if (rv)
return rv;
if (!written)
return 0;
if (!d->waitchecksum)
d->state = YAESU_STATE_DONE;
else if (!d->recv_echo)
d->state = YAESU_STATE_CSUM_WAITACK;
else
d->state = YAESU_STATE_CSUM;
return 0;
}
b = d->curr_block;
to_write = b->len - d->pos;
rv = yaesu_write(d, b->buff + d->pos, to_write, &written);
if (rv)
return rv;
if (!written)
return 0;
d->pos += to_write;
d->data_count += to_write;
return 0;
}
int
handle_yaesu_write_data(struct yaesu_data *d,
unsigned char *buf, gensiods buflen)
{
struct yaesu_block *b;
int rv;
unsigned int i;
if (verbose > 1) {
gensio_time time;
gensio_os_funcs_get_monotonic_time(d->o, &time);
printf("Read (%d: %ld:%6.6ld):", d->state, time.secs, (long)time.nsecs);
for (i = 0; i < buflen; i++)
printf(" %2.2x", buf[i]);
printf("\n");
fflush(stdout);
}
if (d->send_echo) {
bool written;
int err = yaesu_write(d, buf, buflen, &written);
if (err)
return err;
if (!written)