retevis_rt21_rt21v_#1.py

Jim Unroe, 12/08/2021 07:05 pm

Download (43.7 kB)

 
1
# Copyright 2021 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 (
22
    bitwise,
23
    chirp_common,
24
    directory,
25
    errors,
26
    memmap,
27
    util,
28
)
29
from chirp.settings import (
30
    RadioSetting,
31
    RadioSettingGroup,
32
    RadioSettings,
33
    RadioSettingValueBoolean,
34
    RadioSettingValueInteger,
35
    RadioSettingValueList,
36
    RadioSettingValueString,
37
)
38

    
39
LOG = logging.getLogger(__name__)
40

    
41
MEM_FORMAT = """
42
#seekto 0x0010;
43
struct {
44
  lbcd rxfreq[4];       // RX Frequency           0-3
45
  lbcd txfreq[4];       // TX Frequency           4-7
46
  ul16 rx_tone;         // PL/DPL Decode          8-9
47
  ul16 tx_tone;         // PL/DPL Encode          A-B
48
  u8 unknown1:3,        //                        C
49
     bcl:2,             // Busy Lock
50
     unknown2:3;
51
  u8 unknown3:2,        //                        D
52
     highpower:1,       // Power Level
53
     wide:1,            // Bandwidth
54
     unknown4:4;
55
  u8 scramble_type:4,   // Scramble Type          E
56
     unknown5:4;
57
  u8 unknown6:4,
58
     scramble_type2:4;  // Scramble Type 2        F
59
} memory[16];
60

    
61
#seekto 0x011D;
62
struct {
63
  u8 unused:4,
64
     pf1:4;             // Programmable Function Key 1
65
} keys;
66

    
67
#seekto 0x012C;
68
struct {
69
  u8 use_scramble;      // Scramble Enable
70
  u8 unknown1[2];
71
  u8 voice;             // Voice Annunciation
72
  u8 tot;               // Time-out Timer
73
  u8 totalert;          // Time-out Timer Pre-alert
74
  u8 unknown2[2];
75
  u8 squelch;           // Squelch Level
76
  u8 save;              // Battery Saver
77
  u8 unknown3[3];
78
  u8 use_vox;           // VOX Enable
79
  u8 vox;               // VOX Gain
80
} settings;
81

    
82
#seekto 0x017E;
83
u8 skipflags[2];       // SCAN_ADD
84
"""
85

    
86
MEM_FORMAT_RB17A = """
87
struct memory {
88
  lbcd rxfreq[4];      // 0-3
89
  lbcd txfreq[4];      // 4-7
90
  ul16 rx_tone;        // 8-9
91
  ul16 tx_tone;        // A-B
92
  u8 unknown1:1,       // C
93
     compander:1,      // Compand
94
     bcl:2,            // Busy Channel Lock-out
95
     cdcss:1,          // Cdcss Mode
96
     scramble_type:3;  // Scramble Type
97
  u8 unknown2:4,       // D
98
     middlepower:1,    // Power Level-Middle
99
     unknown3:1,       //
100
     highpower:1,      // Power Level-High/Low
101
     wide:1;           // Bandwidth
102
  u8 unknown4;         // E
103
  u8 unknown5;         // F
104
};
105

    
106
#seekto 0x0010;
107
  struct memory lomems[16];
108

    
109
#seekto 0x0200;
110
  struct memory himems[14];
111

    
112
#seekto 0x011D;
113
struct {
114
  u8 pf1;              // 011D PF1 Key
115
  u8 topkey;           // 011E Top Key
116
} keys;
117

    
118
#seekto 0x012C;
119
struct {
120
  u8 use_scramble;     // 012C Scramble Enable
121
  u8 channel;          // 012D Channel Number
122
  u8 alarm;            // 012E Alarm Type
123
  u8 voice;            // 012F Voice Annunciation
124
  u8 tot;              // 0130 Time-out Timer
125
  u8 totalert;         // 0131 Time-out Timer Pre-alert
126
  u8 unknown2[2];
127
  u8 squelch;          // 0134 Squelch Level
128
  u8 save;             // 0135 Battery Saver
129
  u8 unknown3[3];
130
  u8 use_vox;          // 0139 VOX Enable
131
  u8 vox;              // 013A VOX Gain
132
} settings;
133

    
134
#seekto 0x017E;
135
u8 skipflags[4];       // Scan Add
136
"""
137

    
138
MEM_FORMAT_RB26 = """
139
#seekto 0x0000;
140
struct {
141
  lbcd rxfreq[4];      // RX Frequency           0-3
142
  lbcd txfreq[4];      // TX Frequency           4-7
143
  ul16 rx_tone;        // PL/DPL Decode          8-9
144
  ul16 tx_tone;        // PL/DPL Encode          A-B
145
  u8 compander:1,      // Compander              C
146
     unknown1:1,       //
147
     highpower:1,      // Power Level
148
     wide:1,           // Bandwidth
149
     bcl:1,            // Busy Lock  OFF=0 ON=1
150
     unknown2:3;       //
151
  u8 reserved[3];      // Reserved               D-F
152
} memory[30];
153

    
154
#seekto 0x002D;
155
struct {
156
  u8 unknown_1:1,      //                        002D
157
     chnumberd:1,      // Channel Number Disable
158
     gain:1,           // MIC Gain
159
     savem:1,          // Battery Save Mode
160
     save:1,           // Battery Save
161
     beep:1,           // Beep
162
     voice:1,          // Voice Prompts
163
     unknown_2:1;      //
164
  u8 squelch;          // Squelch                002E
165
  u8 tot;              // Time-out Timer         002F
166
  u8 channel_4[13];    //                        0030-003C
167
  u8 unknown_3[3];     //                        003D-003F
168
  u8 channel_5[13];    //                        0040-004C
169
  u8 unknown_4;        //                        004D
170
  u8 unknown_5[2];     //                        004E-004F
171
  u8 channel_6[13];    //                        0050-005C
172
  u8 unknown_6;        //                        005D
173
  u8 unknown_7[2];     //                        005E-005F
174
  u8 channel_7[13];    //                        0060-006C
175
  u8 warn;             // Warn Mode              006D
176
  u8 pf1;              // Key Set PF1            006E
177
  u8 pf2;              // Key Set PF2            006F
178
  u8 channel_8[13];    //                        0070-007C
179
  u8 unknown_8;        //                        007D
180
  u8 tail;             // QT/DQT Tail(inverted)  007E
181
} settings;
182

    
183
#seekto 0x01F0;
184
u8 skipflags[4];       // Scan Add
185

    
186
#seekto 0x029F;
187
struct {
188
  u8 chnumber;         // Channel Number         029F
189
} settings2;
190

    
191
#seekto 0x031D;
192
struct {
193
  u8 unused:7,         //                        031D
194
     vox:1;            // Vox
195
  u8 voxl;             // Vox Level              031E
196
  u8 voxd;             // Vox Delay              031F
197
} settings3;
198
"""
199

    
200
MEM_FORMAT_RT76 = """
201
#seekto 0x0000;
202
struct {
203
  lbcd rxfreq[4];      // RX Frequency           0-3
204
  lbcd txfreq[4];      // TX Frequency           4-7
205
  ul16 rx_tone;        // PL/DPL Decode          8-9
206
  ul16 tx_tone;        // PL/DPL Encode          A-B
207
  u8 compander:1,      // Compander              C
208
     unknown1:1,       //
209
     highpower:1,      // Power Level
210
     wide:1,           // Bandwidth
211
     unknown2:4;       //
212
  u8 reserved[3];      // Reserved               D-F
213
} memory[30];
214

    
215
#seekto 0x002D;
216
struct {
217
  u8 unknown_1:1,      //                        002D
218
     chnumberd:1,      // Channel Number Disable
219
     gain:1,           // MIC Gain                                 ---
220
     savem:1,          // Battery Save Mode                        ---
221
     save:1,           // Battery Save                             ---
222
     beep:1,           // Beep                                     ---
223
     voice:2;          // Voice Prompts                            ---
224
  u8 squelch;          // Squelch                002E              ---
225
  u8 tot;              // Time-out Timer         002F              ---
226
  u8 channel_4[13];    //                        0030-003C
227
  u8 unused:7,         //                        003D
228
     vox:1;            // Vox                                      ---
229
  u8 voxl;             // Vox Level              003E              ---
230
  u8 voxd;             // Vox Delay              003F              ---
231
  u8 channel_5[13];    //                        0040-004C
232
  u8 unknown_4;        //                        004D
233
  u8 unknown_5[2];     //                        004E-004F
234
  u8 channel_6[13];    //                        0050-005C
235
  u8 chnumber;         // Channel Number         005D              ---
236
  u8 unknown_7[2];     //                        005E-005F
237
  u8 channel_7[13];    //                        0060-006C
238
  u8 warn;             //                        006D              ---
239
} settings;
240
"""
241

    
242
CMD_ACK = "\x06"
243

    
244
ALARM_LIST = ["Local Alarm", "Remote Alarm"]
245
BCL_LIST = ["Off", "Carrier", "QT/DQT"]
246
CDCSS_LIST = ["Normal Code", "Special Code 2", "Special Code 1"]
247
GAIN_LIST = ["Standard", "Enhanced"]
248
PFKEY_LIST = ["None", "Monitor", "Lamp", "Warn", "VOX", "VOX Delay",
249
              "Key Lock", "Scan"]
250
SAVE_LIST = ["Standard", "Super"]
251
SCRAMBLE_LIST = ["%s" % x for x in range(1, 9)]
252
TIMEOUTTIMER_LIST = ["%s seconds" % x for x in range(15, 615, 15)]
253
TOTALERT_LIST = ["Off"] + ["%s seconds" % x for x in range(1, 11)]
254
VOICE_LIST = ["Off", "Chinese", "English"]
255
VOICE_LIST2 = ["Off", "English"]
256
VOICE_LIST3 = VOICE_LIST2 + ["Chinese"]
257
VOX_LIST = ["OFF"] + ["%s" % x for x in range(1, 17)]
258
VOXD_LIST = ["0.5", "1.0", "1.5", "2.0", "2.5", "3.0"]
259
VOXL_LIST = ["OFF"] + ["%s" % x for x in range(1, 9)]
260
WARN_LIST = ["OFF", "Native Warn", "Remote Warn"]
261
PF1_CHOICES = ["None", "Monitor", "Scan", "Scramble", "Alarm"]
262
PF1_VALUES = [0x0F, 0x04, 0x06, 0x08, 0x0C]
263
TOPKEY_CHOICES = ["None", "Alarming"]
264
TOPKEY_VALUES = [0xFF, 0x0C]
265

    
266
SETTING_LISTS = {
267
    "alarm": ALARM_LIST,
268
    "bcl": BCL_LIST,
269
    "cdcss": CDCSS_LIST,
270
    "gain": GAIN_LIST,
271
    "pfkey": PFKEY_LIST,
272
    "save": SAVE_LIST,
273
    "scramble": SCRAMBLE_LIST,
274
    "tot": TIMEOUTTIMER_LIST,
275
    "totalert": TOTALERT_LIST,
276
    "voice": VOICE_LIST,
277
    "voice": VOICE_LIST2,
278
    "voice": VOICE_LIST3,
279
    "vox": VOX_LIST,
280
    "voxd": VOXD_LIST,
281
    "voxl": VOXL_LIST,
282
    "warn": WARN_LIST,
283
    }
284

    
285
GMRS_FREQS1 = [462.5625, 462.5875, 462.6125, 462.6375, 462.6625,
286
               462.6875, 462.7125]
287
GMRS_FREQS2 = [467.5625, 467.5875, 467.6125, 467.6375, 467.6625,
288
               467.6875, 467.7125]
289
GMRS_FREQS3 = [462.5500, 462.5750, 462.6000, 462.6250, 462.6500,
290
               462.6750, 462.7000, 462.7250]
291
GMRS_FREQS = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
292

    
293
MURS_FREQS = [151.820, 151.880, 151.940, 154.570, 154.600] ##* 3
294
##FM_MODE = [3, 4, 8, 9, 13, 14]
295

    
296

    
297
def _enter_programming_mode(radio):
298
    serial = radio.pipe
299

    
300
    exito = False
301
    for i in range(0, 5):
302
        serial.write(radio._magic)
303
        ack = serial.read(1)
304
        if ack == "\x00":
305
            ack = serial.read(1)
306

    
307
        try:
308
            if ack == CMD_ACK:
309
                exito = True
310
                break
311
        except:
312
            LOG.debug("Attempt #%s, failed, trying again" % i)
313
            pass
314

    
315
    # check if we had EXITO
316
    if exito is False:
317
        msg = "The radio did not accept program mode after five tries.\n"
318
        msg += "Check you interface cable and power cycle your radio."
319
        raise errors.RadioError(msg)
320

    
321
    try:
322
        serial.write("\x02")
323
        ident = serial.read(8)
324
    except:
325
        raise errors.RadioError("Error communicating with radio")
326

    
327
    if not ident == radio._fingerprint:
328
        LOG.debug(util.hexprint(ident))
329
        raise errors.RadioError("Radio returned unknown identification string")
330

    
331
    try:
332
        serial.write(CMD_ACK)
333
        ack = serial.read(1)
334
    except:
335
        raise errors.RadioError("Error communicating with radio")
336

    
337
    if ack != CMD_ACK:
338
        raise errors.RadioError("Radio refused to enter programming mode")
339

    
340

    
341
def _exit_programming_mode(radio):
342
    serial = radio.pipe
343
    try:
344
        serial.write("E")
345
    except:
346
        raise errors.RadioError("Radio refused to exit programming mode")
347

    
348

    
349
def _read_block(radio, block_addr, block_size):
350
    serial = radio.pipe
351

    
352
    cmd = struct.pack(">cHb", 'R', block_addr, block_size)
353
    expectedresponse = "W" + cmd[1:]
354
    LOG.debug("Reading block %04x..." % (block_addr))
355

    
356
    try:
357
        serial.write(cmd)
358
        response = serial.read(4 + block_size)
359
        if response[:4] != expectedresponse:
360
            raise Exception("Error reading block %04x." % (block_addr))
361

    
362
        block_data = response[4:]
363

    
364
        serial.write(CMD_ACK)
365
        ack = serial.read(1)
366
    except:
367
        raise errors.RadioError("Failed to read block at %04x" % block_addr)
368

    
369
    if ack != CMD_ACK:
370
        raise Exception("No ACK reading block %04x." % (block_addr))
371

    
372
    return block_data
373

    
374

    
375
def _rb26_read_block(radio, block_addr, block_size):
376
    serial = radio.pipe
377

    
378
    cmd = struct.pack(">cHb", 'R', block_addr, block_size)
379
    expectedresponse = "W" + cmd[1:]
380
    LOG.debug("Reading block %04x..." % (block_addr))
381

    
382
    try:
383
        serial.write(cmd)
384
        response = serial.read(4 + block_size)
385
        if response[:4] != expectedresponse:
386
            raise Exception("Error reading block %04x." % (block_addr))
387

    
388
        block_data = response[4:]
389

    
390
        if block_addr != 0:
391
            serial.write(CMD_ACK)
392
            ack = serial.read(1)
393
    except:
394
        raise errors.RadioError("Failed to read block at %04x" % block_addr)
395

    
396
    if block_addr != 0:
397
        if ack != CMD_ACK:
398
            raise Exception("No ACK reading block %04x." % (block_addr))
399

    
400
    return block_data
401

    
402

    
403
def _write_block(radio, block_addr, block_size):
404
    serial = radio.pipe
405

    
406
    cmd = struct.pack(">cHb", 'W', block_addr, block_size)
407
    data = radio.get_mmap()[block_addr:block_addr + block_size]
408

    
409
    LOG.debug("Writing Data:")
410
    LOG.debug(util.hexprint(cmd + data))
411

    
412
    try:
413
        serial.write(cmd + data)
414
        if serial.read(1) != CMD_ACK:
415
            raise Exception("No ACK")
416
    except:
417
        raise errors.RadioError("Failed to send block "
418
                                "to radio at %04x" % block_addr)
419

    
420

    
421
def do_download(radio):
422
    LOG.debug("download")
423
    _enter_programming_mode(radio)
424

    
425
    data = ""
426

    
427
    status = chirp_common.Status()
428
    status.msg = "Cloning from radio"
429

    
430
    status.cur = 0
431
    status.max = radio._memsize
432

    
433
    for addr in range(0, radio._memsize, radio.BLOCK_SIZE):
434
        status.cur = addr + radio.BLOCK_SIZE
435
        radio.status_fn(status)
436

    
437
        if radio.MODEL == "RB26" or radio.MODEL == "RT76":
438
            block = _rb26_read_block(radio, addr, radio.BLOCK_SIZE)
439
        else:
440
            block = _read_block(radio, addr, radio.BLOCK_SIZE)
441
        data += block
442

    
443
        LOG.debug("Address: %04x" % addr)
444
        LOG.debug(util.hexprint(block))
445

    
446
    _exit_programming_mode(radio)
447

    
448
    return memmap.MemoryMap(data)
449

    
450

    
451
def do_upload(radio):
452
    status = chirp_common.Status()
453
    status.msg = "Uploading to radio"
454

    
455
    _enter_programming_mode(radio)
456

    
457
    status.cur = 0
458
    status.max = radio._memsize
459

    
460
    for start_addr, end_addr in radio._ranges:
461
        for addr in range(start_addr, end_addr, radio.BLOCK_SIZE_UP):
462
            status.cur = addr + radio.BLOCK_SIZE_UP
463
            radio.status_fn(status)
464
            _write_block(radio, addr, radio.BLOCK_SIZE_UP)
465

    
466
    _exit_programming_mode(radio)
467

    
468

    
469
def model_match(cls, data):
470
    """Match the opened/downloaded image to the correct version"""
471
    rid = data[0x01B8:0x01BE]
472

    
473
    return rid.startswith("P3207")
474

    
475

    
476
@directory.register
477
class RT21Radio(chirp_common.CloneModeRadio):
478
    """RETEVIS RT21"""
479
    VENDOR = "Retevis"
480
    MODEL = "RT21"
481
    BAUD_RATE = 9600
482
    BLOCK_SIZE = 0x10
483
    BLOCK_SIZE_UP = 0x10
484

    
485
    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=1.00),
486
                    chirp_common.PowerLevel("High", watts=2.50)]
487

    
488
    _magic = "PRMZUNE"
489
    _fingerprint = "P3207s\xF8\xFF"
490
    _upper = 16
491
    _skipflags = True
492
    _reserved = False
493
    _gmrs = False
494
    _murs = False
495

    
496
    _ranges = [
497
               (0x0000, 0x0400),
498
              ]
499
    _memsize = 0x0400
500

    
501
    def get_features(self):
502
        rf = chirp_common.RadioFeatures()
503
        rf.has_settings = False ##True
504
        rf.has_bank = False
505
        rf.has_ctone = True
506
        rf.has_cross = True
507
        rf.has_rx_dtcs = True
508
        rf.has_tuning_step = False
509
        rf.can_odd_split = True
510
        rf.has_name = False
511
        rf.valid_skips = ["", "S"]
512
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
513
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
514
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
515
        rf.valid_power_levels = self.POWER_LEVELS
516
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
517
        rf.valid_modes = ["NFM", "FM"]  # 12.5 KHz, 25 kHz.
518
        rf.memory_bounds = (1, self._upper)
519
        rf.valid_tuning_steps = [2.5, 5., 6.25, 10., 12.5, 25.]
520
        rf.valid_bands = [(400000000, 480000000)]
521

    
522
        return rf
523

    
524
    def process_mmap(self):
525
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
526

    
527
    def validate_memory(self, mem):
528
        msgs = ""
529
        msgs = chirp_common.CloneModeRadio.validate_memory(self, mem)
530

    
531
        _msg_freq = 'Memory location cannot change frequency'
532
        _msg_simplex = 'Memory location only supports Duplex:(None)'
533
        _msg_duplex = 'Memory location only supports Duplex: +'
534
        _msg_offset = 'Memory location only supports Offset: 5.000000'
535
        _msg_nfm = 'Memory location only supports Mode: NFM'
536
        _msg_txp = 'Memory location only supports Power: Low'
537

    
538
        # GMRS models
539
        if self._gmrs:
540
            # range of memories with values set by FCC rules
541
            if mem.freq != int(GMRS_FREQS[mem.number - 1] * 1000000):
542
                # warn user can't change frequency
543
                msgs.append(chirp_common.ValidationError(_msg_freq))
544

    
545
            # channels 1 - 22 are simplex only
546
            if mem.number <= 22:
547
                if str(mem.duplex) != "":
548
                    # warn user can't change duplex
549
                    msgs.append(chirp_common.ValidationError(_msg_simplex))
550

    
551
            # channels 23 - 30 are +5 MHz duplex only
552
            if mem.number >= 23:
553
                if str(mem.duplex) != "+":
554
                    # warn user can't change duplex
555
                    msgs.append(chirp_common.ValidationError(_msg_duplex))
556

    
557
                if str(mem.offset) != "5000000":
558
                    # warn user can't change offset
559
                    msgs.append(chirp_common.ValidationError(_msg_offset))
560

    
561
            # channels 8 - 14 are low power NFM only
562
            if mem.number >= 8 and mem.number <= 14:
563
                if mem.mode != "NFM":
564
                    # warn user can't change mode
565
                    msgs.append(chirp_common.ValidationError(_msg_nfm))
566

    
567
                if mem.power != "Low":
568
                    # warn user can't change power
569
                    msgs.append(chirp_common.ValidationError(_msg_txp))
570

    
571
        # MURS models
572
        if self._murs:
573
            if mem.number <= 5:
574
                # range of memories with values set by FCC rules
575
                if mem.freq != int(MURS_FREQS[mem.number - 1] * 1000000):
576
                    # warn user can't change frequency
577
                    msgs.append(chirp_common.ValidationError(_msg_freq))
578

    
579
            # channels 1 - 15 are simplex only
580
            if mem.duplex not in ['', 'off']:
581
                # warn user can't change duplex
582
                msgs.append(chirp_common.ValidationError(_msg_simplex))
583

    
584
        return msgs
585

    
586
    def sync_in(self):
587
        """Download from radio"""
588
        try:
589
            data = do_download(self)
590
        except errors.RadioError:
591
            # Pass through any real errors we raise
592
            raise
593
        except:
594
            # If anything unexpected happens, make sure we raise
595
            # a RadioError and log the problem
596
            LOG.exception('Unexpected error during download')
597
            raise errors.RadioError('Unexpected error communicating '
598
                                    'with the radio')
599
        self._mmap = data
600
        self.process_mmap()
601

    
602
    def sync_out(self):
603
        """Upload to radio"""
604
        try:
605
            do_upload(self)
606
        except:
607
            # If anything unexpected happens, make sure we raise
608
            # a RadioError and log the problem
609
            LOG.exception('Unexpected error during upload')
610
            raise errors.RadioError('Unexpected error communicating '
611
                                    'with the radio')
612

    
613
    def get_raw_memory(self, number):
614
        return repr(self._memobj.memory[number - 1])
615

    
616
    def _is_txinh(self, _mem):
617
        raw_tx = ""
618
        for i in range(0, 4):
619
            raw_tx += _mem.txfreq[i].get_raw()
620
        return raw_tx == "\xFF\xFF\xFF\xFF"
621

    
622
    def _get_tone(self, _mem, mem):
623
        def _get_dcs(val):
624
            code = int("%03o" % (val & 0x07FF))
625
            pol = (val & 0x8000) and "R" or "N"
626
            return code, pol
627

    
628
        if _mem.tx_tone != 0xFFFF and _mem.tx_tone > 0x2000:
629
            tcode, tpol = _get_dcs(_mem.tx_tone)
630
            mem.dtcs = tcode
631
            txmode = "DTCS"
632
        elif _mem.tx_tone != 0xFFFF:
633
            mem.rtone = _mem.tx_tone / 10.0
634
            txmode = "Tone"
635
        else:
636
            txmode = ""
637

    
638
        if _mem.rx_tone != 0xFFFF and _mem.rx_tone > 0x2000:
639
            rcode, rpol = _get_dcs(_mem.rx_tone)
640
            mem.rx_dtcs = rcode
641
            rxmode = "DTCS"
642
        elif _mem.rx_tone != 0xFFFF:
643
            mem.ctone = _mem.rx_tone / 10.0
644
            rxmode = "Tone"
645
        else:
646
            rxmode = ""
647

    
648
        if txmode == "Tone" and not rxmode:
649
            mem.tmode = "Tone"
650
        elif txmode == rxmode and txmode == "Tone" and mem.rtone == mem.ctone:
651
            mem.tmode = "TSQL"
652
        elif txmode == rxmode and txmode == "DTCS" and mem.dtcs == mem.rx_dtcs:
653
            mem.tmode = "DTCS"
654
        elif rxmode or txmode:
655
            mem.tmode = "Cross"
656
            mem.cross_mode = "%s->%s" % (txmode, rxmode)
657

    
658
        if mem.tmode == "DTCS":
659
            mem.dtcs_polarity = "%s%s" % (tpol, rpol)
660

    
661
        LOG.debug("Got TX %s (%i) RX %s (%i)" %
662
                  (txmode, _mem.tx_tone, rxmode, _mem.rx_tone))
663

    
664
    def get_memory(self, number):
665
        if self._skipflags:
666
            bitpos = (1 << ((number - 1) % 8))
667
            bytepos = ((number - 1) / 8)
668
            LOG.debug("bitpos %s" % bitpos)
669
            LOG.debug("bytepos %s" % bytepos)
670
            _skp = self._memobj.skipflags[bytepos]
671

    
672
        mem = chirp_common.Memory()
673

    
674
        mem.number = number
675

    
676
        if self.MODEL == "RB17A":
677
            if mem.number < 17:
678
                _mem = self._memobj.lomems[number - 1]
679
            else:
680
                _mem = self._memobj.himems[number - 17]
681
        else:
682
            _mem = self._memobj.memory[number - 1]
683

    
684
        if self._reserved:
685
            _rsvd = _mem.reserved.get_raw()
686

    
687
        mem.freq = int(_mem.rxfreq) * 10
688

    
689
        # We'll consider any blank (i.e. 0MHz frequency) to be empty
690
        if mem.freq == 0:
691
            mem.empty = True
692
            return mem
693

    
694
        if _mem.rxfreq.get_raw() == "\xFF\xFF\xFF\xFF":
695
            mem.freq = 0
696
            mem.empty = True
697
            return mem
698

    
699
        if _mem.get_raw() == ("\xFF" * 16):
700
            LOG.debug("Initializing empty memory")
701
            if self.MODEL == "RB17A":
702
                _mem.set_raw("\x00" * 13 + "\x04\xFF\xFF")
703
            if self.MODEL == "RB26" or self.MODEL == "RT76":
704
                _mem.set_raw("\x00" * 13 + _rsvd)
705
            else:
706
                _mem.set_raw("\x00" * 13 + "\x30\x8F\xF8")
707

    
708
        if self._is_txinh(_mem):
709
            mem.duplex = "off"
710
            mem.offset = 0
711
        elif int(_mem.rxfreq) == int(_mem.txfreq):
712
            mem.duplex = ""
713
            mem.offset = 0
714
        else:
715
            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
716
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
717

    
718
        mem.mode = _mem.wide and "FM" or "NFM"
719

    
720
        self._get_tone(_mem, mem)
721

    
722
        mem.power = self.POWER_LEVELS[_mem.highpower]
723

    
724
        if self.MODEL != "RT76":
725
            mem.skip = "" if (_skp & bitpos) else "S"
726
            LOG.debug("mem.skip %s" % mem.skip)
727

    
728
        mem.extra = RadioSettingGroup("Extra", "extra")
729

    
730
        if self.MODEL == "RT21" or self.MODEL == "RT21V" or self.MODEL == "RB17A":
731
            rs = RadioSettingValueList(BCL_LIST, BCL_LIST[_mem.bcl])
732
            rset = RadioSetting("bcl", "Busy Channel Lockout", rs)
733
            mem.extra.append(rset)
734

    
735
            rs = RadioSettingValueList(SCRAMBLE_LIST,
736
                                       SCRAMBLE_LIST[_mem.scramble_type - 8])
737
            rset = RadioSetting("scramble_type", "Scramble Type", rs)
738
            mem.extra.append(rset)
739

    
740
            if self.MODEL == "RB17A":
741
                rs = RadioSettingValueList(CDCSS_LIST, CDCSS_LIST[_mem.cdcss])
742
                rset = RadioSetting("cdcss", "Cdcss Mode", rs)
743
                mem.extra.append(rset)
744

    
745
        if self.MODEL == "RB26" or self.MODEL == "RT76":
746
            if self.MODEL == "RB26":
747
                rs = RadioSettingValueBoolean(_mem.bcl)
748
                rset = RadioSetting("bcl", "Busy Channel Lockout", rs)
749
                mem.extra.append(rset)
750

    
751
            rs = RadioSettingValueBoolean(_mem.compander)
752
            rset = RadioSetting("compander", "Compander", rs)
753
            mem.extra.append(rset)
754

    
755
        if self._gmrs:
756
            GMRS_IMMUTABLE = ["freq", "duplex", "offset"]
757
            if mem.number >= 8 and mem.number <= 14:
758
                mem.immutable = GMRS_IMMUTABLE + ["power", "mode"]
759
            else:
760
                mem.immutable = GMRS_IMMUTABLE
761

    
762
        #if self._murs:
763
        #    MURS_IMMUTABLE = ["freq", "duplex", "offset"]
764
        #    if mem.mode == "FM" and int(mem.number - 1) not in FM_MODE:
765
        #        mem.immutable = MURS_IMMUTABLE + ["mode"]
766
        #    ##else:
767
        #    ##    mem.immutable = MURS_IMMUTABLE
768

    
769
        return mem
770

    
771
    def _set_tone(self, mem, _mem):
772
        def _set_dcs(code, pol):
773
            val = int("%i" % code, 8) + 0x2800
774
            if pol == "R":
775
                val += 0x8000
776
            return val
777

    
778
        rx_mode = tx_mode = None
779
        rx_tone = tx_tone = 0xFFFF
780

    
781
        if mem.tmode == "Tone":
782
            tx_mode = "Tone"
783
            rx_mode = None
784
            tx_tone = int(mem.rtone * 10)
785
        elif mem.tmode == "TSQL":
786
            rx_mode = tx_mode = "Tone"
787
            rx_tone = tx_tone = int(mem.ctone * 10)
788
        elif mem.tmode == "DTCS":
789
            tx_mode = rx_mode = "DTCS"
790
            tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
791
            rx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[1])
792
        elif mem.tmode == "Cross":
793
            tx_mode, rx_mode = mem.cross_mode.split("->")
794
            if tx_mode == "DTCS":
795
                tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
796
            elif tx_mode == "Tone":
797
                tx_tone = int(mem.rtone * 10)
798
            if rx_mode == "DTCS":
799
                rx_tone = _set_dcs(mem.rx_dtcs, mem.dtcs_polarity[1])
800
            elif rx_mode == "Tone":
801
                rx_tone = int(mem.ctone * 10)
802

    
803
        _mem.rx_tone = rx_tone
804
        _mem.tx_tone = tx_tone
805

    
806
        LOG.debug("Set TX %s (%i) RX %s (%i)" %
807
                  (tx_mode, _mem.tx_tone, rx_mode, _mem.rx_tone))
808

    
809
    def set_memory(self, mem):
810
        if self._skipflags:
811
            bitpos = (1 << ((mem.number - 1) % 8))
812
            bytepos = ((mem.number - 1) / 8)
813
            LOG.debug("bitpos %s" % bitpos)
814
            LOG.debug("bytepos %s" % bytepos)
815
            _skp = self._memobj.skipflags[bytepos]
816

    
817
        if self.MODEL == "RB17A":
818
            if mem.number < 17:
819
                _mem = self._memobj.lomems[mem.number - 1]
820
            else:
821
                _mem = self._memobj.himems[mem.number - 17]
822
        else:
823
            _mem = self._memobj.memory[mem.number - 1]
824

    
825
        if self._reserved:
826
            _rsvd = _mem.reserved.get_raw()
827

    
828
        if mem.empty:
829
            if self.MODEL == "RB17A":
830
                _mem.set_raw("\xFF" * 12 + "\x00\x00\xFF\xFF")
831
            elif self.MODEL == "RB26" or self.MODEL == "RT76":
832
                _mem.set_raw("\xFF" * 13 + _rsvd)
833
            else:
834
                _mem.set_raw("\xFF" * (_mem.size() / 8))
835

    
836
            if self._gmrs:
837
                GMRS_FREQ = int(GMRS_FREQS[mem.number - 1] * 100000)
838
                if mem.number > 22:
839
                    _mem.rxfreq = GMRS_FREQ
840
                    _mem.txfreq = int(_mem.rxfreq) + 500000
841
                    _mem.wide = True
842
                else:
843
                    _mem.rxfreq = _mem.txfreq = GMRS_FREQ
844
                if mem.number >= 8 and mem.number <= 14:
845
                    _mem.wide = False
846
                    _mem.highpower = False
847
                else:
848
                    _mem.wide = True
849
                    _mem.highpower = True
850

    
851
            #if self._murs:
852
            #    ##_mem.set_raw("\xFF" * 12 + "\x00\x20\x8F\xF8")
853
            #    MURS_FREQ = int(MURS_FREQS[mem.number - 1] * 100000)
854
            #    print "murs freq"
855
            #    print MURS_FREQ
856
            #    _mem.rxfreq = _mem.txfreq = MURS_FREQ
857
            #    ##_mem.highpower = True
858
            #    if int(mem.number - 1) not in FM_MODE:
859
            #        _mem.wide = False
860
            #    ##else:
861
            #    ##    _mem.wide = True
862

    
863
        if self._murs:
864
            mem.mode = "NFM"
865
            mem.duplex = ''
866
            mem.offset = 0
867
            mem.power = self.POWER_LEVELS[1]
868
            if mem.number <= 5:
869
                MURS_FREQ = int(MURS_FREQS[mem.number - 1] * 1000000)
870
                mem.freq = MURS_FREQ
871
                #mem.duplex = ''
872
                #mem.offset = 0
873
                #mem.power = self.POWER_LEVELS[1]
874
                if mem.number <= 5:
875
                    #mem.duplex = ''
876
                    #mem.offset = 0
877
                    if mem.number <= 3:
878
                        mem.mode = "NFM"
879
                        #mem.power = self.POWER_LEVELS[1]
880
                    else:
881
                        mem.mode = "FM"
882
            elif float(mem.freq) / 1000000 in MURS_FREQS:
883
                print "frequency"
884
                print float(mem.freq) / 1000000
885
                print float(mem.freq) / 1000000 < 154.0
886

    
887
                if float(mem.freq) / 1000000 < 154.0:
888
                    #mem.duplex = ''
889
                    #mem.offset = 0
890
                    mem.mode = "NFM"
891
                    #mem.power = self.POWER_LEVELS[1]
892
                else:
893
                    #mem.duplex = ''
894
                    #mem.offset = 0
895
                    mem.mode = "FM"
896
                    #mem.power = self.POWER_LEVELS[1]
897
            else:
898
                mem.duplex = 'off'
899
                #mem.offset = 0
900
                mem.mode = "FM"
901
                #mem.power = self.POWER_LEVELS[1]
902
                ##for i in range(0, 4):
903
                ##    _mem.txfreq[i].set_raw("\xFF")
904

    
905
        print mem.number
906
        print "duplex"
907
        print mem.duplex
908
        print "offset"
909
        print mem.offset
910

    
911
        if self.MODEL == "RB17A":
912
            _mem.set_raw("\x00" * 13 + "\x00\xFF\xFF")
913
        elif self.MODEL == "RB26" or self.MODEL == "RT76":
914
            _mem.set_raw("\x00" * 13 + _rsvd)
915
        else:
916
            _mem.set_raw("\x00" * 13 + "\x30\x8F\xF8")
917

    
918
        _mem.rxfreq = mem.freq / 10
919

    
920
        if mem.duplex == "off":
921
            for i in range(0, 4):
922
                _mem.txfreq[i].set_raw("\xFF")
923
        elif mem.duplex == "split":
924
            _mem.txfreq = mem.offset / 10
925
        elif mem.duplex == "+":
926
            _mem.txfreq = (mem.freq + mem.offset) / 10
927
        elif mem.duplex == "-":
928
            _mem.txfreq = (mem.freq - mem.offset) / 10
929
        else:
930
            _mem.txfreq = mem.freq / 10
931

    
932
        _mem.wide = mem.mode == "FM"
933

    
934
        self._set_tone(mem, _mem)
935

    
936
        _mem.highpower = mem.power == self.POWER_LEVELS[1]
937

    
938
        if self.MODEL != "RT76":
939
            if mem.skip != "S":
940
                _skp |= bitpos
941
            else:
942
                _skp &= ~bitpos
943
            LOG.debug("_skp %s" % _skp)
944

    
945
        for setting in mem.extra:
946
            if setting.get_name() == "scramble_type":
947
                setattr(_mem, setting.get_name(), int(setting.value) + 8)
948
                setattr(_mem, "scramble_type2", int(setting.value) + 8)
949
            else:
950
                setattr(_mem, setting.get_name(), setting.value)
951

    
952
    def get_settings(self):
953
        _settings = self._memobj.settings
954
        basic = RadioSettingGroup("basic", "Basic Settings")
955
        top = RadioSettings(basic)
956

    
957
        if self.MODEL == "RT21" or self.MODEL == "RT21"  or self.MODEL == "RB17A":
958
            _keys = self._memobj.keys
959

    
960
            rs = RadioSettingValueList(TIMEOUTTIMER_LIST,
961
                                       TIMEOUTTIMER_LIST[_settings.tot - 1])
962
            rset = RadioSetting("tot", "Time-out timer", rs)
963
            basic.append(rset)
964

    
965
            rs = RadioSettingValueList(TOTALERT_LIST,
966
                                       TOTALERT_LIST[_settings.totalert])
967
            rset = RadioSetting("totalert", "TOT Pre-alert", rs)
968
            basic.append(rset)
969

    
970
            rs = RadioSettingValueInteger(0, 9, _settings.squelch)
971
            rset = RadioSetting("squelch", "Squelch Level", rs)
972
            basic.append(rset)
973

    
974
            rs = RadioSettingValueList(VOICE_LIST, VOICE_LIST[_settings.voice])
975
            rset = RadioSetting("voice", "Voice Annumciation", rs)
976
            basic.append(rset)
977

    
978
            if self.MODEL == "RB17A":
979
                rs = RadioSettingValueList(ALARM_LIST,
980
                                           ALARM_LIST[_settings.alarm])
981
                rset = RadioSetting("alarm", "Alarm Type", rs)
982
                basic.append(rset)
983

    
984
            rs = RadioSettingValueBoolean(_settings.save)
985
            rset = RadioSetting("save", "Battery Saver", rs)
986
            basic.append(rset)
987

    
988
            rs = RadioSettingValueBoolean(_settings.use_scramble)
989
            rset = RadioSetting("use_scramble", "Scramble", rs)
990
            basic.append(rset)
991

    
992
            rs = RadioSettingValueBoolean(_settings.use_vox)
993
            rset = RadioSetting("use_vox", "VOX", rs)
994
            basic.append(rset)
995

    
996
            rs = RadioSettingValueList(VOX_LIST, VOX_LIST[_settings.vox])
997
            rset = RadioSetting("vox", "VOX Gain", rs)
998
            basic.append(rset)
999

    
1000
            def apply_pf1_listvalue(setting, obj):
1001
                LOG.debug("Setting value: " + str(
1002
                          setting.value) + " from list")
1003
                val = str(setting.value)
1004
                index = PF1_CHOICES.index(val)
1005
                val = PF1_VALUES[index]
1006
                obj.set_value(val)
1007

    
1008
            if _keys.pf1 in PF1_VALUES:
1009
                idx = PF1_VALUES.index(_keys.pf1)
1010
            else:
1011
                idx = LIST_DTMF_SPECIAL_VALUES.index(0x04)
1012
            rs = RadioSettingValueList(PF1_CHOICES, PF1_CHOICES[idx])
1013
            rset = RadioSetting("keys.pf1", "PF1 Key Function", rs)
1014
            rset.set_apply_callback(apply_pf1_listvalue, _keys.pf1)
1015
            basic.append(rset)
1016

    
1017
            def apply_topkey_listvalue(setting, obj):
1018
                LOG.debug("Setting value: " + str(setting.value) +
1019
                          " from list")
1020
                val = str(setting.value)
1021
                index = TOPKEY_CHOICES.index(val)
1022
                val = TOPKEY_VALUES[index]
1023
                obj.set_value(val)
1024

    
1025
            if self.MODEL == "RB17A":
1026
                if _keys.topkey in TOPKEY_VALUES:
1027
                    idx = TOPKEY_VALUES.index(_keys.topkey)
1028
                else:
1029
                    idx = TOPKEY_VALUES.index(0x0C)
1030
                rs = RadioSettingValueList(TOPKEY_CHOICES, TOPKEY_CHOICES[idx])
1031
                rset = RadioSetting("keys.topkey", "Top Key Function", rs)
1032
                rset.set_apply_callback(apply_topkey_listvalue, _keys.topkey)
1033
                basic.append(rset)
1034

    
1035
        if self.MODEL == "RB26" or self.MODEL == "RT76":
1036
            if self.MODEL == "RB26":
1037
                _settings2 = self._memobj.settings2
1038
                _settings3 = self._memobj.settings3
1039

    
1040
            rs = RadioSettingValueInteger(0, 9, _settings.squelch)
1041
            rset = RadioSetting("squelch", "Squelch Level", rs)
1042
            basic.append(rset)
1043

    
1044
            rs = RadioSettingValueList(TIMEOUTTIMER_LIST,
1045
                                       TIMEOUTTIMER_LIST[_settings.tot - 1])
1046
            rset = RadioSetting("tot", "Time-out timer", rs)
1047
            basic.append(rset)
1048

    
1049
            if self.MODEL == "RT76":
1050
                rs = RadioSettingValueList(VOICE_LIST3,
1051
                                           VOICE_LIST3[_settings.voice])
1052
                rset = RadioSetting("voice", "Voice Annumciation", rs)
1053
                basic.append(rset)
1054

    
1055
            if self.MODEL == "RB26":
1056
                rs = RadioSettingValueList(VOICE_LIST2,
1057
                                           VOICE_LIST2[_settings.voice])
1058
                rset = RadioSetting("voice", "Voice Annumciation", rs)
1059
                basic.append(rset)
1060

    
1061
                rs = RadioSettingValueBoolean(not _settings.chnumberd)
1062
                rset = RadioSetting("chnumberd", "Channel Number Enable", rs)
1063
                basic.append(rset)
1064

    
1065
            rs = RadioSettingValueBoolean(_settings.save)
1066
            rset = RadioSetting("save", "Battery Save", rs)
1067
            basic.append(rset)
1068

    
1069
            rs = RadioSettingValueBoolean(_settings.beep)
1070
            rset = RadioSetting("beep", "Beep", rs)
1071
            basic.append(rset)
1072

    
1073
            if self.MODEL == "RB26":
1074
                rs = RadioSettingValueBoolean(not _settings.tail)
1075
                rset = RadioSetting("tail", "QT/DQT Tail", rs)
1076
                basic.append(rset)
1077

    
1078
            rs = RadioSettingValueList(SAVE_LIST, SAVE_LIST[_settings.savem])
1079
            rset = RadioSetting("savem", "Battery Save Mode", rs)
1080
            basic.append(rset)
1081

    
1082
            rs = RadioSettingValueList(GAIN_LIST, GAIN_LIST[_settings.gain])
1083
            rset = RadioSetting("gain", "MIC Gain", rs)
1084
            basic.append(rset)
1085

    
1086
            rs = RadioSettingValueList(WARN_LIST, WARN_LIST[_settings.warn])
1087
            rset = RadioSetting("warn", "Warn Mode", rs)
1088
            basic.append(rset)
1089

    
1090
            if self.MODEL == "RB26":
1091
                rs = RadioSettingValueBoolean(_settings3.vox)
1092
                rset = RadioSetting("settings3.vox", "Vox Function", rs)
1093
                basic.append(rset)
1094

    
1095
                rs = RadioSettingValueList(VOXL_LIST,
1096
                                           VOXL_LIST[_settings3.voxl])
1097
                rset = RadioSetting("settings3.voxl", "Vox Level", rs)
1098
                basic.append(rset)
1099

    
1100
                rs = RadioSettingValueList(VOXD_LIST,
1101
                                           VOXD_LIST[_settings3.voxd])
1102
                rset = RadioSetting("settings3.voxd", "Vox Delay", rs)
1103
                basic.append(rset)
1104

    
1105
                rs = RadioSettingValueList(PFKEY_LIST,
1106
                                           PFKEY_LIST[_settings.pf1])
1107
                rset = RadioSetting("pf1", "PF1 Key Set", rs)
1108
                basic.append(rset)
1109

    
1110
                rs = RadioSettingValueList(PFKEY_LIST,
1111
                                           PFKEY_LIST[_settings.pf2])
1112
                rset = RadioSetting("pf2", "PF2 Key Set", rs)
1113
                basic.append(rset)
1114

    
1115
                rs = RadioSettingValueInteger(1, 30, _settings2.chnumber + 1)
1116
                rset = RadioSetting("settings2.chnumber", "Channel Number", rs)
1117
                basic.append(rset)
1118

    
1119
            if self.MODEL == "RT76":
1120
                rs = RadioSettingValueBoolean(_settings.vox)
1121
                rset = RadioSetting("vox", "Vox Function", rs)
1122
                basic.append(rset)
1123

    
1124
                rs = RadioSettingValueList(VOXL_LIST,
1125
                                           VOXL_LIST[_settings.voxl])
1126
                rset = RadioSetting("voxl", "Vox Level", rs)
1127
                basic.append(rset)
1128

    
1129
                rs = RadioSettingValueList(VOXD_LIST,
1130
                                           VOXD_LIST[_settings.voxd])
1131
                rset = RadioSetting("voxd", "Vox Delay", rs)
1132
                basic.append(rset)
1133

    
1134
                rs = RadioSettingValueInteger(1, 30, _settings.chnumber + 1)
1135
                rset = RadioSetting("chnumber", "Channel Number", rs)
1136
                basic.append(rset)
1137

    
1138
        return top
1139

    
1140
    def set_settings(self, settings):
1141
        for element in settings:
1142
            if not isinstance(element, RadioSetting):
1143
                self.set_settings(element)
1144
                continue
1145
            else:
1146
                try:
1147
                    if "." in element.get_name():
1148
                        bits = element.get_name().split(".")
1149
                        obj = self._memobj
1150
                        for bit in bits[:-1]:
1151
                            obj = getattr(obj, bit)
1152
                        setting = bits[-1]
1153
                    else:
1154
                        obj = self._memobj.settings
1155
                        setting = element.get_name()
1156

    
1157
                    if element.has_apply_callback():
1158
                        LOG.debug("Using apply callback")
1159
                        element.run_apply_callback()
1160
                    elif setting == "channel":
1161
                        setattr(obj, setting, int(element.value) - 1)
1162
                    elif setting == "chnumber":
1163
                        setattr(obj, setting, int(element.value) - 1)
1164
                    elif setting == "chnumberd":
1165
                        setattr(obj, setting, not int(element.value))
1166
                    elif setting == "tail":
1167
                        setattr(obj, setting, not int(element.value))
1168
                    elif setting == "tot":
1169
                        setattr(obj, setting, int(element.value) + 1)
1170
                    elif element.value.get_mutable():
1171
                        LOG.debug("Setting %s = %s" % (setting, element.value))
1172
                        setattr(obj, setting, element.value)
1173
                except Exception, e:
1174
                    LOG.debug(element.get_name())
1175
                    raise
1176

    
1177
    @classmethod
1178
    def match_model(cls, filedata, filename):
1179
        if cls.MODEL == "RT21":
1180
            # The RT21 is pre-metadata, so do old-school detection
1181
            match_size = False
1182
            match_model = False
1183

    
1184
            # testing the file data size
1185
            if len(filedata) in [0x0400, ]:
1186
                match_size = True
1187

    
1188
            # testing the model fingerprint
1189
            match_model = model_match(cls, filedata)
1190

    
1191
            if match_size and match_model:
1192
                return True
1193
            else:
1194
                return False
1195
        else:
1196
            # Radios that have always been post-metadata, so never do
1197
            # old-school detection
1198
            return False
1199

    
1200

    
1201
@directory.register
1202
class RB17ARadio(RT21Radio):
1203
    """RETEVIS RB17A"""
1204
    VENDOR = "Retevis"
1205
    MODEL = "RB17A"
1206
    BAUD_RATE = 9600
1207
    BLOCK_SIZE = 0x40
1208
    BLOCK_SIZE_UP = 0x10
1209

    
1210
    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=0.50),
1211
                    chirp_common.PowerLevel("High", watts=5.00)]
1212

    
1213
    _magic = "PROA8US"
1214
    _fingerprint = "P3217s\xF8\xFF"
1215
    _upper = 30
1216
    _skipflags = True
1217
    _reserved = False
1218
    _gmrs = True
1219

    
1220
    _ranges = [
1221
               (0x0000, 0x0300),
1222
              ]
1223
    _memsize = 0x0300
1224

    
1225
    def process_mmap(self):
1226
        self._memobj = bitwise.parse(MEM_FORMAT_RB17A, self._mmap)
1227

    
1228

    
1229
@directory.register
1230
class RB26Radio(RT21Radio):
1231
    """RETEVIS RB26"""
1232
    VENDOR = "Retevis"
1233
    MODEL = "RB26"
1234
    BAUD_RATE = 9600
1235
    BLOCK_SIZE = 0x20
1236
    BLOCK_SIZE_UP = 0x10
1237

    
1238
    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=0.50),
1239
                    chirp_common.PowerLevel("High", watts=3.00)]
1240

    
1241
    _magic = "PHOGR" + "\x01" + "0"
1242
    _fingerprint = "P32073" + "\x02\xFF"
1243
    _upper = 30
1244
    _skipflags = True
1245
    _reserved = True
1246
    _gmrs = True
1247

    
1248
    _ranges = [
1249
               (0x0000, 0x0320),
1250
              ]
1251
    _memsize = 0x0320
1252

    
1253
    def process_mmap(self):
1254
        self._memobj = bitwise.parse(MEM_FORMAT_RB26, self._mmap)
1255

    
1256

    
1257
@directory.register
1258
class RT76Radio(RT21Radio):
1259
    """RETEVIS RT76"""
1260
    VENDOR = "Retevis"
1261
    MODEL = "RT76"
1262
    BAUD_RATE = 9600
1263
    BLOCK_SIZE = 0x20
1264
    BLOCK_SIZE_UP = 0x10
1265

    
1266
    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=0.50),
1267
                    chirp_common.PowerLevel("High", watts=5.00)]
1268

    
1269
    _magic = "PHOGR\x14\xD4"
1270
    _fingerprint = "P32073" + "\x02\xFF"
1271
    _upper = 30
1272
    _skipflags = False
1273
    _reserved = True
1274
    _gmrs = True
1275

    
1276
    _ranges = [
1277
               (0x0000, 0x01E0),
1278
              ]
1279
    _memsize = 0x01E0
1280

    
1281
    def process_mmap(self):
1282
        self._memobj = bitwise.parse(MEM_FORMAT_RT76, self._mmap)
1283

    
1284

    
1285
@directory.register
1286
class RT21VRadio(RT21Radio):
1287
    """RETEVIS RT21V"""
1288
    VENDOR = "Retevis"
1289
    MODEL = "RT21V"
1290
    ##BAUD_RATE = 9600
1291
    ##BLOCK_SIZE = 0x10
1292
    ##BLOCK_SIZE_UP = 0x10
1293

    
1294
    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=1.00),
1295
                    chirp_common.PowerLevel("High", watts=2.00)]
1296

    
1297
    ##_magic = "PRMZUNE"
1298
    _fingerprint = "P2207\x01\xF8\xFF"
1299
    _upper = 15
1300
    ##_skipflags = True
1301
    ##_reserved = False
1302
    ##_gmrs = False
1303
    _murs = True
1304

    
1305
    ##_ranges = [
1306
    ##           (0x0000, 0x0400),
1307
    ##          ]
1308
    ##_memsize = 0x0400
1309

    
1310
    def get_features(self):
1311
        rf = RT21Radio.get_features(self)
1312
        rf.valid_bands = [(136000000, 174000000)]
1313
        return rf
1314