Project

General

Profile

Bug #10941 ยป btech_mcb254.py

Jim Unroe, 11/09/2023 07:37 PM

 
1
# Copyright 2016-2023:
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
from builtins import bytes
19

    
20
import struct
21
import logging
22

    
23
from time import sleep
24
from chirp import chirp_common, directory, memmap
25
from chirp import bitwise, errors, util
26
from chirp.settings import RadioSettingGroup, RadioSetting, \
27
    RadioSettingValueBoolean, RadioSettingValueList, \
28
    RadioSettingValueString, RadioSettingValueInteger, \
29
    RadioSettingValueFloat, RadioSettings, InvalidValueError
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 = 0x06
45
MODES = ["FM", "NFM"]
46
SKIP_VALUES = ["S", ""]
47
TONES = chirp_common.TONES
48
DTCS = tuple(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_EARPH = ["Off", "Earphone", "Earphone + Speaker"]
68
LIST_EMCTP = ["TX alarm sound", "TX ANI", "Both"]
69
LIST_EMCTPX = ["Off"] + LIST_EMCTP
70
LIST_LANGUA = ["English", "Chinese"]
71
LIST_MDF = ["Frequency", "Channel", "Name"]
72
LIST_OFF1TO9 = ["Off"] + ["%s seconds" % x for x in range(1, 10)]
73
LIST_OFF1TO10 = ["Off"] + ["%s seconds" % x for x in range(1, 11)]
74
LIST_OFF1TO50 = ["Off"] + ["%s seconds" % x for x in range(1, 51)]
75
LIST_OFF1TO60 = ["Off"] + ["%s seconds" % x for x in range(1, 61)]
76
LIST_PONMSG = ["Full", "Message", "Battery voltage"]
77
LIST_REPM = ["Off", "Carrier", "CTCSS or DCS", "Tone", "DTMF"]
78
LIST_REPS = ["1000 Hz", "1450 Hz", "1750 Hz", "2100 Hz"]
79
LIST_REPSW = ["Off", "RX", "TX"]
80
LIST_RPTDL = ["Off"] + ["%s ms" % x for x in range(1, 11)]
81
LIST_SCMODE = ["Off", "PTT-SC", "MEM-SC", "PON-SC"]
82
LIST_SHIFT = ["Off", "+", "-"]
83
LIST_SKIPTX = ["Off", "Skip 1", "Skip 2"]
84
STEPS = [2.5, 5.0, 6.25, 10.0, 12.5, 25.0]
85
LIST_STEP = [str(x) for x in STEPS]
86
LIST_SYNC = ["Off", "AB", "CD", "AB+CD"]
87
LIST_SYNCV2 = ["Off", "AB", "AC", "BC", "ABC"]
88
# the first 12 TMR choices common to all 4-line color display mobile radios
89
LIST_TMR12 = ["OFF", "M+A", "M+B", "M+C", "M+D", "M+A+B", "M+A+C", "M+A+D",
90
              "M+B+C", "M+B+D", "M+C+D", "M+A+B+C"]
91
# the 16 choice list for color display mobile radios that correctly implement
92
# the full 16 TMR choices
93
LIST_TMR16 = LIST_TMR12 + ["M+A+B+D", "M+A+C+D", "M+B+C+D", "A+B+C+D"]
94
# the 15 choice list for color mobile radios that are missing the M+A+B+D
95
# choice in the TMR menu
96
LIST_TMR15 = LIST_TMR12 + ["M+A+C+D", "M+B+C+D", "A+B+C+D"]
97
# the 7 TMR choices for the 3-line color display mobile radios
98
LIST_TMR7 = ["OFF", "M+A", "M+B", "M+C", "M+AB", "M+AC", "M+BC", "M+ABC"]
99
LIST_TMRTX = ["Track", "Fixed"]
100
LIST_TOT = ["%s sec" % x for x in range(15, 615, 15)]
101
LIST_TXDISP = ["Power", "Mic Volume"]
102
LIST_TXP = ["High", "Low"]
103
LIST_TXP3 = ["High", "Mid", "Low"]
104
LIST_SCREV = ["TO (timeout)", "CO (carrier operated)", "SE (search)"]
105
LIST_VFOMR = ["Frequency", "Channel"]
106
LIST_VOICE = ["Off"] + LIST_LANGUA
107
LIST_VOX = ["Off"] + ["%s" % x for x in range(1, 11)]
108
LIST_VOXT = ["%s seconds" % x for x in range(0, 21)]
109
LIST_WIDE = ["Wide", "Narrow"]
110

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

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

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

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

    
153
GMRS_FREQS1 = [462562500, 462587500, 462612500, 462637500, 462662500,
154
               462687500, 462712500]
155
GMRS_FREQS2 = [467562500, 467587500, 467612500, 467637500, 467662500,
156
               467687500, 467712500]
157
GMRS_FREQS3 = [462550000, 462575000, 462600000, 462625000, 462650000,
158
               462675000, 462700000, 462725000]
159
GMRS_FREQS = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
160

    
161

    
162
# #### ID strings #####################################################
163

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

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

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

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

    
196
# B-TECH UV-25X2 Second Generation (G2)
197
UV25X2G2_fp = b"UCB282"
198

    
199
# B-TECH UV-25X4
200
UV25X4_fp = b"UC4014"
201

    
202
# B-TECH UV-25X4 Second Generation (G2)
203
UV25X4G2_fp = b"UCB284"
204

    
205
# B-TECH UV-50X2
206
UV50X2_fp = b"UC2M12"
207

    
208
# B-TECH GMRS-50X1
209
GMRS50X1_fp = b"NC1802"
210
GMRS50X1_fp1 = b"NC1932"
211

    
212
# B-TECH GMRS-20V2
213
GMRS20V2_fp = b"WP4204"
214
GMRS20V2_fp1 = b"VW2112"
215
GMRS20V2_fp2 = b"VWG728"
216

    
217
# special var to know when we found a BTECH Gen 3
218
BTECH3 = [UV2501G3_fp, UV2501_220G3_fp, UV5001G3_fp]
219

    
220

    
221
# WACCOM Mini-8900
222
MINI8900_fp = b"M28854"
223

    
224

    
225
# QYT KT-UV980
226
KTUV980_fp = b"H28854"
227

    
228
# QYT KT8900
229
KT8900_fp = b"M29154"
230
# New generations KT8900
231
KT8900_fp1 = b"M2C234"
232
KT8900_fp2 = b"M2G1F4"
233
KT8900_fp3 = b"M2G2F4"
234
KT8900_fp4 = b"M2G304"
235
KT8900_fp5 = b"M2G314"
236
KT8900_fp6 = b"M2G424"
237
KT8900_fp7 = b"M27184"
238

    
239
# KT8900R
240
KT8900R_fp = b"M3G1F4"
241
# Second Generation
242
KT8900R_fp1 = b"M3G214"
243
# another model
244
KT8900R_fp2 = b"M3C234"
245
# another model G4?
246
KT8900R_fp3 = b"M39164"
247
# another model
248
KT8900R_fp4 = b"M3G314"
249
# AC3MB: another id
250
KT8900R_fp5 = b"M3B064"
251

    
252
# KT7900D (quad band)
253
KT7900D_fp = b"VC4004"
254
KT7900D_fp1 = b"VC4284"
255
KT7900D_fp2 = b"VC4264"
256
KT7900D_fp3 = b"VC4114"
257
KT7900D_fp4 = b"VC4104"
258
KT7900D_fp5 = b"VC4254"
259
KT7900D_fp6 = b"VC5264"
260
KT7900D_fp7 = b"VC9204"
261
KT7900D_fp8 = b"VC9214"
262
KT7900D_fp9 = b"VC5302"
263

    
264
# QB25 (quad band) - a clone of KT7900D
265
QB25_fp = b"QB-25"
266

    
267
# KT8900D (dual band)
268
KT8900D_fp = b"VC2002"
269
KT8900D_fp1 = b"VC8632"
270
KT8900D_fp2 = b"VC3402"
271
KT8900D_fp3 = b"VC7062"
272

    
273
# LUITON LT-588UV
274
LT588UV_fp = b"V2G1F4"
275
# Added by rstrickoff gen 2 id
276
LT588UV_fp1 = b"V2G214"
277

    
278
# QYT KT-8R (quad band ht)
279
KT8R_fp = b"MCB264"
280
KT8R_fp1 = b"MCB284"
281
KT8R_fp2 = b"MC5264"
282
KT8R_fp3 = b"MCB254"
283

    
284
# QYT KT5800 (dual band)
285
KT5800_fp = b"VCB222"
286

    
287
# QYT KT980Plus (dual band)
288
KT980PLUS_fp = b"VC2002"
289
KT980PLUS_fp1 = b"VC6042"
290

    
291
# Radioddity DB25-G (gmrs)
292
DB25G_fp = b"VC6182"
293
DB25G_fp1 = b"VC7062"
294

    
295
# QYT KT-WP12 and KT-9900
296
KTWP12_fp = b"WP3094"
297

    
298
# Anysecu WP-9900
299
WP9900_fp = b"WP3094"
300

    
301

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

    
327

    
328
def _clean_buffer(radio):
329
    """Cleaning the read serial buffer, hard timeout to survive an infinite
330
    data stream"""
331

    
332
    # touching the serial timeout to optimize the flushing
333
    # restored at the end to the default value
334
    radio.pipe.timeout = 0.1
335
    dump = b"1"
336
    datacount = 0
337

    
338
    try:
339
        while len(dump) > 0:
340
            dump = radio.pipe.read(100)
341
            datacount += len(dump)
342
            # hard limit to survive a infinite serial data stream
343
            # 5 times bigger than a normal rx block (69 bytes)
344
            if datacount > 345:
345
                seriale = "Please check your serial port selection."
346
                raise errors.RadioError(seriale)
347

    
348
        # restore the default serial timeout
349
        radio.pipe.timeout = STIMEOUT
350

    
351
    except Exception:
352
        raise errors.RadioError("Unknown error cleaning the serial buffer")
353

    
354

    
355
def _rawrecv(radio, amount):
356
    """Raw read from the radio device, less intensive way"""
357

    
358
    data = b""
359

    
360
    try:
361
        data = radio.pipe.read(amount)
362

    
363
        # DEBUG
364
        if debug is True:
365
            LOG.debug("<== (%d) bytes:\n\n%s" %
366
                      (len(data), util.hexprint(data)))
367

    
368
        # fail if no data is received
369
        if len(data) == 0:
370
            raise errors.RadioError("No data received from radio")
371

    
372
        # notice on the logs if short
373
        if len(data) < amount:
374
            LOG.warn("Short reading %d bytes from the %d requested." %
375
                     (len(data), amount))
376

    
377
    except:
378
        raise errors.RadioError("Error reading data from radio")
379

    
380
    return data
381

    
382

    
383
def _send(radio, data):
384
    """Send data to the radio device"""
385

    
386
    try:
387
        radio.pipe.write(bytes(data))
388

    
389
        # DEBUG
390
        if debug is True:
391
            LOG.debug("==> (%d) bytes:\n\n%s" %
392
                      (len(data), util.hexprint(data)))
393
    except:
394
        raise errors.RadioError("Error sending data to radio")
395

    
396

    
397
def _make_frame(cmd, addr, length, data=""):
398
    """Pack the info in the header format"""
399
    frame = b"\x06" + struct.pack(">BHB", ord(cmd), addr, length)
400
    # add the data if set
401
    if len(data) != 0:
402
        frame += data
403

    
404
    return frame
405

    
406

    
407
def _recv(radio, addr):
408
    """Get data from the radio all at once to lower syscalls load"""
409

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

    
413
    # get the whole block
414
    block = _rawrecv(radio, BLOCK_SIZE + 5)
415

    
416
    # basic check
417
    if len(block) < (BLOCK_SIZE + 5):
418
        raise errors.RadioError("Short read of the block 0x%04x" % addr)
419

    
420
    # checking for the ack
421
    if block[0] != ACK_CMD:
422
        raise errors.RadioError("Bad ack from radio in block 0x%04x" % addr)
423

    
424
    # header validation
425
    c, a, l = struct.unpack(">BHB", block[1:5])
426
    if a != addr or l != BLOCK_SIZE or c != ord("X"):
427
        LOG.debug("Invalid header for block 0x%04x" % addr)
428
        LOG.debug("CMD: %s  ADDR: %04x  SIZE: %02x" % (c, a, l))
429
        raise errors.RadioError("Invalid header for block 0x%04x:" % addr)
430

    
431
    # return the data
432
    return block[5:]
433

    
434

    
435
def _do_ident(radio, status, upload=False):
436
    """Put the radio in PROGRAM mode & identify it"""
437
    #  set the serial discipline
438
    radio.pipe.baudrate = 9600
439
    radio.pipe.parity = "N"
440

    
441
    # lengthen the timeout here as these radios are resetting due to timeout
442
    radio.pipe.timeout = 0.75
443

    
444
    # send the magic word
445
    _send(radio, radio._magic)
446

    
447
    # Now you get a 50 byte reply if all goes well
448
    ident = _rawrecv(radio, 50)
449

    
450
    # checking for the ack
451
    if ident[0] != ACK_CMD:
452
        raise errors.RadioError("Bad ack from radio")
453

    
454
    # basic check for the ident block
455
    if len(ident) != 50:
456
        raise errors.RadioError("Radio send a short ident block.")
457

    
458
    # check if ident is OK
459
    itis = False
460
    for fp in radio._fileid:
461
        if fp in ident:
462
            # got it!
463
            itis = True
464
            # checking if we are dealing with a Gen 3 BTECH
465
            if radio.VENDOR == "BTECH" and fp in BTECH3:
466
                radio.btech3 = True
467

    
468
            break
469

    
470
    if itis is False:
471
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
472
        raise errors.RadioError("Radio identification failed.")
473

    
474
    # pause here for the radio to catch up
475
    sleep(0.1)
476

    
477
    # the OEM software reads this additional block, so we will, too
478

    
479
    # Get the full 21 bytes at a time to reduce load
480
    # 1 byte ACK + 4 bytes header + 16 bytes of data (BLOCK_SIZE)
481
    frame = _make_frame("S", 0x3DF0, 16)
482
    _send(radio, frame)
483
    id2 = _rawrecv(radio, 21)
484

    
485
    # restore the default serial timeout
486
    radio.pipe.timeout = STIMEOUT
487

    
488
    # checking for the ack
489
    if id2[:1] not in b"\x06\x05":
490
        raise errors.RadioError("Bad ack from radio")
491

    
492
    # basic check for the additional block
493
    if len(id2) < 21:
494
        raise errors.RadioError("The extra ID is short, aborting.")
495

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

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

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

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

    
524
    return True
525

    
526

    
527
def _download(radio):
528
    """Get the memory map"""
529

    
530
    # UI progress
531
    status = chirp_common.Status()
532

    
533
    # put radio in program mode and identify it
534
    _do_ident(radio, status)
535

    
536
    # reset the progress bar in the UI
537
    status.max = MEM_SIZE // BLOCK_SIZE
538
    status.msg = "Cloning from radio..."
539
    status.cur = 0
540
    radio.status_fn(status)
541

    
542
    data = b""
543
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
544
        # sending the read request
545
        _send(radio, _make_frame("S", addr, BLOCK_SIZE))
546

    
547
        # read
548
        d = _recv(radio, addr)
549

    
550
        # aggregate the data
551
        data += d
552

    
553
        # UI Update
554
        status.cur = addr // BLOCK_SIZE
555
        status.msg = "Cloning from radio..."
556
        radio.status_fn(status)
557

    
558
    return data
559

    
560

    
561
def _upload(radio):
562
    """Upload procedure"""
563

    
564
    # The UPLOAD mem is restricted to lower than 0x3100,
565
    # so we will override that here locally
566
    MEM_SIZE = radio.UPLOAD_MEM_SIZE
567

    
568
    # UI progress
569
    status = chirp_common.Status()
570

    
571
    # put radio in program mode and identify it
572
    _do_ident(radio, status, True)
573

    
574
    # get the data to upload to radio
575
    data = radio.get_mmap().get_byte_compatible()
576

    
577
    # Reset the UI progress
578
    status.max = MEM_SIZE // TX_BLOCK_SIZE
579
    status.cur = 0
580
    status.msg = "Cloning to radio..."
581
    radio.status_fn(status)
582

    
583
    # the fun start here
584
    for addr in range(0, MEM_SIZE, TX_BLOCK_SIZE):
585
        # getting the block of data to send
586
        d = data[addr:addr + TX_BLOCK_SIZE]
587

    
588
        # build the frame to send
589
        frame = _make_frame("X", addr, TX_BLOCK_SIZE, d)
590

    
591
        # first block must not send the ACK at the beginning for the
592
        # ones that has the extra id, since this have to do a extra step
593
        if addr == 0:
594
            frame = frame[1:]
595

    
596
        # send the frame
597
        _send(radio, frame)
598

    
599
        # receiving the response
600
        ack = _rawrecv(radio, 1)
601

    
602
        # basic check
603
        if len(ack) != 1:
604
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)
605

    
606
        if ack not in bytes(b"\x06\x05"):
607
            raise errors.RadioError("Bad ACK writing block 0x%04x:" % addr)
608

    
609
        # UI Update
610
        status.cur = addr // TX_BLOCK_SIZE
611
        status.msg = "Cloning to radio..."
612
        radio.status_fn(status)
613

    
614

    
615
def model_match(cls, data):
616
    """Match the opened/downloaded image to the correct version"""
617
    rid = data[0x3f70:0x3f76]
618

    
619
    if rid in cls._fileid:
620
        return True
621

    
622
    return False
623

    
624

    
625
def _decode_ranges(low, high):
626
    """Unpack the data in the ranges zones in the memmap and return
627
    a tuple with the integer corresponding to the MHz it means"""
628
    ilow = int(low[0]) * 100 + int(low[1]) * 10 + int(low[2])
629
    ihigh = int(high[0]) * 100 + int(high[1]) * 10 + int(high[2])
630
    ilow *= 1000000
631
    ihigh *= 1000000
632

    
633
    return (ilow, ihigh)
634

    
635

    
636
def _split(rf, f1, f2):
637
    """Returns False if the two freqs are in the same band (no split)
638
    or True otherwise"""
639

    
640
    # determine if the two freqs are in the same band
641
    for low, high in rf.valid_bands:
642
        if f1 >= low and f1 <= high and \
643
                f2 >= low and f2 <= high:
644
            # if the two freqs are on the same Band this is not a split
645
            return False
646

    
647
    # if you get here is because the freq pairs are split
648
    return True
649

    
650

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

    
678
    @classmethod
679
    def get_prompts(cls):
680
        rp = chirp_common.RadioPrompts()
681
        rp.experimental = \
682
            ('This driver is experimental.\n'
683
             '\n'
684
             'Please keep a copy of your memories with the original software '
685
             'if you treasure them, this driver is new and may contain'
686
             ' bugs.\n'
687
             '\n'
688
             )
689
        rp.pre_download = _(
690
            "Follow these instructions to download your info:\n"
691
            "1 - Turn off your radio\n"
692
            "2 - Connect your interface cable\n"
693
            "3 - Turn on your radio\n"
694
            "4 - Do the download of your radio data\n")
695
        rp.pre_upload = _(
696
            "Follow these instructions to upload your info:\n"
697
            "1 - Turn off your radio\n"
698
            "2 - Connect your interface cable\n"
699
            "3 - Turn on your radio\n"
700
            "4 - Do the upload of your radio data\n")
701
        return rp
702

    
703
    def get_features(self):
704
        """Get the radio's features"""
705

    
706
        # we will use the following var as global
707
        global POWER_LEVELS
708

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

    
740
        # power levels
741
        POWER_LEVELS = self._power_levels
742
        rf.valid_power_levels = POWER_LEVELS
743

    
744
        # normal dual bands
745
        rf.valid_bands = [self._vhf_range, self._uhf_range]
746

    
747
        # 220 band
748
        if self.BANDS == 3 or self.BANDS == 4:
749
            rf.valid_bands.append(self._220_range)
750

    
751
        # 350 band
752
        if self.BANDS == 4:
753
            rf.valid_bands.append(self._350_range)
754

    
755
        return rf
756

    
757
    def sync_in(self):
758
        """Download from radio"""
759
        data = _download(self)
760
        self._mmap = memmap.MemoryMapBytes(data)
761
        self.process_mmap()
762

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

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

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

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

    
793
            tone = DTCS[index]
794
            return 'DTCS', tone, pol
795

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

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

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

    
828
        # Memory number
829
        mem.number = number
830

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

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

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

    
862
        # power
863
        mem.power = POWER_LEVELS[int(_mem.power)]
864

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

    
868
        # skip
869
        mem.skip = SKIP_VALUES[_mem.add]
870

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

    
877
        # Extra
878
        mem.extra = RadioSettingGroup("extra", "Extra")
879

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

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

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

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

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

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

    
916
        immutable = []
917

    
918
        if self._gmrs:
919
            if self.MODEL == "GMRS-50X1":
920
                if mem.number >= 1 and mem.number <= 30:
921
                    if mem.freq in GMRS_FREQS:
922
                        GMRS_FREQ = GMRS_FREQS[mem.number - 1]
923
                        mem.freq = GMRS_FREQ
924
                        immutable = ["empty", "freq"]
925
                    if mem.number >= 1 and mem.number <= 7:
926
                        mem.duplex = ''
927
                        mem.offset = 0
928
                        mem.power = POWER_LEVELS[2]
929
                        immutable += ["duplex", "offset", "power"]
930
                    elif mem.number >= 8 and mem.number <= 14:
931
                        mem.duplex = 'off'
932
                        mem.offset = 0
933
                        immutable += ["duplex", "offset"]
934
                    elif mem.number >= 15 and mem.number <= 22:
935
                        mem.duplex = ''
936
                        mem.offset = 0
937
                        immutable += ["duplex", "offset"]
938
                    elif mem.number >= 23 and mem.number <= 30:
939
                        mem.duplex = '+'
940
                        mem.offset = 5000000
941
                        immutable += ["duplex", "offset"]
942
                else:
943
                    # Not a GMRS channel, so restrict duplex since it will be
944
                    # forced to off.
945
                    mem.duplex = 'off'
946
                    mem.offset = 0
947
                    immutable = ["duplex", "offset"]
948
            elif self.MODEL in ["GMRS-20V2", "GMRS-50V2"]:
949
                if mem.freq in GMRS_FREQS:
950
                    if mem.freq in GMRS_FREQS1:
951
                        # Non-repeater GMRS channels (limit duplex)
952
                        mem.duplex = ''
953
                        mem.offset = 0
954
                        mem.power = POWER_LEVELS[2]
955
                        immutable = ["duplex", "offset", "power"]
956
                    elif mem.freq in GMRS_FREQS2:
957
                        # Non-repeater FRS channels (receive only)
958
                        mem.duplex = 'off'
959
                        mem.offset = 0
960
                        immutable = ["duplex", "offset"]
961
                    elif mem.freq in GMRS_FREQS3:
962
                        # GMRS repeater channels, always either simplex or
963
                        # +5 MHz
964
                        if mem.duplex != '+':
965
                            mem.duplex = ''
966
                            mem.offset = 0
967
                        else:
968
                            mem.offset = 5000000
969
                else:
970
                    # Not a GMRS channel, so restrict duplex since it will be
971
                    # forced to off.
972
                    mem.duplex = 'off'
973
                    mem.offset = 0
974
                    immutable = ["duplex", "offset"]
975
            elif self.MODEL == "DB25-G":
976
                if mem.freq in GMRS_FREQS:
977
                    if mem.freq in GMRS_FREQS1:
978
                        # Non-repeater GMRS channels (limit duplex)
979
                        mem.duplex = ''
980
                        mem.offset = 0
981
                        immutable = ["duplex", "offset"]
982
                    elif mem.freq in GMRS_FREQS2:
983
                        # Non-repeater FRS channels (receive only)
984
                        mem.duplex = 'off'
985
                        mem.offset = 0
986
                        immutable = ["duplex", "offset"]
987
                    elif mem.freq in GMRS_FREQS3:
988
                        # GMRS repeater channels, always either simplex or
989
                        # +5 MHz
990
                        if mem.duplex != '+':
991
                            mem.duplex = ''
992
                            mem.offset = 0
993
                        else:
994
                            mem.offset = 5000000
995

    
996
        mem.immutable = immutable
997

    
998
        return mem
999

    
1000
    def set_memory(self, mem):
1001
        """Set the memory data in the eeprom img from the UI"""
1002
        # get the eprom representation of this channel
1003
        _mem = self._memobj.memory[mem.number]
1004
        _names = self._memobj.names[mem.number]
1005

    
1006
        mem_was_empty = False
1007
        # same method as used in get_memory for determining if mem is empty
1008
        # doing this BEFORE overwriting it with new values ...
1009
        if _mem.get_raw()[0] == "\xFF":
1010
            LOG.debug("This mem was empty before")
1011
            mem_was_empty = True
1012

    
1013
        # if empty memory
1014
        if mem.empty:
1015
            # the channel itself
1016
            _mem.set_raw("\xFF" * 16)
1017
            # the name tag
1018
            _names.set_raw("\xFF" * 16)
1019
            return
1020

    
1021
        if mem_was_empty:
1022
            # Zero the whole memory if we're making it unempty for
1023
            # the first time
1024
            LOG.debug('Zeroing new memory')
1025
            _mem.set_raw('\x00' * 16)
1026

    
1027
        # frequency
1028
        _mem.rxfreq = mem.freq / 10
1029

    
1030
        # duplex
1031
        if mem.duplex == "+":
1032
            _mem.txfreq = (mem.freq + mem.offset) / 10
1033
        elif mem.duplex == "-":
1034
            _mem.txfreq = (mem.freq - mem.offset) / 10
1035
        elif mem.duplex == "off":
1036
            for i in _mem.txfreq:
1037
                i.set_raw("\xFF")
1038
        elif mem.duplex == "split":
1039
            _mem.txfreq = mem.offset / 10
1040
        else:
1041
            _mem.txfreq = mem.freq / 10
1042

    
1043
        # tone data
1044
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
1045
            chirp_common.split_tone_encode(mem)
1046
        self._encode_tone(_mem.txtone, txmode, txtone, txpol)
1047
        self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)
1048

    
1049
        # name TAG of the channel
1050
        _names.name = mem.name.rstrip(' ').ljust(self.NAME_LENGTH, "\xFF")
1051

    
1052
        # power, # default power level is high
1053
        _mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)
1054

    
1055
        # wide/narrow
1056
        _mem.wide = MODES.index(mem.mode)
1057

    
1058
        # scan add property
1059
        _mem.add = SKIP_VALUES.index(mem.skip)
1060

    
1061
        # resetting unknowns, this have to be set by hand
1062
        _mem.unknown0 = 0
1063
        _mem.unknown1 = 0
1064
        _mem.unknown2 = 0
1065
        _mem.unknown3 = 0
1066
        _mem.unknown4 = 0
1067
        _mem.unknown5 = 0
1068
        _mem.unknown6 = 0
1069

    
1070
        def _zero_settings():
1071
            _mem.spmute = 0
1072
            _mem.optsig = 0
1073
            _mem.scramble = 0
1074
            _mem.bcl = 0
1075
            _mem.pttid = 0
1076
            _mem.scode = 0
1077

    
1078
        if self.COLOR_LCD and _mem.scramble:
1079
            LOG.info('Resetting scramble bit for BTECH COLOR_LCD variant')
1080
            _mem.scramble = 0
1081

    
1082
        # extra settings
1083
        if len(mem.extra) > 0:
1084
            # there are setting, parse
1085
            LOG.debug("Extra-Setting supplied. Setting them.")
1086
            # Zero them all first so any not provided by model don't
1087
            # stay set
1088
            _zero_settings()
1089
            for setting in mem.extra:
1090
                setattr(_mem, setting.get_name(), setting.value)
1091
        else:
1092
            if mem.empty:
1093
                LOG.debug("New mem is empty.")
1094
            else:
1095
                LOG.debug("New mem is NOT empty")
1096
                # set extra-settings to default ONLY when a previously empty or
1097
                # deleted memory was edited to prevent errors such as #4121
1098
                if mem_was_empty:
1099
                    LOG.debug("old mem was empty. Setting default for extras.")
1100
                    _zero_settings()
1101

    
1102
        return mem
1103

    
1104
    def get_settings(self):
1105
        """Translate the bit in the mem_struct into settings in the UI"""
1106
        _mem = self._memobj
1107
        basic = RadioSettingGroup("basic", "Basic Settings")
1108
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
1109
        other = RadioSettingGroup("other", "Other Settings")
1110
        work = RadioSettingGroup("work", "Work Mode Settings")
1111
        top = RadioSettings(basic, advanced, other, work)
1112

    
1113
        # Basic
1114
        if self.COLOR_LCD:
1115
            val = min(_mem.settings.tmr, len(self.LIST_TMR) - 1)
1116
            rs = RadioSettingValueList(self.LIST_TMR, self.LIST_TMR[val])
1117
            tmr = RadioSetting("settings.tmr", "Transceiver multi-receive", rs)
1118
            basic.append(tmr)
1119
        else:
1120
            tdr = RadioSetting("settings.tdr", "Transceiver dual receive",
1121
                               RadioSettingValueBoolean(_mem.settings.tdr))
1122
            basic.append(tdr)
1123

    
1124
        val = min(_mem.settings.sql, 9)
1125
        rs = RadioSettingValueInteger(0, 9, val)
1126
        sql = RadioSetting("settings.sql", "Squelch level", rs)
1127
        basic.append(sql)
1128

    
1129
        if self.MODEL == "GMRS-50X1" or self.MODEL == "GMRS-50V2":
1130
            rs = RadioSettingValueBoolean(_mem.settings.autolk)
1131
            autolk = RadioSetting("settings.autolk", "Auto keylock", rs)
1132
            basic.append(autolk)
1133

    
1134
        if self.MODEL == "DB25-G":
1135
            val = min(_mem.settings.mgain2, 127)
1136
            rs = RadioSettingValueInteger(0, 127, val)
1137
            mgain2 = RadioSetting("settings.mgain2", "Mic gain", rs)
1138
            basic.append(mgain2)
1139

    
1140
        val = min(_mem.settings.tot, len(LIST_TOT) - 1)
1141
        rs = RadioSettingValueList(LIST_TOT, LIST_TOT[val])
1142
        tot = RadioSetting("settings.tot", "Time out timer", rs)
1143
        basic.append(tot)
1144

    
1145
        if self.MODEL == "KT-8R":
1146
            rs = RadioSettingValueBoolean(_mem.settings.save)
1147
            save = RadioSetting("settings.save", "Battery Save", rs)
1148
            basic.append(save)
1149

    
1150
        model_list = ["KT-8R", "KT-WP12", "WP-9900"]
1151
        if self.MODEL not in model_list:
1152
            if self.VENDOR == "BTECH" or self.COLOR_LCD:
1153
                val = min(_mem.settings.apo, len(LIST_APO) - 1)
1154
                rs = RadioSettingValueList(LIST_APO, LIST_APO[val])
1155
                apo = RadioSetting("settings.apo", "Auto power off timer", rs)
1156
                basic.append(apo)
1157
            else:
1158
                val = min(_mem.settings.apo, len(LIST_OFF1TO10) - 1)
1159
                rs = RadioSettingValueList(LIST_OFF1TO10, LIST_OFF1TO10[val])
1160
                toa = RadioSetting("settings.apo", "Time out alert timer", rs)
1161
                basic.append(toa)
1162

    
1163
        val = min(_mem.settings.abr, len(LIST_OFF1TO50) - 1)
1164
        rs = RadioSettingValueList(LIST_OFF1TO50, LIST_OFF1TO50[val])
1165
        abr = RadioSetting("settings.abr", "Backlight timer", rs)
1166
        basic.append(abr)
1167

    
1168
        rs = RadioSettingValueBoolean(_mem.settings.beep)
1169
        beep = RadioSetting("settings.beep", "Key beep", rs)
1170
        basic.append(beep)
1171

    
1172
        if self.MODEL == "GMRS-20V2":
1173
            val = min(_mem.settings.volume, 58)
1174
            rs = RadioSettingValueInteger(0, 58, val)
1175
            volume = RadioSetting("settings.volume", "Volume", rs)
1176
            basic.append(volume)
1177

    
1178
        if self.MODEL == "KT-WP12" or self.MODEL == "WP-9900":
1179
            val = min(_mem.settings.volume, 50)
1180
            rs = RadioSettingValueInteger(1, 51, val + 1)
1181
            volume = RadioSetting("settings.volume", "Volume", rs)
1182
            basic.append(volume)
1183

    
1184
        if self.MODEL == "KT-8R":
1185
            rs = RadioSettingValueBoolean(_mem.settings.dsub)
1186
            dsub = RadioSetting("settings.dsub", "CTCSS/DCS code display", rs)
1187
            basic.append(dsub)
1188

    
1189
        if self.MODEL == "KT-8R" or self.COLOR_LCD4:
1190
            rs = RadioSettingValueBoolean(_mem.settings.dtmfst)
1191
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone", rs)
1192
            basic.append(dtmfst)
1193
        else:
1194
            val = min(_mem.settings.dtmfst, len(LIST_DTMFST) - 1)
1195
            rs = RadioSettingValueList(LIST_DTMFST, LIST_DTMFST[val])
1196
            dtmfst = RadioSetting("settings.dtmfst", "DTMF side tone", rs)
1197
            basic.append(dtmfst)
1198

    
1199
        if not self.COLOR_LCD:
1200
            rs = RadioSettingValueBoolean(_mem.settings.prisc)
1201
            prisc = RadioSetting("settings.prisc", "Priority scan", rs)
1202
            basic.append(prisc)
1203

    
1204
            val = min(_mem.settings.prich, self._upper)
1205
            rs = RadioSettingValueInteger(0, self._upper, val)
1206
            prich = RadioSetting("settings.prich", "Priority channel", rs)
1207
            basic.append(prich)
1208

    
1209
        val = min(_mem.settings.screv, len(LIST_SCREV) - 1)
1210
        rs = RadioSettingValueList(LIST_SCREV, LIST_SCREV[val])
1211
        screv = RadioSetting("settings.screv", "Scan resume method", rs)
1212
        basic.append(screv)
1213

    
1214
        val = min(_mem.settings.pttlt, 30)
1215
        rs = RadioSettingValueInteger(0, 30, val)
1216
        pttlt = RadioSetting("settings.pttlt", "PTT transmit delay", rs)
1217
        basic.append(pttlt)
1218

    
1219
        if self.VENDOR == "BTECH" and self.COLOR_LCD and not \
1220
                self.MODEL == "GMRS-20V2":
1221
            val = min(_mem.settings.emctp, len(LIST_EMCTPX) - 1)
1222
            rs = RadioSettingValueList(LIST_EMCTPX, LIST_EMCTPX[val])
1223
            emctp = RadioSetting("settings.emctp", "Alarm mode", rs)
1224
            basic.append(emctp)
1225
        else:
1226
            val = min(_mem.settings.emctp, len(LIST_EMCTP) - 1)
1227
            rs = RadioSettingValueList(LIST_EMCTP, LIST_EMCTP[val])
1228
            emctp = RadioSetting("settings.emctp", "Alarm mode", rs)
1229
            basic.append(emctp)
1230

    
1231
        val = min(_mem.settings.emcch, self._upper)
1232
        rs = RadioSettingValueInteger(0, self._upper, val)
1233
        emcch = RadioSetting("settings.emcch", "Alarm channel", rs)
1234
        basic.append(emcch)
1235

    
1236
        if self.COLOR_LCD:
1237
            rs = RadioSettingValueBoolean(_mem.settings.sigbp)
1238
            sigbp = RadioSetting("settings.sigbp", "Signal beep", rs)
1239
            basic.append(sigbp)
1240
        else:
1241
            val = min(_mem.settings.ringt, len(LIST_OFF1TO9) - 1)
1242
            rs = RadioSettingValueList(LIST_OFF1TO9, LIST_OFF1TO9[val])
1243
            ringt = RadioSetting("settings.ringt", "Ring time", rs)
1244
            basic.append(ringt)
1245

    
1246
        val = min(_mem.settings.camdf, len(LIST_MDF) - 1)
1247
        rs = RadioSettingValueList(LIST_MDF, LIST_MDF[val])
1248
        camdf = RadioSetting("settings.camdf", "Display mode A", rs)
1249
        basic.append(camdf)
1250

    
1251
        val = min(_mem.settings.cbmdf, len(LIST_MDF) - 1)
1252
        rs = RadioSettingValueList(LIST_MDF, LIST_MDF[val])
1253
        cbmdf = RadioSetting("settings.cbmdf", "Display mode B", rs)
1254
        basic.append(cbmdf)
1255

    
1256
        if self.COLOR_LCD:
1257
            val = min(_mem.settings.ccmdf, len(LIST_MDF) - 1)
1258
            rs = RadioSettingValueList(LIST_MDF, LIST_MDF[val])
1259
            ccmdf = RadioSetting("settings.ccmdf", "Display mode C", rs)
1260
            basic.append(ccmdf)
1261

    
1262
            if not self.COLOR_LCD4:
1263
                val = min(_mem.settings.cdmdf, len(LIST_MDF) - 1)
1264
                rs = RadioSettingValueList(LIST_MDF, LIST_MDF[val])
1265
                cdmdf = RadioSetting("settings.cdmdf", "Display mode D", rs)
1266
                basic.append(cdmdf)
1267

    
1268
                if self.MODEL in ["UV-50X2_G2", "UV-25X2_G2", "UV-25X4_G2"]:
1269
                    val = min(_mem.settings.langua, len(LIST_VOX) - 1)
1270
                    rs = RadioSettingValueList(LIST_VOX, LIST_VOX[val])
1271
                    vox = RadioSetting("settings.langua", "VOX", rs)
1272
                    basic.append(vox)
1273
                elif self.MODEL == "GMRS-50V2":
1274
                    val = min(_mem.settings.vox, len(LIST_VOX) - 1)
1275
                    rs = RadioSettingValueList(LIST_VOX, LIST_VOX[val])
1276
                    vox = RadioSetting("settings.vox", "VOX", rs)
1277
                    basic.append(vox)
1278
                else:
1279
                    val = min(_mem.settings.langua, len(LIST_LANGUA) - 1)
1280
                    rs = RadioSettingValueList(LIST_LANGUA, LIST_LANGUA[val])
1281
                    langua = RadioSetting("settings.langua", "Language", rs)
1282
                    basic.append(langua)
1283
        if self.MODEL == "KT-8R":
1284
            val = min(_mem.settings.voice, len(LIST_VOICE) - 1)
1285
            rs = RadioSettingValueList(LIST_VOICE, LIST_VOICE[val])
1286
            voice = RadioSetting("settings.voice", "Voice prompt", rs)
1287
            basic.append(voice)
1288

    
1289
        if self.MODEL == "KT-8R" or self.COLOR_LCD4:
1290
            val = min(_mem.settings.vox, len(LIST_VOX) - 1)
1291
            rs = RadioSettingValueList(LIST_VOX, LIST_VOX[val])
1292
            vox = RadioSetting("settings.vox", "VOX", rs)
1293
            basic.append(vox)
1294

    
1295
            val = min(_mem.settings.voxt, len(LIST_VOX) - 1)
1296
            rs = RadioSettingValueList(LIST_VOXT, LIST_VOXT[val])
1297
            voxt = RadioSetting("settings.voxt", "VOX delay time", rs)
1298
            basic.append(voxt)
1299

    
1300
        if self.VENDOR == "BTECH":
1301
            if self.COLOR_LCD:
1302
                if self.MODEL == "GMRS-20V2":
1303
                    val = min(_mem.settings.sync, len(LIST_SYNCV2) - 1)
1304
                    rs = RadioSettingValueList(LIST_SYNCV2, LIST_SYNCV2[val])
1305
                    sync = RadioSetting("settings.sync",
1306
                                        "Channel display sync", rs)
1307
                    basic.append(sync)
1308
                else:
1309
                    val = min(_mem.settings.sync, len(LIST_SYNC) - 1)
1310
                    rs = RadioSettingValueList(LIST_SYNC, LIST_SYNC[val])
1311
                    sync = RadioSetting("settings.sync",
1312
                                        "Channel display sync", rs)
1313
                    basic.append(sync)
1314
            else:
1315
                rs = RadioSettingValueBoolean(_mem.settings.sync)
1316
                sync = RadioSetting("settings.sync", "A/B channel sync", rs)
1317
                basic.append(sync)
1318

    
1319
        if self.COLOR_LCD4:
1320
            rs = RadioSettingValueBoolean(_mem.settings.autolock)
1321
            autolk = RadioSetting("settings.autolock", "Auto keylock", rs)
1322
            basic.append(autolk)
1323

    
1324
        if not self.COLOR_LCD:
1325
            val = min(_mem.settings.ponmsg, len(LIST_PONMSG) - 1)
1326
            rs = RadioSettingValueList(LIST_PONMSG, LIST_PONMSG[val])
1327
            ponmsg = RadioSetting("settings.ponmsg", "Power-on message", rs)
1328
            basic.append(ponmsg)
1329

    
1330
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3 or
1331
                                   self.COLOR_LCD4):
1332
            val = min(_mem.settings.mainfc, len(LIST_COLOR9) - 1)
1333
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1334
            mainfc = RadioSetting("settings.mainfc",
1335
                                  "Main LCD foreground color", rs)
1336
            basic.append(mainfc)
1337

    
1338
            if not self.COLOR_LCD4:
1339
                val = min(_mem.settings.mainbc, len(LIST_COLOR9) - 1)
1340
                rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1341
                mainbc = RadioSetting("settings.mainbc",
1342
                                      "Main LCD background color", rs)
1343
                basic.append(mainbc)
1344

    
1345
            val = min(_mem.settings.menufc, len(LIST_COLOR9) - 1)
1346
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1347
            menufc = RadioSetting("settings.menufc",
1348
                                  "Menu foreground color", rs)
1349
            basic.append(menufc)
1350

    
1351
            if not self.COLOR_LCD4:
1352
                val = min(_mem.settings.menubc, len(LIST_COLOR9) - 1)
1353
                rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1354
                menubc = RadioSetting("settings.menubc",
1355
                                      "Menu background color", rs)
1356
                basic.append(menubc)
1357

    
1358
            val = min(_mem.settings.stafc, len(LIST_COLOR9) - 1)
1359
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1360
            stafc = RadioSetting("settings.stafc",
1361
                                 "Top status foreground color", rs)
1362
            basic.append(stafc)
1363

    
1364
            if not self.COLOR_LCD4:
1365
                val = min(_mem.settings.stabc, len(LIST_COLOR9) - 1)
1366
                rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1367
                stabc = RadioSetting("settings.stabc",
1368
                                     "Top status background color", rs)
1369
                basic.append(stabc)
1370

    
1371
            val = min(_mem.settings.sigfc, len(LIST_COLOR9) - 1)
1372
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1373
            sigfc = RadioSetting("settings.sigfc",
1374
                                 "Bottom status foreground color", rs)
1375
            basic.append(sigfc)
1376

    
1377
            if not self.COLOR_LCD4:
1378
                val = min(_mem.settings.sigbc, len(LIST_COLOR9) - 1)
1379
                rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1380
                sigbc = RadioSetting("settings.sigbc",
1381
                                     "Bottom status background color", rs)
1382
                basic.append(sigbc)
1383

    
1384
            val = min(_mem.settings.rxfc, len(LIST_COLOR9) - 1)
1385
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1386
            rxfc = RadioSetting("settings.rxfc",
1387
                                "Receiving character color", rs)
1388
            basic.append(rxfc)
1389

    
1390
            val = min(_mem.settings.txfc, len(LIST_COLOR9) - 1)
1391
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1392
            txfc = RadioSetting("settings.txfc",
1393
                                "Transmitting character color", rs)
1394
            basic.append(txfc)
1395

    
1396
            if not self.COLOR_LCD4:
1397
                val = min(_mem.settings.txdisp, len(LIST_TXDISP) - 1)
1398
                rs = RadioSettingValueList(LIST_TXDISP, LIST_TXDISP[val])
1399
                txdisp = RadioSetting("settings.txdisp",
1400
                                      "Transmitting status display", rs)
1401
                basic.append(txdisp)
1402

    
1403
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1404
            val = min(_mem.settings.stfc, len(LIST_COLOR8) - 1)
1405
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1406
            stfc = RadioSetting("settings.stfc", "ST-FC", rs)
1407
            basic.append(stfc)
1408

    
1409
            val = min(_mem.settings.mffc, len(LIST_COLOR8) - 1)
1410
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1411
            mffc = RadioSetting("settings.mffc", "MF-FC", rs)
1412
            basic.append(mffc)
1413

    
1414
            val = min(_mem.settings.sfafc, len(LIST_COLOR8) - 1)
1415
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1416
            sfafc = RadioSetting("settings.sfafc", "SFA-FC", rs)
1417
            basic.append(sfafc)
1418

    
1419
            val = min(_mem.settings.sfbfc, len(LIST_COLOR8) - 1)
1420
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1421
            sfbfc = RadioSetting("settings.sfbfc", "SFB-FC", rs)
1422
            basic.append(sfbfc)
1423

    
1424
            val = min(_mem.settings.sfcfc, len(LIST_COLOR8) - 1)
1425
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1426
            sfcfc = RadioSetting("settings.sfcfc", "SFC-FC", rs)
1427
            basic.append(sfcfc)
1428

    
1429
            val = min(_mem.settings.sfdfc, len(LIST_COLOR8) - 1)
1430
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1431
            sfdfc = RadioSetting("settings.sfdfc", "SFD-FC", rs)
1432
            basic.append(sfdfc)
1433

    
1434
            val = min(_mem.settings.subfc, len(LIST_COLOR8) - 1)
1435
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1436
            subfc = RadioSetting("settings.subfc", "SUB-FC", rs)
1437
            basic.append(subfc)
1438

    
1439
            val = min(_mem.settings.fmfc, len(LIST_COLOR8) - 1)
1440
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1441
            fmfc = RadioSetting("settings.fmfc", "FM-FC", rs)
1442
            basic.append(fmfc)
1443

    
1444
            val = min(_mem.settings.sigfc, len(LIST_COLOR8) - 1)
1445
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1446
            sigfc = RadioSetting("settings.sigfc", "SIG-FC", rs)
1447
            basic.append(sigfc)
1448

    
1449
            if not self.MODEL == "KT-8R":
1450
                val = min(_mem.settings.modfc, len(LIST_COLOR8) - 1)
1451
                rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1452
                modfc = RadioSetting("settings.modfc", "MOD-FC", rs)
1453
                basic.append(modfc)
1454

    
1455
            val = min(_mem.settings.menufc, len(LIST_COLOR8) - 1)
1456
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1457
            menufc = RadioSetting("settings.menufc", "MENUFC", rs)
1458
            basic.append(menufc)
1459

    
1460
            val = min(_mem.settings.txfc, len(LIST_COLOR8) - 1)
1461
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1462
            txfc = RadioSetting("settings.txfc", "TX-FC", rs)
1463
            basic.append(txfc)
1464

    
1465
            if self.MODEL == "KT-8R":
1466
                val = min(_mem.settings.rxfc, len(LIST_COLOR8) - 1)
1467
                rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1468
                rxfc = RadioSetting("settings.rxfc", "RX-FC", rs)
1469
                basic.append(rxfc)
1470

    
1471
            if not self.MODEL == "KT-8R" and not self.MODEL == "GMRS-50V2":
1472
                val = min(_mem.settings.txdisp, len(LIST_TXDISP) - 1)
1473
                rs = RadioSettingValueList(LIST_TXDISP, LIST_TXDISP[val])
1474
                txdisp = RadioSetting("settings.txdisp",
1475
                                      "Transmitting status display", rs)
1476
                basic.append(txdisp)
1477
        elif self.COLOR_LCD4:
1478
            val = min(_mem.settings.asfc, len(LIST_COLOR8) - 1)
1479
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1480
            asfc = RadioSetting("settings.asfc", "Above Stat fore color", rs)
1481
            basic.append(asfc)
1482

    
1483
            val = min(_mem.settings.mainfc, len(LIST_COLOR8) - 1)
1484
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1485
            mainfc = RadioSetting("settings.mainfc", "Main fore color", rs)
1486
            basic.append(mainfc)
1487

    
1488
            val = min(_mem.settings.a_fc, len(LIST_COLOR8) - 1)
1489
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1490
            a_fc = RadioSetting("settings.a_fc", "A - fore color", rs)
1491
            basic.append(a_fc)
1492

    
1493
            val = min(_mem.settings.b_fc, len(LIST_COLOR8) - 1)
1494
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1495
            b_fc = RadioSetting("settings.b_fc", "B - fore color", rs)
1496
            basic.append(b_fc)
1497

    
1498
            val = min(_mem.settings.c_fc, len(LIST_COLOR8) - 1)
1499
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1500
            c_fc = RadioSetting("settings.c_fc", "C - fore color", rs)
1501
            basic.append(c_fc)
1502

    
1503
            val = min(_mem.settings.subfc, len(LIST_COLOR8) - 1)
1504
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1505
            subfc = RadioSetting("settings.subfc", "Sub fore color", rs)
1506
            basic.append(subfc)
1507

    
1508
            val = min(_mem.settings.battfc, len(LIST_COLOR8) - 1)
1509
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1510
            battfc = RadioSetting("settings.battfc", "Battery fore color", rs)
1511
            basic.append(battfc)
1512

    
1513
            val = min(_mem.settings.sigfc, len(LIST_COLOR8) - 1)
1514
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1515
            sigfc = RadioSetting("settings.sigfc", "Signal fore color", rs)
1516
            basic.append(sigfc)
1517

    
1518
            val = min(_mem.settings.menufc, len(LIST_COLOR8) - 1)
1519
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1520
            menufc = RadioSetting("settings.menufc", "Menu fore color", rs)
1521
            basic.append(menufc)
1522

    
1523
            val = min(_mem.settings.txfc, len(LIST_COLOR8) - 1)
1524
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1525
            txfc = RadioSetting("settings.txfc", "TX fore color", rs)
1526
            basic.append(txfc)
1527

    
1528
            val = min(_mem.settings.rxfc, len(LIST_COLOR8) - 1)
1529
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1530
            rxfc = RadioSetting("settings.rxfc", "RX fore color", rs)
1531
            basic.append(rxfc)
1532
        else:
1533
            val = min(_mem.settings.wtled, len(LIST_COLOR4) - 1)
1534
            rs = RadioSettingValueList(LIST_COLOR4, LIST_COLOR4[val])
1535
            wtled = RadioSetting("settings.wtled",
1536
                                 "Standby backlight Color", rs)
1537
            basic.append(wtled)
1538

    
1539
            val = min(_mem.settings.rxled, len(LIST_COLOR4) - 1)
1540
            rs = RadioSettingValueList(LIST_COLOR4, LIST_COLOR4[val])
1541
            rxled = RadioSetting("settings.rxled", "RX backlight Color", rs)
1542
            basic.append(rxled)
1543

    
1544
            val = min(_mem.settings.txled, len(LIST_COLOR4) - 1)
1545
            rs = RadioSettingValueList(LIST_COLOR4, LIST_COLOR4[val])
1546
            txled = RadioSetting("settings.txled", "TX backlight Color", rs)
1547
            basic.append(txled)
1548

    
1549
        val = min(_mem.settings.anil, len(LIST_ANIL) - 1)
1550
        rs = RadioSettingValueList(LIST_ANIL, LIST_ANIL[val])
1551
        anil = RadioSetting("settings.anil", "ANI length", rs)
1552
        basic.append(anil)
1553

    
1554
        val = min(_mem.settings.reps, len(LIST_REPS) - 1)
1555
        rs = RadioSettingValueList(LIST_REPS, LIST_REPS[val])
1556
        reps = RadioSetting("settings.reps", "Relay signal (tone burst)", rs)
1557
        basic.append(reps)
1558

    
1559
        if self.COLOR_LCD4:
1560
            rs = RadioSettingValueBoolean(_mem.settings.dsub)
1561
            dsub = RadioSetting("settings.dsub", "Subtone display", rs)
1562
            basic.append(dsub)
1563

    
1564
        if self.MODEL == "GMRS-20V2":
1565
            val = min(_mem.settings.repsw, len(LIST_REPSW) - 1)
1566
            rs = RadioSettingValueList(LIST_REPSW, LIST_REPSW[val])
1567
            repsw = RadioSetting("settings.repsw", "Repeater SW", rs)
1568
            basic.append(repsw)
1569

    
1570
        model_list = ["KT-8R", "KT-WP12", "WP-9900"]
1571
        if self.MODEL not in model_list:
1572
            val = min(_mem.settings.repm, len(LIST_REPM) - 1)
1573
            rs = RadioSettingValueList(LIST_REPM, LIST_REPM[val])
1574
            repm = RadioSetting("settings.repm", "Relay condition", rs)
1575
            basic.append(repm)
1576

    
1577
        if self.VENDOR == "BTECH" or self.COLOR_LCD:
1578
            if self.COLOR_LCD:
1579
                val = min(_mem.settings.tmrmr, len(LIST_OFF1TO50) - 1)
1580
                rs = RadioSettingValueList(LIST_OFF1TO50, LIST_OFF1TO50[val])
1581
                tmrmr = RadioSetting("settings.tmrmr", "TMR return time", rs)
1582
                basic.append(tmrmr)
1583
            else:
1584
                val = min(_mem.settings.tdrab, len(LIST_OFF1TO50) - 1)
1585
                rs = RadioSettingValueList(LIST_OFF1TO50, LIST_OFF1TO50[val])
1586
                tdrab = RadioSetting("settings.tdrab", "TDR return time", rs)
1587
                basic.append(tdrab)
1588

    
1589
            rs = RadioSettingValueBoolean(_mem.settings.ste)
1590
            ste = RadioSetting("settings.ste", "Squelch tail eliminate", rs)
1591
            basic.append(ste)
1592

    
1593
            if self.COLOR_LCD4:
1594
                val = min(_mem.settings.rpste, len(LIST_OFF1TO10) - 1)
1595
                rs = RadioSettingValueList(LIST_OFF1TO10, LIST_OFF1TO10[val])
1596
                rpste = RadioSetting("settings.rpste", "Repeater STE", rs)
1597
                basic.append(rpste)
1598
            else:
1599
                val = min(_mem.settings.rpste, len(LIST_OFF1TO9) - 1)
1600
                rs = RadioSettingValueList(LIST_OFF1TO9, LIST_OFF1TO9[val])
1601
                rpste = RadioSetting("settings.rpste", "Repeater STE", rs)
1602
                basic.append(rpste)
1603

    
1604
            if self.COLOR_LCD4:
1605
                val = min(_mem.settings.rptdl, len(LIST_OFF1TO60) - 1)
1606
                rs = RadioSettingValueList(LIST_OFF1TO60, LIST_OFF1TO60[val])
1607
                rptdl = RadioSetting("settings.rptdl",
1608
                                     "Repeater STE delay", rs)
1609
                basic.append(rptdl)
1610
            else:
1611
                val = min(_mem.settings.rptdl, len(LIST_RPTDL) - 1)
1612
                rs = RadioSettingValueList(LIST_RPTDL, LIST_RPTDL[val])
1613
                rptdl = RadioSetting("settings.rptdl",
1614
                                     "Repeater STE delay", rs)
1615
                basic.append(rptdl)
1616

    
1617
        if self.MODEL == "DB25-G":
1618
            val = min(_mem.settings.mgain, 1)
1619
            rs = RadioSettingValueBoolean(val)
1620
            mgain = RadioSetting("settings.mgain", "Auto power-on", rs)
1621
            basic.append(mgain)
1622

    
1623
        if str(_mem.fingerprint.fp) in BTECH3:
1624
            val = min(_mem.settings.mgain, 120)
1625
            rs = RadioSettingValueInteger(0, 120, val)
1626
            mgain = RadioSetting("settings.mgain", "Mic gain", rs)
1627
            basic.append(mgain)
1628

    
1629
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1630
            val = min(_mem.settings.dtmfg, 60)
1631
            rs = RadioSettingValueInteger(0, 60, val)
1632
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain", rs)
1633
            basic.append(dtmfg)
1634

    
1635
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1636
            val = min(_mem.settings.mgain, 127)
1637
            rs = RadioSettingValueInteger(0, 127, val)
1638
            mgain = RadioSetting("settings.mgain", "Mic gain", rs)
1639
            basic.append(mgain)
1640

    
1641
            val = min(_mem.settings.skiptx, len(LIST_SKIPTX) - 1)
1642
            rs = RadioSettingValueList(LIST_SKIPTX, LIST_SKIPTX[val])
1643
            skiptx = RadioSetting("settings.skiptx", "Skip TX", rs)
1644
            basic.append(skiptx)
1645

    
1646
            val = min(_mem.settings.scmode, len(LIST_SCMODE) - 1)
1647
            rs = RadioSettingValueList(LIST_SCMODE, LIST_SCMODE[val])
1648
            scmode = RadioSetting("settings.scmode", "Scan mode", rs)
1649
            basic.append(scmode)
1650

    
1651
        if self.MODEL in ["KT-8R", "UV-25X2", "UV-25X4", "UV-50X2",
1652
                          "GMRS-20V2", "UV-50X2_G2", "GMRS-50V2",
1653
                          "UV-25X2_G2", "UV-25X4_G2"]:
1654
            val = min(_mem.settings.tmrtx, len(LIST_TMRTX) - 1)
1655
            rs = RadioSettingValueList(LIST_TMRTX, LIST_TMRTX[val])
1656
            tmrtx = RadioSetting("settings.tmrtx", "TX in multi-standby", rs)
1657
            basic.append(tmrtx)
1658

    
1659
        if self.MODEL in ["UV-50X2_G2", "GMRS-50V2", "UV-25X2_G2",
1660
                          "UV-25X4_G2"]:
1661
            val = min(_mem.settings.earpho, len(LIST_EARPH) - 1)
1662
            rs = RadioSettingValueList(LIST_EARPH, LIST_EARPH[val])
1663
            earpho = RadioSetting("settings.earpho", "Earphone", rs)
1664
            basic.append(earpho)
1665

    
1666
        # Advanced
1667
        def _filter(name):
1668
            filtered = ""
1669
            for char in str(name):
1670
                if char in VALID_CHARS:
1671
                    filtered += char
1672
                else:
1673
                    filtered += " "
1674
            return filtered
1675

    
1676
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3 or
1677
                                   self.COLOR_LCD4):
1678
            _msg = self._memobj.poweron_msg
1679
            line1 = RadioSetting("poweron_msg.line1",
1680
                                 "Power-on message line 1",
1681
                                 RadioSettingValueString(0, 8, _filter(
1682
                                                         _msg.line1)))
1683
            advanced.append(line1)
1684
            line2 = RadioSetting("poweron_msg.line2",
1685
                                 "Power-on message line 2",
1686
                                 RadioSettingValueString(0, 8, _filter(
1687
                                                         _msg.line2)))
1688
            advanced.append(line2)
1689
            line3 = RadioSetting("poweron_msg.line3",
1690
                                 "Power-on message line 3",
1691
                                 RadioSettingValueString(0, 8, _filter(
1692
                                                         _msg.line3)))
1693
            advanced.append(line3)
1694
            line4 = RadioSetting("poweron_msg.line4",
1695
                                 "Power-on message line 4",
1696
                                 RadioSettingValueString(0, 8, _filter(
1697
                                                         _msg.line4)))
1698
            advanced.append(line4)
1699
            line5 = RadioSetting("poweron_msg.line5",
1700
                                 "Power-on message line 5",
1701
                                 RadioSettingValueString(0, 8, _filter(
1702
                                                         _msg.line5)))
1703
            advanced.append(line5)
1704
            line6 = RadioSetting("poweron_msg.line6",
1705
                                 "Power-on message line 6",
1706
                                 RadioSettingValueString(0, 8, _filter(
1707
                                                         _msg.line6)))
1708
            advanced.append(line6)
1709
            line7 = RadioSetting("poweron_msg.line7",
1710
                                 "Power-on message line 7",
1711
                                 RadioSettingValueString(0, 8, _filter(
1712
                                                         _msg.line7)))
1713
            advanced.append(line7)
1714
            line8 = RadioSetting("poweron_msg.line8", "Static message",
1715
                                 RadioSettingValueString(0, 8, _filter(
1716
                                                         _msg.line8)))
1717
            advanced.append(line8)
1718
        elif self.COLOR_LCD2 or self.COLOR_LCD3 or self.COLOR_LCD4:
1719
            _msg = self._memobj.static_msg
1720
            line = RadioSetting("static_msg.line", "Static message",
1721
                                RadioSettingValueString(0, 16, _filter(
1722
                                    _msg.line)))
1723
            advanced.append(line)
1724
        else:
1725
            _msg = self._memobj.poweron_msg
1726
            line1 = RadioSetting("poweron_msg.line1",
1727
                                 "Power-on message line 1",
1728
                                 RadioSettingValueString(0, 6, _filter(
1729
                                                         _msg.line1)))
1730
            advanced.append(line1)
1731
            line2 = RadioSetting("poweron_msg.line2",
1732
                                 "Power-on message line 2",
1733
                                 RadioSettingValueString(0, 6, _filter(
1734
                                                         _msg.line2)))
1735
            advanced.append(line2)
1736

    
1737
        if self.MODEL in ("UV-2501", "UV-5001"):
1738
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1739
                                   RadioSettingValueBoolean(
1740
                                       _mem.settings2.vfomren))
1741
            advanced.append(vfomren)
1742

    
1743
            reseten = RadioSetting("settings2.reseten", "RESET",
1744
                                   RadioSettingValueBoolean(
1745
                                       _mem.settings2.reseten))
1746
            advanced.append(reseten)
1747

    
1748
            menuen = RadioSetting("settings2.menuen", "Menu",
1749
                                  RadioSettingValueBoolean(
1750
                                      _mem.settings2.menuen))
1751
            advanced.append(menuen)
1752

    
1753
        # Other
1754
        def convert_bytes_to_limit(bytes):
1755
            limit = ""
1756
            for byte in bytes:
1757
                if byte < 10:
1758
                    limit += chr(byte + 0x30)
1759
                else:
1760
                    break
1761
            return limit
1762

    
1763
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1764
            _ranges = self._memobj.ranges220
1765
            ranges = "ranges220"
1766
        else:
1767
            _ranges = self._memobj.ranges
1768
            ranges = "ranges"
1769

    
1770
        _limit = convert_bytes_to_limit(_ranges.vhf_low)
1771
        val = RadioSettingValueString(0, 3, _limit)
1772
        val.set_mutable(False)
1773
        vhf_low = RadioSetting("%s.vhf_low" % ranges, "VHF low", val)
1774
        other.append(vhf_low)
1775

    
1776
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1777
        val = RadioSettingValueString(0, 3, _limit)
1778
        val.set_mutable(False)
1779
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1780
        other.append(vhf_high)
1781

    
1782
        if self.BANDS == 3 or self.BANDS == 4:
1783
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1784
            val = RadioSettingValueString(0, 3, _limit)
1785
            val.set_mutable(False)
1786
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1787
            other.append(vhf2_low)
1788

    
1789
            _limit = convert_bytes_to_limit(_ranges.vhf2_high)
1790
            val = RadioSettingValueString(0, 3, _limit)
1791
            val.set_mutable(False)
1792
            vhf2_high = RadioSetting("%s.vhf2_high" % ranges, "VHF2 high", val)
1793
            other.append(vhf2_high)
1794

    
1795
        _limit = convert_bytes_to_limit(_ranges.uhf_low)
1796
        val = RadioSettingValueString(0, 3, _limit)
1797
        val.set_mutable(False)
1798
        uhf_low = RadioSetting("%s.uhf_low" % ranges, "UHF low", val)
1799
        other.append(uhf_low)
1800

    
1801
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1802
        val = RadioSettingValueString(0, 3, _limit)
1803
        val.set_mutable(False)
1804
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1805
        other.append(uhf_high)
1806

    
1807
        if self.BANDS == 4:
1808
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1809
            val = RadioSettingValueString(0, 3, _limit)
1810
            val.set_mutable(False)
1811
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1812
            other.append(uhf2_low)
1813

    
1814
            _limit = convert_bytes_to_limit(_ranges.uhf2_high)
1815
            val = RadioSettingValueString(0, 3, _limit)
1816
            val.set_mutable(False)
1817
            uhf2_high = RadioSetting("%s.uhf2_high" % ranges, "UHF2 high", val)
1818
            other.append(uhf2_high)
1819

    
1820
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1821
        val.set_mutable(False)
1822
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1823
        other.append(fp)
1824

    
1825
        # Work
1826
        if self.COLOR_LCD4:
1827
            val = min(_mem.settings2.dispab, len(LIST_ABC) - 1)
1828
            rs = RadioSettingValueList(LIST_ABC, LIST_ABC[val])
1829
            dispab = RadioSetting("settings2.dispab", "Display", rs)
1830
            work.append(dispab)
1831
        elif self.COLOR_LCD:
1832
            val = min(_mem.settings2.dispab, len(LIST_ABCD) - 1)
1833
            rs = RadioSettingValueList(LIST_ABCD, LIST_ABCD[val])
1834
            dispab = RadioSetting("settings2.dispab", "Display", rs)
1835
            work.append(dispab)
1836
        else:
1837
            val = min(_mem.settings2.dispab, len(LIST_AB) - 1)
1838
            rs = RadioSettingValueList(LIST_AB, LIST_AB[val])
1839
            dispab = RadioSetting("settings2.dispab", "Display", rs)
1840
            work.append(dispab)
1841

    
1842
        if self.COLOR_LCD:
1843
            val = min(_mem.settings2.vfomra, len(LIST_VFOMR) - 1)
1844
            rs = RadioSettingValueList(LIST_VFOMR, LIST_VFOMR[val])
1845
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode", rs)
1846
            work.append(vfomra)
1847

    
1848
            val = min(_mem.settings2.vfomrb, len(LIST_VFOMR) - 1)
1849
            rs = RadioSettingValueList(LIST_VFOMR, LIST_VFOMR[val])
1850
            vfomrb = RadioSetting("settings2.vfomrb", "VFO/MR B mode", rs)
1851
            work.append(vfomrb)
1852

    
1853
            val = min(_mem.settings2.vfomrc, len(LIST_VFOMR) - 1)
1854
            rs = RadioSettingValueList(LIST_VFOMR, LIST_VFOMR[val])
1855
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode", rs)
1856
            work.append(vfomrc)
1857

    
1858
            if not self.COLOR_LCD4:
1859
                val = min(_mem.settings2.vfomrd, len(LIST_VFOMR) - 1)
1860
                rs = RadioSettingValueList(LIST_VFOMR, LIST_VFOMR[val])
1861
                vfomrd = RadioSetting("settings2.vfomrd", "VFO/MR D mode", rs)
1862
                work.append(vfomrd)
1863
        else:
1864
            val = min(_mem.settings2.vfomr, len(LIST_VFOMR) - 1)
1865
            rs = RadioSettingValueList(LIST_VFOMR, LIST_VFOMR[val])
1866
            vfomr = RadioSetting("settings2.vfomr", "VFO/MR mode", rs)
1867
            work.append(vfomr)
1868

    
1869
        rs = RadioSettingValueBoolean(_mem.settings2.keylock)
1870
        keylock = RadioSetting("settings2.keylock", "Keypad lock", rs)
1871
        work.append(keylock)
1872

    
1873
        val = min(_mem.settings2.mrcha, self._upper)
1874
        rs = RadioSettingValueInteger(0, self._upper, val)
1875
        mrcha = RadioSetting("settings2.mrcha", "MR A channel", rs)
1876
        work.append(mrcha)
1877

    
1878
        val = min(_mem.settings2.mrchb, self._upper)
1879
        rs = RadioSettingValueInteger(0, self._upper, val)
1880
        mrchb = RadioSetting("settings2.mrchb", "MR B channel", rs)
1881
        work.append(mrchb)
1882

    
1883
        if self.COLOR_LCD:
1884
            val = min(_mem.settings2.mrchc, self._upper)
1885
            rs = RadioSettingValueInteger(0, self._upper, val)
1886
            mrchc = RadioSetting("settings2.mrchc", "MR C channel", rs)
1887
            work.append(mrchc)
1888

    
1889
            if not self.COLOR_LCD4:
1890
                val = min(_mem.settings2.mrchd, self._upper)
1891
                rs = RadioSettingValueInteger(0, self._upper, val)
1892
                mrchd = RadioSetting("settings2.mrchd", "MR D channel", rs)
1893
                work.append(mrchd)
1894

    
1895
        def convert_bytes_to_freq(bytes):
1896
            real_freq = 0
1897
            for byte in bytes:
1898
                real_freq = (real_freq * 10) + byte
1899
            return chirp_common.format_freq(real_freq * 10)
1900

    
1901
        def my_validate(value):
1902
            _vhf_lower = int(convert_bytes_to_limit(_ranges.vhf_low))
1903
            _vhf_upper = int(convert_bytes_to_limit(_ranges.vhf_high))
1904
            _uhf_lower = int(convert_bytes_to_limit(_ranges.uhf_low))
1905
            _uhf_upper = int(convert_bytes_to_limit(_ranges.uhf_high))
1906
            if self.BANDS == 3 or self.BANDS == 4:
1907
                _vhf2_lower = int(convert_bytes_to_limit(_ranges.vhf2_low))
1908
                _vhf2_upper = int(convert_bytes_to_limit(_ranges.vhf2_high))
1909
            if self.BANDS == 4:
1910
                _uhf2_lower = int(convert_bytes_to_limit(_ranges.uhf2_low))
1911
                _uhf2_upper = int(convert_bytes_to_limit(_ranges.uhf2_high))
1912

    
1913
            value = chirp_common.parse_freq(value)
1914
            msg = ("Can't be less then %i.0000")
1915
            if value > 99000000 and value < _vhf_lower * 1000000:
1916
                raise InvalidValueError(msg % (_vhf_lower))
1917
            msg = ("Can't be betweeb %i.9975-%i.0000")
1918
            if self.BANDS == 2:
1919
                if (_vhf_upper + 1) * 1000000 <= value and \
1920
                        value < _uhf_lower * 1000000:
1921
                    raise InvalidValueError(msg % (_vhf_upper, _uhf_lower))
1922
            if self.BANDS == 3:
1923
                if (_vhf_upper + 1) * 1000000 <= value and \
1924
                        value < _vhf2_lower * 1000000:
1925
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1926
                if (_vhf2_upper + 1) * 1000000 <= value and \
1927
                        value < _uhf_lower * 1000000:
1928
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf_lower))
1929
            if self.BANDS == 4:
1930
                if (_vhf_upper + 1) * 1000000 <= value and \
1931
                        value < _vhf2_lower * 1000000:
1932
                    raise InvalidValueError(msg % (_vhf_upper, _vhf2_lower))
1933
                if (_vhf2_upper + 1) * 1000000 <= value and \
1934
                        value < _uhf2_lower * 1000000:
1935
                    raise InvalidValueError(msg % (_vhf2_upper, _uhf2_lower))
1936
                if (_uhf2_upper + 1) * 1000000 <= value and \
1937
                        value < _uhf_lower * 1000000:
1938
                    raise InvalidValueError(msg % (_uhf2_upper, _uhf_lower))
1939
            msg = ("Can't be greater then %i.9975")
1940
            if value > 99000000 and value >= _uhf_upper * 1000000:
1941
                raise InvalidValueError(msg % (_uhf_upper))
1942
            return chirp_common.format_freq(value)
1943

    
1944
        def apply_freq(setting, obj):
1945
            value = chirp_common.parse_freq(str(setting.value)) / 10
1946
            for i in range(7, -1, -1):
1947
                obj.freq[i] = value % 10
1948
                value /= 10
1949

    
1950
        _vhf_upper = (convert_bytes_to_limit(_ranges.vhf_high))
1951
        _uhf_lower = (convert_bytes_to_limit(_ranges.uhf_low))
1952

    
1953
        val1a = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1954
                                        _mem.vfo.a.freq))
1955
        val1a.set_validate_callback(my_validate)
1956
        vfoafreq = RadioSetting("vfo.a.freq", "VFO A frequency", val1a)
1957
        vfoafreq.set_apply_callback(apply_freq, _mem.vfo.a)
1958
        work.append(vfoafreq)
1959

    
1960
        val = convert_bytes_to_freq(_mem.vfo.b.freq)
1961
        if self.MODEL in ["GMRS-50V2", "GMRS-50X1"]:
1962
            if val[:3] > _vhf_upper and val[:3] < _uhf_lower:
1963
                val = "462.562500"
1964
        val1b = RadioSettingValueString(0, 10, val)
1965
        val1b.set_validate_callback(my_validate)
1966
        vfobfreq = RadioSetting("vfo.b.freq", "VFO B frequency", val1b)
1967
        vfobfreq.set_apply_callback(apply_freq, _mem.vfo.b)
1968
        work.append(vfobfreq)
1969

    
1970
        if self.COLOR_LCD:
1971
            val1c = RadioSettingValueString(0, 10, convert_bytes_to_freq(
1972
                                            _mem.vfo.c.freq))
1973
            val1c.set_validate_callback(my_validate)
1974
            vfocfreq = RadioSetting("vfo.c.freq", "VFO C frequency", val1c)
1975
            vfocfreq.set_apply_callback(apply_freq, _mem.vfo.c)
1976
            work.append(vfocfreq)
1977

    
1978
            if not self.COLOR_LCD4:
1979
                val = convert_bytes_to_freq(_mem.vfo.d.freq)
1980
                if self.MODEL in ["GMRS-50V2", "GMRS-50X1"]:
1981
                    if val[:3] > _vhf_upper and val[:3] < _uhf_lower:
1982
                        val = "462.562500"
1983
                val1d = RadioSettingValueString(0, 10, val)
1984
                val1d.set_validate_callback(my_validate)
1985
                vfodfreq = RadioSetting("vfo.d.freq", "VFO D frequency", val1d)
1986
                vfodfreq.set_apply_callback(apply_freq, _mem.vfo.d)
1987
                work.append(vfodfreq)
1988

    
1989
        if not self.MODEL == "GMRS-50X1":
1990
            val = min(_mem.vfo.a.shiftd, len(LIST_SHIFT) - 1)
1991
            rs = RadioSettingValueList(LIST_SHIFT, LIST_SHIFT[val])
1992
            vfoashiftd = RadioSetting("vfo.a.shiftd", "VFO A shift", rs)
1993
            work.append(vfoashiftd)
1994

    
1995
            val = min(_mem.vfo.b.shiftd, len(LIST_SHIFT) - 1)
1996
            rs = RadioSettingValueList(LIST_SHIFT, LIST_SHIFT[val])
1997
            vfobshiftd = RadioSetting("vfo.b.shiftd", "VFO B shift", rs)
1998
            work.append(vfobshiftd)
1999

    
2000
            if self.COLOR_LCD:
2001
                val = min(_mem.vfo.c.shiftd, len(LIST_SHIFT) - 1)
2002
                rs = RadioSettingValueList(LIST_SHIFT, LIST_SHIFT[val])
2003
                vfocshiftd = RadioSetting("vfo.c.shiftd", "VFO C shift", rs)
2004
                work.append(vfocshiftd)
2005

    
2006
                if not self.COLOR_LCD4:
2007
                    val = min(_mem.vfo.d.shiftd, len(LIST_SHIFT) - 1)
2008
                    rs = RadioSettingValueList(LIST_SHIFT, LIST_SHIFT[val])
2009
                    vfodshiftd = RadioSetting("vfo.d.shiftd",
2010
                                              "VFO D shift", rs)
2011
                    work.append(vfodshiftd)
2012

    
2013
        def convert_bytes_to_offset(bytes):
2014
            real_offset = 0
2015
            for byte in bytes:
2016
                real_offset = (real_offset * 10) + byte
2017
            return chirp_common.format_freq(real_offset * 1000)
2018

    
2019
        def apply_offset(setting, obj):
2020
            value = chirp_common.parse_freq(str(setting.value)) / 1000
2021
            for i in range(5, -1, -1):
2022
                obj.offset[i] = value % 10
2023
                value /= 10
2024

    
2025
        if not self.MODEL == "GMRS-50X1":
2026
            if self.COLOR_LCD:
2027
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2028
                                                _mem.vfo.a.offset))
2029
                vfoaoffset = RadioSetting("vfo.a.offset",
2030
                                          "VFO A offset (0.000-999.999)",
2031
                                          val1a)
2032
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
2033
                work.append(vfoaoffset)
2034

    
2035
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2036
                                                _mem.vfo.b.offset))
2037
                vfoboffset = RadioSetting("vfo.b.offset",
2038
                                          "VFO B offset (0.000-999.999)",
2039
                                          val1b)
2040
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
2041
                work.append(vfoboffset)
2042

    
2043
                val1c = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2044
                                                _mem.vfo.c.offset))
2045
                vfocoffset = RadioSetting("vfo.c.offset",
2046
                                          "VFO C offset (0.000-999.999)",
2047
                                          val1c)
2048
                vfocoffset.set_apply_callback(apply_offset, _mem.vfo.c)
2049
                work.append(vfocoffset)
2050

    
2051
                if not self.COLOR_LCD4:
2052
                    val1d = RadioSettingValueString(0, 10,
2053
                                                    convert_bytes_to_offset(
2054
                                                        _mem.vfo.d.offset))
2055
                    vfodoffset = RadioSetting("vfo.d.offset",
2056
                                              "VFO D offset (0.000-999.999)",
2057
                                              val1d)
2058
                    vfodoffset.set_apply_callback(apply_offset, _mem.vfo.d)
2059
                    work.append(vfodoffset)
2060
            else:
2061
                val1a = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2062
                                                _mem.vfo.a.offset))
2063
                vfoaoffset = RadioSetting("vfo.a.offset",
2064
                                          "VFO A offset (0.000-99.999)", val1a)
2065
                vfoaoffset.set_apply_callback(apply_offset, _mem.vfo.a)
2066
                work.append(vfoaoffset)
2067

    
2068
                val1b = RadioSettingValueString(0, 10, convert_bytes_to_offset(
2069
                                                _mem.vfo.b.offset))
2070
                vfoboffset = RadioSetting("vfo.b.offset",
2071
                                          "VFO B offset (0.000-99.999)", val1b)
2072
                vfoboffset.set_apply_callback(apply_offset, _mem.vfo.b)
2073
                work.append(vfoboffset)
2074

    
2075
        if not self.MODEL == "GMRS-50X1":
2076
            val = min(_mem.vfo.a.power, len(LIST_TXP) - 1)
2077
            rs = RadioSettingValueList(LIST_TXP, LIST_TXP[val])
2078
            vfoatxp = RadioSetting("vfo.a.power", "VFO A power", rs)
2079
            work.append(vfoatxp)
2080

    
2081
            val = min(_mem.vfo.b.power, len(LIST_TXP) - 1)
2082
            rs = RadioSettingValueList(LIST_TXP, LIST_TXP[val])
2083
            vfobtxp = RadioSetting("vfo.b.power", "VFO B power", rs)
2084
            work.append(vfobtxp)
2085

    
2086
            if self.COLOR_LCD:
2087
                val = min(_mem.vfo.c.power, len(LIST_TXP) - 1)
2088
                rs = RadioSettingValueList(LIST_TXP, LIST_TXP[val])
2089
                vfoctxp = RadioSetting("vfo.c.power", "VFO C power", rs)
2090
                work.append(vfoctxp)
2091

    
2092
                if not self.COLOR_LCD4:
2093
                    val = min(_mem.vfo.d.power, len(LIST_TXP) - 1)
2094
                    rs = RadioSettingValueList(LIST_TXP, LIST_TXP[val])
2095
                    vfodtxp = RadioSetting("vfo.d.power", "VFO D power", rs)
2096
                    work.append(vfodtxp)
2097

    
2098
        if not self.MODEL == "GMRS-50X1":
2099
            val = min(_mem.vfo.a.wide, len(LIST_WIDE) - 1)
2100
            rs = RadioSettingValueList(LIST_WIDE, LIST_WIDE[val])
2101
            vfoawide = RadioSetting("vfo.a.wide", "VFO A bandwidth", rs)
2102
            work.append(vfoawide)
2103

    
2104
            val = min(_mem.vfo.b.wide, len(LIST_WIDE) - 1)
2105
            rs = RadioSettingValueList(LIST_WIDE, LIST_WIDE[val])
2106
            vfobwide = RadioSetting("vfo.b.wide", "VFO B bandwidth", rs)
2107
            work.append(vfobwide)
2108

    
2109
            if self.COLOR_LCD:
2110
                val = min(_mem.vfo.c.wide, len(LIST_WIDE) - 1)
2111
                rs = RadioSettingValueList(LIST_WIDE, LIST_WIDE[val])
2112
                vfocwide = RadioSetting("vfo.c.wide", "VFO C bandwidth", rs)
2113
                work.append(vfocwide)
2114

    
2115
                if not self.COLOR_LCD4:
2116
                    val = min(_mem.vfo.d.wide, len(LIST_WIDE) - 1)
2117
                    rs = RadioSettingValueList(LIST_WIDE, LIST_WIDE[val])
2118
                    vfodwide = RadioSetting("vfo.d.wide",
2119
                                            "VFO D bandwidth", rs)
2120
                    work.append(vfodwide)
2121

    
2122
        val = min(_mem.vfo.a.step, len(LIST_STEP) - 1)
2123
        rs = RadioSettingValueList(LIST_STEP, LIST_STEP[val])
2124
        vfoastep = RadioSetting("vfo.a.step", "VFO A step", rs)
2125
        work.append(vfoastep)
2126

    
2127
        val = min(_mem.vfo.b.step, len(LIST_STEP) - 1)
2128
        rs = RadioSettingValueList(LIST_STEP, LIST_STEP[val])
2129
        vfobstep = RadioSetting("vfo.b.step", "VFO B step", rs)
2130
        work.append(vfobstep)
2131

    
2132
        if self.COLOR_LCD:
2133
            val = min(_mem.vfo.c.step, len(LIST_STEP) - 1)
2134
            rs = RadioSettingValueList(LIST_STEP, LIST_STEP[val])
2135
            vfocstep = RadioSetting("vfo.c.step", "VFO C step", rs)
2136
            work.append(vfocstep)
2137

    
2138
            if not self.COLOR_LCD4:
2139
                val = min(_mem.vfo.d.step, len(LIST_STEP) - 1)
2140
                rs = RadioSettingValueList(LIST_STEP, LIST_STEP[val])
2141
                vfodstep = RadioSetting("vfo.d.step", "VFO D step", rs)
2142
                work.append(vfodstep)
2143

    
2144
        val = min(_mem.vfo.a.optsig, len(OPTSIG_LIST) - 1)
2145
        rs = RadioSettingValueList(OPTSIG_LIST, OPTSIG_LIST[val])
2146
        vfoaoptsig = RadioSetting("vfo.a.optsig", "VFO A optional signal", rs)
2147
        work.append(vfoaoptsig)
2148

    
2149
        val = min(_mem.vfo.b.optsig, len(OPTSIG_LIST) - 1)
2150
        rs = RadioSettingValueList(OPTSIG_LIST, OPTSIG_LIST[val])
2151
        vfoboptsig = RadioSetting("vfo.b.optsig", "VFO B optional signal", rs)
2152
        work.append(vfoboptsig)
2153

    
2154
        if self.COLOR_LCD:
2155
            val = min(_mem.vfo.c.optsig, len(OPTSIG_LIST) - 1)
2156
            rs = RadioSettingValueList(OPTSIG_LIST, OPTSIG_LIST[val])
2157
            vfocoptsig = RadioSetting("vfo.c.optsig",
2158
                                      "VFO C optional signal", rs)
2159
            work.append(vfocoptsig)
2160

    
2161
            if not self.COLOR_LCD4:
2162
                val = min(_mem.vfo.d.optsig, len(OPTSIG_LIST) - 1)
2163
                rs = RadioSettingValueList(OPTSIG_LIST, OPTSIG_LIST[val])
2164
                vfodoptsig = RadioSetting("vfo.d.optsig",
2165
                                          "VFO D optional signal", rs)
2166
                work.append(vfodoptsig)
2167

    
2168
        val = min(_mem.vfo.a.spmute, len(SPMUTE_LIST) - 1)
2169
        rs = RadioSettingValueList(SPMUTE_LIST, SPMUTE_LIST[val])
2170
        vfoaspmute = RadioSetting("vfo.a.spmute", "VFO A speaker mute", rs)
2171
        work.append(vfoaspmute)
2172

    
2173
        val = min(_mem.vfo.b.spmute, len(SPMUTE_LIST) - 1)
2174
        rs = RadioSettingValueList(SPMUTE_LIST, SPMUTE_LIST[val])
2175
        vfobspmute = RadioSetting("vfo.b.spmute", "VFO B speaker mute", rs)
2176
        work.append(vfobspmute)
2177

    
2178
        if self.COLOR_LCD:
2179
            val = min(_mem.vfo.c.spmute, len(SPMUTE_LIST) - 1)
2180
            rs = RadioSettingValueList(SPMUTE_LIST, SPMUTE_LIST[val])
2181
            vfocspmute = RadioSetting("vfo.c.spmute", "VFO C speaker mute", rs)
2182
            work.append(vfocspmute)
2183

    
2184
            if not self.COLOR_LCD4:
2185
                val = min(_mem.vfo.d.spmute, len(SPMUTE_LIST) - 1)
2186
                rs = RadioSettingValueList(SPMUTE_LIST, SPMUTE_LIST[val])
2187
                vfodspmute = RadioSetting("vfo.d.spmute",
2188
                                          "VFO D speaker mute", rs)
2189
                work.append(vfodspmute)
2190

    
2191
        if not self.COLOR_LCD or \
2192
                (self.COLOR_LCD and not self.VENDOR == "BTECH"):
2193
            rs = RadioSettingValueBoolean(_mem.vfo.a.scramble)
2194
            vfoascr = RadioSetting("vfo.a.scramble", "VFO A scramble", rs)
2195
            work.append(vfoascr)
2196

    
2197
            rs = RadioSettingValueBoolean(_mem.vfo.b.scramble)
2198
            vfobscr = RadioSetting("vfo.b.scramble", "VFO B scramble", rs)
2199
            work.append(vfobscr)
2200

    
2201
        if self.COLOR_LCD and not self.VENDOR == "BTECH":
2202
            rs = RadioSettingValueBoolean(_mem.vfo.c.scramble)
2203
            vfocscr = RadioSetting("vfo.c.scramble", "VFO C scramble", rs)
2204
            work.append(vfocscr)
2205

    
2206
            rs = RadioSettingValueBoolean(_mem.vfo.d.scramble)
2207
            vfodscr = RadioSetting("vfo.d.scramble", "VFO D scramble", rs)
2208
            work.append(vfodscr)
2209

    
2210
        if not self.MODEL == "GMRS-50X1":
2211
            val = min(_mem.vfo.a.scode, len(PTTIDCODE_LIST) - 1)
2212
            rs = RadioSettingValueList(PTTIDCODE_LIST, PTTIDCODE_LIST[val])
2213
            vfoascode = RadioSetting("vfo.a.scode", "VFO A PTT-ID", rs)
2214
            work.append(vfoascode)
2215

    
2216
            val = min(_mem.vfo.b.scode, len(PTTIDCODE_LIST) - 1)
2217
            rs = RadioSettingValueList(PTTIDCODE_LIST, PTTIDCODE_LIST[val])
2218
            vfobscode = RadioSetting("vfo.b.scode", "VFO B PTT-ID", rs)
2219
            work.append(vfobscode)
2220

    
2221
            if self.COLOR_LCD:
2222
                val = min(_mem.vfo.c.scode, len(PTTIDCODE_LIST) - 1)
2223
                rs = RadioSettingValueList(PTTIDCODE_LIST, PTTIDCODE_LIST[val])
2224
                vfocscode = RadioSetting("vfo.c.scode", "VFO C PTT-ID", rs)
2225
                work.append(vfocscode)
2226

    
2227
                if not self.COLOR_LCD4:
2228
                    val = min(_mem.vfo.d.scode, len(PTTIDCODE_LIST) - 1)
2229
                    rs = RadioSettingValueList(PTTIDCODE_LIST,
2230
                                               PTTIDCODE_LIST[val])
2231
                    vfodscode = RadioSetting("vfo.d.scode", "VFO D PTT-ID", rs)
2232
                    work.append(vfodscode)
2233

    
2234
        if not self.MODEL == "GMRS-50X1":
2235
            val = min(_mem.settings.pttid, len(PTTID_LIST) - 1)
2236
            rs = RadioSettingValueList(PTTID_LIST, PTTID_LIST[val])
2237
            pttid = RadioSetting("settings.pttid", "PTT ID", rs)
2238
            work.append(pttid)
2239

    
2240
        if not self.COLOR_LCD:
2241
            # FM presets
2242
            fm_presets = RadioSettingGroup("fm_presets", "FM Presets")
2243
            top.append(fm_presets)
2244

    
2245
            def fm_validate(value):
2246
                if value == 0:
2247
                    return chirp_common.format_freq(value)
2248
                if not (87.5 <= value and value <= 108.0):  # 87.5-108 MHz
2249
                    msg = ("FM-Preset-Frequency: " +
2250
                           "Must be between 87.5 and 108 MHz")
2251
                    raise InvalidValueError(msg)
2252
                return value
2253

    
2254
            def apply_fm_preset_name(setting, obj):
2255
                valstring = str(setting.value)
2256
                for i in range(0, 6):
2257
                    if valstring[i] in VALID_CHARS:
2258
                        obj[i] = valstring[i]
2259
                    else:
2260
                        obj[i] = '0xff'
2261

    
2262
            def apply_fm_freq(setting, obj):
2263
                value = chirp_common.parse_freq(str(setting.value)) / 10
2264
                for i in range(7, -1, -1):
2265
                    obj.freq[i] = value % 10
2266
                    value /= 10
2267

    
2268
            _presets = self._memobj.fm_radio_preset
2269
            i = 1
2270
            for preset in _presets:
2271
                line = RadioSetting("fm_presets_" + str(i),
2272
                                    "Station name " + str(i),
2273
                                    RadioSettingValueString(0, 6, _filter(
2274
                                        preset.broadcast_station_name)))
2275
                line.set_apply_callback(apply_fm_preset_name,
2276
                                        preset.broadcast_station_name)
2277

    
2278
                val = RadioSettingValueFloat(0, 108,
2279
                                             convert_bytes_to_freq(
2280
                                                 preset.freq))
2281
                fmfreq = RadioSetting("fm_presets_" + str(i) + "_freq",
2282
                                      "Frequency " + str(i), val)
2283
                val.set_validate_callback(fm_validate)
2284
                fmfreq.set_apply_callback(apply_fm_freq, preset)
2285
                fm_presets.append(line)
2286
                fm_presets.append(fmfreq)
2287

    
2288
                i = i + 1
2289

    
2290
        # DTMF-Setting
2291
        dtmf_enc_settings = RadioSettingGroup("dtmf_enc_settings",
2292
                                              "DTMF Encoding Settings")
2293
        dtmf_dec_settings = RadioSettingGroup("dtmf_dec_settings",
2294
                                              "DTMF Decoding Settings")
2295
        top.append(dtmf_enc_settings)
2296
        top.append(dtmf_dec_settings)
2297
        txdisable = RadioSetting("dtmf_settings.txdisable",
2298
                                 "TX-Disable",
2299
                                 RadioSettingValueBoolean(
2300
                                     _mem.dtmf_settings.txdisable))
2301
        dtmf_enc_settings.append(txdisable)
2302

    
2303
        rxdisable = RadioSetting("dtmf_settings.rxdisable",
2304
                                 "RX-Disable",
2305
                                 RadioSettingValueBoolean(
2306
                                     _mem.dtmf_settings.rxdisable))
2307
        dtmf_enc_settings.append(rxdisable)
2308

    
2309
        if _mem.dtmf_settings.dtmfspeed_on > 0x0F:
2310
            val = 0x03
2311
        else:
2312
            val = _mem.dtmf_settings.dtmfspeed_on
2313
        dtmfspeed_on = RadioSetting(
2314
            "dtmf_settings.dtmfspeed_on",
2315
            "DTMF Speed (On Time)",
2316
            RadioSettingValueList(LIST_DTMF_SPEED,
2317
                                  LIST_DTMF_SPEED[
2318
                                      val]))
2319
        dtmf_enc_settings.append(dtmfspeed_on)
2320

    
2321
        if _mem.dtmf_settings.dtmfspeed_off > 0x0F:
2322
            val = 0x03
2323
        else:
2324
            val = _mem.dtmf_settings.dtmfspeed_off
2325
        dtmfspeed_off = RadioSetting(
2326
            "dtmf_settings.dtmfspeed_off",
2327
            "DTMF Speed (Off Time)",
2328
            RadioSettingValueList(LIST_DTMF_SPEED,
2329
                                  LIST_DTMF_SPEED[
2330
                                      val]))
2331
        dtmf_enc_settings.append(dtmfspeed_off)
2332

    
2333
        def memory2string(dmtf_mem):
2334
            dtmf_string = ""
2335
            for digit in dmtf_mem:
2336
                if digit != 255:
2337
                    index = LIST_DTMF_VALUES.index(digit)
2338
                    dtmf_string = dtmf_string + LIST_DTMF_DIGITS[index]
2339
            return dtmf_string
2340

    
2341
        def apply_dmtf_frame(setting, obj):
2342
            LOG.debug("Setting DTMF-Code: " + str(setting.value))
2343
            val_string = str(setting.value)
2344
            for i in range(0, 16):
2345
                obj[i] = 255
2346
            i = 0
2347
            for current_char in val_string:
2348
                current_char = current_char.upper()
2349
                index = LIST_DTMF_DIGITS.index(current_char)
2350
                obj[i] = LIST_DTMF_VALUES[index]
2351
                i = i + 1
2352

    
2353
        codes = self._memobj.dtmf_codes
2354
        i = 1
2355
        for dtmfcode in codes:
2356
            val = RadioSettingValueString(0, 16, memory2string(
2357
                                              dtmfcode.code),
2358
                                          False, CHARSET_DTMF_DIGITS)
2359
            line = RadioSetting("dtmf_code_" + str(i) + "_code",
2360
                                "DMTF Code " + str(i), val)
2361
            line.set_apply_callback(apply_dmtf_frame, dtmfcode.code)
2362
            dtmf_enc_settings.append(line)
2363
            i = i + 1
2364

    
2365
        line = RadioSetting("dtmf_settings.mastervice",
2366
                            "Master and Vice ID",
2367
                            RadioSettingValueBoolean(
2368
                                _mem.dtmf_settings.mastervice))
2369
        dtmf_dec_settings.append(line)
2370

    
2371
        val = RadioSettingValueString(0, 16, memory2string(
2372
                                          _mem.dtmf_settings.masterid),
2373
                                      False, CHARSET_DTMF_DIGITS)
2374
        line = RadioSetting("dtmf_settings.masterid",
2375
                            "Master Control ID ", val)
2376
        line.set_apply_callback(apply_dmtf_frame,
2377
                                _mem.dtmf_settings.masterid)
2378
        dtmf_dec_settings.append(line)
2379

    
2380
        line = RadioSetting("dtmf_settings.minspection",
2381
                            "Master Inspection",
2382
                            RadioSettingValueBoolean(
2383
                                _mem.dtmf_settings.minspection))
2384
        dtmf_dec_settings.append(line)
2385

    
2386
        line = RadioSetting("dtmf_settings.mmonitor",
2387
                            "Master Monitor",
2388
                            RadioSettingValueBoolean(
2389
                                _mem.dtmf_settings.mmonitor))
2390
        dtmf_dec_settings.append(line)
2391

    
2392
        line = RadioSetting("dtmf_settings.mstun",
2393
                            "Master Stun",
2394
                            RadioSettingValueBoolean(
2395
                                _mem.dtmf_settings.mstun))
2396
        dtmf_dec_settings.append(line)
2397

    
2398
        line = RadioSetting("dtmf_settings.mkill",
2399
                            "Master Kill",
2400
                            RadioSettingValueBoolean(
2401
                                _mem.dtmf_settings.mkill))
2402
        dtmf_dec_settings.append(line)
2403

    
2404
        line = RadioSetting("dtmf_settings.mrevive",
2405
                            "Master Revive",
2406
                            RadioSettingValueBoolean(
2407
                                _mem.dtmf_settings.mrevive))
2408
        dtmf_dec_settings.append(line)
2409

    
2410
        val = RadioSettingValueString(0, 16, memory2string(
2411
                                          _mem.dtmf_settings.viceid),
2412
                                      False, CHARSET_DTMF_DIGITS)
2413
        line = RadioSetting("dtmf_settings.viceid",
2414
                            "Vice Control ID ", val)
2415
        line.set_apply_callback(apply_dmtf_frame,
2416
                                _mem.dtmf_settings.viceid)
2417
        dtmf_dec_settings.append(line)
2418

    
2419
        line = RadioSetting("dtmf_settings.vinspection",
2420
                            "Vice Inspection",
2421
                            RadioSettingValueBoolean(
2422
                                _mem.dtmf_settings.vinspection))
2423
        dtmf_dec_settings.append(line)
2424

    
2425
        line = RadioSetting("dtmf_settings.vmonitor",
2426
                            "Vice Monitor",
2427
                            RadioSettingValueBoolean(
2428
                                _mem.dtmf_settings.vmonitor))
2429
        dtmf_dec_settings.append(line)
2430

    
2431
        line = RadioSetting("dtmf_settings.vstun",
2432
                            "Vice Stun",
2433
                            RadioSettingValueBoolean(
2434
                                _mem.dtmf_settings.vstun))
2435
        dtmf_dec_settings.append(line)
2436

    
2437
        line = RadioSetting("dtmf_settings.vkill",
2438
                            "Vice Kill",
2439
                            RadioSettingValueBoolean(
2440
                                _mem.dtmf_settings.vkill))
2441
        dtmf_dec_settings.append(line)
2442

    
2443
        line = RadioSetting("dtmf_settings.vrevive",
2444
                            "Vice Revive",
2445
                            RadioSettingValueBoolean(
2446
                                _mem.dtmf_settings.vrevive))
2447
        dtmf_dec_settings.append(line)
2448

    
2449
        val = RadioSettingValueString(0, 16, memory2string(
2450
                                          _mem.dtmf_settings.inspection),
2451
                                      False, CHARSET_DTMF_DIGITS)
2452
        line = RadioSetting("dtmf_settings.inspection",
2453
                            "Inspection", val)
2454
        line.set_apply_callback(apply_dmtf_frame,
2455
                                _mem.dtmf_settings.inspection)
2456
        dtmf_dec_settings.append(line)
2457

    
2458
        val = RadioSettingValueString(0, 16, memory2string(
2459
                                          _mem.dtmf_settings.alarmcode),
2460
                                      False, CHARSET_DTMF_DIGITS)
2461
        line = RadioSetting("dtmf_settings.alarmcode",
2462
                            "Alarm", val)
2463
        line.set_apply_callback(apply_dmtf_frame,
2464
                                _mem.dtmf_settings.alarmcode)
2465
        dtmf_dec_settings.append(line)
2466

    
2467
        val = RadioSettingValueString(0, 16, memory2string(
2468
                                          _mem.dtmf_settings.kill),
2469
                                      False, CHARSET_DTMF_DIGITS)
2470
        line = RadioSetting("dtmf_settings.kill",
2471
                            "Kill", val)
2472
        line.set_apply_callback(apply_dmtf_frame,
2473
                                _mem.dtmf_settings.kill)
2474
        dtmf_dec_settings.append(line)
2475

    
2476
        val = RadioSettingValueString(0, 16, memory2string(
2477
                                          _mem.dtmf_settings.monitor),
2478
                                      False, CHARSET_DTMF_DIGITS)
2479
        line = RadioSetting("dtmf_settings.monitor",
2480
                            "Monitor", val)
2481
        line.set_apply_callback(apply_dmtf_frame,
2482
                                _mem.dtmf_settings.monitor)
2483
        dtmf_dec_settings.append(line)
2484

    
2485
        val = RadioSettingValueString(0, 16, memory2string(
2486
                                          _mem.dtmf_settings.stun),
2487
                                      False, CHARSET_DTMF_DIGITS)
2488
        line = RadioSetting("dtmf_settings.stun",
2489
                            "Stun", val)
2490
        line.set_apply_callback(apply_dmtf_frame,
2491
                                _mem.dtmf_settings.stun)
2492
        dtmf_dec_settings.append(line)
2493

    
2494
        val = RadioSettingValueString(0, 16, memory2string(
2495
                                          _mem.dtmf_settings.revive),
2496
                                      False, CHARSET_DTMF_DIGITS)
2497
        line = RadioSetting("dtmf_settings.revive",
2498
                            "Revive", val)
2499
        line.set_apply_callback(apply_dmtf_frame,
2500
                                _mem.dtmf_settings.revive)
2501
        dtmf_dec_settings.append(line)
2502

    
2503
        def apply_dmtf_listvalue(setting, obj):
2504
            LOG.debug("Setting value: " + str(setting.value) + " from list")
2505
            val = str(setting.value)
2506
            index = LIST_DTMF_SPECIAL_DIGITS.index(val)
2507
            val = LIST_DTMF_SPECIAL_VALUES[index]
2508
            obj.set_value(val)
2509

    
2510
        if _mem.dtmf_settings.groupcode not in LIST_DTMF_SPECIAL_VALUES:
2511
            val = 0x0B
2512
        else:
2513
            val = _mem.dtmf_settings.groupcode
2514
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2515
        line = RadioSetting(
2516
            "dtmf_settings.groupcode",
2517
            "Group Code",
2518
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2519
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2520
        line.set_apply_callback(apply_dmtf_listvalue,
2521
                                _mem.dtmf_settings.groupcode)
2522
        dtmf_dec_settings.append(line)
2523

    
2524
        if _mem.dtmf_settings.spacecode not in LIST_DTMF_SPECIAL_VALUES:
2525
            val = 0x0C
2526
        else:
2527
            val = _mem.dtmf_settings.spacecode
2528
        idx = LIST_DTMF_SPECIAL_VALUES.index(val)
2529
        line = RadioSetting(
2530
            "dtmf_settings.spacecode",
2531
            "Space Code",
2532
            RadioSettingValueList(LIST_DTMF_SPECIAL_DIGITS,
2533
                                  LIST_DTMF_SPECIAL_DIGITS[idx]))
2534
        line.set_apply_callback(apply_dmtf_listvalue,
2535
                                _mem.dtmf_settings.spacecode)
2536
        dtmf_dec_settings.append(line)
2537

    
2538
        if self.COLOR_LCD:
2539
            if _mem.dtmf_settings.resettime > 0x63:
2540
                val = 0x4F
2541
            else:
2542
                val = _mem.dtmf_settings.resettime
2543
            line = RadioSetting(
2544
                "dtmf_settings.resettime",
2545
                "Reset time",
2546
                RadioSettingValueList(LIST_5TONE_RESET_COLOR,
2547
                                      LIST_5TONE_RESET_COLOR[
2548
                                          val]))
2549
            dtmf_dec_settings.append(line)
2550
        else:
2551
            line = RadioSetting(
2552
                "dtmf_settings.resettime",
2553
                "Reset time",
2554
                RadioSettingValueList(LIST_5TONE_RESET,
2555
                                      LIST_5TONE_RESET[
2556
                                          _mem.dtmf_settings.resettime]))
2557
            dtmf_dec_settings.append(line)
2558

    
2559
        if _mem.dtmf_settings.delayproctime > 0x27:
2560
            val = 0x04
2561
        else:
2562
            val = _mem.dtmf_settings.delayproctime
2563
        line = RadioSetting(
2564
            "dtmf_settings.delayproctime",
2565
            "Delay processing time",
2566
            RadioSettingValueList(LIST_DTMF_DELAY,
2567
                                  LIST_DTMF_DELAY[
2568
                                      val]))
2569
        dtmf_dec_settings.append(line)
2570

    
2571
        # 5 Tone Settings
2572
        stds_5tone = RadioSettingGroup("stds_5tone", "Standards")
2573
        codes_5tone = RadioSettingGroup("codes_5tone", "Codes")
2574

    
2575
        group_5tone = RadioSettingGroup("group_5tone", "5 Tone Settings")
2576
        group_5tone.append(stds_5tone)
2577
        group_5tone.append(codes_5tone)
2578

    
2579
        top.append(group_5tone)
2580

    
2581
        def apply_list_value(setting, obj):
2582
            options = setting.value.get_options()
2583
            obj.set_value(options.index(str(setting.value)))
2584

    
2585
        _5tone_standards = self._memobj._5tone_std_settings
2586
        i = 0
2587
        for standard in _5tone_standards:
2588
            std_5tone = RadioSettingGroup("std_5tone_" + str(i),
2589
                                          LIST_5TONE_STANDARDS[i])
2590
            stds_5tone.append(std_5tone)
2591

    
2592
            period = standard.period
2593
            if period == 255:
2594
                LOG.debug("Period for " + LIST_5TONE_STANDARDS[i] +
2595
                          " is not yet configured. Setting to 70ms.")
2596
                period = 5
2597

    
2598
            if period <= len(LIST_5TONE_STANDARD_PERIODS):
2599
                line = RadioSetting(
2600
                    "_5tone_std_settings_" + str(i) + "_period",
2601
                    "Period (ms)", RadioSettingValueList
2602
                    (LIST_5TONE_STANDARD_PERIODS,
2603
                     LIST_5TONE_STANDARD_PERIODS[period]))
2604
                line.set_apply_callback(apply_list_value, standard.period)
2605
                std_5tone.append(line)
2606
            else:
2607
                LOG.debug("Invalid value for 5tone period! Disabling.")
2608

    
2609
            group_tone = standard.group_tone
2610
            if group_tone == 255:
2611
                LOG.debug("Group-Tone for " + LIST_5TONE_STANDARDS[i] +
2612
                          " is not yet configured. Setting to A.")
2613
                group_tone = 10
2614

    
2615
            if group_tone <= len(LIST_5TONE_DIGITS):
2616
                line = RadioSetting(
2617
                    "_5tone_std_settings_" + str(i) + "_grouptone",
2618
                    "Group Tone",
2619
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2620
                                          LIST_5TONE_DIGITS[
2621
                                              group_tone]))
2622
                line.set_apply_callback(apply_list_value,
2623
                                        standard.group_tone)
2624
                std_5tone.append(line)
2625
            else:
2626
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2627

    
2628
            repeat_tone = standard.repeat_tone
2629
            if repeat_tone == 255:
2630
                LOG.debug("Repeat-Tone for " + LIST_5TONE_STANDARDS[i] +
2631
                          " is not yet configured. Setting to E.")
2632
                repeat_tone = 14
2633

    
2634
            if repeat_tone <= len(LIST_5TONE_DIGITS):
2635
                line = RadioSetting(
2636
                    "_5tone_std_settings_" + str(i) + "_repttone",
2637
                    "Repeat Tone",
2638
                    RadioSettingValueList(LIST_5TONE_DIGITS,
2639
                                          LIST_5TONE_DIGITS[
2640
                                              repeat_tone]))
2641
                line.set_apply_callback(apply_list_value,
2642
                                        standard.repeat_tone)
2643
                std_5tone.append(line)
2644
            else:
2645
                LOG.debug("Invalid value for 5tone digit! Disabling.")
2646
            i = i + 1
2647

    
2648
        def my_apply_5tonestdlist_value(setting, obj):
2649
            if LIST_5TONE_STANDARDS.index(str(setting.value)) == 15:
2650
                obj.set_value(0xFF)
2651
            else:
2652
                obj.set_value(LIST_5TONE_STANDARDS.
2653
                              index(str(setting.value)))
2654

    
2655
        def apply_5tone_frame(setting, obj):
2656
            LOG.debug("Setting 5 Tone: " + str(setting.value))
2657
            valstring = str(setting.value)
2658
            if len(valstring) == 0:
2659
                for i in range(0, 5):
2660
                    obj[i] = 255
2661
            else:
2662
                validFrame = True
2663
                for i in range(0, 5):
2664
                    currentChar = valstring[i].upper()
2665
                    if currentChar in LIST_5TONE_DIGITS:
2666
                        obj[i] = LIST_5TONE_DIGITS.index(currentChar)
2667
                    else:
2668
                        validFrame = False
2669
                        LOG.debug("invalid char: " + str(currentChar))
2670
                if not validFrame:
2671
                    LOG.debug("setting whole frame to FF")
2672
                    for i in range(0, 5):
2673
                        obj[i] = 255
2674

    
2675
        def validate_5tone_frame(value):
2676
            if (len(str(value)) != 5) and (len(str(value)) != 0):
2677
                msg = ("5 Tone must have 5 digits or 0 digits")
2678
                raise InvalidValueError(msg)
2679
            for digit in str(value):
2680
                if digit.upper() not in LIST_5TONE_DIGITS:
2681
                    msg = (str(digit) + " is not a valid digit for 5tones")
2682
                    raise InvalidValueError(msg)
2683
            return value
2684

    
2685
        def frame2string(frame):
2686
            frameString = ""
2687
            for digit in frame:
2688
                if digit != 255:
2689
                    frameString = frameString + LIST_5TONE_DIGITS[digit]
2690
            return frameString
2691

    
2692
        _5tone_codes = self._memobj._5tone_codes
2693
        i = 1
2694
        for code in _5tone_codes:
2695
            code_5tone = RadioSettingGroup("code_5tone_" + str(i),
2696
                                           "5 Tone code " + str(i))
2697
            codes_5tone.append(code_5tone)
2698
            if (code.standard == 255):
2699
                currentVal = 15
2700
            else:
2701
                currentVal = code.standard
2702
            line = RadioSetting("_5tone_code_" + str(i) + "_std",
2703
                                " Standard",
2704
                                RadioSettingValueList(LIST_5TONE_STANDARDS,
2705
                                                      LIST_5TONE_STANDARDS[
2706
                                                          currentVal]))
2707
            line.set_apply_callback(my_apply_5tonestdlist_value,
2708
                                    code.standard)
2709
            code_5tone.append(line)
2710

    
2711
            val = RadioSettingValueString(0, 6,
2712
                                          frame2string(code.frame1), False)
2713
            line = RadioSetting("_5tone_code_" + str(i) + "_frame1",
2714
                                " Frame 1", val)
2715
            val.set_validate_callback(validate_5tone_frame)
2716
            line.set_apply_callback(apply_5tone_frame, code.frame1)
2717
            code_5tone.append(line)
2718

    
2719
            val = RadioSettingValueString(0, 6,
2720
                                          frame2string(code.frame2), False)
2721
            line = RadioSetting("_5tone_code_" + str(i) + "_frame2",
2722
                                " Frame 2", val)
2723
            val.set_validate_callback(validate_5tone_frame)
2724
            line.set_apply_callback(apply_5tone_frame, code.frame2)
2725
            code_5tone.append(line)
2726

    
2727
            val = RadioSettingValueString(0, 6,
2728
                                          frame2string(code.frame3), False)
2729
            line = RadioSetting("_5tone_code_" + str(i) + "_frame3",
2730
                                " Frame 3", val)
2731
            val.set_validate_callback(validate_5tone_frame)
2732
            line.set_apply_callback(apply_5tone_frame, code.frame3)
2733
            code_5tone.append(line)
2734
            i = i + 1
2735

    
2736
        _5_tone_decode1 = RadioSetting(
2737
            "_5tone_settings._5tone_decode_call_frame1",
2738
            "5 Tone decode call Frame 1",
2739
            RadioSettingValueBoolean(
2740
                _mem._5tone_settings._5tone_decode_call_frame1))
2741
        group_5tone.append(_5_tone_decode1)
2742

    
2743
        _5_tone_decode2 = RadioSetting(
2744
            "_5tone_settings._5tone_decode_call_frame2",
2745
            "5 Tone decode call Frame 2",
2746
            RadioSettingValueBoolean(
2747
                _mem._5tone_settings._5tone_decode_call_frame2))
2748
        group_5tone.append(_5_tone_decode2)
2749

    
2750
        _5_tone_decode3 = RadioSetting(
2751
            "_5tone_settings._5tone_decode_call_frame3",
2752
            "5 Tone decode call Frame 3",
2753
            RadioSettingValueBoolean(
2754
                _mem._5tone_settings._5tone_decode_call_frame3))
2755
        group_5tone.append(_5_tone_decode3)
2756

    
2757
        _5_tone_decode_disp1 = RadioSetting(
2758
            "_5tone_settings._5tone_decode_disp_frame1",
2759
            "5 Tone decode disp Frame 1",
2760
            RadioSettingValueBoolean(
2761
                _mem._5tone_settings._5tone_decode_disp_frame1))
2762
        group_5tone.append(_5_tone_decode_disp1)
2763

    
2764
        _5_tone_decode_disp2 = RadioSetting(
2765
            "_5tone_settings._5tone_decode_disp_frame2",
2766
            "5 Tone decode disp Frame 2",
2767
            RadioSettingValueBoolean(
2768
                _mem._5tone_settings._5tone_decode_disp_frame2))
2769
        group_5tone.append(_5_tone_decode_disp2)
2770

    
2771
        _5_tone_decode_disp3 = RadioSetting(
2772
            "_5tone_settings._5tone_decode_disp_frame3",
2773
            "5 Tone decode disp Frame 3",
2774
            RadioSettingValueBoolean(
2775
                _mem._5tone_settings._5tone_decode_disp_frame3))
2776
        group_5tone.append(_5_tone_decode_disp3)
2777

    
2778
        decode_standard = _mem._5tone_settings.decode_standard
2779
        if decode_standard == 255:
2780
            decode_standard = 0
2781
        if decode_standard <= len(LIST_5TONE_STANDARDS_without_none):
2782
            line = RadioSetting("_5tone_settings.decode_standard",
2783
                                "5 Tone-decode Standard",
2784
                                RadioSettingValueList(
2785
                                    LIST_5TONE_STANDARDS_without_none,
2786
                                    LIST_5TONE_STANDARDS_without_none[
2787
                                        decode_standard]))
2788
            group_5tone.append(line)
2789
        else:
2790
            LOG.debug("Invalid decode std...")
2791

    
2792
        _5tone_delay1 = _mem._5tone_settings._5tone_delay1
2793
        if _5tone_delay1 == 255:
2794
            _5tone_delay1 = 20
2795

    
2796
        if _5tone_delay1 <= len(LIST_5TONE_DELAY):
2797
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2798
                                         LIST_5TONE_DELAY[
2799
                                             _5tone_delay1])
2800
            line = RadioSetting("_5tone_settings._5tone_delay1",
2801
                                "5 Tone Delay Frame 1", list)
2802
            group_5tone.append(line)
2803
        else:
2804
            LOG.debug(
2805
                "Invalid value for 5tone delay (frame1) ! Disabling.")
2806

    
2807
        _5tone_delay2 = _mem._5tone_settings._5tone_delay2
2808
        if _5tone_delay2 == 255:
2809
            _5tone_delay2 = 20
2810
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2811

    
2812
        if _5tone_delay2 <= len(LIST_5TONE_DELAY):
2813
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2814
                                         LIST_5TONE_DELAY[
2815
                                             _5tone_delay2])
2816
            line = RadioSetting("_5tone_settings._5tone_delay2",
2817
                                "5 Tone Delay Frame 2", list)
2818
            group_5tone.append(line)
2819
        else:
2820
            LOG.debug("Invalid value for 5tone delay (frame2)! Disabling.")
2821

    
2822
        _5tone_delay3 = _mem._5tone_settings._5tone_delay3
2823
        if _5tone_delay3 == 255:
2824
            _5tone_delay3 = 20
2825
            LOG.debug("5 Tone delay unconfigured! Resetting to 200ms.")
2826

    
2827
        if _5tone_delay3 <= len(LIST_5TONE_DELAY):
2828
            list = RadioSettingValueList(LIST_5TONE_DELAY,
2829
                                         LIST_5TONE_DELAY[
2830
                                             _5tone_delay3])
2831
            line = RadioSetting("_5tone_settings._5tone_delay3",
2832
                                "5 Tone Delay Frame 3", list)
2833
            group_5tone.append(line)
2834
        else:
2835
            LOG.debug("Invalid value for 5tone delay (frame3)! Disabling.")
2836

    
2837
        ext_length = _mem._5tone_settings._5tone_first_digit_ext_length
2838
        if ext_length == 255:
2839
            ext_length = 0
2840
            LOG.debug("1st Tone ext length unconfigured! Resetting to 0")
2841

    
2842
        if ext_length <= len(LIST_5TONE_DELAY):
2843
            list = RadioSettingValueList(
2844
                LIST_5TONE_DELAY,
2845
                LIST_5TONE_DELAY[
2846
                    ext_length])
2847
            line = RadioSetting(
2848
                "_5tone_settings._5tone_first_digit_ext_length",
2849
                "First digit extend length", list)
2850
            group_5tone.append(line)
2851
        else:
2852
            LOG.debug("Invalid value for 5tone ext length! Disabling.")
2853

    
2854
        decode_reset_time = _mem._5tone_settings.decode_reset_time
2855
        if decode_reset_time == 255:
2856
            decode_reset_time = 59
2857
            LOG.debug("Decode reset time unconfigured. resetting.")
2858
        if decode_reset_time <= len(LIST_5TONE_RESET):
2859
            list = RadioSettingValueList(
2860
                LIST_5TONE_RESET,
2861
                LIST_5TONE_RESET[
2862
                    decode_reset_time])
2863
            line = RadioSetting("_5tone_settings.decode_reset_time",
2864
                                "Decode reset time", list)
2865
            group_5tone.append(line)
2866
        else:
2867
            LOG.debug("Invalid value decode reset time! Disabling.")
2868

    
2869
        # 2 Tone
2870
        encode_2tone = RadioSettingGroup("encode_2tone", "2 Tone Encode")
2871
        decode_2tone = RadioSettingGroup("decode_2tone", "2 Code Decode")
2872

    
2873
        top.append(encode_2tone)
2874
        top.append(decode_2tone)
2875

    
2876
        duration_1st_tone = self._memobj._2tone.duration_1st_tone
2877
        if duration_1st_tone == 255:
2878
            LOG.debug("Duration of first 2 Tone digit is not yet " +
2879
                      "configured. Setting to 600ms")
2880
            duration_1st_tone = 60
2881

    
2882
        if duration_1st_tone <= len(LIST_5TONE_DELAY):
2883
            val = RadioSettingValueList(LIST_5TONE_DELAY,
2884
                                        LIST_5TONE_DELAY[
2885
                                            duration_1st_tone])
2886
            line = RadioSetting("_2tone.duration_1st_tone",
2887
                                "Duration 1st Tone", val)
2888
            encode_2tone.append(line)
2889

    
2890
        duration_2nd_tone = self._memobj._2tone.duration_2nd_tone
2891
        if duration_2nd_tone == 255:
2892
            LOG.debug("Duration of second 2 Tone digit is not yet " +
2893
                      "configured. Setting to 600ms")
2894
            duration_2nd_tone = 60
2895

    
2896
        if duration_2nd_tone <= len(LIST_5TONE_DELAY):
2897
            val = RadioSettingValueList(LIST_5TONE_DELAY,
2898
                                        LIST_5TONE_DELAY[
2899
                                            duration_2nd_tone])
2900
            line = RadioSetting("_2tone.duration_2nd_tone",
2901
                                "Duration 2nd Tone", val)
2902
            encode_2tone.append(line)
2903

    
2904
        duration_gap = self._memobj._2tone.duration_gap
2905
        if duration_gap == 255:
2906
            LOG.debug("Duration of gap is not yet " +
2907
                      "configured. Setting to 300ms")
2908
            duration_gap = 30
2909

    
2910
        if duration_gap <= len(LIST_5TONE_DELAY):
2911
            line = RadioSetting("_2tone.duration_gap", "Duration of gap",
2912
                                RadioSettingValueList(LIST_5TONE_DELAY,
2913
                                                      LIST_5TONE_DELAY[
2914
                                                          duration_gap]))
2915
            encode_2tone.append(line)
2916

    
2917
        def _2tone_validate(value):
2918
            if value == 0:
2919
                return 65535
2920
            if value == 65535:
2921
                return value
2922
            if not (300 <= value and value <= 3000):
2923
                msg = ("2 Tone Frequency: Must be between 300 and 3000 Hz")
2924
                raise InvalidValueError(msg)
2925
            return value
2926

    
2927
        def apply_2tone_freq(setting, obj):
2928
            val = int(setting.value)
2929
            if (val == 0) or (val == 65535):
2930
                obj.set_value(65535)
2931
            else:
2932
                obj.set_value(val)
2933

    
2934
        i = 1
2935
        for code in self._memobj._2tone._2tone_encode:
2936
            code_2tone = RadioSettingGroup("code_2tone_" + str(i),
2937
                                           "Encode Code " + str(i))
2938
            encode_2tone.append(code_2tone)
2939

    
2940
            tmp = code.freq1
2941
            if tmp == 65535:
2942
                tmp = 0
2943
            val1 = RadioSettingValueInteger(0, 65535, tmp)
2944
            freq1 = RadioSetting("2tone_code_" + str(i) + "_freq1",
2945
                                 "Frequency 1", val1)
2946
            val1.set_validate_callback(_2tone_validate)
2947
            freq1.set_apply_callback(apply_2tone_freq, code.freq1)
2948
            code_2tone.append(freq1)
2949

    
2950
            tmp = code.freq2
2951
            if tmp == 65535:
2952
                tmp = 0
2953
            val2 = RadioSettingValueInteger(0, 65535, tmp)
2954
            freq2 = RadioSetting("2tone_code_" + str(i) + "_freq2",
2955
                                 "Frequency 2", val2)
2956
            val2.set_validate_callback(_2tone_validate)
2957
            freq2.set_apply_callback(apply_2tone_freq, code.freq2)
2958
            code_2tone.append(freq2)
2959

    
2960
            i = i + 1
2961

    
2962
        decode_reset_time = _mem._2tone.reset_time
2963
        if decode_reset_time == 255:
2964
            decode_reset_time = 59
2965
            LOG.debug("Decode reset time unconfigured. resetting.")
2966
        if decode_reset_time <= len(LIST_5TONE_RESET):
2967
            list = RadioSettingValueList(
2968
                LIST_5TONE_RESET,
2969
                LIST_5TONE_RESET[
2970
                    decode_reset_time])
2971
            line = RadioSetting("_2tone.reset_time",
2972
                                "Decode reset time", list)
2973
            decode_2tone.append(line)
2974
        else:
2975
            LOG.debug("Invalid value decode reset time! Disabling.")
2976

    
2977
        def apply_2tone_freq_pair(setting, obj):
2978
            val = int(setting.value)
2979
            derived_val = 65535
2980
            frqname = str(setting._name[-5:])
2981
            derivedname = "derived_from_" + frqname
2982

    
2983
            if (val == 0):
2984
                val = 65535
2985
                derived_val = 65535
2986
            else:
2987
                derived_val = int(round(2304000.0/val))
2988

    
2989
            obj[frqname].set_value(val)
2990
            obj[derivedname].set_value(derived_val)
2991

    
2992
            LOG.debug("Apply " + frqname + ": " + str(val) + " | " +
2993
                      derivedname + ": " + str(derived_val))
2994

    
2995
        i = 1
2996
        for decode_code in self._memobj._2tone._2tone_decode:
2997
            _2tone_dec_code = RadioSettingGroup("code_2tone_" + str(i),
2998
                                                "Decode Code " + str(i))
2999
            decode_2tone.append(_2tone_dec_code)
3000

    
3001
            j = 1
3002
            for dec in decode_code.decs:
3003
                val = dec.dec
3004
                if val == 255:
3005
                    LOG.debug("Dec for Code " + str(i) + " Dec " + str(j) +
3006
                              " is not yet configured. Setting to 0.")
3007
                    val = 0
3008

    
3009
                if val <= len(LIST_2TONE_DEC):
3010
                    line = RadioSetting(
3011
                        "_2tone_dec_settings_" + str(i) + "_dec_" + str(j),
3012
                        "Dec " + str(j), RadioSettingValueList
3013
                        (LIST_2TONE_DEC,
3014
                         LIST_2TONE_DEC[val]))
3015
                    line.set_apply_callback(apply_list_value, dec.dec)
3016
                    _2tone_dec_code.append(line)
3017
                else:
3018
                    LOG.debug("Invalid value for 2tone dec! Disabling.")
3019

    
3020
                val = dec.response
3021
                if val == 255:
3022
                    LOG.debug("Response for Code " +
3023
                              str(i) + " Dec " + str(j) +
3024
                              " is not yet configured. Setting to 0.")
3025
                    val = 0
3026

    
3027
                if val <= len(LIST_2TONE_RESPONSE):
3028
                    line = RadioSetting(
3029
                        "_2tone_dec_settings_" +
3030
                        str(i) + "_resp_" + str(j),
3031
                        "Response " + str(j), RadioSettingValueList
3032
                        (LIST_2TONE_RESPONSE,
3033
                         LIST_2TONE_RESPONSE[val]))
3034
                    line.set_apply_callback(apply_list_value, dec.response)
3035
                    _2tone_dec_code.append(line)
3036
                else:
3037
                    LOG.debug(
3038
                        "Invalid value for 2tone response! Disabling.")
3039

    
3040
                val = dec.alert
3041
                if val == 255:
3042
                    LOG.debug("Alert for Code " +
3043
                              str(i) + " Dec " + str(j) +
3044
                              " is not yet configured. Setting to 0.")
3045
                    val = 0
3046

    
3047
                if val <= len(PTTIDCODE_LIST):
3048
                    line = RadioSetting(
3049
                        "_2tone_dec_settings_" +
3050
                        str(i) + "_alert_" + str(j),
3051
                        "Alert " + str(j), RadioSettingValueList
3052
                        (PTTIDCODE_LIST,
3053
                         PTTIDCODE_LIST[val]))
3054
                    line.set_apply_callback(apply_list_value, dec.alert)
3055
                    _2tone_dec_code.append(line)
3056
                else:
3057
                    LOG.debug("Invalid value for 2tone alert! Disabling.")
3058
                j = j + 1
3059

    
3060
            freq = self._memobj._2tone.freqs[i-1]
3061
            for char in ['A', 'B', 'C', 'D']:
3062
                setting_name = "freq" + str(char)
3063

    
3064
                tmp = freq[setting_name]
3065
                if tmp == 65535:
3066
                    tmp = 0
3067
                if tmp != 0:
3068
                    expected = int(round(2304000.0/int(tmp)))
3069
                    from_mem = freq["derived_from_" + setting_name]
3070
                    if expected != from_mem:
3071
                        LOG.error("Expected " + str(expected) +
3072
                                  " but read " + str(from_mem) +
3073
                                  ". Disabling 2Tone Decode Freqs!")
3074
                        break
3075
                val = RadioSettingValueInteger(0, 65535, tmp)
3076
                frq = RadioSetting("2tone_dec_" + str(i) +
3077
                                   "_freq" + str(char),
3078
                                   ("Decode Frequency " + str(char)), val)
3079
                val.set_validate_callback(_2tone_validate)
3080
                frq.set_apply_callback(apply_2tone_freq_pair, freq)
3081
                _2tone_dec_code.append(frq)
3082

    
3083
            i = i + 1
3084

    
3085
        return top
3086

    
3087
    def set_settings(self, settings):
3088
        _settings = self._memobj.settings
3089
        for element in settings:
3090
            if not isinstance(element, RadioSetting):
3091
                if element.get_name() == "fm_preset":
3092
                    self._set_fm_preset(element)
3093
                else:
3094
                    self.set_settings(element)
3095
                    continue
3096
            else:
3097
                try:
3098
                    name = element.get_name()
3099
                    if "." in name:
3100
                        bits = name.split(".")
3101
                        obj = self._memobj
3102
                        for bit in bits[:-1]:
3103
                            if "/" in bit:
3104
                                bit, index = bit.split("/", 1)
3105
                                index = int(index)
3106
                                obj = getattr(obj, bit)[index]
3107
                            else:
3108
                                obj = getattr(obj, bit)
3109
                        setting = bits[-1]
3110
                    else:
3111
                        obj = _settings
3112
                        setting = element.get_name()
3113

    
3114
                    if element.has_apply_callback():
3115
                        LOG.debug("Using apply callback")
3116
                        element.run_apply_callback()
3117
                    elif setting == "volume" and self.MODEL == "KT-WP12":
3118
                        setattr(obj, setting, int(element.value) - 1)
3119
                    elif setting == "volume" and self.MODEL == "WP-9900":
3120
                        setattr(obj, setting, int(element.value) - 1)
3121
                    elif element.value.get_mutable():
3122
                        LOG.debug("Setting %s = %s" % (setting, element.value))
3123
                        setattr(obj, setting, element.value)
3124
                except Exception:
3125
                    LOG.debug(element.get_name())
3126
                    raise
3127

    
3128
    @classmethod
3129
    def match_model(cls, filedata, filename):
3130
        match_size = False
3131
        match_model = False
3132

    
3133
        # testing the file data size
3134
        if len(filedata) == MEM_SIZE:
3135
            match_size = True
3136

    
3137
        # testing the firmware model fingerprint
3138
        match_model = model_match(cls, filedata)
3139

    
3140
        if match_size and match_model:
3141
            return True
3142
        else:
3143
            return False
3144

    
3145

    
3146
MEM_FORMAT = """
3147
#seekto 0x0000;
3148
struct {
3149
  lbcd rxfreq[4];
3150
  lbcd txfreq[4];
3151
  ul16 rxtone;
3152
  ul16 txtone;
3153
  u8 unknown0:4,
3154
     scode:4;
3155
  u8 unknown1:2,
3156
     spmute:2,
3157
     unknown2:2,
3158
     optsig:2;
3159
  u8 unknown3:3,
3160
     scramble:1,
3161
     unknown4:3,
3162
     power:1;
3163
  u8 unknown5:1,
3164
     wide:1,
3165
     unknown6:2,
3166
     bcl:1,
3167
     add:1,
3168
     pttid:2;
3169
} memory[200];
3170

    
3171
#seekto 0x0E00;
3172
struct {
3173
  u8 unusedE00:7,
3174
     tdr:1;
3175
  u8 unknown1;
3176
  u8 sql;
3177
  u8 unknown2[2];
3178
  u8 tot;
3179
  u8 apo;           // BTech radios use this as the Auto Power Off time
3180
                    // other radios use this as pre-Time Out Alert
3181
  u8 unknown3;
3182
  u8 abr;
3183
  u8 unusedE09:7,
3184
     beep:1;
3185
  u8 unknown4[4];
3186
  u8 dtmfst;
3187
  u8 unknown5[2];
3188
  u8 unusedE11:7,
3189
     prisc:1;
3190
  u8 prich;
3191
  u8 screv;
3192
  u8 unknown6[2];
3193
  u8 pttid;
3194
  u8 pttlt;
3195
  u8 unknown7;
3196
  u8 emctp;
3197
  u8 emcch;
3198
  u8 ringt;
3199
  u8 unknown8;
3200
  u8 camdf;
3201
  u8 cbmdf;
3202
  u8 unusedE1F:7,
3203
     sync:1;        // BTech radios use this as the display sync setting
3204
                    // other radios use this as the auto keypad lock setting
3205
  u8 ponmsg;
3206
  u8 wtled;
3207
  u8 rxled;
3208
  u8 txled;
3209
  u8 unknown9[5];
3210
  u8 anil;
3211
  u8 reps;
3212
  u8 repm;
3213
  u8 tdrab;
3214
  u8 unusedE2D:7,
3215
     ste:1;
3216
  u8 rpste;
3217
  u8 rptdl;
3218
  u8 mgain;
3219
  u8 dtmfg;
3220
} settings;
3221

    
3222
#seekto 0x0E80;
3223
struct {
3224
  u8 unknown1;
3225
  u8 vfomr;
3226
  u8 unusedE82:7,
3227
     keylock:1;
3228
  u8 unknown2;
3229
  u8 unknown3:4,
3230
     vfomren:1,
3231
     unknown4:1,
3232
     reseten:1,
3233
     menuen:1;
3234
  u8 unknown5[11];
3235
  u8 dispab;
3236
  u8 mrcha;
3237
  u8 mrchb;
3238
  u8 menu;
3239
} settings2;
3240

    
3241
#seekto 0x0EC0;
3242
struct {
3243
  char line1[6];
3244
  char line2[6];
3245
} poweron_msg;
3246

    
3247
struct settings_vfo {
3248
  u8 freq[8];
3249
  u8 offset[6];
3250
  u8 unknown2[2];
3251
  ul16 rxtone;
3252
  ul16 txtone;
3253
  u8 scode;
3254
  u8 spmute;
3255
  u8 optsig;
3256
  u8 unused1:7,
3257
     scramble:1;
3258
  u8 wide;
3259
  u8 power;
3260
  u8 shiftd;
3261
  u8 step;
3262
  u8 unknown3[4];
3263
};
3264

    
3265
#seekto 0x0F00;
3266
struct {
3267
  struct settings_vfo a;
3268
  struct settings_vfo b;
3269
} vfo;
3270

    
3271
#seekto 0x1000;
3272
struct {
3273
  char name[6];
3274
  u8 unknown1[10];
3275
} names[200];
3276

    
3277
#seekto 0x2400;
3278
struct {
3279
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3280
  u8 group_tone;
3281
  u8 repeat_tone;
3282
  u8 unused[13];
3283
} _5tone_std_settings[15];
3284

    
3285
#seekto 0x2500;
3286
struct {
3287
  u8 frame1[5];
3288
  u8 frame2[5];
3289
  u8 frame3[5];
3290
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3291
} _5tone_codes[15];
3292

    
3293
#seekto 0x25F0;
3294
struct {
3295
  u8 _5tone_delay1; // * 10ms
3296
  u8 _5tone_delay2; // * 10ms
3297
  u8 _5tone_delay3; // * 10ms
3298
  u8 _5tone_first_digit_ext_length;
3299
  u8 unknown1;
3300
  u8 unknown2;
3301
  u8 unknown3;
3302
  u8 unknown4;
3303
  u8 decode_standard;
3304
  u8 unknown5:5,
3305
     _5tone_decode_call_frame3:1,
3306
     _5tone_decode_call_frame2:1,
3307
     _5tone_decode_call_frame1:1;
3308
  u8 unknown6:5,
3309
     _5tone_decode_disp_frame3:1,
3310
     _5tone_decode_disp_frame2:1,
3311
     _5tone_decode_disp_frame1:1;
3312
  u8 decode_reset_time; // * 100 + 100ms
3313
} _5tone_settings;
3314

    
3315
#seekto 0x2900;
3316
struct {
3317
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3318
} dtmf_codes[15];
3319

    
3320
#seekto 0x29F0;
3321
struct {
3322
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3323
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3324
  u8 unknown0[14];
3325
  u8 inspection[16];
3326
  u8 monitor[16];
3327
  u8 alarmcode[16];
3328
  u8 stun[16];
3329
  u8 kill[16];
3330
  u8 revive[16];
3331
  u8 unknown1[16];
3332
  u8 unknown2[16];
3333
  u8 unknown3[16];
3334
  u8 unknown4[16];
3335
  u8 unknown5[16];
3336
  u8 unknown6[16];
3337
  u8 unknown7[16];
3338
  u8 masterid[16];
3339
  u8 viceid[16];
3340
  u8 unused01:7,
3341
     mastervice:1;
3342
  u8 unused02:3,
3343
     mrevive:1,
3344
     mkill:1,
3345
     mstun:1,
3346
     mmonitor:1,
3347
     minspection:1;
3348
  u8 unused03:3,
3349
     vrevive:1,
3350
     vkill:1,
3351
     vstun:1,
3352
     vmonitor:1,
3353
     vinspection:1;
3354
  u8 unused04:6,
3355
     txdisable:1,
3356
     rxdisable:1;
3357
  u8 groupcode;
3358
  u8 spacecode;
3359
  u8 delayproctime; // * 100 + 100ms
3360
  u8 resettime;     // * 100 + 100ms
3361
} dtmf_settings;
3362

    
3363
#seekto 0x2D00;
3364
struct {
3365
  struct {
3366
    ul16 freq1;
3367
    u8 unused01[6];
3368
    ul16 freq2;
3369
    u8 unused02[6];
3370
  } _2tone_encode[15];
3371
  u8 duration_1st_tone; // *10ms
3372
  u8 duration_2nd_tone; // *10ms
3373
  u8 duration_gap;      // *10ms
3374
  u8 unused03[13];
3375
  struct {
3376
    struct {
3377
      u8 dec;      // one out of LIST_2TONE_DEC
3378
      u8 response; // one out of LIST_2TONE_RESPONSE
3379
      u8 alert;    // 1-16
3380
    } decs[4];
3381
    u8 unused04[4];
3382
  } _2tone_decode[15];
3383
  u8 unused05[16];
3384

    
3385
  struct {
3386
    ul16 freqA;
3387
    ul16 freqB;
3388
    ul16 freqC;
3389
    ul16 freqD;
3390
    // unknown what those values mean, but they are
3391
    // derived from configured frequencies
3392
    ul16 derived_from_freqA; // 2304000/freqA
3393
    ul16 derived_from_freqB; // 2304000/freqB
3394
    ul16 derived_from_freqC; // 2304000/freqC
3395
    ul16 derived_from_freqD; // 2304000/freqD
3396
  }freqs[15];
3397
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3398
} _2tone;
3399

    
3400
#seekto 0x3000;
3401
struct {
3402
  u8 freq[8];
3403
  char broadcast_station_name[6];
3404
  u8 unknown[2];
3405
} fm_radio_preset[16];
3406

    
3407
#seekto 0x3C90;
3408
struct {
3409
  u8 vhf_low[3];
3410
  u8 vhf_high[3];
3411
  u8 uhf_low[3];
3412
  u8 uhf_high[3];
3413
} ranges;
3414

    
3415
// the UV-2501+220 & KT8900R has different zones for storing ranges
3416

    
3417
#seekto 0x3CD0;
3418
struct {
3419
  u8 vhf_low[3];
3420
  u8 vhf_high[3];
3421
  u8 unknown1[4];
3422
  u8 unknown2[6];
3423
  u8 vhf2_low[3];
3424
  u8 vhf2_high[3];
3425
  u8 unknown3[4];
3426
  u8 unknown4[6];
3427
  u8 uhf_low[3];
3428
  u8 uhf_high[3];
3429
} ranges220;
3430

    
3431
#seekto 0x3F70;
3432
struct {
3433
  char fp[6];
3434
} fingerprint;
3435

    
3436
"""
3437

    
3438

    
3439
class BTech(BTechMobileCommon):
3440
    """BTECH's UV-5001 and alike radios"""
3441
    BANDS = 2
3442
    COLOR_LCD = False
3443
    NAME_LENGTH = 6
3444

    
3445
    def set_options(self):
3446
        """This is to read the options from the image and set it in the
3447
        environment, for now just the limits of the freqs in the VHF/UHF
3448
        ranges"""
3449

    
3450
        # setting the correct ranges for each radio type
3451
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3452
            # the model 2501+220 has a segment in 220
3453
            # and a different position in the memmap
3454
            # also the QYT KT8900R
3455
            ranges = self._memobj.ranges220
3456
        else:
3457
            ranges = self._memobj.ranges
3458

    
3459
        # the normal dual bands
3460
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3461
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3462

    
3463
        # DEBUG
3464
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3465
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3466

    
3467
        # 220 MHz radios case
3468
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
3469
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3470
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3471
            self._220_range = vhf2
3472

    
3473
        # set the class with the real data
3474
        self._vhf_range = vhf
3475
        self._uhf_range = uhf
3476

    
3477
    def process_mmap(self):
3478
        """Process the mem map into the mem object"""
3479

    
3480
        # Get it
3481
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
3482

    
3483
        # load specific parameters from the radio image
3484
        self.set_options()
3485

    
3486

    
3487
# Declaring Aliases (Clones of the real radios)
3488
class JT2705M(chirp_common.Alias):
3489
    VENDOR = "Jetstream"
3490
    MODEL = "JT2705M"
3491

    
3492

    
3493
class JT6188Mini(chirp_common.Alias):
3494
    VENDOR = "Juentai"
3495
    MODEL = "JT-6188 Mini"
3496

    
3497

    
3498
class JT6188Plus(chirp_common.Alias):
3499
    VENDOR = "Juentai"
3500
    MODEL = "JT-6188 Plus"
3501

    
3502

    
3503
class SSGT890(chirp_common.Alias):
3504
    VENDOR = "Sainsonic"
3505
    MODEL = "GT-890"
3506

    
3507

    
3508
class ZastoneMP300(chirp_common.Alias):
3509
    VENDOR = "Zastone"
3510
    MODEL = "MP-300"
3511

    
3512

    
3513
# real radios
3514
@directory.register
3515
class UV2501(BTech):
3516
    """Baofeng Tech UV2501"""
3517
    MODEL = "UV-2501"
3518
    _fileid = [UV2501G3_fp,
3519
               UV2501G2_fp,
3520
               UV2501pp2_fp,
3521
               UV2501pp_fp]
3522

    
3523

    
3524
@directory.register
3525
class UV2501_220(BTech):
3526
    """Baofeng Tech UV2501+220"""
3527
    MODEL = "UV-2501+220"
3528
    BANDS = 3
3529
    _magic = MSTRING_220
3530
    _fileid = [UV2501_220G3_fp,
3531
               UV2501_220G2_fp,
3532
               UV2501_220_fp,
3533
               UV2501_220pp_fp]
3534

    
3535

    
3536
@directory.register
3537
class UV5001(BTech):
3538
    """Baofeng Tech UV5001"""
3539
    MODEL = "UV-5001"
3540
    _fileid = [UV5001G3_fp,
3541
               UV5001G22_fp,
3542
               UV5001G2_fp,
3543
               UV5001alpha_fp,
3544
               UV5001pp_fp]
3545
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
3546
                     chirp_common.PowerLevel("Low", watts=10)]
3547

    
3548

    
3549
@directory.register
3550
class MINI8900(BTech):
3551
    """WACCOM MINI-8900"""
3552
    VENDOR = "WACCOM"
3553
    MODEL = "MINI-8900"
3554
    _magic = MSTRING_MINI8900
3555
    _fileid = [MINI8900_fp]
3556
    # Clones
3557
    ALIASES = [JT6188Plus]
3558

    
3559

    
3560
@directory.register
3561
class KTUV980(BTech):
3562
    """QYT KT-UV980"""
3563
    VENDOR = "QYT"
3564
    MODEL = "KT-UV980"
3565
    _vhf_range = (136000000, 175000000)
3566
    _uhf_range = (400000000, 481000000)
3567
    _magic = MSTRING_MINI8900
3568
    _fileid = [KTUV980_fp]
3569
    # Clones
3570
    ALIASES = [JT2705M]
3571

    
3572
# Please note that there is a version of this radios that is a clone of the
3573
# Waccom Mini8900, maybe an early version?
3574

    
3575

    
3576
class OTGRadioV1(chirp_common.Alias):
3577
    VENDOR = 'OTGSTUFF'
3578
    MODEL = 'OTG Radio v1'
3579

    
3580

    
3581
@directory.register
3582
class KT9800(BTech):
3583
    """QYT KT8900"""
3584
    VENDOR = "QYT"
3585
    MODEL = "KT8900"
3586
    _vhf_range = (136000000, 175000000)
3587
    _uhf_range = (400000000, 481000000)
3588
    _magic = MSTRING_KT8900
3589
    _fileid = [KT8900_fp,
3590
               KT8900_fp1,
3591
               KT8900_fp2,
3592
               KT8900_fp3,
3593
               KT8900_fp4,
3594
               KT8900_fp5,
3595
               KT8900_fp6,
3596
               KT8900_fp7]
3597
    # Clones
3598
    ALIASES = [JT6188Mini, SSGT890, ZastoneMP300]
3599

    
3600

    
3601
@directory.register
3602
class KT9800R(BTech):
3603
    """QYT KT8900R"""
3604
    VENDOR = "QYT"
3605
    MODEL = "KT8900R"
3606
    BANDS = 3
3607
    _vhf_range = (136000000, 175000000)
3608
    _220_range = (240000000, 271000000)
3609
    _uhf_range = (400000000, 481000000)
3610
    _magic = MSTRING_KT8900R
3611
    _fileid = [KT8900R_fp,
3612
               KT8900R_fp1,
3613
               KT8900R_fp2,
3614
               KT8900R_fp3,
3615
               KT8900R_fp4,
3616
               KT8900R_fp5]
3617

    
3618

    
3619
@directory.register
3620
class LT588UV(BTech):
3621
    """LUITON LT-588UV"""
3622
    VENDOR = "LUITON"
3623
    MODEL = "LT-588UV"
3624
    _vhf_range = (136000000, 175000000)
3625
    _uhf_range = (400000000, 481000000)
3626
    _magic = MSTRING_KT8900
3627
    _fileid = [LT588UV_fp,
3628
               LT588UV_fp1]
3629
    _power_levels = [chirp_common.PowerLevel("High", watts=60),
3630
                     chirp_common.PowerLevel("Low", watts=10)]
3631

    
3632

    
3633
COLOR_MEM_FORMAT = """
3634
#seekto 0x0000;
3635
struct {
3636
  lbcd rxfreq[4];
3637
  lbcd txfreq[4];
3638
  ul16 rxtone;
3639
  ul16 txtone;
3640
  u8 unknown0:4,
3641
     scode:4;
3642
  u8 unknown1:2,
3643
     spmute:2,
3644
     unknown2:2,
3645
     optsig:2;
3646
  u8 unknown3:3,
3647
     scramble:1,
3648
     unknown4:2,
3649
     power:2;
3650
  u8 unknown5:1,
3651
     wide:1,
3652
     unknown6:2,
3653
     bcl:1,
3654
     add:1,
3655
     pttid:2;
3656
} memory[200];
3657

    
3658
#seekto 0x0E00;
3659
struct {
3660
  u8 tmr;
3661
  u8 unknown1;
3662
  u8 sql;
3663
  u8 unknown2;
3664
  u8 mgain2;
3665
  u8 tot;
3666
  u8 apo;
3667
  u8 unknown3;
3668
  u8 abr;
3669
  u8 unusedE09:7,
3670
     beep:1;
3671
  u8 unknown4[4];
3672
  u8 dtmfst;
3673
  u8 unknown5[2];
3674
  u8 screv;
3675
  u8 unknown6[2];
3676
  u8 pttid;
3677
  u8 pttlt;
3678
  u8 unknown7;
3679
  u8 emctp;
3680
  u8 emcch;
3681
  u8 unusedE19:7,
3682
     sigbp:1;
3683
  u8 unknown8;
3684
  u8 camdf;
3685
  u8 cbmdf;
3686
  u8 ccmdf;
3687
  u8 cdmdf;
3688
  u8 langua;        // BTech Gen2 radios have removed the Language setting
3689
                    // use this as the VOX setting
3690
  u8 sync;          // BTech radios use this as the display sync
3691
                    // setting, other radios use this as the auto
3692
                    // keypad lock setting
3693
  u8 mainfc;
3694
  u8 mainbc;
3695
  u8 menufc;
3696
  u8 menubc;
3697
  u8 stafc;
3698
  u8 stabc;
3699
  u8 sigfc;
3700
  u8 sigbc;
3701
  u8 rxfc;
3702
  u8 txfc;
3703
  u8 txdisp;
3704
  u8 unknown9[5];
3705
  u8 anil;
3706
  u8 reps;
3707
  u8 repm;
3708
  u8 tmrmr;
3709
  u8 unusedE35:7,
3710
     ste:1;
3711
  u8 rpste;
3712
  u8 rptdl;
3713
  u8 dtmfg;
3714
  u8 mgain;         // used by db25-g for ponyey
3715
  u8 skiptx;
3716
  u8 scmode;
3717
  u8 tmrtx;
3718
  u8 earpho;
3719
} settings;
3720

    
3721
#seekto 0x0E80;
3722
struct {
3723
  u8 unknown1;
3724
  u8 vfomr;
3725
  u8 unusedE82:7,
3726
     keylock:1;
3727
  u8 unknown2;
3728
  u8 unknown3:4,
3729
     vfomren:1,
3730
     unknown4:1,
3731
     reseten:1,
3732
     menuen:1;
3733
  u8 unknown5[11];
3734
  u8 dispab;
3735
  u8 unknown6[2];
3736
  u8 menu;
3737
  u8 unknown7[7];
3738
  u8 vfomra;
3739
  u8 vfomrb;
3740
  u8 vfomrc;
3741
  u8 vfomrd;
3742
  u8 mrcha;
3743
  u8 mrchb;
3744
  u8 mrchc;
3745
  u8 mrchd;
3746
} settings2;
3747

    
3748
struct settings_vfo {
3749
  u8 freq[8];
3750
  u8 offset[6];
3751
  u8 unknown2[2];
3752
  ul16 rxtone;
3753
  ul16 txtone;
3754
  u8 scode;
3755
  u8 spmute;
3756
  u8 optsig;
3757
  u8 unused1:7,
3758
     scramble:1;
3759
  u8 wide;
3760
  u8 power;
3761
  u8 shiftd;
3762
  u8 step;
3763
  u8 unknown3[4];
3764
};
3765

    
3766
#seekto 0x0F00;
3767
struct {
3768
  struct settings_vfo a;
3769
  struct settings_vfo b;
3770
  struct settings_vfo c;
3771
  struct settings_vfo d;
3772
} vfo;
3773

    
3774
#seekto 0x0F80;
3775
struct {
3776
  char line1[8];
3777
  char line2[8];
3778
  char line3[8];
3779
  char line4[8];
3780
  char line5[8];
3781
  char line6[8];
3782
  char line7[8];
3783
  char line8[8];
3784
} poweron_msg;
3785

    
3786
#seekto 0x1000;
3787
struct {
3788
  char name[8];
3789
  u8 unknown1[8];
3790
} names[200];
3791

    
3792
#seekto 0x2400;
3793
struct {
3794
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
3795
  u8 group_tone;
3796
  u8 repeat_tone;
3797
  u8 unused[13];
3798
} _5tone_std_settings[15];
3799

    
3800
#seekto 0x2500;
3801
struct {
3802
  u8 frame1[5];
3803
  u8 frame2[5];
3804
  u8 frame3[5];
3805
  u8 standard;   // one out of LIST_5TONE_STANDARDS
3806
} _5tone_codes[15];
3807

    
3808
#seekto 0x25F0;
3809
struct {
3810
  u8 _5tone_delay1; // * 10ms
3811
  u8 _5tone_delay2; // * 10ms
3812
  u8 _5tone_delay3; // * 10ms
3813
  u8 _5tone_first_digit_ext_length;
3814
  u8 unknown1;
3815
  u8 unknown2;
3816
  u8 unknown3;
3817
  u8 unknown4;
3818
  u8 decode_standard;
3819
  u8 unknown5:5,
3820
     _5tone_decode_call_frame3:1,
3821
     _5tone_decode_call_frame2:1,
3822
     _5tone_decode_call_frame1:1;
3823
  u8 unknown6:5,
3824
     _5tone_decode_disp_frame3:1,
3825
     _5tone_decode_disp_frame2:1,
3826
     _5tone_decode_disp_frame1:1;
3827
  u8 decode_reset_time; // * 100 + 100ms
3828
} _5tone_settings;
3829

    
3830
#seekto 0x2900;
3831
struct {
3832
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
3833
} dtmf_codes[15];
3834

    
3835
#seekto 0x29F0;
3836
struct {
3837
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
3838
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
3839
  u8 unknown0[14];
3840
  u8 inspection[16];
3841
  u8 monitor[16];
3842
  u8 alarmcode[16];
3843
  u8 stun[16];
3844
  u8 kill[16];
3845
  u8 revive[16];
3846
  u8 unknown1[16];
3847
  u8 unknown2[16];
3848
  u8 unknown3[16];
3849
  u8 unknown4[16];
3850
  u8 unknown5[16];
3851
  u8 unknown6[16];
3852
  u8 unknown7[16];
3853
  u8 masterid[16];
3854
  u8 viceid[16];
3855
  u8 unused01:7,
3856
     mastervice:1;
3857
  u8 unused02:3,
3858
     mrevive:1,
3859
     mkill:1,
3860
     mstun:1,
3861
     mmonitor:1,
3862
     minspection:1;
3863
  u8 unused03:3,
3864
     vrevive:1,
3865
     vkill:1,
3866
     vstun:1,
3867
     vmonitor:1,
3868
     vinspection:1;
3869
  u8 unused04:6,
3870
     txdisable:1,
3871
     rxdisable:1;
3872
  u8 groupcode;
3873
  u8 spacecode;
3874
  u8 delayproctime; // * 100 + 100ms
3875
  u8 resettime;     // * 100 + 100ms
3876
} dtmf_settings;
3877

    
3878
#seekto 0x2D00;
3879
struct {
3880
  struct {
3881
    ul16 freq1;
3882
    u8 unused01[6];
3883
    ul16 freq2;
3884
    u8 unused02[6];
3885
  } _2tone_encode[15];
3886
  u8 duration_1st_tone; // *10ms
3887
  u8 duration_2nd_tone; // *10ms
3888
  u8 duration_gap;      // *10ms
3889
  u8 unused03[13];
3890
  struct {
3891
    struct {
3892
      u8 dec;      // one out of LIST_2TONE_DEC
3893
      u8 response; // one out of LIST_2TONE_RESPONSE
3894
      u8 alert;    // 1-16
3895
    } decs[4];
3896
    u8 unused04[4];
3897
  } _2tone_decode[15];
3898
  u8 unused05[16];
3899

    
3900
  struct {
3901
    ul16 freqA;
3902
    ul16 freqB;
3903
    ul16 freqC;
3904
    ul16 freqD;
3905
    // unknown what those values mean, but they are
3906
    // derived from configured frequencies
3907
    ul16 derived_from_freqA; // 2304000/freqA
3908
    ul16 derived_from_freqB; // 2304000/freqB
3909
    ul16 derived_from_freqC; // 2304000/freqC
3910
    ul16 derived_from_freqD; // 2304000/freqD
3911
  }freqs[15];
3912
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
3913
} _2tone;
3914

    
3915
#seekto 0x3D80;
3916
struct {
3917
  u8 vhf_low[3];
3918
  u8 vhf_high[3];
3919
  u8 unknown1[4];
3920
  u8 unknown2[6];
3921
  u8 vhf2_low[3];
3922
  u8 vhf2_high[3];
3923
  u8 unknown3[4];
3924
  u8 unknown4[6];
3925
  u8 uhf_low[3];
3926
  u8 uhf_high[3];
3927
  u8 unknown5[4];
3928
  u8 unknown6[6];
3929
  u8 uhf2_low[3];
3930
  u8 uhf2_high[3];
3931
} ranges;
3932

    
3933
#seekto 0x3F70;
3934
struct {
3935
  char fp[6];
3936
} fingerprint;
3937

    
3938
"""
3939

    
3940

    
3941
class BTechColor(BTechMobileCommon):
3942
    """BTECH's Color LCD Mobile and alike radios"""
3943
    COLOR_LCD = True
3944
    NAME_LENGTH = 8
3945
    LIST_TMR = LIST_TMR16
3946

    
3947
    def process_mmap(self):
3948
        """Process the mem map into the mem object"""
3949

    
3950
        # Get it
3951
        self._memobj = bitwise.parse(COLOR_MEM_FORMAT, self._mmap)
3952

    
3953
        # load specific parameters from the radio image
3954
        self.set_options()
3955

    
3956
    def set_options(self):
3957
        """This is to read the options from the image and set it in the
3958
        environment, for now just the limits of the freqs in the VHF/UHF
3959
        ranges"""
3960

    
3961
        # setting the correct ranges for each radio type
3962
        ranges = self._memobj.ranges
3963

    
3964
        # the normal dual bands
3965
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
3966
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
3967

    
3968
        # DEBUG
3969
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
3970
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
3971

    
3972
        # the additional bands
3973
        if self.MODEL in ["UV-25X4", "KT7900D"]:
3974
            # 200 MHz band
3975
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
3976
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
3977
            self._220_range = vhf2
3978

    
3979
            # 350 MHz band
3980
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
3981
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
3982
            self._350_range = uhf2
3983

    
3984
        # set the class with the real data
3985
        self._vhf_range = vhf
3986
        self._uhf_range = uhf
3987

    
3988

    
3989
# Declaring Aliases (Clones of the real radios)
3990
class SKT8900D(chirp_common.Alias):
3991
    VENDOR = "Surecom"
3992
    MODEL = "S-KT8900D"
3993

    
3994

    
3995
class QB25(chirp_common.Alias):
3996
    VENDOR = "Radioddity"
3997
    MODEL = "QB25"
3998

    
3999

    
4000
# real radios
4001
@directory.register
4002
class UV25X2(BTechColor):
4003
    """Baofeng Tech UV25X2"""
4004
    MODEL = "UV-25X2"
4005
    BANDS = 2
4006
    _vhf_range = (130000000, 180000000)
4007
    _uhf_range = (400000000, 521000000)
4008
    _magic = MSTRING_UV25X2
4009
    _fileid = [UV25X2_fp]
4010

    
4011

    
4012
@directory.register
4013
class UV25X2_G2(UV25X2):
4014
    """Baofeng Tech UV25X2_G2"""
4015
    MODEL = "UV-25X2_G2"
4016
    _fileid = [UV25X2G2_fp]
4017

    
4018

    
4019
@directory.register
4020
class UV25X4(BTechColor):
4021
    """Baofeng Tech UV25X4"""
4022
    MODEL = "UV-25X4"
4023
    BANDS = 4
4024
    _vhf_range = (130000000, 180000000)
4025
    _220_range = (200000000, 271000000)
4026
    _uhf_range = (400000000, 521000000)
4027
    _350_range = (350000000, 391000000)
4028
    _magic = MSTRING_UV25X4
4029
    _fileid = [UV25X4_fp]
4030

    
4031

    
4032
@directory.register
4033
class UV25X4_G2(UV25X4):
4034
    """Baofeng Tech UV25X4_G2"""
4035
    MODEL = "UV-25X4_G2"
4036
    _fileid = [UV25X4G2_fp]
4037

    
4038

    
4039
@directory.register
4040
class UV50X2(BTechColor):
4041
    """Baofeng Tech UV50X2"""
4042
    MODEL = "UV-50X2"
4043
    BANDS = 2
4044
    _vhf_range = (130000000, 180000000)
4045
    _uhf_range = (400000000, 521000000)
4046
    _magic = MSTRING_UV25X2
4047
    _fileid = [UV50X2_fp]
4048
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4049
                     chirp_common.PowerLevel("Low", watts=10)]
4050

    
4051

    
4052
@directory.register
4053
class UV50X2_G2(BTechColor):
4054
    """Baofeng Tech UV50X2_G2"""
4055
    MODEL = "UV-50X2_G2"
4056
    BANDS = 2
4057
    _vhf_range = (130000000, 180000000)
4058
    _uhf_range = (400000000, 521000000)
4059
    _magic = MSTRING_UV25X2
4060
    _fileid = [UV50X2_fp]
4061
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4062
                     chirp_common.PowerLevel("Low", watts=10)]
4063

    
4064
    @classmethod
4065
    def match_model(cls, filedata, filename):
4066
        # This model is only ever matched via metadata
4067
        return False
4068

    
4069

    
4070
@directory.register
4071
class KT7900D(BTechColor):
4072
    """QYT KT7900D"""
4073
    VENDOR = "QYT"
4074
    MODEL = "KT7900D"
4075
    BANDS = 4
4076
    LIST_TMR = LIST_TMR15
4077
    _vhf_range = (136000000, 175000000)
4078
    _220_range = (200000000, 271000000)
4079
    _uhf_range = (400000000, 481000000)
4080
    _350_range = (350000000, 371000000)
4081
    _magic = MSTRING_KT8900D
4082
    _fileid = [KT7900D_fp, KT7900D_fp1, KT7900D_fp2, KT7900D_fp3, KT7900D_fp4,
4083
               KT7900D_fp5, KT7900D_fp6, KT7900D_fp7, KT7900D_fp8, KT7900D_fp9,
4084
               QB25_fp]
4085
    # Clones
4086
    ALIASES = [SKT8900D, QB25]
4087

    
4088

    
4089
@directory.register
4090
class KT8900D(BTechColor):
4091
    """QYT KT8900D"""
4092
    VENDOR = "QYT"
4093
    MODEL = "KT8900D"
4094
    BANDS = 2
4095
    LIST_TMR = LIST_TMR15
4096
    _vhf_range = (136000000, 175000000)
4097
    _uhf_range = (400000000, 481000000)
4098
    _magic = MSTRING_KT8900D
4099
    _fileid = [KT8900D_fp3, KT8900D_fp2, KT8900D_fp1, KT8900D_fp]
4100

    
4101
    # Clones
4102
    ALIASES = [OTGRadioV1]
4103

    
4104

    
4105
@directory.register
4106
class KT5800(BTechColor):
4107
    """QYT KT5800"""
4108
    VENDOR = "QYT"
4109
    MODEL = "KT5800"
4110
    BANDS = 2
4111
    LIST_TMR = LIST_TMR15
4112
    _vhf_range = (136000000, 175000000)
4113
    _uhf_range = (400000000, 481000000)
4114
    _magic = MSTRING_KT8900D
4115
    _fileid = [KT5800_fp]
4116

    
4117

    
4118
@directory.register
4119
class KT980PLUS(BTechColor):
4120
    """QYT KT980PLUS"""
4121
    VENDOR = "QYT"
4122
    MODEL = "KT980PLUS"
4123
    BANDS = 2
4124
    LIST_TMR = LIST_TMR15
4125
    _vhf_range = (136000000, 175000000)
4126
    _uhf_range = (400000000, 481000000)
4127
    _magic = MSTRING_KT8900D
4128
    _fileid = [KT980PLUS_fp1, KT980PLUS_fp]
4129
    _power_levels = [chirp_common.PowerLevel("High", watts=75),
4130
                     chirp_common.PowerLevel("Low", watts=55)]
4131

    
4132
    @classmethod
4133
    def match_model(cls, filedata, filename):
4134
        # This model is only ever matched via metadata
4135
        return False
4136

    
4137

    
4138
@directory.register
4139
class DB25G(BTechColor):
4140
    """Radioddity DB25-G"""
4141
    VENDOR = "Radioddity"
4142
    MODEL = "DB25-G"
4143
    BANDS = 2
4144
    LIST_TMR = LIST_TMR15
4145
    _vhf_range = (136000000, 175000000)
4146
    _uhf_range = (400000000, 481000000)
4147
    _magic = MSTRING_KT8900D
4148
    _fileid = [DB25G_fp1, DB25G_fp]
4149
    _gmrs = True
4150
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
4151
                     chirp_common.PowerLevel("Mid", watts=15),
4152
                     chirp_common.PowerLevel("Low", watts=5)]
4153

    
4154
    def validate_memory(self, mem):
4155
        msgs = super().validate_memory(mem)
4156

    
4157
        _msg_duplex = 'Duplex must be "off" for this frequency'
4158
        _msg_offset = 'Only simplex or +5 MHz offset allowed on GMRS'
4159

    
4160
        if mem.freq in GMRS_FREQS3:
4161
            if mem.duplex and mem.offset != 5000000:
4162
                msgs.append(chirp_common.ValidationWarning(_msg_offset))
4163
            if mem.duplex and mem.duplex != "+":
4164
                msgs.append(chirp_common.ValidationWarning(_msg_offset))
4165

    
4166
        return msgs
4167

    
4168
    def check_set_memory_immutable_policy(self, existing, new):
4169
        existing.immutable = []
4170
        super().check_set_memory_immutable_policy(existing, new)
4171

    
4172
    @classmethod
4173
    def match_model(cls, filedata, filename):
4174
        # This model is only ever matched via metadata
4175
        return False
4176

    
4177

    
4178
GMRS_MEM_FORMAT = """
4179
#seekto 0x0000;
4180
struct {
4181
  lbcd rxfreq[4];
4182
  lbcd txfreq[4];
4183
  ul16 rxtone;
4184
  ul16 txtone;
4185
  u8 unknown0:4,
4186
     scode:4;
4187
  u8 unknown1:2,
4188
     spmute:2,
4189
     unknown2:2,
4190
     optsig:2;
4191
  u8 unknown3:3,
4192
     scramble:1,
4193
     unknown4:2,
4194
     power:2;
4195
  u8 unknown5:1,
4196
     wide:1,
4197
     unknown6:2,
4198
     bcl:1,
4199
     add:1,
4200
     pttid:2;
4201
} memory[256];
4202

    
4203
#seekto 0x1000;
4204
struct {
4205
  char name[7];
4206
  u8 unknown1[9];
4207
} names[256];
4208

    
4209
#seekto 0x2400;
4210
struct {
4211
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4212
  u8 group_tone;
4213
  u8 repeat_tone;
4214
  u8 unused[13];
4215
} _5tone_std_settings[15];
4216

    
4217
#seekto 0x2500;
4218
struct {
4219
  u8 frame1[5];
4220
  u8 frame2[5];
4221
  u8 frame3[5];
4222
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4223
} _5tone_codes[15];
4224

    
4225
#seekto 0x25F0;
4226
struct {
4227
  u8 _5tone_delay1; // * 10ms
4228
  u8 _5tone_delay2; // * 10ms
4229
  u8 _5tone_delay3; // * 10ms
4230
  u8 _5tone_first_digit_ext_length;
4231
  u8 unknown1;
4232
  u8 unknown2;
4233
  u8 unknown3;
4234
  u8 unknown4;
4235
  u8 decode_standard;
4236
  u8 unknown5:5,
4237
     _5tone_decode_call_frame3:1,
4238
     _5tone_decode_call_frame2:1,
4239
     _5tone_decode_call_frame1:1;
4240
  u8 unknown6:5,
4241
     _5tone_decode_disp_frame3:1,
4242
     _5tone_decode_disp_frame2:1,
4243
     _5tone_decode_disp_frame1:1;
4244
  u8 decode_reset_time; // * 100 + 100ms
4245
} _5tone_settings;
4246

    
4247
#seekto 0x2900;
4248
struct {
4249
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4250
} dtmf_codes[15];
4251

    
4252
#seekto 0x29F0;
4253
struct {
4254
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4255
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4256
  u8 unknown0[14];
4257
  u8 inspection[16];
4258
  u8 monitor[16];
4259
  u8 alarmcode[16];
4260
  u8 stun[16];
4261
  u8 kill[16];
4262
  u8 revive[16];
4263
  u8 unknown1[16];
4264
  u8 unknown2[16];
4265
  u8 unknown3[16];
4266
  u8 unknown4[16];
4267
  u8 unknown5[16];
4268
  u8 unknown6[16];
4269
  u8 unknown7[16];
4270
  u8 masterid[16];
4271
  u8 viceid[16];
4272
  u8 unused01:7,
4273
     mastervice:1;
4274
  u8 unused02:3,
4275
     mrevive:1,
4276
     mkill:1,
4277
     mstun:1,
4278
     mmonitor:1,
4279
     minspection:1;
4280
  u8 unused03:3,
4281
     vrevive:1,
4282
     vkill:1,
4283
     vstun:1,
4284
     vmonitor:1,
4285
     vinspection:1;
4286
  u8 unused04:6,
4287
     txdisable:1,
4288
     rxdisable:1;
4289
  u8 groupcode;
4290
  u8 spacecode;
4291
  u8 delayproctime; // * 100 + 100ms
4292
  u8 resettime;     // * 100 + 100ms
4293
} dtmf_settings;
4294

    
4295
#seekto 0x2D00;
4296
struct {
4297
  struct {
4298
    ul16 freq1;
4299
    u8 unused01[6];
4300
    ul16 freq2;
4301
    u8 unused02[6];
4302
  } _2tone_encode[15];
4303
  u8 duration_1st_tone; // *10ms
4304
  u8 duration_2nd_tone; // *10ms
4305
  u8 duration_gap;      // *10ms
4306
  u8 unused03[13];
4307
  struct {
4308
    struct {
4309
      u8 dec;      // one out of LIST_2TONE_DEC
4310
      u8 response; // one out of LIST_2TONE_RESPONSE
4311
      u8 alert;    // 1-16
4312
    } decs[4];
4313
    u8 unused04[4];
4314
  } _2tone_decode[15];
4315
  u8 unused05[16];
4316

    
4317
  struct {
4318
    ul16 freqA;
4319
    ul16 freqB;
4320
    ul16 freqC;
4321
    ul16 freqD;
4322
    // unknown what those values mean, but they are
4323
    // derived from configured frequencies
4324
    ul16 derived_from_freqA; // 2304000/freqA
4325
    ul16 derived_from_freqB; // 2304000/freqB
4326
    ul16 derived_from_freqC; // 2304000/freqC
4327
    ul16 derived_from_freqD; // 2304000/freqD
4328
  }freqs[15];
4329
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4330
} _2tone;
4331

    
4332
#seekto 0x3000;
4333
struct {
4334
  u8 freq[8];
4335
  char broadcast_station_name[6];
4336
  u8 unknown[2];
4337
} fm_radio_preset[16];
4338

    
4339
#seekto 0x3280;
4340
struct {
4341
  u8 unknown1;
4342
  u8 vfomr;
4343
  u8 unused3281:7,
4344
     keylock:1;
4345
  u8 unknown2;
4346
  u8 unknown3:4,
4347
     vfomren:1,
4348
     unknown4:1,
4349
     reseten:1,
4350
     menuen:1;
4351
  u8 unknown5[11];
4352
  u8 dispab;
4353
  u8 unknown6[2];
4354
  u8 smenu;
4355
  u8 unknown7[7];
4356
  u8 vfomra;
4357
  u8 vfomrb;
4358
  u8 vfomrc;
4359
  u8 vfomrd;
4360
  u8 mrcha;
4361
  u8 mrchb;
4362
  u8 mrchc;
4363
  u8 mrchd;
4364
} settings2;
4365

    
4366
struct settings_vfo {
4367
  u8 freq[8];
4368
  u8 offset[6];
4369
  u8 unknown2[2];
4370
  ul16 rxtone;
4371
  ul16 txtone;
4372
  u8 scode;
4373
  u8 spmute;
4374
  u8 optsig;
4375
  u8 unused1:7,
4376
     scramble:1;
4377
  u8 wide;
4378
  u8 power;
4379
  u8 shiftd;
4380
  u8 step;
4381
  u8 unknown3[4];
4382
};
4383

    
4384
#seekto 0x3300;
4385
struct {
4386
  struct settings_vfo a;
4387
  struct settings_vfo b;
4388
  struct settings_vfo c;
4389
  struct settings_vfo d;
4390
} vfo;
4391

    
4392
#seekto 0x3D80;
4393
struct {
4394
  u8 vhf_low[3];
4395
  u8 vhf_high[3];
4396
  u8 unknown1[4];
4397
  u8 unknown2[6];
4398
  u8 vhf2_low[3];
4399
  u8 vhf2_high[3];
4400
  u8 unknown3[4];
4401
  u8 unknown4[6];
4402
  u8 uhf_low[3];
4403
  u8 uhf_high[3];
4404
  u8 unknown5[4];
4405
  u8 unknown6[6];
4406
  u8 uhf2_low[3];
4407
  u8 uhf2_high[3];
4408
} ranges;
4409

    
4410
#seekto 0x33B0;
4411
struct {
4412
  char line[16];
4413
} static_msg;
4414

    
4415
#seekto 0x3F70;
4416
struct {
4417
  char fp[6];
4418
} fingerprint;
4419

    
4420
"""
4421

    
4422

    
4423
GMRS_ORIG_MEM_FORMAT = """
4424
#seekto 0x3200;
4425
struct {
4426
  u8 tmr;
4427
  u8 unknown1;
4428
  u8 sql;
4429
  u8 unknown2;
4430
  u8 unused3204:7,
4431
     autolk:1;
4432
  u8 tot;
4433
  u8 apo;
4434
  u8 unknown3;
4435
  u8 abr;
4436
  u8 unused3209:7,
4437
     beep:1;
4438
  u8 unknown4[4];
4439
  u8 dtmfst;
4440
  u8 unknown5[2];
4441
  u8 screv;
4442
  u8 unknown6[2];
4443
  u8 pttid;
4444
  u8 pttlt;
4445
  u8 unknown7;
4446
  u8 emctp;
4447
  u8 emcch;
4448
  u8 unusedE19:7,
4449
     sigbp:1;
4450
  u8 vox;
4451
  u8 camdf;
4452
  u8 cbmdf;
4453
  u8 ccmdf;
4454
  u8 cdmdf;
4455
  u8 langua;
4456
  u8 sync;
4457
  u8 stfc;
4458
  u8 mffc;
4459
  u8 sfafc;
4460
  u8 sfbfc;
4461
  u8 sfcfc;
4462
  u8 sfdfc;
4463
  u8 subfc;
4464
  u8 fmfc;
4465
  u8 sigfc;
4466
  u8 modfc;
4467
  u8 menufc;
4468
  u8 txfc;
4469
  u8 txdisp;
4470
  u8 unknown9[5];
4471
  u8 anil;
4472
  u8 reps;
4473
  u8 repm;
4474
  u8 tmrmr;
4475
  u8 unusedE37:7,
4476
     ste:1;
4477
  u8 rpste;
4478
  u8 rptdl;
4479
  u8 dtmfg;
4480
  u8 mgain;
4481
  u8 skiptx;
4482
  u8 scmode;
4483
  u8 tmrtx;
4484
  u8 unknown10;
4485
  u8 earpho;
4486
} settings;
4487
"""
4488

    
4489

    
4490
GMRS_V2_MEM_FORMAT = """
4491
#seekto 0x3200;
4492
struct {
4493
  u8 tmr;
4494
  u8 unknown1;
4495
  u8 sql;
4496
  u8 unknown2;
4497
  u8 unused3204:7,
4498
     autolk:1;
4499
  u8 tot;
4500
  u8 apo;
4501
  u8 unknown3;
4502
  u8 abr;
4503
  u8 unused3209:7,
4504
     beep:1;
4505
  u8 unknown4[4];
4506
  u8 dtmfst;
4507
  u8 unknown5[2];
4508
  u8 screv;
4509
  u8 unknown6[2];
4510
  u8 pttid;
4511
  u8 pttlt;
4512
  u8 unknown7;
4513
  u8 emctp;
4514
  u8 emcch;
4515
  u8 unusedE19:7,
4516
     sigbp:1;
4517
  u8 unknown8;   // vox
4518
  u8 camdf;
4519
  u8 cbmdf;
4520
  u8 ccmdf;
4521
  u8 cdmdf;
4522
  u8 vox;        // langua
4523
  u8 sync;
4524
  u8 stfc;
4525
  u8 mffc;
4526
  u8 sfafc;
4527
  u8 sfbfc;
4528
  u8 sfcfc;
4529
  u8 sfdfc;
4530
  u8 subfc;
4531
  u8 fmfc;
4532
  u8 sigfc;
4533
  u8 modfc;
4534
  u8 menufc;
4535
  u8 txfc;
4536
  u8 unknown9[5];
4537
  u8 anil;
4538
  u8 reps;
4539
  u8 repm;
4540
  u8 tmrmr;
4541
  u8 unusedE37:7,
4542
     ste:1;
4543
  u8 rpste;
4544
  u8 rptdl;
4545
  u8 dtmfg;
4546
  u8 mgain;
4547
  u8 skiptx;
4548
  u8 scmode;
4549
  u8 tmrtx;
4550
  u8 unknown10[2];
4551
  u8 earpho;
4552
} settings;
4553
"""
4554

    
4555

    
4556
class BTechGMRS(BTechMobileCommon):
4557
    """BTECH's GMRS Mobile"""
4558
    COLOR_LCD = True
4559
    COLOR_LCD2 = True
4560
    NAME_LENGTH = 7
4561
    UPLOAD_MEM_SIZE = 0X3400
4562

    
4563
    def process_mmap(self):
4564
        """Process the mem map into the mem object"""
4565

    
4566
        # Get it
4567
        mem_format = GMRS_MEM_FORMAT + GMRS_ORIG_MEM_FORMAT
4568
        self._memobj = bitwise.parse(mem_format, self._mmap)
4569

    
4570
        # load specific parameters from the radio image
4571
        self.set_options()
4572

    
4573
    def set_options(self):
4574
        """This is to read the options from the image and set it in the
4575
        environment, for now just the limits of the freqs in the VHF/UHF
4576
        ranges"""
4577

    
4578
        # setting the correct ranges for each radio type
4579
        ranges = self._memobj.ranges
4580

    
4581
        # the normal dual bands
4582
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4583
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4584

    
4585
        # DEBUG
4586
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4587
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4588

    
4589
        # set the class with the real data
4590
        self._vhf_range = vhf
4591
        self._uhf_range = uhf
4592

    
4593

    
4594
# real radios
4595
@directory.register
4596
class GMRS50X1(BTechGMRS):
4597
    """Baofeng Tech GMRS50X1"""
4598
    MODEL = "GMRS-50X1"
4599
    BANDS = 2
4600
    LIST_TMR = LIST_TMR16
4601
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4602
                     chirp_common.PowerLevel("Mid", watts=10),
4603
                     chirp_common.PowerLevel("Low", watts=5)]
4604
    _vhf_range = (136000000, 175000000)
4605
    _uhf_range = (400000000, 521000000)
4606
    _upper = 255
4607
    _magic = MSTRING_GMRS50X1
4608
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp]
4609
    _gmrs = True
4610

    
4611
    def validate_memory(self, mem):
4612
        msgs = super().validate_memory(mem)
4613

    
4614
        _msg_duplex = 'Duplex must be "off" for this frequency'
4615
        _msg_offset = 'Only simplex or +5 MHz offset allowed on GMRS'
4616

    
4617
        if not (mem.number >= 1 and mem.number <= 30):
4618
            if mem.duplex != "off":
4619
                msgs.append(chirp_common.ValidationWarning(_msg_duplex))
4620

    
4621
        return msgs
4622

    
4623
    def check_set_memory_immutable_policy(self, existing, new):
4624
        if not (new.number >= 1 and new.number <= 30):
4625
            existing.immutable = []
4626
        super().check_set_memory_immutable_policy(existing, new)
4627

    
4628

    
4629
@directory.register
4630
class GMRS50V2(BTechGMRS):
4631
    """Baofeng Tech GMRS50V2"""
4632
    MODEL = "GMRS-50V2"
4633
    BANDS = 2
4634
    LIST_TMR = LIST_TMR16
4635
    _power_levels = [chirp_common.PowerLevel("High", watts=50),
4636
                     chirp_common.PowerLevel("Mid", watts=10),
4637
                     chirp_common.PowerLevel("Low", watts=5)]
4638
    _vhf_range = (136000000, 175000000)
4639
    _uhf_range = (400000000, 521000000)
4640
    _upper = 255
4641
    _magic = MSTRING_GMRS50X1
4642
    _fileid = [GMRS50X1_fp1, GMRS50X1_fp]
4643
    _gmrs = True
4644

    
4645
    def process_mmap(self):
4646
        """Process the mem map into the mem object"""
4647

    
4648
        # Get it
4649
        mem_format = GMRS_MEM_FORMAT + GMRS_V2_MEM_FORMAT
4650
        self._memobj = bitwise.parse(mem_format, self._mmap)
4651

    
4652
        # load specific parameters from the radio image
4653
        self.set_options()
4654

    
4655
    def validate_memory(self, mem):
4656
        msgs = super().validate_memory(mem)
4657

    
4658
        _msg_duplex = 'Duplex must be "off" for this frequency'
4659
        _msg_offset = 'Only simplex or +5 MHz offset allowed on GMRS'
4660

    
4661
        if mem.freq not in GMRS_FREQS:
4662
            if mem.duplex != "off":
4663
                msgs.append(chirp_common.ValidationWarning(_msg_duplex))
4664
        elif mem.duplex:
4665
            if mem.duplex and mem.offset != 5000000:
4666
                msgs.append(chirp_common.ValidationWarning(_msg_offset))
4667

    
4668
        return msgs
4669

    
4670
    def check_set_memory_immutable_policy(self, existing, new):
4671
        existing.immutable = []
4672
        super().check_set_memory_immutable_policy(existing, new)
4673

    
4674
    @classmethod
4675
    def match_model(cls, filedata, filename):
4676
        # This model is only ever matched via metadata
4677
        return False
4678

    
4679

    
4680
COLORHT_MEM_FORMAT = """
4681
#seekto 0x0000;
4682
struct {
4683
  lbcd rxfreq[4];
4684
  lbcd txfreq[4];
4685
  ul16 rxtone;
4686
  ul16 txtone;
4687
  u8 unknown0:4,
4688
     scode:4;
4689
  u8 unknown1:2,
4690
     spmute:2,
4691
     unknown2:2,
4692
     optsig:2;
4693
  u8 unknown3:3,
4694
     scramble:1,
4695
     unknown4:3,
4696
     power:1;
4697
  u8 unknown5:1,
4698
     wide:1,
4699
     unknown6:2,
4700
     bcl:1,
4701
     add:1,
4702
     pttid:2;
4703
} memory[200];
4704

    
4705
#seekto 0x0E00;
4706
struct {
4707
  u8 tmr;
4708
  u8 unknownE01;
4709
  u8 sql;
4710
  u8 unknownE03[2];
4711
  u8 tot;
4712
  u8 unusedE06:7,
4713
     save:1;
4714
  u8 unknownE07;
4715
  u8 abr;
4716
  u8 unusedE09:7,
4717
     beep:1;
4718
  u8 unknownE0A[4];
4719
  u8 unusedE0E:7,
4720
     dsub:1;
4721
  u8 unusedE0F:7,
4722
     dtmfst:1;
4723
  u8 screv;
4724
  u8 unknownE11[3];
4725
  u8 pttid;
4726
  u8 unknownE15;
4727
  u8 pttlt;
4728
  u8 unknownE17;
4729
  u8 emctp;
4730
  u8 emcch;
4731
  u8 unusedE1A:7,
4732
     sigbp:1;
4733
  u8 unknownE1B;
4734
  u8 camdf;
4735
  u8 cbmdf;
4736
  u8 ccmdf;
4737
  u8 cdmdf;
4738
  u8 langua;
4739
  u8 voice;
4740
  u8 vox;
4741
  u8 voxt;
4742
  u8 sync;          // BTech radios use this as the display sync setting
4743
                    // other radios use this as the auto keypad lock setting
4744
  u8 stfc;
4745
  u8 mffc;
4746
  u8 sfafc;
4747
  u8 sfbfc;
4748
  u8 sfcfc;
4749
  u8 sfdfc;
4750
  u8 subfc;
4751
  u8 fmfc;
4752
  u8 sigfc;
4753
  u8 menufc;
4754
  u8 txfc;
4755
  u8 rxfc;
4756
  u8 unknownE31[5];
4757
  u8 anil;
4758
  u8 reps;
4759
  u8 tmrmr;
4760
  u8 unusedE39:7,
4761
     ste:1;
4762
  u8 rpste;
4763
  u8 rptdl;
4764
  u8 dtmfg;
4765
  u8 tmrtx;
4766
} settings;
4767

    
4768
#seekto 0x0E80;
4769
struct {
4770
  u8 unknown1;
4771
  u8 vfomr;
4772
  u8 unusedE82:7,
4773
     keylock:1;
4774
  u8 unknown2;
4775
  u8 unknown3:4,
4776
     vfomren:1,
4777
     unknown4:1,
4778
     reseten:1,
4779
     menuen:1;
4780
  u8 unknown5[11];
4781
  u8 dispab;
4782
  u8 unknown6[2];
4783
  u8 menu;
4784
  u8 unknown7[7];
4785
  u8 vfomra;
4786
  u8 vfomrb;
4787
  u8 vfomrc;
4788
  u8 vfomrd;
4789
  u8 mrcha;
4790
  u8 mrchb;
4791
  u8 mrchc;
4792
  u8 mrchd;
4793
} settings2;
4794

    
4795
struct settings_vfo {
4796
  u8 freq[8];
4797
  u8 offset[6];
4798
  u8 unknown2[2];
4799
  ul16 rxtone;
4800
  ul16 txtone;
4801
  u8 scode;
4802
  u8 spmute;
4803
  u8 optsig;
4804
  u8 unused1:7,
4805
     scramble:1;
4806
  u8 wide;
4807
  u8 power;
4808
  u8 shiftd;
4809
  u8 step;
4810
  u8 unknown3[4];
4811
};
4812

    
4813
#seekto 0x0F00;
4814
struct {
4815
  struct settings_vfo a;
4816
  struct settings_vfo b;
4817
  struct settings_vfo c;
4818
  struct settings_vfo d;
4819
} vfo;
4820

    
4821
#seekto 0x0FE0;
4822
struct {
4823
  char line[16];
4824
} static_msg;
4825

    
4826
#seekto 0x1000;
4827
struct {
4828
  char name[8];
4829
  u8 unknown1[8];
4830
} names[200];
4831

    
4832
#seekto 0x2400;
4833
struct {
4834
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4835
  u8 group_tone;
4836
  u8 repeat_tone;
4837
  u8 unused[13];
4838
} _5tone_std_settings[15];
4839

    
4840
#seekto 0x2500;
4841
struct {
4842
  u8 frame1[5];
4843
  u8 frame2[5];
4844
  u8 frame3[5];
4845
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4846
} _5tone_codes[15];
4847

    
4848
#seekto 0x25F0;
4849
struct {
4850
  u8 _5tone_delay1; // * 10ms
4851
  u8 _5tone_delay2; // * 10ms
4852
  u8 _5tone_delay3; // * 10ms
4853
  u8 _5tone_first_digit_ext_length;
4854
  u8 unknown1;
4855
  u8 unknown2;
4856
  u8 unknown3;
4857
  u8 unknown4;
4858
  u8 decode_standard;
4859
  u8 unknown5:5,
4860
     _5tone_decode_call_frame3:1,
4861
     _5tone_decode_call_frame2:1,
4862
     _5tone_decode_call_frame1:1;
4863
  u8 unknown6:5,
4864
     _5tone_decode_disp_frame3:1,
4865
     _5tone_decode_disp_frame2:1,
4866
     _5tone_decode_disp_frame1:1;
4867
  u8 decode_reset_time; // * 100 + 100ms
4868
} _5tone_settings;
4869

    
4870
#seekto 0x2900;
4871
struct {
4872
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4873
} dtmf_codes[15];
4874

    
4875
#seekto 0x29F0;
4876
struct {
4877
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10
4878
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10
4879
  u8 unknown0[14];
4880
  u8 inspection[16];
4881
  u8 monitor[16];
4882
  u8 alarmcode[16];
4883
  u8 stun[16];
4884
  u8 kill[16];
4885
  u8 revive[16];
4886
  u8 unknown1[16];
4887
  u8 unknown2[16];
4888
  u8 unknown3[16];
4889
  u8 unknown4[16];
4890
  u8 unknown5[16];
4891
  u8 unknown6[16];
4892
  u8 unknown7[16];
4893
  u8 masterid[16];
4894
  u8 viceid[16];
4895
  u8 unused01:7,
4896
     mastervice:1;
4897
  u8 unused02:3,
4898
     mrevive:1,
4899
     mkill:1,
4900
     mstun:1,
4901
     mmonitor:1,
4902
     minspection:1;
4903
  u8 unused03:3,
4904
     vrevive:1,
4905
     vkill:1,
4906
     vstun:1,
4907
     vmonitor:1,
4908
     vinspection:1;
4909
  u8 unused04:6,
4910
     txdisable:1,
4911
     rxdisable:1;
4912
  u8 groupcode;
4913
  u8 spacecode;
4914
  u8 delayproctime; // * 100 + 100ms
4915
  u8 resettime;     // * 100 + 100ms
4916
} dtmf_settings;
4917

    
4918
#seekto 0x2D00;
4919
struct {
4920
  struct {
4921
    ul16 freq1;
4922
    u8 unused01[6];
4923
    ul16 freq2;
4924
    u8 unused02[6];
4925
  } _2tone_encode[15];
4926
  u8 duration_1st_tone; // *10ms
4927
  u8 duration_2nd_tone; // *10ms
4928
  u8 duration_gap;      // *10ms
4929
  u8 unused03[13];
4930
  struct {
4931
    struct {
4932
      u8 dec;      // one out of LIST_2TONE_DEC
4933
      u8 response; // one out of LIST_2TONE_RESPONSE
4934
      u8 alert;    // 1-16
4935
    } decs[4];
4936
    u8 unused04[4];
4937
  } _2tone_decode[15];
4938
  u8 unused05[16];
4939

    
4940
  struct {
4941
    ul16 freqA;
4942
    ul16 freqB;
4943
    ul16 freqC;
4944
    ul16 freqD;
4945
    // unknown what those values mean, but they are
4946
    // derived from configured frequencies
4947
    ul16 derived_from_freqA; // 2304000/freqA
4948
    ul16 derived_from_freqB; // 2304000/freqB
4949
    ul16 derived_from_freqC; // 2304000/freqC
4950
    ul16 derived_from_freqD; // 2304000/freqD
4951
  }freqs[15];
4952
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
4953
} _2tone;
4954

    
4955
#seekto 0x3D80;
4956
struct {
4957
  u8 vhf_low[3];
4958
  u8 vhf_high[3];
4959
  u8 unknown1[4];
4960
  u8 unknown2[6];
4961
  u8 vhf2_low[3];
4962
  u8 vhf2_high[3];
4963
  u8 unknown3[4];
4964
  u8 unknown4[6];
4965
  u8 uhf_low[3];
4966
  u8 uhf_high[3];
4967
  u8 unknown5[4];
4968
  u8 unknown6[6];
4969
  u8 uhf2_low[3];
4970
  u8 uhf2_high[3];
4971
} ranges;
4972

    
4973
#seekto 0x3F70;
4974
struct {
4975
  char fp[6];
4976
} fingerprint;
4977

    
4978
"""
4979

    
4980

    
4981
class QYTColorHT(BTechMobileCommon):
4982
    """QTY's Color LCD Handheld and alike radios"""
4983
    COLOR_LCD = True
4984
    COLOR_LCD3 = True
4985
    NAME_LENGTH = 8
4986
    LIST_TMR = LIST_TMR15
4987

    
4988
    def process_mmap(self):
4989
        """Process the mem map into the mem object"""
4990

    
4991
        # Get it
4992
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4993

    
4994
        # load specific parameters from the radio image
4995
        self.set_options()
4996

    
4997
    def set_options(self):
4998
        """This is to read the options from the image and set it in the
4999
        environment, for now just the limits of the freqs in the VHF/UHF
5000
        ranges"""
5001

    
5002
        # setting the correct ranges for each radio type
5003
        ranges = self._memobj.ranges
5004

    
5005
        # the normal dual bands
5006
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
5007
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
5008

    
5009
        # DEBUG
5010
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
5011
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
5012

    
5013
        # the additional bands
5014
        if self.MODEL in ["KT-8R"]:
5015
            # 200 MHz band
5016
            vhf2 = _decode_ranges(ranges.vhf2_low, ranges.vhf2_high)
5017
            LOG.info("Radio ranges: VHF(220) %d to %d" % vhf2)
5018
            self._220_range = vhf2
5019

    
5020
            # 350 MHz band
5021
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
5022
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
5023
            self._350_range = uhf2
5024

    
5025
        # set the class with the real data
5026
        self._vhf_range = vhf
5027
        self._uhf_range = uhf
5028

    
5029

    
5030
# real radios
5031
@directory.register
5032
class KT8R(QYTColorHT):
5033
    """QYT KT8R"""
5034
    VENDOR = "QYT"
5035
    MODEL = "KT-8R"
5036
    BANDS = 4
5037
    LIST_TMR = LIST_TMR16
5038
    _vhf_range = (136000000, 175000000)
5039
    _220_range = (200000000, 261000000)
5040
    _uhf_range = (400000000, 481000000)
5041
    _350_range = (350000000, 391000000)
5042
    _magic = MSTRING_KT8R
5043
    _fileid = [KT8R_fp3, KT8R_fp2, KT8R_fp1, KT8R_fp]
5044
    _power_levels = [chirp_common.PowerLevel("High", watts=5),
5045
                     chirp_common.PowerLevel("Low", watts=1)]
5046

    
5047

    
5048
COLOR9900_MEM_FORMAT = """
5049
#seekto 0x0000;
5050
struct {
5051
  lbcd rxfreq[4];
5052
  lbcd txfreq[4];
5053
  ul16 rxtone;
5054
  ul16 txtone;
5055
  u8 unknown0:4,
5056
     scode:4;
5057
  u8 unknown1:2,
5058
     spmute:2,
5059
     unknown2:2,
5060
     optsig:2;
5061
  u8 unknown3:3,
5062
     scramble:1,
5063
     unknown4:2,
5064
     power:2;
5065
  u8 unknown5:1,
5066
     wide:1,
5067
     unknown6:2,
5068
     bcl:1,
5069
     add:1,
5070
     pttid:2;
5071
} memory[200];
5072

    
5073
#seekto 0x0E00;
5074
struct {
5075
  u8 tmr;
5076
  u8 unknown1;
5077
  u8 sql;
5078
  u8 unknown2[2];
5079
  u8 tot;
5080
  u8 volume;
5081
  u8 unknown3;
5082
  u8 abr;
5083
  u8 unusedE09:7,
5084
     beep:1;
5085
  u8 unknown4[4];
5086
  u8 unusedE0E:7,
5087
     dsub:1;
5088
  u8 unusedE0F:7,
5089
     dtmfst:1;
5090
  u8 unknown_e10;
5091
  u8 unknown_e11;
5092
  u8 screv;
5093
  u8 unknown_e13;
5094
  u8 unknown_e14;
5095
  u8 pttid;
5096
  u8 pttlt;
5097
  u8 unknown7;
5098
  u8 emctp;
5099
  u8 emcch;
5100
  u8 unusedE1A:7,
5101
     sigbp:1;
5102
  u8 unknown8;
5103
  u8 camdf;
5104
  u8 cbmdf;
5105
  u8 ccmdf;
5106
  u8 language;
5107
  u8 tmrtx;
5108
  u8 vox;
5109
  u8 voxt;
5110
  u8 unusedE23:7,
5111
     autolock:1;
5112
  u8 asfc;
5113
  u8 mainfc;
5114
  u8 a_fc;
5115
  u8 b_fc;
5116
  u8 c_fc;
5117
  u8 subfc;
5118
  u8 battfc;
5119
  u8 sigfc;
5120
  u8 menufc;
5121
  u8 txfc;
5122
  u8 rxfc;
5123
  u8 unknown_e2f;
5124
  u8 unknown_e30;
5125
  u8 unknown9[3];
5126
  u8 anil;
5127
  u8 reps;
5128
  u8 tmrmr;
5129
  u8 unusedE37:7,
5130
     ste:1;
5131
  u8 rpste;
5132
  u8 rptdl;
5133
  u8 dtmfg;
5134
} settings;
5135

    
5136
#seekto 0x0E80;
5137
struct {
5138
  u8 unknown1;
5139
  u8 vfomr;
5140
  u8 unusedE82:7,
5141
     keylock:1;
5142
  u8 unknown2;
5143
  u8 unknown3:4,
5144
     vfomren:1,
5145
     unknown4:1,
5146
     reseten:1,
5147
     menuen:1;
5148
  u8 unknown5[11];
5149
  u8 dispab;
5150
  u8 unknown6[2];
5151
  u8 menu;
5152
  u8 unknown7[7];
5153
  u8 vfomra;
5154
  u8 vfomrb;
5155
  u8 vfomrc;
5156
  u8 vfomrd;
5157
  u8 mrcha;
5158
  u8 mrchb;
5159
  u8 mrchc;
5160
  u8 mrchd;
5161
} settings2;
5162

    
5163
struct settings_vfo {
5164
  u8 freq[8];
5165
  u8 offset[6];
5166
  u8 unknown2[2];
5167
  ul16 rxtone;
5168
  ul16 txtone;
5169
  u8 scode;
5170
  u8 spmute;
5171
  u8 optsig;
5172
  u8 unused1:7,
5173
     scramble:1;
5174
  u8 wide;
5175
  u8 power;
5176
  u8 shiftd;
5177
  u8 step;
5178
  u8 unknown3[4];
5179
};
5180

    
5181
#seekto 0x0F00;
5182
struct {
5183
  struct settings_vfo a;
5184
  struct settings_vfo b;
5185
  struct settings_vfo c;
5186
  struct settings_vfo d;
5187
} vfo;
5188

    
5189
#seekto 0x0F80;
5190
struct {
5191
  char line1[8];
5192
  char line2[8];
5193
  char line3[8];
5194
  char line4[8];
5195
  char line5[8];
5196
  char line6[8];
5197
  char line7[8];
5198
  char line8[8];
5199
} poweron_msg;
5200

    
5201
#seekto 0x0FE0;
5202
struct {
5203
  char line[16];
5204
} static_msg;
5205

    
5206
#seekto 0x1000;
5207
struct {
5208
  char name[7];
5209
  u8 unknown1[9];
5210
} names[200];
5211

    
5212
#seekto 0x2400;
5213
struct {
5214
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
5215
  u8 group_tone;
5216
  u8 repeat_tone;
5217
  u8 unused[13];
5218
} _5tone_std_settings[15];
5219

    
5220
#seekto 0x2500;
5221
struct {
5222
  u8 frame1[5];
5223
  u8 frame2[5];
5224
  u8 frame3[5];
5225
  u8 standard;   // one out of LIST_5TONE_STANDARDS
5226
} _5tone_codes[15];
5227

    
5228
#seekto 0x25F0;
5229
struct {
5230
  u8 _5tone_delay1; // * 10ms
5231
  u8 _5tone_delay2; // * 10ms
5232
  u8 _5tone_delay3; // * 10ms
5233
  u8 _5tone_first_digit_ext_length;
5234
  u8 unknown1;
5235
  u8 unknown2;
5236
  u8 unknown3;
5237
  u8 unknown4;
5238
  u8 decode_standard;
5239
  u8 unknown5:5,
5240
     _5tone_decode_call_frame3:1,
5241
     _5tone_decode_call_frame2:1,
5242
     _5tone_decode_call_frame1:1;
5243
  u8 unknown6:5,
5244
     _5tone_decode_disp_frame3:1,
5245
     _5tone_decode_disp_frame2:1,
5246
     _5tone_decode_disp_frame1:1;
5247
  u8 decode_reset_time; // * 100 + 100ms
5248
} _5tone_settings;
5249

    
5250
#seekto 0x2900;
5251
struct {
5252
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
5253
} dtmf_codes[15];
5254

    
5255
#seekto 0x29F0;
5256
struct {
5257
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10      // 9f0
5258
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10      // 9f1
5259
  u8 unknown0[14];                                             // 9f2-9ff
5260
  u8 inspection[16];                                           // a00-a0f
5261
  u8 monitor[16];                                              // a10-a1f
5262
  u8 alarmcode[16];                                            // a20-a2f
5263
  u8 stun[16];                                                 // a30-a3f
5264
  u8 kill[16];                                                 // a40-a4f
5265
  u8 revive[16];                                               // a50-a5f
5266
  u8 unknown1[16];                                             // a60-a6f
5267
  u8 unknown2[16];                                             // a70-a7f
5268
  u8 unknown3[16];                                             // a80-a8f
5269
  u8 unknown4[16];                                             // a90-a9f
5270
  u8 unknown5[16];                                             // aa0-aaf
5271
  u8 unknown6[16];                                             // ab0-abf
5272
  u8 unknown7[16];                                             // ac0-acf
5273
  u8 masterid[16];                                             // ad0-adf
5274
  u8 viceid[16];                                               // ae0-aef
5275
  u8 unused01:7,                                               // af0
5276
     mastervice:1;
5277
  u8 unused02:3,                                               // af1
5278
     mrevive:1,
5279
     mkill:1,
5280
     mstun:1,
5281
     mmonitor:1,
5282
     minspection:1;
5283
  u8 unused03:3,                                               // af2
5284
     vrevive:1,
5285
     vkill:1,
5286
     vstun:1,
5287
     vmonitor:1,
5288
     vinspection:1;
5289
  u8 unused04:6,                                               // af3
5290
     txdisable:1,
5291
     rxdisable:1;
5292
  u8 groupcode;                                                // af4
5293
  u8 spacecode;                                                // af5
5294
  u8 delayproctime; // * 100 + 100ms                           // af6
5295
  u8 resettime;     // * 100 + 100ms                           // af7
5296
} dtmf_settings;
5297

    
5298
#seekto 0x2D00;
5299
struct {
5300
  struct {
5301
    ul16 freq1;
5302
    u8 unused01[6];
5303
    ul16 freq2;
5304
    u8 unused02[6];
5305
  } _2tone_encode[15];
5306
  u8 duration_1st_tone; // *10ms
5307
  u8 duration_2nd_tone; // *10ms
5308
  u8 duration_gap;      // *10ms
5309
  u8 unused03[13];
5310
  struct {
5311
    struct {
5312
      u8 dec;      // one out of LIST_2TONE_DEC
5313
      u8 response; // one out of LIST_2TONE_RESPONSE
5314
      u8 alert;    // 1-16
5315
    } decs[4];
5316
    u8 unused04[4];
5317
  } _2tone_decode[15];
5318
  u8 unused05[16];
5319

    
5320
  struct {
5321
    ul16 freqA;
5322
    ul16 freqB;
5323
    ul16 freqC;
5324
    ul16 freqD;
5325
    // unknown what those values mean, but they are
5326
    // derived from configured frequencies
5327
    ul16 derived_from_freqA; // 2304000/freqA
5328
    ul16 derived_from_freqB; // 2304000/freqB
5329
    ul16 derived_from_freqC; // 2304000/freqC
5330
    ul16 derived_from_freqD; // 2304000/freqD
5331
  }freqs[15];
5332
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
5333
} _2tone;
5334

    
5335
#seekto 0x3D80;
5336
struct {
5337
  u8 vhf_low[3];
5338
  u8 vhf_high[3];
5339
  u8 unknown1[4];
5340
  u8 unknown2[6];
5341
  u8 vhf2_low[3];
5342
  u8 vhf2_high[3];
5343
  u8 unknown3[4];
5344
  u8 unknown4[6];
5345
  u8 uhf_low[3];
5346
  u8 uhf_high[3];
5347
  u8 unknown5[4];
5348
  u8 unknown6[6];
5349
  u8 uhf2_low[3];
5350
  u8 uhf2_high[3];
5351
} ranges;
5352

    
5353
#seekto 0x3F70;
5354
struct {
5355
  char fp[6];
5356
} fingerprint;
5357

    
5358
"""
5359

    
5360

    
5361
COLOR20V2_MEM_FORMAT = """
5362
#seekto 0x0000;
5363
struct {
5364
  lbcd rxfreq[4];
5365
  lbcd txfreq[4];
5366
  ul16 rxtone;
5367
  ul16 txtone;
5368
  u8 unknown0:4,
5369
     scode:4;
5370
  u8 unknown1:2,
5371
     spmute:2,
5372
     unknown2:2,
5373
     optsig:2;
5374
  u8 unknown3:3,
5375
     scramble:1,
5376
     unknown4:2,
5377
     power:2;
5378
  u8 unknown5:1,
5379
     wide:1,
5380
     unknown6:2,
5381
     bcl:1,
5382
     add:1,
5383
     pttid:2;
5384
} memory[200];
5385

    
5386
#seekto 0x0E00;
5387
struct {
5388
  u8 tmr;
5389
  u8 unknown1;
5390
  u8 sql;
5391
  u8 unknown2;
5392
  u8 unusedE04:7,
5393
     autolock:1;
5394
  u8 tot;
5395
  u8 apo;
5396
  u8 unknown3;
5397
  u8 abr;
5398
  u8 unusedE09:7,
5399
     beep:1;
5400
  u8 unknown4[4];
5401
  u8 unusedE0E:7,
5402
     dtmfst:1;
5403
  u8 unknown5[2];
5404
  u8 screv;
5405
  u8 unknown6[2];
5406
  u8 pttid;
5407
  u8 pttlt;
5408
  u8 unknown7;
5409
  u8 emctp;
5410
  u8 emcch;
5411
  u8 unusedE19:7,
5412
     sigbp:1;
5413
  u8 unknown8;
5414
  u8 camdf;
5415
  u8 cbmdf;
5416
  u8 ccmdf;
5417
  u8 vox;
5418
  u8 voxt;
5419
  u8 sync;
5420
  u8 asfc;
5421
  u8 mainfc;
5422
  u8 a_fc;
5423
  u8 b_fc;
5424
  u8 c_fc;
5425
  u8 subfc;
5426
  u8 battfc;
5427
  u8 sigfc;
5428
  u8 menufc;
5429
  u8 txfc;
5430
  u8 rxfc;
5431
  u8 repsw;
5432
  u8 unusedE2D:7,
5433
     dsub:1;
5434
  u8 unknown9[5];
5435
  u8 anil;
5436
  u8 reps;
5437
  u8 repm;
5438
  u8 tmrmr;
5439
  u8 unusedE37:7,
5440
     ste:1;
5441
  u8 rpste;
5442
  u8 rptdl;
5443
  u8 dtmfg;
5444
  u8 mgain;
5445
  u8 skiptx;
5446
  u8 scmode;
5447
  u8 tmrtx;
5448
  u8 volume;
5449
  u8 unknown_10;
5450
  u8 unusedE41:7,
5451
     save:1;
5452
} settings;
5453

    
5454
#seekto 0x0E80;
5455
struct {
5456
  u8 unknown1;
5457
  u8 vfomr;
5458
  u8 unusedE82:7,
5459
     keylock:1;
5460
  u8 unknown2;
5461
  u8 unknown3:4,
5462
     vfomren:1,
5463
     unknown4:1,
5464
     reseten:1,
5465
     menuen:1;
5466
  u8 unknown5[11];
5467
  u8 dispab;
5468
  u8 unknown6[2];
5469
  u8 menu;
5470
  u8 unknown7[7];
5471
  u8 vfomra;
5472
  u8 vfomrb;
5473
  u8 vfomrc;
5474
  u8 vfomrd;
5475
  u8 mrcha;
5476
  u8 mrchb;
5477
  u8 mrchc;
5478
  u8 mrchd;
5479
} settings2;
5480

    
5481
struct settings_vfo {
5482
  u8 freq[8];
5483
  u8 offset[6];
5484
  u8 unknown2[2];
5485
  ul16 rxtone;
5486
  ul16 txtone;
5487
  u8 scode;
5488
  u8 spmute;
5489
  u8 optsig;
5490
  u8 unused1:7,
5491
     scramble:1;
5492
  u8 wide;
5493
  u8 power;
5494
  u8 shiftd;
5495
  u8 step;
5496
  u8 unknown3[4];
5497
};
5498

    
5499
#seekto 0x0F00;
5500
struct {
5501
  struct settings_vfo a;
5502
  struct settings_vfo b;
5503
  struct settings_vfo c;
5504
  struct settings_vfo d;
5505
} vfo;
5506

    
5507
#seekto 0x0F80;
5508
struct {
5509
  char line1[8];
5510
  char line2[8];
5511
  char line3[8];
5512
  char line4[8];
5513
  char line5[8];
5514
  char line6[8];
5515
  char line7[8];
5516
  char line8[8];
5517
} poweron_msg;
5518

    
5519
#seekto 0x0FE0;
5520
struct {
5521
  char line[16];
5522
} static_msg;
5523

    
5524
#seekto 0x1000;
5525
struct {
5526
  char name[7];
5527
  u8 unknown1[9];
5528
} names[200];
5529

    
5530
#seekto 0x2400;
5531
struct {
5532
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
5533
  u8 group_tone;
5534
  u8 repeat_tone;
5535
  u8 unused[13];
5536
} _5tone_std_settings[15];
5537

    
5538
#seekto 0x2500;
5539
struct {
5540
  u8 frame1[5];
5541
  u8 frame2[5];
5542
  u8 frame3[5];
5543
  u8 standard;   // one out of LIST_5TONE_STANDARDS
5544
} _5tone_codes[15];
5545

    
5546
#seekto 0x25F0;
5547
struct {
5548
  u8 _5tone_delay1; // * 10ms
5549
  u8 _5tone_delay2; // * 10ms
5550
  u8 _5tone_delay3; // * 10ms
5551
  u8 _5tone_first_digit_ext_length;
5552
  u8 unknown1;
5553
  u8 unknown2;
5554
  u8 unknown3;
5555
  u8 unknown4;
5556
  u8 decode_standard;
5557
  u8 unknown5:5,
5558
     _5tone_decode_call_frame3:1,
5559
     _5tone_decode_call_frame2:1,
5560
     _5tone_decode_call_frame1:1;
5561
  u8 unknown6:5,
5562
     _5tone_decode_disp_frame3:1,
5563
     _5tone_decode_disp_frame2:1,
5564
     _5tone_decode_disp_frame1:1;
5565
  u8 decode_reset_time; // * 100 + 100ms
5566
} _5tone_settings;
5567

    
5568
#seekto 0x2900;
5569
struct {
5570
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
5571
} dtmf_codes[15];
5572

    
5573
#seekto 0x29F0;
5574
struct {
5575
  u8 dtmfspeed_on;  //list with 50..2000ms in steps of 10      // 9f0
5576
  u8 dtmfspeed_off; //list with 50..2000ms in steps of 10      // 9f1
5577
  u8 unknown0[14];                                             // 9f2-9ff
5578
  u8 inspection[16];                                           // a00-a0f
5579
  u8 monitor[16];                                              // a10-a1f
5580
  u8 alarmcode[16];                                            // a20-a2f
5581
  u8 stun[16];                                                 // a30-a3f
5582
  u8 kill[16];                                                 // a40-a4f
5583
  u8 revive[16];                                               // a50-a5f
5584
  u8 unknown1[16];                                             // a60-a6f
5585
  u8 unknown2[16];                                             // a70-a7f
5586
  u8 unknown3[16];                                             // a80-a8f
5587
  u8 unknown4[16];                                             // a90-a9f
5588
  u8 unknown5[16];                                             // aa0-aaf
5589
  u8 unknown6[16];                                             // ab0-abf
5590
  u8 unknown7[16];                                             // ac0-acf
5591
  u8 masterid[16];                                             // ad0-adf
5592
  u8 viceid[16];                                               // ae0-aef
5593
  u8 unused01:7,                                               // af0
5594
     mastervice:1;
5595
  u8 unused02:3,                                               // af1
5596
     mrevive:1,
5597
     mkill:1,
5598
     mstun:1,
5599
     mmonitor:1,
5600
     minspection:1;
5601
  u8 unused03:3,                                               // af2
5602
     vrevive:1,
5603
     vkill:1,
5604
     vstun:1,
5605
     vmonitor:1,
5606
     vinspection:1;
5607
  u8 unused04:6,                                               // af3
5608
     txdisable:1,
5609
     rxdisable:1;
5610
  u8 groupcode;                                                // af4
5611
  u8 spacecode;                                                // af5
5612
  u8 delayproctime; // * 100 + 100ms                           // af6
5613
  u8 resettime;     // * 100 + 100ms                           // af7
5614
} dtmf_settings;
5615

    
5616
#seekto 0x2D00;
5617
struct {
5618
  struct {
5619
    ul16 freq1;
5620
    u8 unused01[6];
5621
    ul16 freq2;
5622
    u8 unused02[6];
5623
  } _2tone_encode[15];
5624
  u8 duration_1st_tone; // *10ms
5625
  u8 duration_2nd_tone; // *10ms
5626
  u8 duration_gap;      // *10ms
5627
  u8 unused03[13];
5628
  struct {
5629
    struct {
5630
      u8 dec;      // one out of LIST_2TONE_DEC
5631
      u8 response; // one out of LIST_2TONE_RESPONSE
5632
      u8 alert;    // 1-16
5633
    } decs[4];
5634
    u8 unused04[4];
5635
  } _2tone_decode[15];
5636
  u8 unused05[16];
5637

    
5638
  struct {
5639
    ul16 freqA;
5640
    ul16 freqB;
5641
    ul16 freqC;
5642
    ul16 freqD;
5643
    // unknown what those values mean, but they are
5644
    // derived from configured frequencies
5645
    ul16 derived_from_freqA; // 2304000/freqA
5646
    ul16 derived_from_freqB; // 2304000/freqB
5647
    ul16 derived_from_freqC; // 2304000/freqC
5648
    ul16 derived_from_freqD; // 2304000/freqD
5649
  }freqs[15];
5650
  u8 reset_time;  // * 100 + 100ms - 100-8000ms
5651
} _2tone;
5652

    
5653
#seekto 0x3D80;
5654
struct {
5655
  u8 vhf_low[3];
5656
  u8 vhf_high[3];
5657
  u8 unknown1[4];
5658
  u8 unknown2[6];
5659
  u8 vhf2_low[3];
5660
  u8 vhf2_high[3];
5661
  u8 unknown3[4];
5662
  u8 unknown4[6];
5663
  u8 uhf_low[3];
5664
  u8 uhf_high[3];
5665
  u8 unknown5[4];
5666
  u8 unknown6[6];
5667
  u8 uhf2_low[3];
5668
  u8 uhf2_high[3];
5669
} ranges;
5670

    
5671
#seekto 0x3F70;
5672
struct {
5673
  char fp[6];
5674
} fingerprint;
5675

    
5676
"""
5677

    
5678

    
5679
class BTechColorWP(BTechMobileCommon):
5680
    """BTECH's Color WP Mobile and alike radios"""
5681
    COLOR_LCD = True
5682
    COLOR_LCD4 = True
5683
    NAME_LENGTH = 7
5684
    LIST_TMR = LIST_TMR7
5685

    
5686
    def set_options(self):
5687
        """This is to read the options from the image and set it in the
5688
        environment, for now just the limits of the freqs in the VHF/UHF
5689
        ranges"""
5690

    
5691
        # setting the correct ranges for each radio type
5692
        ranges = self._memobj.ranges
5693

    
5694
        # the normal dual bands
5695
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
5696
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
5697

    
5698
        # DEBUG
5699
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
5700
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
5701

    
5702
        # set the class with the real data
5703
        self._vhf_range = vhf
5704
        self._uhf_range = uhf
5705

    
5706

    
5707
# real radios
5708
@directory.register
5709
class KTWP12(BTechColorWP):
5710
    """QYT KT-WP12"""
5711
    VENDOR = "QYT"
5712
    MODEL = "KT-WP12"
5713
    BANDS = 2
5714
    UPLOAD_MEM_SIZE = 0X3100
5715
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
5716
                     chirp_common.PowerLevel("Low", watts=5)]
5717
    _upper = 199
5718
    _magic = MSTRING_KTWP12
5719
    _fileid = [KTWP12_fp]
5720
    _gmrs = False
5721

    
5722
    def process_mmap(self):
5723
        """Process the mem map into the mem object"""
5724

    
5725
        # Get it
5726
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5727

    
5728
        # load specific parameters from the radio image
5729
        self.set_options()
5730

    
5731

    
5732
@directory.register
5733
class WP9900(BTechColorWP):
5734
    """Anysecu WP-9900"""
5735
    VENDOR = "Anysecu"
5736
    MODEL = "WP-9900"
5737
    BANDS = 2
5738
    UPLOAD_MEM_SIZE = 0X3100
5739
    _power_levels = [chirp_common.PowerLevel("High", watts=25),
5740
                     chirp_common.PowerLevel("Low", watts=5)]
5741
    _upper = 199
5742
    _magic = MSTRING_KTWP12
5743
    _fileid = [WP9900_fp]
5744
    _gmrs = False
5745

    
5746
    def process_mmap(self):
5747
        """Process the mem map into the mem object"""
5748

    
5749
        # Get it
5750
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5751

    
5752
        # load specific parameters from the radio image
5753
        self.set_options()
5754

    
5755

    
5756
@directory.register
5757
class GMRS20V2(BTechColorWP):
5758
    """Baofeng Tech GMRS-20V2"""
5759
    MODEL = "GMRS-20V2"
5760
    BANDS = 2
5761
    UPLOAD_MEM_SIZE = 0X3100
5762
    _power_levels = [chirp_common.PowerLevel("High", watts=20),
5763
                     chirp_common.PowerLevel("", watts=6),
5764
                     chirp_common.PowerLevel("Low", watts=5)]
5765
    _upper = 199
5766
    _magic = MSTRING_GMRS20V2
5767
    _fileid = [GMRS20V2_fp2, GMRS20V2_fp1, GMRS20V2_fp]
5768
    _gmrs = True
5769

    
5770
    def validate_memory(self, mem):
5771
        msgs = super().validate_memory(mem)
5772

    
5773
        _msg_duplex = 'Duplex must be "off" for this frequency'
5774
        _msg_offset = 'Only simplex or +5 MHz offset allowed on GMRS'
5775

    
5776
        if mem.freq not in GMRS_FREQS:
5777
            if mem.duplex != "off":
5778
                msgs.append(chirp_common.ValidationWarning(_msg_duplex))
5779
        elif mem.duplex:
5780
            if mem.duplex and mem.offset != 5000000:
5781
                msgs.append(chirp_common.ValidationWarning(_msg_offset))
5782

    
5783
        return msgs
5784

    
5785
    def check_set_memory_immutable_policy(self, existing, new):
5786
        existing.immutable = []
5787
        super().check_set_memory_immutable_policy(existing, new)
5788

    
5789
    def process_mmap(self):
5790
        """Process the mem map into the mem object"""
5791

    
5792
        # Get it
5793
        self._memobj = bitwise.parse(COLOR20V2_MEM_FORMAT, self._mmap)
5794

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