Project

General

Profile

Bug #10422 » retevis_rt22_with_new_id.py

Model ID: 50 33 b2 30 37 33 f8 ff - Jim Unroe, 03/07/2023 01:11 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 os
18
import struct
19
import logging
20

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

    
28
LOG = logging.getLogger(__name__)
29

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

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

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

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

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

    
78
CMD_ACK = b"\x06"
79

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

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

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

    
97
SETTING_LISTS = {
98
    "pf2key": PF2KEY_LIST,
99
    "tot": TIMEOUTTIMER_LIST,
100
    "voice": VOICE_LIST,
101
    "vox": VOX_LIST,
102
    "voxdelay": VOXDELAY_LIST,
103
    }
104

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

    
108

    
109
def _ident_from_data(data):
110
    return data[0x1B8:0x1C0]
111

    
112

    
113
def _ident_from_image(radio):
114
    return _ident_from_data(radio.get_mmap())
115

    
116

    
117
def _get_radio_model(radio):
118
    block = _rt22_read_block(radio, 0x360, 0x10)
119
    block = _rt22_read_block(radio, 0x1B8, 0x10)
120
    version = block[0:8]
121
    return version
122

    
123

    
124
def _rt22_enter_programming_mode(radio):
125
    serial = radio.pipe
126

    
127
    magic = b"PROGRGS"
128
    exito = False
129
    for i in range(0, 5):
130
        for j in range(0, len(magic)):
131
            time.sleep(0.005)
132
            serial.write(magic[j:j + 1])
133
        ack = serial.read(1)
134

    
135
        try:
136
            if ack == CMD_ACK:
137
                exito = True
138
                break
139
        except:
140
            LOG.debug("Attempt #%s, failed, trying again" % i)
141
            pass
142

    
143
    # check if we had EXITO
144
    if exito is False:
145
        msg = "The radio did not accept program mode after five tries.\n"
146
        msg += "Check you interface cable and power cycle your radio."
147
        raise errors.RadioError(msg)
148

    
149
    try:
150
        serial.write(b"\x02")
151
        ident = serial.read(8)
152
    except:
153
        _rt22_exit_programming_mode(radio)
154
        raise errors.RadioError("Error communicating with radio")
155

    
156
    # check if ident is OK
157
    itis = False
158
    for fp in radio._fileid:
159
        if fp in ident:
160
            # got it!
161
            itis = True
162

    
163
            break
164

    
165
    if itis is False:
166
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
167
        raise errors.RadioError("Radio identification failed.")
168

    
169
    try:
170
        serial.write(CMD_ACK)
171
        ack = serial.read(1)
172
    except:
173
        _rt22_exit_programming_mode(radio)
174
        raise errors.RadioError("Error communicating with radio")
175

    
176
    if ack != CMD_ACK:
177
        _rt22_exit_programming_mode(radio)
178
        raise errors.RadioError("Radio refused to enter programming mode")
179

    
180
    try:
181
        serial.write(b"\x07")
182
        ack = serial.read(1)
183
    except:
184
        _rt22_exit_programming_mode(radio)
185
        raise errors.RadioError("Error communicating with radio")
186

    
187
    if ack != b"\x4E":
188
        _rt22_exit_programming_mode(radio)
189
        raise errors.RadioError("Radio refused to enter programming mode")
190

    
191
    return ident
192

    
193

    
194
def _rt22_exit_programming_mode(radio):
195
    serial = radio.pipe
196
    try:
197
        serial.write(b"E")
198
    except:
199
        raise errors.RadioError("Radio refused to exit programming mode")
200

    
201

    
202
def _rt22_read_block(radio, block_addr, block_size):
203
    serial = radio.pipe
204

    
205
    cmd = struct.pack(">cHb", b'R', block_addr, block_size)
206
    expectedresponse = b"W" + cmd[1:]
207
    LOG.debug("Reading block %04x..." % (block_addr))
208

    
209
    try:
210
        for j in range(0, len(cmd)):
211
            time.sleep(0.005)
212
            serial.write(cmd[j:j + 1])
213

    
214
        response = serial.read(4 + block_size)
215
        if response[:4] != expectedresponse:
216
            _rt22_exit_programming_mode(radio)
217
            raise Exception("Error reading block %04x." % (block_addr))
218

    
219
        block_data = response[4:]
220

    
221
        time.sleep(0.005)
222
        serial.write(CMD_ACK)
223
        ack = serial.read(1)
224
    except:
225
        _rt22_exit_programming_mode(radio)
226
        raise errors.RadioError("Failed to read block at %04x" % block_addr)
227

    
228
    if ack != CMD_ACK:
229
        _rt22_exit_programming_mode(radio)
230
        raise Exception("No ACK reading block %04x." % (block_addr))
231

    
232
    return block_data
233

    
234

    
235
def _rt22_write_block(radio, block_addr, block_size, _requires_patch=False,
236
                      _radio_id=""):
237
    serial = radio.pipe
238

    
239
    cmd = struct.pack(">cHb", b'W', block_addr, block_size)
240
    if _requires_patch:
241
        mmap = radio.get_mmap()
242
        data = mmap[block_addr:block_addr + block_size]
243
        # For some radios (RT-622 & RT22FRS) memory at 0x1b8 reads as 0, but
244
        # radio ID should be written instead
245
        if block_addr == 0x1b8:
246
            for fp in _radio_id:
247
                if fp in mmap[0:len(_radio_id)]:
248
                    data = mmap[0:len(_radio_id)] + data[len(_radio_id):]
249
    else:
250
        data = radio.get_mmap()[block_addr:block_addr + block_size]
251

    
252
    LOG.debug("Writing Data:")
253
    LOG.debug(util.hexprint(cmd + data))
254

    
255
    try:
256
        for j in range(0, len(cmd)):
257
            time.sleep(0.005)
258
            serial.write(cmd[j:j + 1])
259
        for j in range(0, len(data)):
260
            time.sleep(0.005)
261
            serial.write(data[j:j + 1])
262
        if serial.read(1) != CMD_ACK:
263
            raise Exception("No ACK")
264
    except:
265
        _rt22_exit_programming_mode(radio)
266
        raise errors.RadioError("Failed to send block "
267
                                "to radio at %04x" % block_addr)
268

    
269

    
270
def do_download(radio):
271
    LOG.debug("download")
272
    radio_ident = _rt22_enter_programming_mode(radio)
273
    LOG.info("Radio Ident is %s" % repr(radio_ident))
274

    
275
    data = b""
276

    
277
    status = chirp_common.Status()
278
    status.msg = "Cloning from radio"
279

    
280
    status.cur = 0
281
    status.max = radio._memsize
282

    
283
    for addr in range(0, radio._memsize, radio._block_size):
284
        status.cur = addr + radio._block_size
285
        radio.status_fn(status)
286

    
287
        block = _rt22_read_block(radio, addr, radio._block_size)
288
        data += block
289

    
290
        LOG.debug("Address: %04x" % addr)
291
        LOG.debug(util.hexprint(block))
292

    
293
    _rt22_exit_programming_mode(radio)
294

    
295
    return memmap.MemoryMapBytes(data)
296

    
297

    
298
def do_upload(radio):
299
    status = chirp_common.Status()
300
    status.msg = "Uploading to radio"
301

    
302
    radio_ident = _rt22_enter_programming_mode(radio)
303
    LOG.info("Radio Ident is %s" % repr(radio_ident))
304

    
305
    image_ident = _ident_from_image(radio)
306
    LOG.info("Image Ident is %s" % repr(image_ident))
307

    
308
    # Determine if upload requires patching
309
    if image_ident == b"\x00\x00\x00\x00\x00\x00\xFF\xFF":
310
        patch_block = True
311
    else:
312
        patch_block = False
313

    
314
    status.cur = 0
315
    status.max = radio._memsize
316

    
317
    for start_addr, end_addr, block_size in radio._ranges:
318
        for addr in range(start_addr, end_addr, block_size):
319
            status.cur = addr + block_size
320
            radio.status_fn(status)
321
            _rt22_write_block(radio, addr, block_size, patch_block,
322
                              radio_ident)
323

    
324
    _rt22_exit_programming_mode(radio)
325

    
326

    
327
def model_match(cls, data):
328
    """Match the opened/downloaded image to the correct version"""
329

    
330
    if len(data) == 0x0408:
331
        rid = data[0x0400:0x0408]
332
        return rid.startswith(cls.MODEL.encode())
333
    else:
334
        return False
335

    
336

    
337
@directory.register
338
class RT22Radio(chirp_common.CloneModeRadio):
339
    """Retevis RT22"""
340
    VENDOR = "Retevis"
341
    MODEL = "RT22"
342
    BAUD_RATE = 9600
343
    NEEDS_COMPAT_SERIAL = False
344

    
345
    _ranges = [
346
               (0x0000, 0x0180, 0x10),
347
               (0x01B8, 0x01F8, 0x10),
348
               (0x01F8, 0x0200, 0x08),
349
               (0x0200, 0x0340, 0x10),
350
              ]
351
    _memsize = 0x0400
352
    _block_size = 0x40
353
    _fileid = [b"P32073", b"P3" + b"\x00\x00\x00" + b"3", b"P3207!",
354
               b"P3" + b"\xB2" + b"073",
355
               b"\x00\x00\x00\x00\x00\x00\xF8\xFF"]
356

    
357
    def get_features(self):
358
        rf = chirp_common.RadioFeatures()
359
        rf.has_settings = True
360
        rf.has_bank = False
361
        rf.has_ctone = True
362
        rf.has_cross = True
363
        rf.has_rx_dtcs = True
364
        rf.has_tuning_step = False
365
        rf.can_odd_split = True
366
        rf.has_name = False
367
        rf.valid_skips = ["", "S"]
368
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
369
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
370
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
371
        rf.valid_power_levels = RT22_POWER_LEVELS
372
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
373
        rf.valid_modes = ["NFM", "FM"]  # 12.5 KHz, 25 kHz.
374
        rf.valid_dtcs_codes = RT22_DTCS
375
        rf.memory_bounds = (1, 16)
376
        rf.valid_tuning_steps = [2.5, 5., 6.25, 10., 12.5, 25.]
377
        rf.valid_bands = [(400000000, 520000000)]
378

    
379
        return rf
380

    
381
    def process_mmap(self):
382
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
383

    
384
    def sync_in(self):
385
        """Download from radio"""
386
        try:
387
            data = do_download(self)
388
        except errors.RadioError:
389
            # Pass through any real errors we raise
390
            raise
391
        except:
392
            # If anything unexpected happens, make sure we raise
393
            # a RadioError and log the problem
394
            LOG.exception('Unexpected error during download')
395
            raise errors.RadioError('Unexpected error communicating '
396
                                    'with the radio')
397
        self._mmap = data
398
        self.process_mmap()
399

    
400
    def sync_out(self):
401
        """Upload to radio"""
402
        try:
403
            do_upload(self)
404
        except:
405
            # If anything unexpected happens, make sure we raise
406
            # a RadioError and log the problem
407
            LOG.exception('Unexpected error during upload')
408
            raise errors.RadioError('Unexpected error communicating '
409
                                    'with the radio')
410

    
411
    def get_raw_memory(self, number):
412
        return repr(self._memobj.memory[number - 1])
413

    
414
    def _get_tone(self, _mem, mem):
415
        def _get_dcs(val):
416
            code = int("%03o" % (val & 0x07FF))
417
            pol = (val & 0x8000) and "R" or "N"
418
            return code, pol
419

    
420
        if _mem.tx_tone != 0xFFFF and _mem.tx_tone > 0x2800:
421
            tcode, tpol = _get_dcs(_mem.tx_tone)
422
            mem.dtcs = tcode
423
            txmode = "DTCS"
424
        elif _mem.tx_tone != 0xFFFF:
425
            mem.rtone = _mem.tx_tone / 10.0
426
            txmode = "Tone"
427
        else:
428
            txmode = ""
429

    
430
        if _mem.rx_tone != 0xFFFF and _mem.rx_tone > 0x2800:
431
            rcode, rpol = _get_dcs(_mem.rx_tone)
432
            mem.rx_dtcs = rcode
433
            rxmode = "DTCS"
434
        elif _mem.rx_tone != 0xFFFF:
435
            mem.ctone = _mem.rx_tone / 10.0
436
            rxmode = "Tone"
437
        else:
438
            rxmode = ""
439

    
440
        if txmode == "Tone" and not rxmode:
441
            mem.tmode = "Tone"
442
        elif txmode == rxmode and txmode == "Tone" and mem.rtone == mem.ctone:
443
            mem.tmode = "TSQL"
444
        elif txmode == rxmode and txmode == "DTCS" and mem.dtcs == mem.rx_dtcs:
445
            mem.tmode = "DTCS"
446
        elif rxmode or txmode:
447
            mem.tmode = "Cross"
448
            mem.cross_mode = "%s->%s" % (txmode, rxmode)
449

    
450
        if mem.tmode == "DTCS":
451
            mem.dtcs_polarity = "%s%s" % (tpol, rpol)
452

    
453
        LOG.debug("Got TX %s (%i) RX %s (%i)" %
454
                  (txmode, _mem.tx_tone, rxmode, _mem.rx_tone))
455

    
456
    def get_memory(self, number):
457
        bitpos = (1 << ((number - 1) % 8))
458
        bytepos = ((number - 1) / 8)
459
        LOG.debug("bitpos %s" % bitpos)
460
        LOG.debug("bytepos %s" % bytepos)
461

    
462
        _mem = self._memobj.memory[number - 1]
463
        _skp = self._memobj.skipflags[bytepos]
464

    
465
        mem = chirp_common.Memory()
466

    
467
        mem.number = number
468
        mem.freq = int(_mem.rxfreq) * 10
469

    
470
        # We'll consider any blank (i.e. 0MHz frequency) to be empty
471
        if mem.freq == 0:
472
            mem.empty = True
473
            return mem
474

    
475
        if _mem.rxfreq.get_raw() == "\xFF\xFF\xFF\xFF":
476
            mem.freq = 0
477
            mem.empty = True
478
            return mem
479

    
480
        if int(_mem.rxfreq) == int(_mem.txfreq):
481
            mem.duplex = ""
482
            mem.offset = 0
483
        else:
484
            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
485
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
486

    
487
        mem.mode = _mem.wide and "FM" or "NFM"
488

    
489
        self._get_tone(_mem, mem)
490

    
491
        mem.power = RT22_POWER_LEVELS[_mem.highpower]
492

    
493
        mem.skip = "" if (_skp & bitpos) else "S"
494
        LOG.debug("mem.skip %s" % mem.skip)
495

    
496
        mem.extra = RadioSettingGroup("Extra", "extra")
497

    
498
        if self.MODEL == "RT22FRS" or self.MODEL == "RT622":
499
            rs = RadioSettingValueBoolean(_mem.bcl)
500
            rset = RadioSetting("bcl", "Busy Channel Lockout", rs)
501
            mem.extra.append(rset)
502

    
503
            rs = RadioSettingValueBoolean(_mem.signal)
504
            rset = RadioSetting("signal", "Signal", rs)
505
            mem.extra.append(rset)
506

    
507
        return mem
508

    
509
    def _set_tone(self, mem, _mem):
510
        def _set_dcs(code, pol):
511
            val = int("%i" % code, 8) + 0x2800
512
            if pol == "R":
513
                val += 0x8000
514
            return val
515

    
516
        rx_mode = tx_mode = None
517
        rx_tone = tx_tone = 0xFFFF
518

    
519
        if mem.tmode == "Tone":
520
            tx_mode = "Tone"
521
            rx_mode = None
522
            tx_tone = int(mem.rtone * 10)
523
        elif mem.tmode == "TSQL":
524
            rx_mode = tx_mode = "Tone"
525
            rx_tone = tx_tone = int(mem.ctone * 10)
526
        elif mem.tmode == "DTCS":
527
            tx_mode = rx_mode = "DTCS"
528
            tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
529
            rx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[1])
530
        elif mem.tmode == "Cross":
531
            tx_mode, rx_mode = mem.cross_mode.split("->")
532
            if tx_mode == "DTCS":
533
                tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
534
            elif tx_mode == "Tone":
535
                tx_tone = int(mem.rtone * 10)
536
            if rx_mode == "DTCS":
537
                rx_tone = _set_dcs(mem.rx_dtcs, mem.dtcs_polarity[1])
538
            elif rx_mode == "Tone":
539
                rx_tone = int(mem.ctone * 10)
540

    
541
        _mem.rx_tone = rx_tone
542
        _mem.tx_tone = tx_tone
543

    
544
        LOG.debug("Set TX %s (%i) RX %s (%i)" %
545
                  (tx_mode, _mem.tx_tone, rx_mode, _mem.rx_tone))
546

    
547
    def set_memory(self, mem):
548
        bitpos = (1 << ((mem.number - 1) % 8))
549
        bytepos = ((mem.number - 1) / 8)
550
        LOG.debug("bitpos %s" % bitpos)
551
        LOG.debug("bytepos %s" % bytepos)
552

    
553
        _mem = self._memobj.memory[mem.number - 1]
554
        _skp = self._memobj.skipflags[bytepos]
555

    
556
        if mem.empty:
557
            _mem.set_raw("\xFF" * (_mem.size() // 8))
558
            return
559

    
560
        _mem.rxfreq = mem.freq / 10
561

    
562
        if mem.duplex == "off":
563
            for i in range(0, 4):
564
                _mem.txfreq[i].set_raw("\xFF")
565
        elif mem.duplex == "split":
566
            _mem.txfreq = mem.offset / 10
567
        elif mem.duplex == "+":
568
            _mem.txfreq = (mem.freq + mem.offset) / 10
569
        elif mem.duplex == "-":
570
            _mem.txfreq = (mem.freq - mem.offset) / 10
571
        else:
572
            _mem.txfreq = mem.freq / 10
573

    
574
        _mem.wide = mem.mode == "FM"
575

    
576
        self._set_tone(mem, _mem)
577

    
578
        _mem.highpower = mem.power == RT22_POWER_LEVELS[1]
579

    
580
        if mem.skip != "S":
581
            _skp |= bitpos
582
        else:
583
            _skp &= ~bitpos
584
        LOG.debug("_skp %s" % _skp)
585

    
586
        for setting in mem.extra:
587
            setattr(_mem, setting.get_name(), setting.value)
588

    
589
    def get_settings(self):
590
        _settings = self._memobj.settings
591
        _message = self._memobj.embedded_msg
592
        basic = RadioSettingGroup("basic", "Basic Settings")
593
        top = RadioSettings(basic)
594

    
595
        rs = RadioSetting("squelch", "Squelch Level",
596
                          RadioSettingValueInteger(0, 9, _settings.squelch))
597
        basic.append(rs)
598

    
599
        rs = RadioSetting("tot", "Time-out timer",
600
                          RadioSettingValueList(
601
                              TIMEOUTTIMER_LIST,
602
                              TIMEOUTTIMER_LIST[_settings.tot]))
603
        basic.append(rs)
604

    
605
        rs = RadioSetting("voice", "Voice Prompts",
606
                          RadioSettingValueList(
607
                              VOICE_LIST, VOICE_LIST[_settings.voice]))
608
        basic.append(rs)
609

    
610
        rs = RadioSetting("pf2key", "PF2 Key",
611
                          RadioSettingValueList(
612
                              PF2KEY_LIST, PF2KEY_LIST[_settings.pf2key]))
613
        basic.append(rs)
614

    
615
        rs = RadioSetting("vox", "Vox",
616
                          RadioSettingValueBoolean(_settings.vox))
617
        basic.append(rs)
618

    
619
        rs = RadioSetting("voxgain", "VOX Level",
620
                          RadioSettingValueList(
621
                              VOX_LIST, VOX_LIST[_settings.voxgain]))
622
        basic.append(rs)
623

    
624
        rs = RadioSetting("voxdelay", "VOX Delay Time (Old | New)",
625
                          RadioSettingValueList(
626
                              VOXDELAY_LIST,
627
                              VOXDELAY_LIST[_settings.voxdelay]))
628
        basic.append(rs)
629

    
630
        rs = RadioSetting("save", "Battery Save",
631
                          RadioSettingValueBoolean(_settings.save))
632
        basic.append(rs)
633

    
634
        rs = RadioSetting("beep", "Beep",
635
                          RadioSettingValueBoolean(_settings.beep))
636
        basic.append(rs)
637

    
638
        def _filter(name):
639
            filtered = ""
640
            for char in str(name):
641
                if char in VALID_CHARS:
642
                    filtered += char
643
                else:
644
                    filtered += " "
645
            return filtered
646

    
647
        val = str(self._memobj.radio.id_0x200)
648
        if val == "\xFF" * 8:
649
            rs = RadioSetting("embedded_msg.line1", "Embedded Message 1",
650
                              RadioSettingValueString(0, 32, _filter(
651
                                  _message.line1)))
652
            basic.append(rs)
653

    
654
            rs = RadioSetting("embedded_msg.line2", "Embedded Message 2",
655
                              RadioSettingValueString(0, 32, _filter(
656
                                  _message.line2)))
657
            basic.append(rs)
658

    
659
        return top
660

    
661
    def set_settings(self, settings):
662
        for element in settings:
663
            if not isinstance(element, RadioSetting):
664
                self.set_settings(element)
665
                continue
666
            else:
667
                try:
668
                    if "." in element.get_name():
669
                        bits = element.get_name().split(".")
670
                        obj = self._memobj
671
                        for bit in bits[:-1]:
672
                            obj = getattr(obj, bit)
673
                        setting = bits[-1]
674
                    else:
675
                        obj = self._memobj.settings
676
                        setting = element.get_name()
677

    
678
                    LOG.debug("Setting %s = %s" % (setting, element.value))
679
                    setattr(obj, setting, element.value)
680
                except Exception as e:
681
                    LOG.debug(element.get_name())
682
                    raise
683

    
684
    @classmethod
685
    def match_model(cls, filedata, filename):
686
        match_size = False
687
        match_model = False
688

    
689
        # testing the file data size
690
        if len(filedata) in [0x0408, ]:
691
            match_size = True
692

    
693
        # testing the model fingerprint
694
        match_model = model_match(cls, filedata)
695

    
696
        if match_size and match_model:
697
            return True
698
        else:
699
            return False
700

    
701

    
702
@directory.register
703
class KDC1(RT22Radio):
704
    """WLN KD-C1"""
705
    VENDOR = "WLN"
706
    MODEL = "KD-C1"
707

    
708

    
709
@directory.register
710
class ZTX6(RT22Radio):
711
    """Zastone ZT-X6"""
712
    VENDOR = "Zastone"
713
    MODEL = "ZT-X6"
714

    
715

    
716
@directory.register
717
class LT316(RT22Radio):
718
    """Luiton LT-316"""
719
    VENDOR = "LUITON"
720
    MODEL = "LT-316"
721

    
722

    
723
@directory.register
724
class TDM8(RT22Radio):
725
    VENDOR = "TID"
726
    MODEL = "TD-M8"
727

    
728

    
729
@directory.register
730
class RT22FRS(RT22Radio):
731
    VENDOR = "Retevis"
732
    MODEL = "RT22FRS"
733

    
734

    
735
@directory.register
736
class RT622(RT22Radio):
737
    VENDOR = "Retevis"
738
    MODEL = "RT622"
(1-1/2)