Project

General

Profile

Feature #10058 ยป btech_sql.py

Jim Unroe, 09/28/2022 03:10 AM

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

    
18
import time
19
import struct
20
import logging
21

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

    
31
LOG = logging.getLogger(__name__)
32

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

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

    
141
# This is a general serial timeout for all serial read functions.
142
STIMEOUT = 0.25
143

    
144
# this var controls the verbosity in the debug and by default it's low (False)
145
# make it True and you will to get a very verbose debug.log
146
debug = False
147

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

    
152
GMRS_FREQS1 = [462.5625, 462.5875, 462.6125, 462.6375, 462.6625,
153
               462.6875, 462.7125]
154
GMRS_FREQS2 = [467.5625, 467.5875, 467.6125, 467.6375, 467.6625,
155
               467.6875, 467.7125]
156
GMRS_FREQS3 = [462.5500, 462.5750, 462.6000, 462.6250, 462.6500,
157
               462.6750, 462.7000, 462.7250]
158
GMRS_FREQS = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
159

    
160

    
161
# #### ID strings #####################################################
162

    
163
# BTECH UV2501 pre-production units
164
UV2501pp_fp = "M2C294"
165
# BTECH UV2501 pre-production units 2 + and 1st Gen radios
166
UV2501pp2_fp = "M29204"
167
# B-TECH UV-2501 second generation (2G) radios
168
UV2501G2_fp = "BTG214"
169
# B-TECH UV-2501 third generation (3G) radios
170
UV2501G3_fp = "BTG324"
171

    
172
# B-TECH UV-2501+220 pre-production units
173
UV2501_220pp_fp = "M3C281"
174
# B-TECH UV-2501+220
175
UV2501_220_fp = "M3G201"
176
# new variant, let's call it Generation 2
177
UV2501_220G2_fp = "BTG211"
178
# B-TECH UV-2501+220 third generation (3G)
179
UV2501_220G3_fp = "BTG311"
180

    
181
# B-TECH UV-5001 pre-production units + 1st Gen radios
182
UV5001pp_fp = "V19204"
183
# B-TECH UV-5001 alpha units
184
UV5001alpha_fp = "V28204"
185
# B-TECH UV-5001 second generation (2G) radios
186
UV5001G2_fp = "BTG214"
187
# B-TECH UV-5001 second generation (2G2)
188
UV5001G22_fp = "V2G204"
189
# B-TECH UV-5001 third generation (3G)
190
UV5001G3_fp = "BTG304"
191

    
192
# B-TECH UV-25X2
193
UV25X2_fp = "UC2012"
194

    
195
# B-TECH UV-25X4
196
UV25X4_fp = "UC4014"
197

    
198
# B-TECH UV-50X2
199
UV50X2_fp = "UC2M12"
200

    
201
# B-TECH GMRS-50X1
202
GMRS50X1_fp = "NC1802"
203
GMRS50X1_fp1 = "NC1932"
204

    
205
# B-TECH GMRS-20V2
206
GMRS20V2_fp = "WP4204"
207
GMRS20V2_fp1 = "VW2112"
208
GMRS20V2_fp2 = "VWG728"
209

    
210
# special var to know when we found a BTECH Gen 3
211
BTECH3 = [UV2501G3_fp, UV2501_220G3_fp, UV5001G3_fp]
212

    
213

    
214
# WACCOM Mini-8900
215
MINI8900_fp = "M28854"
216

    
217

    
218
# QYT KT-UV980
219
KTUV980_fp = "H28854"
220

    
221
# QYT KT8900
222
KT8900_fp = "M29154"
223
# New generations KT8900
224
KT8900_fp1 = "M2C234"
225
KT8900_fp2 = "M2G1F4"
226
KT8900_fp3 = "M2G2F4"
227
KT8900_fp4 = "M2G304"
228
KT8900_fp5 = "M2G314"
229
KT8900_fp6 = "M2G424"
230
KT8900_fp7 = "M27184"
231

    
232
# KT8900R
233
KT8900R_fp = "M3G1F4"
234
# Second Generation
235
KT8900R_fp1 = "M3G214"
236
# another model
237
KT8900R_fp2 = "M3C234"
238
# another model G4?
239
KT8900R_fp3 = "M39164"
240
# another model
241
KT8900R_fp4 = "M3G314"
242
# AC3MB: another id
243
KT8900R_fp5 = "M3B064"
244

    
245
# KT7900D (quad band)
246
KT7900D_fp = "VC4004"
247
KT7900D_fp1 = "VC4284"
248
KT7900D_fp2 = "VC4264"
249
KT7900D_fp3 = "VC4114"
250
KT7900D_fp4 = "VC4104"
251
KT7900D_fp5 = "VC4254"
252
KT7900D_fp6 = "VC5264"
253
KT7900D_fp7 = "VC9204"
254
KT7900D_fp8 = "VC9214"
255

    
256
# QB25 (quad band) - a clone of KT7900D
257
QB25_fp = "QB-25"
258

    
259
# KT8900D (dual band)
260
KT8900D_fp = "VC2002"
261
KT8900D_fp1 = "VC8632"
262
KT8900D_fp2 = "VC3402"
263
KT8900D_fp3 = "VC7062"
264

    
265
# LUITON LT-588UV
266
LT588UV_fp = "V2G1F4"
267
# Added by rstrickoff gen 2 id
268
LT588UV_fp1 = "V2G214"
269

    
270
# QYT KT-8R (quad band ht)
271
KT8R_fp = "MCB264"
272
KT8R_fp1 = "MCB284"
273
KT8R_fp2 = "MC5264"
274

    
275
# QYT KT5800 (dual band)
276
KT5800_fp = "VCB222"
277

    
278
# QYT KT980Plus (dual band)
279
KT980PLUS_fp = "VC2002"
280
KT980PLUS_fp1 = "VC6042"
281

    
282
# Radioddity DB25-G (gmrs)
283
DB25G_fp = "VC6182"
284
DB25G_fp1 = "VC7062"
285

    
286
# QYT KT-WP12 and KT-9900
287
KTWP12_fp = "WP3094"
288

    
289
# Anysecu WP-9900
290
WP9900_fp = "WP3094"
291

    
292

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

    
318

    
319
def _clean_buffer(radio):
320
    """Cleaning the read serial buffer, hard timeout to survive an infinite
321
    data stream"""
322

    
323
    # touching the serial timeout to optimize the flushing
324
    # restored at the end to the default value
325
    radio.pipe.timeout = 0.1
326
    dump = "1"
327
    datacount = 0
328

    
329
    try:
330
        while len(dump) > 0:
331
            dump = radio.pipe.read(100)
332
            datacount += len(dump)
333
            # hard limit to survive a infinite serial data stream
334
            # 5 times bigger than a normal rx block (69 bytes)
335
            if datacount > 345:
336
                seriale = "Please check your serial port selection."
337
                raise errors.RadioError(seriale)
338

    
339
        # restore the default serial timeout
340
        radio.pipe.timeout = STIMEOUT
341

    
342
    except Exception:
343
        raise errors.RadioError("Unknown error cleaning the serial buffer")
344

    
345

    
346
def _rawrecv(radio, amount):
347
    """Raw read from the radio device, less intensive way"""
348

    
349
    data = ""
350

    
351
    try:
352
        data = radio.pipe.read(amount)
353

    
354
        # DEBUG
355
        if debug is True:
356
            LOG.debug("<== (%d) bytes:\n\n%s" %
357
                      (len(data), util.hexprint(data)))
358

    
359
        # fail if no data is received
360
        if len(data) == 0:
361
            raise errors.RadioError("No data received from radio")
362

    
363
        # notice on the logs if short
364
        if len(data) < amount:
365
            LOG.warn("Short reading %d bytes from the %d requested." %
366
                     (len(data), amount))
367

    
368
    except:
369
        raise errors.RadioError("Error reading data from radio")
370

    
371
    return data
372

    
373

    
374
def _send(radio, data):
375
    """Send data to the radio device"""
376

    
377
    try:
378
        radio.pipe.write(data)
379

    
380
        # DEBUG
381
        if debug is True:
382
            LOG.debug("==> (%d) bytes:\n\n%s" %
383
                      (len(data), util.hexprint(data)))
384
    except:
385
        raise errors.RadioError("Error sending data to radio")
386

    
387

    
388
def _make_frame(cmd, addr, length, data=""):
389
    """Pack the info in the headder format"""
390
    frame = "\x06" + struct.pack(">BHB", ord(cmd), addr, length)
391
    # add the data if set
392
    if len(data) != 0:
393
        frame += data
394

    
395
    return frame
396

    
397

    
398
def _recv(radio, addr):
399
    """Get data from the radio all at once to lower syscalls load"""
400

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

    
404
    # get the whole block
405
    block = _rawrecv(radio, BLOCK_SIZE + 5)
406

    
407
    # basic check
408
    if len(block) < (BLOCK_SIZE + 5):
409
        raise errors.RadioError("Short read of the block 0x%04x" % addr)
410

    
411
    # checking for the ack
412
    if block[0] != ACK_CMD:
413
        raise errors.RadioError("Bad ack from radio in block 0x%04x" % addr)
414

    
415
    # header validation
416
    c, a, l = struct.unpack(">BHB", block[1:5])
417
    if a != addr or l != BLOCK_SIZE or c != ord("X"):
418
        LOG.debug("Invalid header for block 0x%04x" % addr)
419
        LOG.debug("CMD: %s  ADDR: %04x  SIZE: %02x" % (c, a, l))
420
        raise errors.RadioError("Invalid header for block 0x%04x:" % addr)
421

    
422
    # return the data
423
    return block[5:]
424

    
425

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

    
432
    # lengthen the timeout here as these radios are resetting due to timeout
433
    radio.pipe.timeout = 0.75
434

    
435
    # send the magic word
436
    _send(radio, radio._magic)
437

    
438
    # Now you get a 50 byte reply if all goes well
439
    ident = _rawrecv(radio, 50)
440

    
441
    # checking for the ack
442
    if ident[0] != ACK_CMD:
443
        raise errors.RadioError("Bad ack from radio")
444

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

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

    
459
            break
460

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

    
465
    # pause here for the radio to catch up
466
    sleep(0.1)
467

    
468
    # the OEM software reads this additional block, so we will, too
469

    
470
    # Get the full 21 bytes at a time to reduce load
471
    # 1 byte ACK + 4 bytes header + 16 bytes of data (BLOCK_SIZE)
472
    frame = _make_frame("S", 0x3DF0, 16)
473
    _send(radio, frame)
474
    id2 = _rawrecv(radio, 21)
475

    
476
    # restore the default serial timeout
477
    radio.pipe.timeout = STIMEOUT
478

    
479
    # checking for the ack
480
    if id2[0] not in "\x06\x05":
481
        raise errors.RadioError("Bad ack from radio")
482

    
483
    # basic check for the additional block
484
    if len(id2) < 21:
485
        raise errors.RadioError("The extra ID is short, aborting.")
486

    
487
    # this radios need a extra request/answer here on the upload
488
    # the amount of data received depends of the radio type
489
    #
490
    # also the first block of TX must no have the ACK at the beginning
491
    # see _upload for this.
492
    if upload is True:
493
        # send an ACK
494
        _send(radio, ACK_CMD)
495

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

    
508
        # checking
509
        if len(ack) == 0 or ack[-1:] != ACK_CMD:
510
            raise errors.RadioError("Radio didn't ACK the upload")
511

    
512
    # DEBUG
513
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))
514

    
515
    return True
516

    
517

    
518
def _download(radio):
519
    """Get the memory map"""
520

    
521
    # UI progress
522
    status = chirp_common.Status()
523

    
524
    # put radio in program mode and identify it
525
    _do_ident(radio, status)
526

    
527
    # reset the progress bar in the UI
528
    status.max = MEM_SIZE / BLOCK_SIZE
529
    status.msg = "Cloning from radio..."
530
    status.cur = 0
531
    radio.status_fn(status)
532

    
533
    data = ""
534
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
535
        # sending the read request
536
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
537

    
538
        # read
539
        d = _recv(radio, addr)
540

    
541
        # aggregate the data
542
        data += d
543

    
544
        # UI Update
545
        status.cur = addr / BLOCK_SIZE
546
        status.msg = "Cloning from radio..."
547
        radio.status_fn(status)
548

    
549
    return data
550

    
551

    
552
def _upload(radio):
553
    """Upload procedure"""
554

    
555
    # The UPLOAD mem is restricted to lower than 0x3100,
556
    # so we will override that here localy
557
    MEM_SIZE = radio.UPLOAD_MEM_SIZE
558

    
559
    # UI progress
560
    status = chirp_common.Status()
561

    
562
    # put radio in program mode and identify it
563
    _do_ident(radio, status, True)
564

    
565
    # get the data to upload to radio
566
    data = radio.get_mmap()
567

    
568
    # Reset the UI progress
569
    status.max = MEM_SIZE / TX_BLOCK_SIZE
570
    status.cur = 0
571
    status.msg = "Cloning to radio..."
572
    radio.status_fn(status)
573

    
574
    # the fun start here
575
    for addr in range(0, MEM_SIZE, TX_BLOCK_SIZE):
576
        # getting the block of data to send
577
        d = data[addr:addr + TX_BLOCK_SIZE]
578

    
579
        # build the frame to send
580
        frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
581

    
582
        # first block must not send the ACK at the beginning for the
583
        # ones that has the extra id, since this have to do a extra step
584
        if addr == 0:
585
            frame = frame[1:]
586

    
587
        # send the frame
588
        _send(radio, frame)
589

    
590
        # receiving the response
591
        ack = _rawrecv(radio, 1)
592

    
593
        # basic check
594
        if len(ack) != 1:
595
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
596

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

    
600
        # UI Update
601
        status.cur = addr / TX_BLOCK_SIZE
602
        status.msg = "Cloning to radio..."
603
        radio.status_fn(status)
604

    
605

    
606
def model_match(cls, data):
607
    """Match the opened/downloaded image to the correct version"""
608
    rid = data[0x3f70:0x3f76]
609

    
610
    if rid in cls._fileid:
611
        return True
612

    
613
    return False
614

    
615

    
616
def _decode_ranges(low, high):
617
    """Unpack the data in the ranges zones in the memmap and return
618
    a tuple with the integer corresponding to the Mhz it means"""
619
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
620
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
621
    ilow *= 1000000
622
    ihigh *= 1000000
623

    
624
    return (ilow, ihigh)
625

    
626

    
627
def _split(rf, f1, f2):
628
    """Returns False if the two freqs are in the same band (no split)
629
    or True otherwise"""
630

    
631
    # determine if the two freqs are in the same band
632
    for low, high in rf.valid_bands:
633
        if f1 >= low and f1 <= high and \
634
                f2 >= low and f2 <= high:
635
            # if the two freqs are on the same Band this is not a split
636
            return False
637

    
638
    # if you get here is because the freq pairs are split
639
    return True
640

    
641

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

    
668
    @classmethod
669
    def get_prompts(cls):
670
        rp = chirp_common.RadioPrompts()
671
        rp.experimental = \
672
            ('This driver is experimental.\n'
673
             '\n'
674
             'Please keep a copy of your memories with the original software '
675
             'if you treasure them, this driver is new and may contain'
676
             ' bugs.\n'
677
             '\n'
678
             )
679
        rp.pre_download = _(dedent("""\
680
            Follow these instructions to download your info:
681

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

    
687
            """))
688
        rp.pre_upload = _(dedent("""\
689
            Follow these instructions to upload your info:
690

    
691
            1 - Turn off your radio
692
            2 - Connect your interface cable
693
            3 - Turn on your radio
694
            4 - Do the upload of your radio data
695

    
696
            """))
697
        return rp
698

    
699
    def get_features(self):
700
        """Get the radio's features"""
701

    
702
        # we will use the following var as global
703
        global POWER_LEVELS
704

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

    
736
        # power levels
737
        POWER_LEVELS = self._power_levels
738
        rf.valid_power_levels = POWER_LEVELS
739

    
740
        # normal dual bands
741
        rf.valid_bands = [self._vhf_range, self._uhf_range]
742

    
743
        # 220 band
744
        if self.BANDS == 3 or self.BANDS == 4:
745
            rf.valid_bands.append(self._220_range)
746

    
747
        # 350 band
748
        if self.BANDS == 4:
749
            rf.valid_bands.append(self._350_range)
750

    
751
        return rf
752

    
753
    def validate_memory(self, mem):
754
        msgs = chirp_common.CloneModeRadio.validate_memory(self, mem)
755

    
756
        _msg_duplex1 = 'Memory location only supports "Low"'
757
        _msg_duplex2 = 'Memory location only supports "off"'
758
        _msg_duplex3 = 'Memory location only supports "(None)", "+" or "off"'
759

    
760
        return msgs
761

    
762
    def sync_in(self):
763
        """Download from radio"""
764
        data = _download(self)
765
        self._mmap = memmap.MemoryMap(data)
766
        self.process_mmap()
767

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

    
777
    def get_raw_memory(self, number):
778
        return repr(self._memobj.memory[number])
779

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

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

    
798
            tone = DTCS[index]
799
            return 'DTCS', tone, pol
800

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

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

    
830
        # Create a high-level memory object to return to the UI
831
        mem = chirp_common.Memory()
832

    
833
        # Memory number
834
        mem.number = number
835

    
836
        if _mem.get_raw()[0] == "\xFF":
837
            mem.empty = True
838
            return mem
839

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

    
864
        # name TAG of the channel
865
        mem.name = str(_names.name).rstrip("\xFF").replace("\xFF", " ")
866

    
867
        # power
868
        mem.power = POWER_LEVELS[int(_mem.power)]
869

    
870
        # wide/narrow
871
        mem.mode = MODES[int(_mem.wide)]
872

    
873
        # skip
874
        mem.skip = SKIP_VALUES[_mem.add]
875

    
876
        # tone data
877
        rxtone = txtone = None
878
        txtone = self._decode_tone(_mem.txtone)
879
        rxtone = self._decode_tone(_mem.rxtone)
880
        chirp_common.split_tone_decode(mem, txtone, rxtone)
881

    
882
        # Extra
883
        mem.extra = RadioSettingGroup("extra", "Extra")
884

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

    
892
        bcl = RadioSetting("bcl", "Busy channel lockout",
893
                           RadioSettingValueBoolean(bool(_mem.bcl)))
894
        mem.extra.append(bcl)
895

    
896
        pttid = RadioSetting("pttid", "PTT ID",
897
                             RadioSettingValueList(PTTID_LIST,
898
                                                   PTTID_LIST[_mem.pttid]))
899
        mem.extra.append(pttid)
900

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

    
909
        optsig = RadioSetting("optsig", "Optional signaling",
910
                              RadioSettingValueList(
911
                                  OPTSIG_LIST,
912
                                  OPTSIG_LIST[_mem.optsig]))
913
        mem.extra.append(optsig)
914

    
915
        spmute = RadioSetting("spmute", "Speaker mute",
916
                              RadioSettingValueList(
917
                                  SPMUTE_LIST,
918
                                  SPMUTE_LIST[_mem.spmute]))
919
        mem.extra.append(spmute)
920

    
921
        return mem
922

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

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

    
936
        # if empty memory
937
        if mem.empty:
938
            # the channel itself
939
            _mem.set_raw("\xFF" * 16)
940
            # the name tag
941
            _names.set_raw("\xFF" * 16)
942
            return
943

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

    
950
        if self._gmrs:
951
            if self.MODEL == "GMRS-50X1":
952
                if mem.number >= 1 and mem.number <= 30:
953
                    GMRS_FREQ = int(GMRS_FREQS[mem.number - 1] * 1000000)
954
                    mem.freq = GMRS_FREQ
955
                    if mem.number <= 22:
956
                        mem.duplex = ''
957
                        mem.offset = 0
958
                        if mem.number <= 7:
959
                            mem.power = POWER_LEVELS[2]
960
                        if mem.number >= 8 and mem.number <= 14:
961
                            mem.duplex = 'off'
962
                            mem.offset = 0
963
                            mem.mode = 'NFM'
964
                            mem.power = POWER_LEVELS[2]
965
                    if mem.number > 22:
966
                        mem.duplex = '+'
967
                        mem.offset = 5000000
968
                else:
969
                    mem.duplex = 'off'
970
                    mem.offset = 0
971
            if self.MODEL == "GMRS-20V2":
972
                if float(mem.freq) / 1000000 in GMRS_FREQS1:
973
                    mem.duplex = ''
974
                    mem.offset = 0
975
                    mem.power = POWER_LEVELS[2]
976
                elif float(mem.freq) / 1000000 in GMRS_FREQS2:
977
                    mem.duplex = 'off'
978
                    mem.offset = 0
979
                    mem.mode = "NFM"
980
                    mem.power = POWER_LEVELS[2]
981
                elif float(mem.freq) / 1000000 in GMRS_FREQS3:
982
                    if mem.power == POWER_LEVELS[1]:
983
                        mem.power = POWER_LEVELS[0]
984
                    if mem.duplex == '+':
985
                        mem.offset = 5000000
986
                    else:
987
                        mem.duplex == ''
988
                        mem.offset = 0
989
                else:
990
                    mem.duplex = 'off'
991
                    mem.offset = 0
992

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

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

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

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

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

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

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

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

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

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

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

    
1071
        return mem
1072

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

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

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

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

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

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

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

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

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

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

    
1146
        if self.MODEL == "GMRS-20V2":
1147
            rs = RadioSettingValueInteger(0, 58, _mem.settings.volume)
1148
            volume = RadioSetting("settings.volume", "Volume", rs)
1149
            basic.append(volume)
1150

    
1151
        if self.MODEL == "KT-WP12" or self.MODEL == "WP-9900":
1152
            rs = RadioSettingValueInteger(1, 51, _mem.settings.volume + 1)
1153
            volume = RadioSetting("settings.volume", "Volume", rs)
1154
            basic.append(volume)
1155

    
1156
        if self.MODEL == "KT-8R":
1157
            dsub = RadioSetting("settings.dsub", "CTCSS/DCS code display",
1158
                                RadioSettingValueBoolean(
1159
                                    _mem.settings.dsub))
1160
            basic.append(dsub)
1161

    
1162
        if self.MODEL == "KT-8R" or self.COLOR_LCD4:
1163
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1164
                                  RadioSettingValueBoolean(
1165
                                      _mem.settings.dtmfst))
1166
            basic.append(dtmfst)
1167
        else:
1168
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone",
1169
                                  RadioSettingValueList(
1170
                                      LIST_DTMFST,
1171
                                      LIST_DTMFST[_mem.settings.dtmfst]))
1172
            basic.append(dtmfst)
1173

    
1174
        if not self.COLOR_LCD:
1175
            prisc = RadioSetting("settings.prisc", "Priority scan",
1176
                                 RadioSettingValueBoolean(
1177
                                     _mem.settings.prisc))
1178
            basic.append(prisc)
1179

    
1180
            prich = RadioSetting("settings.prich", "Priority channel",
1181
                                 RadioSettingValueInteger(0, self._upper,
1182
                                                          _mem.settings.prich))
1183
            basic.append(prich)
1184

    
1185
        screv = RadioSetting("settings.screv", "Scan resume method",
1186
                             RadioSettingValueList(
1187
                                 LIST_SCREV,
1188
                                 LIST_SCREV[_mem.settings.screv]))
1189
        basic.append(screv)
1190

    
1191
        pttlt = RadioSetting("settings.pttlt", "PTT transmit delay",
1192
                             RadioSettingValueInteger(0, 30,
1193
                                                      _mem.settings.pttlt))
1194
        basic.append(pttlt)
1195

    
1196
        if self.VENDOR == "BTECH" and self.COLOR_LCD and not \
1197
                self.MODEL == "GMRS-20V2":
1198
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1199
                                 RadioSettingValueList(
1200
                                     LIST_EMCTPX,
1201
                                     LIST_EMCTPX[_mem.settings.emctp]))
1202
            basic.append(emctp)
1203
        else:
1204
            emctp = RadioSetting("settings.emctp", "Alarm mode",
1205
                                 RadioSettingValueList(
1206
                                     LIST_EMCTP,
1207
                                     LIST_EMCTP[_mem.settings.emctp]))
1208
            basic.append(emctp)
1209

    
1210
        emcch = RadioSetting("settings.emcch", "Alarm channel",
1211
                             RadioSettingValueInteger(0, self._upper,
1212
                                                      _mem.settings.emcch))
1213
        basic.append(emcch)
1214

    
1215
        if self.COLOR_LCD:
1216
            if _mem.settings.sigbp > 0x01:
1217
                val = 0x00
1218
            else:
1219
                val = _mem.settings.sigbp
1220
            sigbp = RadioSetting("settings.sigbp", "Signal beep",
1221
                                 RadioSettingValueBoolean(val))
1222
            basic.append(sigbp)
1223
        else:
1224
            ringt = RadioSetting("settings.ringt", "Ring time",
1225
                                 RadioSettingValueList(
1226
                                     LIST_OFF1TO9,
1227
                                     LIST_OFF1TO9[_mem.settings.ringt]))
1228
            basic.append(ringt)
1229

    
1230
        camdf = RadioSetting("settings.camdf", "Display mode A",
1231
                             RadioSettingValueList(
1232
                                 LIST_MDF,
1233
                                 LIST_MDF[_mem.settings.camdf]))
1234
        basic.append(camdf)
1235

    
1236
        cbmdf = RadioSetting("settings.cbmdf", "Display mode B",
1237
                             RadioSettingValueList(
1238
                                 LIST_MDF,
1239
                                 LIST_MDF[_mem.settings.cbmdf]))
1240
        basic.append(cbmdf)
1241

    
1242
        if self.COLOR_LCD:
1243
            ccmdf = RadioSetting("settings.ccmdf", "Display mode C",
1244
                                 RadioSettingValueList(
1245
                                     LIST_MDF,
1246
                                     LIST_MDF[_mem.settings.ccmdf]))
1247
            basic.append(ccmdf)
1248

    
1249
            if not self.COLOR_LCD4:
1250
                cdmdf = RadioSetting("settings.cdmdf", "Display mode D",
1251
                                     RadioSettingValueList(
1252
                                         LIST_MDF,
1253
                                         LIST_MDF[_mem.settings.cdmdf]))
1254
                basic.append(cdmdf)
1255

    
1256
                langua = RadioSetting("settings.langua", "Language",
1257
                                      RadioSettingValueList(
1258
                                          LIST_LANGUA,
1259
                                          LIST_LANGUA[_mem.settings.langua]))
1260
                basic.append(langua)
1261

    
1262
        if self.MODEL == "KT-8R":
1263
            voice = RadioSetting("settings.voice", "Voice prompt",
1264
                                 RadioSettingValueList(
1265
                                     LIST_VOICE,
1266
                                     LIST_VOICE[_mem.settings.voice]))
1267
            basic.append(voice)
1268

    
1269
        if self.MODEL == "KT-8R" or self.COLOR_LCD4:
1270
            vox = RadioSetting("settings.vox", "VOX",
1271
                               RadioSettingValueList(
1272
                                   LIST_VOX,
1273
                                   LIST_VOX[_mem.settings.vox]))
1274
            basic.append(vox)
1275

    
1276
            voxt = RadioSetting("settings.voxt", "VOX delay time",
1277
                                RadioSettingValueList(
1278
                                    LIST_VOXT,
1279
                                    LIST_VOXT[_mem.settings.voxt]))
1280
            basic.append(voxt)
1281

    
1282
        if self.VENDOR == "BTECH":
1283
            if self.COLOR_LCD:
1284
                if self.MODEL == "GMRS-20V2":
1285
                    val = RadioSettingValueList(LIST_SYNCV2,
1286
                                                LIST_SYNCV2[
1287
                                                    _mem.settings.sync])
1288
                    sync = RadioSetting("settings.sync",
1289
                                        "Channel display sync", val)
1290
                    basic.append(sync)
1291
                else:
1292
                    val = RadioSettingValueList(LIST_SYNC,
1293
                                                LIST_SYNC[_mem.settings.sync])
1294
                    sync = RadioSetting("settings.sync",
1295
                                        "Channel display sync", val)
1296
                    basic.append(sync)
1297
            else:
1298
                sync = RadioSetting("settings.sync", "A/B channel sync",
1299
                                    RadioSettingValueBoolean(
1300
                                        _mem.settings.sync))
1301
                basic.append(sync)
1302

    
1303
        if self.COLOR_LCD4:
1304
            autolk = RadioSetting("settings.autolock", "Auto keylock",
1305
                                  RadioSettingValueBoolean(
1306
                                      _mem.settings.autolock))
1307
            basic.append(autolk)
1308

    
1309
        if not self.COLOR_LCD:
1310
            ponmsg = RadioSetting("settings.ponmsg", "Power-on message",
1311
                                  RadioSettingValueList(
1312
                                      LIST_PONMSG,
1313
                                      LIST_PONMSG[_mem.settings.ponmsg]))
1314
            basic.append(ponmsg)
1315

    
1316
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3 or
1317
                                   self.COLOR_LCD4):
1318
            mainfc = RadioSetting("settings.mainfc",
1319
                                  "Main LCD foreground color",
1320
                                  RadioSettingValueList(
1321
                                      LIST_COLOR9,
1322
                                      LIST_COLOR9[_mem.settings.mainfc]))
1323
            basic.append(mainfc)
1324

    
1325
            if not self.COLOR_LCD4:
1326
                mainbc = RadioSetting("settings.mainbc",
1327
                                      "Main LCD background color",
1328
                                      RadioSettingValueList(
1329
                                          LIST_COLOR9,
1330
                                          LIST_COLOR9[_mem.settings.mainbc]))
1331
                basic.append(mainbc)
1332

    
1333
            menufc = RadioSetting("settings.menufc", "Menu foreground color",
1334
                                  RadioSettingValueList(
1335
                                      LIST_COLOR9,
1336
                                      LIST_COLOR9[_mem.settings.menufc]))
1337
            basic.append(menufc)
1338

    
1339
            if not self.COLOR_LCD4:
1340
                val = RadioSettingValueList(LIST_COLOR9,
1341
                                            LIST_COLOR9[_mem.settings.menubc])
1342
                menubc = RadioSetting("settings.menubc",
1343
                                      "Menu background color", val)
1344
                basic.append(menubc)
1345

    
1346
            stafc = RadioSetting("settings.stafc",
1347
                                 "Top status foreground color",
1348
                                 RadioSettingValueList(
1349
                                     LIST_COLOR9,
1350
                                     LIST_COLOR9[_mem.settings.stafc]))
1351
            basic.append(stafc)
1352

    
1353
            if not self.COLOR_LCD4:
1354
                stabc = RadioSetting("settings.stabc",
1355
                                     "Top status background color",
1356
                                     RadioSettingValueList(
1357
                                         LIST_COLOR9,
1358
                                         LIST_COLOR9[_mem.settings.stabc]))
1359
                basic.append(stabc)
1360

    
1361
            sigfc = RadioSetting("settings.sigfc",
1362
                                 "Bottom status foreground color",
1363
                                 RadioSettingValueList(
1364
                                     LIST_COLOR9,
1365
                                     LIST_COLOR9[_mem.settings.sigfc]))
1366
            basic.append(sigfc)
1367

    
1368
            if not self.COLOR_LCD4:
1369
                sigbc = RadioSetting("settings.sigbc",
1370
                                     "Bottom status background color",
1371
                                     RadioSettingValueList(
1372
                                         LIST_COLOR9,
1373
                                         LIST_COLOR9[_mem.settings.sigbc]))
1374
                basic.append(sigbc)
1375

    
1376
            rxfc = RadioSetting("settings.rxfc", "Receiving character color",
1377
                                RadioSettingValueList(
1378
                                    LIST_COLOR9,
1379
                                    LIST_COLOR9[_mem.settings.rxfc]))
1380
            basic.append(rxfc)
1381

    
1382
            txfc = RadioSetting("settings.txfc",
1383
                                "Transmitting character color",
1384
                                RadioSettingValueList(
1385
                                    LIST_COLOR9,
1386
                                    LIST_COLOR9[_mem.settings.txfc]))
1387
            basic.append(txfc)
1388

    
1389
            if not self.COLOR_LCD4:
1390
                txdisp = RadioSetting("settings.txdisp",
1391
                                      "Transmitting status display",
1392
                                      RadioSettingValueList(
1393
                                          LIST_TXDISP,
1394
                                          LIST_TXDISP[_mem.settings.txdisp]))
1395
                basic.append(txdisp)
1396

    
1397
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1398
            stfc = RadioSetting("settings.stfc",
1399
                                "ST-FC",
1400
                                RadioSettingValueList(
1401
                                    LIST_COLOR8,
1402
                                    LIST_COLOR8[_mem.settings.stfc]))
1403
            basic.append(stfc)
1404

    
1405
            mffc = RadioSetting("settings.mffc",
1406
                                "MF-FC",
1407
                                RadioSettingValueList(
1408
                                    LIST_COLOR8,
1409
                                    LIST_COLOR8[_mem.settings.mffc]))
1410
            basic.append(mffc)
1411

    
1412
            sfafc = RadioSetting("settings.sfafc",
1413
                                 "SFA-FC",
1414
                                 RadioSettingValueList(
1415
                                     LIST_COLOR8,
1416
                                     LIST_COLOR8[_mem.settings.sfafc]))
1417
            basic.append(sfafc)
1418

    
1419
            sfbfc = RadioSetting("settings.sfbfc",
1420
                                 "SFB-FC",
1421
                                 RadioSettingValueList(
1422
                                     LIST_COLOR8,
1423
                                     LIST_COLOR8[_mem.settings.sfbfc]))
1424
            basic.append(sfbfc)
1425

    
1426
            sfcfc = RadioSetting("settings.sfcfc",
1427
                                 "SFC-FC",
1428
                                 RadioSettingValueList(
1429
                                     LIST_COLOR8,
1430
                                     LIST_COLOR8[_mem.settings.sfcfc]))
1431
            basic.append(sfcfc)
1432

    
1433
            sfdfc = RadioSetting("settings.sfdfc",
1434
                                 "SFD-FC",
1435
                                 RadioSettingValueList(
1436
                                     LIST_COLOR8,
1437
                                     LIST_COLOR8[_mem.settings.sfdfc]))
1438
            basic.append(sfdfc)
1439

    
1440
            subfc = RadioSetting("settings.subfc",
1441
                                 "SUB-FC",
1442
                                 RadioSettingValueList(
1443
                                     LIST_COLOR8,
1444
                                     LIST_COLOR8[_mem.settings.subfc]))
1445
            basic.append(subfc)
1446

    
1447
            fmfc = RadioSetting("settings.fmfc",
1448
                                "FM-FC",
1449
                                RadioSettingValueList(
1450
                                    LIST_COLOR8,
1451
                                    LIST_COLOR8[_mem.settings.fmfc]))
1452
            basic.append(fmfc)
1453

    
1454
            sigfc = RadioSetting("settings.sigfc",
1455
                                 "SIG-FC",
1456
                                 RadioSettingValueList(
1457
                                     LIST_COLOR8,
1458
                                     LIST_COLOR8[_mem.settings.sigfc]))
1459
            basic.append(sigfc)
1460

    
1461
            if not self.MODEL == "KT-8R":
1462
                modfc = RadioSetting("settings.modfc",
1463
                                     "MOD-FC",
1464
                                     RadioSettingValueList(
1465
                                         LIST_COLOR8,
1466
                                         LIST_COLOR8[_mem.settings.modfc]))
1467
                basic.append(modfc)
1468

    
1469
            menufc = RadioSetting("settings.menufc",
1470
                                  "MENUFC",
1471
                                  RadioSettingValueList(
1472
                                      LIST_COLOR8,
1473
                                      LIST_COLOR8[_mem.settings.menufc]))
1474
            basic.append(menufc)
1475

    
1476
            txfc = RadioSetting("settings.txfc",
1477
                                "TX-FC",
1478
                                RadioSettingValueList(
1479
                                    LIST_COLOR8,
1480
                                    LIST_COLOR8[_mem.settings.txfc]))
1481
            basic.append(txfc)
1482

    
1483
            if self.MODEL == "KT-8R":
1484
                rxfc = RadioSetting("settings.rxfc",
1485
                                    "RX-FC",
1486
                                    RadioSettingValueList(
1487
                                        LIST_COLOR8,
1488
                                        LIST_COLOR8[_mem.settings.rxfc]))
1489
                basic.append(rxfc)
1490

    
1491
            if not self.MODEL == "KT-8R":
1492
                txdisp = RadioSetting("settings.txdisp",
1493
                                      "Transmitting status display",
1494
                                      RadioSettingValueList(
1495
                                          LIST_TXDISP,
1496
                                          LIST_TXDISP[_mem.settings.txdisp]))
1497
                basic.append(txdisp)
1498
        elif self.COLOR_LCD4:
1499
            asfc = RadioSetting("settings.asfc",
1500
                                "Above Stat fore color",
1501
                                RadioSettingValueList(
1502
                                    LIST_COLOR8,
1503
                                    LIST_COLOR8[_mem.settings.asfc]))
1504
            basic.append(asfc)
1505

    
1506
            mainfc = RadioSetting("settings.mainfc",
1507
                                  "Main fore color",
1508
                                  RadioSettingValueList(
1509
                                     LIST_COLOR8,
1510
                                     LIST_COLOR8[_mem.settings.mainfc]))
1511
            basic.append(mainfc)
1512

    
1513
            a_fc = RadioSetting("settings.a_fc",
1514
                                "A - fore color",
1515
                                RadioSettingValueList(
1516
                                    LIST_COLOR8,
1517
                                    LIST_COLOR8[_mem.settings.a_fc]))
1518
            basic.append(a_fc)
1519

    
1520
            b_fc = RadioSetting("settings.b_fc",
1521
                                "B - fore color",
1522
                                RadioSettingValueList(
1523
                                    LIST_COLOR8,
1524
                                    LIST_COLOR8[_mem.settings.b_fc]))
1525
            basic.append(b_fc)
1526

    
1527
            c_fc = RadioSetting("settings.c_fc",
1528
                                "C - fore color",
1529
                                RadioSettingValueList(
1530
                                    LIST_COLOR8,
1531
                                    LIST_COLOR8[_mem.settings.c_fc]))
1532
            basic.append(c_fc)
1533

    
1534
            subfc = RadioSetting("settings.subfc",
1535
                                 "Sub fore color",
1536
                                 RadioSettingValueList(
1537
                                     LIST_COLOR8,
1538
                                     LIST_COLOR8[_mem.settings.subfc]))
1539
            basic.append(subfc)
1540

    
1541
            battfc = RadioSetting("settings.battfc",
1542
                                  "Battery fore color",
1543
                                  RadioSettingValueList(
1544
                                      LIST_COLOR8,
1545
                                      LIST_COLOR8[_mem.settings.battfc]))
1546
            basic.append(battfc)
1547

    
1548
            sigfc = RadioSetting("settings.sigfc",
1549
                                 "Signal fore color",
1550
                                 RadioSettingValueList(
1551
                                     LIST_COLOR8,
1552
                                     LIST_COLOR8[_mem.settings.sigfc]))
1553
            basic.append(sigfc)
1554

    
1555
            menufc = RadioSetting("settings.menufc",
1556
                                  "Menu fore color",
1557
                                  RadioSettingValueList(
1558
                                      LIST_COLOR8,
1559
                                      LIST_COLOR8[_mem.settings.menufc]))
1560
            basic.append(menufc)
1561

    
1562
            txfc = RadioSetting("settings.txfc",
1563
                                "TX fore color",
1564
                                RadioSettingValueList(
1565
                                    LIST_COLOR8,
1566
                                    LIST_COLOR8[_mem.settings.txfc]))
1567
            basic.append(txfc)
1568

    
1569
            rxfc = RadioSetting("settings.rxfc",
1570
                                "RX fore color",
1571
                                RadioSettingValueList(
1572
                                    LIST_COLOR8,
1573
                                    LIST_COLOR8[_mem.settings.rxfc]))
1574
            basic.append(rxfc)
1575
        else:
1576
            wtled = RadioSetting("settings.wtled", "Standby backlight Color",
1577
                                 RadioSettingValueList(
1578
                                     LIST_COLOR4,
1579
                                     LIST_COLOR4[_mem.settings.wtled]))
1580
            basic.append(wtled)
1581

    
1582
            rxled = RadioSetting("settings.rxled", "RX backlight Color",
1583
                                 RadioSettingValueList(
1584
                                     LIST_COLOR4,
1585
                                     LIST_COLOR4[_mem.settings.rxled]))
1586
            basic.append(rxled)
1587

    
1588
            txled = RadioSetting("settings.txled", "TX backlight Color",
1589
                                 RadioSettingValueList(
1590
                                     LIST_COLOR4,
1591
                                     LIST_COLOR4[_mem.settings.txled]))
1592
            basic.append(txled)
1593

    
1594
        anil = RadioSetting("settings.anil", "ANI length",
1595
                            RadioSettingValueList(
1596
                                LIST_ANIL,
1597
                                LIST_ANIL[_mem.settings.anil]))
1598
        basic.append(anil)
1599

    
1600
        reps = RadioSetting("settings.reps", "Relay signal (tone burst)",
1601
                            RadioSettingValueList(
1602
                                LIST_REPS,
1603
                                LIST_REPS[_mem.settings.reps]))
1604
        basic.append(reps)
1605

    
1606
        if self.COLOR_LCD4:
1607
            dsub = RadioSetting("settings.dsub", "Subtone display",
1608
                                RadioSettingValueBoolean(
1609
                                    _mem.settings.dsub))
1610
            basic.append(dsub)
1611

    
1612
        if self.MODEL == "GMRS-20V2":
1613
            repsw = RadioSetting("settings.repsw", "Repeater SW",
1614
                                 RadioSettingValueList(
1615
                                     LIST_REPSW,
1616
                                     LIST_REPSW[_mem.settings.repsw]))
1617
            basic.append(repsw)
1618

    
1619
        model_list = ["GMRS-50X1", "KT-8R", "KT-WP12", "WP-9900"]
1620
        if self.MODEL not in model_list:
1621
            repm = RadioSetting("settings.repm", "Relay condition",
1622
                                RadioSettingValueList(
1623
                                    LIST_REPM,
1624
                                    LIST_REPM[_mem.settings.repm]))
1625
            basic.append(repm)
1626

    
1627
        if self.VENDOR == "BTECH" or self.COLOR_LCD:
1628
            if self.COLOR_LCD:
1629
                tmrmr = RadioSetting("settings.tmrmr", "TMR return time",
1630
                                     RadioSettingValueList(
1631
                                         LIST_OFF1TO50,
1632
                                         LIST_OFF1TO50[_mem.settings.tmrmr]))
1633
                basic.append(tmrmr)
1634
            else:
1635
                tdrab = RadioSetting("settings.tdrab", "TDR return time",
1636
                                     RadioSettingValueList(
1637
                                         LIST_OFF1TO50,
1638
                                         LIST_OFF1TO50[_mem.settings.tdrab]))
1639
                basic.append(tdrab)
1640

    
1641
            ste = RadioSetting("settings.ste", "Squelch tail eliminate",
1642
                               RadioSettingValueBoolean(_mem.settings.ste))
1643
            basic.append(ste)
1644

    
1645
            if self.COLOR_LCD4:
1646
                rpste = RadioSetting("settings.rpste", "Repeater STE",
1647
                                     RadioSettingValueList(
1648
                                         LIST_OFF1TO10,
1649
                                         LIST_OFF1TO10[_mem.settings.rpste]))
1650
                basic.append(rpste)
1651
            else:
1652
                rpste = RadioSetting("settings.rpste", "Repeater STE",
1653
                                     RadioSettingValueList(
1654
                                         LIST_OFF1TO9,
1655
                                         LIST_OFF1TO9[_mem.settings.rpste]))
1656
                basic.append(rpste)
1657

    
1658
            if self.COLOR_LCD4:
1659
                rptdl = RadioSetting("settings.rptdl", "Repeater STE delay",
1660
                                     RadioSettingValueList(
1661
                                         LIST_OFF1TO60,
1662
                                         LIST_OFF1TO60[_mem.settings.rptdl]))
1663
                basic.append(rptdl)
1664
            else:
1665
                rptdl = RadioSetting("settings.rptdl", "Repeater STE delay",
1666
                                     RadioSettingValueList(
1667
                                         LIST_RPTDL,
1668
                                         LIST_RPTDL[_mem.settings.rptdl]))
1669
                basic.append(rptdl)
1670

    
1671
        if self.MODEL == "DB25-G":
1672
            mgain = RadioSetting("settings.mgain", "Auto power-on",
1673
                                 RadioSettingValueBoolean(_mem.settings.mgain))
1674
            basic.append(mgain)
1675

    
1676
        if str(_mem.fingerprint.fp) in BTECH3:
1677
            mgain = RadioSetting("settings.mgain", "Mic gain",
1678
                                 RadioSettingValueInteger(0, 120,
1679
                                                          _mem.settings.mgain))
1680
            basic.append(mgain)
1681

    
1682
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1683
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain",
1684
                                 RadioSettingValueInteger(0, 60,
1685
                                                          _mem.settings.dtmfg))
1686
            basic.append(dtmfg)
1687

    
1688
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1689
            mgain = RadioSetting("settings.mgain", "Mic gain",
1690
                                 RadioSettingValueInteger(0, 127,
1691
                                                          _mem.settings.mgain))
1692
            basic.append(mgain)
1693

    
1694
            skiptx = RadioSetting("settings.skiptx", "Skip TX",
1695
                                  RadioSettingValueList(
1696
                                      LIST_SKIPTX,
1697
                                      LIST_SKIPTX[_mem.settings.skiptx]))
1698
            basic.append(skiptx)
1699

    
1700
            scmode = RadioSetting("settings.scmode", "Scan mode",
1701
                                  RadioSettingValueList(
1702
                                      LIST_SCMODE,
1703
                                      LIST_SCMODE[_mem.settings.scmode]))
1704
            basic.append(scmode)
1705

    
1706
        if self.MODEL == "KT-8R" or self.MODEL == "UV-25X2" \
1707
                or self.MODEL == "UV-25X4" or self.MODEL == "UV-50X2" \
1708
                or self.MODEL == "GMRS-50X1" or self.MODEL == "GMRS-20V2":
1709
            tmrtx = RadioSetting("settings.tmrtx", "TX in multi-standby",
1710
                                 RadioSettingValueList(
1711
                                     LIST_TMRTX,
1712
                                     LIST_TMRTX[_mem.settings.tmrtx]))
1713
            basic.append(tmrtx)
1714

    
1715
        # Advanced
1716
        def _filter(name):
1717
            filtered = ""
1718
            for char in str(name):
1719
                if char in VALID_CHARS:
1720
                    filtered += char
1721
                else:
1722
                    filtered += " "
1723
            return filtered
1724

    
1725
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3 or
1726
                                   self.COLOR_LCD4):
1727
            _msg = self._memobj.poweron_msg
1728
            line1 = RadioSetting("poweron_msg.line1",
1729
                                 "Power-on message line 1",
1730
                                 RadioSettingValueString(0, 8, _filter(
1731
                                                         _msg.line1)))
1732
            advanced.append(line1)
1733
            line2 = RadioSetting("poweron_msg.line2",
1734
                                 "Power-on message line 2",
1735
                                 RadioSettingValueString(0, 8, _filter(
1736
                                                         _msg.line2)))
1737
            advanced.append(line2)
1738
            line3 = RadioSetting("poweron_msg.line3",
1739
                                 "Power-on message line 3",
1740
                                 RadioSettingValueString(0, 8, _filter(
1741
                                                         _msg.line3)))
1742
            advanced.append(line3)
1743
            line4 = RadioSetting("poweron_msg.line4",
1744
                                 "Power-on message line 4",
1745
                                 RadioSettingValueString(0, 8, _filter(
1746
                                                         _msg.line4)))
1747
            advanced.append(line4)
1748
            line5 = RadioSetting("poweron_msg.line5",
1749
                                 "Power-on message line 5",
1750
                                 RadioSettingValueString(0, 8, _filter(
1751
                                                         _msg.line5)))
1752
            advanced.append(line5)
1753
            line6 = RadioSetting("poweron_msg.line6",
1754
                                 "Power-on message line 6",
1755
                                 RadioSettingValueString(0, 8, _filter(
1756
                                                         _msg.line6)))
1757
            advanced.append(line6)
1758
            line7 = RadioSetting("poweron_msg.line7",
1759
                                 "Power-on message line 7",
1760
                                 RadioSettingValueString(0, 8, _filter(
1761
                                                         _msg.line7)))
1762
            advanced.append(line7)
1763
            line8 = RadioSetting("poweron_msg.line8", "Static message",
1764
                                 RadioSettingValueString(0, 8, _filter(
1765
                                                         _msg.line8)))
1766
            advanced.append(line8)
1767
        elif self.COLOR_LCD2 or self.COLOR_LCD3 or self.COLOR_LCD4:
1768
            _msg = self._memobj.static_msg
1769
            line = RadioSetting("static_msg.line", "Static message",
1770
                                RadioSettingValueString(0, 16, _filter(
1771
                                    _msg.line)))
1772
            advanced.append(line)
1773
        else:
1774
            _msg = self._memobj.poweron_msg
1775
            line1 = RadioSetting("poweron_msg.line1",
1776
                                 "Power-on message line 1",
1777
                                 RadioSettingValueString(0, 6, _filter(
1778
                                                         _msg.line1)))
1779
            advanced.append(line1)
1780
            line2 = RadioSetting("poweron_msg.line2",
1781
                                 "Power-on message line 2",
1782
                                 RadioSettingValueString(0, 6, _filter(
1783
                                                         _msg.line2)))
1784
            advanced.append(line2)
1785

    
1786
        if self.MODEL in ("UV-2501", "UV-5001"):
1787
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1788
                                   RadioSettingValueBoolean(
1789
                                       _mem.settings2.vfomren))
1790
            advanced.append(vfomren)
1791

    
1792
            reseten = RadioSetting("settings2.reseten", "RESET",
1793
                                   RadioSettingValueBoolean(
1794
                                       _mem.settings2.reseten))
1795
            advanced.append(reseten)
1796

    
1797
            menuen = RadioSetting("settings2.menuen", "Menu",
1798
                                  RadioSettingValueBoolean(
1799
                                      _mem.settings2.menuen))
1800
            advanced.append(menuen)
1801

    
1802
        # Other
1803
        def convert_bytes_to_limit(bytes):
1804
            limit = ""
1805
            for byte in bytes:
1806
                if byte < 10:
1807
                    limit += chr(byte + 0x30)
1808
                else:
1809
                    break
1810
            return limit
1811

    
1812
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1813
            _ranges = self._memobj.ranges220
1814
            ranges = "ranges220"
1815
        else:
1816
            _ranges = self._memobj.ranges
1817
            ranges = "ranges"
1818

    
1819
        _limit = convert_bytes_to_limit(_ranges.vhf_low)
1820
        val = RadioSettingValueString(0, 3, _limit)
1821
        val.set_mutable(False)
1822
        vhf_low = RadioSetting("%s.vhf_low" % ranges, "VHF low", val)
1823
        other.append(vhf_low)
1824

    
1825
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1826
        val = RadioSettingValueString(0, 3, _limit)
1827
        val.set_mutable(False)
1828
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1829
        other.append(vhf_high)
1830

    
1831
        if self.BANDS == 3 or self.BANDS == 4:
1832
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1833
            val = RadioSettingValueString(0, 3, _limit)
1834
            val.set_mutable(False)
1835
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1836
            other.append(vhf2_low)
1837

    
1838
            _limit = convert_bytes_to_limit(_ranges.vhf2_high)
1839
            val = RadioSettingValueString(0, 3, _limit)
1840
            val.set_mutable(False)
1841
            vhf2_high = RadioSetting("%s.vhf2_high" % ranges, "VHF2 high", val)
1842
            other.append(vhf2_high)
1843

    
1844
        _limit = convert_bytes_to_limit(_ranges.uhf_low)
1845
        val = RadioSettingValueString(0, 3, _limit)
1846
        val.set_mutable(False)
1847
        uhf_low = RadioSetting("%s.uhf_low" % ranges, "UHF low", val)
1848
        other.append(uhf_low)
1849

    
1850
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1851
        val = RadioSettingValueString(0, 3, _limit)
1852
        val.set_mutable(False)
1853
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1854
        other.append(uhf_high)
1855

    
1856
        if self.BANDS == 4:
1857
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1858
            val = RadioSettingValueString(0, 3, _limit)
1859
            val.set_mutable(False)
1860
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1861
            other.append(uhf2_low)
1862

    
1863
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1864
            val = RadioSettingValueString(0, 3, _limit)
1865
            val.set_mutable(False)
1866
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1867
            other.append(uhf2_high)
1868

    
1869
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1870
        val.set_mutable(False)
1871
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1872
        other.append(fp)
1873

    
1874
        # Work
1875
        if self.COLOR_LCD4:
1876
            dispab = RadioSetting("settings2.dispab", "Display",
1877
                                  RadioSettingValueList(
1878
                                      LIST_ABC,
1879
                                      LIST_ABC[_mem.settings2.dispab]))
1880
            work.append(dispab)
1881
        elif self.COLOR_LCD:
1882
            dispab = RadioSetting("settings2.dispab", "Display",
1883
                                  RadioSettingValueList(
1884
                                      LIST_ABCD,
1885
                                      LIST_ABCD[_mem.settings2.dispab]))
1886
            work.append(dispab)
1887
        else:
1888
            dispab = RadioSetting("settings2.dispab", "Display",
1889
                                  RadioSettingValueList(
1890
                                      LIST_AB,
1891
                                      LIST_AB[_mem.settings2.dispab]))
1892
            work.append(dispab)
1893

    
1894
        if self.COLOR_LCD:
1895
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode",
1896
                                  RadioSettingValueList(
1897
                                      LIST_VFOMR,
1898
                                      LIST_VFOMR[_mem.settings2.vfomra]))
1899
            work.append(vfomra)
1900

    
1901
            vfomrb = RadioSetting("settings2.vfomrb", "VFO/MR B mode",
1902
                                  RadioSettingValueList(
1903
                                      LIST_VFOMR,
1904
                                      LIST_VFOMR[_mem.settings2.vfomrb]))
1905
            work.append(vfomrb)
1906

    
1907
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode",
1908
                                  RadioSettingValueList(
1909
                                      LIST_VFOMR,
1910
                                      LIST_VFOMR[_mem.settings2.vfomrc]))
1911
            work.append(vfomrc)
1912

    
1913
            if not self.COLOR_LCD4:
1914
                vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode",
1915
                                      RadioSettingValueList(
1916
                                          LIST_VFOMR,
1917
                                          LIST_VFOMR[_mem.settings2.vfomrd]))
1918
                work.append(vfomrd)
1919
        else:
1920
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode",
1921
                                 RadioSettingValueList(
1922
                                     LIST_VFOMR,
1923
                                     LIST_VFOMR[_mem.settings2.vfomr]))
1924
            work.append(vfomr)
1925

    
1926
        keylock = RadioSetting("settings2.keylock", "Keypad lock",
1927
                               RadioSettingValueBoolean(
1928
                                   _mem.settings2.keylock))
1929
        work.append(keylock)
1930

    
1931
        mrcha = RadioSetting("settings2.mrcha", "MR A channel",
1932
                             RadioSettingValueInteger(0, self._upper,
1933
                                                      _mem.settings2.mrcha))
1934
        work.append(mrcha)
1935

    
1936
        mrchb = RadioSetting("settings2.mrchb", "MR B channel",
1937
                             RadioSettingValueInteger(0, self._upper,
1938
                                                      _mem.settings2.mrchb))
1939
        work.append(mrchb)
1940

    
1941
        if self.COLOR_LCD:
1942
            mrchc = RadioSetting("settings2.mrchc", "MR C channel",
1943
                                 RadioSettingValueInteger(
1944
                                     0, self._upper, _mem.settings2.mrchc))
1945
            work.append(mrchc)
1946

    
1947
            if not self.COLOR_LCD4:
1948
                mrchd = RadioSetting("settings2.mrchd", "MR D channel",
1949
                                     RadioSettingValueInteger(
1950
                                         0, self._upper, _mem.settings2.mrchd))
1951
                work.append(mrchd)
1952

    
1953
        def convert_bytes_to_freq(bytes):
1954
            real_freq = 0
1955
            for byte in bytes:
1956
                real_freq = (real_freq * 10) + byte
1957
            return chirp_common.format_freq(real_freq * 10)
1958

    
1959
        def my_validate(value):
1960
            _vhf_lower = int(convert_bytes_to_limit(_ranges.vhf_low))
1961
            _vhf_upper = int(convert_bytes_to_limit(_ranges.vhf_high))
1962
            _uhf_lower = int(convert_bytes_to_limit(_ranges.uhf_low))
1963
            _uhf_upper = int(convert_bytes_to_limit(_ranges.uhf_high))
1964
            if self.BANDS == 3 or self.BANDS == 4:
1965
                _vhf2_lower = int(convert_bytes_to_limit(_ranges.vhf2_low))
1966
                _vhf2_upper = int(convert_bytes_to_limit(_ranges.vhf2_high))
1967
            if self.BANDS == 4:
1968
                _uhf2_lower = int(convert_bytes_to_limit(_ranges.uhf2_low))
1969
                _uhf2_upper = int(convert_bytes_to_limit(_ranges.uhf2_high))
1970

    
1971
            value = chirp_common.parse_freq(value)
1972
            msg = ("Can't be less then %i.0000")
1973
            if value > 99000000 and value < _vhf_lower * 1000000:
1974
                raise InvalidValueError(msg % (_vhf_lower))
1975
            msg = ("Can't be betweeb %i.9975-%i.0000")
1976
            if self.BANDS == 2:
1977
                if (_vhf_upper + 1) * 1000000 <= value and \
1978
                        value < _uhf_lower * 1000000:
1979
                    raise InvalidValueError(msg % (_vhf_upper, _uhf_lower))
1980
            if self.BANDS == 3:
1981
                if (_vhf_upper + 1) * 1000000 <= value and \
1982
                        value < _vhf2_lower * 1000000:
1983
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1984
                if (_vhf2_upper + 1) * 1000000 <= value and \
1985
                        value < _uhf_lower * 1000000:
1986
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf_lower))
1987
            if self.BANDS == 4:
1988
                if (_vhf_upper + 1) * 1000000 <= value and \
1989
                        value < _vhf2_lower * 1000000:
1990
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1991
                if (_vhf2_upper + 1) * 1000000 <= value and \
1992
                        value < _uhf2_lower * 1000000:
1993
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf2_lower))
1994
                if (_uhf2_upper + 1) * 1000000 <= value and \
1995
                        value < _uhf_lower * 1000000:
1996
                    raise InvalidValueError(msg % (_uhf2_upper, _uhf_lower))
1997
            msg = ("Can't be greater then %i.9975")
1998
            if value > 99000000 and value >= _uhf_upper * 1000000:
1999
                raise InvalidValueError(msg % (_uhf_upper))
2000
            return chirp_common.format_freq(value)
2001

    
2002
        def apply_freq(setting, obj):
2003
            value = chirp_common.parse_freq(str(setting.value)) / 10
2004
            for i in range(7, -1, -1):
2005
                obj.freq[i] = value % 10
2006
                value /= 10
2007

    
2008
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
2009
                                        _mem.vfo.a.freq))
2010
        val1a.set_validate_callback(my_validate)
2011
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
2012
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
2013
        work.append(vfoafreq)
2014

    
2015
        val1b = RadioSettingValueString(0, 10, convert_bytes_to_freq(
2016
                                        _mem.vfo.b.freq))
2017
        val1b.set_validate_callback(my_validate)
2018
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
2019
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
2020
        work.append(vfobfreq)
2021

    
2022
        if self.COLOR_LCD:
2023
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
2024
                                            _mem.vfo.c.freq))
2025
            val1c.set_validate_callback(my_validate)
2026
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
2027
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
2028
            work.append(vfocfreq)
2029

    
2030
            if not self.COLOR_LCD4:
2031
                val1d = RadioSettingValueString(0, 10, convert_bytes_to_freq(
2032
                                                _mem.vfo.d.freq))
2033
                val1d.set_validate_callback(my_validate)
2034
                vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
2035
                vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
2036
                work.append(vfodfreq)
2037

    
2038
        if not self.MODEL == "GMRS-50X1":
2039
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift",
2040
                                      RadioSettingValueList(
2041
                                          LIST_SHIFT,
2042
                                          LIST_SHIFT[_mem.vfo.a.shiftd]))
2043
            work.append(vfoashiftd)
2044

    
2045
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift",
2046
                                      RadioSettingValueList(
2047
                                          LIST_SHIFT,
2048
                                          LIST_SHIFT[_mem.vfo.b.shiftd]))
2049
            work.append(vfobshiftd)
2050

    
2051
            if self.COLOR_LCD:
2052
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift",
2053
                                          RadioSettingValueList(
2054
                                              LIST_SHIFT,
2055
                                              LIST_SHIFT[_mem.vfo.c.shiftd]))
2056
                work.append(vfocshiftd)
2057

    
2058
                if not self.COLOR_LCD4:
2059
                    val = RadioSettingValueList(LIST_SHIFT,
2060
                                                LIST_SHIFT[_mem.vfo.d.shiftd])
2061
                    vfodshiftd = RadioSetting("vfo.d.shiftd", "VFO D shift",
2062
                                              val)
2063
                    work.append(vfodshiftd)
2064

    
2065
        def convert_bytes_to_offset(bytes):
2066
            real_offset = 0
2067
            for byte in bytes:
2068
                real_offset = (real_offset * 10) + byte
2069
            return chirp_common.format_freq(real_offset * 1000)
2070

    
2071
        def apply_offset(setting, obj):
2072
            value = chirp_common.parse_freq(str(setting.value)) / 1000
2073
            for i in range(5, -1, -1):
2074
                obj.offset[i] = value % 10
2075
                value /= 10
2076

    
2077
        if not self.MODEL == "GMRS-50X1":
2078
            if self.COLOR_LCD:
2079
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2080
                                                _mem.vfo.a.offset))
2081
                vfoaoffset = RadioSetting("vfo.a.offset",
2082
                                          "VFO A offset (0.000-999.999)",
2083
                                          val1a)
2084
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
2085
                work.append(vfoaoffset)
2086

    
2087
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2088
                                                _mem.vfo.b.offset))
2089
                vfoboffset = RadioSetting("vfo.b.offset",
2090
                                          "VFO B offset (0.000-999.999)",
2091
                                          val1b)
2092
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
2093
                work.append(vfoboffset)
2094

    
2095
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2096
                                                _mem.vfo.c.offset))
2097
                vfocoffset = RadioSetting("vfo.c.offset",
2098
                                          "VFO C offset (0.000-999.999)",
2099
                                          val1c)
2100
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
2101
                work.append(vfocoffset)
2102

    
2103
                if not self.COLOR_LCD4:
2104
                    val1d = RadioSettingValueString(0, 10,
2105
                                                    convert_bytes_to_offset(
2106
                                                        _mem.vfo.d.offset))
2107
                    vfodoffset = RadioSetting("vfo.d.offset",
2108
                                              "VFO D offset (0.000-999.999)",
2109
                                              val1d)
2110
                    vfodoffset.set_apply_callback(apply_offset, _mem.vfo.d)
2111
                    work.append(vfodoffset)
2112
            else:
2113
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2114
                                                _mem.vfo.a.offset))
2115
                vfoaoffset = RadioSetting("vfo.a.offset",
2116
                                          "VFO A offset (0.000-99.999)", val1a)
2117
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
2118
                work.append(vfoaoffset)
2119

    
2120
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2121
                                                _mem.vfo.b.offset))
2122
                vfoboffset = RadioSetting("vfo.b.offset",
2123
                                          "VFO B offset (0.000-99.999)", val1b)
2124
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
2125
                work.append(vfoboffset)
2126

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

    
2134
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power",
2135
                                   RadioSettingValueList(
2136
                                       LIST_TXP,
2137
                                       LIST_TXP[_mem.vfo.b.power]))
2138
            work.append(vfobtxp)
2139

    
2140
            if self.COLOR_LCD:
2141
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power",
2142
                                       RadioSettingValueList(
2143
                                           LIST_TXP,
2144
                                           LIST_TXP[_mem.vfo.c.power]))
2145
                work.append(vfoctxp)
2146

    
2147
                if not self.COLOR_LCD4:
2148
                    vfodtxp = RadioSetting("vfo.d.power", "VFO D power",
2149
                                           RadioSettingValueList(
2150
                                               LIST_TXP,
2151
                                               LIST_TXP[_mem.vfo.d.power]))
2152
                    work.append(vfodtxp)
2153

    
2154
        if not self.MODEL == "GMRS-50X1":
2155
            vfoawide = RadioSetting("vfo.a.wide", "VFO A bandwidth",
2156
                                    RadioSettingValueList(
2157
                                        LIST_WIDE,
2158
                                        LIST_WIDE[_mem.vfo.a.wide]))
2159
            work.append(vfoawide)
2160

    
2161
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth",
2162
                                    RadioSettingValueList(
2163
                                        LIST_WIDE,
2164
                                        LIST_WIDE[_mem.vfo.b.wide]))
2165
            work.append(vfobwide)
2166

    
2167
            if self.COLOR_LCD:
2168
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth",
2169
                                        RadioSettingValueList(
2170
                                            LIST_WIDE,
2171
                                            LIST_WIDE[_mem.vfo.c.wide]))
2172
                work.append(vfocwide)
2173

    
2174
                if not self.COLOR_LCD4:
2175
                    vfodwide = RadioSetting("vfo.d.wide", "VFO D bandwidth",
2176
                                            RadioSettingValueList(
2177
                                                LIST_WIDE,
2178
                                                LIST_WIDE[_mem.vfo.d.wide]))
2179
                    work.append(vfodwide)
2180

    
2181
        vfoastep = RadioSetting("vfo.a.step", "VFO A step",
2182
                                RadioSettingValueList(
2183
                                    LIST_STEP,
2184
                                    LIST_STEP[_mem.vfo.a.step]))
2185
        work.append(vfoastep)
2186

    
2187
        vfobstep = RadioSetting("vfo.b.step", "VFO B step",
2188
                                RadioSettingValueList(
2189
                                    LIST_STEP,
2190
                                    LIST_STEP[_mem.vfo.b.step]))
2191
        work.append(vfobstep)
2192

    
2193
        if self.COLOR_LCD:
2194
            vfocstep = RadioSetting("vfo.c.step", "VFO C step",
2195
                                    RadioSettingValueList(
2196
                                        LIST_STEP,
2197
                                        LIST_STEP[_mem.vfo.c.step]))
2198
            work.append(vfocstep)
2199

    
2200
            if not self.COLOR_LCD4:
2201
                vfodstep = RadioSetting("vfo.d.step", "VFO D step",
2202
                                        RadioSettingValueList(
2203
                                            LIST_STEP,
2204
                                            LIST_STEP[_mem.vfo.d.step]))
2205
                work.append(vfodstep)
2206

    
2207
        vfoaoptsig = RadioSetting("vfo.a.optsig", "VFO A optional signal",
2208
                                  RadioSettingValueList(
2209
                                      OPTSIG_LIST,
2210
                                      OPTSIG_LIST[_mem.vfo.a.optsig]))
2211
        work.append(vfoaoptsig)
2212

    
2213
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal",
2214
                                  RadioSettingValueList(
2215
                                      OPTSIG_LIST,
2216
                                      OPTSIG_LIST[_mem.vfo.b.optsig]))
2217
        work.append(vfoboptsig)
2218

    
2219
        if self.COLOR_LCD:
2220
            vfocoptsig = RadioSetting("vfo.c.optsig", "VFO C optional signal",
2221
                                      RadioSettingValueList(
2222
                                          OPTSIG_LIST,
2223
                                          OPTSIG_LIST[_mem.vfo.c.optsig]))
2224
            work.append(vfocoptsig)
2225

    
2226
            if not self.COLOR_LCD4:
2227
                val = RadioSettingValueList(OPTSIG_LIST,
2228
                                            OPTSIG_LIST[_mem.vfo.d.optsig])
2229
                vfodoptsig = RadioSetting("vfo.d.optsig",
2230
                                          "VFO D optional signal", val)
2231
                work.append(vfodoptsig)
2232

    
2233
        vfoaspmute = RadioSetting("vfo.a.spmute", "VFO A speaker mute",
2234
                                  RadioSettingValueList(
2235
                                      SPMUTE_LIST,
2236
                                      SPMUTE_LIST[_mem.vfo.a.spmute]))
2237
        work.append(vfoaspmute)
2238

    
2239
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute",
2240
                                  RadioSettingValueList(
2241
                                      SPMUTE_LIST,
2242
                                      SPMUTE_LIST[_mem.vfo.b.spmute]))
2243
        work.append(vfobspmute)
2244

    
2245
        if self.COLOR_LCD:
2246
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute",
2247
                                      RadioSettingValueList(
2248
                                          SPMUTE_LIST,
2249
                                          SPMUTE_LIST[_mem.vfo.c.spmute]))
2250
            work.append(vfocspmute)
2251

    
2252
            if not self.COLOR_LCD4:
2253
                vfodspmute = RadioSetting("vfo.d.spmute", "VFO D speaker mute",
2254
                                          RadioSettingValueList(
2255
                                              SPMUTE_LIST,
2256
                                              SPMUTE_LIST[_mem.vfo.d.spmute]))
2257
                work.append(vfodspmute)
2258

    
2259
        if not self.COLOR_LCD or \
2260
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2261
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble",
2262
                                   RadioSettingValueBoolean(
2263
                                       _mem.vfo.a.scramble))
2264
            work.append(vfoascr)
2265

    
2266
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble",
2267
                                   RadioSettingValueBoolean(
2268
                                       _mem.vfo.b.scramble))
2269
            work.append(vfobscr)
2270

    
2271
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2272
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble",
2273
                                   RadioSettingValueBoolean(
2274
                                       _mem.vfo.c.scramble))
2275
            work.append(vfocscr)
2276

    
2277
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble",
2278
                                   RadioSettingValueBoolean(
2279
                                       _mem.vfo.d.scramble))
2280
            work.append(vfodscr)
2281

    
2282
        if not self.MODEL == "GMRS-50X1":
2283
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID",
2284
                                     RadioSettingValueList(
2285
                                         PTTIDCODE_LIST,
2286
                                         PTTIDCODE_LIST[_mem.vfo.a.scode]))
2287
            work.append(vfoascode)
2288

    
2289
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID",
2290
                                     RadioSettingValueList(
2291
                                         PTTIDCODE_LIST,
2292
                                         PTTIDCODE_LIST[_mem.vfo.b.scode]))
2293
            work.append(vfobscode)
2294

    
2295
            if self.COLOR_LCD:
2296
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID",
2297
                                         RadioSettingValueList(
2298
                                             PTTIDCODE_LIST,
2299
                                             PTTIDCODE_LIST[_mem.vfo.c.scode]))
2300
                work.append(vfocscode)
2301

    
2302
                if not self.COLOR_LCD4:
2303
                    val = RadioSettingValueList(PTTIDCODE_LIST,
2304
                                                PTTIDCODE_LIST[
2305
                                                    _mem.vfo.d.scode])
2306
                    vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID",
2307
                                             val)
2308
                    work.append(vfodscode)
2309

    
2310
        if not self.MODEL == "GMRS-50X1":
2311
            pttid = RadioSetting("settings.pttid", "PTT ID",
2312
                                 RadioSettingValueList(
2313
                                     PTTID_LIST,
2314
                                     PTTID_LIST[_mem.settings.pttid]))
2315
            work.append(pttid)
2316

    
2317
        if not self.COLOR_LCD:
2318
            # FM presets
2319
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2320
            top.append(fm_presets)
2321

    
2322
            def fm_validate(value):
2323
                if value == 0:
2324
                    return chirp_common.format_freq(value)
2325
                if not (87.5 <= value and value <= 108.0):  # 87.5-108MHz
2326
                    msg = ("FM-Preset-Frequency: " +
2327
                           "Must be between 87.5 and 108 MHz")
2328
                    raise InvalidValueError(msg)
2329
                return value
2330

    
2331
            def apply_fm_preset_name(setting, obj):
2332
                valstring = str(setting.value)
2333
                for i in range(0, 6):
2334
                    if valstring[i] in VALID_CHARS:
2335
                        obj[i] = valstring[i]
2336
                    else:
2337
                        obj[i] = '0xff'
2338

    
2339
            def apply_fm_freq(setting, obj):
2340
                value = chirp_common.parse_freq(str(setting.value)) / 10
2341
                for i in range(7, -1, -1):
2342
                    obj.freq[i] = value % 10
2343
                    value /= 10
2344

    
2345
            _presets = self._memobj.fm_radio_preset
2346
            i = 1
2347
            for preset in _presets:
2348
                line = RadioSetting("fm_presets_" + str(i),
2349
                                    "Station name " + str(i),
2350
                                    RadioSettingValueString(0, 6, _filter(
2351
                                        preset.broadcast_station_name)))
2352
                line.set_apply_callback(apply_fm_preset_name,
2353
                                        preset.broadcast_station_name)
2354

    
2355
                val = RadioSettingValueFloat(0, 108,
2356
                                             convert_bytes_to_freq(
2357
                                                 preset.freq))
2358
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2359
                                      "Frequency " + str(i), val)
2360
                val.set_validate_callback(fm_validate)
2361
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2362
                fm_presets.append(line)
2363
                fm_presets.append(fmfreq)
2364

    
2365
                i = i + 1
2366

    
2367
        # DTMF-Setting
2368
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2369
                                              "DTMF Encoding Settings")
2370
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2371
                                              "DTMF Decoding Settings")
2372
        top.append(dtmf_enc_settings)
2373
        top.append(dtmf_dec_settings)
2374
        txdisable = RadioSetting("dtmf_settings.txdisable",
2375
                                 "TX-Disable",
2376
                                 RadioSettingValueBoolean(
2377
                                     _mem.dtmf_settings.txdisable))
2378
        dtmf_enc_settings.append(txdisable)
2379

    
2380
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2381
                                 "RX-Disable",
2382
                                 RadioSettingValueBoolean(
2383
                                     _mem.dtmf_settings.rxdisable))
2384
        dtmf_enc_settings.append(rxdisable)
2385

    
2386
        if _mem.dtmf_settings.dtmfspeed_on > 0x0F:
2387
            val = 0x03
2388
        else:
2389
            val = _mem.dtmf_settings.dtmfspeed_on
2390
        dtmfspeed_on = RadioSetting(
2391
            "dtmf_settings.dtmfspeed_on",
2392
            "DTMF Speed (On Time)",
2393
            RadioSettingValueList(LIST_DTMF_SPEED,
2394
                                  LIST_DTMF_SPEED[
2395
                                      val]))
2396
        dtmf_enc_settings.append(dtmfspeed_on)
2397

    
2398
        if _mem.dtmf_settings.dtmfspeed_off > 0x0F:
2399
            val = 0x03
2400
        else:
2401
            val = _mem.dtmf_settings.dtmfspeed_off
2402
        dtmfspeed_off = RadioSetting(
2403
            "dtmf_settings.dtmfspeed_off",
2404
            "DTMF Speed (Off Time)",
2405
            RadioSettingValueList(LIST_DTMF_SPEED,
2406
                                  LIST_DTMF_SPEED[
2407
                                      val]))
2408
        dtmf_enc_settings.append(dtmfspeed_off)
2409

    
2410
        def memory2string(dmtf_mem):
2411
            dtmf_string = ""
2412
            for digit in dmtf_mem:
2413
                if digit != 255:
2414
                    index = LIST_DTMF_VALUES.index(digit)
2415
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2416
            return dtmf_string
2417

    
2418
        def apply_dmtf_frame(setting, obj):
2419
            LOG.debug("Setting DTMF-Code: " + str(setting.value))
2420
            val_string = str(setting.value)
2421
            for i in range(0, 16):
2422
                obj[i] = 255
2423
            i = 0
2424
            for current_char in val_string:
2425
                current_char = current_char.upper()
2426
                index = LIST_DTMF_DIGITS.index(current_char)
2427
                obj[i] = LIST_DTMF_VALUES[index]
2428
                i = i + 1
2429

    
2430
        codes = self._memobj.dtmf_codes
2431
        i = 1
2432
        for dtmfcode in codes:
2433
            val = RadioSettingValueString(0, 16, memory2string(
2434
                                              dtmfcode.code),
2435
                                          False, CHARSET_DTMF_DIGITS)
2436
            line = RadioSetting("dtmf_code_" + str(i) + "_code",
2437
                                "DMTF Code " + str(i), val)
2438
            line.set_apply_callback(apply_dmtf_frame, dtmfcode.code)
2439
            dtmf_enc_settings.append(line)
2440
            i = i + 1
2441

    
2442
        line = RadioSetting("dtmf_settings.mastervice",
2443
                            "Master and Vice ID",
2444
                            RadioSettingValueBoolean(
2445
                                _mem.dtmf_settings.mastervice))
2446
        dtmf_dec_settings.append(line)
2447

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

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

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

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

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

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

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

    
2496
        line = RadioSetting("dtmf_settings.vinspection",
2497
                            "Vice Inspection",
2498
                            RadioSettingValueBoolean(
2499
                                _mem.dtmf_settings.vinspection))
2500
        dtmf_dec_settings.append(line)
2501

    
2502
        line = RadioSetting("dtmf_settings.vmonitor",
2503
                            "Vice Monitor",
2504
                            RadioSettingValueBoolean(
2505
                                _mem.dtmf_settings.vmonitor))
2506
        dtmf_dec_settings.append(line)
2507

    
2508
        line = RadioSetting("dtmf_settings.vstun",
2509
                            "Vice Stun",
2510
                            RadioSettingValueBoolean(
2511
                                _mem.dtmf_settings.vstun))
2512
        dtmf_dec_settings.append(line)
2513

    
2514
        line = RadioSetting("dtmf_settings.vkill",
2515
                            "Vice Kill",
2516
                            RadioSettingValueBoolean(
2517
                                _mem.dtmf_settings.vkill))
2518
        dtmf_dec_settings.append(line)
2519

    
2520
        line = RadioSetting("dtmf_settings.vrevive",
2521
                            "Vice Revive",
2522
                            RadioSettingValueBoolean(
2523
                                _mem.dtmf_settings.vrevive))
2524
        dtmf_dec_settings.append(line)
2525

    
2526
        val = RadioSettingValueString(0, 16, memory2string(
2527
                                          _mem.dtmf_settings.inspection),
2528
                                      False, CHARSET_DTMF_DIGITS)
2529
        line = RadioSetting("dtmf_settings.inspection",
2530
                            "Inspection", val)
2531
        line.set_apply_callback(apply_dmtf_frame,
2532
                                _mem.dtmf_settings.inspection)
2533
        dtmf_dec_settings.append(line)
2534

    
2535
        val = RadioSettingValueString(0, 16, memory2string(
2536
                                          _mem.dtmf_settings.alarmcode),
2537
                                      False, CHARSET_DTMF_DIGITS)
2538
        line = RadioSetting("dtmf_settings.alarmcode",
2539
                            "Alarm", val)
2540
        line.set_apply_callback(apply_dmtf_frame,
2541
                                _mem.dtmf_settings.alarmcode)
2542
        dtmf_dec_settings.append(line)
2543

    
2544
        val = RadioSettingValueString(0, 16, memory2string(
2545
                                          _mem.dtmf_settings.kill),
2546
                                      False, CHARSET_DTMF_DIGITS)
2547
        line = RadioSetting("dtmf_settings.kill",
2548
                            "Kill", val)
2549
        line.set_apply_callback(apply_dmtf_frame,
2550
                                _mem.dtmf_settings.kill)
2551
        dtmf_dec_settings.append(line)
2552

    
2553
        val = RadioSettingValueString(0, 16, memory2string(
2554
                                          _mem.dtmf_settings.monitor),
2555
                                      False, CHARSET_DTMF_DIGITS)
2556
        line = RadioSetting("dtmf_settings.monitor",
2557
                            "Monitor", val)
2558
        line.set_apply_callback(apply_dmtf_frame,
2559
                                _mem.dtmf_settings.monitor)
2560
        dtmf_dec_settings.append(line)
2561

    
2562
        val = RadioSettingValueString(0, 16, memory2string(
2563
                                          _mem.dtmf_settings.stun),
2564
                                      False, CHARSET_DTMF_DIGITS)
2565
        line = RadioSetting("dtmf_settings.stun",
2566
                            "Stun", val)
2567
        line.set_apply_callback(apply_dmtf_frame,
2568
                                _mem.dtmf_settings.stun)
2569
        dtmf_dec_settings.append(line)
2570

    
2571
        val = RadioSettingValueString(0, 16, memory2string(
2572
                                          _mem.dtmf_settings.revive),
2573
                                      False, CHARSET_DTMF_DIGITS)
2574
        line = RadioSetting("dtmf_settings.revive",
2575
                            "Revive", val)
2576
        line.set_apply_callback(apply_dmtf_frame,
2577
                                _mem.dtmf_settings.revive)
2578
        dtmf_dec_settings.append(line)
2579

    
2580
        def apply_dmtf_listvalue(setting, obj):
2581
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2582
            val = str(setting.value)
2583
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2584
            val = LIST_DTMF_SPECIAL_VALUES[index]
2585
            obj.set_value(val)
2586

    
2587
        if _mem.dtmf_settings.groupcode not in LIST_DTMF_SPECIAL_VALUES:
2588
            val = 0x0B
2589
        else:
2590
            val = _mem.dtmf_settings.groupcode
2591
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2592
        line = RadioSetting(
2593
            "dtmf_settings.groupcode",
2594
            "Group Code",
2595
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2596
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2597
        line.set_apply_callback(apply_dmtf_listvalue,
2598
                                _mem.dtmf_settings.groupcode)
2599
        dtmf_dec_settings.append(line)
2600

    
2601
        if _mem.dtmf_settings.spacecode not in LIST_DTMF_SPECIAL_VALUES:
2602
            val = 0x0C
2603
        else:
2604
            val = _mem.dtmf_settings.spacecode
2605
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2606
        line = RadioSetting(
2607
            "dtmf_settings.spacecode",
2608
            "Space Code",
2609
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2610
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2611
        line.set_apply_callback(apply_dmtf_listvalue,
2612
                                _mem.dtmf_settings.spacecode)
2613
        dtmf_dec_settings.append(line)
2614

    
2615
        if self.COLOR_LCD:
2616
            if _mem.dtmf_settings.resettime > 0x63:
2617
                val = 0x4F
2618
            else:
2619
                val = _mem.dtmf_settings.resettime
2620
            line = RadioSetting(
2621
                "dtmf_settings.resettime",
2622
                "Reset time",
2623
                RadioSettingValueList(LIST_5TONE_RESET_COLOR,
2624
                                      LIST_5TONE_RESET_COLOR[
2625
                                          val]))
2626
            dtmf_dec_settings.append(line)
2627
        else:
2628
            line = RadioSetting(
2629
                "dtmf_settings.resettime",
2630
                "Reset time",
2631
                RadioSettingValueList(LIST_5TONE_RESET,
2632
                                      LIST_5TONE_RESET[
2633
                                          _mem.dtmf_settings.resettime]))
2634
            dtmf_dec_settings.append(line)
2635

    
2636
        if _mem.dtmf_settings.delayproctime > 0x27:
2637
            val = 0x04
2638
        else:
2639
            val = _mem.dtmf_settings.delayproctime
2640
        line = RadioSetting(
2641
            "dtmf_settings.delayproctime",
2642
            "Delay processing time",
2643
            RadioSettingValueList(LIST_DTMF_DELAY,
2644
                                  LIST_DTMF_DELAY[
2645
                                      val]))
2646
        dtmf_dec_settings.append(line)
2647

    
2648
        # 5 Tone Settings
2649
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2650
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2651

    
2652
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2653
        group_5tone.append(stds_5tone)
2654
        group_5tone.append(codes_5tone)
2655

    
2656
        top.append(group_5tone)
2657

    
2658
        def apply_list_value(setting, obj):
2659
            options = setting.value.get_options()
2660
            obj.set_value(options.index(str(setting.value)))
2661

    
2662
        _5tone_standards = self._memobj._5tone_std_settings
2663
        i = 0
2664
        for standard in _5tone_standards:
2665
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2666
                                          LIST_5TONE_STANDARDS[i])
2667
            stds_5tone.append(std_5tone)
2668

    
2669
            period = standard.period
2670
            if period == 255:
2671
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2672
                          " is not yet configured. Setting to 70ms.")
2673
                period = 5
2674

    
2675
            if period <= len(LIST_5TONE_STANDARD_PERIODS):
2676
                line = RadioSetting(
2677
                    "_5tone_std_settings_" + str(i) + "_period",
2678
                    "Period (ms)", RadioSettingValueList
2679
                    (LIST_5TONE_STANDARD_PERIODS,
2680
                     LIST_5TONE_STANDARD_PERIODS[period]))
2681
                line.set_apply_callback(apply_list_value, standard.period)
2682
                std_5tone.append(line)
2683
            else:
2684
                LOG.debug("Invalid value for 5tone period! Disabling.")
2685

    
2686
            group_tone = standard.group_tone
2687
            if group_tone == 255:
2688
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2689
                          " is not yet configured. Setting to A.")
2690
                group_tone = 10
2691

    
2692
            if group_tone <= len(LIST_5TONE_DIGITS):
2693
                line = RadioSetting(
2694
                    "_5tone_std_settings_" + str(i) + "_grouptone",
2695
                    "Group Tone",
2696
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2697
                                          LIST_5TONE_DIGITS[
2698
                                              group_tone]))
2699
                line.set_apply_callback(apply_list_value,
2700
                                        standard.group_tone)
2701
                std_5tone.append(line)
2702
            else:
2703
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2704

    
2705
            repeat_tone = standard.repeat_tone
2706
            if repeat_tone == 255:
2707
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2708
                          " is not yet configured. Setting to E.")
2709
                repeat_tone = 14
2710

    
2711
            if repeat_tone <= len(LIST_5TONE_DIGITS):
2712
                line = RadioSetting(
2713
                    "_5tone_std_settings_" + str(i) + "_repttone",
2714
                    "Repeat Tone",
2715
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2716
                                          LIST_5TONE_DIGITS[
2717
                                              repeat_tone]))
2718
                line.set_apply_callback(apply_list_value,
2719
                                        standard.repeat_tone)
2720
                std_5tone.append(line)
2721
            else:
2722
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2723
            i = i + 1
2724

    
2725
        def my_apply_5tonestdlist_value(setting, obj):
2726
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2727
                obj.set_value(0xFF)
2728
            else:
2729
                obj.set_value(LIST_5TONE_STANDARDS.
2730
                              index(str(setting.value)))
2731

    
2732
        def apply_5tone_frame(setting, obj):
2733
            LOG.debug("Setting 5 Tone: " + str(setting.value))
2734
            valstring = str(setting.value)
2735
            if len(valstring) == 0:
2736
                for i in range(0, 5):
2737
                    obj[i] = 255
2738
            else:
2739
                validFrame = True
2740
                for i in range(0, 5):
2741
                    currentChar = valstring[i].upper()
2742
                    if currentChar in LIST_5TONE_DIGITS:
2743
                        obj[i] = LIST_5TONE_DIGITS.index(currentChar)
2744
                    else:
2745
                        validFrame = False
2746
                        LOG.debug("invalid char: " + str(currentChar))
2747
                if not validFrame:
2748
                    LOG.debug("setting whole frame to FF")
2749
                    for i in range(0, 5):
2750
                        obj[i] = 255
2751

    
2752
        def validate_5tone_frame(value):
2753
            if (len(str(value)) != 5) and (len(str(value)) != 0):
2754
                msg = ("5 Tone must have 5 digits or 0 digits")
2755
                raise InvalidValueError(msg)
2756
            for digit in str(value):
2757
                if digit.upper() not in LIST_5TONE_DIGITS:
2758
                    msg = (str(digit) + " is not a valid digit for 5tones")
2759
                    raise InvalidValueError(msg)
2760
            return value
2761

    
2762
        def frame2string(frame):
2763
            frameString = ""
2764
            for digit in frame:
2765
                if digit != 255:
2766
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2767
            return frameString
2768

    
2769
        _5tone_codes = self._memobj._5tone_codes
2770
        i = 1
2771
        for code in _5tone_codes:
2772
            code_5tone = RadioSettingGroup("code_5tone_" + str(i),
2773
                                           "5 Tone code " + str(i))
2774
            codes_5tone.append(code_5tone)
2775
            if (code.standard == 255):
2776
                currentVal = 15
2777
            else:
2778
                currentVal = code.standard
2779
            line = RadioSetting("_5tone_code_" + str(i) + "_std",
2780
                                " Standard",
2781
                                RadioSettingValueList(LIST_5TONE_STANDARDS,
2782
                                                      LIST_5TONE_STANDARDS[
2783
                                                          currentVal]))
2784
            line.set_apply_callback(my_apply_5tonestdlist_value,
2785
                                    code.standard)
2786
            code_5tone.append(line)
2787

    
2788
            val = RadioSettingValueString(0, 6,
2789
                                          frame2string(code.frame1), False)
2790
            line = RadioSetting("_5tone_code_" + str(i) + "_frame1",
2791
                                " Frame 1", val)
2792
            val.set_validate_callback(validate_5tone_frame)
2793
            line.set_apply_callback(apply_5tone_frame, code.frame1)
2794
            code_5tone.append(line)
2795

    
2796
            val = RadioSettingValueString(0, 6,
2797
                                          frame2string(code.frame2), False)
2798
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2799
                                " Frame 2", val)
2800
            val.set_validate_callback(validate_5tone_frame)
2801
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2802
            code_5tone.append(line)
2803

    
2804
            val = RadioSettingValueString(0, 6,
2805
                                          frame2string(code.frame3), False)
2806
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2807
                                " Frame 3", val)
2808
            val.set_validate_callback(validate_5tone_frame)
2809
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2810
            code_5tone.append(line)
2811
            i = i + 1
2812

    
2813
        _5_tone_decode1 = RadioSetting(
2814
            "_5tone_settings._5tone_decode_call_frame1",
2815
            "5 Tone decode call Frame 1",
2816
            RadioSettingValueBoolean(
2817
                _mem._5tone_settings._5tone_decode_call_frame1))
2818
        group_5tone.append(_5_tone_decode1)
2819

    
2820
        _5_tone_decode2 = RadioSetting(
2821
            "_5tone_settings._5tone_decode_call_frame2",
2822
            "5 Tone decode call Frame 2",
2823
            RadioSettingValueBoolean(
2824
                _mem._5tone_settings._5tone_decode_call_frame2))
2825
        group_5tone.append(_5_tone_decode2)
2826

    
2827
        _5_tone_decode3 = RadioSetting(
2828
            "_5tone_settings._5tone_decode_call_frame3",
2829
            "5 Tone decode call Frame 3",
2830
            RadioSettingValueBoolean(
2831
                _mem._5tone_settings._5tone_decode_call_frame3))
2832
        group_5tone.append(_5_tone_decode3)
2833

    
2834
        _5_tone_decode_disp1 = RadioSetting(
2835
            "_5tone_settings._5tone_decode_disp_frame1",
2836
            "5 Tone decode disp Frame 1",
2837
            RadioSettingValueBoolean(
2838
                _mem._5tone_settings._5tone_decode_disp_frame1))
2839
        group_5tone.append(_5_tone_decode_disp1)
2840

    
2841
        _5_tone_decode_disp2 = RadioSetting(
2842
            "_5tone_settings._5tone_decode_disp_frame2",
2843
            "5 Tone decode disp Frame 2",
2844
            RadioSettingValueBoolean(
2845
                _mem._5tone_settings._5tone_decode_disp_frame2))
2846
        group_5tone.append(_5_tone_decode_disp2)
2847

    
2848
        _5_tone_decode_disp3 = RadioSetting(
2849
            "_5tone_settings._5tone_decode_disp_frame3",
2850
            "5 Tone decode disp Frame 3",
2851
            RadioSettingValueBoolean(
2852
                _mem._5tone_settings._5tone_decode_disp_frame3))
2853
        group_5tone.append(_5_tone_decode_disp3)
2854

    
2855
        decode_standard = _mem._5tone_settings.decode_standard
2856
        if decode_standard == 255:
2857
            decode_standard = 0
2858
        if decode_standard <= len(LIST_5TONE_STANDARDS_without_none):
2859
            line = RadioSetting("_5tone_settings.decode_standard",
2860
                                "5 Tone-decode Standard",
2861
                                RadioSettingValueList(
2862
                                    LIST_5TONE_STANDARDS_without_none,
2863
                                    LIST_5TONE_STANDARDS_without_none[
2864
                                        decode_standard]))
2865
            group_5tone.append(line)
2866
        else:
2867
            LOG.debug("Invalid decode std...")
2868

    
2869
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2870
        if _5tone_delay1 == 255:
2871
            _5tone_delay1 = 20
2872

    
2873
        if _5tone_delay1 <= len(LIST_5TONE_DELAY):
2874
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2875
                                         LIST_5TONE_DELAY[
2876
                                             _5tone_delay1])
2877
            line = RadioSetting("_5tone_settings._5tone_delay1",
2878
                                "5 Tone Delay Frame 1", list)
2879
            group_5tone.append(line)
2880
        else:
2881
            LOG.debug(
2882
                "Invalid value for 5tone delay (frame1) ! Disabling.")
2883

    
2884
        _5tone_delay2 = _mem._5tone_settings._5tone_delay2
2885
        if _5tone_delay2 == 255:
2886
            _5tone_delay2 = 20
2887
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2888

    
2889
        if _5tone_delay2 <= len(LIST_5TONE_DELAY):
2890
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2891
                                         LIST_5TONE_DELAY[
2892
                                             _5tone_delay2])
2893
            line = RadioSetting("_5tone_settings._5tone_delay2",
2894
                                "5 Tone Delay Frame 2", list)
2895
            group_5tone.append(line)
2896
        else:
2897
            LOG.debug("Invalid value for 5tone delay (frame2)! Disabling.")
2898

    
2899
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2900
        if _5tone_delay3 == 255:
2901
            _5tone_delay3 = 20
2902
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2903

    
2904
        if _5tone_delay3 <= len(LIST_5TONE_DELAY):
2905
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2906
                                         LIST_5TONE_DELAY[
2907
                                             _5tone_delay3])
2908
            line = RadioSetting("_5tone_settings._5tone_delay3",
2909
                                "5 Tone Delay Frame 3", list)
2910
            group_5tone.append(line)
2911
        else:
2912
            LOG.debug("Invalid value for 5tone delay (frame3)! Disabling.")
2913

    
2914
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2915
        if ext_length == 255:
2916
            ext_length = 0
2917
            LOG.debug("1st Tone ext length unconfigured! Resetting to 0")
2918

    
2919
        if ext_length <= len(LIST_5TONE_DELAY):
2920
            list = RadioSettingValueList(
2921
                LIST_5TONE_DELAY,
2922
                LIST_5TONE_DELAY[
2923
                    ext_length])
2924
            line = RadioSetting(
2925
                "_5tone_settings._5tone_first_digit_ext_length",
2926
                "First digit extend length", list)
2927
            group_5tone.append(line)
2928
        else:
2929
            LOG.debug("Invalid value for 5tone ext length! Disabling.")
2930

    
2931
        decode_reset_time = _mem._5tone_settings.decode_reset_time
2932
        if decode_reset_time == 255:
2933
            decode_reset_time = 59
2934
            LOG.debug("Decode reset time unconfigured. resetting.")
2935
        if decode_reset_time <= len(LIST_5TONE_RESET):
2936
            list = RadioSettingValueList(
2937
                LIST_5TONE_RESET,
2938
                LIST_5TONE_RESET[
2939
                    decode_reset_time])
2940
            line = RadioSetting("_5tone_settings.decode_reset_time",
2941
                                "Decode reset time", list)
2942
            group_5tone.append(line)
2943
        else:
2944
            LOG.debug("Invalid value decode reset time! Disabling.")
2945

    
2946
        # 2 Tone
2947
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2948
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2949

    
2950
        top.append(encode_2tone)
2951
        top.append(decode_2tone)
2952

    
2953
        duration_1st_tone = self._memobj._2tone.duration_1st_tone
2954
        if duration_1st_tone == 255:
2955
            LOG.debug("Duration of first 2 Tone digit is not yet " +
2956
                      "configured. Setting to 600ms")
2957
            duration_1st_tone = 60
2958

    
2959
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2960
            val = RadioSettingValueList(LIST_5TONE_DELAY,
2961
                                        LIST_5TONE_DELAY[
2962
                                            duration_1st_tone])
2963
            line = RadioSetting("_2tone.duration_1st_tone",
2964
                                "Duration 1st Tone", val)
2965
            encode_2tone.append(line)
2966

    
2967
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2968
        if duration_2nd_tone == 255:
2969
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2970
                      "configured. Setting to 600ms")
2971
            duration_2nd_tone = 60
2972

    
2973
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2974
            val = RadioSettingValueList(LIST_5TONE_DELAY,
2975
                                        LIST_5TONE_DELAY[
2976
                                            duration_2nd_tone])
2977
            line = RadioSetting("_2tone.duration_2nd_tone",
2978
                                "Duration 2nd Tone", val)
2979
            encode_2tone.append(line)
2980

    
2981
        duration_gap = self._memobj._2tone.duration_gap
2982
        if duration_gap == 255:
2983
            LOG.debug("Duration of gap is not yet " +
2984
                      "configured. Setting to 300ms")
2985
            duration_gap = 30
2986

    
2987
        if duration_gap <= len(LIST_5TONE_DELAY):
2988
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2989
                                RadioSettingValueList(LIST_5TONE_DELAY,
2990
                                                      LIST_5TONE_DELAY[
2991
                                                          duration_gap]))
2992
            encode_2tone.append(line)
2993

    
2994
        def _2tone_validate(value):
2995
            if value == 0:
2996
                return 65535
2997
            if value == 65535:
2998
                return value
2999
            if not (300 <= value and value <= 3000):
3000
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
3001
                raise InvalidValueError(msg)
3002
            return value
3003

    
3004
        def apply_2tone_freq(setting, obj):
3005
            val = int(setting.value)
3006
            if (val == 0) or (val == 65535):
3007
                obj.set_value(65535)
3008
            else:
3009
                obj.set_value(val)
3010

    
3011
        i = 1
3012
        for code in self._memobj._2tone._2tone_encode:
3013
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
3014
                                           "Encode Code " + str(i))
3015
            encode_2tone.append(code_2tone)
3016

    
3017
            tmp = code.freq1
3018
            if tmp == 65535:
3019
                tmp = 0
3020
            val1 = RadioSettingValueInteger(0, 65535, tmp)
3021
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
3022
                                 "Frequency 1", val1)
3023
            val1.set_validate_callback(_2tone_validate)
3024
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
3025
            code_2tone.append(freq1)
3026

    
3027
            tmp = code.freq2
3028
            if tmp == 65535:
3029
                tmp = 0
3030
            val2 = RadioSettingValueInteger(0, 65535, tmp)
3031
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
3032
                                 "Frequency 2", val2)
3033
            val2.set_validate_callback(_2tone_validate)
3034
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
3035
            code_2tone.append(freq2)
3036

    
3037
            i = i + 1
3038

    
3039
        decode_reset_time = _mem._2tone.reset_time
3040
        if decode_reset_time == 255:
3041
            decode_reset_time = 59
3042
            LOG.debug("Decode reset time unconfigured. resetting.")
3043
        if decode_reset_time <= len(LIST_5TONE_RESET):
3044
            list = RadioSettingValueList(
3045
                LIST_5TONE_RESET,
3046
                LIST_5TONE_RESET[
3047
                    decode_reset_time])
3048
            line = RadioSetting("_2tone.reset_time",
3049
                                "Decode reset time", list)
3050
            decode_2tone.append(line)
3051
        else:
3052
            LOG.debug("Invalid value decode reset time! Disabling.")
3053

    
3054
        def apply_2tone_freq_pair(setting, obj):
3055
            val = int(setting.value)
3056
            derived_val = 65535
3057
            frqname = str(setting._name[-5:])
3058
            derivedname = "derived_from_" + frqname
3059

    
3060
            if (val == 0):
3061
                val = 65535
3062
                derived_val = 65535
3063
            else:
3064
                derived_val = int(round(2304000.0/val))
3065

    
3066
            obj[frqname].set_value(val)
3067
            obj[derivedname].set_value(derived_val)
3068

    
3069
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
3070
                      derivedname + ": " + str(derived_val))
3071

    
3072
        i = 1
3073
        for decode_code in self._memobj._2tone._2tone_decode:
3074
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
3075
                                                "Decode Code " + str(i))
3076
            decode_2tone.append(_2tone_dec_code)
3077

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

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

    
3097
                val = dec.response
3098
                if val == 255:
3099
                    LOG.debug("Response for Code " +
3100
                              str(i) + " Dec " + str(j) +
3101
                              " is not yet configured. Setting to 0.")
3102
                    val = 0
3103

    
3104
                if val <= len(LIST_2TONE_RESPONSE):
3105
                    line = RadioSetting(
3106
                        "_2tone_dec_settings_" +
3107
                        str(i) + "_resp_" + str(j),
3108
                        "Response " + str(j), RadioSettingValueList
3109
                        (LIST_2TONE_RESPONSE,
3110
                         LIST_2TONE_RESPONSE[val]))
3111
                    line.set_apply_callback(apply_list_value, dec.response)
3112
                    _2tone_dec_code.append(line)
3113
                else:
3114
                    LOG.debug(
3115
                        "Invalid value for 2tone response! Disabling.")
3116

    
3117
                val = dec.alert
3118
                if val == 255:
3119
                    LOG.debug("Alert for Code " +
3120
                              str(i) + " Dec " + str(j) +
3121
                              " is not yet configured. Setting to 0.")
3122
                    val = 0
3123

    
3124
                if val <= len(PTTIDCODE_LIST):
3125
                    line = RadioSetting(
3126
                        "_2tone_dec_settings_" +
3127
                        str(i) + "_alert_" + str(j),
3128
                        "Alert " + str(j), RadioSettingValueList
3129
                        (PTTIDCODE_LIST,
3130
                         PTTIDCODE_LIST[val]))
3131
                    line.set_apply_callback(apply_list_value, dec.alert)
3132
                    _2tone_dec_code.append(line)
3133
                else:
3134
                    LOG.debug("Invalid value for 2tone alert! Disabling.")
3135
                j = j + 1
3136

    
3137
            freq = self._memobj._2tone.freqs[i-1]
3138
            for char in ['A', 'B', 'C', 'D']:
3139
                setting_name = "freq" + str(char)
3140

    
3141
                tmp = freq[setting_name]
3142
                if tmp == 65535:
3143
                    tmp = 0
3144
                if tmp != 0:
3145
                    expected = int(round(2304000.0/tmp))
3146
                    from_mem = freq["derived_from_" + setting_name]
3147
                    if expected != from_mem:
3148
                        LOG.error("Expected " + str(expected) +
3149
                                  " but read " + str(from_mem) +
3150
                                  ". Disabling 2Tone Decode Freqs!")
3151
                        break
3152
                val = RadioSettingValueInteger(0, 65535, tmp)
3153
                frq = RadioSetting("2tone_dec_" + str(i) +
3154
                                   "_freq" + str(char),
3155
                                   ("Decode Frequency " + str(char)), val)
3156
                val.set_validate_callback(_2tone_validate)
3157
                frq.set_apply_callback(apply_2tone_freq_pair, freq)
3158
                _2tone_dec_code.append(frq)
3159

    
3160
            i = i + 1
3161

    
3162

    
3163

    
3164

    
3165

    
3166

    
3167

    
3168

    
3169

    
3170

    
3171
        if self.COLOR_LCD:
3172
            service = RadioSettingGroup("service", "Service Settings")
3173
            top.append(service)
3174

    
3175
            for band in ["vhf", "uhf"]:
3176
                for index in range(0, 10):
3177
                    key = "squelch_new.%s.sql%i" % (band, index)
3178
                    if band == "vhf":
3179
                        _obj = self._memobj.squelch_new.vhf
3180
                    elif band == "vhf2":
3181
                        _obj = self._memobj.squelch_new.vhf2
3182
                    elif band == "uhf":
3183
                        _obj = self._memobj.squelch_new.uhf
3184
                    elif band == "uhf2":
3185
                        _obj = self._memobj.squelch_new.uhf2
3186
                    name = "%s Squelch %i" % (band.upper(), index)
3187
                    rs = RadioSetting(key, name,
3188
                                      RadioSettingValueInteger(
3189
                                          0, 100,
3190
                                          getattr(_obj, "sql%i" % (index))))
3191
                    service.append(rs)
3192

    
3193
            if self.BANDS == 4:
3194
                for band in ["vhf2", "uhf2"]:
3195
                    for index in range(0, 10):
3196
                        key = "squelch_new.%s.sql%i" % (band, index)
3197
                        if band == "vhf":
3198
                            _obj = self._memobj.squelch_new.vhf
3199
                        elif band == "vhf2":
3200
                            _obj = self._memobj.squelch_new.vhf2
3201
                        elif band == "uhf":
3202
                            _obj = self._memobj.squelch_new.uhf
3203
                        elif band == "uhf2":
3204
                            _obj = self._memobj.squelch_new.uhf2
3205
                        name = "%s Squelch %i" % (band.upper(), index)
3206
                        rs = RadioSetting(key, name,
3207
                                          RadioSettingValueInteger(
3208
                                              0, 100,
3209
                                              getattr(_obj, "sql%i" % (index))))
3210
                        service.append(rs)
3211

    
3212

    
3213

    
3214

    
3215

    
3216

    
3217

    
3218

    
3219

    
3220

    
3221
        return top
3222

    
3223
    def set_settings(self, settings):
3224
        _settings = self._memobj.settings
3225
        for element in settings:
3226
            if not isinstance(element, RadioSetting):
3227
                if element.get_name() == "fm_preset":
3228
                    self._set_fm_preset(element)
3229
                else:
3230
                    self.set_settings(element)
3231
                    continue
3232
            else:
3233
                try:
3234
                    name = element.get_name()
3235
                    if "." in name:
3236
                        bits = name.split(".")
3237
                        obj = self._memobj
3238
                        for bit in bits[:-1]:
3239
                            if "/" in bit:
3240
                                bit, index = bit.split("/", 1)
3241
                                index = int(index)
3242
                                obj = getattr(obj, bit)[index]
3243
                            else:
3244
                                obj = getattr(obj, bit)
3245
                        setting = bits[-1]
3246
                    else:
3247
                        obj = _settings
3248
                        setting = element.get_name()
3249

    
3250
                    if element.has_apply_callback():
3251
                        LOG.debug("Using apply callback")
3252
                        element.run_apply_callback()
3253
                    elif setting == "volume" and self.MODEL == "KT-WP12":
3254
                        setattr(obj, setting, int(element.value) - 1)
3255
                    elif setting == "volume" and self.MODEL == "WP-9900":
3256
                        setattr(obj, setting, int(element.value) - 1)
3257
                    elif element.value.get_mutable():
3258
                        LOG.debug("Setting %s = %s" % (setting, element.value))
3259
                        setattr(obj, setting, element.value)
3260
                except Exception, e:
3261
                    LOG.debug(element.get_name())
3262
                    raise
3263

    
3264
    @classmethod
3265
    def match_model(cls, filedata, filename):
3266
        match_size = False
3267
        match_model = False
3268

    
3269
        # testing the file data size
3270
        if len(filedata) == MEM_SIZE:
3271
            match_size = True
3272

    
3273
        # testing the firmware model fingerprint
3274
        match_model = model_match(cls, filedata)
3275

    
3276
        if match_size and match_model:
3277
            return True
3278
        else:
3279
            return False
3280

    
3281

    
3282
MEM_FORMAT = """
3283
#seekto 0x0000;
3284
struct {
3285
  lbcd rxfreq[4];
3286
  lbcd txfreq[4];
3287
  ul16 rxtone;
3288
  ul16 txtone;
3289
  u8 unknown0:4,
3290
     scode:4;
3291
  u8 unknown1:2,
3292
     spmute:2,
3293
     unknown2:2,
3294
     optsig:2;
3295
  u8 unknown3:3,
3296
     scramble:1,
3297
     unknown4:3,
3298
     power:1;
3299
  u8 unknown5:1,
3300
     wide:1,
3301
     unknown6:2,
3302
     bcl:1,
3303
     add:1,
3304
     pttid:2;
3305
} memory[200];
3306

    
3307
#seekto 0x0E00;
3308
struct {
3309
  u8 tdr;
3310
  u8 unknown1;
3311
  u8 sql;
3312
  u8 unknown2[2];
3313
  u8 tot;
3314
  u8 apo;           // BTech radios use this as the Auto Power Off time
3315
                    // other radios use this as pre-Time Out Alert
3316
  u8 unknown3;
3317
  u8 abr;
3318
  u8 beep;
3319
  u8 unknown4[4];
3320
  u8 dtmfst;
3321
  u8 unknown5[2];
3322
  u8 prisc;
3323
  u8 prich;
3324
  u8 screv;
3325
  u8 unknown6[2];
3326
  u8 pttid;
3327
  u8 pttlt;
3328
  u8 unknown7;
3329
  u8 emctp;
3330
  u8 emcch;
3331
  u8 ringt;
3332
  u8 unknown8;
3333
  u8 camdf;
3334
  u8 cbmdf;
3335
  u8 sync;          // BTech radios use this as the display sync setting
3336
                    // other radios use this as the auto keypad lock setting
3337
  u8 ponmsg;
3338
  u8 wtled;
3339
  u8 rxled;
3340
  u8 txled;
3341
  u8 unknown9[5];
3342
  u8 anil;
3343
  u8 reps;
3344
  u8 repm;
3345
  u8 tdrab;
3346
  u8 ste;
3347
  u8 rpste;
3348
  u8 rptdl;
3349
  u8 mgain;
3350
  u8 dtmfg;
3351
} settings;
3352

    
3353
#seekto 0x0E80;
3354
struct {
3355
  u8 unknown1;
3356
  u8 vfomr;
3357
  u8 keylock;
3358
  u8 unknown2;
3359
  u8 unknown3:4,
3360
     vfomren:1,
3361
     unknown4:1,
3362
     reseten:1,
3363
     menuen:1;
3364
  u8 unknown5[11];
3365
  u8 dispab;
3366
  u8 mrcha;
3367
  u8 mrchb;
3368
  u8 menu;
3369
} settings2;
3370

    
3371
#seekto 0x0EC0;
3372
struct {
3373
  char line1[6];
3374
  char line2[6];
3375
} poweron_msg;
3376

    
3377
struct settings_vfo {
3378
  u8 freq[8];
3379
  u8 offset[6];
3380
  u8 unknown2[2];
3381
  ul16 rxtone;
3382
  ul16 txtone;
3383
  u8 scode;
3384
  u8 spmute;
3385
  u8 optsig;
3386
  u8 scramble;
3387
  u8 wide;
3388
  u8 power;
3389
  u8 shiftd;
3390
  u8 step;
3391
  u8 unknown3[4];
3392
};
3393

    
3394
#seekto 0x0F00;
3395
struct {
3396
  struct settings_vfo a;
3397
  struct settings_vfo b;
3398
} vfo;
3399

    
3400
#seekto 0x1000;
3401
struct {
3402
  char name[6];
3403
  u8 unknown1[10];
3404
} names[200];
3405

    
3406
#seekto 0x2400;
3407
struct {
3408
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3409
  u8 group_tone;
3410
  u8 repeat_tone;
3411
  u8 unused[13];
3412
} _5tone_std_settings[15];
3413

    
3414
#seekto 0x2500;
3415
struct {
3416
  u8 frame1[5];
3417
  u8 frame2[5];
3418
  u8 frame3[5];
3419
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3420
} _5tone_codes[15];
3421

    
3422
#seekto 0x25F0;
3423
struct {
3424
  u8 _5tone_delay1; // * 10ms
3425
  u8 _5tone_delay2; // * 10ms
3426
  u8 _5tone_delay3; // * 10ms
3427
  u8 _5tone_first_digit_ext_length;
3428
  u8 unknown1;
3429
  u8 unknown2;
3430
  u8 unknown3;
3431
  u8 unknown4;
3432
  u8 decode_standard;
3433
  u8 unknown5:5,
3434
     _5tone_decode_call_frame3:1,
3435
     _5tone_decode_call_frame2:1,
3436
     _5tone_decode_call_frame1:1;
3437
  u8 unknown6:5,
3438
     _5tone_decode_disp_frame3:1,
3439
     _5tone_decode_disp_frame2:1,
3440
     _5tone_decode_disp_frame1:1;
3441
  u8 decode_reset_time; // * 100 + 100ms
3442
} _5tone_settings;
3443

    
3444
#seekto 0x2900;
3445
struct {
3446
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3447
} dtmf_codes[15];
3448

    
3449
#seekto 0x29F0;
3450
struct {
3451
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3452
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3453
  u8 unknown0[14];
3454
  u8 inspection[16];
3455
  u8 monitor[16];
3456
  u8 alarmcode[16];
3457
  u8 stun[16];
3458
  u8 kill[16];
3459
  u8 revive[16];
3460
  u8 unknown1[16];
3461
  u8 unknown2[16];
3462
  u8 unknown3[16];
3463
  u8 unknown4[16];
3464
  u8 unknown5[16];
3465
  u8 unknown6[16];
3466
  u8 unknown7[16];
3467
  u8 masterid[16];
3468
  u8 viceid[16];
3469
  u8 unused01:7,
3470
     mastervice:1;
3471
  u8 unused02:3,
3472
     mrevive:1,
3473
     mkill:1,
3474
     mstun:1,
3475
     mmonitor:1,
3476
     minspection:1;
3477
  u8 unused03:3,
3478
     vrevive:1,
3479
     vkill:1,
3480
     vstun:1,
3481
     vmonitor:1,
3482
     vinspection:1;
3483
  u8 unused04:6,
3484
     txdisable:1,
3485
     rxdisable:1;
3486
  u8 groupcode;
3487
  u8 spacecode;
3488
  u8 delayproctime; // * 100 + 100ms
3489
  u8 resettime;     // * 100 + 100ms
3490
} dtmf_settings;
3491

    
3492
#seekto 0x2D00;
3493
struct {
3494
  struct {
3495
    ul16 freq1;
3496
    u8 unused01[6];
3497
    ul16 freq2;
3498
    u8 unused02[6];
3499
  } _2tone_encode[15];
3500
  u8 duration_1st_tone; // *10ms
3501
  u8 duration_2nd_tone; // *10ms
3502
  u8 duration_gap;      // *10ms
3503
  u8 unused03[13];
3504
  struct {
3505
    struct {
3506
      u8 dec;      // one out of LIST_2TONE_DEC
3507
      u8 response; // one out of LIST_2TONE_RESPONSE
3508
      u8 alert;    // 1-16
3509
    } decs[4];
3510
    u8 unused04[4];
3511
  } _2tone_decode[15];
3512
  u8 unused05[16];
3513

    
3514
  struct {
3515
    ul16 freqA;
3516
    ul16 freqB;
3517
    ul16 freqC;
3518
    ul16 freqD;
3519
    // unknown what those values mean, but they are
3520
    // derived from configured frequencies
3521
    ul16 derived_from_freqA; // 2304000/freqA
3522
    ul16 derived_from_freqB; // 2304000/freqB
3523
    ul16 derived_from_freqC; // 2304000/freqC
3524
    ul16 derived_from_freqD; // 2304000/freqD
3525
  }freqs[15];
3526
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3527
} _2tone;
3528

    
3529
#seekto 0x3000;
3530
struct {
3531
  u8 freq[8];
3532
  char broadcast_station_name[6];
3533
  u8 unknown[2];
3534
} fm_radio_preset[16];
3535

    
3536
#seekto 0x3C90;
3537
struct {
3538
  u8 vhf_low[3];
3539
  u8 vhf_high[3];
3540
  u8 uhf_low[3];
3541
  u8 uhf_high[3];
3542
} ranges;
3543

    
3544
// the UV-2501+220 & KT8900R has different zones for storing ranges
3545

    
3546
#seekto 0x3CD0;
3547
struct {
3548
  u8 vhf_low[3];
3549
  u8 vhf_high[3];
3550
  u8 unknown1[4];
3551
  u8 unknown2[6];
3552
  u8 vhf2_low[3];
3553
  u8 vhf2_high[3];
3554
  u8 unknown3[4];
3555
  u8 unknown4[6];
3556
  u8 uhf_low[3];
3557
  u8 uhf_high[3];
3558
} ranges220;
3559

    
3560
#seekto 0x3F70;
3561
struct {
3562
  char fp[6];
3563
} fingerprint;
3564

    
3565
"""
3566

    
3567

    
3568
class BTech(BTechMobileCommon):
3569
    """BTECH's UV-5001 and alike radios"""
3570
    BANDS = 2
3571
    COLOR_LCD = False
3572
    NAME_LENGTH = 6
3573

    
3574
    def set_options(self):
3575
        """This is to read the options from the image and set it in the
3576
        environment, for now just the limits of the freqs in the VHF/UHF
3577
        ranges"""
3578

    
3579
        # setting the correct ranges for each radio type
3580
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3581
            # the model 2501+220 has a segment in 220
3582
            # and a different position in the memmap
3583
            # also the QYT KT8900R
3584
            ranges = self._memobj.ranges220
3585
        else:
3586
            ranges = self._memobj.ranges
3587

    
3588
        # the normal dual bands
3589
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3590
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3591

    
3592
        # DEBUG
3593
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3594
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3595

    
3596
        # 220Mhz radios case
3597
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3598
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3599
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3600
            self._220_range = vhf2
3601

    
3602
        # set the class with the real data
3603
        self._vhf_range = vhf
3604
        self._uhf_range = uhf
3605

    
3606
    def process_mmap(self):
3607
        """Process the mem map into the mem object"""
3608

    
3609
        # Get it
3610
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3611

    
3612
        # load specific parameters from the radio image
3613
        self.set_options()
3614

    
3615

    
3616
# Declaring Aliases (Clones of the real radios)
3617
class JT2705M(chirp_common.Alias):
3618
    VENDOR = "Jetstream"
3619
    MODEL = "JT2705M"
3620

    
3621

    
3622
class JT6188Mini(chirp_common.Alias):
3623
    VENDOR = "Juentai"
3624
    MODEL = "JT-6188 Mini"
3625

    
3626

    
3627
class JT6188Plus(chirp_common.Alias):
3628
    VENDOR = "Juentai"
3629
    MODEL = "JT-6188 Plus"
3630

    
3631

    
3632
class SSGT890(chirp_common.Alias):
3633
    VENDOR = "Sainsonic"
3634
    MODEL = "GT-890"
3635

    
3636

    
3637
class ZastoneMP300(chirp_common.Alias):
3638
    VENDOR = "Zastone"
3639
    MODEL = "MP-300"
3640

    
3641

    
3642
# real radios
3643
@directory.register
3644
class UV2501(BTech):
3645
    """Baofeng Tech UV2501"""
3646
    MODEL = "UV-2501"
3647
    _fileid = [UV2501G3_fp,
3648
               UV2501G2_fp,
3649
               UV2501pp2_fp,
3650
               UV2501pp_fp]
3651

    
3652

    
3653
@directory.register
3654
class UV2501_220(BTech):
3655
    """Baofeng Tech UV2501+220"""
3656
    MODEL = "UV-2501+220"
3657
    BANDS = 3
3658
    _magic = MSTRING_220
3659
    _fileid = [UV2501_220G3_fp,
3660
               UV2501_220G2_fp,
3661
               UV2501_220_fp,
3662
               UV2501_220pp_fp]
3663

    
3664

    
3665
@directory.register
3666
class UV5001(BTech):
3667
    """Baofeng Tech UV5001"""
3668
    MODEL = "UV-5001"
3669
    _fileid = [UV5001G3_fp,
3670
               UV5001G22_fp,
3671
               UV5001G2_fp,
3672
               UV5001alpha_fp,
3673
               UV5001pp_fp]
3674
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3675
                     chirp_common.PowerLevel("Low", watts=10)]
3676

    
3677

    
3678
@directory.register
3679
class MINI8900(BTech):
3680
    """WACCOM MINI-8900"""
3681
    VENDOR = "WACCOM"
3682
    MODEL = "MINI-8900"
3683
    _magic = MSTRING_MINI8900
3684
    _fileid = [MINI8900_fp, ]
3685
    # Clones
3686
    ALIASES = [JT6188Plus, ]
3687

    
3688

    
3689
@directory.register
3690
class KTUV980(BTech):
3691
    """QYT KT-UV980"""
3692
    VENDOR = "QYT"
3693
    MODEL = "KT-UV980"
3694
    _vhf_range = (136000000, 175000000)
3695
    _uhf_range = (400000000, 481000000)
3696
    _magic = MSTRING_MINI8900
3697
    _fileid = [KTUV980_fp, ]
3698
    # Clones
3699
    ALIASES = [JT2705M, ]
3700

    
3701
# Please note that there is a version of this radios that is a clone of the
3702
# Waccom Mini8900, maybe an early version?
3703

    
3704

    
3705
class OTGRadioV1(chirp_common.Alias):
3706
    VENDOR = 'OTGSTUFF'
3707
    MODEL = 'OTG Radio v1'
3708

    
3709

    
3710
@directory.register
3711
class KT9800(BTech):
3712
    """QYT KT8900"""
3713
    VENDOR = "QYT"
3714
    MODEL = "KT8900"
3715
    _vhf_range = (136000000, 175000000)
3716
    _uhf_range = (400000000, 481000000)
3717
    _magic = MSTRING_KT8900
3718
    _fileid = [KT8900_fp,
3719
               KT8900_fp1,
3720
               KT8900_fp2,
3721
               KT8900_fp3,
3722
               KT8900_fp4,
3723
               KT8900_fp5,
3724
               KT8900_fp6,
3725
               KT8900_fp7]
3726
    # Clones
3727
    ALIASES = [JT6188Mini, SSGT890, ZastoneMP300]
3728

    
3729

    
3730
@directory.register
3731
class KT9800R(BTech):
3732
    """QYT KT8900R"""
3733
    VENDOR = "QYT"
3734
    MODEL = "KT8900R"
3735
    BANDS = 3
3736
    _vhf_range = (136000000, 175000000)
3737
    _220_range = (240000000, 271000000)
3738
    _uhf_range = (400000000, 481000000)
3739
    _magic = MSTRING_KT8900R
3740
    _fileid = [KT8900R_fp,
3741
               KT8900R_fp1,
3742
               KT8900R_fp2,
3743
               KT8900R_fp3,
3744
               KT8900R_fp4,
3745
               KT8900R_fp5]
3746

    
3747

    
3748
@directory.register
3749
class LT588UV(BTech):
3750
    """LUITON LT-588UV"""
3751
    VENDOR = "LUITON"
3752
    MODEL = "LT-588UV"
3753
    _vhf_range = (136000000, 175000000)
3754
    _uhf_range = (400000000, 481000000)
3755
    _magic = MSTRING_KT8900
3756
    _fileid = [LT588UV_fp,
3757
               LT588UV_fp1]
3758
    _power_levels = [chirp_common.PowerLevel("High", watts=60),
3759
                     chirp_common.PowerLevel("Low", watts=10)]
3760

    
3761

    
3762
COLOR_MEM_FORMAT = """
3763
#seekto 0x0000;
3764
struct {
3765
  lbcd rxfreq[4];
3766
  lbcd txfreq[4];
3767
  ul16 rxtone;
3768
  ul16 txtone;
3769
  u8 unknown0:4,
3770
     scode:4;
3771
  u8 unknown1:2,
3772
     spmute:2,
3773
     unknown2:2,
3774
     optsig:2;
3775
  u8 unknown3:3,
3776
     scramble:1,
3777
     unknown4:2,
3778
     power:2;
3779
  u8 unknown5:1,
3780
     wide:1,
3781
     unknown6:2,
3782
     bcl:1,
3783
     add:1,
3784
     pttid:2;
3785
} memory[200];
3786

    
3787
#seekto 0x0E00;
3788
struct {
3789
  u8 tmr;
3790
  u8 unknown1;
3791
  u8 sql;
3792
  u8 unknown2;
3793
  u8 mgain2;
3794
  u8 tot;
3795
  u8 apo;
3796
  u8 unknown3;
3797
  u8 abr;
3798
  u8 beep;
3799
  u8 unknown4[4];
3800
  u8 dtmfst;
3801
  u8 unknown5[2];
3802
  u8 screv;
3803
  u8 unknown6[2];
3804
  u8 pttid;
3805
  u8 pttlt;
3806
  u8 unknown7;
3807
  u8 emctp;
3808
  u8 emcch;
3809
  u8 sigbp;
3810
  u8 unknown8;
3811
  u8 camdf;
3812
  u8 cbmdf;
3813
  u8 ccmdf;
3814
  u8 cdmdf;
3815
  u8 langua;
3816
  u8 sync;          // BTech radios use this as the display sync
3817
                    // setting, other radios use this as the auto
3818
                    // keypad lock setting
3819
  u8 mainfc;
3820
  u8 mainbc;
3821
  u8 menufc;
3822
  u8 menubc;
3823
  u8 stafc;
3824
  u8 stabc;
3825
  u8 sigfc;
3826
  u8 sigbc;
3827
  u8 rxfc;
3828
  u8 txfc;
3829
  u8 txdisp;
3830
  u8 unknown9[5];
3831
  u8 anil;
3832
  u8 reps;
3833
  u8 repm;
3834
  u8 tmrmr;
3835
  u8 ste;
3836
  u8 rpste;
3837
  u8 rptdl;
3838
  u8 dtmfg;
3839
  u8 mgain;         // used by db25-g for ponyey
3840
  u8 skiptx;
3841
  u8 scmode;
3842
  u8 tmrtx;
3843
} settings;
3844

    
3845
#seekto 0x0E80;
3846
struct {
3847
  u8 unknown1;
3848
  u8 vfomr;
3849
  u8 keylock;
3850
  u8 unknown2;
3851
  u8 unknown3:4,
3852
     vfomren:1,
3853
     unknown4:1,
3854
     reseten:1,
3855
     menuen:1;
3856
  u8 unknown5[11];
3857
  u8 dispab;
3858
  u8 unknown6[2];
3859
  u8 menu;
3860
  u8 unknown7[7];
3861
  u8 vfomra;
3862
  u8 vfomrb;
3863
  u8 vfomrc;
3864
  u8 vfomrd;
3865
  u8 mrcha;
3866
  u8 mrchb;
3867
  u8 mrchc;
3868
  u8 mrchd;
3869
} settings2;
3870

    
3871
struct settings_vfo {
3872
  u8 freq[8];
3873
  u8 offset[6];
3874
  u8 unknown2[2];
3875
  ul16 rxtone;
3876
  ul16 txtone;
3877
  u8 scode;
3878
  u8 spmute;
3879
  u8 optsig;
3880
  u8 scramble;
3881
  u8 wide;
3882
  u8 power;
3883
  u8 shiftd;
3884
  u8 step;
3885
  u8 unknown3[4];
3886
};
3887

    
3888
#seekto 0x0F00;
3889
struct {
3890
  struct settings_vfo a;
3891
  struct settings_vfo b;
3892
  struct settings_vfo c;
3893
  struct settings_vfo d;
3894
} vfo;
3895

    
3896
#seekto 0x0F80;
3897
struct {
3898
  char line1[8];
3899
  char line2[8];
3900
  char line3[8];
3901
  char line4[8];
3902
  char line5[8];
3903
  char line6[8];
3904
  char line7[8];
3905
  char line8[8];
3906
} poweron_msg;
3907

    
3908
#seekto 0x1000;
3909
struct {
3910
  char name[8];
3911
  u8 unknown1[8];
3912
} names[200];
3913

    
3914
#seekto 0x2400;
3915
struct {
3916
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3917
  u8 group_tone;
3918
  u8 repeat_tone;
3919
  u8 unused[13];
3920
} _5tone_std_settings[15];
3921

    
3922
#seekto 0x2500;
3923
struct {
3924
  u8 frame1[5];
3925
  u8 frame2[5];
3926
  u8 frame3[5];
3927
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3928
} _5tone_codes[15];
3929

    
3930
#seekto 0x25F0;
3931
struct {
3932
  u8 _5tone_delay1; // * 10ms
3933
  u8 _5tone_delay2; // * 10ms
3934
  u8 _5tone_delay3; // * 10ms
3935
  u8 _5tone_first_digit_ext_length;
3936
  u8 unknown1;
3937
  u8 unknown2;
3938
  u8 unknown3;
3939
  u8 unknown4;
3940
  u8 decode_standard;
3941
  u8 unknown5:5,
3942
     _5tone_decode_call_frame3:1,
3943
     _5tone_decode_call_frame2:1,
3944
     _5tone_decode_call_frame1:1;
3945
  u8 unknown6:5,
3946
     _5tone_decode_disp_frame3:1,
3947
     _5tone_decode_disp_frame2:1,
3948
     _5tone_decode_disp_frame1:1;
3949
  u8 decode_reset_time; // * 100 + 100ms
3950
} _5tone_settings;
3951

    
3952
#seekto 0x2900;
3953
struct {
3954
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3955
} dtmf_codes[15];
3956

    
3957
#seekto 0x29F0;
3958
struct {
3959
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3960
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3961
  u8 unknown0[14];
3962
  u8 inspection[16];
3963
  u8 monitor[16];
3964
  u8 alarmcode[16];
3965
  u8 stun[16];
3966
  u8 kill[16];
3967
  u8 revive[16];
3968
  u8 unknown1[16];
3969
  u8 unknown2[16];
3970
  u8 unknown3[16];
3971
  u8 unknown4[16];
3972
  u8 unknown5[16];
3973
  u8 unknown6[16];
3974
  u8 unknown7[16];
3975
  u8 masterid[16];
3976
  u8 viceid[16];
3977
  u8 unused01:7,
3978
     mastervice:1;
3979
  u8 unused02:3,
3980
     mrevive:1,
3981
     mkill:1,
3982
     mstun:1,
3983
     mmonitor:1,
3984
     minspection:1;
3985
  u8 unused03:3,
3986
     vrevive:1,
3987
     vkill:1,
3988
     vstun:1,
3989
     vmonitor:1,
3990
     vinspection:1;
3991
  u8 unused04:6,
3992
     txdisable:1,
3993
     rxdisable:1;
3994
  u8 groupcode;
3995
  u8 spacecode;
3996
  u8 delayproctime; // * 100 + 100ms
3997
  u8 resettime;     // * 100 + 100ms
3998
} dtmf_settings;
3999

    
4000
#seekto 0x2D00;
4001
struct {
4002
  struct {
4003
    ul16 freq1;
4004
    u8 unused01[6];
4005
    ul16 freq2;
4006
    u8 unused02[6];
4007
  } _2tone_encode[15];
4008
  u8 duration_1st_tone; // *10ms
4009
  u8 duration_2nd_tone; // *10ms
4010
  u8 duration_gap;      // *10ms
4011
  u8 unused03[13];
4012
  struct {
4013
    struct {
4014
      u8 dec;      // one out of LIST_2TONE_DEC
4015
      u8 response; // one out of LIST_2TONE_RESPONSE
4016
      u8 alert;    // 1-16
4017
    } decs[4];
4018
    u8 unused04[4];
4019
  } _2tone_decode[15];
4020
  u8 unused05[16];
4021

    
4022
  struct {
4023
    ul16 freqA;
4024
    ul16 freqB;
4025
    ul16 freqC;
4026
    ul16 freqD;
4027
    // unknown what those values mean, but they are
4028
    // derived from configured frequencies
4029
    ul16 derived_from_freqA; // 2304000/freqA
4030
    ul16 derived_from_freqB; // 2304000/freqB
4031
    ul16 derived_from_freqC; // 2304000/freqC
4032
    ul16 derived_from_freqD; // 2304000/freqD
4033
  }freqs[15];
4034
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4035
} _2tone;
4036

    
4037
struct squelch {
4038
  u8 sql0;
4039
  u8 sql1;
4040
  u8 sql2;
4041
  u8 sql3;
4042
  u8 sql4;
4043
  u8 sql5;
4044
  u8 sql6;
4045
  u8 sql7;
4046
  u8 sql8;
4047
  u8 sql9;
4048
};
4049

    
4050
#seekto 0x3C80;
4051
struct {
4052
  struct squelch vhf;
4053
  u8 unknown1[6];
4054
  u8 unknown2[16];
4055
  struct squelch vhf2;
4056
  u8 unknown3[6];
4057
  u8 unknown4[16];
4058
  struct squelch uhf;
4059
  u8 unknown5[6];
4060
  u8 unknown6[16];
4061
  struct squelch uhf2;
4062
} squelch_new;
4063

    
4064
#seekto 0x3D80;
4065
struct {
4066
  u8 vhf_low[3];
4067
  u8 vhf_high[3];
4068
  u8 unknown1[4];
4069
  u8 unknown2[6];
4070
  u8 vhf2_low[3];
4071
  u8 vhf2_high[3];
4072
  u8 unknown3[4];
4073
  u8 unknown4[6];
4074
  u8 uhf_low[3];
4075
  u8 uhf_high[3];
4076
  u8 unknown5[4];
4077
  u8 unknown6[6];
4078
  u8 uhf2_low[3];
4079
  u8 uhf2_high[3];
4080
} ranges;
4081

    
4082
#seekto 0x3F70;
4083
struct {
4084
  char fp[6];
4085
} fingerprint;
4086

    
4087
"""
4088

    
4089

    
4090
class BTechColor(BTechMobileCommon):
4091
    """BTECH's Color LCD Mobile and alike radios"""
4092
    COLOR_LCD = True
4093
    NAME_LENGTH = 8
4094
    LIST_TMR = LIST_TMR16
4095
    ##UPLOAD_MEM_SIZE = 0X3100
4096
    UPLOAD_MEM_SIZE = 0X3D00
4097

    
4098
    def process_mmap(self):
4099
        """Process the mem map into the mem object"""
4100

    
4101
        # Get it
4102
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
4103

    
4104
        # load specific parameters from the radio image
4105
        self.set_options()
4106

    
4107
    def set_options(self):
4108
        """This is to read the options from the image and set it in the
4109
        environment, for now just the limits of the freqs in the VHF/UHF
4110
        ranges"""
4111

    
4112
        # setting the correct ranges for each radio type
4113
        ranges = self._memobj.ranges
4114

    
4115
        # the normal dual bands
4116
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4117
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4118

    
4119
        # DEBUG
4120
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4121
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4122

    
4123
        # the additional bands
4124
        if self.MODEL in ["UV-25X4", "KT7900D"]:
4125
            # 200Mhz band
4126
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
4127
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
4128
            self._220_range = vhf2
4129

    
4130
            # 350Mhz band
4131
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4132
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4133
            self._350_range = uhf2
4134

    
4135
        # set the class with the real data
4136
        self._vhf_range = vhf
4137
        self._uhf_range = uhf
4138

    
4139

    
4140
# Declaring Aliases (Clones of the real radios)
4141
class SKT8900D(chirp_common.Alias):
4142
    VENDOR = "Surecom"
4143
    MODEL = "S-KT8900D"
4144

    
4145

    
4146
class QB25(chirp_common.Alias):
4147
    VENDOR = "Radioddity"
4148
    MODEL = "QB25"
4149

    
4150

    
4151
# real radios
4152
@directory.register
4153
class UV25X2(BTechColor):
4154
    """Baofeng Tech UV25X2"""
4155
    MODEL = "UV-25X2"
4156
    BANDS = 2
4157
    _vhf_range = (130000000, 180000000)
4158
    _uhf_range = (400000000, 521000000)
4159
    _magic = MSTRING_UV25X2
4160
    _fileid = [UV25X2_fp, ]
4161

    
4162

    
4163
@directory.register
4164
class UV25X4(BTechColor):
4165
    """Baofeng Tech UV25X4"""
4166
    MODEL = "UV-25X4"
4167
    BANDS = 4
4168
    _vhf_range = (130000000, 180000000)
4169
    _220_range = (200000000, 271000000)
4170
    _uhf_range = (400000000, 521000000)
4171
    _350_range = (350000000, 391000000)
4172
    _magic = MSTRING_UV25X4
4173
    _fileid = [UV25X4_fp, ]
4174

    
4175

    
4176
@directory.register
4177
class UV50X2(BTechColor):
4178
    """Baofeng Tech UV50X2"""
4179
    MODEL = "UV-50X2"
4180
    BANDS = 2
4181
    _vhf_range = (130000000, 180000000)
4182
    _uhf_range = (400000000, 521000000)
4183
    _magic = MSTRING_UV25X2
4184
    _fileid = [UV50X2_fp, ]
4185
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4186
                     chirp_common.PowerLevel("Low", watts=10)]
4187

    
4188

    
4189
@directory.register
4190
class KT7900D(BTechColor):
4191
    """QYT KT7900D"""
4192
    VENDOR = "QYT"
4193
    MODEL = "KT7900D"
4194
    BANDS = 4
4195
    LIST_TMR = LIST_TMR15
4196
    _vhf_range = (136000000, 175000000)
4197
    _220_range = (200000000, 271000000)
4198
    _uhf_range = (400000000, 481000000)
4199
    _350_range = (350000000, 371000000)
4200
    _magic = MSTRING_KT8900D
4201
    _fileid = [KT7900D_fp, KT7900D_fp1, KT7900D_fp2, KT7900D_fp3, KT7900D_fp4,
4202
               KT7900D_fp5, KT7900D_fp6, KT7900D_fp7, KT7900D_fp8, QB25_fp, ]
4203
    # Clones
4204
    ALIASES = [SKT8900D, QB25, ]
4205

    
4206

    
4207
@directory.register
4208
class KT8900D(BTechColor):
4209
    """QYT KT8900D"""
4210
    VENDOR = "QYT"
4211
    MODEL = "KT8900D"
4212
    BANDS = 2
4213
    LIST_TMR = LIST_TMR15
4214
    _vhf_range = (136000000, 175000000)
4215
    _uhf_range = (400000000, 481000000)
4216
    _magic = MSTRING_KT8900D
4217
    _fileid = [KT8900D_fp3, KT8900D_fp2, KT8900D_fp1, KT8900D_fp]
4218

    
4219
    # Clones
4220
    ALIASES = [OTGRadioV1]
4221

    
4222

    
4223
@directory.register
4224
class KT5800(BTechColor):
4225
    """QYT KT5800"""
4226
    VENDOR = "QYT"
4227
    MODEL = "KT5800"
4228
    BANDS = 2
4229
    LIST_TMR = LIST_TMR15
4230
    _vhf_range = (136000000, 175000000)
4231
    _uhf_range = (400000000, 481000000)
4232
    _magic = MSTRING_KT8900D
4233
    _fileid = [KT5800_fp, ]
4234

    
4235

    
4236
@directory.register
4237
class KT980PLUS(BTechColor):
4238
    """QYT KT980PLUS"""
4239
    VENDOR = "QYT"
4240
    MODEL = "KT980PLUS"
4241
    BANDS = 2
4242
    LIST_TMR = LIST_TMR15
4243
    _vhf_range = (136000000, 175000000)
4244
    _uhf_range = (400000000, 481000000)
4245
    _magic = MSTRING_KT8900D
4246
    _fileid = [KT980PLUS_fp1, KT980PLUS_fp]
4247
    _power_levels = [chirp_common.PowerLevel("High", watts=75),
4248
                     chirp_common.PowerLevel("Low", watts=55)]
4249

    
4250
    @classmethod
4251
    def match_model(cls, filedata, filename):
4252
        # This model is only ever matched via metadata
4253
        return False
4254

    
4255

    
4256
@directory.register
4257
class DB25G(BTechColor):
4258
    """Radioddity DB25-G"""
4259
    VENDOR = "Radioddity"
4260
    MODEL = "DB25-G"
4261
    BANDS = 2
4262
    LIST_TMR = LIST_TMR15
4263
    _vhf_range = (136000000, 175000000)
4264
    _uhf_range = (400000000, 481000000)
4265
    _magic = MSTRING_KT8900D
4266
    _fileid = [DB25G_fp1, DB25G_fp]
4267
    _gmrs = True
4268
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
4269
                     chirp_common.PowerLevel("Mid", watts=15),
4270
                     chirp_common.PowerLevel("Low", watts=5)]
4271

    
4272
    @classmethod
4273
    def match_model(cls, filedata, filename):
4274
        # This model is only ever matched via metadata
4275
        return False
4276

    
4277

    
4278
GMRS_MEM_FORMAT = """
4279
#seekto 0x0000;
4280
struct {
4281
  lbcd rxfreq[4];
4282
  lbcd txfreq[4];
4283
  ul16 rxtone;
4284
  ul16 txtone;
4285
  u8 unknown0:4,
4286
     scode:4;
4287
  u8 unknown1:2,
4288
     spmute:2,
4289
     unknown2:2,
4290
     optsig:2;
4291
  u8 unknown3:3,
4292
     scramble:1,
4293
     unknown4:2,
4294
     power:2;
4295
  u8 unknown5:1,
4296
     wide:1,
4297
     unknown6:2,
4298
     bcl:1,
4299
     add:1,
4300
     pttid:2;
4301
} memory[256];
4302

    
4303
#seekto 0x1000;
4304
struct {
4305
  char name[7];
4306
  u8 unknown1[9];
4307
} names[256];
4308

    
4309
#seekto 0x2400;
4310
struct {
4311
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4312
  u8 group_tone;
4313
  u8 repeat_tone;
4314
  u8 unused[13];
4315
} _5tone_std_settings[15];
4316

    
4317
#seekto 0x2500;
4318
struct {
4319
  u8 frame1[5];
4320
  u8 frame2[5];
4321
  u8 frame3[5];
4322
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4323
} _5tone_codes[15];
4324

    
4325
#seekto 0x25F0;
4326
struct {
4327
  u8 _5tone_delay1; // * 10ms
4328
  u8 _5tone_delay2; // * 10ms
4329
  u8 _5tone_delay3; // * 10ms
4330
  u8 _5tone_first_digit_ext_length;
4331
  u8 unknown1;
4332
  u8 unknown2;
4333
  u8 unknown3;
4334
  u8 unknown4;
4335
  u8 decode_standard;
4336
  u8 unknown5:5,
4337
     _5tone_decode_call_frame3:1,
4338
     _5tone_decode_call_frame2:1,
4339
     _5tone_decode_call_frame1:1;
4340
  u8 unknown6:5,
4341
     _5tone_decode_disp_frame3:1,
4342
     _5tone_decode_disp_frame2:1,
4343
     _5tone_decode_disp_frame1:1;
4344
  u8 decode_reset_time; // * 100 + 100ms
4345
} _5tone_settings;
4346

    
4347
#seekto 0x2900;
4348
struct {
4349
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4350
} dtmf_codes[15];
4351

    
4352
#seekto 0x29F0;
4353
struct {
4354
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4355
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4356
  u8 unknown0[14];
4357
  u8 inspection[16];
4358
  u8 monitor[16];
4359
  u8 alarmcode[16];
4360
  u8 stun[16];
4361
  u8 kill[16];
4362
  u8 revive[16];
4363
  u8 unknown1[16];
4364
  u8 unknown2[16];
4365
  u8 unknown3[16];
4366
  u8 unknown4[16];
4367
  u8 unknown5[16];
4368
  u8 unknown6[16];
4369
  u8 unknown7[16];
4370
  u8 masterid[16];
4371
  u8 viceid[16];
4372
  u8 unused01:7,
4373
     mastervice:1;
4374
  u8 unused02:3,
4375
     mrevive:1,
4376
     mkill:1,
4377
     mstun:1,
4378
     mmonitor:1,
4379
     minspection:1;
4380
  u8 unused03:3,
4381
     vrevive:1,
4382
     vkill:1,
4383
     vstun:1,
4384
     vmonitor:1,
4385
     vinspection:1;
4386
  u8 unused04:6,
4387
     txdisable:1,
4388
     rxdisable:1;
4389
  u8 groupcode;
4390
  u8 spacecode;
4391
  u8 delayproctime; // * 100 + 100ms
4392
  u8 resettime;     // * 100 + 100ms
4393
} dtmf_settings;
4394

    
4395
#seekto 0x2D00;
4396
struct {
4397
  struct {
4398
    ul16 freq1;
4399
    u8 unused01[6];
4400
    ul16 freq2;
4401
    u8 unused02[6];
4402
  } _2tone_encode[15];
4403
  u8 duration_1st_tone; // *10ms
4404
  u8 duration_2nd_tone; // *10ms
4405
  u8 duration_gap;      // *10ms
4406
  u8 unused03[13];
4407
  struct {
4408
    struct {
4409
      u8 dec;      // one out of LIST_2TONE_DEC
4410
      u8 response; // one out of LIST_2TONE_RESPONSE
4411
      u8 alert;    // 1-16
4412
    } decs[4];
4413
    u8 unused04[4];
4414
  } _2tone_decode[15];
4415
  u8 unused05[16];
4416

    
4417
  struct {
4418
    ul16 freqA;
4419
    ul16 freqB;
4420
    ul16 freqC;
4421
    ul16 freqD;
4422
    // unknown what those values mean, but they are
4423
    // derived from configured frequencies
4424
    ul16 derived_from_freqA; // 2304000/freqA
4425
    ul16 derived_from_freqB; // 2304000/freqB
4426
    ul16 derived_from_freqC; // 2304000/freqC
4427
    ul16 derived_from_freqD; // 2304000/freqD
4428
  }freqs[15];
4429
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4430
} _2tone;
4431

    
4432
#seekto 0x3000;
4433
struct {
4434
  u8 freq[8];
4435
  char broadcast_station_name[6];
4436
  u8 unknown[2];
4437
} fm_radio_preset[16];
4438

    
4439
#seekto 0x3200;
4440
struct {
4441
  u8 tmr;
4442
  u8 unknown1;
4443
  u8 sql;
4444
  u8 unknown2;
4445
  u8 autolk;
4446
  u8 tot;
4447
  u8 apo;
4448
  u8 unknown3;
4449
  u8 abr;
4450
  u8 beep;
4451
  u8 unknown4[4];
4452
  u8 dtmfst;
4453
  u8 unknown5[2];
4454
  u8 screv;
4455
  u8 unknown6[2];
4456
  u8 pttid;
4457
  u8 pttlt;
4458
  u8 unknown7;
4459
  u8 emctp;
4460
  u8 emcch;
4461
  u8 sigbp;
4462
  u8 unknown8;
4463
  u8 camdf;
4464
  u8 cbmdf;
4465
  u8 ccmdf;
4466
  u8 cdmdf;
4467
  u8 langua;
4468
  u8 sync;
4469

    
4470

    
4471
  u8 stfc;
4472
  u8 mffc;
4473
  u8 sfafc;
4474
  u8 sfbfc;
4475
  u8 sfcfc;
4476
  u8 sfdfc;
4477
  u8 subfc;
4478
  u8 fmfc;
4479
  u8 sigfc;
4480
  u8 modfc;
4481
  u8 menufc;
4482
  u8 txfc;
4483
  u8 txdisp;
4484
  u8 unknown9[5];
4485
  u8 anil;
4486
  u8 reps;
4487
  u8 repm;
4488
  u8 tmrmr;
4489
  u8 ste;
4490
  u8 rpste;
4491
  u8 rptdl;
4492
  u8 dtmfg;
4493
  u8 mgain;
4494
  u8 skiptx;
4495
  u8 scmode;
4496
  u8 tmrtx;
4497
} settings;
4498

    
4499
#seekto 0x3280;
4500
struct {
4501
  u8 unknown1;
4502
  u8 vfomr;
4503
  u8 keylock;
4504
  u8 unknown2;
4505
  u8 unknown3:4,
4506
     vfomren:1,
4507
     unknown4:1,
4508
     reseten:1,
4509
     menuen:1;
4510
  u8 unknown5[11];
4511
  u8 dispab;
4512
  u8 unknown6[2];
4513
  u8 smenu;
4514
  u8 unknown7[7];
4515
  u8 vfomra;
4516
  u8 vfomrb;
4517
  u8 vfomrc;
4518
  u8 vfomrd;
4519
  u8 mrcha;
4520
  u8 mrchb;
4521
  u8 mrchc;
4522
  u8 mrchd;
4523
} settings2;
4524

    
4525
struct settings_vfo {
4526
  u8 freq[8];
4527
  u8 offset[6];
4528
  u8 unknown2[2];
4529
  ul16 rxtone;
4530
  ul16 txtone;
4531
  u8 scode;
4532
  u8 spmute;
4533
  u8 optsig;
4534
  u8 scramble;
4535
  u8 wide;
4536
  u8 power;
4537
  u8 shiftd;
4538
  u8 step;
4539
  u8 unknown3[4];
4540
};
4541

    
4542
#seekto 0x3300;
4543
struct {
4544
  struct settings_vfo a;
4545
  struct settings_vfo b;
4546
  struct settings_vfo c;
4547
  struct settings_vfo d;
4548
} vfo;
4549

    
4550
#seekto 0x3D80;
4551
struct {
4552
  u8 vhf_low[3];
4553
  u8 vhf_high[3];
4554
  u8 unknown1[4];
4555
  u8 unknown2[6];
4556
  u8 vhf2_low[3];
4557
  u8 vhf2_high[3];
4558
  u8 unknown3[4];
4559
  u8 unknown4[6];
4560
  u8 uhf_low[3];
4561
  u8 uhf_high[3];
4562
  u8 unknown5[4];
4563
  u8 unknown6[6];
4564
  u8 uhf2_low[3];
4565
  u8 uhf2_high[3];
4566
} ranges;
4567

    
4568
#seekto 0x33B0;
4569
struct {
4570
  char line[16];
4571
} static_msg;
4572

    
4573
#seekto 0x3F70;
4574
struct {
4575
  char fp[6];
4576
} fingerprint;
4577

    
4578
"""
4579

    
4580

    
4581
class BTechGMRS(BTechMobileCommon):
4582
    """BTECH's GMRS Mobile"""
4583
    COLOR_LCD = True
4584
    COLOR_LCD2 = True
4585
    NAME_LENGTH = 7
4586
    UPLOAD_MEM_SIZE = 0X3400
4587

    
4588
    def process_mmap(self):
4589
        """Process the mem map into the mem object"""
4590

    
4591
        # Get it
4592
        self._memobj = bitwise.parse(GMRS_MEM_FORMAT, self._mmap)
4593

    
4594
        # load specific parameters from the radio image
4595
        self.set_options()
4596

    
4597
    def set_options(self):
4598
        """This is to read the options from the image and set it in the
4599
        environment, for now just the limits of the freqs in the VHF/UHF
4600
        ranges"""
4601

    
4602
        # setting the correct ranges for each radio type
4603
        ranges = self._memobj.ranges
4604

    
4605
        # the normal dual bands
4606
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4607
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4608

    
4609
        # DEBUG
4610
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4611
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4612

    
4613
        # set the class with the real data
4614
        self._vhf_range = vhf
4615
        self._uhf_range = uhf
4616

    
4617

    
4618
# real radios
4619
@directory.register
4620
class GMRS50X1(BTechGMRS):
4621
    """Baofeng Tech GMRS50X1"""
4622
    MODEL = "GMRS-50X1"
4623
    BANDS = 2
4624
    LIST_TMR = LIST_TMR16
4625
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4626
                     chirp_common.PowerLevel("Mid", watts=10),
4627
                     chirp_common.PowerLevel("Low", watts=5)]
4628
    _vhf_range = (136000000, 175000000)
4629
    _uhf_range = (400000000, 521000000)
4630
    _upper = 255
4631
    _magic = MSTRING_GMRS50X1
4632
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp, ]
4633
    _gmrs = True
4634

    
4635

    
4636
COLORHT_MEM_FORMAT = """
4637
#seekto 0x0000;
4638
struct {
4639
  lbcd rxfreq[4];
4640
  lbcd txfreq[4];
4641
  ul16 rxtone;
4642
  ul16 txtone;
4643
  u8 unknown0:4,
4644
     scode:4;
4645
  u8 unknown1:2,
4646
     spmute:2,
4647
     unknown2:2,
4648
     optsig:2;
4649
  u8 unknown3:3,
4650
     scramble:1,
4651
     unknown4:3,
4652
     power:1;
4653
  u8 unknown5:1,
4654
     wide:1,
4655
     unknown6:2,
4656
     bcl:1,
4657
     add:1,
4658
     pttid:2;
4659
} memory[200];
4660

    
4661
#seekto 0x0E00;
4662
struct {
4663
  u8 tmr;
4664
  u8 unknownE01;
4665
  u8 sql;
4666
  u8 unknownE03[2];
4667
  u8 tot;
4668
  u8 save;
4669
  u8 unknownE07;
4670
  u8 abr;
4671
  u8 beep;
4672
  u8 unknownE0A[4];
4673
  u8 dsub;
4674
  u8 dtmfst;
4675
  u8 screv;
4676
  u8 unknownE11[3];
4677
  u8 pttid;
4678
  u8 unknownE15;
4679
  u8 pttlt;
4680
  u8 unknownE17;
4681
  u8 emctp;
4682
  u8 emcch;
4683
  u8 sigbp;
4684
  u8 unknownE1B;
4685
  u8 camdf;
4686
  u8 cbmdf;
4687
  u8 ccmdf;
4688
  u8 cdmdf;
4689
  u8 langua;
4690
  u8 voice;
4691
  u8 vox;
4692
  u8 voxt;
4693
  u8 sync;          // BTech radios use this as the display sync setting
4694
                    // other radios use this as the auto keypad lock setting
4695
  u8 stfc;
4696
  u8 mffc;
4697
  u8 sfafc;
4698
  u8 sfbfc;
4699
  u8 sfcfc;
4700
  u8 sfdfc;
4701
  u8 subfc;
4702
  u8 fmfc;
4703
  u8 sigfc;
4704
  u8 menufc;
4705
  u8 txfc;
4706
  u8 rxfc;
4707
  u8 unknownE31[5];
4708
  u8 anil;
4709
  u8 reps;
4710
  u8 tmrmr;
4711
  u8 ste;
4712
  u8 rpste;
4713
  u8 rptdl;
4714
  u8 dtmfg;
4715
  u8 tmrtx;
4716
} settings;
4717

    
4718
#seekto 0x0E80;
4719
struct {
4720
  u8 unknown1;
4721
  u8 vfomr;
4722
  u8 keylock;
4723
  u8 unknown2;
4724
  u8 unknown3:4,
4725
     vfomren:1,
4726
     unknown4:1,
4727
     reseten:1,
4728
     menuen:1;
4729
  u8 unknown5[11];
4730
  u8 dispab;
4731
  u8 unknown6[2];
4732
  u8 menu;
4733
  u8 unknown7[7];
4734
  u8 vfomra;
4735
  u8 vfomrb;
4736
  u8 vfomrc;
4737
  u8 vfomrd;
4738
  u8 mrcha;
4739
  u8 mrchb;
4740
  u8 mrchc;
4741
  u8 mrchd;
4742
} settings2;
4743

    
4744
struct settings_vfo {
4745
  u8 freq[8];
4746
  u8 offset[6];
4747
  u8 unknown2[2];
4748
  ul16 rxtone;
4749
  ul16 txtone;
4750
  u8 scode;
4751
  u8 spmute;
4752
  u8 optsig;
4753
  u8 scramble;
4754
  u8 wide;
4755
  u8 power;
4756
  u8 shiftd;
4757
  u8 step;
4758
  u8 unknown3[4];
4759
};
4760

    
4761
#seekto 0x0F00;
4762
struct {
4763
  struct settings_vfo a;
4764
  struct settings_vfo b;
4765
  struct settings_vfo c;
4766
  struct settings_vfo d;
4767
} vfo;
4768

    
4769
#seekto 0x0FE0;
4770
struct {
4771
  char line[16];
4772
} static_msg;
4773

    
4774
#seekto 0x1000;
4775
struct {
4776
  char name[8];
4777
  u8 unknown1[8];
4778
} names[200];
4779

    
4780
#seekto 0x2400;
4781
struct {
4782
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4783
  u8 group_tone;
4784
  u8 repeat_tone;
4785
  u8 unused[13];
4786
} _5tone_std_settings[15];
4787

    
4788
#seekto 0x2500;
4789
struct {
4790
  u8 frame1[5];
4791
  u8 frame2[5];
4792
  u8 frame3[5];
4793
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4794
} _5tone_codes[15];
4795

    
4796
#seekto 0x25F0;
4797
struct {
4798
  u8 _5tone_delay1; // * 10ms
4799
  u8 _5tone_delay2; // * 10ms
4800
  u8 _5tone_delay3; // * 10ms
4801
  u8 _5tone_first_digit_ext_length;
4802
  u8 unknown1;
4803
  u8 unknown2;
4804
  u8 unknown3;
4805
  u8 unknown4;
4806
  u8 decode_standard;
4807
  u8 unknown5:5,
4808
     _5tone_decode_call_frame3:1,
4809
     _5tone_decode_call_frame2:1,
4810
     _5tone_decode_call_frame1:1;
4811
  u8 unknown6:5,
4812
     _5tone_decode_disp_frame3:1,
4813
     _5tone_decode_disp_frame2:1,
4814
     _5tone_decode_disp_frame1:1;
4815
  u8 decode_reset_time; // * 100 + 100ms
4816
} _5tone_settings;
4817

    
4818
#seekto 0x2900;
4819
struct {
4820
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4821
} dtmf_codes[15];
4822

    
4823
#seekto 0x29F0;
4824
struct {
4825
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4826
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4827
  u8 unknown0[14];
4828
  u8 inspection[16];
4829
  u8 monitor[16];
4830
  u8 alarmcode[16];
4831
  u8 stun[16];
4832
  u8 kill[16];
4833
  u8 revive[16];
4834
  u8 unknown1[16];
4835
  u8 unknown2[16];
4836
  u8 unknown3[16];
4837
  u8 unknown4[16];
4838
  u8 unknown5[16];
4839
  u8 unknown6[16];
4840
  u8 unknown7[16];
4841
  u8 masterid[16];
4842
  u8 viceid[16];
4843
  u8 unused01:7,
4844
     mastervice:1;
4845
  u8 unused02:3,
4846
     mrevive:1,
4847
     mkill:1,
4848
     mstun:1,
4849
     mmonitor:1,
4850
     minspection:1;
4851
  u8 unused03:3,
4852
     vrevive:1,
4853
     vkill:1,
4854
     vstun:1,
4855
     vmonitor:1,
4856
     vinspection:1;
4857
  u8 unused04:6,
4858
     txdisable:1,
4859
     rxdisable:1;
4860
  u8 groupcode;
4861
  u8 spacecode;
4862
  u8 delayproctime; // * 100 + 100ms
4863
  u8 resettime;     // * 100 + 100ms
4864
} dtmf_settings;
4865

    
4866
#seekto 0x2D00;
4867
struct {
4868
  struct {
4869
    ul16 freq1;
4870
    u8 unused01[6];
4871
    ul16 freq2;
4872
    u8 unused02[6];
4873
  } _2tone_encode[15];
4874
  u8 duration_1st_tone; // *10ms
4875
  u8 duration_2nd_tone; // *10ms
4876
  u8 duration_gap;      // *10ms
4877
  u8 unused03[13];
4878
  struct {
4879
    struct {
4880
      u8 dec;      // one out of LIST_2TONE_DEC
4881
      u8 response; // one out of LIST_2TONE_RESPONSE
4882
      u8 alert;    // 1-16
4883
    } decs[4];
4884
    u8 unused04[4];
4885
  } _2tone_decode[15];
4886
  u8 unused05[16];
4887

    
4888
  struct {
4889
    ul16 freqA;
4890
    ul16 freqB;
4891
    ul16 freqC;
4892
    ul16 freqD;
4893
    // unknown what those values mean, but they are
4894
    // derived from configured frequencies
4895
    ul16 derived_from_freqA; // 2304000/freqA
4896
    ul16 derived_from_freqB; // 2304000/freqB
4897
    ul16 derived_from_freqC; // 2304000/freqC
4898
    ul16 derived_from_freqD; // 2304000/freqD
4899
  }freqs[15];
4900
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4901
} _2tone;
4902

    
4903
#seekto 0x3D80;
4904
struct {
4905
  u8 vhf_low[3];
4906
  u8 vhf_high[3];
4907
  u8 unknown1[4];
4908
  u8 unknown2[6];
4909
  u8 vhf2_low[3];
4910
  u8 vhf2_high[3];
4911
  u8 unknown3[4];
4912
  u8 unknown4[6];
4913
  u8 uhf_low[3];
4914
  u8 uhf_high[3];
4915
  u8 unknown5[4];
4916
  u8 unknown6[6];
4917
  u8 uhf2_low[3];
4918
  u8 uhf2_high[3];
4919
} ranges;
4920

    
4921
#seekto 0x3F70;
4922
struct {
4923
  char fp[6];
4924
} fingerprint;
4925

    
4926
"""
4927

    
4928

    
4929
class QYTColorHT(BTechMobileCommon):
4930
    """QTY's Color LCD Handheld and alike radios"""
4931
    COLOR_LCD = True
4932
    COLOR_LCD3 = True
4933
    NAME_LENGTH = 8
4934
    LIST_TMR = LIST_TMR15
4935

    
4936
    def process_mmap(self):
4937
        """Process the mem map into the mem object"""
4938

    
4939
        # Get it
4940
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4941

    
4942
        # load specific parameters from the radio image
4943
        self.set_options()
4944

    
4945
    def set_options(self):
4946
        """This is to read the options from the image and set it in the
4947
        environment, for now just the limits of the freqs in the VHF/UHF
4948
        ranges"""
4949

    
4950
        # setting the correct ranges for each radio type
4951
        ranges = self._memobj.ranges
4952

    
4953
        # the normal dual bands
4954
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4955
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4956

    
4957
        # DEBUG
4958
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4959
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4960

    
4961
        # the additional bands
4962
        if self.MODEL in ["KT-8R"]:
4963
            # 200Mhz band
4964
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
4965
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
4966
            self._220_range = vhf2
4967

    
4968
            # 350Mhz band
4969
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
4970
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
4971
            self._350_range = uhf2
4972

    
4973
        # set the class with the real data
4974
        self._vhf_range = vhf
4975
        self._uhf_range = uhf
4976

    
4977

    
4978
# real radios
4979
@directory.register
4980
class KT8R(QYTColorHT):
4981
    """QYT KT8R"""
4982
    VENDOR = "QYT"
4983
    MODEL = "KT-8R"
4984
    BANDS = 4
4985
    LIST_TMR = LIST_TMR16
4986
    _vhf_range = (136000000, 175000000)
4987
    _220_range = (200000000, 261000000)
4988
    _uhf_range = (400000000, 481000000)
4989
    _350_range = (350000000, 391000000)
4990
    _magic = MSTRING_KT8R
4991
    _fileid = [KT8R_fp2, KT8R_fp1, KT8R_fp, ]
4992
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
4993
                     chirp_common.PowerLevel("Low", watts=1)]
4994

    
4995

    
4996
COLOR9900_MEM_FORMAT = """
4997
#seekto 0x0000;
4998
struct {
4999
  lbcd rxfreq[4];
5000
  lbcd txfreq[4];
5001
  ul16 rxtone;
5002
  ul16 txtone;
5003
  u8 unknown0:4,
5004
     scode:4;
5005
  u8 unknown1:2,
5006
     spmute:2,
5007
     unknown2:2,
5008
     optsig:2;
5009
  u8 unknown3:3,
5010
     scramble:1,
5011
     unknown4:2,
5012
     power:2;
5013
  u8 unknown5:1,
5014
     wide:1,
5015
     unknown6:2,
5016
     bcl:1,
5017
     add:1,
5018
     pttid:2;
5019
} memory[200];
5020

    
5021
#seekto 0x0E00;
5022
struct {
5023
  u8 tmr;
5024
  u8 unknown1;
5025
  u8 sql;
5026
  u8 unknown2[2];
5027
  u8 tot;
5028
  u8 volume;
5029
  u8 unknown3;
5030
  u8 abr;
5031
  u8 beep;
5032
  u8 unknown4[4];
5033
  u8 dsub;
5034
  u8 dtmfst;
5035
  u8 unknown_e10;
5036
  u8 unknown_e11;
5037
  u8 screv;
5038
  u8 unknown_e13;
5039
  u8 unknown_e14;
5040
  u8 pttid;
5041
  u8 pttlt;
5042
  u8 unknown7;
5043
  u8 emctp;
5044
  u8 emcch;
5045
  u8 sigbp;
5046
  u8 unknown8;
5047
  u8 camdf;
5048
  u8 cbmdf;
5049
  u8 ccmdf;
5050
  u8 language;
5051
  u8 tmrtx;
5052
  u8 vox;
5053
  u8 voxt;
5054
  u8 autolock;
5055
  u8 asfc;
5056
  u8 mainfc;
5057
  u8 a_fc;
5058
  u8 b_fc;
5059
  u8 c_fc;
5060
  u8 subfc;
5061
  u8 battfc;
5062
  u8 sigfc;
5063
  u8 menufc;
5064
  u8 txfc;
5065
  u8 rxfc;
5066
  u8 unknown_e2f;
5067
  u8 unknown_e30;
5068
  u8 unknown9[3];
5069
  u8 anil;
5070
  u8 reps;
5071
  u8 tmrmr;
5072
  u8 ste;
5073
  u8 rpste;
5074
  u8 rptdl;
5075
  u8 dtmfg;
5076
} settings;
5077

    
5078
#seekto 0x0E80;
5079
struct {
5080
  u8 unknown1;
5081
  u8 vfomr;
5082
  u8 keylock;
5083
  u8 unknown2;
5084
  u8 unknown3:4,
5085
     vfomren:1,
5086
     unknown4:1,
5087
     reseten:1,
5088
     menuen:1;
5089
  u8 unknown5[11];
5090
  u8 dispab;
5091
  u8 unknown6[2];
5092
  u8 menu;
5093
  u8 unknown7[7];
5094
  u8 vfomra;
5095
  u8 vfomrb;
5096
  u8 vfomrc;
5097
  u8 vfomrd;
5098
  u8 mrcha;
5099
  u8 mrchb;
5100
  u8 mrchc;
5101
  u8 mrchd;
5102
} settings2;
5103

    
5104
struct settings_vfo {
5105
  u8 freq[8];
5106
  u8 offset[6];
5107
  u8 unknown2[2];
5108
  ul16 rxtone;
5109
  ul16 txtone;
5110
  u8 scode;
5111
  u8 spmute;
5112
  u8 optsig;
5113
  u8 scramble;
5114
  u8 wide;
5115
  u8 power;
5116
  u8 shiftd;
5117
  u8 step;
5118
  u8 unknown3[4];
5119
};
5120

    
5121
#seekto 0x0F00;
5122
struct {
5123
  struct settings_vfo a;
5124
  struct settings_vfo b;
5125
  struct settings_vfo c;
5126
  struct settings_vfo d;
5127
} vfo;
5128

    
5129
#seekto 0x0F80;
5130
struct {
5131
  char line1[8];
5132
  char line2[8];
5133
  char line3[8];
5134
  char line4[8];
5135
  char line5[8];
5136
  char line6[8];
5137
  char line7[8];
5138
  char line8[8];
5139
} poweron_msg;
5140

    
5141
#seekto 0x0FE0;
5142
struct {
5143
  char line[16];
5144
} static_msg;
5145

    
5146
#seekto 0x1000;
5147
struct {
5148
  char name[7];
5149
  u8 unknown1[9];
5150
} names[200];
5151

    
5152
#seekto 0x2400;
5153
struct {
5154
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
5155
  u8 group_tone;
5156
  u8 repeat_tone;
5157
  u8 unused[13];
5158
} _5tone_std_settings[15];
5159

    
5160
#seekto 0x2500;
5161
struct {
5162
  u8 frame1[5];
5163
  u8 frame2[5];
5164
  u8 frame3[5];
5165
  u8 standard;   // one out of LIST_5TONE_STANDARDS
5166
} _5tone_codes[15];
5167

    
5168
#seekto 0x25F0;
5169
struct {
5170
  u8 _5tone_delay1; // * 10ms
5171
  u8 _5tone_delay2; // * 10ms
5172
  u8 _5tone_delay3; // * 10ms
5173
  u8 _5tone_first_digit_ext_length;
5174
  u8 unknown1;
5175
  u8 unknown2;
5176
  u8 unknown3;
5177
  u8 unknown4;
5178
  u8 decode_standard;
5179
  u8 unknown5:5,
5180
     _5tone_decode_call_frame3:1,
5181
     _5tone_decode_call_frame2:1,
5182
     _5tone_decode_call_frame1:1;
5183
  u8 unknown6:5,
5184
     _5tone_decode_disp_frame3:1,
5185
     _5tone_decode_disp_frame2:1,
5186
     _5tone_decode_disp_frame1:1;
5187
  u8 decode_reset_time; // * 100 + 100ms
5188
} _5tone_settings;
5189

    
5190
#seekto 0x2900;
5191
struct {
5192
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
5193
} dtmf_codes[15];
5194

    
5195
#seekto 0x29F0;
5196
struct {
5197
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10      // 9f0
5198
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10      // 9f1
5199
  u8 unknown0[14];                                             // 9f2-9ff
5200
  u8 inspection[16];                                           // a00-a0f
5201
  u8 monitor[16];                                              // a10-a1f
5202
  u8 alarmcode[16];                                            // a20-a2f
5203
  u8 stun[16];                                                 // a30-a3f
5204
  u8 kill[16];                                                 // a40-a4f
5205
  u8 revive[16];                                               // a50-a5f
5206
  u8 unknown1[16];                                             // a60-a6f
5207
  u8 unknown2[16];                                             // a70-a7f
5208
  u8 unknown3[16];                                             // a80-a8f
5209
  u8 unknown4[16];                                             // a90-a9f
5210
  u8 unknown5[16];                                             // aa0-aaf
5211
  u8 unknown6[16];                                             // ab0-abf
5212
  u8 unknown7[16];                                             // ac0-acf
5213
  u8 masterid[16];                                             // ad0-adf
5214
  u8 viceid[16];                                               // ae0-aef
5215
  u8 unused01:7,                                               // af0
5216
     mastervice:1;
5217
  u8 unused02:3,                                               // af1
5218
     mrevive:1,
5219
     mkill:1,
5220
     mstun:1,
5221
     mmonitor:1,
5222
     minspection:1;
5223
  u8 unused03:3,                                               // af2
5224
     vrevive:1,
5225
     vkill:1,
5226
     vstun:1,
5227
     vmonitor:1,
5228
     vinspection:1;
5229
  u8 unused04:6,                                               // af3
5230
     txdisable:1,
5231
     rxdisable:1;
5232
  u8 groupcode;                                                // af4
5233
  u8 spacecode;                                                // af5
5234
  u8 delayproctime; // * 100 + 100ms                           // af6
5235
  u8 resettime;     // * 100 + 100ms                           // af7
5236
} dtmf_settings;
5237

    
5238
#seekto 0x2D00;
5239
struct {
5240
  struct {
5241
    ul16 freq1;
5242
    u8 unused01[6];
5243
    ul16 freq2;
5244
    u8 unused02[6];
5245
  } _2tone_encode[15];
5246
  u8 duration_1st_tone; // *10ms
5247
  u8 duration_2nd_tone; // *10ms
5248
  u8 duration_gap;      // *10ms
5249
  u8 unused03[13];
5250
  struct {
5251
    struct {
5252
      u8 dec;      // one out of LIST_2TONE_DEC
5253
      u8 response; // one out of LIST_2TONE_RESPONSE
5254
      u8 alert;    // 1-16
5255
    } decs[4];
5256
    u8 unused04[4];
5257
  } _2tone_decode[15];
5258
  u8 unused05[16];
5259

    
5260
  struct {
5261
    ul16 freqA;
5262
    ul16 freqB;
5263
    ul16 freqC;
5264
    ul16 freqD;
5265
    // unknown what those values mean, but they are
5266
    // derived from configured frequencies
5267
    ul16 derived_from_freqA; // 2304000/freqA
5268
    ul16 derived_from_freqB; // 2304000/freqB
5269
    ul16 derived_from_freqC; // 2304000/freqC
5270
    ul16 derived_from_freqD; // 2304000/freqD
5271
  }freqs[15];
5272
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
5273
} _2tone;
5274

    
5275
#seekto 0x3D80;
5276
struct {
5277
  u8 vhf_low[3];
5278
  u8 vhf_high[3];
5279
  u8 unknown1[4];
5280
  u8 unknown2[6];
5281
  u8 vhf2_low[3];
5282
  u8 vhf2_high[3];
5283
  u8 unknown3[4];
5284
  u8 unknown4[6];
5285
  u8 uhf_low[3];
5286
  u8 uhf_high[3];
5287
  u8 unknown5[4];
5288
  u8 unknown6[6];
5289
  u8 uhf2_low[3];
5290
  u8 uhf2_high[3];
5291
} ranges;
5292

    
5293
#seekto 0x3F70;
5294
struct {
5295
  char fp[6];
5296
} fingerprint;
5297

    
5298
"""
5299

    
5300

    
5301
COLOR20V2_MEM_FORMAT = """
5302
#seekto 0x0000;
5303
struct {
5304
  lbcd rxfreq[4];
5305
  lbcd txfreq[4];
5306
  ul16 rxtone;
5307
  ul16 txtone;
5308
  u8 unknown0:4,
5309
     scode:4;
5310
  u8 unknown1:2,
5311
     spmute:2,
5312
     unknown2:2,
5313
     optsig:2;
5314
  u8 unknown3:3,
5315
     scramble:1,
5316
     unknown4:2,
5317
     power:2;
5318
  u8 unknown5:1,
5319
     wide:1,
5320
     unknown6:2,
5321
     bcl:1,
5322
     add:1,
5323
     pttid:2;
5324
} memory[200];
5325

    
5326
#seekto 0x0E00;
5327
struct {
5328
  u8 tmr;
5329
  u8 unknown1;
5330
  u8 sql;
5331
  u8 unknown2;
5332
  u8 autolock;
5333
  u8 tot;
5334
  u8 apo;
5335
  u8 unknown3;
5336
  u8 abr;
5337
  u8 beep;
5338
  u8 unknown4[4];
5339
  u8 dtmfst;
5340
  u8 unknown5[2];
5341
  u8 screv;
5342
  u8 unknown6[2];
5343
  u8 pttid;
5344
  u8 pttlt;
5345
  u8 unknown7;
5346
  u8 emctp;
5347
  u8 emcch;
5348
  u8 sigbp;
5349
  u8 unknown8;
5350
  u8 camdf;
5351
  u8 cbmdf;
5352
  u8 ccmdf;
5353
  u8 vox;
5354
  u8 voxt;
5355
  u8 sync;
5356
  u8 asfc;
5357
  u8 mainfc;
5358
  u8 a_fc;
5359
  u8 b_fc;
5360
  u8 c_fc;
5361
  u8 subfc;
5362
  u8 battfc;
5363
  u8 sigfc;
5364
  u8 menufc;
5365
  u8 txfc;
5366
  u8 rxfc;
5367
  u8 repsw;
5368
  u8 dsub;
5369
  u8 unknown9[5];
5370
  u8 anil;
5371
  u8 reps;
5372
  u8 repm;
5373
  u8 tmrmr;
5374
  u8 ste;
5375
  u8 rpste;
5376
  u8 rptdl;
5377
  u8 dtmfg;
5378
  u8 mgain;
5379
  u8 skiptx;
5380
  u8 scmode;
5381
  u8 tmrtx;
5382
  u8 volume;
5383
  u8 unknown_10;
5384
  u8 save;
5385
} settings;
5386

    
5387
#seekto 0x0E80;
5388
struct {
5389
  u8 unknown1;
5390
  u8 vfomr;
5391
  u8 keylock;
5392
  u8 unknown2;
5393
  u8 unknown3:4,
5394
     vfomren:1,
5395
     unknown4:1,
5396
     reseten:1,
5397
     menuen:1;
5398
  u8 unknown5[11];
5399
  u8 dispab;
5400
  u8 unknown6[2];
5401
  u8 menu;
5402
  u8 unknown7[7];
5403
  u8 vfomra;
5404
  u8 vfomrb;
5405
  u8 vfomrc;
5406
  u8 vfomrd;
5407
  u8 mrcha;
5408
  u8 mrchb;
5409
  u8 mrchc;
5410
  u8 mrchd;
5411
} settings2;
5412

    
5413
struct settings_vfo {
5414
  u8 freq[8];
5415
  u8 offset[6];
5416
  u8 unknown2[2];
5417
  ul16 rxtone;
5418
  ul16 txtone;
5419
  u8 scode;
5420
  u8 spmute;
5421
  u8 optsig;
5422
  u8 scramble;
5423
  u8 wide;
5424
  u8 power;
5425
  u8 shiftd;
5426
  u8 step;
5427
  u8 unknown3[4];
5428
};
5429

    
5430
#seekto 0x0F00;
5431
struct {
5432
  struct settings_vfo a;
5433
  struct settings_vfo b;
5434
  struct settings_vfo c;
5435
  struct settings_vfo d;
5436
} vfo;
5437

    
5438
#seekto 0x0F80;
5439
struct {
5440
  char line1[8];
5441
  char line2[8];
5442
  char line3[8];
5443
  char line4[8];
5444
  char line5[8];
5445
  char line6[8];
5446
  char line7[8];
5447
  char line8[8];
5448
} poweron_msg;
5449

    
5450
#seekto 0x0FE0;
5451
struct {
5452
  char line[16];
5453
} static_msg;
5454

    
5455
#seekto 0x1000;
5456
struct {
5457
  char name[7];
5458
  u8 unknown1[9];
5459
} names[200];
5460

    
5461
#seekto 0x2400;
5462
struct {
5463
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
5464
  u8 group_tone;
5465
  u8 repeat_tone;
5466
  u8 unused[13];
5467
} _5tone_std_settings[15];
5468

    
5469
#seekto 0x2500;
5470
struct {
5471
  u8 frame1[5];
5472
  u8 frame2[5];
5473
  u8 frame3[5];
5474
  u8 standard;   // one out of LIST_5TONE_STANDARDS
5475
} _5tone_codes[15];
5476

    
5477
#seekto 0x25F0;
5478
struct {
5479
  u8 _5tone_delay1; // * 10ms
5480
  u8 _5tone_delay2; // * 10ms
5481
  u8 _5tone_delay3; // * 10ms
5482
  u8 _5tone_first_digit_ext_length;
5483
  u8 unknown1;
5484
  u8 unknown2;
5485
  u8 unknown3;
5486
  u8 unknown4;
5487
  u8 decode_standard;
5488
  u8 unknown5:5,
5489
     _5tone_decode_call_frame3:1,
5490
     _5tone_decode_call_frame2:1,
5491
     _5tone_decode_call_frame1:1;
5492
  u8 unknown6:5,
5493
     _5tone_decode_disp_frame3:1,
5494
     _5tone_decode_disp_frame2:1,
5495
     _5tone_decode_disp_frame1:1;
5496
  u8 decode_reset_time; // * 100 + 100ms
5497
} _5tone_settings;
5498

    
5499
#seekto 0x2900;
5500
struct {
5501
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
5502
} dtmf_codes[15];
5503

    
5504
#seekto 0x29F0;
5505
struct {
5506
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10      // 9f0
5507
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10      // 9f1
5508
  u8 unknown0[14];                                             // 9f2-9ff
5509
  u8 inspection[16];                                           // a00-a0f
5510
  u8 monitor[16];                                              // a10-a1f
5511
  u8 alarmcode[16];                                            // a20-a2f
5512
  u8 stun[16];                                                 // a30-a3f
5513
  u8 kill[16];                                                 // a40-a4f
5514
  u8 revive[16];                                               // a50-a5f
5515
  u8 unknown1[16];                                             // a60-a6f
5516
  u8 unknown2[16];                                             // a70-a7f
5517
  u8 unknown3[16];                                             // a80-a8f
5518
  u8 unknown4[16];                                             // a90-a9f
5519
  u8 unknown5[16];                                             // aa0-aaf
5520
  u8 unknown6[16];                                             // ab0-abf
5521
  u8 unknown7[16];                                             // ac0-acf
5522
  u8 masterid[16];                                             // ad0-adf
5523
  u8 viceid[16];                                               // ae0-aef
5524
  u8 unused01:7,                                               // af0
5525
     mastervice:1;
5526
  u8 unused02:3,                                               // af1
5527
     mrevive:1,
5528
     mkill:1,
5529
     mstun:1,
5530
     mmonitor:1,
5531
     minspection:1;
5532
  u8 unused03:3,                                               // af2
5533
     vrevive:1,
5534
     vkill:1,
5535
     vstun:1,
5536
     vmonitor:1,
5537
     vinspection:1;
5538
  u8 unused04:6,                                               // af3
5539
     txdisable:1,
5540
     rxdisable:1;
5541
  u8 groupcode;                                                // af4
5542
  u8 spacecode;                                                // af5
5543
  u8 delayproctime; // * 100 + 100ms                           // af6
5544
  u8 resettime;     // * 100 + 100ms                           // af7
5545
} dtmf_settings;
5546

    
5547
#seekto 0x2D00;
5548
struct {
5549
  struct {
5550
    ul16 freq1;
5551
    u8 unused01[6];
5552
    ul16 freq2;
5553
    u8 unused02[6];
5554
  } _2tone_encode[15];
5555
  u8 duration_1st_tone; // *10ms
5556
  u8 duration_2nd_tone; // *10ms
5557
  u8 duration_gap;      // *10ms
5558
  u8 unused03[13];
5559
  struct {
5560
    struct {
5561
      u8 dec;      // one out of LIST_2TONE_DEC
5562
      u8 response; // one out of LIST_2TONE_RESPONSE
5563
      u8 alert;    // 1-16
5564
    } decs[4];
5565
    u8 unused04[4];
5566
  } _2tone_decode[15];
5567
  u8 unused05[16];
5568

    
5569
  struct {
5570
    ul16 freqA;
5571
    ul16 freqB;
5572
    ul16 freqC;
5573
    ul16 freqD;
5574
    // unknown what those values mean, but they are
5575
    // derived from configured frequencies
5576
    ul16 derived_from_freqA; // 2304000/freqA
5577
    ul16 derived_from_freqB; // 2304000/freqB
5578
    ul16 derived_from_freqC; // 2304000/freqC
5579
    ul16 derived_from_freqD; // 2304000/freqD
5580
  }freqs[15];
5581
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
5582
} _2tone;
5583

    
5584
#seekto 0x3D80;
5585
struct {
5586
  u8 vhf_low[3];
5587
  u8 vhf_high[3];
5588
  u8 unknown1[4];
5589
  u8 unknown2[6];
5590
  u8 vhf2_low[3];
5591
  u8 vhf2_high[3];
5592
  u8 unknown3[4];
5593
  u8 unknown4[6];
5594
  u8 uhf_low[3];
5595
  u8 uhf_high[3];
5596
  u8 unknown5[4];
5597
  u8 unknown6[6];
5598
  u8 uhf2_low[3];
5599
  u8 uhf2_high[3];
5600
} ranges;
5601

    
5602
#seekto 0x3F70;
5603
struct {
5604
  char fp[6];
5605
} fingerprint;
5606

    
5607
"""
5608

    
5609

    
5610
class BTechColorWP(BTechMobileCommon):
5611
    """BTECH's Color WP Mobile and alike radios"""
5612
    COLOR_LCD = True
5613
    COLOR_LCD4 = True
5614
    NAME_LENGTH = 7
5615
    LIST_TMR = LIST_TMR7
5616

    
5617
    def set_options(self):
5618
        """This is to read the options from the image and set it in the
5619
        environment, for now just the limits of the freqs in the VHF/UHF
5620
        ranges"""
5621

    
5622
        # setting the correct ranges for each radio type
5623
        ranges = self._memobj.ranges
5624

    
5625
        # the normal dual bands
5626
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
5627
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
5628

    
5629
        # DEBUG
5630
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
5631
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
5632

    
5633
        # set the class with the real data
5634
        self._vhf_range = vhf
5635
        self._uhf_range = uhf
5636

    
5637

    
5638
# real radios
5639
@directory.register
5640
class KTWP12(BTechColorWP):
5641
    """QYT KT-WP12"""
5642
    VENDOR = "QYT"
5643
    MODEL = "KT-WP12"
5644
    BANDS = 2
5645
    UPLOAD_MEM_SIZE = 0X3100
5646
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
5647
                     chirp_common.PowerLevel("Low", watts=5)]
5648
    _upper = 199
5649
    _magic = MSTRING_KTWP12
5650
    _fileid = [KTWP12_fp, ]
5651
    _gmrs = False
5652

    
5653
    def process_mmap(self):
5654
        """Process the mem map into the mem object"""
5655

    
5656
        # Get it
5657
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5658

    
5659
        # load specific parameters from the radio image
5660
        self.set_options()
5661

    
5662

    
5663
@directory.register
5664
class WP9900(BTechColorWP):
5665
    """Anysecu WP-9900"""
5666
    VENDOR = "Anysecu"
5667
    MODEL = "WP-9900"
5668
    BANDS = 2
5669
    UPLOAD_MEM_SIZE = 0X3100
5670
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
5671
                     chirp_common.PowerLevel("Low", watts=5)]
5672
    _upper = 199
5673
    _magic = MSTRING_KTWP12
5674
    _fileid = [WP9900_fp, ]
5675
    _gmrs = False
5676

    
5677
    def process_mmap(self):
5678
        """Process the mem map into the mem object"""
5679

    
5680
        # Get it
5681
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5682

    
5683
        # load specific parameters from the radio image
5684
        self.set_options()
5685

    
5686

    
5687
@directory.register
5688
class GMRS20V2(BTechColorWP):
5689
    """Baofeng Tech GMRS-20V2"""
5690
    MODEL = "GMRS-20V2"
5691
    BANDS = 2
5692
    UPLOAD_MEM_SIZE = 0X3100
5693
    _power_levels = [chirp_common.PowerLevel("High", watts=20),
5694
                     chirp_common.PowerLevel("", watts=6),
5695
                     chirp_common.PowerLevel("Low", watts=5)]
5696
    _upper = 199
5697
    _magic = MSTRING_GMRS20V2
5698
    _fileid = [GMRS20V2_fp2, GMRS20V2_fp1, GMRS20V2_fp, ]
5699
    _gmrs = True
5700

    
5701
    def process_mmap(self):
5702
        """Process the mem map into the mem object"""
5703

    
5704
        # Get it
5705
        self._memobj = bitwise.parse(COLOR20V2_MEM_FORMAT, self._mmap)
5706

    
5707
        # load specific parameters from the radio image
5708
        self.set_options()
    (1-1/1)