-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathuvk5.py
2032 lines (1705 loc) · 66.8 KB
/
uvk5.py
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
# Quansheng UV-K5 driver (c) 2023 Jacek Lipkowski <[email protected]>
#
# based on template.py Copyright 2012 Dan Smith <[email protected]>
#
#
# This is a preliminary version of a driver for the UV-K5
# It is based on my reverse engineering effort described here:
# https://github.com/sq5bpf/uvk5-reverse-engineering
#
# Warning: this driver is experimental, it may brick your radio,
# eat your lunch and mess up your configuration.
#
#
# 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, see <http://www.gnu.org/licenses/>.
import struct
import logging
from chirp import chirp_common, directory, bitwise, memmap, errors, util
from chirp.settings import RadioSetting, RadioSettingGroup, \
RadioSettingValueBoolean, RadioSettingValueList, \
RadioSettingValueInteger, RadioSettingValueString, \
RadioSettings
LOG = logging.getLogger(__name__)
# Show the obfuscated version of commands. Not needed normally, but
# might be useful for someone who is debugging a similar radio
DEBUG_SHOW_OBFUSCATED_COMMANDS = False
# Show the memory being written/received. Not needed normally, because
# this is the same information as in the packet hexdumps, but
# might be useful for someone debugging some obscure memory issue
DEBUG_SHOW_MEMORY_ACTIONS = False
# TODO: remove the driver version when it's in mainline chirp
DRIVER_VERSION = "Quansheng UV-K5 driver v20230626 (c) Jacek Lipkowski SQ5BPF"
MEM_FORMAT = """
#seekto 0x0000;
struct {
ul32 freq;
ul32 offset;
u8 rxcode;
u8 txcode;
u8 unknown1:2,
txcodeflag:2,
unknown2:2,
rxcodeflag:2;
//u8 flags1;
u8 flags1_unknown7:1,
flags1_unknown6:1,
flags1_unknown5:1,
enable_am:1,
flags1_unknown3:1,
is_in_scanlist:1,
shift:2;
//u8 flags2;
u8 flags2_unknown7:1,
flags2_unknown6:1,
flags2_unknown5:1,
bclo:1,
txpower:2,
bandwidth:1,
freq_reverse:1;
//u8 dtmf_flags;
u8 dtmf_flags_unknown7:1,
dtmf_flags_unknown6:1,
dtmf_flags_unknown5:1,
dtmf_flags_unknown4:1,
dtmf_flags_unknown3:1,
dtmf_pttid:2,
dtmf_decode:1;
u8 step;
u8 scrambler;
} channel[214];
#seekto 0xd60;
struct {
u8 is_scanlist1:1,
is_scanlist2:1,
unknown1:1,
unknown2:1,
is_free:1,
band:3;
} channel_attributes[200];
#seekto 0xe40;
ul16 fmfreq[20];
#seekto 0xe70;
u8 call_channel;
u8 squelch;
u8 max_talk_time;
u8 noaa_autoscan;
u8 key_lock;
u8 vox_switch;
u8 vox_level;
u8 mic_gain;
u8 unknown3;
u8 channel_display_mode;
u8 crossband;
u8 battery_save;
u8 dual_watch;
u8 backlight_auto_mode;
u8 tail_note_elimination;
u8 vfo_open;
#seekto 0xe90;
u8 beep_control;
u8 key1_shortpress_action;
u8 key1_longpress_action;
u8 key2_shortpress_action;
u8 key2_longpress_action;
u8 scan_resume_mode;
u8 auto_keypad_lock;
u8 power_on_dispmode;
u8 password[4];
#seekto 0xea0;
u8 keypad_tone;
u8 language;
#seekto 0xea8;
u8 alarm_mode;
u8 reminding_of_end_talk;
u8 repeater_tail_elimination;
#seekto 0xeb0;
char logo_line1[16];
char logo_line2[16];
#seekto 0xed0;
struct {
u8 side_tone;
char separate_code;
char group_call_code;
u8 decode_response;
u8 auto_reset_time;
u8 preload_time;
u8 first_code_persist_time;
u8 hash_persist_time;
u8 code_persist_time;
u8 code_interval_time;
u8 permit_remote_kill;
} dtmf_settings;
#seekto 0xee0;
struct {
char dtmf_local_code[3];
char unused1[5];
char kill_code[5];
char unused2[3];
char revive_code[5];
char unused3[3];
char dtmf_up_code[16];
char dtmf_down_code[16];
} dtmf_settings_numbers;
#seekto 0xf18;
u8 scanlist_default;
u8 scanlist1_priority_scan;
u8 scanlist1_priority_ch1;
u8 scanlist1_priority_ch2;
u8 scanlist2_priority_scan;
u8 scanlist2_priority_ch1;
u8 scanlist2_priority_ch2;
u8 scanlist_unknown_0xff;
#seekto 0xf40;
u8 int_flock;
u8 int_350tx;
u8 int_unknown1;
u8 int_200tx;
u8 int_500tx;
u8 int_350en;
u8 int_scren;
#seekto 0xf50;
struct {
char name[16];
} channelname[200];
#seekto 0x1c00;
struct {
char name[8];
char number[3];
char unused_00[5];
} dtmfcontact[16];
"""
# bits that we will save from the channel structure (mostly unknown)
SAVE_MASK_0A = 0b11001100
SAVE_MASK_0B = 0b11101100
SAVE_MASK_0C = 0b11100000
SAVE_MASK_0D = 0b11111000
SAVE_MASK_0E = 0b11110001
SAVE_MASK_0F = 0b11110000
# flags1
FLAGS1_OFFSET_NONE = 0b00
FLAGS1_OFFSET_MINUS = 0b10
FLAGS1_OFFSET_PLUS = 0b01
POWER_HIGH = 0b10
POWER_MEDIUM = 0b01
POWER_LOW = 0b00
# dtmf_flags
PTTID_LIST = ["off", "BOT", "EOT", "BOTH"]
# power
UVK5_POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=1.50),
chirp_common.PowerLevel("Med", watts=3.00),
chirp_common.PowerLevel("High", watts=5.00),
]
# scrambler
SCRAMBLER_LIST = ["off", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
# channel display mode
CHANNELDISP_LIST = ["Frequency", "Channel No", "Channel Name"]
# battery save
BATSAVE_LIST = ["OFF", "1:1", "1:2", "1:3", "1:4"]
# Backlight auto mode
BACKLIGHT_LIST = ["Off", "1s", "2s", "3s", "4s", "5s"]
# Crossband receiving/transmitting
CROSSBAND_LIST = ["Off", "Band A", "Band B"]
DUALWATCH_LIST = CROSSBAND_LIST
# steps
STEPS = [2.5, 5.0, 6.25, 10.0, 12.5, 25.0, 8.33]
# ctcss/dcs codes
TMODES = ["", "Tone", "DTCS", "DTCS"]
TONE_NONE = 0
TONE_CTCSS = 1
TONE_DCS = 2
TONE_RDCS = 3
CTCSS_TONES = [
67.0, 69.3, 71.9, 74.4, 77.0, 79.7, 82.5, 85.4,
88.5, 91.5, 94.8, 97.4, 100.0, 103.5, 107.2, 110.9,
114.8, 118.8, 123.0, 127.3, 131.8, 136.5, 141.3, 146.2,
151.4, 156.7, 159.8, 162.2, 165.5, 167.9, 171.3, 173.8,
177.3, 179.9, 183.5, 186.2, 189.9, 192.8, 196.6, 199.5,
203.5, 206.5, 210.7, 218.1, 225.7, 229.1, 233.6, 241.8,
250.3, 254.1
]
# lifted from ft4.py
DTCS_CODES = [
23, 25, 26, 31, 32, 36, 43, 47, 51, 53, 54,
65, 71, 72, 73, 74, 114, 115, 116, 122, 125, 131,
132, 134, 143, 145, 152, 155, 156, 162, 165, 172, 174,
205, 212, 223, 225, 226, 243, 244, 245, 246, 251, 252,
255, 261, 263, 265, 266, 271, 274, 306, 311, 315, 325,
331, 332, 343, 346, 351, 356, 364, 365, 371, 411, 412,
413, 423, 431, 432, 445, 446, 452, 454, 455, 462, 464,
465, 466, 503, 506, 516, 523, 526, 532, 546, 565, 606,
612, 624, 627, 631, 632, 654, 662, 664, 703, 712, 723,
731, 732, 734, 743, 754
]
FLOCK_LIST = ["Off", "FCC", "CE", "GB", "430", "438"]
SCANRESUME_LIST = ["TO: Resume after 5 seconds",
"CO: Resume after signal dissapears",
"SE: Stop scanning after receiving a signal"]
WELCOME_LIST = ["Full Screen", "Welcome Info", "Voltage"]
KEYPADTONE_LIST = ["Off", "Chinese", "English"]
LANGUAGE_LIST = ["Chinese", "English"]
ALARMMODE_LIST = ["SITE", "TONE"]
REMENDOFTALK_LIST = ["Off", "ROGER", "MDC"]
RTE_LIST = ["Off", "100ms", "200ms", "300ms", "400ms",
"500ms", "600ms", "700ms", "800ms", "900ms"]
MEM_SIZE = 0x2000 # size of all memory
PROG_SIZE = 0x1d00 # size of the memory that we will write
MEM_BLOCK = 0x80 # largest block of memory that we can reliably write
# fm radio supported frequencies
FMMIN = 76.0
FMMAX = 108.0
# bands supported by the UV-K5
BANDS = {
0: [50.0, 76.0],
1: [108.0, 135.9999],
2: [136.0, 199.9990],
3: [200.0, 299.9999],
4: [350.0, 399.9999],
5: [400.0, 469.9999],
6: [470.0, 600.0]
}
# for radios with modified firmware:
BANDS_NOLIMITS = {
0: [18.0, 76.0],
1: [108.0, 135.9999],
2: [136.0, 199.9990],
3: [200.0, 299.9999],
4: [350.0, 399.9999],
5: [400.0, 469.9999],
6: [470.0, 1300.0]
}
SPECIALS = {
"F1(50M-76M)A": 200,
"F1(50M-76M)B": 201,
"F2(108M-136M)A": 202,
"F2(108M-136M)B": 203,
"F3(136M-174M)A": 204,
"F3(136M-174M)B": 205,
"F4(174M-350M)A": 206,
"F4(174M-350M)B": 207,
"F5(350M-400M)A": 208,
"F5(350M-400M)B": 209,
"F6(400M-470M)A": 210,
"F6(400M-470M)B": 211,
"F7(470M-600M)A": 212,
"F7(470M-600M)B": 213
}
VFO_CHANNEL_NAMES = ["F1(50M-76M)A", "F1(50M-76M)B",
"F2(108M-136M)A", "F2(108M-136M)B",
"F3(136M-174M)A", "F3(136M-174M)B",
"F4(174M-350M)A", "F4(174M-350M)B",
"F5(350M-400M)A", "F5(350M-400M)B",
"F6(400M-470M)A", "F6(400M-470M)B",
"F7(470M-600M)A", "F7(470M-600M)B"]
SCANLIST_LIST = ["None", "1", "2", "1+2"]
DTMF_CHARS = "0123456789ABCD*# "
DTMF_CHARS_ID = "0123456789ABCDabcd"
DTMF_CHARS_KILL = "0123456789ABCDabcd"
DTMF_CHARS_UPDOWN = "0123456789ABCDabcd#* "
DTMF_CODE_CHARS = "ABCD*# "
DTMF_DECODE_RESPONSE_LIST = ["None", "Ring", "Reply", "Both"]
KEYACTIONS_LIST = ["None", "Flashlight on/off", "Power select",
"Monitor", "Scan on/off", "VOX on/off",
"Alarm on/off", "FM radio on/off", "Transmit 1750Hz"]
# the communication is obfuscated using this fine mechanism
def xorarr(data: bytes):
tbl = [22, 108, 20, 230, 46, 145, 13, 64, 33, 53, 213, 64, 19, 3, 233, 128]
x = b""
r = 0
for byte in data:
x += bytes([byte ^ tbl[r]])
r = (r+1) % len(tbl)
return x
# if this crc was used for communication to AND from the radio, then it
# would be a measure to increase reliability.
# but it's only used towards the radio, so it's for further obfuscation
def calculate_crc16_xmodem(data: bytes):
poly = 0x1021
crc = 0x0
for byte in data:
crc = crc ^ (byte << 8)
for i in range(8):
crc = crc << 1
if (crc & 0x10000):
crc = (crc ^ poly) & 0xFFFF
return crc & 0xFFFF
def _send_command(serport, data: bytes):
"""Send a command to UV-K5 radio"""
LOG.debug("Sending command (unobfuscated) len=0x%4.4x:\n%s" %
(len(data), util.hexprint(data)))
crc = calculate_crc16_xmodem(data)
data2 = data + struct.pack("<H", crc)
command = struct.pack(">HBB", 0xabcd, len(data), 0) + \
xorarr(data2) + \
struct.pack(">H", 0xdcba)
if DEBUG_SHOW_OBFUSCATED_COMMANDS:
LOG.debug("Sending command (obfuscated):\n%s" % util.hexprint(command))
try:
result = serport.write(command)
except Exception:
raise errors.RadioError("Error writing data to radio")
return result
def _receive_reply(serport):
header = serport.read(4)
if len(header) != 4:
LOG.warning("Header short read: [%s] len=%i" %
(util.hexprint(header), len(header)))
raise errors.RadioError("Header short read")
if header[0] != 0xAB or header[1] != 0xCD or header[3] != 0x00:
LOG.warning("Bad response header: %s len=%i" %
(util.hexprint(header), len(header)))
raise errors.RadioError("Bad response header")
cmd = serport.read(int(header[2]))
if len(cmd) != int(header[2]):
LOG.warning("Body short read: [%s] len=%i" %
(util.hexprint(cmd), len(cmd)))
raise errors.RadioError("Command body short read")
footer = serport.read(4)
if len(footer) != 4:
LOG.warning("Footer short read: [%s] len=%i" %
(util.hexprint(footer), len(footer)))
raise errors.RadioError("Footer short read")
if footer[2] != 0xDC or footer[3] != 0xBA:
LOG.debug(
"Reply before bad response footer (obfuscated)"
"len=0x%4.4x:\n%s" % (len(cmd), util.hexprint(cmd)))
LOG.warning("Bad response footer: %s len=%i" %
(util.hexprint(footer), len(footer)))
raise errors.RadioError("Bad response footer")
if DEBUG_SHOW_OBFUSCATED_COMMANDS:
LOG.debug("Received reply (obfuscated) len=0x%4.4x:\n%s" %
(len(cmd), util.hexprint(cmd)))
cmd2 = xorarr(cmd)
LOG.debug("Received reply (unobfuscated) len=0x%4.4x:\n%s" %
(len(cmd2), util.hexprint(cmd2)))
return cmd2
def _getstring(data: bytes, begin, maxlen):
tmplen = min(maxlen+1, len(data))
s = [data[i] for i in range(begin, tmplen)]
for key, val in enumerate(s):
if val < ord(' ') or val > ord('~'):
break
return ''.join(chr(x) for x in s[0:key])
def _sayhello(serport):
hellopacket = b"\x14\x05\x04\x00\x6a\x39\x57\x64"
tries = 5
while True:
LOG.debug("Sending hello packet")
_send_command(serport, hellopacket)
o = _receive_reply(serport)
if (o):
break
tries -= 1
if tries == 0:
LOG.warning("Failed to initialise radio")
raise errors.RadioError("Failed to initialize radio")
firmware = _getstring(o, 4, 16)
LOG.info("Found firmware: %s" % firmware)
return firmware
def _readmem(serport, offset, length):
LOG.debug("Sending readmem offset=0x%4.4x len=0x%4.4x" % (offset, length))
readmem = b"\x1b\x05\x08\x00" + \
struct.pack("<HBB", offset, length, 0) + \
b"\x6a\x39\x57\x64"
_send_command(serport, readmem)
o = _receive_reply(serport)
if DEBUG_SHOW_MEMORY_ACTIONS:
LOG.debug("readmem Received data len=0x%4.4x:\n%s" %
(len(o), util.hexprint(o)))
return o[8:]
def _writemem(serport, data, offset):
LOG.debug("Sending writemem offset=0x%4.4x len=0x%4.4x" %
(offset, len(data)))
if DEBUG_SHOW_MEMORY_ACTIONS:
LOG.debug("writemem sent data offset=0x%4.4x len=0x%4.4x:\n%s" %
(offset, len(data), util.hexprint(data)))
dlen = len(data)
writemem = b"\x1d\x05" + \
struct.pack("<BBHBB", dlen+8, 0, offset, dlen, 1) + \
b"\x6a\x39\x57\x64"+data
_send_command(serport, writemem)
o = _receive_reply(serport)
LOG.debug("writemem Received data: %s len=%i" % (util.hexprint(o), len(o)))
if (o[0] == 0x1e
and
o[4] == (offset & 0xff)
and
o[5] == (offset >> 8) & 0xff):
return True
else:
LOG.warning("Bad data from writemem")
raise errors.RadioError("Bad response to writemem")
def _resetradio(serport):
resetpacket = b"\xdd\x05\x00\x00"
_send_command(serport, resetpacket)
def do_download(radio):
serport = radio.pipe
serport.timeout = 0.5
status = chirp_common.Status()
status.cur = 0
status.max = MEM_SIZE
status.msg = "Downloading from radio"
radio.status_fn(status)
eeprom = b""
f = _sayhello(serport)
if f:
radio.FIRMWARE_VERSION = f
else:
return False
addr = 0
while addr < MEM_SIZE:
o = _readmem(serport, addr, MEM_BLOCK)
status.cur = addr
radio.status_fn(status)
if o and len(o) == MEM_BLOCK:
eeprom += o
addr += MEM_BLOCK
else:
raise errors.RadioError("Memory download incomplete")
return memmap.MemoryMapBytes(eeprom)
def do_upload(radio):
serport = radio.pipe
serport.timeout = 0.5
status = chirp_common.Status()
status.cur = 0
status.max = PROG_SIZE
status.msg = "Uploading to radio"
radio.status_fn(status)
f = _sayhello(serport)
if f:
radio.FIRMWARE_VERSION = f
else:
return False
addr = 0
while addr < PROG_SIZE:
o = radio.get_mmap()[addr:addr+MEM_BLOCK]
_writemem(serport, o, addr)
status.cur = addr
radio.status_fn(status)
if o:
addr += MEM_BLOCK
else:
raise errors.RadioError("Memory upload incomplete")
status.msg = "Uploaded OK"
_resetradio(serport)
return True
def _find_band(self, hz):
mhz = hz/1000000.0
if self.FIRMWARE_NOLIMITS:
B = BANDS_NOLIMITS
else:
B = BANDS
# currently the hacked firmware sets band=1 below 50MHz
if self.FIRMWARE_NOLIMITS and mhz < 50.0:
return 1
for a in B:
if mhz >= B[a][0] and mhz <= B[a][1]:
return a
return False
@directory.register
class UVK5Radio(chirp_common.CloneModeRadio):
"""Quansheng UV-K5"""
VENDOR = "Quansheng"
MODEL = "UV-K5"
BAUD_RATE = 38400
NEEDS_COMPAT_SERIAL = False
FIRMWARE_VERSION = ""
FIRMWARE_NOLIMITS = False
def get_prompts(x=None):
rp = chirp_common.RadioPrompts()
rp.experimental = \
('This is an experimental driver for the Quansheng UV-K5. '
'It may harm your radio, or worse. Use at your own risk.\n\n'
'Before attempting to do any changes please download'
'the memory image from the radio with chirp '
'and keep it. This can be later used to recover the '
'original settings. \n\n'
'some details are not yet implemented')
rp.pre_download = _(
"1. Turn radio on.\n"
"2. Connect cable to mic/spkr connector.\n"
"3. Make sure connector is firmly connected.\n"
"4. Click OK to download image from device.\n\n"
"It will may not work if you turn on the radio "
"with the cable already attached\n")
rp.pre_upload = _(
"1. Turn radio on.\n"
"2. Connect cable to mic/spkr connector.\n"
"3. Make sure connector is firmly connected.\n"
"4. Click OK to upload the image to device.\n\n"
"It will may not work if you turn on the radio "
"with the cable already attached")
return rp
# Return information about this radio's features, including
# how many memories it has, what bands it supports, etc
def get_features(self):
rf = chirp_common.RadioFeatures()
rf.has_bank = False
rf.valid_dtcs_codes = DTCS_CODES
rf.has_rx_dtcs = True
rf.has_ctone = True
rf.has_settings = True
rf.has_comment = False
rf.valid_name_length = 10
rf.valid_power_levels = UVK5_POWER_LEVELS
rf.valid_special_chans = list(SPECIALS.keys())
# hack so we can input any frequency,
# the 0.1 and 0.01 steps don't work unfortunately
rf.valid_tuning_steps = [0.01, 0.1, 1.0] + STEPS
rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
"->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
rf.valid_characters = chirp_common.CHARSET_ASCII
rf.valid_modes = ["FM", "NFM", "AM", "NAM"]
rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
rf.valid_skips = [""]
# This radio supports memories 1-200, 201-214 are the VFO memories
rf.memory_bounds = (1, 200)
# This is what the BK4819 chip supports
# Will leave it in a comment, might be useful someday
# rf.valid_bands = [(18000000, 620000000),
# (840000000, 1300000000)
# ]
rf.valid_bands = []
for a in BANDS:
rf.valid_bands.append(
(int(BANDS[a][0]*1000000), int(BANDS[a][1]*1000000)))
return rf
# Do a download of the radio from the serial port
def sync_in(self):
self._mmap = do_download(self)
self.process_mmap()
# Do an upload of the radio to the serial port
def sync_out(self):
do_upload(self)
# Convert the raw byte array into a memory object structure
def process_mmap(self):
self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
# Return a raw representation of the memory object, which
# is very helpful for development
def get_raw_memory(self, number):
return repr(self._memobj.channel[number-1])
def validate_memory(self, mem):
msgs = super().validate_memory(mem)
# find tx frequency
if mem.duplex == '-':
txfreq = mem.freq - mem.offset
elif mem.duplex == '+':
txfreq = mem.freq + mem.offset
else:
txfreq = mem.freq
# find band
band = _find_band(self, txfreq)
if band is False:
msg = "Transmit frequency %.4fMHz is not supported by this radio" \
% (txfreq/1000000.0)
msgs.append(chirp_common.ValidationWarning(msg))
band = _find_band(self, mem.freq)
if band is False:
msg = "The frequency %.4fMHz is not supported by this radio" \
% (mem.freq/1000000.0)
msgs.append(chirp_common.ValidationWarning(msg))
return msgs
def _set_tone(self, mem, _mem):
((txmode, txtone, txpol),
(rxmode, rxtone, rxpol)) = chirp_common.split_tone_encode(mem)
if txmode == "Tone":
txtoval = CTCSS_TONES.index(txtone)
txmoval = 0b01
elif txmode == "DTCS":
txmoval = txpol == "R" and 0b11 or 0b10
txtoval = DTCS_CODES.index(txtone)
else:
txmoval = 0
txtoval = 0
if rxmode == "Tone":
rxtoval = CTCSS_TONES.index(rxtone)
rxmoval = 0b01
elif rxmode == "DTCS":
rxmoval = rxpol == "R" and 0b11 or 0b10
rxtoval = DTCS_CODES.index(rxtone)
else:
rxmoval = 0
rxtoval = 0
_mem.rxcodeflag = rxmoval
_mem.txcodeflag = txmoval
_mem.unknown1 = 0
_mem.unknown2 = 0
_mem.rxcode = rxtoval
_mem.txcode = txtoval
def _get_tone(self, mem, _mem):
rxtype = _mem.rxcodeflag
txtype = _mem.txcodeflag
rx_tmode = TMODES[rxtype]
tx_tmode = TMODES[txtype]
rx_tone = tx_tone = None
if tx_tmode == "Tone":
if _mem.txcode < len(CTCSS_TONES):
tx_tone = CTCSS_TONES[_mem.txcode]
else:
tx_tone = 0
tx_tmode = ""
elif tx_tmode == "DTCS":
if _mem.txcode < len(DTCS_CODES):
tx_tone = DTCS_CODES[_mem.txcode]
else:
tx_tone = 0
tx_tmode = ""
if rx_tmode == "Tone":
if _mem.rxcode < len(CTCSS_TONES):
rx_tone = CTCSS_TONES[_mem.rxcode]
else:
rx_tone = 0
rx_tmode = ""
elif rx_tmode == "DTCS":
if _mem.rxcode < len(DTCS_CODES):
rx_tone = DTCS_CODES[_mem.rxcode]
else:
rx_tone = 0
rx_tmode = ""
tx_pol = txtype == 0x03 and "R" or "N"
rx_pol = rxtype == 0x03 and "R" or "N"
chirp_common.split_tone_decode(mem, (tx_tmode, tx_tone, tx_pol),
(rx_tmode, rx_tone, rx_pol))
# Extract a high-level memory object from the low-level memory map
# This is called to populate a memory in the UI
def get_memory(self, number2):
mem = chirp_common.Memory()
if isinstance(number2, str):
number = SPECIALS[number2]
mem.extd_number = number2
else:
number = number2 - 1
mem.number = number + 1
_mem = self._memobj.channel[number]
tmpcomment = ""
is_empty = False
# We'll consider any blank (i.e. 0MHz frequency) to be empty
if (_mem.freq == 0xffffffff) or (_mem.freq == 0):
is_empty = True
tmpscn = SCANLIST_LIST[0]
# We'll also look at the channel attributes if a memory has them
if number < 200:
_mem3 = self._memobj.channel_attributes[number]
# free memory bit
if _mem3.is_free > 0:
is_empty = True
# scanlists
if _mem3.is_scanlist1 > 0 and _mem3.is_scanlist2 > 0:
tmpscn = SCANLIST_LIST[3] # "1+2"
elif _mem3.is_scanlist1 > 0:
tmpscn = SCANLIST_LIST[1] # "1"
elif _mem3.is_scanlist2 > 0:
tmpscn = SCANLIST_LIST[2] # "2"
if is_empty:
mem.empty = True
# set some sane defaults:
mem.power = UVK5_POWER_LEVELS[2]
mem.extra = RadioSettingGroup("Extra", "extra")
rs = RadioSetting("bclo", "BCLO", RadioSettingValueBoolean(False))
mem.extra.append(rs)
rs = RadioSetting("frev", "FreqRev",
RadioSettingValueBoolean(False))
mem.extra.append(rs)
rs = RadioSetting("pttid", "PTTID", RadioSettingValueList(
PTTID_LIST, PTTID_LIST[0]))
mem.extra.append(rs)
rs = RadioSetting("dtmfdecode", "DTMF decode",
RadioSettingValueBoolean(False))
mem.extra.append(rs)
rs = RadioSetting("scrambler", "Scrambler", RadioSettingValueList(
SCRAMBLER_LIST, SCRAMBLER_LIST[0]))
mem.extra.append(rs)
rs = RadioSetting("scanlists", "Scanlists", RadioSettingValueList(
SCANLIST_LIST, SCANLIST_LIST[0]))
mem.extra.append(rs)
# actually the step and duplex are overwritten by chirp based on
# bandplan. they are here to document sane defaults for IARU r1
# mem.tuning_step = 25.0
# mem.duplex = "off"
return mem
if number > 199:
mem.name = VFO_CHANNEL_NAMES[number-200]
mem.immutable = ["name", "scanlists"]
else:
_mem2 = self._memobj.channelname[number]
for char in _mem2.name:
if str(char) == "\xFF" or str(char) == "\x00":
break
mem.name += str(char)
mem.name = mem.name.rstrip()
# Convert your low-level frequency to Hertz
mem.freq = int(_mem.freq)*10
mem.offset = int(_mem.offset)*10
if (mem.offset == 0):
mem.duplex = ''
else:
if _mem.shift == FLAGS1_OFFSET_MINUS:
mem.duplex = '-'
elif _mem.shift == FLAGS1_OFFSET_PLUS:
mem.duplex = '+'
else:
mem.duplex = ''
# tone data
self._get_tone(mem, _mem)
# mode
if _mem.enable_am > 0:
if _mem.bandwidth > 0:
mem.mode = "NAM"
else:
mem.mode = "AM"
else:
if _mem.bandwidth > 0:
mem.mode = "NFM"
else:
mem.mode = "FM"
# tuning step
tstep = _mem.step & 0x7
if tstep < len(STEPS):
mem.tuning_step = STEPS[tstep]
else:
mem.tuning_step = 2.5
# power
if _mem.txpower == POWER_HIGH:
mem.power = UVK5_POWER_LEVELS[2]
elif _mem.txpower == POWER_MEDIUM:
mem.power = UVK5_POWER_LEVELS[1]
else:
mem.power = UVK5_POWER_LEVELS[0]
# We'll consider any blank (i.e. 0MHz frequency) to be empty
if (_mem.freq == 0xffffffff) or (_mem.freq == 0):
mem.empty = True
else:
mem.empty = False
mem.extra = RadioSettingGroup("Extra", "extra")
# BCLO
is_bclo = bool(_mem.bclo > 0)
rs = RadioSetting("bclo", "BCLO", RadioSettingValueBoolean(is_bclo))
mem.extra.append(rs)
tmpcomment += "BCLO:"+(is_bclo and "ON" or "off")+" "
# Frequency reverse - whatever that means, don't see it in the manual
is_frev = bool(_mem.freq_reverse > 0)
rs = RadioSetting("frev", "FreqRev", RadioSettingValueBoolean(is_frev))
mem.extra.append(rs)
tmpcomment += "FreqReverse:"+(is_frev and "ON" or "off")+" "
# PTTID
pttid = _mem.dtmf_pttid
rs = RadioSetting("pttid", "PTTID", RadioSettingValueList(
PTTID_LIST, PTTID_LIST[pttid]))
mem.extra.append(rs)
tmpcomment += "PTTid:"+PTTID_LIST[pttid]+" "
# DTMF DECODE
is_dtmf = bool(_mem.dtmf_decode > 0)
rs = RadioSetting("dtmfdecode", "DTMF decode",
RadioSettingValueBoolean(is_dtmf))
mem.extra.append(rs)
tmpcomment += "DTMFdecode:"+(is_dtmf and "ON" or "off")+" "
# Scrambler
if _mem.scrambler & 0x0f < len(SCRAMBLER_LIST):
enc = _mem.scrambler & 0x0f
else:
enc = 0
rs = RadioSetting("scrambler", "Scrambler", RadioSettingValueList(
SCRAMBLER_LIST, SCRAMBLER_LIST[enc]))
mem.extra.append(rs)
tmpcomment += "Scrambler:"+SCRAMBLER_LIST[enc]+" "
rs = RadioSetting("scanlists", "Scanlists", RadioSettingValueList(
SCANLIST_LIST, tmpscn))
mem.extra.append(rs)
return mem
def set_settings(self, settings):
_mem = self._memobj
for element in settings:
if not isinstance(element, RadioSetting):
self.set_settings(element)
continue
# basic settings
# call channel
if element.get_name() == "call_channel":
_mem.call_channel = int(element.value)-1
# squelch
if element.get_name() == "squelch":
_mem.squelch = int(element.value)
# TOT