Project

General

Profile

Bug #9982 » retevis_rb26_temp_fix.py

Jim Unroe, 08/11/2022 02:32 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("Low", watts=1.00),
540
                    chirp_common.PowerLevel("High", watts=2.50)]
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
        rf.valid_skips = ["", "S"]
567
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
568
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
569
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
570
        rf.valid_power_levels = self.POWER_LEVELS
571
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
572
        rf.valid_modes = ["NFM", "FM"]  # 12.5 KHz, 25 kHz.
573
        rf.memory_bounds = (1, self._upper)
574
        rf.valid_tuning_steps = [2.5, 5., 6.25, 10., 12.5, 25.]
575
        rf.valid_bands = self.VALID_BANDS
576

    
577
        return rf
578

    
579
    def process_mmap(self):
580
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
581

    
582
    def validate_memory(self, mem):
583
        msgs = ""
584
        msgs = chirp_common.CloneModeRadio.validate_memory(self, mem)
585

    
586
        _msg_freq = 'Memory location cannot change frequency'
587
        _msg_simplex = 'Memory location only supports Duplex:(None)'
588
        _msg_duplex = 'Memory location only supports Duplex: +'
589
        _msg_offset = 'Memory location only supports Offset: 5.000000'
590
        _msg_nfm = 'Memory location only supports Mode: NFM'
591
        _msg_txp = 'Memory location only supports Power: Low'
592

    
593
        # GMRS models
594
        if self._gmrs:
595
            # range of memories with values set by FCC rules
596
            if mem.freq != int(GMRS_FREQS[mem.number - 1] * 1000000):
597
                # warn user can't change frequency
598
                msgs.append(chirp_common.ValidationError(_msg_freq))
599

    
600
            # channels 1 - 22 are simplex only
601
            if mem.number <= 22:
602
                if str(mem.duplex) != "":
603
                    # warn user can't change duplex
604
                    msgs.append(chirp_common.ValidationError(_msg_simplex))
605

    
606
            # channels 23 - 30 are +5 MHz duplex only
607
            if mem.number >= 23:
608
                if str(mem.duplex) != "+":
609
                    # warn user can't change duplex
610
                    msgs.append(chirp_common.ValidationError(_msg_duplex))
611

    
612
                if str(mem.offset) != "5000000":
613
                    # warn user can't change offset
614
                    msgs.append(chirp_common.ValidationError(_msg_offset))
615

    
616
            # channels 8 - 14 are low power NFM only
617
            if mem.number >= 8 and mem.number <= 14:
618
                if mem.mode != "NFM":
619
                    # warn user can't change mode
620
                    msgs.append(chirp_common.ValidationError(_msg_nfm))
621

    
622
                #if mem.power != "Low":
623
                #    # warn user can't change power
624
                #    msgs.append(chirp_common.ValidationError(_msg_txp))
625

    
626
        return msgs
627

    
628
    def sync_in(self):
629
        """Download from radio"""
630
        try:
631
            data = do_download(self)
632
        except errors.RadioError:
633
            # Pass through any real errors we raise
634
            raise
635
        except:
636
            # If anything unexpected happens, make sure we raise
637
            # a RadioError and log the problem
638
            LOG.exception('Unexpected error during download')
639
            raise errors.RadioError('Unexpected error communicating '
640
                                    'with the radio')
641
        self._mmap = data
642
        self.process_mmap()
643

    
644
    def sync_out(self):
645
        """Upload to radio"""
646
        try:
647
            do_upload(self)
648
        except:
649
            # If anything unexpected happens, make sure we raise
650
            # a RadioError and log the problem
651
            LOG.exception('Unexpected error during upload')
652
            raise errors.RadioError('Unexpected error communicating '
653
                                    'with the radio')
654

    
655
    def get_raw_memory(self, number):
656
        return repr(self._memobj.memory[number - 1])
657

    
658
    def _get_tone(self, _mem, mem):
659
        def _get_dcs(val):
660
            code = int("%03o" % (val & 0x07FF))
661
            pol = (val & 0x8000) and "R" or "N"
662
            return code, pol
663

    
664
        if _mem.tx_tone != 0xFFFF and _mem.tx_tone > 0x2000:
665
            tcode, tpol = _get_dcs(_mem.tx_tone)
666
            mem.dtcs = tcode
667
            txmode = "DTCS"
668
        elif _mem.tx_tone != 0xFFFF:
669
            mem.rtone = _mem.tx_tone / 10.0
670
            txmode = "Tone"
671
        else:
672
            txmode = ""
673

    
674
        if _mem.rx_tone != 0xFFFF and _mem.rx_tone > 0x2000:
675
            rcode, rpol = _get_dcs(_mem.rx_tone)
676
            mem.rx_dtcs = rcode
677
            rxmode = "DTCS"
678
        elif _mem.rx_tone != 0xFFFF:
679
            mem.ctone = _mem.rx_tone / 10.0
680
            rxmode = "Tone"
681
        else:
682
            rxmode = ""
683

    
684
        if txmode == "Tone" and not rxmode:
685
            mem.tmode = "Tone"
686
        elif txmode == rxmode and txmode == "Tone" and mem.rtone == mem.ctone:
687
            mem.tmode = "TSQL"
688
        elif txmode == rxmode and txmode == "DTCS" and mem.dtcs == mem.rx_dtcs:
689
            mem.tmode = "DTCS"
690
        elif rxmode or txmode:
691
            mem.tmode = "Cross"
692
            mem.cross_mode = "%s->%s" % (txmode, rxmode)
693

    
694
        if mem.tmode == "DTCS":
695
            mem.dtcs_polarity = "%s%s" % (tpol, rpol)
696

    
697
        LOG.debug("Got TX %s (%i) RX %s (%i)" %
698
                  (txmode, _mem.tx_tone, rxmode, _mem.rx_tone))
699

    
700
    def get_memory(self, number):
701
        if self._skipflags:
702
            bitpos = (1 << ((number - 1) % 8))
703
            bytepos = ((number - 1) / 8)
704
            LOG.debug("bitpos %s" % bitpos)
705
            LOG.debug("bytepos %s" % bytepos)
706
            _skp = self._memobj.skipflags[bytepos]
707

    
708
        mem = chirp_common.Memory()
709

    
710
        mem.number = number
711

    
712
        if self.MODEL == "RB17A":
713
            if mem.number < 17:
714
                _mem = self._memobj.lomems[number - 1]
715
            else:
716
                _mem = self._memobj.himems[number - 17]
717
        else:
718
            _mem = self._memobj.memory[number - 1]
719

    
720
        if self._reserved:
721
            _rsvd = _mem.reserved.get_raw()
722

    
723
        mem.freq = int(_mem.rxfreq) * 10
724

    
725
        # We'll consider any blank (i.e. 0MHz frequency) to be empty
726
        if mem.freq == 0:
727
            mem.empty = True
728
            return mem
729

    
730
        if _mem.rxfreq.get_raw() == "\xFF\xFF\xFF\xFF":
731
            mem.freq = 0
732
            mem.empty = True
733
            return mem
734

    
735
        if _mem.get_raw() == ("\xFF" * 16):
736
            LOG.debug("Initializing empty memory")
737
            if self.MODEL == "RB17A":
738
                _mem.set_raw("\x00" * 13 + "\x04\xFF\xFF")
739
            if self.MODEL == "RB26" or self.MODEL == "RT76":
740
                _mem.set_raw("\x00" * 13 + _rsvd)
741
            else:
742
                _mem.set_raw("\x00" * 13 + "\x30\x8F\xF8")
743

    
744
        if int(_mem.rxfreq) == int(_mem.txfreq):
745
            mem.duplex = ""
746
            mem.offset = 0
747
        else:
748
            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
749
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
750

    
751
        mem.mode = _mem.wide and "FM" or "NFM"
752

    
753
        self._get_tone(_mem, mem)
754

    
755
        mem.power = self.POWER_LEVELS[_mem.highpower]
756

    
757
        if self.MODEL != "RT76":
758
            mem.skip = "" if (_skp & bitpos) else "S"
759
            LOG.debug("mem.skip %s" % mem.skip)
760

    
761
        mem.extra = RadioSettingGroup("Extra", "extra")
762

    
763
        if self.MODEL == "RT21" or self.MODEL == "RB17A" or \
764
                self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
765
            rs = RadioSettingValueList(BCL_LIST, BCL_LIST[_mem.bcl])
766
            rset = RadioSetting("bcl", "Busy Channel Lockout", rs)
767
            mem.extra.append(rset)
768

    
769
            rs = RadioSettingValueInteger(1, 8, _mem.scramble_type + 1)
770
            rset = RadioSetting("scramble_type", "Scramble Type", rs)
771
            mem.extra.append(rset)
772

    
773
            if self.MODEL == "RB17A":
774
                rs = RadioSettingValueList(CDCSS_LIST, CDCSS_LIST[_mem.cdcss])
775
                rset = RadioSetting("cdcss", "Cdcss Mode", rs)
776
                mem.extra.append(rset)
777

    
778
            if self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
779
                rs = RadioSettingValueList(CDCSS2_LIST,
780
                                           CDCSS2_LIST[_mem.cdcss])
781
                rset = RadioSetting("cdcss", "Cdcss Mode", rs)
782
                mem.extra.append(rset)
783

    
784
            if self.MODEL == "RB17A" or self.MODEL == "RT29_UHF" or \
785
                    self.MODEL == "RT29_VHF":
786
                rs = RadioSettingValueBoolean(_mem.compander)
787
                rset = RadioSetting("compander", "Compander", rs)
788
                mem.extra.append(rset)
789

    
790
        if self.MODEL == "RB26" or self.MODEL == "RT76":
791
            if self.MODEL == "RB26":
792
                rs = RadioSettingValueBoolean(_mem.bcl)
793
                rset = RadioSetting("bcl", "Busy Channel Lockout", rs)
794
                mem.extra.append(rset)
795

    
796
            rs = RadioSettingValueBoolean(_mem.compander)
797
            rset = RadioSetting("compander", "Compander", rs)
798
            mem.extra.append(rset)
799

    
800
        if self._gmrs:
801
            GMRS_IMMUTABLE = ["freq", "duplex", "offset"]
802
            if mem.number >= 8 and mem.number <= 14:
803
                mem.immutable = GMRS_IMMUTABLE + ["power", "mode"]
804
            else:
805
                mem.immutable = GMRS_IMMUTABLE
806

    
807
        return mem
808

    
809
    def _set_tone(self, mem, _mem):
810
        def _set_dcs(code, pol):
811
            val = int("%i" % code, 8) + 0x2800
812
            if pol == "R":
813
                val += 0x8000
814
            return val
815

    
816
        rx_mode = tx_mode = None
817
        rx_tone = tx_tone = 0xFFFF
818

    
819
        if mem.tmode == "Tone":
820
            tx_mode = "Tone"
821
            rx_mode = None
822
            tx_tone = int(mem.rtone * 10)
823
        elif mem.tmode == "TSQL":
824
            rx_mode = tx_mode = "Tone"
825
            rx_tone = tx_tone = int(mem.ctone * 10)
826
        elif mem.tmode == "DTCS":
827
            tx_mode = rx_mode = "DTCS"
828
            tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
829
            rx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[1])
830
        elif mem.tmode == "Cross":
831
            tx_mode, rx_mode = mem.cross_mode.split("->")
832
            if tx_mode == "DTCS":
833
                tx_tone = _set_dcs(mem.dtcs, mem.dtcs_polarity[0])
834
            elif tx_mode == "Tone":
835
                tx_tone = int(mem.rtone * 10)
836
            if rx_mode == "DTCS":
837
                rx_tone = _set_dcs(mem.rx_dtcs, mem.dtcs_polarity[1])
838
            elif rx_mode == "Tone":
839
                rx_tone = int(mem.ctone * 10)
840

    
841
        _mem.rx_tone = rx_tone
842
        _mem.tx_tone = tx_tone
843

    
844
        LOG.debug("Set TX %s (%i) RX %s (%i)" %
845
                  (tx_mode, _mem.tx_tone, rx_mode, _mem.rx_tone))
846

    
847
    def set_memory(self, mem):
848
        if self._skipflags:
849
            bitpos = (1 << ((mem.number - 1) % 8))
850
            bytepos = ((mem.number - 1) / 8)
851
            LOG.debug("bitpos %s" % bitpos)
852
            LOG.debug("bytepos %s" % bytepos)
853
            _skp = self._memobj.skipflags[bytepos]
854

    
855
        if self.MODEL == "RB17A":
856
            if mem.number < 17:
857
                _mem = self._memobj.lomems[mem.number - 1]
858
            else:
859
                _mem = self._memobj.himems[mem.number - 17]
860
        else:
861
            _mem = self._memobj.memory[mem.number - 1]
862

    
863
        if self._reserved:
864
            _rsvd = _mem.reserved.get_raw()
865

    
866
        if mem.empty:
867
            if self.MODEL == "RB17A":
868
                _mem.set_raw("\xFF" * 12 + "\x00\x00\xFF\xFF")
869
            elif self.MODEL == "RB26" or self.MODEL == "RT76":
870
                _mem.set_raw("\xFF" * 13 + _rsvd)
871
            else:
872
                _mem.set_raw("\xFF" * (_mem.size() / 8))
873

    
874
            if self._gmrs:
875
                GMRS_FREQ = int(GMRS_FREQS[mem.number - 1] * 100000)
876
                if mem.number > 22:
877
                    _mem.rxfreq = GMRS_FREQ
878
                    _mem.txfreq = int(_mem.rxfreq) + 500000
879
                    _mem.wide = True
880
                else:
881
                    _mem.rxfreq = _mem.txfreq = GMRS_FREQ
882
                if mem.number >= 8 and mem.number <= 14:
883
                    _mem.wide = False
884
                    _mem.highpower = False
885
                else:
886
                    _mem.wide = True
887
                    _mem.highpower = True
888

    
889
            return
890

    
891
        if self.MODEL == "RB17A":
892
            _mem.set_raw("\x00" * 13 + "\x00\xFF\xFF")
893
        elif self.MODEL == "RB26" or self.MODEL == "RT76":
894
            _mem.set_raw("\x00" * 13 + _rsvd)
895
        else:
896
            _mem.set_raw("\x00" * 13 + "\x30\x8F\xF8")
897

    
898
        _mem.rxfreq = mem.freq / 10
899

    
900
        if mem.duplex == "off":
901
            for i in range(0, 4):
902
                _mem.txfreq[i].set_raw("\xFF")
903
        elif mem.duplex == "split":
904
            _mem.txfreq = mem.offset / 10
905
        elif mem.duplex == "+":
906
            _mem.txfreq = (mem.freq + mem.offset) / 10
907
        elif mem.duplex == "-":
908
            _mem.txfreq = (mem.freq - mem.offset) / 10
909
        else:
910
            _mem.txfreq = mem.freq / 10
911

    
912
        _mem.wide = mem.mode == "FM"
913

    
914
        self._set_tone(mem, _mem)
915

    
916
        _mem.highpower = mem.power == self.POWER_LEVELS[1]
917

    
918
        if self.MODEL != "RT76":
919
            if mem.skip != "S":
920
                _skp |= bitpos
921
            else:
922
                _skp &= ~bitpos
923
            LOG.debug("_skp %s" % _skp)
924

    
925
        for setting in mem.extra:
926
            if setting.get_name() == "scramble_type":
927
                setattr(_mem, setting.get_name(), int(setting.value) - 1)
928
                if self.MODEL == "RT21":
929
                    setattr(_mem, "scramble_type2", int(setting.value) - 1)
930
            else:
931
                setattr(_mem, setting.get_name(), setting.value)
932

    
933
    def get_settings(self):
934
        _settings = self._memobj.settings
935
        basic = RadioSettingGroup("basic", "Basic Settings")
936
        top = RadioSettings(basic)
937

    
938
        if self.MODEL == "RT21" or self.MODEL == "RB17A" or \
939
                self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
940
            _keys = self._memobj.keys
941

    
942
            rs = RadioSettingValueList(TIMEOUTTIMER_LIST,
943
                                       TIMEOUTTIMER_LIST[_settings.tot])
944
            rset = RadioSetting("tot", "Time-out timer", rs)
945
            basic.append(rset)
946

    
947
            rs = RadioSettingValueList(TOTALERT_LIST,
948
                                       TOTALERT_LIST[_settings.totalert])
949
            rset = RadioSetting("totalert", "TOT Pre-alert", rs)
950
            basic.append(rset)
951

    
952
            rs = RadioSettingValueInteger(0, 9, _settings.squelch)
953
            rset = RadioSetting("squelch", "Squelch Level", rs)
954
            basic.append(rset)
955

    
956
            rs = RadioSettingValueList(VOICE_LIST, VOICE_LIST[_settings.voice])
957
            rset = RadioSetting("voice", "Voice Annumciation", rs)
958
            basic.append(rset)
959

    
960
            if self.MODEL == "RB17A":
961
                rs = RadioSettingValueList(ALARM_LIST,
962
                                           ALARM_LIST[_settings.alarm])
963
                rset = RadioSetting("alarm", "Alarm Type", rs)
964
                basic.append(rset)
965

    
966
            rs = RadioSettingValueBoolean(_settings.save)
967
            rset = RadioSetting("save", "Battery Saver", rs)
968
            basic.append(rset)
969

    
970
            rs = RadioSettingValueBoolean(_settings.use_scramble)
971
            rset = RadioSetting("use_scramble", "Scramble", rs)
972
            basic.append(rset)
973

    
974
            rs = RadioSettingValueBoolean(_settings.use_vox)
975
            rset = RadioSetting("use_vox", "VOX", rs)
976
            basic.append(rset)
977

    
978
            rs = RadioSettingValueList(VOX_LIST, VOX_LIST[_settings.vox])
979
            rset = RadioSetting("vox", "VOX Gain", rs)
980
            basic.append(rset)
981

    
982
            if self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
983
                rs = RadioSettingValueList(VOXD_LIST,
984
                                           VOXD_LIST[_settings.voxd])
985
                rset = RadioSetting("voxd", "Vox Delay", rs)
986
                basic.append(rset)
987

    
988
            def apply_pf1_listvalue(setting, obj):
989
                LOG.debug("Setting value: " + str(
990
                          setting.value) + " from list")
991
                val = str(setting.value)
992
                index = PF1_CHOICES.index(val)
993
                val = PF1_VALUES[index]
994
                obj.set_value(val)
995

    
996
            if self.MODEL == "RT21":
997
                if _keys.pf1 in PF1_VALUES:
998
                    idx = PF1_VALUES.index(_keys.pf1)
999
                else:
1000
                    idx = LIST_DTMF_SPECIAL_VALUES.index(0x04)
1001
                rs = RadioSettingValueList(PF1_CHOICES, PF1_CHOICES[idx])
1002
                rset = RadioSetting("keys.pf1", "PF1 Key Function", rs)
1003
                rset.set_apply_callback(apply_pf1_listvalue, _keys.pf1)
1004
                basic.append(rset)
1005

    
1006
            def apply_pf1_17a_listvalue(setting, obj):
1007
                LOG.debug("Setting value: " + str(
1008
                          setting.value) + " from list")
1009
                val = str(setting.value)
1010
                index = PF1_17A_CHOICES.index(val)
1011
                val = PF1_17A_VALUES[index]
1012
                obj.set_value(val)
1013

    
1014
            if self.MODEL == "RB17A":
1015
                if _keys.pf1 in PF1_17A_VALUES:
1016
                    idx = PF1_17A_VALUES.index(_keys.pf1)
1017
                else:
1018
                    idx = LIST_DTMF_SPECIAL_VALUES.index(0x04)
1019
                rs = RadioSettingValueList(PF1_17A_CHOICES,
1020
                                           PF1_17A_CHOICES[idx])
1021
                rset = RadioSetting("keys.pf1", "PF1 Key Function", rs)
1022
                rset.set_apply_callback(apply_pf1_17a_listvalue, _keys.pf1)
1023
                basic.append(rset)
1024

    
1025
            def apply_topkey_listvalue(setting, obj):
1026
                LOG.debug("Setting value: " + str(setting.value) +
1027
                          " from list")
1028
                val = str(setting.value)
1029
                index = TOPKEY_CHOICES.index(val)
1030
                val = TOPKEY_VALUES[index]
1031
                obj.set_value(val)
1032

    
1033
            if self.MODEL == "RB17A":
1034
                if _keys.topkey in TOPKEY_VALUES:
1035
                    idx = TOPKEY_VALUES.index(_keys.topkey)
1036
                else:
1037
                    idx = TOPKEY_VALUES.index(0x0C)
1038
                rs = RadioSettingValueList(TOPKEY_CHOICES, TOPKEY_CHOICES[idx])
1039
                rset = RadioSetting("keys.topkey", "Top Key Function", rs)
1040
                rset.set_apply_callback(apply_topkey_listvalue, _keys.topkey)
1041
                basic.append(rset)
1042

    
1043
            def apply_pfkey_listvalue(setting, obj):
1044
                LOG.debug("Setting value: " + str(setting.value) +
1045
                          " from list")
1046
                val = str(setting.value)
1047
                index = PFKEY_CHOICES.index(val)
1048
                val = PFKEY_VALUES[index]
1049
                obj.set_value(val)
1050

    
1051
            if self.MODEL == "RT29_UHF" or self.MODEL == "RT29_VHF":
1052
                if _keys.pf1 in PFKEY_VALUES:
1053
                    idx = PFKEY_VALUES.index(_keys.pf1)
1054
                else:
1055
                    idx = PFKEY_VALUES.index(0x04)
1056
                rs = RadioSettingValueList(PFKEY_CHOICES, PFKEY_CHOICES[idx])
1057
                rset = RadioSetting("keys.pf1", "PF1 Key Function", rs)
1058
                rset.set_apply_callback(apply_pfkey_listvalue, _keys.pf1)
1059
                basic.append(rset)
1060

    
1061
                if _keys.pf2 in PFKEY_VALUES:
1062
                    idx = PFKEY_VALUES.index(_keys.pf2)
1063
                else:
1064
                    idx = PFKEY_VALUES.index(0x0A)
1065
                rs = RadioSettingValueList(PFKEY_CHOICES, PFKEY_CHOICES[idx])
1066
                rset = RadioSetting("keys.pf2", "PF2 Key Function", rs)
1067
                rset.set_apply_callback(apply_pfkey_listvalue, _keys.pf2)
1068
                basic.append(rset)
1069

    
1070
        if self.MODEL == "RB26" or self.MODEL == "RT76":
1071
            if self.MODEL == "RB26":
1072
                _settings2 = self._memobj.settings2
1073
                _settings3 = self._memobj.settings3
1074

    
1075
            rs = RadioSettingValueInteger(0, 9, _settings.squelch)
1076
            rset = RadioSetting("squelch", "Squelch Level", rs)
1077
            basic.append(rset)
1078

    
1079
            rs = RadioSettingValueList(TIMEOUTTIMER_LIST,
1080
                                       TIMEOUTTIMER_LIST[_settings.tot])
1081
            rset = RadioSetting("tot", "Time-out timer", rs)
1082
            basic.append(rset)
1083

    
1084
            if self.MODEL == "RT76":
1085
                rs = RadioSettingValueList(VOICE_LIST3,
1086
                                           VOICE_LIST3[_settings.voice])
1087
                rset = RadioSetting("voice", "Voice Annumciation", rs)
1088
                basic.append(rset)
1089

    
1090
            if self.MODEL == "RB26":
1091
                rs = RadioSettingValueList(VOICE_LIST2,
1092
                                           VOICE_LIST2[_settings.voice])
1093
                rset = RadioSetting("voice", "Voice Annumciation", rs)
1094
                basic.append(rset)
1095

    
1096
                rs = RadioSettingValueBoolean(not _settings.chnumberd)
1097
                rset = RadioSetting("chnumberd", "Channel Number Enable", rs)
1098
                basic.append(rset)
1099

    
1100
            rs = RadioSettingValueBoolean(_settings.save)
1101
            rset = RadioSetting("save", "Battery Save", rs)
1102
            basic.append(rset)
1103

    
1104
            rs = RadioSettingValueBoolean(_settings.beep)
1105
            rset = RadioSetting("beep", "Beep", rs)
1106
            basic.append(rset)
1107

    
1108
            if self.MODEL == "RB26":
1109
                rs = RadioSettingValueBoolean(not _settings.tail)
1110
                rset = RadioSetting("tail", "QT/DQT Tail", rs)
1111
                basic.append(rset)
1112

    
1113
            rs = RadioSettingValueList(SAVE_LIST, SAVE_LIST[_settings.savem])
1114
            rset = RadioSetting("savem", "Battery Save Mode", rs)
1115
            basic.append(rset)
1116

    
1117
            rs = RadioSettingValueList(GAIN_LIST, GAIN_LIST[_settings.gain])
1118
            rset = RadioSetting("gain", "MIC Gain", rs)
1119
            basic.append(rset)
1120

    
1121
            rs = RadioSettingValueList(WARN_LIST, WARN_LIST[_settings.warn])
1122
            rset = RadioSetting("warn", "Warn Mode", rs)
1123
            basic.append(rset)
1124

    
1125
            if self.MODEL == "RB26":
1126
                rs = RadioSettingValueBoolean(_settings3.vox)
1127
                rset = RadioSetting("settings3.vox", "Vox Function", rs)
1128
                basic.append(rset)
1129

    
1130
                rs = RadioSettingValueList(VOXL_LIST,
1131
                                           VOXL_LIST[_settings3.voxl])
1132
                rset = RadioSetting("settings3.voxl", "Vox Level", rs)
1133
                basic.append(rset)
1134

    
1135
                rs = RadioSettingValueList(VOXD_LIST,
1136
                                           VOXD_LIST[_settings3.voxd])
1137
                rset = RadioSetting("settings3.voxd", "Vox Delay", rs)
1138
                basic.append(rset)
1139

    
1140
                rs = RadioSettingValueList(PFKEY_LIST,
1141
                                           PFKEY_LIST[_settings.pf1])
1142
                rset = RadioSetting("pf1", "PF1 Key Set", rs)
1143
                basic.append(rset)
1144

    
1145
                rs = RadioSettingValueList(PFKEY_LIST,
1146
                                           PFKEY_LIST[_settings.pf2])
1147
                rset = RadioSetting("pf2", "PF2 Key Set", rs)
1148
                basic.append(rset)
1149

    
1150
                rs = RadioSettingValueInteger(1, 30, _settings2.chnumber + 1)
1151
                rset = RadioSetting("settings2.chnumber", "Channel Number", rs)
1152
                basic.append(rset)
1153

    
1154
            if self.MODEL == "RT76":
1155
                rs = RadioSettingValueBoolean(_settings.vox)
1156
                rset = RadioSetting("vox", "Vox Function", rs)
1157
                basic.append(rset)
1158

    
1159
                rs = RadioSettingValueList(VOXL_LIST,
1160
                                           VOXL_LIST[_settings.voxl])
1161
                rset = RadioSetting("voxl", "Vox Level", rs)
1162
                basic.append(rset)
1163

    
1164
                rs = RadioSettingValueList(VOXD_LIST,
1165
                                           VOXD_LIST[_settings.voxd])
1166
                rset = RadioSetting("voxd", "Vox Delay", rs)
1167
                basic.append(rset)
1168

    
1169
                rs = RadioSettingValueInteger(1, 30, _settings.chnumber + 1)
1170
                rset = RadioSetting("chnumber", "Channel Number", rs)
1171
                basic.append(rset)
1172

    
1173
        return top
1174

    
1175
    def set_settings(self, settings):
1176
        for element in settings:
1177
            if not isinstance(element, RadioSetting):
1178
                self.set_settings(element)
1179
                continue
1180
            else:
1181
                try:
1182
                    if "." in element.get_name():
1183
                        bits = element.get_name().split(".")
1184
                        obj = self._memobj
1185
                        for bit in bits[:-1]:
1186
                            obj = getattr(obj, bit)
1187
                        setting = bits[-1]
1188
                    else:
1189
                        obj = self._memobj.settings
1190
                        setting = element.get_name()
1191

    
1192
                    if element.has_apply_callback():
1193
                        LOG.debug("Using apply callback")
1194
                        element.run_apply_callback()
1195
                    elif setting == "channel":
1196
                        setattr(obj, setting, int(element.value) - 1)
1197
                    elif setting == "chnumber":
1198
                        setattr(obj, setting, int(element.value) - 1)
1199
                    elif setting == "chnumberd":
1200
                        setattr(obj, setting, not int(element.value))
1201
                    elif setting == "tail":
1202
                        setattr(obj, setting, not int(element.value))
1203
                    elif element.value.get_mutable():
1204
                        LOG.debug("Setting %s = %s" % (setting, element.value))
1205
                        setattr(obj, setting, element.value)
1206
                except Exception, e:
1207
                    LOG.debug(element.get_name())
1208
                    raise
1209

    
1210
    @classmethod
1211
    def match_model(cls, filedata, filename):
1212
        if cls.MODEL == "RT21":
1213
            # The RT21 is pre-metadata, so do old-school detection
1214
            match_size = False
1215
            match_model = False
1216

    
1217
            # testing the file data size
1218
            if len(filedata) in [0x0400, ]:
1219
                match_size = True
1220

    
1221
            # testing the model fingerprint
1222
            match_model = model_match(cls, filedata)
1223

    
1224
            if match_size and match_model:
1225
                return True
1226
            else:
1227
                return False
1228
        else:
1229
            # Radios that have always been post-metadata, so never do
1230
            # old-school detection
1231
            return False
1232

    
1233

    
1234
@directory.register
1235
class RB17ARadio(RT21Radio):
1236
    """RETEVIS RB17A"""
1237
    VENDOR = "Retevis"
1238
    MODEL = "RB17A"
1239
    BAUD_RATE = 9600
1240
    BLOCK_SIZE = 0x40
1241
    BLOCK_SIZE_UP = 0x10
1242

    
1243
    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=0.50),
1244
                    chirp_common.PowerLevel("High", watts=5.00)]
1245

    
1246
    _magic = "PROA8US"
1247
    _fingerprint = "P3217s\xF8\xFF"
1248
    _upper = 30
1249
    _skipflags = True
1250
    _reserved = False
1251
    _gmrs = True
1252

    
1253
    _ranges = [
1254
               (0x0000, 0x0300),
1255
              ]
1256
    _memsize = 0x0300
1257

    
1258
    def process_mmap(self):
1259
        self._memobj = bitwise.parse(MEM_FORMAT_RB17A, self._mmap)
1260

    
1261

    
1262
@directory.register
1263
class RB26Radio(RT21Radio):
1264
    """RETEVIS RB26"""
1265
    VENDOR = "Retevis"
1266
    MODEL = "RB26"
1267
    BAUD_RATE = 9600
1268
    BLOCK_SIZE = 0x20
1269
    BLOCK_SIZE_UP = 0x10
1270

    
1271
    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=0.50),
1272
                    chirp_common.PowerLevel("High", watts=3.00)]
1273

    
1274
    _magic = "PHOGR" + "\x01" + "0"
1275
    _fingerprint = "P32073" + "\x02\xFF"
1276
    _upper = 30
1277
    _skipflags = True
1278
    _reserved = True
1279
    _gmrs = True
1280

    
1281
    _ranges = [
1282
               (0x0000, 0x0320),
1283
              ]
1284
    _memsize = 0x0320
1285

    
1286
    def process_mmap(self):
1287
        self._memobj = bitwise.parse(MEM_FORMAT_RB26, self._mmap)
1288

    
1289

    
1290
@directory.register
1291
class RT76Radio(RT21Radio):
1292
    """RETEVIS RT76"""
1293
    VENDOR = "Retevis"
1294
    MODEL = "RT76"
1295
    BAUD_RATE = 9600
1296
    BLOCK_SIZE = 0x20
1297
    BLOCK_SIZE_UP = 0x10
1298

    
1299
    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=0.50),
1300
                    chirp_common.PowerLevel("High", watts=5.00)]
1301

    
1302
    _magic = "PHOGR\x14\xD4"
1303
    _fingerprint = "P32073" + "\x02\xFF"
1304
    _upper = 30
1305
    _skipflags = False
1306
    _reserved = True
1307
    _gmrs = True
1308

    
1309
    _ranges = [
1310
               (0x0000, 0x01E0),
1311
              ]
1312
    _memsize = 0x01E0
1313

    
1314
    def process_mmap(self):
1315
        self._memobj = bitwise.parse(MEM_FORMAT_RT76, self._mmap)
1316

    
1317

    
1318
@directory.register
1319
class RT29UHFRadio(RT21Radio):
1320
    """RETEVIS RT29UHF"""
1321
    VENDOR = "Retevis"
1322
    MODEL = "RT29_UHF"
1323
    BLOCK_SIZE = 0x40
1324
    BLOCK_SIZE_UP = 0x10
1325

    
1326
    POWER_LEVELS = [chirp_common.PowerLevel("Mid", watts=5.00),
1327
                    chirp_common.PowerLevel("High", watts=10.00),
1328
                    chirp_common.PowerLevel("Low", watts=1.00)]
1329

    
1330
    _magic = "PROHRAM"
1331
    _fingerprint = "P3207" + "\x13\xF8\xFF"  # UHF model
1332
    _upper = 16
1333
    _skipflags = True
1334
    _reserved = False
1335

    
1336
    _ranges = [
1337
               (0x0000, 0x0300),
1338
              ]
1339
    _memsize = 0x0400
1340

    
1341
    def process_mmap(self):
1342
        self._memobj = bitwise.parse(MEM_FORMAT_RT29, self._mmap)
1343

    
1344

    
1345
@directory.register
1346
class RT29VHFRadio(RT29UHFRadio):
1347
    """RETEVIS RT29VHF"""
1348
    VENDOR = "Retevis"
1349
    MODEL = "RT29_VHF"
1350

    
1351
    POWER_LEVELS = [chirp_common.PowerLevel("Mid", watts=5.00),
1352
                    chirp_common.PowerLevel("High", watts=10.00),
1353
                    chirp_common.PowerLevel("Low", watts=1.00)]
1354

    
1355
    VALID_BANDS = [(136000000, 174000000)]
1356

    
1357
    _magic = "PROHRAM"
1358
    _fingerprint = "P2207" + "\x01\xF8\xFF"  # VHF model
(3-3/6)