Project

General

Profile

Bug #11357 ยป btech - vc3062.py

Jim Unroe, 05/22/2024 08:19 AM

 
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
import struct
19
import logging
20

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

    
30
LOG = logging.getLogger(__name__)
31

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

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

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

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

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

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

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

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

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

    
160

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

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

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

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

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

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

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

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

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

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

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

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

    
219

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

    
223

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

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

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

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

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

    
266
# KT8900D (dual band)
267
KT8900D_fp = b"VC2002"
268
KT8900D_fp1 = b"VC8632"
269
KT8900D_fp2 = b"VC3402"
270
KT8900D_fp3 = b"VC7062"
271
KT8900D_fp4 = b"VC3062"
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

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

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

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

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

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

    
300

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

    
326

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

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

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

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

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

    
353

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

    
357
    data = b""
358

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

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

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

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

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

    
379
    return data
380

    
381

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

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

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

    
395

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

    
403
    return frame
404

    
405

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

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

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

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

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

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

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

    
433

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

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

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

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

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

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

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

    
467
            break
468

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

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

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

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

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

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

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

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

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

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

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

    
523
    return True
524

    
525

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

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

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

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

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

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

    
549
        # aggregate the data
550
        data += d
551

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

    
557
    return data
558

    
559

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
613

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

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

    
621
    return False
622

    
623

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

    
632
    return (ilow, ihigh)
633

    
634

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

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

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

    
649

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

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

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

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

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

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

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

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

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

    
754
        return rf
755

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

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

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

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

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

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

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

    
819
    def _is_txinh(self, _mem):
820
        raw_tx = b""
821
        for i in range(0, 4):
822
            raw_tx += _mem.txfreq[i].get_raw()
823
        return raw_tx == b"\xFF\xFF\xFF\xFF"
824

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

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

    
833
        # Memory number
834
        mem.number = number
835

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
921
        immutable = []
922

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

    
1001
        mem.immutable = immutable
1002

    
1003
        return mem
1004

    
1005
    def set_memory(self, mem):
1006
        """Set the memory data in the eeprom img from the UI"""
1007
        # get the eprom representation of this channel
1008
        _mem = self._memobj.memory[mem.number]
1009
        _names = self._memobj.names[mem.number]
1010

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

    
1018
        # if empty memory
1019
        if mem.empty:
1020
            # the channel itself
1021
            _mem.set_raw("\xFF" * 16)
1022
            # the name tag
1023
            _names.set_raw("\xFF" * 16)
1024
            return
1025

    
1026
        if mem_was_empty:
1027
            # Zero the whole memory if we're making it unempty for
1028
            # the first time
1029
            LOG.debug('Zeroing new memory')
1030
            _mem.set_raw('\x00' * 16)
1031

    
1032
        # frequency
1033
        _mem.rxfreq = mem.freq / 10
1034

    
1035
        # duplex
1036
        if mem.duplex == "+":
1037
            _mem.txfreq = (mem.freq + mem.offset) / 10
1038
        elif mem.duplex == "-":
1039
            _mem.txfreq = (mem.freq - mem.offset) / 10
1040
        elif mem.duplex == "off":
1041
            for i in _mem.txfreq:
1042
                i.set_raw("\xFF")
1043
        elif mem.duplex == "split":
1044
            _mem.txfreq = mem.offset / 10
1045
        else:
1046
            _mem.txfreq = mem.freq / 10
1047

    
1048
        # tone data
1049
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
1050
            chirp_common.split_tone_encode(mem)
1051
        self._encode_tone(_mem.txtone, txmode, txtone, txpol)
1052
        self._encode_tone(_mem.rxtone, rxmode, rxtone, rxpol)
1053

    
1054
        # name TAG of the channel
1055
        _names.name = mem.name.rstrip(' ').ljust(self.NAME_LENGTH, "\xFF")
1056

    
1057
        # power, # default power level is high
1058
        _mem.power = 0 if mem.power is None else POWER_LEVELS.index(mem.power)
1059

    
1060
        # wide/narrow
1061
        _mem.wide = MODES.index(mem.mode)
1062

    
1063
        # scan add property
1064
        _mem.add = SKIP_VALUES.index(mem.skip)
1065

    
1066
        # resetting unknowns, this have to be set by hand
1067
        _mem.unknown0 = 0
1068
        _mem.unknown1 = 0
1069
        _mem.unknown2 = 0
1070
        _mem.unknown3 = 0
1071
        _mem.unknown4 = 0
1072
        _mem.unknown5 = 0
1073
        _mem.unknown6 = 0
1074

    
1075
        def _zero_settings():
1076
            _mem.spmute = 0
1077
            _mem.optsig = 0
1078
            _mem.scramble = 0
1079
            _mem.bcl = 0
1080
            _mem.pttid = 0
1081
            _mem.scode = 0
1082

    
1083
        if self.COLOR_LCD and _mem.scramble:
1084
            LOG.info('Resetting scramble bit for BTECH COLOR_LCD variant')
1085
            _mem.scramble = 0
1086

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

    
1107
        return mem
1108

    
1109
    def get_settings(self):
1110
        """Translate the bit in the mem_struct into settings in the UI"""
1111
        _mem = self._memobj
1112
        basic = RadioSettingGroup("basic", "Basic Settings")
1113
        advanced = RadioSettingGroup("advanced", "Advanced Settings")
1114
        other = RadioSettingGroup("other", "Other Settings")
1115
        work = RadioSettingGroup("work", "Work Mode Settings")
1116
        top = RadioSettings(basic, advanced, other, work)
1117

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

    
1129
        val = min(_mem.settings.sql, 9)
1130
        rs = RadioSettingValueInteger(0, 9, val)
1131
        sql = RadioSetting("settings.sql", "Squelch level", rs)
1132
        basic.append(sql)
1133

    
1134
        if self.MODEL == "GMRS-50X1" or self.MODEL == "GMRS-50V2":
1135
            rs = RadioSettingValueBoolean(_mem.settings.autolk)
1136
            autolk = RadioSetting("settings.autolk", "Auto keylock", rs)
1137
            basic.append(autolk)
1138

    
1139
        if self.MODEL == "DB25-G":
1140
            val = min(_mem.settings.mgain2, 127)
1141
            rs = RadioSettingValueInteger(0, 127, val)
1142
            mgain2 = RadioSetting("settings.mgain2", "Mic gain", rs)
1143
            basic.append(mgain2)
1144

    
1145
        val = min(_mem.settings.tot, len(LIST_TOT) - 1)
1146
        rs = RadioSettingValueList(LIST_TOT, LIST_TOT[val])
1147
        tot = RadioSetting("settings.tot", "Time out timer", rs)
1148
        basic.append(tot)
1149

    
1150
        if self.MODEL == "KT-8R":
1151
            rs = RadioSettingValueBoolean(_mem.settings.save)
1152
            save = RadioSetting("settings.save", "Battery Save", rs)
1153
            basic.append(save)
1154

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

    
1168
        val = min(_mem.settings.abr, len(LIST_OFF1TO50) - 1)
1169
        rs = RadioSettingValueList(LIST_OFF1TO50, LIST_OFF1TO50[val])
1170
        abr = RadioSetting("settings.abr", "Backlight timer", rs)
1171
        basic.append(abr)
1172

    
1173
        rs = RadioSettingValueBoolean(_mem.settings.beep)
1174
        beep = RadioSetting("settings.beep", "Key beep", rs)
1175
        basic.append(beep)
1176

    
1177
        if self.MODEL == "GMRS-20V2":
1178
            val = min(_mem.settings.volume, 58)
1179
            rs = RadioSettingValueInteger(0, 58, val)
1180
            volume = RadioSetting("settings.volume", "Volume", rs)
1181
            basic.append(volume)
1182

    
1183
        if self.MODEL == "KT-WP12" or self.MODEL == "WP-9900":
1184
            val = min(_mem.settings.volume, 50)
1185
            rs = RadioSettingValueInteger(1, 51, val + 1)
1186
            volume = RadioSetting("settings.volume", "Volume", rs)
1187
            basic.append(volume)
1188

    
1189
        if self.MODEL == "KT-8R":
1190
            rs = RadioSettingValueBoolean(_mem.settings.dsub)
1191
            dsub = RadioSetting("settings.dsub", "CTCSS/DCS code display", rs)
1192
            basic.append(dsub)
1193

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

    
1204
        if not self.COLOR_LCD:
1205
            rs = RadioSettingValueBoolean(_mem.settings.prisc)
1206
            prisc = RadioSetting("settings.prisc", "Priority scan", rs)
1207
            basic.append(prisc)
1208

    
1209
            val = min(_mem.settings.prich, self._upper)
1210
            rs = RadioSettingValueInteger(0, self._upper, val)
1211
            prich = RadioSetting("settings.prich", "Priority channel", rs)
1212
            basic.append(prich)
1213

    
1214
        val = min(_mem.settings.screv, len(LIST_SCREV) - 1)
1215
        rs = RadioSettingValueList(LIST_SCREV, LIST_SCREV[val])
1216
        screv = RadioSetting("settings.screv", "Scan resume method", rs)
1217
        basic.append(screv)
1218

    
1219
        val = min(_mem.settings.pttlt, 30)
1220
        rs = RadioSettingValueInteger(0, 30, val)
1221
        pttlt = RadioSetting("settings.pttlt", "PTT transmit delay", rs)
1222
        basic.append(pttlt)
1223

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

    
1236
        val = min(_mem.settings.emcch, self._upper)
1237
        rs = RadioSettingValueInteger(0, self._upper, val)
1238
        emcch = RadioSetting("settings.emcch", "Alarm channel", rs)
1239
        basic.append(emcch)
1240

    
1241
        if self.COLOR_LCD:
1242
            rs = RadioSettingValueBoolean(_mem.settings.sigbp)
1243
            sigbp = RadioSetting("settings.sigbp", "Signal beep", rs)
1244
            basic.append(sigbp)
1245
        else:
1246
            val = min(_mem.settings.ringt, len(LIST_OFF1TO9) - 1)
1247
            rs = RadioSettingValueList(LIST_OFF1TO9, LIST_OFF1TO9[val])
1248
            ringt = RadioSetting("settings.ringt", "Ring time", rs)
1249
            basic.append(ringt)
1250

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

    
1256
        val = min(_mem.settings.cbmdf, len(LIST_MDF) - 1)
1257
        rs = RadioSettingValueList(LIST_MDF, LIST_MDF[val])
1258
        cbmdf = RadioSetting("settings.cbmdf", "Display mode B", rs)
1259
        basic.append(cbmdf)
1260

    
1261
        if self.COLOR_LCD:
1262
            val = min(_mem.settings.ccmdf, len(LIST_MDF) - 1)
1263
            rs = RadioSettingValueList(LIST_MDF, LIST_MDF[val])
1264
            ccmdf = RadioSetting("settings.ccmdf", "Display mode C", rs)
1265
            basic.append(ccmdf)
1266

    
1267
            if not self.COLOR_LCD4:
1268
                val = min(_mem.settings.cdmdf, len(LIST_MDF) - 1)
1269
                rs = RadioSettingValueList(LIST_MDF, LIST_MDF[val])
1270
                cdmdf = RadioSetting("settings.cdmdf", "Display mode D", rs)
1271
                basic.append(cdmdf)
1272

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

    
1294
        if self.MODEL == "KT-8R" or self.COLOR_LCD4:
1295
            val = min(_mem.settings.vox, len(LIST_VOX) - 1)
1296
            rs = RadioSettingValueList(LIST_VOX, LIST_VOX[val])
1297
            vox = RadioSetting("settings.vox", "VOX", rs)
1298
            basic.append(vox)
1299

    
1300
            val = min(_mem.settings.voxt, len(LIST_VOX) - 1)
1301
            rs = RadioSettingValueList(LIST_VOXT, LIST_VOXT[val])
1302
            voxt = RadioSetting("settings.voxt", "VOX delay time", rs)
1303
            basic.append(voxt)
1304

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

    
1324
        if self.COLOR_LCD4:
1325
            rs = RadioSettingValueBoolean(_mem.settings.autolock)
1326
            autolk = RadioSetting("settings.autolock", "Auto keylock", rs)
1327
            basic.append(autolk)
1328

    
1329
        if not self.COLOR_LCD:
1330
            val = min(_mem.settings.ponmsg, len(LIST_PONMSG) - 1)
1331
            rs = RadioSettingValueList(LIST_PONMSG, LIST_PONMSG[val])
1332
            ponmsg = RadioSetting("settings.ponmsg", "Power-on message", rs)
1333
            basic.append(ponmsg)
1334

    
1335
        if self.COLOR_LCD and not (self.COLOR_LCD2 or self.COLOR_LCD3 or
1336
                                   self.COLOR_LCD4):
1337
            val = min(_mem.settings.mainfc, len(LIST_COLOR9) - 1)
1338
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1339
            mainfc = RadioSetting("settings.mainfc",
1340
                                  "Main LCD foreground color", rs)
1341
            basic.append(mainfc)
1342

    
1343
            if not self.COLOR_LCD4:
1344
                val = min(_mem.settings.mainbc, len(LIST_COLOR9) - 1)
1345
                rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1346
                mainbc = RadioSetting("settings.mainbc",
1347
                                      "Main LCD background color", rs)
1348
                basic.append(mainbc)
1349

    
1350
            val = min(_mem.settings.menufc, len(LIST_COLOR9) - 1)
1351
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1352
            menufc = RadioSetting("settings.menufc",
1353
                                  "Menu foreground color", rs)
1354
            basic.append(menufc)
1355

    
1356
            if not self.COLOR_LCD4:
1357
                val = min(_mem.settings.menubc, len(LIST_COLOR9) - 1)
1358
                rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1359
                menubc = RadioSetting("settings.menubc",
1360
                                      "Menu background color", rs)
1361
                basic.append(menubc)
1362

    
1363
            val = min(_mem.settings.stafc, len(LIST_COLOR9) - 1)
1364
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1365
            stafc = RadioSetting("settings.stafc",
1366
                                 "Top status foreground color", rs)
1367
            basic.append(stafc)
1368

    
1369
            if not self.COLOR_LCD4:
1370
                val = min(_mem.settings.stabc, len(LIST_COLOR9) - 1)
1371
                rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1372
                stabc = RadioSetting("settings.stabc",
1373
                                     "Top status background color", rs)
1374
                basic.append(stabc)
1375

    
1376
            val = min(_mem.settings.sigfc, len(LIST_COLOR9) - 1)
1377
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1378
            sigfc = RadioSetting("settings.sigfc",
1379
                                 "Bottom status foreground color", rs)
1380
            basic.append(sigfc)
1381

    
1382
            if not self.COLOR_LCD4:
1383
                val = min(_mem.settings.sigbc, len(LIST_COLOR9) - 1)
1384
                rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1385
                sigbc = RadioSetting("settings.sigbc",
1386
                                     "Bottom status background color", rs)
1387
                basic.append(sigbc)
1388

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

    
1395
            val = min(_mem.settings.txfc, len(LIST_COLOR9) - 1)
1396
            rs = RadioSettingValueList(LIST_COLOR9, LIST_COLOR9[val])
1397
            txfc = RadioSetting("settings.txfc",
1398
                                "Transmitting character color", rs)
1399
            basic.append(txfc)
1400

    
1401
            if not self.COLOR_LCD4:
1402
                val = min(_mem.settings.txdisp, len(LIST_TXDISP) - 1)
1403
                rs = RadioSettingValueList(LIST_TXDISP, LIST_TXDISP[val])
1404
                txdisp = RadioSetting("settings.txdisp",
1405
                                      "Transmitting status display", rs)
1406
                basic.append(txdisp)
1407

    
1408
        elif self.COLOR_LCD2 or self.COLOR_LCD3:
1409
            val = min(_mem.settings.stfc, len(LIST_COLOR8) - 1)
1410
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1411
            stfc = RadioSetting("settings.stfc", "ST-FC", rs)
1412
            basic.append(stfc)
1413

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

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

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

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

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

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

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

    
1449
            val = min(_mem.settings.sigfc, len(LIST_COLOR8) - 1)
1450
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1451
            sigfc = RadioSetting("settings.sigfc", "SIG-FC", rs)
1452
            basic.append(sigfc)
1453

    
1454
            if not self.MODEL == "KT-8R":
1455
                val = min(_mem.settings.modfc, len(LIST_COLOR8) - 1)
1456
                rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1457
                modfc = RadioSetting("settings.modfc", "MOD-FC", rs)
1458
                basic.append(modfc)
1459

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

    
1465
            val = min(_mem.settings.txfc, len(LIST_COLOR8) - 1)
1466
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1467
            txfc = RadioSetting("settings.txfc", "TX-FC", rs)
1468
            basic.append(txfc)
1469

    
1470
            if self.MODEL == "KT-8R":
1471
                val = min(_mem.settings.rxfc, len(LIST_COLOR8) - 1)
1472
                rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1473
                rxfc = RadioSetting("settings.rxfc", "RX-FC", rs)
1474
                basic.append(rxfc)
1475

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

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

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

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

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

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

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

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

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

    
1528
            val = min(_mem.settings.txfc, len(LIST_COLOR8) - 1)
1529
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1530
            txfc = RadioSetting("settings.txfc", "TX fore color", rs)
1531
            basic.append(txfc)
1532

    
1533
            val = min(_mem.settings.rxfc, len(LIST_COLOR8) - 1)
1534
            rs = RadioSettingValueList(LIST_COLOR8, LIST_COLOR8[val])
1535
            rxfc = RadioSetting("settings.rxfc", "RX fore color", rs)
1536
            basic.append(rxfc)
1537
        else:
1538
            val = min(_mem.settings.wtled, len(LIST_COLOR4) - 1)
1539
            rs = RadioSettingValueList(LIST_COLOR4, LIST_COLOR4[val])
1540
            wtled = RadioSetting("settings.wtled",
1541
                                 "Standby backlight Color", rs)
1542
            basic.append(wtled)
1543

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

    
1549
            val = min(_mem.settings.txled, len(LIST_COLOR4) - 1)
1550
            rs = RadioSettingValueList(LIST_COLOR4, LIST_COLOR4[val])
1551
            txled = RadioSetting("settings.txled", "TX backlight Color", rs)
1552
            basic.append(txled)
1553

    
1554
        val = min(_mem.settings.anil, len(LIST_ANIL) - 1)
1555
        rs = RadioSettingValueList(LIST_ANIL, LIST_ANIL[val])
1556
        anil = RadioSetting("settings.anil", "ANI length", rs)
1557
        basic.append(anil)
1558

    
1559
        val = min(_mem.settings.reps, len(LIST_REPS) - 1)
1560
        rs = RadioSettingValueList(LIST_REPS, LIST_REPS[val])
1561
        reps = RadioSetting("settings.reps", "Relay signal (tone burst)", rs)
1562
        basic.append(reps)
1563

    
1564
        if self.COLOR_LCD4:
1565
            rs = RadioSettingValueBoolean(_mem.settings.dsub)
1566
            dsub = RadioSetting("settings.dsub", "Subtone display", rs)
1567
            basic.append(dsub)
1568

    
1569
        if self.MODEL == "GMRS-20V2":
1570
            val = min(_mem.settings.repsw, len(LIST_REPSW) - 1)
1571
            rs = RadioSettingValueList(LIST_REPSW, LIST_REPSW[val])
1572
            repsw = RadioSetting("settings.repsw", "Repeater SW", rs)
1573
            basic.append(repsw)
1574

    
1575
        model_list = ["KT-8R", "KT-WP12", "WP-9900"]
1576
        if self.MODEL not in model_list:
1577
            val = min(_mem.settings.repm, len(LIST_REPM) - 1)
1578
            rs = RadioSettingValueList(LIST_REPM, LIST_REPM[val])
1579
            repm = RadioSetting("settings.repm", "Relay condition", rs)
1580
            basic.append(repm)
1581

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

    
1594
            rs = RadioSettingValueBoolean(_mem.settings.ste)
1595
            ste = RadioSetting("settings.ste", "Squelch tail eliminate", rs)
1596
            basic.append(ste)
1597

    
1598
            if self.COLOR_LCD4:
1599
                val = min(_mem.settings.rpste, len(LIST_OFF1TO10) - 1)
1600
                rs = RadioSettingValueList(LIST_OFF1TO10, LIST_OFF1TO10[val])
1601
                rpste = RadioSetting("settings.rpste", "Repeater STE", rs)
1602
                basic.append(rpste)
1603
            else:
1604
                val = min(_mem.settings.rpste, len(LIST_OFF1TO9) - 1)
1605
                rs = RadioSettingValueList(LIST_OFF1TO9, LIST_OFF1TO9[val])
1606
                rpste = RadioSetting("settings.rpste", "Repeater STE", rs)
1607
                basic.append(rpste)
1608

    
1609
            if self.COLOR_LCD4:
1610
                val = min(_mem.settings.rptdl, len(LIST_OFF1TO60) - 1)
1611
                rs = RadioSettingValueList(LIST_OFF1TO60, LIST_OFF1TO60[val])
1612
                rptdl = RadioSetting("settings.rptdl",
1613
                                     "Repeater STE delay", rs)
1614
                basic.append(rptdl)
1615
            else:
1616
                val = min(_mem.settings.rptdl, len(LIST_RPTDL) - 1)
1617
                rs = RadioSettingValueList(LIST_RPTDL, LIST_RPTDL[val])
1618
                rptdl = RadioSetting("settings.rptdl",
1619
                                     "Repeater STE delay", rs)
1620
                basic.append(rptdl)
1621

    
1622
        if self.MODEL == "DB25-G":
1623
            val = min(_mem.settings.mgain, 1)
1624
            rs = RadioSettingValueBoolean(val)
1625
            mgain = RadioSetting("settings.mgain", "Auto power-on", rs)
1626
            basic.append(mgain)
1627

    
1628
        if str(_mem.fingerprint.fp) in BTECH3:
1629
            val = min(_mem.settings.mgain, 120)
1630
            rs = RadioSettingValueInteger(0, 120, val)
1631
            mgain = RadioSetting("settings.mgain", "Mic gain", rs)
1632
            basic.append(mgain)
1633

    
1634
        if str(_mem.fingerprint.fp) in BTECH3 or self.COLOR_LCD:
1635
            val = min(_mem.settings.dtmfg, 60)
1636
            rs = RadioSettingValueInteger(0, 60, val)
1637
            dtmfg = RadioSetting("settings.dtmfg", "DTMF gain", rs)
1638
            basic.append(dtmfg)
1639

    
1640
        if self.VENDOR == "BTECH" and self.COLOR_LCD:
1641
            val = min(_mem.settings.mgain, 127)
1642
            rs = RadioSettingValueInteger(0, 127, val)
1643
            mgain = RadioSetting("settings.mgain", "Mic gain", rs)
1644
            basic.append(mgain)
1645

    
1646
            val = min(_mem.settings.skiptx, len(LIST_SKIPTX) - 1)
1647
            rs = RadioSettingValueList(LIST_SKIPTX, LIST_SKIPTX[val])
1648
            skiptx = RadioSetting("settings.skiptx", "Skip TX", rs)
1649
            basic.append(skiptx)
1650

    
1651
            val = min(_mem.settings.scmode, len(LIST_SCMODE) - 1)
1652
            rs = RadioSettingValueList(LIST_SCMODE, LIST_SCMODE[val])
1653
            scmode = RadioSetting("settings.scmode", "Scan mode", rs)
1654
            basic.append(scmode)
1655

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

    
1664
        if self.MODEL in ["UV-50X2_G2", "GMRS-50V2", "UV-25X2_G2",
1665
                          "UV-25X4_G2"]:
1666
            val = min(_mem.settings.earpho, len(LIST_EARPH) - 1)
1667
            rs = RadioSettingValueList(LIST_EARPH, LIST_EARPH[val])
1668
            earpho = RadioSetting("settings.earpho", "Earphone", rs)
1669
            basic.append(earpho)
1670

    
1671
        # Advanced
1672
        def _filter(name):
1673
            filtered = ""
1674
            for char in str(name):
1675
                if char in VALID_CHARS:
1676
                    filtered += char
1677
                else:
1678
                    filtered += " "
1679
            return filtered
1680

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

    
1742
        if self.MODEL in ("UV-2501", "UV-5001"):
1743
            vfomren = RadioSetting("settings2.vfomren", "VFO/MR switching",
1744
                                   RadioSettingValueBoolean(
1745
                                       _mem.settings2.vfomren))
1746
            advanced.append(vfomren)
1747

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

    
1753
            menuen = RadioSetting("settings2.menuen", "Menu",
1754
                                  RadioSettingValueBoolean(
1755
                                      _mem.settings2.menuen))
1756
            advanced.append(menuen)
1757

    
1758
        # Other
1759
        def convert_bytes_to_limit(bytes):
1760
            limit = ""
1761
            for byte in bytes:
1762
                if byte < 10:
1763
                    limit += chr(byte + 0x30)
1764
                else:
1765
                    break
1766
            return limit
1767

    
1768
        if self.MODEL in ["UV-2501+220", "KT8900R"]:
1769
            _ranges = self._memobj.ranges220
1770
            ranges = "ranges220"
1771
        else:
1772
            _ranges = self._memobj.ranges
1773
            ranges = "ranges"
1774

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

    
1781
        _limit = convert_bytes_to_limit(_ranges.vhf_high)
1782
        val = RadioSettingValueString(0, 3, _limit)
1783
        val.set_mutable(False)
1784
        vhf_high = RadioSetting("%s.vhf_high" % ranges, "VHF high", val)
1785
        other.append(vhf_high)
1786

    
1787
        if self.BANDS == 3 or self.BANDS == 4:
1788
            _limit = convert_bytes_to_limit(_ranges.vhf2_low)
1789
            val = RadioSettingValueString(0, 3, _limit)
1790
            val.set_mutable(False)
1791
            vhf2_low = RadioSetting("%s.vhf2_low" % ranges, "VHF2 low", val)
1792
            other.append(vhf2_low)
1793

    
1794
            _limit = convert_bytes_to_limit(_ranges.vhf2_high)
1795
            val = RadioSettingValueString(0, 3, _limit)
1796
            val.set_mutable(False)
1797
            vhf2_high = RadioSetting("%s.vhf2_high" % ranges, "VHF2 high", val)
1798
            other.append(vhf2_high)
1799

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

    
1806
        _limit = convert_bytes_to_limit(_ranges.uhf_high)
1807
        val = RadioSettingValueString(0, 3, _limit)
1808
        val.set_mutable(False)
1809
        uhf_high = RadioSetting("%s.uhf_high" % ranges, "UHF high", val)
1810
        other.append(uhf_high)
1811

    
1812
        if self.BANDS == 4:
1813
            _limit = convert_bytes_to_limit(_ranges.uhf2_low)
1814
            val = RadioSettingValueString(0, 3, _limit)
1815
            val.set_mutable(False)
1816
            uhf2_low = RadioSetting("%s.uhf2_low" % ranges, "UHF2 low", val)
1817
            other.append(uhf2_low)
1818

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

    
1825
        val = RadioSettingValueString(0, 6, _filter(_mem.fingerprint.fp))
1826
        val.set_mutable(False)
1827
        fp = RadioSetting("fingerprint.fp", "Fingerprint", val)
1828
        other.append(fp)
1829

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

    
1847
        if self.COLOR_LCD:
1848
            val = min(_mem.settings2.vfomra, len(LIST_VFOMR) - 1)
1849
            rs = RadioSettingValueList(LIST_VFOMR, LIST_VFOMR[val])
1850
            vfomra = RadioSetting("settings2.vfomra", "VFO/MR A mode", rs)
1851
            work.append(vfomra)
1852

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

    
1858
            val = min(_mem.settings2.vfomrc, len(LIST_VFOMR) - 1)
1859
            rs = RadioSettingValueList(LIST_VFOMR, LIST_VFOMR[val])
1860
            vfomrc = RadioSetting("settings2.vfomrc", "VFO/MR C mode", rs)
1861
            work.append(vfomrc)
1862

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

    
1874
        rs = RadioSettingValueBoolean(_mem.settings2.keylock)
1875
        keylock = RadioSetting("settings2.keylock", "Keypad lock", rs)
1876
        work.append(keylock)
1877

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

    
1883
        val = min(_mem.settings2.mrchb, self._upper)
1884
        rs = RadioSettingValueInteger(0, self._upper, val)
1885
        mrchb = RadioSetting("settings2.mrchb", "MR B channel", rs)
1886
        work.append(mrchb)
1887

    
1888
        if self.COLOR_LCD:
1889
            val = min(_mem.settings2.mrchc, self._upper)
1890
            rs = RadioSettingValueInteger(0, self._upper, val)
1891
            mrchc = RadioSetting("settings2.mrchc", "MR C channel", rs)
1892
            work.append(mrchc)
1893

    
1894
            if not self.COLOR_LCD4:
1895
                val = min(_mem.settings2.mrchd, self._upper)
1896
                rs = RadioSettingValueInteger(0, self._upper, val)
1897
                mrchd = RadioSetting("settings2.mrchd", "MR D channel", rs)
1898
                work.append(mrchd)
1899

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

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

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

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

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

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

    
1969
        if self.COLOR_LCD:
1970
            val1c = RadioSettingValueString(0, 10,
1971
                                            bfc.bcd_decode_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 = bfc.bcd_decode_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
                                             bfc.bcd_decode_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_fp4, 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

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

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

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

    
4394
#seekto 0x33B0;
4395
struct {
4396
  char line[16];
4397
} static_msg;
4398

    
4399
#seekto 0x3D80;
4400
struct {
4401
  u8 vhf_low[3];
4402
  u8 vhf_high[3];
4403
  u8 unknown1[4];
4404
  u8 unknown2[6];
4405
  u8 vhf2_low[3];
4406
  u8 vhf2_high[3];
4407
  u8 unknown3[4];
4408
  u8 unknown4[6];
4409
  u8 uhf_low[3];
4410
  u8 uhf_high[3];
4411
  u8 unknown5[4];
4412
  u8 unknown6[6];
4413
  u8 uhf2_low[3];
4414
  u8 uhf2_high[3];
4415
} ranges;
4416

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

    
4422
"""
4423

    
4424

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

    
4491

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

    
4557

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

    
4565
    def process_mmap(self):
4566
        """Process the mem map into the mem object"""
4567

    
4568
        # Get it
4569
        mem_format = GMRS_MEM_FORMAT + GMRS_ORIG_MEM_FORMAT + \
4570
            GMRS_MEM_FORMAT_PT2
4571
        self._memobj = bitwise.parse(mem_format, self._mmap)
4572

    
4573
        # load specific parameters from the radio image
4574
        self.set_options()
4575

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

    
4581
        # setting the correct ranges for each radio type
4582
        ranges = self._memobj.ranges
4583

    
4584
        # the normal dual bands
4585
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
4586
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
4587

    
4588
        # DEBUG
4589
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
4590
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
4591

    
4592
        # set the class with the real data
4593
        self._vhf_range = vhf
4594
        self._uhf_range = uhf
4595

    
4596

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

    
4614
    def validate_memory(self, mem):
4615
        msgs = super().validate_memory(mem)
4616

    
4617
        _msg_duplex = 'Duplex must be "off" for this frequency'
4618
        _msg_offset = 'Only simplex or +5 MHz offset allowed on GMRS'
4619

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

    
4624
        return msgs
4625

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

    
4631

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

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

    
4651
        # Get it
4652
        mem_format = GMRS_MEM_FORMAT + GMRS_V2_MEM_FORMAT + \
4653
            GMRS_MEM_FORMAT_PT2
4654
        self._memobj = bitwise.parse(mem_format, self._mmap)
4655

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

    
4659
    def validate_memory(self, mem):
4660
        msgs = super().validate_memory(mem)
4661

    
4662
        _msg_duplex = 'Duplex must be "off" for this frequency'
4663
        _msg_offset = 'Only simplex or +5 MHz offset allowed on GMRS'
4664

    
4665
        if mem.freq not in GMRS_FREQS:
4666
            if mem.duplex != "off":
4667
                msgs.append(chirp_common.ValidationWarning(_msg_duplex))
4668
        elif mem.duplex:
4669
            if mem.duplex and mem.offset != 5000000:
4670
                msgs.append(chirp_common.ValidationWarning(_msg_offset))
4671

    
4672
        return msgs
4673

    
4674
    def check_set_memory_immutable_policy(self, existing, new):
4675
        existing.immutable = []
4676
        super().check_set_memory_immutable_policy(existing, new)
4677

    
4678
    @classmethod
4679
    def match_model(cls, filedata, filename):
4680
        # This model is only ever matched via metadata
4681
        return False
4682

    
4683

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

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

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

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

    
4817
#seekto 0x0F00;
4818
struct {
4819
  struct settings_vfo a;
4820
  struct settings_vfo b;
4821
  struct settings_vfo c;
4822
  struct settings_vfo d;
4823
} vfo;
4824

    
4825
#seekto 0x0FE0;
4826
struct {
4827
  char line[16];
4828
} static_msg;
4829

    
4830
#seekto 0x1000;
4831
struct {
4832
  char name[8];
4833
  u8 unknown1[8];
4834
} names[200];
4835

    
4836
#seekto 0x2400;
4837
struct {
4838
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
4839
  u8 group_tone;
4840
  u8 repeat_tone;
4841
  u8 unused[13];
4842
} _5tone_std_settings[15];
4843

    
4844
#seekto 0x2500;
4845
struct {
4846
  u8 frame1[5];
4847
  u8 frame2[5];
4848
  u8 frame3[5];
4849
  u8 standard;   // one out of LIST_5TONE_STANDARDS
4850
} _5tone_codes[15];
4851

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

    
4874
#seekto 0x2900;
4875
struct {
4876
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
4877
} dtmf_codes[15];
4878

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

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

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

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

    
4977
#seekto 0x3F70;
4978
struct {
4979
  char fp[6];
4980
} fingerprint;
4981

    
4982
"""
4983

    
4984

    
4985
class QYTColorHT(BTechMobileCommon):
4986
    """QTY's Color LCD Handheld and alike radios"""
4987
    COLOR_LCD = True
4988
    COLOR_LCD3 = True
4989
    NAME_LENGTH = 8
4990
    LIST_TMR = LIST_TMR15
4991

    
4992
    def process_mmap(self):
4993
        """Process the mem map into the mem object"""
4994

    
4995
        # Get it
4996
        self._memobj = bitwise.parse(COLORHT_MEM_FORMAT, self._mmap)
4997

    
4998
        # load specific parameters from the radio image
4999
        self.set_options()
5000

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

    
5006
        # setting the correct ranges for each radio type
5007
        ranges = self._memobj.ranges
5008

    
5009
        # the normal dual bands
5010
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
5011
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
5012

    
5013
        # DEBUG
5014
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
5015
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
5016

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

    
5024
            # 350 MHz band
5025
            uhf2 = _decode_ranges(ranges.uhf2_low, ranges.uhf2_high)
5026
            LOG.info("Radio ranges: UHF(350) %d to %d" % uhf2)
5027
            self._350_range = uhf2
5028

    
5029
        # set the class with the real data
5030
        self._vhf_range = vhf
5031
        self._uhf_range = uhf
5032

    
5033

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

    
5051

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

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

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

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

    
5185
#seekto 0x0F00;
5186
struct {
5187
  struct settings_vfo a;
5188
  struct settings_vfo b;
5189
  struct settings_vfo c;
5190
  struct settings_vfo d;
5191
} vfo;
5192

    
5193
// #seekto 0x0F80;
5194
struct {
5195
  char line1[8];
5196
  char line2[8];
5197
  char line3[8];
5198
  char line4[8];
5199
  char line5[8];
5200
  char line6[8];
5201
  char line7[8];
5202
  char line8[8];
5203
} poweron_msg;
5204

    
5205
#seekto 0x0FE0;
5206
struct {
5207
  char line[16];
5208
} static_msg;
5209

    
5210
#seekto 0x1000;
5211
struct {
5212
  char name[7];
5213
  u8 unknown1[9];
5214
} names[200];
5215

    
5216
#seekto 0x2400;
5217
struct {
5218
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
5219
  u8 group_tone;
5220
  u8 repeat_tone;
5221
  u8 unused[13];
5222
} _5tone_std_settings[15];
5223

    
5224
#seekto 0x2500;
5225
struct {
5226
  u8 frame1[5];
5227
  u8 frame2[5];
5228
  u8 frame3[5];
5229
  u8 standard;   // one out of LIST_5TONE_STANDARDS
5230
} _5tone_codes[15];
5231

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

    
5254
#seekto 0x2900;
5255
struct {
5256
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
5257
} dtmf_codes[15];
5258

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

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

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

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

    
5357
#seekto 0x3F70;
5358
struct {
5359
  char fp[6];
5360
} fingerprint;
5361

    
5362
"""
5363

    
5364

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

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

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

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

    
5503
#seekto 0x0F00;
5504
struct {
5505
  struct settings_vfo a;
5506
  struct settings_vfo b;
5507
  struct settings_vfo c;
5508
  struct settings_vfo d;
5509
} vfo;
5510

    
5511
// #seekto 0x0F80;
5512
struct {
5513
  char line1[8];
5514
  char line2[8];
5515
  char line3[8];
5516
  char line4[8];
5517
  char line5[8];
5518
  char line6[8];
5519
  char line7[8];
5520
  char line8[8];
5521
} poweron_msg;
5522

    
5523
#seekto 0x0FE0;
5524
struct {
5525
  char line[16];
5526
} static_msg;
5527

    
5528
#seekto 0x1000;
5529
struct {
5530
  char name[7];
5531
  u8 unknown1[9];
5532
} names[200];
5533

    
5534
#seekto 0x2400;
5535
struct {
5536
  u8 period; // one out of LIST_5TONE_STANDARD_PERIODS
5537
  u8 group_tone;
5538
  u8 repeat_tone;
5539
  u8 unused[13];
5540
} _5tone_std_settings[15];
5541

    
5542
#seekto 0x2500;
5543
struct {
5544
  u8 frame1[5];
5545
  u8 frame2[5];
5546
  u8 frame3[5];
5547
  u8 standard;   // one out of LIST_5TONE_STANDARDS
5548
} _5tone_codes[15];
5549

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

    
5572
#seekto 0x2900;
5573
struct {
5574
  u8 code[16]; // 0=x0A, A=0x0D, B=0x0E, C=0x0F, D=0x00, #=0x0C *=0x0B
5575
} dtmf_codes[15];
5576

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

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

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

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

    
5675
#seekto 0x3F70;
5676
struct {
5677
  char fp[6];
5678
} fingerprint;
5679

    
5680
"""
5681

    
5682

    
5683
class BTechColorWP(BTechMobileCommon):
5684
    """BTECH's Color WP Mobile and alike radios"""
5685
    COLOR_LCD = True
5686
    COLOR_LCD4 = True
5687
    NAME_LENGTH = 7
5688
    LIST_TMR = LIST_TMR7
5689

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

    
5695
        # setting the correct ranges for each radio type
5696
        ranges = self._memobj.ranges
5697

    
5698
        # the normal dual bands
5699
        vhf = _decode_ranges(ranges.vhf_low, ranges.vhf_high)
5700
        uhf = _decode_ranges(ranges.uhf_low, ranges.uhf_high)
5701

    
5702
        # DEBUG
5703
        LOG.info("Radio ranges: VHF %d to %d" % vhf)
5704
        LOG.info("Radio ranges: UHF %d to %d" % uhf)
5705

    
5706
        # set the class with the real data
5707
        self._vhf_range = vhf
5708
        self._uhf_range = uhf
5709

    
5710

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

    
5726
    def process_mmap(self):
5727
        """Process the mem map into the mem object"""
5728

    
5729
        # Get it
5730
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5731

    
5732
        # load specific parameters from the radio image
5733
        self.set_options()
5734

    
5735

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

    
5750
    def process_mmap(self):
5751
        """Process the mem map into the mem object"""
5752

    
5753
        # Get it
5754
        self._memobj = bitwise.parse(COLOR9900_MEM_FORMAT, self._mmap)
5755

    
5756
        # load specific parameters from the radio image
5757
        self.set_options()
5758

    
5759

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

    
5774
    def validate_memory(self, mem):
5775
        msgs = super().validate_memory(mem)
5776

    
5777
        _msg_duplex = 'Duplex must be "off" for this frequency'
5778
        _msg_offset = 'Only simplex or +5 MHz offset allowed on GMRS'
5779

    
5780
        if mem.freq not in GMRS_FREQS:
5781
            if mem.duplex != "off":
5782
                msgs.append(chirp_common.ValidationWarning(_msg_duplex))
5783
        elif mem.duplex:
5784
            if mem.duplex and mem.offset != 5000000:
5785
                msgs.append(chirp_common.ValidationWarning(_msg_offset))
5786

    
5787
        return msgs
5788

    
5789
    def check_set_memory_immutable_policy(self, existing, new):
5790
        existing.immutable = []
5791
        super().check_set_memory_immutable_policy(existing, new)
5792

    
5793
    def process_mmap(self):
5794
        """Process the mem map into the mem object"""
5795

    
5796
        # Get it
5797
        self._memobj = bitwise.parse(COLOR20V2_MEM_FORMAT, self._mmap)
5798

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