Project

General

Profile

New Model #9827 » btech_wp-9900_kt-wp12_draft_#3.py

Jim Unroe, 05/24/2022 08:25 PM

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

    
18
import time
19
import struct
20
import logging
21

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

    
31
LOG = logging.getLogger(__name__)
32

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

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

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

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

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

    
140
# This is a general serial timeout for all serial read functions.
141
STIMEOUT = 0.25
142

    
143
# this var controls the verbosity in the debug and by default it's low (False)
144
# make it True and you will to get a very verbose debug.log
145
debug = False
146

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

    
151
GMRS_FREQS1 = [462.5625, 462.5875, 462.6125, 462.6375, 462.6625,
152
               462.6875, 462.7125]
153
GMRS_FREQS2 = [467.5625, 467.5875, 467.6125, 467.6375, 467.6625,
154
               467.6875, 467.7125]
155
GMRS_FREQS3 = [462.5500, 462.5750, 462.6000, 462.6250, 462.6500,
156
               462.6750, 462.7000, 462.7250]
157
GMRS_FREQS = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
158

    
159

    
160
# #### ID strings #####################################################
161

    
162
# BTECH UV2501 pre-production units
163
UV2501pp_fp = "M2C294"
164
# BTECH UV2501 pre-production units 2 + and 1st Gen radios
165
UV2501pp2_fp = "M29204"
166
# B-TECH UV-2501 second generation (2G) radios
167
UV2501G2_fp = "BTG214"
168
# B-TECH UV-2501 third generation (3G) radios
169
UV2501G3_fp = "BTG324"
170

    
171
# B-TECH UV-2501+220 pre-production units
172
UV2501_220pp_fp = "M3C281"
173
# B-TECH UV-2501+220
174
UV2501_220_fp = "M3G201"
175
# new variant, let's call it Generation 2
176
UV2501_220G2_fp = "BTG211"
177
# B-TECH UV-2501+220 third generation (3G)
178
UV2501_220G3_fp = "BTG311"
179

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

    
191
# B-TECH UV-25X2
192
UV25X2_fp = "UC2012"
193

    
194
# B-TECH UV-25X4
195
UV25X4_fp = "UC4014"
196

    
197
# B-TECH UV-50X2
198
UV50X2_fp = "UC2M12"
199

    
200
# B-TECH GMRS-50X1
201
GMRS50X1_fp = "NC1802"
202
GMRS50X1_fp1 = "NC1932"
203

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

    
207

    
208
# WACCOM Mini-8900
209
MINI8900_fp = "M28854"
210

    
211

    
212
# QYT KT-UV980
213
KTUV980_fp = "H28854"
214

    
215
# QYT KT8900
216
KT8900_fp = "M29154"
217
# New generations KT8900
218
KT8900_fp1 = "M2C234"
219
KT8900_fp2 = "M2G1F4"
220
KT8900_fp3 = "M2G2F4"
221
KT8900_fp4 = "M2G304"
222
KT8900_fp5 = "M2G314"
223
KT8900_fp6 = "M2G424"
224
KT8900_fp7 = "M27184"
225

    
226
# KT8900R
227
KT8900R_fp = "M3G1F4"
228
# Second Generation
229
KT8900R_fp1 = "M3G214"
230
# another model
231
KT8900R_fp2 = "M3C234"
232
# another model G4?
233
KT8900R_fp3 = "M39164"
234
# another model
235
KT8900R_fp4 = "M3G314"
236
# AC3MB: another id
237
KT8900R_fp5 = "M3B064"
238

    
239
# KT7900D (quad band)
240
KT7900D_fp = "VC4004"
241
KT7900D_fp1 = "VC4284"
242
KT7900D_fp2 = "VC4264"
243
KT7900D_fp3 = "VC4114"
244
KT7900D_fp4 = "VC4104"
245
KT7900D_fp5 = "VC4254"
246
KT7900D_fp6 = "VC5264"
247
KT7900D_fp7 = "VC9204"
248

    
249
# QB25 (quad band) - a clone of KT7900D
250
QB25_fp = "QB-25"
251

    
252
# KT8900D (dual band)
253
KT8900D_fp = "VC2002"
254
KT8900D_fp1 = "VC8632"
255
KT8900D_fp2 = "VC3402"
256
KT8900D_fp3 = "VC7062"
257

    
258
# LUITON LT-588UV
259
LT588UV_fp = "V2G1F4"
260
# Added by rstrickoff gen 2 id
261
LT588UV_fp1 = "V2G214"
262

    
263
# QYT KT-8R (quad band ht)
264
KT8R_fp = "MCB264"
265
KT8R_fp1 = "MCB284"
266
KT8R_fp2 = "MC5264"
267

    
268
# QYT KT5800 (dual band)
269
KT5800_fp = "VCB222"
270

    
271
# QYT KT980Plus (dual band)
272
KT980PLUS_fp = "VC2002"
273
KT980PLUS_fp1 = "VC6042"
274

    
275
# Radioddity DB25-G (gmrs)
276
DB25G_fp = "VC6182"
277
DB25G_fp1 = "VC7062"
278

    
279
# QYT KT-WP12 and KT-9900
280
KTWP12_fp = "WP3094"
281

    
282
# Anysecu WP-9900
283
WP9900_fp = "WP3094"
284

    
285

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

    
309

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

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

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

    
330
        # restore the default serial timeout
331
        radio.pipe.timeout = STIMEOUT
332

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

    
336

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

    
340
    data = ""
341

    
342
    try:
343
        data = radio.pipe.read(amount)
344

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

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

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

    
359
    except:
360
        raise errors.RadioError("Error reading data from radio")
361

    
362
    return data
363

    
364

    
365
def _send(radio, data):
366
    """Send data to the radio device"""
367

    
368
    try:
369
        radio.pipe.write(data)
370

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

    
378

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

    
386
    return frame
387

    
388

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

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

    
395
    # get the whole block
396
    block = _rawrecv(radio, BLOCK_SIZE + 5)
397

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

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

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

    
413
    # return the data
414
    return block[5:]
415

    
416

    
417
def _do_ident(radio, status, upload=False):
418
    """Put the radio in PROGRAM mode & identify it"""
419
    #  set the serial discipline
420
    radio.pipe.baudrate = 9600
421
    radio.pipe.parity = "N"
422

    
423
    # lengthen the timeout here as these radios are reseting due to timeout
424
    radio.pipe.timeout = 0.75
425

    
426
    # send the magic word
427
    _send(radio, radio._magic)
428

    
429
    # Now you get a 50 byte reply if all goes well
430
    ident = _rawrecv(radio, 50)
431

    
432
    # checking for the ack
433
    if ident[0] != ACK_CMD:
434
        raise errors.RadioError("Bad ack from radio")
435

    
436
    # basic check for the ident block
437
    if len(ident) != 50:
438
        raise errors.RadioError("Radio send a short ident block.")
439

    
440
    # check if ident is OK
441
    itis = False
442
    for fp in radio._fileid:
443
        if fp in ident:
444
            # got it!
445
            itis = True
446
            # checking if we are dealing with a Gen 3 BTECH
447
            if radio.VENDOR == "BTECH" and fp in BTECH3:
448
                radio.btech3 = True
449

    
450
            break
451

    
452
    if itis is False:
453
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
454
        raise errors.RadioError("Radio identification failed.")
455

    
456
    # pause here for the radio to catch up
457
    sleep(0.1)
458

    
459
    # the OEM software reads this additional block, so we will, too
460

    
461
    # Get the full 21 bytes at a time to reduce load
462
    # 1 byte ACK + 4 bytes header + 16 bytes of data (BLOCK_SIZE)
463
    frame = _make_frame("S", 0x3DF0, 16)
464
    _send(radio, frame)
465
    id2 = _rawrecv(radio, 21)
466

    
467
    # restore the default serial timeout
468
    radio.pipe.timeout = STIMEOUT
469

    
470
    # checking for the ack
471
    if id2[0] not in "\x06\x05":
472
        raise errors.RadioError("Bad ack from radio")
473

    
474
    # basic check for the additional block
475
    if len(id2) < 21:
476
        raise errors.RadioError("The extra ID is short, aborting.")
477

    
478
    # this radios need a extra request/answer here on the upload
479
    # the amount of data received depends of the radio type
480
    #
481
    # also the first block of TX must no have the ACK at the beginning
482
    # see _upload for this.
483
    if upload is True:
484
        # send an ACK
485
        _send(radio, ACK_CMD)
486

    
487
        # the amount of data depend on the radio, so far we have two radios
488
        # reading two bytes with an ACK at the end and just ONE with just
489
        # one byte (QYT KT8900)
490
        # the JT-6188 appears a clone of the last, but reads TWO bytes.
491
        #
492
        # we will read two bytes with a custom timeout to not penalize the
493
        # users for this.
494
        #
495
        # we just check for a response and last byte being a ACK, that is
496
        # the common stone for all radios (3 so far)
497
        ack = _rawrecv(radio, 2)
498

    
499
        # checking
500
        if len(ack) == 0 or ack[-1:] != ACK_CMD:
501
            raise errors.RadioError("Radio didn't ACK the upload")
502

    
503
    # DEBUG
504
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
505

    
506
    return True
507

    
508

    
509
def _download(radio):
510
    """Get the memory map"""
511

    
512
    # UI progress
513
    status = chirp_common.Status()
514

    
515
    # put radio in program mode and identify it
516
    _do_ident(radio, status)
517

    
518
    # reset the progress bar in the UI
519
    status.max = MEM_SIZE / BLOCK_SIZE
520
    status.msg = "Cloning from radio..."
521
    status.cur = 0
522
    radio.status_fn(status)
523

    
524
    data = ""
525
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
526
        # sending the read request
527
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
528

    
529
        # read
530
        d = _recv(radio, addr)
531

    
532
        # aggregate the data
533
        data += d
534

    
535
        # UI Update
536
        status.cur = addr / BLOCK_SIZE
537
        status.msg = "Cloning from radio..."
538
        radio.status_fn(status)
539

    
540
    return data
541

    
542

    
543
def _upload(radio):
544
    """Upload procedure"""
545

    
546
    # The UPLOAD mem is restricted to lower than 0x3100,
547
    # so we will overide that here localy
548
    MEM_SIZE = radio.UPLOAD_MEM_SIZE
549

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

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

    
556
    # get the data to upload to radio
557
    data = radio.get_mmap()
558

    
559
    # Reset the UI progress
560
    status.max = MEM_SIZE / TX_BLOCK_SIZE
561
    status.cur = 0
562
    status.msg = "Cloning to radio..."
563
    radio.status_fn(status)
564

    
565
    # the fun start here
566
    for addr in range(0, MEM_SIZE, TX_BLOCK_SIZE):
567
        # getting the block of data to send
568
        d = data[addr:addr + TX_BLOCK_SIZE]
569

    
570
        # build the frame to send
571
        frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
572

    
573
        # first block must not send the ACK at the beginning for the
574
        # ones that has the extra id, since this have to do a extra step
575
        if addr == 0:
576
            frame = frame[1:]
577

    
578
        # send the frame
579
        _send(radio, frame)
580

    
581
        # receiving the response
582
        ack = _rawrecv(radio, 1)
583

    
584
        # basic check
585
        if len(ack) != 1:
586
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
587

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

    
591
        # UI Update
592
        status.cur = addr / TX_BLOCK_SIZE
593
        status.msg = "Cloning to radio..."
594
        radio.status_fn(status)
595

    
596

    
597
def model_match(cls, data):
598
    """Match the opened/downloaded image to the correct version"""
599
    rid = data[0x3f70:0x3f76]
600

    
601
    if rid in cls._fileid:
602
        return True
603

    
604
    return False
605

    
606

    
607
def _decode_ranges(low, high):
608
    """Unpack the data in the ranges zones in the memmap and return
609
    a tuple with the integer corresponding to the Mhz it means"""
610
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
611
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
612
    ilow *= 1000000
613
    ihigh *= 1000000
614

    
615
    return (ilow, ihigh)
616

    
617

    
618
def _split(rf, f1, f2):
619
    """Returns False if the two freqs are in the same band (no split)
620
    or True otherwise"""
621

    
622
    # determine if the two freqs are in the same band
623
    for low, high in rf.valid_bands:
624
        if f1 >= low and f1 <= high and \
625
                f2 >= low and f2 <= high:
626
            # if the two freqs are on the same Band this is not a split
627
            return False
628

    
629
    # if you get here is because the freq pairs are split
630
    return True
631

    
632

    
633
class BTechMobileCommon(chirp_common.CloneModeRadio,
634
                        chirp_common.ExperimentalRadio):
635
    """BTECH's UV-5001 and alike radios"""
636
    VENDOR = "BTECH"
637
    MODEL = ""
638
    IDENT = ""
639
    BANDS = 2
640
    COLOR_LCD = False
641
    COLOR_LCD2 = False  # BTech Original GMRS Radios
642
    COLOR_LCD3 = False  # Color HT Radios
643
    COLOR_LCD4 = False  # Waterproof Mobile Radios
644
    NAME_LENGTH = 6
645
    UPLOAD_MEM_SIZE = 0X3100
646
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
647
                     chirp_common.PowerLevel("Low", watts=10)]
648
    _vhf_range = (130000000, 180000000)
649
    _220_range = (200000000, 271000000)
650
    _uhf_range = (400000000, 521000000)
651
    _350_range = (350000000, 391000000)
652
    _upper = 199
653
    _magic = MSTRING
654
    _fileid = None
655
    _id2 = False
656
    btech3 = False
657
    _gmrs = False
658

    
659
    @classmethod
660
    def get_prompts(cls):
661
        rp = chirp_common.RadioPrompts()
662
        rp.experimental = \
663
            ('This driver is experimental.\n'
664
             '\n'
665
             'Please keep a copy of your memories with the original software '
666
             'if you treasure them, this driver is new and may contain'
667
             ' bugs.\n'
668
             '\n'
669
             )
670
        rp.pre_download = _(dedent("""\
671
            Follow these instructions to download your info:
672

    
673
            1 - Turn off your radio
674
            2 - Connect your interface cable
675
            3 - Turn on your radio
676
            4 - Do the download of your radio data
677

    
678
            """))
679
        rp.pre_upload = _(dedent("""\
680
            Follow these instructions to upload your info:
681

    
682
            1 - Turn off your radio
683
            2 - Connect your interface cable
684
            3 - Turn on your radio
685
            4 - Do the upload of your radio data
686

    
687
            """))
688
        return rp
689

    
690
    def get_features(self):
691
        """Get the radio's features"""
692

    
693
        # we will use the following var as global
694
        global POWER_LEVELS
695

    
696
        rf = chirp_common.RadioFeatures()
697
        rf.has_settings = True
698
        rf.has_bank = False
699
        rf.has_tuning_step = False
700
        rf.can_odd_split = True
701
        rf.has_name = True
702
        rf.has_offset = True
703
        rf.has_mode = True
704
        rf.has_dtcs = True
705
        rf.has_rx_dtcs = True
706
        rf.has_dtcs_polarity = True
707
        rf.has_ctone = True
708
        rf.has_cross = True
709
        rf.valid_modes = MODES
710
        rf.valid_characters = VALID_CHARS
711
        rf.valid_name_length = self.NAME_LENGTH
712
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
713
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
714
        rf.valid_cross_modes = [
715
            "Tone->Tone",
716
            "DTCS->",
717
            "->DTCS",
718
            "Tone->DTCS",
719
            "DTCS->Tone",
720
            "->Tone",
721
            "DTCS->DTCS"]
722
        rf.valid_skips = SKIP_VALUES
723
        rf.valid_dtcs_codes = DTCS
724
        rf.valid_tuning_steps = STEPS
725
        rf.memory_bounds = (0, self._upper)
726

    
727
        # power levels
728
        POWER_LEVELS = self._power_levels
729
        rf.valid_power_levels = POWER_LEVELS
730

    
731
        # normal dual bands
732
        rf.valid_bands = [self._vhf_range, self._uhf_range]
733

    
734
        # 220 band
735
        if self.BANDS == 3 or self.BANDS == 4:
736
            rf.valid_bands.append(self._220_range)
737

    
738
        # 350 band
739
        if self.BANDS == 4:
740
            rf.valid_bands.append(self._350_range)
741

    
742
        return rf
743

    
744
    def validate_memory(self, mem):
745
        msgs = chirp_common.CloneModeRadio.validate_memory(self, mem)
746

    
747
        _msg_duplex1 = 'Memory location only supports "Low"'
748
        _msg_duplex2 = 'Memory location only supports "off"'
749
        _msg_duplex3 = 'Memory location only supports "(None)", "+" or "off"'
750

    
751
        return msgs
752

    
753
    def sync_in(self):
754
        """Download from radio"""
755
        data = _download(self)
756
        self._mmap = memmap.MemoryMap(data)
757
        self.process_mmap()
758

    
759
    def sync_out(self):
760
        """Upload to radio"""
761
        try:
762
            _upload(self)
763
        except errors.RadioError:
764
            raise
765
        except Exception, e:
766
            raise errors.RadioError("Error: %s" % e)
767

    
768
    def get_raw_memory(self, number):
769
        return repr(self._memobj.memory[number])
770

    
771
    def _decode_tone(self, val):
772
        """Parse the tone data to decode from mem, it returns:
773
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
774
        pol = None
775

    
776
        if val in [0, 65535]:
777
            return '', None, None
778
        elif val > 0x0258:
779
            a = val / 10.0
780
            return 'Tone', a, pol
781
        else:
782
            if val > 0x69:
783
                index = val - 0x6A
784
                pol = "R"
785
            else:
786
                index = val - 1
787
                pol = "N"
788

    
789
            tone = DTCS[index]
790
            return 'DTCS', tone, pol
791

    
792
    def _encode_tone(self, memval, mode, val, pol):
793
        """Parse the tone data to encode from UI to mem"""
794
        if mode == '' or mode is None:
795
            memval.set_raw("\x00\x00")
796
        elif mode == 'Tone':
797
            memval.set_value(val * 10)
798
        elif mode == 'DTCS':
799
            # detect the index in the DTCS list
800
            try:
801
                index = DTCS.index(val)
802
                if pol == "N":
803
                    index += 1
804
                else:
805
                    index += 0x6A
806
                memval.set_value(index)
807
            except:
808
                msg = "Digital Tone '%d' is not supported" % value
809
                LOG.error(msg)
810
                raise errors.RadioError(msg)
811
        else:
812
            msg = "Internal error: invalid mode '%s'" % mode
813
            LOG.error(msg)
814
            raise errors.InvalidDataError(msg)
815

    
816
    def get_memory(self, number):
817
        """Get the mem representation from the radio image"""
818
        _mem = self._memobj.memory[number]
819
        _names = self._memobj.names[number]
820

    
821
        # Create a high-level memory object to return to the UI
822
        mem = chirp_common.Memory()
823

    
824
        # Memory number
825
        mem.number = number
826

    
827
        if _mem.get_raw()[0] == "\xFF":
828
            mem.empty = True
829
            return mem
830

    
831
        # Freq and offset
832
        mem.freq = int(_mem.rxfreq) * 10
833
        # tx freq can be blank
834
        if _mem.get_raw()[4] == "\xFF":
835
            # TX freq not set
836
            mem.offset = 0
837
            mem.duplex = "off"
838
        else:
839
            # TX freq set
840
            offset = (int(_mem.txfreq) * 10) - mem.freq
841
            if offset != 0:
842
                if _split(self.get_features(), mem.freq, int(
843
                          _mem.txfreq) * 10):
844
                    mem.duplex = "split"
845
                    mem.offset = int(_mem.txfreq) * 10
846
                elif offset < 0:
847
                    mem.offset = abs(offset)
848
                    mem.duplex = "-"
849
                elif offset > 0:
850
                    mem.offset = offset
851
                    mem.duplex = "+"
852
            else:
853
                mem.offset = 0
854

    
855
        # name TAG of the channel
856
        mem.name = str(_names.name).rstrip("\xFF").replace("\xFF", " ")
857

    
858
        # power
859
        mem.power = POWER_LEVELS[int(_mem.power)]
860

    
861
        # wide/narrow
862
        mem.mode = MODES[int(_mem.wide)]
863

    
864
        # skip
865
        mem.skip = SKIP_VALUES[_mem.add]
866

    
867
        # tone data
868
        rxtone = txtone = None
869
        txtone = self._decode_tone(_mem.txtone)
870
        rxtone = self._decode_tone(_mem.rxtone)
871
        chirp_common.split_tone_decode(mem, txtone, rxtone)
872

    
873
        # Extra
874
        mem.extra = RadioSettingGroup("extra", "Extra")
875

    
876
        if not self.COLOR_LCD or \
877
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
878
            scramble = RadioSetting("scramble", "Scramble",
879
                                    RadioSettingValueBoolean(bool(
880
                                        _mem.scramble)))
881
            mem.extra.append(scramble)
882

    
883
        bcl = RadioSetting("bcl", "Busy channel lockout",
884
                           RadioSettingValueBoolean(bool(_mem.bcl)))
885
        mem.extra.append(bcl)
886

    
887
        pttid = RadioSetting("pttid", "PTT ID",
888
                             RadioSettingValueList(PTTID_LIST,
889
                                                   PTTID_LIST[_mem.pttid]))
890
        mem.extra.append(pttid)
891

    
892
        # validating scode
893
        scode = _mem.scode if _mem.scode != 15 else 0
894
        pttidcode = RadioSetting("scode", "PTT ID signal code",
895
                                 RadioSettingValueList(
896
                                     PTTIDCODE_LIST,
897
                                     PTTIDCODE_LIST[scode]))
898
        mem.extra.append(pttidcode)
899

    
900
        optsig = RadioSetting("optsig", "Optional signaling",
901
                              RadioSettingValueList(
902
                                  OPTSIG_LIST,
903
                                  OPTSIG_LIST[_mem.optsig]))
904
        mem.extra.append(optsig)
905

    
906
        spmute = RadioSetting("spmute", "Speaker mute",
907
                              RadioSettingValueList(
908
                                  SPMUTE_LIST,
909
                                  SPMUTE_LIST[_mem.spmute]))
910
        mem.extra.append(spmute)
911

    
912
        return mem
913

    
914
    def set_memory(self, mem):
915
        """Set the memory data in the eeprom img from the UI"""
916
        # get the eprom representation of this channel
917
        _mem = self._memobj.memory[mem.number]
918
        _names = self._memobj.names[mem.number]
919

    
920
        mem_was_empty = False
921
        # same method as used in get_memory for determining if mem is empty
922
        # doing this BEFORE overwriting it with new values ...
923
        if _mem.get_raw()[0] == "\xFF":
924
            LOG.debug("This mem was empty before")
925
            mem_was_empty = True
926

    
927
        # if empty memmory
928
        if mem.empty:
929
            # the channel itself
930
            _mem.set_raw("\xFF" * 16)
931
            # the name tag
932
            _names.set_raw("\xFF" * 16)
933
            return
934

    
935
        if mem_was_empty:
936
            # Zero the whole memory if we're making it unempty for
937
            # the first time
938
            LOG.debug('Zeroing new memory')
939
            _mem.set_raw('\x00' * 16)
940

    
941
        if self._gmrs:
942
            if mem.number >= 1 and mem.number <= 30:
943
                GMRS_FREQ = int(GMRS_FREQS[mem.number - 1] * 1000000)
944
                mem.freq = GMRS_FREQ
945
                if mem.number <= 22:
946
                    mem.duplex = ''
947
                    mem.offset = 0
948
                    mem.mode = "FM"
949
                    mem.power = POWER_LEVELS[0]
950
                    if mem.number >= 8 and mem.number <= 14:
951
                        mem.duplex = 'off'
952
                        mem.offset = 0
953
                        mem.mode = "NFM"
954
                        mem.power = POWER_LEVELS[2]
955
                if mem.number > 22:
956
                    mem.duplex = '+'
957
                    mem.offset = 5000000
958
                    mem.mode = "FM"
959
                    mem.power = POWER_LEVELS[0]
960
            elif float(mem.freq) / 1000000 in GMRS_FREQS:
961
                if float(mem.freq) / 1000000 in GMRS_FREQS1:
962
                    mem.duplex = ''
963
                    mem.offset = 0
964
                    mem.mode = "FM"
965
                    mem.power = POWER_LEVELS[0]
966
                if float(mem.freq) / 1000000 in GMRS_FREQS2:
967
                    mem.duplex = 'off'
968
                    mem.offset = 0
969
                    mem.mode = "NFM"
970
                    mem.power = POWER_LEVELS[2]
971
                if float(mem.freq) / 1000000 in GMRS_FREQS3:
972
                    if mem.duplex == '+':
973
                        mem.offset = 5000000
974
                    else:
975
                        mem.offset = 0
976
                    mem.mode = "FM"
977
                    mem.power = POWER_LEVELS[0]
978
            else:
979
                mem.duplex = 'off'
980
                mem.offset = 0
981

    
982
        # frequency
983
        _mem.rxfreq = mem.freq / 10
984

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

    
998
        # tone data
999
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
1000
            chirp_common.split_tone_encode(mem)
1001
        self._encode_tone(_mem.txtone, txmode, txtone, txpol)
1002
        self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)
1003

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

    
1010
        # power, # default power level is high
1011
        _mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)
1012

    
1013
        # wide/narrow
1014
        _mem.wide = MODES.index(mem.mode)
1015

    
1016
        # scan add property
1017
        _mem.add = SKIP_VALUES.index(mem.skip)
1018

    
1019
        # reseting unknowns, this have to be set by hand
1020
        _mem.unknown0 = 0
1021
        _mem.unknown1 = 0
1022
        _mem.unknown2 = 0
1023
        _mem.unknown3 = 0
1024
        _mem.unknown4 = 0
1025
        _mem.unknown5 = 0
1026
        _mem.unknown6 = 0
1027

    
1028
        def _zero_settings():
1029
            _mem.spmute = 0
1030
            _mem.optsig = 0
1031
            _mem.scramble = 0
1032
            _mem.bcl = 0
1033
            _mem.pttid = 0
1034
            _mem.scode = 0
1035

    
1036
        if self.COLOR_LCD and _mem.scramble:
1037
            LOG.info('Resetting scramble bit for BTECH COLOR_LCD variant')
1038
            _mem.scramble = 0
1039

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

    
1060
        return mem
1061

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

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

    
1083
        sql = RadioSetting("settings.sql", "Squelch level",
1084
                           RadioSettingValueInteger(0, 9, _mem.settings.sql))
1085
        basic.append(sql)
1086

    
1087
        if self.MODEL == "GMRS-50X1":
1088
            autolk = RadioSetting("settings.autolk", "Auto keylock",
1089
                                  RadioSettingValueBoolean(
1090
                                      _mem.settings.autolk))
1091
            basic.append(autolk)
1092

    
1093
        if self.MODEL == "DB25-G":
1094
            rs = RadioSettingValueInteger(0, 127, _mem.settings.mgain2)
1095
            mgain2 = RadioSetting("settings.mgain2", "Mic gain", rs)
1096
            basic.append(mgain2)
1097

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

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

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

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

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

    
1135
        if self.MODEL == "KT-WP12" or self.MODEL == "WP-9900":
1136
            rs = RadioSettingValueInteger(1, 51, _mem.settings.volume + 1)
1137
            volume = RadioSetting("settings.volume", "Volume", rs)
1138
            basic.append(volume)
1139

    
1140
        if self.MODEL == "KT-8R":
1141
            dsub = RadioSetting("settings.dsub", "CTCSS/DCS code display",
1142
                                RadioSettingValueBoolean(
1143
                                    _mem.settings.dsub))
1144
            basic.append(dsub)
1145

    
1146
        if self.MODEL == "KT-8R" or self.COLOR_LCD4:
1147
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1148
                                  RadioSettingValueBoolean(
1149
                                      _mem.settings.dtmfst))
1150
            basic.append(dtmfst)
1151
        else:
1152
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1153
                                  RadioSettingValueList(
1154
                                      LIST_DTMFST,
1155
                                      LIST_DTMFST[_mem.settings.dtmfst]))
1156
            basic.append(dtmfst)
1157

    
1158
        if not self.COLOR_LCD:
1159
            prisc = RadioSetting("settings.prisc", "Priority scan",
1160
                                 RadioSettingValueBoolean(
1161
                                     _mem.settings.prisc))
1162
            basic.append(prisc)
1163

    
1164
            prich = RadioSetting("settings.prich", "Priority channel",
1165
                                 RadioSettingValueInteger(0, self._upper,
1166
                                                          _mem.settings.prich))
1167
            basic.append(prich)
1168

    
1169
        screv = RadioSetting("settings.screv", "Scan resume method",
1170
                             RadioSettingValueList(
1171
                                 LIST_SCREV,
1172
                                 LIST_SCREV[_mem.settings.screv]))
1173
        basic.append(screv)
1174

    
1175
        pttlt = RadioSetting("settings.pttlt", "PTT transmit delay",
1176
                             RadioSettingValueInteger(0, 30,
1177
                                                      _mem.settings.pttlt))
1178
        basic.append(pttlt)
1179

    
1180
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1181
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1182
                                 RadioSettingValueList(
1183
                                     LIST_EMCTPX,
1184
                                     LIST_EMCTPX[_mem.settings.emctp]))
1185
            basic.append(emctp)
1186
        else:
1187
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1188
                                 RadioSettingValueList(
1189
                                     LIST_EMCTP,
1190
                                     LIST_EMCTP[_mem.settings.emctp]))
1191
            basic.append(emctp)
1192

    
1193
        emcch = RadioSetting("settings.emcch", "Alarm channel",
1194
                             RadioSettingValueInteger(0, self._upper,
1195
                                                      _mem.settings.emcch))
1196
        basic.append(emcch)
1197

    
1198
        if self.COLOR_LCD:
1199
            if _mem.settings.sigbp > 0x01:
1200
                val = 0x00
1201
            else:
1202
                val = _mem.settings.sigbp
1203
            sigbp = RadioSetting("settings.sigbp", "Signal beep",
1204
                                 RadioSettingValueBoolean(val))
1205
            basic.append(sigbp)
1206
        else:
1207
            ringt = RadioSetting("settings.ringt", "Ring time",
1208
                                 RadioSettingValueList(
1209
                                     LIST_OFF1TO9,
1210
                                     LIST_OFF1TO9[_mem.settings.ringt]))
1211
            basic.append(ringt)
1212

    
1213
        camdf = RadioSetting("settings.camdf", "Display mode A",
1214
                             RadioSettingValueList(
1215
                                 LIST_MDF,
1216
                                 LIST_MDF[_mem.settings.camdf]))
1217
        basic.append(camdf)
1218

    
1219
        cbmdf = RadioSetting("settings.cbmdf", "Display mode B",
1220
                             RadioSettingValueList(
1221
                                 LIST_MDF,
1222
                                 LIST_MDF[_mem.settings.cbmdf]))
1223
        basic.append(cbmdf)
1224

    
1225
        if self.COLOR_LCD:
1226
            ccmdf = RadioSetting("settings.ccmdf", "Display mode C",
1227
                                 RadioSettingValueList(
1228
                                     LIST_MDF,
1229
                                     LIST_MDF[_mem.settings.ccmdf]))
1230
            basic.append(ccmdf)
1231

    
1232
            if not self.COLOR_LCD4:
1233
                cdmdf = RadioSetting("settings.cdmdf", "Display mode D",
1234
                                     RadioSettingValueList(
1235
                                         LIST_MDF,
1236
                                         LIST_MDF[_mem.settings.cdmdf]))
1237
                basic.append(cdmdf)
1238

    
1239
                langua = RadioSetting("settings.langua", "Language",
1240
                                      RadioSettingValueList(
1241
                                          LIST_LANGUA,
1242
                                          LIST_LANGUA[_mem.settings.langua]))
1243
                basic.append(langua)
1244

    
1245
        if self.MODEL == "KT-8R":
1246
            voice = RadioSetting("settings.voice", "Voice prompt",
1247
                                 RadioSettingValueList(
1248
                                     LIST_VOICE,
1249
                                     LIST_VOICE[_mem.settings.voice]))
1250
            basic.append(voice)
1251

    
1252
        if self.MODEL == "KT-8R" or self.COLOR_LCD4:
1253
            vox = RadioSetting("settings.vox", "VOX",
1254
                               RadioSettingValueList(
1255
                                   LIST_VOX,
1256
                                   LIST_VOX[_mem.settings.vox]))
1257
            basic.append(vox)
1258

    
1259
            voxt = RadioSetting("settings.voxt", "VOX delay time",
1260
                                RadioSettingValueList(
1261
                                    LIST_VOXT,
1262
                                    LIST_VOXT[_mem.settings.voxt]))
1263
            basic.append(voxt)
1264

    
1265
        if self.VENDOR == "BTECH":
1266
            if self.COLOR_LCD:
1267
                val = RadioSettingValueList(LIST_SYNC,
1268
                                            LIST_SYNC[_mem.settings.sync])
1269
                sync = RadioSetting("settings.sync",
1270
                                    "Channel display sync", val)
1271
                basic.append(sync)
1272
            else:
1273
                sync = RadioSetting("settings.sync", "A/B channel sync",
1274
                                    RadioSettingValueBoolean(
1275
                                        _mem.settings.sync))
1276
                basic.append(sync)
1277

    
1278
        if self.COLOR_LCD4:
1279
            autolk = RadioSetting("settings.autolock", "Auto keylock",
1280
                                  RadioSettingValueBoolean(
1281
                                      _mem.settings.autolock))
1282
            basic.append(autolk)
1283

    
1284
        if not self.COLOR_LCD:
1285
            ponmsg = RadioSetting("settings.ponmsg", "Power-on message",
1286
                                  RadioSettingValueList(
1287
                                      LIST_PONMSG,
1288
                                      LIST_PONMSG[_mem.settings.ponmsg]))
1289
            basic.append(ponmsg)
1290

    
1291
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3 or
1292
                                   self.COLOR_LCD4):
1293
            mainfc = RadioSetting("settings.mainfc",
1294
                                  "Main LCD foreground color",
1295
                                  RadioSettingValueList(
1296
                                      LIST_COLOR9,
1297
                                      LIST_COLOR9[_mem.settings.mainfc]))
1298
            basic.append(mainfc)
1299

    
1300
            if not self.COLOR_LCD4:
1301
                mainbc = RadioSetting("settings.mainbc",
1302
                                      "Main LCD background color",
1303
                                      RadioSettingValueList(
1304
                                          LIST_COLOR9,
1305
                                          LIST_COLOR9[_mem.settings.mainbc]))
1306
                basic.append(mainbc)
1307

    
1308
            menufc = RadioSetting("settings.menufc", "Menu foreground color",
1309
                                  RadioSettingValueList(
1310
                                      LIST_COLOR9,
1311
                                      LIST_COLOR9[_mem.settings.menufc]))
1312
            basic.append(menufc)
1313

    
1314
            if not self.COLOR_LCD4:
1315
                val = RadioSettingValueList(LIST_COLOR9,
1316
                                            LIST_COLOR9[_mem.settings.menubc])
1317
                menubc = RadioSetting("settings.menubc",
1318
                                      "Menu background color", val)
1319
                basic.append(menubc)
1320

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

    
1328
            if not self.COLOR_LCD4:
1329
                stabc = RadioSetting("settings.stabc",
1330
                                     "Top status background color",
1331
                                     RadioSettingValueList(
1332
                                         LIST_COLOR9,
1333
                                         LIST_COLOR9[_mem.settings.stabc]))
1334
                basic.append(stabc)
1335

    
1336
            sigfc = RadioSetting("settings.sigfc",
1337
                                 "Bottom status foreground color",
1338
                                 RadioSettingValueList(
1339
                                     LIST_COLOR9,
1340
                                     LIST_COLOR9[_mem.settings.sigfc]))
1341
            basic.append(sigfc)
1342

    
1343
            if not self.COLOR_LCD4:
1344
                sigbc = RadioSetting("settings.sigbc",
1345
                                     "Bottom status background color",
1346
                                     RadioSettingValueList(
1347
                                         LIST_COLOR9,
1348
                                         LIST_COLOR9[_mem.settings.sigbc]))
1349
                basic.append(sigbc)
1350

    
1351
            rxfc = RadioSetting("settings.rxfc", "Receiving character color",
1352
                                RadioSettingValueList(
1353
                                    LIST_COLOR9,
1354
                                    LIST_COLOR9[_mem.settings.rxfc]))
1355
            basic.append(rxfc)
1356

    
1357
            txfc = RadioSetting("settings.txfc",
1358
                                "Transmitting character color",
1359
                                RadioSettingValueList(
1360
                                    LIST_COLOR9,
1361
                                    LIST_COLOR9[_mem.settings.txfc]))
1362
            basic.append(txfc)
1363

    
1364
            if not self.COLOR_LCD4:
1365
                txdisp = RadioSetting("settings.txdisp",
1366
                                      "Transmitting status display",
1367
                                      RadioSettingValueList(
1368
                                          LIST_TXDISP,
1369
                                          LIST_TXDISP[_mem.settings.txdisp]))
1370
                basic.append(txdisp)
1371

    
1372
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1373
            stfc = RadioSetting("settings.stfc",
1374
                                "ST-FC",
1375
                                RadioSettingValueList(
1376
                                    LIST_COLOR8,
1377
                                    LIST_COLOR8[_mem.settings.stfc]))
1378
            basic.append(stfc)
1379

    
1380
            mffc = RadioSetting("settings.mffc",
1381
                                "MF-FC",
1382
                                RadioSettingValueList(
1383
                                    LIST_COLOR8,
1384
                                    LIST_COLOR8[_mem.settings.mffc]))
1385
            basic.append(mffc)
1386

    
1387
            sfafc = RadioSetting("settings.sfafc",
1388
                                 "SFA-FC",
1389
                                 RadioSettingValueList(
1390
                                     LIST_COLOR8,
1391
                                     LIST_COLOR8[_mem.settings.sfafc]))
1392
            basic.append(sfafc)
1393

    
1394
            sfbfc = RadioSetting("settings.sfbfc",
1395
                                 "SFB-FC",
1396
                                 RadioSettingValueList(
1397
                                     LIST_COLOR8,
1398
                                     LIST_COLOR8[_mem.settings.sfbfc]))
1399
            basic.append(sfbfc)
1400

    
1401
            sfcfc = RadioSetting("settings.sfcfc",
1402
                                 "SFC-FC",
1403
                                 RadioSettingValueList(
1404
                                     LIST_COLOR8,
1405
                                     LIST_COLOR8[_mem.settings.sfcfc]))
1406
            basic.append(sfcfc)
1407

    
1408
            sfdfc = RadioSetting("settings.sfdfc",
1409
                                 "SFD-FC",
1410
                                 RadioSettingValueList(
1411
                                     LIST_COLOR8,
1412
                                     LIST_COLOR8[_mem.settings.sfdfc]))
1413
            basic.append(sfdfc)
1414

    
1415
            subfc = RadioSetting("settings.subfc",
1416
                                 "SUB-FC",
1417
                                 RadioSettingValueList(
1418
                                     LIST_COLOR8,
1419
                                     LIST_COLOR8[_mem.settings.subfc]))
1420
            basic.append(subfc)
1421

    
1422
            fmfc = RadioSetting("settings.fmfc",
1423
                                "FM-FC",
1424
                                RadioSettingValueList(
1425
                                    LIST_COLOR8,
1426
                                    LIST_COLOR8[_mem.settings.fmfc]))
1427
            basic.append(fmfc)
1428

    
1429
            sigfc = RadioSetting("settings.sigfc",
1430
                                 "SIG-FC",
1431
                                 RadioSettingValueList(
1432
                                     LIST_COLOR8,
1433
                                     LIST_COLOR8[_mem.settings.sigfc]))
1434
            basic.append(sigfc)
1435

    
1436
            if not self.MODEL == "KT-8R":
1437
                modfc = RadioSetting("settings.modfc",
1438
                                     "MOD-FC",
1439
                                     RadioSettingValueList(
1440
                                         LIST_COLOR8,
1441
                                         LIST_COLOR8[_mem.settings.modfc]))
1442
                basic.append(modfc)
1443

    
1444
            menufc = RadioSetting("settings.menufc",
1445
                                  "MENUFC",
1446
                                  RadioSettingValueList(
1447
                                      LIST_COLOR8,
1448
                                      LIST_COLOR8[_mem.settings.menufc]))
1449
            basic.append(menufc)
1450

    
1451
            txfc = RadioSetting("settings.txfc",
1452
                                "TX-FC",
1453
                                RadioSettingValueList(
1454
                                    LIST_COLOR8,
1455
                                    LIST_COLOR8[_mem.settings.txfc]))
1456
            basic.append(txfc)
1457

    
1458
            if self.MODEL == "KT-8R":
1459
                rxfc = RadioSetting("settings.rxfc",
1460
                                    "RX-FC",
1461
                                    RadioSettingValueList(
1462
                                        LIST_COLOR8,
1463
                                        LIST_COLOR8[_mem.settings.rxfc]))
1464
                basic.append(rxfc)
1465

    
1466
            if not self.MODEL == "KT-8R":
1467
                txdisp = RadioSetting("settings.txdisp",
1468
                                      "Transmitting status display",
1469
                                      RadioSettingValueList(
1470
                                          LIST_TXDISP,
1471
                                          LIST_TXDISP[_mem.settings.txdisp]))
1472
                basic.append(txdisp)
1473
        elif self.COLOR_LCD4:
1474
            asfc = RadioSetting("settings.asfc",
1475
                                "Above Stat fore color",
1476
                                RadioSettingValueList(
1477
                                    LIST_COLOR8,
1478
                                    LIST_COLOR8[_mem.settings.asfc]))
1479
            basic.append(asfc)
1480

    
1481
            mainfc = RadioSetting("settings.mainfc",
1482
                                  "Main fore color",
1483
                                  RadioSettingValueList(
1484
                                     LIST_COLOR8,
1485
                                     LIST_COLOR8[_mem.settings.mainfc]))
1486
            basic.append(mainfc)
1487

    
1488
            a_fc = RadioSetting("settings.a_fc",
1489
                                "A - fore color",
1490
                                RadioSettingValueList(
1491
                                    LIST_COLOR8,
1492
                                    LIST_COLOR8[_mem.settings.a_fc]))
1493
            basic.append(a_fc)
1494

    
1495
            b_fc = RadioSetting("settings.b_fc",
1496
                                "B - fore color",
1497
                                RadioSettingValueList(
1498
                                    LIST_COLOR8,
1499
                                    LIST_COLOR8[_mem.settings.b_fc]))
1500
            basic.append(b_fc)
1501

    
1502
            c_fc = RadioSetting("settings.c_fc",
1503
                                "C - fore color",
1504
                                RadioSettingValueList(
1505
                                    LIST_COLOR8,
1506
                                    LIST_COLOR8[_mem.settings.c_fc]))
1507
            basic.append(c_fc)
1508

    
1509
            subfc = RadioSetting("settings.subfc",
1510
                                 "Sub fore color",
1511
                                 RadioSettingValueList(
1512
                                     LIST_COLOR8,
1513
                                     LIST_COLOR8[_mem.settings.subfc]))
1514
            basic.append(subfc)
1515

    
1516
            battfc = RadioSetting("settings.battfc",
1517
                                  "Battery fore color",
1518
                                  RadioSettingValueList(
1519
                                      LIST_COLOR8,
1520
                                      LIST_COLOR8[_mem.settings.battfc]))
1521
            basic.append(battfc)
1522

    
1523
            sigfc = RadioSetting("settings.sigfc",
1524
                                 "Signal fore color",
1525
                                 RadioSettingValueList(
1526
                                     LIST_COLOR8,
1527
                                     LIST_COLOR8[_mem.settings.sigfc]))
1528
            basic.append(sigfc)
1529

    
1530
            menufc = RadioSetting("settings.menufc",
1531
                                  "Menu fore color",
1532
                                  RadioSettingValueList(
1533
                                      LIST_COLOR8,
1534
                                      LIST_COLOR8[_mem.settings.menufc]))
1535
            basic.append(menufc)
1536

    
1537
            txfc = RadioSetting("settings.txfc",
1538
                                "TX fore color",
1539
                                RadioSettingValueList(
1540
                                    LIST_COLOR8,
1541
                                    LIST_COLOR8[_mem.settings.txfc]))
1542
            basic.append(txfc)
1543

    
1544
            rxfc = RadioSetting("settings.rxfc",
1545
                                "RX fore color",
1546
                                RadioSettingValueList(
1547
                                    LIST_COLOR8,
1548
                                    LIST_COLOR8[_mem.settings.rxfc]))
1549
            basic.append(rxfc)
1550
        else:
1551
            wtled = RadioSetting("settings.wtled", "Standby backlight Color",
1552
                                 RadioSettingValueList(
1553
                                     LIST_COLOR4,
1554
                                     LIST_COLOR4[_mem.settings.wtled]))
1555
            basic.append(wtled)
1556

    
1557
            rxled = RadioSetting("settings.rxled", "RX backlight Color",
1558
                                 RadioSettingValueList(
1559
                                     LIST_COLOR4,
1560
                                     LIST_COLOR4[_mem.settings.rxled]))
1561
            basic.append(rxled)
1562

    
1563
            txled = RadioSetting("settings.txled", "TX backlight Color",
1564
                                 RadioSettingValueList(
1565
                                     LIST_COLOR4,
1566
                                     LIST_COLOR4[_mem.settings.txled]))
1567
            basic.append(txled)
1568

    
1569
        anil = RadioSetting("settings.anil", "ANI length",
1570
                            RadioSettingValueList(
1571
                                LIST_ANIL,
1572
                                LIST_ANIL[_mem.settings.anil]))
1573
        basic.append(anil)
1574

    
1575
        reps = RadioSetting("settings.reps", "Relay signal (tone burst)",
1576
                            RadioSettingValueList(
1577
                                LIST_REPS,
1578
                                LIST_REPS[_mem.settings.reps]))
1579
        basic.append(reps)
1580

    
1581
        if self.COLOR_LCD4:
1582
            dsub = RadioSetting("settings.dsub", "Subtone display",
1583
                                RadioSettingValueBoolean(
1584
                                    _mem.settings.dsub))
1585
            basic.append(dsub)
1586

    
1587
        model_list = ["GMRS-50X1", "KT-8R", "KT-WP12", "WP-9900"]
1588
        if self.MODEL not in model_list:
1589
            repm = RadioSetting("settings.repm", "Relay condition",
1590
                                RadioSettingValueList(
1591
                                    LIST_REPM,
1592
                                    LIST_REPM[_mem.settings.repm]))
1593
            basic.append(repm)
1594

    
1595
        if self.VENDOR == "BTECH" or self.COLOR_LCD:
1596
            if self.COLOR_LCD:
1597
                tmrmr = RadioSetting("settings.tmrmr", "TMR return time",
1598
                                     RadioSettingValueList(
1599
                                         LIST_OFF1TO50,
1600
                                         LIST_OFF1TO50[_mem.settings.tmrmr]))
1601
                basic.append(tmrmr)
1602
            else:
1603
                tdrab = RadioSetting("settings.tdrab", "TDR return time",
1604
                                     RadioSettingValueList(
1605
                                         LIST_OFF1TO50,
1606
                                         LIST_OFF1TO50[_mem.settings.tdrab]))
1607
                basic.append(tdrab)
1608

    
1609
            ste = RadioSetting("settings.ste", "Squelch tail eliminate",
1610
                               RadioSettingValueBoolean(_mem.settings.ste))
1611
            basic.append(ste)
1612

    
1613
            if self.COLOR_LCD4:
1614
                rpste = RadioSetting("settings.rpste", "Repeater STE",
1615
                                     RadioSettingValueList(
1616
                                         LIST_OFF1TO10,
1617
                                         LIST_OFF1TO10[_mem.settings.rpste]))
1618
                basic.append(rpste)
1619
            else:
1620
                rpste = RadioSetting("settings.rpste", "Repeater STE",
1621
                                     RadioSettingValueList(
1622
                                         LIST_OFF1TO9,
1623
                                         LIST_OFF1TO9[_mem.settings.rpste]))
1624
                basic.append(rpste)
1625

    
1626
            if self.COLOR_LCD4:
1627
                rptdl = RadioSetting("settings.rptdl", "Repeater STE delay",
1628
                                     RadioSettingValueList(
1629
                                         LIST_OFF1TO60,
1630
                                         LIST_OFF1TO60[_mem.settings.rptdl]))
1631
                basic.append(rptdl)
1632
            else:
1633
                rptdl = RadioSetting("settings.rptdl", "Repeater STE delay",
1634
                                     RadioSettingValueList(
1635
                                         LIST_RPTDL,
1636
                                         LIST_RPTDL[_mem.settings.rptdl]))
1637
                basic.append(rptdl)
1638

    
1639
        if self.MODEL == "DB25-G":
1640
            mgain = RadioSetting("settings.mgain", "Auto power-on",
1641
                                 RadioSettingValueBoolean(_mem.settings.mgain))
1642
            basic.append(mgain)
1643

    
1644
        if str(_mem.fingerprint.fp) in BTECH3:
1645
            mgain = RadioSetting("settings.mgain", "Mic gain",
1646
                                 RadioSettingValueInteger(0, 120,
1647
                                                          _mem.settings.mgain))
1648
            basic.append(mgain)
1649

    
1650
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1651
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain",
1652
                                 RadioSettingValueInteger(0, 60,
1653
                                                          _mem.settings.dtmfg))
1654
            basic.append(dtmfg)
1655

    
1656
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1657
            mgain = RadioSetting("settings.mgain", "Mic gain",
1658
                                 RadioSettingValueInteger(0, 127,
1659
                                                          _mem.settings.mgain))
1660
            basic.append(mgain)
1661

    
1662
            skiptx = RadioSetting("settings.skiptx", "Skip TX",
1663
                                  RadioSettingValueList(
1664
                                      LIST_SKIPTX,
1665
                                      LIST_SKIPTX[_mem.settings.skiptx]))
1666
            basic.append(skiptx)
1667

    
1668
            scmode = RadioSetting("settings.scmode", "Scan mode",
1669
                                  RadioSettingValueList(
1670
                                      LIST_SCMODE,
1671
                                      LIST_SCMODE[_mem.settings.scmode]))
1672
            basic.append(scmode)
1673

    
1674
        if self.MODEL == "KT-8R":
1675
            tmrtx = RadioSetting("settings.tmrtx", "TX in multi-standby",
1676
                                 RadioSettingValueList(
1677
                                     LIST_TMRTX,
1678
                                     LIST_TMRTX[_mem.settings.tmrtx]))
1679
            basic.append(tmrtx)
1680

    
1681
        # Advanced
1682
        def _filter(name):
1683
            filtered = ""
1684
            for char in str(name):
1685
                if char in VALID_CHARS:
1686
                    filtered += char
1687
                else:
1688
                    filtered += " "
1689
            return filtered
1690

    
1691
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3 or
1692
                                   self.COLOR_LCD4):
1693
            _msg = self._memobj.poweron_msg
1694
            line1 = RadioSetting("poweron_msg.line1",
1695
                                 "Power-on message line 1",
1696
                                 RadioSettingValueString(0, 8, _filter(
1697
                                                         _msg.line1)))
1698
            advanced.append(line1)
1699
            line2 = RadioSetting("poweron_msg.line2",
1700
                                 "Power-on message line 2",
1701
                                 RadioSettingValueString(0, 8, _filter(
1702
                                                         _msg.line2)))
1703
            advanced.append(line2)
1704
            line3 = RadioSetting("poweron_msg.line3",
1705
                                 "Power-on message line 3",
1706
                                 RadioSettingValueString(0, 8, _filter(
1707
                                                         _msg.line3)))
1708
            advanced.append(line3)
1709
            line4 = RadioSetting("poweron_msg.line4",
1710
                                 "Power-on message line 4",
1711
                                 RadioSettingValueString(0, 8, _filter(
1712
                                                         _msg.line4)))
1713
            advanced.append(line4)
1714
            line5 = RadioSetting("poweron_msg.line5",
1715
                                 "Power-on message line 5",
1716
                                 RadioSettingValueString(0, 8, _filter(
1717
                                                         _msg.line5)))
1718
            advanced.append(line5)
1719
            line6 = RadioSetting("poweron_msg.line6",
1720
                                 "Power-on message line 6",
1721
                                 RadioSettingValueString(0, 8, _filter(
1722
                                                         _msg.line6)))
1723
            advanced.append(line6)
1724
            line7 = RadioSetting("poweron_msg.line7",
1725
                                 "Power-on message line 7",
1726
                                 RadioSettingValueString(0, 8, _filter(
1727
                                                         _msg.line7)))
1728
            advanced.append(line7)
1729
            line8 = RadioSetting("poweron_msg.line8", "Static message",
1730
                                 RadioSettingValueString(0, 8, _filter(
1731
                                                         _msg.line8)))
1732
            advanced.append(line8)
1733
        elif self.COLOR_LCD2 or self.COLOR_LCD3 or self.COLOR_LCD4:
1734
            _msg = self._memobj.static_msg
1735
            line = RadioSetting("static_msg.line", "Static message",
1736
                                RadioSettingValueString(0, 16, _filter(
1737
                                    _msg.line)))
1738
            advanced.append(line)
1739
        else:
1740
            _msg = self._memobj.poweron_msg
1741
            line1 = RadioSetting("poweron_msg.line1",
1742
                                 "Power-on message line 1",
1743
                                 RadioSettingValueString(0, 6, _filter(
1744
                                                         _msg.line1)))
1745
            advanced.append(line1)
1746
            line2 = RadioSetting("poweron_msg.line2",
1747
                                 "Power-on message line 2",
1748
                                 RadioSettingValueString(0, 6, _filter(
1749
                                                         _msg.line2)))
1750
            advanced.append(line2)
1751

    
1752
        if self.MODEL in ("UV-2501", "UV-5001"):
1753
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1754
                                   RadioSettingValueBoolean(
1755
                                       _mem.settings2.vfomren))
1756
            advanced.append(vfomren)
1757

    
1758
            reseten = RadioSetting("settings2.reseten", "RESET",
1759
                                   RadioSettingValueBoolean(
1760
                                       _mem.settings2.reseten))
1761
            advanced.append(reseten)
1762

    
1763
            menuen = RadioSetting("settings2.menuen", "Menu",
1764
                                  RadioSettingValueBoolean(
1765
                                      _mem.settings2.menuen))
1766
            advanced.append(menuen)
1767

    
1768
        # Other
1769
        def convert_bytes_to_limit(bytes):
1770
            limit = ""
1771
            for byte in bytes:
1772
                if byte < 10:
1773
                    limit += chr(byte + 0x30)
1774
                else:
1775
                    break
1776
            return limit
1777

    
1778
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1779
            _ranges = self._memobj.ranges220
1780
            ranges = "ranges220"
1781
        else:
1782
            _ranges = self._memobj.ranges
1783
            ranges = "ranges"
1784

    
1785
        _limit = convert_bytes_to_limit(_ranges.vhf_low)
1786
        val = RadioSettingValueString(0, 3, _limit)
1787
        val.set_mutable(False)
1788
        vhf_low = RadioSetting("%s.vhf_low" % ranges, "VHF low", val)
1789
        other.append(vhf_low)
1790

    
1791
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1792
        val = RadioSettingValueString(0, 3, _limit)
1793
        val.set_mutable(False)
1794
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1795
        other.append(vhf_high)
1796

    
1797
        if self.BANDS == 3 or self.BANDS == 4:
1798
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1799
            val = RadioSettingValueString(0, 3, _limit)
1800
            val.set_mutable(False)
1801
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1802
            other.append(vhf2_low)
1803

    
1804
            _limit = convert_bytes_to_limit(_ranges.vhf2_high)
1805
            val = RadioSettingValueString(0, 3, _limit)
1806
            val.set_mutable(False)
1807
            vhf2_high = RadioSetting("%s.vhf2_high" % ranges, "VHF2 high", val)
1808
            other.append(vhf2_high)
1809

    
1810
        _limit = convert_bytes_to_limit(_ranges.uhf_low)
1811
        val = RadioSettingValueString(0, 3, _limit)
1812
        val.set_mutable(False)
1813
        uhf_low = RadioSetting("%s.uhf_low" % ranges, "UHF low", val)
1814
        other.append(uhf_low)
1815

    
1816
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1817
        val = RadioSettingValueString(0, 3, _limit)
1818
        val.set_mutable(False)
1819
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1820
        other.append(uhf_high)
1821

    
1822
        if self.BANDS == 4:
1823
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1824
            val = RadioSettingValueString(0, 3, _limit)
1825
            val.set_mutable(False)
1826
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1827
            other.append(uhf2_low)
1828

    
1829
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1830
            val = RadioSettingValueString(0, 3, _limit)
1831
            val.set_mutable(False)
1832
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1833
            other.append(uhf2_high)
1834

    
1835
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1836
        val.set_mutable(False)
1837
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1838
        other.append(fp)
1839

    
1840
        # Work
1841
        if self.COLOR_LCD:
1842
            dispab = RadioSetting("settings2.dispab", "Display",
1843
                                  RadioSettingValueList(
1844
                                      LIST_ABCD,
1845
                                      LIST_ABCD[_mem.settings2.dispab]))
1846
            work.append(dispab)
1847
        else:
1848
            dispab = RadioSetting("settings2.dispab", "Display",
1849
                                  RadioSettingValueList(
1850
                                      LIST_AB,
1851
                                      LIST_AB[_mem.settings2.dispab]))
1852
            work.append(dispab)
1853

    
1854
        if self.COLOR_LCD:
1855
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode",
1856
                                  RadioSettingValueList(
1857
                                      LIST_VFOMR,
1858
                                      LIST_VFOMR[_mem.settings2.vfomra]))
1859
            work.append(vfomra)
1860

    
1861
            vfomrb = RadioSetting("settings2.vfomrb", "VFO/MR B mode",
1862
                                  RadioSettingValueList(
1863
                                      LIST_VFOMR,
1864
                                      LIST_VFOMR[_mem.settings2.vfomrb]))
1865
            work.append(vfomrb)
1866

    
1867
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode",
1868
                                  RadioSettingValueList(
1869
                                      LIST_VFOMR,
1870
                                      LIST_VFOMR[_mem.settings2.vfomrc]))
1871
            work.append(vfomrc)
1872

    
1873
            if not self.COLOR_LCD4:
1874
                vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode",
1875
                                      RadioSettingValueList(
1876
                                          LIST_VFOMR,
1877
                                          LIST_VFOMR[_mem.settings2.vfomrd]))
1878
                work.append(vfomrd)
1879
        else:
1880
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode",
1881
                                 RadioSettingValueList(
1882
                                     LIST_VFOMR,
1883
                                     LIST_VFOMR[_mem.settings2.vfomr]))
1884
            work.append(vfomr)
1885

    
1886
        keylock = RadioSetting("settings2.keylock", "Keypad lock",
1887
                               RadioSettingValueBoolean(
1888
                                   _mem.settings2.keylock))
1889
        work.append(keylock)
1890

    
1891
        mrcha = RadioSetting("settings2.mrcha", "MR A channel",
1892
                             RadioSettingValueInteger(0, self._upper,
1893
                                                      _mem.settings2.mrcha))
1894
        work.append(mrcha)
1895

    
1896
        mrchb = RadioSetting("settings2.mrchb", "MR B channel",
1897
                             RadioSettingValueInteger(0, self._upper,
1898
                                                      _mem.settings2.mrchb))
1899
        work.append(mrchb)
1900

    
1901
        if self.COLOR_LCD:
1902
            mrchc = RadioSetting("settings2.mrchc", "MR C channel",
1903
                                 RadioSettingValueInteger(
1904
                                     0, self._upper, _mem.settings2.mrchc))
1905
            work.append(mrchc)
1906

    
1907
            if not self.COLOR_LCD4:
1908
                mrchd = RadioSetting("settings2.mrchd", "MR D channel",
1909
                                     RadioSettingValueInteger(
1910
                                         0, self._upper, _mem.settings2.mrchd))
1911
                work.append(mrchd)
1912

    
1913
        def convert_bytes_to_freq(bytes):
1914
            real_freq = 0
1915
            for byte in bytes:
1916
                real_freq = (real_freq * 10) + byte
1917
            return chirp_common.format_freq(real_freq * 10)
1918

    
1919
        def my_validate(value):
1920
            _vhf_lower = int(convert_bytes_to_limit(_ranges.vhf_low))
1921
            _vhf_upper = int(convert_bytes_to_limit(_ranges.vhf_high))
1922
            _uhf_lower = int(convert_bytes_to_limit(_ranges.uhf_low))
1923
            _uhf_upper = int(convert_bytes_to_limit(_ranges.uhf_high))
1924
            if self.BANDS == 3 or self.BANDS == 4:
1925
                _vhf2_lower = int(convert_bytes_to_limit(_ranges.vhf2_low))
1926
                _vhf2_upper = int(convert_bytes_to_limit(_ranges.vhf2_high))
1927
            if self.BANDS == 4:
1928
                _uhf2_lower = int(convert_bytes_to_limit(_ranges.uhf2_low))
1929
                _uhf2_upper = int(convert_bytes_to_limit(_ranges.uhf2_high))
1930

    
1931
            value = chirp_common.parse_freq(value)
1932
            msg = ("Can't be less then %i.0000")
1933
            if value > 99000000 and value < _vhf_lower * 1000000:
1934
                raise InvalidValueError(msg % (_vhf_lower))
1935
            msg = ("Can't be betweeb %i.9975-%i.0000")
1936
            if self.BANDS == 2:
1937
                if (_vhf_upper + 1) * 1000000 <= value and \
1938
                        value < _uhf_lower * 1000000:
1939
                    raise InvalidValueError(msg % (_vhf_upper, _uhf_lower))
1940
            if self.BANDS == 3:
1941
                if (_vhf_upper + 1) * 1000000 <= value and \
1942
                        value < _vhf2_lower * 1000000:
1943
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1944
                if (_vhf2_upper + 1) * 1000000 <= value and \
1945
                        value < _uhf_lower * 1000000:
1946
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf_lower))
1947
            if self.BANDS == 4:
1948
                if (_vhf_upper + 1) * 1000000 <= value and \
1949
                        value < _vhf2_lower * 1000000:
1950
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1951
                if (_vhf2_upper + 1) * 1000000 <= value and \
1952
                        value < _uhf2_lower * 1000000:
1953
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf2_lower))
1954
                if (_uhf2_upper + 1) * 1000000 <= value and \
1955
                        value < _uhf_lower * 1000000:
1956
                    raise InvalidValueError(msg % (_uhf2_upper, _uhf_lower))
1957
            msg = ("Can't be greater then %i.9975")
1958
            if value > 99000000 and value >= _uhf_upper * 1000000:
1959
                raise InvalidValueError(msg % (_uhf_upper))
1960
            return chirp_common.format_freq(value)
1961

    
1962
        def apply_freq(setting, obj):
1963
            value = chirp_common.parse_freq(str(setting.value)) / 10
1964
            for i in range(7, -1, -1):
1965
                obj.freq[i] = value % 10
1966
                value /= 10
1967

    
1968
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1969
                                        _mem.vfo.a.freq))
1970
        val1a.set_validate_callback(my_validate)
1971
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
1972
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
1973
        work.append(vfoafreq)
1974

    
1975
        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1976
                                        _mem.vfo.b.freq))
1977
        val1b.set_validate_callback(my_validate)
1978
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
1979
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
1980
        work.append(vfobfreq)
1981

    
1982
        if self.COLOR_LCD:
1983
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1984
                                            _mem.vfo.c.freq))
1985
            val1c.set_validate_callback(my_validate)
1986
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
1987
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
1988
            work.append(vfocfreq)
1989

    
1990
            if not self.COLOR_LCD4:
1991
                val1d = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1992
                                                _mem.vfo.d.freq))
1993
                val1d.set_validate_callback(my_validate)
1994
                vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
1995
                vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
1996
                work.append(vfodfreq)
1997

    
1998
        if not self.MODEL == "GMRS-50X1":
1999
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift",
2000
                                      RadioSettingValueList(
2001
                                          LIST_SHIFT,
2002
                                          LIST_SHIFT[_mem.vfo.a.shiftd]))
2003
            work.append(vfoashiftd)
2004

    
2005
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift",
2006
                                      RadioSettingValueList(
2007
                                          LIST_SHIFT,
2008
                                          LIST_SHIFT[_mem.vfo.b.shiftd]))
2009
            work.append(vfobshiftd)
2010

    
2011
            if self.COLOR_LCD:
2012
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift",
2013
                                          RadioSettingValueList(
2014
                                              LIST_SHIFT,
2015
                                              LIST_SHIFT[_mem.vfo.c.shiftd]))
2016
                work.append(vfocshiftd)
2017

    
2018
                if not self.COLOR_LCD4:
2019
                    val = RadioSettingValueList(LIST_SHIFT,
2020
                                                LIST_SHIFT[_mem.vfo.d.shiftd])
2021
                    vfodshiftd = RadioSetting("vfo.d.shiftd", "VFO D shift",
2022
                                              val)
2023
                    work.append(vfodshiftd)
2024

    
2025
        def convert_bytes_to_offset(bytes):
2026
            real_offset = 0
2027
            for byte in bytes:
2028
                real_offset = (real_offset * 10) + byte
2029
            return chirp_common.format_freq(real_offset * 1000)
2030

    
2031
        def apply_offset(setting, obj):
2032
            value = chirp_common.parse_freq(str(setting.value)) / 1000
2033
            for i in range(5, -1, -1):
2034
                obj.offset[i] = value % 10
2035
                value /= 10
2036

    
2037
        if not self.MODEL == "GMRS-50X1":
2038
            if self.COLOR_LCD:
2039
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2040
                                                _mem.vfo.a.offset))
2041
                vfoaoffset = RadioSetting("vfo.a.offset",
2042
                                          "VFO A offset (0.000-999.999)",
2043
                                          val1a)
2044
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
2045
                work.append(vfoaoffset)
2046

    
2047
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2048
                                                _mem.vfo.b.offset))
2049
                vfoboffset = RadioSetting("vfo.b.offset",
2050
                                          "VFO B offset (0.000-999.999)",
2051
                                          val1b)
2052
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
2053
                work.append(vfoboffset)
2054

    
2055
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2056
                                                _mem.vfo.c.offset))
2057
                vfocoffset = RadioSetting("vfo.c.offset",
2058
                                          "VFO C offset (0.000-999.999)",
2059
                                          val1c)
2060
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
2061
                work.append(vfocoffset)
2062

    
2063
                if not self.COLOR_LCD4:
2064
                    val1d = RadioSettingValueString(0, 10,
2065
                                                    convert_bytes_to_offset(
2066
                                                        _mem.vfo.d.offset))
2067
                    vfodoffset = RadioSetting("vfo.d.offset",
2068
                                              "VFO D offset (0.000-999.999)",
2069
                                              val1d)
2070
                    vfodoffset.set_apply_callback(apply_offset, _mem.vfo.d)
2071
                    work.append(vfodoffset)
2072
            else:
2073
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2074
                                                _mem.vfo.a.offset))
2075
                vfoaoffset = RadioSetting("vfo.a.offset",
2076
                                          "VFO A offset (0.000-99.999)", val1a)
2077
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
2078
                work.append(vfoaoffset)
2079

    
2080
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2081
                                                _mem.vfo.b.offset))
2082
                vfoboffset = RadioSetting("vfo.b.offset",
2083
                                          "VFO B offset (0.000-99.999)", val1b)
2084
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
2085
                work.append(vfoboffset)
2086

    
2087
        if not self.MODEL == "GMRS-50X1":
2088
            vfoatxp = RadioSetting("vfo.a.power", "VFO A power",
2089
                                   RadioSettingValueList(
2090
                                       LIST_TXP,
2091
                                       LIST_TXP[_mem.vfo.a.power]))
2092
            work.append(vfoatxp)
2093

    
2094
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power",
2095
                                   RadioSettingValueList(
2096
                                       LIST_TXP,
2097
                                       LIST_TXP[_mem.vfo.b.power]))
2098
            work.append(vfobtxp)
2099

    
2100
            if self.COLOR_LCD:
2101
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power",
2102
                                       RadioSettingValueList(
2103
                                           LIST_TXP,
2104
                                           LIST_TXP[_mem.vfo.c.power]))
2105
                work.append(vfoctxp)
2106

    
2107
                if not self.COLOR_LCD4:
2108
                    vfodtxp = RadioSetting("vfo.d.power", "VFO D power",
2109
                                           RadioSettingValueList(
2110
                                               LIST_TXP,
2111
                                               LIST_TXP[_mem.vfo.d.power]))
2112
                    work.append(vfodtxp)
2113

    
2114
        if not self.MODEL == "GMRS-50X1":
2115
            vfoawide = RadioSetting("vfo.a.wide", "VFO A bandwidth",
2116
                                    RadioSettingValueList(
2117
                                        LIST_WIDE,
2118
                                        LIST_WIDE[_mem.vfo.a.wide]))
2119
            work.append(vfoawide)
2120

    
2121
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth",
2122
                                    RadioSettingValueList(
2123
                                        LIST_WIDE,
2124
                                        LIST_WIDE[_mem.vfo.b.wide]))
2125
            work.append(vfobwide)
2126

    
2127
            if self.COLOR_LCD:
2128
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth",
2129
                                        RadioSettingValueList(
2130
                                            LIST_WIDE,
2131
                                            LIST_WIDE[_mem.vfo.c.wide]))
2132
                work.append(vfocwide)
2133

    
2134
                if not self.COLOR_LCD4:
2135
                    vfodwide = RadioSetting("vfo.d.wide", "VFO D bandwidth",
2136
                                            RadioSettingValueList(
2137
                                                LIST_WIDE,
2138
                                                LIST_WIDE[_mem.vfo.d.wide]))
2139
                    work.append(vfodwide)
2140

    
2141
        vfoastep = RadioSetting("vfo.a.step", "VFO A step",
2142
                                RadioSettingValueList(
2143
                                    LIST_STEP,
2144
                                    LIST_STEP[_mem.vfo.a.step]))
2145
        work.append(vfoastep)
2146

    
2147
        vfobstep = RadioSetting("vfo.b.step", "VFO B step",
2148
                                RadioSettingValueList(
2149
                                    LIST_STEP,
2150
                                    LIST_STEP[_mem.vfo.b.step]))
2151
        work.append(vfobstep)
2152

    
2153
        if self.COLOR_LCD:
2154
            vfocstep = RadioSetting("vfo.c.step", "VFO C step",
2155
                                    RadioSettingValueList(
2156
                                        LIST_STEP,
2157
                                        LIST_STEP[_mem.vfo.c.step]))
2158
            work.append(vfocstep)
2159

    
2160
            if not self.COLOR_LCD4:
2161
                vfodstep = RadioSetting("vfo.d.step", "VFO D step",
2162
                                        RadioSettingValueList(
2163
                                            LIST_STEP,
2164
                                            LIST_STEP[_mem.vfo.d.step]))
2165
                work.append(vfodstep)
2166

    
2167
        vfoaoptsig = RadioSetting("vfo.a.optsig", "VFO A optional signal",
2168
                                  RadioSettingValueList(
2169
                                      OPTSIG_LIST,
2170
                                      OPTSIG_LIST[_mem.vfo.a.optsig]))
2171
        work.append(vfoaoptsig)
2172

    
2173
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal",
2174
                                  RadioSettingValueList(
2175
                                      OPTSIG_LIST,
2176
                                      OPTSIG_LIST[_mem.vfo.b.optsig]))
2177
        work.append(vfoboptsig)
2178

    
2179
        if self.COLOR_LCD:
2180
            vfocoptsig = RadioSetting("vfo.c.optsig", "VFO C optional signal",
2181
                                      RadioSettingValueList(
2182
                                          OPTSIG_LIST,
2183
                                          OPTSIG_LIST[_mem.vfo.c.optsig]))
2184
            work.append(vfocoptsig)
2185

    
2186
            if not self.COLOR_LCD4:
2187
                val = RadioSettingValueList(OPTSIG_LIST,
2188
                                            OPTSIG_LIST[_mem.vfo.d.optsig])
2189
                vfodoptsig = RadioSetting("vfo.d.optsig",
2190
                                          "VFO D optional signal", val)
2191
                work.append(vfodoptsig)
2192

    
2193
        vfoaspmute = RadioSetting("vfo.a.spmute", "VFO A speaker mute",
2194
                                  RadioSettingValueList(
2195
                                      SPMUTE_LIST,
2196
                                      SPMUTE_LIST[_mem.vfo.a.spmute]))
2197
        work.append(vfoaspmute)
2198

    
2199
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute",
2200
                                  RadioSettingValueList(
2201
                                      SPMUTE_LIST,
2202
                                      SPMUTE_LIST[_mem.vfo.b.spmute]))
2203
        work.append(vfobspmute)
2204

    
2205
        if self.COLOR_LCD:
2206
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute",
2207
                                      RadioSettingValueList(
2208
                                          SPMUTE_LIST,
2209
                                          SPMUTE_LIST[_mem.vfo.c.spmute]))
2210
            work.append(vfocspmute)
2211

    
2212
            if not self.COLOR_LCD4:
2213
                vfodspmute = RadioSetting("vfo.d.spmute", "VFO D speaker mute",
2214
                                          RadioSettingValueList(
2215
                                              SPMUTE_LIST,
2216
                                              SPMUTE_LIST[_mem.vfo.d.spmute]))
2217
                work.append(vfodspmute)
2218

    
2219
        if not self.COLOR_LCD or \
2220
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2221
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble",
2222
                                   RadioSettingValueBoolean(
2223
                                       _mem.vfo.a.scramble))
2224
            work.append(vfoascr)
2225

    
2226
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble",
2227
                                   RadioSettingValueBoolean(
2228
                                       _mem.vfo.b.scramble))
2229
            work.append(vfobscr)
2230

    
2231
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2232
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble",
2233
                                   RadioSettingValueBoolean(
2234
                                       _mem.vfo.c.scramble))
2235
            work.append(vfocscr)
2236

    
2237
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble",
2238
                                   RadioSettingValueBoolean(
2239
                                       _mem.vfo.d.scramble))
2240
            work.append(vfodscr)
2241

    
2242
        if not self.MODEL == "GMRS-50X1":
2243
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID",
2244
                                     RadioSettingValueList(
2245
                                         PTTIDCODE_LIST,
2246
                                         PTTIDCODE_LIST[_mem.vfo.a.scode]))
2247
            work.append(vfoascode)
2248

    
2249
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID",
2250
                                     RadioSettingValueList(
2251
                                         PTTIDCODE_LIST,
2252
                                         PTTIDCODE_LIST[_mem.vfo.b.scode]))
2253
            work.append(vfobscode)
2254

    
2255
            if self.COLOR_LCD:
2256
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID",
2257
                                         RadioSettingValueList(
2258
                                             PTTIDCODE_LIST,
2259
                                             PTTIDCODE_LIST[_mem.vfo.c.scode]))
2260
                work.append(vfocscode)
2261

    
2262
                if not self.COLOR_LCD4:
2263
                    val = RadioSettingValueList(PTTIDCODE_LIST,
2264
                                                PTTIDCODE_LIST[
2265
                                                    _mem.vfo.d.scode])
2266
                    vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID",
2267
                                             val)
2268
                    work.append(vfodscode)
2269

    
2270
        if not self.MODEL == "GMRS-50X1":
2271
            pttid = RadioSetting("settings.pttid", "PTT ID",
2272
                                 RadioSettingValueList(
2273
                                     PTTID_LIST,
2274
                                     PTTID_LIST[_mem.settings.pttid]))
2275
            work.append(pttid)
2276

    
2277
        if not self.COLOR_LCD:
2278
            # FM presets
2279
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2280
            top.append(fm_presets)
2281

    
2282
            def fm_validate(value):
2283
                if value == 0:
2284
                    return chirp_common.format_freq(value)
2285
                if not (87.5 <= value and value <= 108.0):  # 87.5-108MHz
2286
                    msg = ("FM-Preset-Frequency: " +
2287
                           "Must be between 87.5 and 108 MHz")
2288
                    raise InvalidValueError(msg)
2289
                return value
2290

    
2291
            def apply_fm_preset_name(setting, obj):
2292
                valstring = str(setting.value)
2293
                for i in range(0, 6):
2294
                    if valstring[i] in VALID_CHARS:
2295
                        obj[i] = valstring[i]
2296
                    else:
2297
                        obj[i] = '0xff'
2298

    
2299
            def apply_fm_freq(setting, obj):
2300
                value = chirp_common.parse_freq(str(setting.value)) / 10
2301
                for i in range(7, -1, -1):
2302
                    obj.freq[i] = value % 10
2303
                    value /= 10
2304

    
2305
            _presets = self._memobj.fm_radio_preset
2306
            i = 1
2307
            for preset in _presets:
2308
                line = RadioSetting("fm_presets_" + str(i),
2309
                                    "Station name " + str(i),
2310
                                    RadioSettingValueString(0, 6, _filter(
2311
                                        preset.broadcast_station_name)))
2312
                line.set_apply_callback(apply_fm_preset_name,
2313
                                        preset.broadcast_station_name)
2314

    
2315
                val = RadioSettingValueFloat(0, 108,
2316
                                             convert_bytes_to_freq(
2317
                                                 preset.freq))
2318
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2319
                                      "Frequency " + str(i), val)
2320
                val.set_validate_callback(fm_validate)
2321
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2322
                fm_presets.append(line)
2323
                fm_presets.append(fmfreq)
2324

    
2325
                i = i + 1
2326

    
2327
        # DTMF-Setting
2328
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2329
                                              "DTMF Encoding Settings")
2330
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2331
                                              "DTMF Decoding Settings")
2332
        top.append(dtmf_enc_settings)
2333
        top.append(dtmf_dec_settings)
2334
        txdisable = RadioSetting("dtmf_settings.txdisable",
2335
                                 "TX-Disable",
2336
                                 RadioSettingValueBoolean(
2337
                                     _mem.dtmf_settings.txdisable))
2338
        dtmf_enc_settings.append(txdisable)
2339

    
2340
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2341
                                 "RX-Disable",
2342
                                 RadioSettingValueBoolean(
2343
                                     _mem.dtmf_settings.rxdisable))
2344
        dtmf_enc_settings.append(rxdisable)
2345

    
2346
        if _mem.dtmf_settings.dtmfspeed_on > 0x0F:
2347
            val = 0x03
2348
        else:
2349
            val = _mem.dtmf_settings.dtmfspeed_on
2350
        dtmfspeed_on = RadioSetting(
2351
            "dtmf_settings.dtmfspeed_on",
2352
            "DTMF Speed (On Time)",
2353
            RadioSettingValueList(LIST_DTMF_SPEED,
2354
                                  LIST_DTMF_SPEED[
2355
                                      val]))
2356
        dtmf_enc_settings.append(dtmfspeed_on)
2357

    
2358
        if _mem.dtmf_settings.dtmfspeed_off > 0x0F:
2359
            val = 0x03
2360
        else:
2361
            val = _mem.dtmf_settings.dtmfspeed_off
2362
        dtmfspeed_off = RadioSetting(
2363
            "dtmf_settings.dtmfspeed_off",
2364
            "DTMF Speed (Off Time)",
2365
            RadioSettingValueList(LIST_DTMF_SPEED,
2366
                                  LIST_DTMF_SPEED[
2367
                                      val]))
2368
        dtmf_enc_settings.append(dtmfspeed_off)
2369

    
2370
        def memory2string(dmtf_mem):
2371
            dtmf_string = ""
2372
            for digit in dmtf_mem:
2373
                if digit != 255:
2374
                    index = LIST_DTMF_VALUES.index(digit)
2375
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2376
            return dtmf_string
2377

    
2378
        def apply_dmtf_frame(setting, obj):
2379
            LOG.debug("Setting DTMF-Code: " + str(setting.value))
2380
            val_string = str(setting.value)
2381
            for i in range(0, 16):
2382
                obj[i] = 255
2383
            i = 0
2384
            for current_char in val_string:
2385
                current_char = current_char.upper()
2386
                index = LIST_DTMF_DIGITS.index(current_char)
2387
                obj[i] = LIST_DTMF_VALUES[index]
2388
                i = i + 1
2389

    
2390
        codes = self._memobj.dtmf_codes
2391
        i = 1
2392
        for dtmfcode in codes:
2393
            val = RadioSettingValueString(0, 16, memory2string(
2394
                                              dtmfcode.code),
2395
                                          False, CHARSET_DTMF_DIGITS)
2396
            line = RadioSetting("dtmf_code_" + str(i) + "_code",
2397
                                "DMTF Code " + str(i), val)
2398
            line.set_apply_callback(apply_dmtf_frame, dtmfcode.code)
2399
            dtmf_enc_settings.append(line)
2400
            i = i + 1
2401

    
2402
        line = RadioSetting("dtmf_settings.mastervice",
2403
                            "Master and Vice ID",
2404
                            RadioSettingValueBoolean(
2405
                                _mem.dtmf_settings.mastervice))
2406
        dtmf_dec_settings.append(line)
2407

    
2408
        val = RadioSettingValueString(0, 16, memory2string(
2409
                                          _mem.dtmf_settings.masterid),
2410
                                      False, CHARSET_DTMF_DIGITS)
2411
        line = RadioSetting("dtmf_settings.masterid",
2412
                            "Master Control ID ", val)
2413
        line.set_apply_callback(apply_dmtf_frame,
2414
                                _mem.dtmf_settings.masterid)
2415
        dtmf_dec_settings.append(line)
2416

    
2417
        line = RadioSetting("dtmf_settings.minspection",
2418
                            "Master Inspection",
2419
                            RadioSettingValueBoolean(
2420
                                _mem.dtmf_settings.minspection))
2421
        dtmf_dec_settings.append(line)
2422

    
2423
        line = RadioSetting("dtmf_settings.mmonitor",
2424
                            "Master Monitor",
2425
                            RadioSettingValueBoolean(
2426
                                _mem.dtmf_settings.mmonitor))
2427
        dtmf_dec_settings.append(line)
2428

    
2429
        line = RadioSetting("dtmf_settings.mstun",
2430
                            "Master Stun",
2431
                            RadioSettingValueBoolean(
2432
                                _mem.dtmf_settings.mstun))
2433
        dtmf_dec_settings.append(line)
2434

    
2435
        line = RadioSetting("dtmf_settings.mkill",
2436
                            "Master Kill",
2437
                            RadioSettingValueBoolean(
2438
                                _mem.dtmf_settings.mkill))
2439
        dtmf_dec_settings.append(line)
2440

    
2441
        line = RadioSetting("dtmf_settings.mrevive",
2442
                            "Master Revive",
2443
                            RadioSettingValueBoolean(
2444
                                _mem.dtmf_settings.mrevive))
2445
        dtmf_dec_settings.append(line)
2446

    
2447
        val = RadioSettingValueString(0, 16, memory2string(
2448
                                          _mem.dtmf_settings.viceid),
2449
                                      False, CHARSET_DTMF_DIGITS)
2450
        line = RadioSetting("dtmf_settings.viceid",
2451
                            "Vice Control ID ", val)
2452
        line.set_apply_callback(apply_dmtf_frame,
2453
                                _mem.dtmf_settings.viceid)
2454
        dtmf_dec_settings.append(line)
2455

    
2456
        line = RadioSetting("dtmf_settings.vinspection",
2457
                            "Vice Inspection",
2458
                            RadioSettingValueBoolean(
2459
                                _mem.dtmf_settings.vinspection))
2460
        dtmf_dec_settings.append(line)
2461

    
2462
        line = RadioSetting("dtmf_settings.vmonitor",
2463
                            "Vice Monitor",
2464
                            RadioSettingValueBoolean(
2465
                                _mem.dtmf_settings.vmonitor))
2466
        dtmf_dec_settings.append(line)
2467

    
2468
        line = RadioSetting("dtmf_settings.vstun",
2469
                            "Vice Stun",
2470
                            RadioSettingValueBoolean(
2471
                                _mem.dtmf_settings.vstun))
2472
        dtmf_dec_settings.append(line)
2473

    
2474
        line = RadioSetting("dtmf_settings.vkill",
2475
                            "Vice Kill",
2476
                            RadioSettingValueBoolean(
2477
                                _mem.dtmf_settings.vkill))
2478
        dtmf_dec_settings.append(line)
2479

    
2480
        line = RadioSetting("dtmf_settings.vrevive",
2481
                            "Vice Revive",
2482
                            RadioSettingValueBoolean(
2483
                                _mem.dtmf_settings.vrevive))
2484
        dtmf_dec_settings.append(line)
2485

    
2486
        val = RadioSettingValueString(0, 16, memory2string(
2487
                                          _mem.dtmf_settings.inspection),
2488
                                      False, CHARSET_DTMF_DIGITS)
2489
        line = RadioSetting("dtmf_settings.inspection",
2490
                            "Inspection", val)
2491
        line.set_apply_callback(apply_dmtf_frame,
2492
                                _mem.dtmf_settings.inspection)
2493
        dtmf_dec_settings.append(line)
2494

    
2495
        val = RadioSettingValueString(0, 16, memory2string(
2496
                                          _mem.dtmf_settings.alarmcode),
2497
                                      False, CHARSET_DTMF_DIGITS)
2498
        line = RadioSetting("dtmf_settings.alarmcode",
2499
                            "Alarm", val)
2500
        line.set_apply_callback(apply_dmtf_frame,
2501
                                _mem.dtmf_settings.alarmcode)
2502
        dtmf_dec_settings.append(line)
2503

    
2504
        val = RadioSettingValueString(0, 16, memory2string(
2505
                                          _mem.dtmf_settings.kill),
2506
                                      False, CHARSET_DTMF_DIGITS)
2507
        line = RadioSetting("dtmf_settings.kill",
2508
                            "Kill", val)
2509
        line.set_apply_callback(apply_dmtf_frame,
2510
                                _mem.dtmf_settings.kill)
2511
        dtmf_dec_settings.append(line)
2512

    
2513
        val = RadioSettingValueString(0, 16, memory2string(
2514
                                          _mem.dtmf_settings.monitor),
2515
                                      False, CHARSET_DTMF_DIGITS)
2516
        line = RadioSetting("dtmf_settings.monitor",
2517
                            "Monitor", val)
2518
        line.set_apply_callback(apply_dmtf_frame,
2519
                                _mem.dtmf_settings.monitor)
2520
        dtmf_dec_settings.append(line)
2521

    
2522
        val = RadioSettingValueString(0, 16, memory2string(
2523
                                          _mem.dtmf_settings.stun),
2524
                                      False, CHARSET_DTMF_DIGITS)
2525
        line = RadioSetting("dtmf_settings.stun",
2526
                            "Stun", val)
2527
        line.set_apply_callback(apply_dmtf_frame,
2528
                                _mem.dtmf_settings.stun)
2529
        dtmf_dec_settings.append(line)
2530

    
2531
        val = RadioSettingValueString(0, 16, memory2string(
2532
                                          _mem.dtmf_settings.revive),
2533
                                      False, CHARSET_DTMF_DIGITS)
2534
        line = RadioSetting("dtmf_settings.revive",
2535
                            "Revive", val)
2536
        line.set_apply_callback(apply_dmtf_frame,
2537
                                _mem.dtmf_settings.revive)
2538
        dtmf_dec_settings.append(line)
2539

    
2540
        def apply_dmtf_listvalue(setting, obj):
2541
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2542
            val = str(setting.value)
2543
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2544
            val = LIST_DTMF_SPECIAL_VALUES[index]
2545
            obj.set_value(val)
2546

    
2547
        if _mem.dtmf_settings.groupcode not in LIST_DTMF_SPECIAL_VALUES:
2548
            val = 0x0B
2549
        else:
2550
            val = _mem.dtmf_settings.groupcode
2551
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2552
        line = RadioSetting(
2553
            "dtmf_settings.groupcode",
2554
            "Group Code",
2555
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2556
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2557
        line.set_apply_callback(apply_dmtf_listvalue,
2558
                                _mem.dtmf_settings.groupcode)
2559
        dtmf_dec_settings.append(line)
2560

    
2561
        if _mem.dtmf_settings.spacecode not in LIST_DTMF_SPECIAL_VALUES:
2562
            val = 0x0C
2563
        else:
2564
            val = _mem.dtmf_settings.spacecode
2565
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2566
        line = RadioSetting(
2567
            "dtmf_settings.spacecode",
2568
            "Space Code",
2569
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2570
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2571
        line.set_apply_callback(apply_dmtf_listvalue,
2572
                                _mem.dtmf_settings.spacecode)
2573
        dtmf_dec_settings.append(line)
2574

    
2575
        if self.COLOR_LCD:
2576
            if _mem.dtmf_settings.resettime > 0x63:
2577
                val = 0x4F
2578
            else:
2579
                val = _mem.dtmf_settings.resettime
2580
            line = RadioSetting(
2581
                "dtmf_settings.resettime",
2582
                "Reset time",
2583
                RadioSettingValueList(LIST_5TONE_RESET_COLOR,
2584
                                      LIST_5TONE_RESET_COLOR[
2585
                                          val]))
2586
            dtmf_dec_settings.append(line)
2587
        else:
2588
            line = RadioSetting(
2589
                "dtmf_settings.resettime",
2590
                "Reset time",
2591
                RadioSettingValueList(LIST_5TONE_RESET,
2592
                                      LIST_5TONE_RESET[
2593
                                          _mem.dtmf_settings.resettime]))
2594
            dtmf_dec_settings.append(line)
2595

    
2596
        if _mem.dtmf_settings.delayproctime > 0x27:
2597
            val = 0x04
2598
        else:
2599
            val = _mem.dtmf_settings.delayproctime
2600
        line = RadioSetting(
2601
            "dtmf_settings.delayproctime",
2602
            "Delay processing time",
2603
            RadioSettingValueList(LIST_DTMF_DELAY,
2604
                                  LIST_DTMF_DELAY[
2605
                                      val]))
2606
        dtmf_dec_settings.append(line)
2607

    
2608
        # 5 Tone Settings
2609
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2610
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2611

    
2612
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2613
        group_5tone.append(stds_5tone)
2614
        group_5tone.append(codes_5tone)
2615

    
2616
        top.append(group_5tone)
2617

    
2618
        def apply_list_value(setting, obj):
2619
            options = setting.value.get_options()
2620
            obj.set_value(options.index(str(setting.value)))
2621

    
2622
        _5tone_standards = self._memobj._5tone_std_settings
2623
        i = 0
2624
        for standard in _5tone_standards:
2625
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2626
                                          LIST_5TONE_STANDARDS[i])
2627
            stds_5tone.append(std_5tone)
2628

    
2629
            period = standard.period
2630
            if period == 255:
2631
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2632
                          " is not yet configured. Setting to 70ms.")
2633
                period = 5
2634

    
2635
            if period <= len(LIST_5TONE_STANDARD_PERIODS):
2636
                line = RadioSetting(
2637
                    "_5tone_std_settings_" + str(i) + "_period",
2638
                    "Period (ms)", RadioSettingValueList
2639
                    (LIST_5TONE_STANDARD_PERIODS,
2640
                     LIST_5TONE_STANDARD_PERIODS[period]))
2641
                line.set_apply_callback(apply_list_value, standard.period)
2642
                std_5tone.append(line)
2643
            else:
2644
                LOG.debug("Invalid value for 5tone period! Disabling.")
2645

    
2646
            group_tone = standard.group_tone
2647
            if group_tone == 255:
2648
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2649
                          " is not yet configured. Setting to A.")
2650
                group_tone = 10
2651

    
2652
            if group_tone <= len(LIST_5TONE_DIGITS):
2653
                line = RadioSetting(
2654
                    "_5tone_std_settings_" + str(i) + "_grouptone",
2655
                    "Group Tone",
2656
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2657
                                          LIST_5TONE_DIGITS[
2658
                                              group_tone]))
2659
                line.set_apply_callback(apply_list_value,
2660
                                        standard.group_tone)
2661
                std_5tone.append(line)
2662
            else:
2663
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2664

    
2665
            repeat_tone = standard.repeat_tone
2666
            if repeat_tone == 255:
2667
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2668
                          " is not yet configured. Setting to E.")
2669
                repeat_tone = 14
2670

    
2671
            if repeat_tone <= len(LIST_5TONE_DIGITS):
2672
                line = RadioSetting(
2673
                    "_5tone_std_settings_" + str(i) + "_repttone",
2674
                    "Repeat Tone",
2675
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2676
                                          LIST_5TONE_DIGITS[
2677
                                              repeat_tone]))
2678
                line.set_apply_callback(apply_list_value,
2679
                                        standard.repeat_tone)
2680
                std_5tone.append(line)
2681
            else:
2682
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2683
            i = i + 1
2684

    
2685
        def my_apply_5tonestdlist_value(setting, obj):
2686
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2687
                obj.set_value(0xFF)
2688
            else:
2689
                obj.set_value(LIST_5TONE_STANDARDS.
2690
                              index(str(setting.value)))
2691

    
2692
        def apply_5tone_frame(setting, obj):
2693
            LOG.debug("Setting 5 Tone: " + str(setting.value))
2694
            valstring = str(setting.value)
2695
            if len(valstring) == 0:
2696
                for i in range(0, 5):
2697
                    obj[i] = 255
2698
            else:
2699
                validFrame = True
2700
                for i in range(0, 5):
2701
                    currentChar = valstring[i].upper()
2702
                    if currentChar in LIST_5TONE_DIGITS:
2703
                        obj[i] = LIST_5TONE_DIGITS.index(currentChar)
2704
                    else:
2705
                        validFrame = False
2706
                        LOG.debug("invalid char: " + str(currentChar))
2707
                if not validFrame:
2708
                    LOG.debug("setting whole frame to FF")
2709
                    for i in range(0, 5):
2710
                        obj[i] = 255
2711

    
2712
        def validate_5tone_frame(value):
2713
            if (len(str(value)) != 5) and (len(str(value)) != 0):
2714
                msg = ("5 Tone must have 5 digits or 0 digits")
2715
                raise InvalidValueError(msg)
2716
            for digit in str(value):
2717
                if digit.upper() not in LIST_5TONE_DIGITS:
2718
                    msg = (str(digit) + " is not a valid digit for 5tones")
2719
                    raise InvalidValueError(msg)
2720
            return value
2721

    
2722
        def frame2string(frame):
2723
            frameString = ""
2724
            for digit in frame:
2725
                if digit != 255:
2726
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2727
            return frameString
2728

    
2729
        _5tone_codes = self._memobj._5tone_codes
2730
        i = 1
2731
        for code in _5tone_codes:
2732
            code_5tone = RadioSettingGroup("code_5tone_" + str(i),
2733
                                           "5 Tone code " + str(i))
2734
            codes_5tone.append(code_5tone)
2735
            if (code.standard == 255):
2736
                currentVal = 15
2737
            else:
2738
                currentVal = code.standard
2739
            line = RadioSetting("_5tone_code_" + str(i) + "_std",
2740
                                " Standard",
2741
                                RadioSettingValueList(LIST_5TONE_STANDARDS,
2742
                                                      LIST_5TONE_STANDARDS[
2743
                                                          currentVal]))
2744
            line.set_apply_callback(my_apply_5tonestdlist_value,
2745
                                    code.standard)
2746
            code_5tone.append(line)
2747

    
2748
            val = RadioSettingValueString(0, 6,
2749
                                          frame2string(code.frame1), False)
2750
            line = RadioSetting("_5tone_code_" + str(i) + "_frame1",
2751
                                " Frame 1", val)
2752
            val.set_validate_callback(validate_5tone_frame)
2753
            line.set_apply_callback(apply_5tone_frame, code.frame1)
2754
            code_5tone.append(line)
2755

    
2756
            val = RadioSettingValueString(0, 6,
2757
                                          frame2string(code.frame2), False)
2758
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2759
                                " Frame 2", val)
2760
            val.set_validate_callback(validate_5tone_frame)
2761
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2762
            code_5tone.append(line)
2763

    
2764
            val = RadioSettingValueString(0, 6,
2765
                                          frame2string(code.frame3), False)
2766
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2767
                                " Frame 3", val)
2768
            val.set_validate_callback(validate_5tone_frame)
2769
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2770
            code_5tone.append(line)
2771
            i = i + 1
2772

    
2773
        _5_tone_decode1 = RadioSetting(
2774
            "_5tone_settings._5tone_decode_call_frame1",
2775
            "5 Tone decode call Frame 1",
2776
            RadioSettingValueBoolean(
2777
                _mem._5tone_settings._5tone_decode_call_frame1))
2778
        group_5tone.append(_5_tone_decode1)
2779

    
2780
        _5_tone_decode2 = RadioSetting(
2781
            "_5tone_settings._5tone_decode_call_frame2",
2782
            "5 Tone decode call Frame 2",
2783
            RadioSettingValueBoolean(
2784
                _mem._5tone_settings._5tone_decode_call_frame2))
2785
        group_5tone.append(_5_tone_decode2)
2786

    
2787
        _5_tone_decode3 = RadioSetting(
2788
            "_5tone_settings._5tone_decode_call_frame3",
2789
            "5 Tone decode call Frame 3",
2790
            RadioSettingValueBoolean(
2791
                _mem._5tone_settings._5tone_decode_call_frame3))
2792
        group_5tone.append(_5_tone_decode3)
2793

    
2794
        _5_tone_decode_disp1 = RadioSetting(
2795
            "_5tone_settings._5tone_decode_disp_frame1",
2796
            "5 Tone decode disp Frame 1",
2797
            RadioSettingValueBoolean(
2798
                _mem._5tone_settings._5tone_decode_disp_frame1))
2799
        group_5tone.append(_5_tone_decode_disp1)
2800

    
2801
        _5_tone_decode_disp2 = RadioSetting(
2802
            "_5tone_settings._5tone_decode_disp_frame2",
2803
            "5 Tone decode disp Frame 2",
2804
            RadioSettingValueBoolean(
2805
                _mem._5tone_settings._5tone_decode_disp_frame2))
2806
        group_5tone.append(_5_tone_decode_disp2)
2807

    
2808
        _5_tone_decode_disp3 = RadioSetting(
2809
            "_5tone_settings._5tone_decode_disp_frame3",
2810
            "5 Tone decode disp Frame 3",
2811
            RadioSettingValueBoolean(
2812
                _mem._5tone_settings._5tone_decode_disp_frame3))
2813
        group_5tone.append(_5_tone_decode_disp3)
2814

    
2815
        decode_standard = _mem._5tone_settings.decode_standard
2816
        if decode_standard == 255:
2817
            decode_standard = 0
2818
        if decode_standard <= len(LIST_5TONE_STANDARDS_without_none):
2819
            line = RadioSetting("_5tone_settings.decode_standard",
2820
                                "5 Tone-decode Standard",
2821
                                RadioSettingValueList(
2822
                                    LIST_5TONE_STANDARDS_without_none,
2823
                                    LIST_5TONE_STANDARDS_without_none[
2824
                                        decode_standard]))
2825
            group_5tone.append(line)
2826
        else:
2827
            LOG.debug("Invalid decode std...")
2828

    
2829
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2830
        if _5tone_delay1 == 255:
2831
            _5tone_delay1 = 20
2832

    
2833
        if _5tone_delay1 <= len(LIST_5TONE_DELAY):
2834
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2835
                                         LIST_5TONE_DELAY[
2836
                                             _5tone_delay1])
2837
            line = RadioSetting("_5tone_settings._5tone_delay1",
2838
                                "5 Tone Delay Frame 1", list)
2839
            group_5tone.append(line)
2840
        else:
2841
            LOG.debug(
2842
                "Invalid value for 5tone delay (frame1) ! Disabling.")
2843

    
2844
        _5tone_delay2 = _mem._5tone_settings._5tone_delay2
2845
        if _5tone_delay2 == 255:
2846
            _5tone_delay2 = 20
2847
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2848

    
2849
        if _5tone_delay2 <= len(LIST_5TONE_DELAY):
2850
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2851
                                         LIST_5TONE_DELAY[
2852
                                             _5tone_delay2])
2853
            line = RadioSetting("_5tone_settings._5tone_delay2",
2854
                                "5 Tone Delay Frame 2", list)
2855
            group_5tone.append(line)
2856
        else:
2857
            LOG.debug("Invalid value for 5tone delay (frame2)! Disabling.")
2858

    
2859
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2860
        if _5tone_delay3 == 255:
2861
            _5tone_delay3 = 20
2862
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2863

    
2864
        if _5tone_delay3 <= len(LIST_5TONE_DELAY):
2865
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2866
                                         LIST_5TONE_DELAY[
2867
                                             _5tone_delay3])
2868
            line = RadioSetting("_5tone_settings._5tone_delay3",
2869
                                "5 Tone Delay Frame 3", list)
2870
            group_5tone.append(line)
2871
        else:
2872
            LOG.debug("Invalid value for 5tone delay (frame3)! Disabling.")
2873

    
2874
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2875
        if ext_length == 255:
2876
            ext_length = 0
2877
            LOG.debug("1st Tone ext lenght unconfigured! Resetting to 0")
2878

    
2879
        if ext_length <= len(LIST_5TONE_DELAY):
2880
            list = RadioSettingValueList(
2881
                LIST_5TONE_DELAY,
2882
                LIST_5TONE_DELAY[
2883
                    ext_length])
2884
            line = RadioSetting(
2885
                "_5tone_settings._5tone_first_digit_ext_length",
2886
                "First digit extend length", list)
2887
            group_5tone.append(line)
2888
        else:
2889
            LOG.debug("Invalid value for 5tone ext length! Disabling.")
2890

    
2891
        decode_reset_time = _mem._5tone_settings.decode_reset_time
2892
        if decode_reset_time == 255:
2893
            decode_reset_time = 59
2894
            LOG.debug("Decode reset time unconfigured. resetting.")
2895
        if decode_reset_time <= len(LIST_5TONE_RESET):
2896
            list = RadioSettingValueList(
2897
                LIST_5TONE_RESET,
2898
                LIST_5TONE_RESET[
2899
                    decode_reset_time])
2900
            line = RadioSetting("_5tone_settings.decode_reset_time",
2901
                                "Decode reset time", list)
2902
            group_5tone.append(line)
2903
        else:
2904
            LOG.debug("Invalid value decode reset time! Disabling.")
2905

    
2906
        # 2 Tone
2907
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2908
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2909

    
2910
        top.append(encode_2tone)
2911
        top.append(decode_2tone)
2912

    
2913
        duration_1st_tone = self._memobj._2tone.duration_1st_tone
2914
        if duration_1st_tone == 255:
2915
            LOG.debug("Duration of first 2 Tone digit is not yet " +
2916
                      "configured. Setting to 600ms")
2917
            duration_1st_tone = 60
2918

    
2919
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2920
            val = RadioSettingValueList(LIST_5TONE_DELAY,
2921
                                        LIST_5TONE_DELAY[
2922
                                            duration_1st_tone])
2923
            line = RadioSetting("_2tone.duration_1st_tone",
2924
                                "Duration 1st Tone", val)
2925
            encode_2tone.append(line)
2926

    
2927
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2928
        if duration_2nd_tone == 255:
2929
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2930
                      "configured. Setting to 600ms")
2931
            duration_2nd_tone = 60
2932

    
2933
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2934
            val = RadioSettingValueList(LIST_5TONE_DELAY,
2935
                                        LIST_5TONE_DELAY[
2936
                                            duration_2nd_tone])
2937
            line = RadioSetting("_2tone.duration_2nd_tone",
2938
                                "Duration 2nd Tone", val)
2939
            encode_2tone.append(line)
2940

    
2941
        duration_gap = self._memobj._2tone.duration_gap
2942
        if duration_gap == 255:
2943
            LOG.debug("Duration of gap is not yet " +
2944
                      "configured. Setting to 300ms")
2945
            duration_gap = 30
2946

    
2947
        if duration_gap <= len(LIST_5TONE_DELAY):
2948
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2949
                                RadioSettingValueList(LIST_5TONE_DELAY,
2950
                                                      LIST_5TONE_DELAY[
2951
                                                          duration_gap]))
2952
            encode_2tone.append(line)
2953

    
2954
        def _2tone_validate(value):
2955
            if value == 0:
2956
                return 65535
2957
            if value == 65535:
2958
                return value
2959
            if not (300 <= value and value <= 3000):
2960
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
2961
                raise InvalidValueError(msg)
2962
            return value
2963

    
2964
        def apply_2tone_freq(setting, obj):
2965
            val = int(setting.value)
2966
            if (val == 0) or (val == 65535):
2967
                obj.set_value(65535)
2968
            else:
2969
                obj.set_value(val)
2970

    
2971
        i = 1
2972
        for code in self._memobj._2tone._2tone_encode:
2973
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
2974
                                           "Encode Code " + str(i))
2975
            encode_2tone.append(code_2tone)
2976

    
2977
            tmp = code.freq1
2978
            if tmp == 65535:
2979
                tmp = 0
2980
            val1 = RadioSettingValueInteger(0, 65535, tmp)
2981
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
2982
                                 "Frequency 1", val1)
2983
            val1.set_validate_callback(_2tone_validate)
2984
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
2985
            code_2tone.append(freq1)
2986

    
2987
            tmp = code.freq2
2988
            if tmp == 65535:
2989
                tmp = 0
2990
            val2 = RadioSettingValueInteger(0, 65535, tmp)
2991
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
2992
                                 "Frequency 2", val2)
2993
            val2.set_validate_callback(_2tone_validate)
2994
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
2995
            code_2tone.append(freq2)
2996

    
2997
            i = i + 1
2998

    
2999
        decode_reset_time = _mem._2tone.reset_time
3000
        if decode_reset_time == 255:
3001
            decode_reset_time = 59
3002
            LOG.debug("Decode reset time unconfigured. resetting.")
3003
        if decode_reset_time <= len(LIST_5TONE_RESET):
3004
            list = RadioSettingValueList(
3005
                LIST_5TONE_RESET,
3006
                LIST_5TONE_RESET[
3007
                    decode_reset_time])
3008
            line = RadioSetting("_2tone.reset_time",
3009
                                "Decode reset time", list)
3010
            decode_2tone.append(line)
3011
        else:
3012
            LOG.debug("Invalid value decode reset time! Disabling.")
3013

    
3014
        def apply_2tone_freq_pair(setting, obj):
3015
            val = int(setting.value)
3016
            derived_val = 65535
3017
            frqname = str(setting._name[-5:])
3018
            derivedname = "derived_from_" + frqname
3019

    
3020
            if (val == 0):
3021
                val = 65535
3022
                derived_val = 65535
3023
            else:
3024
                derived_val = int(round(2304000.0/val))
3025

    
3026
            obj[frqname].set_value(val)
3027
            obj[derivedname].set_value(derived_val)
3028

    
3029
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
3030
                      derivedname + ": " + str(derived_val))
3031

    
3032
        i = 1
3033
        for decode_code in self._memobj._2tone._2tone_decode:
3034
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
3035
                                                "Decode Code " + str(i))
3036
            decode_2tone.append(_2tone_dec_code)
3037

    
3038
            j = 1
3039
            for dec in decode_code.decs:
3040
                val = dec.dec
3041
                if val == 255:
3042
                    LOG.debug("Dec for Code " + str(i) + " Dec " + str(j) +
3043
                              " is not yet configured. Setting to 0.")
3044
                    val = 0
3045

    
3046
                if val <= len(LIST_2TONE_DEC):
3047
                    line = RadioSetting(
3048
                        "_2tone_dec_settings_" + str(i) + "_dec_" + str(j),
3049
                        "Dec " + str(j), RadioSettingValueList
3050
                        (LIST_2TONE_DEC,
3051
                         LIST_2TONE_DEC[val]))
3052
                    line.set_apply_callback(apply_list_value, dec.dec)
3053
                    _2tone_dec_code.append(line)
3054
                else:
3055
                    LOG.debug("Invalid value for 2tone dec! Disabling.")
3056

    
3057
                val = dec.response
3058
                if val == 255:
3059
                    LOG.debug("Response for Code " +
3060
                              str(i) + " Dec " + str(j) +
3061
                              " is not yet configured. Setting to 0.")
3062
                    val = 0
3063

    
3064
                if val <= len(LIST_2TONE_RESPONSE):
3065
                    line = RadioSetting(
3066
                        "_2tone_dec_settings_" +
3067
                        str(i) + "_resp_" + str(j),
3068
                        "Response " + str(j), RadioSettingValueList
3069
                        (LIST_2TONE_RESPONSE,
3070
                         LIST_2TONE_RESPONSE[val]))
3071
                    line.set_apply_callback(apply_list_value, dec.response)
3072
                    _2tone_dec_code.append(line)
3073
                else:
3074
                    LOG.debug(
3075
                        "Invalid value for 2tone response! Disabling.")
3076

    
3077
                val = dec.alert
3078
                if val == 255:
3079
                    LOG.debug("Alert for Code " +
3080
                              str(i) + " Dec " + str(j) +
3081
                              " is not yet configured. Setting to 0.")
3082
                    val = 0
3083

    
3084
                if val <= len(PTTIDCODE_LIST):
3085
                    line = RadioSetting(
3086
                        "_2tone_dec_settings_" +
3087
                        str(i) + "_alert_" + str(j),
3088
                        "Alert " + str(j), RadioSettingValueList
3089
                        (PTTIDCODE_LIST,
3090
                         PTTIDCODE_LIST[val]))
3091
                    line.set_apply_callback(apply_list_value, dec.alert)
3092
                    _2tone_dec_code.append(line)
3093
                else:
3094
                    LOG.debug("Invalid value for 2tone alert! Disabling.")
3095
                j = j + 1
3096

    
3097
            freq = self._memobj._2tone.freqs[i-1]
3098
            for char in ['A', 'B', 'C', 'D']:
3099
                setting_name = "freq" + str(char)
3100

    
3101
                tmp = freq[setting_name]
3102
                if tmp == 65535:
3103
                    tmp = 0
3104
                if tmp != 0:
3105
                    expected = int(round(2304000.0/tmp))
3106
                    from_mem = freq["derived_from_" + setting_name]
3107
                    if expected != from_mem:
3108
                        LOG.error("Expected " + str(expected) +
3109
                                  " but read " + str(from_mem) +
3110
                                  ". Disabling 2Tone Decode Freqs!")
3111
                        break
3112
                val = RadioSettingValueInteger(0, 65535, tmp)
3113
                frq = RadioSetting("2tone_dec_" + str(i) +
3114
                                   "_freq" + str(char),
3115
                                   ("Decode Frequency " + str(char)), val)
3116
                val.set_validate_callback(_2tone_validate)
3117
                frq.set_apply_callback(apply_2tone_freq_pair, freq)
3118
                _2tone_dec_code.append(frq)
3119

    
3120
            i = i + 1
3121

    
3122
        return top
3123

    
3124
    def set_settings(self, settings):
3125
        _settings = self._memobj.settings
3126
        for element in settings:
3127
            if not isinstance(element, RadioSetting):
3128
                if element.get_name() == "fm_preset":
3129
                    self._set_fm_preset(element)
3130
                else:
3131
                    self.set_settings(element)
3132
                    continue
3133
            else:
3134
                try:
3135
                    name = element.get_name()
3136
                    if "." in name:
3137
                        bits = name.split(".")
3138
                        obj = self._memobj
3139
                        for bit in bits[:-1]:
3140
                            if "/" in bit:
3141
                                bit, index = bit.split("/", 1)
3142
                                index = int(index)
3143
                                obj = getattr(obj, bit)[index]
3144
                            else:
3145
                                obj = getattr(obj, bit)
3146
                        setting = bits[-1]
3147
                    else:
3148
                        obj = _settings
3149
                        setting = element.get_name()
3150

    
3151
                    if element.has_apply_callback():
3152
                        LOG.debug("Using apply callback")
3153
                        element.run_apply_callback()
3154
                    elif setting == "volume" and self.MODEL == "KT-WP12":
3155
                        setattr(obj, setting, int(element.value) - 1)
3156
                    elif setting == "volume" and self.MODEL == "WP-9900":
3157
                        setattr(obj, setting, int(element.value) - 1)
3158
                    elif element.value.get_mutable():
3159
                        LOG.debug("Setting %s = %s" % (setting, element.value))
3160
                        setattr(obj, setting, element.value)
3161
                except Exception, e:
3162
                    LOG.debug(element.get_name())
3163
                    raise
3164

    
3165
    @classmethod
3166
    def match_model(cls, filedata, filename):
3167
        match_size = False
3168
        match_model = False
3169

    
3170
        # testing the file data size
3171
        if len(filedata) == MEM_SIZE:
3172
            match_size = True
3173

    
3174
        # testing the firmware model fingerprint
3175
        match_model = model_match(cls, filedata)
3176

    
3177
        if match_size and match_model:
3178
            return True
3179
        else:
3180
            return False
3181

    
3182

    
3183
MEM_FORMAT = """
3184
#seekto 0x0000;
3185
struct {
3186
  lbcd rxfreq[4];
3187
  lbcd txfreq[4];
3188
  ul16 rxtone;
3189
  ul16 txtone;
3190
  u8 unknown0:4,
3191
     scode:4;
3192
  u8 unknown1:2,
3193
     spmute:2,
3194
     unknown2:2,
3195
     optsig:2;
3196
  u8 unknown3:3,
3197
     scramble:1,
3198
     unknown4:3,
3199
     power:1;
3200
  u8 unknown5:1,
3201
     wide:1,
3202
     unknown6:2,
3203
     bcl:1,
3204
     add:1,
3205
     pttid:2;
3206
} memory[200];
3207

    
3208
#seekto 0x0E00;
3209
struct {
3210
  u8 tdr;
3211
  u8 unknown1;
3212
  u8 sql;
3213
  u8 unknown2[2];
3214
  u8 tot;
3215
  u8 apo;           // BTech radios use this as the Auto Power Off time
3216
                    // other radios use this as pre-Time Out Alert
3217
  u8 unknown3;
3218
  u8 abr;
3219
  u8 beep;
3220
  u8 unknown4[4];
3221
  u8 dtmfst;
3222
  u8 unknown5[2];
3223
  u8 prisc;
3224
  u8 prich;
3225
  u8 screv;
3226
  u8 unknown6[2];
3227
  u8 pttid;
3228
  u8 pttlt;
3229
  u8 unknown7;
3230
  u8 emctp;
3231
  u8 emcch;
3232
  u8 ringt;
3233
  u8 unknown8;
3234
  u8 camdf;
3235
  u8 cbmdf;
3236
  u8 sync;          // BTech radios use this as the display sync setting
3237
                    // other radios use this as the auto keypad lock setting
3238
  u8 ponmsg;
3239
  u8 wtled;
3240
  u8 rxled;
3241
  u8 txled;
3242
  u8 unknown9[5];
3243
  u8 anil;
3244
  u8 reps;
3245
  u8 repm;
3246
  u8 tdrab;
3247
  u8 ste;
3248
  u8 rpste;
3249
  u8 rptdl;
3250
  u8 mgain;
3251
  u8 dtmfg;
3252
} settings;
3253

    
3254
#seekto 0x0E80;
3255
struct {
3256
  u8 unknown1;
3257
  u8 vfomr;
3258
  u8 keylock;
3259
  u8 unknown2;
3260
  u8 unknown3:4,
3261
     vfomren:1,
3262
     unknown4:1,
3263
     reseten:1,
3264
     menuen:1;
3265
  u8 unknown5[11];
3266
  u8 dispab;
3267
  u8 mrcha;
3268
  u8 mrchb;
3269
  u8 menu;
3270
} settings2;
3271

    
3272
#seekto 0x0EC0;
3273
struct {
3274
  char line1[6];
3275
  char line2[6];
3276
} poweron_msg;
3277

    
3278
struct settings_vfo {
3279
  u8 freq[8];
3280
  u8 offset[6];
3281
  u8 unknown2[2];
3282
  ul16 rxtone;
3283
  ul16 txtone;
3284
  u8 scode;
3285
  u8 spmute;
3286
  u8 optsig;
3287
  u8 scramble;
3288
  u8 wide;
3289
  u8 power;
3290
  u8 shiftd;
3291
  u8 step;
3292
  u8 unknown3[4];
3293
};
3294

    
3295
#seekto 0x0F00;
3296
struct {
3297
  struct settings_vfo a;
3298
  struct settings_vfo b;
3299
} vfo;
3300

    
3301
#seekto 0x1000;
3302
struct {
3303
  char name[6];
3304
  u8 unknown1[10];
3305
} names[200];
3306

    
3307
#seekto 0x2400;
3308
struct {
3309
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3310
  u8 group_tone;
3311
  u8 repeat_tone;
3312
  u8 unused[13];
3313
} _5tone_std_settings[15];
3314

    
3315
#seekto 0x2500;
3316
struct {
3317
  u8 frame1[5];
3318
  u8 frame2[5];
3319
  u8 frame3[5];
3320
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3321
} _5tone_codes[15];
3322

    
3323
#seekto 0x25F0;
3324
struct {
3325
  u8 _5tone_delay1; // * 10ms
3326
  u8 _5tone_delay2; // * 10ms
3327
  u8 _5tone_delay3; // * 10ms
3328
  u8 _5tone_first_digit_ext_length;
3329
  u8 unknown1;
3330
  u8 unknown2;
3331
  u8 unknown3;
3332
  u8 unknown4;
3333
  u8 decode_standard;
3334
  u8 unknown5:5,
3335
     _5tone_decode_call_frame3:1,
3336
     _5tone_decode_call_frame2:1,
3337
     _5tone_decode_call_frame1:1;
3338
  u8 unknown6:5,
3339
     _5tone_decode_disp_frame3:1,
3340
     _5tone_decode_disp_frame2:1,
3341
     _5tone_decode_disp_frame1:1;
3342
  u8 decode_reset_time; // * 100 + 100ms
3343
} _5tone_settings;
3344

    
3345
#seekto 0x2900;
3346
struct {
3347
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3348
} dtmf_codes[15];
3349

    
3350
#seekto 0x29F0;
3351
struct {
3352
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3353
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3354
  u8 unknown0[14];
3355
  u8 inspection[16];
3356
  u8 monitor[16];
3357
  u8 alarmcode[16];
3358
  u8 stun[16];
3359
  u8 kill[16];
3360
  u8 revive[16];
3361
  u8 unknown1[16];
3362
  u8 unknown2[16];
3363
  u8 unknown3[16];
3364
  u8 unknown4[16];
3365
  u8 unknown5[16];
3366
  u8 unknown6[16];
3367
  u8 unknown7[16];
3368
  u8 masterid[16];
3369
  u8 viceid[16];
3370
  u8 unused01:7,
3371
     mastervice:1;
3372
  u8 unused02:3,
3373
     mrevive:1,
3374
     mkill:1,
3375
     mstun:1,
3376
     mmonitor:1,
3377
     minspection:1;
3378
  u8 unused03:3,
3379
     vrevive:1,
3380
     vkill:1,
3381
     vstun:1,
3382
     vmonitor:1,
3383
     vinspection:1;
3384
  u8 unused04:6,
3385
     txdisable:1,
3386
     rxdisable:1;
3387
  u8 groupcode;
3388
  u8 spacecode;
3389
  u8 delayproctime; // * 100 + 100ms
3390
  u8 resettime;     // * 100 + 100ms
3391
} dtmf_settings;
3392

    
3393
#seekto 0x2D00;
3394
struct {
3395
  struct {
3396
    ul16 freq1;
3397
    u8 unused01[6];
3398
    ul16 freq2;
3399
    u8 unused02[6];
3400
  } _2tone_encode[15];
3401
  u8 duration_1st_tone; // *10ms
3402
  u8 duration_2nd_tone; // *10ms
3403
  u8 duration_gap;      // *10ms
3404
  u8 unused03[13];
3405
  struct {
3406
    struct {
3407
      u8 dec;      // one out of LIST_2TONE_DEC
3408
      u8 response; // one out of LIST_2TONE_RESPONSE
3409
      u8 alert;    // 1-16
3410
    } decs[4];
3411
    u8 unused04[4];
3412
  } _2tone_decode[15];
3413
  u8 unused05[16];
3414

    
3415
  struct {
3416
    ul16 freqA;
3417
    ul16 freqB;
3418
    ul16 freqC;
3419
    ul16 freqD;
3420
    // unknown what those values mean, but they are
3421
    // derived from configured frequencies
3422
    ul16 derived_from_freqA; // 2304000/freqA
3423
    ul16 derived_from_freqB; // 2304000/freqB
3424
    ul16 derived_from_freqC; // 2304000/freqC
3425
    ul16 derived_from_freqD; // 2304000/freqD
3426
  }freqs[15];
3427
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3428
} _2tone;
3429

    
3430
#seekto 0x3000;
3431
struct {
3432
  u8 freq[8];
3433
  char broadcast_station_name[6];
3434
  u8 unknown[2];
3435
} fm_radio_preset[16];
3436

    
3437
#seekto 0x3C90;
3438
struct {
3439
  u8 vhf_low[3];
3440
  u8 vhf_high[3];
3441
  u8 uhf_low[3];
3442
  u8 uhf_high[3];
3443
} ranges;
3444

    
3445
// the UV-2501+220 & KT8900R has different zones for storing ranges
3446

    
3447
#seekto 0x3CD0;
3448
struct {
3449
  u8 vhf_low[3];
3450
  u8 vhf_high[3];
3451
  u8 unknown1[4];
3452
  u8 unknown2[6];
3453
  u8 vhf2_low[3];
3454
  u8 vhf2_high[3];
3455
  u8 unknown3[4];
3456
  u8 unknown4[6];
3457
  u8 uhf_low[3];
3458
  u8 uhf_high[3];
3459
} ranges220;
3460

    
3461
#seekto 0x3F70;
3462
struct {
3463
  char fp[6];
3464
} fingerprint;
3465

    
3466
"""
3467

    
3468

    
3469
class BTech(BTechMobileCommon):
3470
    """BTECH's UV-5001 and alike radios"""
3471
    BANDS = 2
3472
    COLOR_LCD = False
3473
    NAME_LENGTH = 6
3474

    
3475
    def set_options(self):
3476
        """This is to read the options from the image and set it in the
3477
        environment, for now just the limits of the freqs in the VHF/UHF
3478
        ranges"""
3479

    
3480
        # setting the correct ranges for each radio type
3481
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3482
            # the model 2501+220 has a segment in 220
3483
            # and a different position in the memmap
3484
            # also the QYT KT8900R
3485
            ranges = self._memobj.ranges220
3486
        else:
3487
            ranges = self._memobj.ranges
3488

    
3489
        # the normal dual bands
3490
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3491
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3492

    
3493
        # DEBUG
3494
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3495
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3496

    
3497
        # 220Mhz radios case
3498
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3499
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3500
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3501
            self._220_range = vhf2
3502

    
3503
        # set the class with the real data
3504
        self._vhf_range = vhf
3505
        self._uhf_range = uhf
3506

    
3507
    def process_mmap(self):
3508
        """Process the mem map into the mem object"""
3509

    
3510
        # Get it
3511
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3512

    
3513
        # load specific parameters from the radio image
3514
        self.set_options()
3515

    
3516

    
3517
# Declaring Aliases (Clones of the real radios)
3518
class JT2705M(chirp_common.Alias):
3519
    VENDOR = "Jetstream"
3520
    MODEL = "JT2705M"
3521

    
3522

    
3523
class JT6188Mini(chirp_common.Alias):
3524
    VENDOR = "Juentai"
3525
    MODEL = "JT-6188 Mini"
3526

    
3527

    
3528
class JT6188Plus(chirp_common.Alias):
3529
    VENDOR = "Juentai"
3530
    MODEL = "JT-6188 Plus"
3531

    
3532

    
3533
class SSGT890(chirp_common.Alias):
3534
    VENDOR = "Sainsonic"
3535
    MODEL = "GT-890"
3536

    
3537

    
3538
class ZastoneMP300(chirp_common.Alias):
3539
    VENDOR = "Zastone"
3540
    MODEL = "MP-300"
3541

    
3542

    
3543
# real radios
3544
@directory.register
3545
class UV2501(BTech):
3546
    """Baofeng Tech UV2501"""
3547
    MODEL = "UV-2501"
3548
    _fileid = [UV2501G3_fp,
3549
               UV2501G2_fp,
3550
               UV2501pp2_fp,
3551
               UV2501pp_fp]
3552

    
3553

    
3554
@directory.register
3555
class UV2501_220(BTech):
3556
    """Baofeng Tech UV2501+220"""
3557
    MODEL = "UV-2501+220"
3558
    BANDS = 3
3559
    _magic = MSTRING_220
3560
    _fileid = [UV2501_220G3_fp,
3561
               UV2501_220G2_fp,
3562
               UV2501_220_fp,
3563
               UV2501_220pp_fp]
3564

    
3565

    
3566
@directory.register
3567
class UV5001(BTech):
3568
    """Baofeng Tech UV5001"""
3569
    MODEL = "UV-5001"
3570
    _fileid = [UV5001G3_fp,
3571
               UV5001G22_fp,
3572
               UV5001G2_fp,
3573
               UV5001alpha_fp,
3574
               UV5001pp_fp]
3575
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3576
                     chirp_common.PowerLevel("Low", watts=10)]
3577

    
3578

    
3579
@directory.register
3580
class MINI8900(BTech):
3581
    """WACCOM MINI-8900"""
3582
    VENDOR = "WACCOM"
3583
    MODEL = "MINI-8900"
3584
    _magic = MSTRING_MINI8900
3585
    _fileid = [MINI8900_fp, ]
3586
    # Clones
3587
    ALIASES = [JT6188Plus, ]
3588

    
3589

    
3590
@directory.register
3591
class KTUV980(BTech):
3592
    """QYT KT-UV980"""
3593
    VENDOR = "QYT"
3594
    MODEL = "KT-UV980"
3595
    _vhf_range = (136000000, 175000000)
3596
    _uhf_range = (400000000, 481000000)
3597
    _magic = MSTRING_MINI8900
3598
    _fileid = [KTUV980_fp, ]
3599
    # Clones
3600
    ALIASES = [JT2705M, ]
3601

    
3602
# Please note that there is a version of this radios that is a clone of the
3603
# Waccom Mini8900, maybe an early version?
3604

    
3605

    
3606
class OTGRadioV1(chirp_common.Alias):
3607
    VENDOR = 'OTGSTUFF'
3608
    MODEL = 'OTG Radio v1'
3609

    
3610

    
3611
@directory.register
3612
class KT9800(BTech):
3613
    """QYT KT8900"""
3614
    VENDOR = "QYT"
3615
    MODEL = "KT8900"
3616
    _vhf_range = (136000000, 175000000)
3617
    _uhf_range = (400000000, 481000000)
3618
    _magic = MSTRING_KT8900
3619
    _fileid = [KT8900_fp,
3620
               KT8900_fp1,
3621
               KT8900_fp2,
3622
               KT8900_fp3,
3623
               KT8900_fp4,
3624
               KT8900_fp5,
3625
               KT8900_fp6,
3626
               KT8900_fp7]
3627
    # Clones
3628
    ALIASES = [JT6188Mini, SSGT890, ZastoneMP300]
3629

    
3630

    
3631
@directory.register
3632
class KT9800R(BTech):
3633
    """QYT KT8900R"""
3634
    VENDOR = "QYT"
3635
    MODEL = "KT8900R"
3636
    BANDS = 3
3637
    _vhf_range = (136000000, 175000000)
3638
    _220_range = (240000000, 271000000)
3639
    _uhf_range = (400000000, 481000000)
3640
    _magic = MSTRING_KT8900R
3641
    _fileid = [KT8900R_fp,
3642
               KT8900R_fp1,
3643
               KT8900R_fp2,
3644
               KT8900R_fp3,
3645
               KT8900R_fp4,
3646
               KT8900R_fp5]
3647

    
3648

    
3649
@directory.register
3650
class LT588UV(BTech):
3651
    """LUITON LT-588UV"""
3652
    VENDOR = "LUITON"
3653
    MODEL = "LT-588UV"
3654
    _vhf_range = (136000000, 175000000)
3655
    _uhf_range = (400000000, 481000000)
3656
    _magic = MSTRING_KT8900
3657
    _fileid = [LT588UV_fp,
3658
               LT588UV_fp1]
3659
    _power_levels = [chirp_common.PowerLevel("High", watts=60),
3660
                     chirp_common.PowerLevel("Low", watts=10)]
3661

    
3662

    
3663
COLOR_MEM_FORMAT = """
3664
#seekto 0x0000;
3665
struct {
3666
  lbcd rxfreq[4];
3667
  lbcd txfreq[4];
3668
  ul16 rxtone;
3669
  ul16 txtone;
3670
  u8 unknown0:4,
3671
     scode:4;
3672
  u8 unknown1:2,
3673
     spmute:2,
3674
     unknown2:2,
3675
     optsig:2;
3676
  u8 unknown3:3,
3677
     scramble:1,
3678
     unknown4:2,
3679
     power:2;
3680
  u8 unknown5:1,
3681
     wide:1,
3682
     unknown6:2,
3683
     bcl:1,
3684
     add:1,
3685
     pttid:2;
3686
} memory[200];
3687

    
3688
#seekto 0x0E00;
3689
struct {
3690
  u8 tmr;
3691
  u8 unknown1;
3692
  u8 sql;
3693
  u8 unknown2;
3694
  u8 mgain2;
3695
  u8 tot;
3696
  u8 apo;
3697
  u8 unknown3;
3698
  u8 abr;
3699
  u8 beep;
3700
  u8 unknown4[4];
3701
  u8 dtmfst;
3702
  u8 unknown5[2];
3703
  u8 screv;
3704
  u8 unknown6[2];
3705
  u8 pttid;
3706
  u8 pttlt;
3707
  u8 unknown7;
3708
  u8 emctp;
3709
  u8 emcch;
3710
  u8 sigbp;
3711
  u8 unknown8;
3712
  u8 camdf;
3713
  u8 cbmdf;
3714
  u8 ccmdf;
3715
  u8 cdmdf;
3716
  u8 langua;
3717
  u8 sync;          // BTech radios use this as the display sync
3718
                    // setting, other radios use this as the auto
3719
                    // keypad lock setting
3720
  u8 mainfc;
3721
  u8 mainbc;
3722
  u8 menufc;
3723
  u8 menubc;
3724
  u8 stafc;
3725
  u8 stabc;
3726
  u8 sigfc;
3727
  u8 sigbc;
3728
  u8 rxfc;
3729
  u8 txfc;
3730
  u8 txdisp;
3731
  u8 unknown9[5];
3732
  u8 anil;
3733
  u8 reps;
3734
  u8 repm;
3735
  u8 tmrmr;
3736
  u8 ste;
3737
  u8 rpste;
3738
  u8 rptdl;
3739
  u8 dtmfg;
3740
  u8 mgain;         // used by db25-g for ponyey
3741
  u8 skiptx;
3742
  u8 scmode;
3743
} settings;
3744

    
3745
#seekto 0x0E80;
3746
struct {
3747
  u8 unknown1;
3748
  u8 vfomr;
3749
  u8 keylock;
3750
  u8 unknown2;
3751
  u8 unknown3:4,
3752
     vfomren:1,
3753
     unknown4:1,
3754
     reseten:1,
3755
     menuen:1;
3756
  u8 unknown5[11];
3757
  u8 dispab;
3758
  u8 unknown6[2];
3759
  u8 menu;
3760
  u8 unknown7[7];
3761
  u8 vfomra;
3762
  u8 vfomrb;
3763
  u8 vfomrc;
3764
  u8 vfomrd;
3765
  u8 mrcha;
3766
  u8 mrchb;
3767
  u8 mrchc;
3768
  u8 mrchd;
3769
} settings2;
3770

    
3771
struct settings_vfo {
3772
  u8 freq[8];
3773
  u8 offset[6];
3774
  u8 unknown2[2];
3775
  ul16 rxtone;
3776
  ul16 txtone;
3777
  u8 scode;
3778
  u8 spmute;
3779
  u8 optsig;
3780
  u8 scramble;
3781
  u8 wide;
3782
  u8 power;
3783
  u8 shiftd;
3784
  u8 step;
3785
  u8 unknown3[4];
3786
};
3787

    
3788
#seekto 0x0F00;
3789
struct {
3790
  struct settings_vfo a;
3791
  struct settings_vfo b;
3792
  struct settings_vfo c;
3793
  struct settings_vfo d;
3794
} vfo;
3795

    
3796
#seekto 0x0F80;
3797
struct {
3798
  char line1[8];
3799
  char line2[8];
3800
  char line3[8];
3801
  char line4[8];
3802
  char line5[8];
3803
  char line6[8];
3804
  char line7[8];
3805
  char line8[8];
3806
} poweron_msg;
3807

    
3808
#seekto 0x1000;
3809
struct {
3810
  char name[8];
3811
  u8 unknown1[8];
3812
} names[200];
3813

    
3814
#seekto 0x2400;
3815
struct {
3816
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3817
  u8 group_tone;
3818
  u8 repeat_tone;
3819
  u8 unused[13];
3820
} _5tone_std_settings[15];
3821

    
3822
#seekto 0x2500;
3823
struct {
3824
  u8 frame1[5];
3825
  u8 frame2[5];
3826
  u8 frame3[5];
3827
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3828
} _5tone_codes[15];
3829

    
3830
#seekto 0x25F0;
3831
struct {
3832
  u8 _5tone_delay1; // * 10ms
3833
  u8 _5tone_delay2; // * 10ms
3834
  u8 _5tone_delay3; // * 10ms
3835
  u8 _5tone_first_digit_ext_length;
3836
  u8 unknown1;
3837
  u8 unknown2;
3838
  u8 unknown3;
3839
  u8 unknown4;
3840
  u8 decode_standard;
3841
  u8 unknown5:5,
3842
     _5tone_decode_call_frame3:1,
3843
     _5tone_decode_call_frame2:1,
3844
     _5tone_decode_call_frame1:1;
3845
  u8 unknown6:5,
3846
     _5tone_decode_disp_frame3:1,
3847
     _5tone_decode_disp_frame2:1,
3848
     _5tone_decode_disp_frame1:1;
3849
  u8 decode_reset_time; // * 100 + 100ms
3850
} _5tone_settings;
3851

    
3852
#seekto 0x2900;
3853
struct {
3854
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3855
} dtmf_codes[15];
3856

    
3857
#seekto 0x29F0;
3858
struct {
3859
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3860
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3861
  u8 unknown0[14];
3862
  u8 inspection[16];
3863
  u8 monitor[16];
3864
  u8 alarmcode[16];
3865
  u8 stun[16];
3866
  u8 kill[16];
3867
  u8 revive[16];
3868
  u8 unknown1[16];
3869
  u8 unknown2[16];
3870
  u8 unknown3[16];
3871
  u8 unknown4[16];
3872
  u8 unknown5[16];
3873
  u8 unknown6[16];
3874
  u8 unknown7[16];
3875
  u8 masterid[16];
3876
  u8 viceid[16];
3877
  u8 unused01:7,
3878
     mastervice:1;
3879
  u8 unused02:3,
3880
     mrevive:1,
3881
     mkill:1,
3882
     mstun:1,
3883
     mmonitor:1,
3884
     minspection:1;
3885
  u8 unused03:3,
3886
     vrevive:1,
3887
     vkill:1,
3888
     vstun:1,
3889
     vmonitor:1,
3890
     vinspection:1;
3891
  u8 unused04:6,
3892
     txdisable:1,
3893
     rxdisable:1;
3894
  u8 groupcode;
3895
  u8 spacecode;
3896
  u8 delayproctime; // * 100 + 100ms
3897
  u8 resettime;     // * 100 + 100ms
3898
} dtmf_settings;
3899

    
3900
#seekto 0x2D00;
3901
struct {
3902
  struct {
3903
    ul16 freq1;
3904
    u8 unused01[6];
3905
    ul16 freq2;
3906
    u8 unused02[6];
3907
  } _2tone_encode[15];
3908
  u8 duration_1st_tone; // *10ms
3909
  u8 duration_2nd_tone; // *10ms
3910
  u8 duration_gap;      // *10ms
3911
  u8 unused03[13];
3912
  struct {
3913
    struct {
3914
      u8 dec;      // one out of LIST_2TONE_DEC
3915
      u8 response; // one out of LIST_2TONE_RESPONSE
3916
      u8 alert;    // 1-16
3917
    } decs[4];
3918
    u8 unused04[4];
3919
  } _2tone_decode[15];
3920
  u8 unused05[16];
3921

    
3922
  struct {
3923
    ul16 freqA;
3924
    ul16 freqB;
3925
    ul16 freqC;
3926
    ul16 freqD;
3927
    // unknown what those values mean, but they are
3928
    // derived from configured frequencies
3929
    ul16 derived_from_freqA; // 2304000/freqA
3930
    ul16 derived_from_freqB; // 2304000/freqB
3931
    ul16 derived_from_freqC; // 2304000/freqC
3932
    ul16 derived_from_freqD; // 2304000/freqD
3933
  }freqs[15];
3934
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3935
} _2tone;
3936

    
3937
#seekto 0x3D80;
3938
struct {
3939
  u8 vhf_low[3];
3940
  u8 vhf_high[3];
3941
  u8 unknown1[4];
3942
  u8 unknown2[6];
3943
  u8 vhf2_low[3];
3944
  u8 vhf2_high[3];
3945
  u8 unknown3[4];
3946
  u8 unknown4[6];
3947
  u8 uhf_low[3];
3948
  u8 uhf_high[3];
3949
  u8 unknown5[4];
3950
  u8 unknown6[6];
3951
  u8 uhf2_low[3];
3952
  u8 uhf2_high[3];
3953
} ranges;
3954

    
3955
#seekto 0x3F70;
3956
struct {
3957
  char fp[6];
3958
} fingerprint;
3959

    
3960
"""
3961

    
3962

    
3963
class BTechColor(BTechMobileCommon):
3964
    """BTECH's Color LCD Mobile and alike radios"""
3965
    COLOR_LCD = True
3966
    NAME_LENGTH = 8
3967
    LIST_TMR = LIST_TMR16
3968

    
3969
    def process_mmap(self):
3970
        """Process the mem map into the mem object"""
3971

    
3972
        # Get it
3973
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
3974

    
3975
        # load specific parameters from the radio image
3976
        self.set_options()
3977

    
3978
    def set_options(self):
3979
        """This is to read the options from the image and set it in the
3980
        environment, for now just the limits of the freqs in the VHF/UHF
3981
        ranges"""
3982

    
3983
        # setting the correct ranges for each radio type
3984
        ranges = self._memobj.ranges
3985

    
3986
        # the normal dual bands
3987
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3988
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3989

    
3990
        # DEBUG
3991
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3992
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3993

    
3994
        # the additional bands
3995
        if self.MODEL in ["UV-25X4", "KT7900D"]:
3996
            # 200Mhz band
3997
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3998
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3999
            self._220_range = vhf2
4000

    
4001
            # 350Mhz band
4002
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4003
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4004
            self._350_range = uhf2
4005

    
4006
        # set the class with the real data
4007
        self._vhf_range = vhf
4008
        self._uhf_range = uhf
4009

    
4010

    
4011
# Declaring Aliases (Clones of the real radios)
4012
class SKT8900D(chirp_common.Alias):
4013
    VENDOR = "Surecom"
4014
    MODEL = "S-KT8900D"
4015

    
4016

    
4017
class QB25(chirp_common.Alias):
4018
    VENDOR = "Radioddity"
4019
    MODEL = "QB25"
4020

    
4021

    
4022
# real radios
4023
@directory.register
4024
class UV25X2(BTechColor):
4025
    """Baofeng Tech UV25X2"""
4026
    MODEL = "UV-25X2"
4027
    BANDS = 2
4028
    _vhf_range = (130000000, 180000000)
4029
    _uhf_range = (400000000, 521000000)
4030
    _magic = MSTRING_UV25X2
4031
    _fileid = [UV25X2_fp, ]
4032

    
4033

    
4034
@directory.register
4035
class UV25X4(BTechColor):
4036
    """Baofeng Tech UV25X4"""
4037
    MODEL = "UV-25X4"
4038
    BANDS = 4
4039
    _vhf_range = (130000000, 180000000)
4040
    _220_range = (200000000, 271000000)
4041
    _uhf_range = (400000000, 521000000)
4042
    _350_range = (350000000, 391000000)
4043
    _magic = MSTRING_UV25X4
4044
    _fileid = [UV25X4_fp, ]
4045

    
4046

    
4047
@directory.register
4048
class UV50X2(BTechColor):
4049
    """Baofeng Tech UV50X2"""
4050
    MODEL = "UV-50X2"
4051
    BANDS = 2
4052
    _vhf_range = (130000000, 180000000)
4053
    _uhf_range = (400000000, 521000000)
4054
    _magic = MSTRING_UV25X2
4055
    _fileid = [UV50X2_fp, ]
4056
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4057
                     chirp_common.PowerLevel("Low", watts=10)]
4058

    
4059

    
4060
@directory.register
4061
class KT7900D(BTechColor):
4062
    """QYT KT7900D"""
4063
    VENDOR = "QYT"
4064
    MODEL = "KT7900D"
4065
    BANDS = 4
4066
    LIST_TMR = LIST_TMR15
4067
    _vhf_range = (136000000, 175000000)
4068
    _220_range = (200000000, 271000000)
4069
    _uhf_range = (400000000, 481000000)
4070
    _350_range = (350000000, 371000000)
4071
    _magic = MSTRING_KT8900D
4072
    _fileid = [KT7900D_fp, KT7900D_fp1, KT7900D_fp2, KT7900D_fp3, KT7900D_fp4,
4073
               KT7900D_fp5, KT7900D_fp6, KT7900D_fp7, QB25_fp, ]
4074
    # Clones
4075
    ALIASES = [SKT8900D, QB25, ]
4076

    
4077

    
4078
@directory.register
4079
class KT8900D(BTechColor):
4080
    """QYT KT8900D"""
4081
    VENDOR = "QYT"
4082
    MODEL = "KT8900D"
4083
    BANDS = 2
4084
    LIST_TMR = LIST_TMR15
4085
    _vhf_range = (136000000, 175000000)
4086
    _uhf_range = (400000000, 481000000)
4087
    _magic = MSTRING_KT8900D
4088
    _fileid = [KT8900D_fp3, KT8900D_fp2, KT8900D_fp1, KT8900D_fp]
4089

    
4090
    # Clones
4091
    ALIASES = [OTGRadioV1]
4092

    
4093

    
4094
@directory.register
4095
class KT5800(BTechColor):
4096
    """QYT KT5800"""
4097
    VENDOR = "QYT"
4098
    MODEL = "KT5800"
4099
    BANDS = 2
4100
    LIST_TMR = LIST_TMR15
4101
    _vhf_range = (136000000, 175000000)
4102
    _uhf_range = (400000000, 481000000)
4103
    _magic = MSTRING_KT8900D
4104
    _fileid = [KT5800_fp, ]
4105

    
4106

    
4107
@directory.register
4108
class KT980PLUS(BTechColor):
4109
    """QYT KT980PLUS"""
4110
    VENDOR = "QYT"
4111
    MODEL = "KT980PLUS"
4112
    BANDS = 2
4113
    LIST_TMR = LIST_TMR15
4114
    _vhf_range = (136000000, 175000000)
4115
    _uhf_range = (400000000, 481000000)
4116
    _magic = MSTRING_KT8900D
4117
    _fileid = [KT980PLUS_fp1, KT980PLUS_fp]
4118
    _power_levels = [chirp_common.PowerLevel("High", watts=75),
4119
                     chirp_common.PowerLevel("Low", watts=55)]
4120

    
4121
    @classmethod
4122
    def match_model(cls, filedata, filename):
4123
        # This model is only ever matched via metadata
4124
        return False
4125

    
4126

    
4127
@directory.register
4128
class DB25G(BTechColor):
4129
    """Radioddity DB25-G"""
4130
    VENDOR = "Radioddity"
4131
    MODEL = "DB25-G"
4132
    BANDS = 2
4133
    LIST_TMR = LIST_TMR15
4134
    _vhf_range = (136000000, 175000000)
4135
    _uhf_range = (400000000, 481000000)
4136
    _magic = MSTRING_KT8900D
4137
    _fileid = [DB25G_fp1, DB25G_fp]
4138
    _gmrs = True
4139
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
4140
                     chirp_common.PowerLevel("Mid", watts=15),
4141
                     chirp_common.PowerLevel("Low", watts=5)]
4142

    
4143
    @classmethod
4144
    def match_model(cls, filedata, filename):
4145
        # This model is only ever matched via metadata
4146
        return False
4147

    
4148

    
4149
GMRS_MEM_FORMAT = """
4150
#seekto 0x0000;
4151
struct {
4152
  lbcd rxfreq[4];
4153
  lbcd txfreq[4];
4154
  ul16 rxtone;
4155
  ul16 txtone;
4156
  u8 unknown0:4,
4157
     scode:4;
4158
  u8 unknown1:2,
4159
     spmute:2,
4160
     unknown2:2,
4161
     optsig:2;
4162
  u8 unknown3:3,
4163
     scramble:1,
4164
     unknown4:2,
4165
     power:2;
4166
  u8 unknown5:1,
4167
     wide:1,
4168
     unknown6:2,
4169
     bcl:1,
4170
     add:1,
4171
     pttid:2;
4172
} memory[256];
4173

    
4174
#seekto 0x1000;
4175
struct {
4176
  char name[7];
4177
  u8 unknown1[9];
4178
} names[256];
4179

    
4180
#seekto 0x2400;
4181
struct {
4182
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4183
  u8 group_tone;
4184
  u8 repeat_tone;
4185
  u8 unused[13];
4186
} _5tone_std_settings[15];
4187

    
4188
#seekto 0x2500;
4189
struct {
4190
  u8 frame1[5];
4191
  u8 frame2[5];
4192
  u8 frame3[5];
4193
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4194
} _5tone_codes[15];
4195

    
4196
#seekto 0x25F0;
4197
struct {
4198
  u8 _5tone_delay1; // * 10ms
4199
  u8 _5tone_delay2; // * 10ms
4200
  u8 _5tone_delay3; // * 10ms
4201
  u8 _5tone_first_digit_ext_length;
4202
  u8 unknown1;
4203
  u8 unknown2;
4204
  u8 unknown3;
4205
  u8 unknown4;
4206
  u8 decode_standard;
4207
  u8 unknown5:5,
4208
     _5tone_decode_call_frame3:1,
4209
     _5tone_decode_call_frame2:1,
4210
     _5tone_decode_call_frame1:1;
4211
  u8 unknown6:5,
4212
     _5tone_decode_disp_frame3:1,
4213
     _5tone_decode_disp_frame2:1,
4214
     _5tone_decode_disp_frame1:1;
4215
  u8 decode_reset_time; // * 100 + 100ms
4216
} _5tone_settings;
4217

    
4218
#seekto 0x2900;
4219
struct {
4220
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4221
} dtmf_codes[15];
4222

    
4223
#seekto 0x29F0;
4224
struct {
4225
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4226
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4227
  u8 unknown0[14];
4228
  u8 inspection[16];
4229
  u8 monitor[16];
4230
  u8 alarmcode[16];
4231
  u8 stun[16];
4232
  u8 kill[16];
4233
  u8 revive[16];
4234
  u8 unknown1[16];
4235
  u8 unknown2[16];
4236
  u8 unknown3[16];
4237
  u8 unknown4[16];
4238
  u8 unknown5[16];
4239
  u8 unknown6[16];
4240
  u8 unknown7[16];
4241
  u8 masterid[16];
4242
  u8 viceid[16];
4243
  u8 unused01:7,
4244
     mastervice:1;
4245
  u8 unused02:3,
4246
     mrevive:1,
4247
     mkill:1,
4248
     mstun:1,
4249
     mmonitor:1,
4250
     minspection:1;
4251
  u8 unused03:3,
4252
     vrevive:1,
4253
     vkill:1,
4254
     vstun:1,
4255
     vmonitor:1,
4256
     vinspection:1;
4257
  u8 unused04:6,
4258
     txdisable:1,
4259
     rxdisable:1;
4260
  u8 groupcode;
4261
  u8 spacecode;
4262
  u8 delayproctime; // * 100 + 100ms
4263
  u8 resettime;     // * 100 + 100ms
4264
} dtmf_settings;
4265

    
4266
#seekto 0x2D00;
4267
struct {
4268
  struct {
4269
    ul16 freq1;
4270
    u8 unused01[6];
4271
    ul16 freq2;
4272
    u8 unused02[6];
4273
  } _2tone_encode[15];
4274
  u8 duration_1st_tone; // *10ms
4275
  u8 duration_2nd_tone; // *10ms
4276
  u8 duration_gap;      // *10ms
4277
  u8 unused03[13];
4278
  struct {
4279
    struct {
4280
      u8 dec;      // one out of LIST_2TONE_DEC
4281
      u8 response; // one out of LIST_2TONE_RESPONSE
4282
      u8 alert;    // 1-16
4283
    } decs[4];
4284
    u8 unused04[4];
4285
  } _2tone_decode[15];
4286
  u8 unused05[16];
4287

    
4288
  struct {
4289
    ul16 freqA;
4290
    ul16 freqB;
4291
    ul16 freqC;
4292
    ul16 freqD;
4293
    // unknown what those values mean, but they are
4294
    // derived from configured frequencies
4295
    ul16 derived_from_freqA; // 2304000/freqA
4296
    ul16 derived_from_freqB; // 2304000/freqB
4297
    ul16 derived_from_freqC; // 2304000/freqC
4298
    ul16 derived_from_freqD; // 2304000/freqD
4299
  }freqs[15];
4300
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4301
} _2tone;
4302

    
4303
#seekto 0x3000;
4304
struct {
4305
  u8 freq[8];
4306
  char broadcast_station_name[6];
4307
  u8 unknown[2];
4308
} fm_radio_preset[16];
4309

    
4310
#seekto 0x3200;
4311
struct {
4312
  u8 tmr;
4313
  u8 unknown1;
4314
  u8 sql;
4315
  u8 unknown2;
4316
  u8 autolk;
4317
  u8 tot;
4318
  u8 apo;
4319
  u8 unknown3;
4320
  u8 abr;
4321
  u8 beep;
4322
  u8 unknown4[4];
4323
  u8 dtmfst;
4324
  u8 unknown5[2];
4325
  u8 screv;
4326
  u8 unknown6[2];
4327
  u8 pttid;
4328
  u8 pttlt;
4329
  u8 unknown7;
4330
  u8 emctp;
4331
  u8 emcch;
4332
  u8 sigbp;
4333
  u8 unknown8;
4334
  u8 camdf;
4335
  u8 cbmdf;
4336
  u8 ccmdf;
4337
  u8 cdmdf;
4338
  u8 langua;
4339
  u8 sync;
4340

    
4341

    
4342
  u8 stfc;
4343
  u8 mffc;
4344
  u8 sfafc;
4345
  u8 sfbfc;
4346
  u8 sfcfc;
4347
  u8 sfdfc;
4348
  u8 subfc;
4349
  u8 fmfc;
4350
  u8 sigfc;
4351
  u8 modfc;
4352
  u8 menufc;
4353
  u8 txfc;
4354
  u8 txdisp;
4355
  u8 unknown9[5];
4356
  u8 anil;
4357
  u8 reps;
4358
  u8 repm;
4359
  u8 tmrmr;
4360
  u8 ste;
4361
  u8 rpste;
4362
  u8 rptdl;
4363
  u8 dtmfg;
4364
  u8 mgain;
4365
  u8 skiptx;
4366
  u8 scmode;
4367
} settings;
4368

    
4369
#seekto 0x3280;
4370
struct {
4371
  u8 unknown1;
4372
  u8 vfomr;
4373
  u8 keylock;
4374
  u8 unknown2;
4375
  u8 unknown3:4,
4376
     vfomren:1,
4377
     unknown4:1,
4378
     reseten:1,
4379
     menuen:1;
4380
  u8 unknown5[11];
4381
  u8 dispab;
4382
  u8 unknown6[2];
4383
  u8 smenu;
4384
  u8 unknown7[7];
4385
  u8 vfomra;
4386
  u8 vfomrb;
4387
  u8 vfomrc;
4388
  u8 vfomrd;
4389
  u8 mrcha;
4390
  u8 mrchb;
4391
  u8 mrchc;
4392
  u8 mrchd;
4393
} settings2;
4394

    
4395
struct settings_vfo {
4396
  u8 freq[8];
4397
  u8 offset[6];
4398
  u8 unknown2[2];
4399
  ul16 rxtone;
4400
  ul16 txtone;
4401
  u8 scode;
4402
  u8 spmute;
4403
  u8 optsig;
4404
  u8 scramble;
4405
  u8 wide;
4406
  u8 power;
4407
  u8 shiftd;
4408
  u8 step;
4409
  u8 unknown3[4];
4410
};
4411

    
4412
#seekto 0x3300;
4413
struct {
4414
  struct settings_vfo a;
4415
  struct settings_vfo b;
4416
  struct settings_vfo c;
4417
  struct settings_vfo d;
4418
} vfo;
4419

    
4420
#seekto 0x3D80;
4421
struct {
4422
  u8 vhf_low[3];
4423
  u8 vhf_high[3];
4424
  u8 unknown1[4];
4425
  u8 unknown2[6];
4426
  u8 vhf2_low[3];
4427
  u8 vhf2_high[3];
4428
  u8 unknown3[4];
4429
  u8 unknown4[6];
4430
  u8 uhf_low[3];
4431
  u8 uhf_high[3];
4432
  u8 unknown5[4];
4433
  u8 unknown6[6];
4434
  u8 uhf2_low[3];
4435
  u8 uhf2_high[3];
4436
} ranges;
4437

    
4438
#seekto 0x33B0;
4439
struct {
4440
  char line[16];
4441
} static_msg;
4442

    
4443
#seekto 0x3F70;
4444
struct {
4445
  char fp[6];
4446
} fingerprint;
4447

    
4448
"""
4449

    
4450

    
4451
class BTechGMRS(BTechMobileCommon):
4452
    """BTECH's GMRS Mobile"""
4453
    COLOR_LCD = True
4454
    COLOR_LCD2 = True
4455
    NAME_LENGTH = 7
4456
    UPLOAD_MEM_SIZE = 0X3400
4457

    
4458
    def process_mmap(self):
4459
        """Process the mem map into the mem object"""
4460

    
4461
        # Get it
4462
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
4463

    
4464
        # load specific parameters from the radio image
4465
        self.set_options()
4466

    
4467
    def set_options(self):
4468
        """This is to read the options from the image and set it in the
4469
        environment, for now just the limits of the freqs in the VHF/UHF
4470
        ranges"""
4471

    
4472
        # setting the correct ranges for each radio type
4473
        ranges = self._memobj.ranges
4474

    
4475
        # the normal dual bands
4476
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4477
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4478

    
4479
        # DEBUG
4480
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4481
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4482

    
4483
        # set the class with the real data
4484
        self._vhf_range = vhf
4485
        self._uhf_range = uhf
4486

    
4487

    
4488
# real radios
4489
@directory.register
4490
class GMRS50X1(BTechGMRS):
4491
    """Baofeng Tech GMRS50X1"""
4492
    MODEL = "GMRS-50X1"
4493
    BANDS = 2
4494
    LIST_TMR = LIST_TMR16
4495
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4496
                     chirp_common.PowerLevel("Mid", watts=10),
4497
                     chirp_common.PowerLevel("Low", watts=5)]
4498
    _vhf_range = (136000000, 175000000)
4499
    _uhf_range = (400000000, 521000000)
4500
    _upper = 255
4501
    _magic = MSTRING_GMRS50X1
4502
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
4503

    
4504

    
4505
COLORHT_MEM_FORMAT = """
4506
#seekto 0x0000;
4507
struct {
4508
  lbcd rxfreq[4];
4509
  lbcd txfreq[4];
4510
  ul16 rxtone;
4511
  ul16 txtone;
4512
  u8 unknown0:4,
4513
     scode:4;
4514
  u8 unknown1:2,
4515
     spmute:2,
4516
     unknown2:2,
4517
     optsig:2;
4518
  u8 unknown3:3,
4519
     scramble:1,
4520
     unknown4:3,
4521
     power:1;
4522
  u8 unknown5:1,
4523
     wide:1,
4524
     unknown6:2,
4525
     bcl:1,
4526
     add:1,
4527
     pttid:2;
4528
} memory[200];
4529

    
4530
#seekto 0x0E00;
4531
struct {
4532
  u8 tmr;
4533
  u8 unknownE01;
4534
  u8 sql;
4535
  u8 unknownE03[2];
4536
  u8 tot;
4537
  u8 save;
4538
  u8 unknownE07;
4539
  u8 abr;
4540
  u8 beep;
4541
  u8 unknownE0A[4];
4542
  u8 dsub;
4543
  u8 dtmfst;
4544
  u8 screv;
4545
  u8 unknownE11[3];
4546
  u8 pttid;
4547
  u8 unknownE15;
4548
  u8 pttlt;
4549
  u8 unknownE17;
4550
  u8 emctp;
4551
  u8 emcch;
4552
  u8 sigbp;
4553
  u8 unknownE1B;
4554
  u8 camdf;
4555
  u8 cbmdf;
4556
  u8 ccmdf;
4557
  u8 cdmdf;
4558
  u8 langua;
4559
  u8 voice;
4560
  u8 vox;
4561
  u8 voxt;
4562
  u8 sync;          // BTech radios use this as the display sync setting
4563
                    // other radios use this as the auto keypad lock setting
4564
  u8 stfc;
4565
  u8 mffc;
4566
  u8 sfafc;
4567
  u8 sfbfc;
4568
  u8 sfcfc;
4569
  u8 sfdfc;
4570
  u8 subfc;
4571
  u8 fmfc;
4572
  u8 sigfc;
4573
  u8 menufc;
4574
  u8 txfc;
4575
  u8 rxfc;
4576
  u8 unknownE31[5];
4577
  u8 anil;
4578
  u8 reps;
4579
  u8 tmrmr;
4580
  u8 ste;
4581
  u8 rpste;
4582
  u8 rptdl;
4583
  u8 dtmfg;
4584
  u8 tmrtx;
4585
} settings;
4586

    
4587
#seekto 0x0E80;
4588
struct {
4589
  u8 unknown1;
4590
  u8 vfomr;
4591
  u8 keylock;
4592
  u8 unknown2;
4593
  u8 unknown3:4,
4594
     vfomren:1,
4595
     unknown4:1,
4596
     reseten:1,
4597
     menuen:1;
4598
  u8 unknown5[11];
4599
  u8 dispab;
4600
  u8 unknown6[2];
4601
  u8 menu;
4602
  u8 unknown7[7];
4603
  u8 vfomra;
4604
  u8 vfomrb;
4605
  u8 vfomrc;
4606
  u8 vfomrd;
4607
  u8 mrcha;
4608
  u8 mrchb;
4609
  u8 mrchc;
4610
  u8 mrchd;
4611
} settings2;
4612

    
4613
struct settings_vfo {
4614
  u8 freq[8];
4615
  u8 offset[6];
4616
  u8 unknown2[2];
4617
  ul16 rxtone;
4618
  ul16 txtone;
4619
  u8 scode;
4620
  u8 spmute;
4621
  u8 optsig;
4622
  u8 scramble;
4623
  u8 wide;
4624
  u8 power;
4625
  u8 shiftd;
4626
  u8 step;
4627
  u8 unknown3[4];
4628
};
4629

    
4630
#seekto 0x0F00;
4631
struct {
4632
  struct settings_vfo a;
4633
  struct settings_vfo b;
4634
  struct settings_vfo c;
4635
  struct settings_vfo d;
4636
} vfo;
4637

    
4638
#seekto 0x0FE0;
4639
struct {
4640
  char line[16];
4641
} static_msg;
4642

    
4643
#seekto 0x1000;
4644
struct {
4645
  char name[8];
4646
  u8 unknown1[8];
4647
} names[200];
4648

    
4649
#seekto 0x2400;
4650
struct {
4651
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4652
  u8 group_tone;
4653
  u8 repeat_tone;
4654
  u8 unused[13];
4655
} _5tone_std_settings[15];
4656

    
4657
#seekto 0x2500;
4658
struct {
4659
  u8 frame1[5];
4660
  u8 frame2[5];
4661
  u8 frame3[5];
4662
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4663
} _5tone_codes[15];
4664

    
4665
#seekto 0x25F0;
4666
struct {
4667
  u8 _5tone_delay1; // * 10ms
4668
  u8 _5tone_delay2; // * 10ms
4669
  u8 _5tone_delay3; // * 10ms
4670
  u8 _5tone_first_digit_ext_length;
4671
  u8 unknown1;
4672
  u8 unknown2;
4673
  u8 unknown3;
4674
  u8 unknown4;
4675
  u8 decode_standard;
4676
  u8 unknown5:5,
4677
     _5tone_decode_call_frame3:1,
4678
     _5tone_decode_call_frame2:1,
4679
     _5tone_decode_call_frame1:1;
4680
  u8 unknown6:5,
4681
     _5tone_decode_disp_frame3:1,
4682
     _5tone_decode_disp_frame2:1,
4683
     _5tone_decode_disp_frame1:1;
4684
  u8 decode_reset_time; // * 100 + 100ms
4685
} _5tone_settings;
4686

    
4687
#seekto 0x2900;
4688
struct {
4689
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4690
} dtmf_codes[15];
4691

    
4692
#seekto 0x29F0;
4693
struct {
4694
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4695
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4696
  u8 unknown0[14];
4697
  u8 inspection[16];
4698
  u8 monitor[16];
4699
  u8 alarmcode[16];
4700
  u8 stun[16];
4701
  u8 kill[16];
4702
  u8 revive[16];
4703
  u8 unknown1[16];
4704
  u8 unknown2[16];
4705
  u8 unknown3[16];
4706
  u8 unknown4[16];
4707
  u8 unknown5[16];
4708
  u8 unknown6[16];
4709
  u8 unknown7[16];
4710
  u8 masterid[16];
4711
  u8 viceid[16];
4712
  u8 unused01:7,
4713
     mastervice:1;
4714
  u8 unused02:3,
4715
     mrevive:1,
4716
     mkill:1,
4717
     mstun:1,
4718
     mmonitor:1,
4719
     minspection:1;
4720
  u8 unused03:3,
4721
     vrevive:1,
4722
     vkill:1,
4723
     vstun:1,
4724
     vmonitor:1,
4725
     vinspection:1;
4726
  u8 unused04:6,
4727
     txdisable:1,
4728
     rxdisable:1;
4729
  u8 groupcode;
4730
  u8 spacecode;
4731
  u8 delayproctime; // * 100 + 100ms
4732
  u8 resettime;     // * 100 + 100ms
4733
} dtmf_settings;
4734

    
4735
#seekto 0x2D00;
4736
struct {
4737
  struct {
4738
    ul16 freq1;
4739
    u8 unused01[6];
4740
    ul16 freq2;
4741
    u8 unused02[6];
4742
  } _2tone_encode[15];
4743
  u8 duration_1st_tone; // *10ms
4744
  u8 duration_2nd_tone; // *10ms
4745
  u8 duration_gap;      // *10ms
4746
  u8 unused03[13];
4747
  struct {
4748
    struct {
4749
      u8 dec;      // one out of LIST_2TONE_DEC
4750
      u8 response; // one out of LIST_2TONE_RESPONSE
4751
      u8 alert;    // 1-16
4752
    } decs[4];
4753
    u8 unused04[4];
4754
  } _2tone_decode[15];
4755
  u8 unused05[16];
4756

    
4757
  struct {
4758
    ul16 freqA;
4759
    ul16 freqB;
4760
    ul16 freqC;
4761
    ul16 freqD;
4762
    // unknown what those values mean, but they are
4763
    // derived from configured frequencies
4764
    ul16 derived_from_freqA; // 2304000/freqA
4765
    ul16 derived_from_freqB; // 2304000/freqB
4766
    ul16 derived_from_freqC; // 2304000/freqC
4767
    ul16 derived_from_freqD; // 2304000/freqD
4768
  }freqs[15];
4769
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4770
} _2tone;
4771

    
4772
#seekto 0x3D80;
4773
struct {
4774
  u8 vhf_low[3];
4775
  u8 vhf_high[3];
4776
  u8 unknown1[4];
4777
  u8 unknown2[6];
4778
  u8 vhf2_low[3];
4779
  u8 vhf2_high[3];
4780
  u8 unknown3[4];
4781
  u8 unknown4[6];
4782
  u8 uhf_low[3];
4783
  u8 uhf_high[3];
4784
  u8 unknown5[4];
4785
  u8 unknown6[6];
4786
  u8 uhf2_low[3];
4787
  u8 uhf2_high[3];
4788
} ranges;
4789

    
4790
#seekto 0x3F70;
4791
struct {
4792
  char fp[6];
4793
} fingerprint;
4794

    
4795
"""
4796

    
4797

    
4798
class QYTColorHT(BTechMobileCommon):
4799
    """QTY's Color LCD Handheld and alike radios"""
4800
    COLOR_LCD = True
4801
    COLOR_LCD3 = True
4802
    NAME_LENGTH = 8
4803
    LIST_TMR = LIST_TMR15
4804

    
4805
    def process_mmap(self):
4806
        """Process the mem map into the mem object"""
4807

    
4808
        # Get it
4809
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4810

    
4811
        # load specific parameters from the radio image
4812
        self.set_options()
4813

    
4814
    def set_options(self):
4815
        """This is to read the options from the image and set it in the
4816
        environment, for now just the limits of the freqs in the VHF/UHF
4817
        ranges"""
4818

    
4819
        # setting the correct ranges for each radio type
4820
        ranges = self._memobj.ranges
4821

    
4822
        # the normal dual bands
4823
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4824
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4825

    
4826
        # DEBUG
4827
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4828
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4829

    
4830
        # the additional bands
4831
        if self.MODEL in ["KT-8R"]:
4832
            # 200Mhz band
4833
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
4834
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
4835
            self._220_range = vhf2
4836

    
4837
            # 350Mhz band
4838
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4839
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4840
            self._350_range = uhf2
4841

    
4842
        # set the class with the real data
4843
        self._vhf_range = vhf
4844
        self._uhf_range = uhf
4845

    
4846

    
4847
# real radios
4848
@directory.register
4849
class KT8R(QYTColorHT):
4850
    """QYT KT8R"""
4851
    VENDOR = "QYT"
4852
    MODEL = "KT-8R"
4853
    BANDS = 4
4854
    LIST_TMR = LIST_TMR16
4855
    _vhf_range = (136000000, 175000000)
4856
    _220_range = (200000000, 261000000)
4857
    _uhf_range = (400000000, 481000000)
4858
    _350_range = (350000000, 391000000)
4859
    _magic = MSTRING_KT8R
4860
    _fileid = [KT8R_fp2, KT8R_fp1, KT8R_fp, ]
4861
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
4862
                     chirp_common.PowerLevel("Low", watts=1)]
4863

    
4864

    
4865
COLOR9900_MEM_FORMAT = """
4866
#seekto 0x0000;
4867
struct {
4868
  lbcd rxfreq[4];
4869
  lbcd txfreq[4];
4870
  ul16 rxtone;
4871
  ul16 txtone;
4872
  u8 unknown0:4,
4873
     scode:4;
4874
  u8 unknown1:2,
4875
     spmute:2,
4876
     unknown2:2,
4877
     optsig:2;
4878
  u8 unknown3:3,
4879
     scramble:1,
4880
     unknown4:2,
4881
     power:2;
4882
  u8 unknown5:1,
4883
     wide:1,
4884
     unknown6:2,
4885
     bcl:1,
4886
     add:1,
4887
     pttid:2;
4888
} memory[200];
4889

    
4890
#seekto 0x0E00;
4891
struct {
4892
  u8 tmr;
4893
  u8 unknown1;
4894
  u8 sql;
4895
  u8 unknown2[2];
4896
  u8 tot;
4897
  u8 volume;
4898
  u8 unknown3;
4899
  u8 abr;
4900
  u8 beep;
4901
  u8 unknown4[4];
4902
  u8 dsub;
4903
  u8 dtmfst;
4904
  u8 unknown_e10;
4905
  u8 unknown_e11;
4906
  u8 screv;
4907
  u8 unknown_e13;
4908
  u8 unknown_e14;
4909
  u8 pttid;
4910
  u8 pttlt;
4911
  u8 unknown7;
4912
  u8 emctp;
4913
  u8 emcch;
4914
  u8 sigbp;
4915
  u8 unknown8;
4916
  u8 camdf;
4917
  u8 cbmdf;
4918
  u8 ccmdf;
4919
  u8 language;
4920
  u8 tmrtx;
4921
  u8 vox;
4922
  u8 voxt;
4923
  u8 autolock;
4924
  u8 asfc;
4925
  u8 mainfc;
4926
  u8 a_fc;
4927
  u8 b_fc;
4928
  u8 c_fc;
4929
  u8 subfc;
4930
  u8 battfc;
4931
  u8 sigfc;
4932
  u8 menufc;
4933
  u8 txfc;
4934
  u8 rxfc;
4935
  u8 unknown_e2f;
4936
  u8 unknown_e30;
4937
  u8 unknown9[3];
4938
  u8 anil;
4939
  u8 reps;
4940
  u8 tmrmr;
4941
  u8 ste;
4942
  u8 rpste;
4943
  u8 rptdl;
4944
  u8 dtmfg;
4945
} settings;
4946

    
4947
#seekto 0x0E80;
4948
struct {
4949
  u8 unknown1;
4950
  u8 vfomr;
4951
  u8 keylock;
4952
  u8 unknown2;
4953
  u8 unknown3:4,
4954
     vfomren:1,
4955
     unknown4:1,
4956
     reseten:1,
4957
     menuen:1;
4958
  u8 unknown5[11];
4959
  u8 dispab;
4960
  u8 unknown6[2];
4961
  u8 menu;
4962
  u8 unknown7[7];
4963
  u8 vfomra;
4964
  u8 vfomrb;
4965
  u8 vfomrc;
4966
  u8 vfomrd;
4967
  u8 mrcha;
4968
  u8 mrchb;
4969
  u8 mrchc;
4970
  u8 mrchd;
4971
} settings2;
4972

    
4973
struct settings_vfo {
4974
  u8 freq[8];
4975
  u8 offset[6];
4976
  u8 unknown2[2];
4977
  ul16 rxtone;
4978
  ul16 txtone;
4979
  u8 scode;
4980
  u8 spmute;
4981
  u8 optsig;
4982
  u8 scramble;
4983
  u8 wide;
4984
  u8 power;
4985
  u8 shiftd;
4986
  u8 step;
4987
  u8 unknown3[4];
4988
};
4989

    
4990
#seekto 0x0F00;
4991
struct {
4992
  struct settings_vfo a;
4993
  struct settings_vfo b;
4994
  struct settings_vfo c;
4995
  struct settings_vfo d;
4996
} vfo;
4997

    
4998
#seekto 0x0F80;
4999
struct {
5000
  char line1[8];
5001
  char line2[8];
5002
  char line3[8];
5003
  char line4[8];
5004
  char line5[8];
5005
  char line6[8];
5006
  char line7[8];
5007
  char line8[8];
5008
} poweron_msg;
5009

    
5010
#seekto 0x0FE0;
5011
struct {
5012
  char line[16];
5013
} static_msg;
5014

    
5015
#seekto 0x1000;
5016
struct {
5017
  char name[7];
5018
  u8 unknown1[9];
5019
} names[200];
5020

    
5021
#seekto 0x2400;
5022
struct {
5023
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
5024
  u8 group_tone;
5025
  u8 repeat_tone;
5026
  u8 unused[13];
5027
} _5tone_std_settings[15];
5028

    
5029
#seekto 0x2500;
5030
struct {
5031
  u8 frame1[5];
5032
  u8 frame2[5];
5033
  u8 frame3[5];
5034
  u8 standard;   // one out of LIST_5TONE_STANDARDS
5035
} _5tone_codes[15];
5036

    
5037
#seekto 0x25F0;
5038
struct {
5039
  u8 _5tone_delay1; // * 10ms
5040
  u8 _5tone_delay2; // * 10ms
5041
  u8 _5tone_delay3; // * 10ms
5042
  u8 _5tone_first_digit_ext_length;
5043
  u8 unknown1;
5044
  u8 unknown2;
5045
  u8 unknown3;
5046
  u8 unknown4;
5047
  u8 decode_standard;
5048
  u8 unknown5:5,
5049
     _5tone_decode_call_frame3:1,
5050
     _5tone_decode_call_frame2:1,
5051
     _5tone_decode_call_frame1:1;
5052
  u8 unknown6:5,
5053
     _5tone_decode_disp_frame3:1,
5054
     _5tone_decode_disp_frame2:1,
5055
     _5tone_decode_disp_frame1:1;
5056
  u8 decode_reset_time; // * 100 + 100ms
5057
} _5tone_settings;
5058

    
5059
#seekto 0x2900;
5060
struct {
5061
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
5062
} dtmf_codes[15];
5063

    
5064
#seekto 0x29F0;
5065
struct {
5066
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10      // 9f0
5067
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10      // 9f1
5068
  u8 unknown0[14];                                             // 9f2-9ff
5069
  u8 inspection[16];                                           // a00-a0f
5070
  u8 monitor[16];                                              // a10-a1f
5071
  u8 alarmcode[16];                                            // a20-a2f
5072
  u8 stun[16];                                                 // a30-a3f
5073
  u8 kill[16];                                                 // a40-a4f
5074
  u8 revive[16];                                               // a50-a5f
5075
  u8 unknown1[16];                                             // a60-a6f
5076
  u8 unknown2[16];                                             // a70-a7f
5077
  u8 unknown3[16];                                             // a80-a8f
5078
  u8 unknown4[16];                                             // a90-a9f
5079
  u8 unknown5[16];                                             // aa0-aaf
5080
  u8 unknown6[16];                                             // ab0-abf
5081
  u8 unknown7[16];                                             // ac0-acf
5082
  u8 masterid[16];                                             // ad0-adf
5083
  u8 viceid[16];                                               // ae0-aef
5084
  u8 unused01:7,                                               // af0
5085
     mastervice:1;
5086
  u8 unused02:3,                                               // af1
5087
     mrevive:1,
5088
     mkill:1,
5089
     mstun:1,
5090
     mmonitor:1,
5091
     minspection:1;
5092
  u8 unused03:3,                                               // af2
5093
     vrevive:1,
5094
     vkill:1,
5095
     vstun:1,
5096
     vmonitor:1,
5097
     vinspection:1;
5098
  u8 unused04:6,                                               // af3
5099
     txdisable:1,
5100
     rxdisable:1;
5101
  u8 groupcode;                                                // af4
5102
  u8 spacecode;                                                // af5
5103
  u8 delayproctime; // * 100 + 100ms                           // af6
5104
  u8 resettime;     // * 100 + 100ms                           // af7
5105
} dtmf_settings;
5106

    
5107
#seekto 0x2D00;
5108
struct {
5109
  struct {
5110
    ul16 freq1;
5111
    u8 unused01[6];
5112
    ul16 freq2;
5113
    u8 unused02[6];
5114
  } _2tone_encode[15];
5115
  u8 duration_1st_tone; // *10ms
5116
  u8 duration_2nd_tone; // *10ms
5117
  u8 duration_gap;      // *10ms
5118
  u8 unused03[13];
5119
  struct {
5120
    struct {
5121
      u8 dec;      // one out of LIST_2TONE_DEC
5122
      u8 response; // one out of LIST_2TONE_RESPONSE
5123
      u8 alert;    // 1-16
5124
    } decs[4];
5125
    u8 unused04[4];
5126
  } _2tone_decode[15];
5127
  u8 unused05[16];
5128

    
5129
  struct {
5130
    ul16 freqA;
5131
    ul16 freqB;
5132
    ul16 freqC;
5133
    ul16 freqD;
5134
    // unknown what those values mean, but they are
5135
    // derived from configured frequencies
5136
    ul16 derived_from_freqA; // 2304000/freqA
5137
    ul16 derived_from_freqB; // 2304000/freqB
5138
    ul16 derived_from_freqC; // 2304000/freqC
5139
    ul16 derived_from_freqD; // 2304000/freqD
5140
  }freqs[15];
5141
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
5142
} _2tone;
5143

    
5144
#seekto 0x3D80;
5145
struct {
5146
  u8 vhf_low[3];
5147
  u8 vhf_high[3];
5148
  u8 unknown1[4];
5149
  u8 unknown2[6];
5150
  u8 vhf2_low[3];
5151
  u8 vhf2_high[3];
5152
  u8 unknown3[4];
5153
  u8 unknown4[6];
5154
  u8 uhf_low[3];
5155
  u8 uhf_high[3];
5156
  u8 unknown5[4];
5157
  u8 unknown6[6];
5158
  u8 uhf2_low[3];
5159
  u8 uhf2_high[3];
5160
} ranges;
5161

    
5162
#seekto 0x3F70;
5163
struct {
5164
  char fp[6];
5165
} fingerprint;
5166

    
5167
"""
5168

    
5169

    
5170
class BTechColorWP(BTechMobileCommon):
5171
    """BTECH's Color WP Mobile and alike radios"""
5172
    COLOR_LCD = True
5173
    COLOR_LCD4 = True
5174
    NAME_LENGTH = 7
5175
    LIST_TMR = LIST_TMR7
5176

    
5177
    def set_options(self):
5178
        """This is to read the options from the image and set it in the
5179
        environment, for now just the limits of the freqs in the VHF/UHF
5180
        ranges"""
5181

    
5182
        # setting the correct ranges for each radio type
5183
        ranges = self._memobj.ranges
5184

    
5185
        # the normal dual bands
5186
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
5187
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
5188

    
5189
        # DEBUG
5190
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
5191
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
5192

    
5193
        # set the class with the real data
5194
        self._vhf_range = vhf
5195
        self._uhf_range = uhf
5196

    
5197

    
5198
# real radios
5199
@directory.register
5200
class KTWP12(BTechColorWP):
5201
    """QYT KT-WP12"""
5202
    VENDOR = "QYT"
5203
    MODEL = "KT-WP12"
5204
    BANDS = 2
5205
    # <delete me> LIST_TMR = LIST_TMR7
5206
    UPLOAD_MEM_SIZE = 0X3100
5207
    # <delete me> UPLOAD_MEM_SIZE = 0X4000
5208
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
5209
                     chirp_common.PowerLevel("Low", watts=5)]
5210
    _upper = 199
5211
    _magic = MSTRING_KTWP12
5212
    _fileid = [KTWP12_fp, ]
5213
    _gmrs = False
5214

    
5215
    def process_mmap(self):
5216
        """Process the mem map into the mem object"""
5217

    
5218
        # Get it
5219
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5220

    
5221
        # load specific parameters from the radio image
5222
        self.set_options()
5223

    
5224

    
5225
@directory.register
5226
class WP9900(BTechColorWP):
5227
    """Anysecu WP-9900"""
5228
    VENDOR = "Anysecu"
5229
    MODEL = "WP-9900"
5230
    BANDS = 2
5231
    # <delete me> LIST_TMR = LIST_TMR7
5232
    UPLOAD_MEM_SIZE = 0X3100
5233
    # <delete me> UPLOAD_MEM_SIZE = 0X4000
5234
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
5235
                     chirp_common.PowerLevel("Low", watts=5)]
5236
    _upper = 199
5237
    _magic = MSTRING_KTWP12
5238
    _fileid = [WP9900_fp, ]
5239
    _gmrs = False
5240

    
5241
    def process_mmap(self):
5242
        """Process the mem map into the mem object"""
5243

    
5244
        # Get it
5245
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5246

    
5247
        # load specific parameters from the radio image
5248
        self.set_options()
(11-11/13)