Project

General

Profile

New Model #9821 » retevis_rt22 - t20.py

choose Baofeng T-20 - Jim Unroe, 12/15/2023 01:31 PM

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

    
16
import time
17
import struct
18
import logging
19

    
20
from chirp import chirp_common, directory, memmap
21
from chirp import bitwise, errors, util
22
from chirp.settings import RadioSetting, RadioSettingGroup, \
23
    RadioSettingValueInteger, RadioSettingValueList, \
24
    RadioSettingValueBoolean, RadioSettings, \
25
    RadioSettingValueString
26

    
27
LOG = logging.getLogger(__name__)
28

    
29
MEM_FORMAT = """
30
#seekto 0x0010;
31
struct {
32
  lbcd rxfreq[4];
33
  lbcd txfreq[4];
34
  ul16 rx_tone;
35
  ul16 tx_tone;
36
  u8 unknown1;
37
  u8 unknown3:2,
38
     highpower:1, // Power Level
39
     wide:1,      // Bandwidth
40
     unknown4:2,
41
     signal:1,    // Signal
42
     bcl:1;       // BCL
43
  u8 unknown5[2];
44
} memory[16];
45

    
46
#seekto 0x012F;
47
struct {
48
  u8 voice;       // Voice Annunciation
49
  u8 tot;         // Time-out Timer
50
  u8 unknown1[3];
51
  u8 squelch;     // Squelch Level
52
  u8 save;        // Battery Saver
53
  u8 beep;        // Beep
54
  u8 unknown2[2];
55
  u8 vox;         // VOX
56
  u8 voxgain;     // VOX Gain
57
  u8 voxdelay;    // VOX Delay
58
  u8 unknown3[2];
59
  u8 pf2key;      // PF2 Key
60
} settings;
61

    
62
#seekto 0x017E;
63
u8 skipflags[2];  // SCAN_ADD
64

    
65
#seekto 0x0200;
66
struct {
67
  char id_0x200[8];  // Radio ID @ 0x0200
68
} radio;
69

    
70
#seekto 0x0300;
71
struct {
72
  char line1[32];
73
  char line2[32];
74
} embedded_msg;
75
"""
76

    
77
CMD_ACK = b"\x06"
78

    
79
RT22_POWER_LEVELS = [chirp_common.PowerLevel("Low",  watts=2.00),
80
                     chirp_common.PowerLevel("High", watts=5.00)]
81

    
82
RT22_DTCS = tuple(sorted(chirp_common.DTCS_CODES + (645,)))
83

    
84
PF2KEY_LIST = ["Scan", "Local Alarm", "Remote Alarm"]
85
TIMEOUTTIMER_LIST = ["Off"] + ["%s seconds" % x for x in range(15, 615, 15)]
86
VOICE_LIST = ["Off", "Chinese", "English"]
87
VOX_LIST = ["OFF"] + ["%s" % x for x in range(1, 17)]
88
VOXDELAY_LIST = ["0.5 | Off",
89
                 "1.0 | 0",
90
                 "1.5 | 1",
91
                 "2.0 | 2",
92
                 "2.5 | 3",
93
                 "3.0 | 4",
94
                 "--- | 5"]
95

    
96
VALID_CHARS = chirp_common.CHARSET_ALPHANUMERIC + \
97
    "`{|}!\"#$%&'()*+,-./:;<=>?@[]^_"
98

    
99

    
100
def _ident_from_data(data):
101
    return data[0x1B8:0x1C0]
102

    
103

    
104
def _ident_from_image(radio):
105
    return _ident_from_data(radio.get_mmap())
106

    
107

    
108
def _get_radio_model(radio):
109
    block = _rt22_read_block(radio, 0x360, 0x10)
110
    block = _rt22_read_block(radio, 0x1B8, 0x10)
111
    version = block[0:8]
112
    return version
113

    
114

    
115
def _rt22_enter_programming_mode(radio):
116
    serial = radio.pipe
117

    
118
    magic = b"PROGRGS"
119
    exito = False
120
    for i in range(0, 5):
121
        for j in range(0, len(magic)):
122
            time.sleep(0.005)
123
            serial.write(magic[j:j + 1])
124
        ack = serial.read(1)
125

    
126
        try:
127
            if ack == CMD_ACK:
128
                exito = True
129
                break
130
        except:
131
            LOG.debug("Attempt #%s, failed, trying again" % i)
132
            pass
133

    
134
    # check if we had EXITO
135
    if exito is False:
136
        msg = "The radio did not accept program mode after five tries.\n"
137
        msg += "Check you interface cable and power cycle your radio."
138
        raise errors.RadioError(msg)
139

    
140
    try:
141
        serial.write(b"\x02")
142
        ident = serial.read(8)
143
    except:
144
        _rt22_exit_programming_mode(radio)
145
        raise errors.RadioError("Error communicating with radio")
146

    
147
    # check if ident is OK
148
    itis = False
149
    for fp in radio._fileid:
150
        if fp in ident:
151
            # got it!
152
            itis = True
153

    
154
            break
155

    
156
    if itis is False:
157
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
158
        raise errors.RadioError("Radio identification failed.")
159

    
160
    try:
161
        serial.write(CMD_ACK)
162
        ack = serial.read(1)
163
    except:
164
        _rt22_exit_programming_mode(radio)
165
        raise errors.RadioError("Error communicating with radio")
166

    
167
    if ack != CMD_ACK:
168
        _rt22_exit_programming_mode(radio)
169
        raise errors.RadioError("Radio refused to enter programming mode")
170

    
171
    try:
172
        serial.write(b"\x07")
173
        ack = serial.read(1)
174
    except:
175
        _rt22_exit_programming_mode(radio)
176
        raise errors.RadioError("Error communicating with radio")
177

    
178
    if ack != b"\x4E":
179
        _rt22_exit_programming_mode(radio)
180
        raise errors.RadioError("Radio refused to enter programming mode")
181

    
182
    return ident
183

    
184

    
185
def _rt22_exit_programming_mode(radio):
186
    serial = radio.pipe
187
    try:
188
        serial.write(b"E")
189
    except:
190
        raise errors.RadioError("Radio refused to exit programming mode")
191

    
192

    
193
def _rt22_read_block(radio, block_addr, block_size):
194
    serial = radio.pipe
195

    
196
    cmd = struct.pack(">cHb", b'R', block_addr, block_size)
197
    expectedresponse = b"W" + cmd[1:]
198
    LOG.debug("Reading block %04x..." % (block_addr))
199

    
200
    try:
201
        for j in range(0, len(cmd)):
202
            time.sleep(0.005)
203
            serial.write(cmd[j:j + 1])
204

    
205
        response = serial.read(4 + block_size)
206
        if response[:4] != expectedresponse:
207
            _rt22_exit_programming_mode(radio)
208
            raise Exception("Error reading block %04x." % (block_addr))
209

    
210
        block_data = response[4:]
211

    
212
        time.sleep(0.005)
213
        serial.write(CMD_ACK)
214
        ack = serial.read(1)
215
    except:
216
        _rt22_exit_programming_mode(radio)
217
        raise errors.RadioError("Failed to read block at %04x" % block_addr)
218

    
219
    if ack != CMD_ACK:
220
        _rt22_exit_programming_mode(radio)
221
        raise Exception("No ACK reading block %04x." % (block_addr))
222

    
223
    return block_data
224

    
225

    
226
def _rt22_write_block(radio, block_addr, block_size, _requires_patch=False,
227
                      _radio_id=""):
228
    serial = radio.pipe
229

    
230
    cmd = struct.pack(">cHb", b'W', block_addr, block_size)
231
    if _requires_patch:
232
        mmap = radio.get_mmap()
233
        data = mmap[block_addr:block_addr + block_size]
234
        # For some radios (RT-622 & RT22FRS) memory at 0x1b8 reads as 0, but
235
        # radio ID should be written instead
236
        if block_addr == 0x1b8:
237
            for fp in _radio_id:
238
                if fp in mmap[0:len(_radio_id)]:
239
                    data = mmap[0:len(_radio_id)] + data[len(_radio_id):]
240
    else:
241
        data = radio.get_mmap()[block_addr:block_addr + block_size]
242

    
243
    LOG.debug("Writing Data:")
244
    LOG.debug(util.hexprint(cmd + data))
245

    
246
    try:
247
        for j in range(0, len(cmd)):
248
            time.sleep(0.005)
249
            serial.write(cmd[j:j + 1])
250
        for j in range(0, len(data)):
251
            time.sleep(0.005)
252
            serial.write(data[j:j + 1])
253
        if serial.read(1) != CMD_ACK:
254
            raise Exception("No ACK")
255
    except:
256
        _rt22_exit_programming_mode(radio)
257
        raise errors.RadioError("Failed to send block "
258
                                "to radio at %04x" % block_addr)
259

    
260

    
261
def do_download(radio):
262
    LOG.debug("download")
263
    radio_ident = _rt22_enter_programming_mode(radio)
264
    LOG.info("Radio Ident is %s" % repr(radio_ident))
265

    
266
    data = b""
267

    
268
    status = chirp_common.Status()
269
    status.msg = "Cloning from radio"
270

    
271
    status.cur = 0
272
    status.max = radio._memsize
273

    
274
    for addr in range(0, radio._memsize, radio._block_size):
275
        status.cur = addr + radio._block_size
276
        radio.status_fn(status)
277

    
278
        block = _rt22_read_block(radio, addr, radio._block_size)
279
        data += block
280

    
281
        LOG.debug("Address: %04x" % addr)
282
        LOG.debug(util.hexprint(block))
283

    
284
    _rt22_exit_programming_mode(radio)
285

    
286
    return memmap.MemoryMapBytes(data)
287

    
288

    
289
def do_upload(radio):
290
    status = chirp_common.Status()
291
    status.msg = "Uploading to radio"
292

    
293
    radio_ident = _rt22_enter_programming_mode(radio)
294
    LOG.info("Radio Ident is %s" % repr(radio_ident))
295

    
296
    image_ident = _ident_from_image(radio)
297
    LOG.info("Image Ident is %s" % repr(image_ident))
298

    
299
    # Determine if upload requires patching
300
    if image_ident == b"\x00\x00\x00\x00\x00\x00\xFF\xFF":
301
        patch_block = True
302
    else:
303
        patch_block = False
304

    
305
    status.cur = 0
306
    status.max = radio._memsize
307

    
308
    for start_addr, end_addr, block_size in radio._ranges:
309
        for addr in range(start_addr, end_addr, block_size):
310
            status.cur = addr + block_size
311
            radio.status_fn(status)
312
            _rt22_write_block(radio, addr, block_size, patch_block,
313
                              radio_ident)
314

    
315
    _rt22_exit_programming_mode(radio)
316

    
317

    
318
def model_match(cls, data):
319
    """Match the opened/downloaded image to the correct version"""
320

    
321
    if len(data) == 0x0408:
322
        rid = data[0x0400:0x0408]
323
        return rid.startswith(cls.MODEL.encode())
324
    else:
325
        return False
326

    
327

    
328
@directory.register
329
class RT22Radio(chirp_common.CloneModeRadio):
330
    """Retevis RT22"""
331
    VENDOR = "Retevis"
332
    MODEL = "RT22"
333
    BAUD_RATE = 9600
334
    NEEDS_COMPAT_SERIAL = False
335

    
336
    _ranges = [
337
               (0x0000, 0x0180, 0x10),
338
               (0x01B8, 0x01F8, 0x10),
339
               (0x01F8, 0x0200, 0x08),
340
               (0x0200, 0x0340, 0x10),
341
              ]
342
    _memsize = 0x0400
343
    _block_size = 0x40
344
    _fileid = [b"P32073", b"P3" + b"\x00\x00\x00" + b"3", b"P3207!",
345
               b"\x00\x00\x00\x00\x00\x00\xF8\xFF"]
346

    
347
    def get_features(self):
348
        rf = chirp_common.RadioFeatures()
349
        rf.has_settings = True
350
        rf.has_bank = False
351
        rf.has_ctone = True
352
        rf.has_cross = True
353
        rf.has_rx_dtcs = True
354
        rf.has_tuning_step = False
355
        rf.can_odd_split = True
356
        rf.has_name = False
357
        rf.valid_skips = ["", "S"]
358
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
359
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
360
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
361
        rf.valid_power_levels = RT22_POWER_LEVELS
362
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
363
        rf.valid_modes = ["NFM", "FM"]  # 12.5 kHz, 25 kHz.
364
        rf.valid_dtcs_codes = RT22_DTCS
365
        rf.memory_bounds = (1, 16)
366
        rf.valid_tuning_steps = [2.5, 5., 6.25, 10., 12.5, 25.]
367
        rf.valid_bands = [(400000000, 520000000)]
368

    
369
        return rf
370

    
371
    def process_mmap(self):
372
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
373

    
374
    def sync_in(self):
375
        """Download from radio"""
376
        try:
377
            data = do_download(self)
378
        except errors.RadioError:
379
            # Pass through any real errors we raise
380
            raise
381
        except:
382
            # If anything unexpected happens, make sure we raise
383
            # a RadioError and log the problem
384
            LOG.exception('Unexpected error during download')
385
            raise errors.RadioError('Unexpected error communicating '
386
                                    'with the radio')
387
        self._mmap = data
388
        self.process_mmap()
389

    
390
    def sync_out(self):
391
        """Upload to radio"""
392
        try:
393
            do_upload(self)
394
        except:
395
            # If anything unexpected happens, make sure we raise
396
            # a RadioError and log the problem
397
            LOG.exception('Unexpected error during upload')
398
            raise errors.RadioError('Unexpected error communicating '
399
                                    'with the radio')
400

    
401
    def get_raw_memory(self, number):
402
        return repr(self._memobj.memory[number - 1])
403

    
404
    def _get_tone(self, _mem, mem):
405
        def _get_dcs(val):
406
            code = int("%03o" % (val & 0x07FF))
407
            pol = (val & 0x8000) and "R" or "N"
408
            return code, pol
409

    
410
        if _mem.tx_tone != 0xFFFF and _mem.tx_tone > 0x2800:
411
            tcode, tpol = _get_dcs(_mem.tx_tone)
412
            mem.dtcs = tcode
413
            txmode = "DTCS"
414
        elif _mem.tx_tone != 0xFFFF:
415
            mem.rtone = _mem.tx_tone / 10.0
416
            txmode = "Tone"
417
        else:
418
            txmode = ""
419

    
420
        if _mem.rx_tone != 0xFFFF and _mem.rx_tone > 0x2800:
421
            rcode, rpol = _get_dcs(_mem.rx_tone)
422
            mem.rx_dtcs = rcode
423
            rxmode = "DTCS"
424
        elif _mem.rx_tone != 0xFFFF:
425
            mem.ctone = _mem.rx_tone / 10.0
426
            rxmode = "Tone"
427
        else:
428
            rxmode = ""
429

    
430
        if txmode == "Tone" and not rxmode:
431
            mem.tmode = "Tone"
432
        elif txmode == rxmode and txmode == "Tone" and mem.rtone == mem.ctone:
433
            mem.tmode = "TSQL"
434
        elif txmode == rxmode and txmode == "DTCS" and mem.dtcs == mem.rx_dtcs:
435
            mem.tmode = "DTCS"
436
        elif rxmode or txmode:
437
            mem.tmode = "Cross"
438
            mem.cross_mode = "%s->%s" % (txmode, rxmode)
439

    
440
        if mem.tmode == "DTCS":
441
            mem.dtcs_polarity = "%s%s" % (tpol, rpol)
442

    
443
        LOG.debug("Got TX %s (%i) RX %s (%i)" %
444
                  (txmode, _mem.tx_tone, rxmode, _mem.rx_tone))
445

    
446
    def get_memory(self, number):
447
        bitpos = (1 << ((number - 1) % 8))
448
        bytepos = ((number - 1) / 8)
449
        LOG.debug("bitpos %s" % bitpos)
450
        LOG.debug("bytepos %s" % bytepos)
451

    
452
        _mem = self._memobj.memory[number - 1]
453
        _skp = self._memobj.skipflags[bytepos]
454

    
455
        mem = chirp_common.Memory()
456

    
457
        mem.number = number
458
        mem.freq = int(_mem.rxfreq) * 10
459

    
460
        # We'll consider any blank (i.e. 0 MHz frequency) to be empty
461
        if mem.freq == 0:
462
            mem.empty = True
463
            return mem
464

    
465
        if _mem.rxfreq.get_raw() == b"\xFF\xFF\xFF\xFF":
466
            mem.freq = 0
467
            mem.empty = True
468
            return mem
469

    
470
        if int(_mem.rxfreq) == int(_mem.txfreq):
471
            mem.duplex = ""
472
            mem.offset = 0
473
        else:
474
            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
475
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
476

    
477
        mem.mode = _mem.wide and "FM" or "NFM"
478

    
479
        self._get_tone(_mem, mem)
480

    
481
        mem.power = RT22_POWER_LEVELS[_mem.highpower]
482

    
483
        mem.skip = "" if (_skp & bitpos) else "S"
484
        LOG.debug("mem.skip %s" % mem.skip)
485

    
486
        mem.extra = RadioSettingGroup("Extra", "extra")
487

    
488
        if self.MODEL == "RT22FRS" or self.MODEL == "RT622":
489
            rs = RadioSettingValueBoolean(_mem.bcl)
490
            rset = RadioSetting("bcl", "Busy Channel Lockout", rs)
491
            mem.extra.append(rset)
492

    
493
            rs = RadioSettingValueBoolean(_mem.signal)
494
            rset = RadioSetting("signal", "Signal", rs)
495
            mem.extra.append(rset)
496

    
497
        return mem
498

    
499
    def _set_tone(self, mem, _mem):
500
        def _set_dcs(code, pol):
501
            val = int("%i" % code, 8) + 0x2800
502
            if pol == "R":
503
                val += 0x8000
504
            return val
505

    
506
        rx_mode = tx_mode = None
507
        rx_tone = tx_tone = 0xFFFF
508

    
509
        if mem.tmode == "Tone":
510
            tx_mode = "Tone"
511
            rx_mode = None
512
            tx_tone = int(mem.rtone * 10)
513
        elif mem.tmode == "TSQL":
514
            rx_mode = tx_mode = "Tone"
515
            rx_tone = tx_tone = int(mem.ctone * 10)
516
        elif mem.tmode == "DTCS":
517
            tx_mode = rx_mode = "DTCS"
518
            tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
519
            rx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[1])
520
        elif mem.tmode == "Cross":
521
            tx_mode, rx_mode = mem.cross_mode.split("->")
522
            if tx_mode == "DTCS":
523
                tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
524
            elif tx_mode == "Tone":
525
                tx_tone = int(mem.rtone * 10)
526
            if rx_mode == "DTCS":
527
                rx_tone = _set_dcs(mem.rx_dtcs, mem.dtcs_polarity[1])
528
            elif rx_mode == "Tone":
529
                rx_tone = int(mem.ctone * 10)
530

    
531
        _mem.rx_tone = rx_tone
532
        _mem.tx_tone = tx_tone
533

    
534
        LOG.debug("Set TX %s (%i) RX %s (%i)" %
535
                  (tx_mode, _mem.tx_tone, rx_mode, _mem.rx_tone))
536

    
537
    def set_memory(self, mem):
538
        bitpos = (1 << ((mem.number - 1) % 8))
539
        bytepos = ((mem.number - 1) / 8)
540
        LOG.debug("bitpos %s" % bitpos)
541
        LOG.debug("bytepos %s" % bytepos)
542

    
543
        _mem = self._memobj.memory[mem.number - 1]
544
        _skp = self._memobj.skipflags[bytepos]
545

    
546
        if mem.empty:
547
            _mem.set_raw("\xFF" * (_mem.size() // 8))
548
            return
549

    
550
        _mem.rxfreq = mem.freq / 10
551

    
552
        if mem.duplex == "off":
553
            for i in range(0, 4):
554
                _mem.txfreq[i].set_raw("\xFF")
555
        elif mem.duplex == "split":
556
            _mem.txfreq = mem.offset / 10
557
        elif mem.duplex == "+":
558
            _mem.txfreq = (mem.freq + mem.offset) / 10
559
        elif mem.duplex == "-":
560
            _mem.txfreq = (mem.freq - mem.offset) / 10
561
        else:
562
            _mem.txfreq = mem.freq / 10
563

    
564
        _mem.wide = mem.mode == "FM"
565

    
566
        self._set_tone(mem, _mem)
567

    
568
        _mem.highpower = mem.power == RT22_POWER_LEVELS[1]
569

    
570
        if mem.skip != "S":
571
            _skp |= bitpos
572
        else:
573
            _skp &= ~bitpos
574
        LOG.debug("_skp %s" % _skp)
575

    
576
        for setting in mem.extra:
577
            setattr(_mem, setting.get_name(), setting.value)
578

    
579
    def get_settings(self):
580
        _settings = self._memobj.settings
581
        _message = self._memobj.embedded_msg
582
        basic = RadioSettingGroup("basic", "Basic Settings")
583
        top = RadioSettings(basic)
584

    
585
        rs = RadioSetting("squelch", "Squelch Level",
586
                          RadioSettingValueInteger(0, 9, _settings.squelch))
587
        basic.append(rs)
588

    
589
        rs = RadioSetting("tot", "Time-out timer",
590
                          RadioSettingValueList(
591
                              TIMEOUTTIMER_LIST,
592
                              TIMEOUTTIMER_LIST[_settings.tot]))
593
        basic.append(rs)
594

    
595
        rs = RadioSetting("voice", "Voice Prompts",
596
                          RadioSettingValueList(
597
                              VOICE_LIST, VOICE_LIST[_settings.voice]))
598
        basic.append(rs)
599

    
600
        rs = RadioSetting("pf2key", "PF2 Key",
601
                          RadioSettingValueList(
602
                              PF2KEY_LIST, PF2KEY_LIST[_settings.pf2key]))
603
        basic.append(rs)
604

    
605
        rs = RadioSetting("vox", "Vox",
606
                          RadioSettingValueBoolean(_settings.vox))
607
        basic.append(rs)
608

    
609
        rs = RadioSetting("voxgain", "VOX Level",
610
                          RadioSettingValueList(
611
                              VOX_LIST, VOX_LIST[_settings.voxgain]))
612
        basic.append(rs)
613

    
614
        rs = RadioSetting("voxdelay", "VOX Delay Time (Old | New)",
615
                          RadioSettingValueList(
616
                              VOXDELAY_LIST,
617
                              VOXDELAY_LIST[_settings.voxdelay]))
618
        basic.append(rs)
619

    
620
        rs = RadioSetting("save", "Battery Save",
621
                          RadioSettingValueBoolean(_settings.save))
622
        basic.append(rs)
623

    
624
        rs = RadioSetting("beep", "Beep",
625
                          RadioSettingValueBoolean(_settings.beep))
626
        basic.append(rs)
627

    
628
        if self.MODEL != "W31E":
629
            def _filter(name):
630
                filtered = ""
631
                for char in str(name):
632
                    if char in VALID_CHARS:
633
                        filtered += char
634
                    else:
635
                        filtered += " "
636
                return filtered
637

    
638
            val = str(self._memobj.radio.id_0x200)
639
            if val == "\xFF" * 8:
640
                rs = RadioSetting("embedded_msg.line1", "Embedded Message 1",
641
                                  RadioSettingValueString(0, 32, _filter(
642
                                      _message.line1)))
643
                basic.append(rs)
644

    
645
                rs = RadioSetting("embedded_msg.line2", "Embedded Message 2",
646
                                  RadioSettingValueString(0, 32, _filter(
647
                                      _message.line2)))
648
                basic.append(rs)
649

    
650
        return top
651

    
652
    def set_settings(self, settings):
653
        for element in settings:
654
            if not isinstance(element, RadioSetting):
655
                self.set_settings(element)
656
                continue
657
            else:
658
                try:
659
                    if "." in element.get_name():
660
                        bits = element.get_name().split(".")
661
                        obj = self._memobj
662
                        for bit in bits[:-1]:
663
                            obj = getattr(obj, bit)
664
                        setting = bits[-1]
665
                    else:
666
                        obj = self._memobj.settings
667
                        setting = element.get_name()
668

    
669
                    LOG.debug("Setting %s = %s" % (setting, element.value))
670
                    setattr(obj, setting, element.value)
671
                except Exception:
672
                    LOG.debug(element.get_name())
673
                    raise
674

    
675
    @classmethod
676
    def match_model(cls, filedata, filename):
677
        match_size = False
678
        match_model = False
679

    
680
        # testing the file data size
681
        if len(filedata) in [0x0408, ]:
682
            match_size = True
683

    
684
        # testing the model fingerprint
685
        match_model = model_match(cls, filedata)
686

    
687
        if match_size and match_model:
688
            return True
689
        else:
690
            return False
691

    
692

    
693
@directory.register
694
class KDC1(RT22Radio):
695
    """WLN KD-C1"""
696
    VENDOR = "WLN"
697
    MODEL = "KD-C1"
698

    
699

    
700
@directory.register
701
class ZTX6(RT22Radio):
702
    """Zastone ZT-X6"""
703
    VENDOR = "Zastone"
704
    MODEL = "ZT-X6"
705

    
706

    
707
@directory.register
708
class LT316(RT22Radio):
709
    """Luiton LT-316"""
710
    VENDOR = "LUITON"
711
    MODEL = "LT-316"
712

    
713

    
714
@directory.register
715
class TDM8(RT22Radio):
716
    VENDOR = "TID"
717
    MODEL = "TD-M8"
718

    
719

    
720
@directory.register
721
class RT22FRS(RT22Radio):
722
    VENDOR = "Retevis"
723
    MODEL = "RT22FRS"
724

    
725

    
726
@directory.register
727
class RT622(RT22Radio):
728
    VENDOR = "Retevis"
729
    MODEL = "RT622"
730

    
731

    
732
@directory.register
733
class W31E(RT22Radio):
734
    """Baofeng W31E"""
735
    VENDOR = "Baofeng"
736
    MODEL = "W31E"
737

    
738
    _ranges = [
739
               (0x0000, 0x0200, 0x10),
740
              ]
741
    _memsize = 0x0200
742
    _block_size = 0x40
743

    
744

    
745
@directory.register
746
class BFT20(RT22Radio):
747
    """Baofeng BF-T20"""
748
    VENDOR = "Baofeng"
749
    MODEL = "BF-T20"
750

    
751
    _fileid = [b"P330h33", ]
(1-1/2)