Project

General

Profile

Bug #8199 » btech_VC4254.py

Custom driver module for testing radios with MCU Version VC4254 - Jim Unroe, 08/25/2020 06:44 AM

 
1
# Copyright 2016-2017:
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_COLOR7 = ["White", "Red", "Blue", "Green", "Yellow", "Indego",
63
               "Purple", "Gray"]
64
LIST_COLOR8 = ["Black"] + LIST_COLOR7
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_TOT = ["%s sec" % x for x in range(15, 615, 15)]
93
LIST_TXDISP = ["Power", "Mic Volume"]
94
LIST_TXP = ["High", "Low"]
95
LIST_TXP3 = ["High", "Mid", "Low"]
96
LIST_SCREV = ["TO (timeout)", "CO (carrier operated)", "SE (search)"]
97
LIST_VFOMR = ["Frequency", "Channel"]
98
LIST_WIDE = ["Wide", "Narrow"]
99

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

    
131
# This is a general serial timeout for all serial read functions.
132
# Practice has show that about 0.7 sec will be enough to cover all radios.
133
STIMEOUT = 0.7
134

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

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

    
143

    
144
# #### ID strings #####################################################
145

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

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

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

    
178
# B-TECH UV-25X2
179
UV25X2_fp = "UC2012"
180

    
181
# B-TECH UV-25X4
182
UV25X4_fp = "UC4014"
183

    
184
# B-TECH UV-50X2
185
UV50X2_fp = "UC2M12"
186

    
187
# B-TECH GMRS-50X1
188
GMRS50X1_fp = "NC1802"
189
GMRS50X1_fp1 = "NC1932"
190

    
191
# special var to know when we found a BTECH Gen 3
192
BTECH3 = [UV2501G3_fp, UV2501_220G3_fp, UV5001G3_fp]
193

    
194

    
195
# WACCOM Mini-8900
196
MINI8900_fp = "M28854"
197

    
198

    
199
# QYT KT-UV980
200
KTUV980_fp = "H28854"
201

    
202
# QYT KT8900
203
KT8900_fp = "M29154"
204
# New generations KT8900
205
KT8900_fp1 = "M2C234"
206
KT8900_fp2 = "M2G1F4"
207
KT8900_fp3 = "M2G2F4"
208
KT8900_fp4 = "M2G304"
209
KT8900_fp5 = "M2G314"
210
# this radio has an extra ID
211
KT8900_id = "303688"
212

    
213
# KT8900R
214
KT8900R_fp = "M3G1F4"
215
# Second Generation
216
KT8900R_fp1 = "M3G214"
217
# another model
218
KT8900R_fp2 = "M3C234"
219
# another model G4?
220
KT8900R_fp3 = "M39164"
221
# another model
222
KT8900R_fp4 = "M3G314"
223
# this radio has an extra ID
224
KT8900R_id = "280528"
225
# another extra ID in dec/2018
226
KT8900R_id2 = "\x05\x58\x3d\xf0\x10"
227

    
228
# KT7900D (quad band)
229
KT7900D_fp = "VC4004"
230
KT7900D_fp1 = "VC4284"
231
KT7900D_fp2 = "VC4264"
232
KT7900D_fp3 = "VC4114"
233
KT7900D_fp4 = "VC4104"
234
KT7900D_fp5 = "VC4254"
235

    
236
# QB25 (quad band) - a clone of KT7900D
237
QB25_fp = "QB-25"
238

    
239
# KT8900D (dual band)
240
KT8900D_fp = "VC2002"
241
KT8900D_fp1 = "VC8632"
242

    
243
# LUITON LT-588UV
244
LT588UV_fp = "V2G1F4"
245
# Added by rstrickoff gen 2 id
246
LT588UV_fp1 = "V2G214"
247

    
248

    
249
# ### MAGICS
250
# for the Waccom Mini-8900
251
MSTRING_MINI8900 = "\x55\xA5\xB5\x45\x55\x45\x4d\x02"
252
# for the B-TECH UV-2501+220 (including pre production ones)
253
MSTRING_220 = "\x55\x20\x15\x12\x12\x01\x4d\x02"
254
# for the QYT KT8900 & R
255
MSTRING_KT8900 = "\x55\x20\x15\x09\x16\x45\x4D\x02"
256
MSTRING_KT8900R = "\x55\x20\x15\x09\x25\x01\x4D\x02"
257
# magic string for all other models
258
MSTRING = "\x55\x20\x15\x09\x20\x45\x4d\x02"
259
# for the QYT KT7900D & KT8900D
260
MSTRING_KT8900D = "\x55\x20\x16\x08\x01\xFF\xDC\x02"
261
# for the BTECH UV-25X2 and UV-50X2
262
MSTRING_UV25X2 = "\x55\x20\x16\x12\x28\xFF\xDC\x02"
263
# for the BTECH UV-25X4
264
MSTRING_UV25X4 = "\x55\x20\x16\x11\x18\xFF\xDC\x02"
265
# for the BTECH GMRS-50X1
266
MSTRING_GMRS50X1 = "\x55\x20\x18\x10\x18\xFF\xDC\x02"
267

    
268

    
269
def _clean_buffer(radio):
270
    """Cleaning the read serial buffer, hard timeout to survive an infinite
271
    data stream"""
272

    
273
    # touching the serial timeout to optimize the flushing
274
    # restored at the end to the default value
275
    radio.pipe.timeout = 0.1
276
    dump = "1"
277
    datacount = 0
278

    
279
    try:
280
        while len(dump) > 0:
281
            dump = radio.pipe.read(100)
282
            datacount += len(dump)
283
            # hard limit to survive a infinite serial data stream
284
            # 5 times bigger than a normal rx block (69 bytes)
285
            if datacount > 345:
286
                seriale = "Please check your serial port selection."
287
                raise errors.RadioError(seriale)
288

    
289
        # restore the default serial timeout
290
        radio.pipe.timeout = STIMEOUT
291

    
292
    except Exception:
293
        raise errors.RadioError("Unknown error cleaning the serial buffer")
294

    
295

    
296
def _rawrecv(radio, amount):
297
    """Raw read from the radio device, less intensive way"""
298

    
299
    data = ""
300

    
301
    try:
302
        data = radio.pipe.read(amount)
303

    
304
        # DEBUG
305
        if debug is True:
306
            LOG.debug("<== (%d) bytes:\n\n%s" %
307
                      (len(data), util.hexprint(data)))
308

    
309
        # fail if no data is received
310
        if len(data) == 0:
311
            raise errors.RadioError("No data received from radio")
312

    
313
        # notice on the logs if short
314
        if len(data) < amount:
315
            LOG.warn("Short reading %d bytes from the %d requested." %
316
                     (len(data), amount))
317

    
318
    except:
319
        raise errors.RadioError("Error reading data from radio")
320

    
321
    return data
322

    
323

    
324
def _send(radio, data):
325
    """Send data to the radio device"""
326

    
327
    try:
328
        for byte in data:
329
            radio.pipe.write(byte)
330
            # Some OS (mainly Linux ones) are too fast on the serial and
331
            # get the MCU inside the radio stuck in the early stages, this
332
            # hits some models more than others.
333
            #
334
            # To cope with that we introduce a delay on the writes.
335
            # Many option have been tested (delaying only after error occures,
336
            # after short reads, only for linux, ...)
337
            # Finally, a static delay was chosen as simplest of all solutions
338
            # (Michael Wagner, OE4AMW)
339
            # (for details, see issue 3993)
340
            sleep(0.002)
341

    
342
        # DEBUG
343
        if debug is True:
344
            LOG.debug("==> (%d) bytes:\n\n%s" %
345
                      (len(data), util.hexprint(data)))
346
    except:
347
        raise errors.RadioError("Error sending data to radio")
348

    
349

    
350
def _make_frame(cmd, addr, length, data=""):
351
    """Pack the info in the headder format"""
352
    frame = "\x06" + struct.pack(">BHB", ord(cmd), addr, length)
353
    # add the data if set
354
    if len(data) != 0:
355
        frame += data
356

    
357
    return frame
358

    
359

    
360
def _recv(radio, addr):
361
    """Get data from the radio all at once to lower syscalls load"""
362

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

    
366
    # get the whole block
367
    block = _rawrecv(radio, BLOCK_SIZE + 5)
368

    
369
    # basic check
370
    if len(block) < (BLOCK_SIZE + 5):
371
        raise errors.RadioError("Short read of the block 0x%04x" % addr)
372

    
373
    # checking for the ack
374
    if block[0] != ACK_CMD:
375
        raise errors.RadioError("Bad ack from radio in block 0x%04x" % addr)
376

    
377
    # header validation
378
    c, a, l = struct.unpack(">BHB", block[1:5])
379
    if a != addr or l != BLOCK_SIZE or c != ord("X"):
380
        LOG.debug("Invalid header for block 0x%04x" % addr)
381
        LOG.debug("CMD: %s  ADDR: %04x  SIZE: %02x" % (c, a, l))
382
        raise errors.RadioError("Invalid header for block 0x%04x:" % addr)
383

    
384
    # return the data
385
    return block[5:]
386

    
387

    
388
def _start_clone_mode(radio, status):
389
    """Put the radio in clone mode and get the ident string, 3 tries"""
390

    
391
    # cleaning the serial buffer
392
    _clean_buffer(radio)
393

    
394
    # prep the data to show in the UI
395
    status.cur = 0
396
    status.msg = "Identifying the radio..."
397
    status.max = 3
398
    radio.status_fn(status)
399

    
400
    try:
401
        for a in range(0, status.max):
402
            # Update the UI
403
            status.cur = a + 1
404
            radio.status_fn(status)
405

    
406
            # send the magic word
407
            _send(radio, radio._magic)
408

    
409
            # Now you get a x06 of ACK if all goes well
410
            ack = radio.pipe.read(1)
411

    
412
            if ack == "\x06":
413
                # DEBUG
414
                LOG.info("Magic ACK received")
415
                status.cur = status.max
416
                radio.status_fn(status)
417

    
418
                return True
419

    
420
        return False
421

    
422
    except errors.RadioError:
423
        raise
424
    except Exception, e:
425
        raise errors.RadioError("Error sending Magic to radio:\n%s" % e)
426

    
427

    
428
def _do_ident(radio, status, upload=False):
429
    """Put the radio in PROGRAM mode & identify it"""
430
    #  set the serial discipline
431
    radio.pipe.baudrate = 9600
432
    radio.pipe.parity = "N"
433

    
434
    # open the radio into program mode
435
    if _start_clone_mode(radio, status) is False:
436
        msg = "Radio did not enter clone mode"
437
        # warning about old versions of QYT KT8900
438
        if radio.MODEL == "KT8900":
439
            msg += ". You may want to try it as a WACCOM MINI-8900, there is a"
440
            msg += " known variant of this radios that is a clone of it."
441
        raise errors.RadioError(msg)
442

    
443
    # Ok, get the ident string
444
    ident = _rawrecv(radio, 49)
445

    
446
    # basic check for the ident
447
    if len(ident) != 49:
448
        raise errors.RadioError("Radio send a short ident block.")
449

    
450
    # check if ident is OK
451
    itis = False
452
    for fp in radio._fileid:
453
        if fp in ident:
454
            # got it!
455
            itis = True
456
            # checking if we are dealing with a Gen 3 BTECH
457
            if radio.VENDOR == "BTECH" and fp in BTECH3:
458
                radio.btech3 = True
459

    
460
            break
461

    
462
    if itis is False:
463
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
464
        raise errors.RadioError("Radio identification failed.")
465

    
466
    # some radios needs a extra read and check for a code on it, this ones
467
    # has the check value in the _id2 var, others simply False
468
    if radio._id2 is not False:
469
        # lower the timeout here as this radios are reseting due to timeout
470
        radio.pipe.timeout = 0.05
471

    
472
        # query & receive the extra ID
473
        _send(radio, _make_frame("S", 0x3DF0, 16))
474
        id2 = _rawrecv(radio, 21)
475

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

    
483
        # ok, the correct string must be in the received data
484
        # the radio._id2 var will be always a list
485
        flag2 = False
486
        for _id2 in radio._id2:
487
            if _id2 in id2:
488
                flag2 = True
489

    
490
        if not flag2:
491
            LOG.debug("Full *BAD* extra ID on the %s is: \n%s" %
492
                      (radio.MODEL, util.hexprint(id2)))
493
            raise errors.RadioError("The extra ID is wrong, aborting.")
494

    
495
        # this radios need a extra request/answer here on the upload
496
        # the amount of data received depends of the radio type
497
        #
498
        # also the first block of TX must no have the ACK at the beginning
499
        # see _upload for this.
500
        if upload is True:
501
            # send an ACK
502
            _send(radio, ACK_CMD)
503

    
504
            # the amount of data depend on the radio, so far we have two radios
505
            # reading two bytes with an ACK at the end and just ONE with just
506
            # one byte (QYT KT8900)
507
            # the JT-6188 appears a clone of the last, but reads TWO bytes.
508
            #
509
            # we will read two bytes with a custom timeout to not penalize the
510
            # users for this.
511
            #
512
            # we just check for a response and last byte being a ACK, that is
513
            # the common stone for all radios (3 so far)
514
            ack = _rawrecv(radio, 2)
515

    
516
            # checking
517
            if len(ack) == 0 or ack[-1:] != ACK_CMD:
518
                raise errors.RadioError("Radio didn't ACK the upload")
519

    
520
            # restore the default serial timeout
521
            radio.pipe.timeout = STIMEOUT
522

    
523
    # DEBUG
524
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
525

    
526
    return True
527

    
528

    
529
def _download(radio):
530
    """Get the memory map"""
531

    
532
    # UI progress
533
    status = chirp_common.Status()
534

    
535
    # put radio in program mode and identify it
536
    _do_ident(radio, status)
537

    
538
    # the models that doesn't have the extra ID have to make a dummy read here
539
    if radio._id2 is False:
540
        _send(radio, _make_frame("S", 0, BLOCK_SIZE))
541
        discard = _rawrecv(radio, BLOCK_SIZE + 5)
542

    
543
        if debug is True:
544
            LOG.info("Dummy first block read done, got this:\n\n %s",
545
                     util.hexprint(discard))
546

    
547
    # reset the progress bar in the UI
548
    status.max = MEM_SIZE / BLOCK_SIZE
549
    status.msg = "Cloning from radio..."
550
    status.cur = 0
551
    radio.status_fn(status)
552

    
553
    # cleaning the serial buffer
554
    _clean_buffer(radio)
555

    
556
    data = ""
557
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
558
        # sending the read request
559
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
560

    
561
        # read
562
        d = _recv(radio, addr)
563

    
564
        # aggregate the data
565
        data += d
566

    
567
        # UI Update
568
        status.cur = addr / BLOCK_SIZE
569
        status.msg = "Cloning from radio..."
570
        radio.status_fn(status)
571

    
572
    return data
573

    
574

    
575
def _upload(radio):
576
    """Upload procedure"""
577

    
578
    # The UPLOAD mem is restricted to lower than 0x3100,
579
    # so we will overide that here localy
580
    MEM_SIZE = radio.UPLOAD_MEM_SIZE
581

    
582
    # UI progress
583
    status = chirp_common.Status()
584

    
585
    # put radio in program mode and identify it
586
    _do_ident(radio, status, True)
587

    
588
    # get the data to upload to radio
589
    data = radio.get_mmap()
590

    
591
    # Reset the UI progress
592
    status.max = MEM_SIZE / TX_BLOCK_SIZE
593
    status.cur = 0
594
    status.msg = "Cloning to radio..."
595
    radio.status_fn(status)
596

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

    
601
    # cleaning the serial buffer
602
    _clean_buffer(radio)
603

    
604
    # the fun start here
605
    for addr in range(0, MEM_SIZE, TX_BLOCK_SIZE):
606
        # getting the block of data to send
607
        d = data[addr:addr + TX_BLOCK_SIZE]
608

    
609
        # build the frame to send
610
        frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
611

    
612
        # first block must not send the ACK at the beginning for the
613
        # ones that has the extra id, since this have to do a extra step
614
        if addr == 0 and radio._id2 is not False:
615
            frame = frame[1:]
616

    
617
        # send the frame
618
        _send(radio, frame)
619

    
620
        # receiving the response
621
        ack = _rawrecv(radio, 1)
622

    
623
        # basic check
624
        if len(ack) != 1:
625
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
626

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

    
630
        # UI Update
631
        status.cur = addr / TX_BLOCK_SIZE
632
        status.msg = "Cloning to radio..."
633
        radio.status_fn(status)
634

    
635

    
636
def model_match(cls, data):
637
    """Match the opened/downloaded image to the correct version"""
638
    rid = data[0x3f70:0x3f76]
639

    
640
    if rid in cls._fileid:
641
        return True
642

    
643
    return False
644

    
645

    
646
def _decode_ranges(low, high):
647
    """Unpack the data in the ranges zones in the memmap and return
648
    a tuple with the integer corresponding to the Mhz it means"""
649
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
650
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
651
    ilow *= 1000000
652
    ihigh *= 1000000
653

    
654
    return (ilow, ihigh)
655

    
656

    
657
def _split(rf, f1, f2):
658
    """Returns False if the two freqs are in the same band (no split)
659
    or True otherwise"""
660

    
661
    # determine if the two freqs are in the same band
662
    for low, high in rf.valid_bands:
663
        if f1 >= low and f1 <= high and \
664
                f2 >= low and f2 <= high:
665
            # if the two freqs are on the same Band this is not a split
666
            return False
667

    
668
    # if you get here is because the freq pairs are split
669
    return True
670

    
671

    
672
class BTechMobileCommon(chirp_common.CloneModeRadio,
673
                        chirp_common.ExperimentalRadio):
674
    """BTECH's UV-5001 and alike radios"""
675
    VENDOR = "BTECH"
676
    MODEL = ""
677
    IDENT = ""
678
    BANDS = 2
679
    COLOR_LCD = False
680
    COLOR_LCD2 = False
681
    NAME_LENGTH = 6
682
    UPLOAD_MEM_SIZE = 0X3100
683
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
684
                     chirp_common.PowerLevel("Low", watts=10)]
685
    _vhf_range = (130000000, 180000000)
686
    _220_range = (200000000, 271000000)
687
    _uhf_range = (400000000, 521000000)
688
    _350_range = (350000000, 391000000)
689
    _upper = 199
690
    _magic = MSTRING
691
    _fileid = None
692
    _id2 = False
693
    btech3 = False
694

    
695
    @classmethod
696
    def get_prompts(cls):
697
        rp = chirp_common.RadioPrompts()
698
        rp.experimental = \
699
            ('This driver is experimental.\n'
700
             '\n'
701
             'Please keep a copy of your memories with the original software '
702
             'if you treasure them, this driver is new and may contain'
703
             ' bugs.\n'
704
             '\n'
705
             )
706
        rp.pre_download = _(dedent("""\
707
            Follow these instructions to download your info:
708

    
709
            1 - Turn off your radio
710
            2 - Connect your interface cable
711
            3 - Turn on your radio
712
            4 - Do the download of your radio data
713

    
714
            """))
715
        rp.pre_upload = _(dedent("""\
716
            Follow these instructions to upload your info:
717

    
718
            1 - Turn off your radio
719
            2 - Connect your interface cable
720
            3 - Turn on your radio
721
            4 - Do the upload of your radio data
722

    
723
            """))
724
        return rp
725

    
726
    def get_features(self):
727
        """Get the radio's features"""
728

    
729
        # we will use the following var as global
730
        global POWER_LEVELS
731

    
732
        rf = chirp_common.RadioFeatures()
733
        rf.has_settings = True
734
        rf.has_bank = False
735
        rf.has_tuning_step = False
736
        rf.can_odd_split = True
737
        rf.has_name = True
738
        rf.has_offset = True
739
        rf.has_mode = True
740
        rf.has_dtcs = True
741
        rf.has_rx_dtcs = True
742
        rf.has_dtcs_polarity = True
743
        rf.has_ctone = True
744
        rf.has_cross = True
745
        rf.valid_modes = MODES
746
        rf.valid_characters = VALID_CHARS
747
        rf.valid_name_length = self.NAME_LENGTH
748
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
749
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
750
        rf.valid_cross_modes = [
751
            "Tone->Tone",
752
            "DTCS->",
753
            "->DTCS",
754
            "Tone->DTCS",
755
            "DTCS->Tone",
756
            "->Tone",
757
            "DTCS->DTCS"]
758
        rf.valid_skips = SKIP_VALUES
759
        rf.valid_dtcs_codes = DTCS
760
        rf.valid_tuning_steps = STEPS
761
        rf.memory_bounds = (0, self._upper)
762

    
763
        # power levels
764
        POWER_LEVELS = self._power_levels
765
        rf.valid_power_levels = POWER_LEVELS
766

    
767
        # normal dual bands
768
        rf.valid_bands = [self._vhf_range, self._uhf_range]
769

    
770
        # 220 band
771
        if self.BANDS == 3 or self.BANDS == 4:
772
            rf.valid_bands.append(self._220_range)
773

    
774
        # 350 band
775
        if self.BANDS == 4:
776
            rf.valid_bands.append(self._350_range)
777

    
778
        return rf
779

    
780
    def sync_in(self):
781
        """Download from radio"""
782
        data = _download(self)
783
        self._mmap = memmap.MemoryMap(data)
784
        self.process_mmap()
785

    
786
    def sync_out(self):
787
        """Upload to radio"""
788
        try:
789
            _upload(self)
790
        except errors.RadioError:
791
            raise
792
        except Exception, e:
793
            raise errors.RadioError("Error: %s" % e)
794

    
795
    def get_raw_memory(self, number):
796
        return repr(self._memobj.memory[number])
797

    
798
    def _decode_tone(self, val):
799
        """Parse the tone data to decode from mem, it returns:
800
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
801
        pol = None
802

    
803
        if val in [0, 65535]:
804
            return '', None, None
805
        elif val > 0x0258:
806
            a = val / 10.0
807
            return 'Tone', a, pol
808
        else:
809
            if val > 0x69:
810
                index = val - 0x6A
811
                pol = "R"
812
            else:
813
                index = val - 1
814
                pol = "N"
815

    
816
            tone = DTCS[index]
817
            return 'DTCS', tone, pol
818

    
819
    def _encode_tone(self, memval, mode, val, pol):
820
        """Parse the tone data to encode from UI to mem"""
821
        if mode == '' or mode is None:
822
            memval.set_raw("\x00\x00")
823
        elif mode == 'Tone':
824
            memval.set_value(val * 10)
825
        elif mode == 'DTCS':
826
            # detect the index in the DTCS list
827
            try:
828
                index = DTCS.index(val)
829
                if pol == "N":
830
                    index += 1
831
                else:
832
                    index += 0x6A
833
                memval.set_value(index)
834
            except:
835
                msg = "Digital Tone '%d' is not supported" % value
836
                LOG.error(msg)
837
                raise errors.RadioError(msg)
838
        else:
839
            msg = "Internal error: invalid mode '%s'" % mode
840
            LOG.error(msg)
841
            raise errors.InvalidDataError(msg)
842

    
843
    def get_memory(self, number):
844
        """Get the mem representation from the radio image"""
845
        _mem = self._memobj.memory[number]
846
        _names = self._memobj.names[number]
847

    
848
        # Create a high-level memory object to return to the UI
849
        mem = chirp_common.Memory()
850

    
851
        # Memory number
852
        mem.number = number
853

    
854
        if _mem.get_raw()[0] == "\xFF":
855
            mem.empty = True
856
            return mem
857

    
858
        # Freq and offset
859
        mem.freq = int(_mem.rxfreq) * 10
860
        # tx freq can be blank
861
        if _mem.get_raw()[4] == "\xFF":
862
            # TX freq not set
863
            mem.offset = 0
864
            mem.duplex = "off"
865
        else:
866
            # TX freq set
867
            offset = (int(_mem.txfreq) * 10) - mem.freq
868
            if offset != 0:
869
                if _split(self.get_features(), mem.freq, int(
870
                          _mem.txfreq) * 10):
871
                    mem.duplex = "split"
872
                    mem.offset = int(_mem.txfreq) * 10
873
                elif offset < 0:
874
                    mem.offset = abs(offset)
875
                    mem.duplex = "-"
876
                elif offset > 0:
877
                    mem.offset = offset
878
                    mem.duplex = "+"
879
            else:
880
                mem.offset = 0
881

    
882
        # name TAG of the channel
883
        mem.name = str(_names.name).rstrip("\xFF").replace("\xFF", " ")
884

    
885
        # power
886
        mem.power = POWER_LEVELS[int(_mem.power)]
887

    
888
        # wide/narrow
889
        mem.mode = MODES[int(_mem.wide)]
890

    
891
        # skip
892
        mem.skip = SKIP_VALUES[_mem.add]
893

    
894
        # tone data
895
        rxtone = txtone = None
896
        txtone = self._decode_tone(_mem.txtone)
897
        rxtone = self._decode_tone(_mem.rxtone)
898
        chirp_common.split_tone_decode(mem, txtone, rxtone)
899

    
900
        # Extra
901
        mem.extra = RadioSettingGroup("extra", "Extra")
902

    
903
        if not self.COLOR_LCD or \
904
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
905
            scramble = RadioSetting("scramble", "Scramble",
906
                                    RadioSettingValueBoolean(bool(
907
                                        _mem.scramble)))
908
            mem.extra.append(scramble)
909

    
910
        bcl = RadioSetting("bcl", "Busy channel lockout",
911
                           RadioSettingValueBoolean(bool(_mem.bcl)))
912
        mem.extra.append(bcl)
913

    
914
        pttid = RadioSetting("pttid", "PTT ID",
915
                             RadioSettingValueList(PTTID_LIST,
916
                                                   PTTID_LIST[_mem.pttid]))
917
        mem.extra.append(pttid)
918

    
919
        # validating scode
920
        scode = _mem.scode if _mem.scode != 15 else 0
921
        pttidcode = RadioSetting("scode", "PTT ID signal code",
922
                                 RadioSettingValueList(
923
                                     PTTIDCODE_LIST,
924
                                     PTTIDCODE_LIST[scode]))
925
        mem.extra.append(pttidcode)
926

    
927
        optsig = RadioSetting("optsig", "Optional signaling",
928
                              RadioSettingValueList(
929
                                  OPTSIG_LIST,
930
                                  OPTSIG_LIST[_mem.optsig]))
931
        mem.extra.append(optsig)
932

    
933
        spmute = RadioSetting("spmute", "Speaker mute",
934
                              RadioSettingValueList(
935
                                  SPMUTE_LIST,
936
                                  SPMUTE_LIST[_mem.spmute]))
937
        mem.extra.append(spmute)
938

    
939
        return mem
940

    
941
    def set_memory(self, mem):
942
        """Set the memory data in the eeprom img from the UI"""
943
        # get the eprom representation of this channel
944
        _mem = self._memobj.memory[mem.number]
945
        _names = self._memobj.names[mem.number]
946

    
947
        mem_was_empty = False
948
        # same method as used in get_memory for determining if mem is empty
949
        # doing this BEFORE overwriting it with new values ...
950
        if _mem.get_raw()[0] == "\xFF":
951
            LOG.debug("This mem was empty before")
952
            mem_was_empty = True
953

    
954
        # if empty memmory
955
        if mem.empty:
956
            # the channel itself
957
            _mem.set_raw("\xFF" * 16)
958
            # the name tag
959
            _names.set_raw("\xFF" * 16)
960
            return
961

    
962
        if mem_was_empty:
963
            # Zero the whole memory if we're making it unempty for
964
            # the first time
965
            LOG.debug('Zeroing new memory')
966
            _mem.set_raw('\x00' * 16)
967

    
968
        # frequency
969
        _mem.rxfreq = mem.freq / 10
970

    
971
        # duplex
972
        if mem.duplex == "+":
973
            _mem.txfreq = (mem.freq + mem.offset) / 10
974
        elif mem.duplex == "-":
975
            _mem.txfreq = (mem.freq - mem.offset) / 10
976
        elif mem.duplex == "off":
977
            for i in _mem.txfreq:
978
                i.set_raw("\xFF")
979
        elif mem.duplex == "split":
980
            _mem.txfreq = mem.offset / 10
981
        else:
982
            _mem.txfreq = mem.freq / 10
983

    
984
        # tone data
985
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
986
            chirp_common.split_tone_encode(mem)
987
        self._encode_tone(_mem.txtone, txmode, txtone, txpol)
988
        self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)
989

    
990
        # name TAG of the channel
991
        if len(mem.name) < self.NAME_LENGTH:
992
            # we must pad to self.NAME_LENGTH chars, " " = "\xFF"
993
            mem.name = str(mem.name).ljust(self.NAME_LENGTH, " ")
994
        _names.name = str(mem.name).replace(" ", "\xFF")
995

    
996
        # power, # default power level is high
997
        _mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)
998

    
999
        # wide/narrow
1000
        _mem.wide = MODES.index(mem.mode)
1001

    
1002
        # scan add property
1003
        _mem.add = SKIP_VALUES.index(mem.skip)
1004

    
1005
        # reseting unknowns, this have to be set by hand
1006
        _mem.unknown0 = 0
1007
        _mem.unknown1 = 0
1008
        _mem.unknown2 = 0
1009
        _mem.unknown3 = 0
1010
        _mem.unknown4 = 0
1011
        _mem.unknown5 = 0
1012
        _mem.unknown6 = 0
1013

    
1014
        def _zero_settings():
1015
            _mem.spmute = 0
1016
            _mem.optsig = 0
1017
            _mem.scramble = 0
1018
            _mem.bcl = 0
1019
            _mem.pttid = 0
1020
            _mem.scode = 0
1021

    
1022
        if self.COLOR_LCD and _mem.scramble:
1023
            LOG.info('Resetting scramble bit for BTECH COLOR_LCD variant')
1024
            _mem.scramble = 0
1025

    
1026
        # extra settings
1027
        if len(mem.extra) > 0:
1028
            # there are setting, parse
1029
            LOG.debug("Extra-Setting supplied. Setting them.")
1030
            # Zero them all first so any not provided by model don't
1031
            # stay set
1032
            _zero_settings()
1033
            for setting in mem.extra:
1034
                setattr(_mem, setting.get_name(), setting.value)
1035
        else:
1036
            if mem.empty:
1037
                LOG.debug("New mem is empty.")
1038
            else:
1039
                LOG.debug("New mem is NOT empty")
1040
                # set extra-settings to default ONLY when apreviously empty or
1041
                # deleted memory was edited to prevent errors such as #4121
1042
                if mem_was_empty:
1043
                    LOG.debug("old mem was empty. Setting default for extras.")
1044
                    _zero_settings()
1045

    
1046
        return mem
1047

    
1048
    def get_settings(self):
1049
        """Translate the bit in the mem_struct into settings in the UI"""
1050
        _mem = self._memobj
1051
        basic = RadioSettingGroup("basic", "Basic Settings")
1052
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
1053
        other = RadioSettingGroup("other", "Other Settings")
1054
        work = RadioSettingGroup("work", "Work Mode Settings")
1055
        top = RadioSettings(basic, advanced, other, work)
1056

    
1057
        # Basic
1058
        if self.COLOR_LCD:
1059
            tmr = RadioSetting("settings.tmr", "Transceiver multi-receive",
1060
                               RadioSettingValueList(
1061
                                   self.LIST_TMR,
1062
                                   self.LIST_TMR[_mem.settings.tmr]))
1063
            basic.append(tmr)
1064
        else:
1065
            tdr = RadioSetting("settings.tdr", "Transceiver dual receive",
1066
                               RadioSettingValueBoolean(_mem.settings.tdr))
1067
            basic.append(tdr)
1068

    
1069
        sql = RadioSetting("settings.sql", "Squelch level",
1070
                           RadioSettingValueInteger(0, 9, _mem.settings.sql))
1071
        basic.append(sql)
1072

    
1073
        if self.MODEL == "GMRS-50X1":
1074
            autolk = RadioSetting("settings.autolk", "Auto keylock",
1075
                                  RadioSettingValueBoolean(
1076
                                      _mem.settings.autolk))
1077
            basic.append(autolk)
1078

    
1079
        tot = RadioSetting("settings.tot", "Time out timer",
1080
                           RadioSettingValueList(
1081
                               LIST_TOT,
1082
                               LIST_TOT[_mem.settings.tot]))
1083
        basic.append(tot)
1084

    
1085
        if self.VENDOR == "BTECH" or self.COLOR_LCD:
1086
            apo = RadioSetting("settings.apo", "Auto power off timer",
1087
                               RadioSettingValueList(
1088
                                   LIST_APO,
1089
                                   LIST_APO[_mem.settings.apo]))
1090
            basic.append(apo)
1091
        else:
1092
            toa = RadioSetting("settings.apo", "Time out alert timer",
1093
                               RadioSettingValueList(
1094
                                   LIST_OFF1TO10,
1095
                                   LIST_OFF1TO10[_mem.settings.apo]))
1096
            basic.append(toa)
1097

    
1098
        abr = RadioSetting("settings.abr", "Backlight timer",
1099
                           RadioSettingValueList(
1100
                               LIST_OFF1TO50,
1101
                               LIST_OFF1TO50[_mem.settings.abr]))
1102
        basic.append(abr)
1103

    
1104
        beep = RadioSetting("settings.beep", "Key beep",
1105
                            RadioSettingValueBoolean(_mem.settings.beep))
1106
        basic.append(beep)
1107

    
1108
        dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1109
                              RadioSettingValueList(
1110
                                  LIST_DTMFST,
1111
                                  LIST_DTMFST[_mem.settings.dtmfst]))
1112
        basic.append(dtmfst)
1113

    
1114
        if not self.COLOR_LCD:
1115
            prisc = RadioSetting("settings.prisc", "Priority scan",
1116
                                 RadioSettingValueBoolean(
1117
                                     _mem.settings.prisc))
1118
            basic.append(prisc)
1119

    
1120
            prich = RadioSetting("settings.prich", "Priority channel",
1121
                                 RadioSettingValueInteger(0, self._upper,
1122
                                                          _mem.settings.prich))
1123
            basic.append(prich)
1124

    
1125
        screv = RadioSetting("settings.screv", "Scan resume method",
1126
                             RadioSettingValueList(
1127
                                 LIST_SCREV,
1128
                                 LIST_SCREV[_mem.settings.screv]))
1129
        basic.append(screv)
1130

    
1131
        pttlt = RadioSetting("settings.pttlt", "PTT transmit delay",
1132
                             RadioSettingValueInteger(0, 30,
1133
                                                      _mem.settings.pttlt))
1134
        basic.append(pttlt)
1135

    
1136
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1137
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1138
                                 RadioSettingValueList(
1139
                                     LIST_EMCTPX,
1140
                                     LIST_EMCTPX[_mem.settings.emctp]))
1141
            basic.append(emctp)
1142
        else:
1143
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1144
                                 RadioSettingValueList(
1145
                                     LIST_EMCTP,
1146
                                     LIST_EMCTP[_mem.settings.emctp]))
1147
            basic.append(emctp)
1148

    
1149
        emcch = RadioSetting("settings.emcch", "Alarm channel",
1150
                             RadioSettingValueInteger(0, self._upper,
1151
                                                      _mem.settings.emcch))
1152
        basic.append(emcch)
1153

    
1154
        if self.COLOR_LCD:
1155
            if _mem.settings.sigbp > 0x01:
1156
                val = 0x00
1157
            else:
1158
                val = _mem.settings.sigbp
1159
            sigbp = RadioSetting("settings.sigbp", "Signal beep",
1160
                                 RadioSettingValueBoolean(val))
1161
            basic.append(sigbp)
1162
        else:
1163
            ringt = RadioSetting("settings.ringt", "Ring time",
1164
                                 RadioSettingValueList(
1165
                                     LIST_OFF1TO9,
1166
                                     LIST_OFF1TO9[_mem.settings.ringt]))
1167
            basic.append(ringt)
1168

    
1169
        camdf = RadioSetting("settings.camdf", "Display mode A",
1170
                             RadioSettingValueList(
1171
                                 LIST_MDF,
1172
                                 LIST_MDF[_mem.settings.camdf]))
1173
        basic.append(camdf)
1174

    
1175
        cbmdf = RadioSetting("settings.cbmdf", "Display mode B",
1176
                             RadioSettingValueList(
1177
                                 LIST_MDF,
1178
                                 LIST_MDF[_mem.settings.cbmdf]))
1179
        basic.append(cbmdf)
1180

    
1181
        if self.COLOR_LCD:
1182
            ccmdf = RadioSetting("settings.ccmdf", "Display mode C",
1183
                                 RadioSettingValueList(
1184
                                     LIST_MDF,
1185
                                     LIST_MDF[_mem.settings.ccmdf]))
1186
            basic.append(ccmdf)
1187

    
1188
            cdmdf = RadioSetting("settings.cdmdf", "Display mode D",
1189
                                 RadioSettingValueList(
1190
                                     LIST_MDF,
1191
                                     LIST_MDF[_mem.settings.cdmdf]))
1192
            basic.append(cdmdf)
1193

    
1194
            langua = RadioSetting("settings.langua", "Language",
1195
                                  RadioSettingValueList(
1196
                                      LIST_LANGUA,
1197
                                      LIST_LANGUA[_mem.settings.langua]))
1198
            basic.append(langua)
1199

    
1200
        if self.VENDOR == "BTECH":
1201
            if self.COLOR_LCD:
1202
                sync = RadioSetting("settings.sync", "Channel display sync",
1203
                                    RadioSettingValueList(
1204
                                        LIST_SYNC,
1205
                                        LIST_SYNC[_mem.settings.sync]))
1206
                basic.append(sync)
1207
            else:
1208
                sync = RadioSetting("settings.sync", "A/B channel sync",
1209
                                    RadioSettingValueBoolean(
1210
                                        _mem.settings.sync))
1211
                basic.append(sync)
1212
        else:
1213
            autolk = RadioSetting("settings.sync", "Auto keylock",
1214
                                  RadioSettingValueBoolean(
1215
                                      _mem.settings.sync))
1216
            basic.append(autolk)
1217

    
1218
        if not self.COLOR_LCD:
1219
            ponmsg = RadioSetting("settings.ponmsg", "Power-on message",
1220
                                  RadioSettingValueList(
1221
                                      LIST_PONMSG,
1222
                                      LIST_PONMSG[_mem.settings.ponmsg]))
1223
            basic.append(ponmsg)
1224

    
1225
        if self.COLOR_LCD and not self.COLOR_LCD2:
1226
            mainfc = RadioSetting("settings.mainfc",
1227
                                  "Main LCD foreground color",
1228
                                  RadioSettingValueList(
1229
                                      LIST_COLOR8,
1230
                                      LIST_COLOR8[_mem.settings.mainfc]))
1231
            basic.append(mainfc)
1232

    
1233
            mainbc = RadioSetting("settings.mainbc",
1234
                                  "Main LCD background color",
1235
                                  RadioSettingValueList(
1236
                                      LIST_COLOR8,
1237
                                      LIST_COLOR8[_mem.settings.mainbc]))
1238
            basic.append(mainbc)
1239

    
1240
            menufc = RadioSetting("settings.menufc", "Menu foreground color",
1241
                                  RadioSettingValueList(
1242
                                      LIST_COLOR8,
1243
                                      LIST_COLOR8[_mem.settings.menufc]))
1244
            basic.append(menufc)
1245

    
1246
            menubc = RadioSetting("settings.menubc", "Menu background color",
1247
                                  RadioSettingValueList(
1248
                                      LIST_COLOR8,
1249
                                      LIST_COLOR8[_mem.settings.menubc]))
1250
            basic.append(menubc)
1251

    
1252
            stafc = RadioSetting("settings.stafc",
1253
                                 "Top status foreground color",
1254
                                 RadioSettingValueList(
1255
                                     LIST_COLOR8,
1256
                                     LIST_COLOR8[_mem.settings.stafc]))
1257
            basic.append(stafc)
1258

    
1259
            stabc = RadioSetting("settings.stabc",
1260
                                 "Top status background color",
1261
                                 RadioSettingValueList(
1262
                                     LIST_COLOR8,
1263
                                     LIST_COLOR8[_mem.settings.stabc]))
1264
            basic.append(stabc)
1265

    
1266
            sigfc = RadioSetting("settings.sigfc",
1267
                                 "Bottom status foreground color",
1268
                                 RadioSettingValueList(
1269
                                     LIST_COLOR8,
1270
                                     LIST_COLOR8[_mem.settings.sigfc]))
1271
            basic.append(sigfc)
1272

    
1273
            sigbc = RadioSetting("settings.sigbc",
1274
                                 "Bottom status background color",
1275
                                 RadioSettingValueList(
1276
                                     LIST_COLOR8,
1277
                                     LIST_COLOR8[_mem.settings.sigbc]))
1278
            basic.append(sigbc)
1279

    
1280
            rxfc = RadioSetting("settings.rxfc", "Receiving character color",
1281
                                RadioSettingValueList(
1282
                                    LIST_COLOR8,
1283
                                    LIST_COLOR8[_mem.settings.rxfc]))
1284
            basic.append(rxfc)
1285

    
1286
            txfc = RadioSetting("settings.txfc",
1287
                                "Transmitting character color",
1288
                                RadioSettingValueList(
1289
                                    LIST_COLOR8,
1290
                                    LIST_COLOR8[_mem.settings.txfc]))
1291
            basic.append(txfc)
1292

    
1293
            txdisp = RadioSetting("settings.txdisp",
1294
                                  "Transmitting status display",
1295
                                  RadioSettingValueList(
1296
                                      LIST_TXDISP,
1297
                                      LIST_TXDISP[_mem.settings.txdisp]))
1298
            basic.append(txdisp)
1299
        elif self.COLOR_LCD2:
1300
            stfc = RadioSetting("settings.stfc",
1301
                                "ST-FC",
1302
                                RadioSettingValueList(
1303
                                    LIST_COLOR7,
1304
                                    LIST_COLOR7[_mem.settings.stfc]))
1305
            basic.append(stfc)
1306

    
1307
            mffc = RadioSetting("settings.mffc",
1308
                                "MF-FC",
1309
                                RadioSettingValueList(
1310
                                    LIST_COLOR7,
1311
                                    LIST_COLOR7[_mem.settings.mffc]))
1312
            basic.append(mffc)
1313

    
1314
            sfafc = RadioSetting("settings.sfafc",
1315
                                 "SFA-FC",
1316
                                 RadioSettingValueList(
1317
                                     LIST_COLOR7,
1318
                                     LIST_COLOR7[_mem.settings.sfafc]))
1319
            basic.append(sfafc)
1320

    
1321
            sfbfc = RadioSetting("settings.sfbfc",
1322
                                 "SFB-FC",
1323
                                 RadioSettingValueList(
1324
                                     LIST_COLOR7,
1325
                                     LIST_COLOR7[_mem.settings.sfbfc]))
1326
            basic.append(sfbfc)
1327

    
1328
            sfcfc = RadioSetting("settings.sfcfc",
1329
                                 "SFC-FC",
1330
                                 RadioSettingValueList(
1331
                                     LIST_COLOR7,
1332
                                     LIST_COLOR7[_mem.settings.sfcfc]))
1333
            basic.append(sfcfc)
1334

    
1335
            sfdfc = RadioSetting("settings.sfdfc",
1336
                                 "SFD-FC",
1337
                                 RadioSettingValueList(
1338
                                     LIST_COLOR7,
1339
                                     LIST_COLOR7[_mem.settings.sfdfc]))
1340
            basic.append(sfdfc)
1341

    
1342
            subfc = RadioSetting("settings.subfc",
1343
                                 "SUB-FC",
1344
                                 RadioSettingValueList(
1345
                                     LIST_COLOR7,
1346
                                     LIST_COLOR7[_mem.settings.subfc]))
1347
            basic.append(subfc)
1348

    
1349
            fmfc = RadioSetting("settings.fmfc",
1350
                                "FM-FC",
1351
                                RadioSettingValueList(
1352
                                    LIST_COLOR7,
1353
                                    LIST_COLOR7[_mem.settings.fmfc]))
1354
            basic.append(fmfc)
1355

    
1356
            sigfc = RadioSetting("settings.sigfc",
1357
                                 "SIG-FC",
1358
                                 RadioSettingValueList(
1359
                                     LIST_COLOR7,
1360
                                     LIST_COLOR7[_mem.settings.sigfc]))
1361
            basic.append(sigfc)
1362

    
1363
            modfc = RadioSetting("settings.modfc",
1364
                                 "MOD-FC",
1365
                                 RadioSettingValueList(
1366
                                     LIST_COLOR7,
1367
                                     LIST_COLOR7[_mem.settings.modfc]))
1368
            basic.append(modfc)
1369

    
1370
            menufc = RadioSetting("settings.menufc",
1371
                                  "MENUFC",
1372
                                  RadioSettingValueList(
1373
                                      LIST_COLOR7,
1374
                                      LIST_COLOR7[_mem.settings.menufc]))
1375
            basic.append(menufc)
1376

    
1377
            txfc = RadioSetting("settings.txfc",
1378
                                "TX-FC",
1379
                                RadioSettingValueList(
1380
                                    LIST_COLOR7,
1381
                                    LIST_COLOR7[_mem.settings.txfc]))
1382
            basic.append(txfc)
1383

    
1384
            txdisp = RadioSetting("settings.txdisp",
1385
                                  "Transmitting status display",
1386
                                  RadioSettingValueList(
1387
                                      LIST_TXDISP,
1388
                                      LIST_TXDISP[_mem.settings.txdisp]))
1389
            basic.append(txdisp)
1390
        else:
1391
            wtled = RadioSetting("settings.wtled", "Standby backlight Color",
1392
                                 RadioSettingValueList(
1393
                                     LIST_COLOR4,
1394
                                     LIST_COLOR4[_mem.settings.wtled]))
1395
            basic.append(wtled)
1396

    
1397
            rxled = RadioSetting("settings.rxled", "RX backlight Color",
1398
                                 RadioSettingValueList(
1399
                                     LIST_COLOR4,
1400
                                     LIST_COLOR4[_mem.settings.rxled]))
1401
            basic.append(rxled)
1402

    
1403
            txled = RadioSetting("settings.txled", "TX backlight Color",
1404
                                 RadioSettingValueList(
1405
                                     LIST_COLOR4,
1406
                                     LIST_COLOR4[_mem.settings.txled]))
1407
            basic.append(txled)
1408

    
1409
        anil = RadioSetting("settings.anil", "ANI length",
1410
                            RadioSettingValueList(
1411
                                LIST_ANIL,
1412
                                LIST_ANIL[_mem.settings.anil]))
1413
        basic.append(anil)
1414

    
1415
        reps = RadioSetting("settings.reps", "Relay signal (tone burst)",
1416
                            RadioSettingValueList(
1417
                                LIST_REPS,
1418
                                LIST_REPS[_mem.settings.reps]))
1419
        basic.append(reps)
1420

    
1421
        if not self.MODEL == "GMRS-50X1":
1422
            repm = RadioSetting("settings.repm", "Relay condition",
1423
                                RadioSettingValueList(
1424
                                    LIST_REPM,
1425
                                    LIST_REPM[_mem.settings.repm]))
1426
            basic.append(repm)
1427

    
1428
        if self.VENDOR == "BTECH" or self.COLOR_LCD:
1429
            if self.COLOR_LCD:
1430
                tmrmr = RadioSetting("settings.tmrmr", "TMR return time",
1431
                                     RadioSettingValueList(
1432
                                         LIST_OFF1TO50,
1433
                                         LIST_OFF1TO50[_mem.settings.tmrmr]))
1434
                basic.append(tmrmr)
1435
            else:
1436
                tdrab = RadioSetting("settings.tdrab", "TDR return time",
1437
                                     RadioSettingValueList(
1438
                                         LIST_OFF1TO50,
1439
                                         LIST_OFF1TO50[_mem.settings.tdrab]))
1440
                basic.append(tdrab)
1441

    
1442
            ste = RadioSetting("settings.ste", "Squelch tail eliminate",
1443
                               RadioSettingValueBoolean(_mem.settings.ste))
1444
            basic.append(ste)
1445

    
1446
            rpste = RadioSetting("settings.rpste", "Repeater STE",
1447
                                 RadioSettingValueList(
1448
                                     LIST_OFF1TO9,
1449
                                     LIST_OFF1TO9[_mem.settings.rpste]))
1450
            basic.append(rpste)
1451

    
1452
            rptdl = RadioSetting("settings.rptdl", "Repeater STE delay",
1453
                                 RadioSettingValueList(
1454
                                     LIST_RPTDL,
1455
                                     LIST_RPTDL[_mem.settings.rptdl]))
1456
            basic.append(rptdl)
1457

    
1458
        if str(_mem.fingerprint.fp) in BTECH3:
1459
            mgain = RadioSetting("settings.mgain", "Mic gain",
1460
                                 RadioSettingValueInteger(0, 120,
1461
                                                          _mem.settings.mgain))
1462
            basic.append(mgain)
1463

    
1464
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1465
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain",
1466
                                 RadioSettingValueInteger(0, 60,
1467
                                                          _mem.settings.dtmfg))
1468
            basic.append(dtmfg)
1469

    
1470
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1471
            mgain = RadioSetting("settings.mgain", "Mic gain",
1472
                                 RadioSettingValueInteger(0, 120,
1473
                                                          _mem.settings.mgain))
1474
            basic.append(mgain)
1475

    
1476
            skiptx = RadioSetting("settings.skiptx", "Skip TX",
1477
                                  RadioSettingValueList(
1478
                                      LIST_SKIPTX,
1479
                                      LIST_SKIPTX[_mem.settings.skiptx]))
1480
            basic.append(skiptx)
1481

    
1482
            scmode = RadioSetting("settings.scmode", "Scan mode",
1483
                                  RadioSettingValueList(
1484
                                      LIST_SCMODE,
1485
                                      LIST_SCMODE[_mem.settings.scmode]))
1486
            basic.append(scmode)
1487

    
1488
        # Advanced
1489
        def _filter(name):
1490
            filtered = ""
1491
            for char in str(name):
1492
                if char in VALID_CHARS:
1493
                    filtered += char
1494
                else:
1495
                    filtered += " "
1496
            return filtered
1497

    
1498
        if self.COLOR_LCD and not self.COLOR_LCD2:
1499
            _msg = self._memobj.poweron_msg
1500
            line1 = RadioSetting("poweron_msg.line1",
1501
                                 "Power-on message line 1",
1502
                                 RadioSettingValueString(0, 8, _filter(
1503
                                                         _msg.line1)))
1504
            advanced.append(line1)
1505
            line2 = RadioSetting("poweron_msg.line2",
1506
                                 "Power-on message line 2",
1507
                                 RadioSettingValueString(0, 8, _filter(
1508
                                                         _msg.line2)))
1509
            advanced.append(line2)
1510
            line3 = RadioSetting("poweron_msg.line3",
1511
                                 "Power-on message line 3",
1512
                                 RadioSettingValueString(0, 8, _filter(
1513
                                                         _msg.line3)))
1514
            advanced.append(line3)
1515
            line4 = RadioSetting("poweron_msg.line4",
1516
                                 "Power-on message line 4",
1517
                                 RadioSettingValueString(0, 8, _filter(
1518
                                                         _msg.line4)))
1519
            advanced.append(line4)
1520
            line5 = RadioSetting("poweron_msg.line5",
1521
                                 "Power-on message line 5",
1522
                                 RadioSettingValueString(0, 8, _filter(
1523
                                                         _msg.line5)))
1524
            advanced.append(line5)
1525
            line6 = RadioSetting("poweron_msg.line6",
1526
                                 "Power-on message line 6",
1527
                                 RadioSettingValueString(0, 8, _filter(
1528
                                                         _msg.line6)))
1529
            advanced.append(line6)
1530
            line7 = RadioSetting("poweron_msg.line7",
1531
                                 "Power-on message line 7",
1532
                                 RadioSettingValueString(0, 8, _filter(
1533
                                                         _msg.line7)))
1534
            advanced.append(line7)
1535
            line8 = RadioSetting("poweron_msg.line8", "Static message",
1536
                                 RadioSettingValueString(0, 8, _filter(
1537
                                                         _msg.line8)))
1538
            advanced.append(line8)
1539
        elif self.COLOR_LCD2:
1540
            _msg = self._memobj.static_msg
1541
            line = RadioSetting("static_msg.line", "Static message",
1542
                                RadioSettingValueString(0, 16, _filter(
1543
                                    _msg.line)))
1544
            advanced.append(line)
1545
        else:
1546
            _msg = self._memobj.poweron_msg
1547
            line1 = RadioSetting("poweron_msg.line1",
1548
                                 "Power-on message line 1",
1549
                                 RadioSettingValueString(0, 6, _filter(
1550
                                                         _msg.line1)))
1551
            advanced.append(line1)
1552
            line2 = RadioSetting("poweron_msg.line2",
1553
                                 "Power-on message line 2",
1554
                                 RadioSettingValueString(0, 6, _filter(
1555
                                                         _msg.line2)))
1556
            advanced.append(line2)
1557

    
1558
        if self.MODEL in ("UV-2501", "UV-5001"):
1559
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1560
                                   RadioSettingValueBoolean(
1561
                                       _mem.settings2.vfomren))
1562
            advanced.append(vfomren)
1563

    
1564
            reseten = RadioSetting("settings2.reseten", "RESET",
1565
                                   RadioSettingValueBoolean(
1566
                                       _mem.settings2.reseten))
1567
            advanced.append(reseten)
1568

    
1569
            menuen = RadioSetting("settings2.menuen", "Menu",
1570
                                  RadioSettingValueBoolean(
1571
                                      _mem.settings2.menuen))
1572
            advanced.append(menuen)
1573

    
1574
        # Other
1575
        def convert_bytes_to_limit(bytes):
1576
            limit = ""
1577
            for byte in bytes:
1578
                if byte < 10:
1579
                    limit += chr(byte + 0x30)
1580
                else:
1581
                    break
1582
            return limit
1583

    
1584
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1585
            _ranges = self._memobj.ranges220
1586
            ranges = "ranges220"
1587
        else:
1588
            _ranges = self._memobj.ranges
1589
            ranges = "ranges"
1590

    
1591
        _limit = convert_bytes_to_limit(_ranges.vhf_low)
1592
        val = RadioSettingValueString(0, 3, _limit)
1593
        val.set_mutable(False)
1594
        vhf_low = RadioSetting("%s.vhf_low" % ranges, "VHF low", val)
1595
        other.append(vhf_low)
1596

    
1597
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1598
        val = RadioSettingValueString(0, 3, _limit)
1599
        val.set_mutable(False)
1600
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1601
        other.append(vhf_high)
1602

    
1603
        if self.BANDS == 3 or self.BANDS == 4:
1604
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1605
            val = RadioSettingValueString(0, 3, _limit)
1606
            val.set_mutable(False)
1607
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1608
            other.append(vhf2_low)
1609

    
1610
            _limit = convert_bytes_to_limit(_ranges.vhf2_high)
1611
            val = RadioSettingValueString(0, 3, _limit)
1612
            val.set_mutable(False)
1613
            vhf2_high = RadioSetting("%s.vhf2_high" % ranges, "VHF2 high", val)
1614
            other.append(vhf2_high)
1615

    
1616
        _limit = convert_bytes_to_limit(_ranges.uhf_low)
1617
        val = RadioSettingValueString(0, 3, _limit)
1618
        val.set_mutable(False)
1619
        uhf_low = RadioSetting("%s.uhf_low" % ranges, "UHF low", val)
1620
        other.append(uhf_low)
1621

    
1622
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1623
        val = RadioSettingValueString(0, 3, _limit)
1624
        val.set_mutable(False)
1625
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1626
        other.append(uhf_high)
1627

    
1628
        if self.BANDS == 4:
1629
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1630
            val = RadioSettingValueString(0, 3, _limit)
1631
            val.set_mutable(False)
1632
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1633
            other.append(uhf2_low)
1634

    
1635
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1636
            val = RadioSettingValueString(0, 3, _limit)
1637
            val.set_mutable(False)
1638
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1639
            other.append(uhf2_high)
1640

    
1641
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1642
        val.set_mutable(False)
1643
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1644
        other.append(fp)
1645

    
1646
        # Work
1647
        if self.COLOR_LCD:
1648
            dispab = RadioSetting("settings2.dispab", "Display",
1649
                                  RadioSettingValueList(
1650
                                      LIST_ABCD,
1651
                                      LIST_ABCD[_mem.settings2.dispab]))
1652
            work.append(dispab)
1653
        else:
1654
            dispab = RadioSetting("settings2.dispab", "Display",
1655
                                  RadioSettingValueList(
1656
                                      LIST_AB,
1657
                                      LIST_AB[_mem.settings2.dispab]))
1658
            work.append(dispab)
1659

    
1660
        if self.COLOR_LCD:
1661
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode",
1662
                                  RadioSettingValueList(
1663
                                      LIST_VFOMR,
1664
                                      LIST_VFOMR[_mem.settings2.vfomra]))
1665
            work.append(vfomra)
1666

    
1667
            vfomrb = RadioSetting("settings2.vfomrb", "VFO/MR B mode",
1668
                                  RadioSettingValueList(
1669
                                      LIST_VFOMR,
1670
                                      LIST_VFOMR[_mem.settings2.vfomrb]))
1671
            work.append(vfomrb)
1672

    
1673
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode",
1674
                                  RadioSettingValueList(
1675
                                      LIST_VFOMR,
1676
                                      LIST_VFOMR[_mem.settings2.vfomrc]))
1677
            work.append(vfomrc)
1678

    
1679
            vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode",
1680
                                  RadioSettingValueList(
1681
                                      LIST_VFOMR,
1682
                                      LIST_VFOMR[_mem.settings2.vfomrd]))
1683
            work.append(vfomrd)
1684
        else:
1685
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode",
1686
                                 RadioSettingValueList(
1687
                                     LIST_VFOMR,
1688
                                     LIST_VFOMR[_mem.settings2.vfomr]))
1689
            work.append(vfomr)
1690

    
1691
        keylock = RadioSetting("settings2.keylock", "Keypad lock",
1692
                               RadioSettingValueBoolean(
1693
                                   _mem.settings2.keylock))
1694
        work.append(keylock)
1695

    
1696
        mrcha = RadioSetting("settings2.mrcha", "MR A channel",
1697
                             RadioSettingValueInteger(0, self._upper,
1698
                                                      _mem.settings2.mrcha))
1699
        work.append(mrcha)
1700

    
1701
        mrchb = RadioSetting("settings2.mrchb", "MR B channel",
1702
                             RadioSettingValueInteger(0, self._upper,
1703
                                                      _mem.settings2.mrchb))
1704
        work.append(mrchb)
1705

    
1706
        if self.COLOR_LCD:
1707
            mrchc = RadioSetting("settings2.mrchc", "MR C channel",
1708
                                 RadioSettingValueInteger(
1709
                                     0, self._upper, _mem.settings2.mrchc))
1710
            work.append(mrchc)
1711

    
1712
            mrchd = RadioSetting("settings2.mrchd", "MR D channel",
1713
                                 RadioSettingValueInteger(
1714
                                     0, self._upper, _mem.settings2.mrchd))
1715
            work.append(mrchd)
1716

    
1717
        def convert_bytes_to_freq(bytes):
1718
            real_freq = 0
1719
            for byte in bytes:
1720
                real_freq = (real_freq * 10) + byte
1721
            return chirp_common.format_freq(real_freq * 10)
1722

    
1723
        def my_validate(value):
1724
            _vhf_lower = int(convert_bytes_to_limit(_ranges.vhf_low))
1725
            _vhf_upper = int(convert_bytes_to_limit(_ranges.vhf_high))
1726
            _uhf_lower = int(convert_bytes_to_limit(_ranges.uhf_low))
1727
            _uhf_upper = int(convert_bytes_to_limit(_ranges.uhf_high))
1728
            if self.BANDS == 3 or self.BANDS == 4:
1729
                _vhf2_lower = int(convert_bytes_to_limit(_ranges.vhf2_low))
1730
                _vhf2_upper = int(convert_bytes_to_limit(_ranges.vhf2_high))
1731
            if self.BANDS == 4:
1732
                _uhf2_lower = int(convert_bytes_to_limit(_ranges.uhf2_low))
1733
                _uhf2_upper = int(convert_bytes_to_limit(_ranges.uhf2_high))
1734

    
1735
            value = chirp_common.parse_freq(value)
1736
            msg = ("Can't be less then %i.0000")
1737
            if value > 99000000 and value < _vhf_lower * 1000000:
1738
                raise InvalidValueError(msg % (_vhf_lower))
1739
            msg = ("Can't be betweeb %i.9975-%i.0000")
1740
            if self.BANDS == 2:
1741
                if (_vhf_upper + 1) * 1000000 <= value and \
1742
                        value < _uhf_lower * 1000000:
1743
                    raise InvalidValueError(msg % (_vhf_upper, _uhf_lower))
1744
            if self.BANDS == 3:
1745
                if (_vhf_upper + 1) * 1000000 <= value and \
1746
                        value < _vhf2_lower * 1000000:
1747
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1748
                if (_vhf2_upper + 1) * 1000000 <= value and \
1749
                        value < _uhf_lower * 1000000:
1750
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf_lower))
1751
            if self.BANDS == 4:
1752
                if (_vhf_upper + 1) * 1000000 <= value and \
1753
                        value < _vhf2_lower * 1000000:
1754
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1755
                if (_vhf2_upper + 1) * 1000000 <= value and \
1756
                        value < _uhf2_lower * 1000000:
1757
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf2_lower))
1758
                if (_uhf2_upper + 1) * 1000000 <= value and \
1759
                        value < _uhf_lower * 1000000:
1760
                    raise InvalidValueError(msg % (_uhf2_upper, _uhf_lower))
1761
            msg = ("Can't be greater then %i.9975")
1762
            if value > 99000000 and value >= _uhf_upper * 1000000:
1763
                raise InvalidValueError(msg % (_uhf_upper))
1764
            return chirp_common.format_freq(value)
1765

    
1766
        def apply_freq(setting, obj):
1767
            value = chirp_common.parse_freq(str(setting.value)) / 10
1768
            for i in range(7, -1, -1):
1769
                obj.freq[i] = value % 10
1770
                value /= 10
1771

    
1772
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1773
                                        _mem.vfo.a.freq))
1774
        val1a.set_validate_callback(my_validate)
1775
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
1776
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
1777
        work.append(vfoafreq)
1778

    
1779
        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1780
                                        _mem.vfo.b.freq))
1781
        val1b.set_validate_callback(my_validate)
1782
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
1783
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
1784
        work.append(vfobfreq)
1785

    
1786
        if self.COLOR_LCD:
1787
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1788
                                            _mem.vfo.c.freq))
1789
            val1c.set_validate_callback(my_validate)
1790
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
1791
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
1792
            work.append(vfocfreq)
1793

    
1794
            val1d = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1795
                                            _mem.vfo.d.freq))
1796
            val1d.set_validate_callback(my_validate)
1797
            vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
1798
            vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
1799
            work.append(vfodfreq)
1800

    
1801
        if not self.MODEL == "GMRS-50X1":
1802
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift",
1803
                                      RadioSettingValueList(
1804
                                          LIST_SHIFT,
1805
                                          LIST_SHIFT[_mem.vfo.a.shiftd]))
1806
            work.append(vfoashiftd)
1807

    
1808
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift",
1809
                                      RadioSettingValueList(
1810
                                          LIST_SHIFT,
1811
                                          LIST_SHIFT[_mem.vfo.b.shiftd]))
1812
            work.append(vfobshiftd)
1813

    
1814
            if self.COLOR_LCD:
1815
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift",
1816
                                          RadioSettingValueList(
1817
                                              LIST_SHIFT,
1818
                                              LIST_SHIFT[_mem.vfo.c.shiftd]))
1819
                work.append(vfocshiftd)
1820

    
1821
                vfodshiftd = RadioSetting("vfo.d.shiftd", "VFO D shift",
1822
                                          RadioSettingValueList(
1823
                                              LIST_SHIFT,
1824
                                              LIST_SHIFT[_mem.vfo.d.shiftd]))
1825
                work.append(vfodshiftd)
1826

    
1827
        def convert_bytes_to_offset(bytes):
1828
            real_offset = 0
1829
            for byte in bytes:
1830
                real_offset = (real_offset * 10) + byte
1831
            return chirp_common.format_freq(real_offset * 1000)
1832

    
1833
        def apply_offset(setting, obj):
1834
            value = chirp_common.parse_freq(str(setting.value)) / 1000
1835
            for i in range(5, -1, -1):
1836
                obj.offset[i] = value % 10
1837
                value /= 10
1838

    
1839
        if not self.MODEL == "GMRS-50X1":
1840
            if self.COLOR_LCD:
1841
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1842
                                                _mem.vfo.a.offset))
1843
                vfoaoffset = RadioSetting("vfo.a.offset",
1844
                                          "VFO A offset (0.000-999.999)",
1845
                                          val1a)
1846
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
1847
                work.append(vfoaoffset)
1848

    
1849
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1850
                                                _mem.vfo.b.offset))
1851
                vfoboffset = RadioSetting("vfo.b.offset",
1852
                                          "VFO B offset (0.000-999.999)",
1853
                                          val1b)
1854
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1855
                work.append(vfoboffset)
1856

    
1857
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1858
                                                _mem.vfo.c.offset))
1859
                vfocoffset = RadioSetting("vfo.c.offset",
1860
                                          "VFO C offset (0.000-999.999)",
1861
                                          val1c)
1862
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
1863
                work.append(vfocoffset)
1864

    
1865
                val1d = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1866
                                                _mem.vfo.d.offset))
1867
                vfodoffset = RadioSetting("vfo.d.offset",
1868
                                          "VFO D offset (0.000-999.999)",
1869
                                          val1d)
1870
                vfodoffset.set_apply_callback(apply_offset, _mem.vfo.d)
1871
                work.append(vfodoffset)
1872
            else:
1873
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1874
                                                _mem.vfo.a.offset))
1875
                vfoaoffset = RadioSetting("vfo.a.offset",
1876
                                          "VFO A offset (0.000-99.999)", val1a)
1877
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
1878
                work.append(vfoaoffset)
1879

    
1880
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
1881
                                                _mem.vfo.b.offset))
1882
                vfoboffset = RadioSetting("vfo.b.offset",
1883
                                          "VFO B offset (0.000-99.999)", val1b)
1884
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
1885
                work.append(vfoboffset)
1886

    
1887
        if not self.MODEL == "GMRS-50X1":
1888
            vfoatxp = RadioSetting("vfo.a.power", "VFO A power",
1889
                                   RadioSettingValueList(
1890
                                       LIST_TXP,
1891
                                       LIST_TXP[_mem.vfo.a.power]))
1892
            work.append(vfoatxp)
1893

    
1894
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power",
1895
                                   RadioSettingValueList(
1896
                                       LIST_TXP,
1897
                                       LIST_TXP[_mem.vfo.b.power]))
1898
            work.append(vfobtxp)
1899

    
1900
            if self.COLOR_LCD:
1901
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power",
1902
                                       RadioSettingValueList(
1903
                                           LIST_TXP,
1904
                                           LIST_TXP[_mem.vfo.c.power]))
1905
                work.append(vfoctxp)
1906

    
1907
                vfodtxp = RadioSetting("vfo.d.power", "VFO D power",
1908
                                       RadioSettingValueList(
1909
                                           LIST_TXP,
1910
                                           LIST_TXP[_mem.vfo.d.power]))
1911
                work.append(vfodtxp)
1912

    
1913
        if not self.MODEL == "GMRS-50X1":
1914
            vfoawide = RadioSetting("vfo.a.wide", "VFO A bandwidth",
1915
                                    RadioSettingValueList(
1916
                                        LIST_WIDE,
1917
                                        LIST_WIDE[_mem.vfo.a.wide]))
1918
            work.append(vfoawide)
1919

    
1920
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth",
1921
                                    RadioSettingValueList(
1922
                                        LIST_WIDE,
1923
                                        LIST_WIDE[_mem.vfo.b.wide]))
1924
            work.append(vfobwide)
1925

    
1926
            if self.COLOR_LCD:
1927
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth",
1928
                                        RadioSettingValueList(
1929
                                            LIST_WIDE,
1930
                                            LIST_WIDE[_mem.vfo.c.wide]))
1931
                work.append(vfocwide)
1932

    
1933
                vfodwide = RadioSetting("vfo.d.wide", "VFO D bandwidth",
1934
                                        RadioSettingValueList(
1935
                                            LIST_WIDE,
1936
                                            LIST_WIDE[_mem.vfo.d.wide]))
1937
                work.append(vfodwide)
1938

    
1939
        vfoastep = RadioSetting("vfo.a.step", "VFO A step",
1940
                                RadioSettingValueList(
1941
                                    LIST_STEP,
1942
                                    LIST_STEP[_mem.vfo.a.step]))
1943
        work.append(vfoastep)
1944

    
1945
        vfobstep = RadioSetting("vfo.b.step", "VFO B step",
1946
                                RadioSettingValueList(
1947
                                    LIST_STEP,
1948
                                    LIST_STEP[_mem.vfo.b.step]))
1949
        work.append(vfobstep)
1950

    
1951
        if self.COLOR_LCD:
1952
            vfocstep = RadioSetting("vfo.c.step", "VFO C step",
1953
                                    RadioSettingValueList(
1954
                                        LIST_STEP,
1955
                                        LIST_STEP[_mem.vfo.c.step]))
1956
            work.append(vfocstep)
1957

    
1958
            vfodstep = RadioSetting("vfo.d.step", "VFO D step",
1959
                                    RadioSettingValueList(
1960
                                        LIST_STEP,
1961
                                        LIST_STEP[_mem.vfo.d.step]))
1962
            work.append(vfodstep)
1963

    
1964
        vfoaoptsig = RadioSetting("vfo.a.optsig", "VFO A optional signal",
1965
                                  RadioSettingValueList(
1966
                                      OPTSIG_LIST,
1967
                                      OPTSIG_LIST[_mem.vfo.a.optsig]))
1968
        work.append(vfoaoptsig)
1969

    
1970
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal",
1971
                                  RadioSettingValueList(
1972
                                      OPTSIG_LIST,
1973
                                      OPTSIG_LIST[_mem.vfo.b.optsig]))
1974
        work.append(vfoboptsig)
1975

    
1976
        if self.COLOR_LCD:
1977
            vfocoptsig = RadioSetting("vfo.c.optsig", "VFO C optional signal",
1978
                                      RadioSettingValueList(
1979
                                          OPTSIG_LIST,
1980
                                          OPTSIG_LIST[_mem.vfo.c.optsig]))
1981
            work.append(vfocoptsig)
1982

    
1983
            vfodoptsig = RadioSetting("vfo.d.optsig", "VFO D optional signal",
1984
                                      RadioSettingValueList(
1985
                                          OPTSIG_LIST,
1986
                                          OPTSIG_LIST[_mem.vfo.d.optsig]))
1987
            work.append(vfodoptsig)
1988

    
1989
        vfoaspmute = RadioSetting("vfo.a.spmute", "VFO A speaker mute",
1990
                                  RadioSettingValueList(
1991
                                      SPMUTE_LIST,
1992
                                      SPMUTE_LIST[_mem.vfo.a.spmute]))
1993
        work.append(vfoaspmute)
1994

    
1995
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute",
1996
                                  RadioSettingValueList(
1997
                                      SPMUTE_LIST,
1998
                                      SPMUTE_LIST[_mem.vfo.b.spmute]))
1999
        work.append(vfobspmute)
2000

    
2001
        if self.COLOR_LCD:
2002
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute",
2003
                                      RadioSettingValueList(
2004
                                          SPMUTE_LIST,
2005
                                          SPMUTE_LIST[_mem.vfo.c.spmute]))
2006
            work.append(vfocspmute)
2007

    
2008
            vfodspmute = RadioSetting("vfo.d.spmute", "VFO D speaker mute",
2009
                                      RadioSettingValueList(
2010
                                          SPMUTE_LIST,
2011
                                          SPMUTE_LIST[_mem.vfo.d.spmute]))
2012
            work.append(vfodspmute)
2013

    
2014
        if not self.COLOR_LCD or \
2015
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2016
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble",
2017
                                   RadioSettingValueBoolean(
2018
                                       _mem.vfo.a.scramble))
2019
            work.append(vfoascr)
2020

    
2021
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble",
2022
                                   RadioSettingValueBoolean(
2023
                                       _mem.vfo.b.scramble))
2024
            work.append(vfobscr)
2025

    
2026
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2027
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble",
2028
                                   RadioSettingValueBoolean(
2029
                                       _mem.vfo.c.scramble))
2030
            work.append(vfocscr)
2031

    
2032
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble",
2033
                                   RadioSettingValueBoolean(
2034
                                       _mem.vfo.d.scramble))
2035
            work.append(vfodscr)
2036

    
2037
        if not self.MODEL == "GMRS-50X1":
2038
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID",
2039
                                     RadioSettingValueList(
2040
                                         PTTIDCODE_LIST,
2041
                                         PTTIDCODE_LIST[_mem.vfo.a.scode]))
2042
            work.append(vfoascode)
2043

    
2044
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID",
2045
                                     RadioSettingValueList(
2046
                                         PTTIDCODE_LIST,
2047
                                         PTTIDCODE_LIST[_mem.vfo.b.scode]))
2048
            work.append(vfobscode)
2049

    
2050
            if self.COLOR_LCD:
2051
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID",
2052
                                         RadioSettingValueList(
2053
                                             PTTIDCODE_LIST,
2054
                                             PTTIDCODE_LIST[_mem.vfo.c.scode]))
2055
                work.append(vfocscode)
2056

    
2057
                vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID",
2058
                                         RadioSettingValueList(
2059
                                             PTTIDCODE_LIST,
2060
                                             PTTIDCODE_LIST[_mem.vfo.d.scode]))
2061
                work.append(vfodscode)
2062

    
2063
        if not self.MODEL == "GMRS-50X1":
2064
            pttid = RadioSetting("settings.pttid", "PTT ID",
2065
                                 RadioSettingValueList(
2066
                                     PTTID_LIST,
2067
                                     PTTID_LIST[_mem.settings.pttid]))
2068
            work.append(pttid)
2069

    
2070
        if not self.COLOR_LCD:
2071
            # FM presets
2072
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2073
            top.append(fm_presets)
2074

    
2075
            def fm_validate(value):
2076
                if value == 0:
2077
                    return chirp_common.format_freq(value)
2078
                if not (87.5 <= value and value <= 108.0):  # 87.5-108MHz
2079
                    msg = ("FM-Preset-Frequency: " +
2080
                           "Must be between 87.5 and 108 MHz")
2081
                    raise InvalidValueError(msg)
2082
                return value
2083

    
2084
            def apply_fm_preset_name(setting, obj):
2085
                valstring = str(setting.value)
2086
                for i in range(0, 6):
2087
                    if valstring[i] in VALID_CHARS:
2088
                        obj[i] = valstring[i]
2089
                    else:
2090
                        obj[i] = '0xff'
2091

    
2092
            def apply_fm_freq(setting, obj):
2093
                value = chirp_common.parse_freq(str(setting.value)) / 10
2094
                for i in range(7, -1, -1):
2095
                    obj.freq[i] = value % 10
2096
                    value /= 10
2097

    
2098
            _presets = self._memobj.fm_radio_preset
2099
            i = 1
2100
            for preset in _presets:
2101
                line = RadioSetting("fm_presets_" + str(i),
2102
                                    "Station name " + str(i),
2103
                                    RadioSettingValueString(0, 6, _filter(
2104
                                        preset.broadcast_station_name)))
2105
                line.set_apply_callback(apply_fm_preset_name,
2106
                                        preset.broadcast_station_name)
2107

    
2108
                val = RadioSettingValueFloat(0, 108,
2109
                                             convert_bytes_to_freq(
2110
                                                 preset.freq))
2111
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2112
                                      "Frequency " + str(i), val)
2113
                val.set_validate_callback(fm_validate)
2114
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2115
                fm_presets.append(line)
2116
                fm_presets.append(fmfreq)
2117

    
2118
                i = i + 1
2119

    
2120
        # DTMF-Setting
2121
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2122
                                              "DTMF Encoding Settings")
2123
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2124
                                              "DTMF Decoding Settings")
2125
        top.append(dtmf_enc_settings)
2126
        top.append(dtmf_dec_settings)
2127
        txdisable = RadioSetting("dtmf_settings.txdisable",
2128
                                 "TX-Disable",
2129
                                 RadioSettingValueBoolean(
2130
                                     _mem.dtmf_settings.txdisable))
2131
        dtmf_enc_settings.append(txdisable)
2132

    
2133
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2134
                                 "RX-Disable",
2135
                                 RadioSettingValueBoolean(
2136
                                     _mem.dtmf_settings.rxdisable))
2137
        dtmf_enc_settings.append(rxdisable)
2138

    
2139
        dtmfspeed_on = RadioSetting(
2140
            "dtmf_settings.dtmfspeed_on",
2141
            "DTMF Speed (On Time)",
2142
            RadioSettingValueList(LIST_DTMF_SPEED,
2143
                                  LIST_DTMF_SPEED[
2144
                                      _mem.dtmf_settings.dtmfspeed_on]))
2145
        dtmf_enc_settings.append(dtmfspeed_on)
2146

    
2147
        dtmfspeed_off = RadioSetting(
2148
            "dtmf_settings.dtmfspeed_off",
2149
            "DTMF Speed (Off Time)",
2150
            RadioSettingValueList(LIST_DTMF_SPEED,
2151
                                  LIST_DTMF_SPEED[
2152
                                      _mem.dtmf_settings.dtmfspeed_off]))
2153
        dtmf_enc_settings.append(dtmfspeed_off)
2154

    
2155
        def memory2string(dmtf_mem):
2156
            dtmf_string = ""
2157
            for digit in dmtf_mem:
2158
                if digit != 255:
2159
                    index = LIST_DTMF_VALUES.index(digit)
2160
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2161
            return dtmf_string
2162

    
2163
        def apply_dmtf_frame(setting, obj):
2164
            LOG.debug("Setting DTMF-Code: " + str(setting.value))
2165
            val_string = str(setting.value)
2166
            for i in range(0, 16):
2167
                obj[i] = 255
2168
            i = 0
2169
            for current_char in val_string:
2170
                current_char = current_char.upper()
2171
                index = LIST_DTMF_DIGITS.index(current_char)
2172
                obj[i] = LIST_DTMF_VALUES[index]
2173
                i = i + 1
2174

    
2175
        codes = self._memobj.dtmf_codes
2176
        i = 1
2177
        for dtmfcode in codes:
2178
            val = RadioSettingValueString(0, 16, memory2string(
2179
                                              dtmfcode.code),
2180
                                          False, CHARSET_DTMF_DIGITS)
2181
            line = RadioSetting("dtmf_code_" + str(i) + "_code",
2182
                                "DMTF Code " + str(i), val)
2183
            line.set_apply_callback(apply_dmtf_frame, dtmfcode.code)
2184
            dtmf_enc_settings.append(line)
2185
            i = i + 1
2186

    
2187
        line = RadioSetting("dtmf_settings.mastervice",
2188
                            "Master and Vice ID",
2189
                            RadioSettingValueBoolean(
2190
                                _mem.dtmf_settings.mastervice))
2191
        dtmf_dec_settings.append(line)
2192

    
2193
        val = RadioSettingValueString(0, 16, memory2string(
2194
                                          _mem.dtmf_settings.masterid),
2195
                                      False, CHARSET_DTMF_DIGITS)
2196
        line = RadioSetting("dtmf_settings.masterid",
2197
                            "Master Control ID ", val)
2198
        line.set_apply_callback(apply_dmtf_frame,
2199
                                _mem.dtmf_settings.masterid)
2200
        dtmf_dec_settings.append(line)
2201

    
2202
        line = RadioSetting("dtmf_settings.minspection",
2203
                            "Master Inspection",
2204
                            RadioSettingValueBoolean(
2205
                                _mem.dtmf_settings.minspection))
2206
        dtmf_dec_settings.append(line)
2207

    
2208
        line = RadioSetting("dtmf_settings.mmonitor",
2209
                            "Master Monitor",
2210
                            RadioSettingValueBoolean(
2211
                                _mem.dtmf_settings.mmonitor))
2212
        dtmf_dec_settings.append(line)
2213

    
2214
        line = RadioSetting("dtmf_settings.mstun",
2215
                            "Master Stun",
2216
                            RadioSettingValueBoolean(
2217
                                _mem.dtmf_settings.mstun))
2218
        dtmf_dec_settings.append(line)
2219

    
2220
        line = RadioSetting("dtmf_settings.mkill",
2221
                            "Master Kill",
2222
                            RadioSettingValueBoolean(
2223
                                _mem.dtmf_settings.mkill))
2224
        dtmf_dec_settings.append(line)
2225

    
2226
        line = RadioSetting("dtmf_settings.mrevive",
2227
                            "Master Revive",
2228
                            RadioSettingValueBoolean(
2229
                                _mem.dtmf_settings.mrevive))
2230
        dtmf_dec_settings.append(line)
2231

    
2232
        val = RadioSettingValueString(0, 16, memory2string(
2233
                                          _mem.dtmf_settings.viceid),
2234
                                      False, CHARSET_DTMF_DIGITS)
2235
        line = RadioSetting("dtmf_settings.viceid",
2236
                            "Vice Control ID ", val)
2237
        line.set_apply_callback(apply_dmtf_frame,
2238
                                _mem.dtmf_settings.viceid)
2239
        dtmf_dec_settings.append(line)
2240

    
2241
        line = RadioSetting("dtmf_settings.vinspection",
2242
                            "Vice Inspection",
2243
                            RadioSettingValueBoolean(
2244
                                _mem.dtmf_settings.vinspection))
2245
        dtmf_dec_settings.append(line)
2246

    
2247
        line = RadioSetting("dtmf_settings.vmonitor",
2248
                            "Vice Monitor",
2249
                            RadioSettingValueBoolean(
2250
                                _mem.dtmf_settings.vmonitor))
2251
        dtmf_dec_settings.append(line)
2252

    
2253
        line = RadioSetting("dtmf_settings.vstun",
2254
                            "Vice Stun",
2255
                            RadioSettingValueBoolean(
2256
                                _mem.dtmf_settings.vstun))
2257
        dtmf_dec_settings.append(line)
2258

    
2259
        line = RadioSetting("dtmf_settings.vkill",
2260
                            "Vice Kill",
2261
                            RadioSettingValueBoolean(
2262
                                _mem.dtmf_settings.vkill))
2263
        dtmf_dec_settings.append(line)
2264

    
2265
        line = RadioSetting("dtmf_settings.vrevive",
2266
                            "Vice Revive",
2267
                            RadioSettingValueBoolean(
2268
                                _mem.dtmf_settings.vrevive))
2269
        dtmf_dec_settings.append(line)
2270

    
2271
        val = RadioSettingValueString(0, 16, memory2string(
2272
                                          _mem.dtmf_settings.inspection),
2273
                                      False, CHARSET_DTMF_DIGITS)
2274
        line = RadioSetting("dtmf_settings.inspection",
2275
                            "Inspection", val)
2276
        line.set_apply_callback(apply_dmtf_frame,
2277
                                _mem.dtmf_settings.inspection)
2278
        dtmf_dec_settings.append(line)
2279

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

    
2289
        val = RadioSettingValueString(0, 16, memory2string(
2290
                                          _mem.dtmf_settings.kill),
2291
                                      False, CHARSET_DTMF_DIGITS)
2292
        line = RadioSetting("dtmf_settings.kill",
2293
                            "Kill", val)
2294
        line.set_apply_callback(apply_dmtf_frame,
2295
                                _mem.dtmf_settings.kill)
2296
        dtmf_dec_settings.append(line)
2297

    
2298
        val = RadioSettingValueString(0, 16, memory2string(
2299
                                          _mem.dtmf_settings.monitor),
2300
                                      False, CHARSET_DTMF_DIGITS)
2301
        line = RadioSetting("dtmf_settings.monitor",
2302
                            "Monitor", val)
2303
        line.set_apply_callback(apply_dmtf_frame,
2304
                                _mem.dtmf_settings.monitor)
2305
        dtmf_dec_settings.append(line)
2306

    
2307
        val = RadioSettingValueString(0, 16, memory2string(
2308
                                          _mem.dtmf_settings.stun),
2309
                                      False, CHARSET_DTMF_DIGITS)
2310
        line = RadioSetting("dtmf_settings.stun",
2311
                            "Stun", val)
2312
        line.set_apply_callback(apply_dmtf_frame,
2313
                                _mem.dtmf_settings.stun)
2314
        dtmf_dec_settings.append(line)
2315

    
2316
        val = RadioSettingValueString(0, 16, memory2string(
2317
                                          _mem.dtmf_settings.revive),
2318
                                      False, CHARSET_DTMF_DIGITS)
2319
        line = RadioSetting("dtmf_settings.revive",
2320
                            "Revive", val)
2321
        line.set_apply_callback(apply_dmtf_frame,
2322
                                _mem.dtmf_settings.revive)
2323
        dtmf_dec_settings.append(line)
2324

    
2325
        def apply_dmtf_listvalue(setting, obj):
2326
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2327
            val = str(setting.value)
2328
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2329
            val = LIST_DTMF_SPECIAL_VALUES[index]
2330
            obj.set_value(val)
2331

    
2332
        idx = LIST_DTMF_SPECIAL_VALUES.index(_mem.dtmf_settings.groupcode)
2333
        line = RadioSetting(
2334
            "dtmf_settings.groupcode",
2335
            "Group Code",
2336
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2337
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2338
        line.set_apply_callback(apply_dmtf_listvalue,
2339
                                _mem.dtmf_settings.groupcode)
2340
        dtmf_dec_settings.append(line)
2341

    
2342
        idx = LIST_DTMF_SPECIAL_VALUES.index(_mem.dtmf_settings.spacecode)
2343
        line = RadioSetting(
2344
            "dtmf_settings.spacecode",
2345
            "Space Code",
2346
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2347
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2348
        line.set_apply_callback(apply_dmtf_listvalue,
2349
                                _mem.dtmf_settings.spacecode)
2350
        dtmf_dec_settings.append(line)
2351

    
2352
        if self.COLOR_LCD:
2353
            line = RadioSetting(
2354
                "dtmf_settings.resettime",
2355
                "Reset time",
2356
                RadioSettingValueList(LIST_5TONE_RESET_COLOR,
2357
                                      LIST_5TONE_RESET_COLOR[
2358
                                          _mem.dtmf_settings.resettime]))
2359
            dtmf_dec_settings.append(line)
2360
        else:
2361
            line = RadioSetting(
2362
                "dtmf_settings.resettime",
2363
                "Reset time",
2364
                RadioSettingValueList(LIST_5TONE_RESET,
2365
                                      LIST_5TONE_RESET[
2366
                                          _mem.dtmf_settings.resettime]))
2367
            dtmf_dec_settings.append(line)
2368

    
2369
        line = RadioSetting(
2370
            "dtmf_settings.delayproctime",
2371
            "Delay processing time",
2372
            RadioSettingValueList(LIST_DTMF_DELAY,
2373
                                  LIST_DTMF_DELAY[
2374
                                      _mem.dtmf_settings.delayproctime]))
2375
        dtmf_dec_settings.append(line)
2376

    
2377
        # 5 Tone Settings
2378
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2379
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2380

    
2381
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2382
        group_5tone.append(stds_5tone)
2383
        group_5tone.append(codes_5tone)
2384

    
2385
        top.append(group_5tone)
2386

    
2387
        def apply_list_value(setting, obj):
2388
            options = setting.value.get_options()
2389
            obj.set_value(options.index(str(setting.value)))
2390

    
2391
        _5tone_standards = self._memobj._5tone_std_settings
2392
        i = 0
2393
        for standard in _5tone_standards:
2394
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2395
                                          LIST_5TONE_STANDARDS[i])
2396
            stds_5tone.append(std_5tone)
2397

    
2398
            period = standard.period
2399
            if period == 255:
2400
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2401
                          " is not yet configured. Setting to 70ms.")
2402
                period = 5
2403

    
2404
            if period <= len(LIST_5TONE_STANDARD_PERIODS):
2405
                line = RadioSetting(
2406
                    "_5tone_std_settings_" + str(i) + "_period",
2407
                    "Period (ms)", RadioSettingValueList
2408
                    (LIST_5TONE_STANDARD_PERIODS,
2409
                     LIST_5TONE_STANDARD_PERIODS[period]))
2410
                line.set_apply_callback(apply_list_value, standard.period)
2411
                std_5tone.append(line)
2412
            else:
2413
                LOG.debug("Invalid value for 5tone period! Disabling.")
2414

    
2415
            group_tone = standard.group_tone
2416
            if group_tone == 255:
2417
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2418
                          " is not yet configured. Setting to A.")
2419
                group_tone = 10
2420

    
2421
            if group_tone <= len(LIST_5TONE_DIGITS):
2422
                line = RadioSetting(
2423
                    "_5tone_std_settings_" + str(i) + "_grouptone",
2424
                    "Group Tone",
2425
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2426
                                          LIST_5TONE_DIGITS[
2427
                                              group_tone]))
2428
                line.set_apply_callback(apply_list_value,
2429
                                        standard.group_tone)
2430
                std_5tone.append(line)
2431
            else:
2432
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2433

    
2434
            repeat_tone = standard.repeat_tone
2435
            if repeat_tone == 255:
2436
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2437
                          " is not yet configured. Setting to E.")
2438
                repeat_tone = 14
2439

    
2440
            if repeat_tone <= len(LIST_5TONE_DIGITS):
2441
                line = RadioSetting(
2442
                    "_5tone_std_settings_" + str(i) + "_repttone",
2443
                    "Repeat Tone",
2444
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2445
                                          LIST_5TONE_DIGITS[
2446
                                              repeat_tone]))
2447
                line.set_apply_callback(apply_list_value,
2448
                                        standard.repeat_tone)
2449
                std_5tone.append(line)
2450
            else:
2451
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2452
            i = i + 1
2453

    
2454
        def my_apply_5tonestdlist_value(setting, obj):
2455
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2456
                obj.set_value(0xFF)
2457
            else:
2458
                obj.set_value(LIST_5TONE_STANDARDS.
2459
                              index(str(setting.value)))
2460

    
2461
        def apply_5tone_frame(setting, obj):
2462
            LOG.debug("Setting 5 Tone: " + str(setting.value))
2463
            valstring = str(setting.value)
2464
            if len(valstring) == 0:
2465
                for i in range(0, 5):
2466
                    obj[i] = 255
2467
            else:
2468
                validFrame = True
2469
                for i in range(0, 5):
2470
                    currentChar = valstring[i].upper()
2471
                    if currentChar in LIST_5TONE_DIGITS:
2472
                        obj[i] = LIST_5TONE_DIGITS.index(currentChar)
2473
                    else:
2474
                        validFrame = False
2475
                        LOG.debug("invalid char: " + str(currentChar))
2476
                if not validFrame:
2477
                    LOG.debug("setting whole frame to FF")
2478
                    for i in range(0, 5):
2479
                        obj[i] = 255
2480

    
2481
        def validate_5tone_frame(value):
2482
            if (len(str(value)) != 5) and (len(str(value)) != 0):
2483
                msg = ("5 Tone must have 5 digits or 0 digits")
2484
                raise InvalidValueError(msg)
2485
            for digit in str(value):
2486
                if digit.upper() not in LIST_5TONE_DIGITS:
2487
                    msg = (str(digit) + " is not a valid digit for 5tones")
2488
                    raise InvalidValueError(msg)
2489
            return value
2490

    
2491
        def frame2string(frame):
2492
            frameString = ""
2493
            for digit in frame:
2494
                if digit != 255:
2495
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2496
            return frameString
2497

    
2498
        _5tone_codes = self._memobj._5tone_codes
2499
        i = 1
2500
        for code in _5tone_codes:
2501
            code_5tone = RadioSettingGroup("code_5tone_" + str(i),
2502
                                           "5 Tone code " + str(i))
2503
            codes_5tone.append(code_5tone)
2504
            if (code.standard == 255):
2505
                currentVal = 15
2506
            else:
2507
                currentVal = code.standard
2508
            line = RadioSetting("_5tone_code_" + str(i) + "_std",
2509
                                " Standard",
2510
                                RadioSettingValueList(LIST_5TONE_STANDARDS,
2511
                                                      LIST_5TONE_STANDARDS[
2512
                                                          currentVal]))
2513
            line.set_apply_callback(my_apply_5tonestdlist_value,
2514
                                    code.standard)
2515
            code_5tone.append(line)
2516

    
2517
            val = RadioSettingValueString(0, 6,
2518
                                          frame2string(code.frame1), False)
2519
            line = RadioSetting("_5tone_code_" + str(i) + "_frame1",
2520
                                " Frame 1", val)
2521
            val.set_validate_callback(validate_5tone_frame)
2522
            line.set_apply_callback(apply_5tone_frame, code.frame1)
2523
            code_5tone.append(line)
2524

    
2525
            val = RadioSettingValueString(0, 6,
2526
                                          frame2string(code.frame2), False)
2527
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2528
                                " Frame 2", val)
2529
            val.set_validate_callback(validate_5tone_frame)
2530
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2531
            code_5tone.append(line)
2532

    
2533
            val = RadioSettingValueString(0, 6,
2534
                                          frame2string(code.frame3), False)
2535
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2536
                                " Frame 3", val)
2537
            val.set_validate_callback(validate_5tone_frame)
2538
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2539
            code_5tone.append(line)
2540
            i = i + 1
2541

    
2542
        _5_tone_decode1 = RadioSetting(
2543
            "_5tone_settings._5tone_decode_call_frame1",
2544
            "5 Tone decode call Frame 1",
2545
            RadioSettingValueBoolean(
2546
                _mem._5tone_settings._5tone_decode_call_frame1))
2547
        group_5tone.append(_5_tone_decode1)
2548

    
2549
        _5_tone_decode2 = RadioSetting(
2550
            "_5tone_settings._5tone_decode_call_frame2",
2551
            "5 Tone decode call Frame 2",
2552
            RadioSettingValueBoolean(
2553
                _mem._5tone_settings._5tone_decode_call_frame2))
2554
        group_5tone.append(_5_tone_decode2)
2555

    
2556
        _5_tone_decode3 = RadioSetting(
2557
            "_5tone_settings._5tone_decode_call_frame3",
2558
            "5 Tone decode call Frame 3",
2559
            RadioSettingValueBoolean(
2560
                _mem._5tone_settings._5tone_decode_call_frame3))
2561
        group_5tone.append(_5_tone_decode3)
2562

    
2563
        _5_tone_decode_disp1 = RadioSetting(
2564
            "_5tone_settings._5tone_decode_disp_frame1",
2565
            "5 Tone decode disp Frame 1",
2566
            RadioSettingValueBoolean(
2567
                _mem._5tone_settings._5tone_decode_disp_frame1))
2568
        group_5tone.append(_5_tone_decode_disp1)
2569

    
2570
        _5_tone_decode_disp2 = RadioSetting(
2571
            "_5tone_settings._5tone_decode_disp_frame2",
2572
            "5 Tone decode disp Frame 2",
2573
            RadioSettingValueBoolean(
2574
                _mem._5tone_settings._5tone_decode_disp_frame2))
2575
        group_5tone.append(_5_tone_decode_disp2)
2576

    
2577
        _5_tone_decode_disp3 = RadioSetting(
2578
            "_5tone_settings._5tone_decode_disp_frame3",
2579
            "5 Tone decode disp Frame 3",
2580
            RadioSettingValueBoolean(
2581
                _mem._5tone_settings._5tone_decode_disp_frame3))
2582
        group_5tone.append(_5_tone_decode_disp3)
2583

    
2584
        decode_standard = _mem._5tone_settings.decode_standard
2585
        if decode_standard == 255:
2586
            decode_standard = 0
2587
        if decode_standard <= len(LIST_5TONE_STANDARDS_without_none):
2588
            line = RadioSetting("_5tone_settings.decode_standard",
2589
                                "5 Tone-decode Standard",
2590
                                RadioSettingValueList(
2591
                                    LIST_5TONE_STANDARDS_without_none,
2592
                                    LIST_5TONE_STANDARDS_without_none[
2593
                                        decode_standard]))
2594
            group_5tone.append(line)
2595
        else:
2596
            LOG.debug("Invalid decode std...")
2597

    
2598
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2599
        if _5tone_delay1 == 255:
2600
            _5tone_delay1 = 20
2601

    
2602
        if _5tone_delay1 <= len(LIST_5TONE_DELAY):
2603
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2604
                                         LIST_5TONE_DELAY[
2605
                                             _5tone_delay1])
2606
            line = RadioSetting("_5tone_settings._5tone_delay1",
2607
                                "5 Tone Delay Frame 1", list)
2608
            group_5tone.append(line)
2609
        else:
2610
            LOG.debug("Invalid value for 5tone delay (frame1) ! Disabling.")
2611

    
2612
        _5tone_delay2 = _mem._5tone_settings._5tone_delay2
2613
        if _5tone_delay2 == 255:
2614
            _5tone_delay2 = 20
2615
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2616

    
2617
        if _5tone_delay2 <= len(LIST_5TONE_DELAY):
2618
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2619
                                         LIST_5TONE_DELAY[
2620
                                             _5tone_delay2])
2621
            line = RadioSetting("_5tone_settings._5tone_delay2",
2622
                                "5 Tone Delay Frame 2", list)
2623
            group_5tone.append(line)
2624
        else:
2625
            LOG.debug("Invalid value for 5tone delay (frame2)! Disabling.")
2626

    
2627
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2628
        if _5tone_delay3 == 255:
2629
            _5tone_delay3 = 20
2630
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2631

    
2632
        if _5tone_delay3 <= len(LIST_5TONE_DELAY):
2633
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2634
                                         LIST_5TONE_DELAY[
2635
                                             _5tone_delay3])
2636
            line = RadioSetting("_5tone_settings._5tone_delay3",
2637
                                "5 Tone Delay Frame 3", list)
2638
            group_5tone.append(line)
2639
        else:
2640
            LOG.debug("Invalid value for 5tone delay (frame3)! Disabling.")
2641

    
2642
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2643
        if ext_length == 255:
2644
            ext_length = 0
2645
            LOG.debug("1st Tone ext lenght unconfigured! Resetting to 0")
2646

    
2647
        if ext_length <= len(LIST_5TONE_DELAY):
2648
            list = RadioSettingValueList(
2649
                LIST_5TONE_DELAY,
2650
                LIST_5TONE_DELAY[
2651
                    ext_length])
2652
            line = RadioSetting(
2653
                "_5tone_settings._5tone_first_digit_ext_length",
2654
                "First digit extend length", list)
2655
            group_5tone.append(line)
2656
        else:
2657
            LOG.debug("Invalid value for 5tone ext length! Disabling.")
2658

    
2659
        decode_reset_time = _mem._5tone_settings.decode_reset_time
2660
        if decode_reset_time == 255:
2661
            decode_reset_time = 59
2662
            LOG.debug("Decode reset time unconfigured. resetting.")
2663
        if decode_reset_time <= len(LIST_5TONE_RESET):
2664
            list = RadioSettingValueList(
2665
                LIST_5TONE_RESET,
2666
                LIST_5TONE_RESET[
2667
                    decode_reset_time])
2668
            line = RadioSetting("_5tone_settings.decode_reset_time",
2669
                                "Decode reset time", list)
2670
            group_5tone.append(line)
2671
        else:
2672
            LOG.debug("Invalid value decode reset time! Disabling.")
2673

    
2674
        # 2 Tone
2675
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2676
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2677

    
2678
        top.append(encode_2tone)
2679
        top.append(decode_2tone)
2680

    
2681
        duration_1st_tone = self._memobj._2tone.duration_1st_tone
2682
        if duration_1st_tone == 255:
2683
            LOG.debug("Duration of first 2 Tone digit is not yet " +
2684
                      "configured. Setting to 600ms")
2685
            duration_1st_tone = 60
2686

    
2687
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2688
            line = RadioSetting("_2tone.duration_1st_tone",
2689
                                "Duration 1st Tone",
2690
                                RadioSettingValueList(LIST_5TONE_DELAY,
2691
                                                      LIST_5TONE_DELAY[
2692
                                                          duration_1st_tone]))
2693
            encode_2tone.append(line)
2694

    
2695
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2696
        if duration_2nd_tone == 255:
2697
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2698
                      "configured. Setting to 600ms")
2699
            duration_2nd_tone = 60
2700

    
2701
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2702
            line = RadioSetting("_2tone.duration_2nd_tone",
2703
                                "Duration 2nd Tone",
2704
                                RadioSettingValueList(LIST_5TONE_DELAY,
2705
                                                      LIST_5TONE_DELAY[
2706
                                                          duration_2nd_tone]))
2707
            encode_2tone.append(line)
2708

    
2709
        duration_gap = self._memobj._2tone.duration_gap
2710
        if duration_gap == 255:
2711
            LOG.debug("Duration of gap is not yet " +
2712
                      "configured. Setting to 300ms")
2713
            duration_gap = 30
2714

    
2715
        if duration_gap <= len(LIST_5TONE_DELAY):
2716
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2717
                                RadioSettingValueList(LIST_5TONE_DELAY,
2718
                                                      LIST_5TONE_DELAY[
2719
                                                          duration_gap]))
2720
            encode_2tone.append(line)
2721

    
2722
        def _2tone_validate(value):
2723
            if value == 0:
2724
                return 65535
2725
            if value == 65535:
2726
                return value
2727
            if not (300 <= value and value <= 3000):
2728
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
2729
                raise InvalidValueError(msg)
2730
            return value
2731

    
2732
        def apply_2tone_freq(setting, obj):
2733
            val = int(setting.value)
2734
            if (val == 0) or (val == 65535):
2735
                obj.set_value(65535)
2736
            else:
2737
                obj.set_value(val)
2738

    
2739
        i = 1
2740
        for code in self._memobj._2tone._2tone_encode:
2741
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
2742
                                           "Encode Code " + str(i))
2743
            encode_2tone.append(code_2tone)
2744

    
2745
            tmp = code.freq1
2746
            if tmp == 65535:
2747
                tmp = 0
2748
            val1 = RadioSettingValueInteger(0, 65535, tmp)
2749
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
2750
                                 "Frequency 1", val1)
2751
            val1.set_validate_callback(_2tone_validate)
2752
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
2753
            code_2tone.append(freq1)
2754

    
2755
            tmp = code.freq2
2756
            if tmp == 65535:
2757
                tmp = 0
2758
            val2 = RadioSettingValueInteger(0, 65535, tmp)
2759
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
2760
                                 "Frequency 2", val2)
2761
            val2.set_validate_callback(_2tone_validate)
2762
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
2763
            code_2tone.append(freq2)
2764

    
2765
            i = i + 1
2766

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

    
2782
        def apply_2tone_freq_pair(setting, obj):
2783
            val = int(setting.value)
2784
            derived_val = 65535
2785
            frqname = str(setting._name[-5:])
2786
            derivedname = "derived_from_" + frqname
2787

    
2788
            if (val == 0):
2789
                val = 65535
2790
                derived_val = 65535
2791
            else:
2792
                derived_val = int(round(2304000.0/val))
2793

    
2794
            obj[frqname].set_value(val)
2795
            obj[derivedname].set_value(derived_val)
2796

    
2797
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
2798
                      derivedname + ": " + str(derived_val))
2799

    
2800
        i = 1
2801
        for decode_code in self._memobj._2tone._2tone_decode:
2802
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
2803
                                                "Decode Code " + str(i))
2804
            decode_2tone.append(_2tone_dec_code)
2805

    
2806
            j = 1
2807
            for dec in decode_code.decs:
2808
                val = dec.dec
2809
                if val == 255:
2810
                    LOG.debug("Dec for Code " + str(i) + " Dec " + str(j) +
2811
                              " is not yet configured. Setting to 0.")
2812
                    val = 0
2813

    
2814
                if val <= len(LIST_2TONE_DEC):
2815
                    line = RadioSetting(
2816
                        "_2tone_dec_settings_" + str(i) + "_dec_" + str(j),
2817
                        "Dec " + str(j), RadioSettingValueList
2818
                        (LIST_2TONE_DEC,
2819
                         LIST_2TONE_DEC[val]))
2820
                    line.set_apply_callback(apply_list_value, dec.dec)
2821
                    _2tone_dec_code.append(line)
2822
                else:
2823
                    LOG.debug("Invalid value for 2tone dec! Disabling.")
2824

    
2825
                val = dec.response
2826
                if val == 255:
2827
                    LOG.debug("Response for Code " + str(i) + " Dec " +
2828
                              str(j) + " is not yet configured. Setting to 0.")
2829
                    val = 0
2830

    
2831
                if val <= len(LIST_2TONE_RESPONSE):
2832
                    line = RadioSetting(
2833
                        "_2tone_dec_settings_" + str(i) + "_resp_" + str(j),
2834
                        "Response " + str(j), RadioSettingValueList
2835
                        (LIST_2TONE_RESPONSE,
2836
                         LIST_2TONE_RESPONSE[val]))
2837
                    line.set_apply_callback(apply_list_value, dec.response)
2838
                    _2tone_dec_code.append(line)
2839
                else:
2840
                    LOG.debug("Invalid value for 2tone response! Disabling.")
2841

    
2842
                val = dec.alert
2843
                if val == 255:
2844
                    LOG.debug("Alert for Code " + str(i) + " Dec " + str(j) +
2845
                              " is not yet configured. Setting to 0.")
2846
                    val = 0
2847

    
2848
                if val <= len(PTTIDCODE_LIST):
2849
                    line = RadioSetting(
2850
                        "_2tone_dec_settings_" + str(i) + "_alert_" + str(j),
2851
                        "Alert " + str(j), RadioSettingValueList
2852
                        (PTTIDCODE_LIST,
2853
                         PTTIDCODE_LIST[val]))
2854
                    line.set_apply_callback(apply_list_value, dec.alert)
2855
                    _2tone_dec_code.append(line)
2856
                else:
2857
                    LOG.debug("Invalid value for 2tone alert! Disabling.")
2858
                j = j + 1
2859

    
2860
            freq = self._memobj._2tone.freqs[i-1]
2861
            for char in ['A', 'B', 'C', 'D']:
2862
                setting_name = "freq" + str(char)
2863

    
2864
                tmp = freq[setting_name]
2865
                if tmp == 65535:
2866
                    tmp = 0
2867
                if tmp != 0:
2868
                    expected = int(round(2304000.0/tmp))
2869
                    from_mem = freq["derived_from_" + setting_name]
2870
                    if expected != from_mem:
2871
                        LOG.error("Expected " + str(expected) +
2872
                                  " but read " + str(from_mem) +
2873
                                  ". Disabling 2Tone Decode Freqs!")
2874
                        break
2875
                val = RadioSettingValueInteger(0, 65535, tmp)
2876
                frq = RadioSetting("2tone_dec_" + str(i) + "_freq" + str(char),
2877
                                   ("Decode Frequency " + str(char)), val)
2878
                val.set_validate_callback(_2tone_validate)
2879
                frq.set_apply_callback(apply_2tone_freq_pair, freq)
2880
                _2tone_dec_code.append(frq)
2881

    
2882
            i = i + 1
2883

    
2884
        return top
2885

    
2886
    def set_settings(self, settings):
2887
        _settings = self._memobj.settings
2888
        for element in settings:
2889
            if not isinstance(element, RadioSetting):
2890
                if element.get_name() == "fm_preset":
2891
                    self._set_fm_preset(element)
2892
                else:
2893
                    self.set_settings(element)
2894
                    continue
2895
            else:
2896
                try:
2897
                    name = element.get_name()
2898
                    if "." in name:
2899
                        bits = name.split(".")
2900
                        obj = self._memobj
2901
                        for bit in bits[:-1]:
2902
                            if "/" in bit:
2903
                                bit, index = bit.split("/", 1)
2904
                                index = int(index)
2905
                                obj = getattr(obj, bit)[index]
2906
                            else:
2907
                                obj = getattr(obj, bit)
2908
                        setting = bits[-1]
2909
                    else:
2910
                        obj = _settings
2911
                        setting = element.get_name()
2912

    
2913
                    if element.has_apply_callback():
2914
                        LOG.debug("Using apply callback")
2915
                        element.run_apply_callback()
2916
                    elif element.value.get_mutable():
2917
                        LOG.debug("Setting %s = %s" % (setting, element.value))
2918
                        setattr(obj, setting, element.value)
2919
                except Exception, e:
2920
                    LOG.debug(element.get_name())
2921
                    raise
2922

    
2923
    @classmethod
2924
    def match_model(cls, filedata, filename):
2925
        match_size = False
2926
        match_model = False
2927

    
2928
        # testing the file data size
2929
        if len(filedata) == MEM_SIZE:
2930
            match_size = True
2931

    
2932
        # testing the firmware model fingerprint
2933
        match_model = model_match(cls, filedata)
2934

    
2935
        if match_size and match_model:
2936
            return True
2937
        else:
2938
            return False
2939

    
2940

    
2941
MEM_FORMAT = """
2942
#seekto 0x0000;
2943
struct {
2944
  lbcd rxfreq[4];
2945
  lbcd txfreq[4];
2946
  ul16 rxtone;
2947
  ul16 txtone;
2948
  u8 unknown0:4,
2949
     scode:4;
2950
  u8 unknown1:2,
2951
     spmute:1,
2952
     unknown2:3,
2953
     optsig:2;
2954
  u8 unknown3:3,
2955
     scramble:1,
2956
     unknown4:3,
2957
     power:1;
2958
  u8 unknown5:1,
2959
     wide:1,
2960
     unknown6:2,
2961
     bcl:1,
2962
     add:1,
2963
     pttid:2;
2964
} memory[200];
2965

    
2966
#seekto 0x0E00;
2967
struct {
2968
  u8 tdr;
2969
  u8 unknown1;
2970
  u8 sql;
2971
  u8 unknown2[2];
2972
  u8 tot;
2973
  u8 apo;           // BTech radios use this as the Auto Power Off time
2974
                    // other radios use this as pre-Time Out Alert
2975
  u8 unknown3;
2976
  u8 abr;
2977
  u8 beep;
2978
  u8 unknown4[4];
2979
  u8 dtmfst;
2980
  u8 unknown5[2];
2981
  u8 prisc;
2982
  u8 prich;
2983
  u8 screv;
2984
  u8 unknown6[2];
2985
  u8 pttid;
2986
  u8 pttlt;
2987
  u8 unknown7;
2988
  u8 emctp;
2989
  u8 emcch;
2990
  u8 ringt;
2991
  u8 unknown8;
2992
  u8 camdf;
2993
  u8 cbmdf;
2994
  u8 sync;          // BTech radios use this as the display sync setting
2995
                    // other radios use this as the auto keypad lock setting
2996
  u8 ponmsg;
2997
  u8 wtled;
2998
  u8 rxled;
2999
  u8 txled;
3000
  u8 unknown9[5];
3001
  u8 anil;
3002
  u8 reps;
3003
  u8 repm;
3004
  u8 tdrab;
3005
  u8 ste;
3006
  u8 rpste;
3007
  u8 rptdl;
3008
  u8 mgain;
3009
  u8 dtmfg;
3010
} settings;
3011

    
3012
#seekto 0x0E80;
3013
struct {
3014
  u8 unknown1;
3015
  u8 vfomr;
3016
  u8 keylock;
3017
  u8 unknown2;
3018
  u8 unknown3:4,
3019
     vfomren:1,
3020
     unknown4:1,
3021
     reseten:1,
3022
     menuen:1;
3023
  u8 unknown5[11];
3024
  u8 dispab;
3025
  u8 mrcha;
3026
  u8 mrchb;
3027
  u8 menu;
3028
} settings2;
3029

    
3030
#seekto 0x0EC0;
3031
struct {
3032
  char line1[6];
3033
  char line2[6];
3034
} poweron_msg;
3035

    
3036
struct settings_vfo {
3037
  u8 freq[8];
3038
  u8 offset[6];
3039
  u8 unknown2[2];
3040
  ul16 rxtone;
3041
  ul16 txtone;
3042
  u8 scode;
3043
  u8 spmute;
3044
  u8 optsig;
3045
  u8 scramble;
3046
  u8 wide;
3047
  u8 power;
3048
  u8 shiftd;
3049
  u8 step;
3050
  u8 unknown3[4];
3051
};
3052

    
3053
#seekto 0x0F00;
3054
struct {
3055
  struct settings_vfo a;
3056
  struct settings_vfo b;
3057
} vfo;
3058

    
3059
#seekto 0x1000;
3060
struct {
3061
  char name[6];
3062
  u8 unknown1[10];
3063
} names[200];
3064

    
3065
#seekto 0x2400;
3066
struct {
3067
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3068
  u8 group_tone;
3069
  u8 repeat_tone;
3070
  u8 unused[13];
3071
} _5tone_std_settings[15];
3072

    
3073
#seekto 0x2500;
3074
struct {
3075
  u8 frame1[5];
3076
  u8 frame2[5];
3077
  u8 frame3[5];
3078
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3079
} _5tone_codes[15];
3080

    
3081
#seekto 0x25F0;
3082
struct {
3083
  u8 _5tone_delay1; // * 10ms
3084
  u8 _5tone_delay2; // * 10ms
3085
  u8 _5tone_delay3; // * 10ms
3086
  u8 _5tone_first_digit_ext_length;
3087
  u8 unknown1;
3088
  u8 unknown2;
3089
  u8 unknown3;
3090
  u8 unknown4;
3091
  u8 decode_standard;
3092
  u8 unknown5:5,
3093
     _5tone_decode_call_frame3:1,
3094
     _5tone_decode_call_frame2:1,
3095
     _5tone_decode_call_frame1:1;
3096
  u8 unknown6:5,
3097
     _5tone_decode_disp_frame3:1,
3098
     _5tone_decode_disp_frame2:1,
3099
     _5tone_decode_disp_frame1:1;
3100
  u8 decode_reset_time; // * 100 + 100ms
3101
} _5tone_settings;
3102

    
3103
#seekto 0x2900;
3104
struct {
3105
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3106
} dtmf_codes[15];
3107

    
3108
#seekto 0x29F0;
3109
struct {
3110
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3111
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3112
  u8 unknown0[14];
3113
  u8 inspection[16];
3114
  u8 monitor[16];
3115
  u8 alarmcode[16];
3116
  u8 stun[16];
3117
  u8 kill[16];
3118
  u8 revive[16];
3119
  u8 unknown1[16];
3120
  u8 unknown2[16];
3121
  u8 unknown3[16];
3122
  u8 unknown4[16];
3123
  u8 unknown5[16];
3124
  u8 unknown6[16];
3125
  u8 unknown7[16];
3126
  u8 masterid[16];
3127
  u8 viceid[16];
3128
  u8 unused01:7,
3129
     mastervice:1;
3130
  u8 unused02:3,
3131
     mrevive:1,
3132
     mkill:1,
3133
     mstun:1,
3134
     mmonitor:1,
3135
     minspection:1;
3136
  u8 unused03:3,
3137
     vrevive:1,
3138
     vkill:1,
3139
     vstun:1,
3140
     vmonitor:1,
3141
     vinspection:1;
3142
  u8 unused04:6,
3143
     txdisable:1,
3144
     rxdisable:1;
3145
  u8 groupcode;
3146
  u8 spacecode;
3147
  u8 delayproctime; // * 100 + 100ms
3148
  u8 resettime;     // * 100 + 100ms
3149
} dtmf_settings;
3150

    
3151
#seekto 0x2D00;
3152
struct {
3153
  struct {
3154
    ul16 freq1;
3155
    u8 unused01[6];
3156
    ul16 freq2;
3157
    u8 unused02[6];
3158
  } _2tone_encode[15];
3159
  u8 duration_1st_tone; // *10ms
3160
  u8 duration_2nd_tone; // *10ms
3161
  u8 duration_gap;      // *10ms
3162
  u8 unused03[13];
3163
  struct {
3164
    struct {
3165
      u8 dec;      // one out of LIST_2TONE_DEC
3166
      u8 response; // one out of LIST_2TONE_RESPONSE
3167
      u8 alert;    // 1-16
3168
    } decs[4];
3169
    u8 unused04[4];
3170
  } _2tone_decode[15];
3171
  u8 unused05[16];
3172

    
3173
  struct {
3174
    ul16 freqA;
3175
    ul16 freqB;
3176
    ul16 freqC;
3177
    ul16 freqD;
3178
    // unknown what those values mean, but they are
3179
    // derived from configured frequencies
3180
    ul16 derived_from_freqA; // 2304000/freqA
3181
    ul16 derived_from_freqB; // 2304000/freqB
3182
    ul16 derived_from_freqC; // 2304000/freqC
3183
    ul16 derived_from_freqD; // 2304000/freqD
3184
  }freqs[15];
3185
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3186
} _2tone;
3187

    
3188
#seekto 0x3000;
3189
struct {
3190
  u8 freq[8];
3191
  char broadcast_station_name[6];
3192
  u8 unknown[2];
3193
} fm_radio_preset[16];
3194

    
3195
#seekto 0x3C90;
3196
struct {
3197
  u8 vhf_low[3];
3198
  u8 vhf_high[3];
3199
  u8 uhf_low[3];
3200
  u8 uhf_high[3];
3201
} ranges;
3202

    
3203
// the UV-2501+220 & KT8900R has different zones for storing ranges
3204

    
3205
#seekto 0x3CD0;
3206
struct {
3207
  u8 vhf_low[3];
3208
  u8 vhf_high[3];
3209
  u8 unknown1[4];
3210
  u8 unknown2[6];
3211
  u8 vhf2_low[3];
3212
  u8 vhf2_high[3];
3213
  u8 unknown3[4];
3214
  u8 unknown4[6];
3215
  u8 uhf_low[3];
3216
  u8 uhf_high[3];
3217
} ranges220;
3218

    
3219
#seekto 0x3F70;
3220
struct {
3221
  char fp[6];
3222
} fingerprint;
3223

    
3224
"""
3225

    
3226

    
3227
class BTech(BTechMobileCommon):
3228
    """BTECH's UV-5001 and alike radios"""
3229
    BANDS = 2
3230
    COLOR_LCD = False
3231
    NAME_LENGTH = 6
3232

    
3233
    def set_options(self):
3234
        """This is to read the options from the image and set it in the
3235
        environment, for now just the limits of the freqs in the VHF/UHF
3236
        ranges"""
3237

    
3238
        # setting the correct ranges for each radio type
3239
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3240
            # the model 2501+220 has a segment in 220
3241
            # and a different position in the memmap
3242
            # also the QYT KT8900R
3243
            ranges = self._memobj.ranges220
3244
        else:
3245
            ranges = self._memobj.ranges
3246

    
3247
        # the normal dual bands
3248
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3249
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3250

    
3251
        # DEBUG
3252
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3253
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3254

    
3255
        # 220Mhz radios case
3256
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3257
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3258
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3259
            self._220_range = vhf2
3260

    
3261
        # set the class with the real data
3262
        self._vhf_range = vhf
3263
        self._uhf_range = uhf
3264

    
3265
    def process_mmap(self):
3266
        """Process the mem map into the mem object"""
3267

    
3268
        # Get it
3269
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3270

    
3271
        # load specific parameters from the radio image
3272
        self.set_options()
3273

    
3274

    
3275
# Declaring Aliases (Clones of the real radios)
3276
class JT2705M(chirp_common.Alias):
3277
    VENDOR = "Jetstream"
3278
    MODEL = "JT2705M"
3279

    
3280

    
3281
class JT6188Mini(chirp_common.Alias):
3282
    VENDOR = "Juentai"
3283
    MODEL = "JT-6188 Mini"
3284

    
3285

    
3286
class JT6188Plus(chirp_common.Alias):
3287
    VENDOR = "Juentai"
3288
    MODEL = "JT-6188 Plus"
3289

    
3290

    
3291
class SSGT890(chirp_common.Alias):
3292
    VENDOR = "Sainsonic"
3293
    MODEL = "GT-890"
3294

    
3295

    
3296
class ZastoneMP300(chirp_common.Alias):
3297
    VENDOR = "Zastone"
3298
    MODEL = "MP-300"
3299

    
3300

    
3301
# real radios
3302
@directory.register
3303
class UV2501(BTech):
3304
    """Baofeng Tech UV2501"""
3305
    MODEL = "UV-2501"
3306
    _fileid = [UV2501G3_fp,
3307
               UV2501G2_fp,
3308
               UV2501pp2_fp,
3309
               UV2501pp_fp]
3310

    
3311

    
3312
@directory.register
3313
class UV2501_220(BTech):
3314
    """Baofeng Tech UV2501+220"""
3315
    MODEL = "UV-2501+220"
3316
    BANDS = 3
3317
    _magic = MSTRING_220
3318
    _id2 = [UV2501_220pp_id, ]
3319
    _fileid = [UV2501_220G3_fp,
3320
               UV2501_220G2_fp,
3321
               UV2501_220_fp,
3322
               UV2501_220pp_fp]
3323

    
3324

    
3325
@directory.register
3326
class UV5001(BTech):
3327
    """Baofeng Tech UV5001"""
3328
    MODEL = "UV-5001"
3329
    _fileid = [UV5001G3_fp,
3330
               UV5001G22_fp,
3331
               UV5001G2_fp,
3332
               UV5001alpha_fp,
3333
               UV5001pp_fp]
3334
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3335
                     chirp_common.PowerLevel("Low", watts=10)]
3336

    
3337

    
3338
@directory.register
3339
class MINI8900(BTech):
3340
    """WACCOM MINI-8900"""
3341
    VENDOR = "WACCOM"
3342
    MODEL = "MINI-8900"
3343
    _magic = MSTRING_MINI8900
3344
    _fileid = [MINI8900_fp, ]
3345
    # Clones
3346
    ALIASES = [JT6188Plus, ]
3347

    
3348

    
3349
@directory.register
3350
class KTUV980(BTech):
3351
    """QYT KT-UV980"""
3352
    VENDOR = "QYT"
3353
    MODEL = "KT-UV980"
3354
    _vhf_range = (136000000, 175000000)
3355
    _uhf_range = (400000000, 481000000)
3356
    _magic = MSTRING_MINI8900
3357
    _fileid = [KTUV980_fp, ]
3358
    # Clones
3359
    ALIASES = [JT2705M, ]
3360

    
3361
# Please note that there is a version of this radios that is a clone of the
3362
# Waccom Mini8900, maybe an early version?
3363

    
3364

    
3365
@directory.register
3366
class KT9800(BTech):
3367
    """QYT KT8900"""
3368
    VENDOR = "QYT"
3369
    MODEL = "KT8900"
3370
    _vhf_range = (136000000, 175000000)
3371
    _uhf_range = (400000000, 481000000)
3372
    _magic = MSTRING_KT8900
3373
    _fileid = [KT8900_fp,
3374
               KT8900_fp1,
3375
               KT8900_fp2,
3376
               KT8900_fp3,
3377
               KT8900_fp4,
3378
               KT8900_fp5]
3379
    _id2 = [KT8900_id, ]
3380
    # Clones
3381
    ALIASES = [JT6188Mini, SSGT890, ZastoneMP300]
3382

    
3383

    
3384
@directory.register
3385
class KT9800R(BTech):
3386
    """QYT KT8900R"""
3387
    VENDOR = "QYT"
3388
    MODEL = "KT8900R"
3389
    BANDS = 3
3390
    _vhf_range = (136000000, 175000000)
3391
    _220_range = (240000000, 271000000)
3392
    _uhf_range = (400000000, 481000000)
3393
    _magic = MSTRING_KT8900R
3394
    _fileid = [KT8900R_fp,
3395
               KT8900R_fp1,
3396
               KT8900R_fp2,
3397
               KT8900R_fp3,
3398
               KT8900R_fp4]
3399
    _id2 = [KT8900R_id, KT8900R_id2]
3400

    
3401

    
3402
@directory.register
3403
class LT588UV(BTech):
3404
    """LUITON LT-588UV"""
3405
    VENDOR = "LUITON"
3406
    MODEL = "LT-588UV"
3407
    _vhf_range = (136000000, 175000000)
3408
    _uhf_range = (400000000, 481000000)
3409
    _magic = MSTRING_KT8900
3410
    _fileid = [LT588UV_fp,
3411
               LT588UV_fp1]
3412
    _power_levels = [chirp_common.PowerLevel("High", watts=60),
3413
                     chirp_common.PowerLevel("Low", watts=10)]
3414

    
3415

    
3416
COLOR_MEM_FORMAT = """
3417
#seekto 0x0000;
3418
struct {
3419
  lbcd rxfreq[4];
3420
  lbcd txfreq[4];
3421
  ul16 rxtone;
3422
  ul16 txtone;
3423
  u8 unknown0:4,
3424
     scode:4;
3425
  u8 unknown1:2,
3426
     spmute:1,
3427
     unknown2:3,
3428
     optsig:2;
3429
  u8 unknown3:3,
3430
     scramble:1,
3431
     unknown4:3,
3432
     power:1;
3433
  u8 unknown5:1,
3434
     wide:1,
3435
     unknown6:2,
3436
     bcl:1,
3437
     add:1,
3438
     pttid:2;
3439
} memory[200];
3440

    
3441
#seekto 0x0E00;
3442
struct {
3443
  u8 tmr;
3444
  u8 unknown1;
3445
  u8 sql;
3446
  u8 unknown2[2];
3447
  u8 tot;
3448
  u8 apo;
3449
  u8 unknown3;
3450
  u8 abr;
3451
  u8 beep;
3452
  u8 unknown4[4];
3453
  u8 dtmfst;
3454
  u8 unknown5[2];
3455
  u8 screv;
3456
  u8 unknown6[2];
3457
  u8 pttid;
3458
  u8 pttlt;
3459
  u8 unknown7;
3460
  u8 emctp;
3461
  u8 emcch;
3462
  u8 sigbp;
3463
  u8 unknown8;
3464
  u8 camdf;
3465
  u8 cbmdf;
3466
  u8 ccmdf;
3467
  u8 cdmdf;
3468
  u8 langua;
3469
  u8 sync;          // BTech radios use this as the display sync
3470
                    // setting, other radios use this as the auto
3471
                    // keypad lock setting
3472
  u8 mainfc;
3473
  u8 mainbc;
3474
  u8 menufc;
3475
  u8 menubc;
3476
  u8 stafc;
3477
  u8 stabc;
3478
  u8 sigfc;
3479
  u8 sigbc;
3480
  u8 rxfc;
3481
  u8 txfc;
3482
  u8 txdisp;
3483
  u8 unknown9[5];
3484
  u8 anil;
3485
  u8 reps;
3486
  u8 repm;
3487
  u8 tmrmr;
3488
  u8 ste;
3489
  u8 rpste;
3490
  u8 rptdl;
3491
  u8 dtmfg;
3492
  u8 mgain;
3493
  u8 skiptx;
3494
  u8 scmode;
3495
} settings;
3496

    
3497
#seekto 0x0E80;
3498
struct {
3499
  u8 unknown1;
3500
  u8 vfomr;
3501
  u8 keylock;
3502
  u8 unknown2;
3503
  u8 unknown3:4,
3504
     vfomren:1,
3505
     unknown4:1,
3506
     reseten:1,
3507
     menuen:1;
3508
  u8 unknown5[11];
3509
  u8 dispab;
3510
  u8 unknown6[2];
3511
  u8 menu;
3512
  u8 unknown7[7];
3513
  u8 vfomra;
3514
  u8 vfomrb;
3515
  u8 vfomrc;
3516
  u8 vfomrd;
3517
  u8 mrcha;
3518
  u8 mrchb;
3519
  u8 mrchc;
3520
  u8 mrchd;
3521
} settings2;
3522

    
3523
struct settings_vfo {
3524
  u8 freq[8];
3525
  u8 offset[6];
3526
  u8 unknown2[2];
3527
  ul16 rxtone;
3528
  ul16 txtone;
3529
  u8 scode;
3530
  u8 spmute;
3531
  u8 optsig;
3532
  u8 scramble;
3533
  u8 wide;
3534
  u8 power;
3535
  u8 shiftd;
3536
  u8 step;
3537
  u8 unknown3[4];
3538
};
3539

    
3540
#seekto 0x0F00;
3541
struct {
3542
  struct settings_vfo a;
3543
  struct settings_vfo b;
3544
  struct settings_vfo c;
3545
  struct settings_vfo d;
3546
} vfo;
3547

    
3548
#seekto 0x0F80;
3549
struct {
3550
  char line1[8];
3551
  char line2[8];
3552
  char line3[8];
3553
  char line4[8];
3554
  char line5[8];
3555
  char line6[8];
3556
  char line7[8];
3557
  char line8[8];
3558
} poweron_msg;
3559

    
3560
#seekto 0x1000;
3561
struct {
3562
  char name[8];
3563
  u8 unknown1[8];
3564
} names[200];
3565

    
3566
#seekto 0x2400;
3567
struct {
3568
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3569
  u8 group_tone;
3570
  u8 repeat_tone;
3571
  u8 unused[13];
3572
} _5tone_std_settings[15];
3573

    
3574
#seekto 0x2500;
3575
struct {
3576
  u8 frame1[5];
3577
  u8 frame2[5];
3578
  u8 frame3[5];
3579
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3580
} _5tone_codes[15];
3581

    
3582
#seekto 0x25F0;
3583
struct {
3584
  u8 _5tone_delay1; // * 10ms
3585
  u8 _5tone_delay2; // * 10ms
3586
  u8 _5tone_delay3; // * 10ms
3587
  u8 _5tone_first_digit_ext_length;
3588
  u8 unknown1;
3589
  u8 unknown2;
3590
  u8 unknown3;
3591
  u8 unknown4;
3592
  u8 decode_standard;
3593
  u8 unknown5:5,
3594
     _5tone_decode_call_frame3:1,
3595
     _5tone_decode_call_frame2:1,
3596
     _5tone_decode_call_frame1:1;
3597
  u8 unknown6:5,
3598
     _5tone_decode_disp_frame3:1,
3599
     _5tone_decode_disp_frame2:1,
3600
     _5tone_decode_disp_frame1:1;
3601
  u8 decode_reset_time; // * 100 + 100ms
3602
} _5tone_settings;
3603

    
3604
#seekto 0x2900;
3605
struct {
3606
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3607
} dtmf_codes[15];
3608

    
3609
#seekto 0x29F0;
3610
struct {
3611
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3612
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3613
  u8 unknown0[14];
3614
  u8 inspection[16];
3615
  u8 monitor[16];
3616
  u8 alarmcode[16];
3617
  u8 stun[16];
3618
  u8 kill[16];
3619
  u8 revive[16];
3620
  u8 unknown1[16];
3621
  u8 unknown2[16];
3622
  u8 unknown3[16];
3623
  u8 unknown4[16];
3624
  u8 unknown5[16];
3625
  u8 unknown6[16];
3626
  u8 unknown7[16];
3627
  u8 masterid[16];
3628
  u8 viceid[16];
3629
  u8 unused01:7,
3630
     mastervice:1;
3631
  u8 unused02:3,
3632
     mrevive:1,
3633
     mkill:1,
3634
     mstun:1,
3635
     mmonitor:1,
3636
     minspection:1;
3637
  u8 unused03:3,
3638
     vrevive:1,
3639
     vkill:1,
3640
     vstun:1,
3641
     vmonitor:1,
3642
     vinspection:1;
3643
  u8 unused04:6,
3644
     txdisable:1,
3645
     rxdisable:1;
3646
  u8 groupcode;
3647
  u8 spacecode;
3648
  u8 delayproctime; // * 100 + 100ms
3649
  u8 resettime;     // * 100 + 100ms
3650
} dtmf_settings;
3651

    
3652
#seekto 0x2D00;
3653
struct {
3654
  struct {
3655
    ul16 freq1;
3656
    u8 unused01[6];
3657
    ul16 freq2;
3658
    u8 unused02[6];
3659
  } _2tone_encode[15];
3660
  u8 duration_1st_tone; // *10ms
3661
  u8 duration_2nd_tone; // *10ms
3662
  u8 duration_gap;      // *10ms
3663
  u8 unused03[13];
3664
  struct {
3665
    struct {
3666
      u8 dec;      // one out of LIST_2TONE_DEC
3667
      u8 response; // one out of LIST_2TONE_RESPONSE
3668
      u8 alert;    // 1-16
3669
    } decs[4];
3670
    u8 unused04[4];
3671
  } _2tone_decode[15];
3672
  u8 unused05[16];
3673

    
3674
  struct {
3675
    ul16 freqA;
3676
    ul16 freqB;
3677
    ul16 freqC;
3678
    ul16 freqD;
3679
    // unknown what those values mean, but they are
3680
    // derived from configured frequencies
3681
    ul16 derived_from_freqA; // 2304000/freqA
3682
    ul16 derived_from_freqB; // 2304000/freqB
3683
    ul16 derived_from_freqC; // 2304000/freqC
3684
    ul16 derived_from_freqD; // 2304000/freqD
3685
  }freqs[15];
3686
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3687
} _2tone;
3688

    
3689
#seekto 0x3D80;
3690
struct {
3691
  u8 vhf_low[3];
3692
  u8 vhf_high[3];
3693
  u8 unknown1[4];
3694
  u8 unknown2[6];
3695
  u8 vhf2_low[3];
3696
  u8 vhf2_high[3];
3697
  u8 unknown3[4];
3698
  u8 unknown4[6];
3699
  u8 uhf_low[3];
3700
  u8 uhf_high[3];
3701
  u8 unknown5[4];
3702
  u8 unknown6[6];
3703
  u8 uhf2_low[3];
3704
  u8 uhf2_high[3];
3705
} ranges;
3706

    
3707
#seekto 0x3F70;
3708
struct {
3709
  char fp[6];
3710
} fingerprint;
3711

    
3712
"""
3713

    
3714

    
3715
class BTechColor(BTechMobileCommon):
3716
    """BTECH's Color LCD Mobile and alike radios"""
3717
    COLOR_LCD = True
3718
    NAME_LENGTH = 8
3719
    LIST_TMR = LIST_TMR16
3720

    
3721
    def process_mmap(self):
3722
        """Process the mem map into the mem object"""
3723

    
3724
        # Get it
3725
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
3726

    
3727
        # load specific parameters from the radio image
3728
        self.set_options()
3729

    
3730
    def set_options(self):
3731
        """This is to read the options from the image and set it in the
3732
        environment, for now just the limits of the freqs in the VHF/UHF
3733
        ranges"""
3734

    
3735
        # setting the correct ranges for each radio type
3736
        ranges = self._memobj.ranges
3737

    
3738
        # the normal dual bands
3739
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3740
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3741

    
3742
        # DEBUG
3743
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3744
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3745

    
3746
        # the additional bands
3747
        if self.MODEL in ["UV-25X4", "KT7900D"]:
3748
            # 200Mhz band
3749
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3750
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3751
            self._220_range = vhf2
3752

    
3753
            # 350Mhz band
3754
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
3755
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
3756
            self._350_range = uhf2
3757

    
3758
        # set the class with the real data
3759
        self._vhf_range = vhf
3760
        self._uhf_range = uhf
3761

    
3762

    
3763
# Declaring Aliases (Clones of the real radios)
3764
class SKT8900D(chirp_common.Alias):
3765
    VENDOR = "Surecom"
3766
    MODEL = "S-KT8900D"
3767

    
3768

    
3769
class QB25(chirp_common.Alias):
3770
    VENDOR = "Radioddity"
3771
    MODEL = "QB25"
3772

    
3773

    
3774
# real radios
3775
@directory.register
3776
class UV25X2(BTechColor):
3777
    """Baofeng Tech UV25X2"""
3778
    MODEL = "UV-25X2"
3779
    BANDS = 2
3780
    _vhf_range = (130000000, 180000000)
3781
    _uhf_range = (400000000, 521000000)
3782
    _magic = MSTRING_UV25X2
3783
    _fileid = [UV25X2_fp, ]
3784

    
3785

    
3786
@directory.register
3787
class UV25X4(BTechColor):
3788
    """Baofeng Tech UV25X4"""
3789
    MODEL = "UV-25X4"
3790
    BANDS = 4
3791
    _vhf_range = (130000000, 180000000)
3792
    _220_range = (200000000, 271000000)
3793
    _uhf_range = (400000000, 521000000)
3794
    _350_range = (350000000, 391000000)
3795
    _magic = MSTRING_UV25X4
3796
    _fileid = [UV25X4_fp, ]
3797

    
3798

    
3799
@directory.register
3800
class UV50X2(BTechColor):
3801
    """Baofeng Tech UV50X2"""
3802
    MODEL = "UV-50X2"
3803
    BANDS = 2
3804
    _vhf_range = (130000000, 180000000)
3805
    _uhf_range = (400000000, 521000000)
3806
    _magic = MSTRING_UV25X2
3807
    _fileid = [UV50X2_fp, ]
3808
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3809
                     chirp_common.PowerLevel("Low", watts=10)]
3810

    
3811

    
3812
@directory.register
3813
class KT7900D(BTechColor):
3814
    """QYT KT7900D"""
3815
    VENDOR = "QYT"
3816
    MODEL = "KT7900D"
3817
    BANDS = 4
3818
    LIST_TMR = LIST_TMR15
3819
    _vhf_range = (136000000, 175000000)
3820
    _220_range = (200000000, 271000000)
3821
    _uhf_range = (400000000, 481000000)
3822
    _350_range = (350000000, 371000000)
3823
    _magic = MSTRING_KT8900D
3824
    _fileid = [KT7900D_fp, KT7900D_fp1, KT7900D_fp2, KT7900D_fp3, KT7900D_fp4,
3825
               KT7900D_fp5, QB25_fp, ]
3826
    # Clones
3827
    ALIASES = [SKT8900D, QB25, ]
3828

    
3829

    
3830
@directory.register
3831
class KT8900D(BTechColor):
3832
    """QYT KT8900D"""
3833
    VENDOR = "QYT"
3834
    MODEL = "KT8900D"
3835
    BANDS = 2
3836
    LIST_TMR = LIST_TMR15
3837
    _vhf_range = (136000000, 175000000)
3838
    _uhf_range = (400000000, 481000000)
3839
    _magic = MSTRING_KT8900D
3840
    _fileid = [KT8900D_fp, KT8900D_fp1]
3841

    
3842

    
3843
GMRS_MEM_FORMAT = """
3844
#seekto 0x0000;
3845
struct {
3846
  lbcd rxfreq[4];
3847
  lbcd txfreq[4];
3848
  ul16 rxtone;
3849
  ul16 txtone;
3850
  u8 unknown0:4,
3851
     scode:4;
3852
  u8 unknown1:2,
3853
     spmute:1,
3854
     unknown2:3,
3855
     optsig:2;
3856
  u8 unknown3:3,
3857
     scramble:1,
3858
     unknown4:2,
3859
     power:2;
3860
  u8 unknown5:1,
3861
     wide:1,
3862
     unknown6:2,
3863
     bcl:1,
3864
     add:1,
3865
     pttid:2;
3866
} memory[256];
3867

    
3868
#seekto 0x1000;
3869
struct {
3870
  char name[7];
3871
  u8 unknown1[9];
3872
} names[256];
3873

    
3874
#seekto 0x2400;
3875
struct {
3876
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3877
  u8 group_tone;
3878
  u8 repeat_tone;
3879
  u8 unused[13];
3880
} _5tone_std_settings[15];
3881

    
3882
#seekto 0x2500;
3883
struct {
3884
  u8 frame1[5];
3885
  u8 frame2[5];
3886
  u8 frame3[5];
3887
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3888
} _5tone_codes[15];
3889

    
3890
#seekto 0x25F0;
3891
struct {
3892
  u8 _5tone_delay1; // * 10ms
3893
  u8 _5tone_delay2; // * 10ms
3894
  u8 _5tone_delay3; // * 10ms
3895
  u8 _5tone_first_digit_ext_length;
3896
  u8 unknown1;
3897
  u8 unknown2;
3898
  u8 unknown3;
3899
  u8 unknown4;
3900
  u8 decode_standard;
3901
  u8 unknown5:5,
3902
     _5tone_decode_call_frame3:1,
3903
     _5tone_decode_call_frame2:1,
3904
     _5tone_decode_call_frame1:1;
3905
  u8 unknown6:5,
3906
     _5tone_decode_disp_frame3:1,
3907
     _5tone_decode_disp_frame2:1,
3908
     _5tone_decode_disp_frame1:1;
3909
  u8 decode_reset_time; // * 100 + 100ms
3910
} _5tone_settings;
3911

    
3912
#seekto 0x2900;
3913
struct {
3914
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3915
} dtmf_codes[15];
3916

    
3917
#seekto 0x29F0;
3918
struct {
3919
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3920
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3921
  u8 unknown0[14];
3922
  u8 inspection[16];
3923
  u8 monitor[16];
3924
  u8 alarmcode[16];
3925
  u8 stun[16];
3926
  u8 kill[16];
3927
  u8 revive[16];
3928
  u8 unknown1[16];
3929
  u8 unknown2[16];
3930
  u8 unknown3[16];
3931
  u8 unknown4[16];
3932
  u8 unknown5[16];
3933
  u8 unknown6[16];
3934
  u8 unknown7[16];
3935
  u8 masterid[16];
3936
  u8 viceid[16];
3937
  u8 unused01:7,
3938
     mastervice:1;
3939
  u8 unused02:3,
3940
     mrevive:1,
3941
     mkill:1,
3942
     mstun:1,
3943
     mmonitor:1,
3944
     minspection:1;
3945
  u8 unused03:3,
3946
     vrevive:1,
3947
     vkill:1,
3948
     vstun:1,
3949
     vmonitor:1,
3950
     vinspection:1;
3951
  u8 unused04:6,
3952
     txdisable:1,
3953
     rxdisable:1;
3954
  u8 groupcode;
3955
  u8 spacecode;
3956
  u8 delayproctime; // * 100 + 100ms
3957
  u8 resettime;     // * 100 + 100ms
3958
} dtmf_settings;
3959

    
3960
#seekto 0x2D00;
3961
struct {
3962
  struct {
3963
    ul16 freq1;
3964
    u8 unused01[6];
3965
    ul16 freq2;
3966
    u8 unused02[6];
3967
  } _2tone_encode[15];
3968
  u8 duration_1st_tone; // *10ms
3969
  u8 duration_2nd_tone; // *10ms
3970
  u8 duration_gap;      // *10ms
3971
  u8 unused03[13];
3972
  struct {
3973
    struct {
3974
      u8 dec;      // one out of LIST_2TONE_DEC
3975
      u8 response; // one out of LIST_2TONE_RESPONSE
3976
      u8 alert;    // 1-16
3977
    } decs[4];
3978
    u8 unused04[4];
3979
  } _2tone_decode[15];
3980
  u8 unused05[16];
3981

    
3982
  struct {
3983
    ul16 freqA;
3984
    ul16 freqB;
3985
    ul16 freqC;
3986
    ul16 freqD;
3987
    // unknown what those values mean, but they are
3988
    // derived from configured frequencies
3989
    ul16 derived_from_freqA; // 2304000/freqA
3990
    ul16 derived_from_freqB; // 2304000/freqB
3991
    ul16 derived_from_freqC; // 2304000/freqC
3992
    ul16 derived_from_freqD; // 2304000/freqD
3993
  }freqs[15];
3994
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3995
} _2tone;
3996

    
3997
#seekto 0x3000;
3998
struct {
3999
  u8 freq[8];
4000
  char broadcast_station_name[6];
4001
  u8 unknown[2];
4002
} fm_radio_preset[16];
4003

    
4004
#seekto 0x3200;
4005
struct {
4006
  u8 tmr;
4007
  u8 unknown1;
4008
  u8 sql;
4009
  u8 unknown2;
4010
  u8 autolk;
4011
  u8 tot;
4012
  u8 apo;
4013
  u8 unknown3;
4014
  u8 abr;
4015
  u8 beep;
4016
  u8 unknown4[4];
4017
  u8 dtmfst;
4018
  u8 unknown5[2];
4019
  u8 screv;
4020
  u8 unknown6[2];
4021
  u8 pttid;
4022
  u8 pttlt;
4023
  u8 unknown7;
4024
  u8 emctp;
4025
  u8 emcch;
4026
  u8 sigbp;
4027
  u8 unknown8;
4028
  u8 camdf;
4029
  u8 cbmdf;
4030
  u8 ccmdf;
4031
  u8 cdmdf;
4032
  u8 langua;
4033
  u8 sync;
4034

    
4035

    
4036
  u8 stfc;
4037
  u8 mffc;
4038
  u8 sfafc;
4039
  u8 sfbfc;
4040
  u8 sfcfc;
4041
  u8 sfdfc;
4042
  u8 subfc;
4043
  u8 fmfc;
4044
  u8 sigfc;
4045
  u8 modfc;
4046
  u8 menufc;
4047
  u8 txfc;
4048
  u8 txdisp;
4049
  u8 unknown9[5];
4050
  u8 anil;
4051
  u8 reps;
4052
  u8 repm;
4053
  u8 tmrmr;
4054
  u8 ste;
4055
  u8 rpste;
4056
  u8 rptdl;
4057
  u8 dtmfg;
4058
  u8 mgain;
4059
  u8 skiptx;
4060
  u8 scmode;
4061
} settings;
4062

    
4063
#seekto 0x3280;
4064
struct {
4065
  u8 unknown1;
4066
  u8 vfomr;
4067
  u8 keylock;
4068
  u8 unknown2;
4069
  u8 unknown3:4,
4070
     vfomren:1,
4071
     unknown4:1,
4072
     reseten:1,
4073
     menuen:1;
4074
  u8 unknown5[11];
4075
  u8 dispab;
4076
  u8 unknown6[2];
4077
  u8 smenu;
4078
  u8 unknown7[7];
4079
  u8 vfomra;
4080
  u8 vfomrb;
4081
  u8 vfomrc;
4082
  u8 vfomrd;
4083
  u8 mrcha;
4084
  u8 mrchb;
4085
  u8 mrchc;
4086
  u8 mrchd;
4087
} settings2;
4088

    
4089
struct settings_vfo {
4090
  u8 freq[8];
4091
  u8 offset[6];
4092
  u8 unknown2[2];
4093
  ul16 rxtone;
4094
  ul16 txtone;
4095
  u8 scode;
4096
  u8 spmute;
4097
  u8 optsig;
4098
  u8 scramble;
4099
  u8 wide;
4100
  u8 power;
4101
  u8 shiftd;
4102
  u8 step;
4103
  u8 unknown3[4];
4104
};
4105

    
4106
#seekto 0x3300;
4107
struct {
4108
  struct settings_vfo a;
4109
  struct settings_vfo b;
4110
  struct settings_vfo c;
4111
  struct settings_vfo d;
4112
} vfo;
4113

    
4114
#seekto 0x3D80;
4115
struct {
4116
  u8 vhf_low[3];
4117
  u8 vhf_high[3];
4118
  u8 unknown1[4];
4119
  u8 unknown2[6];
4120
  u8 vhf2_low[3];
4121
  u8 vhf2_high[3];
4122
  u8 unknown3[4];
4123
  u8 unknown4[6];
4124
  u8 uhf_low[3];
4125
  u8 uhf_high[3];
4126
  u8 unknown5[4];
4127
  u8 unknown6[6];
4128
  u8 uhf2_low[3];
4129
  u8 uhf2_high[3];
4130
} ranges;
4131

    
4132
#seekto 0x33B0;
4133
struct {
4134
  char line[16];
4135
} static_msg;
4136

    
4137
#seekto 0x3F70;
4138
struct {
4139
  char fp[6];
4140
} fingerprint;
4141

    
4142
"""
4143

    
4144

    
4145
class BTechGMRS(BTechMobileCommon):
4146
    """BTECH's GMRS Mobile"""
4147
    COLOR_LCD = True
4148
    COLOR_LCD2 = True
4149
    NAME_LENGTH = 7
4150
    UPLOAD_MEM_SIZE = 0X3400
4151

    
4152
    def process_mmap(self):
4153
        """Process the mem map into the mem object"""
4154

    
4155
        # Get it
4156
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
4157

    
4158
        # load specific parameters from the radio image
4159
        self.set_options()
4160

    
4161
    def set_options(self):
4162
        """This is to read the options from the image and set it in the
4163
        environment, for now just the limits of the freqs in the VHF/UHF
4164
        ranges"""
4165

    
4166
        # setting the correct ranges for each radio type
4167
        ranges = self._memobj.ranges
4168

    
4169
        # the normal dual bands
4170
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4171
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4172

    
4173
        # DEBUG
4174
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4175
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4176

    
4177
        # set the class with the real data
4178
        self._vhf_range = vhf
4179
        self._uhf_range = uhf
4180

    
4181

    
4182
# real radios
4183
@directory.register
4184
class GMRS50X1(BTechGMRS):
4185
    """Baofeng Tech GMRS50X1"""
4186
    MODEL = "GMRS-50X1"
4187
    BANDS = 2
4188
    LIST_TMR = LIST_TMR16
4189
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4190
                     chirp_common.PowerLevel("Mid", watts=10),
4191
                     chirp_common.PowerLevel("Low", watts=5)]
4192
    _vhf_range = (136000000, 175000000)
4193
    _uhf_range = (400000000, 521000000)
4194
    _upper = 255
4195
    _magic = MSTRING_GMRS50X1
4196
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
(2-2/3)