Project

General

Profile

Bug #9139 » btech_vc3402.py

Test driver module which includes the VC3402 MCU version - Jim Unroe, 06/17/2021 05:17 AM

 
1
# Copyright 2016-2020:
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, 10)]
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
# Practice has show that about 0.7 sec will be enough to cover all radios.
137
STIMEOUT = 0.7
138

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

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

    
147

    
148
# #### ID strings #####################################################
149

    
150
# BTECH UV2501 pre-production units
151
UV2501pp_fp = "M2C294"
152
# BTECH UV2501 pre-production units 2 + and 1st Gen radios
153
UV2501pp2_fp = "M29204"
154
# B-TECH UV-2501 second generation (2G) radios
155
UV2501G2_fp = "BTG214"
156
# B-TECH UV-2501 third generation (3G) radios
157
UV2501G3_fp = "BTG324"
158

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

    
171
# B-TECH UV-5001 pre-production units + 1st Gen radios
172
UV5001pp_fp = "V19204"
173
# B-TECH UV-5001 alpha units
174
UV5001alpha_fp = "V28204"
175
# B-TECH UV-5001 second generation (2G) radios
176
UV5001G2_fp = "BTG214"
177
# B-TECH UV-5001 second generation (2G2)
178
UV5001G22_fp = "V2G204"
179
# B-TECH UV-5001 third generation (3G)
180
UV5001G3_fp = "BTG304"
181

    
182
# B-TECH UV-25X2
183
UV25X2_fp = "UC2012"
184

    
185
# B-TECH UV-25X4
186
UV25X4_fp = "UC4014"
187

    
188
# B-TECH UV-50X2
189
UV50X2_fp = "UC2M12"
190

    
191
# B-TECH GMRS-50X1
192
GMRS50X1_fp = "NC1802"
193
GMRS50X1_fp1 = "NC1932"
194

    
195
# special var to know when we found a BTECH Gen 3
196
BTECH3 = [UV2501G3_fp, UV2501_220G3_fp, UV5001G3_fp]
197

    
198

    
199
# WACCOM Mini-8900
200
MINI8900_fp = "M28854"
201

    
202

    
203
# QYT KT-UV980
204
KTUV980_fp = "H28854"
205

    
206
# QYT KT8900
207
KT8900_fp = "M29154"
208
# New generations KT8900
209
KT8900_fp1 = "M2C234"
210
KT8900_fp2 = "M2G1F4"
211
KT8900_fp3 = "M2G2F4"
212
KT8900_fp4 = "M2G304"
213
KT8900_fp5 = "M2G314"
214
KT8900_fp6 = "M2G424"
215
# this radio has an extra ID
216
KT8900_id = "303688"
217

    
218
# KT8900R
219
KT8900R_fp = "M3G1F4"
220
# Second Generation
221
KT8900R_fp1 = "M3G214"
222
# another model
223
KT8900R_fp2 = "M3C234"
224
# another model G4?
225
KT8900R_fp3 = "M39164"
226
# another model
227
KT8900R_fp4 = "M3G314"
228
# this radio has an extra ID
229
KT8900R_id = "280528"
230
# another extra ID in dec/2018
231
KT8900R_id2 = "\x05\x58\x3d\xf0\x10"
232

    
233
# KT7900D (quad band)
234
KT7900D_fp = "VC4004"
235
KT7900D_fp1 = "VC4284"
236
KT7900D_fp2 = "VC4264"
237
KT7900D_fp3 = "VC4114"
238
KT7900D_fp4 = "VC4104"
239
KT7900D_fp5 = "VC4254"
240
KT7900D_fp6 = "VC5264"
241

    
242
# QB25 (quad band) - a clone of KT7900D
243
QB25_fp = "QB-25"
244

    
245
# KT8900D (dual band)
246
KT8900D_fp = "VC2002"
247
KT8900D_fp1 = "VC8632"
248
KT8900D_fp2 = "VC3402"
249

    
250
# LUITON LT-588UV
251
LT588UV_fp = "V2G1F4"
252
# Added by rstrickoff gen 2 id
253
LT588UV_fp1 = "V2G214"
254

    
255
# QYT KT-8R (quad band ht)
256
KT8R_fp = "MCB264"
257
KT8R_fp1 = "MCB284"
258
KT8R_fp2 = "MC5264"
259

    
260
# QYT KT5800 (dual band)
261
KT5800_fp = "VCB222"
262

    
263

    
264
# ### MAGICS
265
# for the Waccom Mini-8900
266
MSTRING_MINI8900 = "\x55\xA5\xB5\x45\x55\x45\x4d\x02"
267
# for the B-TECH UV-2501+220 (including pre production ones)
268
MSTRING_220 = "\x55\x20\x15\x12\x12\x01\x4d\x02"
269
# for the QYT KT8900 & R
270
MSTRING_KT8900 = "\x55\x20\x15\x09\x16\x45\x4D\x02"
271
MSTRING_KT8900R = "\x55\x20\x15\x09\x25\x01\x4D\x02"
272
# magic string for all other models
273
MSTRING = "\x55\x20\x15\x09\x20\x45\x4d\x02"
274
# for the QYT KT7900D & KT8900D
275
MSTRING_KT8900D = "\x55\x20\x16\x08\x01\xFF\xDC\x02"
276
# for the BTECH UV-25X2 and UV-50X2
277
MSTRING_UV25X2 = "\x55\x20\x16\x12\x28\xFF\xDC\x02"
278
# for the BTECH UV-25X4
279
MSTRING_UV25X4 = "\x55\x20\x16\x11\x18\xFF\xDC\x02"
280
# for the BTECH GMRS-50X1
281
MSTRING_GMRS50X1 = "\x55\x20\x18\x10\x18\xFF\xDC\x02"
282
# for the QYT KT-8R
283
MSTRING_KT8R = "\x55\x20\x17\x07\x03\xFF\xDC\x02"
284

    
285

    
286
def _clean_buffer(radio):
287
    """Cleaning the read serial buffer, hard timeout to survive an infinite
288
    data stream"""
289

    
290
    # touching the serial timeout to optimize the flushing
291
    # restored at the end to the default value
292
    radio.pipe.timeout = 0.1
293
    dump = "1"
294
    datacount = 0
295

    
296
    try:
297
        while len(dump) > 0:
298
            dump = radio.pipe.read(100)
299
            datacount += len(dump)
300
            # hard limit to survive a infinite serial data stream
301
            # 5 times bigger than a normal rx block (69 bytes)
302
            if datacount > 345:
303
                seriale = "Please check your serial port selection."
304
                raise errors.RadioError(seriale)
305

    
306
        # restore the default serial timeout
307
        radio.pipe.timeout = STIMEOUT
308

    
309
    except Exception:
310
        raise errors.RadioError("Unknown error cleaning the serial buffer")
311

    
312

    
313
def _rawrecv(radio, amount):
314
    """Raw read from the radio device, less intensive way"""
315

    
316
    data = ""
317

    
318
    try:
319
        data = radio.pipe.read(amount)
320

    
321
        # DEBUG
322
        if debug is True:
323
            LOG.debug("<== (%d) bytes:\n\n%s" %
324
                      (len(data), util.hexprint(data)))
325

    
326
        # fail if no data is received
327
        if len(data) == 0:
328
            raise errors.RadioError("No data received from radio")
329

    
330
        # notice on the logs if short
331
        if len(data) < amount:
332
            LOG.warn("Short reading %d bytes from the %d requested." %
333
                     (len(data), amount))
334

    
335
    except:
336
        raise errors.RadioError("Error reading data from radio")
337

    
338
    return data
339

    
340

    
341
def _send(radio, data):
342
    """Send data to the radio device"""
343

    
344
    try:
345
        for byte in data:
346
            radio.pipe.write(byte)
347
            # Some OS (mainly Linux ones) are too fast on the serial and
348
            # get the MCU inside the radio stuck in the early stages, this
349
            # hits some models more than others.
350
            #
351
            # To cope with that we introduce a delay on the writes.
352
            # Many option have been tested (delaying only after error occures,
353
            # after short reads, only for linux, ...)
354
            # Finally, a static delay was chosen as simplest of all solutions
355
            # (Michael Wagner, OE4AMW)
356
            # (for details, see issue 3993)
357
            sleep(0.002)
358

    
359
        # DEBUG
360
        if debug is True:
361
            LOG.debug("==> (%d) bytes:\n\n%s" %
362
                      (len(data), util.hexprint(data)))
363
    except:
364
        raise errors.RadioError("Error sending data to radio")
365

    
366

    
367
def _make_frame(cmd, addr, length, data=""):
368
    """Pack the info in the headder format"""
369
    frame = "\x06" + struct.pack(">BHB", ord(cmd), addr, length)
370
    # add the data if set
371
    if len(data) != 0:
372
        frame += data
373

    
374
    return frame
375

    
376

    
377
def _recv(radio, addr):
378
    """Get data from the radio all at once to lower syscalls load"""
379

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

    
383
    # get the whole block
384
    block = _rawrecv(radio, BLOCK_SIZE + 5)
385

    
386
    # basic check
387
    if len(block) < (BLOCK_SIZE + 5):
388
        raise errors.RadioError("Short read of the block 0x%04x" % addr)
389

    
390
    # checking for the ack
391
    if block[0] != ACK_CMD:
392
        raise errors.RadioError("Bad ack from radio in block 0x%04x" % addr)
393

    
394
    # header validation
395
    c, a, l = struct.unpack(">BHB", block[1:5])
396
    if a != addr or l != BLOCK_SIZE or c != ord("X"):
397
        LOG.debug("Invalid header for block 0x%04x" % addr)
398
        LOG.debug("CMD: %s  ADDR: %04x  SIZE: %02x" % (c, a, l))
399
        raise errors.RadioError("Invalid header for block 0x%04x:" % addr)
400

    
401
    # return the data
402
    return block[5:]
403

    
404

    
405
def _start_clone_mode(radio, status):
406
    """Put the radio in clone mode and get the ident string, 3 tries"""
407

    
408
    # cleaning the serial buffer
409
    _clean_buffer(radio)
410

    
411
    # prep the data to show in the UI
412
    status.cur = 0
413
    status.msg = "Identifying the radio..."
414
    status.max = 3
415
    radio.status_fn(status)
416

    
417
    try:
418
        for a in range(0, status.max):
419
            # Update the UI
420
            status.cur = a + 1
421
            radio.status_fn(status)
422

    
423
            # send the magic word
424
            _send(radio, radio._magic)
425

    
426
            # Now you get a x06 of ACK if all goes well
427
            ack = radio.pipe.read(1)
428

    
429
            if ack == "\x06":
430
                # DEBUG
431
                LOG.info("Magic ACK received")
432
                status.cur = status.max
433
                radio.status_fn(status)
434

    
435
                return True
436

    
437
        return False
438

    
439
    except errors.RadioError:
440
        raise
441
    except Exception, e:
442
        raise errors.RadioError("Error sending Magic to radio:\n%s" % e)
443

    
444

    
445
def _do_ident(radio, status, upload=False):
446
    """Put the radio in PROGRAM mode & identify it"""
447
    #  set the serial discipline
448
    radio.pipe.baudrate = 9600
449
    radio.pipe.parity = "N"
450

    
451
    # open the radio into program mode
452
    if _start_clone_mode(radio, status) is False:
453
        msg = "Radio did not enter clone mode"
454
        # warning about old versions of QYT KT8900
455
        if radio.MODEL == "KT8900":
456
            msg += ". You may want to try it as a WACCOM MINI-8900, there is a"
457
            msg += " known variant of this radios that is a clone of it."
458
        raise errors.RadioError(msg)
459

    
460
    # Ok, get the ident string
461
    ident = _rawrecv(radio, 49)
462

    
463
    # basic check for the ident
464
    if len(ident) != 49:
465
        raise errors.RadioError("Radio send a short ident block.")
466

    
467
    # check if ident is OK
468
    itis = False
469
    for fp in radio._fileid:
470
        if fp in ident:
471
            # got it!
472
            itis = True
473
            # checking if we are dealing with a Gen 3 BTECH
474
            if radio.VENDOR == "BTECH" and fp in BTECH3:
475
                radio.btech3 = True
476

    
477
            break
478

    
479
    if itis is False:
480
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
481
        raise errors.RadioError("Radio identification failed.")
482

    
483
    # some radios needs a extra read and check for a code on it, this ones
484
    # has the check value in the _id2 var, others simply False
485
    if radio._id2 is not False:
486
        # lower the timeout here as this radios are reseting due to timeout
487
        radio.pipe.timeout = 0.05
488

    
489
        # query & receive the extra ID
490
        _send(radio, _make_frame("S", 0x3DF0, 16))
491
        id2 = _rawrecv(radio, 21)
492

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

    
500
        # ok, the correct string must be in the received data
501
        # the radio._id2 var will be always a list
502
        flag2 = False
503
        for _id2 in radio._id2:
504
            if _id2 in id2:
505
                flag2 = True
506

    
507
        if not flag2:
508
            LOG.debug("Full *BAD* extra ID on the %s is: \n%s" %
509
                      (radio.MODEL, util.hexprint(id2)))
510
            raise errors.RadioError("The extra ID is wrong, aborting.")
511

    
512
        # this radios need a extra request/answer here on the upload
513
        # the amount of data received depends of the radio type
514
        #
515
        # also the first block of TX must no have the ACK at the beginning
516
        # see _upload for this.
517
        if upload is True:
518
            # send an ACK
519
            _send(radio, ACK_CMD)
520

    
521
            # the amount of data depend on the radio, so far we have two radios
522
            # reading two bytes with an ACK at the end and just ONE with just
523
            # one byte (QYT KT8900)
524
            # the JT-6188 appears a clone of the last, but reads TWO bytes.
525
            #
526
            # we will read two bytes with a custom timeout to not penalize the
527
            # users for this.
528
            #
529
            # we just check for a response and last byte being a ACK, that is
530
            # the common stone for all radios (3 so far)
531
            ack = _rawrecv(radio, 2)
532

    
533
            # checking
534
            if len(ack) == 0 or ack[-1:] != ACK_CMD:
535
                raise errors.RadioError("Radio didn't ACK the upload")
536

    
537
            # restore the default serial timeout
538
            radio.pipe.timeout = STIMEOUT
539

    
540
    # DEBUG
541
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
542

    
543
    return True
544

    
545

    
546
def _download(radio):
547
    """Get the memory map"""
548

    
549
    # UI progress
550
    status = chirp_common.Status()
551

    
552
    # put radio in program mode and identify it
553
    _do_ident(radio, status)
554

    
555
    # the models that doesn't have the extra ID have to make a dummy read here
556
    if radio._id2 is False:
557
        _send(radio, _make_frame("S", 0, BLOCK_SIZE))
558
        discard = _rawrecv(radio, BLOCK_SIZE + 5)
559

    
560
        if debug is True:
561
            LOG.info("Dummy first block read done, got this:\n\n %s",
562
                     util.hexprint(discard))
563

    
564
    # reset the progress bar in the UI
565
    status.max = MEM_SIZE / BLOCK_SIZE
566
    status.msg = "Cloning from radio..."
567
    status.cur = 0
568
    radio.status_fn(status)
569

    
570
    # cleaning the serial buffer
571
    _clean_buffer(radio)
572

    
573
    data = ""
574
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
575
        # sending the read request
576
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
577

    
578
        # read
579
        d = _recv(radio, addr)
580

    
581
        # aggregate the data
582
        data += d
583

    
584
        # UI Update
585
        status.cur = addr / BLOCK_SIZE
586
        status.msg = "Cloning from radio..."
587
        radio.status_fn(status)
588

    
589
    return data
590

    
591

    
592
def _upload(radio):
593
    """Upload procedure"""
594

    
595
    # The UPLOAD mem is restricted to lower than 0x3100,
596
    # so we will overide that here localy
597
    MEM_SIZE = radio.UPLOAD_MEM_SIZE
598

    
599
    # UI progress
600
    status = chirp_common.Status()
601

    
602
    # put radio in program mode and identify it
603
    _do_ident(radio, status, True)
604

    
605
    # get the data to upload to radio
606
    data = radio.get_mmap()
607

    
608
    # Reset the UI progress
609
    status.max = MEM_SIZE / TX_BLOCK_SIZE
610
    status.cur = 0
611
    status.msg = "Cloning to radio..."
612
    radio.status_fn(status)
613

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

    
618
    # cleaning the serial buffer
619
    _clean_buffer(radio)
620

    
621
    # the fun start here
622
    for addr in range(0, MEM_SIZE, TX_BLOCK_SIZE):
623
        # getting the block of data to send
624
        d = data[addr:addr + TX_BLOCK_SIZE]
625

    
626
        # build the frame to send
627
        frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
628

    
629
        # first block must not send the ACK at the beginning for the
630
        # ones that has the extra id, since this have to do a extra step
631
        if addr == 0 and radio._id2 is not False:
632
            frame = frame[1:]
633

    
634
        # send the frame
635
        _send(radio, frame)
636

    
637
        # receiving the response
638
        ack = _rawrecv(radio, 1)
639

    
640
        # basic check
641
        if len(ack) != 1:
642
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
643

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

    
647
        # UI Update
648
        status.cur = addr / TX_BLOCK_SIZE
649
        status.msg = "Cloning to radio..."
650
        radio.status_fn(status)
651

    
652

    
653
def model_match(cls, data):
654
    """Match the opened/downloaded image to the correct version"""
655
    rid = data[0x3f70:0x3f76]
656

    
657
    if rid in cls._fileid:
658
        return True
659

    
660
    return False
661

    
662

    
663
def _decode_ranges(low, high):
664
    """Unpack the data in the ranges zones in the memmap and return
665
    a tuple with the integer corresponding to the Mhz it means"""
666
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
667
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
668
    ilow *= 1000000
669
    ihigh *= 1000000
670

    
671
    return (ilow, ihigh)
672

    
673

    
674
def _split(rf, f1, f2):
675
    """Returns False if the two freqs are in the same band (no split)
676
    or True otherwise"""
677

    
678
    # determine if the two freqs are in the same band
679
    for low, high in rf.valid_bands:
680
        if f1 >= low and f1 <= high and \
681
                f2 >= low and f2 <= high:
682
            # if the two freqs are on the same Band this is not a split
683
            return False
684

    
685
    # if you get here is because the freq pairs are split
686
    return True
687

    
688

    
689
class BTechMobileCommon(chirp_common.CloneModeRadio,
690
                        chirp_common.ExperimentalRadio):
691
    """BTECH's UV-5001 and alike radios"""
692
    VENDOR = "BTECH"
693
    MODEL = ""
694
    IDENT = ""
695
    BANDS = 2
696
    COLOR_LCD = False
697
    COLOR_LCD2 = False
698
    COLOR_LCD3 = False
699
    NAME_LENGTH = 6
700
    UPLOAD_MEM_SIZE = 0X3100
701
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
702
                     chirp_common.PowerLevel("Low", watts=10)]
703
    _vhf_range = (130000000, 180000000)
704
    _220_range = (200000000, 271000000)
705
    _uhf_range = (400000000, 521000000)
706
    _350_range = (350000000, 391000000)
707
    _upper = 199
708
    _magic = MSTRING
709
    _fileid = None
710
    _id2 = False
711
    btech3 = False
712

    
713
    @classmethod
714
    def get_prompts(cls):
715
        rp = chirp_common.RadioPrompts()
716
        rp.experimental = \
717
            ('This driver is experimental.\n'
718
             '\n'
719
             'Please keep a copy of your memories with the original software '
720
             'if you treasure them, this driver is new and may contain'
721
             ' bugs.\n'
722
             '\n'
723
             )
724
        rp.pre_download = _(dedent("""\
725
            Follow these instructions to download your info:
726

    
727
            1 - Turn off your radio
728
            2 - Connect your interface cable
729
            3 - Turn on your radio
730
            4 - Do the download of your radio data
731

    
732
            """))
733
        rp.pre_upload = _(dedent("""\
734
            Follow these instructions to upload your info:
735

    
736
            1 - Turn off your radio
737
            2 - Connect your interface cable
738
            3 - Turn on your radio
739
            4 - Do the upload of your radio data
740

    
741
            """))
742
        return rp
743

    
744
    def get_features(self):
745
        """Get the radio's features"""
746

    
747
        # we will use the following var as global
748
        global POWER_LEVELS
749

    
750
        rf = chirp_common.RadioFeatures()
751
        rf.has_settings = True
752
        rf.has_bank = False
753
        rf.has_tuning_step = False
754
        rf.can_odd_split = True
755
        rf.has_name = True
756
        rf.has_offset = True
757
        rf.has_mode = True
758
        rf.has_dtcs = True
759
        rf.has_rx_dtcs = True
760
        rf.has_dtcs_polarity = True
761
        rf.has_ctone = True
762
        rf.has_cross = True
763
        rf.valid_modes = MODES
764
        rf.valid_characters = VALID_CHARS
765
        rf.valid_name_length = self.NAME_LENGTH
766
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
767
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
768
        rf.valid_cross_modes = [
769
            "Tone->Tone",
770
            "DTCS->",
771
            "->DTCS",
772
            "Tone->DTCS",
773
            "DTCS->Tone",
774
            "->Tone",
775
            "DTCS->DTCS"]
776
        rf.valid_skips = SKIP_VALUES
777
        rf.valid_dtcs_codes = DTCS
778
        rf.valid_tuning_steps = STEPS
779
        rf.memory_bounds = (0, self._upper)
780

    
781
        # power levels
782
        POWER_LEVELS = self._power_levels
783
        rf.valid_power_levels = POWER_LEVELS
784

    
785
        # normal dual bands
786
        rf.valid_bands = [self._vhf_range, self._uhf_range]
787

    
788
        # 220 band
789
        if self.BANDS == 3 or self.BANDS == 4:
790
            rf.valid_bands.append(self._220_range)
791

    
792
        # 350 band
793
        if self.BANDS == 4:
794
            rf.valid_bands.append(self._350_range)
795

    
796
        return rf
797

    
798
    def sync_in(self):
799
        """Download from radio"""
800
        data = _download(self)
801
        self._mmap = memmap.MemoryMap(data)
802
        self.process_mmap()
803

    
804
    def sync_out(self):
805
        """Upload to radio"""
806
        try:
807
            _upload(self)
808
        except errors.RadioError:
809
            raise
810
        except Exception, e:
811
            raise errors.RadioError("Error: %s" % e)
812

    
813
    def get_raw_memory(self, number):
814
        return repr(self._memobj.memory[number])
815

    
816
    def _decode_tone(self, val):
817
        """Parse the tone data to decode from mem, it returns:
818
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
819
        pol = None
820

    
821
        if val in [0, 65535]:
822
            return '', None, None
823
        elif val > 0x0258:
824
            a = val / 10.0
825
            return 'Tone', a, pol
826
        else:
827
            if val > 0x69:
828
                index = val - 0x6A
829
                pol = "R"
830
            else:
831
                index = val - 1
832
                pol = "N"
833

    
834
            tone = DTCS[index]
835
            return 'DTCS', tone, pol
836

    
837
    def _encode_tone(self, memval, mode, val, pol):
838
        """Parse the tone data to encode from UI to mem"""
839
        if mode == '' or mode is None:
840
            memval.set_raw("\x00\x00")
841
        elif mode == 'Tone':
842
            memval.set_value(val * 10)
843
        elif mode == 'DTCS':
844
            # detect the index in the DTCS list
845
            try:
846
                index = DTCS.index(val)
847
                if pol == "N":
848
                    index += 1
849
                else:
850
                    index += 0x6A
851
                memval.set_value(index)
852
            except:
853
                msg = "Digital Tone '%d' is not supported" % value
854
                LOG.error(msg)
855
                raise errors.RadioError(msg)
856
        else:
857
            msg = "Internal error: invalid mode '%s'" % mode
858
            LOG.error(msg)
859
            raise errors.InvalidDataError(msg)
860

    
861
    def get_memory(self, number):
862
        """Get the mem representation from the radio image"""
863
        _mem = self._memobj.memory[number]
864
        _names = self._memobj.names[number]
865

    
866
        # Create a high-level memory object to return to the UI
867
        mem = chirp_common.Memory()
868

    
869
        # Memory number
870
        mem.number = number
871

    
872
        if _mem.get_raw()[0] == "\xFF":
873
            mem.empty = True
874
            return mem
875

    
876
        # Freq and offset
877
        mem.freq = int(_mem.rxfreq) * 10
878
        # tx freq can be blank
879
        if _mem.get_raw()[4] == "\xFF":
880
            # TX freq not set
881
            mem.offset = 0
882
            mem.duplex = "off"
883
        else:
884
            # TX freq set
885
            offset = (int(_mem.txfreq) * 10) - mem.freq
886
            if offset != 0:
887
                if _split(self.get_features(), mem.freq, int(
888
                          _mem.txfreq) * 10):
889
                    mem.duplex = "split"
890
                    mem.offset = int(_mem.txfreq) * 10
891
                elif offset < 0:
892
                    mem.offset = abs(offset)
893
                    mem.duplex = "-"
894
                elif offset > 0:
895
                    mem.offset = offset
896
                    mem.duplex = "+"
897
            else:
898
                mem.offset = 0
899

    
900
        # name TAG of the channel
901
        mem.name = str(_names.name).rstrip("\xFF").replace("\xFF", " ")
902

    
903
        # power
904
        mem.power = POWER_LEVELS[int(_mem.power)]
905

    
906
        # wide/narrow
907
        mem.mode = MODES[int(_mem.wide)]
908

    
909
        # skip
910
        mem.skip = SKIP_VALUES[_mem.add]
911

    
912
        # tone data
913
        rxtone = txtone = None
914
        txtone = self._decode_tone(_mem.txtone)
915
        rxtone = self._decode_tone(_mem.rxtone)
916
        chirp_common.split_tone_decode(mem, txtone, rxtone)
917

    
918
        # Extra
919
        mem.extra = RadioSettingGroup("extra", "Extra")
920

    
921
        if not self.COLOR_LCD or \
922
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
923
            scramble = RadioSetting("scramble", "Scramble",
924
                                    RadioSettingValueBoolean(bool(
925
                                        _mem.scramble)))
926
            mem.extra.append(scramble)
927

    
928
        bcl = RadioSetting("bcl", "Busy channel lockout",
929
                           RadioSettingValueBoolean(bool(_mem.bcl)))
930
        mem.extra.append(bcl)
931

    
932
        pttid = RadioSetting("pttid", "PTT ID",
933
                             RadioSettingValueList(PTTID_LIST,
934
                                                   PTTID_LIST[_mem.pttid]))
935
        mem.extra.append(pttid)
936

    
937
        # validating scode
938
        scode = _mem.scode if _mem.scode != 15 else 0
939
        pttidcode = RadioSetting("scode", "PTT ID signal code",
940
                                 RadioSettingValueList(
941
                                     PTTIDCODE_LIST,
942
                                     PTTIDCODE_LIST[scode]))
943
        mem.extra.append(pttidcode)
944

    
945
        optsig = RadioSetting("optsig", "Optional signaling",
946
                              RadioSettingValueList(
947
                                  OPTSIG_LIST,
948
                                  OPTSIG_LIST[_mem.optsig]))
949
        mem.extra.append(optsig)
950

    
951
        spmute = RadioSetting("spmute", "Speaker mute",
952
                              RadioSettingValueList(
953
                                  SPMUTE_LIST,
954
                                  SPMUTE_LIST[_mem.spmute]))
955
        mem.extra.append(spmute)
956

    
957
        return mem
958

    
959
    def set_memory(self, mem):
960
        """Set the memory data in the eeprom img from the UI"""
961
        # get the eprom representation of this channel
962
        _mem = self._memobj.memory[mem.number]
963
        _names = self._memobj.names[mem.number]
964

    
965
        mem_was_empty = False
966
        # same method as used in get_memory for determining if mem is empty
967
        # doing this BEFORE overwriting it with new values ...
968
        if _mem.get_raw()[0] == "\xFF":
969
            LOG.debug("This mem was empty before")
970
            mem_was_empty = True
971

    
972
        # if empty memmory
973
        if mem.empty:
974
            # the channel itself
975
            _mem.set_raw("\xFF" * 16)
976
            # the name tag
977
            _names.set_raw("\xFF" * 16)
978
            return
979

    
980
        if mem_was_empty:
981
            # Zero the whole memory if we're making it unempty for
982
            # the first time
983
            LOG.debug('Zeroing new memory')
984
            _mem.set_raw('\x00' * 16)
985

    
986
        # frequency
987
        _mem.rxfreq = mem.freq / 10
988

    
989
        # duplex
990
        if mem.duplex == "+":
991
            _mem.txfreq = (mem.freq + mem.offset) / 10
992
        elif mem.duplex == "-":
993
            _mem.txfreq = (mem.freq - mem.offset) / 10
994
        elif mem.duplex == "off":
995
            for i in _mem.txfreq:
996
                i.set_raw("\xFF")
997
        elif mem.duplex == "split":
998
            _mem.txfreq = mem.offset / 10
999
        else:
1000
            _mem.txfreq = mem.freq / 10
1001

    
1002
        # tone data
1003
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
1004
            chirp_common.split_tone_encode(mem)
1005
        self._encode_tone(_mem.txtone, txmode, txtone, txpol)
1006
        self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)
1007

    
1008
        # name TAG of the channel
1009
        if len(mem.name) < self.NAME_LENGTH:
1010
            # we must pad to self.NAME_LENGTH chars, " " = "\xFF"
1011
            mem.name = str(mem.name).ljust(self.NAME_LENGTH, " ")
1012
        _names.name = str(mem.name).replace(" ", "\xFF")
1013

    
1014
        # power, # default power level is high
1015
        _mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)
1016

    
1017
        # wide/narrow
1018
        _mem.wide = MODES.index(mem.mode)
1019

    
1020
        # scan add property
1021
        _mem.add = SKIP_VALUES.index(mem.skip)
1022

    
1023
        # reseting unknowns, this have to be set by hand
1024
        _mem.unknown0 = 0
1025
        _mem.unknown1 = 0
1026
        _mem.unknown2 = 0
1027
        _mem.unknown3 = 0
1028
        _mem.unknown4 = 0
1029
        _mem.unknown5 = 0
1030
        _mem.unknown6 = 0
1031

    
1032
        def _zero_settings():
1033
            _mem.spmute = 0
1034
            _mem.optsig = 0
1035
            _mem.scramble = 0
1036
            _mem.bcl = 0
1037
            _mem.pttid = 0
1038
            _mem.scode = 0
1039

    
1040
        if self.COLOR_LCD and _mem.scramble:
1041
            LOG.info('Resetting scramble bit for BTECH COLOR_LCD variant')
1042
            _mem.scramble = 0
1043

    
1044
        # extra settings
1045
        if len(mem.extra) > 0:
1046
            # there are setting, parse
1047
            LOG.debug("Extra-Setting supplied. Setting them.")
1048
            # Zero them all first so any not provided by model don't
1049
            # stay set
1050
            _zero_settings()
1051
            for setting in mem.extra:
1052
                setattr(_mem, setting.get_name(), setting.value)
1053
        else:
1054
            if mem.empty:
1055
                LOG.debug("New mem is empty.")
1056
            else:
1057
                LOG.debug("New mem is NOT empty")
1058
                # set extra-settings to default ONLY when apreviously empty or
1059
                # deleted memory was edited to prevent errors such as #4121
1060
                if mem_was_empty:
1061
                    LOG.debug("old mem was empty. Setting default for extras.")
1062
                    _zero_settings()
1063

    
1064
        return mem
1065

    
1066
    def get_settings(self):
1067
        """Translate the bit in the mem_struct into settings in the UI"""
1068
        _mem = self._memobj
1069
        basic = RadioSettingGroup("basic", "Basic Settings")
1070
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
1071
        other = RadioSettingGroup("other", "Other Settings")
1072
        work = RadioSettingGroup("work", "Work Mode Settings")
1073
        top = RadioSettings(basic, advanced, other, work)
1074

    
1075
        # Basic
1076
        if self.COLOR_LCD:
1077
            tmr = RadioSetting("settings.tmr", "Transceiver multi-receive",
1078
                               RadioSettingValueList(
1079
                                   self.LIST_TMR,
1080
                                   self.LIST_TMR[_mem.settings.tmr]))
1081
            basic.append(tmr)
1082
        else:
1083
            tdr = RadioSetting("settings.tdr", "Transceiver dual receive",
1084
                               RadioSettingValueBoolean(_mem.settings.tdr))
1085
            basic.append(tdr)
1086

    
1087
        sql = RadioSetting("settings.sql", "Squelch level",
1088
                           RadioSettingValueInteger(0, 9, _mem.settings.sql))
1089
        basic.append(sql)
1090

    
1091
        if self.MODEL == "GMRS-50X1":
1092
            autolk = RadioSetting("settings.autolk", "Auto keylock",
1093
                                  RadioSettingValueBoolean(
1094
                                      _mem.settings.autolk))
1095
            basic.append(autolk)
1096

    
1097
        tot = RadioSetting("settings.tot", "Time out timer",
1098
                           RadioSettingValueList(
1099
                               LIST_TOT,
1100
                               LIST_TOT[_mem.settings.tot]))
1101
        basic.append(tot)
1102

    
1103
        if self.MODEL == "KT-8R":
1104
                save = RadioSetting("settings.save", "Battery Save",
1105
                                    RadioSettingValueBoolean(
1106
                                        _mem.settings.save))
1107
                basic.append(save)
1108

    
1109
        if not self.MODEL == "KT-8R":
1110
            if self.VENDOR == "BTECH" or self.COLOR_LCD:
1111
                apo = RadioSetting("settings.apo", "Auto power off timer",
1112
                                   RadioSettingValueList(
1113
                                       LIST_APO,
1114
                                       LIST_APO[_mem.settings.apo]))
1115
                basic.append(apo)
1116
            else:
1117
                toa = RadioSetting("settings.apo", "Time out alert timer",
1118
                                   RadioSettingValueList(
1119
                                       LIST_OFF1TO10,
1120
                                       LIST_OFF1TO10[_mem.settings.apo]))
1121
                basic.append(toa)
1122

    
1123
        abr = RadioSetting("settings.abr", "Backlight timer",
1124
                           RadioSettingValueList(
1125
                               LIST_OFF1TO50,
1126
                               LIST_OFF1TO50[_mem.settings.abr]))
1127
        basic.append(abr)
1128

    
1129
        beep = RadioSetting("settings.beep", "Key beep",
1130
                            RadioSettingValueBoolean(_mem.settings.beep))
1131
        basic.append(beep)
1132

    
1133
        if self.MODEL == "KT-8R":
1134
                dsub = RadioSetting("settings.dsub", "CTCSS/DCS code display",
1135
                                    RadioSettingValueBoolean(
1136
                                        _mem.settings.dsub))
1137
                basic.append(dsub)
1138

    
1139
        if self.MODEL == "KT-8R":
1140
                dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1141
                                      RadioSettingValueBoolean(
1142
                                          _mem.settings.dtmfst))
1143
                basic.append(dtmfst)
1144
        else:
1145
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1146
                                  RadioSettingValueList(
1147
                                      LIST_DTMFST,
1148
                                      LIST_DTMFST[_mem.settings.dtmfst]))
1149
            basic.append(dtmfst)
1150

    
1151
        if not self.COLOR_LCD:
1152
            prisc = RadioSetting("settings.prisc", "Priority scan",
1153
                                 RadioSettingValueBoolean(
1154
                                     _mem.settings.prisc))
1155
            basic.append(prisc)
1156

    
1157
            prich = RadioSetting("settings.prich", "Priority channel",
1158
                                 RadioSettingValueInteger(0, self._upper,
1159
                                                          _mem.settings.prich))
1160
            basic.append(prich)
1161

    
1162
        screv = RadioSetting("settings.screv", "Scan resume method",
1163
                             RadioSettingValueList(
1164
                                 LIST_SCREV,
1165
                                 LIST_SCREV[_mem.settings.screv]))
1166
        basic.append(screv)
1167

    
1168
        pttlt = RadioSetting("settings.pttlt", "PTT transmit delay",
1169
                             RadioSettingValueInteger(0, 30,
1170
                                                      _mem.settings.pttlt))
1171
        basic.append(pttlt)
1172

    
1173
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1174
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1175
                                 RadioSettingValueList(
1176
                                     LIST_EMCTPX,
1177
                                     LIST_EMCTPX[_mem.settings.emctp]))
1178
            basic.append(emctp)
1179
        else:
1180
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1181
                                 RadioSettingValueList(
1182
                                     LIST_EMCTP,
1183
                                     LIST_EMCTP[_mem.settings.emctp]))
1184
            basic.append(emctp)
1185

    
1186
        emcch = RadioSetting("settings.emcch", "Alarm channel",
1187
                             RadioSettingValueInteger(0, self._upper,
1188
                                                      _mem.settings.emcch))
1189
        basic.append(emcch)
1190

    
1191
        if self.COLOR_LCD:
1192
            if _mem.settings.sigbp > 0x01:
1193
                val = 0x00
1194
            else:
1195
                val = _mem.settings.sigbp
1196
            sigbp = RadioSetting("settings.sigbp", "Signal beep",
1197
                                 RadioSettingValueBoolean(val))
1198
            basic.append(sigbp)
1199
        else:
1200
            ringt = RadioSetting("settings.ringt", "Ring time",
1201
                                 RadioSettingValueList(
1202
                                     LIST_OFF1TO9,
1203
                                     LIST_OFF1TO9[_mem.settings.ringt]))
1204
            basic.append(ringt)
1205

    
1206
        camdf = RadioSetting("settings.camdf", "Display mode A",
1207
                             RadioSettingValueList(
1208
                                 LIST_MDF,
1209
                                 LIST_MDF[_mem.settings.camdf]))
1210
        basic.append(camdf)
1211

    
1212
        cbmdf = RadioSetting("settings.cbmdf", "Display mode B",
1213
                             RadioSettingValueList(
1214
                                 LIST_MDF,
1215
                                 LIST_MDF[_mem.settings.cbmdf]))
1216
        basic.append(cbmdf)
1217

    
1218
        if self.COLOR_LCD:
1219
            ccmdf = RadioSetting("settings.ccmdf", "Display mode C",
1220
                                 RadioSettingValueList(
1221
                                     LIST_MDF,
1222
                                     LIST_MDF[_mem.settings.ccmdf]))
1223
            basic.append(ccmdf)
1224

    
1225
            cdmdf = RadioSetting("settings.cdmdf", "Display mode D",
1226
                                 RadioSettingValueList(
1227
                                     LIST_MDF,
1228
                                     LIST_MDF[_mem.settings.cdmdf]))
1229
            basic.append(cdmdf)
1230

    
1231
            langua = RadioSetting("settings.langua", "Language",
1232
                                  RadioSettingValueList(
1233
                                      LIST_LANGUA,
1234
                                      LIST_LANGUA[_mem.settings.langua]))
1235
            basic.append(langua)
1236

    
1237
        if self.MODEL == "KT-8R":
1238
            voice = RadioSetting("settings.voice", "Voice prompt",
1239
                                 RadioSettingValueList(
1240
                                     LIST_VOICE,
1241
                                     LIST_VOICE[_mem.settings.voice]))
1242
            basic.append(voice)
1243

    
1244
            vox = RadioSetting("settings.vox", "VOX",
1245
                               RadioSettingValueList(
1246
                                   LIST_VOX,
1247
                                   LIST_VOX[_mem.settings.vox]))
1248
            basic.append(vox)
1249

    
1250
            voxt = RadioSetting("settings.voxt", "VOX delay time",
1251
                                RadioSettingValueList(
1252
                                    LIST_VOXT,
1253
                                    LIST_VOXT[_mem.settings.voxt]))
1254
            basic.append(voxt)
1255

    
1256
        if self.VENDOR == "BTECH":
1257
            if self.COLOR_LCD:
1258
                sync = RadioSetting("settings.sync", "Channel display sync",
1259
                                    RadioSettingValueList(
1260
                                        LIST_SYNC,
1261
                                        LIST_SYNC[_mem.settings.sync]))
1262
                basic.append(sync)
1263
            else:
1264
                sync = RadioSetting("settings.sync", "A/B channel sync",
1265
                                    RadioSettingValueBoolean(
1266
                                        _mem.settings.sync))
1267
                basic.append(sync)
1268
        else:
1269
            autolk = RadioSetting("settings.sync", "Auto keylock",
1270
                                  RadioSettingValueBoolean(
1271
                                      _mem.settings.sync))
1272
            basic.append(autolk)
1273

    
1274
        if not self.COLOR_LCD:
1275
            ponmsg = RadioSetting("settings.ponmsg", "Power-on message",
1276
                                  RadioSettingValueList(
1277
                                      LIST_PONMSG,
1278
                                      LIST_PONMSG[_mem.settings.ponmsg]))
1279
            basic.append(ponmsg)
1280

    
1281
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3):
1282
            mainfc = RadioSetting("settings.mainfc",
1283
                                  "Main LCD foreground color",
1284
                                  RadioSettingValueList(
1285
                                      LIST_COLOR9,
1286
                                      LIST_COLOR9[_mem.settings.mainfc]))
1287
            basic.append(mainfc)
1288

    
1289
            mainbc = RadioSetting("settings.mainbc",
1290
                                  "Main LCD background color",
1291
                                  RadioSettingValueList(
1292
                                      LIST_COLOR9,
1293
                                      LIST_COLOR9[_mem.settings.mainbc]))
1294
            basic.append(mainbc)
1295

    
1296
            menufc = RadioSetting("settings.menufc", "Menu foreground color",
1297
                                  RadioSettingValueList(
1298
                                      LIST_COLOR9,
1299
                                      LIST_COLOR9[_mem.settings.menufc]))
1300
            basic.append(menufc)
1301

    
1302
            menubc = RadioSetting("settings.menubc", "Menu background color",
1303
                                  RadioSettingValueList(
1304
                                      LIST_COLOR9,
1305
                                      LIST_COLOR9[_mem.settings.menubc]))
1306
            basic.append(menubc)
1307

    
1308
            stafc = RadioSetting("settings.stafc",
1309
                                 "Top status foreground color",
1310
                                 RadioSettingValueList(
1311
                                     LIST_COLOR9,
1312
                                     LIST_COLOR9[_mem.settings.stafc]))
1313
            basic.append(stafc)
1314

    
1315
            stabc = RadioSetting("settings.stabc",
1316
                                 "Top status background color",
1317
                                 RadioSettingValueList(
1318
                                     LIST_COLOR9,
1319
                                     LIST_COLOR9[_mem.settings.stabc]))
1320
            basic.append(stabc)
1321

    
1322
            sigfc = RadioSetting("settings.sigfc",
1323
                                 "Bottom status foreground color",
1324
                                 RadioSettingValueList(
1325
                                     LIST_COLOR9,
1326
                                     LIST_COLOR9[_mem.settings.sigfc]))
1327
            basic.append(sigfc)
1328

    
1329
            sigbc = RadioSetting("settings.sigbc",
1330
                                 "Bottom status background color",
1331
                                 RadioSettingValueList(
1332
                                     LIST_COLOR9,
1333
                                     LIST_COLOR9[_mem.settings.sigbc]))
1334
            basic.append(sigbc)
1335

    
1336
            rxfc = RadioSetting("settings.rxfc", "Receiving character color",
1337
                                RadioSettingValueList(
1338
                                    LIST_COLOR9,
1339
                                    LIST_COLOR9[_mem.settings.rxfc]))
1340
            basic.append(rxfc)
1341

    
1342
            txfc = RadioSetting("settings.txfc",
1343
                                "Transmitting character color",
1344
                                RadioSettingValueList(
1345
                                    LIST_COLOR9,
1346
                                    LIST_COLOR9[_mem.settings.txfc]))
1347
            basic.append(txfc)
1348

    
1349
            txdisp = RadioSetting("settings.txdisp",
1350
                                  "Transmitting status display",
1351
                                  RadioSettingValueList(
1352
                                      LIST_TXDISP,
1353
                                      LIST_TXDISP[_mem.settings.txdisp]))
1354
            basic.append(txdisp)
1355
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1356
            stfc = RadioSetting("settings.stfc",
1357
                                "ST-FC",
1358
                                RadioSettingValueList(
1359
                                    LIST_COLOR8,
1360
                                    LIST_COLOR8[_mem.settings.stfc]))
1361
            basic.append(stfc)
1362

    
1363
            mffc = RadioSetting("settings.mffc",
1364
                                "MF-FC",
1365
                                RadioSettingValueList(
1366
                                    LIST_COLOR8,
1367
                                    LIST_COLOR8[_mem.settings.mffc]))
1368
            basic.append(mffc)
1369

    
1370
            sfafc = RadioSetting("settings.sfafc",
1371
                                 "SFA-FC",
1372
                                 RadioSettingValueList(
1373
                                     LIST_COLOR8,
1374
                                     LIST_COLOR8[_mem.settings.sfafc]))
1375
            basic.append(sfafc)
1376

    
1377
            sfbfc = RadioSetting("settings.sfbfc",
1378
                                 "SFB-FC",
1379
                                 RadioSettingValueList(
1380
                                     LIST_COLOR8,
1381
                                     LIST_COLOR8[_mem.settings.sfbfc]))
1382
            basic.append(sfbfc)
1383

    
1384
            sfcfc = RadioSetting("settings.sfcfc",
1385
                                 "SFC-FC",
1386
                                 RadioSettingValueList(
1387
                                     LIST_COLOR8,
1388
                                     LIST_COLOR8[_mem.settings.sfcfc]))
1389
            basic.append(sfcfc)
1390

    
1391
            sfdfc = RadioSetting("settings.sfdfc",
1392
                                 "SFD-FC",
1393
                                 RadioSettingValueList(
1394
                                     LIST_COLOR8,
1395
                                     LIST_COLOR8[_mem.settings.sfdfc]))
1396
            basic.append(sfdfc)
1397

    
1398
            subfc = RadioSetting("settings.subfc",
1399
                                 "SUB-FC",
1400
                                 RadioSettingValueList(
1401
                                     LIST_COLOR8,
1402
                                     LIST_COLOR8[_mem.settings.subfc]))
1403
            basic.append(subfc)
1404

    
1405
            fmfc = RadioSetting("settings.fmfc",
1406
                                "FM-FC",
1407
                                RadioSettingValueList(
1408
                                    LIST_COLOR8,
1409
                                    LIST_COLOR8[_mem.settings.fmfc]))
1410
            basic.append(fmfc)
1411

    
1412
            sigfc = RadioSetting("settings.sigfc",
1413
                                 "SIG-FC",
1414
                                 RadioSettingValueList(
1415
                                     LIST_COLOR8,
1416
                                     LIST_COLOR8[_mem.settings.sigfc]))
1417
            basic.append(sigfc)
1418

    
1419
            if not self.MODEL == "KT-8R":
1420
                modfc = RadioSetting("settings.modfc",
1421
                                     "MOD-FC",
1422
                                     RadioSettingValueList(
1423
                                         LIST_COLOR8,
1424
                                         LIST_COLOR8[_mem.settings.modfc]))
1425
                basic.append(modfc)
1426

    
1427
            menufc = RadioSetting("settings.menufc",
1428
                                  "MENUFC",
1429
                                  RadioSettingValueList(
1430
                                      LIST_COLOR8,
1431
                                      LIST_COLOR8[_mem.settings.menufc]))
1432
            basic.append(menufc)
1433

    
1434
            txfc = RadioSetting("settings.txfc",
1435
                                "TX-FC",
1436
                                RadioSettingValueList(
1437
                                    LIST_COLOR8,
1438
                                    LIST_COLOR8[_mem.settings.txfc]))
1439
            basic.append(txfc)
1440

    
1441
            if self.MODEL == "KT-8R":
1442
                rxfc = RadioSetting("settings.rxfc",
1443
                                    "RX-FC",
1444
                                    RadioSettingValueList(
1445
                                        LIST_COLOR8,
1446
                                        LIST_COLOR8[_mem.settings.rxfc]))
1447
                basic.append(rxfc)
1448

    
1449
            if not self.MODEL == "KT-8R":
1450
                txdisp = RadioSetting("settings.txdisp",
1451
                                      "Transmitting status display",
1452
                                      RadioSettingValueList(
1453
                                          LIST_TXDISP,
1454
                                          LIST_TXDISP[_mem.settings.txdisp]))
1455
                basic.append(txdisp)
1456
        else:
1457
            wtled = RadioSetting("settings.wtled", "Standby backlight Color",
1458
                                 RadioSettingValueList(
1459
                                     LIST_COLOR4,
1460
                                     LIST_COLOR4[_mem.settings.wtled]))
1461
            basic.append(wtled)
1462

    
1463
            rxled = RadioSetting("settings.rxled", "RX backlight Color",
1464
                                 RadioSettingValueList(
1465
                                     LIST_COLOR4,
1466
                                     LIST_COLOR4[_mem.settings.rxled]))
1467
            basic.append(rxled)
1468

    
1469
            txled = RadioSetting("settings.txled", "TX backlight Color",
1470
                                 RadioSettingValueList(
1471
                                     LIST_COLOR4,
1472
                                     LIST_COLOR4[_mem.settings.txled]))
1473
            basic.append(txled)
1474

    
1475
        anil = RadioSetting("settings.anil", "ANI length",
1476
                            RadioSettingValueList(
1477
                                LIST_ANIL,
1478
                                LIST_ANIL[_mem.settings.anil]))
1479
        basic.append(anil)
1480

    
1481
        reps = RadioSetting("settings.reps", "Relay signal (tone burst)",
1482
                            RadioSettingValueList(
1483
                                LIST_REPS,
1484
                                LIST_REPS[_mem.settings.reps]))
1485
        basic.append(reps)
1486

    
1487
        if not self.MODEL == "GMRS-50X1" and not self.MODEL == "KT-8R":
1488
            repm = RadioSetting("settings.repm", "Relay condition",
1489
                                RadioSettingValueList(
1490
                                    LIST_REPM,
1491
                                    LIST_REPM[_mem.settings.repm]))
1492
            basic.append(repm)
1493

    
1494
        if self.VENDOR == "BTECH" or self.COLOR_LCD:
1495
            if self.COLOR_LCD:
1496
                tmrmr = RadioSetting("settings.tmrmr", "TMR return time",
1497
                                     RadioSettingValueList(
1498
                                         LIST_OFF1TO50,
1499
                                         LIST_OFF1TO50[_mem.settings.tmrmr]))
1500
                basic.append(tmrmr)
1501
            else:
1502
                tdrab = RadioSetting("settings.tdrab", "TDR return time",
1503
                                     RadioSettingValueList(
1504
                                         LIST_OFF1TO50,
1505
                                         LIST_OFF1TO50[_mem.settings.tdrab]))
1506
                basic.append(tdrab)
1507

    
1508
            ste = RadioSetting("settings.ste", "Squelch tail eliminate",
1509
                               RadioSettingValueBoolean(_mem.settings.ste))
1510
            basic.append(ste)
1511

    
1512
            rpste = RadioSetting("settings.rpste", "Repeater STE",
1513
                                 RadioSettingValueList(
1514
                                     LIST_OFF1TO9,
1515
                                     LIST_OFF1TO9[_mem.settings.rpste]))
1516
            basic.append(rpste)
1517

    
1518
            rptdl = RadioSetting("settings.rptdl", "Repeater STE delay",
1519
                                 RadioSettingValueList(
1520
                                     LIST_RPTDL,
1521
                                     LIST_RPTDL[_mem.settings.rptdl]))
1522
            basic.append(rptdl)
1523

    
1524
        if str(_mem.fingerprint.fp) in BTECH3:
1525
            mgain = RadioSetting("settings.mgain", "Mic gain",
1526
                                 RadioSettingValueInteger(0, 120,
1527
                                                          _mem.settings.mgain))
1528
            basic.append(mgain)
1529

    
1530
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1531
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain",
1532
                                 RadioSettingValueInteger(0, 60,
1533
                                                          _mem.settings.dtmfg))
1534
            basic.append(dtmfg)
1535

    
1536
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1537
            mgain = RadioSetting("settings.mgain", "Mic gain",
1538
                                 RadioSettingValueInteger(0, 120,
1539
                                                          _mem.settings.mgain))
1540
            basic.append(mgain)
1541

    
1542
            skiptx = RadioSetting("settings.skiptx", "Skip TX",
1543
                                  RadioSettingValueList(
1544
                                      LIST_SKIPTX,
1545
                                      LIST_SKIPTX[_mem.settings.skiptx]))
1546
            basic.append(skiptx)
1547

    
1548
            scmode = RadioSetting("settings.scmode", "Scan mode",
1549
                                  RadioSettingValueList(
1550
                                      LIST_SCMODE,
1551
                                      LIST_SCMODE[_mem.settings.scmode]))
1552
            basic.append(scmode)
1553

    
1554
        if self.MODEL == "KT-8R":
1555
            tmrtx = RadioSetting("settings.tmrtx", "TX in multi-standby",
1556
                                 RadioSettingValueList(
1557
                                     LIST_TMRTX,
1558
                                     LIST_TMRTX[_mem.settings.tmrtx]))
1559
            basic.append(tmrtx)
1560

    
1561
        # Advanced
1562
        def _filter(name):
1563
            filtered = ""
1564
            for char in str(name):
1565
                if char in VALID_CHARS:
1566
                    filtered += char
1567
                else:
1568
                    filtered += " "
1569
            return filtered
1570

    
1571
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3):
1572
            _msg = self._memobj.poweron_msg
1573
            line1 = RadioSetting("poweron_msg.line1",
1574
                                 "Power-on message line 1",
1575
                                 RadioSettingValueString(0, 8, _filter(
1576
                                                         _msg.line1)))
1577
            advanced.append(line1)
1578
            line2 = RadioSetting("poweron_msg.line2",
1579
                                 "Power-on message line 2",
1580
                                 RadioSettingValueString(0, 8, _filter(
1581
                                                         _msg.line2)))
1582
            advanced.append(line2)
1583
            line3 = RadioSetting("poweron_msg.line3",
1584
                                 "Power-on message line 3",
1585
                                 RadioSettingValueString(0, 8, _filter(
1586
                                                         _msg.line3)))
1587
            advanced.append(line3)
1588
            line4 = RadioSetting("poweron_msg.line4",
1589
                                 "Power-on message line 4",
1590
                                 RadioSettingValueString(0, 8, _filter(
1591
                                                         _msg.line4)))
1592
            advanced.append(line4)
1593
            line5 = RadioSetting("poweron_msg.line5",
1594
                                 "Power-on message line 5",
1595
                                 RadioSettingValueString(0, 8, _filter(
1596
                                                         _msg.line5)))
1597
            advanced.append(line5)
1598
            line6 = RadioSetting("poweron_msg.line6",
1599
                                 "Power-on message line 6",
1600
                                 RadioSettingValueString(0, 8, _filter(
1601
                                                         _msg.line6)))
1602
            advanced.append(line6)
1603
            line7 = RadioSetting("poweron_msg.line7",
1604
                                 "Power-on message line 7",
1605
                                 RadioSettingValueString(0, 8, _filter(
1606
                                                         _msg.line7)))
1607
            advanced.append(line7)
1608
            line8 = RadioSetting("poweron_msg.line8", "Static message",
1609
                                 RadioSettingValueString(0, 8, _filter(
1610
                                                         _msg.line8)))
1611
            advanced.append(line8)
1612
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1613
            _msg = self._memobj.static_msg
1614
            line = RadioSetting("static_msg.line", "Static message",
1615
                                RadioSettingValueString(0, 16, _filter(
1616
                                    _msg.line)))
1617
            advanced.append(line)
1618
        else:
1619
            _msg = self._memobj.poweron_msg
1620
            line1 = RadioSetting("poweron_msg.line1",
1621
                                 "Power-on message line 1",
1622
                                 RadioSettingValueString(0, 6, _filter(
1623
                                                         _msg.line1)))
1624
            advanced.append(line1)
1625
            line2 = RadioSetting("poweron_msg.line2",
1626
                                 "Power-on message line 2",
1627
                                 RadioSettingValueString(0, 6, _filter(
1628
                                                         _msg.line2)))
1629
            advanced.append(line2)
1630

    
1631
        if self.MODEL in ("UV-2501", "UV-5001"):
1632
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1633
                                   RadioSettingValueBoolean(
1634
                                       _mem.settings2.vfomren))
1635
            advanced.append(vfomren)
1636

    
1637
            reseten = RadioSetting("settings2.reseten", "RESET",
1638
                                   RadioSettingValueBoolean(
1639
                                       _mem.settings2.reseten))
1640
            advanced.append(reseten)
1641

    
1642
            menuen = RadioSetting("settings2.menuen", "Menu",
1643
                                  RadioSettingValueBoolean(
1644
                                      _mem.settings2.menuen))
1645
            advanced.append(menuen)
1646

    
1647
        # Other
1648
        def convert_bytes_to_limit(bytes):
1649
            limit = ""
1650
            for byte in bytes:
1651
                if byte < 10:
1652
                    limit += chr(byte + 0x30)
1653
                else:
1654
                    break
1655
            return limit
1656

    
1657
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1658
            _ranges = self._memobj.ranges220
1659
            ranges = "ranges220"
1660
        else:
1661
            _ranges = self._memobj.ranges
1662
            ranges = "ranges"
1663

    
1664
        _limit = convert_bytes_to_limit(_ranges.vhf_low)
1665
        val = RadioSettingValueString(0, 3, _limit)
1666
        val.set_mutable(False)
1667
        vhf_low = RadioSetting("%s.vhf_low" % ranges, "VHF low", val)
1668
        other.append(vhf_low)
1669

    
1670
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1671
        val = RadioSettingValueString(0, 3, _limit)
1672
        val.set_mutable(False)
1673
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1674
        other.append(vhf_high)
1675

    
1676
        if self.BANDS == 3 or self.BANDS == 4:
1677
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1678
            val = RadioSettingValueString(0, 3, _limit)
1679
            val.set_mutable(False)
1680
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1681
            other.append(vhf2_low)
1682

    
1683
            _limit = convert_bytes_to_limit(_ranges.vhf2_high)
1684
            val = RadioSettingValueString(0, 3, _limit)
1685
            val.set_mutable(False)
1686
            vhf2_high = RadioSetting("%s.vhf2_high" % ranges, "VHF2 high", val)
1687
            other.append(vhf2_high)
1688

    
1689
        _limit = convert_bytes_to_limit(_ranges.uhf_low)
1690
        val = RadioSettingValueString(0, 3, _limit)
1691
        val.set_mutable(False)
1692
        uhf_low = RadioSetting("%s.uhf_low" % ranges, "UHF low", val)
1693
        other.append(uhf_low)
1694

    
1695
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1696
        val = RadioSettingValueString(0, 3, _limit)
1697
        val.set_mutable(False)
1698
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1699
        other.append(uhf_high)
1700

    
1701
        if self.BANDS == 4:
1702
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1703
            val = RadioSettingValueString(0, 3, _limit)
1704
            val.set_mutable(False)
1705
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1706
            other.append(uhf2_low)
1707

    
1708
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1709
            val = RadioSettingValueString(0, 3, _limit)
1710
            val.set_mutable(False)
1711
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1712
            other.append(uhf2_high)
1713

    
1714
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1715
        val.set_mutable(False)
1716
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1717
        other.append(fp)
1718

    
1719
        # Work
1720
        if self.COLOR_LCD:
1721
            dispab = RadioSetting("settings2.dispab", "Display",
1722
                                  RadioSettingValueList(
1723
                                      LIST_ABCD,
1724
                                      LIST_ABCD[_mem.settings2.dispab]))
1725
            work.append(dispab)
1726
        else:
1727
            dispab = RadioSetting("settings2.dispab", "Display",
1728
                                  RadioSettingValueList(
1729
                                      LIST_AB,
1730
                                      LIST_AB[_mem.settings2.dispab]))
1731
            work.append(dispab)
1732

    
1733
        if self.COLOR_LCD:
1734
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode",
1735
                                  RadioSettingValueList(
1736
                                      LIST_VFOMR,
1737
                                      LIST_VFOMR[_mem.settings2.vfomra]))
1738
            work.append(vfomra)
1739

    
1740
            vfomrb = RadioSetting("settings2.vfomrb", "VFO/MR B mode",
1741
                                  RadioSettingValueList(
1742
                                      LIST_VFOMR,
1743
                                      LIST_VFOMR[_mem.settings2.vfomrb]))
1744
            work.append(vfomrb)
1745

    
1746
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode",
1747
                                  RadioSettingValueList(
1748
                                      LIST_VFOMR,
1749
                                      LIST_VFOMR[_mem.settings2.vfomrc]))
1750
            work.append(vfomrc)
1751

    
1752
            vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode",
1753
                                  RadioSettingValueList(
1754
                                      LIST_VFOMR,
1755
                                      LIST_VFOMR[_mem.settings2.vfomrd]))
1756
            work.append(vfomrd)
1757
        else:
1758
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode",
1759
                                 RadioSettingValueList(
1760
                                     LIST_VFOMR,
1761
                                     LIST_VFOMR[_mem.settings2.vfomr]))
1762
            work.append(vfomr)
1763

    
1764
        keylock = RadioSetting("settings2.keylock", "Keypad lock",
1765
                               RadioSettingValueBoolean(
1766
                                   _mem.settings2.keylock))
1767
        work.append(keylock)
1768

    
1769
        mrcha = RadioSetting("settings2.mrcha", "MR A channel",
1770
                             RadioSettingValueInteger(0, self._upper,
1771
                                                      _mem.settings2.mrcha))
1772
        work.append(mrcha)
1773

    
1774
        mrchb = RadioSetting("settings2.mrchb", "MR B channel",
1775
                             RadioSettingValueInteger(0, self._upper,
1776
                                                      _mem.settings2.mrchb))
1777
        work.append(mrchb)
1778

    
1779
        if self.COLOR_LCD:
1780
            mrchc = RadioSetting("settings2.mrchc", "MR C channel",
1781
                                 RadioSettingValueInteger(
1782
                                     0, self._upper, _mem.settings2.mrchc))
1783
            work.append(mrchc)
1784

    
1785
            mrchd = RadioSetting("settings2.mrchd", "MR D channel",
1786
                                 RadioSettingValueInteger(
1787
                                     0, self._upper, _mem.settings2.mrchd))
1788
            work.append(mrchd)
1789

    
1790
        def convert_bytes_to_freq(bytes):
1791
            real_freq = 0
1792
            for byte in bytes:
1793
                real_freq = (real_freq * 10) + byte
1794
            return chirp_common.format_freq(real_freq * 10)
1795

    
1796
        def my_validate(value):
1797
            _vhf_lower = int(convert_bytes_to_limit(_ranges.vhf_low))
1798
            _vhf_upper = int(convert_bytes_to_limit(_ranges.vhf_high))
1799
            _uhf_lower = int(convert_bytes_to_limit(_ranges.uhf_low))
1800
            _uhf_upper = int(convert_bytes_to_limit(_ranges.uhf_high))
1801
            if self.BANDS == 3 or self.BANDS == 4:
1802
                _vhf2_lower = int(convert_bytes_to_limit(_ranges.vhf2_low))
1803
                _vhf2_upper = int(convert_bytes_to_limit(_ranges.vhf2_high))
1804
            if self.BANDS == 4:
1805
                _uhf2_lower = int(convert_bytes_to_limit(_ranges.uhf2_low))
1806
                _uhf2_upper = int(convert_bytes_to_limit(_ranges.uhf2_high))
1807

    
1808
            value = chirp_common.parse_freq(value)
1809
            msg = ("Can't be less then %i.0000")
1810
            if value > 99000000 and value < _vhf_lower * 1000000:
1811
                raise InvalidValueError(msg % (_vhf_lower))
1812
            msg = ("Can't be betweeb %i.9975-%i.0000")
1813
            if self.BANDS == 2:
1814
                if (_vhf_upper + 1) * 1000000 <= value and \
1815
                        value < _uhf_lower * 1000000:
1816
                    raise InvalidValueError(msg % (_vhf_upper, _uhf_lower))
1817
            if self.BANDS == 3:
1818
                if (_vhf_upper + 1) * 1000000 <= value and \
1819
                        value < _vhf2_lower * 1000000:
1820
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1821
                if (_vhf2_upper + 1) * 1000000 <= value and \
1822
                        value < _uhf_lower * 1000000:
1823
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf_lower))
1824
            if self.BANDS == 4:
1825
                if (_vhf_upper + 1) * 1000000 <= value and \
1826
                        value < _vhf2_lower * 1000000:
1827
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1828
                if (_vhf2_upper + 1) * 1000000 <= value and \
1829
                        value < _uhf2_lower * 1000000:
1830
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf2_lower))
1831
                if (_uhf2_upper + 1) * 1000000 <= value and \
1832
                        value < _uhf_lower * 1000000:
1833
                    raise InvalidValueError(msg % (_uhf2_upper, _uhf_lower))
1834
            msg = ("Can't be greater then %i.9975")
1835
            if value > 99000000 and value >= _uhf_upper * 1000000:
1836
                raise InvalidValueError(msg % (_uhf_upper))
1837
            return chirp_common.format_freq(value)
1838

    
1839
        def apply_freq(setting, obj):
1840
            value = chirp_common.parse_freq(str(setting.value)) / 10
1841
            for i in range(7, -1, -1):
1842
                obj.freq[i] = value % 10
1843
                value /= 10
1844

    
1845
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1846
                                        _mem.vfo.a.freq))
1847
        val1a.set_validate_callback(my_validate)
1848
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
1849
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
1850
        work.append(vfoafreq)
1851

    
1852
        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1853
                                        _mem.vfo.b.freq))
1854
        val1b.set_validate_callback(my_validate)
1855
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
1856
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
1857
        work.append(vfobfreq)
1858

    
1859
        if self.COLOR_LCD:
1860
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1861
                                            _mem.vfo.c.freq))
1862
            val1c.set_validate_callback(my_validate)
1863
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
1864
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
1865
            work.append(vfocfreq)
1866

    
1867
            val1d = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1868
                                            _mem.vfo.d.freq))
1869
            val1d.set_validate_callback(my_validate)
1870
            vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
1871
            vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
1872
            work.append(vfodfreq)
1873

    
1874
        if not self.MODEL == "GMRS-50X1":
1875
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift",
1876
                                      RadioSettingValueList(
1877
                                          LIST_SHIFT,
1878
                                          LIST_SHIFT[_mem.vfo.a.shiftd]))
1879
            work.append(vfoashiftd)
1880

    
1881
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift",
1882
                                      RadioSettingValueList(
1883
                                          LIST_SHIFT,
1884
                                          LIST_SHIFT[_mem.vfo.b.shiftd]))
1885
            work.append(vfobshiftd)
1886

    
1887
            if self.COLOR_LCD:
1888
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift",
1889
                                          RadioSettingValueList(
1890
                                              LIST_SHIFT,
1891
                                              LIST_SHIFT[_mem.vfo.c.shiftd]))
1892
                work.append(vfocshiftd)
1893

    
1894
                vfodshiftd = RadioSetting("vfo.d.shiftd", "VFO D shift",
1895
                                          RadioSettingValueList(
1896
                                              LIST_SHIFT,
1897
                                              LIST_SHIFT[_mem.vfo.d.shiftd]))
1898
                work.append(vfodshiftd)
1899

    
1900
        def convert_bytes_to_offset(bytes):
1901
            real_offset = 0
1902
            for byte in bytes:
1903
                real_offset = (real_offset * 10) + byte
1904
            return chirp_common.format_freq(real_offset * 1000)
1905

    
1906
        def apply_offset(setting, obj):
1907
            value = chirp_common.parse_freq(str(setting.value)) / 1000
1908
            for i in range(5, -1, -1):
1909
                obj.offset[i] = value % 10
1910
                value /= 10
1911

    
1912
        if not self.MODEL == "GMRS-50X1":
1913
            if self.COLOR_LCD:
1914
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1915
                                                _mem.vfo.a.offset))
1916
                vfoaoffset = RadioSetting("vfo.a.offset",
1917
                                          "VFO A offset (0.000-999.999)",
1918
                                          val1a)
1919
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
1920
                work.append(vfoaoffset)
1921

    
1922
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1923
                                                _mem.vfo.b.offset))
1924
                vfoboffset = RadioSetting("vfo.b.offset",
1925
                                          "VFO B offset (0.000-999.999)",
1926
                                          val1b)
1927
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1928
                work.append(vfoboffset)
1929

    
1930
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1931
                                                _mem.vfo.c.offset))
1932
                vfocoffset = RadioSetting("vfo.c.offset",
1933
                                          "VFO C offset (0.000-999.999)",
1934
                                          val1c)
1935
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
1936
                work.append(vfocoffset)
1937

    
1938
                val1d = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1939
                                                _mem.vfo.d.offset))
1940
                vfodoffset = RadioSetting("vfo.d.offset",
1941
                                          "VFO D offset (0.000-999.999)",
1942
                                          val1d)
1943
                vfodoffset.set_apply_callback(apply_offset, _mem.vfo.d)
1944
                work.append(vfodoffset)
1945
            else:
1946
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1947
                                                _mem.vfo.a.offset))
1948
                vfoaoffset = RadioSetting("vfo.a.offset",
1949
                                          "VFO A offset (0.000-99.999)", val1a)
1950
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
1951
                work.append(vfoaoffset)
1952

    
1953
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1954
                                                _mem.vfo.b.offset))
1955
                vfoboffset = RadioSetting("vfo.b.offset",
1956
                                          "VFO B offset (0.000-99.999)", val1b)
1957
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1958
                work.append(vfoboffset)
1959

    
1960
        if not self.MODEL == "GMRS-50X1":
1961
            vfoatxp = RadioSetting("vfo.a.power", "VFO A power",
1962
                                   RadioSettingValueList(
1963
                                       LIST_TXP,
1964
                                       LIST_TXP[_mem.vfo.a.power]))
1965
            work.append(vfoatxp)
1966

    
1967
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power",
1968
                                   RadioSettingValueList(
1969
                                       LIST_TXP,
1970
                                       LIST_TXP[_mem.vfo.b.power]))
1971
            work.append(vfobtxp)
1972

    
1973
            if self.COLOR_LCD:
1974
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power",
1975
                                       RadioSettingValueList(
1976
                                           LIST_TXP,
1977
                                           LIST_TXP[_mem.vfo.c.power]))
1978
                work.append(vfoctxp)
1979

    
1980
                vfodtxp = RadioSetting("vfo.d.power", "VFO D power",
1981
                                       RadioSettingValueList(
1982
                                           LIST_TXP,
1983
                                           LIST_TXP[_mem.vfo.d.power]))
1984
                work.append(vfodtxp)
1985

    
1986
        if not self.MODEL == "GMRS-50X1":
1987
            vfoawide = RadioSetting("vfo.a.wide", "VFO A bandwidth",
1988
                                    RadioSettingValueList(
1989
                                        LIST_WIDE,
1990
                                        LIST_WIDE[_mem.vfo.a.wide]))
1991
            work.append(vfoawide)
1992

    
1993
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth",
1994
                                    RadioSettingValueList(
1995
                                        LIST_WIDE,
1996
                                        LIST_WIDE[_mem.vfo.b.wide]))
1997
            work.append(vfobwide)
1998

    
1999
            if self.COLOR_LCD:
2000
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth",
2001
                                        RadioSettingValueList(
2002
                                            LIST_WIDE,
2003
                                            LIST_WIDE[_mem.vfo.c.wide]))
2004
                work.append(vfocwide)
2005

    
2006
                vfodwide = RadioSetting("vfo.d.wide", "VFO D bandwidth",
2007
                                        RadioSettingValueList(
2008
                                            LIST_WIDE,
2009
                                            LIST_WIDE[_mem.vfo.d.wide]))
2010
                work.append(vfodwide)
2011

    
2012
        vfoastep = RadioSetting("vfo.a.step", "VFO A step",
2013
                                RadioSettingValueList(
2014
                                    LIST_STEP,
2015
                                    LIST_STEP[_mem.vfo.a.step]))
2016
        work.append(vfoastep)
2017

    
2018
        vfobstep = RadioSetting("vfo.b.step", "VFO B step",
2019
                                RadioSettingValueList(
2020
                                    LIST_STEP,
2021
                                    LIST_STEP[_mem.vfo.b.step]))
2022
        work.append(vfobstep)
2023

    
2024
        if self.COLOR_LCD:
2025
            vfocstep = RadioSetting("vfo.c.step", "VFO C step",
2026
                                    RadioSettingValueList(
2027
                                        LIST_STEP,
2028
                                        LIST_STEP[_mem.vfo.c.step]))
2029
            work.append(vfocstep)
2030

    
2031
            vfodstep = RadioSetting("vfo.d.step", "VFO D step",
2032
                                    RadioSettingValueList(
2033
                                        LIST_STEP,
2034
                                        LIST_STEP[_mem.vfo.d.step]))
2035
            work.append(vfodstep)
2036

    
2037
        vfoaoptsig = RadioSetting("vfo.a.optsig", "VFO A optional signal",
2038
                                  RadioSettingValueList(
2039
                                      OPTSIG_LIST,
2040
                                      OPTSIG_LIST[_mem.vfo.a.optsig]))
2041
        work.append(vfoaoptsig)
2042

    
2043
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal",
2044
                                  RadioSettingValueList(
2045
                                      OPTSIG_LIST,
2046
                                      OPTSIG_LIST[_mem.vfo.b.optsig]))
2047
        work.append(vfoboptsig)
2048

    
2049
        if self.COLOR_LCD:
2050
            vfocoptsig = RadioSetting("vfo.c.optsig", "VFO C optional signal",
2051
                                      RadioSettingValueList(
2052
                                          OPTSIG_LIST,
2053
                                          OPTSIG_LIST[_mem.vfo.c.optsig]))
2054
            work.append(vfocoptsig)
2055

    
2056
            vfodoptsig = RadioSetting("vfo.d.optsig", "VFO D optional signal",
2057
                                      RadioSettingValueList(
2058
                                          OPTSIG_LIST,
2059
                                          OPTSIG_LIST[_mem.vfo.d.optsig]))
2060
            work.append(vfodoptsig)
2061

    
2062
        vfoaspmute = RadioSetting("vfo.a.spmute", "VFO A speaker mute",
2063
                                  RadioSettingValueList(
2064
                                      SPMUTE_LIST,
2065
                                      SPMUTE_LIST[_mem.vfo.a.spmute]))
2066
        work.append(vfoaspmute)
2067

    
2068
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute",
2069
                                  RadioSettingValueList(
2070
                                      SPMUTE_LIST,
2071
                                      SPMUTE_LIST[_mem.vfo.b.spmute]))
2072
        work.append(vfobspmute)
2073

    
2074
        if self.COLOR_LCD:
2075
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute",
2076
                                      RadioSettingValueList(
2077
                                          SPMUTE_LIST,
2078
                                          SPMUTE_LIST[_mem.vfo.c.spmute]))
2079
            work.append(vfocspmute)
2080

    
2081
            vfodspmute = RadioSetting("vfo.d.spmute", "VFO D speaker mute",
2082
                                      RadioSettingValueList(
2083
                                          SPMUTE_LIST,
2084
                                          SPMUTE_LIST[_mem.vfo.d.spmute]))
2085
            work.append(vfodspmute)
2086

    
2087
        if not self.COLOR_LCD or \
2088
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2089
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble",
2090
                                   RadioSettingValueBoolean(
2091
                                       _mem.vfo.a.scramble))
2092
            work.append(vfoascr)
2093

    
2094
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble",
2095
                                   RadioSettingValueBoolean(
2096
                                       _mem.vfo.b.scramble))
2097
            work.append(vfobscr)
2098

    
2099
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2100
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble",
2101
                                   RadioSettingValueBoolean(
2102
                                       _mem.vfo.c.scramble))
2103
            work.append(vfocscr)
2104

    
2105
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble",
2106
                                   RadioSettingValueBoolean(
2107
                                       _mem.vfo.d.scramble))
2108
            work.append(vfodscr)
2109

    
2110
        if not self.MODEL == "GMRS-50X1":
2111
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID",
2112
                                     RadioSettingValueList(
2113
                                         PTTIDCODE_LIST,
2114
                                         PTTIDCODE_LIST[_mem.vfo.a.scode]))
2115
            work.append(vfoascode)
2116

    
2117
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID",
2118
                                     RadioSettingValueList(
2119
                                         PTTIDCODE_LIST,
2120
                                         PTTIDCODE_LIST[_mem.vfo.b.scode]))
2121
            work.append(vfobscode)
2122

    
2123
            if self.COLOR_LCD:
2124
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID",
2125
                                         RadioSettingValueList(
2126
                                             PTTIDCODE_LIST,
2127
                                             PTTIDCODE_LIST[_mem.vfo.c.scode]))
2128
                work.append(vfocscode)
2129

    
2130
                vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID",
2131
                                         RadioSettingValueList(
2132
                                             PTTIDCODE_LIST,
2133
                                             PTTIDCODE_LIST[_mem.vfo.d.scode]))
2134
                work.append(vfodscode)
2135

    
2136
        if not self.MODEL == "GMRS-50X1":
2137
            pttid = RadioSetting("settings.pttid", "PTT ID",
2138
                                 RadioSettingValueList(
2139
                                     PTTID_LIST,
2140
                                     PTTID_LIST[_mem.settings.pttid]))
2141
            work.append(pttid)
2142

    
2143
        if not self.COLOR_LCD:
2144
            # FM presets
2145
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2146
            top.append(fm_presets)
2147

    
2148
            def fm_validate(value):
2149
                if value == 0:
2150
                    return chirp_common.format_freq(value)
2151
                if not (87.5 <= value and value <= 108.0):  # 87.5-108MHz
2152
                    msg = ("FM-Preset-Frequency: " +
2153
                           "Must be between 87.5 and 108 MHz")
2154
                    raise InvalidValueError(msg)
2155
                return value
2156

    
2157
            def apply_fm_preset_name(setting, obj):
2158
                valstring = str(setting.value)
2159
                for i in range(0, 6):
2160
                    if valstring[i] in VALID_CHARS:
2161
                        obj[i] = valstring[i]
2162
                    else:
2163
                        obj[i] = '0xff'
2164

    
2165
            def apply_fm_freq(setting, obj):
2166
                value = chirp_common.parse_freq(str(setting.value)) / 10
2167
                for i in range(7, -1, -1):
2168
                    obj.freq[i] = value % 10
2169
                    value /= 10
2170

    
2171
            _presets = self._memobj.fm_radio_preset
2172
            i = 1
2173
            for preset in _presets:
2174
                line = RadioSetting("fm_presets_" + str(i),
2175
                                    "Station name " + str(i),
2176
                                    RadioSettingValueString(0, 6, _filter(
2177
                                        preset.broadcast_station_name)))
2178
                line.set_apply_callback(apply_fm_preset_name,
2179
                                        preset.broadcast_station_name)
2180

    
2181
                val = RadioSettingValueFloat(0, 108,
2182
                                             convert_bytes_to_freq(
2183
                                                 preset.freq))
2184
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2185
                                      "Frequency " + str(i), val)
2186
                val.set_validate_callback(fm_validate)
2187
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2188
                fm_presets.append(line)
2189
                fm_presets.append(fmfreq)
2190

    
2191
                i = i + 1
2192

    
2193
        # DTMF-Setting
2194
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2195
                                              "DTMF Encoding Settings")
2196
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2197
                                              "DTMF Decoding Settings")
2198
        top.append(dtmf_enc_settings)
2199
        top.append(dtmf_dec_settings)
2200
        txdisable = RadioSetting("dtmf_settings.txdisable",
2201
                                 "TX-Disable",
2202
                                 RadioSettingValueBoolean(
2203
                                     _mem.dtmf_settings.txdisable))
2204
        dtmf_enc_settings.append(txdisable)
2205

    
2206
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2207
                                 "RX-Disable",
2208
                                 RadioSettingValueBoolean(
2209
                                     _mem.dtmf_settings.rxdisable))
2210
        dtmf_enc_settings.append(rxdisable)
2211

    
2212
        if _mem.dtmf_settings.dtmfspeed_on > 0x0F:
2213
            val = 0x03
2214
        else:
2215
            val = _mem.dtmf_settings.dtmfspeed_on
2216
        dtmfspeed_on = RadioSetting(
2217
            "dtmf_settings.dtmfspeed_on",
2218
            "DTMF Speed (On Time)",
2219
            RadioSettingValueList(LIST_DTMF_SPEED,
2220
                                  LIST_DTMF_SPEED[
2221
                                      val]))
2222
        dtmf_enc_settings.append(dtmfspeed_on)
2223

    
2224
        if _mem.dtmf_settings.dtmfspeed_off > 0x0F:
2225
            val = 0x03
2226
        else:
2227
            val = _mem.dtmf_settings.dtmfspeed_off
2228
        dtmfspeed_off = RadioSetting(
2229
            "dtmf_settings.dtmfspeed_off",
2230
            "DTMF Speed (Off Time)",
2231
            RadioSettingValueList(LIST_DTMF_SPEED,
2232
                                  LIST_DTMF_SPEED[
2233
                                      val]))
2234
        dtmf_enc_settings.append(dtmfspeed_off)
2235

    
2236
        def memory2string(dmtf_mem):
2237
            dtmf_string = ""
2238
            for digit in dmtf_mem:
2239
                if digit != 255:
2240
                    index = LIST_DTMF_VALUES.index(digit)
2241
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2242
            return dtmf_string
2243

    
2244
        def apply_dmtf_frame(setting, obj):
2245
            LOG.debug("Setting DTMF-Code: " + str(setting.value))
2246
            val_string = str(setting.value)
2247
            for i in range(0, 16):
2248
                obj[i] = 255
2249
            i = 0
2250
            for current_char in val_string:
2251
                current_char = current_char.upper()
2252
                index = LIST_DTMF_DIGITS.index(current_char)
2253
                obj[i] = LIST_DTMF_VALUES[index]
2254
                i = i + 1
2255

    
2256
        codes = self._memobj.dtmf_codes
2257
        i = 1
2258
        for dtmfcode in codes:
2259
            val = RadioSettingValueString(0, 16, memory2string(
2260
                                              dtmfcode.code),
2261
                                          False, CHARSET_DTMF_DIGITS)
2262
            line = RadioSetting("dtmf_code_" + str(i) + "_code",
2263
                                "DMTF Code " + str(i), val)
2264
            line.set_apply_callback(apply_dmtf_frame, dtmfcode.code)
2265
            dtmf_enc_settings.append(line)
2266
            i = i + 1
2267

    
2268
        line = RadioSetting("dtmf_settings.mastervice",
2269
                            "Master and Vice ID",
2270
                            RadioSettingValueBoolean(
2271
                                _mem.dtmf_settings.mastervice))
2272
        dtmf_dec_settings.append(line)
2273

    
2274
        val = RadioSettingValueString(0, 16, memory2string(
2275
                                          _mem.dtmf_settings.masterid),
2276
                                      False, CHARSET_DTMF_DIGITS)
2277
        line = RadioSetting("dtmf_settings.masterid",
2278
                            "Master Control ID ", val)
2279
        line.set_apply_callback(apply_dmtf_frame,
2280
                                _mem.dtmf_settings.masterid)
2281
        dtmf_dec_settings.append(line)
2282

    
2283
        line = RadioSetting("dtmf_settings.minspection",
2284
                            "Master Inspection",
2285
                            RadioSettingValueBoolean(
2286
                                _mem.dtmf_settings.minspection))
2287
        dtmf_dec_settings.append(line)
2288

    
2289
        line = RadioSetting("dtmf_settings.mmonitor",
2290
                            "Master Monitor",
2291
                            RadioSettingValueBoolean(
2292
                                _mem.dtmf_settings.mmonitor))
2293
        dtmf_dec_settings.append(line)
2294

    
2295
        line = RadioSetting("dtmf_settings.mstun",
2296
                            "Master Stun",
2297
                            RadioSettingValueBoolean(
2298
                                _mem.dtmf_settings.mstun))
2299
        dtmf_dec_settings.append(line)
2300

    
2301
        line = RadioSetting("dtmf_settings.mkill",
2302
                            "Master Kill",
2303
                            RadioSettingValueBoolean(
2304
                                _mem.dtmf_settings.mkill))
2305
        dtmf_dec_settings.append(line)
2306

    
2307
        line = RadioSetting("dtmf_settings.mrevive",
2308
                            "Master Revive",
2309
                            RadioSettingValueBoolean(
2310
                                _mem.dtmf_settings.mrevive))
2311
        dtmf_dec_settings.append(line)
2312

    
2313
        val = RadioSettingValueString(0, 16, memory2string(
2314
                                          _mem.dtmf_settings.viceid),
2315
                                      False, CHARSET_DTMF_DIGITS)
2316
        line = RadioSetting("dtmf_settings.viceid",
2317
                            "Vice Control ID ", val)
2318
        line.set_apply_callback(apply_dmtf_frame,
2319
                                _mem.dtmf_settings.viceid)
2320
        dtmf_dec_settings.append(line)
2321

    
2322
        line = RadioSetting("dtmf_settings.vinspection",
2323
                            "Vice Inspection",
2324
                            RadioSettingValueBoolean(
2325
                                _mem.dtmf_settings.vinspection))
2326
        dtmf_dec_settings.append(line)
2327

    
2328
        line = RadioSetting("dtmf_settings.vmonitor",
2329
                            "Vice Monitor",
2330
                            RadioSettingValueBoolean(
2331
                                _mem.dtmf_settings.vmonitor))
2332
        dtmf_dec_settings.append(line)
2333

    
2334
        line = RadioSetting("dtmf_settings.vstun",
2335
                            "Vice Stun",
2336
                            RadioSettingValueBoolean(
2337
                                _mem.dtmf_settings.vstun))
2338
        dtmf_dec_settings.append(line)
2339

    
2340
        line = RadioSetting("dtmf_settings.vkill",
2341
                            "Vice Kill",
2342
                            RadioSettingValueBoolean(
2343
                                _mem.dtmf_settings.vkill))
2344
        dtmf_dec_settings.append(line)
2345

    
2346
        line = RadioSetting("dtmf_settings.vrevive",
2347
                            "Vice Revive",
2348
                            RadioSettingValueBoolean(
2349
                                _mem.dtmf_settings.vrevive))
2350
        dtmf_dec_settings.append(line)
2351

    
2352
        val = RadioSettingValueString(0, 16, memory2string(
2353
                                          _mem.dtmf_settings.inspection),
2354
                                      False, CHARSET_DTMF_DIGITS)
2355
        line = RadioSetting("dtmf_settings.inspection",
2356
                            "Inspection", val)
2357
        line.set_apply_callback(apply_dmtf_frame,
2358
                                _mem.dtmf_settings.inspection)
2359
        dtmf_dec_settings.append(line)
2360

    
2361
        val = RadioSettingValueString(0, 16, memory2string(
2362
                                          _mem.dtmf_settings.alarmcode),
2363
                                      False, CHARSET_DTMF_DIGITS)
2364
        line = RadioSetting("dtmf_settings.alarmcode",
2365
                            "Alarm", val)
2366
        line.set_apply_callback(apply_dmtf_frame,
2367
                                _mem.dtmf_settings.alarmcode)
2368
        dtmf_dec_settings.append(line)
2369

    
2370
        val = RadioSettingValueString(0, 16, memory2string(
2371
                                          _mem.dtmf_settings.kill),
2372
                                      False, CHARSET_DTMF_DIGITS)
2373
        line = RadioSetting("dtmf_settings.kill",
2374
                            "Kill", val)
2375
        line.set_apply_callback(apply_dmtf_frame,
2376
                                _mem.dtmf_settings.kill)
2377
        dtmf_dec_settings.append(line)
2378

    
2379
        val = RadioSettingValueString(0, 16, memory2string(
2380
                                          _mem.dtmf_settings.monitor),
2381
                                      False, CHARSET_DTMF_DIGITS)
2382
        line = RadioSetting("dtmf_settings.monitor",
2383
                            "Monitor", val)
2384
        line.set_apply_callback(apply_dmtf_frame,
2385
                                _mem.dtmf_settings.monitor)
2386
        dtmf_dec_settings.append(line)
2387

    
2388
        val = RadioSettingValueString(0, 16, memory2string(
2389
                                          _mem.dtmf_settings.stun),
2390
                                      False, CHARSET_DTMF_DIGITS)
2391
        line = RadioSetting("dtmf_settings.stun",
2392
                            "Stun", val)
2393
        line.set_apply_callback(apply_dmtf_frame,
2394
                                _mem.dtmf_settings.stun)
2395
        dtmf_dec_settings.append(line)
2396

    
2397
        val = RadioSettingValueString(0, 16, memory2string(
2398
                                          _mem.dtmf_settings.revive),
2399
                                      False, CHARSET_DTMF_DIGITS)
2400
        line = RadioSetting("dtmf_settings.revive",
2401
                            "Revive", val)
2402
        line.set_apply_callback(apply_dmtf_frame,
2403
                                _mem.dtmf_settings.revive)
2404
        dtmf_dec_settings.append(line)
2405

    
2406
        def apply_dmtf_listvalue(setting, obj):
2407
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2408
            val = str(setting.value)
2409
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2410
            val = LIST_DTMF_SPECIAL_VALUES[index]
2411
            obj.set_value(val)
2412

    
2413
        if _mem.dtmf_settings.groupcode not in LIST_DTMF_SPECIAL_VALUES:
2414
            val = 0x0B
2415
        else:
2416
            val = _mem.dtmf_settings.groupcode
2417
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2418
        line = RadioSetting(
2419
            "dtmf_settings.groupcode",
2420
            "Group Code",
2421
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2422
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2423
        line.set_apply_callback(apply_dmtf_listvalue,
2424
                                _mem.dtmf_settings.groupcode)
2425
        dtmf_dec_settings.append(line)
2426

    
2427
        if _mem.dtmf_settings.spacecode not in LIST_DTMF_SPECIAL_VALUES:
2428
            val = 0x0C
2429
        else:
2430
            val = _mem.dtmf_settings.spacecode
2431
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2432
        line = RadioSetting(
2433
            "dtmf_settings.spacecode",
2434
            "Space Code",
2435
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2436
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2437
        line.set_apply_callback(apply_dmtf_listvalue,
2438
                                _mem.dtmf_settings.spacecode)
2439
        dtmf_dec_settings.append(line)
2440

    
2441
        if self.COLOR_LCD:
2442
            if _mem.dtmf_settings.resettime > 0x63:
2443
                val = 0x4F
2444
            else:
2445
                val = _mem.dtmf_settings.resettime
2446
            line = RadioSetting(
2447
                "dtmf_settings.resettime",
2448
                "Reset time",
2449
                RadioSettingValueList(LIST_5TONE_RESET_COLOR,
2450
                                      LIST_5TONE_RESET_COLOR[
2451
                                          val]))
2452
            dtmf_dec_settings.append(line)
2453
        else:
2454
            line = RadioSetting(
2455
                "dtmf_settings.resettime",
2456
                "Reset time",
2457
                RadioSettingValueList(LIST_5TONE_RESET,
2458
                                      LIST_5TONE_RESET[
2459
                                          _mem.dtmf_settings.resettime]))
2460
            dtmf_dec_settings.append(line)
2461

    
2462
        if _mem.dtmf_settings.delayproctime > 0x27:
2463
            val = 0x04
2464
        else:
2465
            val = _mem.dtmf_settings.delayproctime
2466
        line = RadioSetting(
2467
            "dtmf_settings.delayproctime",
2468
            "Delay processing time",
2469
            RadioSettingValueList(LIST_DTMF_DELAY,
2470
                                  LIST_DTMF_DELAY[
2471
                                      val]))
2472
        dtmf_dec_settings.append(line)
2473

    
2474
        # 5 Tone Settings
2475
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2476
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2477

    
2478
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2479
        group_5tone.append(stds_5tone)
2480
        group_5tone.append(codes_5tone)
2481

    
2482
        top.append(group_5tone)
2483

    
2484
        def apply_list_value(setting, obj):
2485
            options = setting.value.get_options()
2486
            obj.set_value(options.index(str(setting.value)))
2487

    
2488
        _5tone_standards = self._memobj._5tone_std_settings
2489
        i = 0
2490
        for standard in _5tone_standards:
2491
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2492
                                          LIST_5TONE_STANDARDS[i])
2493
            stds_5tone.append(std_5tone)
2494

    
2495
            period = standard.period
2496
            if period == 255:
2497
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2498
                          " is not yet configured. Setting to 70ms.")
2499
                period = 5
2500

    
2501
            if period <= len(LIST_5TONE_STANDARD_PERIODS):
2502
                line = RadioSetting(
2503
                    "_5tone_std_settings_" + str(i) + "_period",
2504
                    "Period (ms)", RadioSettingValueList
2505
                    (LIST_5TONE_STANDARD_PERIODS,
2506
                     LIST_5TONE_STANDARD_PERIODS[period]))
2507
                line.set_apply_callback(apply_list_value, standard.period)
2508
                std_5tone.append(line)
2509
            else:
2510
                LOG.debug("Invalid value for 5tone period! Disabling.")
2511

    
2512
            group_tone = standard.group_tone
2513
            if group_tone == 255:
2514
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2515
                          " is not yet configured. Setting to A.")
2516
                group_tone = 10
2517

    
2518
            if group_tone <= len(LIST_5TONE_DIGITS):
2519
                line = RadioSetting(
2520
                    "_5tone_std_settings_" + str(i) + "_grouptone",
2521
                    "Group Tone",
2522
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2523
                                          LIST_5TONE_DIGITS[
2524
                                              group_tone]))
2525
                line.set_apply_callback(apply_list_value,
2526
                                        standard.group_tone)
2527
                std_5tone.append(line)
2528
            else:
2529
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2530

    
2531
            repeat_tone = standard.repeat_tone
2532
            if repeat_tone == 255:
2533
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2534
                          " is not yet configured. Setting to E.")
2535
                repeat_tone = 14
2536

    
2537
            if repeat_tone <= len(LIST_5TONE_DIGITS):
2538
                line = RadioSetting(
2539
                    "_5tone_std_settings_" + str(i) + "_repttone",
2540
                    "Repeat Tone",
2541
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2542
                                          LIST_5TONE_DIGITS[
2543
                                              repeat_tone]))
2544
                line.set_apply_callback(apply_list_value,
2545
                                        standard.repeat_tone)
2546
                std_5tone.append(line)
2547
            else:
2548
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2549
            i = i + 1
2550

    
2551
        def my_apply_5tonestdlist_value(setting, obj):
2552
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2553
                obj.set_value(0xFF)
2554
            else:
2555
                obj.set_value(LIST_5TONE_STANDARDS.
2556
                              index(str(setting.value)))
2557

    
2558
        def apply_5tone_frame(setting, obj):
2559
            LOG.debug("Setting 5 Tone: " + str(setting.value))
2560
            valstring = str(setting.value)
2561
            if len(valstring) == 0:
2562
                for i in range(0, 5):
2563
                    obj[i] = 255
2564
            else:
2565
                validFrame = True
2566
                for i in range(0, 5):
2567
                    currentChar = valstring[i].upper()
2568
                    if currentChar in LIST_5TONE_DIGITS:
2569
                        obj[i] = LIST_5TONE_DIGITS.index(currentChar)
2570
                    else:
2571
                        validFrame = False
2572
                        LOG.debug("invalid char: " + str(currentChar))
2573
                if not validFrame:
2574
                    LOG.debug("setting whole frame to FF")
2575
                    for i in range(0, 5):
2576
                        obj[i] = 255
2577

    
2578
        def validate_5tone_frame(value):
2579
            if (len(str(value)) != 5) and (len(str(value)) != 0):
2580
                msg = ("5 Tone must have 5 digits or 0 digits")
2581
                raise InvalidValueError(msg)
2582
            for digit in str(value):
2583
                if digit.upper() not in LIST_5TONE_DIGITS:
2584
                    msg = (str(digit) + " is not a valid digit for 5tones")
2585
                    raise InvalidValueError(msg)
2586
            return value
2587

    
2588
        def frame2string(frame):
2589
            frameString = ""
2590
            for digit in frame:
2591
                if digit != 255:
2592
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2593
            return frameString
2594

    
2595
        _5tone_codes = self._memobj._5tone_codes
2596
        i = 1
2597
        for code in _5tone_codes:
2598
            code_5tone = RadioSettingGroup("code_5tone_" + str(i),
2599
                                           "5 Tone code " + str(i))
2600
            codes_5tone.append(code_5tone)
2601
            if (code.standard == 255):
2602
                currentVal = 15
2603
            else:
2604
                currentVal = code.standard
2605
            line = RadioSetting("_5tone_code_" + str(i) + "_std",
2606
                                " Standard",
2607
                                RadioSettingValueList(LIST_5TONE_STANDARDS,
2608
                                                      LIST_5TONE_STANDARDS[
2609
                                                          currentVal]))
2610
            line.set_apply_callback(my_apply_5tonestdlist_value,
2611
                                    code.standard)
2612
            code_5tone.append(line)
2613

    
2614
            val = RadioSettingValueString(0, 6,
2615
                                          frame2string(code.frame1), False)
2616
            line = RadioSetting("_5tone_code_" + str(i) + "_frame1",
2617
                                " Frame 1", val)
2618
            val.set_validate_callback(validate_5tone_frame)
2619
            line.set_apply_callback(apply_5tone_frame, code.frame1)
2620
            code_5tone.append(line)
2621

    
2622
            val = RadioSettingValueString(0, 6,
2623
                                          frame2string(code.frame2), False)
2624
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2625
                                " Frame 2", val)
2626
            val.set_validate_callback(validate_5tone_frame)
2627
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2628
            code_5tone.append(line)
2629

    
2630
            val = RadioSettingValueString(0, 6,
2631
                                          frame2string(code.frame3), False)
2632
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2633
                                " Frame 3", val)
2634
            val.set_validate_callback(validate_5tone_frame)
2635
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2636
            code_5tone.append(line)
2637
            i = i + 1
2638

    
2639
        _5_tone_decode1 = RadioSetting(
2640
            "_5tone_settings._5tone_decode_call_frame1",
2641
            "5 Tone decode call Frame 1",
2642
            RadioSettingValueBoolean(
2643
                _mem._5tone_settings._5tone_decode_call_frame1))
2644
        group_5tone.append(_5_tone_decode1)
2645

    
2646
        _5_tone_decode2 = RadioSetting(
2647
            "_5tone_settings._5tone_decode_call_frame2",
2648
            "5 Tone decode call Frame 2",
2649
            RadioSettingValueBoolean(
2650
                _mem._5tone_settings._5tone_decode_call_frame2))
2651
        group_5tone.append(_5_tone_decode2)
2652

    
2653
        _5_tone_decode3 = RadioSetting(
2654
            "_5tone_settings._5tone_decode_call_frame3",
2655
            "5 Tone decode call Frame 3",
2656
            RadioSettingValueBoolean(
2657
                _mem._5tone_settings._5tone_decode_call_frame3))
2658
        group_5tone.append(_5_tone_decode3)
2659

    
2660
        _5_tone_decode_disp1 = RadioSetting(
2661
            "_5tone_settings._5tone_decode_disp_frame1",
2662
            "5 Tone decode disp Frame 1",
2663
            RadioSettingValueBoolean(
2664
                _mem._5tone_settings._5tone_decode_disp_frame1))
2665
        group_5tone.append(_5_tone_decode_disp1)
2666

    
2667
        _5_tone_decode_disp2 = RadioSetting(
2668
            "_5tone_settings._5tone_decode_disp_frame2",
2669
            "5 Tone decode disp Frame 2",
2670
            RadioSettingValueBoolean(
2671
                _mem._5tone_settings._5tone_decode_disp_frame2))
2672
        group_5tone.append(_5_tone_decode_disp2)
2673

    
2674
        _5_tone_decode_disp3 = RadioSetting(
2675
            "_5tone_settings._5tone_decode_disp_frame3",
2676
            "5 Tone decode disp Frame 3",
2677
            RadioSettingValueBoolean(
2678
                _mem._5tone_settings._5tone_decode_disp_frame3))
2679
        group_5tone.append(_5_tone_decode_disp3)
2680

    
2681
        decode_standard = _mem._5tone_settings.decode_standard
2682
        if decode_standard == 255:
2683
            decode_standard = 0
2684
        if decode_standard <= len(LIST_5TONE_STANDARDS_without_none):
2685
            line = RadioSetting("_5tone_settings.decode_standard",
2686
                                "5 Tone-decode Standard",
2687
                                RadioSettingValueList(
2688
                                    LIST_5TONE_STANDARDS_without_none,
2689
                                    LIST_5TONE_STANDARDS_without_none[
2690
                                        decode_standard]))
2691
            group_5tone.append(line)
2692
        else:
2693
            LOG.debug("Invalid decode std...")
2694

    
2695
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2696
        if _5tone_delay1 == 255:
2697
            _5tone_delay1 = 20
2698

    
2699
        if _5tone_delay1 <= len(LIST_5TONE_DELAY):
2700
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2701
                                         LIST_5TONE_DELAY[
2702
                                             _5tone_delay1])
2703
            line = RadioSetting("_5tone_settings._5tone_delay1",
2704
                                "5 Tone Delay Frame 1", list)
2705
            group_5tone.append(line)
2706
        else:
2707
            LOG.debug("Invalid value for 5tone delay (frame1) ! Disabling.")
2708

    
2709
        _5tone_delay2 = _mem._5tone_settings._5tone_delay2
2710
        if _5tone_delay2 == 255:
2711
            _5tone_delay2 = 20
2712
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2713

    
2714
        if _5tone_delay2 <= len(LIST_5TONE_DELAY):
2715
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2716
                                         LIST_5TONE_DELAY[
2717
                                             _5tone_delay2])
2718
            line = RadioSetting("_5tone_settings._5tone_delay2",
2719
                                "5 Tone Delay Frame 2", list)
2720
            group_5tone.append(line)
2721
        else:
2722
            LOG.debug("Invalid value for 5tone delay (frame2)! Disabling.")
2723

    
2724
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2725
        if _5tone_delay3 == 255:
2726
            _5tone_delay3 = 20
2727
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2728

    
2729
        if _5tone_delay3 <= len(LIST_5TONE_DELAY):
2730
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2731
                                         LIST_5TONE_DELAY[
2732
                                             _5tone_delay3])
2733
            line = RadioSetting("_5tone_settings._5tone_delay3",
2734
                                "5 Tone Delay Frame 3", list)
2735
            group_5tone.append(line)
2736
        else:
2737
            LOG.debug("Invalid value for 5tone delay (frame3)! Disabling.")
2738

    
2739
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2740
        if ext_length == 255:
2741
            ext_length = 0
2742
            LOG.debug("1st Tone ext lenght unconfigured! Resetting to 0")
2743

    
2744
        if ext_length <= len(LIST_5TONE_DELAY):
2745
            list = RadioSettingValueList(
2746
                LIST_5TONE_DELAY,
2747
                LIST_5TONE_DELAY[
2748
                    ext_length])
2749
            line = RadioSetting(
2750
                "_5tone_settings._5tone_first_digit_ext_length",
2751
                "First digit extend length", list)
2752
            group_5tone.append(line)
2753
        else:
2754
            LOG.debug("Invalid value for 5tone ext length! Disabling.")
2755

    
2756
        decode_reset_time = _mem._5tone_settings.decode_reset_time
2757
        if decode_reset_time == 255:
2758
            decode_reset_time = 59
2759
            LOG.debug("Decode reset time unconfigured. resetting.")
2760
        if decode_reset_time <= len(LIST_5TONE_RESET):
2761
            list = RadioSettingValueList(
2762
                LIST_5TONE_RESET,
2763
                LIST_5TONE_RESET[
2764
                    decode_reset_time])
2765
            line = RadioSetting("_5tone_settings.decode_reset_time",
2766
                                "Decode reset time", list)
2767
            group_5tone.append(line)
2768
        else:
2769
            LOG.debug("Invalid value decode reset time! Disabling.")
2770

    
2771
        # 2 Tone
2772
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2773
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2774

    
2775
        top.append(encode_2tone)
2776
        top.append(decode_2tone)
2777

    
2778
        duration_1st_tone = self._memobj._2tone.duration_1st_tone
2779
        if duration_1st_tone == 255:
2780
            LOG.debug("Duration of first 2 Tone digit is not yet " +
2781
                      "configured. Setting to 600ms")
2782
            duration_1st_tone = 60
2783

    
2784
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2785
            line = RadioSetting("_2tone.duration_1st_tone",
2786
                                "Duration 1st Tone",
2787
                                RadioSettingValueList(LIST_5TONE_DELAY,
2788
                                                      LIST_5TONE_DELAY[
2789
                                                          duration_1st_tone]))
2790
            encode_2tone.append(line)
2791

    
2792
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2793
        if duration_2nd_tone == 255:
2794
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2795
                      "configured. Setting to 600ms")
2796
            duration_2nd_tone = 60
2797

    
2798
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2799
            line = RadioSetting("_2tone.duration_2nd_tone",
2800
                                "Duration 2nd Tone",
2801
                                RadioSettingValueList(LIST_5TONE_DELAY,
2802
                                                      LIST_5TONE_DELAY[
2803
                                                          duration_2nd_tone]))
2804
            encode_2tone.append(line)
2805

    
2806
        duration_gap = self._memobj._2tone.duration_gap
2807
        if duration_gap == 255:
2808
            LOG.debug("Duration of gap is not yet " +
2809
                      "configured. Setting to 300ms")
2810
            duration_gap = 30
2811

    
2812
        if duration_gap <= len(LIST_5TONE_DELAY):
2813
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2814
                                RadioSettingValueList(LIST_5TONE_DELAY,
2815
                                                      LIST_5TONE_DELAY[
2816
                                                          duration_gap]))
2817
            encode_2tone.append(line)
2818

    
2819
        def _2tone_validate(value):
2820
            if value == 0:
2821
                return 65535
2822
            if value == 65535:
2823
                return value
2824
            if not (300 <= value and value <= 3000):
2825
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
2826
                raise InvalidValueError(msg)
2827
            return value
2828

    
2829
        def apply_2tone_freq(setting, obj):
2830
            val = int(setting.value)
2831
            if (val == 0) or (val == 65535):
2832
                obj.set_value(65535)
2833
            else:
2834
                obj.set_value(val)
2835

    
2836
        i = 1
2837
        for code in self._memobj._2tone._2tone_encode:
2838
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
2839
                                           "Encode Code " + str(i))
2840
            encode_2tone.append(code_2tone)
2841

    
2842
            tmp = code.freq1
2843
            if tmp == 65535:
2844
                tmp = 0
2845
            val1 = RadioSettingValueInteger(0, 65535, tmp)
2846
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
2847
                                 "Frequency 1", val1)
2848
            val1.set_validate_callback(_2tone_validate)
2849
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
2850
            code_2tone.append(freq1)
2851

    
2852
            tmp = code.freq2
2853
            if tmp == 65535:
2854
                tmp = 0
2855
            val2 = RadioSettingValueInteger(0, 65535, tmp)
2856
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
2857
                                 "Frequency 2", val2)
2858
            val2.set_validate_callback(_2tone_validate)
2859
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
2860
            code_2tone.append(freq2)
2861

    
2862
            i = i + 1
2863

    
2864
        decode_reset_time = _mem._2tone.reset_time
2865
        if decode_reset_time == 255:
2866
            decode_reset_time = 59
2867
            LOG.debug("Decode reset time unconfigured. resetting.")
2868
        if decode_reset_time <= len(LIST_5TONE_RESET):
2869
            list = RadioSettingValueList(
2870
                LIST_5TONE_RESET,
2871
                LIST_5TONE_RESET[
2872
                    decode_reset_time])
2873
            line = RadioSetting("_2tone.reset_time",
2874
                                "Decode reset time", list)
2875
            decode_2tone.append(line)
2876
        else:
2877
            LOG.debug("Invalid value decode reset time! Disabling.")
2878

    
2879
        def apply_2tone_freq_pair(setting, obj):
2880
            val = int(setting.value)
2881
            derived_val = 65535
2882
            frqname = str(setting._name[-5:])
2883
            derivedname = "derived_from_" + frqname
2884

    
2885
            if (val == 0):
2886
                val = 65535
2887
                derived_val = 65535
2888
            else:
2889
                derived_val = int(round(2304000.0/val))
2890

    
2891
            obj[frqname].set_value(val)
2892
            obj[derivedname].set_value(derived_val)
2893

    
2894
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
2895
                      derivedname + ": " + str(derived_val))
2896

    
2897
        i = 1
2898
        for decode_code in self._memobj._2tone._2tone_decode:
2899
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
2900
                                                "Decode Code " + str(i))
2901
            decode_2tone.append(_2tone_dec_code)
2902

    
2903
            j = 1
2904
            for dec in decode_code.decs:
2905
                val = dec.dec
2906
                if val == 255:
2907
                    LOG.debug("Dec for Code " + str(i) + " Dec " + str(j) +
2908
                              " is not yet configured. Setting to 0.")
2909
                    val = 0
2910

    
2911
                if val <= len(LIST_2TONE_DEC):
2912
                    line = RadioSetting(
2913
                        "_2tone_dec_settings_" + str(i) + "_dec_" + str(j),
2914
                        "Dec " + str(j), RadioSettingValueList
2915
                        (LIST_2TONE_DEC,
2916
                         LIST_2TONE_DEC[val]))
2917
                    line.set_apply_callback(apply_list_value, dec.dec)
2918
                    _2tone_dec_code.append(line)
2919
                else:
2920
                    LOG.debug("Invalid value for 2tone dec! Disabling.")
2921

    
2922
                val = dec.response
2923
                if val == 255:
2924
                    LOG.debug("Response for Code " + str(i) + " Dec " +
2925
                              str(j) + " is not yet configured. Setting to 0.")
2926
                    val = 0
2927

    
2928
                if val <= len(LIST_2TONE_RESPONSE):
2929
                    line = RadioSetting(
2930
                        "_2tone_dec_settings_" + str(i) + "_resp_" + str(j),
2931
                        "Response " + str(j), RadioSettingValueList
2932
                        (LIST_2TONE_RESPONSE,
2933
                         LIST_2TONE_RESPONSE[val]))
2934
                    line.set_apply_callback(apply_list_value, dec.response)
2935
                    _2tone_dec_code.append(line)
2936
                else:
2937
                    LOG.debug("Invalid value for 2tone response! Disabling.")
2938

    
2939
                val = dec.alert
2940
                if val == 255:
2941
                    LOG.debug("Alert for Code " + str(i) + " Dec " + str(j) +
2942
                              " is not yet configured. Setting to 0.")
2943
                    val = 0
2944

    
2945
                if val <= len(PTTIDCODE_LIST):
2946
                    line = RadioSetting(
2947
                        "_2tone_dec_settings_" + str(i) + "_alert_" + str(j),
2948
                        "Alert " + str(j), RadioSettingValueList
2949
                        (PTTIDCODE_LIST,
2950
                         PTTIDCODE_LIST[val]))
2951
                    line.set_apply_callback(apply_list_value, dec.alert)
2952
                    _2tone_dec_code.append(line)
2953
                else:
2954
                    LOG.debug("Invalid value for 2tone alert! Disabling.")
2955
                j = j + 1
2956

    
2957
            freq = self._memobj._2tone.freqs[i-1]
2958
            for char in ['A', 'B', 'C', 'D']:
2959
                setting_name = "freq" + str(char)
2960

    
2961
                tmp = freq[setting_name]
2962
                if tmp == 65535:
2963
                    tmp = 0
2964
                if tmp != 0:
2965
                    expected = int(round(2304000.0/tmp))
2966
                    from_mem = freq["derived_from_" + setting_name]
2967
                    if expected != from_mem:
2968
                        LOG.error("Expected " + str(expected) +
2969
                                  " but read " + str(from_mem) +
2970
                                  ". Disabling 2Tone Decode Freqs!")
2971
                        break
2972
                val = RadioSettingValueInteger(0, 65535, tmp)
2973
                frq = RadioSetting("2tone_dec_" + str(i) + "_freq" + str(char),
2974
                                   ("Decode Frequency " + str(char)), val)
2975
                val.set_validate_callback(_2tone_validate)
2976
                frq.set_apply_callback(apply_2tone_freq_pair, freq)
2977
                _2tone_dec_code.append(frq)
2978

    
2979
            i = i + 1
2980

    
2981
        return top
2982

    
2983
    def set_settings(self, settings):
2984
        _settings = self._memobj.settings
2985
        for element in settings:
2986
            if not isinstance(element, RadioSetting):
2987
                if element.get_name() == "fm_preset":
2988
                    self._set_fm_preset(element)
2989
                else:
2990
                    self.set_settings(element)
2991
                    continue
2992
            else:
2993
                try:
2994
                    name = element.get_name()
2995
                    if "." in name:
2996
                        bits = name.split(".")
2997
                        obj = self._memobj
2998
                        for bit in bits[:-1]:
2999
                            if "/" in bit:
3000
                                bit, index = bit.split("/", 1)
3001
                                index = int(index)
3002
                                obj = getattr(obj, bit)[index]
3003
                            else:
3004
                                obj = getattr(obj, bit)
3005
                        setting = bits[-1]
3006
                    else:
3007
                        obj = _settings
3008
                        setting = element.get_name()
3009

    
3010
                    if element.has_apply_callback():
3011
                        LOG.debug("Using apply callback")
3012
                        element.run_apply_callback()
3013
                    elif element.value.get_mutable():
3014
                        LOG.debug("Setting %s = %s" % (setting, element.value))
3015
                        setattr(obj, setting, element.value)
3016
                except Exception, e:
3017
                    LOG.debug(element.get_name())
3018
                    raise
3019

    
3020
    @classmethod
3021
    def match_model(cls, filedata, filename):
3022
        match_size = False
3023
        match_model = False
3024

    
3025
        # testing the file data size
3026
        if len(filedata) == MEM_SIZE:
3027
            match_size = True
3028

    
3029
        # testing the firmware model fingerprint
3030
        match_model = model_match(cls, filedata)
3031

    
3032
        if match_size and match_model:
3033
            return True
3034
        else:
3035
            return False
3036

    
3037

    
3038
MEM_FORMAT = """
3039
#seekto 0x0000;
3040
struct {
3041
  lbcd rxfreq[4];
3042
  lbcd txfreq[4];
3043
  ul16 rxtone;
3044
  ul16 txtone;
3045
  u8 unknown0:4,
3046
     scode:4;
3047
  u8 unknown1:2,
3048
     spmute:2,
3049
     unknown2:2,
3050
     optsig:2;
3051
  u8 unknown3:3,
3052
     scramble:1,
3053
     unknown4:3,
3054
     power:1;
3055
  u8 unknown5:1,
3056
     wide:1,
3057
     unknown6:2,
3058
     bcl:1,
3059
     add:1,
3060
     pttid:2;
3061
} memory[200];
3062

    
3063
#seekto 0x0E00;
3064
struct {
3065
  u8 tdr;
3066
  u8 unknown1;
3067
  u8 sql;
3068
  u8 unknown2[2];
3069
  u8 tot;
3070
  u8 apo;           // BTech radios use this as the Auto Power Off time
3071
                    // other radios use this as pre-Time Out Alert
3072
  u8 unknown3;
3073
  u8 abr;
3074
  u8 beep;
3075
  u8 unknown4[4];
3076
  u8 dtmfst;
3077
  u8 unknown5[2];
3078
  u8 prisc;
3079
  u8 prich;
3080
  u8 screv;
3081
  u8 unknown6[2];
3082
  u8 pttid;
3083
  u8 pttlt;
3084
  u8 unknown7;
3085
  u8 emctp;
3086
  u8 emcch;
3087
  u8 ringt;
3088
  u8 unknown8;
3089
  u8 camdf;
3090
  u8 cbmdf;
3091
  u8 sync;          // BTech radios use this as the display sync setting
3092
                    // other radios use this as the auto keypad lock setting
3093
  u8 ponmsg;
3094
  u8 wtled;
3095
  u8 rxled;
3096
  u8 txled;
3097
  u8 unknown9[5];
3098
  u8 anil;
3099
  u8 reps;
3100
  u8 repm;
3101
  u8 tdrab;
3102
  u8 ste;
3103
  u8 rpste;
3104
  u8 rptdl;
3105
  u8 mgain;
3106
  u8 dtmfg;
3107
} settings;
3108

    
3109
#seekto 0x0E80;
3110
struct {
3111
  u8 unknown1;
3112
  u8 vfomr;
3113
  u8 keylock;
3114
  u8 unknown2;
3115
  u8 unknown3:4,
3116
     vfomren:1,
3117
     unknown4:1,
3118
     reseten:1,
3119
     menuen:1;
3120
  u8 unknown5[11];
3121
  u8 dispab;
3122
  u8 mrcha;
3123
  u8 mrchb;
3124
  u8 menu;
3125
} settings2;
3126

    
3127
#seekto 0x0EC0;
3128
struct {
3129
  char line1[6];
3130
  char line2[6];
3131
} poweron_msg;
3132

    
3133
struct settings_vfo {
3134
  u8 freq[8];
3135
  u8 offset[6];
3136
  u8 unknown2[2];
3137
  ul16 rxtone;
3138
  ul16 txtone;
3139
  u8 scode;
3140
  u8 spmute;
3141
  u8 optsig;
3142
  u8 scramble;
3143
  u8 wide;
3144
  u8 power;
3145
  u8 shiftd;
3146
  u8 step;
3147
  u8 unknown3[4];
3148
};
3149

    
3150
#seekto 0x0F00;
3151
struct {
3152
  struct settings_vfo a;
3153
  struct settings_vfo b;
3154
} vfo;
3155

    
3156
#seekto 0x1000;
3157
struct {
3158
  char name[6];
3159
  u8 unknown1[10];
3160
} names[200];
3161

    
3162
#seekto 0x2400;
3163
struct {
3164
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3165
  u8 group_tone;
3166
  u8 repeat_tone;
3167
  u8 unused[13];
3168
} _5tone_std_settings[15];
3169

    
3170
#seekto 0x2500;
3171
struct {
3172
  u8 frame1[5];
3173
  u8 frame2[5];
3174
  u8 frame3[5];
3175
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3176
} _5tone_codes[15];
3177

    
3178
#seekto 0x25F0;
3179
struct {
3180
  u8 _5tone_delay1; // * 10ms
3181
  u8 _5tone_delay2; // * 10ms
3182
  u8 _5tone_delay3; // * 10ms
3183
  u8 _5tone_first_digit_ext_length;
3184
  u8 unknown1;
3185
  u8 unknown2;
3186
  u8 unknown3;
3187
  u8 unknown4;
3188
  u8 decode_standard;
3189
  u8 unknown5:5,
3190
     _5tone_decode_call_frame3:1,
3191
     _5tone_decode_call_frame2:1,
3192
     _5tone_decode_call_frame1:1;
3193
  u8 unknown6:5,
3194
     _5tone_decode_disp_frame3:1,
3195
     _5tone_decode_disp_frame2:1,
3196
     _5tone_decode_disp_frame1:1;
3197
  u8 decode_reset_time; // * 100 + 100ms
3198
} _5tone_settings;
3199

    
3200
#seekto 0x2900;
3201
struct {
3202
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3203
} dtmf_codes[15];
3204

    
3205
#seekto 0x29F0;
3206
struct {
3207
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3208
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3209
  u8 unknown0[14];
3210
  u8 inspection[16];
3211
  u8 monitor[16];
3212
  u8 alarmcode[16];
3213
  u8 stun[16];
3214
  u8 kill[16];
3215
  u8 revive[16];
3216
  u8 unknown1[16];
3217
  u8 unknown2[16];
3218
  u8 unknown3[16];
3219
  u8 unknown4[16];
3220
  u8 unknown5[16];
3221
  u8 unknown6[16];
3222
  u8 unknown7[16];
3223
  u8 masterid[16];
3224
  u8 viceid[16];
3225
  u8 unused01:7,
3226
     mastervice:1;
3227
  u8 unused02:3,
3228
     mrevive:1,
3229
     mkill:1,
3230
     mstun:1,
3231
     mmonitor:1,
3232
     minspection:1;
3233
  u8 unused03:3,
3234
     vrevive:1,
3235
     vkill:1,
3236
     vstun:1,
3237
     vmonitor:1,
3238
     vinspection:1;
3239
  u8 unused04:6,
3240
     txdisable:1,
3241
     rxdisable:1;
3242
  u8 groupcode;
3243
  u8 spacecode;
3244
  u8 delayproctime; // * 100 + 100ms
3245
  u8 resettime;     // * 100 + 100ms
3246
} dtmf_settings;
3247

    
3248
#seekto 0x2D00;
3249
struct {
3250
  struct {
3251
    ul16 freq1;
3252
    u8 unused01[6];
3253
    ul16 freq2;
3254
    u8 unused02[6];
3255
  } _2tone_encode[15];
3256
  u8 duration_1st_tone; // *10ms
3257
  u8 duration_2nd_tone; // *10ms
3258
  u8 duration_gap;      // *10ms
3259
  u8 unused03[13];
3260
  struct {
3261
    struct {
3262
      u8 dec;      // one out of LIST_2TONE_DEC
3263
      u8 response; // one out of LIST_2TONE_RESPONSE
3264
      u8 alert;    // 1-16
3265
    } decs[4];
3266
    u8 unused04[4];
3267
  } _2tone_decode[15];
3268
  u8 unused05[16];
3269

    
3270
  struct {
3271
    ul16 freqA;
3272
    ul16 freqB;
3273
    ul16 freqC;
3274
    ul16 freqD;
3275
    // unknown what those values mean, but they are
3276
    // derived from configured frequencies
3277
    ul16 derived_from_freqA; // 2304000/freqA
3278
    ul16 derived_from_freqB; // 2304000/freqB
3279
    ul16 derived_from_freqC; // 2304000/freqC
3280
    ul16 derived_from_freqD; // 2304000/freqD
3281
  }freqs[15];
3282
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3283
} _2tone;
3284

    
3285
#seekto 0x3000;
3286
struct {
3287
  u8 freq[8];
3288
  char broadcast_station_name[6];
3289
  u8 unknown[2];
3290
} fm_radio_preset[16];
3291

    
3292
#seekto 0x3C90;
3293
struct {
3294
  u8 vhf_low[3];
3295
  u8 vhf_high[3];
3296
  u8 uhf_low[3];
3297
  u8 uhf_high[3];
3298
} ranges;
3299

    
3300
// the UV-2501+220 & KT8900R has different zones for storing ranges
3301

    
3302
#seekto 0x3CD0;
3303
struct {
3304
  u8 vhf_low[3];
3305
  u8 vhf_high[3];
3306
  u8 unknown1[4];
3307
  u8 unknown2[6];
3308
  u8 vhf2_low[3];
3309
  u8 vhf2_high[3];
3310
  u8 unknown3[4];
3311
  u8 unknown4[6];
3312
  u8 uhf_low[3];
3313
  u8 uhf_high[3];
3314
} ranges220;
3315

    
3316
#seekto 0x3F70;
3317
struct {
3318
  char fp[6];
3319
} fingerprint;
3320

    
3321
"""
3322

    
3323

    
3324
class BTech(BTechMobileCommon):
3325
    """BTECH's UV-5001 and alike radios"""
3326
    BANDS = 2
3327
    COLOR_LCD = False
3328
    NAME_LENGTH = 6
3329

    
3330
    def set_options(self):
3331
        """This is to read the options from the image and set it in the
3332
        environment, for now just the limits of the freqs in the VHF/UHF
3333
        ranges"""
3334

    
3335
        # setting the correct ranges for each radio type
3336
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3337
            # the model 2501+220 has a segment in 220
3338
            # and a different position in the memmap
3339
            # also the QYT KT8900R
3340
            ranges = self._memobj.ranges220
3341
        else:
3342
            ranges = self._memobj.ranges
3343

    
3344
        # the normal dual bands
3345
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3346
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3347

    
3348
        # DEBUG
3349
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3350
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3351

    
3352
        # 220Mhz radios case
3353
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3354
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3355
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3356
            self._220_range = vhf2
3357

    
3358
        # set the class with the real data
3359
        self._vhf_range = vhf
3360
        self._uhf_range = uhf
3361

    
3362
    def process_mmap(self):
3363
        """Process the mem map into the mem object"""
3364

    
3365
        # Get it
3366
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3367

    
3368
        # load specific parameters from the radio image
3369
        self.set_options()
3370

    
3371

    
3372
# Declaring Aliases (Clones of the real radios)
3373
class JT2705M(chirp_common.Alias):
3374
    VENDOR = "Jetstream"
3375
    MODEL = "JT2705M"
3376

    
3377

    
3378
class JT6188Mini(chirp_common.Alias):
3379
    VENDOR = "Juentai"
3380
    MODEL = "JT-6188 Mini"
3381

    
3382

    
3383
class JT6188Plus(chirp_common.Alias):
3384
    VENDOR = "Juentai"
3385
    MODEL = "JT-6188 Plus"
3386

    
3387

    
3388
class SSGT890(chirp_common.Alias):
3389
    VENDOR = "Sainsonic"
3390
    MODEL = "GT-890"
3391

    
3392

    
3393
class ZastoneMP300(chirp_common.Alias):
3394
    VENDOR = "Zastone"
3395
    MODEL = "MP-300"
3396

    
3397

    
3398
# real radios
3399
@directory.register
3400
class UV2501(BTech):
3401
    """Baofeng Tech UV2501"""
3402
    MODEL = "UV-2501"
3403
    _fileid = [UV2501G3_fp,
3404
               UV2501G2_fp,
3405
               UV2501pp2_fp,
3406
               UV2501pp_fp]
3407

    
3408

    
3409
@directory.register
3410
class UV2501_220(BTech):
3411
    """Baofeng Tech UV2501+220"""
3412
    MODEL = "UV-2501+220"
3413
    BANDS = 3
3414
    _magic = MSTRING_220
3415
    _id2 = [UV2501_220pp_id, ]
3416
    _fileid = [UV2501_220G3_fp,
3417
               UV2501_220G2_fp,
3418
               UV2501_220_fp,
3419
               UV2501_220pp_fp]
3420

    
3421

    
3422
@directory.register
3423
class UV5001(BTech):
3424
    """Baofeng Tech UV5001"""
3425
    MODEL = "UV-5001"
3426
    _fileid = [UV5001G3_fp,
3427
               UV5001G22_fp,
3428
               UV5001G2_fp,
3429
               UV5001alpha_fp,
3430
               UV5001pp_fp]
3431
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3432
                     chirp_common.PowerLevel("Low", watts=10)]
3433

    
3434

    
3435
@directory.register
3436
class MINI8900(BTech):
3437
    """WACCOM MINI-8900"""
3438
    VENDOR = "WACCOM"
3439
    MODEL = "MINI-8900"
3440
    _magic = MSTRING_MINI8900
3441
    _fileid = [MINI8900_fp, ]
3442
    # Clones
3443
    ALIASES = [JT6188Plus, ]
3444

    
3445

    
3446
@directory.register
3447
class KTUV980(BTech):
3448
    """QYT KT-UV980"""
3449
    VENDOR = "QYT"
3450
    MODEL = "KT-UV980"
3451
    _vhf_range = (136000000, 175000000)
3452
    _uhf_range = (400000000, 481000000)
3453
    _magic = MSTRING_MINI8900
3454
    _fileid = [KTUV980_fp, ]
3455
    # Clones
3456
    ALIASES = [JT2705M, ]
3457

    
3458
# Please note that there is a version of this radios that is a clone of the
3459
# Waccom Mini8900, maybe an early version?
3460

    
3461

    
3462
class OTGRadioV1(chirp_common.Alias):
3463
    VENDOR = 'OTGSTUFF'
3464
    MODEL = 'OTG Radio v1'
3465

    
3466

    
3467
@directory.register
3468
class KT9800(BTech):
3469
    """QYT KT8900"""
3470
    VENDOR = "QYT"
3471
    MODEL = "KT8900"
3472
    _vhf_range = (136000000, 175000000)
3473
    _uhf_range = (400000000, 481000000)
3474
    _magic = MSTRING_KT8900
3475
    _fileid = [KT8900_fp,
3476
               KT8900_fp1,
3477
               KT8900_fp2,
3478
               KT8900_fp3,
3479
               KT8900_fp4,
3480
               KT8900_fp5,
3481
               KT8900_fp6]
3482
    _id2 = [KT8900_id, ]
3483
    # Clones
3484
    ALIASES = [JT6188Mini, SSGT890, ZastoneMP300]
3485

    
3486

    
3487
@directory.register
3488
class KT9800R(BTech):
3489
    """QYT KT8900R"""
3490
    VENDOR = "QYT"
3491
    MODEL = "KT8900R"
3492
    BANDS = 3
3493
    _vhf_range = (136000000, 175000000)
3494
    _220_range = (240000000, 271000000)
3495
    _uhf_range = (400000000, 481000000)
3496
    _magic = MSTRING_KT8900R
3497
    _fileid = [KT8900R_fp,
3498
               KT8900R_fp1,
3499
               KT8900R_fp2,
3500
               KT8900R_fp3,
3501
               KT8900R_fp4]
3502
    _id2 = [KT8900R_id, KT8900R_id2]
3503

    
3504

    
3505
@directory.register
3506
class LT588UV(BTech):
3507
    """LUITON LT-588UV"""
3508
    VENDOR = "LUITON"
3509
    MODEL = "LT-588UV"
3510
    _vhf_range = (136000000, 175000000)
3511
    _uhf_range = (400000000, 481000000)
3512
    _magic = MSTRING_KT8900
3513
    _fileid = [LT588UV_fp,
3514
               LT588UV_fp1]
3515
    _power_levels = [chirp_common.PowerLevel("High", watts=60),
3516
                     chirp_common.PowerLevel("Low", watts=10)]
3517

    
3518

    
3519
COLOR_MEM_FORMAT = """
3520
#seekto 0x0000;
3521
struct {
3522
  lbcd rxfreq[4];
3523
  lbcd txfreq[4];
3524
  ul16 rxtone;
3525
  ul16 txtone;
3526
  u8 unknown0:4,
3527
     scode:4;
3528
  u8 unknown1:2,
3529
     spmute:2,
3530
     unknown2:2,
3531
     optsig:2;
3532
  u8 unknown3:3,
3533
     scramble:1,
3534
     unknown4:3,
3535
     power:1;
3536
  u8 unknown5:1,
3537
     wide:1,
3538
     unknown6:2,
3539
     bcl:1,
3540
     add:1,
3541
     pttid:2;
3542
} memory[200];
3543

    
3544
#seekto 0x0E00;
3545
struct {
3546
  u8 tmr;
3547
  u8 unknown1;
3548
  u8 sql;
3549
  u8 unknown2[2];
3550
  u8 tot;
3551
  u8 apo;
3552
  u8 unknown3;
3553
  u8 abr;
3554
  u8 beep;
3555
  u8 unknown4[4];
3556
  u8 dtmfst;
3557
  u8 unknown5[2];
3558
  u8 screv;
3559
  u8 unknown6[2];
3560
  u8 pttid;
3561
  u8 pttlt;
3562
  u8 unknown7;
3563
  u8 emctp;
3564
  u8 emcch;
3565
  u8 sigbp;
3566
  u8 unknown8;
3567
  u8 camdf;
3568
  u8 cbmdf;
3569
  u8 ccmdf;
3570
  u8 cdmdf;
3571
  u8 langua;
3572
  u8 sync;          // BTech radios use this as the display sync
3573
                    // setting, other radios use this as the auto
3574
                    // keypad lock setting
3575
  u8 mainfc;
3576
  u8 mainbc;
3577
  u8 menufc;
3578
  u8 menubc;
3579
  u8 stafc;
3580
  u8 stabc;
3581
  u8 sigfc;
3582
  u8 sigbc;
3583
  u8 rxfc;
3584
  u8 txfc;
3585
  u8 txdisp;
3586
  u8 unknown9[5];
3587
  u8 anil;
3588
  u8 reps;
3589
  u8 repm;
3590
  u8 tmrmr;
3591
  u8 ste;
3592
  u8 rpste;
3593
  u8 rptdl;
3594
  u8 dtmfg;
3595
  u8 mgain;
3596
  u8 skiptx;
3597
  u8 scmode;
3598
} settings;
3599

    
3600
#seekto 0x0E80;
3601
struct {
3602
  u8 unknown1;
3603
  u8 vfomr;
3604
  u8 keylock;
3605
  u8 unknown2;
3606
  u8 unknown3:4,
3607
     vfomren:1,
3608
     unknown4:1,
3609
     reseten:1,
3610
     menuen:1;
3611
  u8 unknown5[11];
3612
  u8 dispab;
3613
  u8 unknown6[2];
3614
  u8 menu;
3615
  u8 unknown7[7];
3616
  u8 vfomra;
3617
  u8 vfomrb;
3618
  u8 vfomrc;
3619
  u8 vfomrd;
3620
  u8 mrcha;
3621
  u8 mrchb;
3622
  u8 mrchc;
3623
  u8 mrchd;
3624
} settings2;
3625

    
3626
struct settings_vfo {
3627
  u8 freq[8];
3628
  u8 offset[6];
3629
  u8 unknown2[2];
3630
  ul16 rxtone;
3631
  ul16 txtone;
3632
  u8 scode;
3633
  u8 spmute;
3634
  u8 optsig;
3635
  u8 scramble;
3636
  u8 wide;
3637
  u8 power;
3638
  u8 shiftd;
3639
  u8 step;
3640
  u8 unknown3[4];
3641
};
3642

    
3643
#seekto 0x0F00;
3644
struct {
3645
  struct settings_vfo a;
3646
  struct settings_vfo b;
3647
  struct settings_vfo c;
3648
  struct settings_vfo d;
3649
} vfo;
3650

    
3651
#seekto 0x0F80;
3652
struct {
3653
  char line1[8];
3654
  char line2[8];
3655
  char line3[8];
3656
  char line4[8];
3657
  char line5[8];
3658
  char line6[8];
3659
  char line7[8];
3660
  char line8[8];
3661
} poweron_msg;
3662

    
3663
#seekto 0x1000;
3664
struct {
3665
  char name[8];
3666
  u8 unknown1[8];
3667
} names[200];
3668

    
3669
#seekto 0x2400;
3670
struct {
3671
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3672
  u8 group_tone;
3673
  u8 repeat_tone;
3674
  u8 unused[13];
3675
} _5tone_std_settings[15];
3676

    
3677
#seekto 0x2500;
3678
struct {
3679
  u8 frame1[5];
3680
  u8 frame2[5];
3681
  u8 frame3[5];
3682
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3683
} _5tone_codes[15];
3684

    
3685
#seekto 0x25F0;
3686
struct {
3687
  u8 _5tone_delay1; // * 10ms
3688
  u8 _5tone_delay2; // * 10ms
3689
  u8 _5tone_delay3; // * 10ms
3690
  u8 _5tone_first_digit_ext_length;
3691
  u8 unknown1;
3692
  u8 unknown2;
3693
  u8 unknown3;
3694
  u8 unknown4;
3695
  u8 decode_standard;
3696
  u8 unknown5:5,
3697
     _5tone_decode_call_frame3:1,
3698
     _5tone_decode_call_frame2:1,
3699
     _5tone_decode_call_frame1:1;
3700
  u8 unknown6:5,
3701
     _5tone_decode_disp_frame3:1,
3702
     _5tone_decode_disp_frame2:1,
3703
     _5tone_decode_disp_frame1:1;
3704
  u8 decode_reset_time; // * 100 + 100ms
3705
} _5tone_settings;
3706

    
3707
#seekto 0x2900;
3708
struct {
3709
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3710
} dtmf_codes[15];
3711

    
3712
#seekto 0x29F0;
3713
struct {
3714
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3715
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3716
  u8 unknown0[14];
3717
  u8 inspection[16];
3718
  u8 monitor[16];
3719
  u8 alarmcode[16];
3720
  u8 stun[16];
3721
  u8 kill[16];
3722
  u8 revive[16];
3723
  u8 unknown1[16];
3724
  u8 unknown2[16];
3725
  u8 unknown3[16];
3726
  u8 unknown4[16];
3727
  u8 unknown5[16];
3728
  u8 unknown6[16];
3729
  u8 unknown7[16];
3730
  u8 masterid[16];
3731
  u8 viceid[16];
3732
  u8 unused01:7,
3733
     mastervice:1;
3734
  u8 unused02:3,
3735
     mrevive:1,
3736
     mkill:1,
3737
     mstun:1,
3738
     mmonitor:1,
3739
     minspection:1;
3740
  u8 unused03:3,
3741
     vrevive:1,
3742
     vkill:1,
3743
     vstun:1,
3744
     vmonitor:1,
3745
     vinspection:1;
3746
  u8 unused04:6,
3747
     txdisable:1,
3748
     rxdisable:1;
3749
  u8 groupcode;
3750
  u8 spacecode;
3751
  u8 delayproctime; // * 100 + 100ms
3752
  u8 resettime;     // * 100 + 100ms
3753
} dtmf_settings;
3754

    
3755
#seekto 0x2D00;
3756
struct {
3757
  struct {
3758
    ul16 freq1;
3759
    u8 unused01[6];
3760
    ul16 freq2;
3761
    u8 unused02[6];
3762
  } _2tone_encode[15];
3763
  u8 duration_1st_tone; // *10ms
3764
  u8 duration_2nd_tone; // *10ms
3765
  u8 duration_gap;      // *10ms
3766
  u8 unused03[13];
3767
  struct {
3768
    struct {
3769
      u8 dec;      // one out of LIST_2TONE_DEC
3770
      u8 response; // one out of LIST_2TONE_RESPONSE
3771
      u8 alert;    // 1-16
3772
    } decs[4];
3773
    u8 unused04[4];
3774
  } _2tone_decode[15];
3775
  u8 unused05[16];
3776

    
3777
  struct {
3778
    ul16 freqA;
3779
    ul16 freqB;
3780
    ul16 freqC;
3781
    ul16 freqD;
3782
    // unknown what those values mean, but they are
3783
    // derived from configured frequencies
3784
    ul16 derived_from_freqA; // 2304000/freqA
3785
    ul16 derived_from_freqB; // 2304000/freqB
3786
    ul16 derived_from_freqC; // 2304000/freqC
3787
    ul16 derived_from_freqD; // 2304000/freqD
3788
  }freqs[15];
3789
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3790
} _2tone;
3791

    
3792
#seekto 0x3D80;
3793
struct {
3794
  u8 vhf_low[3];
3795
  u8 vhf_high[3];
3796
  u8 unknown1[4];
3797
  u8 unknown2[6];
3798
  u8 vhf2_low[3];
3799
  u8 vhf2_high[3];
3800
  u8 unknown3[4];
3801
  u8 unknown4[6];
3802
  u8 uhf_low[3];
3803
  u8 uhf_high[3];
3804
  u8 unknown5[4];
3805
  u8 unknown6[6];
3806
  u8 uhf2_low[3];
3807
  u8 uhf2_high[3];
3808
} ranges;
3809

    
3810
#seekto 0x3F70;
3811
struct {
3812
  char fp[6];
3813
} fingerprint;
3814

    
3815
"""
3816

    
3817

    
3818
class BTechColor(BTechMobileCommon):
3819
    """BTECH's Color LCD Mobile and alike radios"""
3820
    COLOR_LCD = True
3821
    NAME_LENGTH = 8
3822
    LIST_TMR = LIST_TMR16
3823

    
3824
    def process_mmap(self):
3825
        """Process the mem map into the mem object"""
3826

    
3827
        # Get it
3828
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
3829

    
3830
        # load specific parameters from the radio image
3831
        self.set_options()
3832

    
3833
    def set_options(self):
3834
        """This is to read the options from the image and set it in the
3835
        environment, for now just the limits of the freqs in the VHF/UHF
3836
        ranges"""
3837

    
3838
        # setting the correct ranges for each radio type
3839
        ranges = self._memobj.ranges
3840

    
3841
        # the normal dual bands
3842
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3843
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3844

    
3845
        # DEBUG
3846
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3847
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3848

    
3849
        # the additional bands
3850
        if self.MODEL in ["UV-25X4", "KT7900D"]:
3851
            # 200Mhz band
3852
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3853
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3854
            self._220_range = vhf2
3855

    
3856
            # 350Mhz band
3857
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
3858
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
3859
            self._350_range = uhf2
3860

    
3861
        # set the class with the real data
3862
        self._vhf_range = vhf
3863
        self._uhf_range = uhf
3864

    
3865

    
3866
# Declaring Aliases (Clones of the real radios)
3867
class SKT8900D(chirp_common.Alias):
3868
    VENDOR = "Surecom"
3869
    MODEL = "S-KT8900D"
3870

    
3871

    
3872
class QB25(chirp_common.Alias):
3873
    VENDOR = "Radioddity"
3874
    MODEL = "QB25"
3875

    
3876

    
3877
# real radios
3878
@directory.register
3879
class UV25X2(BTechColor):
3880
    """Baofeng Tech UV25X2"""
3881
    MODEL = "UV-25X2"
3882
    BANDS = 2
3883
    _vhf_range = (130000000, 180000000)
3884
    _uhf_range = (400000000, 521000000)
3885
    _magic = MSTRING_UV25X2
3886
    _fileid = [UV25X2_fp, ]
3887

    
3888

    
3889
@directory.register
3890
class UV25X4(BTechColor):
3891
    """Baofeng Tech UV25X4"""
3892
    MODEL = "UV-25X4"
3893
    BANDS = 4
3894
    _vhf_range = (130000000, 180000000)
3895
    _220_range = (200000000, 271000000)
3896
    _uhf_range = (400000000, 521000000)
3897
    _350_range = (350000000, 391000000)
3898
    _magic = MSTRING_UV25X4
3899
    _fileid = [UV25X4_fp, ]
3900

    
3901

    
3902
@directory.register
3903
class UV50X2(BTechColor):
3904
    """Baofeng Tech UV50X2"""
3905
    MODEL = "UV-50X2"
3906
    BANDS = 2
3907
    _vhf_range = (130000000, 180000000)
3908
    _uhf_range = (400000000, 521000000)
3909
    _magic = MSTRING_UV25X2
3910
    _fileid = [UV50X2_fp, ]
3911
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3912
                     chirp_common.PowerLevel("Low", watts=10)]
3913

    
3914

    
3915
@directory.register
3916
class KT7900D(BTechColor):
3917
    """QYT KT7900D"""
3918
    VENDOR = "QYT"
3919
    MODEL = "KT7900D"
3920
    BANDS = 4
3921
    LIST_TMR = LIST_TMR15
3922
    _vhf_range = (136000000, 175000000)
3923
    _220_range = (200000000, 271000000)
3924
    _uhf_range = (400000000, 481000000)
3925
    _350_range = (350000000, 371000000)
3926
    _magic = MSTRING_KT8900D
3927
    _fileid = [KT7900D_fp, KT7900D_fp1, KT7900D_fp2, KT7900D_fp3, KT7900D_fp4,
3928
               KT7900D_fp5, KT7900D_fp6, QB25_fp, ]
3929
    # Clones
3930
    ALIASES = [SKT8900D, QB25, ]
3931

    
3932

    
3933
@directory.register
3934
class KT8900D(BTechColor):
3935
    """QYT KT8900D"""
3936
    VENDOR = "QYT"
3937
    MODEL = "KT8900D"
3938
    BANDS = 2
3939
    LIST_TMR = LIST_TMR15
3940
    _vhf_range = (136000000, 175000000)
3941
    _uhf_range = (400000000, 481000000)
3942
    _magic = MSTRING_KT8900D
3943
    _fileid = [KT8900D_fp2, KT8900D_fp, KT8900D_fp1]
3944

    
3945
    # Clones
3946
    ALIASES = [OTGRadioV1]
3947

    
3948

    
3949
@directory.register
3950
class KT5800(BTechColor):
3951
    """QYT KT5800"""
3952
    VENDOR = "QYT"
3953
    MODEL = "KT5800"
3954
    BANDS = 2
3955
    LIST_TMR = LIST_TMR15
3956
    _vhf_range = (136000000, 175000000)
3957
    _uhf_range = (400000000, 481000000)
3958
    _magic = MSTRING_KT8900D
3959
    _fileid = [KT5800_fp, ]
3960

    
3961

    
3962
GMRS_MEM_FORMAT = """
3963
#seekto 0x0000;
3964
struct {
3965
  lbcd rxfreq[4];
3966
  lbcd txfreq[4];
3967
  ul16 rxtone;
3968
  ul16 txtone;
3969
  u8 unknown0:4,
3970
     scode:4;
3971
  u8 unknown1:2,
3972
     spmute:2,
3973
     unknown2:2,
3974
     optsig:2;
3975
  u8 unknown3:3,
3976
     scramble:1,
3977
     unknown4:2,
3978
     power:2;
3979
  u8 unknown5:1,
3980
     wide:1,
3981
     unknown6:2,
3982
     bcl:1,
3983
     add:1,
3984
     pttid:2;
3985
} memory[256];
3986

    
3987
#seekto 0x1000;
3988
struct {
3989
  char name[7];
3990
  u8 unknown1[9];
3991
} names[256];
3992

    
3993
#seekto 0x2400;
3994
struct {
3995
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3996
  u8 group_tone;
3997
  u8 repeat_tone;
3998
  u8 unused[13];
3999
} _5tone_std_settings[15];
4000

    
4001
#seekto 0x2500;
4002
struct {
4003
  u8 frame1[5];
4004
  u8 frame2[5];
4005
  u8 frame3[5];
4006
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4007
} _5tone_codes[15];
4008

    
4009
#seekto 0x25F0;
4010
struct {
4011
  u8 _5tone_delay1; // * 10ms
4012
  u8 _5tone_delay2; // * 10ms
4013
  u8 _5tone_delay3; // * 10ms
4014
  u8 _5tone_first_digit_ext_length;
4015
  u8 unknown1;
4016
  u8 unknown2;
4017
  u8 unknown3;
4018
  u8 unknown4;
4019
  u8 decode_standard;
4020
  u8 unknown5:5,
4021
     _5tone_decode_call_frame3:1,
4022
     _5tone_decode_call_frame2:1,
4023
     _5tone_decode_call_frame1:1;
4024
  u8 unknown6:5,
4025
     _5tone_decode_disp_frame3:1,
4026
     _5tone_decode_disp_frame2:1,
4027
     _5tone_decode_disp_frame1:1;
4028
  u8 decode_reset_time; // * 100 + 100ms
4029
} _5tone_settings;
4030

    
4031
#seekto 0x2900;
4032
struct {
4033
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4034
} dtmf_codes[15];
4035

    
4036
#seekto 0x29F0;
4037
struct {
4038
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4039
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4040
  u8 unknown0[14];
4041
  u8 inspection[16];
4042
  u8 monitor[16];
4043
  u8 alarmcode[16];
4044
  u8 stun[16];
4045
  u8 kill[16];
4046
  u8 revive[16];
4047
  u8 unknown1[16];
4048
  u8 unknown2[16];
4049
  u8 unknown3[16];
4050
  u8 unknown4[16];
4051
  u8 unknown5[16];
4052
  u8 unknown6[16];
4053
  u8 unknown7[16];
4054
  u8 masterid[16];
4055
  u8 viceid[16];
4056
  u8 unused01:7,
4057
     mastervice:1;
4058
  u8 unused02:3,
4059
     mrevive:1,
4060
     mkill:1,
4061
     mstun:1,
4062
     mmonitor:1,
4063
     minspection:1;
4064
  u8 unused03:3,
4065
     vrevive:1,
4066
     vkill:1,
4067
     vstun:1,
4068
     vmonitor:1,
4069
     vinspection:1;
4070
  u8 unused04:6,
4071
     txdisable:1,
4072
     rxdisable:1;
4073
  u8 groupcode;
4074
  u8 spacecode;
4075
  u8 delayproctime; // * 100 + 100ms
4076
  u8 resettime;     // * 100 + 100ms
4077
} dtmf_settings;
4078

    
4079
#seekto 0x2D00;
4080
struct {
4081
  struct {
4082
    ul16 freq1;
4083
    u8 unused01[6];
4084
    ul16 freq2;
4085
    u8 unused02[6];
4086
  } _2tone_encode[15];
4087
  u8 duration_1st_tone; // *10ms
4088
  u8 duration_2nd_tone; // *10ms
4089
  u8 duration_gap;      // *10ms
4090
  u8 unused03[13];
4091
  struct {
4092
    struct {
4093
      u8 dec;      // one out of LIST_2TONE_DEC
4094
      u8 response; // one out of LIST_2TONE_RESPONSE
4095
      u8 alert;    // 1-16
4096
    } decs[4];
4097
    u8 unused04[4];
4098
  } _2tone_decode[15];
4099
  u8 unused05[16];
4100

    
4101
  struct {
4102
    ul16 freqA;
4103
    ul16 freqB;
4104
    ul16 freqC;
4105
    ul16 freqD;
4106
    // unknown what those values mean, but they are
4107
    // derived from configured frequencies
4108
    ul16 derived_from_freqA; // 2304000/freqA
4109
    ul16 derived_from_freqB; // 2304000/freqB
4110
    ul16 derived_from_freqC; // 2304000/freqC
4111
    ul16 derived_from_freqD; // 2304000/freqD
4112
  }freqs[15];
4113
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4114
} _2tone;
4115

    
4116
#seekto 0x3000;
4117
struct {
4118
  u8 freq[8];
4119
  char broadcast_station_name[6];
4120
  u8 unknown[2];
4121
} fm_radio_preset[16];
4122

    
4123
#seekto 0x3200;
4124
struct {
4125
  u8 tmr;
4126
  u8 unknown1;
4127
  u8 sql;
4128
  u8 unknown2;
4129
  u8 autolk;
4130
  u8 tot;
4131
  u8 apo;
4132
  u8 unknown3;
4133
  u8 abr;
4134
  u8 beep;
4135
  u8 unknown4[4];
4136
  u8 dtmfst;
4137
  u8 unknown5[2];
4138
  u8 screv;
4139
  u8 unknown6[2];
4140
  u8 pttid;
4141
  u8 pttlt;
4142
  u8 unknown7;
4143
  u8 emctp;
4144
  u8 emcch;
4145
  u8 sigbp;
4146
  u8 unknown8;
4147
  u8 camdf;
4148
  u8 cbmdf;
4149
  u8 ccmdf;
4150
  u8 cdmdf;
4151
  u8 langua;
4152
  u8 sync;
4153

    
4154

    
4155
  u8 stfc;
4156
  u8 mffc;
4157
  u8 sfafc;
4158
  u8 sfbfc;
4159
  u8 sfcfc;
4160
  u8 sfdfc;
4161
  u8 subfc;
4162
  u8 fmfc;
4163
  u8 sigfc;
4164
  u8 modfc;
4165
  u8 menufc;
4166
  u8 txfc;
4167
  u8 txdisp;
4168
  u8 unknown9[5];
4169
  u8 anil;
4170
  u8 reps;
4171
  u8 repm;
4172
  u8 tmrmr;
4173
  u8 ste;
4174
  u8 rpste;
4175
  u8 rptdl;
4176
  u8 dtmfg;
4177
  u8 mgain;
4178
  u8 skiptx;
4179
  u8 scmode;
4180
} settings;
4181

    
4182
#seekto 0x3280;
4183
struct {
4184
  u8 unknown1;
4185
  u8 vfomr;
4186
  u8 keylock;
4187
  u8 unknown2;
4188
  u8 unknown3:4,
4189
     vfomren:1,
4190
     unknown4:1,
4191
     reseten:1,
4192
     menuen:1;
4193
  u8 unknown5[11];
4194
  u8 dispab;
4195
  u8 unknown6[2];
4196
  u8 smenu;
4197
  u8 unknown7[7];
4198
  u8 vfomra;
4199
  u8 vfomrb;
4200
  u8 vfomrc;
4201
  u8 vfomrd;
4202
  u8 mrcha;
4203
  u8 mrchb;
4204
  u8 mrchc;
4205
  u8 mrchd;
4206
} settings2;
4207

    
4208
struct settings_vfo {
4209
  u8 freq[8];
4210
  u8 offset[6];
4211
  u8 unknown2[2];
4212
  ul16 rxtone;
4213
  ul16 txtone;
4214
  u8 scode;
4215
  u8 spmute;
4216
  u8 optsig;
4217
  u8 scramble;
4218
  u8 wide;
4219
  u8 power;
4220
  u8 shiftd;
4221
  u8 step;
4222
  u8 unknown3[4];
4223
};
4224

    
4225
#seekto 0x3300;
4226
struct {
4227
  struct settings_vfo a;
4228
  struct settings_vfo b;
4229
  struct settings_vfo c;
4230
  struct settings_vfo d;
4231
} vfo;
4232

    
4233
#seekto 0x3D80;
4234
struct {
4235
  u8 vhf_low[3];
4236
  u8 vhf_high[3];
4237
  u8 unknown1[4];
4238
  u8 unknown2[6];
4239
  u8 vhf2_low[3];
4240
  u8 vhf2_high[3];
4241
  u8 unknown3[4];
4242
  u8 unknown4[6];
4243
  u8 uhf_low[3];
4244
  u8 uhf_high[3];
4245
  u8 unknown5[4];
4246
  u8 unknown6[6];
4247
  u8 uhf2_low[3];
4248
  u8 uhf2_high[3];
4249
} ranges;
4250

    
4251
#seekto 0x33B0;
4252
struct {
4253
  char line[16];
4254
} static_msg;
4255

    
4256
#seekto 0x3F70;
4257
struct {
4258
  char fp[6];
4259
} fingerprint;
4260

    
4261
"""
4262

    
4263

    
4264
class BTechGMRS(BTechMobileCommon):
4265
    """BTECH's GMRS Mobile"""
4266
    COLOR_LCD = True
4267
    COLOR_LCD2 = True
4268
    NAME_LENGTH = 7
4269
    UPLOAD_MEM_SIZE = 0X3400
4270

    
4271
    def process_mmap(self):
4272
        """Process the mem map into the mem object"""
4273

    
4274
        # Get it
4275
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
4276

    
4277
        # load specific parameters from the radio image
4278
        self.set_options()
4279

    
4280
    def set_options(self):
4281
        """This is to read the options from the image and set it in the
4282
        environment, for now just the limits of the freqs in the VHF/UHF
4283
        ranges"""
4284

    
4285
        # setting the correct ranges for each radio type
4286
        ranges = self._memobj.ranges
4287

    
4288
        # the normal dual bands
4289
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4290
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4291

    
4292
        # DEBUG
4293
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4294
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4295

    
4296
        # set the class with the real data
4297
        self._vhf_range = vhf
4298
        self._uhf_range = uhf
4299

    
4300

    
4301
# real radios
4302
@directory.register
4303
class GMRS50X1(BTechGMRS):
4304
    """Baofeng Tech GMRS50X1"""
4305
    MODEL = "GMRS-50X1"
4306
    BANDS = 2
4307
    LIST_TMR = LIST_TMR16
4308
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4309
                     chirp_common.PowerLevel("Mid", watts=10),
4310
                     chirp_common.PowerLevel("Low", watts=5)]
4311
    _vhf_range = (136000000, 175000000)
4312
    _uhf_range = (400000000, 521000000)
4313
    _upper = 255
4314
    _magic = MSTRING_GMRS50X1
4315
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
4316

    
4317

    
4318
COLORHT_MEM_FORMAT = """
4319
#seekto 0x0000;
4320
struct {
4321
  lbcd rxfreq[4];
4322
  lbcd txfreq[4];
4323
  ul16 rxtone;
4324
  ul16 txtone;
4325
  u8 unknown0:4,
4326
     scode:4;
4327
  u8 unknown1:2,
4328
     spmute:2,
4329
     unknown2:2,
4330
     optsig:2;
4331
  u8 unknown3:3,
4332
     scramble:1,
4333
     unknown4:3,
4334
     power:1;
4335
  u8 unknown5:1,
4336
     wide:1,
4337
     unknown6:2,
4338
     bcl:1,
4339
     add:1,
4340
     pttid:2;
4341
} memory[200];
4342

    
4343
#seekto 0x0E00;
4344
struct {
4345
  u8 tmr;
4346
  u8 unknownE01;
4347
  u8 sql;
4348
  u8 unknownE03[2];
4349
  u8 tot;
4350
  u8 save;
4351
  u8 unknownE07;
4352
  u8 abr;
4353
  u8 beep;
4354
  u8 unknownE0A[4];
4355
  u8 dsub;
4356
  u8 dtmfst;
4357
  u8 screv;
4358
  u8 unknownE11[3];
4359
  u8 pttid;
4360
  u8 unknownE15;
4361
  u8 pttlt;
4362
  u8 unknownE17;
4363
  u8 emctp;
4364
  u8 emcch;
4365
  u8 sigbp;
4366
  u8 unknownE1B;
4367
  u8 camdf;
4368
  u8 cbmdf;
4369
  u8 ccmdf;
4370
  u8 cdmdf;
4371
  u8 langua;
4372
  u8 voice;
4373
  u8 vox;
4374
  u8 voxt;
4375
  u8 sync;          // BTech radios use this as the display sync setting
4376
                    // other radios use this as the auto keypad lock setting
4377
  u8 stfc;
4378
  u8 mffc;
4379
  u8 sfafc;
4380
  u8 sfbfc;
4381
  u8 sfcfc;
4382
  u8 sfdfc;
4383
  u8 subfc;
4384
  u8 fmfc;
4385
  u8 sigfc;
4386
  u8 menufc;
4387
  u8 txfc;
4388
  u8 rxfc;
4389
  u8 unknownE31[5];
4390
  u8 anil;
4391
  u8 reps;
4392
  u8 tmrmr;
4393
  u8 ste;
4394
  u8 rpste;
4395
  u8 rptdl;
4396
  u8 dtmfg;
4397
  u8 tmrtx;
4398
} settings;
4399

    
4400
#seekto 0x0E80;
4401
struct {
4402
  u8 unknown1;
4403
  u8 vfomr;
4404
  u8 keylock;
4405
  u8 unknown2;
4406
  u8 unknown3:4,
4407
     vfomren:1,
4408
     unknown4:1,
4409
     reseten:1,
4410
     menuen:1;
4411
  u8 unknown5[11];
4412
  u8 dispab;
4413
  u8 unknown6[2];
4414
  u8 menu;
4415
  u8 unknown7[7];
4416
  u8 vfomra;
4417
  u8 vfomrb;
4418
  u8 vfomrc;
4419
  u8 vfomrd;
4420
  u8 mrcha;
4421
  u8 mrchb;
4422
  u8 mrchc;
4423
  u8 mrchd;
4424
} settings2;
4425

    
4426
struct settings_vfo {
4427
  u8 freq[8];
4428
  u8 offset[6];
4429
  u8 unknown2[2];
4430
  ul16 rxtone;
4431
  ul16 txtone;
4432
  u8 scode;
4433
  u8 spmute;
4434
  u8 optsig;
4435
  u8 scramble;
4436
  u8 wide;
4437
  u8 power;
4438
  u8 shiftd;
4439
  u8 step;
4440
  u8 unknown3[4];
4441
};
4442

    
4443
#seekto 0x0F00;
4444
struct {
4445
  struct settings_vfo a;
4446
  struct settings_vfo b;
4447
  struct settings_vfo c;
4448
  struct settings_vfo d;
4449
} vfo;
4450

    
4451
#seekto 0x0FE0;
4452
struct {
4453
  char line[16];
4454
} static_msg;
4455

    
4456
#seekto 0x1000;
4457
struct {
4458
  char name[8];
4459
  u8 unknown1[8];
4460
} names[200];
4461

    
4462
#seekto 0x2400;
4463
struct {
4464
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4465
  u8 group_tone;
4466
  u8 repeat_tone;
4467
  u8 unused[13];
4468
} _5tone_std_settings[15];
4469

    
4470
#seekto 0x2500;
4471
struct {
4472
  u8 frame1[5];
4473
  u8 frame2[5];
4474
  u8 frame3[5];
4475
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4476
} _5tone_codes[15];
4477

    
4478
#seekto 0x25F0;
4479
struct {
4480
  u8 _5tone_delay1; // * 10ms
4481
  u8 _5tone_delay2; // * 10ms
4482
  u8 _5tone_delay3; // * 10ms
4483
  u8 _5tone_first_digit_ext_length;
4484
  u8 unknown1;
4485
  u8 unknown2;
4486
  u8 unknown3;
4487
  u8 unknown4;
4488
  u8 decode_standard;
4489
  u8 unknown5:5,
4490
     _5tone_decode_call_frame3:1,
4491
     _5tone_decode_call_frame2:1,
4492
     _5tone_decode_call_frame1:1;
4493
  u8 unknown6:5,
4494
     _5tone_decode_disp_frame3:1,
4495
     _5tone_decode_disp_frame2:1,
4496
     _5tone_decode_disp_frame1:1;
4497
  u8 decode_reset_time; // * 100 + 100ms
4498
} _5tone_settings;
4499

    
4500
#seekto 0x2900;
4501
struct {
4502
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4503
} dtmf_codes[15];
4504

    
4505
#seekto 0x29F0;
4506
struct {
4507
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4508
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4509
  u8 unknown0[14];
4510
  u8 inspection[16];
4511
  u8 monitor[16];
4512
  u8 alarmcode[16];
4513
  u8 stun[16];
4514
  u8 kill[16];
4515
  u8 revive[16];
4516
  u8 unknown1[16];
4517
  u8 unknown2[16];
4518
  u8 unknown3[16];
4519
  u8 unknown4[16];
4520
  u8 unknown5[16];
4521
  u8 unknown6[16];
4522
  u8 unknown7[16];
4523
  u8 masterid[16];
4524
  u8 viceid[16];
4525
  u8 unused01:7,
4526
     mastervice:1;
4527
  u8 unused02:3,
4528
     mrevive:1,
4529
     mkill:1,
4530
     mstun:1,
4531
     mmonitor:1,
4532
     minspection:1;
4533
  u8 unused03:3,
4534
     vrevive:1,
4535
     vkill:1,
4536
     vstun:1,
4537
     vmonitor:1,
4538
     vinspection:1;
4539
  u8 unused04:6,
4540
     txdisable:1,
4541
     rxdisable:1;
4542
  u8 groupcode;
4543
  u8 spacecode;
4544
  u8 delayproctime; // * 100 + 100ms
4545
  u8 resettime;     // * 100 + 100ms
4546
} dtmf_settings;
4547

    
4548
#seekto 0x2D00;
4549
struct {
4550
  struct {
4551
    ul16 freq1;
4552
    u8 unused01[6];
4553
    ul16 freq2;
4554
    u8 unused02[6];
4555
  } _2tone_encode[15];
4556
  u8 duration_1st_tone; // *10ms
4557
  u8 duration_2nd_tone; // *10ms
4558
  u8 duration_gap;      // *10ms
4559
  u8 unused03[13];
4560
  struct {
4561
    struct {
4562
      u8 dec;      // one out of LIST_2TONE_DEC
4563
      u8 response; // one out of LIST_2TONE_RESPONSE
4564
      u8 alert;    // 1-16
4565
    } decs[4];
4566
    u8 unused04[4];
4567
  } _2tone_decode[15];
4568
  u8 unused05[16];
4569

    
4570
  struct {
4571
    ul16 freqA;
4572
    ul16 freqB;
4573
    ul16 freqC;
4574
    ul16 freqD;
4575
    // unknown what those values mean, but they are
4576
    // derived from configured frequencies
4577
    ul16 derived_from_freqA; // 2304000/freqA
4578
    ul16 derived_from_freqB; // 2304000/freqB
4579
    ul16 derived_from_freqC; // 2304000/freqC
4580
    ul16 derived_from_freqD; // 2304000/freqD
4581
  }freqs[15];
4582
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4583
} _2tone;
4584

    
4585
#seekto 0x3D80;
4586
struct {
4587
  u8 vhf_low[3];
4588
  u8 vhf_high[3];
4589
  u8 unknown1[4];
4590
  u8 unknown2[6];
4591
  u8 vhf2_low[3];
4592
  u8 vhf2_high[3];
4593
  u8 unknown3[4];
4594
  u8 unknown4[6];
4595
  u8 uhf_low[3];
4596
  u8 uhf_high[3];
4597
  u8 unknown5[4];
4598
  u8 unknown6[6];
4599
  u8 uhf2_low[3];
4600
  u8 uhf2_high[3];
4601
} ranges;
4602

    
4603
#seekto 0x3F70;
4604
struct {
4605
  char fp[6];
4606
} fingerprint;
4607

    
4608
"""
4609

    
4610

    
4611
class QYTColorHT(BTechMobileCommon):
4612
    """QTY's Color LCD Handheld and alike radios"""
4613
    COLOR_LCD = True
4614
    COLOR_LCD3 = True
4615
    NAME_LENGTH = 8
4616
    LIST_TMR = LIST_TMR15
4617

    
4618
    def process_mmap(self):
4619
        """Process the mem map into the mem object"""
4620

    
4621
        # Get it
4622
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4623

    
4624
        # load specific parameters from the radio image
4625
        self.set_options()
4626

    
4627
    def set_options(self):
4628
        """This is to read the options from the image and set it in the
4629
        environment, for now just the limits of the freqs in the VHF/UHF
4630
        ranges"""
4631

    
4632
        # setting the correct ranges for each radio type
4633
        ranges = self._memobj.ranges
4634

    
4635
        # the normal dual bands
4636
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4637
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4638

    
4639
        # DEBUG
4640
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4641
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4642

    
4643
        # the additional bands
4644
        if self.MODEL in ["KT-8R"]:
4645
            # 200Mhz band
4646
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
4647
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
4648
            self._220_range = vhf2
4649

    
4650
            # 350Mhz band
4651
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4652
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4653
            self._350_range = uhf2
4654

    
4655
        # set the class with the real data
4656
        self._vhf_range = vhf
4657
        self._uhf_range = uhf
4658

    
4659

    
4660
# real radios
4661
@directory.register
4662
class KT8R(QYTColorHT):
4663
    """QYT KT8R"""
4664
    VENDOR = "QYT"
4665
    MODEL = "KT-8R"
4666
    BANDS = 4
4667
    LIST_TMR = LIST_TMR15
4668
    _vhf_range = (136000000, 175000000)
4669
    _220_range = (200000000, 261000000)
4670
    _uhf_range = (400000000, 481000000)
4671
    _350_range = (350000000, 391000000)
4672
    _magic = MSTRING_KT8R
4673
    _fileid = [KT8R_fp2, KT8R_fp1, KT8R_fp, ]
4674
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
4675
                     chirp_common.PowerLevel("Low", watts=1)]
(2-2/3)