Project

General

Profile

Bug #9904 » btech_vc9214.py

Jim Unroe, 06/06/2022 09:56 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
KT7900D_fp8 = "VC9214"
249

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

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

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

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

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

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

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

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

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

    
286

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

    
310

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

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

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

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

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

    
337

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

    
341
    data = ""
342

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

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

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

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

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

    
363
    return data
364

    
365

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

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

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

    
379

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

    
387
    return frame
388

    
389

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

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

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

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

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

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

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

    
417

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

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

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

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

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

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

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

    
451
            break
452

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

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

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

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

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

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

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

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

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

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

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

    
507
    return True
508

    
509

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

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

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

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

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

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

    
533
        # aggregate the data
534
        data += d
535

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

    
541
    return data
542

    
543

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
597

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

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

    
605
    return False
606

    
607

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

    
616
    return (ilow, ihigh)
617

    
618

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

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

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

    
633

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

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

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

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

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

    
688
            """))
689
        return rp
690

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

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

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

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

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

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

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

    
743
        return rf
744

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

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

    
752
        return msgs
753

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

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

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

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

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

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

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

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

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

    
825
        # Memory number
826
        mem.number = number
827

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
913
        return mem
914

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1061
        return mem
1062

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2326
                i = i + 1
2327

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2617
        top.append(group_5tone)
2618

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2998
            i = i + 1
2999

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3121
            i = i + 1
3122

    
3123
        return top
3124

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

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

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

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

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

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

    
3183

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3467
"""
3468

    
3469

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

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

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

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

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

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

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

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

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

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

    
3517

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

    
3523

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

    
3528

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

    
3533

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

    
3538

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

    
3543

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

    
3554

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

    
3566

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

    
3579

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

    
3590

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

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

    
3606

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

    
3611

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

    
3631

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

    
3649

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

    
3663

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3961
"""
3962

    
3963

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

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

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

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

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

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

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

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

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

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

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

    
4011

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

    
4017

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

    
4022

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

    
4034

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

    
4047

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

    
4060

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

    
4078

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

    
4091
    # Clones
4092
    ALIASES = [OTGRadioV1]
4093

    
4094

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

    
4107

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

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

    
4127

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

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

    
4149

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

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

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

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

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

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

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

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

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

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

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

    
4342

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

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

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

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

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

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

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

    
4449
"""
4450

    
4451

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

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

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

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

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

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

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

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

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

    
4488

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

    
4505

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
4796
"""
4797

    
4798

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

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

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

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

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

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

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

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

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

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

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

    
4847

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

    
4865

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
5168
"""
5169

    
5170

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

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

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

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

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

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

    
5198

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

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

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

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

    
5223

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

    
5238
    def process_mmap(self):
5239
        """Process the mem map into the mem object"""
5240

    
5241
        # Get it
5242
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5243

    
5244
        # load specific parameters from the radio image
5245
        self.set_options()
(3-3/3)