Project

General

Profile

New Model #9407 » db25-g_test_#3.py

Jim Unroe, 10/20/2021 12:27 PM

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

    
18
import time
19
import struct
20
import logging
21

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

    
31
LOG = logging.getLogger(__name__)
32

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

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

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

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

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

    
135
# This is a general serial timeout for all serial read functions.
136
# Practice has show that about 0.7 sec will be enough to cover all radios.
137
STIMEOUT = 0.7
138

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

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

    
147

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

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

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

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

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

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

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

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

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

    
198

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

    
202

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

    
206
# QYT KT8900
207
KT8900_fp = "M29154"
208
# New generations KT8900
209
KT8900_fp1 = "M2C234"
210
KT8900_fp2 = "M2G1F4"
211
KT8900_fp3 = "M2G2F4"
212
KT8900_fp4 = "M2G304"
213
KT8900_fp5 = "M2G314"
214
KT8900_fp6 = "M2G424"
215
# this radio has an extra ID
216
KT8900_id = "303688"
217
# another extra ID in sep/2021
218
KT8900_id2 = "\x05\x58\x3d\xf0\x10"
219

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

    
235
# KT7900D (quad band)
236
KT7900D_fp = "VC4004"
237
KT7900D_fp1 = "VC4284"
238
KT7900D_fp2 = "VC4264"
239
KT7900D_fp3 = "VC4114"
240
KT7900D_fp4 = "VC4104"
241
KT7900D_fp5 = "VC4254"
242
KT7900D_fp6 = "VC5264"
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
# Radioddity DB25-G (gmrs)
267
DB25G_fp = "VC6182"
268

    
269

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

    
291

    
292
def _clean_buffer(radio):
293
    """Cleaning the read serial buffer, hard timeout to survive an infinite
294
    data stream"""
295

    
296
    # touching the serial timeout to optimize the flushing
297
    # restored at the end to the default value
298
    radio.pipe.timeout = 0.1
299
    dump = "1"
300
    datacount = 0
301

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

    
312
        # restore the default serial timeout
313
        radio.pipe.timeout = STIMEOUT
314

    
315
    except Exception:
316
        raise errors.RadioError("Unknown error cleaning the serial buffer")
317

    
318

    
319
def _rawrecv(radio, amount):
320
    """Raw read from the radio device, less intensive way"""
321

    
322
    data = ""
323

    
324
    try:
325
        data = radio.pipe.read(amount)
326

    
327
        # DEBUG
328
        if debug is True:
329
            LOG.debug("<== (%d) bytes:\n\n%s" %
330
                      (len(data), util.hexprint(data)))
331

    
332
        # fail if no data is received
333
        if len(data) == 0:
334
            raise errors.RadioError("No data received from radio")
335

    
336
        # notice on the logs if short
337
        if len(data) < amount:
338
            LOG.warn("Short reading %d bytes from the %d requested." %
339
                     (len(data), amount))
340

    
341
    except:
342
        raise errors.RadioError("Error reading data from radio")
343

    
344
    return data
345

    
346

    
347
def _send(radio, data):
348
    """Send data to the radio device"""
349

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

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

    
372

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

    
380
    return frame
381

    
382

    
383
def _recv(radio, addr):
384
    """Get data from the radio all at once to lower syscalls load"""
385

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

    
389
    # get the whole block
390
    block = _rawrecv(radio, BLOCK_SIZE + 5)
391

    
392
    # basic check
393
    if len(block) < (BLOCK_SIZE + 5):
394
        raise errors.RadioError("Short read of the block 0x%04x" % addr)
395

    
396
    # checking for the ack
397
    if block[0] != ACK_CMD:
398
        raise errors.RadioError("Bad ack from radio in block 0x%04x" % addr)
399

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

    
407
    # return the data
408
    return block[5:]
409

    
410

    
411
def _start_clone_mode(radio, status):
412
    """Put the radio in clone mode and get the ident string, 3 tries"""
413

    
414
    # cleaning the serial buffer
415
    _clean_buffer(radio)
416

    
417
    # prep the data to show in the UI
418
    status.cur = 0
419
    status.msg = "Identifying the radio..."
420
    status.max = 3
421
    radio.status_fn(status)
422

    
423
    try:
424
        for a in range(0, status.max):
425
            # Update the UI
426
            status.cur = a + 1
427
            radio.status_fn(status)
428

    
429
            # send the magic word
430
            _send(radio, radio._magic)
431

    
432
            # Now you get a x06 of ACK if all goes well
433
            ack = radio.pipe.read(1)
434

    
435
            if ack == "\x06":
436
                # DEBUG
437
                LOG.info("Magic ACK received")
438
                status.cur = status.max
439
                radio.status_fn(status)
440

    
441
                return True
442

    
443
        return False
444

    
445
    except errors.RadioError:
446
        raise
447
    except Exception, e:
448
        raise errors.RadioError("Error sending Magic to radio:\n%s" % e)
449

    
450

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

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

    
466
    # Ok, get the ident string
467
    ident = _rawrecv(radio, 49)
468

    
469
    # basic check for the ident
470
    if len(ident) != 49:
471
        raise errors.RadioError("Radio send a short ident block.")
472

    
473
    # check if ident is OK
474
    itis = False
475
    for fp in radio._fileid:
476
        if fp in ident:
477
            # got it!
478
            itis = True
479
            # checking if we are dealing with a Gen 3 BTECH
480
            if radio.VENDOR == "BTECH" and fp in BTECH3:
481
                radio.btech3 = True
482

    
483
            break
484

    
485
    if itis is False:
486
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
487
        raise errors.RadioError("Radio identification failed.")
488

    
489
    # some radios needs a extra read and check for a code on it, this ones
490
    # has the check value in the _id2 var, others simply False
491
    if radio._id2 is not False:
492
        # lower the timeout here as this radios are reseting due to timeout
493
        radio.pipe.timeout = 0.05
494

    
495
        # query & receive the extra ID
496
        _send(radio, _make_frame("S", 0x3DF0, 16))
497
        id2 = _rawrecv(radio, 21)
498

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

    
506
        # ok, the correct string must be in the received data
507
        # the radio._id2 var will be always a list
508
        flag2 = False
509
        for _id2 in radio._id2:
510
            if _id2 in id2:
511
                flag2 = True
512

    
513
        if not flag2:
514
            LOG.debug("Full *BAD* extra ID on the %s is: \n%s" %
515
                      (radio.MODEL, util.hexprint(id2)))
516
            raise errors.RadioError("The extra ID is wrong, aborting.")
517

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

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

    
539
            # checking
540
            if len(ack) == 0 or ack[-1:] != ACK_CMD:
541
                raise errors.RadioError("Radio didn't ACK the upload")
542

    
543
            # restore the default serial timeout
544
            radio.pipe.timeout = STIMEOUT
545

    
546
    # DEBUG
547
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
548

    
549
    return True
550

    
551

    
552
def _download(radio):
553
    """Get the memory map"""
554

    
555
    # UI progress
556
    status = chirp_common.Status()
557

    
558
    # put radio in program mode and identify it
559
    _do_ident(radio, status)
560

    
561
    # the models that doesn't have the extra ID have to make a dummy read here
562
    if radio._id2 is False:
563
        _send(radio, _make_frame("S", 0, BLOCK_SIZE))
564
        discard = _rawrecv(radio, BLOCK_SIZE + 5)
565

    
566
        if debug is True:
567
            LOG.info("Dummy first block read done, got this:\n\n %s",
568
                     util.hexprint(discard))
569

    
570
    # reset the progress bar in the UI
571
    status.max = MEM_SIZE / BLOCK_SIZE
572
    status.msg = "Cloning from radio..."
573
    status.cur = 0
574
    radio.status_fn(status)
575

    
576
    # cleaning the serial buffer
577
    _clean_buffer(radio)
578

    
579
    data = ""
580
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
581
        # sending the read request
582
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
583

    
584
        # read
585
        d = _recv(radio, addr)
586

    
587
        # aggregate the data
588
        data += d
589

    
590
        # UI Update
591
        status.cur = addr / BLOCK_SIZE
592
        status.msg = "Cloning from radio..."
593
        radio.status_fn(status)
594

    
595
    return data
596

    
597

    
598
def _upload(radio):
599
    """Upload procedure"""
600

    
601
    ## The UPLOAD mem is restricted to lower than 0x3100,
602
    ## so we will overide that here localy
603
    #MEM_SIZE = radio.UPLOAD_MEM_SIZE
604

    
605
    ## UI progress
606
    #status = chirp_common.Status()
607

    
608
    ## put radio in program mode and identify it
609
    #_do_ident(radio, status, True)
610

    
611
    ## get the data to upload to radio
612
    #data = radio.get_mmap()
613

    
614
    ## Reset the UI progress
615
    #status.max = MEM_SIZE / TX_BLOCK_SIZE
616
    #status.cur = 0
617
    #status.msg = "Cloning to radio..."
618
    #radio.status_fn(status)
619

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

    
624
    ## cleaning the serial buffer
625
    #_clean_buffer(radio)
626

    
627
    ## the fun start here
628
    #for addr in range(0, MEM_SIZE, TX_BLOCK_SIZE):
629
    #    # getting the block of data to send
630
    #    d = data[addr:addr + TX_BLOCK_SIZE]
631

    
632
    #    # build the frame to send
633
    #    frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
634

    
635
    #    # first block must not send the ACK at the beginning for the
636
    #    # ones that has the extra id, since this have to do a extra step
637
    #    if addr == 0 and radio._id2 is not False:
638
    #        frame = frame[1:]
639

    
640
    #    # send the frame
641
    #    _send(radio, frame)
642

    
643
    #    # receiving the response
644
    #    ack = _rawrecv(radio, 1)
645

    
646
    #    # basic check
647
    #    if len(ack) != 1:
648
    #        raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
649

    
650
    #    if ack not in "\x06\x05":
651
    #        raise errors.RadioError("Bad ACK writing block 0x%04x:" % addr)
652

    
653
    #    # UI Update
654
    #    status.cur = addr / TX_BLOCK_SIZE
655
    #    status.msg = "Cloning to radio..."
656
    #    radio.status_fn(status)
657

    
658

    
659
def model_match(cls, data):
660
    """Match the opened/downloaded image to the correct version"""
661
    rid = data[0x3f70:0x3f76]
662

    
663
    if rid in cls._fileid:
664
        return True
665

    
666
    return False
667

    
668

    
669
def _decode_ranges(low, high):
670
    """Unpack the data in the ranges zones in the memmap and return
671
    a tuple with the integer corresponding to the Mhz it means"""
672
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
673
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
674
    ilow *= 1000000
675
    ihigh *= 1000000
676

    
677
    return (ilow, ihigh)
678

    
679

    
680
def _split(rf, f1, f2):
681
    """Returns False if the two freqs are in the same band (no split)
682
    or True otherwise"""
683

    
684
    # determine if the two freqs are in the same band
685
    for low, high in rf.valid_bands:
686
        if f1 >= low and f1 <= high and \
687
                f2 >= low and f2 <= high:
688
            # if the two freqs are on the same Band this is not a split
689
            return False
690

    
691
    # if you get here is because the freq pairs are split
692
    return True
693

    
694

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

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

    
733
            1 - Turn off your radio
734
            2 - Connect your interface cable
735
            3 - Turn on your radio
736
            4 - Do the download of your radio data
737

    
738
            """))
739
        rp.pre_upload = _(dedent("""\
740
            Follow these instructions to upload your info:
741

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

    
747
            """))
748
        return rp
749

    
750
    def get_features(self):
751
        """Get the radio's features"""
752

    
753
        # we will use the following var as global
754
        global POWER_LEVELS
755

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

    
787
        # power levels
788
        POWER_LEVELS = self._power_levels
789
        rf.valid_power_levels = POWER_LEVELS
790

    
791
        # normal dual bands
792
        rf.valid_bands = [self._vhf_range, self._uhf_range]
793

    
794
        # 220 band
795
        if self.BANDS == 3 or self.BANDS == 4:
796
            rf.valid_bands.append(self._220_range)
797

    
798
        # 350 band
799
        if self.BANDS == 4:
800
            rf.valid_bands.append(self._350_range)
801

    
802
        return rf
803

    
804
    def sync_in(self):
805
        """Download from radio"""
806
        data = _download(self)
807
        self._mmap = memmap.MemoryMap(data)
808
        self.process_mmap()
809

    
810
    def sync_out(self):
811
        """Upload to radio"""
812
        try:
813
            _upload(self)
814
        except errors.RadioError:
815
            raise
816
        except Exception, e:
817
            raise errors.RadioError("Error: %s" % e)
818

    
819
    def get_raw_memory(self, number):
820
        return repr(self._memobj.memory[number])
821

    
822
    def _decode_tone(self, val):
823
        """Parse the tone data to decode from mem, it returns:
824
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
825
        pol = None
826

    
827
        if val in [0, 65535]:
828
            return '', None, None
829
        elif val > 0x0258:
830
            a = val / 10.0
831
            return 'Tone', a, pol
832
        else:
833
            if val > 0x69:
834
                index = val - 0x6A
835
                pol = "R"
836
            else:
837
                index = val - 1
838
                pol = "N"
839

    
840
            tone = DTCS[index]
841
            return 'DTCS', tone, pol
842

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

    
867
    def get_memory(self, number):
868
        """Get the mem representation from the radio image"""
869
        _mem = self._memobj.memory[number]
870
        _names = self._memobj.names[number]
871

    
872
        # Create a high-level memory object to return to the UI
873
        mem = chirp_common.Memory()
874

    
875
        # Memory number
876
        mem.number = number
877

    
878
        if _mem.get_raw()[0] == "\xFF":
879
            mem.empty = True
880
            return mem
881

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

    
906
        # name TAG of the channel
907
        mem.name = str(_names.name).rstrip("\xFF").replace("\xFF", " ")
908

    
909
        # power
910
        mem.power = POWER_LEVELS[int(_mem.power)]
911

    
912
        # wide/narrow
913
        mem.mode = MODES[int(_mem.wide)]
914

    
915
        # skip
916
        mem.skip = SKIP_VALUES[_mem.add]
917

    
918
        # tone data
919
        rxtone = txtone = None
920
        txtone = self._decode_tone(_mem.txtone)
921
        rxtone = self._decode_tone(_mem.rxtone)
922
        chirp_common.split_tone_decode(mem, txtone, rxtone)
923

    
924
        # Extra
925
        mem.extra = RadioSettingGroup("extra", "Extra")
926

    
927
        if not self.COLOR_LCD or \
928
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
929
            scramble = RadioSetting("scramble", "Scramble",
930
                                    RadioSettingValueBoolean(bool(
931
                                        _mem.scramble)))
932
            mem.extra.append(scramble)
933

    
934
        bcl = RadioSetting("bcl", "Busy channel lockout",
935
                           RadioSettingValueBoolean(bool(_mem.bcl)))
936
        mem.extra.append(bcl)
937

    
938
        pttid = RadioSetting("pttid", "PTT ID",
939
                             RadioSettingValueList(PTTID_LIST,
940
                                                   PTTID_LIST[_mem.pttid]))
941
        mem.extra.append(pttid)
942

    
943
        # validating scode
944
        scode = _mem.scode if _mem.scode != 15 else 0
945
        pttidcode = RadioSetting("scode", "PTT ID signal code",
946
                                 RadioSettingValueList(
947
                                     PTTIDCODE_LIST,
948
                                     PTTIDCODE_LIST[scode]))
949
        mem.extra.append(pttidcode)
950

    
951
        optsig = RadioSetting("optsig", "Optional signaling",
952
                              RadioSettingValueList(
953
                                  OPTSIG_LIST,
954
                                  OPTSIG_LIST[_mem.optsig]))
955
        mem.extra.append(optsig)
956

    
957
        spmute = RadioSetting("spmute", "Speaker mute",
958
                              RadioSettingValueList(
959
                                  SPMUTE_LIST,
960
                                  SPMUTE_LIST[_mem.spmute]))
961
        mem.extra.append(spmute)
962

    
963
        return mem
964

    
965
    def set_memory(self, mem):
966
        """Set the memory data in the eeprom img from the UI"""
967
        # get the eprom representation of this channel
968
        _mem = self._memobj.memory[mem.number]
969
        _names = self._memobj.names[mem.number]
970

    
971
        mem_was_empty = False
972
        # same method as used in get_memory for determining if mem is empty
973
        # doing this BEFORE overwriting it with new values ...
974
        if _mem.get_raw()[0] == "\xFF":
975
            LOG.debug("This mem was empty before")
976
            mem_was_empty = True
977

    
978
        # if empty memmory
979
        if mem.empty:
980
            # the channel itself
981
            _mem.set_raw("\xFF" * 16)
982
            # the name tag
983
            _names.set_raw("\xFF" * 16)
984
            return
985

    
986
        if mem_was_empty:
987
            # Zero the whole memory if we're making it unempty for
988
            # the first time
989
            LOG.debug('Zeroing new memory')
990
            _mem.set_raw('\x00' * 16)
991

    
992
        # frequency
993
        _mem.rxfreq = mem.freq / 10
994

    
995
        # duplex
996
        if mem.duplex == "+":
997
            _mem.txfreq = (mem.freq + mem.offset) / 10
998
        elif mem.duplex == "-":
999
            _mem.txfreq = (mem.freq - mem.offset) / 10
1000
        elif mem.duplex == "off":
1001
            for i in _mem.txfreq:
1002
                i.set_raw("\xFF")
1003
        elif mem.duplex == "split":
1004
            _mem.txfreq = mem.offset / 10
1005
        else:
1006
            _mem.txfreq = mem.freq / 10
1007

    
1008
        # tone data
1009
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
1010
            chirp_common.split_tone_encode(mem)
1011
        self._encode_tone(_mem.txtone, txmode, txtone, txpol)
1012
        self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)
1013

    
1014
        # name TAG of the channel
1015
        if len(mem.name) < self.NAME_LENGTH:
1016
            # we must pad to self.NAME_LENGTH chars, " " = "\xFF"
1017
            mem.name = str(mem.name).ljust(self.NAME_LENGTH, " ")
1018
        _names.name = str(mem.name).replace(" ", "\xFF")
1019

    
1020
        # power, # default power level is high
1021
        _mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)
1022

    
1023
        # wide/narrow
1024
        _mem.wide = MODES.index(mem.mode)
1025

    
1026
        # scan add property
1027
        _mem.add = SKIP_VALUES.index(mem.skip)
1028

    
1029
        # reseting unknowns, this have to be set by hand
1030
        _mem.unknown0 = 0
1031
        _mem.unknown1 = 0
1032
        _mem.unknown2 = 0
1033
        _mem.unknown3 = 0
1034
        _mem.unknown4 = 0
1035
        _mem.unknown5 = 0
1036
        _mem.unknown6 = 0
1037

    
1038
        def _zero_settings():
1039
            _mem.spmute = 0
1040
            _mem.optsig = 0
1041
            _mem.scramble = 0
1042
            _mem.bcl = 0
1043
            _mem.pttid = 0
1044
            _mem.scode = 0
1045

    
1046
        if self.COLOR_LCD and _mem.scramble:
1047
            LOG.info('Resetting scramble bit for BTECH COLOR_LCD variant')
1048
            _mem.scramble = 0
1049

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

    
1070
        return mem
1071

    
1072
    def get_settings(self):
1073
        """Translate the bit in the mem_struct into settings in the UI"""
1074
        _mem = self._memobj
1075
        basic = RadioSettingGroup("basic", "Basic Settings")
1076
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
1077
        other = RadioSettingGroup("other", "Other Settings")
1078
        work = RadioSettingGroup("work", "Work Mode Settings")
1079
        top = RadioSettings(basic, advanced, other, work)
1080

    
1081
        # Basic
1082
        if self.COLOR_LCD:
1083
            tmr = RadioSetting("settings.tmr", "Transceiver multi-receive",
1084
                               RadioSettingValueList(
1085
                                   self.LIST_TMR,
1086
                                   self.LIST_TMR[_mem.settings.tmr]))
1087
            basic.append(tmr)
1088
        else:
1089
            tdr = RadioSetting("settings.tdr", "Transceiver dual receive",
1090
                               RadioSettingValueBoolean(_mem.settings.tdr))
1091
            basic.append(tdr)
1092

    
1093
        sql = RadioSetting("settings.sql", "Squelch level",
1094
                           RadioSettingValueInteger(0, 9, _mem.settings.sql))
1095
        basic.append(sql)
1096

    
1097
        if self.MODEL == "GMRS-50X1":
1098
            autolk = RadioSetting("settings.autolk", "Auto keylock",
1099
                                  RadioSettingValueBoolean(
1100
                                      _mem.settings.autolk))
1101
            basic.append(autolk)
1102

    
1103
        if self.MODEL == "DM25-G":
1104
            mgain2 = RadioSetting("settings.mgain2", "Mic gain",
1105
                                  RadioSettingValueInteger(0, 127,
1106
                                                           _mem.settings.mgain2))
1107
            basic.append(mgain2)
1108

    
1109
        tot = RadioSetting("settings.tot", "Time out timer",
1110
                           RadioSettingValueList(
1111
                               LIST_TOT,
1112
                               LIST_TOT[_mem.settings.tot]))
1113
        basic.append(tot)
1114

    
1115
        if self.MODEL == "KT-8R":
1116
                save = RadioSetting("settings.save", "Battery Save",
1117
                                    RadioSettingValueBoolean(
1118
                                        _mem.settings.save))
1119
                basic.append(save)
1120

    
1121
        if not self.MODEL == "KT-8R":
1122
            if self.VENDOR == "BTECH" or self.COLOR_LCD:
1123
                apo = RadioSetting("settings.apo", "Auto power off timer",
1124
                                   RadioSettingValueList(
1125
                                       LIST_APO,
1126
                                       LIST_APO[_mem.settings.apo]))
1127
                basic.append(apo)
1128
            else:
1129
                toa = RadioSetting("settings.apo", "Time out alert timer",
1130
                                   RadioSettingValueList(
1131
                                       LIST_OFF1TO10,
1132
                                       LIST_OFF1TO10[_mem.settings.apo]))
1133
                basic.append(toa)
1134

    
1135
        abr = RadioSetting("settings.abr", "Backlight timer",
1136
                           RadioSettingValueList(
1137
                               LIST_OFF1TO50,
1138
                               LIST_OFF1TO50[_mem.settings.abr]))
1139
        basic.append(abr)
1140

    
1141
        beep = RadioSetting("settings.beep", "Key beep",
1142
                            RadioSettingValueBoolean(_mem.settings.beep))
1143
        basic.append(beep)
1144

    
1145
        if self.MODEL == "KT-8R":
1146
                dsub = RadioSetting("settings.dsub", "CTCSS/DCS code display",
1147
                                    RadioSettingValueBoolean(
1148
                                        _mem.settings.dsub))
1149
                basic.append(dsub)
1150

    
1151
        if self.MODEL == "KT-8R":
1152
                dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1153
                                      RadioSettingValueBoolean(
1154
                                          _mem.settings.dtmfst))
1155
                basic.append(dtmfst)
1156
        else:
1157
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1158
                                  RadioSettingValueList(
1159
                                      LIST_DTMFST,
1160
                                      LIST_DTMFST[_mem.settings.dtmfst]))
1161
            basic.append(dtmfst)
1162

    
1163
        if not self.COLOR_LCD:
1164
            prisc = RadioSetting("settings.prisc", "Priority scan",
1165
                                 RadioSettingValueBoolean(
1166
                                     _mem.settings.prisc))
1167
            basic.append(prisc)
1168

    
1169
            prich = RadioSetting("settings.prich", "Priority channel",
1170
                                 RadioSettingValueInteger(0, self._upper,
1171
                                                          _mem.settings.prich))
1172
            basic.append(prich)
1173

    
1174
        screv = RadioSetting("settings.screv", "Scan resume method",
1175
                             RadioSettingValueList(
1176
                                 LIST_SCREV,
1177
                                 LIST_SCREV[_mem.settings.screv]))
1178
        basic.append(screv)
1179

    
1180
        pttlt = RadioSetting("settings.pttlt", "PTT transmit delay",
1181
                             RadioSettingValueInteger(0, 30,
1182
                                                      _mem.settings.pttlt))
1183
        basic.append(pttlt)
1184

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

    
1198
        emcch = RadioSetting("settings.emcch", "Alarm channel",
1199
                             RadioSettingValueInteger(0, self._upper,
1200
                                                      _mem.settings.emcch))
1201
        basic.append(emcch)
1202

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

    
1218
        camdf = RadioSetting("settings.camdf", "Display mode A",
1219
                             RadioSettingValueList(
1220
                                 LIST_MDF,
1221
                                 LIST_MDF[_mem.settings.camdf]))
1222
        basic.append(camdf)
1223

    
1224
        cbmdf = RadioSetting("settings.cbmdf", "Display mode B",
1225
                             RadioSettingValueList(
1226
                                 LIST_MDF,
1227
                                 LIST_MDF[_mem.settings.cbmdf]))
1228
        basic.append(cbmdf)
1229

    
1230
        if self.COLOR_LCD:
1231
            ccmdf = RadioSetting("settings.ccmdf", "Display mode C",
1232
                                 RadioSettingValueList(
1233
                                     LIST_MDF,
1234
                                     LIST_MDF[_mem.settings.ccmdf]))
1235
            basic.append(ccmdf)
1236

    
1237
            cdmdf = RadioSetting("settings.cdmdf", "Display mode D",
1238
                                 RadioSettingValueList(
1239
                                     LIST_MDF,
1240
                                     LIST_MDF[_mem.settings.cdmdf]))
1241
            basic.append(cdmdf)
1242

    
1243
            langua = RadioSetting("settings.langua", "Language",
1244
                                  RadioSettingValueList(
1245
                                      LIST_LANGUA,
1246
                                      LIST_LANGUA[_mem.settings.langua]))
1247
            basic.append(langua)
1248

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

    
1256
            vox = RadioSetting("settings.vox", "VOX",
1257
                               RadioSettingValueList(
1258
                                   LIST_VOX,
1259
                                   LIST_VOX[_mem.settings.vox]))
1260
            basic.append(vox)
1261

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

    
1268
        if self.VENDOR == "BTECH":
1269
            if self.COLOR_LCD:
1270
                sync = RadioSetting("settings.sync", "Channel display sync",
1271
                                    RadioSettingValueList(
1272
                                        LIST_SYNC,
1273
                                        LIST_SYNC[_mem.settings.sync]))
1274
                basic.append(sync)
1275
            else:
1276
                sync = RadioSetting("settings.sync", "A/B channel sync",
1277
                                    RadioSettingValueBoolean(
1278
                                        _mem.settings.sync))
1279
                basic.append(sync)
1280
        else:
1281
            autolk = RadioSetting("settings.sync", "Auto keylock",
1282
                                  RadioSettingValueBoolean(
1283
                                      _mem.settings.sync))
1284
            basic.append(autolk)
1285

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

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

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

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

    
1314
            menubc = RadioSetting("settings.menubc", "Menu background color",
1315
                                  RadioSettingValueList(
1316
                                      LIST_COLOR9,
1317
                                      LIST_COLOR9[_mem.settings.menubc]))
1318
            basic.append(menubc)
1319

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

    
1327
            stabc = RadioSetting("settings.stabc",
1328
                                 "Top status background color",
1329
                                 RadioSettingValueList(
1330
                                     LIST_COLOR9,
1331
                                     LIST_COLOR9[_mem.settings.stabc]))
1332
            basic.append(stabc)
1333

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

    
1341
            sigbc = RadioSetting("settings.sigbc",
1342
                                 "Bottom status background color",
1343
                                 RadioSettingValueList(
1344
                                     LIST_COLOR9,
1345
                                     LIST_COLOR9[_mem.settings.sigbc]))
1346
            basic.append(sigbc)
1347

    
1348
            rxfc = RadioSetting("settings.rxfc", "Receiving character color",
1349
                                RadioSettingValueList(
1350
                                    LIST_COLOR9,
1351
                                    LIST_COLOR9[_mem.settings.rxfc]))
1352
            basic.append(rxfc)
1353

    
1354
            txfc = RadioSetting("settings.txfc",
1355
                                "Transmitting character color",
1356
                                RadioSettingValueList(
1357
                                    LIST_COLOR9,
1358
                                    LIST_COLOR9[_mem.settings.txfc]))
1359
            basic.append(txfc)
1360

    
1361
            txdisp = RadioSetting("settings.txdisp",
1362
                                  "Transmitting status display",
1363
                                  RadioSettingValueList(
1364
                                      LIST_TXDISP,
1365
                                      LIST_TXDISP[_mem.settings.txdisp]))
1366
            basic.append(txdisp)
1367
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1368
            stfc = RadioSetting("settings.stfc",
1369
                                "ST-FC",
1370
                                RadioSettingValueList(
1371
                                    LIST_COLOR8,
1372
                                    LIST_COLOR8[_mem.settings.stfc]))
1373
            basic.append(stfc)
1374

    
1375
            mffc = RadioSetting("settings.mffc",
1376
                                "MF-FC",
1377
                                RadioSettingValueList(
1378
                                    LIST_COLOR8,
1379
                                    LIST_COLOR8[_mem.settings.mffc]))
1380
            basic.append(mffc)
1381

    
1382
            sfafc = RadioSetting("settings.sfafc",
1383
                                 "SFA-FC",
1384
                                 RadioSettingValueList(
1385
                                     LIST_COLOR8,
1386
                                     LIST_COLOR8[_mem.settings.sfafc]))
1387
            basic.append(sfafc)
1388

    
1389
            sfbfc = RadioSetting("settings.sfbfc",
1390
                                 "SFB-FC",
1391
                                 RadioSettingValueList(
1392
                                     LIST_COLOR8,
1393
                                     LIST_COLOR8[_mem.settings.sfbfc]))
1394
            basic.append(sfbfc)
1395

    
1396
            sfcfc = RadioSetting("settings.sfcfc",
1397
                                 "SFC-FC",
1398
                                 RadioSettingValueList(
1399
                                     LIST_COLOR8,
1400
                                     LIST_COLOR8[_mem.settings.sfcfc]))
1401
            basic.append(sfcfc)
1402

    
1403
            sfdfc = RadioSetting("settings.sfdfc",
1404
                                 "SFD-FC",
1405
                                 RadioSettingValueList(
1406
                                     LIST_COLOR8,
1407
                                     LIST_COLOR8[_mem.settings.sfdfc]))
1408
            basic.append(sfdfc)
1409

    
1410
            subfc = RadioSetting("settings.subfc",
1411
                                 "SUB-FC",
1412
                                 RadioSettingValueList(
1413
                                     LIST_COLOR8,
1414
                                     LIST_COLOR8[_mem.settings.subfc]))
1415
            basic.append(subfc)
1416

    
1417
            fmfc = RadioSetting("settings.fmfc",
1418
                                "FM-FC",
1419
                                RadioSettingValueList(
1420
                                    LIST_COLOR8,
1421
                                    LIST_COLOR8[_mem.settings.fmfc]))
1422
            basic.append(fmfc)
1423

    
1424
            sigfc = RadioSetting("settings.sigfc",
1425
                                 "SIG-FC",
1426
                                 RadioSettingValueList(
1427
                                     LIST_COLOR8,
1428
                                     LIST_COLOR8[_mem.settings.sigfc]))
1429
            basic.append(sigfc)
1430

    
1431
            if not self.MODEL == "KT-8R":
1432
                modfc = RadioSetting("settings.modfc",
1433
                                     "MOD-FC",
1434
                                     RadioSettingValueList(
1435
                                         LIST_COLOR8,
1436
                                         LIST_COLOR8[_mem.settings.modfc]))
1437
                basic.append(modfc)
1438

    
1439
            menufc = RadioSetting("settings.menufc",
1440
                                  "MENUFC",
1441
                                  RadioSettingValueList(
1442
                                      LIST_COLOR8,
1443
                                      LIST_COLOR8[_mem.settings.menufc]))
1444
            basic.append(menufc)
1445

    
1446
            txfc = RadioSetting("settings.txfc",
1447
                                "TX-FC",
1448
                                RadioSettingValueList(
1449
                                    LIST_COLOR8,
1450
                                    LIST_COLOR8[_mem.settings.txfc]))
1451
            basic.append(txfc)
1452

    
1453
            if self.MODEL == "KT-8R":
1454
                rxfc = RadioSetting("settings.rxfc",
1455
                                    "RX-FC",
1456
                                    RadioSettingValueList(
1457
                                        LIST_COLOR8,
1458
                                        LIST_COLOR8[_mem.settings.rxfc]))
1459
                basic.append(rxfc)
1460

    
1461
            if not self.MODEL == "KT-8R":
1462
                txdisp = RadioSetting("settings.txdisp",
1463
                                      "Transmitting status display",
1464
                                      RadioSettingValueList(
1465
                                          LIST_TXDISP,
1466
                                          LIST_TXDISP[_mem.settings.txdisp]))
1467
                basic.append(txdisp)
1468
        else:
1469
            wtled = RadioSetting("settings.wtled", "Standby backlight Color",
1470
                                 RadioSettingValueList(
1471
                                     LIST_COLOR4,
1472
                                     LIST_COLOR4[_mem.settings.wtled]))
1473
            basic.append(wtled)
1474

    
1475
            rxled = RadioSetting("settings.rxled", "RX backlight Color",
1476
                                 RadioSettingValueList(
1477
                                     LIST_COLOR4,
1478
                                     LIST_COLOR4[_mem.settings.rxled]))
1479
            basic.append(rxled)
1480

    
1481
            txled = RadioSetting("settings.txled", "TX backlight Color",
1482
                                 RadioSettingValueList(
1483
                                     LIST_COLOR4,
1484
                                     LIST_COLOR4[_mem.settings.txled]))
1485
            basic.append(txled)
1486

    
1487
        anil = RadioSetting("settings.anil", "ANI length",
1488
                            RadioSettingValueList(
1489
                                LIST_ANIL,
1490
                                LIST_ANIL[_mem.settings.anil]))
1491
        basic.append(anil)
1492

    
1493
        reps = RadioSetting("settings.reps", "Relay signal (tone burst)",
1494
                            RadioSettingValueList(
1495
                                LIST_REPS,
1496
                                LIST_REPS[_mem.settings.reps]))
1497
        basic.append(reps)
1498

    
1499
        if not self.MODEL == "GMRS-50X1" and not self.MODEL == "KT-8R":
1500
            repm = RadioSetting("settings.repm", "Relay condition",
1501
                                RadioSettingValueList(
1502
                                    LIST_REPM,
1503
                                    LIST_REPM[_mem.settings.repm]))
1504
            basic.append(repm)
1505

    
1506
        if self.VENDOR == "BTECH" or self.COLOR_LCD:
1507
            if self.COLOR_LCD:
1508
                tmrmr = RadioSetting("settings.tmrmr", "TMR return time",
1509
                                     RadioSettingValueList(
1510
                                         LIST_OFF1TO50,
1511
                                         LIST_OFF1TO50[_mem.settings.tmrmr]))
1512
                basic.append(tmrmr)
1513
            else:
1514
                tdrab = RadioSetting("settings.tdrab", "TDR return time",
1515
                                     RadioSettingValueList(
1516
                                         LIST_OFF1TO50,
1517
                                         LIST_OFF1TO50[_mem.settings.tdrab]))
1518
                basic.append(tdrab)
1519

    
1520
            ste = RadioSetting("settings.ste", "Squelch tail eliminate",
1521
                               RadioSettingValueBoolean(_mem.settings.ste))
1522
            basic.append(ste)
1523

    
1524
            rpste = RadioSetting("settings.rpste", "Repeater STE",
1525
                                 RadioSettingValueList(
1526
                                     LIST_OFF1TO9,
1527
                                     LIST_OFF1TO9[_mem.settings.rpste]))
1528
            basic.append(rpste)
1529

    
1530
            rptdl = RadioSetting("settings.rptdl", "Repeater STE delay",
1531
                                 RadioSettingValueList(
1532
                                     LIST_RPTDL,
1533
                                     LIST_RPTDL[_mem.settings.rptdl]))
1534
            basic.append(rptdl)
1535

    
1536
        if str(_mem.fingerprint.fp) in BTECH3:
1537
            mgain = RadioSetting("settings.mgain", "Mic gain",
1538
                                 RadioSettingValueInteger(0, 120,
1539
                                                          _mem.settings.mgain))
1540
            basic.append(mgain)
1541

    
1542
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1543
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain",
1544
                                 RadioSettingValueInteger(0, 60,
1545
                                                          _mem.settings.dtmfg))
1546
            basic.append(dtmfg)
1547

    
1548
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1549
            mgain = RadioSetting("settings.mgain", "Mic gain",
1550
                                 RadioSettingValueInteger(0, 120,
1551
                                                          _mem.settings.mgain))
1552
            basic.append(mgain)
1553

    
1554
            skiptx = RadioSetting("settings.skiptx", "Skip TX",
1555
                                  RadioSettingValueList(
1556
                                      LIST_SKIPTX,
1557
                                      LIST_SKIPTX[_mem.settings.skiptx]))
1558
            basic.append(skiptx)
1559

    
1560
            scmode = RadioSetting("settings.scmode", "Scan mode",
1561
                                  RadioSettingValueList(
1562
                                      LIST_SCMODE,
1563
                                      LIST_SCMODE[_mem.settings.scmode]))
1564
            basic.append(scmode)
1565

    
1566
        if self.MODEL == "KT-8R":
1567
            tmrtx = RadioSetting("settings.tmrtx", "TX in multi-standby",
1568
                                 RadioSettingValueList(
1569
                                     LIST_TMRTX,
1570
                                     LIST_TMRTX[_mem.settings.tmrtx]))
1571
            basic.append(tmrtx)
1572

    
1573
        # Advanced
1574
        def _filter(name):
1575
            filtered = ""
1576
            for char in str(name):
1577
                if char in VALID_CHARS:
1578
                    filtered += char
1579
                else:
1580
                    filtered += " "
1581
            return filtered
1582

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

    
1643
        if self.MODEL in ("UV-2501", "UV-5001"):
1644
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1645
                                   RadioSettingValueBoolean(
1646
                                       _mem.settings2.vfomren))
1647
            advanced.append(vfomren)
1648

    
1649
            reseten = RadioSetting("settings2.reseten", "RESET",
1650
                                   RadioSettingValueBoolean(
1651
                                       _mem.settings2.reseten))
1652
            advanced.append(reseten)
1653

    
1654
            menuen = RadioSetting("settings2.menuen", "Menu",
1655
                                  RadioSettingValueBoolean(
1656
                                      _mem.settings2.menuen))
1657
            advanced.append(menuen)
1658

    
1659
        # Other
1660
        def convert_bytes_to_limit(bytes):
1661
            limit = ""
1662
            for byte in bytes:
1663
                if byte < 10:
1664
                    limit += chr(byte + 0x30)
1665
                else:
1666
                    break
1667
            return limit
1668

    
1669
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1670
            _ranges = self._memobj.ranges220
1671
            ranges = "ranges220"
1672
        else:
1673
            _ranges = self._memobj.ranges
1674
            ranges = "ranges"
1675

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

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

    
1688
        if self.BANDS == 3 or self.BANDS == 4:
1689
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1690
            val = RadioSettingValueString(0, 3, _limit)
1691
            val.set_mutable(False)
1692
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1693
            other.append(vhf2_low)
1694

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

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

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

    
1713
        if self.BANDS == 4:
1714
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1715
            val = RadioSettingValueString(0, 3, _limit)
1716
            val.set_mutable(False)
1717
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1718
            other.append(uhf2_low)
1719

    
1720
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1721
            val = RadioSettingValueString(0, 3, _limit)
1722
            val.set_mutable(False)
1723
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1724
            other.append(uhf2_high)
1725

    
1726
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1727
        val.set_mutable(False)
1728
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1729
        other.append(fp)
1730

    
1731
        # Work
1732
        if self.COLOR_LCD:
1733
            dispab = RadioSetting("settings2.dispab", "Display",
1734
                                  RadioSettingValueList(
1735
                                      LIST_ABCD,
1736
                                      LIST_ABCD[_mem.settings2.dispab]))
1737
            work.append(dispab)
1738
        else:
1739
            dispab = RadioSetting("settings2.dispab", "Display",
1740
                                  RadioSettingValueList(
1741
                                      LIST_AB,
1742
                                      LIST_AB[_mem.settings2.dispab]))
1743
            work.append(dispab)
1744

    
1745
        if self.COLOR_LCD:
1746
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode",
1747
                                  RadioSettingValueList(
1748
                                      LIST_VFOMR,
1749
                                      LIST_VFOMR[_mem.settings2.vfomra]))
1750
            work.append(vfomra)
1751

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

    
1758
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode",
1759
                                  RadioSettingValueList(
1760
                                      LIST_VFOMR,
1761
                                      LIST_VFOMR[_mem.settings2.vfomrc]))
1762
            work.append(vfomrc)
1763

    
1764
            vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode",
1765
                                  RadioSettingValueList(
1766
                                      LIST_VFOMR,
1767
                                      LIST_VFOMR[_mem.settings2.vfomrd]))
1768
            work.append(vfomrd)
1769
        else:
1770
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode",
1771
                                 RadioSettingValueList(
1772
                                     LIST_VFOMR,
1773
                                     LIST_VFOMR[_mem.settings2.vfomr]))
1774
            work.append(vfomr)
1775

    
1776
        keylock = RadioSetting("settings2.keylock", "Keypad lock",
1777
                               RadioSettingValueBoolean(
1778
                                   _mem.settings2.keylock))
1779
        work.append(keylock)
1780

    
1781
        mrcha = RadioSetting("settings2.mrcha", "MR A channel",
1782
                             RadioSettingValueInteger(0, self._upper,
1783
                                                      _mem.settings2.mrcha))
1784
        work.append(mrcha)
1785

    
1786
        mrchb = RadioSetting("settings2.mrchb", "MR B channel",
1787
                             RadioSettingValueInteger(0, self._upper,
1788
                                                      _mem.settings2.mrchb))
1789
        work.append(mrchb)
1790

    
1791
        if self.COLOR_LCD:
1792
            mrchc = RadioSetting("settings2.mrchc", "MR C channel",
1793
                                 RadioSettingValueInteger(
1794
                                     0, self._upper, _mem.settings2.mrchc))
1795
            work.append(mrchc)
1796

    
1797
            mrchd = RadioSetting("settings2.mrchd", "MR D channel",
1798
                                 RadioSettingValueInteger(
1799
                                     0, self._upper, _mem.settings2.mrchd))
1800
            work.append(mrchd)
1801

    
1802
        def convert_bytes_to_freq(bytes):
1803
            real_freq = 0
1804
            for byte in bytes:
1805
                real_freq = (real_freq * 10) + byte
1806
            return chirp_common.format_freq(real_freq * 10)
1807

    
1808
        def my_validate(value):
1809
            _vhf_lower = int(convert_bytes_to_limit(_ranges.vhf_low))
1810
            _vhf_upper = int(convert_bytes_to_limit(_ranges.vhf_high))
1811
            _uhf_lower = int(convert_bytes_to_limit(_ranges.uhf_low))
1812
            _uhf_upper = int(convert_bytes_to_limit(_ranges.uhf_high))
1813
            if self.BANDS == 3 or self.BANDS == 4:
1814
                _vhf2_lower = int(convert_bytes_to_limit(_ranges.vhf2_low))
1815
                _vhf2_upper = int(convert_bytes_to_limit(_ranges.vhf2_high))
1816
            if self.BANDS == 4:
1817
                _uhf2_lower = int(convert_bytes_to_limit(_ranges.uhf2_low))
1818
                _uhf2_upper = int(convert_bytes_to_limit(_ranges.uhf2_high))
1819

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

    
1851
        def apply_freq(setting, obj):
1852
            value = chirp_common.parse_freq(str(setting.value)) / 10
1853
            for i in range(7, -1, -1):
1854
                obj.freq[i] = value % 10
1855
                value /= 10
1856

    
1857
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1858
                                        _mem.vfo.a.freq))
1859
        val1a.set_validate_callback(my_validate)
1860
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
1861
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
1862
        work.append(vfoafreq)
1863

    
1864
        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1865
                                        _mem.vfo.b.freq))
1866
        val1b.set_validate_callback(my_validate)
1867
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
1868
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
1869
        work.append(vfobfreq)
1870

    
1871
        if self.COLOR_LCD:
1872
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1873
                                            _mem.vfo.c.freq))
1874
            val1c.set_validate_callback(my_validate)
1875
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
1876
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
1877
            work.append(vfocfreq)
1878

    
1879
            val1d = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1880
                                            _mem.vfo.d.freq))
1881
            val1d.set_validate_callback(my_validate)
1882
            vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
1883
            vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
1884
            work.append(vfodfreq)
1885

    
1886
        if not self.MODEL == "GMRS-50X1":
1887
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift",
1888
                                      RadioSettingValueList(
1889
                                          LIST_SHIFT,
1890
                                          LIST_SHIFT[_mem.vfo.a.shiftd]))
1891
            work.append(vfoashiftd)
1892

    
1893
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift",
1894
                                      RadioSettingValueList(
1895
                                          LIST_SHIFT,
1896
                                          LIST_SHIFT[_mem.vfo.b.shiftd]))
1897
            work.append(vfobshiftd)
1898

    
1899
            if self.COLOR_LCD:
1900
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift",
1901
                                          RadioSettingValueList(
1902
                                              LIST_SHIFT,
1903
                                              LIST_SHIFT[_mem.vfo.c.shiftd]))
1904
                work.append(vfocshiftd)
1905

    
1906
                vfodshiftd = RadioSetting("vfo.d.shiftd", "VFO D shift",
1907
                                          RadioSettingValueList(
1908
                                              LIST_SHIFT,
1909
                                              LIST_SHIFT[_mem.vfo.d.shiftd]))
1910
                work.append(vfodshiftd)
1911

    
1912
        def convert_bytes_to_offset(bytes):
1913
            real_offset = 0
1914
            for byte in bytes:
1915
                real_offset = (real_offset * 10) + byte
1916
            return chirp_common.format_freq(real_offset * 1000)
1917

    
1918
        def apply_offset(setting, obj):
1919
            value = chirp_common.parse_freq(str(setting.value)) / 1000
1920
            for i in range(5, -1, -1):
1921
                obj.offset[i] = value % 10
1922
                value /= 10
1923

    
1924
        if not self.MODEL == "GMRS-50X1":
1925
            if self.COLOR_LCD:
1926
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1927
                                                _mem.vfo.a.offset))
1928
                vfoaoffset = RadioSetting("vfo.a.offset",
1929
                                          "VFO A offset (0.000-999.999)",
1930
                                          val1a)
1931
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
1932
                work.append(vfoaoffset)
1933

    
1934
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1935
                                                _mem.vfo.b.offset))
1936
                vfoboffset = RadioSetting("vfo.b.offset",
1937
                                          "VFO B offset (0.000-999.999)",
1938
                                          val1b)
1939
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1940
                work.append(vfoboffset)
1941

    
1942
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1943
                                                _mem.vfo.c.offset))
1944
                vfocoffset = RadioSetting("vfo.c.offset",
1945
                                          "VFO C offset (0.000-999.999)",
1946
                                          val1c)
1947
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
1948
                work.append(vfocoffset)
1949

    
1950
                val1d = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1951
                                                _mem.vfo.d.offset))
1952
                vfodoffset = RadioSetting("vfo.d.offset",
1953
                                          "VFO D offset (0.000-999.999)",
1954
                                          val1d)
1955
                vfodoffset.set_apply_callback(apply_offset, _mem.vfo.d)
1956
                work.append(vfodoffset)
1957
            else:
1958
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1959
                                                _mem.vfo.a.offset))
1960
                vfoaoffset = RadioSetting("vfo.a.offset",
1961
                                          "VFO A offset (0.000-99.999)", val1a)
1962
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
1963
                work.append(vfoaoffset)
1964

    
1965
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1966
                                                _mem.vfo.b.offset))
1967
                vfoboffset = RadioSetting("vfo.b.offset",
1968
                                          "VFO B offset (0.000-99.999)", val1b)
1969
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1970
                work.append(vfoboffset)
1971

    
1972
        if not self.MODEL == "GMRS-50X1":
1973
            vfoatxp = RadioSetting("vfo.a.power", "VFO A power",
1974
                                   RadioSettingValueList(
1975
                                       LIST_TXP,
1976
                                       LIST_TXP[_mem.vfo.a.power]))
1977
            work.append(vfoatxp)
1978

    
1979
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power",
1980
                                   RadioSettingValueList(
1981
                                       LIST_TXP,
1982
                                       LIST_TXP[_mem.vfo.b.power]))
1983
            work.append(vfobtxp)
1984

    
1985
            if self.COLOR_LCD:
1986
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power",
1987
                                       RadioSettingValueList(
1988
                                           LIST_TXP,
1989
                                           LIST_TXP[_mem.vfo.c.power]))
1990
                work.append(vfoctxp)
1991

    
1992
                vfodtxp = RadioSetting("vfo.d.power", "VFO D power",
1993
                                       RadioSettingValueList(
1994
                                           LIST_TXP,
1995
                                           LIST_TXP[_mem.vfo.d.power]))
1996
                work.append(vfodtxp)
1997

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

    
2005
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth",
2006
                                    RadioSettingValueList(
2007
                                        LIST_WIDE,
2008
                                        LIST_WIDE[_mem.vfo.b.wide]))
2009
            work.append(vfobwide)
2010

    
2011
            if self.COLOR_LCD:
2012
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth",
2013
                                        RadioSettingValueList(
2014
                                            LIST_WIDE,
2015
                                            LIST_WIDE[_mem.vfo.c.wide]))
2016
                work.append(vfocwide)
2017

    
2018
                vfodwide = RadioSetting("vfo.d.wide", "VFO D bandwidth",
2019
                                        RadioSettingValueList(
2020
                                            LIST_WIDE,
2021
                                            LIST_WIDE[_mem.vfo.d.wide]))
2022
                work.append(vfodwide)
2023

    
2024
        vfoastep = RadioSetting("vfo.a.step", "VFO A step",
2025
                                RadioSettingValueList(
2026
                                    LIST_STEP,
2027
                                    LIST_STEP[_mem.vfo.a.step]))
2028
        work.append(vfoastep)
2029

    
2030
        vfobstep = RadioSetting("vfo.b.step", "VFO B step",
2031
                                RadioSettingValueList(
2032
                                    LIST_STEP,
2033
                                    LIST_STEP[_mem.vfo.b.step]))
2034
        work.append(vfobstep)
2035

    
2036
        if self.COLOR_LCD:
2037
            vfocstep = RadioSetting("vfo.c.step", "VFO C step",
2038
                                    RadioSettingValueList(
2039
                                        LIST_STEP,
2040
                                        LIST_STEP[_mem.vfo.c.step]))
2041
            work.append(vfocstep)
2042

    
2043
            vfodstep = RadioSetting("vfo.d.step", "VFO D step",
2044
                                    RadioSettingValueList(
2045
                                        LIST_STEP,
2046
                                        LIST_STEP[_mem.vfo.d.step]))
2047
            work.append(vfodstep)
2048

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

    
2055
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal",
2056
                                  RadioSettingValueList(
2057
                                      OPTSIG_LIST,
2058
                                      OPTSIG_LIST[_mem.vfo.b.optsig]))
2059
        work.append(vfoboptsig)
2060

    
2061
        if self.COLOR_LCD:
2062
            vfocoptsig = RadioSetting("vfo.c.optsig", "VFO C optional signal",
2063
                                      RadioSettingValueList(
2064
                                          OPTSIG_LIST,
2065
                                          OPTSIG_LIST[_mem.vfo.c.optsig]))
2066
            work.append(vfocoptsig)
2067

    
2068
            vfodoptsig = RadioSetting("vfo.d.optsig", "VFO D optional signal",
2069
                                      RadioSettingValueList(
2070
                                          OPTSIG_LIST,
2071
                                          OPTSIG_LIST[_mem.vfo.d.optsig]))
2072
            work.append(vfodoptsig)
2073

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

    
2080
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute",
2081
                                  RadioSettingValueList(
2082
                                      SPMUTE_LIST,
2083
                                      SPMUTE_LIST[_mem.vfo.b.spmute]))
2084
        work.append(vfobspmute)
2085

    
2086
        if self.COLOR_LCD:
2087
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute",
2088
                                      RadioSettingValueList(
2089
                                          SPMUTE_LIST,
2090
                                          SPMUTE_LIST[_mem.vfo.c.spmute]))
2091
            work.append(vfocspmute)
2092

    
2093
            vfodspmute = RadioSetting("vfo.d.spmute", "VFO D speaker mute",
2094
                                      RadioSettingValueList(
2095
                                          SPMUTE_LIST,
2096
                                          SPMUTE_LIST[_mem.vfo.d.spmute]))
2097
            work.append(vfodspmute)
2098

    
2099
        if not self.COLOR_LCD or \
2100
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2101
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble",
2102
                                   RadioSettingValueBoolean(
2103
                                       _mem.vfo.a.scramble))
2104
            work.append(vfoascr)
2105

    
2106
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble",
2107
                                   RadioSettingValueBoolean(
2108
                                       _mem.vfo.b.scramble))
2109
            work.append(vfobscr)
2110

    
2111
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2112
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble",
2113
                                   RadioSettingValueBoolean(
2114
                                       _mem.vfo.c.scramble))
2115
            work.append(vfocscr)
2116

    
2117
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble",
2118
                                   RadioSettingValueBoolean(
2119
                                       _mem.vfo.d.scramble))
2120
            work.append(vfodscr)
2121

    
2122
        if not self.MODEL == "GMRS-50X1":
2123
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID",
2124
                                     RadioSettingValueList(
2125
                                         PTTIDCODE_LIST,
2126
                                         PTTIDCODE_LIST[_mem.vfo.a.scode]))
2127
            work.append(vfoascode)
2128

    
2129
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID",
2130
                                     RadioSettingValueList(
2131
                                         PTTIDCODE_LIST,
2132
                                         PTTIDCODE_LIST[_mem.vfo.b.scode]))
2133
            work.append(vfobscode)
2134

    
2135
            if self.COLOR_LCD:
2136
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID",
2137
                                         RadioSettingValueList(
2138
                                             PTTIDCODE_LIST,
2139
                                             PTTIDCODE_LIST[_mem.vfo.c.scode]))
2140
                work.append(vfocscode)
2141

    
2142
                vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID",
2143
                                         RadioSettingValueList(
2144
                                             PTTIDCODE_LIST,
2145
                                             PTTIDCODE_LIST[_mem.vfo.d.scode]))
2146
                work.append(vfodscode)
2147

    
2148
        if not self.MODEL == "GMRS-50X1":
2149
            pttid = RadioSetting("settings.pttid", "PTT ID",
2150
                                 RadioSettingValueList(
2151
                                     PTTID_LIST,
2152
                                     PTTID_LIST[_mem.settings.pttid]))
2153
            work.append(pttid)
2154

    
2155
        if not self.COLOR_LCD:
2156
            # FM presets
2157
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2158
            top.append(fm_presets)
2159

    
2160
            def fm_validate(value):
2161
                if value == 0:
2162
                    return chirp_common.format_freq(value)
2163
                if not (87.5 <= value and value <= 108.0):  # 87.5-108MHz
2164
                    msg = ("FM-Preset-Frequency: " +
2165
                           "Must be between 87.5 and 108 MHz")
2166
                    raise InvalidValueError(msg)
2167
                return value
2168

    
2169
            def apply_fm_preset_name(setting, obj):
2170
                valstring = str(setting.value)
2171
                for i in range(0, 6):
2172
                    if valstring[i] in VALID_CHARS:
2173
                        obj[i] = valstring[i]
2174
                    else:
2175
                        obj[i] = '0xff'
2176

    
2177
            def apply_fm_freq(setting, obj):
2178
                value = chirp_common.parse_freq(str(setting.value)) / 10
2179
                for i in range(7, -1, -1):
2180
                    obj.freq[i] = value % 10
2181
                    value /= 10
2182

    
2183
            _presets = self._memobj.fm_radio_preset
2184
            i = 1
2185
            for preset in _presets:
2186
                line = RadioSetting("fm_presets_" + str(i),
2187
                                    "Station name " + str(i),
2188
                                    RadioSettingValueString(0, 6, _filter(
2189
                                        preset.broadcast_station_name)))
2190
                line.set_apply_callback(apply_fm_preset_name,
2191
                                        preset.broadcast_station_name)
2192

    
2193
                val = RadioSettingValueFloat(0, 108,
2194
                                             convert_bytes_to_freq(
2195
                                                 preset.freq))
2196
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2197
                                      "Frequency " + str(i), val)
2198
                val.set_validate_callback(fm_validate)
2199
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2200
                fm_presets.append(line)
2201
                fm_presets.append(fmfreq)
2202

    
2203
                i = i + 1
2204

    
2205
        # DTMF-Setting
2206
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2207
                                              "DTMF Encoding Settings")
2208
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2209
                                              "DTMF Decoding Settings")
2210
        top.append(dtmf_enc_settings)
2211
        top.append(dtmf_dec_settings)
2212
        txdisable = RadioSetting("dtmf_settings.txdisable",
2213
                                 "TX-Disable",
2214
                                 RadioSettingValueBoolean(
2215
                                     _mem.dtmf_settings.txdisable))
2216
        dtmf_enc_settings.append(txdisable)
2217

    
2218
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2219
                                 "RX-Disable",
2220
                                 RadioSettingValueBoolean(
2221
                                     _mem.dtmf_settings.rxdisable))
2222
        dtmf_enc_settings.append(rxdisable)
2223

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

    
2236
        if _mem.dtmf_settings.dtmfspeed_off > 0x0F:
2237
            val = 0x03
2238
        else:
2239
            val = _mem.dtmf_settings.dtmfspeed_off
2240
        dtmfspeed_off = RadioSetting(
2241
            "dtmf_settings.dtmfspeed_off",
2242
            "DTMF Speed (Off Time)",
2243
            RadioSettingValueList(LIST_DTMF_SPEED,
2244
                                  LIST_DTMF_SPEED[
2245
                                      val]))
2246
        dtmf_enc_settings.append(dtmfspeed_off)
2247

    
2248
        def memory2string(dmtf_mem):
2249
            dtmf_string = ""
2250
            for digit in dmtf_mem:
2251
                if digit != 255:
2252
                    index = LIST_DTMF_VALUES.index(digit)
2253
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2254
            return dtmf_string
2255

    
2256
        def apply_dmtf_frame(setting, obj):
2257
            LOG.debug("Setting DTMF-Code: " + str(setting.value))
2258
            val_string = str(setting.value)
2259
            for i in range(0, 16):
2260
                obj[i] = 255
2261
            i = 0
2262
            for current_char in val_string:
2263
                current_char = current_char.upper()
2264
                index = LIST_DTMF_DIGITS.index(current_char)
2265
                obj[i] = LIST_DTMF_VALUES[index]
2266
                i = i + 1
2267

    
2268
        codes = self._memobj.dtmf_codes
2269
        i = 1
2270
        for dtmfcode in codes:
2271
            val = RadioSettingValueString(0, 16, memory2string(
2272
                                              dtmfcode.code),
2273
                                          False, CHARSET_DTMF_DIGITS)
2274
            line = RadioSetting("dtmf_code_" + str(i) + "_code",
2275
                                "DMTF Code " + str(i), val)
2276
            line.set_apply_callback(apply_dmtf_frame, dtmfcode.code)
2277
            dtmf_enc_settings.append(line)
2278
            i = i + 1
2279

    
2280
        line = RadioSetting("dtmf_settings.mastervice",
2281
                            "Master and Vice ID",
2282
                            RadioSettingValueBoolean(
2283
                                _mem.dtmf_settings.mastervice))
2284
        dtmf_dec_settings.append(line)
2285

    
2286
        val = RadioSettingValueString(0, 16, memory2string(
2287
                                          _mem.dtmf_settings.masterid),
2288
                                      False, CHARSET_DTMF_DIGITS)
2289
        line = RadioSetting("dtmf_settings.masterid",
2290
                            "Master Control ID ", val)
2291
        line.set_apply_callback(apply_dmtf_frame,
2292
                                _mem.dtmf_settings.masterid)
2293
        dtmf_dec_settings.append(line)
2294

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

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

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

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

    
2319
        line = RadioSetting("dtmf_settings.mrevive",
2320
                            "Master Revive",
2321
                            RadioSettingValueBoolean(
2322
                                _mem.dtmf_settings.mrevive))
2323
        dtmf_dec_settings.append(line)
2324

    
2325
        val = RadioSettingValueString(0, 16, memory2string(
2326
                                          _mem.dtmf_settings.viceid),
2327
                                      False, CHARSET_DTMF_DIGITS)
2328
        line = RadioSetting("dtmf_settings.viceid",
2329
                            "Vice Control ID ", val)
2330
        line.set_apply_callback(apply_dmtf_frame,
2331
                                _mem.dtmf_settings.viceid)
2332
        dtmf_dec_settings.append(line)
2333

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

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

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

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

    
2358
        line = RadioSetting("dtmf_settings.vrevive",
2359
                            "Vice Revive",
2360
                            RadioSettingValueBoolean(
2361
                                _mem.dtmf_settings.vrevive))
2362
        dtmf_dec_settings.append(line)
2363

    
2364
        val = RadioSettingValueString(0, 16, memory2string(
2365
                                          _mem.dtmf_settings.inspection),
2366
                                      False, CHARSET_DTMF_DIGITS)
2367
        line = RadioSetting("dtmf_settings.inspection",
2368
                            "Inspection", val)
2369
        line.set_apply_callback(apply_dmtf_frame,
2370
                                _mem.dtmf_settings.inspection)
2371
        dtmf_dec_settings.append(line)
2372

    
2373
        val = RadioSettingValueString(0, 16, memory2string(
2374
                                          _mem.dtmf_settings.alarmcode),
2375
                                      False, CHARSET_DTMF_DIGITS)
2376
        line = RadioSetting("dtmf_settings.alarmcode",
2377
                            "Alarm", val)
2378
        line.set_apply_callback(apply_dmtf_frame,
2379
                                _mem.dtmf_settings.alarmcode)
2380
        dtmf_dec_settings.append(line)
2381

    
2382
        val = RadioSettingValueString(0, 16, memory2string(
2383
                                          _mem.dtmf_settings.kill),
2384
                                      False, CHARSET_DTMF_DIGITS)
2385
        line = RadioSetting("dtmf_settings.kill",
2386
                            "Kill", val)
2387
        line.set_apply_callback(apply_dmtf_frame,
2388
                                _mem.dtmf_settings.kill)
2389
        dtmf_dec_settings.append(line)
2390

    
2391
        val = RadioSettingValueString(0, 16, memory2string(
2392
                                          _mem.dtmf_settings.monitor),
2393
                                      False, CHARSET_DTMF_DIGITS)
2394
        line = RadioSetting("dtmf_settings.monitor",
2395
                            "Monitor", val)
2396
        line.set_apply_callback(apply_dmtf_frame,
2397
                                _mem.dtmf_settings.monitor)
2398
        dtmf_dec_settings.append(line)
2399

    
2400
        val = RadioSettingValueString(0, 16, memory2string(
2401
                                          _mem.dtmf_settings.stun),
2402
                                      False, CHARSET_DTMF_DIGITS)
2403
        line = RadioSetting("dtmf_settings.stun",
2404
                            "Stun", val)
2405
        line.set_apply_callback(apply_dmtf_frame,
2406
                                _mem.dtmf_settings.stun)
2407
        dtmf_dec_settings.append(line)
2408

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

    
2418
        def apply_dmtf_listvalue(setting, obj):
2419
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2420
            val = str(setting.value)
2421
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2422
            val = LIST_DTMF_SPECIAL_VALUES[index]
2423
            obj.set_value(val)
2424

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

    
2439
        if _mem.dtmf_settings.spacecode not in LIST_DTMF_SPECIAL_VALUES:
2440
            val = 0x0C
2441
        else:
2442
            val = _mem.dtmf_settings.spacecode
2443
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2444
        line = RadioSetting(
2445
            "dtmf_settings.spacecode",
2446
            "Space Code",
2447
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2448
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2449
        line.set_apply_callback(apply_dmtf_listvalue,
2450
                                _mem.dtmf_settings.spacecode)
2451
        dtmf_dec_settings.append(line)
2452

    
2453
        if self.COLOR_LCD:
2454
            if _mem.dtmf_settings.resettime > 0x63:
2455
                val = 0x4F
2456
            else:
2457
                val = _mem.dtmf_settings.resettime
2458
            line = RadioSetting(
2459
                "dtmf_settings.resettime",
2460
                "Reset time",
2461
                RadioSettingValueList(LIST_5TONE_RESET_COLOR,
2462
                                      LIST_5TONE_RESET_COLOR[
2463
                                          val]))
2464
            dtmf_dec_settings.append(line)
2465
        else:
2466
            line = RadioSetting(
2467
                "dtmf_settings.resettime",
2468
                "Reset time",
2469
                RadioSettingValueList(LIST_5TONE_RESET,
2470
                                      LIST_5TONE_RESET[
2471
                                          _mem.dtmf_settings.resettime]))
2472
            dtmf_dec_settings.append(line)
2473

    
2474
        if _mem.dtmf_settings.delayproctime > 0x27:
2475
            val = 0x04
2476
        else:
2477
            val = _mem.dtmf_settings.delayproctime
2478
        line = RadioSetting(
2479
            "dtmf_settings.delayproctime",
2480
            "Delay processing time",
2481
            RadioSettingValueList(LIST_DTMF_DELAY,
2482
                                  LIST_DTMF_DELAY[
2483
                                      val]))
2484
        dtmf_dec_settings.append(line)
2485

    
2486
        # 5 Tone Settings
2487
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2488
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2489

    
2490
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2491
        group_5tone.append(stds_5tone)
2492
        group_5tone.append(codes_5tone)
2493

    
2494
        top.append(group_5tone)
2495

    
2496
        def apply_list_value(setting, obj):
2497
            options = setting.value.get_options()
2498
            obj.set_value(options.index(str(setting.value)))
2499

    
2500
        _5tone_standards = self._memobj._5tone_std_settings
2501
        i = 0
2502
        for standard in _5tone_standards:
2503
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2504
                                          LIST_5TONE_STANDARDS[i])
2505
            stds_5tone.append(std_5tone)
2506

    
2507
            period = standard.period
2508
            if period == 255:
2509
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2510
                          " is not yet configured. Setting to 70ms.")
2511
                period = 5
2512

    
2513
            if period <= len(LIST_5TONE_STANDARD_PERIODS):
2514
                line = RadioSetting(
2515
                    "_5tone_std_settings_" + str(i) + "_period",
2516
                    "Period (ms)", RadioSettingValueList
2517
                    (LIST_5TONE_STANDARD_PERIODS,
2518
                     LIST_5TONE_STANDARD_PERIODS[period]))
2519
                line.set_apply_callback(apply_list_value, standard.period)
2520
                std_5tone.append(line)
2521
            else:
2522
                LOG.debug("Invalid value for 5tone period! Disabling.")
2523

    
2524
            group_tone = standard.group_tone
2525
            if group_tone == 255:
2526
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2527
                          " is not yet configured. Setting to A.")
2528
                group_tone = 10
2529

    
2530
            if group_tone <= len(LIST_5TONE_DIGITS):
2531
                line = RadioSetting(
2532
                    "_5tone_std_settings_" + str(i) + "_grouptone",
2533
                    "Group Tone",
2534
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2535
                                          LIST_5TONE_DIGITS[
2536
                                              group_tone]))
2537
                line.set_apply_callback(apply_list_value,
2538
                                        standard.group_tone)
2539
                std_5tone.append(line)
2540
            else:
2541
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2542

    
2543
            repeat_tone = standard.repeat_tone
2544
            if repeat_tone == 255:
2545
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2546
                          " is not yet configured. Setting to E.")
2547
                repeat_tone = 14
2548

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

    
2563
        def my_apply_5tonestdlist_value(setting, obj):
2564
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2565
                obj.set_value(0xFF)
2566
            else:
2567
                obj.set_value(LIST_5TONE_STANDARDS.
2568
                              index(str(setting.value)))
2569

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

    
2590
        def validate_5tone_frame(value):
2591
            if (len(str(value)) != 5) and (len(str(value)) != 0):
2592
                msg = ("5 Tone must have 5 digits or 0 digits")
2593
                raise InvalidValueError(msg)
2594
            for digit in str(value):
2595
                if digit.upper() not in LIST_5TONE_DIGITS:
2596
                    msg = (str(digit) + " is not a valid digit for 5tones")
2597
                    raise InvalidValueError(msg)
2598
            return value
2599

    
2600
        def frame2string(frame):
2601
            frameString = ""
2602
            for digit in frame:
2603
                if digit != 255:
2604
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2605
            return frameString
2606

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

    
2626
            val = RadioSettingValueString(0, 6,
2627
                                          frame2string(code.frame1), False)
2628
            line = RadioSetting("_5tone_code_" + str(i) + "_frame1",
2629
                                " Frame 1", val)
2630
            val.set_validate_callback(validate_5tone_frame)
2631
            line.set_apply_callback(apply_5tone_frame, code.frame1)
2632
            code_5tone.append(line)
2633

    
2634
            val = RadioSettingValueString(0, 6,
2635
                                          frame2string(code.frame2), False)
2636
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2637
                                " Frame 2", val)
2638
            val.set_validate_callback(validate_5tone_frame)
2639
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2640
            code_5tone.append(line)
2641

    
2642
            val = RadioSettingValueString(0, 6,
2643
                                          frame2string(code.frame3), False)
2644
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2645
                                " Frame 3", val)
2646
            val.set_validate_callback(validate_5tone_frame)
2647
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2648
            code_5tone.append(line)
2649
            i = i + 1
2650

    
2651
        _5_tone_decode1 = RadioSetting(
2652
            "_5tone_settings._5tone_decode_call_frame1",
2653
            "5 Tone decode call Frame 1",
2654
            RadioSettingValueBoolean(
2655
                _mem._5tone_settings._5tone_decode_call_frame1))
2656
        group_5tone.append(_5_tone_decode1)
2657

    
2658
        _5_tone_decode2 = RadioSetting(
2659
            "_5tone_settings._5tone_decode_call_frame2",
2660
            "5 Tone decode call Frame 2",
2661
            RadioSettingValueBoolean(
2662
                _mem._5tone_settings._5tone_decode_call_frame2))
2663
        group_5tone.append(_5_tone_decode2)
2664

    
2665
        _5_tone_decode3 = RadioSetting(
2666
            "_5tone_settings._5tone_decode_call_frame3",
2667
            "5 Tone decode call Frame 3",
2668
            RadioSettingValueBoolean(
2669
                _mem._5tone_settings._5tone_decode_call_frame3))
2670
        group_5tone.append(_5_tone_decode3)
2671

    
2672
        _5_tone_decode_disp1 = RadioSetting(
2673
            "_5tone_settings._5tone_decode_disp_frame1",
2674
            "5 Tone decode disp Frame 1",
2675
            RadioSettingValueBoolean(
2676
                _mem._5tone_settings._5tone_decode_disp_frame1))
2677
        group_5tone.append(_5_tone_decode_disp1)
2678

    
2679
        _5_tone_decode_disp2 = RadioSetting(
2680
            "_5tone_settings._5tone_decode_disp_frame2",
2681
            "5 Tone decode disp Frame 2",
2682
            RadioSettingValueBoolean(
2683
                _mem._5tone_settings._5tone_decode_disp_frame2))
2684
        group_5tone.append(_5_tone_decode_disp2)
2685

    
2686
        _5_tone_decode_disp3 = RadioSetting(
2687
            "_5tone_settings._5tone_decode_disp_frame3",
2688
            "5 Tone decode disp Frame 3",
2689
            RadioSettingValueBoolean(
2690
                _mem._5tone_settings._5tone_decode_disp_frame3))
2691
        group_5tone.append(_5_tone_decode_disp3)
2692

    
2693
        decode_standard = _mem._5tone_settings.decode_standard
2694
        if decode_standard == 255:
2695
            decode_standard = 0
2696
        if decode_standard <= len(LIST_5TONE_STANDARDS_without_none):
2697
            line = RadioSetting("_5tone_settings.decode_standard",
2698
                                "5 Tone-decode Standard",
2699
                                RadioSettingValueList(
2700
                                    LIST_5TONE_STANDARDS_without_none,
2701
                                    LIST_5TONE_STANDARDS_without_none[
2702
                                        decode_standard]))
2703
            group_5tone.append(line)
2704
        else:
2705
            LOG.debug("Invalid decode std...")
2706

    
2707
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2708
        if _5tone_delay1 == 255:
2709
            _5tone_delay1 = 20
2710

    
2711
        if _5tone_delay1 <= len(LIST_5TONE_DELAY):
2712
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2713
                                         LIST_5TONE_DELAY[
2714
                                             _5tone_delay1])
2715
            line = RadioSetting("_5tone_settings._5tone_delay1",
2716
                                "5 Tone Delay Frame 1", list)
2717
            group_5tone.append(line)
2718
        else:
2719
            LOG.debug("Invalid value for 5tone delay (frame1) ! Disabling.")
2720

    
2721
        _5tone_delay2 = _mem._5tone_settings._5tone_delay2
2722
        if _5tone_delay2 == 255:
2723
            _5tone_delay2 = 20
2724
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2725

    
2726
        if _5tone_delay2 <= len(LIST_5TONE_DELAY):
2727
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2728
                                         LIST_5TONE_DELAY[
2729
                                             _5tone_delay2])
2730
            line = RadioSetting("_5tone_settings._5tone_delay2",
2731
                                "5 Tone Delay Frame 2", list)
2732
            group_5tone.append(line)
2733
        else:
2734
            LOG.debug("Invalid value for 5tone delay (frame2)! Disabling.")
2735

    
2736
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2737
        if _5tone_delay3 == 255:
2738
            _5tone_delay3 = 20
2739
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2740

    
2741
        if _5tone_delay3 <= len(LIST_5TONE_DELAY):
2742
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2743
                                         LIST_5TONE_DELAY[
2744
                                             _5tone_delay3])
2745
            line = RadioSetting("_5tone_settings._5tone_delay3",
2746
                                "5 Tone Delay Frame 3", list)
2747
            group_5tone.append(line)
2748
        else:
2749
            LOG.debug("Invalid value for 5tone delay (frame3)! Disabling.")
2750

    
2751
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2752
        if ext_length == 255:
2753
            ext_length = 0
2754
            LOG.debug("1st Tone ext lenght unconfigured! Resetting to 0")
2755

    
2756
        if ext_length <= len(LIST_5TONE_DELAY):
2757
            list = RadioSettingValueList(
2758
                LIST_5TONE_DELAY,
2759
                LIST_5TONE_DELAY[
2760
                    ext_length])
2761
            line = RadioSetting(
2762
                "_5tone_settings._5tone_first_digit_ext_length",
2763
                "First digit extend length", list)
2764
            group_5tone.append(line)
2765
        else:
2766
            LOG.debug("Invalid value for 5tone ext length! Disabling.")
2767

    
2768
        decode_reset_time = _mem._5tone_settings.decode_reset_time
2769
        if decode_reset_time == 255:
2770
            decode_reset_time = 59
2771
            LOG.debug("Decode reset time unconfigured. resetting.")
2772
        if decode_reset_time <= len(LIST_5TONE_RESET):
2773
            list = RadioSettingValueList(
2774
                LIST_5TONE_RESET,
2775
                LIST_5TONE_RESET[
2776
                    decode_reset_time])
2777
            line = RadioSetting("_5tone_settings.decode_reset_time",
2778
                                "Decode reset time", list)
2779
            group_5tone.append(line)
2780
        else:
2781
            LOG.debug("Invalid value decode reset time! Disabling.")
2782

    
2783
        # 2 Tone
2784
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2785
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2786

    
2787
        top.append(encode_2tone)
2788
        top.append(decode_2tone)
2789

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

    
2796
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2797
            line = RadioSetting("_2tone.duration_1st_tone",
2798
                                "Duration 1st Tone",
2799
                                RadioSettingValueList(LIST_5TONE_DELAY,
2800
                                                      LIST_5TONE_DELAY[
2801
                                                          duration_1st_tone]))
2802
            encode_2tone.append(line)
2803

    
2804
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2805
        if duration_2nd_tone == 255:
2806
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2807
                      "configured. Setting to 600ms")
2808
            duration_2nd_tone = 60
2809

    
2810
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2811
            line = RadioSetting("_2tone.duration_2nd_tone",
2812
                                "Duration 2nd Tone",
2813
                                RadioSettingValueList(LIST_5TONE_DELAY,
2814
                                                      LIST_5TONE_DELAY[
2815
                                                          duration_2nd_tone]))
2816
            encode_2tone.append(line)
2817

    
2818
        duration_gap = self._memobj._2tone.duration_gap
2819
        if duration_gap == 255:
2820
            LOG.debug("Duration of gap is not yet " +
2821
                      "configured. Setting to 300ms")
2822
            duration_gap = 30
2823

    
2824
        if duration_gap <= len(LIST_5TONE_DELAY):
2825
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2826
                                RadioSettingValueList(LIST_5TONE_DELAY,
2827
                                                      LIST_5TONE_DELAY[
2828
                                                          duration_gap]))
2829
            encode_2tone.append(line)
2830

    
2831
        def _2tone_validate(value):
2832
            if value == 0:
2833
                return 65535
2834
            if value == 65535:
2835
                return value
2836
            if not (300 <= value and value <= 3000):
2837
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
2838
                raise InvalidValueError(msg)
2839
            return value
2840

    
2841
        def apply_2tone_freq(setting, obj):
2842
            val = int(setting.value)
2843
            if (val == 0) or (val == 65535):
2844
                obj.set_value(65535)
2845
            else:
2846
                obj.set_value(val)
2847

    
2848
        i = 1
2849
        for code in self._memobj._2tone._2tone_encode:
2850
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
2851
                                           "Encode Code " + str(i))
2852
            encode_2tone.append(code_2tone)
2853

    
2854
            tmp = code.freq1
2855
            if tmp == 65535:
2856
                tmp = 0
2857
            val1 = RadioSettingValueInteger(0, 65535, tmp)
2858
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
2859
                                 "Frequency 1", val1)
2860
            val1.set_validate_callback(_2tone_validate)
2861
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
2862
            code_2tone.append(freq1)
2863

    
2864
            tmp = code.freq2
2865
            if tmp == 65535:
2866
                tmp = 0
2867
            val2 = RadioSettingValueInteger(0, 65535, tmp)
2868
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
2869
                                 "Frequency 2", val2)
2870
            val2.set_validate_callback(_2tone_validate)
2871
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
2872
            code_2tone.append(freq2)
2873

    
2874
            i = i + 1
2875

    
2876
        decode_reset_time = _mem._2tone.reset_time
2877
        if decode_reset_time == 255:
2878
            decode_reset_time = 59
2879
            LOG.debug("Decode reset time unconfigured. resetting.")
2880
        if decode_reset_time <= len(LIST_5TONE_RESET):
2881
            list = RadioSettingValueList(
2882
                LIST_5TONE_RESET,
2883
                LIST_5TONE_RESET[
2884
                    decode_reset_time])
2885
            line = RadioSetting("_2tone.reset_time",
2886
                                "Decode reset time", list)
2887
            decode_2tone.append(line)
2888
        else:
2889
            LOG.debug("Invalid value decode reset time! Disabling.")
2890

    
2891
        def apply_2tone_freq_pair(setting, obj):
2892
            val = int(setting.value)
2893
            derived_val = 65535
2894
            frqname = str(setting._name[-5:])
2895
            derivedname = "derived_from_" + frqname
2896

    
2897
            if (val == 0):
2898
                val = 65535
2899
                derived_val = 65535
2900
            else:
2901
                derived_val = int(round(2304000.0/val))
2902

    
2903
            obj[frqname].set_value(val)
2904
            obj[derivedname].set_value(derived_val)
2905

    
2906
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
2907
                      derivedname + ": " + str(derived_val))
2908

    
2909
        i = 1
2910
        for decode_code in self._memobj._2tone._2tone_decode:
2911
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
2912
                                                "Decode Code " + str(i))
2913
            decode_2tone.append(_2tone_dec_code)
2914

    
2915
            j = 1
2916
            for dec in decode_code.decs:
2917
                val = dec.dec
2918
                if val == 255:
2919
                    LOG.debug("Dec for Code " + str(i) + " Dec " + str(j) +
2920
                              " is not yet configured. Setting to 0.")
2921
                    val = 0
2922

    
2923
                if val <= len(LIST_2TONE_DEC):
2924
                    line = RadioSetting(
2925
                        "_2tone_dec_settings_" + str(i) + "_dec_" + str(j),
2926
                        "Dec " + str(j), RadioSettingValueList
2927
                        (LIST_2TONE_DEC,
2928
                         LIST_2TONE_DEC[val]))
2929
                    line.set_apply_callback(apply_list_value, dec.dec)
2930
                    _2tone_dec_code.append(line)
2931
                else:
2932
                    LOG.debug("Invalid value for 2tone dec! Disabling.")
2933

    
2934
                val = dec.response
2935
                if val == 255:
2936
                    LOG.debug("Response for Code " + str(i) + " Dec " +
2937
                              str(j) + " is not yet configured. Setting to 0.")
2938
                    val = 0
2939

    
2940
                if val <= len(LIST_2TONE_RESPONSE):
2941
                    line = RadioSetting(
2942
                        "_2tone_dec_settings_" + str(i) + "_resp_" + str(j),
2943
                        "Response " + str(j), RadioSettingValueList
2944
                        (LIST_2TONE_RESPONSE,
2945
                         LIST_2TONE_RESPONSE[val]))
2946
                    line.set_apply_callback(apply_list_value, dec.response)
2947
                    _2tone_dec_code.append(line)
2948
                else:
2949
                    LOG.debug("Invalid value for 2tone response! Disabling.")
2950

    
2951
                val = dec.alert
2952
                if val == 255:
2953
                    LOG.debug("Alert for Code " + str(i) + " Dec " + str(j) +
2954
                              " is not yet configured. Setting to 0.")
2955
                    val = 0
2956

    
2957
                if val <= len(PTTIDCODE_LIST):
2958
                    line = RadioSetting(
2959
                        "_2tone_dec_settings_" + str(i) + "_alert_" + str(j),
2960
                        "Alert " + str(j), RadioSettingValueList
2961
                        (PTTIDCODE_LIST,
2962
                         PTTIDCODE_LIST[val]))
2963
                    line.set_apply_callback(apply_list_value, dec.alert)
2964
                    _2tone_dec_code.append(line)
2965
                else:
2966
                    LOG.debug("Invalid value for 2tone alert! Disabling.")
2967
                j = j + 1
2968

    
2969
            freq = self._memobj._2tone.freqs[i-1]
2970
            for char in ['A', 'B', 'C', 'D']:
2971
                setting_name = "freq" + str(char)
2972

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

    
2991
            i = i + 1
2992

    
2993
        return top
2994

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

    
3022
                    if element.has_apply_callback():
3023
                        LOG.debug("Using apply callback")
3024
                        element.run_apply_callback()
3025
                    elif element.value.get_mutable():
3026
                        LOG.debug("Setting %s = %s" % (setting, element.value))
3027
                        setattr(obj, setting, element.value)
3028
                except Exception, e:
3029
                    LOG.debug(element.get_name())
3030
                    raise
3031

    
3032
    @classmethod
3033
    def match_model(cls, filedata, filename):
3034
        match_size = False
3035
        match_model = False
3036

    
3037
        # testing the file data size
3038
        if len(filedata) == MEM_SIZE:
3039
            match_size = True
3040

    
3041
        # testing the firmware model fingerprint
3042
        match_model = model_match(cls, filedata)
3043

    
3044
        if match_size and match_model:
3045
            return True
3046
        else:
3047
            return False
3048

    
3049

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

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

    
3121
#seekto 0x0E80;
3122
struct {
3123
  u8 unknown1;
3124
  u8 vfomr;
3125
  u8 keylock;
3126
  u8 unknown2;
3127
  u8 unknown3:4,
3128
     vfomren:1,
3129
     unknown4:1,
3130
     reseten:1,
3131
     menuen:1;
3132
  u8 unknown5[11];
3133
  u8 dispab;
3134
  u8 mrcha;
3135
  u8 mrchb;
3136
  u8 menu;
3137
} settings2;
3138

    
3139
#seekto 0x0EC0;
3140
struct {
3141
  char line1[6];
3142
  char line2[6];
3143
} poweron_msg;
3144

    
3145
struct settings_vfo {
3146
  u8 freq[8];
3147
  u8 offset[6];
3148
  u8 unknown2[2];
3149
  ul16 rxtone;
3150
  ul16 txtone;
3151
  u8 scode;
3152
  u8 spmute;
3153
  u8 optsig;
3154
  u8 scramble;
3155
  u8 wide;
3156
  u8 power;
3157
  u8 shiftd;
3158
  u8 step;
3159
  u8 unknown3[4];
3160
};
3161

    
3162
#seekto 0x0F00;
3163
struct {
3164
  struct settings_vfo a;
3165
  struct settings_vfo b;
3166
} vfo;
3167

    
3168
#seekto 0x1000;
3169
struct {
3170
  char name[6];
3171
  u8 unknown1[10];
3172
} names[200];
3173

    
3174
#seekto 0x2400;
3175
struct {
3176
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3177
  u8 group_tone;
3178
  u8 repeat_tone;
3179
  u8 unused[13];
3180
} _5tone_std_settings[15];
3181

    
3182
#seekto 0x2500;
3183
struct {
3184
  u8 frame1[5];
3185
  u8 frame2[5];
3186
  u8 frame3[5];
3187
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3188
} _5tone_codes[15];
3189

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

    
3212
#seekto 0x2900;
3213
struct {
3214
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3215
} dtmf_codes[15];
3216

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

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

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

    
3297
#seekto 0x3000;
3298
struct {
3299
  u8 freq[8];
3300
  char broadcast_station_name[6];
3301
  u8 unknown[2];
3302
} fm_radio_preset[16];
3303

    
3304
#seekto 0x3C90;
3305
struct {
3306
  u8 vhf_low[3];
3307
  u8 vhf_high[3];
3308
  u8 uhf_low[3];
3309
  u8 uhf_high[3];
3310
} ranges;
3311

    
3312
// the UV-2501+220 & KT8900R has different zones for storing ranges
3313

    
3314
#seekto 0x3CD0;
3315
struct {
3316
  u8 vhf_low[3];
3317
  u8 vhf_high[3];
3318
  u8 unknown1[4];
3319
  u8 unknown2[6];
3320
  u8 vhf2_low[3];
3321
  u8 vhf2_high[3];
3322
  u8 unknown3[4];
3323
  u8 unknown4[6];
3324
  u8 uhf_low[3];
3325
  u8 uhf_high[3];
3326
} ranges220;
3327

    
3328
#seekto 0x3F70;
3329
struct {
3330
  char fp[6];
3331
} fingerprint;
3332

    
3333
"""
3334

    
3335

    
3336
class BTech(BTechMobileCommon):
3337
    """BTECH's UV-5001 and alike radios"""
3338
    BANDS = 2
3339
    COLOR_LCD = False
3340
    NAME_LENGTH = 6
3341

    
3342
    def set_options(self):
3343
        """This is to read the options from the image and set it in the
3344
        environment, for now just the limits of the freqs in the VHF/UHF
3345
        ranges"""
3346

    
3347
        # setting the correct ranges for each radio type
3348
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3349
            # the model 2501+220 has a segment in 220
3350
            # and a different position in the memmap
3351
            # also the QYT KT8900R
3352
            ranges = self._memobj.ranges220
3353
        else:
3354
            ranges = self._memobj.ranges
3355

    
3356
        # the normal dual bands
3357
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3358
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3359

    
3360
        # DEBUG
3361
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3362
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3363

    
3364
        # 220Mhz radios case
3365
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3366
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3367
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3368
            self._220_range = vhf2
3369

    
3370
        # set the class with the real data
3371
        self._vhf_range = vhf
3372
        self._uhf_range = uhf
3373

    
3374
    def process_mmap(self):
3375
        """Process the mem map into the mem object"""
3376

    
3377
        # Get it
3378
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3379

    
3380
        # load specific parameters from the radio image
3381
        self.set_options()
3382

    
3383

    
3384
# Declaring Aliases (Clones of the real radios)
3385
class JT2705M(chirp_common.Alias):
3386
    VENDOR = "Jetstream"
3387
    MODEL = "JT2705M"
3388

    
3389

    
3390
class JT6188Mini(chirp_common.Alias):
3391
    VENDOR = "Juentai"
3392
    MODEL = "JT-6188 Mini"
3393

    
3394

    
3395
class JT6188Plus(chirp_common.Alias):
3396
    VENDOR = "Juentai"
3397
    MODEL = "JT-6188 Plus"
3398

    
3399

    
3400
class SSGT890(chirp_common.Alias):
3401
    VENDOR = "Sainsonic"
3402
    MODEL = "GT-890"
3403

    
3404

    
3405
class ZastoneMP300(chirp_common.Alias):
3406
    VENDOR = "Zastone"
3407
    MODEL = "MP-300"
3408

    
3409

    
3410
# real radios
3411
@directory.register
3412
class UV2501(BTech):
3413
    """Baofeng Tech UV2501"""
3414
    MODEL = "UV-2501"
3415
    _fileid = [UV2501G3_fp,
3416
               UV2501G2_fp,
3417
               UV2501pp2_fp,
3418
               UV2501pp_fp]
3419

    
3420

    
3421
@directory.register
3422
class UV2501_220(BTech):
3423
    """Baofeng Tech UV2501+220"""
3424
    MODEL = "UV-2501+220"
3425
    BANDS = 3
3426
    _magic = MSTRING_220
3427
    _id2 = [UV2501_220pp_id, ]
3428
    _fileid = [UV2501_220G3_fp,
3429
               UV2501_220G2_fp,
3430
               UV2501_220_fp,
3431
               UV2501_220pp_fp]
3432

    
3433

    
3434
@directory.register
3435
class UV5001(BTech):
3436
    """Baofeng Tech UV5001"""
3437
    MODEL = "UV-5001"
3438
    _fileid = [UV5001G3_fp,
3439
               UV5001G22_fp,
3440
               UV5001G2_fp,
3441
               UV5001alpha_fp,
3442
               UV5001pp_fp]
3443
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3444
                     chirp_common.PowerLevel("Low", watts=10)]
3445

    
3446

    
3447
@directory.register
3448
class MINI8900(BTech):
3449
    """WACCOM MINI-8900"""
3450
    VENDOR = "WACCOM"
3451
    MODEL = "MINI-8900"
3452
    _magic = MSTRING_MINI8900
3453
    _fileid = [MINI8900_fp, ]
3454
    # Clones
3455
    ALIASES = [JT6188Plus, ]
3456

    
3457

    
3458
@directory.register
3459
class KTUV980(BTech):
3460
    """QYT KT-UV980"""
3461
    VENDOR = "QYT"
3462
    MODEL = "KT-UV980"
3463
    _vhf_range = (136000000, 175000000)
3464
    _uhf_range = (400000000, 481000000)
3465
    _magic = MSTRING_MINI8900
3466
    _fileid = [KTUV980_fp, ]
3467
    # Clones
3468
    ALIASES = [JT2705M, ]
3469

    
3470
# Please note that there is a version of this radios that is a clone of the
3471
# Waccom Mini8900, maybe an early version?
3472

    
3473

    
3474
class OTGRadioV1(chirp_common.Alias):
3475
    VENDOR = 'OTGSTUFF'
3476
    MODEL = 'OTG Radio v1'
3477

    
3478

    
3479
@directory.register
3480
class KT9800(BTech):
3481
    """QYT KT8900"""
3482
    VENDOR = "QYT"
3483
    MODEL = "KT8900"
3484
    _vhf_range = (136000000, 175000000)
3485
    _uhf_range = (400000000, 481000000)
3486
    _magic = MSTRING_KT8900
3487
    _fileid = [KT8900_fp,
3488
               KT8900_fp1,
3489
               KT8900_fp2,
3490
               KT8900_fp3,
3491
               KT8900_fp4,
3492
               KT8900_fp5,
3493
               KT8900_fp6]
3494
    _id2 = [KT8900_id, KT8900_id2]
3495
    # Clones
3496
    ALIASES = [JT6188Mini, SSGT890, ZastoneMP300]
3497

    
3498

    
3499
@directory.register
3500
class KT9800R(BTech):
3501
    """QYT KT8900R"""
3502
    VENDOR = "QYT"
3503
    MODEL = "KT8900R"
3504
    BANDS = 3
3505
    _vhf_range = (136000000, 175000000)
3506
    _220_range = (240000000, 271000000)
3507
    _uhf_range = (400000000, 481000000)
3508
    _magic = MSTRING_KT8900R
3509
    _fileid = [KT8900R_fp,
3510
               KT8900R_fp1,
3511
               KT8900R_fp2,
3512
               KT8900R_fp3,
3513
               KT8900R_fp4]
3514
    _id2 = [KT8900R_id, KT8900R_id2]
3515

    
3516

    
3517
@directory.register
3518
class LT588UV(BTech):
3519
    """LUITON LT-588UV"""
3520
    VENDOR = "LUITON"
3521
    MODEL = "LT-588UV"
3522
    _vhf_range = (136000000, 175000000)
3523
    _uhf_range = (400000000, 481000000)
3524
    _magic = MSTRING_KT8900
3525
    _fileid = [LT588UV_fp,
3526
               LT588UV_fp1]
3527
    _power_levels = [chirp_common.PowerLevel("High", watts=60),
3528
                     chirp_common.PowerLevel("Low", watts=10)]
3529

    
3530

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

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

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

    
3639
struct settings_vfo {
3640
  u8 freq[8];
3641
  u8 offset[6];
3642
  u8 unknown2[2];
3643
  ul16 rxtone;
3644
  ul16 txtone;
3645
  u8 scode;
3646
  u8 spmute;
3647
  u8 optsig;
3648
  u8 scramble;
3649
  u8 wide;
3650
  u8 power;
3651
  u8 shiftd;
3652
  u8 step;
3653
  u8 unknown3[4];
3654
};
3655

    
3656
#seekto 0x0F00;
3657
struct {
3658
  struct settings_vfo a;
3659
  struct settings_vfo b;
3660
  struct settings_vfo c;
3661
  struct settings_vfo d;
3662
} vfo;
3663

    
3664
#seekto 0x0F80;
3665
struct {
3666
  char line1[8];
3667
  char line2[8];
3668
  char line3[8];
3669
  char line4[8];
3670
  char line5[8];
3671
  char line6[8];
3672
  char line7[8];
3673
  char line8[8];
3674
} poweron_msg;
3675

    
3676
#seekto 0x1000;
3677
struct {
3678
  char name[8];
3679
  u8 unknown1[8];
3680
} names[200];
3681

    
3682
#seekto 0x2400;
3683
struct {
3684
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3685
  u8 group_tone;
3686
  u8 repeat_tone;
3687
  u8 unused[13];
3688
} _5tone_std_settings[15];
3689

    
3690
#seekto 0x2500;
3691
struct {
3692
  u8 frame1[5];
3693
  u8 frame2[5];
3694
  u8 frame3[5];
3695
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3696
} _5tone_codes[15];
3697

    
3698
#seekto 0x25F0;
3699
struct {
3700
  u8 _5tone_delay1; // * 10ms
3701
  u8 _5tone_delay2; // * 10ms
3702
  u8 _5tone_delay3; // * 10ms
3703
  u8 _5tone_first_digit_ext_length;
3704
  u8 unknown1;
3705
  u8 unknown2;
3706
  u8 unknown3;
3707
  u8 unknown4;
3708
  u8 decode_standard;
3709
  u8 unknown5:5,
3710
     _5tone_decode_call_frame3:1,
3711
     _5tone_decode_call_frame2:1,
3712
     _5tone_decode_call_frame1:1;
3713
  u8 unknown6:5,
3714
     _5tone_decode_disp_frame3:1,
3715
     _5tone_decode_disp_frame2:1,
3716
     _5tone_decode_disp_frame1:1;
3717
  u8 decode_reset_time; // * 100 + 100ms
3718
} _5tone_settings;
3719

    
3720
#seekto 0x2900;
3721
struct {
3722
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3723
} dtmf_codes[15];
3724

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

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

    
3790
  struct {
3791
    ul16 freqA;
3792
    ul16 freqB;
3793
    ul16 freqC;
3794
    ul16 freqD;
3795
    // unknown what those values mean, but they are
3796
    // derived from configured frequencies
3797
    ul16 derived_from_freqA; // 2304000/freqA
3798
    ul16 derived_from_freqB; // 2304000/freqB
3799
    ul16 derived_from_freqC; // 2304000/freqC
3800
    ul16 derived_from_freqD; // 2304000/freqD
3801
  }freqs[15];
3802
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3803
} _2tone;
3804

    
3805
#seekto 0x3D80;
3806
struct {
3807
  u8 vhf_low[3];
3808
  u8 vhf_high[3];
3809
  u8 unknown1[4];
3810
  u8 unknown2[6];
3811
  u8 vhf2_low[3];
3812
  u8 vhf2_high[3];
3813
  u8 unknown3[4];
3814
  u8 unknown4[6];
3815
  u8 uhf_low[3];
3816
  u8 uhf_high[3];
3817
  u8 unknown5[4];
3818
  u8 unknown6[6];
3819
  u8 uhf2_low[3];
3820
  u8 uhf2_high[3];
3821
} ranges;
3822

    
3823
#seekto 0x3F70;
3824
struct {
3825
  char fp[6];
3826
} fingerprint;
3827

    
3828
"""
3829

    
3830

    
3831
class BTechColor(BTechMobileCommon):
3832
    """BTECH's Color LCD Mobile and alike radios"""
3833
    COLOR_LCD = True
3834
    NAME_LENGTH = 8
3835
    LIST_TMR = LIST_TMR16
3836

    
3837
    def process_mmap(self):
3838
        """Process the mem map into the mem object"""
3839

    
3840
        # Get it
3841
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
3842

    
3843
        # load specific parameters from the radio image
3844
        self.set_options()
3845

    
3846
    def set_options(self):
3847
        """This is to read the options from the image and set it in the
3848
        environment, for now just the limits of the freqs in the VHF/UHF
3849
        ranges"""
3850

    
3851
        # setting the correct ranges for each radio type
3852
        ranges = self._memobj.ranges
3853

    
3854
        # the normal dual bands
3855
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3856
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3857

    
3858
        # DEBUG
3859
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3860
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3861

    
3862
        # the additional bands
3863
        if self.MODEL in ["UV-25X4", "KT7900D"]:
3864
            # 200Mhz band
3865
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3866
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3867
            self._220_range = vhf2
3868

    
3869
            # 350Mhz band
3870
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
3871
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
3872
            self._350_range = uhf2
3873

    
3874
        # set the class with the real data
3875
        self._vhf_range = vhf
3876
        self._uhf_range = uhf
3877

    
3878

    
3879
# Declaring Aliases (Clones of the real radios)
3880
class SKT8900D(chirp_common.Alias):
3881
    VENDOR = "Surecom"
3882
    MODEL = "S-KT8900D"
3883

    
3884

    
3885
class QB25(chirp_common.Alias):
3886
    VENDOR = "Radioddity"
3887
    MODEL = "QB25"
3888

    
3889

    
3890
# real radios
3891
@directory.register
3892
class UV25X2(BTechColor):
3893
    """Baofeng Tech UV25X2"""
3894
    MODEL = "UV-25X2"
3895
    BANDS = 2
3896
    _vhf_range = (130000000, 180000000)
3897
    _uhf_range = (400000000, 521000000)
3898
    _magic = MSTRING_UV25X2
3899
    _fileid = [UV25X2_fp, ]
3900

    
3901

    
3902
@directory.register
3903
class UV25X4(BTechColor):
3904
    """Baofeng Tech UV25X4"""
3905
    MODEL = "UV-25X4"
3906
    BANDS = 4
3907
    _vhf_range = (130000000, 180000000)
3908
    _220_range = (200000000, 271000000)
3909
    _uhf_range = (400000000, 521000000)
3910
    _350_range = (350000000, 391000000)
3911
    _magic = MSTRING_UV25X4
3912
    _fileid = [UV25X4_fp, ]
3913

    
3914

    
3915
@directory.register
3916
class UV50X2(BTechColor):
3917
    """Baofeng Tech UV50X2"""
3918
    MODEL = "UV-50X2"
3919
    BANDS = 2
3920
    _vhf_range = (130000000, 180000000)
3921
    _uhf_range = (400000000, 521000000)
3922
    _magic = MSTRING_UV25X2
3923
    _fileid = [UV50X2_fp, ]
3924
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3925
                     chirp_common.PowerLevel("Low", watts=10)]
3926

    
3927

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

    
3945

    
3946
@directory.register
3947
class KT8900D(BTechColor):
3948
    """QYT KT8900D"""
3949
    VENDOR = "QYT"
3950
    MODEL = "KT8900D"
3951
    BANDS = 2
3952
    LIST_TMR = LIST_TMR15
3953
    _vhf_range = (136000000, 175000000)
3954
    _uhf_range = (400000000, 481000000)
3955
    _magic = MSTRING_KT8900D
3956
    _fileid = [KT8900D_fp3, KT8900D_fp2, KT8900D_fp1, KT8900D_fp]
3957

    
3958
    # Clones
3959
    ALIASES = [OTGRadioV1]
3960

    
3961

    
3962
@directory.register
3963
class KT5800(BTechColor):
3964
    """QYT KT5800"""
3965
    VENDOR = "QYT"
3966
    MODEL = "KT5800"
3967
    BANDS = 2
3968
    LIST_TMR = LIST_TMR15
3969
    _vhf_range = (136000000, 175000000)
3970
    _uhf_range = (400000000, 481000000)
3971
    _magic = MSTRING_KT8900D
3972
    _fileid = [KT5800_fp, ]
3973

    
3974

    
3975
@directory.register
3976
class DB25G(BTechColor):
3977
    """Radioddity DB25-G"""
3978
    VENDOR = "Radioddity"
3979
    MODEL = "DB25-G"
3980
    BANDS = 2
3981
    LIST_TMR = LIST_TMR15
3982
    _vhf_range = (136000000, 175000000)
3983
    _uhf_range = (400000000, 481000000)
3984
    _magic = MSTRING_KT8900D
3985
    _fileid = [DB25G_fp, ]
3986

    
3987
    @classmethod
3988
    def match_model(cls, filedata, filename):
3989
        # This model is only ever matched via metadata
3990
        return False
3991

    
3992

    
3993
GMRS_MEM_FORMAT = """
3994
#seekto 0x0000;
3995
struct {
3996
  lbcd rxfreq[4];
3997
  lbcd txfreq[4];
3998
  ul16 rxtone;
3999
  ul16 txtone;
4000
  u8 unknown0:4,
4001
     scode:4;
4002
  u8 unknown1:2,
4003
     spmute:2,
4004
     unknown2:2,
4005
     optsig:2;
4006
  u8 unknown3:3,
4007
     scramble:1,
4008
     unknown4:2,
4009
     power:2;
4010
  u8 unknown5:1,
4011
     wide:1,
4012
     unknown6:2,
4013
     bcl:1,
4014
     add:1,
4015
     pttid:2;
4016
} memory[256];
4017

    
4018
#seekto 0x1000;
4019
struct {
4020
  char name[7];
4021
  u8 unknown1[9];
4022
} names[256];
4023

    
4024
#seekto 0x2400;
4025
struct {
4026
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4027
  u8 group_tone;
4028
  u8 repeat_tone;
4029
  u8 unused[13];
4030
} _5tone_std_settings[15];
4031

    
4032
#seekto 0x2500;
4033
struct {
4034
  u8 frame1[5];
4035
  u8 frame2[5];
4036
  u8 frame3[5];
4037
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4038
} _5tone_codes[15];
4039

    
4040
#seekto 0x25F0;
4041
struct {
4042
  u8 _5tone_delay1; // * 10ms
4043
  u8 _5tone_delay2; // * 10ms
4044
  u8 _5tone_delay3; // * 10ms
4045
  u8 _5tone_first_digit_ext_length;
4046
  u8 unknown1;
4047
  u8 unknown2;
4048
  u8 unknown3;
4049
  u8 unknown4;
4050
  u8 decode_standard;
4051
  u8 unknown5:5,
4052
     _5tone_decode_call_frame3:1,
4053
     _5tone_decode_call_frame2:1,
4054
     _5tone_decode_call_frame1:1;
4055
  u8 unknown6:5,
4056
     _5tone_decode_disp_frame3:1,
4057
     _5tone_decode_disp_frame2:1,
4058
     _5tone_decode_disp_frame1:1;
4059
  u8 decode_reset_time; // * 100 + 100ms
4060
} _5tone_settings;
4061

    
4062
#seekto 0x2900;
4063
struct {
4064
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4065
} dtmf_codes[15];
4066

    
4067
#seekto 0x29F0;
4068
struct {
4069
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4070
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4071
  u8 unknown0[14];
4072
  u8 inspection[16];
4073
  u8 monitor[16];
4074
  u8 alarmcode[16];
4075
  u8 stun[16];
4076
  u8 kill[16];
4077
  u8 revive[16];
4078
  u8 unknown1[16];
4079
  u8 unknown2[16];
4080
  u8 unknown3[16];
4081
  u8 unknown4[16];
4082
  u8 unknown5[16];
4083
  u8 unknown6[16];
4084
  u8 unknown7[16];
4085
  u8 masterid[16];
4086
  u8 viceid[16];
4087
  u8 unused01:7,
4088
     mastervice:1;
4089
  u8 unused02:3,
4090
     mrevive:1,
4091
     mkill:1,
4092
     mstun:1,
4093
     mmonitor:1,
4094
     minspection:1;
4095
  u8 unused03:3,
4096
     vrevive:1,
4097
     vkill:1,
4098
     vstun:1,
4099
     vmonitor:1,
4100
     vinspection:1;
4101
  u8 unused04:6,
4102
     txdisable:1,
4103
     rxdisable:1;
4104
  u8 groupcode;
4105
  u8 spacecode;
4106
  u8 delayproctime; // * 100 + 100ms
4107
  u8 resettime;     // * 100 + 100ms
4108
} dtmf_settings;
4109

    
4110
#seekto 0x2D00;
4111
struct {
4112
  struct {
4113
    ul16 freq1;
4114
    u8 unused01[6];
4115
    ul16 freq2;
4116
    u8 unused02[6];
4117
  } _2tone_encode[15];
4118
  u8 duration_1st_tone; // *10ms
4119
  u8 duration_2nd_tone; // *10ms
4120
  u8 duration_gap;      // *10ms
4121
  u8 unused03[13];
4122
  struct {
4123
    struct {
4124
      u8 dec;      // one out of LIST_2TONE_DEC
4125
      u8 response; // one out of LIST_2TONE_RESPONSE
4126
      u8 alert;    // 1-16
4127
    } decs[4];
4128
    u8 unused04[4];
4129
  } _2tone_decode[15];
4130
  u8 unused05[16];
4131

    
4132
  struct {
4133
    ul16 freqA;
4134
    ul16 freqB;
4135
    ul16 freqC;
4136
    ul16 freqD;
4137
    // unknown what those values mean, but they are
4138
    // derived from configured frequencies
4139
    ul16 derived_from_freqA; // 2304000/freqA
4140
    ul16 derived_from_freqB; // 2304000/freqB
4141
    ul16 derived_from_freqC; // 2304000/freqC
4142
    ul16 derived_from_freqD; // 2304000/freqD
4143
  }freqs[15];
4144
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4145
} _2tone;
4146

    
4147
#seekto 0x3000;
4148
struct {
4149
  u8 freq[8];
4150
  char broadcast_station_name[6];
4151
  u8 unknown[2];
4152
} fm_radio_preset[16];
4153

    
4154
#seekto 0x3200;
4155
struct {
4156
  u8 tmr;
4157
  u8 unknown1;
4158
  u8 sql;
4159
  u8 unknown2;
4160
  u8 autolk;
4161
  u8 tot;
4162
  u8 apo;
4163
  u8 unknown3;
4164
  u8 abr;
4165
  u8 beep;
4166
  u8 unknown4[4];
4167
  u8 dtmfst;
4168
  u8 unknown5[2];
4169
  u8 screv;
4170
  u8 unknown6[2];
4171
  u8 pttid;
4172
  u8 pttlt;
4173
  u8 unknown7;
4174
  u8 emctp;
4175
  u8 emcch;
4176
  u8 sigbp;
4177
  u8 unknown8;
4178
  u8 camdf;
4179
  u8 cbmdf;
4180
  u8 ccmdf;
4181
  u8 cdmdf;
4182
  u8 langua;
4183
  u8 sync;
4184

    
4185

    
4186
  u8 stfc;
4187
  u8 mffc;
4188
  u8 sfafc;
4189
  u8 sfbfc;
4190
  u8 sfcfc;
4191
  u8 sfdfc;
4192
  u8 subfc;
4193
  u8 fmfc;
4194
  u8 sigfc;
4195
  u8 modfc;
4196
  u8 menufc;
4197
  u8 txfc;
4198
  u8 txdisp;
4199
  u8 unknown9[5];
4200
  u8 anil;
4201
  u8 reps;
4202
  u8 repm;
4203
  u8 tmrmr;
4204
  u8 ste;
4205
  u8 rpste;
4206
  u8 rptdl;
4207
  u8 dtmfg;
4208
  u8 mgain;
4209
  u8 skiptx;
4210
  u8 scmode;
4211
} settings;
4212

    
4213
#seekto 0x3280;
4214
struct {
4215
  u8 unknown1;
4216
  u8 vfomr;
4217
  u8 keylock;
4218
  u8 unknown2;
4219
  u8 unknown3:4,
4220
     vfomren:1,
4221
     unknown4:1,
4222
     reseten:1,
4223
     menuen:1;
4224
  u8 unknown5[11];
4225
  u8 dispab;
4226
  u8 unknown6[2];
4227
  u8 smenu;
4228
  u8 unknown7[7];
4229
  u8 vfomra;
4230
  u8 vfomrb;
4231
  u8 vfomrc;
4232
  u8 vfomrd;
4233
  u8 mrcha;
4234
  u8 mrchb;
4235
  u8 mrchc;
4236
  u8 mrchd;
4237
} settings2;
4238

    
4239
struct settings_vfo {
4240
  u8 freq[8];
4241
  u8 offset[6];
4242
  u8 unknown2[2];
4243
  ul16 rxtone;
4244
  ul16 txtone;
4245
  u8 scode;
4246
  u8 spmute;
4247
  u8 optsig;
4248
  u8 scramble;
4249
  u8 wide;
4250
  u8 power;
4251
  u8 shiftd;
4252
  u8 step;
4253
  u8 unknown3[4];
4254
};
4255

    
4256
#seekto 0x3300;
4257
struct {
4258
  struct settings_vfo a;
4259
  struct settings_vfo b;
4260
  struct settings_vfo c;
4261
  struct settings_vfo d;
4262
} vfo;
4263

    
4264
#seekto 0x3D80;
4265
struct {
4266
  u8 vhf_low[3];
4267
  u8 vhf_high[3];
4268
  u8 unknown1[4];
4269
  u8 unknown2[6];
4270
  u8 vhf2_low[3];
4271
  u8 vhf2_high[3];
4272
  u8 unknown3[4];
4273
  u8 unknown4[6];
4274
  u8 uhf_low[3];
4275
  u8 uhf_high[3];
4276
  u8 unknown5[4];
4277
  u8 unknown6[6];
4278
  u8 uhf2_low[3];
4279
  u8 uhf2_high[3];
4280
} ranges;
4281

    
4282
#seekto 0x33B0;
4283
struct {
4284
  char line[16];
4285
} static_msg;
4286

    
4287
#seekto 0x3F70;
4288
struct {
4289
  char fp[6];
4290
} fingerprint;
4291

    
4292
"""
4293

    
4294

    
4295
class BTechGMRS(BTechMobileCommon):
4296
    """BTECH's GMRS Mobile"""
4297
    COLOR_LCD = True
4298
    COLOR_LCD2 = True
4299
    NAME_LENGTH = 7
4300
    UPLOAD_MEM_SIZE = 0X3400
4301

    
4302
    def process_mmap(self):
4303
        """Process the mem map into the mem object"""
4304

    
4305
        # Get it
4306
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
4307

    
4308
        # load specific parameters from the radio image
4309
        self.set_options()
4310

    
4311
    def set_options(self):
4312
        """This is to read the options from the image and set it in the
4313
        environment, for now just the limits of the freqs in the VHF/UHF
4314
        ranges"""
4315

    
4316
        # setting the correct ranges for each radio type
4317
        ranges = self._memobj.ranges
4318

    
4319
        # the normal dual bands
4320
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4321
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4322

    
4323
        # DEBUG
4324
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4325
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4326

    
4327
        # set the class with the real data
4328
        self._vhf_range = vhf
4329
        self._uhf_range = uhf
4330

    
4331

    
4332
# real radios
4333
@directory.register
4334
class GMRS50X1(BTechGMRS):
4335
    """Baofeng Tech GMRS50X1"""
4336
    MODEL = "GMRS-50X1"
4337
    BANDS = 2
4338
    LIST_TMR = LIST_TMR16
4339
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4340
                     chirp_common.PowerLevel("Mid", watts=10),
4341
                     chirp_common.PowerLevel("Low", watts=5)]
4342
    _vhf_range = (136000000, 175000000)
4343
    _uhf_range = (400000000, 521000000)
4344
    _upper = 255
4345
    _magic = MSTRING_GMRS50X1
4346
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
4347

    
4348

    
4349
COLORHT_MEM_FORMAT = """
4350
#seekto 0x0000;
4351
struct {
4352
  lbcd rxfreq[4];
4353
  lbcd txfreq[4];
4354
  ul16 rxtone;
4355
  ul16 txtone;
4356
  u8 unknown0:4,
4357
     scode:4;
4358
  u8 unknown1:2,
4359
     spmute:2,
4360
     unknown2:2,
4361
     optsig:2;
4362
  u8 unknown3:3,
4363
     scramble:1,
4364
     unknown4:3,
4365
     power:1;
4366
  u8 unknown5:1,
4367
     wide:1,
4368
     unknown6:2,
4369
     bcl:1,
4370
     add:1,
4371
     pttid:2;
4372
} memory[200];
4373

    
4374
#seekto 0x0E00;
4375
struct {
4376
  u8 tmr;
4377
  u8 unknownE01;
4378
  u8 sql;
4379
  u8 unknownE03[2];
4380
  u8 tot;
4381
  u8 save;
4382
  u8 unknownE07;
4383
  u8 abr;
4384
  u8 beep;
4385
  u8 unknownE0A[4];
4386
  u8 dsub;
4387
  u8 dtmfst;
4388
  u8 screv;
4389
  u8 unknownE11[3];
4390
  u8 pttid;
4391
  u8 unknownE15;
4392
  u8 pttlt;
4393
  u8 unknownE17;
4394
  u8 emctp;
4395
  u8 emcch;
4396
  u8 sigbp;
4397
  u8 unknownE1B;
4398
  u8 camdf;
4399
  u8 cbmdf;
4400
  u8 ccmdf;
4401
  u8 cdmdf;
4402
  u8 langua;
4403
  u8 voice;
4404
  u8 vox;
4405
  u8 voxt;
4406
  u8 sync;          // BTech radios use this as the display sync setting
4407
                    // other radios use this as the auto keypad lock setting
4408
  u8 stfc;
4409
  u8 mffc;
4410
  u8 sfafc;
4411
  u8 sfbfc;
4412
  u8 sfcfc;
4413
  u8 sfdfc;
4414
  u8 subfc;
4415
  u8 fmfc;
4416
  u8 sigfc;
4417
  u8 menufc;
4418
  u8 txfc;
4419
  u8 rxfc;
4420
  u8 unknownE31[5];
4421
  u8 anil;
4422
  u8 reps;
4423
  u8 tmrmr;
4424
  u8 ste;
4425
  u8 rpste;
4426
  u8 rptdl;
4427
  u8 dtmfg;
4428
  u8 tmrtx;
4429
} settings;
4430

    
4431
#seekto 0x0E80;
4432
struct {
4433
  u8 unknown1;
4434
  u8 vfomr;
4435
  u8 keylock;
4436
  u8 unknown2;
4437
  u8 unknown3:4,
4438
     vfomren:1,
4439
     unknown4:1,
4440
     reseten:1,
4441
     menuen:1;
4442
  u8 unknown5[11];
4443
  u8 dispab;
4444
  u8 unknown6[2];
4445
  u8 menu;
4446
  u8 unknown7[7];
4447
  u8 vfomra;
4448
  u8 vfomrb;
4449
  u8 vfomrc;
4450
  u8 vfomrd;
4451
  u8 mrcha;
4452
  u8 mrchb;
4453
  u8 mrchc;
4454
  u8 mrchd;
4455
} settings2;
4456

    
4457
struct settings_vfo {
4458
  u8 freq[8];
4459
  u8 offset[6];
4460
  u8 unknown2[2];
4461
  ul16 rxtone;
4462
  ul16 txtone;
4463
  u8 scode;
4464
  u8 spmute;
4465
  u8 optsig;
4466
  u8 scramble;
4467
  u8 wide;
4468
  u8 power;
4469
  u8 shiftd;
4470
  u8 step;
4471
  u8 unknown3[4];
4472
};
4473

    
4474
#seekto 0x0F00;
4475
struct {
4476
  struct settings_vfo a;
4477
  struct settings_vfo b;
4478
  struct settings_vfo c;
4479
  struct settings_vfo d;
4480
} vfo;
4481

    
4482
#seekto 0x0FE0;
4483
struct {
4484
  char line[16];
4485
} static_msg;
4486

    
4487
#seekto 0x1000;
4488
struct {
4489
  char name[8];
4490
  u8 unknown1[8];
4491
} names[200];
4492

    
4493
#seekto 0x2400;
4494
struct {
4495
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4496
  u8 group_tone;
4497
  u8 repeat_tone;
4498
  u8 unused[13];
4499
} _5tone_std_settings[15];
4500

    
4501
#seekto 0x2500;
4502
struct {
4503
  u8 frame1[5];
4504
  u8 frame2[5];
4505
  u8 frame3[5];
4506
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4507
} _5tone_codes[15];
4508

    
4509
#seekto 0x25F0;
4510
struct {
4511
  u8 _5tone_delay1; // * 10ms
4512
  u8 _5tone_delay2; // * 10ms
4513
  u8 _5tone_delay3; // * 10ms
4514
  u8 _5tone_first_digit_ext_length;
4515
  u8 unknown1;
4516
  u8 unknown2;
4517
  u8 unknown3;
4518
  u8 unknown4;
4519
  u8 decode_standard;
4520
  u8 unknown5:5,
4521
     _5tone_decode_call_frame3:1,
4522
     _5tone_decode_call_frame2:1,
4523
     _5tone_decode_call_frame1:1;
4524
  u8 unknown6:5,
4525
     _5tone_decode_disp_frame3:1,
4526
     _5tone_decode_disp_frame2:1,
4527
     _5tone_decode_disp_frame1:1;
4528
  u8 decode_reset_time; // * 100 + 100ms
4529
} _5tone_settings;
4530

    
4531
#seekto 0x2900;
4532
struct {
4533
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4534
} dtmf_codes[15];
4535

    
4536
#seekto 0x29F0;
4537
struct {
4538
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4539
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4540
  u8 unknown0[14];
4541
  u8 inspection[16];
4542
  u8 monitor[16];
4543
  u8 alarmcode[16];
4544
  u8 stun[16];
4545
  u8 kill[16];
4546
  u8 revive[16];
4547
  u8 unknown1[16];
4548
  u8 unknown2[16];
4549
  u8 unknown3[16];
4550
  u8 unknown4[16];
4551
  u8 unknown5[16];
4552
  u8 unknown6[16];
4553
  u8 unknown7[16];
4554
  u8 masterid[16];
4555
  u8 viceid[16];
4556
  u8 unused01:7,
4557
     mastervice:1;
4558
  u8 unused02:3,
4559
     mrevive:1,
4560
     mkill:1,
4561
     mstun:1,
4562
     mmonitor:1,
4563
     minspection:1;
4564
  u8 unused03:3,
4565
     vrevive:1,
4566
     vkill:1,
4567
     vstun:1,
4568
     vmonitor:1,
4569
     vinspection:1;
4570
  u8 unused04:6,
4571
     txdisable:1,
4572
     rxdisable:1;
4573
  u8 groupcode;
4574
  u8 spacecode;
4575
  u8 delayproctime; // * 100 + 100ms
4576
  u8 resettime;     // * 100 + 100ms
4577
} dtmf_settings;
4578

    
4579
#seekto 0x2D00;
4580
struct {
4581
  struct {
4582
    ul16 freq1;
4583
    u8 unused01[6];
4584
    ul16 freq2;
4585
    u8 unused02[6];
4586
  } _2tone_encode[15];
4587
  u8 duration_1st_tone; // *10ms
4588
  u8 duration_2nd_tone; // *10ms
4589
  u8 duration_gap;      // *10ms
4590
  u8 unused03[13];
4591
  struct {
4592
    struct {
4593
      u8 dec;      // one out of LIST_2TONE_DEC
4594
      u8 response; // one out of LIST_2TONE_RESPONSE
4595
      u8 alert;    // 1-16
4596
    } decs[4];
4597
    u8 unused04[4];
4598
  } _2tone_decode[15];
4599
  u8 unused05[16];
4600

    
4601
  struct {
4602
    ul16 freqA;
4603
    ul16 freqB;
4604
    ul16 freqC;
4605
    ul16 freqD;
4606
    // unknown what those values mean, but they are
4607
    // derived from configured frequencies
4608
    ul16 derived_from_freqA; // 2304000/freqA
4609
    ul16 derived_from_freqB; // 2304000/freqB
4610
    ul16 derived_from_freqC; // 2304000/freqC
4611
    ul16 derived_from_freqD; // 2304000/freqD
4612
  }freqs[15];
4613
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4614
} _2tone;
4615

    
4616
#seekto 0x3D80;
4617
struct {
4618
  u8 vhf_low[3];
4619
  u8 vhf_high[3];
4620
  u8 unknown1[4];
4621
  u8 unknown2[6];
4622
  u8 vhf2_low[3];
4623
  u8 vhf2_high[3];
4624
  u8 unknown3[4];
4625
  u8 unknown4[6];
4626
  u8 uhf_low[3];
4627
  u8 uhf_high[3];
4628
  u8 unknown5[4];
4629
  u8 unknown6[6];
4630
  u8 uhf2_low[3];
4631
  u8 uhf2_high[3];
4632
} ranges;
4633

    
4634
#seekto 0x3F70;
4635
struct {
4636
  char fp[6];
4637
} fingerprint;
4638

    
4639
"""
4640

    
4641

    
4642
class QYTColorHT(BTechMobileCommon):
4643
    """QTY's Color LCD Handheld and alike radios"""
4644
    COLOR_LCD = True
4645
    COLOR_LCD3 = True
4646
    NAME_LENGTH = 8
4647
    LIST_TMR = LIST_TMR15
4648

    
4649
    def process_mmap(self):
4650
        """Process the mem map into the mem object"""
4651

    
4652
        # Get it
4653
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4654

    
4655
        # load specific parameters from the radio image
4656
        self.set_options()
4657

    
4658
    def set_options(self):
4659
        """This is to read the options from the image and set it in the
4660
        environment, for now just the limits of the freqs in the VHF/UHF
4661
        ranges"""
4662

    
4663
        # setting the correct ranges for each radio type
4664
        ranges = self._memobj.ranges
4665

    
4666
        # the normal dual bands
4667
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4668
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4669

    
4670
        # DEBUG
4671
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4672
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4673

    
4674
        # the additional bands
4675
        if self.MODEL in ["KT-8R"]:
4676
            # 200Mhz band
4677
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
4678
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
4679
            self._220_range = vhf2
4680

    
4681
            # 350Mhz band
4682
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4683
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4684
            self._350_range = uhf2
4685

    
4686
        # set the class with the real data
4687
        self._vhf_range = vhf
4688
        self._uhf_range = uhf
4689

    
4690

    
4691
# real radios
4692
@directory.register
4693
class KT8R(QYTColorHT):
4694
    """QYT KT8R"""
4695
    VENDOR = "QYT"
4696
    MODEL = "KT-8R"
4697
    BANDS = 4
4698
    LIST_TMR = LIST_TMR15
4699
    _vhf_range = (136000000, 175000000)
4700
    _220_range = (200000000, 261000000)
4701
    _uhf_range = (400000000, 481000000)
4702
    _350_range = (350000000, 391000000)
4703
    _magic = MSTRING_KT8R
4704
    _fileid = [KT8R_fp2, KT8R_fp1, KT8R_fp, ]
4705
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
4706
                     chirp_common.PowerLevel("Low", watts=1)]
(8-8/15)