Project

General

Profile

Bug #9845 ยป btech_v2.0.py

Jim Unroe, 04/26/2022 09:51 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_PONMSG = ["Full", "Message", "Battery voltage"]
74
LIST_REPM = ["Off", "Carrier", "CTCSS or DCS", "Tone", "DTMF"]
75
LIST_REPS = ["1000 Hz", "1450 Hz", "1750 Hz", "2100Hz"]
76
LIST_RPTDL = ["Off"] + ["%s ms" % x for x in range(1, 11)]
77
LIST_SCMODE = ["Off", "PTT-SC", "MEM-SC", "PON-SC"]
78
LIST_SHIFT = ["Off", "+", "-"]
79
LIST_SKIPTX = ["Off", "Skip 1", "Skip 2"]
80
STEPS = [2.5, 5.0, 6.25, 10.0, 12.5, 25.0]
81
LIST_STEP = [str(x) for x in STEPS]
82
LIST_SYNC = ["Off", "AB", "CD", "AB+CD"]
83
# the first 12 TMR choices common to all color display mobile radios
84
LIST_TMR12 = ["OFF", "M+A", "M+B", "M+C", "M+D", "M+A+B", "M+A+C", "M+A+D",
85
              "M+B+C", "M+B+D", "M+C+D", "M+A+B+C"]
86
# the 16 choice list for color display mobile radios that correctly implement
87
# the full 16 TMR choices
88
LIST_TMR16 = LIST_TMR12 + ["M+A+B+D", "M+A+C+D", "M+B+C+D", "A+B+C+D"]
89
# the 15 choice list for color mobile radios that are missing the M+A+B+D
90
# choice in the TMR menu
91
LIST_TMR15 = LIST_TMR12 + ["M+A+C+D", "M+B+C+D", "A+B+C+D"]
92
LIST_TMRTX = ["Track", "Fixed"]
93
LIST_TOT = ["%s sec" % x for x in range(15, 615, 15)]
94
LIST_TXDISP = ["Power", "Mic Volume"]
95
LIST_TXP = ["High", "Low"]
96
LIST_TXP3 = ["High", "Mid", "Low"]
97
LIST_SCREV = ["TO (timeout)", "CO (carrier operated)", "SE (search)"]
98
LIST_VFOMR = ["Frequency", "Channel"]
99
LIST_VOICE = ["Off"] + LIST_LANGUA
100
LIST_VOX = ["Off"] + ["%s" % x for x in range(1, 11)]
101
LIST_VOXT = ["%s seconds" % x for x in range(0, 21)]
102
LIST_WIDE = ["Wide", "Narrow"]
103

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

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

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

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

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

    
154

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

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

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

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

    
186
# B-TECH UV-25X2
187
UV25X2_fp = "UC2012"
188

    
189
# B-TECH UV-25X4
190
UV25X4_fp = "UC4014"
191

    
192
# B-TECH UV-50X2
193
UV50X2_fp = "UC2M12"
194

    
195
# B-TECH GMRS-50X1
196
GMRS50X1_fp = "NC1802"
197
GMRS50X1_fp1 = "NC1932"
198

    
199
# special var to know when we found a BTECH Gen 3
200
BTECH3 = [UV2501G3_fp, UV2501_220G3_fp, UV5001G3_fp]
201

    
202

    
203
# WACCOM Mini-8900
204
MINI8900_fp = "M28854"
205

    
206

    
207
# QYT KT-UV980
208
KTUV980_fp = "H28854"
209

    
210
# QYT KT8900
211
KT8900_fp = "M29154"
212
# New generations KT8900
213
KT8900_fp1 = "M2C234"
214
KT8900_fp2 = "M2G1F4"
215
KT8900_fp3 = "M2G2F4"
216
KT8900_fp4 = "M2G304"
217
KT8900_fp5 = "M2G314"
218
KT8900_fp6 = "M2G424"
219
KT8900_fp7 = "M27184"
220

    
221
# KT8900R
222
KT8900R_fp = "M3G1F4"
223
# Second Generation
224
KT8900R_fp1 = "M3G214"
225
# another model
226
KT8900R_fp2 = "M3C234"
227
# another model G4?
228
KT8900R_fp3 = "M39164"
229
# another model
230
KT8900R_fp4 = "M3G314"
231
# AC3MB: another id
232
KT8900R_fp5 = "M3B064"
233

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

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

    
247
# KT8900D (dual band)
248
KT8900D_fp = "VC2002"
249
KT8900D_fp1 = "VC8632"
250
KT8900D_fp2 = "VC3402"
251
KT8900D_fp3 = "VC7062"
252

    
253
# LUITON LT-588UV
254
LT588UV_fp = "V2G1F4"
255
# Added by rstrickoff gen 2 id
256
LT588UV_fp1 = "V2G214"
257

    
258
# QYT KT-8R (quad band ht)
259
KT8R_fp = "MCB264"
260
KT8R_fp1 = "MCB284"
261
KT8R_fp2 = "MC5264"
262

    
263
# QYT KT5800 (dual band)
264
KT5800_fp = "VCB222"
265

    
266
# QYT KT980Plus (dual band)
267
KT980PLUS_fp = "VC2002"
268
KT980PLUS_fp1 = "VC6042"
269

    
270
# Radioddity DB25-G (gmrs)
271
DB25G_fp = "VC6182"
272
DB25G_fp1 = "VC7062"
273

    
274

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

    
296

    
297
def _clean_buffer(radio):
298
    """Cleaning the read serial buffer, hard timeout to survive an infinite
299
    data stream"""
300

    
301
    # touching the serial timeout to optimize the flushing
302
    # restored at the end to the default value
303
    radio.pipe.timeout = 0.1
304
    dump = "1"
305
    datacount = 0
306

    
307
    try:
308
        while len(dump) > 0:
309
            dump = radio.pipe.read(100)
310
            datacount += len(dump)
311
            # hard limit to survive a infinite serial data stream
312
            # 5 times bigger than a normal rx block (69 bytes)
313
            if datacount > 345:
314
                seriale = "Please check your serial port selection."
315
                raise errors.RadioError(seriale)
316

    
317
        # restore the default serial timeout
318
        radio.pipe.timeout = STIMEOUT
319

    
320
    except Exception:
321
        raise errors.RadioError("Unknown error cleaning the serial buffer")
322

    
323

    
324
def _rawrecv(radio, amount):
325
    """Raw read from the radio device, less intensive way"""
326

    
327
    data = ""
328

    
329
    try:
330
        data = radio.pipe.read(amount)
331

    
332
        # DEBUG
333
        if debug is True:
334
            LOG.debug("<== (%d) bytes:\n\n%s" %
335
                      (len(data), util.hexprint(data)))
336

    
337
        # fail if no data is received
338
        if len(data) == 0:
339
            raise errors.RadioError("No data received from radio")
340

    
341
        # notice on the logs if short
342
        if len(data) < amount:
343
            LOG.warn("Short reading %d bytes from the %d requested." %
344
                     (len(data), amount))
345

    
346
    except:
347
        raise errors.RadioError("Error reading data from radio")
348

    
349
    return data
350

    
351

    
352
def _send(radio, data):
353
    """Send data to the radio device"""
354

    
355
    try:
356
        radio.pipe.write(data)
357

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

    
365

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

    
373
    return frame
374

    
375

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

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

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

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

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

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

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

    
403

    
404
def _do_ident(radio, status, upload=False):
405
    """Put the radio in PROGRAM mode & identify it"""
406
    #  set the serial discipline
407
    radio.pipe.baudrate = 9600
408
    radio.pipe.parity = "N"
409

    
410
    # lengthen the timeout here as these radios are reseting due to timeout
411
    radio.pipe.timeout = 0.75
412

    
413
    # send the magic word
414
    _send(radio, radio._magic)
415

    
416
    # Now you get a 50 byte reply if all goes well
417
    ident = _rawrecv(radio, 50)
418

    
419
    # checking for the ack
420
    if ident[0] != ACK_CMD:
421
        raise errors.RadioError("Bad ack from radio")
422

    
423
    # basic check for the ident block
424
    if len(ident) != 50:
425
        raise errors.RadioError("Radio send a short ident block.")
426

    
427
    # check if ident is OK
428
    itis = False
429
    for fp in radio._fileid:
430
        if fp in ident:
431
            # got it!
432
            itis = True
433
            # checking if we are dealing with a Gen 3 BTECH
434
            if radio.VENDOR == "BTECH" and fp in BTECH3:
435
                radio.btech3 = True
436

    
437
            break
438

    
439
    if itis is False:
440
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
441
        raise errors.RadioError("Radio identification failed.")
442

    
443
    # pause here for the radio to catch up
444
    sleep(0.1)
445

    
446
    # the OEM software reads this additional block, so we will, too
447

    
448
    # Get the full 21 bytes at a time to reduce load
449
    # 1 byte ACK + 4 bytes header + 16 bytes of data (BLOCK_SIZE)
450
    frame = _make_frame("S", 0x3DF0, 16)
451
    _send(radio, frame)
452
    id2 = _rawrecv(radio, 21)
453

    
454
    # restore the default serial timeout
455
    radio.pipe.timeout = STIMEOUT
456

    
457
    # checking for the ack
458
    if id2[0] not in "\x06\x05":
459
        raise errors.RadioError("Bad ack from radio")
460

    
461
    # basic check for the additional block
462
    if len(id2) < 21:
463
        raise errors.RadioError("The extra ID is short, aborting.")
464

    
465
    # this radios need a extra request/answer here on the upload
466
    # the amount of data received depends of the radio type
467
    #
468
    # also the first block of TX must no have the ACK at the beginning
469
    # see _upload for this.
470
    if upload is True:
471
        # send an ACK
472
        _send(radio, ACK_CMD)
473

    
474
        # the amount of data depend on the radio, so far we have two radios
475
        # reading two bytes with an ACK at the end and just ONE with just
476
        # one byte (QYT KT8900)
477
        # the JT-6188 appears a clone of the last, but reads TWO bytes.
478
        #
479
        # we will read two bytes with a custom timeout to not penalize the
480
        # users for this.
481
        #
482
        # we just check for a response and last byte being a ACK, that is
483
        # the common stone for all radios (3 so far)
484
        ack = _rawrecv(radio, 2)
485

    
486
        # checking
487
        if len(ack) == 0 or ack[-1:] != ACK_CMD:
488
            raise errors.RadioError("Radio didn't ACK the upload")
489

    
490
    # DEBUG
491
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
492

    
493
    return True
494

    
495

    
496
def _download(radio):
497
    """Get the memory map"""
498

    
499
    # UI progress
500
    status = chirp_common.Status()
501

    
502
    # put radio in program mode and identify it
503
    _do_ident(radio, status)
504

    
505
    # reset the progress bar in the UI
506
    status.max = MEM_SIZE / BLOCK_SIZE
507
    status.msg = "Cloning from radio..."
508
    status.cur = 0
509
    radio.status_fn(status)
510

    
511
    data = ""
512
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
513
        # sending the read request
514
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
515

    
516
        # read
517
        d = _recv(radio, addr)
518

    
519
        # aggregate the data
520
        data += d
521

    
522
        # UI Update
523
        status.cur = addr / BLOCK_SIZE
524
        status.msg = "Cloning from radio..."
525
        radio.status_fn(status)
526

    
527
    return data
528

    
529

    
530
def _upload(radio):
531
    """Upload procedure"""
532

    
533
    # The UPLOAD mem is restricted to lower than 0x3100,
534
    # so we will overide that here localy
535
    MEM_SIZE = radio.UPLOAD_MEM_SIZE
536

    
537
    # UI progress
538
    status = chirp_common.Status()
539

    
540
    # put radio in program mode and identify it
541
    _do_ident(radio, status, True)
542

    
543
    # get the data to upload to radio
544
    data = radio.get_mmap()
545

    
546
    # Reset the UI progress
547
    status.max = MEM_SIZE / TX_BLOCK_SIZE
548
    status.cur = 0
549
    status.msg = "Cloning to radio..."
550
    radio.status_fn(status)
551

    
552
    # the fun start here
553
    for addr in range(0, MEM_SIZE, TX_BLOCK_SIZE):
554
        # getting the block of data to send
555
        d = data[addr:addr + TX_BLOCK_SIZE]
556

    
557
        # build the frame to send
558
        frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
559

    
560
        # first block must not send the ACK at the beginning for the
561
        # ones that has the extra id, since this have to do a extra step
562
        if addr == 0:
563
            frame = frame[1:]
564

    
565
        # send the frame
566
        _send(radio, frame)
567

    
568
        # receiving the response
569
        ack = _rawrecv(radio, 1)
570

    
571
        # basic check
572
        if len(ack) != 1:
573
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
574

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

    
578
        # UI Update
579
        status.cur = addr / TX_BLOCK_SIZE
580
        status.msg = "Cloning to radio..."
581
        radio.status_fn(status)
582

    
583

    
584
def model_match(cls, data):
585
    """Match the opened/downloaded image to the correct version"""
586
    rid = data[0x3f70:0x3f76]
587

    
588
    if rid in cls._fileid:
589
        return True
590

    
591
    return False
592

    
593

    
594
def _decode_ranges(low, high):
595
    """Unpack the data in the ranges zones in the memmap and return
596
    a tuple with the integer corresponding to the Mhz it means"""
597
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
598
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
599
    ilow *= 1000000
600
    ihigh *= 1000000
601

    
602
    return (ilow, ihigh)
603

    
604

    
605
def _split(rf, f1, f2):
606
    """Returns False if the two freqs are in the same band (no split)
607
    or True otherwise"""
608

    
609
    # determine if the two freqs are in the same band
610
    for low, high in rf.valid_bands:
611
        if f1 >= low and f1 <= high and \
612
                f2 >= low and f2 <= high:
613
            # if the two freqs are on the same Band this is not a split
614
            return False
615

    
616
    # if you get here is because the freq pairs are split
617
    return True
618

    
619

    
620
class BTechMobileCommon(chirp_common.CloneModeRadio,
621
                        chirp_common.ExperimentalRadio):
622
    """BTECH's UV-5001 and alike radios"""
623
    VENDOR = "BTECH"
624
    MODEL = ""
625
    IDENT = ""
626
    BANDS = 2
627
    COLOR_LCD = False
628
    COLOR_LCD2 = False
629
    COLOR_LCD3 = False
630
    NAME_LENGTH = 6
631
    UPLOAD_MEM_SIZE = 0X3100
632
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
633
                     chirp_common.PowerLevel("Low", watts=10)]
634
    _vhf_range = (130000000, 180000000)
635
    _220_range = (200000000, 271000000)
636
    _uhf_range = (400000000, 521000000)
637
    _350_range = (350000000, 391000000)
638
    _upper = 199
639
    _magic = MSTRING
640
    _fileid = None
641
    _id2 = False
642
    btech3 = False
643
    _gmrs = False
644

    
645
    @classmethod
646
    def get_prompts(cls):
647
        rp = chirp_common.RadioPrompts()
648
        rp.experimental = \
649
            ('This driver is experimental.\n'
650
             '\n'
651
             'Please keep a copy of your memories with the original software '
652
             'if you treasure them, this driver is new and may contain'
653
             ' bugs.\n'
654
             '\n'
655
             )
656
        rp.pre_download = _(dedent("""\
657
            Follow these instructions to download your info:
658

    
659
            1 - Turn off your radio
660
            2 - Connect your interface cable
661
            3 - Turn on your radio
662
            4 - Do the download of your radio data
663

    
664
            """))
665
        rp.pre_upload = _(dedent("""\
666
            Follow these instructions to upload your info:
667

    
668
            1 - Turn off your radio
669
            2 - Connect your interface cable
670
            3 - Turn on your radio
671
            4 - Do the upload of your radio data
672

    
673
            """))
674
        return rp
675

    
676
    def get_features(self):
677
        """Get the radio's features"""
678

    
679
        # we will use the following var as global
680
        global POWER_LEVELS
681

    
682
        rf = chirp_common.RadioFeatures()
683
        rf.has_settings = True
684
        rf.has_bank = False
685
        rf.has_tuning_step = False
686
        rf.can_odd_split = True
687
        rf.has_name = True
688
        rf.has_offset = True
689
        rf.has_mode = True
690
        rf.has_dtcs = True
691
        rf.has_rx_dtcs = True
692
        rf.has_dtcs_polarity = True
693
        rf.has_ctone = True
694
        rf.has_cross = True
695
        rf.valid_modes = MODES
696
        rf.valid_characters = VALID_CHARS
697
        rf.valid_name_length = self.NAME_LENGTH
698
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
699
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
700
        rf.valid_cross_modes = [
701
            "Tone->Tone",
702
            "DTCS->",
703
            "->DTCS",
704
            "Tone->DTCS",
705
            "DTCS->Tone",
706
            "->Tone",
707
            "DTCS->DTCS"]
708
        rf.valid_skips = SKIP_VALUES
709
        rf.valid_dtcs_codes = DTCS
710
        rf.valid_tuning_steps = STEPS
711
        rf.memory_bounds = (0, self._upper)
712

    
713
        # power levels
714
        POWER_LEVELS = self._power_levels
715
        rf.valid_power_levels = POWER_LEVELS
716

    
717
        # normal dual bands
718
        rf.valid_bands = [self._vhf_range, self._uhf_range]
719

    
720
        # 220 band
721
        if self.BANDS == 3 or self.BANDS == 4:
722
            rf.valid_bands.append(self._220_range)
723

    
724
        # 350 band
725
        if self.BANDS == 4:
726
            rf.valid_bands.append(self._350_range)
727

    
728
        return rf
729

    
730
    def validate_memory(self, mem):
731
        msgs = chirp_common.CloneModeRadio.validate_memory(self, mem)
732

    
733
        _msg_duplex1 = 'Memory location only supports "Low"'
734
        _msg_duplex2 = 'Memory location only supports "off"'
735
        _msg_duplex3 = 'Memory location only supports "(None)", "+" or "off"'
736

    
737
        if self._gmrs:
738
            if mem.number < 1 or mem.number > 30:
739
                if float(mem.freq) / 1000000 in GMRS_FREQS1:
740
                    if mem.duplex not in ['', 'off']:
741
                        # warn user wrong Duplex
742
                        msgs.append(chirp_common.ValidationError(_msg_duplex2))
743
                    if mem.power != self._power_levels[2]:
744
                        # warn user wrong Duplex
745
                        msgs.append(chirp_common.ValidationError(_msg_duplex1))
746

    
747
                if float(mem.freq) / 1000000 in GMRS_FREQS2:
748
                    if mem.duplex not in ['off', ]:
749
                        # warn user wrong Duplex
750
                        msgs.append(chirp_common.ValidationError(_msg_duplex2))
751

    
752
                if float(mem.freq) / 1000000 in GMRS_FREQS3:
753
                    if mem.duplex not in ['', '+', 'off']:
754
                        # warn user wrong Duplex
755
                        msgs.append(chirp_common.ValidationError(_msg_duplex3))
756

    
757
        return msgs
758

    
759
    def sync_in(self):
760
        """Download from radio"""
761
        data = _download(self)
762
        self._mmap = memmap.MemoryMap(data)
763
        self.process_mmap()
764

    
765
    def sync_out(self):
766
        """Upload to radio"""
767
        try:
768
            _upload(self)
769
        except errors.RadioError:
770
            raise
771
        except Exception, e:
772
            raise errors.RadioError("Error: %s" % e)
773

    
774
    def get_raw_memory(self, number):
775
        return repr(self._memobj.memory[number])
776

    
777
    def _decode_tone(self, val):
778
        """Parse the tone data to decode from mem, it returns:
779
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
780
        pol = None
781

    
782
        if val in [0, 65535]:
783
            return '', None, None
784
        elif val > 0x0258:
785
            a = val / 10.0
786
            return 'Tone', a, pol
787
        else:
788
            if val > 0x69:
789
                index = val - 0x6A
790
                pol = "R"
791
            else:
792
                index = val - 1
793
                pol = "N"
794

    
795
            tone = DTCS[index]
796
            return 'DTCS', tone, pol
797

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

    
822
    def get_memory(self, number):
823
        """Get the mem representation from the radio image"""
824
        _mem = self._memobj.memory[number]
825
        _names = self._memobj.names[number]
826

    
827
        # Create a high-level memory object to return to the UI
828
        mem = chirp_common.Memory()
829

    
830
        # Memory number
831
        mem.number = number
832

    
833
        if _mem.get_raw()[0] == "\xFF":
834
            mem.empty = True
835
            return mem
836

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

    
861
        # name TAG of the channel
862
        mem.name = str(_names.name).rstrip("\xFF").replace("\xFF", " ")
863

    
864
        # power
865
        mem.power = POWER_LEVELS[int(_mem.power)]
866

    
867
        # wide/narrow
868
        mem.mode = MODES[int(_mem.wide)]
869

    
870
        # skip
871
        mem.skip = SKIP_VALUES[_mem.add]
872

    
873
        # tone data
874
        rxtone = txtone = None
875
        txtone = self._decode_tone(_mem.txtone)
876
        rxtone = self._decode_tone(_mem.rxtone)
877
        chirp_common.split_tone_decode(mem, txtone, rxtone)
878

    
879
        # Extra
880
        mem.extra = RadioSettingGroup("extra", "Extra")
881

    
882
        if not self.COLOR_LCD or \
883
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
884
            scramble = RadioSetting("scramble", "Scramble",
885
                                    RadioSettingValueBoolean(bool(
886
                                        _mem.scramble)))
887
            mem.extra.append(scramble)
888

    
889
        bcl = RadioSetting("bcl", "Busy channel lockout",
890
                           RadioSettingValueBoolean(bool(_mem.bcl)))
891
        mem.extra.append(bcl)
892

    
893
        pttid = RadioSetting("pttid", "PTT ID",
894
                             RadioSettingValueList(PTTID_LIST,
895
                                                   PTTID_LIST[_mem.pttid]))
896
        mem.extra.append(pttid)
897

    
898
        # validating scode
899
        scode = _mem.scode if _mem.scode != 15 else 0
900
        pttidcode = RadioSetting("scode", "PTT ID signal code",
901
                                 RadioSettingValueList(
902
                                     PTTIDCODE_LIST,
903
                                     PTTIDCODE_LIST[scode]))
904
        mem.extra.append(pttidcode)
905

    
906
        optsig = RadioSetting("optsig", "Optional signaling",
907
                              RadioSettingValueList(
908
                                  OPTSIG_LIST,
909
                                  OPTSIG_LIST[_mem.optsig]))
910
        mem.extra.append(optsig)
911

    
912
        spmute = RadioSetting("spmute", "Speaker mute",
913
                              RadioSettingValueList(
914
                                  SPMUTE_LIST,
915
                                  SPMUTE_LIST[_mem.spmute]))
916
        mem.extra.append(spmute)
917

    
918
        return mem
919

    
920
    def set_memory(self, mem):
921
        """Set the memory data in the eeprom img from the UI"""
922
        # get the eprom representation of this channel
923
        _mem = self._memobj.memory[mem.number]
924
        _names = self._memobj.names[mem.number]
925

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

    
933
        # if empty memmory
934
        if mem.empty:
935
            # the channel itself
936
            _mem.set_raw("\xFF" * 16)
937
            # the name tag
938
            _names.set_raw("\xFF" * 16)
939
            return
940

    
941
        if mem_was_empty:
942
            # Zero the whole memory if we're making it unempty for
943
            # the first time
944
            LOG.debug('Zeroing new memory')
945
            _mem.set_raw('\x00' * 16)
946

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

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

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

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

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

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

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

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

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

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

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

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

    
1060
        return mem
1061

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1525
        if self.MODEL == "DB25-G":
1526
            mgain = RadioSetting("settings.mgain", "Auto power-on",
1527
                                 RadioSettingValueBoolean(_mem.settings.mgain))
1528
            basic.append(mgain)
1529

    
1530
        if str(_mem.fingerprint.fp) in BTECH3:
1531
            mgain = RadioSetting("settings.mgain", "Mic gain",
1532
                                 RadioSettingValueInteger(0, 120,
1533
                                                          _mem.settings.mgain))
1534
            basic.append(mgain)
1535

    
1536
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1537
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain",
1538
                                 RadioSettingValueInteger(0, 60,
1539
                                                          _mem.settings.dtmfg))
1540
            basic.append(dtmfg)
1541

    
1542
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1543
            mgain = RadioSetting("settings.mgain", "Mic gain",
1544
                                 RadioSettingValueInteger(0, 120,
1545
                                                          _mem.settings.mgain))
1546
            basic.append(mgain)
1547

    
1548
            skiptx = RadioSetting("settings.skiptx", "Skip TX",
1549
                                  RadioSettingValueList(
1550
                                      LIST_SKIPTX,
1551
                                      LIST_SKIPTX[_mem.settings.skiptx]))
1552
            basic.append(skiptx)
1553

    
1554
            scmode = RadioSetting("settings.scmode", "Scan mode",
1555
                                  RadioSettingValueList(
1556
                                      LIST_SCMODE,
1557
                                      LIST_SCMODE[_mem.settings.scmode]))
1558
            basic.append(scmode)
1559

    
1560
        if self.MODEL == "KT-8R":
1561
            tmrtx = RadioSetting("settings.tmrtx", "TX in multi-standby",
1562
                                 RadioSettingValueList(
1563
                                     LIST_TMRTX,
1564
                                     LIST_TMRTX[_mem.settings.tmrtx]))
1565
            basic.append(tmrtx)
1566

    
1567
        # Advanced
1568
        def _filter(name):
1569
            filtered = ""
1570
            for char in str(name):
1571
                if char in VALID_CHARS:
1572
                    filtered += char
1573
                else:
1574
                    filtered += " "
1575
            return filtered
1576

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

    
1637
        if self.MODEL in ("UV-2501", "UV-5001"):
1638
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1639
                                   RadioSettingValueBoolean(
1640
                                       _mem.settings2.vfomren))
1641
            advanced.append(vfomren)
1642

    
1643
            reseten = RadioSetting("settings2.reseten", "RESET",
1644
                                   RadioSettingValueBoolean(
1645
                                       _mem.settings2.reseten))
1646
            advanced.append(reseten)
1647

    
1648
            menuen = RadioSetting("settings2.menuen", "Menu",
1649
                                  RadioSettingValueBoolean(
1650
                                      _mem.settings2.menuen))
1651
            advanced.append(menuen)
1652

    
1653
        # Other
1654
        def convert_bytes_to_limit(bytes):
1655
            limit = ""
1656
            for byte in bytes:
1657
                if byte < 10:
1658
                    limit += chr(byte + 0x30)
1659
                else:
1660
                    break
1661
            return limit
1662

    
1663
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1664
            _ranges = self._memobj.ranges220
1665
            ranges = "ranges220"
1666
        else:
1667
            _ranges = self._memobj.ranges
1668
            ranges = "ranges"
1669

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

    
1676
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1677
        val = RadioSettingValueString(0, 3, _limit)
1678
        val.set_mutable(False)
1679
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1680
        other.append(vhf_high)
1681

    
1682
        if self.BANDS == 3 or self.BANDS == 4:
1683
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1684
            val = RadioSettingValueString(0, 3, _limit)
1685
            val.set_mutable(False)
1686
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1687
            other.append(vhf2_low)
1688

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

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

    
1701
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1702
        val = RadioSettingValueString(0, 3, _limit)
1703
        val.set_mutable(False)
1704
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1705
        other.append(uhf_high)
1706

    
1707
        if self.BANDS == 4:
1708
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1709
            val = RadioSettingValueString(0, 3, _limit)
1710
            val.set_mutable(False)
1711
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1712
            other.append(uhf2_low)
1713

    
1714
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1715
            val = RadioSettingValueString(0, 3, _limit)
1716
            val.set_mutable(False)
1717
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1718
            other.append(uhf2_high)
1719

    
1720
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1721
        val.set_mutable(False)
1722
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1723
        other.append(fp)
1724

    
1725
        # Work
1726
        if self.COLOR_LCD:
1727
            dispab = RadioSetting("settings2.dispab", "Display",
1728
                                  RadioSettingValueList(
1729
                                      LIST_ABCD,
1730
                                      LIST_ABCD[_mem.settings2.dispab]))
1731
            work.append(dispab)
1732
        else:
1733
            dispab = RadioSetting("settings2.dispab", "Display",
1734
                                  RadioSettingValueList(
1735
                                      LIST_AB,
1736
                                      LIST_AB[_mem.settings2.dispab]))
1737
            work.append(dispab)
1738

    
1739
        if self.COLOR_LCD:
1740
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode",
1741
                                  RadioSettingValueList(
1742
                                      LIST_VFOMR,
1743
                                      LIST_VFOMR[_mem.settings2.vfomra]))
1744
            work.append(vfomra)
1745

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

    
1752
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode",
1753
                                  RadioSettingValueList(
1754
                                      LIST_VFOMR,
1755
                                      LIST_VFOMR[_mem.settings2.vfomrc]))
1756
            work.append(vfomrc)
1757

    
1758
            vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode",
1759
                                  RadioSettingValueList(
1760
                                      LIST_VFOMR,
1761
                                      LIST_VFOMR[_mem.settings2.vfomrd]))
1762
            work.append(vfomrd)
1763
        else:
1764
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode",
1765
                                 RadioSettingValueList(
1766
                                     LIST_VFOMR,
1767
                                     LIST_VFOMR[_mem.settings2.vfomr]))
1768
            work.append(vfomr)
1769

    
1770
        keylock = RadioSetting("settings2.keylock", "Keypad lock",
1771
                               RadioSettingValueBoolean(
1772
                                   _mem.settings2.keylock))
1773
        work.append(keylock)
1774

    
1775
        mrcha = RadioSetting("settings2.mrcha", "MR A channel",
1776
                             RadioSettingValueInteger(0, self._upper,
1777
                                                      _mem.settings2.mrcha))
1778
        work.append(mrcha)
1779

    
1780
        mrchb = RadioSetting("settings2.mrchb", "MR B channel",
1781
                             RadioSettingValueInteger(0, self._upper,
1782
                                                      _mem.settings2.mrchb))
1783
        work.append(mrchb)
1784

    
1785
        if self.COLOR_LCD:
1786
            mrchc = RadioSetting("settings2.mrchc", "MR C channel",
1787
                                 RadioSettingValueInteger(
1788
                                     0, self._upper, _mem.settings2.mrchc))
1789
            work.append(mrchc)
1790

    
1791
            mrchd = RadioSetting("settings2.mrchd", "MR D channel",
1792
                                 RadioSettingValueInteger(
1793
                                     0, self._upper, _mem.settings2.mrchd))
1794
            work.append(mrchd)
1795

    
1796
        def convert_bytes_to_freq(bytes):
1797
            real_freq = 0
1798
            for byte in bytes:
1799
                real_freq = (real_freq * 10) + byte
1800
            return chirp_common.format_freq(real_freq * 10)
1801

    
1802
        def my_validate(value):
1803
            _vhf_lower = int(convert_bytes_to_limit(_ranges.vhf_low))
1804
            _vhf_upper = int(convert_bytes_to_limit(_ranges.vhf_high))
1805
            _uhf_lower = int(convert_bytes_to_limit(_ranges.uhf_low))
1806
            _uhf_upper = int(convert_bytes_to_limit(_ranges.uhf_high))
1807
            if self.BANDS == 3 or self.BANDS == 4:
1808
                _vhf2_lower = int(convert_bytes_to_limit(_ranges.vhf2_low))
1809
                _vhf2_upper = int(convert_bytes_to_limit(_ranges.vhf2_high))
1810
            if self.BANDS == 4:
1811
                _uhf2_lower = int(convert_bytes_to_limit(_ranges.uhf2_low))
1812
                _uhf2_upper = int(convert_bytes_to_limit(_ranges.uhf2_high))
1813

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

    
1845
        def apply_freq(setting, obj):
1846
            value = chirp_common.parse_freq(str(setting.value)) / 10
1847
            for i in range(7, -1, -1):
1848
                obj.freq[i] = value % 10
1849
                value /= 10
1850

    
1851
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1852
                                        _mem.vfo.a.freq))
1853
        val1a.set_validate_callback(my_validate)
1854
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
1855
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
1856
        work.append(vfoafreq)
1857

    
1858
        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1859
                                        _mem.vfo.b.freq))
1860
        val1b.set_validate_callback(my_validate)
1861
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
1862
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
1863
        work.append(vfobfreq)
1864

    
1865
        if self.COLOR_LCD:
1866
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1867
                                            _mem.vfo.c.freq))
1868
            val1c.set_validate_callback(my_validate)
1869
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
1870
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
1871
            work.append(vfocfreq)
1872

    
1873
            val1d = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1874
                                            _mem.vfo.d.freq))
1875
            val1d.set_validate_callback(my_validate)
1876
            vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
1877
            vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
1878
            work.append(vfodfreq)
1879

    
1880
        if not self.MODEL == "GMRS-50X1":
1881
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift",
1882
                                      RadioSettingValueList(
1883
                                          LIST_SHIFT,
1884
                                          LIST_SHIFT[_mem.vfo.a.shiftd]))
1885
            work.append(vfoashiftd)
1886

    
1887
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift",
1888
                                      RadioSettingValueList(
1889
                                          LIST_SHIFT,
1890
                                          LIST_SHIFT[_mem.vfo.b.shiftd]))
1891
            work.append(vfobshiftd)
1892

    
1893
            if self.COLOR_LCD:
1894
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift",
1895
                                          RadioSettingValueList(
1896
                                              LIST_SHIFT,
1897
                                              LIST_SHIFT[_mem.vfo.c.shiftd]))
1898
                work.append(vfocshiftd)
1899

    
1900
                vfodshiftd = RadioSetting("vfo.d.shiftd", "VFO D shift",
1901
                                          RadioSettingValueList(
1902
                                              LIST_SHIFT,
1903
                                              LIST_SHIFT[_mem.vfo.d.shiftd]))
1904
                work.append(vfodshiftd)
1905

    
1906
        def convert_bytes_to_offset(bytes):
1907
            real_offset = 0
1908
            for byte in bytes:
1909
                real_offset = (real_offset * 10) + byte
1910
            return chirp_common.format_freq(real_offset * 1000)
1911

    
1912
        def apply_offset(setting, obj):
1913
            value = chirp_common.parse_freq(str(setting.value)) / 1000
1914
            for i in range(5, -1, -1):
1915
                obj.offset[i] = value % 10
1916
                value /= 10
1917

    
1918
        if not self.MODEL == "GMRS-50X1":
1919
            if self.COLOR_LCD:
1920
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1921
                                                _mem.vfo.a.offset))
1922
                vfoaoffset = RadioSetting("vfo.a.offset",
1923
                                          "VFO A offset (0.000-999.999)",
1924
                                          val1a)
1925
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
1926
                work.append(vfoaoffset)
1927

    
1928
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1929
                                                _mem.vfo.b.offset))
1930
                vfoboffset = RadioSetting("vfo.b.offset",
1931
                                          "VFO B offset (0.000-999.999)",
1932
                                          val1b)
1933
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1934
                work.append(vfoboffset)
1935

    
1936
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1937
                                                _mem.vfo.c.offset))
1938
                vfocoffset = RadioSetting("vfo.c.offset",
1939
                                          "VFO C offset (0.000-999.999)",
1940
                                          val1c)
1941
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
1942
                work.append(vfocoffset)
1943

    
1944
                val1d = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1945
                                                _mem.vfo.d.offset))
1946
                vfodoffset = RadioSetting("vfo.d.offset",
1947
                                          "VFO D offset (0.000-999.999)",
1948
                                          val1d)
1949
                vfodoffset.set_apply_callback(apply_offset, _mem.vfo.d)
1950
                work.append(vfodoffset)
1951
            else:
1952
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1953
                                                _mem.vfo.a.offset))
1954
                vfoaoffset = RadioSetting("vfo.a.offset",
1955
                                          "VFO A offset (0.000-99.999)", val1a)
1956
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
1957
                work.append(vfoaoffset)
1958

    
1959
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1960
                                                _mem.vfo.b.offset))
1961
                vfoboffset = RadioSetting("vfo.b.offset",
1962
                                          "VFO B offset (0.000-99.999)", val1b)
1963
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1964
                work.append(vfoboffset)
1965

    
1966
        if not self.MODEL == "GMRS-50X1":
1967
            vfoatxp = RadioSetting("vfo.a.power", "VFO A power",
1968
                                   RadioSettingValueList(
1969
                                       LIST_TXP,
1970
                                       LIST_TXP[_mem.vfo.a.power]))
1971
            work.append(vfoatxp)
1972

    
1973
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power",
1974
                                   RadioSettingValueList(
1975
                                       LIST_TXP,
1976
                                       LIST_TXP[_mem.vfo.b.power]))
1977
            work.append(vfobtxp)
1978

    
1979
            if self.COLOR_LCD:
1980
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power",
1981
                                       RadioSettingValueList(
1982
                                           LIST_TXP,
1983
                                           LIST_TXP[_mem.vfo.c.power]))
1984
                work.append(vfoctxp)
1985

    
1986
                vfodtxp = RadioSetting("vfo.d.power", "VFO D power",
1987
                                       RadioSettingValueList(
1988
                                           LIST_TXP,
1989
                                           LIST_TXP[_mem.vfo.d.power]))
1990
                work.append(vfodtxp)
1991

    
1992
        if not self.MODEL == "GMRS-50X1":
1993
            vfoawide = RadioSetting("vfo.a.wide", "VFO A bandwidth",
1994
                                    RadioSettingValueList(
1995
                                        LIST_WIDE,
1996
                                        LIST_WIDE[_mem.vfo.a.wide]))
1997
            work.append(vfoawide)
1998

    
1999
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth",
2000
                                    RadioSettingValueList(
2001
                                        LIST_WIDE,
2002
                                        LIST_WIDE[_mem.vfo.b.wide]))
2003
            work.append(vfobwide)
2004

    
2005
            if self.COLOR_LCD:
2006
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth",
2007
                                        RadioSettingValueList(
2008
                                            LIST_WIDE,
2009
                                            LIST_WIDE[_mem.vfo.c.wide]))
2010
                work.append(vfocwide)
2011

    
2012
                vfodwide = RadioSetting("vfo.d.wide", "VFO D bandwidth",
2013
                                        RadioSettingValueList(
2014
                                            LIST_WIDE,
2015
                                            LIST_WIDE[_mem.vfo.d.wide]))
2016
                work.append(vfodwide)
2017

    
2018
        vfoastep = RadioSetting("vfo.a.step", "VFO A step",
2019
                                RadioSettingValueList(
2020
                                    LIST_STEP,
2021
                                    LIST_STEP[_mem.vfo.a.step]))
2022
        work.append(vfoastep)
2023

    
2024
        vfobstep = RadioSetting("vfo.b.step", "VFO B step",
2025
                                RadioSettingValueList(
2026
                                    LIST_STEP,
2027
                                    LIST_STEP[_mem.vfo.b.step]))
2028
        work.append(vfobstep)
2029

    
2030
        if self.COLOR_LCD:
2031
            vfocstep = RadioSetting("vfo.c.step", "VFO C step",
2032
                                    RadioSettingValueList(
2033
                                        LIST_STEP,
2034
                                        LIST_STEP[_mem.vfo.c.step]))
2035
            work.append(vfocstep)
2036

    
2037
            vfodstep = RadioSetting("vfo.d.step", "VFO D step",
2038
                                    RadioSettingValueList(
2039
                                        LIST_STEP,
2040
                                        LIST_STEP[_mem.vfo.d.step]))
2041
            work.append(vfodstep)
2042

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

    
2049
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal",
2050
                                  RadioSettingValueList(
2051
                                      OPTSIG_LIST,
2052
                                      OPTSIG_LIST[_mem.vfo.b.optsig]))
2053
        work.append(vfoboptsig)
2054

    
2055
        if self.COLOR_LCD:
2056
            vfocoptsig = RadioSetting("vfo.c.optsig", "VFO C optional signal",
2057
                                      RadioSettingValueList(
2058
                                          OPTSIG_LIST,
2059
                                          OPTSIG_LIST[_mem.vfo.c.optsig]))
2060
            work.append(vfocoptsig)
2061

    
2062
            vfodoptsig = RadioSetting("vfo.d.optsig", "VFO D optional signal",
2063
                                      RadioSettingValueList(
2064
                                          OPTSIG_LIST,
2065
                                          OPTSIG_LIST[_mem.vfo.d.optsig]))
2066
            work.append(vfodoptsig)
2067

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

    
2074
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute",
2075
                                  RadioSettingValueList(
2076
                                      SPMUTE_LIST,
2077
                                      SPMUTE_LIST[_mem.vfo.b.spmute]))
2078
        work.append(vfobspmute)
2079

    
2080
        if self.COLOR_LCD:
2081
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute",
2082
                                      RadioSettingValueList(
2083
                                          SPMUTE_LIST,
2084
                                          SPMUTE_LIST[_mem.vfo.c.spmute]))
2085
            work.append(vfocspmute)
2086

    
2087
            vfodspmute = RadioSetting("vfo.d.spmute", "VFO D speaker mute",
2088
                                      RadioSettingValueList(
2089
                                          SPMUTE_LIST,
2090
                                          SPMUTE_LIST[_mem.vfo.d.spmute]))
2091
            work.append(vfodspmute)
2092

    
2093
        if not self.COLOR_LCD or \
2094
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2095
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble",
2096
                                   RadioSettingValueBoolean(
2097
                                       _mem.vfo.a.scramble))
2098
            work.append(vfoascr)
2099

    
2100
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble",
2101
                                   RadioSettingValueBoolean(
2102
                                       _mem.vfo.b.scramble))
2103
            work.append(vfobscr)
2104

    
2105
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2106
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble",
2107
                                   RadioSettingValueBoolean(
2108
                                       _mem.vfo.c.scramble))
2109
            work.append(vfocscr)
2110

    
2111
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble",
2112
                                   RadioSettingValueBoolean(
2113
                                       _mem.vfo.d.scramble))
2114
            work.append(vfodscr)
2115

    
2116
        if not self.MODEL == "GMRS-50X1":
2117
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID",
2118
                                     RadioSettingValueList(
2119
                                         PTTIDCODE_LIST,
2120
                                         PTTIDCODE_LIST[_mem.vfo.a.scode]))
2121
            work.append(vfoascode)
2122

    
2123
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID",
2124
                                     RadioSettingValueList(
2125
                                         PTTIDCODE_LIST,
2126
                                         PTTIDCODE_LIST[_mem.vfo.b.scode]))
2127
            work.append(vfobscode)
2128

    
2129
            if self.COLOR_LCD:
2130
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID",
2131
                                         RadioSettingValueList(
2132
                                             PTTIDCODE_LIST,
2133
                                             PTTIDCODE_LIST[_mem.vfo.c.scode]))
2134
                work.append(vfocscode)
2135

    
2136
                vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID",
2137
                                         RadioSettingValueList(
2138
                                             PTTIDCODE_LIST,
2139
                                             PTTIDCODE_LIST[_mem.vfo.d.scode]))
2140
                work.append(vfodscode)
2141

    
2142
        if not self.MODEL == "GMRS-50X1":
2143
            pttid = RadioSetting("settings.pttid", "PTT ID",
2144
                                 RadioSettingValueList(
2145
                                     PTTID_LIST,
2146
                                     PTTID_LIST[_mem.settings.pttid]))
2147
            work.append(pttid)
2148

    
2149
        if not self.COLOR_LCD:
2150
            # FM presets
2151
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2152
            top.append(fm_presets)
2153

    
2154
            def fm_validate(value):
2155
                if value == 0:
2156
                    return chirp_common.format_freq(value)
2157
                if not (87.5 <= value and value <= 108.0):  # 87.5-108MHz
2158
                    msg = ("FM-Preset-Frequency: " +
2159
                           "Must be between 87.5 and 108 MHz")
2160
                    raise InvalidValueError(msg)
2161
                return value
2162

    
2163
            def apply_fm_preset_name(setting, obj):
2164
                valstring = str(setting.value)
2165
                for i in range(0, 6):
2166
                    if valstring[i] in VALID_CHARS:
2167
                        obj[i] = valstring[i]
2168
                    else:
2169
                        obj[i] = '0xff'
2170

    
2171
            def apply_fm_freq(setting, obj):
2172
                value = chirp_common.parse_freq(str(setting.value)) / 10
2173
                for i in range(7, -1, -1):
2174
                    obj.freq[i] = value % 10
2175
                    value /= 10
2176

    
2177
            _presets = self._memobj.fm_radio_preset
2178
            i = 1
2179
            for preset in _presets:
2180
                line = RadioSetting("fm_presets_" + str(i),
2181
                                    "Station name " + str(i),
2182
                                    RadioSettingValueString(0, 6, _filter(
2183
                                        preset.broadcast_station_name)))
2184
                line.set_apply_callback(apply_fm_preset_name,
2185
                                        preset.broadcast_station_name)
2186

    
2187
                val = RadioSettingValueFloat(0, 108,
2188
                                             convert_bytes_to_freq(
2189
                                                 preset.freq))
2190
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2191
                                      "Frequency " + str(i), val)
2192
                val.set_validate_callback(fm_validate)
2193
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2194
                fm_presets.append(line)
2195
                fm_presets.append(fmfreq)
2196

    
2197
                i = i + 1
2198

    
2199
        # DTMF-Setting
2200
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2201
                                              "DTMF Encoding Settings")
2202
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2203
                                              "DTMF Decoding Settings")
2204
        top.append(dtmf_enc_settings)
2205
        top.append(dtmf_dec_settings)
2206
        txdisable = RadioSetting("dtmf_settings.txdisable",
2207
                                 "TX-Disable",
2208
                                 RadioSettingValueBoolean(
2209
                                     _mem.dtmf_settings.txdisable))
2210
        dtmf_enc_settings.append(txdisable)
2211

    
2212
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2213
                                 "RX-Disable",
2214
                                 RadioSettingValueBoolean(
2215
                                     _mem.dtmf_settings.rxdisable))
2216
        dtmf_enc_settings.append(rxdisable)
2217

    
2218
        if _mem.dtmf_settings.dtmfspeed_on > 0x0F:
2219
            val = 0x03
2220
        else:
2221
            val = _mem.dtmf_settings.dtmfspeed_on
2222
        dtmfspeed_on = RadioSetting(
2223
            "dtmf_settings.dtmfspeed_on",
2224
            "DTMF Speed (On Time)",
2225
            RadioSettingValueList(LIST_DTMF_SPEED,
2226
                                  LIST_DTMF_SPEED[
2227
                                      val]))
2228
        dtmf_enc_settings.append(dtmfspeed_on)
2229

    
2230
        if _mem.dtmf_settings.dtmfspeed_off > 0x0F:
2231
            val = 0x03
2232
        else:
2233
            val = _mem.dtmf_settings.dtmfspeed_off
2234
        dtmfspeed_off = RadioSetting(
2235
            "dtmf_settings.dtmfspeed_off",
2236
            "DTMF Speed (Off Time)",
2237
            RadioSettingValueList(LIST_DTMF_SPEED,
2238
                                  LIST_DTMF_SPEED[
2239
                                      val]))
2240
        dtmf_enc_settings.append(dtmfspeed_off)
2241

    
2242
        def memory2string(dmtf_mem):
2243
            dtmf_string = ""
2244
            for digit in dmtf_mem:
2245
                if digit != 255:
2246
                    index = LIST_DTMF_VALUES.index(digit)
2247
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2248
            return dtmf_string
2249

    
2250
        def apply_dmtf_frame(setting, obj):
2251
            LOG.debug("Setting DTMF-Code: " + str(setting.value))
2252
            val_string = str(setting.value)
2253
            for i in range(0, 16):
2254
                obj[i] = 255
2255
            i = 0
2256
            for current_char in val_string:
2257
                current_char = current_char.upper()
2258
                index = LIST_DTMF_DIGITS.index(current_char)
2259
                obj[i] = LIST_DTMF_VALUES[index]
2260
                i = i + 1
2261

    
2262
        codes = self._memobj.dtmf_codes
2263
        i = 1
2264
        for dtmfcode in codes:
2265
            val = RadioSettingValueString(0, 16, memory2string(
2266
                                              dtmfcode.code),
2267
                                          False, CHARSET_DTMF_DIGITS)
2268
            line = RadioSetting("dtmf_code_" + str(i) + "_code",
2269
                                "DMTF Code " + str(i), val)
2270
            line.set_apply_callback(apply_dmtf_frame, dtmfcode.code)
2271
            dtmf_enc_settings.append(line)
2272
            i = i + 1
2273

    
2274
        line = RadioSetting("dtmf_settings.mastervice",
2275
                            "Master and Vice ID",
2276
                            RadioSettingValueBoolean(
2277
                                _mem.dtmf_settings.mastervice))
2278
        dtmf_dec_settings.append(line)
2279

    
2280
        val = RadioSettingValueString(0, 16, memory2string(
2281
                                          _mem.dtmf_settings.masterid),
2282
                                      False, CHARSET_DTMF_DIGITS)
2283
        line = RadioSetting("dtmf_settings.masterid",
2284
                            "Master Control ID ", val)
2285
        line.set_apply_callback(apply_dmtf_frame,
2286
                                _mem.dtmf_settings.masterid)
2287
        dtmf_dec_settings.append(line)
2288

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

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

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

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

    
2313
        line = RadioSetting("dtmf_settings.mrevive",
2314
                            "Master Revive",
2315
                            RadioSettingValueBoolean(
2316
                                _mem.dtmf_settings.mrevive))
2317
        dtmf_dec_settings.append(line)
2318

    
2319
        val = RadioSettingValueString(0, 16, memory2string(
2320
                                          _mem.dtmf_settings.viceid),
2321
                                      False, CHARSET_DTMF_DIGITS)
2322
        line = RadioSetting("dtmf_settings.viceid",
2323
                            "Vice Control ID ", val)
2324
        line.set_apply_callback(apply_dmtf_frame,
2325
                                _mem.dtmf_settings.viceid)
2326
        dtmf_dec_settings.append(line)
2327

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

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

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

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

    
2352
        line = RadioSetting("dtmf_settings.vrevive",
2353
                            "Vice Revive",
2354
                            RadioSettingValueBoolean(
2355
                                _mem.dtmf_settings.vrevive))
2356
        dtmf_dec_settings.append(line)
2357

    
2358
        val = RadioSettingValueString(0, 16, memory2string(
2359
                                          _mem.dtmf_settings.inspection),
2360
                                      False, CHARSET_DTMF_DIGITS)
2361
        line = RadioSetting("dtmf_settings.inspection",
2362
                            "Inspection", val)
2363
        line.set_apply_callback(apply_dmtf_frame,
2364
                                _mem.dtmf_settings.inspection)
2365
        dtmf_dec_settings.append(line)
2366

    
2367
        val = RadioSettingValueString(0, 16, memory2string(
2368
                                          _mem.dtmf_settings.alarmcode),
2369
                                      False, CHARSET_DTMF_DIGITS)
2370
        line = RadioSetting("dtmf_settings.alarmcode",
2371
                            "Alarm", val)
2372
        line.set_apply_callback(apply_dmtf_frame,
2373
                                _mem.dtmf_settings.alarmcode)
2374
        dtmf_dec_settings.append(line)
2375

    
2376
        val = RadioSettingValueString(0, 16, memory2string(
2377
                                          _mem.dtmf_settings.kill),
2378
                                      False, CHARSET_DTMF_DIGITS)
2379
        line = RadioSetting("dtmf_settings.kill",
2380
                            "Kill", val)
2381
        line.set_apply_callback(apply_dmtf_frame,
2382
                                _mem.dtmf_settings.kill)
2383
        dtmf_dec_settings.append(line)
2384

    
2385
        val = RadioSettingValueString(0, 16, memory2string(
2386
                                          _mem.dtmf_settings.monitor),
2387
                                      False, CHARSET_DTMF_DIGITS)
2388
        line = RadioSetting("dtmf_settings.monitor",
2389
                            "Monitor", val)
2390
        line.set_apply_callback(apply_dmtf_frame,
2391
                                _mem.dtmf_settings.monitor)
2392
        dtmf_dec_settings.append(line)
2393

    
2394
        val = RadioSettingValueString(0, 16, memory2string(
2395
                                          _mem.dtmf_settings.stun),
2396
                                      False, CHARSET_DTMF_DIGITS)
2397
        line = RadioSetting("dtmf_settings.stun",
2398
                            "Stun", val)
2399
        line.set_apply_callback(apply_dmtf_frame,
2400
                                _mem.dtmf_settings.stun)
2401
        dtmf_dec_settings.append(line)
2402

    
2403
        val = RadioSettingValueString(0, 16, memory2string(
2404
                                          _mem.dtmf_settings.revive),
2405
                                      False, CHARSET_DTMF_DIGITS)
2406
        line = RadioSetting("dtmf_settings.revive",
2407
                            "Revive", val)
2408
        line.set_apply_callback(apply_dmtf_frame,
2409
                                _mem.dtmf_settings.revive)
2410
        dtmf_dec_settings.append(line)
2411

    
2412
        def apply_dmtf_listvalue(setting, obj):
2413
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2414
            val = str(setting.value)
2415
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2416
            val = LIST_DTMF_SPECIAL_VALUES[index]
2417
            obj.set_value(val)
2418

    
2419
        if _mem.dtmf_settings.groupcode not in LIST_DTMF_SPECIAL_VALUES:
2420
            val = 0x0B
2421
        else:
2422
            val = _mem.dtmf_settings.groupcode
2423
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2424
        line = RadioSetting(
2425
            "dtmf_settings.groupcode",
2426
            "Group Code",
2427
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2428
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2429
        line.set_apply_callback(apply_dmtf_listvalue,
2430
                                _mem.dtmf_settings.groupcode)
2431
        dtmf_dec_settings.append(line)
2432

    
2433
        if _mem.dtmf_settings.spacecode not in LIST_DTMF_SPECIAL_VALUES:
2434
            val = 0x0C
2435
        else:
2436
            val = _mem.dtmf_settings.spacecode
2437
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2438
        line = RadioSetting(
2439
            "dtmf_settings.spacecode",
2440
            "Space Code",
2441
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2442
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2443
        line.set_apply_callback(apply_dmtf_listvalue,
2444
                                _mem.dtmf_settings.spacecode)
2445
        dtmf_dec_settings.append(line)
2446

    
2447
        if self.COLOR_LCD:
2448
            if _mem.dtmf_settings.resettime > 0x63:
2449
                val = 0x4F
2450
            else:
2451
                val = _mem.dtmf_settings.resettime
2452
            line = RadioSetting(
2453
                "dtmf_settings.resettime",
2454
                "Reset time",
2455
                RadioSettingValueList(LIST_5TONE_RESET_COLOR,
2456
                                      LIST_5TONE_RESET_COLOR[
2457
                                          val]))
2458
            dtmf_dec_settings.append(line)
2459
        else:
2460
            line = RadioSetting(
2461
                "dtmf_settings.resettime",
2462
                "Reset time",
2463
                RadioSettingValueList(LIST_5TONE_RESET,
2464
                                      LIST_5TONE_RESET[
2465
                                          _mem.dtmf_settings.resettime]))
2466
            dtmf_dec_settings.append(line)
2467

    
2468
        if _mem.dtmf_settings.delayproctime > 0x27:
2469
            val = 0x04
2470
        else:
2471
            val = _mem.dtmf_settings.delayproctime
2472
        line = RadioSetting(
2473
            "dtmf_settings.delayproctime",
2474
            "Delay processing time",
2475
            RadioSettingValueList(LIST_DTMF_DELAY,
2476
                                  LIST_DTMF_DELAY[
2477
                                      val]))
2478
        dtmf_dec_settings.append(line)
2479

    
2480
        # 5 Tone Settings
2481
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2482
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2483

    
2484
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2485
        group_5tone.append(stds_5tone)
2486
        group_5tone.append(codes_5tone)
2487

    
2488
        top.append(group_5tone)
2489

    
2490
        def apply_list_value(setting, obj):
2491
            options = setting.value.get_options()
2492
            obj.set_value(options.index(str(setting.value)))
2493

    
2494
        _5tone_standards = self._memobj._5tone_std_settings
2495
        i = 0
2496
        for standard in _5tone_standards:
2497
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2498
                                          LIST_5TONE_STANDARDS[i])
2499
            stds_5tone.append(std_5tone)
2500

    
2501
            period = standard.period
2502
            if period == 255:
2503
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2504
                          " is not yet configured. Setting to 70ms.")
2505
                period = 5
2506

    
2507
            if period <= len(LIST_5TONE_STANDARD_PERIODS):
2508
                line = RadioSetting(
2509
                    "_5tone_std_settings_" + str(i) + "_period",
2510
                    "Period (ms)", RadioSettingValueList
2511
                    (LIST_5TONE_STANDARD_PERIODS,
2512
                     LIST_5TONE_STANDARD_PERIODS[period]))
2513
                line.set_apply_callback(apply_list_value, standard.period)
2514
                std_5tone.append(line)
2515
            else:
2516
                LOG.debug("Invalid value for 5tone period! Disabling.")
2517

    
2518
            group_tone = standard.group_tone
2519
            if group_tone == 255:
2520
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2521
                          " is not yet configured. Setting to A.")
2522
                group_tone = 10
2523

    
2524
            if group_tone <= len(LIST_5TONE_DIGITS):
2525
                line = RadioSetting(
2526
                    "_5tone_std_settings_" + str(i) + "_grouptone",
2527
                    "Group Tone",
2528
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2529
                                          LIST_5TONE_DIGITS[
2530
                                              group_tone]))
2531
                line.set_apply_callback(apply_list_value,
2532
                                        standard.group_tone)
2533
                std_5tone.append(line)
2534
            else:
2535
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2536

    
2537
            repeat_tone = standard.repeat_tone
2538
            if repeat_tone == 255:
2539
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2540
                          " is not yet configured. Setting to E.")
2541
                repeat_tone = 14
2542

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

    
2557
        def my_apply_5tonestdlist_value(setting, obj):
2558
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2559
                obj.set_value(0xFF)
2560
            else:
2561
                obj.set_value(LIST_5TONE_STANDARDS.
2562
                              index(str(setting.value)))
2563

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

    
2584
        def validate_5tone_frame(value):
2585
            if (len(str(value)) != 5) and (len(str(value)) != 0):
2586
                msg = ("5 Tone must have 5 digits or 0 digits")
2587
                raise InvalidValueError(msg)
2588
            for digit in str(value):
2589
                if digit.upper() not in LIST_5TONE_DIGITS:
2590
                    msg = (str(digit) + " is not a valid digit for 5tones")
2591
                    raise InvalidValueError(msg)
2592
            return value
2593

    
2594
        def frame2string(frame):
2595
            frameString = ""
2596
            for digit in frame:
2597
                if digit != 255:
2598
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2599
            return frameString
2600

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

    
2620
            val = RadioSettingValueString(0, 6,
2621
                                          frame2string(code.frame1), False)
2622
            line = RadioSetting("_5tone_code_" + str(i) + "_frame1",
2623
                                " Frame 1", val)
2624
            val.set_validate_callback(validate_5tone_frame)
2625
            line.set_apply_callback(apply_5tone_frame, code.frame1)
2626
            code_5tone.append(line)
2627

    
2628
            val = RadioSettingValueString(0, 6,
2629
                                          frame2string(code.frame2), False)
2630
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2631
                                " Frame 2", val)
2632
            val.set_validate_callback(validate_5tone_frame)
2633
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2634
            code_5tone.append(line)
2635

    
2636
            val = RadioSettingValueString(0, 6,
2637
                                          frame2string(code.frame3), False)
2638
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2639
                                " Frame 3", val)
2640
            val.set_validate_callback(validate_5tone_frame)
2641
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2642
            code_5tone.append(line)
2643
            i = i + 1
2644

    
2645
        _5_tone_decode1 = RadioSetting(
2646
            "_5tone_settings._5tone_decode_call_frame1",
2647
            "5 Tone decode call Frame 1",
2648
            RadioSettingValueBoolean(
2649
                _mem._5tone_settings._5tone_decode_call_frame1))
2650
        group_5tone.append(_5_tone_decode1)
2651

    
2652
        _5_tone_decode2 = RadioSetting(
2653
            "_5tone_settings._5tone_decode_call_frame2",
2654
            "5 Tone decode call Frame 2",
2655
            RadioSettingValueBoolean(
2656
                _mem._5tone_settings._5tone_decode_call_frame2))
2657
        group_5tone.append(_5_tone_decode2)
2658

    
2659
        _5_tone_decode3 = RadioSetting(
2660
            "_5tone_settings._5tone_decode_call_frame3",
2661
            "5 Tone decode call Frame 3",
2662
            RadioSettingValueBoolean(
2663
                _mem._5tone_settings._5tone_decode_call_frame3))
2664
        group_5tone.append(_5_tone_decode3)
2665

    
2666
        _5_tone_decode_disp1 = RadioSetting(
2667
            "_5tone_settings._5tone_decode_disp_frame1",
2668
            "5 Tone decode disp Frame 1",
2669
            RadioSettingValueBoolean(
2670
                _mem._5tone_settings._5tone_decode_disp_frame1))
2671
        group_5tone.append(_5_tone_decode_disp1)
2672

    
2673
        _5_tone_decode_disp2 = RadioSetting(
2674
            "_5tone_settings._5tone_decode_disp_frame2",
2675
            "5 Tone decode disp Frame 2",
2676
            RadioSettingValueBoolean(
2677
                _mem._5tone_settings._5tone_decode_disp_frame2))
2678
        group_5tone.append(_5_tone_decode_disp2)
2679

    
2680
        _5_tone_decode_disp3 = RadioSetting(
2681
            "_5tone_settings._5tone_decode_disp_frame3",
2682
            "5 Tone decode disp Frame 3",
2683
            RadioSettingValueBoolean(
2684
                _mem._5tone_settings._5tone_decode_disp_frame3))
2685
        group_5tone.append(_5_tone_decode_disp3)
2686

    
2687
        decode_standard = _mem._5tone_settings.decode_standard
2688
        if decode_standard == 255:
2689
            decode_standard = 0
2690
        if decode_standard <= len(LIST_5TONE_STANDARDS_without_none):
2691
            line = RadioSetting("_5tone_settings.decode_standard",
2692
                                "5 Tone-decode Standard",
2693
                                RadioSettingValueList(
2694
                                    LIST_5TONE_STANDARDS_without_none,
2695
                                    LIST_5TONE_STANDARDS_without_none[
2696
                                        decode_standard]))
2697
            group_5tone.append(line)
2698
        else:
2699
            LOG.debug("Invalid decode std...")
2700

    
2701
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2702
        if _5tone_delay1 == 255:
2703
            _5tone_delay1 = 20
2704

    
2705
        if _5tone_delay1 <= len(LIST_5TONE_DELAY):
2706
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2707
                                         LIST_5TONE_DELAY[
2708
                                             _5tone_delay1])
2709
            line = RadioSetting("_5tone_settings._5tone_delay1",
2710
                                "5 Tone Delay Frame 1", list)
2711
            group_5tone.append(line)
2712
        else:
2713
            LOG.debug("Invalid value for 5tone delay (frame1) ! Disabling.")
2714

    
2715
        _5tone_delay2 = _mem._5tone_settings._5tone_delay2
2716
        if _5tone_delay2 == 255:
2717
            _5tone_delay2 = 20
2718
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2719

    
2720
        if _5tone_delay2 <= len(LIST_5TONE_DELAY):
2721
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2722
                                         LIST_5TONE_DELAY[
2723
                                             _5tone_delay2])
2724
            line = RadioSetting("_5tone_settings._5tone_delay2",
2725
                                "5 Tone Delay Frame 2", list)
2726
            group_5tone.append(line)
2727
        else:
2728
            LOG.debug("Invalid value for 5tone delay (frame2)! Disabling.")
2729

    
2730
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2731
        if _5tone_delay3 == 255:
2732
            _5tone_delay3 = 20
2733
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2734

    
2735
        if _5tone_delay3 <= len(LIST_5TONE_DELAY):
2736
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2737
                                         LIST_5TONE_DELAY[
2738
                                             _5tone_delay3])
2739
            line = RadioSetting("_5tone_settings._5tone_delay3",
2740
                                "5 Tone Delay Frame 3", list)
2741
            group_5tone.append(line)
2742
        else:
2743
            LOG.debug("Invalid value for 5tone delay (frame3)! Disabling.")
2744

    
2745
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2746
        if ext_length == 255:
2747
            ext_length = 0
2748
            LOG.debug("1st Tone ext lenght unconfigured! Resetting to 0")
2749

    
2750
        if ext_length <= len(LIST_5TONE_DELAY):
2751
            list = RadioSettingValueList(
2752
                LIST_5TONE_DELAY,
2753
                LIST_5TONE_DELAY[
2754
                    ext_length])
2755
            line = RadioSetting(
2756
                "_5tone_settings._5tone_first_digit_ext_length",
2757
                "First digit extend length", list)
2758
            group_5tone.append(line)
2759
        else:
2760
            LOG.debug("Invalid value for 5tone ext length! Disabling.")
2761

    
2762
        decode_reset_time = _mem._5tone_settings.decode_reset_time
2763
        if decode_reset_time == 255:
2764
            decode_reset_time = 59
2765
            LOG.debug("Decode reset time unconfigured. resetting.")
2766
        if decode_reset_time <= len(LIST_5TONE_RESET):
2767
            list = RadioSettingValueList(
2768
                LIST_5TONE_RESET,
2769
                LIST_5TONE_RESET[
2770
                    decode_reset_time])
2771
            line = RadioSetting("_5tone_settings.decode_reset_time",
2772
                                "Decode reset time", list)
2773
            group_5tone.append(line)
2774
        else:
2775
            LOG.debug("Invalid value decode reset time! Disabling.")
2776

    
2777
        # 2 Tone
2778
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2779
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2780

    
2781
        top.append(encode_2tone)
2782
        top.append(decode_2tone)
2783

    
2784
        duration_1st_tone = self._memobj._2tone.duration_1st_tone
2785
        if duration_1st_tone == 255:
2786
            LOG.debug("Duration of first 2 Tone digit is not yet " +
2787
                      "configured. Setting to 600ms")
2788
            duration_1st_tone = 60
2789

    
2790
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2791
            line = RadioSetting("_2tone.duration_1st_tone",
2792
                                "Duration 1st Tone",
2793
                                RadioSettingValueList(LIST_5TONE_DELAY,
2794
                                                      LIST_5TONE_DELAY[
2795
                                                          duration_1st_tone]))
2796
            encode_2tone.append(line)
2797

    
2798
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2799
        if duration_2nd_tone == 255:
2800
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2801
                      "configured. Setting to 600ms")
2802
            duration_2nd_tone = 60
2803

    
2804
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2805
            line = RadioSetting("_2tone.duration_2nd_tone",
2806
                                "Duration 2nd Tone",
2807
                                RadioSettingValueList(LIST_5TONE_DELAY,
2808
                                                      LIST_5TONE_DELAY[
2809
                                                          duration_2nd_tone]))
2810
            encode_2tone.append(line)
2811

    
2812
        duration_gap = self._memobj._2tone.duration_gap
2813
        if duration_gap == 255:
2814
            LOG.debug("Duration of gap is not yet " +
2815
                      "configured. Setting to 300ms")
2816
            duration_gap = 30
2817

    
2818
        if duration_gap <= len(LIST_5TONE_DELAY):
2819
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2820
                                RadioSettingValueList(LIST_5TONE_DELAY,
2821
                                                      LIST_5TONE_DELAY[
2822
                                                          duration_gap]))
2823
            encode_2tone.append(line)
2824

    
2825
        def _2tone_validate(value):
2826
            if value == 0:
2827
                return 65535
2828
            if value == 65535:
2829
                return value
2830
            if not (300 <= value and value <= 3000):
2831
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
2832
                raise InvalidValueError(msg)
2833
            return value
2834

    
2835
        def apply_2tone_freq(setting, obj):
2836
            val = int(setting.value)
2837
            if (val == 0) or (val == 65535):
2838
                obj.set_value(65535)
2839
            else:
2840
                obj.set_value(val)
2841

    
2842
        i = 1
2843
        for code in self._memobj._2tone._2tone_encode:
2844
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
2845
                                           "Encode Code " + str(i))
2846
            encode_2tone.append(code_2tone)
2847

    
2848
            tmp = code.freq1
2849
            if tmp == 65535:
2850
                tmp = 0
2851
            val1 = RadioSettingValueInteger(0, 65535, tmp)
2852
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
2853
                                 "Frequency 1", val1)
2854
            val1.set_validate_callback(_2tone_validate)
2855
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
2856
            code_2tone.append(freq1)
2857

    
2858
            tmp = code.freq2
2859
            if tmp == 65535:
2860
                tmp = 0
2861
            val2 = RadioSettingValueInteger(0, 65535, tmp)
2862
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
2863
                                 "Frequency 2", val2)
2864
            val2.set_validate_callback(_2tone_validate)
2865
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
2866
            code_2tone.append(freq2)
2867

    
2868
            i = i + 1
2869

    
2870
        decode_reset_time = _mem._2tone.reset_time
2871
        if decode_reset_time == 255:
2872
            decode_reset_time = 59
2873
            LOG.debug("Decode reset time unconfigured. resetting.")
2874
        if decode_reset_time <= len(LIST_5TONE_RESET):
2875
            list = RadioSettingValueList(
2876
                LIST_5TONE_RESET,
2877
                LIST_5TONE_RESET[
2878
                    decode_reset_time])
2879
            line = RadioSetting("_2tone.reset_time",
2880
                                "Decode reset time", list)
2881
            decode_2tone.append(line)
2882
        else:
2883
            LOG.debug("Invalid value decode reset time! Disabling.")
2884

    
2885
        def apply_2tone_freq_pair(setting, obj):
2886
            val = int(setting.value)
2887
            derived_val = 65535
2888
            frqname = str(setting._name[-5:])
2889
            derivedname = "derived_from_" + frqname
2890

    
2891
            if (val == 0):
2892
                val = 65535
2893
                derived_val = 65535
2894
            else:
2895
                derived_val = int(round(2304000.0/val))
2896

    
2897
            obj[frqname].set_value(val)
2898
            obj[derivedname].set_value(derived_val)
2899

    
2900
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
2901
                      derivedname + ": " + str(derived_val))
2902

    
2903
        i = 1
2904
        for decode_code in self._memobj._2tone._2tone_decode:
2905
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
2906
                                                "Decode Code " + str(i))
2907
            decode_2tone.append(_2tone_dec_code)
2908

    
2909
            j = 1
2910
            for dec in decode_code.decs:
2911
                val = dec.dec
2912
                if val == 255:
2913
                    LOG.debug("Dec for Code " + str(i) + " Dec " + str(j) +
2914
                              " is not yet configured. Setting to 0.")
2915
                    val = 0
2916

    
2917
                if val <= len(LIST_2TONE_DEC):
2918
                    line = RadioSetting(
2919
                        "_2tone_dec_settings_" + str(i) + "_dec_" + str(j),
2920
                        "Dec " + str(j), RadioSettingValueList
2921
                        (LIST_2TONE_DEC,
2922
                         LIST_2TONE_DEC[val]))
2923
                    line.set_apply_callback(apply_list_value, dec.dec)
2924
                    _2tone_dec_code.append(line)
2925
                else:
2926
                    LOG.debug("Invalid value for 2tone dec! Disabling.")
2927

    
2928
                val = dec.response
2929
                if val == 255:
2930
                    LOG.debug("Response for Code " + str(i) + " Dec " +
2931
                              str(j) + " is not yet configured. Setting to 0.")
2932
                    val = 0
2933

    
2934
                if val <= len(LIST_2TONE_RESPONSE):
2935
                    line = RadioSetting(
2936
                        "_2tone_dec_settings_" + str(i) + "_resp_" + str(j),
2937
                        "Response " + str(j), RadioSettingValueList
2938
                        (LIST_2TONE_RESPONSE,
2939
                         LIST_2TONE_RESPONSE[val]))
2940
                    line.set_apply_callback(apply_list_value, dec.response)
2941
                    _2tone_dec_code.append(line)
2942
                else:
2943
                    LOG.debug("Invalid value for 2tone response! Disabling.")
2944

    
2945
                val = dec.alert
2946
                if val == 255:
2947
                    LOG.debug("Alert for Code " + str(i) + " Dec " + str(j) +
2948
                              " is not yet configured. Setting to 0.")
2949
                    val = 0
2950

    
2951
                if val <= len(PTTIDCODE_LIST):
2952
                    line = RadioSetting(
2953
                        "_2tone_dec_settings_" + str(i) + "_alert_" + str(j),
2954
                        "Alert " + str(j), RadioSettingValueList
2955
                        (PTTIDCODE_LIST,
2956
                         PTTIDCODE_LIST[val]))
2957
                    line.set_apply_callback(apply_list_value, dec.alert)
2958
                    _2tone_dec_code.append(line)
2959
                else:
2960
                    LOG.debug("Invalid value for 2tone alert! Disabling.")
2961
                j = j + 1
2962

    
2963
            freq = self._memobj._2tone.freqs[i-1]
2964
            for char in ['A', 'B', 'C', 'D']:
2965
                setting_name = "freq" + str(char)
2966

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

    
2985
            i = i + 1
2986

    
2987
        return top
2988

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

    
3016
                    if element.has_apply_callback():
3017
                        LOG.debug("Using apply callback")
3018
                        element.run_apply_callback()
3019
                    elif element.value.get_mutable():
3020
                        LOG.debug("Setting %s = %s" % (setting, element.value))
3021
                        setattr(obj, setting, element.value)
3022
                except Exception, e:
3023
                    LOG.debug(element.get_name())
3024
                    raise
3025

    
3026
    @classmethod
3027
    def match_model(cls, filedata, filename):
3028
        match_size = False
3029
        match_model = False
3030

    
3031
        # testing the file data size
3032
        if len(filedata) == MEM_SIZE:
3033
            match_size = True
3034

    
3035
        # testing the firmware model fingerprint
3036
        match_model = model_match(cls, filedata)
3037

    
3038
        if match_size and match_model:
3039
            return True
3040
        else:
3041
            return False
3042

    
3043

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

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

    
3115
#seekto 0x0E80;
3116
struct {
3117
  u8 unknown1;
3118
  u8 vfomr;
3119
  u8 keylock;
3120
  u8 unknown2;
3121
  u8 unknown3:4,
3122
     vfomren:1,
3123
     unknown4:1,
3124
     reseten:1,
3125
     menuen:1;
3126
  u8 unknown5[11];
3127
  u8 dispab;
3128
  u8 mrcha;
3129
  u8 mrchb;
3130
  u8 menu;
3131
} settings2;
3132

    
3133
#seekto 0x0EC0;
3134
struct {
3135
  char line1[6];
3136
  char line2[6];
3137
} poweron_msg;
3138

    
3139
struct settings_vfo {
3140
  u8 freq[8];
3141
  u8 offset[6];
3142
  u8 unknown2[2];
3143
  ul16 rxtone;
3144
  ul16 txtone;
3145
  u8 scode;
3146
  u8 spmute;
3147
  u8 optsig;
3148
  u8 scramble;
3149
  u8 wide;
3150
  u8 power;
3151
  u8 shiftd;
3152
  u8 step;
3153
  u8 unknown3[4];
3154
};
3155

    
3156
#seekto 0x0F00;
3157
struct {
3158
  struct settings_vfo a;
3159
  struct settings_vfo b;
3160
} vfo;
3161

    
3162
#seekto 0x1000;
3163
struct {
3164
  char name[6];
3165
  u8 unknown1[10];
3166
} names[200];
3167

    
3168
#seekto 0x2400;
3169
struct {
3170
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3171
  u8 group_tone;
3172
  u8 repeat_tone;
3173
  u8 unused[13];
3174
} _5tone_std_settings[15];
3175

    
3176
#seekto 0x2500;
3177
struct {
3178
  u8 frame1[5];
3179
  u8 frame2[5];
3180
  u8 frame3[5];
3181
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3182
} _5tone_codes[15];
3183

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

    
3206
#seekto 0x2900;
3207
struct {
3208
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3209
} dtmf_codes[15];
3210

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

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

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

    
3291
#seekto 0x3000;
3292
struct {
3293
  u8 freq[8];
3294
  char broadcast_station_name[6];
3295
  u8 unknown[2];
3296
} fm_radio_preset[16];
3297

    
3298
#seekto 0x3C90;
3299
struct {
3300
  u8 vhf_low[3];
3301
  u8 vhf_high[3];
3302
  u8 uhf_low[3];
3303
  u8 uhf_high[3];
3304
} ranges;
3305

    
3306
// the UV-2501+220 & KT8900R has different zones for storing ranges
3307

    
3308
#seekto 0x3CD0;
3309
struct {
3310
  u8 vhf_low[3];
3311
  u8 vhf_high[3];
3312
  u8 unknown1[4];
3313
  u8 unknown2[6];
3314
  u8 vhf2_low[3];
3315
  u8 vhf2_high[3];
3316
  u8 unknown3[4];
3317
  u8 unknown4[6];
3318
  u8 uhf_low[3];
3319
  u8 uhf_high[3];
3320
} ranges220;
3321

    
3322
#seekto 0x3F70;
3323
struct {
3324
  char fp[6];
3325
} fingerprint;
3326

    
3327
"""
3328

    
3329

    
3330
class BTech(BTechMobileCommon):
3331
    """BTECH's UV-5001 and alike radios"""
3332
    BANDS = 2
3333
    COLOR_LCD = False
3334
    NAME_LENGTH = 6
3335

    
3336
    def set_options(self):
3337
        """This is to read the options from the image and set it in the
3338
        environment, for now just the limits of the freqs in the VHF/UHF
3339
        ranges"""
3340

    
3341
        # setting the correct ranges for each radio type
3342
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3343
            # the model 2501+220 has a segment in 220
3344
            # and a different position in the memmap
3345
            # also the QYT KT8900R
3346
            ranges = self._memobj.ranges220
3347
        else:
3348
            ranges = self._memobj.ranges
3349

    
3350
        # the normal dual bands
3351
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3352
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3353

    
3354
        # DEBUG
3355
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3356
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3357

    
3358
        # 220Mhz radios case
3359
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3360
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3361
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3362
            self._220_range = vhf2
3363

    
3364
        # set the class with the real data
3365
        self._vhf_range = vhf
3366
        self._uhf_range = uhf
3367

    
3368
    def process_mmap(self):
3369
        """Process the mem map into the mem object"""
3370

    
3371
        # Get it
3372
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3373

    
3374
        # load specific parameters from the radio image
3375
        self.set_options()
3376

    
3377

    
3378
# Declaring Aliases (Clones of the real radios)
3379
class JT2705M(chirp_common.Alias):
3380
    VENDOR = "Jetstream"
3381
    MODEL = "JT2705M"
3382

    
3383

    
3384
class JT6188Mini(chirp_common.Alias):
3385
    VENDOR = "Juentai"
3386
    MODEL = "JT-6188 Mini"
3387

    
3388

    
3389
class JT6188Plus(chirp_common.Alias):
3390
    VENDOR = "Juentai"
3391
    MODEL = "JT-6188 Plus"
3392

    
3393

    
3394
class SSGT890(chirp_common.Alias):
3395
    VENDOR = "Sainsonic"
3396
    MODEL = "GT-890"
3397

    
3398

    
3399
class ZastoneMP300(chirp_common.Alias):
3400
    VENDOR = "Zastone"
3401
    MODEL = "MP-300"
3402

    
3403

    
3404
# real radios
3405
@directory.register
3406
class UV2501(BTech):
3407
    """Baofeng Tech UV2501"""
3408
    MODEL = "UV-2501"
3409
    _fileid = [UV2501G3_fp,
3410
               UV2501G2_fp,
3411
               UV2501pp2_fp,
3412
               UV2501pp_fp]
3413

    
3414

    
3415
@directory.register
3416
class UV2501_220(BTech):
3417
    """Baofeng Tech UV2501+220"""
3418
    MODEL = "UV-2501+220"
3419
    BANDS = 3
3420
    _magic = MSTRING_220
3421
    _fileid = [UV2501_220G3_fp,
3422
               UV2501_220G2_fp,
3423
               UV2501_220_fp,
3424
               UV2501_220pp_fp]
3425

    
3426

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

    
3439

    
3440
@directory.register
3441
class MINI8900(BTech):
3442
    """WACCOM MINI-8900"""
3443
    VENDOR = "WACCOM"
3444
    MODEL = "MINI-8900"
3445
    _magic = MSTRING_MINI8900
3446
    _fileid = [MINI8900_fp, ]
3447
    # Clones
3448
    ALIASES = [JT6188Plus, ]
3449

    
3450

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

    
3463
# Please note that there is a version of this radios that is a clone of the
3464
# Waccom Mini8900, maybe an early version?
3465

    
3466

    
3467
class OTGRadioV1(chirp_common.Alias):
3468
    VENDOR = 'OTGSTUFF'
3469
    MODEL = 'OTG Radio v1'
3470

    
3471

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

    
3491

    
3492
@directory.register
3493
class KT9800R(BTech):
3494
    """QYT KT8900R"""
3495
    VENDOR = "QYT"
3496
    MODEL = "KT8900R"
3497
    BANDS = 3
3498
    _vhf_range = (136000000, 175000000)
3499
    _220_range = (240000000, 271000000)
3500
    _uhf_range = (400000000, 481000000)
3501
    _magic = MSTRING_KT8900R
3502
    _fileid = [KT8900R_fp,
3503
               KT8900R_fp1,
3504
               KT8900R_fp2,
3505
               KT8900R_fp3,
3506
               KT8900R_fp4,
3507
               KT8900R_fp5]
3508

    
3509

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

    
3523

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

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

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

    
3632
struct settings_vfo {
3633
  u8 freq[8];
3634
  u8 offset[6];
3635
  u8 unknown2[2];
3636
  ul16 rxtone;
3637
  ul16 txtone;
3638
  u8 scode;
3639
  u8 spmute;
3640
  u8 optsig;
3641
  u8 scramble;
3642
  u8 wide;
3643
  u8 power;
3644
  u8 shiftd;
3645
  u8 step;
3646
  u8 unknown3[4];
3647
};
3648

    
3649
#seekto 0x0F00;
3650
struct {
3651
  struct settings_vfo a;
3652
  struct settings_vfo b;
3653
  struct settings_vfo c;
3654
  struct settings_vfo d;
3655
} vfo;
3656

    
3657
#seekto 0x0F80;
3658
struct {
3659
  char line1[8];
3660
  char line2[8];
3661
  char line3[8];
3662
  char line4[8];
3663
  char line5[8];
3664
  char line6[8];
3665
  char line7[8];
3666
  char line8[8];
3667
} poweron_msg;
3668

    
3669
#seekto 0x1000;
3670
struct {
3671
  char name[8];
3672
  u8 unknown1[8];
3673
} names[200];
3674

    
3675
#seekto 0x2400;
3676
struct {
3677
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3678
  u8 group_tone;
3679
  u8 repeat_tone;
3680
  u8 unused[13];
3681
} _5tone_std_settings[15];
3682

    
3683
#seekto 0x2500;
3684
struct {
3685
  u8 frame1[5];
3686
  u8 frame2[5];
3687
  u8 frame3[5];
3688
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3689
} _5tone_codes[15];
3690

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

    
3713
#seekto 0x2900;
3714
struct {
3715
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3716
} dtmf_codes[15];
3717

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

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

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

    
3798
#seekto 0x3D80;
3799
struct {
3800
  u8 vhf_low[3];
3801
  u8 vhf_high[3];
3802
  u8 unknown1[4];
3803
  u8 unknown2[6];
3804
  u8 vhf2_low[3];
3805
  u8 vhf2_high[3];
3806
  u8 unknown3[4];
3807
  u8 unknown4[6];
3808
  u8 uhf_low[3];
3809
  u8 uhf_high[3];
3810
  u8 unknown5[4];
3811
  u8 unknown6[6];
3812
  u8 uhf2_low[3];
3813
  u8 uhf2_high[3];
3814
} ranges;
3815

    
3816
#seekto 0x3F70;
3817
struct {
3818
  char fp[6];
3819
} fingerprint;
3820

    
3821
"""
3822

    
3823

    
3824
class BTechColor(BTechMobileCommon):
3825
    """BTECH's Color LCD Mobile and alike radios"""
3826
    COLOR_LCD = True
3827
    NAME_LENGTH = 8
3828
    LIST_TMR = LIST_TMR16
3829

    
3830
    def process_mmap(self):
3831
        """Process the mem map into the mem object"""
3832

    
3833
        # Get it
3834
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
3835

    
3836
        # load specific parameters from the radio image
3837
        self.set_options()
3838

    
3839
    def set_options(self):
3840
        """This is to read the options from the image and set it in the
3841
        environment, for now just the limits of the freqs in the VHF/UHF
3842
        ranges"""
3843

    
3844
        # setting the correct ranges for each radio type
3845
        ranges = self._memobj.ranges
3846

    
3847
        # the normal dual bands
3848
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3849
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3850

    
3851
        # DEBUG
3852
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3853
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3854

    
3855
        # the additional bands
3856
        if self.MODEL in ["UV-25X4", "KT7900D"]:
3857
            # 200Mhz band
3858
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3859
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3860
            self._220_range = vhf2
3861

    
3862
            # 350Mhz band
3863
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
3864
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
3865
            self._350_range = uhf2
3866

    
3867
        # set the class with the real data
3868
        self._vhf_range = vhf
3869
        self._uhf_range = uhf
3870

    
3871

    
3872
# Declaring Aliases (Clones of the real radios)
3873
class SKT8900D(chirp_common.Alias):
3874
    VENDOR = "Surecom"
3875
    MODEL = "S-KT8900D"
3876

    
3877

    
3878
class QB25(chirp_common.Alias):
3879
    VENDOR = "Radioddity"
3880
    MODEL = "QB25"
3881

    
3882

    
3883
# real radios
3884
@directory.register
3885
class UV25X2(BTechColor):
3886
    """Baofeng Tech UV25X2"""
3887
    MODEL = "UV-25X2"
3888
    BANDS = 2
3889
    _vhf_range = (130000000, 180000000)
3890
    _uhf_range = (400000000, 521000000)
3891
    _magic = MSTRING_UV25X2
3892
    _fileid = [UV25X2_fp, ]
3893

    
3894

    
3895
@directory.register
3896
class UV25X4(BTechColor):
3897
    """Baofeng Tech UV25X4"""
3898
    MODEL = "UV-25X4"
3899
    BANDS = 4
3900
    _vhf_range = (130000000, 180000000)
3901
    _220_range = (200000000, 271000000)
3902
    _uhf_range = (400000000, 521000000)
3903
    _350_range = (350000000, 391000000)
3904
    _magic = MSTRING_UV25X4
3905
    _fileid = [UV25X4_fp, ]
3906

    
3907

    
3908
@directory.register
3909
class UV50X2(BTechColor):
3910
    """Baofeng Tech UV50X2"""
3911
    MODEL = "UV-50X2"
3912
    BANDS = 2
3913
    _vhf_range = (130000000, 180000000)
3914
    _uhf_range = (400000000, 521000000)
3915
    _magic = MSTRING_UV25X2
3916
    _fileid = [UV50X2_fp, ]
3917
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3918
                     chirp_common.PowerLevel("Low", watts=10)]
3919

    
3920

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

    
3938

    
3939
@directory.register
3940
class KT8900D(BTechColor):
3941
    """QYT KT8900D"""
3942
    VENDOR = "QYT"
3943
    MODEL = "KT8900D"
3944
    BANDS = 2
3945
    LIST_TMR = LIST_TMR15
3946
    _vhf_range = (136000000, 175000000)
3947
    _uhf_range = (400000000, 481000000)
3948
    _magic = MSTRING_KT8900D
3949
    _fileid = [KT8900D_fp3, KT8900D_fp2, KT8900D_fp1, KT8900D_fp]
3950

    
3951
    # Clones
3952
    ALIASES = [OTGRadioV1]
3953

    
3954

    
3955
@directory.register
3956
class KT5800(BTechColor):
3957
    """QYT KT5800"""
3958
    VENDOR = "QYT"
3959
    MODEL = "KT5800"
3960
    BANDS = 2
3961
    LIST_TMR = LIST_TMR15
3962
    _vhf_range = (136000000, 175000000)
3963
    _uhf_range = (400000000, 481000000)
3964
    _magic = MSTRING_KT8900D
3965
    _fileid = [KT5800_fp, ]
3966

    
3967

    
3968
@directory.register
3969
class KT980PLUS(BTechColor):
3970
    """QYT KT980PLUS"""
3971
    VENDOR = "QYT"
3972
    MODEL = "KT980PLUS"
3973
    BANDS = 2
3974
    LIST_TMR = LIST_TMR15
3975
    _vhf_range = (136000000, 175000000)
3976
    _uhf_range = (400000000, 481000000)
3977
    _magic = MSTRING_KT8900D
3978
    _fileid = [KT980PLUS_fp1, KT980PLUS_fp]
3979
    _power_levels = [chirp_common.PowerLevel("High", watts=75),
3980
                     chirp_common.PowerLevel("Low", watts=55)]
3981

    
3982
    @classmethod
3983
    def match_model(cls, filedata, filename):
3984
        # This model is only ever matched via metadata
3985
        return False
3986

    
3987

    
3988
@directory.register
3989
class DB25G(BTechColor):
3990
    """Radioddity DB25-G"""
3991
    VENDOR = "Radioddity"
3992
    MODEL = "DB25-G"
3993
    BANDS = 2
3994
    LIST_TMR = LIST_TMR15
3995
    _vhf_range = (136000000, 175000000)
3996
    _uhf_range = (400000000, 481000000)
3997
    _magic = MSTRING_KT8900D
3998
    _fileid = [DB25G_fp1, DB25G_fp]
3999
    _gmrs = True
4000
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
4001
                     chirp_common.PowerLevel("Mid", watts=15),
4002
                     chirp_common.PowerLevel("Low", watts=5)]
4003

    
4004
    @classmethod
4005
    def match_model(cls, filedata, filename):
4006
        # This model is only ever matched via metadata
4007
        return False
4008

    
4009

    
4010
GMRS_MEM_FORMAT = """
4011
#seekto 0x0000;
4012
struct {
4013
  lbcd rxfreq[4];
4014
  lbcd txfreq[4];
4015
  ul16 rxtone;
4016
  ul16 txtone;
4017
  u8 unknown0:4,
4018
     scode:4;
4019
  u8 unknown1:2,
4020
     spmute:2,
4021
     unknown2:2,
4022
     optsig:2;
4023
  u8 unknown3:3,
4024
     scramble:1,
4025
     unknown4:2,
4026
     power:2;
4027
  u8 unknown5:1,
4028
     wide:1,
4029
     unknown6:2,
4030
     bcl:1,
4031
     add:1,
4032
     pttid:2;
4033
} memory[256];
4034

    
4035
#seekto 0x1000;
4036
struct {
4037
  char name[7];
4038
  u8 unknown1[9];
4039
} names[256];
4040

    
4041
#seekto 0x2400;
4042
struct {
4043
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4044
  u8 group_tone;
4045
  u8 repeat_tone;
4046
  u8 unused[13];
4047
} _5tone_std_settings[15];
4048

    
4049
#seekto 0x2500;
4050
struct {
4051
  u8 frame1[5];
4052
  u8 frame2[5];
4053
  u8 frame3[5];
4054
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4055
} _5tone_codes[15];
4056

    
4057
#seekto 0x25F0;
4058
struct {
4059
  u8 _5tone_delay1; // * 10ms
4060
  u8 _5tone_delay2; // * 10ms
4061
  u8 _5tone_delay3; // * 10ms
4062
  u8 _5tone_first_digit_ext_length;
4063
  u8 unknown1;
4064
  u8 unknown2;
4065
  u8 unknown3;
4066
  u8 unknown4;
4067
  u8 decode_standard;
4068
  u8 unknown5:5,
4069
     _5tone_decode_call_frame3:1,
4070
     _5tone_decode_call_frame2:1,
4071
     _5tone_decode_call_frame1:1;
4072
  u8 unknown6:5,
4073
     _5tone_decode_disp_frame3:1,
4074
     _5tone_decode_disp_frame2:1,
4075
     _5tone_decode_disp_frame1:1;
4076
  u8 decode_reset_time; // * 100 + 100ms
4077
} _5tone_settings;
4078

    
4079
#seekto 0x2900;
4080
struct {
4081
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4082
} dtmf_codes[15];
4083

    
4084
#seekto 0x29F0;
4085
struct {
4086
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4087
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4088
  u8 unknown0[14];
4089
  u8 inspection[16];
4090
  u8 monitor[16];
4091
  u8 alarmcode[16];
4092
  u8 stun[16];
4093
  u8 kill[16];
4094
  u8 revive[16];
4095
  u8 unknown1[16];
4096
  u8 unknown2[16];
4097
  u8 unknown3[16];
4098
  u8 unknown4[16];
4099
  u8 unknown5[16];
4100
  u8 unknown6[16];
4101
  u8 unknown7[16];
4102
  u8 masterid[16];
4103
  u8 viceid[16];
4104
  u8 unused01:7,
4105
     mastervice:1;
4106
  u8 unused02:3,
4107
     mrevive:1,
4108
     mkill:1,
4109
     mstun:1,
4110
     mmonitor:1,
4111
     minspection:1;
4112
  u8 unused03:3,
4113
     vrevive:1,
4114
     vkill:1,
4115
     vstun:1,
4116
     vmonitor:1,
4117
     vinspection:1;
4118
  u8 unused04:6,
4119
     txdisable:1,
4120
     rxdisable:1;
4121
  u8 groupcode;
4122
  u8 spacecode;
4123
  u8 delayproctime; // * 100 + 100ms
4124
  u8 resettime;     // * 100 + 100ms
4125
} dtmf_settings;
4126

    
4127
#seekto 0x2D00;
4128
struct {
4129
  struct {
4130
    ul16 freq1;
4131
    u8 unused01[6];
4132
    ul16 freq2;
4133
    u8 unused02[6];
4134
  } _2tone_encode[15];
4135
  u8 duration_1st_tone; // *10ms
4136
  u8 duration_2nd_tone; // *10ms
4137
  u8 duration_gap;      // *10ms
4138
  u8 unused03[13];
4139
  struct {
4140
    struct {
4141
      u8 dec;      // one out of LIST_2TONE_DEC
4142
      u8 response; // one out of LIST_2TONE_RESPONSE
4143
      u8 alert;    // 1-16
4144
    } decs[4];
4145
    u8 unused04[4];
4146
  } _2tone_decode[15];
4147
  u8 unused05[16];
4148

    
4149
  struct {
4150
    ul16 freqA;
4151
    ul16 freqB;
4152
    ul16 freqC;
4153
    ul16 freqD;
4154
    // unknown what those values mean, but they are
4155
    // derived from configured frequencies
4156
    ul16 derived_from_freqA; // 2304000/freqA
4157
    ul16 derived_from_freqB; // 2304000/freqB
4158
    ul16 derived_from_freqC; // 2304000/freqC
4159
    ul16 derived_from_freqD; // 2304000/freqD
4160
  }freqs[15];
4161
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4162
} _2tone;
4163

    
4164
#seekto 0x3000;
4165
struct {
4166
  u8 freq[8];
4167
  char broadcast_station_name[6];
4168
  u8 unknown[2];
4169
} fm_radio_preset[16];
4170

    
4171
#seekto 0x3200;
4172
struct {
4173
  u8 tmr;
4174
  u8 unknown1;
4175
  u8 sql;
4176
  u8 unknown2;
4177
  u8 autolk;
4178
  u8 tot;
4179
  u8 apo;
4180
  u8 unknown3;
4181
  u8 abr;
4182
  u8 beep;
4183
  u8 unknown4[4];
4184
  u8 dtmfst;
4185
  u8 unknown5[2];
4186
  u8 screv;
4187
  u8 unknown6[2];
4188
  u8 pttid;
4189
  u8 pttlt;
4190
  u8 unknown7;
4191
  u8 emctp;
4192
  u8 emcch;
4193
  u8 sigbp;
4194
  u8 unknown8;
4195
  u8 camdf;
4196
  u8 cbmdf;
4197
  u8 ccmdf;
4198
  u8 cdmdf;
4199
  u8 langua;
4200
  u8 sync;
4201

    
4202

    
4203
  u8 stfc;
4204
  u8 mffc;
4205
  u8 sfafc;
4206
  u8 sfbfc;
4207
  u8 sfcfc;
4208
  u8 sfdfc;
4209
  u8 subfc;
4210
  u8 fmfc;
4211
  u8 sigfc;
4212
  u8 modfc;
4213
  u8 menufc;
4214
  u8 txfc;
4215
  u8 txdisp;
4216
  u8 unknown9[5];
4217
  u8 anil;
4218
  u8 reps;
4219
  u8 repm;
4220
  u8 tmrmr;
4221
  u8 ste;
4222
  u8 rpste;
4223
  u8 rptdl;
4224
  u8 dtmfg;
4225
  u8 mgain;
4226
  u8 skiptx;
4227
  u8 scmode;
4228
} settings;
4229

    
4230
#seekto 0x3280;
4231
struct {
4232
  u8 unknown1;
4233
  u8 vfomr;
4234
  u8 keylock;
4235
  u8 unknown2;
4236
  u8 unknown3:4,
4237
     vfomren:1,
4238
     unknown4:1,
4239
     reseten:1,
4240
     menuen:1;
4241
  u8 unknown5[11];
4242
  u8 dispab;
4243
  u8 unknown6[2];
4244
  u8 smenu;
4245
  u8 unknown7[7];
4246
  u8 vfomra;
4247
  u8 vfomrb;
4248
  u8 vfomrc;
4249
  u8 vfomrd;
4250
  u8 mrcha;
4251
  u8 mrchb;
4252
  u8 mrchc;
4253
  u8 mrchd;
4254
} settings2;
4255

    
4256
struct settings_vfo {
4257
  u8 freq[8];
4258
  u8 offset[6];
4259
  u8 unknown2[2];
4260
  ul16 rxtone;
4261
  ul16 txtone;
4262
  u8 scode;
4263
  u8 spmute;
4264
  u8 optsig;
4265
  u8 scramble;
4266
  u8 wide;
4267
  u8 power;
4268
  u8 shiftd;
4269
  u8 step;
4270
  u8 unknown3[4];
4271
};
4272

    
4273
#seekto 0x3300;
4274
struct {
4275
  struct settings_vfo a;
4276
  struct settings_vfo b;
4277
  struct settings_vfo c;
4278
  struct settings_vfo d;
4279
} vfo;
4280

    
4281
#seekto 0x3D80;
4282
struct {
4283
  u8 vhf_low[3];
4284
  u8 vhf_high[3];
4285
  u8 unknown1[4];
4286
  u8 unknown2[6];
4287
  u8 vhf2_low[3];
4288
  u8 vhf2_high[3];
4289
  u8 unknown3[4];
4290
  u8 unknown4[6];
4291
  u8 uhf_low[3];
4292
  u8 uhf_high[3];
4293
  u8 unknown5[4];
4294
  u8 unknown6[6];
4295
  u8 uhf2_low[3];
4296
  u8 uhf2_high[3];
4297
} ranges;
4298

    
4299
#seekto 0x33B0;
4300
struct {
4301
  char line[16];
4302
} static_msg;
4303

    
4304
#seekto 0x3F70;
4305
struct {
4306
  char fp[6];
4307
} fingerprint;
4308

    
4309
"""
4310

    
4311

    
4312
class BTechGMRS(BTechMobileCommon):
4313
    """BTECH's GMRS Mobile"""
4314
    COLOR_LCD = True
4315
    COLOR_LCD2 = True
4316
    NAME_LENGTH = 7
4317
    UPLOAD_MEM_SIZE = 0X3400
4318

    
4319
    def process_mmap(self):
4320
        """Process the mem map into the mem object"""
4321

    
4322
        # Get it
4323
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
4324

    
4325
        # load specific parameters from the radio image
4326
        self.set_options()
4327

    
4328
    def set_options(self):
4329
        """This is to read the options from the image and set it in the
4330
        environment, for now just the limits of the freqs in the VHF/UHF
4331
        ranges"""
4332

    
4333
        # setting the correct ranges for each radio type
4334
        ranges = self._memobj.ranges
4335

    
4336
        # the normal dual bands
4337
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4338
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4339

    
4340
        # DEBUG
4341
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4342
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4343

    
4344
        # set the class with the real data
4345
        self._vhf_range = vhf
4346
        self._uhf_range = uhf
4347

    
4348

    
4349
# real radios
4350
@directory.register
4351
class GMRS50X1(BTechGMRS):
4352
    """Baofeng Tech GMRS50X1"""
4353
    MODEL = "GMRS-50X1"
4354
    BANDS = 2
4355
    LIST_TMR = LIST_TMR16
4356
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4357
                     chirp_common.PowerLevel("Mid", watts=10),
4358
                     chirp_common.PowerLevel("Low", watts=5)]
4359
    _vhf_range = (136000000, 175000000)
4360
    _uhf_range = (400000000, 521000000)
4361
    _upper = 255
4362
    _magic = MSTRING_GMRS50X1
4363
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
4364

    
4365

    
4366
COLORHT_MEM_FORMAT = """
4367
#seekto 0x0000;
4368
struct {
4369
  lbcd rxfreq[4];
4370
  lbcd txfreq[4];
4371
  ul16 rxtone;
4372
  ul16 txtone;
4373
  u8 unknown0:4,
4374
     scode:4;
4375
  u8 unknown1:2,
4376
     spmute:2,
4377
     unknown2:2,
4378
     optsig:2;
4379
  u8 unknown3:3,
4380
     scramble:1,
4381
     unknown4:3,
4382
     power:1;
4383
  u8 unknown5:1,
4384
     wide:1,
4385
     unknown6:2,
4386
     bcl:1,
4387
     add:1,
4388
     pttid:2;
4389
} memory[200];
4390

    
4391
#seekto 0x0E00;
4392
struct {
4393
  u8 tmr;
4394
  u8 unknownE01;
4395
  u8 sql;
4396
  u8 unknownE03[2];
4397
  u8 tot;
4398
  u8 save;
4399
  u8 unknownE07;
4400
  u8 abr;
4401
  u8 beep;
4402
  u8 unknownE0A[4];
4403
  u8 dsub;
4404
  u8 dtmfst;
4405
  u8 screv;
4406
  u8 unknownE11[3];
4407
  u8 pttid;
4408
  u8 unknownE15;
4409
  u8 pttlt;
4410
  u8 unknownE17;
4411
  u8 emctp;
4412
  u8 emcch;
4413
  u8 sigbp;
4414
  u8 unknownE1B;
4415
  u8 camdf;
4416
  u8 cbmdf;
4417
  u8 ccmdf;
4418
  u8 cdmdf;
4419
  u8 langua;
4420
  u8 voice;
4421
  u8 vox;
4422
  u8 voxt;
4423
  u8 sync;          // BTech radios use this as the display sync setting
4424
                    // other radios use this as the auto keypad lock setting
4425
  u8 stfc;
4426
  u8 mffc;
4427
  u8 sfafc;
4428
  u8 sfbfc;
4429
  u8 sfcfc;
4430
  u8 sfdfc;
4431
  u8 subfc;
4432
  u8 fmfc;
4433
  u8 sigfc;
4434
  u8 menufc;
4435
  u8 txfc;
4436
  u8 rxfc;
4437
  u8 unknownE31[5];
4438
  u8 anil;
4439
  u8 reps;
4440
  u8 tmrmr;
4441
  u8 ste;
4442
  u8 rpste;
4443
  u8 rptdl;
4444
  u8 dtmfg;
4445
  u8 tmrtx;
4446
} settings;
4447

    
4448
#seekto 0x0E80;
4449
struct {
4450
  u8 unknown1;
4451
  u8 vfomr;
4452
  u8 keylock;
4453
  u8 unknown2;
4454
  u8 unknown3:4,
4455
     vfomren:1,
4456
     unknown4:1,
4457
     reseten:1,
4458
     menuen:1;
4459
  u8 unknown5[11];
4460
  u8 dispab;
4461
  u8 unknown6[2];
4462
  u8 menu;
4463
  u8 unknown7[7];
4464
  u8 vfomra;
4465
  u8 vfomrb;
4466
  u8 vfomrc;
4467
  u8 vfomrd;
4468
  u8 mrcha;
4469
  u8 mrchb;
4470
  u8 mrchc;
4471
  u8 mrchd;
4472
} settings2;
4473

    
4474
struct settings_vfo {
4475
  u8 freq[8];
4476
  u8 offset[6];
4477
  u8 unknown2[2];
4478
  ul16 rxtone;
4479
  ul16 txtone;
4480
  u8 scode;
4481
  u8 spmute;
4482
  u8 optsig;
4483
  u8 scramble;
4484
  u8 wide;
4485
  u8 power;
4486
  u8 shiftd;
4487
  u8 step;
4488
  u8 unknown3[4];
4489
};
4490

    
4491
#seekto 0x0F00;
4492
struct {
4493
  struct settings_vfo a;
4494
  struct settings_vfo b;
4495
  struct settings_vfo c;
4496
  struct settings_vfo d;
4497
} vfo;
4498

    
4499
#seekto 0x0FE0;
4500
struct {
4501
  char line[16];
4502
} static_msg;
4503

    
4504
#seekto 0x1000;
4505
struct {
4506
  char name[8];
4507
  u8 unknown1[8];
4508
} names[200];
4509

    
4510
#seekto 0x2400;
4511
struct {
4512
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4513
  u8 group_tone;
4514
  u8 repeat_tone;
4515
  u8 unused[13];
4516
} _5tone_std_settings[15];
4517

    
4518
#seekto 0x2500;
4519
struct {
4520
  u8 frame1[5];
4521
  u8 frame2[5];
4522
  u8 frame3[5];
4523
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4524
} _5tone_codes[15];
4525

    
4526
#seekto 0x25F0;
4527
struct {
4528
  u8 _5tone_delay1; // * 10ms
4529
  u8 _5tone_delay2; // * 10ms
4530
  u8 _5tone_delay3; // * 10ms
4531
  u8 _5tone_first_digit_ext_length;
4532
  u8 unknown1;
4533
  u8 unknown2;
4534
  u8 unknown3;
4535
  u8 unknown4;
4536
  u8 decode_standard;
4537
  u8 unknown5:5,
4538
     _5tone_decode_call_frame3:1,
4539
     _5tone_decode_call_frame2:1,
4540
     _5tone_decode_call_frame1:1;
4541
  u8 unknown6:5,
4542
     _5tone_decode_disp_frame3:1,
4543
     _5tone_decode_disp_frame2:1,
4544
     _5tone_decode_disp_frame1:1;
4545
  u8 decode_reset_time; // * 100 + 100ms
4546
} _5tone_settings;
4547

    
4548
#seekto 0x2900;
4549
struct {
4550
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4551
} dtmf_codes[15];
4552

    
4553
#seekto 0x29F0;
4554
struct {
4555
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4556
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4557
  u8 unknown0[14];
4558
  u8 inspection[16];
4559
  u8 monitor[16];
4560
  u8 alarmcode[16];
4561
  u8 stun[16];
4562
  u8 kill[16];
4563
  u8 revive[16];
4564
  u8 unknown1[16];
4565
  u8 unknown2[16];
4566
  u8 unknown3[16];
4567
  u8 unknown4[16];
4568
  u8 unknown5[16];
4569
  u8 unknown6[16];
4570
  u8 unknown7[16];
4571
  u8 masterid[16];
4572
  u8 viceid[16];
4573
  u8 unused01:7,
4574
     mastervice:1;
4575
  u8 unused02:3,
4576
     mrevive:1,
4577
     mkill:1,
4578
     mstun:1,
4579
     mmonitor:1,
4580
     minspection:1;
4581
  u8 unused03:3,
4582
     vrevive:1,
4583
     vkill:1,
4584
     vstun:1,
4585
     vmonitor:1,
4586
     vinspection:1;
4587
  u8 unused04:6,
4588
     txdisable:1,
4589
     rxdisable:1;
4590
  u8 groupcode;
4591
  u8 spacecode;
4592
  u8 delayproctime; // * 100 + 100ms
4593
  u8 resettime;     // * 100 + 100ms
4594
} dtmf_settings;
4595

    
4596
#seekto 0x2D00;
4597
struct {
4598
  struct {
4599
    ul16 freq1;
4600
    u8 unused01[6];
4601
    ul16 freq2;
4602
    u8 unused02[6];
4603
  } _2tone_encode[15];
4604
  u8 duration_1st_tone; // *10ms
4605
  u8 duration_2nd_tone; // *10ms
4606
  u8 duration_gap;      // *10ms
4607
  u8 unused03[13];
4608
  struct {
4609
    struct {
4610
      u8 dec;      // one out of LIST_2TONE_DEC
4611
      u8 response; // one out of LIST_2TONE_RESPONSE
4612
      u8 alert;    // 1-16
4613
    } decs[4];
4614
    u8 unused04[4];
4615
  } _2tone_decode[15];
4616
  u8 unused05[16];
4617

    
4618
  struct {
4619
    ul16 freqA;
4620
    ul16 freqB;
4621
    ul16 freqC;
4622
    ul16 freqD;
4623
    // unknown what those values mean, but they are
4624
    // derived from configured frequencies
4625
    ul16 derived_from_freqA; // 2304000/freqA
4626
    ul16 derived_from_freqB; // 2304000/freqB
4627
    ul16 derived_from_freqC; // 2304000/freqC
4628
    ul16 derived_from_freqD; // 2304000/freqD
4629
  }freqs[15];
4630
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4631
} _2tone;
4632

    
4633
#seekto 0x3D80;
4634
struct {
4635
  u8 vhf_low[3];
4636
  u8 vhf_high[3];
4637
  u8 unknown1[4];
4638
  u8 unknown2[6];
4639
  u8 vhf2_low[3];
4640
  u8 vhf2_high[3];
4641
  u8 unknown3[4];
4642
  u8 unknown4[6];
4643
  u8 uhf_low[3];
4644
  u8 uhf_high[3];
4645
  u8 unknown5[4];
4646
  u8 unknown6[6];
4647
  u8 uhf2_low[3];
4648
  u8 uhf2_high[3];
4649
} ranges;
4650

    
4651
#seekto 0x3F70;
4652
struct {
4653
  char fp[6];
4654
} fingerprint;
4655

    
4656
"""
4657

    
4658

    
4659
class QYTColorHT(BTechMobileCommon):
4660
    """QTY's Color LCD Handheld and alike radios"""
4661
    COLOR_LCD = True
4662
    COLOR_LCD3 = True
4663
    NAME_LENGTH = 8
4664
    LIST_TMR = LIST_TMR15
4665

    
4666
    def process_mmap(self):
4667
        """Process the mem map into the mem object"""
4668

    
4669
        # Get it
4670
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4671

    
4672
        # load specific parameters from the radio image
4673
        self.set_options()
4674

    
4675
    def set_options(self):
4676
        """This is to read the options from the image and set it in the
4677
        environment, for now just the limits of the freqs in the VHF/UHF
4678
        ranges"""
4679

    
4680
        # setting the correct ranges for each radio type
4681
        ranges = self._memobj.ranges
4682

    
4683
        # the normal dual bands
4684
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4685
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4686

    
4687
        # DEBUG
4688
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4689
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4690

    
4691
        # the additional bands
4692
        if self.MODEL in ["KT-8R"]:
4693
            # 200Mhz band
4694
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
4695
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
4696
            self._220_range = vhf2
4697

    
4698
            # 350Mhz band
4699
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4700
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4701
            self._350_range = uhf2
4702

    
4703
        # set the class with the real data
4704
        self._vhf_range = vhf
4705
        self._uhf_range = uhf
4706

    
4707

    
4708
# real radios
4709
@directory.register
4710
class KT8R(QYTColorHT):
4711
    """QYT KT8R"""
4712
    VENDOR = "QYT"
4713
    MODEL = "KT-8R"
4714
    BANDS = 4
4715
    LIST_TMR = LIST_TMR16
4716
    _vhf_range = (136000000, 175000000)
4717
    _220_range = (200000000, 261000000)
4718
    _uhf_range = (400000000, 481000000)
4719
    _350_range = (350000000, 391000000)
4720
    _magic = MSTRING_KT8R
4721
    _fileid = [KT8R_fp2, KT8R_fp1, KT8R_fp, ]
4722
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
4723
                     chirp_common.PowerLevel("Low", watts=1)]
    (1-1/1)