-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathuv-5r.c
1519 lines (1342 loc) · 46.4 KB
/
uv-5r.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
/*
* Interface to Baofeng UV-5R and compatibles.
*
* Copyright (C) 2013 Serge Vakulenko, KK6ABQ
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "radio.h"
#include "util.h"
#define NCHAN 128
static const char *PTTID_NAME[] = { "-", "Begin", "End", "Both" };
static const char *STEP_NAME[] = { "2.5", "5.0", "6.25", "10.0", "12.5", "20.0", "25.0", "50.0" };
static const char *SAVER_NAME[] = { "Off", "1", "2", "3", "4", "?5?", "?6?", "?7?" };
static const char *VOX_NAME[] = { "Off", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "?11?", "?12?", "?13?", "?14?", "?15?" };
static const char *ABR_NAME[] = { "Off", "1", "2", "3", "4", "5", "?6?", "?7?" };
static const char *DTMF_SIDETONE_NAME[] = { "Off", "DTMF Only", "ANI Only", "DTMF+ANI" };
static const char *SCAN_RESUME_NAME[] = { "TO", "CO", "SE", "??" };
static const char *DISPLAY_MODE_NAME[] = { "Channel", "Name", "Frequency", "??" };
static const char *COLOR_NAME[] = { "Off", "Blue", "Orange", "Purple" };
static const char *ALARM_NAME[] = { "Site", "Tone", "Code", "??" };
static const char *RPSTE_NAME[] = { "Off", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "?11?", "?12?", "?13?", "?14?", "?15?" };
static const char *VOICE_NAME[] = { "Off", "English", "Chinese", "??" };
static const char *OFF_ON[] = { "Off", "On" };
//
// Print a generic information about the device.
//
static void uv5r_print_version(FILE *out, int show_version)
{
// Don't print firmware and serial number to file,
// to prevent the user to copy them from one radio to another.
if (show_version) {
// Copy the string, trim spaces.
char buf[17];
const char *version = trim_str((const char *)&radio_mem[0x1EC0 + 0x30], 14, buf);
// 3+poweron message
fprintf(out, "Firmware: %s\n", version);
// Copy the string, trim spaces.
const char *serial = trim_str((const char *)&radio_mem[0x1EC0 + 0x10], 16, buf);
// 6+poweron message
fprintf(out, "Serial: %.16s\n", serial);
}
}
static void aged_print_version(FILE *out, int show_version)
{
// Nothing to print.
}
//
// Check that the radio does support this frequency.
//
static int is_valid_frequency(int mhz)
{
if (mhz >= 136 && mhz <= 174)
return 1;
if (mhz >= 400 && mhz <= 520)
return 1;
return 0;
}
//
// Read block of data, up to 64 bytes.
// Halt the program on any error.
//
static void read_block(int fd, int start, unsigned char *data, int nbytes)
{
unsigned char cmd[4], reply[4];
int addr, len;
// Send command.
cmd[0] = 'S';
cmd[1] = start >> 8;
cmd[2] = start;
cmd[3] = nbytes;
serial_write(fd, cmd, 4);
// Read reply.
if (serial_read(fd, reply, 4) != 4) {
fprintf(stderr, "Radio refused to send block 0x%04x.\n", start);
exit(-1);
}
// On BF-F8HP, we may get acknowledge from previous block.
if (reply[0] == 0x06) {
reply[0] = reply[1];
reply[1] = reply[2];
reply[2] = reply[3];
if (serial_read(fd, &reply[3], 1) != 1) {
fprintf(stderr, "Radio refused to send block 0x%04x.\n", start);
exit(-1);
}
}
addr = reply[1] << 8 | reply[2];
if (reply[0] != 'X' || addr != start || reply[3] != nbytes) {
fprintf(stderr, "Bad reply for block 0x%04x of %d bytes: %02x-%02x-%02x-%02x\n", start,
nbytes, reply[0], reply[1], reply[2], reply[3]);
exit(-1);
}
// Read data.
len = serial_read(fd, data, 0x40);
if (len != nbytes) {
fprintf(stderr, "Reading block 0x%04x: got only %d bytes.\n", start, len);
exit(-1);
}
// Get acknowledge.
// Note that on BF-F8HP acknowledge may be delayed until next command.
serial_write(fd, "\x06", 1);
if (serial_read(fd, reply, 1) == 1) {
if (reply[0] != 0x06) {
fprintf(stderr, "Bad acknowledge after block 0x%04x: %02x\n", start, reply[0]);
exit(-1);
}
}
if (trace_flag) {
printf("# Read 0x%04x: ", start);
print_hex(data, nbytes);
printf("\n");
} else {
++radio_progress;
if (radio_progress % 2 == 0) {
fprintf(stderr, "#");
fflush(stderr);
}
}
}
//
// Write block of data, up to 16 bytes.
// Halt the program on any error.
//
static void write_block(int fd, int start, const unsigned char *data, int nbytes)
{
unsigned char cmd[4], reply;
// Send command.
cmd[0] = 'X';
cmd[1] = start >> 8;
cmd[2] = start;
cmd[3] = nbytes;
serial_write(fd, cmd, 4);
serial_write(fd, data, nbytes);
// Get acknowledge.
if (serial_read(fd, &reply, 1) != 1) {
fprintf(stderr, "No acknowledge after block 0x%04x.\n", start);
exit(-1);
}
if (reply != 0x06) {
fprintf(stderr, "Bad acknowledge after block 0x%04x: %02x\n", start, reply);
exit(-1);
}
if (trace_flag) {
printf("# Write 0x%04x: ", start);
print_hex(data, nbytes);
printf("\n");
} else {
++radio_progress;
if (radio_progress % 8 == 0) {
fprintf(stderr, "#");
fflush(stderr);
}
}
}
//
// Read memory image from the device.
//
static void uv5r_download()
{
int addr;
// Main block.
for (addr = 0; addr < 0x1800; addr += 0x40)
read_block(radio_port, addr, &radio_mem[addr], 0x40);
// Auxiliary block starts at 0x1EC0.
for (addr = 0x1EC0; addr < 0x2000; addr += 0x40)
read_block(radio_port, addr, &radio_mem[addr], 0x40);
}
static void aged_download()
{
int addr;
// Main block only.
for (addr = 0; addr < 0x1800; addr += 0x40)
read_block(radio_port, addr, &radio_mem[addr], 0x40);
}
//
// Write memory image to the device.
//
static void uv5r_upload(int cont_flag)
{
int addr;
// Main block.
for (addr = 0; addr < 0x1800; addr += 0x10)
write_block(radio_port, addr, &radio_mem[addr], 0x10);
// Auxiliary block starts at 0x1EC0.
for (addr = 0x1EC0; addr < 0x2000; addr += 0x10)
write_block(radio_port, addr, &radio_mem[addr], 0x10);
}
static void aged_upload()
{
int addr;
// Main block only.
for (addr = 0; addr < 0x1800; addr += 0x10)
write_block(radio_port, addr, &radio_mem[addr], 0x10);
}
static void decode_squelch(uint16_t index, int *ctcs, int *dcs)
{
if (index == 0 || index == 0xffff) {
// Squelch disabled.
return;
}
if (index >= 0x0258) {
// CTCSS value is Hz multiplied by 10.
*ctcs = index;
*dcs = 0;
return;
}
// DCS mode.
if (index - 1 < NDCS)
*dcs = DCS_CODES[index - 1];
else
*dcs = -DCS_CODES[index - 1 - NDCS];
*ctcs = 0;
}
//
// Convert squelch string to tone value in BCD format.
// Four possible formats:
// nnn.n - CTCSS frequency
// DnnnN - DCS normal
// DnnnI - DCS inverted
// '-' - Disabled
//
static int encode_squelch(char *str)
{
unsigned val;
if (*str == 'D' || *str == 'd') {
// DCS tone
char *e;
val = strtol(++str, &e, 10);
// Find a valid index in DCS table.
int i;
for (i = 1; i <= NDCS; i++)
if (DCS_CODES[i - 1] == val)
break;
if (i > NDCS)
return 0;
if (*e == 'N' || *e == 'n') {
val = i;
} else if (*e == 'I' || *e == 'i') {
val = i + NDCS + 1;
} else {
return 0;
}
} else if (*str >= '0' && *str <= '9') {
// CTCSS tone
float hz;
if (sscanf(str, "%f", &hz) != 1)
return 0;
// Round to integer.
val = iround(hz * 10.0);
if (val < 0x0258)
return 0;
} else {
// Disabled
return 0;
}
return val;
}
typedef struct {
uint32_t rxfreq; // binary coded decimal, 8 digits
uint32_t txfreq; // binary coded decimal, 8 digits
uint16_t rxtone;
uint16_t txtone;
uint8_t scode : 4;
uint8_t _u1 : 4;
uint8_t _u2;
uint8_t lowpower : 1;
uint8_t _u3 : 7;
uint8_t pttidbot : 1;
uint8_t pttideot : 1;
uint8_t scan : 1;
uint8_t bcl : 1;
uint8_t _u5 : 2;
uint8_t wide : 1;
uint8_t _u4 : 1;
} memory_channel_t;
static void decode_channel(int i, char *name, int *rx_hz, int *tx_hz, int *rx_ctcs, int *tx_ctcs,
int *rx_dcs, int *tx_dcs, int *lowpower, int *wide, int *scan, int *bcl,
int *pttid, int *scode)
{
memory_channel_t *ch = i + (memory_channel_t *)radio_mem;
*rx_hz = *tx_hz = *rx_ctcs = *tx_ctcs = *rx_dcs = *tx_dcs = 0;
*name = 0;
if (ch->rxfreq == 0 || bcd_invalid(ch->rxfreq))
return;
// Extract channel name; strip trailing FF's.
char *p;
strncpy(name, (char *)&radio_mem[0x1000 + i * 16], 7);
name[7] = 0;
for (p = name + 6; p >= name && *p == '\xff'; p--)
*p = 0;
// Decode channel frequencies.
*rx_hz = bcd_to_int(ch->rxfreq) * 10;
*tx_hz = bcd_to_int(ch->txfreq) * 10;
// Decode squelch modes.
decode_squelch(ch->rxtone, rx_ctcs, rx_dcs);
decode_squelch(ch->txtone, tx_ctcs, tx_dcs);
// Other parameters.
*lowpower = ch->lowpower;
*wide = ch->wide;
*scan = ch->scan;
*bcl = ch->bcl;
*scode = ch->scode;
*pttid = ch->pttidbot | (ch->pttideot << 1);
}
//
// Encode a character from ASCII to internal index.
//
static int encode_char(int c)
{
// Replace underscore by space.
if (c == '_')
c = ' ';
// Only uppercase letters.
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
if (c > '~')
c = '~';
return c;
}
//
// Set a name for the channel.
//
static void encode_name(int i, char *name)
{
unsigned char *data = &radio_mem[0x1000 + i * 16];
int n;
if (name && *name && *name != '-') {
// Setup channel name.
for (n = 0; n < 7 && name[n]; n++) {
data[n] = encode_char(name[n]);
}
for (; n < 7; n++)
data[n] = ' ';
} else {
// Clear name.
for (n = 0; n < 7; n++)
data[n] = 0xff;
}
}
static void setup_channel(int i, char *name, double rx_mhz, double tx_mhz, int rq, int tq,
int lowpower, int wide, int scan, int bcl, int scode, int pttid)
{
memory_channel_t *ch = i + (memory_channel_t *)radio_mem;
ch->rxfreq = int_to_bcd(iround(rx_mhz * 100000.0));
if (is_valid_frequency(tx_mhz)) {
ch->txfreq = int_to_bcd(iround(tx_mhz * 100000.0));
} else {
// disable TX
ch->txfreq = 0xffffffff;
}
ch->rxtone = rq;
ch->txtone = tq;
ch->lowpower = lowpower;
ch->wide = wide;
ch->scan = scan;
ch->bcl = bcl;
ch->scode = scode;
ch->pttidbot = pttid & 1;
ch->pttideot = pttid >> 1;
ch->_u1 = 0;
ch->_u2 = 0;
ch->_u3 = 0;
ch->_u4 = 0;
ch->_u5 = 0;
encode_name(i, name);
}
static void erase_channel(int i)
{
memory_channel_t *ch = i + (memory_channel_t *)radio_mem;
// Erase channel name.
memset(ch, 0xff, 16);
memset(&radio_mem[0x1000 + i * 16], 0xff, 7);
}
typedef struct {
uint8_t enable;
uint8_t lower_msb; // binary coded decimal, 4 digits
uint8_t lower_lsb;
uint8_t upper_msb; // binary coded decimal, 4 digits
uint8_t upper_lsb;
} limits_t;
//
// Looks like limits are not implemented on old firmware
// (prior to version 291).
//
static void decode_limits(char band, int *enable, int *lower, int *upper)
{
int offset = (band == 'V') ? 0x1EC0 + 0x100 : 0x1EC0 + 0x105;
limits_t *limits = (limits_t *)(radio_mem + offset);
*enable = limits->enable;
*lower = ((limits->lower_msb >> 4) & 15) * 1000 + (limits->lower_msb & 15) * 100 +
((limits->lower_lsb >> 4) & 15) * 10 + (limits->lower_lsb & 15);
*upper = ((limits->upper_msb >> 4) & 15) * 1000 + (limits->upper_msb & 15) * 100 +
((limits->upper_lsb >> 4) & 15) * 10 + (limits->upper_lsb & 15);
}
static void setup_limits(char band, int enable, int lower, int upper)
{
int offset = (band == 'V') ? 0x1EC0 + 0x100 : 0x1EC0 + 0x105;
limits_t *limits = (limits_t *)(radio_mem + offset);
limits->enable = enable;
limits->lower_msb = ((lower / 1000) % 10) << 4 | ((lower / 100) % 10);
limits->lower_lsb = ((lower / 10) % 10) << 4 | (lower % 10);
limits->upper_msb = ((upper / 1000) % 10) << 4 | ((upper / 100) % 10);
limits->upper_lsb = ((upper / 10) % 10) << 4 | (upper % 10);
}
static void fetch_ani(char *ani)
{
int i;
for (i = 0; i < 5; i++)
ani[i] = "0123456789ABCDEF"[radio_mem[0x0CAA + i] & 0x0f];
}
static void setup_ani(char *ani)
{
int i, v;
for (i = 0; i < 5; i++) {
v = ani[i];
// Get next hex digit.
if (v >= '0' && v <= '9')
v -= '0';
else if (v >= 'A' && v <= 'F')
v -= 'A' - 10;
else if (v >= 'a' && v <= 'f')
v -= 'a' - 10;
else
v = 0;
radio_mem[0x0CAA + i] = v;
}
}
typedef struct {
uint8_t freq[8]; // binary coded decimal, 8 digits
uint8_t _u1;
uint8_t offset[4]; // binary coded decimal, 8 digits
uint8_t _u2;
uint16_t rxtone;
uint16_t txtone;
uint8_t band : 1;
uint8_t _u3 : 7;
uint8_t _u4;
uint8_t scode : 4;
uint8_t _u5 : 4;
uint8_t _u6;
uint8_t _u7 : 4;
uint8_t step : 3;
uint8_t _u8 : 1;
uint8_t _u9 : 6;
uint8_t narrow : 1;
uint8_t lowpower : 1;
} vfo_t;
void uv5r_decode_vfo(int index, int *band, int *hz, int *offset, int *rx_ctcs, int *tx_ctcs,
int *rx_dcs, int *tx_dcs, int *lowpower, int *wide, int *step, int *scode)
{
vfo_t *vfo = (vfo_t *)&radio_mem[index ? 0x0F28 : 0x0F08];
*band = *hz = *offset = *rx_ctcs = *tx_ctcs = *rx_dcs = *tx_dcs = 0;
*lowpower = *wide = *step = *scode = 0;
*band = vfo->band;
*hz = (vfo->freq[0] & 15) * 100000000 + (vfo->freq[1] & 15) * 10000000 +
(vfo->freq[2] & 15) * 1000000 + (vfo->freq[3] & 15) * 100000 +
(vfo->freq[4] & 15) * 10000 + (vfo->freq[5] & 15) * 1000 + (vfo->freq[6] & 15) * 100 +
(vfo->freq[7] & 15) * 10;
*offset = (vfo->offset[0] & 15) * 100000000 + (vfo->offset[1] & 15) * 10000000 +
(vfo->offset[2] & 15) * 1000000 + (vfo->offset[3] & 15) * 100000;
decode_squelch(vfo->rxtone, rx_ctcs, rx_dcs);
decode_squelch(vfo->txtone, tx_ctcs, tx_dcs);
*lowpower = vfo->lowpower;
*wide = !vfo->narrow;
*step = vfo->step;
*scode = vfo->scode;
}
void uv5r_setup_vfo(int index, int band, double rx_mhz, double tx_offset_mhz, int rxtone, int txtone, int step,
int lowpower, int wide, int scode)
{
vfo_t *vfo = (vfo_t *)&radio_mem[index ? 0x0F28 : 0x0F08];
unsigned hz = iround(rx_mhz * 1000000.0);
unsigned offset = iround(tx_offset_mhz * 1000000.0);
vfo->band = (band == 'U');
vfo->freq[0] = (hz / 100000000) % 10;
vfo->freq[1] = (hz / 10000000) % 10;
vfo->freq[2] = (hz / 1000000) % 10;
vfo->freq[3] = (hz / 100000) % 10;
vfo->freq[4] = (hz / 10000) % 10;
vfo->freq[5] = (hz / 1000) % 10;
vfo->freq[6] = (hz / 100) % 10;
vfo->freq[7] = (hz / 10) % 10;
vfo->offset[0] = (offset / 100000000) % 10;
vfo->offset[1] = (offset / 10000000) % 10;
vfo->offset[2] = (offset / 1000000) % 10;
vfo->offset[3] = (offset / 100000) % 10;
vfo->rxtone = rxtone;
vfo->txtone = txtone;
vfo->lowpower = lowpower;
vfo->narrow = !wide;
vfo->step = step;
vfo->scode = scode;
}
static void print_offset(FILE *out, int delta)
{
if (delta == 0) {
fprintf(out, " 0 ");
} else {
if (delta > 0) {
fprintf(out, "+");
} else {
fprintf(out, "-");
delta = -delta;
}
if (delta % 1000000 == 0)
fprintf(out, "%-7u", delta / 1000000);
else
fprintf(out, "%-7.3f", delta / 1000000.0);
}
}
static void print_squelch(FILE *out, int ctcs, int dcs)
{
if (ctcs)
fprintf(out, "%5.1f", ctcs / 10.0);
else if (dcs > 0)
fprintf(out, "D%03dN", dcs);
else if (dcs < 0)
fprintf(out, "D%03dI", -dcs);
else
fprintf(out, " - ");
}
static void print_vfo(FILE *out, char name, int band, int hz, int offset, int rx_ctcs, int tx_ctcs,
int rx_dcs, int tx_dcs, int lowpower, int wide, int step, int scode)
{
fprintf(out, " %c %3s %8.4f ", name, band ? "UHF" : "VHF", hz / 1000000.0);
print_offset(out, offset);
fprintf(out, " ");
print_squelch(out, rx_ctcs, rx_dcs);
fprintf(out, " ");
print_squelch(out, tx_ctcs, tx_dcs);
char sgroup[8];
if (scode == 0)
strcpy(sgroup, "-");
else
sprintf(sgroup, "%u", scode);
fprintf(out, " %-4s %-4s %-6s %s\n", STEP_NAME[step], lowpower ? "Low" : "High",
wide ? "Wide" : "Narrow", sgroup);
}
//
// Generic settings.
//
typedef struct {
uint8_t squelch; // Carrier Squelch Level
uint8_t step;
uint8_t _u1;
uint8_t save; // Battery Saver
uint8_t vox; // VOX Level
uint8_t _u2;
uint8_t abr; // Backlight Timeout
uint8_t tdr; // Dual Watch
uint8_t beep; // Beep
uint8_t timeout; // Timeout Timer
uint8_t _u3[4];
uint8_t voice; // Voice
uint8_t _u4;
uint8_t dtmfst; // DTMF Sidetone
uint8_t _u5;
uint8_t screv; // Scan Resume
uint8_t pttid;
uint8_t pttlt;
uint8_t mdfa; // Display Mode (A)
uint8_t mdfb; // Display Mode (B)
uint8_t bcl; // Busy Channel Lockout
uint8_t autolk; // Automatic Key Lock
uint8_t sftd;
uint8_t _u6[3];
uint8_t wtled; // Standby LED Color
uint8_t rxled; // RX LED Color
uint8_t txled; // TX LED Color
uint8_t almod; // Alarm Mode
uint8_t band;
uint8_t tdrab; // Dual Watch Priority
uint8_t ste; // Squelch Tail Eliminate (HT to HT)
uint8_t rpste; // Squelch Tail Eliminate (repeater)
uint8_t rptrl; // STE Repeater Delay
uint8_t ponmsg; // Power-On Message
uint8_t roger; // Roger Beep
} settings_t;
//
// Transient modes.
//
typedef struct {
uint8_t displayab : 1; // Display
uint8_t _u1 : 2;
uint8_t fmradio : 1; // Broadcast FM Radio
uint8_t alarm : 1;
uint8_t _u2 : 1;
uint8_t reset : 1; // RESET Menu
uint8_t menu : 1; // All Menus
uint8_t _u3;
uint8_t workmode; // VFO/MR Mode
uint8_t keylock; // Keypad Lock
} extra_settings_t;
//
// Print full information about the device configuration.
//
static void print_config(FILE *out, int verbose, int is_aged)
{
int i;
// Power-on message.
if (verbose) {
fprintf(out, "\n");
fprintf(out, "# Display this message on power-on.\n");
fprintf(out, "# 14 characters split into two lines of 7 symbols each.\n");
}
fprintf(out, "Message: %.14s\n", &radio_mem[0x1EC0 + 0x20]);
// Print memory channels.
fprintf(out, "\n");
if (verbose) {
fprintf(out, "# Table of preprogrammed channels.\n");
fprintf(out, "# 1) Channel number: 0-%d\n", NCHAN - 1);
fprintf(out, "# 2) Name: up to 7 characters, no spaces\n");
fprintf(out, "# 3) Receive frequency in MHz\n");
fprintf(out, "# 4) Offset of transmit frequency in MHz, or '-' to disable transmit\n");
fprintf(out, "# 5) Squelch tone for receive, or '-' to disable\n");
fprintf(out, "# 6) Squelch tone for transmit, or '-' to disable\n");
fprintf(out, "# 7) Transmit power: Low, High\n");
fprintf(out, "# 8) Modulation width: Wide, Narrow\n");
fprintf(out, "# 9) Add this channel to scan list: -, +\n");
fprintf(out, "# 10) Busy channel lockout: -, +\n");
fprintf(out, "# 11) Last (6-th) character of ANI code, or '-'\n");
fprintf(out, "# 12) Transmit PTT ID (ANI code) on this channel: -, +\n");
fprintf(out, "#\n");
}
fprintf(
out,
"Channel Name Receive TxOffset R-Squel T-Squel Power FM Scan BCL Scode PTTID\n");
for (i = 0; i < NCHAN; i++) {
int rx_hz, tx_hz, rx_ctcs, tx_ctcs, rx_dcs, tx_dcs;
int lowpower, wide, scan, bcl, pttid, scode;
char name[17];
decode_channel(i, name, &rx_hz, &tx_hz, &rx_ctcs, &tx_ctcs, &rx_dcs, &tx_dcs, &lowpower,
&wide, &scan, &bcl, &pttid, &scode);
if (rx_hz == 0) {
// Channel is disabled
continue;
}
fprintf(out, "%5d %-7s %8.4f ", i, name[0] ? name : "-", rx_hz / 1000000.0);
if (is_valid_frequency(tx_hz / 1000000.0)) {
print_offset(out, tx_hz - rx_hz);
} else {
fprintf(out, " - ");
}
fprintf(out, " ");
print_squelch(out, rx_ctcs, rx_dcs);
fprintf(out, " ");
print_squelch(out, tx_ctcs, tx_dcs);
char sgroup[8];
if (scode == 0)
strcpy(sgroup, "-");
else
sprintf(sgroup, "%u", scode);
fprintf(out, " %-4s %-6s %-4s %-3s %-5s %s\n", lowpower ? "Low" : "High",
wide ? "Wide" : "Narrow", scan ? "+" : "-", bcl ? "+" : "-", sgroup,
PTTID_NAME[pttid]);
}
if (verbose)
print_squelch_tones(out, 0);
// Print frequency mode VFO settings.
int band, hz, offset, rx_ctcs, tx_ctcs, rx_dcs, tx_dcs;
int lowpower, wide, step, scode;
fprintf(out, "\n");
if (verbose) {
fprintf(out, "# Table of VFO settings.\n");
fprintf(out, "# 1) VFO index: A, B\n");
fprintf(out, "# 2) Band: VHF, UHF\n");
fprintf(out, "# 3) Receive frequency in MHz\n");
fprintf(out, "# 4) Offset of transmit frequency in MHz\n");
fprintf(out, "# 5) Squelch tone for receive, or '-' to disable\n");
fprintf(out, "# 6) Squelch tone for transmit, or '-' to disable\n");
fprintf(out, "# 7) Frequency step in kHz: 2.5, 5.0, 6.25, 10.0, 12.5, 20.0, 25.0, 50.0\n");
fprintf(out, "# 8) Transmit power: Low, High\n");
fprintf(out, "# 9) Modulation width: Wide, Narrow\n");
fprintf(out, "# 10) Last (6-th) character of ANI code, or '-'\n");
fprintf(out, "#\n");
}
uv5r_decode_vfo(0, &band, &hz, &offset, &rx_ctcs, &tx_ctcs, &rx_dcs, &tx_dcs, &lowpower, &wide,
&step, &scode);
fprintf(out, "VFO Band Receive TxOffset R-Squel T-Squel Step Power FM Scode\n");
print_vfo(out, 'A', band, hz, offset, rx_ctcs, tx_ctcs, rx_dcs, tx_dcs, lowpower, wide, step,
scode);
uv5r_decode_vfo(1, &band, &hz, &offset, &rx_ctcs, &tx_ctcs, &rx_dcs, &tx_dcs, &lowpower, &wide,
&step, &scode);
print_vfo(out, 'B', band, hz, offset, rx_ctcs, tx_ctcs, rx_dcs, tx_dcs, lowpower, wide, step,
scode);
if (!is_aged) {
// Print band limits.
int vhf_enable, vhf_lower, vhf_upper, uhf_enable, uhf_lower, uhf_upper;
decode_limits('V', &vhf_enable, &vhf_lower, &vhf_upper);
decode_limits('U', &uhf_enable, &uhf_lower, &uhf_upper);
fprintf(out, "\n");
if (verbose) {
fprintf(out, "# Table of band limits.\n");
fprintf(out, "# 1) Band: VHF, UHF\n");
fprintf(out, "# 2) Lower frequency in MHz\n");
fprintf(out, "# 3) Upper frequency in MHz\n");
fprintf(out, "# 4) Enable flag: -, +\n");
fprintf(out, "#\n");
}
fprintf(out, "Limit Lower Upper Enable\n");
fprintf(out, " VHF %4d %4d %s\n", vhf_lower, vhf_upper, vhf_enable ? "+" : "-");
fprintf(out, " UHF %4d %4d %s\n", uhf_lower, uhf_upper, uhf_enable ? "+" : "-");
}
// Get atomatic number identifier.
char ani[5];
fetch_ani(ani);
// Print other settings.
settings_t *mode = (settings_t *)&radio_mem[0x0E20];
fprintf(out, "\n");
if (verbose) {
fprintf(out, "# Mute the speaker when a received signal is below this level.\n");
fprintf(out, "# Options: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9\n");
}
fprintf(out, "Squelch Level: %u\n", mode->squelch);
if (verbose)
print_options(out, SAVER_NAME, 5, "Decrease the amount of power used when idle.");
fprintf(out, "Battery Saver: %s\n", SAVER_NAME[mode->save & 7]);
if (verbose)
print_options(out, VOX_NAME, 11, "Microphone sensitivity for VOX control.");
fprintf(out, "VOX Level: %s\n", VOX_NAME[mode->vox & 15]);
if (verbose)
print_options(out, ABR_NAME, 6, "Number of seconds for display backlight.");
fprintf(out, "Backlight Timeout: %s\n", ABR_NAME[mode->abr & 7]);
if (verbose)
print_options(out, OFF_ON, 2,
"Automatically switch A/B when signal is received on another frequency.");
fprintf(out, "Dual Watch: %s\n", mode->tdr ? "On" : "Off");
if (verbose)
print_options(out, OFF_ON, 2, "Keypad beep sound.");
fprintf(out, "Keypad Beep: %s\n", mode->beep ? "On" : "Off");
if (verbose) {
fprintf(out, "\n# Stop tramsmission after specified number of seconds.\n");
fprintf(out, "# Options: 15, 30, 45, 60, ... 585, 600\n");
}
fprintf(out, "TX Timer: %u\n", (mode->timeout + 1) * 15);
if (verbose)
print_options(out, VOICE_NAME, 3, "Enable voice messages.");
fprintf(out, "Voice Prompt: %s\n", VOICE_NAME[mode->voice & 3]);
if (verbose) {
fprintf(out, "\n# Automatic number identification: first 5 characters of\n");
fprintf(out, "# PTT ID code, which is transmitted on PTT button press and/or release.\n");
fprintf(out, "# Last, 6-th character of ANI code is programmed individually\n");
fprintf(out, "# for every channel (see Scode above).\n");
fprintf(out, "# Characters allowed: 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
}
fprintf(out, "ANI Code: %c%c%c%c%c\n", ani[0], ani[1], ani[2], ani[3], ani[4]);
if (verbose)
print_options(out, DTMF_SIDETONE_NAME, 4,
"Play DTMF tones when keycode or PTT ID is transmitted.");
fprintf(out, "DTMF Sidetone: %s\n", DTMF_SIDETONE_NAME[mode->dtmfst & 3]);
if (verbose) {
fprintf(out, "\n# Method of resuming the scan after stop on active channel.\n");
fprintf(out, "# TO - resume after a timeout.\n");
fprintf(out, "# CO - resume after a carrier dropped off.\n");
fprintf(out, "# SE - search and stop on next active frequency.\n");
}
fprintf(out, "Scan Resume: %s\n", SCAN_RESUME_NAME[mode->screv & 3]);
if (verbose)
print_options(out, DISPLAY_MODE_NAME, 3, "What information to display for channel A.");
fprintf(out, "Display Mode A: %s\n", DISPLAY_MODE_NAME[mode->mdfa & 3]);
if (verbose)
print_options(out, DISPLAY_MODE_NAME, 3, "What information to display for channel B.");
fprintf(out, "Display Mode B: %s\n", DISPLAY_MODE_NAME[mode->mdfb & 3]);
if (verbose)
print_options(out, OFF_ON, 2, "Prevent transmission when a signal is received.");
fprintf(out, "Busy Channel Lockout: %s\n", mode->bcl ? "On" : "Off");
if (verbose)
print_options(out, OFF_ON, 2, "Lock keypad automatically.");
fprintf(out, "Auto Key Lock: %s\n", mode->autolk ? "On" : "Off");
if (verbose)
print_options(out, OFF_ON, 2, "Color of display backlight when idle.");
fprintf(out, "Standby LED Color: %s\n", COLOR_NAME[mode->wtled & 3]);
if (verbose)
print_options(out, OFF_ON, 2, "Color of display backlight when signal is received.");
fprintf(out, "RX LED Color: %s\n", COLOR_NAME[mode->rxled & 3]);
if (verbose)
print_options(out, OFF_ON, 2, "Color of display backlight when transmitting.");
fprintf(out, "TX LED Color: %s\n", COLOR_NAME[mode->txled & 3]);
if (verbose) {
fprintf(out, "\n# When alarm button is pressed:\n");
fprintf(out, "# Site - play local alarm sound, no transmit.\n");
fprintf(out, "# Tone - transmit an intermittent sound to remote station.\n");
fprintf(out, "# Code - transmit a DTMF code (PTT ID) to remote station.\n");
}
fprintf(out, "Alarm Mode: %s\n", ALARM_NAME[mode->almod & 3]);
if (verbose)
print_options(out, OFF_ON, 2,
"Reduce the squelch tail when communicating with simplex station.");
fprintf(out, "Squelch Tail Eliminate: %s\n", mode->ste ? "On" : "Off");
if (verbose)
print_options(out, OFF_ON, 2, "Reduce the squelch tail when communicating via repeater.");
fprintf(out, "Squelch Tail Eliminate for Repeater: %s\n", RPSTE_NAME[mode->rpste & 15]);
if (verbose)
print_options(out, OFF_ON, 2, "Delay the squelch tail for repeater.");
fprintf(out, "Squelch Tail Repeater Delay: %s\n", RPSTE_NAME[mode->rptrl & 15]);
if (verbose)
print_options(out, OFF_ON, 2, "Display the power-on message (see above).");
fprintf(out, "Power-On Message: %s\n", mode->ponmsg ? "On" : "Off");
if (verbose)
print_options(out, OFF_ON, 2, "Transmit 'roger' tone when PTT released.");
fprintf(out, "Roger Beep: %s\n", mode->roger ? "On" : "Off");
}
//
// Print full information about the device configuration.
//
static void uv5r_print_config(FILE *out, int verbose)
{
print_config(out, verbose, 0);
}
static void aged_print_config(FILE *out, int verbose)
{
print_config(out, verbose, 1);
}
//
// Read memory image from the binary file.
//
static void uv5r_read_image(FILE *img, unsigned char *ident)
{
if (fread(ident, 1, 8, img) != 8) {
fprintf(stderr, "Error reading image header.\n");
exit(-1);
}
if (fread(&radio_mem[0], 1, 0x1800, img) != 0x1800) {
fprintf(stderr, "Error reading image data.\n");
exit(-1);
}
if (fread(&radio_mem[0x1EC0], 1, 0x2000 - 0x1EC0, img) != 0x2000 - 0x1EC0) {
fprintf(stderr, "Error reading image footer.\n");
exit(-1);
}
}
static void aged_read_image(FILE *img, unsigned char *ident)
{
if (fread(ident, 1, 8, img) != 8) {
fprintf(stderr, "Error reading image header.\n");
exit(-1);
}
if (fread(&radio_mem[0], 1, 0x1800, img) != 0x1800) {