Project

General

Profile

Bug #9982 » retevis_rb26_fix_b1.0.py

Jim Unroe, 08/14/2022 12:55 AM

 
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 unknown7:1,        //                        E
56
     scramble_type:3,   // Scramble Type
57
     unknown5:4;
58
  u8 unknown6:5,
59
     scramble_type2:3;  // Scramble Type 2        F
60
} memory[16];
61

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
243
MEM_FORMAT_RT29 = """
244
#seekto 0x0010;
245
struct {
246
  lbcd rxfreq[4];       // RX Frequency           0-3
247
  lbcd txfreq[4];       // TX Frequency           4-7
248
  ul16 rx_tone;         // PL/DPL Decode          8-9
249
  ul16 tx_tone;         // PL/DPL Encode          A-B
250
  u8 unknown1:2,        //                        C
251
     compander:1,       // Compander
252
     bcl:2,             // Busy Lock
253
     unknown2:3;
254
  u8 unknown3:1,        //                        D
255
     highpower:2,       // Power Level
256
     wide:1,            // Bandwidth
257
     unknown4:3,
258
     cdcss:1;           // Cdcss Mode
259
  u8 unknown5;          //                        E
260
  u8 unknown6:5,
261
     scramble_type:3;   // Scramble Type          F
262
} memory[16];
263

    
264
#seekto 0x011D;
265
struct {
266
  u8 unused1:4,
267
     pf1:4;             // Programmable Function Key 1
268
  u8 unused2:4,
269
     pf2:4;             // Programmable Function Key 2
270
} keys;
271

    
272
#seekto 0x012C;
273
struct {
274
  u8 use_scramble;      // Scramble Enable
275
  u8 unknown1[2];
276
  u8 voice;             // Voice Annunciation
277
  u8 tot;               // Time-out Timer
278
  u8 totalert;          // Time-out Timer Pre-alert
279
  u8 unknown2[2];
280
  u8 squelch;           // Squelch Level
281
  u8 save;              // Battery Saver
282
  u8 unknown3[3];
283
  u8 use_vox;           // VOX Enable
284
  u8 vox;               // VOX Gain
285
  u8 voxd;              // Vox Delay
286
} settings;
287

    
288
#seekto 0x017E;
289
u8 skipflags[2];       // SCAN_ADD
290

    
291
#seekto 0x01B8;
292
u8 fingerprint[5];     // Fingerprint
293
"""
294

    
295
CMD_ACK = "\x06"
296

    
297
ALARM_LIST = ["Local Alarm", "Remote Alarm"]
298
BCL_LIST = ["Off", "Carrier", "QT/DQT"]
299
CDCSS_LIST = ["Normal Code", "Special Code 2", "Special Code 1"]
300
CDCSS2_LIST = ["Normal Code", "Special Code"]  # RT29 UHF and RT29 VHF
301
GAIN_LIST = ["Standard", "Enhanced"]
302
PFKEY_LIST = ["None", "Monitor", "Lamp", "Warn", "VOX", "VOX Delay",
303
              "Key Lock", "Scan"]
304
SAVE_LIST = ["Standard", "Super"]
305
TIMEOUTTIMER_LIST = ["Off"] + ["%s seconds" % x for x in range(15, 615, 15)]
306
TOTALERT_LIST = ["Off"] + ["%s seconds" % x for x in range(1, 11)]
307
VOICE_LIST = ["Off", "Chinese", "English"]
308
VOICE_LIST2 = ["Off", "English"]
309
VOICE_LIST3 = VOICE_LIST2 + ["Chinese"]
310
VOX_LIST = ["OFF"] + ["%s" % x for x in range(1, 17)]
311
VOXD_LIST = ["0.5", "1.0", "1.5", "2.0", "2.5", "3.0"]
312
VOXL_LIST = ["OFF"] + ["%s" % x for x in range(1, 9)]
313
WARN_LIST = ["OFF", "Native Warn", "Remote Warn"]
314
PF1_CHOICES = ["None", "Monitor", "Scan", "Scramble", "Alarm"]
315
PF1_VALUES = [0x0F, 0x04, 0x06, 0x08, 0x0C]
316
PF1_17A_CHOICES = ["None", "Monitor", "Scan", "Scramble"]
317
PF1_17A_VALUES = [0x0F, 0x04, 0x06, 0x08]
318
PFKEY_CHOICES = ["None", "Monitor", "Scan", "Scramble", "VOX", "Alarm"]
319
PFKEY_VALUES = [0x0F, 0x04, 0x06, 0x08, 0x09, 0x0A]
320
TOPKEY_CHOICES = ["None", "Alarming"]
321
TOPKEY_VALUES = [0xFF, 0x0C]
322

    
323
SETTING_LISTS = {
324
    "alarm": ALARM_LIST,
325
    "bcl": BCL_LIST,
326
    "cdcss": CDCSS_LIST,
327
    "cdcss": CDCSS2_LIST,
328
    "gain": GAIN_LIST,
329
    "pfkey": PFKEY_LIST,
330
    "save": SAVE_LIST,
331
    "tot": TIMEOUTTIMER_LIST,
332
    "totalert": TOTALERT_LIST,
333
    "voice": VOICE_LIST,
334
    "voice": VOICE_LIST2,
335
    "voice": VOICE_LIST3,
336
    "vox": VOX_LIST,
337
    "voxd": VOXD_LIST,
338
    "voxl": VOXL_LIST,
339
    "warn": WARN_LIST,
340
    }
341

    
342
GMRS_FREQS1 = [462.5625, 462.5875, 462.6125, 462.6375, 462.6625,
343
               462.6875, 462.7125]
344
GMRS_FREQS2 = [467.5625, 467.5875, 467.6125, 467.6375, 467.6625,
345
               467.6875, 467.7125]
346
GMRS_FREQS3 = [462.5500, 462.5750, 462.6000, 462.6250, 462.6500,
347
               462.6750, 462.7000, 462.7250]
348
GMRS_FREQS = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
349

    
350

    
351
def _enter_programming_mode(radio):
352
    serial = radio.pipe
353

    
354
    exito = False
355
    for i in range(0, 5):
356
        serial.write(radio._magic)
357
        ack = serial.read(1)
358
        if ack == "\x00":
359
            ack = serial.read(1)
360

    
361
        try:
362
            if ack == CMD_ACK:
363
                exito = True
364
                break
365
        except:
366
            LOG.debug("Attempt #%s, failed, trying again" % i)
367
            pass
368

    
369
    # check if we had EXITO
370
    if exito is False:
371
        msg = "The radio did not accept program mode after five tries.\n"
372
        msg += "Check you interface cable and power cycle your radio."
373
        raise errors.RadioError(msg)
374

    
375
    try:
376
        serial.write("\x02")
377
        ident = serial.read(8)
378
    except:
379
        raise errors.RadioError("Error communicating with radio")
380

    
381
    if not ident == radio._fingerprint:
382
        LOG.debug(util.hexprint(ident))
383
        raise errors.RadioError("Radio returned unknown identification string")
384

    
385
    try:
386
        serial.write(CMD_ACK)
387
        ack = serial.read(1)
388
    except:
389
        raise errors.RadioError("Error communicating with radio")
390

    
391
    if ack != CMD_ACK:
392
        raise errors.RadioError("Radio refused to enter programming mode")
393

    
394

    
395
def _exit_programming_mode(radio):
396
    serial = radio.pipe
397
    try:
398
        serial.write("E")
399
    except:
400
        raise errors.RadioError("Radio refused to exit programming mode")
401

    
402

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

    
406
    cmd = struct.pack(">cHb", 'R', block_addr, block_size)
407
    expectedresponse = "W" + cmd[1:]
408
    LOG.debug("Reading block %04x..." % (block_addr))
409

    
410
    try:
411
        serial.write(cmd)
412
        response = serial.read(4 + block_size)
413
        if response[:4] != expectedresponse:
414
            raise Exception("Error reading block %04x." % (block_addr))
415

    
416
        block_data = response[4:]
417

    
418
        serial.write(CMD_ACK)
419
        ack = serial.read(1)
420
    except:
421
        raise errors.RadioError("Failed to read block at %04x" % block_addr)
422

    
423
    if ack != CMD_ACK:
424
        raise Exception("No ACK reading block %04x." % (block_addr))
425

    
426
    return block_data
427

    
428

    
429
def _rb26_read_block(radio, block_addr, block_size):
430
    serial = radio.pipe
431

    
432
    cmd = struct.pack(">cHb", 'R', block_addr, block_size)
433
    expectedresponse = "W" + cmd[1:]
434
    LOG.debug("Reading block %04x..." % (block_addr))
435

    
436
    try:
437
        serial.write(cmd)
438
        response = serial.read(4 + block_size)
439
        if response[:4] != expectedresponse:
440
            raise Exception("Error reading block %04x." % (block_addr))
441

    
442
        block_data = response[4:]
443

    
444
        if block_addr != 0:
445
            serial.write(CMD_ACK)
446
            ack = serial.read(1)
447
    except:
448
        raise errors.RadioError("Failed to read block at %04x" % block_addr)
449

    
450
    if block_addr != 0:
451
        if ack != CMD_ACK:
452
            raise Exception("No ACK reading block %04x." % (block_addr))
453

    
454
    return block_data
455

    
456

    
457
def _write_block(radio, block_addr, block_size):
458
    serial = radio.pipe
459

    
460
    cmd = struct.pack(">cHb", 'W', block_addr, block_size)
461
    data = radio.get_mmap()[block_addr:block_addr + block_size]
462

    
463
    LOG.debug("Writing Data:")
464
    LOG.debug(util.hexprint(cmd + data))
465

    
466
    try:
467
        serial.write(cmd + data)
468
        if serial.read(1) != CMD_ACK:
469
            raise Exception("No ACK")
470
    except:
471
        raise errors.RadioError("Failed to send block "
472
                                "to radio at %04x" % block_addr)
473

    
474

    
475
def do_download(radio):
476
    LOG.debug("download")
477
    _enter_programming_mode(radio)
478

    
479
    data = ""
480

    
481
    status = chirp_common.Status()
482
    status.msg = "Cloning from radio"
483

    
484
    status.cur = 0
485
    status.max = radio._memsize
486

    
487
    for addr in range(0, radio._memsize, radio.BLOCK_SIZE):
488
        status.cur = addr + radio.BLOCK_SIZE
489
        radio.status_fn(status)
490

    
491
        if radio.MODEL == "RB26" or radio.MODEL == "RT76":
492
            block = _rb26_read_block(radio, addr, radio.BLOCK_SIZE)
493
        else:
494
            block = _read_block(radio, addr, radio.BLOCK_SIZE)
495
        data += block
496

    
497
        LOG.debug("Address: %04x" % addr)
498
        LOG.debug(util.hexprint(block))
499

    
500
    _exit_programming_mode(radio)
501

    
502
    return memmap.MemoryMap(data)
503

    
504

    
505
def do_upload(radio):
506
    status = chirp_common.Status()
507
    status.msg = "Uploading to radio"
508

    
509
    _enter_programming_mode(radio)
510

    
511
    status.cur = 0
512
    status.max = radio._memsize
513

    
514
    for start_addr, end_addr in radio._ranges:
515
        for addr in range(start_addr, end_addr, radio.BLOCK_SIZE_UP):
516
            status.cur = addr + radio.BLOCK_SIZE_UP
517
            radio.status_fn(status)
518
            _write_block(radio, addr, radio.BLOCK_SIZE_UP)
519

    
520
    _exit_programming_mode(radio)
521

    
522

    
523
def model_match(cls, data):
524
    """Match the opened/downloaded image to the correct version"""
525
    rid = data[0x01B8:0x01BE]
526

    
527
    return rid.startswith("P3207")
528

    
529

    
530
@directory.register
531
class RT21Radio(chirp_common.CloneModeRadio):
532
    """RETEVIS RT21"""
533
    VENDOR = "Retevis"
534
    MODEL = "RT21"
535
    BAUD_RATE = 9600
536
    BLOCK_SIZE = 0x10
537
    BLOCK_SIZE_UP = 0x10
538

    
539
    POWER_LEVELS = [chirp_common.PowerLevel("High", watts=2.50),
540
                    chirp_common.PowerLevel("Low", watts=1.00)]
541

    
542
    VALID_BANDS = [(400000000, 480000000)]
543

    
544
    _magic = "PRMZUNE"
545
    _fingerprint = "P3207s\xF8\xFF"
546
    _upper = 16
547
    _skipflags = True
548
    _reserved = False
549
    _gmrs = False
550

    
551
    _ranges = [
552
               (0x0000, 0x0400),
553
              ]
554
    _memsize = 0x0400
555

    
556
    def get_features(self):
557
        rf = chirp_common.RadioFeatures()
558
        rf.has_settings = True
559
        rf.has_bank = False
560
        rf.has_ctone = True
561
        rf.has_cross = True
562
        rf.has_rx_dtcs = True
563
        rf.has_tuning_step = False
564
        rf.can_odd_split = True
565
        rf.has_name = False
566
        if self.MODEL == "RT76":
567
            rf.valid_skips = []
568
        else:
569
            rf.valid_skips = ["", "S"]
570
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
571
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
572
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
573
        rf.valid_power_levels = self.POWER_LEVELS
574
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
575
        rf.valid_modes = ["FM", "NFM"]  # 25 KHz, 12.5 kHz.
576
        rf.memory_bounds = (1, self._upper)
577
        rf.valid_tuning_steps = [2.5, 5., 6.25, 10., 12.5, 25.]
578
        rf.valid_bands = self.VALID_BANDS
579

    
580
        return rf
581

    
582
    def process_mmap(self):
583
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
584

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

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

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

    
615
    def _get_tone(self, _mem, mem):
616
        def _get_dcs(val):
617
            code = int("%03o" % (val & 0x07FF))
618
            pol = (val & 0x8000) and "R" or "N"
619
            return code, pol
620

    
621
        if _mem.tx_tone != 0xFFFF and _mem.tx_tone > 0x2000:
622
            tcode, tpol = _get_dcs(_mem.tx_tone)
623
            mem.dtcs = tcode
624
            txmode = "DTCS"
625
        elif _mem.tx_tone != 0xFFFF:
626
            mem.rtone = _mem.tx_tone / 10.0
627
            txmode = "Tone"
628
        else:
629
            txmode = ""
630

    
631
        if _mem.rx_tone != 0xFFFF and _mem.rx_tone > 0x2000:
632
            rcode, rpol = _get_dcs(_mem.rx_tone)
633
            mem.rx_dtcs = rcode
634
            rxmode = "DTCS"
635
        elif _mem.rx_tone != 0xFFFF:
636
            mem.ctone = _mem.rx_tone / 10.0
637
            rxmode = "Tone"
638
        else:
639
            rxmode = ""
640

    
641
        if txmode == "Tone" and not rxmode:
642
            mem.tmode = "Tone"
643
        elif txmode == rxmode and txmode == "Tone" and mem.rtone == mem.ctone:
644
            mem.tmode = "TSQL"
645
        elif txmode == rxmode and txmode == "DTCS" and mem.dtcs == mem.rx_dtcs:
646
            mem.tmode = "DTCS"
647
        elif rxmode or txmode:
648
            mem.tmode = "Cross"
649
            mem.cross_mode = "%s->%s" % (txmode, rxmode)
650

    
651
        if mem.tmode == "DTCS":
652
            mem.dtcs_polarity = "%s%s" % (tpol, rpol)
653

    
654
        LOG.debug("Got TX %s (%i) RX %s (%i)" %
655
                  (txmode, _mem.tx_tone, rxmode, _mem.rx_tone))
656

    
657
    def get_memory(self, number):
658
        if self._skipflags:
659
            bitpos = (1 << ((number - 1) % 8))
660
            bytepos = ((number - 1) / 8)
661
            LOG.debug("bitpos %s" % bitpos)
662
            LOG.debug("bytepos %s" % bytepos)
663
            _skp = self._memobj.skipflags[bytepos]
664

    
665
        mem = chirp_common.Memory()
666

    
667
        mem.number = number
668

    
669
        if self.MODEL == "RB17A":
670
            if mem.number < 17:
671
                _mem = self._memobj.lomems[number - 1]
672
            else:
673
                _mem = self._memobj.himems[number - 17]
674
        else:
675
            _mem = self._memobj.memory[number - 1]
676

    
677
        if self._reserved:
678
            _rsvd = _mem.reserved.get_raw()
679

    
680
        mem.freq = int(_mem.rxfreq) * 10
681

    
682
        # We'll consider any blank (i.e. 0MHz frequency) to be empty
683
        if mem.freq == 0:
684
            mem.empty = True
685
            return mem
686

    
687
        if _mem.rxfreq.get_raw() == "\xFF\xFF\xFF\xFF":
688
            mem.freq = 0
689
            mem.empty = True
690
            return mem
691

    
692
        if int(_mem.rxfreq) == int(_mem.txfreq):
693
            mem.duplex = ""
694
            mem.offset = 0
695
        else:
696
            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
697
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
698

    
699
        mem.mode = _mem.wide and "FM" or "NFM"
700

    
701
        self._get_tone(_mem, mem)
702

    
703
        mem.power = self.POWER_LEVELS[1 - _mem.highpower]
704

    
705
        if self.MODEL != "RT76":
706
            mem.skip = "" if (_skp & bitpos) else "S"
707
            LOG.debug("mem.skip %s" % mem.skip)
708

    
709
        mem.extra = RadioSettingGroup("Extra", "extra")
710

    
711
        if self.MODEL == "RT21" or self.MODEL == "RB17A" or \
712
                self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
713
            rs = RadioSettingValueList(BCL_LIST, BCL_LIST[_mem.bcl])
714
            rset = RadioSetting("bcl", "Busy Channel Lockout", rs)
715
            mem.extra.append(rset)
716

    
717
            rs = RadioSettingValueInteger(1, 8, _mem.scramble_type + 1)
718
            rset = RadioSetting("scramble_type", "Scramble Type", rs)
719
            mem.extra.append(rset)
720

    
721
            if self.MODEL == "RB17A":
722
                rs = RadioSettingValueList(CDCSS_LIST, CDCSS_LIST[_mem.cdcss])
723
                rset = RadioSetting("cdcss", "Cdcss Mode", rs)
724
                mem.extra.append(rset)
725

    
726
            if self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
727
                rs = RadioSettingValueList(CDCSS2_LIST,
728
                                           CDCSS2_LIST[_mem.cdcss])
729
                rset = RadioSetting("cdcss", "Cdcss Mode", rs)
730
                mem.extra.append(rset)
731

    
732
            if self.MODEL == "RB17A" or self.MODEL == "RT29_UHF" or \
733
                    self.MODEL == "RT29_VHF":
734
                rs = RadioSettingValueBoolean(_mem.compander)
735
                rset = RadioSetting("compander", "Compander", rs)
736
                mem.extra.append(rset)
737

    
738
        if self.MODEL == "RB26" or self.MODEL == "RT76":
739
            if self.MODEL == "RB26":
740
                rs = RadioSettingValueBoolean(_mem.bcl)
741
                rset = RadioSetting("bcl", "Busy Channel Lockout", rs)
742
                mem.extra.append(rset)
743

    
744
            rs = RadioSettingValueBoolean(_mem.compander)
745
            rset = RadioSetting("compander", "Compander", rs)
746
            mem.extra.append(rset)
747

    
748
        return mem
749

    
750
    def _set_tone(self, mem, _mem):
751
        def _set_dcs(code, pol):
752
            val = int("%i" % code, 8) + 0x2800
753
            if pol == "R":
754
                val += 0x8000
755
            return val
756

    
757
        rx_mode = tx_mode = None
758
        rx_tone = tx_tone = 0xFFFF
759

    
760
        if mem.tmode == "Tone":
761
            tx_mode = "Tone"
762
            rx_mode = None
763
            tx_tone = int(mem.rtone * 10)
764
        elif mem.tmode == "TSQL":
765
            rx_mode = tx_mode = "Tone"
766
            rx_tone = tx_tone = int(mem.ctone * 10)
767
        elif mem.tmode == "DTCS":
768
            tx_mode = rx_mode = "DTCS"
769
            tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
770
            rx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[1])
771
        elif mem.tmode == "Cross":
772
            tx_mode, rx_mode = mem.cross_mode.split("->")
773
            if tx_mode == "DTCS":
774
                tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
775
            elif tx_mode == "Tone":
776
                tx_tone = int(mem.rtone * 10)
777
            if rx_mode == "DTCS":
778
                rx_tone = _set_dcs(mem.rx_dtcs, mem.dtcs_polarity[1])
779
            elif rx_mode == "Tone":
780
                rx_tone = int(mem.ctone * 10)
781

    
782
        _mem.rx_tone = rx_tone
783
        _mem.tx_tone = tx_tone
784

    
785
        LOG.debug("Set TX %s (%i) RX %s (%i)" %
786
                  (tx_mode, _mem.tx_tone, rx_mode, _mem.rx_tone))
787

    
788
    def set_memory(self, mem):
789
        if self._skipflags:
790
            bitpos = (1 << ((mem.number - 1) % 8))
791
            bytepos = ((mem.number - 1) / 8)
792
            LOG.debug("bitpos %s" % bitpos)
793
            LOG.debug("bytepos %s" % bytepos)
794
            _skp = self._memobj.skipflags[bytepos]
795

    
796
        if self.MODEL == "RB17A":
797
            if mem.number < 17:
798
                _mem = self._memobj.lomems[mem.number - 1]
799
            else:
800
                _mem = self._memobj.himems[mem.number - 17]
801
        else:
802
            _mem = self._memobj.memory[mem.number - 1]
803

    
804
        if self._reserved:
805
            _rsvd = _mem.reserved.get_raw()
806

    
807
        if mem.empty:
808
            if self.MODEL == "RB26" or self.MODEL == "RT76":
809
                _mem.set_raw("\xFF" * 13 + _rsvd)
810
            else:
811
                _mem.set_raw("\xFF" * (_mem.size() / 8))
812

    
813
            return
814

    
815
        if self.MODEL == "RB17A":
816
            _mem.set_raw("\x00" * 14 + "\xFF\xFF")
817
        elif self.MODEL == "RB26" or self.MODEL == "RT76":
818
            _mem.set_raw("\x00" * 13 + _rsvd)
819
        else:
820
            _mem.set_raw("\x00" * 13 + "\x30\x8F\xF8")
821

    
822
        if self._gmrs:
823
            if mem.number >= 1 and mem.number <= 30:
824
                GMRS_FREQ = int(GMRS_FREQS[mem.number - 1] * 1000000)
825
                mem.freq = GMRS_FREQ
826
                if mem.number <= 22:
827
                    mem.duplex = ''
828
                    mem.offset = 0
829
                    if mem.number >= 8 and mem.number <= 14:
830
                        mem.mode = "NFM"
831
                        mem.power = self.POWER_LEVELS[1]
832
                if mem.number > 22:
833
                    mem.duplex = '+'
834
                    mem.offset = 5000000
835

    
836
        _mem.rxfreq = mem.freq / 10
837

    
838
        if mem.duplex == "off":
839
            for i in range(0, 4):
840
                _mem.txfreq[i].set_raw("\xFF")
841
        elif mem.duplex == "split":
842
            _mem.txfreq = mem.offset / 10
843
        elif mem.duplex == "+":
844
            _mem.txfreq = (mem.freq + mem.offset) / 10
845
        elif mem.duplex == "-":
846
            _mem.txfreq = (mem.freq - mem.offset) / 10
847
        else:
848
            _mem.txfreq = mem.freq / 10
849

    
850
        _mem.wide = mem.mode == "FM"
851

    
852
        self._set_tone(mem, _mem)
853

    
854
        _mem.highpower = mem.power == self.POWER_LEVELS[0]
855

    
856
        if self.MODEL != "RT76":
857
            if mem.skip != "S":
858
                _skp |= bitpos
859
            else:
860
                _skp &= ~bitpos
861
            LOG.debug("_skp %s" % _skp)
862

    
863
        for setting in mem.extra:
864
            if setting.get_name() == "scramble_type":
865
                setattr(_mem, setting.get_name(), int(setting.value) - 1)
866
                if self.MODEL == "RT21":
867
                    setattr(_mem, "scramble_type2", int(setting.value) - 1)
868
            else:
869
                setattr(_mem, setting.get_name(), setting.value)
870

    
871
    def get_settings(self):
872
        _settings = self._memobj.settings
873
        basic = RadioSettingGroup("basic", "Basic Settings")
874
        top = RadioSettings(basic)
875

    
876
        if self.MODEL == "RT21" or self.MODEL == "RB17A" or \
877
                self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
878
            _keys = self._memobj.keys
879

    
880
            rs = RadioSettingValueList(TIMEOUTTIMER_LIST,
881
                                       TIMEOUTTIMER_LIST[_settings.tot])
882
            rset = RadioSetting("tot", "Time-out timer", rs)
883
            basic.append(rset)
884

    
885
            rs = RadioSettingValueList(TOTALERT_LIST,
886
                                       TOTALERT_LIST[_settings.totalert])
887
            rset = RadioSetting("totalert", "TOT Pre-alert", rs)
888
            basic.append(rset)
889

    
890
            rs = RadioSettingValueInteger(0, 9, _settings.squelch)
891
            rset = RadioSetting("squelch", "Squelch Level", rs)
892
            basic.append(rset)
893

    
894
            rs = RadioSettingValueList(VOICE_LIST, VOICE_LIST[_settings.voice])
895
            rset = RadioSetting("voice", "Voice Annumciation", rs)
896
            basic.append(rset)
897

    
898
            if self.MODEL == "RB17A":
899
                rs = RadioSettingValueList(ALARM_LIST,
900
                                           ALARM_LIST[_settings.alarm])
901
                rset = RadioSetting("alarm", "Alarm Type", rs)
902
                basic.append(rset)
903

    
904
            rs = RadioSettingValueBoolean(_settings.save)
905
            rset = RadioSetting("save", "Battery Saver", rs)
906
            basic.append(rset)
907

    
908
            rs = RadioSettingValueBoolean(_settings.use_scramble)
909
            rset = RadioSetting("use_scramble", "Scramble", rs)
910
            basic.append(rset)
911

    
912
            rs = RadioSettingValueBoolean(_settings.use_vox)
913
            rset = RadioSetting("use_vox", "VOX", rs)
914
            basic.append(rset)
915

    
916
            rs = RadioSettingValueList(VOX_LIST, VOX_LIST[_settings.vox])
917
            rset = RadioSetting("vox", "VOX Gain", rs)
918
            basic.append(rset)
919

    
920
            if self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
921
                rs = RadioSettingValueList(VOXD_LIST,
922
                                           VOXD_LIST[_settings.voxd])
923
                rset = RadioSetting("voxd", "Vox Delay", rs)
924
                basic.append(rset)
925

    
926
            def apply_pf1_listvalue(setting, obj):
927
                LOG.debug("Setting value: " + str(
928
                          setting.value) + " from list")
929
                val = str(setting.value)
930
                index = PF1_CHOICES.index(val)
931
                val = PF1_VALUES[index]
932
                obj.set_value(val)
933

    
934
            if self.MODEL == "RT21":
935
                if _keys.pf1 in PF1_VALUES:
936
                    idx = PF1_VALUES.index(_keys.pf1)
937
                else:
938
                    idx = LIST_DTMF_SPECIAL_VALUES.index(0x04)
939
                rs = RadioSettingValueList(PF1_CHOICES, PF1_CHOICES[idx])
940
                rset = RadioSetting("keys.pf1", "PF1 Key Function", rs)
941
                rset.set_apply_callback(apply_pf1_listvalue, _keys.pf1)
942
                basic.append(rset)
943

    
944
            def apply_pf1_17a_listvalue(setting, obj):
945
                LOG.debug("Setting value: " + str(
946
                          setting.value) + " from list")
947
                val = str(setting.value)
948
                index = PF1_17A_CHOICES.index(val)
949
                val = PF1_17A_VALUES[index]
950
                obj.set_value(val)
951

    
952
            if self.MODEL == "RB17A":
953
                if _keys.pf1 in PF1_17A_VALUES:
954
                    idx = PF1_17A_VALUES.index(_keys.pf1)
955
                else:
956
                    idx = LIST_DTMF_SPECIAL_VALUES.index(0x04)
957
                rs = RadioSettingValueList(PF1_17A_CHOICES,
958
                                           PF1_17A_CHOICES[idx])
959
                rset = RadioSetting("keys.pf1", "PF1 Key Function", rs)
960
                rset.set_apply_callback(apply_pf1_17a_listvalue, _keys.pf1)
961
                basic.append(rset)
962

    
963
            def apply_topkey_listvalue(setting, obj):
964
                LOG.debug("Setting value: " + str(setting.value) +
965
                          " from list")
966
                val = str(setting.value)
967
                index = TOPKEY_CHOICES.index(val)
968
                val = TOPKEY_VALUES[index]
969
                obj.set_value(val)
970

    
971
            if self.MODEL == "RB17A":
972
                if _keys.topkey in TOPKEY_VALUES:
973
                    idx = TOPKEY_VALUES.index(_keys.topkey)
974
                else:
975
                    idx = TOPKEY_VALUES.index(0x0C)
976
                rs = RadioSettingValueList(TOPKEY_CHOICES, TOPKEY_CHOICES[idx])
977
                rset = RadioSetting("keys.topkey", "Top Key Function", rs)
978
                rset.set_apply_callback(apply_topkey_listvalue, _keys.topkey)
979
                basic.append(rset)
980

    
981
            def apply_pfkey_listvalue(setting, obj):
982
                LOG.debug("Setting value: " + str(setting.value) +
983
                          " from list")
984
                val = str(setting.value)
985
                index = PFKEY_CHOICES.index(val)
986
                val = PFKEY_VALUES[index]
987
                obj.set_value(val)
988

    
989
            if self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
990
                if _keys.pf1 in PFKEY_VALUES:
991
                    idx = PFKEY_VALUES.index(_keys.pf1)
992
                else:
993
                    idx = PFKEY_VALUES.index(0x04)
994
                rs = RadioSettingValueList(PFKEY_CHOICES, PFKEY_CHOICES[idx])
995
                rset = RadioSetting("keys.pf1", "PF1 Key Function", rs)
996
                rset.set_apply_callback(apply_pfkey_listvalue, _keys.pf1)
997
                basic.append(rset)
998

    
999
                if _keys.pf2 in PFKEY_VALUES:
1000
                    idx = PFKEY_VALUES.index(_keys.pf2)
1001
                else:
1002
                    idx = PFKEY_VALUES.index(0x0A)
1003
                rs = RadioSettingValueList(PFKEY_CHOICES, PFKEY_CHOICES[idx])
1004
                rset = RadioSetting("keys.pf2", "PF2 Key Function", rs)
1005
                rset.set_apply_callback(apply_pfkey_listvalue, _keys.pf2)
1006
                basic.append(rset)
1007

    
1008
        if self.MODEL == "RB26" or self.MODEL == "RT76":
1009
            if self.MODEL == "RB26":
1010
                _settings2 = self._memobj.settings2
1011
                _settings3 = self._memobj.settings3
1012

    
1013
            rs = RadioSettingValueInteger(0, 9, _settings.squelch)
1014
            rset = RadioSetting("squelch", "Squelch Level", rs)
1015
            basic.append(rset)
1016

    
1017
            rs = RadioSettingValueList(TIMEOUTTIMER_LIST,
1018
                                       TIMEOUTTIMER_LIST[_settings.tot])
1019
            rset = RadioSetting("tot", "Time-out timer", rs)
1020
            basic.append(rset)
1021

    
1022
            if self.MODEL == "RT76":
1023
                rs = RadioSettingValueList(VOICE_LIST3,
1024
                                           VOICE_LIST3[_settings.voice])
1025
                rset = RadioSetting("voice", "Voice Annumciation", rs)
1026
                basic.append(rset)
1027

    
1028
            if self.MODEL == "RB26":
1029
                rs = RadioSettingValueList(VOICE_LIST2,
1030
                                           VOICE_LIST2[_settings.voice])
1031
                rset = RadioSetting("voice", "Voice Annumciation", rs)
1032
                basic.append(rset)
1033

    
1034
                rs = RadioSettingValueBoolean(not _settings.chnumberd)
1035
                rset = RadioSetting("chnumberd", "Channel Number Enable", rs)
1036
                basic.append(rset)
1037

    
1038
            rs = RadioSettingValueBoolean(_settings.save)
1039
            rset = RadioSetting("save", "Battery Save", rs)
1040
            basic.append(rset)
1041

    
1042
            rs = RadioSettingValueBoolean(_settings.beep)
1043
            rset = RadioSetting("beep", "Beep", rs)
1044
            basic.append(rset)
1045

    
1046
            if self.MODEL == "RB26":
1047
                rs = RadioSettingValueBoolean(not _settings.tail)
1048
                rset = RadioSetting("tail", "QT/DQT Tail", rs)
1049
                basic.append(rset)
1050

    
1051
            rs = RadioSettingValueList(SAVE_LIST, SAVE_LIST[_settings.savem])
1052
            rset = RadioSetting("savem", "Battery Save Mode", rs)
1053
            basic.append(rset)
1054

    
1055
            rs = RadioSettingValueList(GAIN_LIST, GAIN_LIST[_settings.gain])
1056
            rset = RadioSetting("gain", "MIC Gain", rs)
1057
            basic.append(rset)
1058

    
1059
            rs = RadioSettingValueList(WARN_LIST, WARN_LIST[_settings.warn])
1060
            rset = RadioSetting("warn", "Warn Mode", rs)
1061
            basic.append(rset)
1062

    
1063
            if self.MODEL == "RB26":
1064
                rs = RadioSettingValueBoolean(_settings3.vox)
1065
                rset = RadioSetting("settings3.vox", "Vox Function", rs)
1066
                basic.append(rset)
1067

    
1068
                rs = RadioSettingValueList(VOXL_LIST,
1069
                                           VOXL_LIST[_settings3.voxl])
1070
                rset = RadioSetting("settings3.voxl", "Vox Level", rs)
1071
                basic.append(rset)
1072

    
1073
                rs = RadioSettingValueList(VOXD_LIST,
1074
                                           VOXD_LIST[_settings3.voxd])
1075
                rset = RadioSetting("settings3.voxd", "Vox Delay", rs)
1076
                basic.append(rset)
1077

    
1078
                rs = RadioSettingValueList(PFKEY_LIST,
1079
                                           PFKEY_LIST[_settings.pf1])
1080
                rset = RadioSetting("pf1", "PF1 Key Set", rs)
1081
                basic.append(rset)
1082

    
1083
                rs = RadioSettingValueList(PFKEY_LIST,
1084
                                           PFKEY_LIST[_settings.pf2])
1085
                rset = RadioSetting("pf2", "PF2 Key Set", rs)
1086
                basic.append(rset)
1087

    
1088
                rs = RadioSettingValueInteger(1, 30, _settings2.chnumber + 1)
1089
                rset = RadioSetting("settings2.chnumber", "Channel Number", rs)
1090
                basic.append(rset)
1091

    
1092
            if self.MODEL == "RT76":
1093
                rs = RadioSettingValueBoolean(_settings.vox)
1094
                rset = RadioSetting("vox", "Vox Function", rs)
1095
                basic.append(rset)
1096

    
1097
                rs = RadioSettingValueList(VOXL_LIST,
1098
                                           VOXL_LIST[_settings.voxl])
1099
                rset = RadioSetting("voxl", "Vox Level", rs)
1100
                basic.append(rset)
1101

    
1102
                rs = RadioSettingValueList(VOXD_LIST,
1103
                                           VOXD_LIST[_settings.voxd])
1104
                rset = RadioSetting("voxd", "Vox Delay", rs)
1105
                basic.append(rset)
1106

    
1107
                rs = RadioSettingValueInteger(1, 30, _settings.chnumber + 1)
1108
                rset = RadioSetting("chnumber", "Channel Number", rs)
1109
                basic.append(rset)
1110

    
1111
        return top
1112

    
1113
    def set_settings(self, settings):
1114
        for element in settings:
1115
            if not isinstance(element, RadioSetting):
1116
                self.set_settings(element)
1117
                continue
1118
            else:
1119
                try:
1120
                    if "." in element.get_name():
1121
                        bits = element.get_name().split(".")
1122
                        obj = self._memobj
1123
                        for bit in bits[:-1]:
1124
                            obj = getattr(obj, bit)
1125
                        setting = bits[-1]
1126
                    else:
1127
                        obj = self._memobj.settings
1128
                        setting = element.get_name()
1129

    
1130
                    if element.has_apply_callback():
1131
                        LOG.debug("Using apply callback")
1132
                        element.run_apply_callback()
1133
                    elif setting == "channel":
1134
                        setattr(obj, setting, int(element.value) - 1)
1135
                    elif setting == "chnumber":
1136
                        setattr(obj, setting, int(element.value) - 1)
1137
                    elif setting == "chnumberd":
1138
                        setattr(obj, setting, not int(element.value))
1139
                    elif setting == "tail":
1140
                        setattr(obj, setting, not int(element.value))
1141
                    elif element.value.get_mutable():
1142
                        LOG.debug("Setting %s = %s" % (setting, element.value))
1143
                        setattr(obj, setting, element.value)
1144
                except Exception, e:
1145
                    LOG.debug(element.get_name())
1146
                    raise
1147

    
1148
    @classmethod
1149
    def match_model(cls, filedata, filename):
1150
        if cls.MODEL == "RT21":
1151
            # The RT21 is pre-metadata, so do old-school detection
1152
            match_size = False
1153
            match_model = False
1154

    
1155
            # testing the file data size
1156
            if len(filedata) in [0x0400, ]:
1157
                match_size = True
1158

    
1159
            # testing the model fingerprint
1160
            match_model = model_match(cls, filedata)
1161

    
1162
            if match_size and match_model:
1163
                return True
1164
            else:
1165
                return False
1166
        else:
1167
            # Radios that have always been post-metadata, so never do
1168
            # old-school detection
1169
            return False
1170

    
1171

    
1172
@directory.register
1173
class RB17ARadio(RT21Radio):
1174
    """RETEVIS RB17A"""
1175
    VENDOR = "Retevis"
1176
    MODEL = "RB17A"
1177
    BAUD_RATE = 9600
1178
    BLOCK_SIZE = 0x40
1179
    BLOCK_SIZE_UP = 0x10
1180

    
1181
    POWER_LEVELS = [chirp_common.PowerLevel("High", watts=5.00),
1182
                    chirp_common.PowerLevel("Low", watts=0.50)]
1183

    
1184
    _magic = "PROA8US"
1185
    _fingerprint = "P3217s\xF8\xFF"
1186
    _upper = 30
1187
    _skipflags = True
1188
    _reserved = False
1189
    _gmrs = True
1190

    
1191
    _ranges = [
1192
               (0x0000, 0x0300),
1193
              ]
1194
    _memsize = 0x0300
1195

    
1196
    def process_mmap(self):
1197
        self._memobj = bitwise.parse(MEM_FORMAT_RB17A, self._mmap)
1198

    
1199

    
1200
@directory.register
1201
class RB26Radio(RT21Radio):
1202
    """RETEVIS RB26"""
1203
    VENDOR = "Retevis"
1204
    MODEL = "RB26"
1205
    BAUD_RATE = 9600
1206
    BLOCK_SIZE = 0x20
1207
    BLOCK_SIZE_UP = 0x10
1208

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

    
1212
    _magic = "PHOGR" + "\x01" + "0"
1213
    _fingerprint = "P32073" + "\x02\xFF"
1214
    _upper = 30
1215
    _skipflags = True
1216
    _reserved = True
1217
    _gmrs = True
1218

    
1219
    _ranges = [
1220
               (0x0000, 0x0320),
1221
              ]
1222
    _memsize = 0x0320
1223

    
1224
    def process_mmap(self):
1225
        self._memobj = bitwise.parse(MEM_FORMAT_RB26, self._mmap)
1226

    
1227

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

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

    
1240
    _magic = "PHOGR\x14\xD4"
1241
    _fingerprint = "P32073" + "\x02\xFF"
1242
    _upper = 30
1243
    _skipflags = False
1244
    _reserved = True
1245
    _gmrs = True
1246

    
1247
    _ranges = [
1248
               (0x0000, 0x01E0),
1249
              ]
1250
    _memsize = 0x01E0
1251

    
1252
    def process_mmap(self):
1253
        self._memobj = bitwise.parse(MEM_FORMAT_RT76, self._mmap)
1254

    
1255

    
1256
@directory.register
1257
class RT29UHFRadio(RT21Radio):
1258
    """RETEVIS RT29UHF"""
1259
    VENDOR = "Retevis"
1260
    MODEL = "RT29_UHF"
1261
    BLOCK_SIZE = 0x40
1262
    BLOCK_SIZE_UP = 0x10
1263

    
1264
    POWER_LEVELS = [chirp_common.PowerLevel("High", watts=10.00),
1265
                    chirp_common.PowerLevel("Mid", watts=5.00),
1266
                    chirp_common.PowerLevel("Low", watts=1.00)]
1267

    
1268
    _magic = "PROHRAM"
1269
    _fingerprint = "P3207" + "\x13\xF8\xFF"  # UHF model
1270
    _upper = 16
1271
    _skipflags = True
1272
    _reserved = False
1273

    
1274
    _ranges = [
1275
               (0x0000, 0x0300),
1276
              ]
1277
    _memsize = 0x0400
1278

    
1279
    def process_mmap(self):
1280
        self._memobj = bitwise.parse(MEM_FORMAT_RT29, self._mmap)
1281

    
1282

    
1283
@directory.register
1284
class RT29VHFRadio(RT29UHFRadio):
1285
    """RETEVIS RT29VHF"""
1286
    VENDOR = "Retevis"
1287
    MODEL = "RT29_VHF"
1288

    
1289
    POWER_LEVELS = [chirp_common.PowerLevel("High", watts=10.00),
1290
                    chirp_common.PowerLevel("Mid", watts=5.00),
1291
                    chirp_common.PowerLevel("Low", watts=1.00)]
1292

    
1293
    VALID_BANDS = [(136000000, 174000000)]
1294

    
1295
    _magic = "PROHRAM"
1296
    _fingerprint = "P2207" + "\x01\xF8\xFF"  # VHF model
(5-5/6)