Project

General

Profile

Feature #9825 » btech-set_kt8900_band_limits_#2.py

for KT8900 and similar radios only - Jim Unroe, 04/11/2022 01:20 AM

 
1
# Copyright 2016-2022:
2
# * Pavel Milanes CO7WT, <pavelmc@gmail.com>
3
# * Jim Unroe KC9HI, <rock.unroe@gmail.com>
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17

    
18
import time
19
import struct
20
import logging
21

    
22
from time import sleep
23
from chirp import chirp_common, directory, memmap
24
from chirp import bitwise, errors, util
25
from chirp.settings import RadioSettingGroup, RadioSetting, \
26
    RadioSettingValueBoolean, RadioSettingValueList, \
27
    RadioSettingValueString, RadioSettingValueInteger, \
28
    RadioSettingValueFloat, RadioSettings, InvalidValueError
29
from textwrap import dedent
30

    
31
LOG = logging.getLogger(__name__)
32

    
33
# A note about the memmory in these radios
34
#
35
# The real memory of these radios extends to 0x4000
36
# On read the factory software only uses up to 0x3200
37
# On write it just uploads the contents up to 0x3100
38
#
39
# The mem beyond 0x3200 holds the ID data
40

    
41
MEM_SIZE = 0x4000
42
BLOCK_SIZE = 0x40
43
TX_BLOCK_SIZE = 0x10
44
ACK_CMD = "\x06"
45
MODES = ["FM", "NFM"]
46
SKIP_VALUES = ["S", ""]
47
TONES = chirp_common.TONES
48
DTCS = sorted(chirp_common.DTCS_CODES + [645])
49

    
50
# lists related to "extra" settings
51
PTTID_LIST = ["OFF", "BOT", "EOT", "BOTH"]
52
PTTIDCODE_LIST = ["%s" % x for x in range(1, 16)]
53
OPTSIG_LIST = ["OFF", "DTMF", "2TONE", "5TONE"]
54
SPMUTE_LIST = ["Tone/DTCS", "Tone/DTCS and Optsig", "Tone/DTCS or Optsig"]
55

    
56
# lists
57
LIST_AB = ["A", "B"]
58
LIST_ABCD = LIST_AB + ["C", "D"]
59
LIST_ANIL = ["3", "4", "5"]
60
LIST_APO = ["Off"] + ["%s minutes" % x for x in range(30, 330, 30)]
61
LIST_COLOR4 = ["Off", "Blue", "Orange", "Purple"]
62
LIST_COLOR8 = ["White", "Red", "Blue", "Green", "Yellow", "Indego",
63
               "Purple", "Gray"]
64
LIST_COLOR9 = ["Black"] + LIST_COLOR8
65
LIST_DTMFST = ["OFF", "Keyboard", "ANI", "Keyboad + ANI"]
66
LIST_EMCTP = ["TX alarm sound", "TX ANI", "Both"]
67
LIST_EMCTPX = ["Off"] + LIST_EMCTP
68
LIST_LANGUA = ["English", "Chinese"]
69
LIST_MDF = ["Frequency", "Channel", "Name"]
70
LIST_OFF1TO9 = ["Off"] + ["%s seconds" % x for x in range(1, 10)]
71
LIST_OFF1TO10 = ["Off"] + ["%s seconds" % x for x in range(1, 11)]
72
LIST_OFF1TO50 = ["Off"] + ["%s seconds" % x for x in range(1, 51)]
73
LIST_PONMSG = ["Full", "Message", "Battery voltage"]
74
LIST_REPM = ["Off", "Carrier", "CTCSS or DCS", "Tone", "DTMF"]
75
LIST_REPS = ["1000 Hz", "1450 Hz", "1750 Hz", "2100Hz"]
76
LIST_RPTDL = ["Off"] + ["%s ms" % x for x in range(1, 11)]
77
LIST_SCMODE = ["Off", "PTT-SC", "MEM-SC", "PON-SC"]
78
LIST_SHIFT = ["Off", "+", "-"]
79
LIST_SKIPTX = ["Off", "Skip 1", "Skip 2"]
80
STEPS = [2.5, 5.0, 6.25, 10.0, 12.5, 25.0]
81
LIST_STEP = [str(x) for x in STEPS]
82
LIST_SYNC = ["Off", "AB", "CD", "AB+CD"]
83
# the first 12 TMR choices common to all color display mobile radios
84
LIST_TMR12 = ["OFF", "M+A", "M+B", "M+C", "M+D", "M+A+B", "M+A+C", "M+A+D",
85
              "M+B+C", "M+B+D", "M+C+D", "M+A+B+C"]
86
# the 16 choice list for color display mobile radios that correctly implement
87
# the full 16 TMR choices
88
LIST_TMR16 = LIST_TMR12 + ["M+A+B+D", "M+A+C+D", "M+B+C+D", "A+B+C+D"]
89
# the 15 choice list for color mobile radios that are missing the M+A+B+D
90
# choice in the TMR menu
91
LIST_TMR15 = LIST_TMR12 + ["M+A+C+D", "M+B+C+D", "A+B+C+D"]
92
LIST_TMRTX = ["Track", "Fixed"]
93
LIST_TOT = ["%s sec" % x for x in range(15, 615, 15)]
94
LIST_TXDISP = ["Power", "Mic Volume"]
95
LIST_TXP = ["High", "Low"]
96
LIST_TXP3 = ["High", "Mid", "Low"]
97
LIST_SCREV = ["TO (timeout)", "CO (carrier operated)", "SE (search)"]
98
LIST_VFOMR = ["Frequency", "Channel"]
99
LIST_VOICE = ["Off"] + LIST_LANGUA
100
LIST_VOX = ["Off"] + ["%s" % x for x in range(1, 11)]
101
LIST_VOXT = ["%s seconds" % x for x in range(0, 21)]
102
LIST_WIDE = ["Wide", "Narrow"]
103

    
104
# lists related to DTMF, 2TONE and 5TONE settings
105
LIST_5TONE_STANDARDS = ["CCIR1", "CCIR2", "PCCIR", "ZVEI1", "ZVEI2", "ZVEI3",
106
                        "PZVEI", "DZVEI", "PDZVEI", "EEA", "EIA", "EURO",
107
                        "CCITT", "NATEL", "MODAT", "none"]
108
LIST_5TONE_STANDARDS_without_none = ["CCIR1", "CCIR2", "PCCIR", "ZVEI1",
109
                                     "ZVEI2", "ZVEI3",
110
                                     "PZVEI", "DZVEI", "PDZVEI", "EEA", "EIA",
111
                                     "EURO", "CCITT", "NATEL", "MODAT"]
112
LIST_5TONE_STANDARD_PERIODS = ["20", "30", "40", "50", "60", "70", "80", "90",
113
                               "100", "110", "120", "130", "140", "150", "160",
114
                               "170", "180", "190", "200"]
115
LIST_5TONE_DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
116
                     "B", "C", "D", "E", "F"]
117
LIST_5TONE_DELAY = ["%s ms" % x for x in range(0, 1010, 10)]
118
LIST_5TONE_RESET = ["%s ms" % x for x in range(100, 8100, 100)]
119
LIST_5TONE_RESET_COLOR = ["%s ms" % x for x in range(100, 20100, 100)]
120
LIST_DTMF_SPEED = ["%s ms" % x for x in range(50, 2010, 10)]
121
LIST_DTMF_DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B",
122
                    "C", "D", "#", "*"]
123
LIST_DTMF_VALUES = [0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
124
                    0x0D, 0x0E, 0x0F, 0x00, 0x0C, 0x0B]
125
LIST_DTMF_SPECIAL_DIGITS = ["*", "#", "A", "B", "C", "D"]
126
LIST_DTMF_SPECIAL_VALUES = [0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00]
127
LIST_DTMF_DELAY = ["%s ms" % x for x in range(100, 4100, 100)]
128
CHARSET_DTMF_DIGITS = "0123456789AaBbCcDd#*"
129
LIST_2TONE_DEC = ["A-B", "A-C", "A-D",
130
                  "B-A", "B-C", "B-D",
131
                  "C-A", "C-B", "C-D",
132
                  "D-A", "D-B", "D-C"]
133
LIST_2TONE_RESPONSE = ["None", "Alert", "Transpond", "Alert+Transpond"]
134

    
135
# This is a general serial timeout for all serial read functions.
136
STIMEOUT = 0.25
137

    
138
# this var controls the verbosity in the debug and by default it's low (False)
139
# make it True and you will to get a very verbose debug.log
140
debug = False
141

    
142
# valid chars on the LCD, Note that " " (space) is stored as "\xFF"
143
VALID_CHARS = chirp_common.CHARSET_ALPHANUMERIC + \
144
    "`{|}!\"#$%&'()*+,-./:;<=>?@[]^_"
145

    
146
GMRS_FREQS1 = [462.5625, 462.5875, 462.6125, 462.6375, 462.6625,
147
               462.6875, 462.7125]
148
GMRS_FREQS2 = [467.5625, 467.5875, 467.6125, 467.6375, 467.6625,
149
               467.6875, 467.7125]
150
GMRS_FREQS3 = [462.5500, 462.5750, 462.6000, 462.6250, 462.6500,
151
               462.6750, 462.7000, 462.7250]
152
GMRS_FREQS = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
153

    
154

    
155
# #### ID strings #####################################################
156

    
157
# BTECH UV2501 pre-production units
158
UV2501pp_fp = "M2C294"
159
# BTECH UV2501 pre-production units 2 + and 1st Gen radios
160
UV2501pp2_fp = "M29204"
161
# B-TECH UV-2501 second generation (2G) radios
162
UV2501G2_fp = "BTG214"
163
# B-TECH UV-2501 third generation (3G) radios
164
UV2501G3_fp = "BTG324"
165

    
166
# B-TECH UV-2501+220 pre-production units
167
UV2501_220pp_fp = "M3C281"
168
# extra block read for the 2501+220 pre-production units
169
# the same for all of this radios so far
170
UV2501_220pp_id = "      280528"
171
# B-TECH UV-2501+220
172
UV2501_220_fp = "M3G201"
173
# new variant, let's call it Generation 2
174
UV2501_220G2_fp = "BTG211"
175
# B-TECH UV-2501+220 third generation (3G)
176
UV2501_220G3_fp = "BTG311"
177

    
178
# B-TECH UV-5001 pre-production units + 1st Gen radios
179
UV5001pp_fp = "V19204"
180
# B-TECH UV-5001 alpha units
181
UV5001alpha_fp = "V28204"
182
# B-TECH UV-5001 second generation (2G) radios
183
UV5001G2_fp = "BTG214"
184
# B-TECH UV-5001 second generation (2G2)
185
UV5001G22_fp = "V2G204"
186
# B-TECH UV-5001 third generation (3G)
187
UV5001G3_fp = "BTG304"
188

    
189
# B-TECH UV-25X2
190
UV25X2_fp = "UC2012"
191

    
192
# B-TECH UV-25X4
193
UV25X4_fp = "UC4014"
194

    
195
# B-TECH UV-50X2
196
UV50X2_fp = "UC2M12"
197

    
198
# B-TECH GMRS-50X1
199
GMRS50X1_fp = "NC1802"
200
GMRS50X1_fp1 = "NC1932"
201

    
202
# special var to know when we found a BTECH Gen 3
203
BTECH3 = [UV2501G3_fp, UV2501_220G3_fp, UV5001G3_fp]
204

    
205

    
206
# WACCOM Mini-8900
207
MINI8900_fp = "M28854"
208

    
209

    
210
# QYT KT-UV980
211
KTUV980_fp = "H28854"
212

    
213
# QYT KT8900
214
KT8900_fp = "M29154"
215
# New generations KT8900
216
KT8900_fp1 = "M2C234"
217
KT8900_fp2 = "M2G1F4"
218
KT8900_fp3 = "M2G2F4"
219
KT8900_fp4 = "M2G304"
220
KT8900_fp5 = "M2G314"
221
KT8900_fp6 = "M2G424"
222
KT8900_fp7 = "M27184"
223
# this radio has an extra ID
224
KT8900_id = "303688"
225
# another extra ID in sep/2021
226
KT8900_id2 = "\x05\x58\x3d\xf0\x10"
227

    
228
# KT8900R
229
KT8900R_fp = "M3G1F4"
230
# Second Generation
231
KT8900R_fp1 = "M3G214"
232
# another model
233
KT8900R_fp2 = "M3C234"
234
# another model G4?
235
KT8900R_fp3 = "M39164"
236
# another model
237
KT8900R_fp4 = "M3G314"
238
# AC3MB: another id
239
KT8900R_fp5 = "M3B064"
240
# this radio has an extra ID
241
KT8900R_id = "280528"
242
# another extra ID in dec/2018
243
KT8900R_id2 = "\x05\x58\x3d\xf0\x10"
244

    
245
# KT7900D (quad band)
246
KT7900D_fp = "VC4004"
247
KT7900D_fp1 = "VC4284"
248
KT7900D_fp2 = "VC4264"
249
KT7900D_fp3 = "VC4114"
250
KT7900D_fp4 = "VC4104"
251
KT7900D_fp5 = "VC4254"
252
KT7900D_fp6 = "VC5264"
253
KT7900D_fp7 = "VC9204"
254

    
255
# QB25 (quad band) - a clone of KT7900D
256
QB25_fp = "QB-25"
257

    
258
# KT8900D (dual band)
259
KT8900D_fp = "VC2002"
260
KT8900D_fp1 = "VC8632"
261
KT8900D_fp2 = "VC3402"
262
KT8900D_fp3 = "VC7062"
263

    
264
# LUITON LT-588UV
265
LT588UV_fp = "V2G1F4"
266
# Added by rstrickoff gen 2 id
267
LT588UV_fp1 = "V2G214"
268

    
269
# QYT KT-8R (quad band ht)
270
KT8R_fp = "MCB264"
271
KT8R_fp1 = "MCB284"
272
KT8R_fp2 = "MC5264"
273

    
274
# QYT KT5800 (dual band)
275
KT5800_fp = "VCB222"
276

    
277
# QYT KT980Plus (dual band)
278
KT980PLUS_fp = "VC2002"
279
KT980PLUS_fp1 = "VC6042"
280

    
281
# Radioddity DB25-G (gmrs)
282
DB25G_fp = "VC6182"
283
DB25G_fp1 = "VC7062"
284

    
285

    
286
# ### MAGICS
287
# for the Waccom Mini-8900
288
MSTRING_MINI8900 = "\x55\xA5\xB5\x45\x55\x45\x4d\x02"
289
# for the B-TECH UV-2501+220 (including pre production ones)
290
MSTRING_220 = "\x55\x20\x15\x12\x12\x01\x4d\x02"
291
# for the QYT KT8900 & R
292
MSTRING_KT8900 = "\x55\x20\x15\x09\x16\x45\x4D\x02"
293
MSTRING_KT8900R = "\x55\x20\x15\x09\x25\x01\x4D\x02"
294
# magic string for all other models
295
MSTRING = "\x55\x20\x15\x09\x20\x45\x4d\x02"
296
# for the QYT KT7900D & KT8900D
297
MSTRING_KT8900D = "\x55\x20\x16\x08\x01\xFF\xDC\x02"
298
# for the BTECH UV-25X2 and UV-50X2
299
MSTRING_UV25X2 = "\x55\x20\x16\x12\x28\xFF\xDC\x02"
300
# for the BTECH UV-25X4
301
MSTRING_UV25X4 = "\x55\x20\x16\x11\x18\xFF\xDC\x02"
302
# for the BTECH GMRS-50X1
303
MSTRING_GMRS50X1 = "\x55\x20\x18\x10\x18\xFF\xDC\x02"
304
# for the QYT KT-8R
305
MSTRING_KT8R = "\x55\x20\x17\x07\x03\xFF\xDC\x02"
306

    
307

    
308
def _clean_buffer(radio):
309
    """Cleaning the read serial buffer, hard timeout to survive an infinite
310
    data stream"""
311

    
312
    # touching the serial timeout to optimize the flushing
313
    # restored at the end to the default value
314
    radio.pipe.timeout = 0.1
315
    dump = "1"
316
    datacount = 0
317

    
318
    try:
319
        while len(dump) > 0:
320
            dump = radio.pipe.read(100)
321
            datacount += len(dump)
322
            # hard limit to survive a infinite serial data stream
323
            # 5 times bigger than a normal rx block (69 bytes)
324
            if datacount > 345:
325
                seriale = "Please check your serial port selection."
326
                raise errors.RadioError(seriale)
327

    
328
        # restore the default serial timeout
329
        radio.pipe.timeout = STIMEOUT
330

    
331
    except Exception:
332
        raise errors.RadioError("Unknown error cleaning the serial buffer")
333

    
334

    
335
def _rawrecv(radio, amount):
336
    """Raw read from the radio device, less intensive way"""
337

    
338
    data = ""
339

    
340
    try:
341
        data = radio.pipe.read(amount)
342

    
343
        # DEBUG
344
        if debug is True:
345
            LOG.debug("<== (%d) bytes:\n\n%s" %
346
                      (len(data), util.hexprint(data)))
347

    
348
        # fail if no data is received
349
        if len(data) == 0:
350
            raise errors.RadioError("No data received from radio")
351

    
352
        # notice on the logs if short
353
        if len(data) < amount:
354
            LOG.warn("Short reading %d bytes from the %d requested." %
355
                     (len(data), amount))
356

    
357
    except:
358
        raise errors.RadioError("Error reading data from radio")
359

    
360
    return data
361

    
362

    
363
def _send(radio, data):
364
    """Send data to the radio device"""
365

    
366
    try:
367
        radio.pipe.write(data)
368

    
369
        # DEBUG
370
        if debug is True:
371
            LOG.debug("==> (%d) bytes:\n\n%s" %
372
                      (len(data), util.hexprint(data)))
373
    except:
374
        raise errors.RadioError("Error sending data to radio")
375

    
376

    
377
def _make_frame(cmd, addr, length, data=""):
378
    """Pack the info in the headder format"""
379
    frame = "\x06" + struct.pack(">BHB", ord(cmd), addr, length)
380
    # add the data if set
381
    if len(data) != 0:
382
        frame += data
383

    
384
    return frame
385

    
386

    
387
def _recv(radio, addr):
388
    """Get data from the radio all at once to lower syscalls load"""
389

    
390
    # Get the full 69 bytes at a time to reduce load
391
    # 1 byte ACK + 4 bytes header + 64 bytes of data (BLOCK_SIZE)
392

    
393
    # get the whole block
394
    block = _rawrecv(radio, BLOCK_SIZE + 5)
395

    
396
    # basic check
397
    if len(block) < (BLOCK_SIZE + 5):
398
        raise errors.RadioError("Short read of the block 0x%04x" % addr)
399

    
400
    # checking for the ack
401
    if block[0] != ACK_CMD:
402
        raise errors.RadioError("Bad ack from radio in block 0x%04x" % addr)
403

    
404
    # header validation
405
    c, a, l = struct.unpack(">BHB", block[1:5])
406
    if a != addr or l != BLOCK_SIZE or c != ord("X"):
407
        LOG.debug("Invalid header for block 0x%04x" % addr)
408
        LOG.debug("CMD: %s  ADDR: %04x  SIZE: %02x" % (c, a, l))
409
        raise errors.RadioError("Invalid header for block 0x%04x:" % addr)
410

    
411
    # return the data
412
    return block[5:]
413

    
414

    
415
def _start_clone_mode(radio, status):
416
    """Put the radio in clone mode and get the ident string, 3 tries"""
417

    
418
    # cleaning the serial buffer
419
    _clean_buffer(radio)
420

    
421
    # prep the data to show in the UI
422
    status.cur = 0
423
    status.msg = "Identifying the radio..."
424
    status.max = 3
425
    radio.status_fn(status)
426

    
427
    try:
428
        for a in range(0, status.max):
429
            # Update the UI
430
            status.cur = a + 1
431
            radio.status_fn(status)
432

    
433
            # send the magic word
434
            _send(radio, radio._magic)
435

    
436
            # Now you get a x06 of ACK if all goes well
437
            ack = radio.pipe.read(1)
438

    
439
            if ack == "\x06":
440
                # DEBUG
441
                LOG.info("Magic ACK received")
442
                status.cur = status.max
443
                radio.status_fn(status)
444

    
445
                return True
446

    
447
        return False
448

    
449
    except errors.RadioError:
450
        raise
451
    except Exception, e:
452
        raise errors.RadioError("Error sending Magic to radio:\n%s" % e)
453

    
454

    
455
def _do_ident(radio, status, upload=False):
456
    """Put the radio in PROGRAM mode & identify it"""
457
    #  set the serial discipline
458
    radio.pipe.baudrate = 9600
459
    radio.pipe.parity = "N"
460

    
461
    # open the radio into program mode
462
    if _start_clone_mode(radio, status) is False:
463
        msg = "Radio did not enter clone mode"
464
        # warning about old versions of QYT KT8900
465
        if radio.MODEL == "KT8900":
466
            msg += ". You may want to try it as a WACCOM MINI-8900, there is a"
467
            msg += " known variant of this radios that is a clone of it."
468
        raise errors.RadioError(msg)
469

    
470
    # lengthen the timeout here as these radios are reseting due to timeout
471
    radio.pipe.timeout = 0.75
472

    
473
    # Ok, get the ident string
474
    ident = _rawrecv(radio, 49)
475

    
476
    # restore the default serial timeout
477
    radio.pipe.timeout = STIMEOUT
478

    
479
    # basic check for the ident
480
    if len(ident) != 49:
481
        raise errors.RadioError("Radio send a short ident block.")
482

    
483
    # check if ident is OK
484
    itis = False
485
    for fp in radio._fileid:
486
        if fp in ident:
487
            # got it!
488
            itis = True
489
            # checking if we are dealing with a Gen 3 BTECH
490
            if radio.VENDOR == "BTECH" and fp in BTECH3:
491
                radio.btech3 = True
492

    
493
            break
494

    
495
    if itis is False:
496
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
497
        raise errors.RadioError("Radio identification failed.")
498

    
499
    # some radios needs a extra read and check for a code on it, this ones
500
    # has the check value in the _id2 var, others simply False
501
    if radio._id2 is not False:
502
        # query & receive the extra ID
503
        _send(radio, _make_frame("S", 0x3DF0, 16))
504
        id2 = _rawrecv(radio, 21)
505

    
506
        # WARNING !!!!!!
507
        # different radios send a response with a different amount of data
508
        # it seems that it's padded with \xff, \x20 and some times with \x00
509
        # we just care about the first 16, our magic string is in there
510
        if len(id2) < 16:
511
            raise errors.RadioError("The extra ID is short, aborting.")
512

    
513
        # ok, the correct string must be in the received data
514
        # the radio._id2 var will be always a list
515
        flag2 = False
516
        for _id2 in radio._id2:
517
            if _id2 in id2:
518
                flag2 = True
519

    
520
        if not flag2:
521
            LOG.debug("Full *BAD* extra ID on the %s is: \n%s" %
522
                      (radio.MODEL, util.hexprint(id2)))
523
            raise errors.RadioError("The extra ID is wrong, aborting.")
524

    
525
        # this radios need a extra request/answer here on the upload
526
        # the amount of data received depends of the radio type
527
        #
528
        # also the first block of TX must no have the ACK at the beginning
529
        # see _upload for this.
530
        if upload is True:
531
            # send an ACK
532
            _send(radio, ACK_CMD)
533

    
534
            # the amount of data depend on the radio, so far we have two radios
535
            # reading two bytes with an ACK at the end and just ONE with just
536
            # one byte (QYT KT8900)
537
            # the JT-6188 appears a clone of the last, but reads TWO bytes.
538
            #
539
            # we will read two bytes with a custom timeout to not penalize the
540
            # users for this.
541
            #
542
            # we just check for a response and last byte being a ACK, that is
543
            # the common stone for all radios (3 so far)
544
            ack = _rawrecv(radio, 2)
545

    
546
            # checking
547
            if len(ack) == 0 or ack[-1:] != ACK_CMD:
548
                raise errors.RadioError("Radio didn't ACK the upload")
549

    
550
    # DEBUG
551
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
552

    
553
    return True
554

    
555

    
556
def _download(radio):
557
    """Get the memory map"""
558

    
559
    # UI progress
560
    status = chirp_common.Status()
561

    
562
    # put radio in program mode and identify it
563
    _do_ident(radio, status)
564

    
565
    # pause here for the radio to catch up
566
    sleep(0.1)
567

    
568
    # the models that doesn't have the extra ID have to make a dummy read here
569
    if radio._id2 is False:
570
        _send(radio, _make_frame("S", 0, BLOCK_SIZE))
571
        discard = _rawrecv(radio, BLOCK_SIZE + 5)
572

    
573
        if debug is True:
574
            LOG.info("Dummy first block read done, got this:\n\n %s",
575
                     util.hexprint(discard))
576

    
577
    # reset the progress bar in the UI
578
    status.max = MEM_SIZE / BLOCK_SIZE
579
    status.msg = "Cloning from radio..."
580
    status.cur = 0
581
    radio.status_fn(status)
582

    
583
    # cleaning the serial buffer
584
    _clean_buffer(radio)
585

    
586
    data = ""
587
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
588
        # sending the read request
589
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
590

    
591
        # read
592
        d = _recv(radio, addr)
593

    
594
        # aggregate the data
595
        data += d
596

    
597
        # UI Update
598
        status.cur = addr / BLOCK_SIZE
599
        status.msg = "Cloning from radio..."
600
        radio.status_fn(status)
601

    
602
    return data
603

    
604

    
605
def _upload(radio):
606
    """Upload procedure"""
607

    
608
    # The UPLOAD mem is restricted to lower than 0x3100,
609
    # so we will overide that here localy
610
    MEM_SIZE = radio.UPLOAD_MEM_SIZE
611

    
612
    # UI progress
613
    status = chirp_common.Status()
614

    
615
    # put radio in program mode and identify it
616
    _do_ident(radio, status, True)
617

    
618
    # get the data to upload to radio
619
    data = radio.get_mmap()
620

    
621
    # Reset the UI progress
622
    status.max = MEM_SIZE / TX_BLOCK_SIZE
623
    status.cur = 0
624
    status.msg = "Cloning to radio..."
625
    radio.status_fn(status)
626

    
627
    # the radios that doesn't have the extra ID 'may' do a dummy write, I found
628
    # that leveraging the bad ACK and NOT doing the dummy write is ok, as the
629
    # dummy write is accepted (it actually writes to the mem!) by the radio.
630

    
631
    # cleaning the serial buffer
632
    _clean_buffer(radio)
633

    
634
    # the fun start here
635
    for addr in range(0x3C90, 0x3C9F, TX_BLOCK_SIZE):  # set band limits for KT-8900 and similar radios only
636
        # getting the block of data to send
637
        d = data[addr:addr + TX_BLOCK_SIZE]
638

    
639
        # build the frame to send
640
        frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
641

    
642
        # first block must not send the ACK at the beginning for the
643
        # ones that has the extra id, since this have to do a extra step
644
        if addr == 0 and radio._id2 is not False:
645
            frame = frame[1:]
646

    
647
        # send the frame
648
        _send(radio, frame)
649

    
650
        # receiving the response
651
        ack = _rawrecv(radio, 1)
652

    
653
        # basic check
654
        if len(ack) != 1:
655
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
656

    
657
        if ack not in "\x06\x05":
658
            raise errors.RadioError("Bad ACK writing block 0x%04x:" % addr)
659

    
660
        # UI Update
661
        status.cur = addr / TX_BLOCK_SIZE
662
        status.msg = "Cloning to radio..."
663
        radio.status_fn(status)
664

    
665

    
666
def model_match(cls, data):
667
    """Match the opened/downloaded image to the correct version"""
668
    rid = data[0x3f70:0x3f76]
669

    
670
    if rid in cls._fileid:
671
        return True
672

    
673
    return False
674

    
675

    
676
def _decode_ranges(low, high):
677
    """Unpack the data in the ranges zones in the memmap and return
678
    a tuple with the integer corresponding to the Mhz it means"""
679
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
680
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
681
    ilow *= 1000000
682
    ihigh *= 1000000
683

    
684
    return (ilow, ihigh)
685

    
686

    
687
def _split(rf, f1, f2):
688
    """Returns False if the two freqs are in the same band (no split)
689
    or True otherwise"""
690

    
691
    # determine if the two freqs are in the same band
692
    for low, high in rf.valid_bands:
693
        if f1 >= low and f1 <= high and \
694
                f2 >= low and f2 <= high:
695
            # if the two freqs are on the same Band this is not a split
696
            return False
697

    
698
    # if you get here is because the freq pairs are split
699
    return True
700

    
701

    
702
class BTechMobileCommon(chirp_common.CloneModeRadio,
703
                        chirp_common.ExperimentalRadio):
704
    """BTECH's UV-5001 and alike radios"""
705
    VENDOR = "BTECH"
706
    MODEL = ""
707
    IDENT = ""
708
    BANDS = 2
709
    COLOR_LCD = False
710
    COLOR_LCD2 = False
711
    COLOR_LCD3 = False
712
    NAME_LENGTH = 6
713
    UPLOAD_MEM_SIZE = 0X3100
714
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
715
                     chirp_common.PowerLevel("Low", watts=10)]
716
    _vhf_range = (130000000, 180000000)
717
    _220_range = (200000000, 271000000)
718
    _uhf_range = (400000000, 521000000)
719
    _350_range = (350000000, 391000000)
720
    _upper = 199
721
    _magic = MSTRING
722
    _fileid = None
723
    _id2 = False
724
    btech3 = False
725
    _gmrs = False
726

    
727
    @classmethod
728
    def get_prompts(cls):
729
        rp = chirp_common.RadioPrompts()
730
        rp.experimental = \
731
            ('This driver is experimental.\n'
732
             '\n'
733
             'Please keep a copy of your memories with the original software '
734
             'if you treasure them, this driver is new and may contain'
735
             ' bugs.\n'
736
             '\n'
737
             )
738
        rp.pre_download = _(dedent("""\
739
            Follow these instructions to download your info:
740

    
741
            1 - Turn off your radio
742
            2 - Connect your interface cable
743
            3 - Turn on your radio
744
            4 - Do the download of your radio data
745

    
746
            """))
747
        rp.pre_upload = _(dedent("""\
748
            Follow these instructions to upload your info:
749

    
750
            1 - Turn off your radio
751
            2 - Connect your interface cable
752
            3 - Turn on your radio
753
            4 - Do the upload of your radio data
754

    
755
            """))
756
        return rp
757

    
758
    def get_features(self):
759
        """Get the radio's features"""
760

    
761
        # we will use the following var as global
762
        global POWER_LEVELS
763

    
764
        rf = chirp_common.RadioFeatures()
765
        rf.has_settings = False
766
        rf.has_bank = False
767
        rf.has_tuning_step = False
768
        rf.can_odd_split = True
769
        rf.has_name = True
770
        rf.has_offset = True
771
        rf.has_mode = True
772
        rf.has_dtcs = True
773
        rf.has_rx_dtcs = True
774
        rf.has_dtcs_polarity = True
775
        rf.has_ctone = True
776
        rf.has_cross = True
777
        rf.valid_modes = MODES
778
        rf.valid_characters = VALID_CHARS
779
        rf.valid_name_length = self.NAME_LENGTH
780
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
781
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
782
        rf.valid_cross_modes = [
783
            "Tone->Tone",
784
            "DTCS->",
785
            "->DTCS",
786
            "Tone->DTCS",
787
            "DTCS->Tone",
788
            "->Tone",
789
            "DTCS->DTCS"]
790
        rf.valid_skips = SKIP_VALUES
791
        rf.valid_dtcs_codes = DTCS
792
        rf.valid_tuning_steps = STEPS
793
        rf.memory_bounds = (0, self._upper)
794

    
795
        # power levels
796
        POWER_LEVELS = self._power_levels
797
        rf.valid_power_levels = POWER_LEVELS
798

    
799
        # normal dual bands
800
        rf.valid_bands = [self._vhf_range, self._uhf_range]
801

    
802
        # 220 band
803
        if self.BANDS == 3 or self.BANDS == 4:
804
            rf.valid_bands.append(self._220_range)
805

    
806
        # 350 band
807
        if self.BANDS == 4:
808
            rf.valid_bands.append(self._350_range)
809

    
810
        return rf
811

    
812
    def validate_memory(self, mem):
813
        msgs = chirp_common.CloneModeRadio.validate_memory(self, mem)
814

    
815
        _msg_duplex1 = 'Memory location only supports "Low"'
816
        _msg_duplex2 = 'Memory location only supports "off"'
817
        _msg_duplex3 = 'Memory location only supports "(None)", "+" or "off"'
818

    
819
        if self._gmrs:
820
            if mem.number < 1 or mem.number > 30:
821
                if float(mem.freq) / 1000000 in GMRS_FREQS1:
822
                    if mem.duplex not in ['', 'off']:
823
                        # warn user wrong Duplex
824
                        msgs.append(chirp_common.ValidationError(_msg_duplex2))
825
                    if mem.power != self._power_levels[2]:
826
                        # warn user wrong Duplex
827
                        msgs.append(chirp_common.ValidationError(_msg_duplex1))
828

    
829
                if float(mem.freq) / 1000000 in GMRS_FREQS2:
830
                    if mem.duplex not in ['off', ]:
831
                        # warn user wrong Duplex
832
                        msgs.append(chirp_common.ValidationError(_msg_duplex2))
833

    
834
                if float(mem.freq) / 1000000 in GMRS_FREQS3:
835
                    if mem.duplex not in ['', '+', 'off']:
836
                        # warn user wrong Duplex
837
                        msgs.append(chirp_common.ValidationError(_msg_duplex3))
838

    
839
        return msgs
840

    
841
    def sync_in(self):
842
        """Download from radio"""
843
        data = _download(self)
844
        self._mmap = memmap.MemoryMap(data)
845
        self.process_mmap()
846

    
847
    def sync_out(self):
848
        """Upload to radio"""
849
        try:
850
            _upload(self)
851
        except errors.RadioError:
852
            raise
853
        except Exception, e:
854
            raise errors.RadioError("Error: %s" % e)
855

    
856
    def get_raw_memory(self, number):
857
        return repr(self._memobj.memory[number])
858

    
859
    def _decode_tone(self, val):
860
        """Parse the tone data to decode from mem, it returns:
861
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
862
        pol = None
863

    
864
        if val in [0, 65535]:
865
            return '', None, None
866
        elif val > 0x0258:
867
            a = val / 10.0
868
            return 'Tone', a, pol
869
        else:
870
            if val > 0x69:
871
                index = val - 0x6A
872
                pol = "R"
873
            else:
874
                index = val - 1
875
                pol = "N"
876

    
877
            tone = DTCS[index]
878
            return 'DTCS', tone, pol
879

    
880
    def _encode_tone(self, memval, mode, val, pol):
881
        """Parse the tone data to encode from UI to mem"""
882
        if mode == '' or mode is None:
883
            memval.set_raw("\x00\x00")
884
        elif mode == 'Tone':
885
            memval.set_value(val * 10)
886
        elif mode == 'DTCS':
887
            # detect the index in the DTCS list
888
            try:
889
                index = DTCS.index(val)
890
                if pol == "N":
891
                    index += 1
892
                else:
893
                    index += 0x6A
894
                memval.set_value(index)
895
            except:
896
                msg = "Digital Tone '%d' is not supported" % value
897
                LOG.error(msg)
898
                raise errors.RadioError(msg)
899
        else:
900
            msg = "Internal error: invalid mode '%s'" % mode
901
            LOG.error(msg)
902
            raise errors.InvalidDataError(msg)
903

    
904
    def get_memory(self, number):
905
        """Get the mem representation from the radio image"""
906
        _mem = self._memobj.memory[number]
907
        _names = self._memobj.names[number]
908

    
909
        # Create a high-level memory object to return to the UI
910
        mem = chirp_common.Memory()
911

    
912
        # Memory number
913
        mem.number = number
914

    
915
        if _mem.get_raw()[0] == "\xFF":
916
            mem.empty = True
917
            return mem
918

    
919
        # Freq and offset
920
        mem.freq = int(_mem.rxfreq) * 10
921
        # tx freq can be blank
922
        if _mem.get_raw()[4] == "\xFF":
923
            # TX freq not set
924
            mem.offset = 0
925
            mem.duplex = "off"
926
        else:
927
            # TX freq set
928
            offset = (int(_mem.txfreq) * 10) - mem.freq
929
            if offset != 0:
930
                if _split(self.get_features(), mem.freq, int(
931
                          _mem.txfreq) * 10):
932
                    mem.duplex = "split"
933
                    mem.offset = int(_mem.txfreq) * 10
934
                elif offset < 0:
935
                    mem.offset = abs(offset)
936
                    mem.duplex = "-"
937
                elif offset > 0:
938
                    mem.offset = offset
939
                    mem.duplex = "+"
940
            else:
941
                mem.offset = 0
942

    
943
        # name TAG of the channel
944
        mem.name = str(_names.name).rstrip("\xFF").replace("\xFF", " ")
945

    
946
        # power
947
        mem.power = POWER_LEVELS[int(_mem.power)]
948

    
949
        # wide/narrow
950
        mem.mode = MODES[int(_mem.wide)]
951

    
952
        # skip
953
        mem.skip = SKIP_VALUES[_mem.add]
954

    
955
        # tone data
956
        rxtone = txtone = None
957
        txtone = self._decode_tone(_mem.txtone)
958
        rxtone = self._decode_tone(_mem.rxtone)
959
        chirp_common.split_tone_decode(mem, txtone, rxtone)
960

    
961
        # Extra
962
        mem.extra = RadioSettingGroup("extra", "Extra")
963

    
964
        if not self.COLOR_LCD or \
965
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
966
            scramble = RadioSetting("scramble", "Scramble",
967
                                    RadioSettingValueBoolean(bool(
968
                                        _mem.scramble)))
969
            mem.extra.append(scramble)
970

    
971
        bcl = RadioSetting("bcl", "Busy channel lockout",
972
                           RadioSettingValueBoolean(bool(_mem.bcl)))
973
        mem.extra.append(bcl)
974

    
975
        pttid = RadioSetting("pttid", "PTT ID",
976
                             RadioSettingValueList(PTTID_LIST,
977
                                                   PTTID_LIST[_mem.pttid]))
978
        mem.extra.append(pttid)
979

    
980
        # validating scode
981
        scode = _mem.scode if _mem.scode != 15 else 0
982
        pttidcode = RadioSetting("scode", "PTT ID signal code",
983
                                 RadioSettingValueList(
984
                                     PTTIDCODE_LIST,
985
                                     PTTIDCODE_LIST[scode]))
986
        mem.extra.append(pttidcode)
987

    
988
        optsig = RadioSetting("optsig", "Optional signaling",
989
                              RadioSettingValueList(
990
                                  OPTSIG_LIST,
991
                                  OPTSIG_LIST[_mem.optsig]))
992
        mem.extra.append(optsig)
993

    
994
        spmute = RadioSetting("spmute", "Speaker mute",
995
                              RadioSettingValueList(
996
                                  SPMUTE_LIST,
997
                                  SPMUTE_LIST[_mem.spmute]))
998
        mem.extra.append(spmute)
999

    
1000
        return mem
1001

    
1002
    def set_memory(self, mem):
1003
        """Set the memory data in the eeprom img from the UI"""
1004
        # get the eprom representation of this channel
1005
        _mem = self._memobj.memory[mem.number]
1006
        _names = self._memobj.names[mem.number]
1007

    
1008
        mem_was_empty = False
1009
        # same method as used in get_memory for determining if mem is empty
1010
        # doing this BEFORE overwriting it with new values ...
1011
        if _mem.get_raw()[0] == "\xFF":
1012
            LOG.debug("This mem was empty before")
1013
            mem_was_empty = True
1014

    
1015
        # if empty memmory
1016
        if mem.empty:
1017
            # the channel itself
1018
            _mem.set_raw("\xFF" * 16)
1019
            # the name tag
1020
            _names.set_raw("\xFF" * 16)
1021
            return
1022

    
1023
        if mem_was_empty:
1024
            # Zero the whole memory if we're making it unempty for
1025
            # the first time
1026
            LOG.debug('Zeroing new memory')
1027
            _mem.set_raw('\x00' * 16)
1028

    
1029
        if self._gmrs:
1030
            if mem.number >= 1 and mem.number <= 30:
1031
                GMRS_FREQ = int(GMRS_FREQS[mem.number - 1] * 1000000)
1032
                mem.freq = GMRS_FREQ
1033
                if mem.number <= 22:
1034
                    mem.duplex = ''
1035
                    mem.offset = 0
1036
                    if mem.number <= 7:
1037
                        mem.power = self._power_levels[2]
1038
                    if mem.number >= 8 and mem.number <= 14:
1039
                        mem.duplex = 'off'
1040
                        mem.mode = "NFM"
1041
                        mem.power = self._power_levels[2]
1042
                if mem.number > 22:
1043
                    mem.duplex = '+'
1044
                    mem.offset = 5000000
1045
            elif float(mem.freq) / 1000000 in GMRS_FREQS:
1046
                if float(mem.freq) / 1000000 in GMRS_FREQS1:
1047
                    mem.duplex = ''
1048
                    mem.offset = 0
1049
                    mem.power = self._power_levels[2]
1050
                if float(mem.freq) / 1000000 in GMRS_FREQS2:
1051
                    mem.duplex = 'off'
1052
                    mem.offset = 0
1053
                    mem.mode = "NFM"
1054
                    mem.power = self._power_levels[2]
1055
                if float(mem.freq) / 1000000 in GMRS_FREQS3:
1056
                    if mem.duplex == '+':
1057
                        mem.offset = 5000000
1058
                    else:
1059
                        mem.offset = 0
1060
            else:
1061
                mem.duplex = 'off'
1062
                mem.offset = 0
1063

    
1064
        # frequency
1065
        _mem.rxfreq = mem.freq / 10
1066

    
1067
        # duplex
1068
        if mem.duplex == "+":
1069
            _mem.txfreq = (mem.freq + mem.offset) / 10
1070
        elif mem.duplex == "-":
1071
            _mem.txfreq = (mem.freq - mem.offset) / 10
1072
        elif mem.duplex == "off":
1073
            for i in _mem.txfreq:
1074
                i.set_raw("\xFF")
1075
        elif mem.duplex == "split":
1076
            _mem.txfreq = mem.offset / 10
1077
        else:
1078
            _mem.txfreq = mem.freq / 10
1079

    
1080
        # tone data
1081
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
1082
            chirp_common.split_tone_encode(mem)
1083
        self._encode_tone(_mem.txtone, txmode, txtone, txpol)
1084
        self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)
1085

    
1086
        # name TAG of the channel
1087
        if len(mem.name) < self.NAME_LENGTH:
1088
            # we must pad to self.NAME_LENGTH chars, " " = "\xFF"
1089
            mem.name = str(mem.name).ljust(self.NAME_LENGTH, " ")
1090
        _names.name = str(mem.name).replace(" ", "\xFF")
1091

    
1092
        # power, # default power level is high
1093
        _mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)
1094

    
1095
        # wide/narrow
1096
        _mem.wide = MODES.index(mem.mode)
1097

    
1098
        # scan add property
1099
        _mem.add = SKIP_VALUES.index(mem.skip)
1100

    
1101
        # reseting unknowns, this have to be set by hand
1102
        _mem.unknown0 = 0
1103
        _mem.unknown1 = 0
1104
        _mem.unknown2 = 0
1105
        _mem.unknown3 = 0
1106
        _mem.unknown4 = 0
1107
        _mem.unknown5 = 0
1108
        _mem.unknown6 = 0
1109

    
1110
        def _zero_settings():
1111
            _mem.spmute = 0
1112
            _mem.optsig = 0
1113
            _mem.scramble = 0
1114
            _mem.bcl = 0
1115
            _mem.pttid = 0
1116
            _mem.scode = 0
1117

    
1118
        if self.COLOR_LCD and _mem.scramble:
1119
            LOG.info('Resetting scramble bit for BTECH COLOR_LCD variant')
1120
            _mem.scramble = 0
1121

    
1122
        # extra settings
1123
        if len(mem.extra) > 0:
1124
            # there are setting, parse
1125
            LOG.debug("Extra-Setting supplied. Setting them.")
1126
            # Zero them all first so any not provided by model don't
1127
            # stay set
1128
            _zero_settings()
1129
            for setting in mem.extra:
1130
                setattr(_mem, setting.get_name(), setting.value)
1131
        else:
1132
            if mem.empty:
1133
                LOG.debug("New mem is empty.")
1134
            else:
1135
                LOG.debug("New mem is NOT empty")
1136
                # set extra-settings to default ONLY when apreviously empty or
1137
                # deleted memory was edited to prevent errors such as #4121
1138
                if mem_was_empty:
1139
                    LOG.debug("old mem was empty. Setting default for extras.")
1140
                    _zero_settings()
1141

    
1142
        return mem
1143

    
1144
    def set_settings(self, settings):
1145
        _settings = self._memobj.settings
1146
        for element in settings:
1147
            if not isinstance(element, RadioSetting):
1148
                if element.get_name() == "fm_preset":
1149
                    self._set_fm_preset(element)
1150
                else:
1151
                    self.set_settings(element)
1152
                    continue
1153
            else:
1154
                try:
1155
                    name = element.get_name()
1156
                    if "." in name:
1157
                        bits = name.split(".")
1158
                        obj = self._memobj
1159
                        for bit in bits[:-1]:
1160
                            if "/" in bit:
1161
                                bit, index = bit.split("/", 1)
1162
                                index = int(index)
1163
                                obj = getattr(obj, bit)[index]
1164
                            else:
1165
                                obj = getattr(obj, bit)
1166
                        setting = bits[-1]
1167
                    else:
1168
                        obj = _settings
1169
                        setting = element.get_name()
1170

    
1171
                    if element.has_apply_callback():
1172
                        LOG.debug("Using apply callback")
1173
                        element.run_apply_callback()
1174
                    elif element.value.get_mutable():
1175
                        LOG.debug("Setting %s = %s" % (setting, element.value))
1176
                        setattr(obj, setting, element.value)
1177
                except Exception, e:
1178
                    LOG.debug(element.get_name())
1179
                    raise
1180

    
1181
    @classmethod
1182
    def match_model(cls, filedata, filename):
1183
        match_size = False
1184
        match_model = False
1185

    
1186
        # testing the file data size
1187
        if len(filedata) == MEM_SIZE:
1188
            match_size = True
1189

    
1190
        # testing the firmware model fingerprint
1191
        match_model = model_match(cls, filedata)
1192

    
1193
        if match_size and match_model:
1194
            return True
1195
        else:
1196
            return False
1197

    
1198

    
1199
MEM_FORMAT = """
1200
#seekto 0x0000;
1201
struct {
1202
  lbcd rxfreq[4];
1203
  lbcd txfreq[4];
1204
  ul16 rxtone;
1205
  ul16 txtone;
1206
  u8 unknown0:4,
1207
     scode:4;
1208
  u8 unknown1:2,
1209
     spmute:2,
1210
     unknown2:2,
1211
     optsig:2;
1212
  u8 unknown3:3,
1213
     scramble:1,
1214
     unknown4:3,
1215
     power:1;
1216
  u8 unknown5:1,
1217
     wide:1,
1218
     unknown6:2,
1219
     bcl:1,
1220
     add:1,
1221
     pttid:2;
1222
} memory[200];
1223

    
1224
#seekto 0x0E00;
1225
struct {
1226
  u8 tdr;
1227
  u8 unknown1;
1228
  u8 sql;
1229
  u8 unknown2[2];
1230
  u8 tot;
1231
  u8 apo;           // BTech radios use this as the Auto Power Off time
1232
                    // other radios use this as pre-Time Out Alert
1233
  u8 unknown3;
1234
  u8 abr;
1235
  u8 beep;
1236
  u8 unknown4[4];
1237
  u8 dtmfst;
1238
  u8 unknown5[2];
1239
  u8 prisc;
1240
  u8 prich;
1241
  u8 screv;
1242
  u8 unknown6[2];
1243
  u8 pttid;
1244
  u8 pttlt;
1245
  u8 unknown7;
1246
  u8 emctp;
1247
  u8 emcch;
1248
  u8 ringt;
1249
  u8 unknown8;
1250
  u8 camdf;
1251
  u8 cbmdf;
1252
  u8 sync;          // BTech radios use this as the display sync setting
1253
                    // other radios use this as the auto keypad lock setting
1254
  u8 ponmsg;
1255
  u8 wtled;
1256
  u8 rxled;
1257
  u8 txled;
1258
  u8 unknown9[5];
1259
  u8 anil;
1260
  u8 reps;
1261
  u8 repm;
1262
  u8 tdrab;
1263
  u8 ste;
1264
  u8 rpste;
1265
  u8 rptdl;
1266
  u8 mgain;
1267
  u8 dtmfg;
1268
} settings;
1269

    
1270
#seekto 0x0E80;
1271
struct {
1272
  u8 unknown1;
1273
  u8 vfomr;
1274
  u8 keylock;
1275
  u8 unknown2;
1276
  u8 unknown3:4,
1277
     vfomren:1,
1278
     unknown4:1,
1279
     reseten:1,
1280
     menuen:1;
1281
  u8 unknown5[11];
1282
  u8 dispab;
1283
  u8 mrcha;
1284
  u8 mrchb;
1285
  u8 menu;
1286
} settings2;
1287

    
1288
#seekto 0x0EC0;
1289
struct {
1290
  char line1[6];
1291
  char line2[6];
1292
} poweron_msg;
1293

    
1294
struct settings_vfo {
1295
  u8 freq[8];
1296
  u8 offset[6];
1297
  u8 unknown2[2];
1298
  ul16 rxtone;
1299
  ul16 txtone;
1300
  u8 scode;
1301
  u8 spmute;
1302
  u8 optsig;
1303
  u8 scramble;
1304
  u8 wide;
1305
  u8 power;
1306
  u8 shiftd;
1307
  u8 step;
1308
  u8 unknown3[4];
1309
};
1310

    
1311
#seekto 0x0F00;
1312
struct {
1313
  struct settings_vfo a;
1314
  struct settings_vfo b;
1315
} vfo;
1316

    
1317
#seekto 0x1000;
1318
struct {
1319
  char name[6];
1320
  u8 unknown1[10];
1321
} names[200];
1322

    
1323
#seekto 0x2400;
1324
struct {
1325
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
1326
  u8 group_tone;
1327
  u8 repeat_tone;
1328
  u8 unused[13];
1329
} _5tone_std_settings[15];
1330

    
1331
#seekto 0x2500;
1332
struct {
1333
  u8 frame1[5];
1334
  u8 frame2[5];
1335
  u8 frame3[5];
1336
  u8 standard;   // one out of LIST_5TONE_STANDARDS
1337
} _5tone_codes[15];
1338

    
1339
#seekto 0x25F0;
1340
struct {
1341
  u8 _5tone_delay1; // * 10ms
1342
  u8 _5tone_delay2; // * 10ms
1343
  u8 _5tone_delay3; // * 10ms
1344
  u8 _5tone_first_digit_ext_length;
1345
  u8 unknown1;
1346
  u8 unknown2;
1347
  u8 unknown3;
1348
  u8 unknown4;
1349
  u8 decode_standard;
1350
  u8 unknown5:5,
1351
     _5tone_decode_call_frame3:1,
1352
     _5tone_decode_call_frame2:1,
1353
     _5tone_decode_call_frame1:1;
1354
  u8 unknown6:5,
1355
     _5tone_decode_disp_frame3:1,
1356
     _5tone_decode_disp_frame2:1,
1357
     _5tone_decode_disp_frame1:1;
1358
  u8 decode_reset_time; // * 100 + 100ms
1359
} _5tone_settings;
1360

    
1361
#seekto 0x2900;
1362
struct {
1363
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
1364
} dtmf_codes[15];
1365

    
1366
#seekto 0x29F0;
1367
struct {
1368
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
1369
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
1370
  u8 unknown0[14];
1371
  u8 inspection[16];
1372
  u8 monitor[16];
1373
  u8 alarmcode[16];
1374
  u8 stun[16];
1375
  u8 kill[16];
1376
  u8 revive[16];
1377
  u8 unknown1[16];
1378
  u8 unknown2[16];
1379
  u8 unknown3[16];
1380
  u8 unknown4[16];
1381
  u8 unknown5[16];
1382
  u8 unknown6[16];
1383
  u8 unknown7[16];
1384
  u8 masterid[16];
1385
  u8 viceid[16];
1386
  u8 unused01:7,
1387
     mastervice:1;
1388
  u8 unused02:3,
1389
     mrevive:1,
1390
     mkill:1,
1391
     mstun:1,
1392
     mmonitor:1,
1393
     minspection:1;
1394
  u8 unused03:3,
1395
     vrevive:1,
1396
     vkill:1,
1397
     vstun:1,
1398
     vmonitor:1,
1399
     vinspection:1;
1400
  u8 unused04:6,
1401
     txdisable:1,
1402
     rxdisable:1;
1403
  u8 groupcode;
1404
  u8 spacecode;
1405
  u8 delayproctime; // * 100 + 100ms
1406
  u8 resettime;     // * 100 + 100ms
1407
} dtmf_settings;
1408

    
1409
#seekto 0x2D00;
1410
struct {
1411
  struct {
1412
    ul16 freq1;
1413
    u8 unused01[6];
1414
    ul16 freq2;
1415
    u8 unused02[6];
1416
  } _2tone_encode[15];
1417
  u8 duration_1st_tone; // *10ms
1418
  u8 duration_2nd_tone; // *10ms
1419
  u8 duration_gap;      // *10ms
1420
  u8 unused03[13];
1421
  struct {
1422
    struct {
1423
      u8 dec;      // one out of LIST_2TONE_DEC
1424
      u8 response; // one out of LIST_2TONE_RESPONSE
1425
      u8 alert;    // 1-16
1426
    } decs[4];
1427
    u8 unused04[4];
1428
  } _2tone_decode[15];
1429
  u8 unused05[16];
1430

    
1431
  struct {
1432
    ul16 freqA;
1433
    ul16 freqB;
1434
    ul16 freqC;
1435
    ul16 freqD;
1436
    // unknown what those values mean, but they are
1437
    // derived from configured frequencies
1438
    ul16 derived_from_freqA; // 2304000/freqA
1439
    ul16 derived_from_freqB; // 2304000/freqB
1440
    ul16 derived_from_freqC; // 2304000/freqC
1441
    ul16 derived_from_freqD; // 2304000/freqD
1442
  }freqs[15];
1443
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
1444
} _2tone;
1445

    
1446
#seekto 0x3000;
1447
struct {
1448
  u8 freq[8];
1449
  char broadcast_station_name[6];
1450
  u8 unknown[2];
1451
} fm_radio_preset[16];
1452

    
1453
#seekto 0x3C90;
1454
struct {
1455
  u8 vhf_low[3];
1456
  u8 vhf_high[3];
1457
  u8 uhf_low[3];
1458
  u8 uhf_high[3];
1459
} ranges;
1460

    
1461
// the UV-2501+220 & KT8900R has different zones for storing ranges
1462

    
1463
#seekto 0x3CD0;
1464
struct {
1465
  u8 vhf_low[3];
1466
  u8 vhf_high[3];
1467
  u8 unknown1[4];
1468
  u8 unknown2[6];
1469
  u8 vhf2_low[3];
1470
  u8 vhf2_high[3];
1471
  u8 unknown3[4];
1472
  u8 unknown4[6];
1473
  u8 uhf_low[3];
1474
  u8 uhf_high[3];
1475
} ranges220;
1476

    
1477
#seekto 0x3F70;
1478
struct {
1479
  char fp[6];
1480
} fingerprint;
1481

    
1482
"""
1483

    
1484

    
1485
class BTech(BTechMobileCommon):
1486
    """BTECH's UV-5001 and alike radios"""
1487
    BANDS = 2
1488
    COLOR_LCD = False
1489
    NAME_LENGTH = 6
1490

    
1491
    def set_options(self):
1492
        """This is to read the options from the image and set it in the
1493
        environment, for now just the limits of the freqs in the VHF/UHF
1494
        ranges"""
1495

    
1496
        # setting the correct ranges for each radio type
1497
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1498
            # the model 2501+220 has a segment in 220
1499
            # and a different position in the memmap
1500
            # also the QYT KT8900R
1501
            ranges = self._memobj.ranges220
1502
        else:
1503
            ranges = self._memobj.ranges
1504

    
1505
        # the normal dual bands
1506
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
1507
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
1508

    
1509
        # DEBUG
1510
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
1511
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
1512

    
1513
        # 220Mhz radios case
1514
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1515
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
1516
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
1517
            self._220_range = vhf2
1518

    
1519
        # set the class with the real data
1520
        self._vhf_range = vhf
1521
        self._uhf_range = uhf
1522

    
1523
    def process_mmap(self):
1524
        """Process the mem map into the mem object"""
1525

    
1526
        # Get it
1527
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
1528

    
1529
        # load specific parameters from the radio image
1530
        self.set_options()
1531

    
1532

    
1533
# Declaring Aliases (Clones of the real radios)
1534
class JT2705M(chirp_common.Alias):
1535
    VENDOR = "Jetstream"
1536
    MODEL = "JT2705M"
1537

    
1538

    
1539
class JT6188Mini(chirp_common.Alias):
1540
    VENDOR = "Juentai"
1541
    MODEL = "JT-6188 Mini"
1542

    
1543

    
1544
class JT6188Plus(chirp_common.Alias):
1545
    VENDOR = "Juentai"
1546
    MODEL = "JT-6188 Plus"
1547

    
1548

    
1549
class SSGT890(chirp_common.Alias):
1550
    VENDOR = "Sainsonic"
1551
    MODEL = "GT-890"
1552

    
1553

    
1554
class ZastoneMP300(chirp_common.Alias):
1555
    VENDOR = "Zastone"
1556
    MODEL = "MP-300"
1557

    
1558

    
1559
# real radios
1560
@directory.register
1561
class UV2501(BTech):
1562
    """Baofeng Tech UV2501"""
1563
    MODEL = "UV-2501"
1564
    _fileid = [UV2501G3_fp,
1565
               UV2501G2_fp,
1566
               UV2501pp2_fp,
1567
               UV2501pp_fp]
1568

    
1569

    
1570
@directory.register
1571
class UV2501_220(BTech):
1572
    """Baofeng Tech UV2501+220"""
1573
    MODEL = "UV-2501+220"
1574
    BANDS = 3
1575
    _magic = MSTRING_220
1576
    _id2 = [UV2501_220pp_id, ]
1577
    _fileid = [UV2501_220G3_fp,
1578
               UV2501_220G2_fp,
1579
               UV2501_220_fp,
1580
               UV2501_220pp_fp]
1581

    
1582

    
1583
@directory.register
1584
class UV5001(BTech):
1585
    """Baofeng Tech UV5001"""
1586
    MODEL = "UV-5001"
1587
    _fileid = [UV5001G3_fp,
1588
               UV5001G22_fp,
1589
               UV5001G2_fp,
1590
               UV5001alpha_fp,
1591
               UV5001pp_fp]
1592
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
1593
                     chirp_common.PowerLevel("Low", watts=10)]
1594

    
1595

    
1596
@directory.register
1597
class MINI8900(BTech):
1598
    """WACCOM MINI-8900"""
1599
    VENDOR = "WACCOM"
1600
    MODEL = "MINI-8900"
1601
    _magic = MSTRING_MINI8900
1602
    _fileid = [MINI8900_fp, ]
1603
    # Clones
1604
    ALIASES = [JT6188Plus, ]
1605

    
1606

    
1607
@directory.register
1608
class KTUV980(BTech):
1609
    """QYT KT-UV980"""
1610
    VENDOR = "QYT"
1611
    MODEL = "KT-UV980"
1612
    _vhf_range = (136000000, 175000000)
1613
    _uhf_range = (400000000, 481000000)
1614
    _magic = MSTRING_MINI8900
1615
    _fileid = [KTUV980_fp, ]
1616
    # Clones
1617
    ALIASES = [JT2705M, ]
1618

    
1619
# Please note that there is a version of this radios that is a clone of the
1620
# Waccom Mini8900, maybe an early version?
1621

    
1622

    
1623
class OTGRadioV1(chirp_common.Alias):
1624
    VENDOR = 'OTGSTUFF'
1625
    MODEL = 'OTG Radio v1'
1626

    
1627

    
1628
@directory.register
1629
class KT9800(BTech):
1630
    """QYT KT8900"""
1631
    VENDOR = "QYT"
1632
    MODEL = "KT8900"
1633
    _vhf_range = (136000000, 175000000)
1634
    _uhf_range = (400000000, 481000000)
1635
    _magic = MSTRING_KT8900
1636
    _fileid = [KT8900_fp,
1637
               KT8900_fp1,
1638
               KT8900_fp2,
1639
               KT8900_fp3,
1640
               KT8900_fp4,
1641
               KT8900_fp5,
1642
               KT8900_fp6,
1643
               KT8900_fp7]
1644
    _id2 = [KT8900_id, KT8900_id2]
1645
    # Clones
1646
    ALIASES = [JT6188Mini, SSGT890, ZastoneMP300]
1647

    
1648

    
1649
@directory.register
1650
class KT9800R(BTech):
1651
    """QYT KT8900R"""
1652
    VENDOR = "QYT"
1653
    MODEL = "KT8900R"
1654
    BANDS = 3
1655
    _vhf_range = (136000000, 175000000)
1656
    _220_range = (240000000, 271000000)
1657
    _uhf_range = (400000000, 481000000)
1658
    _magic = MSTRING_KT8900R
1659
    _fileid = [KT8900R_fp,
1660
               KT8900R_fp1,
1661
               KT8900R_fp2,
1662
               KT8900R_fp3,
1663
               KT8900R_fp4,
1664
               KT8900R_fp5]
1665
    _id2 = [KT8900R_id, KT8900R_id2]
1666

    
1667

    
1668
@directory.register
1669
class LT588UV(BTech):
1670
    """LUITON LT-588UV"""
1671
    VENDOR = "LUITON"
1672
    MODEL = "LT-588UV"
1673
    _vhf_range = (136000000, 175000000)
1674
    _uhf_range = (400000000, 481000000)
1675
    _magic = MSTRING_KT8900
1676
    _fileid = [LT588UV_fp,
1677
               LT588UV_fp1]
1678
    _power_levels = [chirp_common.PowerLevel("High", watts=60),
1679
                     chirp_common.PowerLevel("Low", watts=10)]
1680

    
1681

    
1682
COLOR_MEM_FORMAT = """
1683
#seekto 0x0000;
1684
struct {
1685
  lbcd rxfreq[4];
1686
  lbcd txfreq[4];
1687
  ul16 rxtone;
1688
  ul16 txtone;
1689
  u8 unknown0:4,
1690
     scode:4;
1691
  u8 unknown1:2,
1692
     spmute:2,
1693
     unknown2:2,
1694
     optsig:2;
1695
  u8 unknown3:3,
1696
     scramble:1,
1697
     unknown4:2,
1698
     power:2;
1699
  u8 unknown5:1,
1700
     wide:1,
1701
     unknown6:2,
1702
     bcl:1,
1703
     add:1,
1704
     pttid:2;
1705
} memory[200];
1706

    
1707
#seekto 0x0E00;
1708
struct {
1709
  u8 tmr;
1710
  u8 unknown1;
1711
  u8 sql;
1712
  u8 unknown2;
1713
  u8 mgain2;
1714
  u8 tot;
1715
  u8 apo;
1716
  u8 unknown3;
1717
  u8 abr;
1718
  u8 beep;
1719
  u8 unknown4[4];
1720
  u8 dtmfst;
1721
  u8 unknown5[2];
1722
  u8 screv;
1723
  u8 unknown6[2];
1724
  u8 pttid;
1725
  u8 pttlt;
1726
  u8 unknown7;
1727
  u8 emctp;
1728
  u8 emcch;
1729
  u8 sigbp;
1730
  u8 unknown8;
1731
  u8 camdf;
1732
  u8 cbmdf;
1733
  u8 ccmdf;
1734
  u8 cdmdf;
1735
  u8 langua;
1736
  u8 sync;          // BTech radios use this as the display sync
1737
                    // setting, other radios use this as the auto
1738
                    // keypad lock setting
1739
  u8 mainfc;
1740
  u8 mainbc;
1741
  u8 menufc;
1742
  u8 menubc;
1743
  u8 stafc;
1744
  u8 stabc;
1745
  u8 sigfc;
1746
  u8 sigbc;
1747
  u8 rxfc;
1748
  u8 txfc;
1749
  u8 txdisp;
1750
  u8 unknown9[5];
1751
  u8 anil;
1752
  u8 reps;
1753
  u8 repm;
1754
  u8 tmrmr;
1755
  u8 ste;
1756
  u8 rpste;
1757
  u8 rptdl;
1758
  u8 dtmfg;
1759
  u8 mgain;         // used by db25-g for ponyey
1760
  u8 skiptx;
1761
  u8 scmode;
1762
} settings;
1763

    
1764
#seekto 0x0E80;
1765
struct {
1766
  u8 unknown1;
1767
  u8 vfomr;
1768
  u8 keylock;
1769
  u8 unknown2;
1770
  u8 unknown3:4,
1771
     vfomren:1,
1772
     unknown4:1,
1773
     reseten:1,
1774
     menuen:1;
1775
  u8 unknown5[11];
1776
  u8 dispab;
1777
  u8 unknown6[2];
1778
  u8 menu;
1779
  u8 unknown7[7];
1780
  u8 vfomra;
1781
  u8 vfomrb;
1782
  u8 vfomrc;
1783
  u8 vfomrd;
1784
  u8 mrcha;
1785
  u8 mrchb;
1786
  u8 mrchc;
1787
  u8 mrchd;
1788
} settings2;
1789

    
1790
struct settings_vfo {
1791
  u8 freq[8];
1792
  u8 offset[6];
1793
  u8 unknown2[2];
1794
  ul16 rxtone;
1795
  ul16 txtone;
1796
  u8 scode;
1797
  u8 spmute;
1798
  u8 optsig;
1799
  u8 scramble;
1800
  u8 wide;
1801
  u8 power;
1802
  u8 shiftd;
1803
  u8 step;
1804
  u8 unknown3[4];
1805
};
1806

    
1807
#seekto 0x0F00;
1808
struct {
1809
  struct settings_vfo a;
1810
  struct settings_vfo b;
1811
  struct settings_vfo c;
1812
  struct settings_vfo d;
1813
} vfo;
1814

    
1815
#seekto 0x0F80;
1816
struct {
1817
  char line1[8];
1818
  char line2[8];
1819
  char line3[8];
1820
  char line4[8];
1821
  char line5[8];
1822
  char line6[8];
1823
  char line7[8];
1824
  char line8[8];
1825
} poweron_msg;
1826

    
1827
#seekto 0x1000;
1828
struct {
1829
  char name[8];
1830
  u8 unknown1[8];
1831
} names[200];
1832

    
1833
#seekto 0x2400;
1834
struct {
1835
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
1836
  u8 group_tone;
1837
  u8 repeat_tone;
1838
  u8 unused[13];
1839
} _5tone_std_settings[15];
1840

    
1841
#seekto 0x2500;
1842
struct {
1843
  u8 frame1[5];
1844
  u8 frame2[5];
1845
  u8 frame3[5];
1846
  u8 standard;   // one out of LIST_5TONE_STANDARDS
1847
} _5tone_codes[15];
1848

    
1849
#seekto 0x25F0;
1850
struct {
1851
  u8 _5tone_delay1; // * 10ms
1852
  u8 _5tone_delay2; // * 10ms
1853
  u8 _5tone_delay3; // * 10ms
1854
  u8 _5tone_first_digit_ext_length;
1855
  u8 unknown1;
1856
  u8 unknown2;
1857
  u8 unknown3;
1858
  u8 unknown4;
1859
  u8 decode_standard;
1860
  u8 unknown5:5,
1861
     _5tone_decode_call_frame3:1,
1862
     _5tone_decode_call_frame2:1,
1863
     _5tone_decode_call_frame1:1;
1864
  u8 unknown6:5,
1865
     _5tone_decode_disp_frame3:1,
1866
     _5tone_decode_disp_frame2:1,
1867
     _5tone_decode_disp_frame1:1;
1868
  u8 decode_reset_time; // * 100 + 100ms
1869
} _5tone_settings;
1870

    
1871
#seekto 0x2900;
1872
struct {
1873
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
1874
} dtmf_codes[15];
1875

    
1876
#seekto 0x29F0;
1877
struct {
1878
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
1879
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
1880
  u8 unknown0[14];
1881
  u8 inspection[16];
1882
  u8 monitor[16];
1883
  u8 alarmcode[16];
1884
  u8 stun[16];
1885
  u8 kill[16];
1886
  u8 revive[16];
1887
  u8 unknown1[16];
1888
  u8 unknown2[16];
1889
  u8 unknown3[16];
1890
  u8 unknown4[16];
1891
  u8 unknown5[16];
1892
  u8 unknown6[16];
1893
  u8 unknown7[16];
1894
  u8 masterid[16];
1895
  u8 viceid[16];
1896
  u8 unused01:7,
1897
     mastervice:1;
1898
  u8 unused02:3,
1899
     mrevive:1,
1900
     mkill:1,
1901
     mstun:1,
1902
     mmonitor:1,
1903
     minspection:1;
1904
  u8 unused03:3,
1905
     vrevive:1,
1906
     vkill:1,
1907
     vstun:1,
1908
     vmonitor:1,
1909
     vinspection:1;
1910
  u8 unused04:6,
1911
     txdisable:1,
1912
     rxdisable:1;
1913
  u8 groupcode;
1914
  u8 spacecode;
1915
  u8 delayproctime; // * 100 + 100ms
1916
  u8 resettime;     // * 100 + 100ms
1917
} dtmf_settings;
1918

    
1919
#seekto 0x2D00;
1920
struct {
1921
  struct {
1922
    ul16 freq1;
1923
    u8 unused01[6];
1924
    ul16 freq2;
1925
    u8 unused02[6];
1926
  } _2tone_encode[15];
1927
  u8 duration_1st_tone; // *10ms
1928
  u8 duration_2nd_tone; // *10ms
1929
  u8 duration_gap;      // *10ms
1930
  u8 unused03[13];
1931
  struct {
1932
    struct {
1933
      u8 dec;      // one out of LIST_2TONE_DEC
1934
      u8 response; // one out of LIST_2TONE_RESPONSE
1935
      u8 alert;    // 1-16
1936
    } decs[4];
1937
    u8 unused04[4];
1938
  } _2tone_decode[15];
1939
  u8 unused05[16];
1940

    
1941
  struct {
1942
    ul16 freqA;
1943
    ul16 freqB;
1944
    ul16 freqC;
1945
    ul16 freqD;
1946
    // unknown what those values mean, but they are
1947
    // derived from configured frequencies
1948
    ul16 derived_from_freqA; // 2304000/freqA
1949
    ul16 derived_from_freqB; // 2304000/freqB
1950
    ul16 derived_from_freqC; // 2304000/freqC
1951
    ul16 derived_from_freqD; // 2304000/freqD
1952
  }freqs[15];
1953
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
1954
} _2tone;
1955

    
1956
#seekto 0x3D80;
1957
struct {
1958
  u8 vhf_low[3];
1959
  u8 vhf_high[3];
1960
  u8 unknown1[4];
1961
  u8 unknown2[6];
1962
  u8 vhf2_low[3];
1963
  u8 vhf2_high[3];
1964
  u8 unknown3[4];
1965
  u8 unknown4[6];
1966
  u8 uhf_low[3];
1967
  u8 uhf_high[3];
1968
  u8 unknown5[4];
1969
  u8 unknown6[6];
1970
  u8 uhf2_low[3];
1971
  u8 uhf2_high[3];
1972
} ranges;
1973

    
1974
#seekto 0x3F70;
1975
struct {
1976
  char fp[6];
1977
} fingerprint;
1978

    
1979
"""
1980

    
1981

    
1982
class BTechColor(BTechMobileCommon):
1983
    """BTECH's Color LCD Mobile and alike radios"""
1984
    COLOR_LCD = True
1985
    NAME_LENGTH = 8
1986
    LIST_TMR = LIST_TMR16
1987

    
1988
    def process_mmap(self):
1989
        """Process the mem map into the mem object"""
1990

    
1991
        # Get it
1992
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
1993

    
1994
        # load specific parameters from the radio image
1995
        self.set_options()
1996

    
1997
    def set_options(self):
1998
        """This is to read the options from the image and set it in the
1999
        environment, for now just the limits of the freqs in the VHF/UHF
2000
        ranges"""
2001

    
2002
        # setting the correct ranges for each radio type
2003
        ranges = self._memobj.ranges
2004

    
2005
        # the normal dual bands
2006
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
2007
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
2008

    
2009
        # DEBUG
2010
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
2011
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
2012

    
2013
        # the additional bands
2014
        if self.MODEL in ["UV-25X4", "KT7900D"]:
2015
            # 200Mhz band
2016
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
2017
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
2018
            self._220_range = vhf2
2019

    
2020
            # 350Mhz band
2021
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
2022
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
2023
            self._350_range = uhf2
2024

    
2025
        # set the class with the real data
2026
        self._vhf_range = vhf
2027
        self._uhf_range = uhf
2028

    
2029

    
2030
# Declaring Aliases (Clones of the real radios)
2031
class SKT8900D(chirp_common.Alias):
2032
    VENDOR = "Surecom"
2033
    MODEL = "S-KT8900D"
2034

    
2035

    
2036
class QB25(chirp_common.Alias):
2037
    VENDOR = "Radioddity"
2038
    MODEL = "QB25"
2039

    
2040

    
2041
# real radios
2042
@directory.register
2043
class UV25X2(BTechColor):
2044
    """Baofeng Tech UV25X2"""
2045
    MODEL = "UV-25X2"
2046
    BANDS = 2
2047
    _vhf_range = (130000000, 180000000)
2048
    _uhf_range = (400000000, 521000000)
2049
    _magic = MSTRING_UV25X2
2050
    _fileid = [UV25X2_fp, ]
2051

    
2052

    
2053
@directory.register
2054
class UV25X4(BTechColor):
2055
    """Baofeng Tech UV25X4"""
2056
    MODEL = "UV-25X4"
2057
    BANDS = 4
2058
    _vhf_range = (130000000, 180000000)
2059
    _220_range = (200000000, 271000000)
2060
    _uhf_range = (400000000, 521000000)
2061
    _350_range = (350000000, 391000000)
2062
    _magic = MSTRING_UV25X4
2063
    _fileid = [UV25X4_fp, ]
2064

    
2065

    
2066
@directory.register
2067
class UV50X2(BTechColor):
2068
    """Baofeng Tech UV50X2"""
2069
    MODEL = "UV-50X2"
2070
    BANDS = 2
2071
    _vhf_range = (130000000, 180000000)
2072
    _uhf_range = (400000000, 521000000)
2073
    _magic = MSTRING_UV25X2
2074
    _fileid = [UV50X2_fp, ]
2075
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
2076
                     chirp_common.PowerLevel("Low", watts=10)]
2077

    
2078

    
2079
@directory.register
2080
class KT7900D(BTechColor):
2081
    """QYT KT7900D"""
2082
    VENDOR = "QYT"
2083
    MODEL = "KT7900D"
2084
    BANDS = 4
2085
    LIST_TMR = LIST_TMR15
2086
    _vhf_range = (136000000, 175000000)
2087
    _220_range = (200000000, 271000000)
2088
    _uhf_range = (400000000, 481000000)
2089
    _350_range = (350000000, 371000000)
2090
    _magic = MSTRING_KT8900D
2091
    _fileid = [KT7900D_fp, KT7900D_fp1, KT7900D_fp2, KT7900D_fp3, KT7900D_fp4,
2092
               KT7900D_fp5, KT7900D_fp6, KT7900D_fp7, QB25_fp, ]
2093
    # Clones
2094
    ALIASES = [SKT8900D, QB25, ]
2095

    
2096

    
2097
@directory.register
2098
class KT8900D(BTechColor):
2099
    """QYT KT8900D"""
2100
    VENDOR = "QYT"
2101
    MODEL = "KT8900D"
2102
    BANDS = 2
2103
    LIST_TMR = LIST_TMR15
2104
    _vhf_range = (136000000, 175000000)
2105
    _uhf_range = (400000000, 481000000)
2106
    _magic = MSTRING_KT8900D
2107
    _fileid = [KT8900D_fp3, KT8900D_fp2, KT8900D_fp1, KT8900D_fp]
2108

    
2109
    # Clones
2110
    ALIASES = [OTGRadioV1]
2111

    
2112

    
2113
@directory.register
2114
class KT5800(BTechColor):
2115
    """QYT KT5800"""
2116
    VENDOR = "QYT"
2117
    MODEL = "KT5800"
2118
    BANDS = 2
2119
    LIST_TMR = LIST_TMR15
2120
    _vhf_range = (136000000, 175000000)
2121
    _uhf_range = (400000000, 481000000)
2122
    _magic = MSTRING_KT8900D
2123
    _fileid = [KT5800_fp, ]
2124

    
2125

    
2126
@directory.register
2127
class KT980PLUS(BTechColor):
2128
    """QYT KT980PLUS"""
2129
    VENDOR = "QYT"
2130
    MODEL = "KT980PLUS"
2131
    BANDS = 2
2132
    LIST_TMR = LIST_TMR15
2133
    _vhf_range = (136000000, 175000000)
2134
    _uhf_range = (400000000, 481000000)
2135
    _magic = MSTRING_KT8900D
2136
    _fileid = [KT980PLUS_fp1, KT980PLUS_fp]
2137
    _power_levels = [chirp_common.PowerLevel("High", watts=75),
2138
                     chirp_common.PowerLevel("Low", watts=55)]
2139

    
2140
    @classmethod
2141
    def match_model(cls, filedata, filename):
2142
        # This model is only ever matched via metadata
2143
        return False
2144

    
2145

    
2146
@directory.register
2147
class DB25G(BTechColor):
2148
    """Radioddity DB25-G"""
2149
    VENDOR = "Radioddity"
2150
    MODEL = "DB25-G"
2151
    BANDS = 2
2152
    LIST_TMR = LIST_TMR15
2153
    _vhf_range = (136000000, 175000000)
2154
    _uhf_range = (400000000, 481000000)
2155
    _magic = MSTRING_KT8900D
2156
    _fileid = [DB25G_fp1, DB25G_fp]
2157
    _gmrs = True
2158
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
2159
                     chirp_common.PowerLevel("Mid", watts=15),
2160
                     chirp_common.PowerLevel("Low", watts=5)]
2161

    
2162
    @classmethod
2163
    def match_model(cls, filedata, filename):
2164
        # This model is only ever matched via metadata
2165
        return False
2166

    
2167

    
2168
GMRS_MEM_FORMAT = """
2169
#seekto 0x0000;
2170
struct {
2171
  lbcd rxfreq[4];
2172
  lbcd txfreq[4];
2173
  ul16 rxtone;
2174
  ul16 txtone;
2175
  u8 unknown0:4,
2176
     scode:4;
2177
  u8 unknown1:2,
2178
     spmute:2,
2179
     unknown2:2,
2180
     optsig:2;
2181
  u8 unknown3:3,
2182
     scramble:1,
2183
     unknown4:2,
2184
     power:2;
2185
  u8 unknown5:1,
2186
     wide:1,
2187
     unknown6:2,
2188
     bcl:1,
2189
     add:1,
2190
     pttid:2;
2191
} memory[256];
2192

    
2193
#seekto 0x1000;
2194
struct {
2195
  char name[7];
2196
  u8 unknown1[9];
2197
} names[256];
2198

    
2199
#seekto 0x2400;
2200
struct {
2201
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
2202
  u8 group_tone;
2203
  u8 repeat_tone;
2204
  u8 unused[13];
2205
} _5tone_std_settings[15];
2206

    
2207
#seekto 0x2500;
2208
struct {
2209
  u8 frame1[5];
2210
  u8 frame2[5];
2211
  u8 frame3[5];
2212
  u8 standard;   // one out of LIST_5TONE_STANDARDS
2213
} _5tone_codes[15];
2214

    
2215
#seekto 0x25F0;
2216
struct {
2217
  u8 _5tone_delay1; // * 10ms
2218
  u8 _5tone_delay2; // * 10ms
2219
  u8 _5tone_delay3; // * 10ms
2220
  u8 _5tone_first_digit_ext_length;
2221
  u8 unknown1;
2222
  u8 unknown2;
2223
  u8 unknown3;
2224
  u8 unknown4;
2225
  u8 decode_standard;
2226
  u8 unknown5:5,
2227
     _5tone_decode_call_frame3:1,
2228
     _5tone_decode_call_frame2:1,
2229
     _5tone_decode_call_frame1:1;
2230
  u8 unknown6:5,
2231
     _5tone_decode_disp_frame3:1,
2232
     _5tone_decode_disp_frame2:1,
2233
     _5tone_decode_disp_frame1:1;
2234
  u8 decode_reset_time; // * 100 + 100ms
2235
} _5tone_settings;
2236

    
2237
#seekto 0x2900;
2238
struct {
2239
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
2240
} dtmf_codes[15];
2241

    
2242
#seekto 0x29F0;
2243
struct {
2244
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
2245
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
2246
  u8 unknown0[14];
2247
  u8 inspection[16];
2248
  u8 monitor[16];
2249
  u8 alarmcode[16];
2250
  u8 stun[16];
2251
  u8 kill[16];
2252
  u8 revive[16];
2253
  u8 unknown1[16];
2254
  u8 unknown2[16];
2255
  u8 unknown3[16];
2256
  u8 unknown4[16];
2257
  u8 unknown5[16];
2258
  u8 unknown6[16];
2259
  u8 unknown7[16];
2260
  u8 masterid[16];
2261
  u8 viceid[16];
2262
  u8 unused01:7,
2263
     mastervice:1;
2264
  u8 unused02:3,
2265
     mrevive:1,
2266
     mkill:1,
2267
     mstun:1,
2268
     mmonitor:1,
2269
     minspection:1;
2270
  u8 unused03:3,
2271
     vrevive:1,
2272
     vkill:1,
2273
     vstun:1,
2274
     vmonitor:1,
2275
     vinspection:1;
2276
  u8 unused04:6,
2277
     txdisable:1,
2278
     rxdisable:1;
2279
  u8 groupcode;
2280
  u8 spacecode;
2281
  u8 delayproctime; // * 100 + 100ms
2282
  u8 resettime;     // * 100 + 100ms
2283
} dtmf_settings;
2284

    
2285
#seekto 0x2D00;
2286
struct {
2287
  struct {
2288
    ul16 freq1;
2289
    u8 unused01[6];
2290
    ul16 freq2;
2291
    u8 unused02[6];
2292
  } _2tone_encode[15];
2293
  u8 duration_1st_tone; // *10ms
2294
  u8 duration_2nd_tone; // *10ms
2295
  u8 duration_gap;      // *10ms
2296
  u8 unused03[13];
2297
  struct {
2298
    struct {
2299
      u8 dec;      // one out of LIST_2TONE_DEC
2300
      u8 response; // one out of LIST_2TONE_RESPONSE
2301
      u8 alert;    // 1-16
2302
    } decs[4];
2303
    u8 unused04[4];
2304
  } _2tone_decode[15];
2305
  u8 unused05[16];
2306

    
2307
  struct {
2308
    ul16 freqA;
2309
    ul16 freqB;
2310
    ul16 freqC;
2311
    ul16 freqD;
2312
    // unknown what those values mean, but they are
2313
    // derived from configured frequencies
2314
    ul16 derived_from_freqA; // 2304000/freqA
2315
    ul16 derived_from_freqB; // 2304000/freqB
2316
    ul16 derived_from_freqC; // 2304000/freqC
2317
    ul16 derived_from_freqD; // 2304000/freqD
2318
  }freqs[15];
2319
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
2320
} _2tone;
2321

    
2322
#seekto 0x3000;
2323
struct {
2324
  u8 freq[8];
2325
  char broadcast_station_name[6];
2326
  u8 unknown[2];
2327
} fm_radio_preset[16];
2328

    
2329
#seekto 0x3200;
2330
struct {
2331
  u8 tmr;
2332
  u8 unknown1;
2333
  u8 sql;
2334
  u8 unknown2;
2335
  u8 autolk;
2336
  u8 tot;
2337
  u8 apo;
2338
  u8 unknown3;
2339
  u8 abr;
2340
  u8 beep;
2341
  u8 unknown4[4];
2342
  u8 dtmfst;
2343
  u8 unknown5[2];
2344
  u8 screv;
2345
  u8 unknown6[2];
2346
  u8 pttid;
2347
  u8 pttlt;
2348
  u8 unknown7;
2349
  u8 emctp;
2350
  u8 emcch;
2351
  u8 sigbp;
2352
  u8 unknown8;
2353
  u8 camdf;
2354
  u8 cbmdf;
2355
  u8 ccmdf;
2356
  u8 cdmdf;
2357
  u8 langua;
2358
  u8 sync;
2359

    
2360

    
2361
  u8 stfc;
2362
  u8 mffc;
2363
  u8 sfafc;
2364
  u8 sfbfc;
2365
  u8 sfcfc;
2366
  u8 sfdfc;
2367
  u8 subfc;
2368
  u8 fmfc;
2369
  u8 sigfc;
2370
  u8 modfc;
2371
  u8 menufc;
2372
  u8 txfc;
2373
  u8 txdisp;
2374
  u8 unknown9[5];
2375
  u8 anil;
2376
  u8 reps;
2377
  u8 repm;
2378
  u8 tmrmr;
2379
  u8 ste;
2380
  u8 rpste;
2381
  u8 rptdl;
2382
  u8 dtmfg;
2383
  u8 mgain;
2384
  u8 skiptx;
2385
  u8 scmode;
2386
} settings;
2387

    
2388
#seekto 0x3280;
2389
struct {
2390
  u8 unknown1;
2391
  u8 vfomr;
2392
  u8 keylock;
2393
  u8 unknown2;
2394
  u8 unknown3:4,
2395
     vfomren:1,
2396
     unknown4:1,
2397
     reseten:1,
2398
     menuen:1;
2399
  u8 unknown5[11];
2400
  u8 dispab;
2401
  u8 unknown6[2];
2402
  u8 smenu;
2403
  u8 unknown7[7];
2404
  u8 vfomra;
2405
  u8 vfomrb;
2406
  u8 vfomrc;
2407
  u8 vfomrd;
2408
  u8 mrcha;
2409
  u8 mrchb;
2410
  u8 mrchc;
2411
  u8 mrchd;
2412
} settings2;
2413

    
2414
struct settings_vfo {
2415
  u8 freq[8];
2416
  u8 offset[6];
2417
  u8 unknown2[2];
2418
  ul16 rxtone;
2419
  ul16 txtone;
2420
  u8 scode;
2421
  u8 spmute;
2422
  u8 optsig;
2423
  u8 scramble;
2424
  u8 wide;
2425
  u8 power;
2426
  u8 shiftd;
2427
  u8 step;
2428
  u8 unknown3[4];
2429
};
2430

    
2431
#seekto 0x3300;
2432
struct {
2433
  struct settings_vfo a;
2434
  struct settings_vfo b;
2435
  struct settings_vfo c;
2436
  struct settings_vfo d;
2437
} vfo;
2438

    
2439
#seekto 0x3D80;
2440
struct {
2441
  u8 vhf_low[3];
2442
  u8 vhf_high[3];
2443
  u8 unknown1[4];
2444
  u8 unknown2[6];
2445
  u8 vhf2_low[3];
2446
  u8 vhf2_high[3];
2447
  u8 unknown3[4];
2448
  u8 unknown4[6];
2449
  u8 uhf_low[3];
2450
  u8 uhf_high[3];
2451
  u8 unknown5[4];
2452
  u8 unknown6[6];
2453
  u8 uhf2_low[3];
2454
  u8 uhf2_high[3];
2455
} ranges;
2456

    
2457
#seekto 0x33B0;
2458
struct {
2459
  char line[16];
2460
} static_msg;
2461

    
2462
#seekto 0x3F70;
2463
struct {
2464
  char fp[6];
2465
} fingerprint;
2466

    
2467
"""
2468

    
2469

    
2470
class BTechGMRS(BTechMobileCommon):
2471
    """BTECH's GMRS Mobile"""
2472
    COLOR_LCD = True
2473
    COLOR_LCD2 = True
2474
    NAME_LENGTH = 7
2475
    UPLOAD_MEM_SIZE = 0X3400
2476

    
2477
    def process_mmap(self):
2478
        """Process the mem map into the mem object"""
2479

    
2480
        # Get it
2481
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
2482

    
2483
        # load specific parameters from the radio image
2484
        self.set_options()
2485

    
2486
    def set_options(self):
2487
        """This is to read the options from the image and set it in the
2488
        environment, for now just the limits of the freqs in the VHF/UHF
2489
        ranges"""
2490

    
2491
        # setting the correct ranges for each radio type
2492
        ranges = self._memobj.ranges
2493

    
2494
        # the normal dual bands
2495
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
2496
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
2497

    
2498
        # DEBUG
2499
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
2500
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
2501

    
2502
        # set the class with the real data
2503
        self._vhf_range = vhf
2504
        self._uhf_range = uhf
2505

    
2506

    
2507
# real radios
2508
@directory.register
2509
class GMRS50X1(BTechGMRS):
2510
    """Baofeng Tech GMRS50X1"""
2511
    MODEL = "GMRS-50X1"
2512
    BANDS = 2
2513
    LIST_TMR = LIST_TMR16
2514
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
2515
                     chirp_common.PowerLevel("Mid", watts=10),
2516
                     chirp_common.PowerLevel("Low", watts=5)]
2517
    _vhf_range = (136000000, 175000000)
2518
    _uhf_range = (400000000, 521000000)
2519
    _upper = 255
2520
    _magic = MSTRING_GMRS50X1
2521
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
2522

    
2523

    
2524
COLORHT_MEM_FORMAT = """
2525
#seekto 0x0000;
2526
struct {
2527
  lbcd rxfreq[4];
2528
  lbcd txfreq[4];
2529
  ul16 rxtone;
2530
  ul16 txtone;
2531
  u8 unknown0:4,
2532
     scode:4;
2533
  u8 unknown1:2,
2534
     spmute:2,
2535
     unknown2:2,
2536
     optsig:2;
2537
  u8 unknown3:3,
2538
     scramble:1,
2539
     unknown4:3,
2540
     power:1;
2541
  u8 unknown5:1,
2542
     wide:1,
2543
     unknown6:2,
2544
     bcl:1,
2545
     add:1,
2546
     pttid:2;
2547
} memory[200];
2548

    
2549
#seekto 0x0E00;
2550
struct {
2551
  u8 tmr;
2552
  u8 unknownE01;
2553
  u8 sql;
2554
  u8 unknownE03[2];
2555
  u8 tot;
2556
  u8 save;
2557
  u8 unknownE07;
2558
  u8 abr;
2559
  u8 beep;
2560
  u8 unknownE0A[4];
2561
  u8 dsub;
2562
  u8 dtmfst;
2563
  u8 screv;
2564
  u8 unknownE11[3];
2565
  u8 pttid;
2566
  u8 unknownE15;
2567
  u8 pttlt;
2568
  u8 unknownE17;
2569
  u8 emctp;
2570
  u8 emcch;
2571
  u8 sigbp;
2572
  u8 unknownE1B;
2573
  u8 camdf;
2574
  u8 cbmdf;
2575
  u8 ccmdf;
2576
  u8 cdmdf;
2577
  u8 langua;
2578
  u8 voice;
2579
  u8 vox;
2580
  u8 voxt;
2581
  u8 sync;          // BTech radios use this as the display sync setting
2582
                    // other radios use this as the auto keypad lock setting
2583
  u8 stfc;
2584
  u8 mffc;
2585
  u8 sfafc;
2586
  u8 sfbfc;
2587
  u8 sfcfc;
2588
  u8 sfdfc;
2589
  u8 subfc;
2590
  u8 fmfc;
2591
  u8 sigfc;
2592
  u8 menufc;
2593
  u8 txfc;
2594
  u8 rxfc;
2595
  u8 unknownE31[5];
2596
  u8 anil;
2597
  u8 reps;
2598
  u8 tmrmr;
2599
  u8 ste;
2600
  u8 rpste;
2601
  u8 rptdl;
2602
  u8 dtmfg;
2603
  u8 tmrtx;
2604
} settings;
2605

    
2606
#seekto 0x0E80;
2607
struct {
2608
  u8 unknown1;
2609
  u8 vfomr;
2610
  u8 keylock;
2611
  u8 unknown2;
2612
  u8 unknown3:4,
2613
     vfomren:1,
2614
     unknown4:1,
2615
     reseten:1,
2616
     menuen:1;
2617
  u8 unknown5[11];
2618
  u8 dispab;
2619
  u8 unknown6[2];
2620
  u8 menu;
2621
  u8 unknown7[7];
2622
  u8 vfomra;
2623
  u8 vfomrb;
2624
  u8 vfomrc;
2625
  u8 vfomrd;
2626
  u8 mrcha;
2627
  u8 mrchb;
2628
  u8 mrchc;
2629
  u8 mrchd;
2630
} settings2;
2631

    
2632
struct settings_vfo {
2633
  u8 freq[8];
2634
  u8 offset[6];
2635
  u8 unknown2[2];
2636
  ul16 rxtone;
2637
  ul16 txtone;
2638
  u8 scode;
2639
  u8 spmute;
2640
  u8 optsig;
2641
  u8 scramble;
2642
  u8 wide;
2643
  u8 power;
2644
  u8 shiftd;
2645
  u8 step;
2646
  u8 unknown3[4];
2647
};
2648

    
2649
#seekto 0x0F00;
2650
struct {
2651
  struct settings_vfo a;
2652
  struct settings_vfo b;
2653
  struct settings_vfo c;
2654
  struct settings_vfo d;
2655
} vfo;
2656

    
2657
#seekto 0x0FE0;
2658
struct {
2659
  char line[16];
2660
} static_msg;
2661

    
2662
#seekto 0x1000;
2663
struct {
2664
  char name[8];
2665
  u8 unknown1[8];
2666
} names[200];
2667

    
2668
#seekto 0x2400;
2669
struct {
2670
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
2671
  u8 group_tone;
2672
  u8 repeat_tone;
2673
  u8 unused[13];
2674
} _5tone_std_settings[15];
2675

    
2676
#seekto 0x2500;
2677
struct {
2678
  u8 frame1[5];
2679
  u8 frame2[5];
2680
  u8 frame3[5];
2681
  u8 standard;   // one out of LIST_5TONE_STANDARDS
2682
} _5tone_codes[15];
2683

    
2684
#seekto 0x25F0;
2685
struct {
2686
  u8 _5tone_delay1; // * 10ms
2687
  u8 _5tone_delay2; // * 10ms
2688
  u8 _5tone_delay3; // * 10ms
2689
  u8 _5tone_first_digit_ext_length;
2690
  u8 unknown1;
2691
  u8 unknown2;
2692
  u8 unknown3;
2693
  u8 unknown4;
2694
  u8 decode_standard;
2695
  u8 unknown5:5,
2696
     _5tone_decode_call_frame3:1,
2697
     _5tone_decode_call_frame2:1,
2698
     _5tone_decode_call_frame1:1;
2699
  u8 unknown6:5,
2700
     _5tone_decode_disp_frame3:1,
2701
     _5tone_decode_disp_frame2:1,
2702
     _5tone_decode_disp_frame1:1;
2703
  u8 decode_reset_time; // * 100 + 100ms
2704
} _5tone_settings;
2705

    
2706
#seekto 0x2900;
2707
struct {
2708
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
2709
} dtmf_codes[15];
2710

    
2711
#seekto 0x29F0;
2712
struct {
2713
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
2714
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
2715
  u8 unknown0[14];
2716
  u8 inspection[16];
2717
  u8 monitor[16];
2718
  u8 alarmcode[16];
2719
  u8 stun[16];
2720
  u8 kill[16];
2721
  u8 revive[16];
2722
  u8 unknown1[16];
2723
  u8 unknown2[16];
2724
  u8 unknown3[16];
2725
  u8 unknown4[16];
2726
  u8 unknown5[16];
2727
  u8 unknown6[16];
2728
  u8 unknown7[16];
2729
  u8 masterid[16];
2730
  u8 viceid[16];
2731
  u8 unused01:7,
2732
     mastervice:1;
2733
  u8 unused02:3,
2734
     mrevive:1,
2735
     mkill:1,
2736
     mstun:1,
2737
     mmonitor:1,
2738
     minspection:1;
2739
  u8 unused03:3,
2740
     vrevive:1,
2741
     vkill:1,
2742
     vstun:1,
2743
     vmonitor:1,
2744
     vinspection:1;
2745
  u8 unused04:6,
2746
     txdisable:1,
2747
     rxdisable:1;
2748
  u8 groupcode;
2749
  u8 spacecode;
2750
  u8 delayproctime; // * 100 + 100ms
2751
  u8 resettime;     // * 100 + 100ms
2752
} dtmf_settings;
2753

    
2754
#seekto 0x2D00;
2755
struct {
2756
  struct {
2757
    ul16 freq1;
2758
    u8 unused01[6];
2759
    ul16 freq2;
2760
    u8 unused02[6];
2761
  } _2tone_encode[15];
2762
  u8 duration_1st_tone; // *10ms
2763
  u8 duration_2nd_tone; // *10ms
2764
  u8 duration_gap;      // *10ms
2765
  u8 unused03[13];
2766
  struct {
2767
    struct {
2768
      u8 dec;      // one out of LIST_2TONE_DEC
2769
      u8 response; // one out of LIST_2TONE_RESPONSE
2770
      u8 alert;    // 1-16
2771
    } decs[4];
2772
    u8 unused04[4];
2773
  } _2tone_decode[15];
2774
  u8 unused05[16];
2775

    
2776
  struct {
2777
    ul16 freqA;
2778
    ul16 freqB;
2779
    ul16 freqC;
2780
    ul16 freqD;
2781
    // unknown what those values mean, but they are
2782
    // derived from configured frequencies
2783
    ul16 derived_from_freqA; // 2304000/freqA
2784
    ul16 derived_from_freqB; // 2304000/freqB
2785
    ul16 derived_from_freqC; // 2304000/freqC
2786
    ul16 derived_from_freqD; // 2304000/freqD
2787
  }freqs[15];
2788
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
2789
} _2tone;
2790

    
2791
#seekto 0x3D80;
2792
struct {
2793
  u8 vhf_low[3];
2794
  u8 vhf_high[3];
2795
  u8 unknown1[4];
2796
  u8 unknown2[6];
2797
  u8 vhf2_low[3];
2798
  u8 vhf2_high[3];
2799
  u8 unknown3[4];
2800
  u8 unknown4[6];
2801
  u8 uhf_low[3];
2802
  u8 uhf_high[3];
2803
  u8 unknown5[4];
2804
  u8 unknown6[6];
2805
  u8 uhf2_low[3];
2806
  u8 uhf2_high[3];
2807
} ranges;
2808

    
2809
#seekto 0x3F70;
2810
struct {
2811
  char fp[6];
2812
} fingerprint;
2813

    
2814
"""
2815

    
2816

    
2817
class QYTColorHT(BTechMobileCommon):
2818
    """QTY's Color LCD Handheld and alike radios"""
2819
    COLOR_LCD = True
2820
    COLOR_LCD3 = True
2821
    NAME_LENGTH = 8
2822
    LIST_TMR = LIST_TMR15
2823

    
2824
    def process_mmap(self):
2825
        """Process the mem map into the mem object"""
2826

    
2827
        # Get it
2828
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
2829

    
2830
        # load specific parameters from the radio image
2831
        self.set_options()
2832

    
2833
    def set_options(self):
2834
        """This is to read the options from the image and set it in the
2835
        environment, for now just the limits of the freqs in the VHF/UHF
2836
        ranges"""
2837

    
2838
        # setting the correct ranges for each radio type
2839
        ranges = self._memobj.ranges
2840

    
2841
        # the normal dual bands
2842
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
2843
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
2844

    
2845
        # DEBUG
2846
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
2847
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
2848

    
2849
        # the additional bands
2850
        if self.MODEL in ["KT-8R"]:
2851
            # 200Mhz band
2852
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
2853
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
2854
            self._220_range = vhf2
2855

    
2856
            # 350Mhz band
2857
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
2858
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
2859
            self._350_range = uhf2
2860

    
2861
        # set the class with the real data
2862
        self._vhf_range = vhf
2863
        self._uhf_range = uhf
2864

    
2865

    
2866
# real radios
2867
@directory.register
2868
class KT8R(QYTColorHT):
2869
    """QYT KT8R"""
2870
    VENDOR = "QYT"
2871
    MODEL = "KT-8R"
2872
    BANDS = 4
2873
    LIST_TMR = LIST_TMR16
2874
    _vhf_range = (136000000, 175000000)
2875
    _220_range = (200000000, 261000000)
2876
    _uhf_range = (400000000, 481000000)
2877
    _350_range = (350000000, 391000000)
2878
    _magic = MSTRING_KT8R
2879
    _fileid = [KT8R_fp2, KT8R_fp1, KT8R_fp, ]
2880
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
2881
                     chirp_common.PowerLevel("Low", watts=1)]
(8-8/16)