Project

General

Profile

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

Jim Unroe, 10/21/2021 01:26 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 == "DB25-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 self.MODEL == "DB25-G":
1537
            mgain = RadioSetting("settings.mgain", "Auto power-on",
1538
                                 RadioSettingValueBoolean(_mem.settings.mgain))
1539
            basic.append(mgain)
1540

    
1541
        if str(_mem.fingerprint.fp) in BTECH3:
1542
            mgain = RadioSetting("settings.mgain", "Mic gain",
1543
                                 RadioSettingValueInteger(0, 120,
1544
                                                          _mem.settings.mgain))
1545
            basic.append(mgain)
1546

    
1547
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1548
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain",
1549
                                 RadioSettingValueInteger(0, 60,
1550
                                                          _mem.settings.dtmfg))
1551
            basic.append(dtmfg)
1552

    
1553
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1554
            mgain = RadioSetting("settings.mgain", "Mic gain",
1555
                                 RadioSettingValueInteger(0, 120,
1556
                                                          _mem.settings.mgain))
1557
            basic.append(mgain)
1558

    
1559
            skiptx = RadioSetting("settings.skiptx", "Skip TX",
1560
                                  RadioSettingValueList(
1561
                                      LIST_SKIPTX,
1562
                                      LIST_SKIPTX[_mem.settings.skiptx]))
1563
            basic.append(skiptx)
1564

    
1565
            scmode = RadioSetting("settings.scmode", "Scan mode",
1566
                                  RadioSettingValueList(
1567
                                      LIST_SCMODE,
1568
                                      LIST_SCMODE[_mem.settings.scmode]))
1569
            basic.append(scmode)
1570

    
1571
        if self.MODEL == "KT-8R":
1572
            tmrtx = RadioSetting("settings.tmrtx", "TX in multi-standby",
1573
                                 RadioSettingValueList(
1574
                                     LIST_TMRTX,
1575
                                     LIST_TMRTX[_mem.settings.tmrtx]))
1576
            basic.append(tmrtx)
1577

    
1578
        # Advanced
1579
        def _filter(name):
1580
            filtered = ""
1581
            for char in str(name):
1582
                if char in VALID_CHARS:
1583
                    filtered += char
1584
                else:
1585
                    filtered += " "
1586
            return filtered
1587

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

    
1648
        if self.MODEL in ("UV-2501", "UV-5001"):
1649
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1650
                                   RadioSettingValueBoolean(
1651
                                       _mem.settings2.vfomren))
1652
            advanced.append(vfomren)
1653

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

    
1659
            menuen = RadioSetting("settings2.menuen", "Menu",
1660
                                  RadioSettingValueBoolean(
1661
                                      _mem.settings2.menuen))
1662
            advanced.append(menuen)
1663

    
1664
        # Other
1665
        def convert_bytes_to_limit(bytes):
1666
            limit = ""
1667
            for byte in bytes:
1668
                if byte < 10:
1669
                    limit += chr(byte + 0x30)
1670
                else:
1671
                    break
1672
            return limit
1673

    
1674
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1675
            _ranges = self._memobj.ranges220
1676
            ranges = "ranges220"
1677
        else:
1678
            _ranges = self._memobj.ranges
1679
            ranges = "ranges"
1680

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

    
1687
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1688
        val = RadioSettingValueString(0, 3, _limit)
1689
        val.set_mutable(False)
1690
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1691
        other.append(vhf_high)
1692

    
1693
        if self.BANDS == 3 or self.BANDS == 4:
1694
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1695
            val = RadioSettingValueString(0, 3, _limit)
1696
            val.set_mutable(False)
1697
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1698
            other.append(vhf2_low)
1699

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

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

    
1712
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1713
        val = RadioSettingValueString(0, 3, _limit)
1714
        val.set_mutable(False)
1715
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1716
        other.append(uhf_high)
1717

    
1718
        if self.BANDS == 4:
1719
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1720
            val = RadioSettingValueString(0, 3, _limit)
1721
            val.set_mutable(False)
1722
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1723
            other.append(uhf2_low)
1724

    
1725
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1726
            val = RadioSettingValueString(0, 3, _limit)
1727
            val.set_mutable(False)
1728
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1729
            other.append(uhf2_high)
1730

    
1731
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1732
        val.set_mutable(False)
1733
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1734
        other.append(fp)
1735

    
1736
        # Work
1737
        if self.COLOR_LCD:
1738
            dispab = RadioSetting("settings2.dispab", "Display",
1739
                                  RadioSettingValueList(
1740
                                      LIST_ABCD,
1741
                                      LIST_ABCD[_mem.settings2.dispab]))
1742
            work.append(dispab)
1743
        else:
1744
            dispab = RadioSetting("settings2.dispab", "Display",
1745
                                  RadioSettingValueList(
1746
                                      LIST_AB,
1747
                                      LIST_AB[_mem.settings2.dispab]))
1748
            work.append(dispab)
1749

    
1750
        if self.COLOR_LCD:
1751
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode",
1752
                                  RadioSettingValueList(
1753
                                      LIST_VFOMR,
1754
                                      LIST_VFOMR[_mem.settings2.vfomra]))
1755
            work.append(vfomra)
1756

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

    
1763
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode",
1764
                                  RadioSettingValueList(
1765
                                      LIST_VFOMR,
1766
                                      LIST_VFOMR[_mem.settings2.vfomrc]))
1767
            work.append(vfomrc)
1768

    
1769
            vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode",
1770
                                  RadioSettingValueList(
1771
                                      LIST_VFOMR,
1772
                                      LIST_VFOMR[_mem.settings2.vfomrd]))
1773
            work.append(vfomrd)
1774
        else:
1775
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode",
1776
                                 RadioSettingValueList(
1777
                                     LIST_VFOMR,
1778
                                     LIST_VFOMR[_mem.settings2.vfomr]))
1779
            work.append(vfomr)
1780

    
1781
        keylock = RadioSetting("settings2.keylock", "Keypad lock",
1782
                               RadioSettingValueBoolean(
1783
                                   _mem.settings2.keylock))
1784
        work.append(keylock)
1785

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

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

    
1796
        if self.COLOR_LCD:
1797
            mrchc = RadioSetting("settings2.mrchc", "MR C channel",
1798
                                 RadioSettingValueInteger(
1799
                                     0, self._upper, _mem.settings2.mrchc))
1800
            work.append(mrchc)
1801

    
1802
            mrchd = RadioSetting("settings2.mrchd", "MR D channel",
1803
                                 RadioSettingValueInteger(
1804
                                     0, self._upper, _mem.settings2.mrchd))
1805
            work.append(mrchd)
1806

    
1807
        def convert_bytes_to_freq(bytes):
1808
            real_freq = 0
1809
            for byte in bytes:
1810
                real_freq = (real_freq * 10) + byte
1811
            return chirp_common.format_freq(real_freq * 10)
1812

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

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

    
1856
        def apply_freq(setting, obj):
1857
            value = chirp_common.parse_freq(str(setting.value)) / 10
1858
            for i in range(7, -1, -1):
1859
                obj.freq[i] = value % 10
1860
                value /= 10
1861

    
1862
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1863
                                        _mem.vfo.a.freq))
1864
        val1a.set_validate_callback(my_validate)
1865
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
1866
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
1867
        work.append(vfoafreq)
1868

    
1869
        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1870
                                        _mem.vfo.b.freq))
1871
        val1b.set_validate_callback(my_validate)
1872
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
1873
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
1874
        work.append(vfobfreq)
1875

    
1876
        if self.COLOR_LCD:
1877
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1878
                                            _mem.vfo.c.freq))
1879
            val1c.set_validate_callback(my_validate)
1880
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
1881
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
1882
            work.append(vfocfreq)
1883

    
1884
            val1d = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1885
                                            _mem.vfo.d.freq))
1886
            val1d.set_validate_callback(my_validate)
1887
            vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
1888
            vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
1889
            work.append(vfodfreq)
1890

    
1891
        if not self.MODEL == "GMRS-50X1":
1892
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift",
1893
                                      RadioSettingValueList(
1894
                                          LIST_SHIFT,
1895
                                          LIST_SHIFT[_mem.vfo.a.shiftd]))
1896
            work.append(vfoashiftd)
1897

    
1898
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift",
1899
                                      RadioSettingValueList(
1900
                                          LIST_SHIFT,
1901
                                          LIST_SHIFT[_mem.vfo.b.shiftd]))
1902
            work.append(vfobshiftd)
1903

    
1904
            if self.COLOR_LCD:
1905
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift",
1906
                                          RadioSettingValueList(
1907
                                              LIST_SHIFT,
1908
                                              LIST_SHIFT[_mem.vfo.c.shiftd]))
1909
                work.append(vfocshiftd)
1910

    
1911
                vfodshiftd = RadioSetting("vfo.d.shiftd", "VFO D shift",
1912
                                          RadioSettingValueList(
1913
                                              LIST_SHIFT,
1914
                                              LIST_SHIFT[_mem.vfo.d.shiftd]))
1915
                work.append(vfodshiftd)
1916

    
1917
        def convert_bytes_to_offset(bytes):
1918
            real_offset = 0
1919
            for byte in bytes:
1920
                real_offset = (real_offset * 10) + byte
1921
            return chirp_common.format_freq(real_offset * 1000)
1922

    
1923
        def apply_offset(setting, obj):
1924
            value = chirp_common.parse_freq(str(setting.value)) / 1000
1925
            for i in range(5, -1, -1):
1926
                obj.offset[i] = value % 10
1927
                value /= 10
1928

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

    
1939
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1940
                                                _mem.vfo.b.offset))
1941
                vfoboffset = RadioSetting("vfo.b.offset",
1942
                                          "VFO B offset (0.000-999.999)",
1943
                                          val1b)
1944
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1945
                work.append(vfoboffset)
1946

    
1947
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1948
                                                _mem.vfo.c.offset))
1949
                vfocoffset = RadioSetting("vfo.c.offset",
1950
                                          "VFO C offset (0.000-999.999)",
1951
                                          val1c)
1952
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
1953
                work.append(vfocoffset)
1954

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

    
1970
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1971
                                                _mem.vfo.b.offset))
1972
                vfoboffset = RadioSetting("vfo.b.offset",
1973
                                          "VFO B offset (0.000-99.999)", val1b)
1974
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1975
                work.append(vfoboffset)
1976

    
1977
        if not self.MODEL == "GMRS-50X1":
1978
            vfoatxp = RadioSetting("vfo.a.power", "VFO A power",
1979
                                   RadioSettingValueList(
1980
                                       LIST_TXP,
1981
                                       LIST_TXP[_mem.vfo.a.power]))
1982
            work.append(vfoatxp)
1983

    
1984
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power",
1985
                                   RadioSettingValueList(
1986
                                       LIST_TXP,
1987
                                       LIST_TXP[_mem.vfo.b.power]))
1988
            work.append(vfobtxp)
1989

    
1990
            if self.COLOR_LCD:
1991
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power",
1992
                                       RadioSettingValueList(
1993
                                           LIST_TXP,
1994
                                           LIST_TXP[_mem.vfo.c.power]))
1995
                work.append(vfoctxp)
1996

    
1997
                vfodtxp = RadioSetting("vfo.d.power", "VFO D power",
1998
                                       RadioSettingValueList(
1999
                                           LIST_TXP,
2000
                                           LIST_TXP[_mem.vfo.d.power]))
2001
                work.append(vfodtxp)
2002

    
2003
        if not self.MODEL == "GMRS-50X1":
2004
            vfoawide = RadioSetting("vfo.a.wide", "VFO A bandwidth",
2005
                                    RadioSettingValueList(
2006
                                        LIST_WIDE,
2007
                                        LIST_WIDE[_mem.vfo.a.wide]))
2008
            work.append(vfoawide)
2009

    
2010
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth",
2011
                                    RadioSettingValueList(
2012
                                        LIST_WIDE,
2013
                                        LIST_WIDE[_mem.vfo.b.wide]))
2014
            work.append(vfobwide)
2015

    
2016
            if self.COLOR_LCD:
2017
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth",
2018
                                        RadioSettingValueList(
2019
                                            LIST_WIDE,
2020
                                            LIST_WIDE[_mem.vfo.c.wide]))
2021
                work.append(vfocwide)
2022

    
2023
                vfodwide = RadioSetting("vfo.d.wide", "VFO D bandwidth",
2024
                                        RadioSettingValueList(
2025
                                            LIST_WIDE,
2026
                                            LIST_WIDE[_mem.vfo.d.wide]))
2027
                work.append(vfodwide)
2028

    
2029
        vfoastep = RadioSetting("vfo.a.step", "VFO A step",
2030
                                RadioSettingValueList(
2031
                                    LIST_STEP,
2032
                                    LIST_STEP[_mem.vfo.a.step]))
2033
        work.append(vfoastep)
2034

    
2035
        vfobstep = RadioSetting("vfo.b.step", "VFO B step",
2036
                                RadioSettingValueList(
2037
                                    LIST_STEP,
2038
                                    LIST_STEP[_mem.vfo.b.step]))
2039
        work.append(vfobstep)
2040

    
2041
        if self.COLOR_LCD:
2042
            vfocstep = RadioSetting("vfo.c.step", "VFO C step",
2043
                                    RadioSettingValueList(
2044
                                        LIST_STEP,
2045
                                        LIST_STEP[_mem.vfo.c.step]))
2046
            work.append(vfocstep)
2047

    
2048
            vfodstep = RadioSetting("vfo.d.step", "VFO D step",
2049
                                    RadioSettingValueList(
2050
                                        LIST_STEP,
2051
                                        LIST_STEP[_mem.vfo.d.step]))
2052
            work.append(vfodstep)
2053

    
2054
        vfoaoptsig = RadioSetting("vfo.a.optsig", "VFO A optional signal",
2055
                                  RadioSettingValueList(
2056
                                      OPTSIG_LIST,
2057
                                      OPTSIG_LIST[_mem.vfo.a.optsig]))
2058
        work.append(vfoaoptsig)
2059

    
2060
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal",
2061
                                  RadioSettingValueList(
2062
                                      OPTSIG_LIST,
2063
                                      OPTSIG_LIST[_mem.vfo.b.optsig]))
2064
        work.append(vfoboptsig)
2065

    
2066
        if self.COLOR_LCD:
2067
            vfocoptsig = RadioSetting("vfo.c.optsig", "VFO C optional signal",
2068
                                      RadioSettingValueList(
2069
                                          OPTSIG_LIST,
2070
                                          OPTSIG_LIST[_mem.vfo.c.optsig]))
2071
            work.append(vfocoptsig)
2072

    
2073
            vfodoptsig = RadioSetting("vfo.d.optsig", "VFO D optional signal",
2074
                                      RadioSettingValueList(
2075
                                          OPTSIG_LIST,
2076
                                          OPTSIG_LIST[_mem.vfo.d.optsig]))
2077
            work.append(vfodoptsig)
2078

    
2079
        vfoaspmute = RadioSetting("vfo.a.spmute", "VFO A speaker mute",
2080
                                  RadioSettingValueList(
2081
                                      SPMUTE_LIST,
2082
                                      SPMUTE_LIST[_mem.vfo.a.spmute]))
2083
        work.append(vfoaspmute)
2084

    
2085
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute",
2086
                                  RadioSettingValueList(
2087
                                      SPMUTE_LIST,
2088
                                      SPMUTE_LIST[_mem.vfo.b.spmute]))
2089
        work.append(vfobspmute)
2090

    
2091
        if self.COLOR_LCD:
2092
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute",
2093
                                      RadioSettingValueList(
2094
                                          SPMUTE_LIST,
2095
                                          SPMUTE_LIST[_mem.vfo.c.spmute]))
2096
            work.append(vfocspmute)
2097

    
2098
            vfodspmute = RadioSetting("vfo.d.spmute", "VFO D speaker mute",
2099
                                      RadioSettingValueList(
2100
                                          SPMUTE_LIST,
2101
                                          SPMUTE_LIST[_mem.vfo.d.spmute]))
2102
            work.append(vfodspmute)
2103

    
2104
        if not self.COLOR_LCD or \
2105
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2106
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble",
2107
                                   RadioSettingValueBoolean(
2108
                                       _mem.vfo.a.scramble))
2109
            work.append(vfoascr)
2110

    
2111
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble",
2112
                                   RadioSettingValueBoolean(
2113
                                       _mem.vfo.b.scramble))
2114
            work.append(vfobscr)
2115

    
2116
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2117
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble",
2118
                                   RadioSettingValueBoolean(
2119
                                       _mem.vfo.c.scramble))
2120
            work.append(vfocscr)
2121

    
2122
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble",
2123
                                   RadioSettingValueBoolean(
2124
                                       _mem.vfo.d.scramble))
2125
            work.append(vfodscr)
2126

    
2127
        if not self.MODEL == "GMRS-50X1":
2128
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID",
2129
                                     RadioSettingValueList(
2130
                                         PTTIDCODE_LIST,
2131
                                         PTTIDCODE_LIST[_mem.vfo.a.scode]))
2132
            work.append(vfoascode)
2133

    
2134
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID",
2135
                                     RadioSettingValueList(
2136
                                         PTTIDCODE_LIST,
2137
                                         PTTIDCODE_LIST[_mem.vfo.b.scode]))
2138
            work.append(vfobscode)
2139

    
2140
            if self.COLOR_LCD:
2141
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID",
2142
                                         RadioSettingValueList(
2143
                                             PTTIDCODE_LIST,
2144
                                             PTTIDCODE_LIST[_mem.vfo.c.scode]))
2145
                work.append(vfocscode)
2146

    
2147
                vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID",
2148
                                         RadioSettingValueList(
2149
                                             PTTIDCODE_LIST,
2150
                                             PTTIDCODE_LIST[_mem.vfo.d.scode]))
2151
                work.append(vfodscode)
2152

    
2153
        if not self.MODEL == "GMRS-50X1":
2154
            pttid = RadioSetting("settings.pttid", "PTT ID",
2155
                                 RadioSettingValueList(
2156
                                     PTTID_LIST,
2157
                                     PTTID_LIST[_mem.settings.pttid]))
2158
            work.append(pttid)
2159

    
2160
        if not self.COLOR_LCD:
2161
            # FM presets
2162
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2163
            top.append(fm_presets)
2164

    
2165
            def fm_validate(value):
2166
                if value == 0:
2167
                    return chirp_common.format_freq(value)
2168
                if not (87.5 <= value and value <= 108.0):  # 87.5-108MHz
2169
                    msg = ("FM-Preset-Frequency: " +
2170
                           "Must be between 87.5 and 108 MHz")
2171
                    raise InvalidValueError(msg)
2172
                return value
2173

    
2174
            def apply_fm_preset_name(setting, obj):
2175
                valstring = str(setting.value)
2176
                for i in range(0, 6):
2177
                    if valstring[i] in VALID_CHARS:
2178
                        obj[i] = valstring[i]
2179
                    else:
2180
                        obj[i] = '0xff'
2181

    
2182
            def apply_fm_freq(setting, obj):
2183
                value = chirp_common.parse_freq(str(setting.value)) / 10
2184
                for i in range(7, -1, -1):
2185
                    obj.freq[i] = value % 10
2186
                    value /= 10
2187

    
2188
            _presets = self._memobj.fm_radio_preset
2189
            i = 1
2190
            for preset in _presets:
2191
                line = RadioSetting("fm_presets_" + str(i),
2192
                                    "Station name " + str(i),
2193
                                    RadioSettingValueString(0, 6, _filter(
2194
                                        preset.broadcast_station_name)))
2195
                line.set_apply_callback(apply_fm_preset_name,
2196
                                        preset.broadcast_station_name)
2197

    
2198
                val = RadioSettingValueFloat(0, 108,
2199
                                             convert_bytes_to_freq(
2200
                                                 preset.freq))
2201
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2202
                                      "Frequency " + str(i), val)
2203
                val.set_validate_callback(fm_validate)
2204
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2205
                fm_presets.append(line)
2206
                fm_presets.append(fmfreq)
2207

    
2208
                i = i + 1
2209

    
2210
        # DTMF-Setting
2211
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2212
                                              "DTMF Encoding Settings")
2213
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2214
                                              "DTMF Decoding Settings")
2215
        top.append(dtmf_enc_settings)
2216
        top.append(dtmf_dec_settings)
2217
        txdisable = RadioSetting("dtmf_settings.txdisable",
2218
                                 "TX-Disable",
2219
                                 RadioSettingValueBoolean(
2220
                                     _mem.dtmf_settings.txdisable))
2221
        dtmf_enc_settings.append(txdisable)
2222

    
2223
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2224
                                 "RX-Disable",
2225
                                 RadioSettingValueBoolean(
2226
                                     _mem.dtmf_settings.rxdisable))
2227
        dtmf_enc_settings.append(rxdisable)
2228

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

    
2241
        if _mem.dtmf_settings.dtmfspeed_off > 0x0F:
2242
            val = 0x03
2243
        else:
2244
            val = _mem.dtmf_settings.dtmfspeed_off
2245
        dtmfspeed_off = RadioSetting(
2246
            "dtmf_settings.dtmfspeed_off",
2247
            "DTMF Speed (Off Time)",
2248
            RadioSettingValueList(LIST_DTMF_SPEED,
2249
                                  LIST_DTMF_SPEED[
2250
                                      val]))
2251
        dtmf_enc_settings.append(dtmfspeed_off)
2252

    
2253
        def memory2string(dmtf_mem):
2254
            dtmf_string = ""
2255
            for digit in dmtf_mem:
2256
                if digit != 255:
2257
                    index = LIST_DTMF_VALUES.index(digit)
2258
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2259
            return dtmf_string
2260

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

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

    
2285
        line = RadioSetting("dtmf_settings.mastervice",
2286
                            "Master and Vice ID",
2287
                            RadioSettingValueBoolean(
2288
                                _mem.dtmf_settings.mastervice))
2289
        dtmf_dec_settings.append(line)
2290

    
2291
        val = RadioSettingValueString(0, 16, memory2string(
2292
                                          _mem.dtmf_settings.masterid),
2293
                                      False, CHARSET_DTMF_DIGITS)
2294
        line = RadioSetting("dtmf_settings.masterid",
2295
                            "Master Control ID ", val)
2296
        line.set_apply_callback(apply_dmtf_frame,
2297
                                _mem.dtmf_settings.masterid)
2298
        dtmf_dec_settings.append(line)
2299

    
2300
        line = RadioSetting("dtmf_settings.minspection",
2301
                            "Master Inspection",
2302
                            RadioSettingValueBoolean(
2303
                                _mem.dtmf_settings.minspection))
2304
        dtmf_dec_settings.append(line)
2305

    
2306
        line = RadioSetting("dtmf_settings.mmonitor",
2307
                            "Master Monitor",
2308
                            RadioSettingValueBoolean(
2309
                                _mem.dtmf_settings.mmonitor))
2310
        dtmf_dec_settings.append(line)
2311

    
2312
        line = RadioSetting("dtmf_settings.mstun",
2313
                            "Master Stun",
2314
                            RadioSettingValueBoolean(
2315
                                _mem.dtmf_settings.mstun))
2316
        dtmf_dec_settings.append(line)
2317

    
2318
        line = RadioSetting("dtmf_settings.mkill",
2319
                            "Master Kill",
2320
                            RadioSettingValueBoolean(
2321
                                _mem.dtmf_settings.mkill))
2322
        dtmf_dec_settings.append(line)
2323

    
2324
        line = RadioSetting("dtmf_settings.mrevive",
2325
                            "Master Revive",
2326
                            RadioSettingValueBoolean(
2327
                                _mem.dtmf_settings.mrevive))
2328
        dtmf_dec_settings.append(line)
2329

    
2330
        val = RadioSettingValueString(0, 16, memory2string(
2331
                                          _mem.dtmf_settings.viceid),
2332
                                      False, CHARSET_DTMF_DIGITS)
2333
        line = RadioSetting("dtmf_settings.viceid",
2334
                            "Vice Control ID ", val)
2335
        line.set_apply_callback(apply_dmtf_frame,
2336
                                _mem.dtmf_settings.viceid)
2337
        dtmf_dec_settings.append(line)
2338

    
2339
        line = RadioSetting("dtmf_settings.vinspection",
2340
                            "Vice Inspection",
2341
                            RadioSettingValueBoolean(
2342
                                _mem.dtmf_settings.vinspection))
2343
        dtmf_dec_settings.append(line)
2344

    
2345
        line = RadioSetting("dtmf_settings.vmonitor",
2346
                            "Vice Monitor",
2347
                            RadioSettingValueBoolean(
2348
                                _mem.dtmf_settings.vmonitor))
2349
        dtmf_dec_settings.append(line)
2350

    
2351
        line = RadioSetting("dtmf_settings.vstun",
2352
                            "Vice Stun",
2353
                            RadioSettingValueBoolean(
2354
                                _mem.dtmf_settings.vstun))
2355
        dtmf_dec_settings.append(line)
2356

    
2357
        line = RadioSetting("dtmf_settings.vkill",
2358
                            "Vice Kill",
2359
                            RadioSettingValueBoolean(
2360
                                _mem.dtmf_settings.vkill))
2361
        dtmf_dec_settings.append(line)
2362

    
2363
        line = RadioSetting("dtmf_settings.vrevive",
2364
                            "Vice Revive",
2365
                            RadioSettingValueBoolean(
2366
                                _mem.dtmf_settings.vrevive))
2367
        dtmf_dec_settings.append(line)
2368

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

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

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

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

    
2405
        val = RadioSettingValueString(0, 16, memory2string(
2406
                                          _mem.dtmf_settings.stun),
2407
                                      False, CHARSET_DTMF_DIGITS)
2408
        line = RadioSetting("dtmf_settings.stun",
2409
                            "Stun", val)
2410
        line.set_apply_callback(apply_dmtf_frame,
2411
                                _mem.dtmf_settings.stun)
2412
        dtmf_dec_settings.append(line)
2413

    
2414
        val = RadioSettingValueString(0, 16, memory2string(
2415
                                          _mem.dtmf_settings.revive),
2416
                                      False, CHARSET_DTMF_DIGITS)
2417
        line = RadioSetting("dtmf_settings.revive",
2418
                            "Revive", val)
2419
        line.set_apply_callback(apply_dmtf_frame,
2420
                                _mem.dtmf_settings.revive)
2421
        dtmf_dec_settings.append(line)
2422

    
2423
        def apply_dmtf_listvalue(setting, obj):
2424
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2425
            val = str(setting.value)
2426
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2427
            val = LIST_DTMF_SPECIAL_VALUES[index]
2428
            obj.set_value(val)
2429

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

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

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

    
2479
        if _mem.dtmf_settings.delayproctime > 0x27:
2480
            val = 0x04
2481
        else:
2482
            val = _mem.dtmf_settings.delayproctime
2483
        line = RadioSetting(
2484
            "dtmf_settings.delayproctime",
2485
            "Delay processing time",
2486
            RadioSettingValueList(LIST_DTMF_DELAY,
2487
                                  LIST_DTMF_DELAY[
2488
                                      val]))
2489
        dtmf_dec_settings.append(line)
2490

    
2491
        # 5 Tone Settings
2492
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2493
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2494

    
2495
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2496
        group_5tone.append(stds_5tone)
2497
        group_5tone.append(codes_5tone)
2498

    
2499
        top.append(group_5tone)
2500

    
2501
        def apply_list_value(setting, obj):
2502
            options = setting.value.get_options()
2503
            obj.set_value(options.index(str(setting.value)))
2504

    
2505
        _5tone_standards = self._memobj._5tone_std_settings
2506
        i = 0
2507
        for standard in _5tone_standards:
2508
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2509
                                          LIST_5TONE_STANDARDS[i])
2510
            stds_5tone.append(std_5tone)
2511

    
2512
            period = standard.period
2513
            if period == 255:
2514
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2515
                          " is not yet configured. Setting to 70ms.")
2516
                period = 5
2517

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

    
2529
            group_tone = standard.group_tone
2530
            if group_tone == 255:
2531
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2532
                          " is not yet configured. Setting to A.")
2533
                group_tone = 10
2534

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

    
2548
            repeat_tone = standard.repeat_tone
2549
            if repeat_tone == 255:
2550
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2551
                          " is not yet configured. Setting to E.")
2552
                repeat_tone = 14
2553

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

    
2568
        def my_apply_5tonestdlist_value(setting, obj):
2569
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2570
                obj.set_value(0xFF)
2571
            else:
2572
                obj.set_value(LIST_5TONE_STANDARDS.
2573
                              index(str(setting.value)))
2574

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

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

    
2605
        def frame2string(frame):
2606
            frameString = ""
2607
            for digit in frame:
2608
                if digit != 255:
2609
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2610
            return frameString
2611

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

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

    
2639
            val = RadioSettingValueString(0, 6,
2640
                                          frame2string(code.frame2), False)
2641
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2642
                                " Frame 2", val)
2643
            val.set_validate_callback(validate_5tone_frame)
2644
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2645
            code_5tone.append(line)
2646

    
2647
            val = RadioSettingValueString(0, 6,
2648
                                          frame2string(code.frame3), False)
2649
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2650
                                " Frame 3", val)
2651
            val.set_validate_callback(validate_5tone_frame)
2652
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2653
            code_5tone.append(line)
2654
            i = i + 1
2655

    
2656
        _5_tone_decode1 = RadioSetting(
2657
            "_5tone_settings._5tone_decode_call_frame1",
2658
            "5 Tone decode call Frame 1",
2659
            RadioSettingValueBoolean(
2660
                _mem._5tone_settings._5tone_decode_call_frame1))
2661
        group_5tone.append(_5_tone_decode1)
2662

    
2663
        _5_tone_decode2 = RadioSetting(
2664
            "_5tone_settings._5tone_decode_call_frame2",
2665
            "5 Tone decode call Frame 2",
2666
            RadioSettingValueBoolean(
2667
                _mem._5tone_settings._5tone_decode_call_frame2))
2668
        group_5tone.append(_5_tone_decode2)
2669

    
2670
        _5_tone_decode3 = RadioSetting(
2671
            "_5tone_settings._5tone_decode_call_frame3",
2672
            "5 Tone decode call Frame 3",
2673
            RadioSettingValueBoolean(
2674
                _mem._5tone_settings._5tone_decode_call_frame3))
2675
        group_5tone.append(_5_tone_decode3)
2676

    
2677
        _5_tone_decode_disp1 = RadioSetting(
2678
            "_5tone_settings._5tone_decode_disp_frame1",
2679
            "5 Tone decode disp Frame 1",
2680
            RadioSettingValueBoolean(
2681
                _mem._5tone_settings._5tone_decode_disp_frame1))
2682
        group_5tone.append(_5_tone_decode_disp1)
2683

    
2684
        _5_tone_decode_disp2 = RadioSetting(
2685
            "_5tone_settings._5tone_decode_disp_frame2",
2686
            "5 Tone decode disp Frame 2",
2687
            RadioSettingValueBoolean(
2688
                _mem._5tone_settings._5tone_decode_disp_frame2))
2689
        group_5tone.append(_5_tone_decode_disp2)
2690

    
2691
        _5_tone_decode_disp3 = RadioSetting(
2692
            "_5tone_settings._5tone_decode_disp_frame3",
2693
            "5 Tone decode disp Frame 3",
2694
            RadioSettingValueBoolean(
2695
                _mem._5tone_settings._5tone_decode_disp_frame3))
2696
        group_5tone.append(_5_tone_decode_disp3)
2697

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

    
2712
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2713
        if _5tone_delay1 == 255:
2714
            _5tone_delay1 = 20
2715

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

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

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

    
2741
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2742
        if _5tone_delay3 == 255:
2743
            _5tone_delay3 = 20
2744
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2745

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

    
2756
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2757
        if ext_length == 255:
2758
            ext_length = 0
2759
            LOG.debug("1st Tone ext lenght unconfigured! Resetting to 0")
2760

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

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

    
2788
        # 2 Tone
2789
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2790
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2791

    
2792
        top.append(encode_2tone)
2793
        top.append(decode_2tone)
2794

    
2795
        duration_1st_tone = self._memobj._2tone.duration_1st_tone
2796
        if duration_1st_tone == 255:
2797
            LOG.debug("Duration of first 2 Tone digit is not yet " +
2798
                      "configured. Setting to 600ms")
2799
            duration_1st_tone = 60
2800

    
2801
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2802
            line = RadioSetting("_2tone.duration_1st_tone",
2803
                                "Duration 1st Tone",
2804
                                RadioSettingValueList(LIST_5TONE_DELAY,
2805
                                                      LIST_5TONE_DELAY[
2806
                                                          duration_1st_tone]))
2807
            encode_2tone.append(line)
2808

    
2809
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2810
        if duration_2nd_tone == 255:
2811
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2812
                      "configured. Setting to 600ms")
2813
            duration_2nd_tone = 60
2814

    
2815
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2816
            line = RadioSetting("_2tone.duration_2nd_tone",
2817
                                "Duration 2nd Tone",
2818
                                RadioSettingValueList(LIST_5TONE_DELAY,
2819
                                                      LIST_5TONE_DELAY[
2820
                                                          duration_2nd_tone]))
2821
            encode_2tone.append(line)
2822

    
2823
        duration_gap = self._memobj._2tone.duration_gap
2824
        if duration_gap == 255:
2825
            LOG.debug("Duration of gap is not yet " +
2826
                      "configured. Setting to 300ms")
2827
            duration_gap = 30
2828

    
2829
        if duration_gap <= len(LIST_5TONE_DELAY):
2830
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2831
                                RadioSettingValueList(LIST_5TONE_DELAY,
2832
                                                      LIST_5TONE_DELAY[
2833
                                                          duration_gap]))
2834
            encode_2tone.append(line)
2835

    
2836
        def _2tone_validate(value):
2837
            if value == 0:
2838
                return 65535
2839
            if value == 65535:
2840
                return value
2841
            if not (300 <= value and value <= 3000):
2842
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
2843
                raise InvalidValueError(msg)
2844
            return value
2845

    
2846
        def apply_2tone_freq(setting, obj):
2847
            val = int(setting.value)
2848
            if (val == 0) or (val == 65535):
2849
                obj.set_value(65535)
2850
            else:
2851
                obj.set_value(val)
2852

    
2853
        i = 1
2854
        for code in self._memobj._2tone._2tone_encode:
2855
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
2856
                                           "Encode Code " + str(i))
2857
            encode_2tone.append(code_2tone)
2858

    
2859
            tmp = code.freq1
2860
            if tmp == 65535:
2861
                tmp = 0
2862
            val1 = RadioSettingValueInteger(0, 65535, tmp)
2863
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
2864
                                 "Frequency 1", val1)
2865
            val1.set_validate_callback(_2tone_validate)
2866
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
2867
            code_2tone.append(freq1)
2868

    
2869
            tmp = code.freq2
2870
            if tmp == 65535:
2871
                tmp = 0
2872
            val2 = RadioSettingValueInteger(0, 65535, tmp)
2873
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
2874
                                 "Frequency 2", val2)
2875
            val2.set_validate_callback(_2tone_validate)
2876
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
2877
            code_2tone.append(freq2)
2878

    
2879
            i = i + 1
2880

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

    
2896
        def apply_2tone_freq_pair(setting, obj):
2897
            val = int(setting.value)
2898
            derived_val = 65535
2899
            frqname = str(setting._name[-5:])
2900
            derivedname = "derived_from_" + frqname
2901

    
2902
            if (val == 0):
2903
                val = 65535
2904
                derived_val = 65535
2905
            else:
2906
                derived_val = int(round(2304000.0/val))
2907

    
2908
            obj[frqname].set_value(val)
2909
            obj[derivedname].set_value(derived_val)
2910

    
2911
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
2912
                      derivedname + ": " + str(derived_val))
2913

    
2914
        i = 1
2915
        for decode_code in self._memobj._2tone._2tone_decode:
2916
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
2917
                                                "Decode Code " + str(i))
2918
            decode_2tone.append(_2tone_dec_code)
2919

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

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

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

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

    
2956
                val = dec.alert
2957
                if val == 255:
2958
                    LOG.debug("Alert for Code " + str(i) + " Dec " + str(j) +
2959
                              " is not yet configured. Setting to 0.")
2960
                    val = 0
2961

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

    
2974
            freq = self._memobj._2tone.freqs[i-1]
2975
            for char in ['A', 'B', 'C', 'D']:
2976
                setting_name = "freq" + str(char)
2977

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

    
2996
            i = i + 1
2997

    
2998
        return top
2999

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

    
3027
                    if element.has_apply_callback():
3028
                        LOG.debug("Using apply callback")
3029
                        element.run_apply_callback()
3030
                    elif element.value.get_mutable():
3031
                        LOG.debug("Setting %s = %s" % (setting, element.value))
3032
                        setattr(obj, setting, element.value)
3033
                except Exception, e:
3034
                    LOG.debug(element.get_name())
3035
                    raise
3036

    
3037
    @classmethod
3038
    def match_model(cls, filedata, filename):
3039
        match_size = False
3040
        match_model = False
3041

    
3042
        # testing the file data size
3043
        if len(filedata) == MEM_SIZE:
3044
            match_size = True
3045

    
3046
        # testing the firmware model fingerprint
3047
        match_model = model_match(cls, filedata)
3048

    
3049
        if match_size and match_model:
3050
            return True
3051
        else:
3052
            return False
3053

    
3054

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

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

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

    
3144
#seekto 0x0EC0;
3145
struct {
3146
  char line1[6];
3147
  char line2[6];
3148
} poweron_msg;
3149

    
3150
struct settings_vfo {
3151
  u8 freq[8];
3152
  u8 offset[6];
3153
  u8 unknown2[2];
3154
  ul16 rxtone;
3155
  ul16 txtone;
3156
  u8 scode;
3157
  u8 spmute;
3158
  u8 optsig;
3159
  u8 scramble;
3160
  u8 wide;
3161
  u8 power;
3162
  u8 shiftd;
3163
  u8 step;
3164
  u8 unknown3[4];
3165
};
3166

    
3167
#seekto 0x0F00;
3168
struct {
3169
  struct settings_vfo a;
3170
  struct settings_vfo b;
3171
} vfo;
3172

    
3173
#seekto 0x1000;
3174
struct {
3175
  char name[6];
3176
  u8 unknown1[10];
3177
} names[200];
3178

    
3179
#seekto 0x2400;
3180
struct {
3181
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3182
  u8 group_tone;
3183
  u8 repeat_tone;
3184
  u8 unused[13];
3185
} _5tone_std_settings[15];
3186

    
3187
#seekto 0x2500;
3188
struct {
3189
  u8 frame1[5];
3190
  u8 frame2[5];
3191
  u8 frame3[5];
3192
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3193
} _5tone_codes[15];
3194

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

    
3217
#seekto 0x2900;
3218
struct {
3219
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3220
} dtmf_codes[15];
3221

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

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

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

    
3302
#seekto 0x3000;
3303
struct {
3304
  u8 freq[8];
3305
  char broadcast_station_name[6];
3306
  u8 unknown[2];
3307
} fm_radio_preset[16];
3308

    
3309
#seekto 0x3C90;
3310
struct {
3311
  u8 vhf_low[3];
3312
  u8 vhf_high[3];
3313
  u8 uhf_low[3];
3314
  u8 uhf_high[3];
3315
} ranges;
3316

    
3317
// the UV-2501+220 & KT8900R has different zones for storing ranges
3318

    
3319
#seekto 0x3CD0;
3320
struct {
3321
  u8 vhf_low[3];
3322
  u8 vhf_high[3];
3323
  u8 unknown1[4];
3324
  u8 unknown2[6];
3325
  u8 vhf2_low[3];
3326
  u8 vhf2_high[3];
3327
  u8 unknown3[4];
3328
  u8 unknown4[6];
3329
  u8 uhf_low[3];
3330
  u8 uhf_high[3];
3331
} ranges220;
3332

    
3333
#seekto 0x3F70;
3334
struct {
3335
  char fp[6];
3336
} fingerprint;
3337

    
3338
"""
3339

    
3340

    
3341
class BTech(BTechMobileCommon):
3342
    """BTECH's UV-5001 and alike radios"""
3343
    BANDS = 2
3344
    COLOR_LCD = False
3345
    NAME_LENGTH = 6
3346

    
3347
    def set_options(self):
3348
        """This is to read the options from the image and set it in the
3349
        environment, for now just the limits of the freqs in the VHF/UHF
3350
        ranges"""
3351

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

    
3361
        # the normal dual bands
3362
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3363
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3364

    
3365
        # DEBUG
3366
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3367
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3368

    
3369
        # 220Mhz radios case
3370
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3371
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3372
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3373
            self._220_range = vhf2
3374

    
3375
        # set the class with the real data
3376
        self._vhf_range = vhf
3377
        self._uhf_range = uhf
3378

    
3379
    def process_mmap(self):
3380
        """Process the mem map into the mem object"""
3381

    
3382
        # Get it
3383
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3384

    
3385
        # load specific parameters from the radio image
3386
        self.set_options()
3387

    
3388

    
3389
# Declaring Aliases (Clones of the real radios)
3390
class JT2705M(chirp_common.Alias):
3391
    VENDOR = "Jetstream"
3392
    MODEL = "JT2705M"
3393

    
3394

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

    
3399

    
3400
class JT6188Plus(chirp_common.Alias):
3401
    VENDOR = "Juentai"
3402
    MODEL = "JT-6188 Plus"
3403

    
3404

    
3405
class SSGT890(chirp_common.Alias):
3406
    VENDOR = "Sainsonic"
3407
    MODEL = "GT-890"
3408

    
3409

    
3410
class ZastoneMP300(chirp_common.Alias):
3411
    VENDOR = "Zastone"
3412
    MODEL = "MP-300"
3413

    
3414

    
3415
# real radios
3416
@directory.register
3417
class UV2501(BTech):
3418
    """Baofeng Tech UV2501"""
3419
    MODEL = "UV-2501"
3420
    _fileid = [UV2501G3_fp,
3421
               UV2501G2_fp,
3422
               UV2501pp2_fp,
3423
               UV2501pp_fp]
3424

    
3425

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

    
3438

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

    
3451

    
3452
@directory.register
3453
class MINI8900(BTech):
3454
    """WACCOM MINI-8900"""
3455
    VENDOR = "WACCOM"
3456
    MODEL = "MINI-8900"
3457
    _magic = MSTRING_MINI8900
3458
    _fileid = [MINI8900_fp, ]
3459
    # Clones
3460
    ALIASES = [JT6188Plus, ]
3461

    
3462

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

    
3475
# Please note that there is a version of this radios that is a clone of the
3476
# Waccom Mini8900, maybe an early version?
3477

    
3478

    
3479
class OTGRadioV1(chirp_common.Alias):
3480
    VENDOR = 'OTGSTUFF'
3481
    MODEL = 'OTG Radio v1'
3482

    
3483

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

    
3503

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

    
3521

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

    
3535

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

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

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

    
3644
struct settings_vfo {
3645
  u8 freq[8];
3646
  u8 offset[6];
3647
  u8 unknown2[2];
3648
  ul16 rxtone;
3649
  ul16 txtone;
3650
  u8 scode;
3651
  u8 spmute;
3652
  u8 optsig;
3653
  u8 scramble;
3654
  u8 wide;
3655
  u8 power;
3656
  u8 shiftd;
3657
  u8 step;
3658
  u8 unknown3[4];
3659
};
3660

    
3661
#seekto 0x0F00;
3662
struct {
3663
  struct settings_vfo a;
3664
  struct settings_vfo b;
3665
  struct settings_vfo c;
3666
  struct settings_vfo d;
3667
} vfo;
3668

    
3669
#seekto 0x0F80;
3670
struct {
3671
  char line1[8];
3672
  char line2[8];
3673
  char line3[8];
3674
  char line4[8];
3675
  char line5[8];
3676
  char line6[8];
3677
  char line7[8];
3678
  char line8[8];
3679
} poweron_msg;
3680

    
3681
#seekto 0x1000;
3682
struct {
3683
  char name[8];
3684
  u8 unknown1[8];
3685
} names[200];
3686

    
3687
#seekto 0x2400;
3688
struct {
3689
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3690
  u8 group_tone;
3691
  u8 repeat_tone;
3692
  u8 unused[13];
3693
} _5tone_std_settings[15];
3694

    
3695
#seekto 0x2500;
3696
struct {
3697
  u8 frame1[5];
3698
  u8 frame2[5];
3699
  u8 frame3[5];
3700
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3701
} _5tone_codes[15];
3702

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

    
3725
#seekto 0x2900;
3726
struct {
3727
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3728
} dtmf_codes[15];
3729

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

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

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

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

    
3828
#seekto 0x3F70;
3829
struct {
3830
  char fp[6];
3831
} fingerprint;
3832

    
3833
"""
3834

    
3835

    
3836
class BTechColor(BTechMobileCommon):
3837
    """BTECH's Color LCD Mobile and alike radios"""
3838
    COLOR_LCD = True
3839
    NAME_LENGTH = 8
3840
    LIST_TMR = LIST_TMR16
3841

    
3842
    def process_mmap(self):
3843
        """Process the mem map into the mem object"""
3844

    
3845
        # Get it
3846
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
3847

    
3848
        # load specific parameters from the radio image
3849
        self.set_options()
3850

    
3851
    def set_options(self):
3852
        """This is to read the options from the image and set it in the
3853
        environment, for now just the limits of the freqs in the VHF/UHF
3854
        ranges"""
3855

    
3856
        # setting the correct ranges for each radio type
3857
        ranges = self._memobj.ranges
3858

    
3859
        # the normal dual bands
3860
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3861
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3862

    
3863
        # DEBUG
3864
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3865
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3866

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

    
3874
            # 350Mhz band
3875
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
3876
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
3877
            self._350_range = uhf2
3878

    
3879
        # set the class with the real data
3880
        self._vhf_range = vhf
3881
        self._uhf_range = uhf
3882

    
3883

    
3884
# Declaring Aliases (Clones of the real radios)
3885
class SKT8900D(chirp_common.Alias):
3886
    VENDOR = "Surecom"
3887
    MODEL = "S-KT8900D"
3888

    
3889

    
3890
class QB25(chirp_common.Alias):
3891
    VENDOR = "Radioddity"
3892
    MODEL = "QB25"
3893

    
3894

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

    
3906

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

    
3919

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

    
3932

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

    
3950

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

    
3963
    # Clones
3964
    ALIASES = [OTGRadioV1]
3965

    
3966

    
3967
@directory.register
3968
class KT5800(BTechColor):
3969
    """QYT KT5800"""
3970
    VENDOR = "QYT"
3971
    MODEL = "KT5800"
3972
    BANDS = 2
3973
    LIST_TMR = LIST_TMR15
3974
    _vhf_range = (136000000, 175000000)
3975
    _uhf_range = (400000000, 481000000)
3976
    _magic = MSTRING_KT8900D
3977
    _fileid = [KT5800_fp, ]
3978

    
3979

    
3980
@directory.register
3981
class DB25G(BTechColor):
3982
    """Radioddity DB25-G"""
3983
    VENDOR = "Radioddity"
3984
    MODEL = "DB25-G"
3985
    BANDS = 2
3986
    LIST_TMR = LIST_TMR15
3987
    _vhf_range = (136000000, 175000000)
3988
    _uhf_range = (400000000, 481000000)
3989
    _magic = MSTRING_KT8900D
3990
    _fileid = [DB25G_fp, ]
3991
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
3992
                     chirp_common.PowerLevel("Mid", watts=15),
3993
                     chirp_common.PowerLevel("Low", watts=5)]
3994

    
3995
    @classmethod
3996
    def match_model(cls, filedata, filename):
3997
        # This model is only ever matched via metadata
3998
        return False
3999

    
4000

    
4001
GMRS_MEM_FORMAT = """
4002
#seekto 0x0000;
4003
struct {
4004
  lbcd rxfreq[4];
4005
  lbcd txfreq[4];
4006
  ul16 rxtone;
4007
  ul16 txtone;
4008
  u8 unknown0:4,
4009
     scode:4;
4010
  u8 unknown1:2,
4011
     spmute:2,
4012
     unknown2:2,
4013
     optsig:2;
4014
  u8 unknown3:3,
4015
     scramble:1,
4016
     unknown4:2,
4017
     power:2;
4018
  u8 unknown5:1,
4019
     wide:1,
4020
     unknown6:2,
4021
     bcl:1,
4022
     add:1,
4023
     pttid:2;
4024
} memory[256];
4025

    
4026
#seekto 0x1000;
4027
struct {
4028
  char name[7];
4029
  u8 unknown1[9];
4030
} names[256];
4031

    
4032
#seekto 0x2400;
4033
struct {
4034
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4035
  u8 group_tone;
4036
  u8 repeat_tone;
4037
  u8 unused[13];
4038
} _5tone_std_settings[15];
4039

    
4040
#seekto 0x2500;
4041
struct {
4042
  u8 frame1[5];
4043
  u8 frame2[5];
4044
  u8 frame3[5];
4045
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4046
} _5tone_codes[15];
4047

    
4048
#seekto 0x25F0;
4049
struct {
4050
  u8 _5tone_delay1; // * 10ms
4051
  u8 _5tone_delay2; // * 10ms
4052
  u8 _5tone_delay3; // * 10ms
4053
  u8 _5tone_first_digit_ext_length;
4054
  u8 unknown1;
4055
  u8 unknown2;
4056
  u8 unknown3;
4057
  u8 unknown4;
4058
  u8 decode_standard;
4059
  u8 unknown5:5,
4060
     _5tone_decode_call_frame3:1,
4061
     _5tone_decode_call_frame2:1,
4062
     _5tone_decode_call_frame1:1;
4063
  u8 unknown6:5,
4064
     _5tone_decode_disp_frame3:1,
4065
     _5tone_decode_disp_frame2:1,
4066
     _5tone_decode_disp_frame1:1;
4067
  u8 decode_reset_time; // * 100 + 100ms
4068
} _5tone_settings;
4069

    
4070
#seekto 0x2900;
4071
struct {
4072
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4073
} dtmf_codes[15];
4074

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

    
4118
#seekto 0x2D00;
4119
struct {
4120
  struct {
4121
    ul16 freq1;
4122
    u8 unused01[6];
4123
    ul16 freq2;
4124
    u8 unused02[6];
4125
  } _2tone_encode[15];
4126
  u8 duration_1st_tone; // *10ms
4127
  u8 duration_2nd_tone; // *10ms
4128
  u8 duration_gap;      // *10ms
4129
  u8 unused03[13];
4130
  struct {
4131
    struct {
4132
      u8 dec;      // one out of LIST_2TONE_DEC
4133
      u8 response; // one out of LIST_2TONE_RESPONSE
4134
      u8 alert;    // 1-16
4135
    } decs[4];
4136
    u8 unused04[4];
4137
  } _2tone_decode[15];
4138
  u8 unused05[16];
4139

    
4140
  struct {
4141
    ul16 freqA;
4142
    ul16 freqB;
4143
    ul16 freqC;
4144
    ul16 freqD;
4145
    // unknown what those values mean, but they are
4146
    // derived from configured frequencies
4147
    ul16 derived_from_freqA; // 2304000/freqA
4148
    ul16 derived_from_freqB; // 2304000/freqB
4149
    ul16 derived_from_freqC; // 2304000/freqC
4150
    ul16 derived_from_freqD; // 2304000/freqD
4151
  }freqs[15];
4152
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4153
} _2tone;
4154

    
4155
#seekto 0x3000;
4156
struct {
4157
  u8 freq[8];
4158
  char broadcast_station_name[6];
4159
  u8 unknown[2];
4160
} fm_radio_preset[16];
4161

    
4162
#seekto 0x3200;
4163
struct {
4164
  u8 tmr;
4165
  u8 unknown1;
4166
  u8 sql;
4167
  u8 unknown2;
4168
  u8 autolk;
4169
  u8 tot;
4170
  u8 apo;
4171
  u8 unknown3;
4172
  u8 abr;
4173
  u8 beep;
4174
  u8 unknown4[4];
4175
  u8 dtmfst;
4176
  u8 unknown5[2];
4177
  u8 screv;
4178
  u8 unknown6[2];
4179
  u8 pttid;
4180
  u8 pttlt;
4181
  u8 unknown7;
4182
  u8 emctp;
4183
  u8 emcch;
4184
  u8 sigbp;
4185
  u8 unknown8;
4186
  u8 camdf;
4187
  u8 cbmdf;
4188
  u8 ccmdf;
4189
  u8 cdmdf;
4190
  u8 langua;
4191
  u8 sync;
4192

    
4193

    
4194
  u8 stfc;
4195
  u8 mffc;
4196
  u8 sfafc;
4197
  u8 sfbfc;
4198
  u8 sfcfc;
4199
  u8 sfdfc;
4200
  u8 subfc;
4201
  u8 fmfc;
4202
  u8 sigfc;
4203
  u8 modfc;
4204
  u8 menufc;
4205
  u8 txfc;
4206
  u8 txdisp;
4207
  u8 unknown9[5];
4208
  u8 anil;
4209
  u8 reps;
4210
  u8 repm;
4211
  u8 tmrmr;
4212
  u8 ste;
4213
  u8 rpste;
4214
  u8 rptdl;
4215
  u8 dtmfg;
4216
  u8 mgain;
4217
  u8 skiptx;
4218
  u8 scmode;
4219
} settings;
4220

    
4221
#seekto 0x3280;
4222
struct {
4223
  u8 unknown1;
4224
  u8 vfomr;
4225
  u8 keylock;
4226
  u8 unknown2;
4227
  u8 unknown3:4,
4228
     vfomren:1,
4229
     unknown4:1,
4230
     reseten:1,
4231
     menuen:1;
4232
  u8 unknown5[11];
4233
  u8 dispab;
4234
  u8 unknown6[2];
4235
  u8 smenu;
4236
  u8 unknown7[7];
4237
  u8 vfomra;
4238
  u8 vfomrb;
4239
  u8 vfomrc;
4240
  u8 vfomrd;
4241
  u8 mrcha;
4242
  u8 mrchb;
4243
  u8 mrchc;
4244
  u8 mrchd;
4245
} settings2;
4246

    
4247
struct settings_vfo {
4248
  u8 freq[8];
4249
  u8 offset[6];
4250
  u8 unknown2[2];
4251
  ul16 rxtone;
4252
  ul16 txtone;
4253
  u8 scode;
4254
  u8 spmute;
4255
  u8 optsig;
4256
  u8 scramble;
4257
  u8 wide;
4258
  u8 power;
4259
  u8 shiftd;
4260
  u8 step;
4261
  u8 unknown3[4];
4262
};
4263

    
4264
#seekto 0x3300;
4265
struct {
4266
  struct settings_vfo a;
4267
  struct settings_vfo b;
4268
  struct settings_vfo c;
4269
  struct settings_vfo d;
4270
} vfo;
4271

    
4272
#seekto 0x3D80;
4273
struct {
4274
  u8 vhf_low[3];
4275
  u8 vhf_high[3];
4276
  u8 unknown1[4];
4277
  u8 unknown2[6];
4278
  u8 vhf2_low[3];
4279
  u8 vhf2_high[3];
4280
  u8 unknown3[4];
4281
  u8 unknown4[6];
4282
  u8 uhf_low[3];
4283
  u8 uhf_high[3];
4284
  u8 unknown5[4];
4285
  u8 unknown6[6];
4286
  u8 uhf2_low[3];
4287
  u8 uhf2_high[3];
4288
} ranges;
4289

    
4290
#seekto 0x33B0;
4291
struct {
4292
  char line[16];
4293
} static_msg;
4294

    
4295
#seekto 0x3F70;
4296
struct {
4297
  char fp[6];
4298
} fingerprint;
4299

    
4300
"""
4301

    
4302

    
4303
class BTechGMRS(BTechMobileCommon):
4304
    """BTECH's GMRS Mobile"""
4305
    COLOR_LCD = True
4306
    COLOR_LCD2 = True
4307
    NAME_LENGTH = 7
4308
    UPLOAD_MEM_SIZE = 0X3400
4309

    
4310
    def process_mmap(self):
4311
        """Process the mem map into the mem object"""
4312

    
4313
        # Get it
4314
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
4315

    
4316
        # load specific parameters from the radio image
4317
        self.set_options()
4318

    
4319
    def set_options(self):
4320
        """This is to read the options from the image and set it in the
4321
        environment, for now just the limits of the freqs in the VHF/UHF
4322
        ranges"""
4323

    
4324
        # setting the correct ranges for each radio type
4325
        ranges = self._memobj.ranges
4326

    
4327
        # the normal dual bands
4328
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4329
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4330

    
4331
        # DEBUG
4332
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4333
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4334

    
4335
        # set the class with the real data
4336
        self._vhf_range = vhf
4337
        self._uhf_range = uhf
4338

    
4339

    
4340
# real radios
4341
@directory.register
4342
class GMRS50X1(BTechGMRS):
4343
    """Baofeng Tech GMRS50X1"""
4344
    MODEL = "GMRS-50X1"
4345
    BANDS = 2
4346
    LIST_TMR = LIST_TMR16
4347
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4348
                     chirp_common.PowerLevel("Mid", watts=10),
4349
                     chirp_common.PowerLevel("Low", watts=5)]
4350
    _vhf_range = (136000000, 175000000)
4351
    _uhf_range = (400000000, 521000000)
4352
    _upper = 255
4353
    _magic = MSTRING_GMRS50X1
4354
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
4355

    
4356

    
4357
COLORHT_MEM_FORMAT = """
4358
#seekto 0x0000;
4359
struct {
4360
  lbcd rxfreq[4];
4361
  lbcd txfreq[4];
4362
  ul16 rxtone;
4363
  ul16 txtone;
4364
  u8 unknown0:4,
4365
     scode:4;
4366
  u8 unknown1:2,
4367
     spmute:2,
4368
     unknown2:2,
4369
     optsig:2;
4370
  u8 unknown3:3,
4371
     scramble:1,
4372
     unknown4:3,
4373
     power:1;
4374
  u8 unknown5:1,
4375
     wide:1,
4376
     unknown6:2,
4377
     bcl:1,
4378
     add:1,
4379
     pttid:2;
4380
} memory[200];
4381

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

    
4439
#seekto 0x0E80;
4440
struct {
4441
  u8 unknown1;
4442
  u8 vfomr;
4443
  u8 keylock;
4444
  u8 unknown2;
4445
  u8 unknown3:4,
4446
     vfomren:1,
4447
     unknown4:1,
4448
     reseten:1,
4449
     menuen:1;
4450
  u8 unknown5[11];
4451
  u8 dispab;
4452
  u8 unknown6[2];
4453
  u8 menu;
4454
  u8 unknown7[7];
4455
  u8 vfomra;
4456
  u8 vfomrb;
4457
  u8 vfomrc;
4458
  u8 vfomrd;
4459
  u8 mrcha;
4460
  u8 mrchb;
4461
  u8 mrchc;
4462
  u8 mrchd;
4463
} settings2;
4464

    
4465
struct settings_vfo {
4466
  u8 freq[8];
4467
  u8 offset[6];
4468
  u8 unknown2[2];
4469
  ul16 rxtone;
4470
  ul16 txtone;
4471
  u8 scode;
4472
  u8 spmute;
4473
  u8 optsig;
4474
  u8 scramble;
4475
  u8 wide;
4476
  u8 power;
4477
  u8 shiftd;
4478
  u8 step;
4479
  u8 unknown3[4];
4480
};
4481

    
4482
#seekto 0x0F00;
4483
struct {
4484
  struct settings_vfo a;
4485
  struct settings_vfo b;
4486
  struct settings_vfo c;
4487
  struct settings_vfo d;
4488
} vfo;
4489

    
4490
#seekto 0x0FE0;
4491
struct {
4492
  char line[16];
4493
} static_msg;
4494

    
4495
#seekto 0x1000;
4496
struct {
4497
  char name[8];
4498
  u8 unknown1[8];
4499
} names[200];
4500

    
4501
#seekto 0x2400;
4502
struct {
4503
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4504
  u8 group_tone;
4505
  u8 repeat_tone;
4506
  u8 unused[13];
4507
} _5tone_std_settings[15];
4508

    
4509
#seekto 0x2500;
4510
struct {
4511
  u8 frame1[5];
4512
  u8 frame2[5];
4513
  u8 frame3[5];
4514
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4515
} _5tone_codes[15];
4516

    
4517
#seekto 0x25F0;
4518
struct {
4519
  u8 _5tone_delay1; // * 10ms
4520
  u8 _5tone_delay2; // * 10ms
4521
  u8 _5tone_delay3; // * 10ms
4522
  u8 _5tone_first_digit_ext_length;
4523
  u8 unknown1;
4524
  u8 unknown2;
4525
  u8 unknown3;
4526
  u8 unknown4;
4527
  u8 decode_standard;
4528
  u8 unknown5:5,
4529
     _5tone_decode_call_frame3:1,
4530
     _5tone_decode_call_frame2:1,
4531
     _5tone_decode_call_frame1:1;
4532
  u8 unknown6:5,
4533
     _5tone_decode_disp_frame3:1,
4534
     _5tone_decode_disp_frame2:1,
4535
     _5tone_decode_disp_frame1:1;
4536
  u8 decode_reset_time; // * 100 + 100ms
4537
} _5tone_settings;
4538

    
4539
#seekto 0x2900;
4540
struct {
4541
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4542
} dtmf_codes[15];
4543

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

    
4587
#seekto 0x2D00;
4588
struct {
4589
  struct {
4590
    ul16 freq1;
4591
    u8 unused01[6];
4592
    ul16 freq2;
4593
    u8 unused02[6];
4594
  } _2tone_encode[15];
4595
  u8 duration_1st_tone; // *10ms
4596
  u8 duration_2nd_tone; // *10ms
4597
  u8 duration_gap;      // *10ms
4598
  u8 unused03[13];
4599
  struct {
4600
    struct {
4601
      u8 dec;      // one out of LIST_2TONE_DEC
4602
      u8 response; // one out of LIST_2TONE_RESPONSE
4603
      u8 alert;    // 1-16
4604
    } decs[4];
4605
    u8 unused04[4];
4606
  } _2tone_decode[15];
4607
  u8 unused05[16];
4608

    
4609
  struct {
4610
    ul16 freqA;
4611
    ul16 freqB;
4612
    ul16 freqC;
4613
    ul16 freqD;
4614
    // unknown what those values mean, but they are
4615
    // derived from configured frequencies
4616
    ul16 derived_from_freqA; // 2304000/freqA
4617
    ul16 derived_from_freqB; // 2304000/freqB
4618
    ul16 derived_from_freqC; // 2304000/freqC
4619
    ul16 derived_from_freqD; // 2304000/freqD
4620
  }freqs[15];
4621
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4622
} _2tone;
4623

    
4624
#seekto 0x3D80;
4625
struct {
4626
  u8 vhf_low[3];
4627
  u8 vhf_high[3];
4628
  u8 unknown1[4];
4629
  u8 unknown2[6];
4630
  u8 vhf2_low[3];
4631
  u8 vhf2_high[3];
4632
  u8 unknown3[4];
4633
  u8 unknown4[6];
4634
  u8 uhf_low[3];
4635
  u8 uhf_high[3];
4636
  u8 unknown5[4];
4637
  u8 unknown6[6];
4638
  u8 uhf2_low[3];
4639
  u8 uhf2_high[3];
4640
} ranges;
4641

    
4642
#seekto 0x3F70;
4643
struct {
4644
  char fp[6];
4645
} fingerprint;
4646

    
4647
"""
4648

    
4649

    
4650
class QYTColorHT(BTechMobileCommon):
4651
    """QTY's Color LCD Handheld and alike radios"""
4652
    COLOR_LCD = True
4653
    COLOR_LCD3 = True
4654
    NAME_LENGTH = 8
4655
    LIST_TMR = LIST_TMR15
4656

    
4657
    def process_mmap(self):
4658
        """Process the mem map into the mem object"""
4659

    
4660
        # Get it
4661
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4662

    
4663
        # load specific parameters from the radio image
4664
        self.set_options()
4665

    
4666
    def set_options(self):
4667
        """This is to read the options from the image and set it in the
4668
        environment, for now just the limits of the freqs in the VHF/UHF
4669
        ranges"""
4670

    
4671
        # setting the correct ranges for each radio type
4672
        ranges = self._memobj.ranges
4673

    
4674
        # the normal dual bands
4675
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4676
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4677

    
4678
        # DEBUG
4679
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4680
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4681

    
4682
        # the additional bands
4683
        if self.MODEL in ["KT-8R"]:
4684
            # 200Mhz band
4685
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
4686
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
4687
            self._220_range = vhf2
4688

    
4689
            # 350Mhz band
4690
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4691
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4692
            self._350_range = uhf2
4693

    
4694
        # set the class with the real data
4695
        self._vhf_range = vhf
4696
        self._uhf_range = uhf
4697

    
4698

    
4699
# real radios
4700
@directory.register
4701
class KT8R(QYTColorHT):
4702
    """QYT KT8R"""
4703
    VENDOR = "QYT"
4704
    MODEL = "KT-8R"
4705
    BANDS = 4
4706
    LIST_TMR = LIST_TMR15
4707
    _vhf_range = (136000000, 175000000)
4708
    _220_range = (200000000, 261000000)
4709
    _uhf_range = (400000000, 481000000)
4710
    _350_range = (350000000, 391000000)
4711
    _magic = MSTRING_KT8R
4712
    _fileid = [KT8R_fp2, KT8R_fp1, KT8R_fp, ]
4713
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
4714
                     chirp_common.PowerLevel("Low", watts=1)]
(13-13/15)