Project

General

Profile

Bug #6793 » radioddity_r2.py

Lucent W, 05/17/2019 02:59 AM

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

    
16
import time
17
import os
18
import struct
19
import logging
20

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

    
28
LOG = logging.getLogger(__name__)
29

    
30
# memory map
31
# 0000 copy of channel 16: 0100 - 010F
32
# 0010 Channel 1
33
# 0020 Channel 2
34
# 0030 Channel 3
35
# 0040 Channel 4
36
# 0050 Channel 5
37
# 0060 Channel 6
38
# 0070 Channel 7
39
# 0080 Channel 8
40
# 0090 Channel 9
41
# 00A0 Channel 10
42
# 00B0 Channel 11
43
# 00C0 Channel 12
44
# 00D0 Channel 13
45
# 00E0 Channel 14
46
# 00F0 Channel 15
47
# 0100 Channel 16
48
# 03C0 various settings
49

    
50
# the last three bytes of every channel are identical
51
# to the first three bytes of the next channel in row.
52
# However it will automatically be filled by the radio itself
53

    
54
MEM_FORMAT = """
55
#seekto 0x0010;
56
struct {
57
  lbcd rx_freq[4];
58
  lbcd tx_freq[4];
59
  lbcd rx_tone[2];
60
  lbcd tx_tone[2];
61
  u8 unknown1:1,
62
    compand:1,
63
    scramb:1,
64
    scanadd:1,
65
    power:1,
66
    mode:1,
67
    unknown2:1,
68
    bclo:1;
69
  u8 unknown3[3];
70
} memory[16];
71

    
72
#seekto 0x03C0;
73
struct {
74
  u8 unknown3c08:1,
75
      scanmode:1,
76
      unknown3c06:1,
77
      unknown3c05:1,
78
      voice:2,
79
      save:1,
80
      beep:1;
81
  u8 squelch;
82
  u8 unknown3c2;
83
  u8 timeout;
84
  u8 voxgain;
85
  u8 specialcode;
86
  u8 unknown3c6;
87
  u8 voxdelay;
88
} settings;
89

    
90
"""
91

    
92
CMD_ACK = "\x06"
93
CMD_STX = "\x02"
94
CMD_ENQ = "\x05"
95

    
96
POWER_LEVELS = [chirp_common.PowerLevel("Low",  watts=0.50),
97
                chirp_common.PowerLevel("High", watts=3.00)]
98
TIMEOUT_LIST = ["Off"] + ["%s seconds" % x for x in range(30, 330, 30)]
99
SCANMODE_LIST = ["Carrier", "Timer"]
100
VOICE_LIST = ["Off", "Chinese", "English"]
101
VOX_LIST = ["Off"] + ["%s" % x for x in range(1, 9)]
102
VOXDELAY_LIST = ["0.5", "1.0", "1.5", "2.0", "2.5", "3.0"]
103
MODE_LIST = ["FM", "NFM"]
104
STEPS = [2.5, 5.0, 6.25, 10.0, 12.5, 25.0]
105
STEP_LIST = [str(x) for x in STEPS]
106

    
107
TONES = chirp_common.TONES
108
DTCS_CODES = chirp_common.DTCS_CODES
109

    
110
SETTING_LISTS = {
111
    "tot": TIMEOUT_LIST,
112
    "scanmode": SCANMODE_LIST,
113
    "voice": VOICE_LIST,
114
    "vox": VOX_LIST,
115
    "voxdelay": VOXDELAY_LIST,
116
    "mode": MODE_LIST,
117
    }
118

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

    
122

    
123
def _r2_enter_programming_mode(radio):
124
    serial = radio.pipe
125

    
126
    magic = "TYOGRAM"
127
    exito = False
128
    serial.write(CMD_STX)
129
    for i in range(0, 5):
130
        for j in range(0, len(magic)):
131
            serial.write(magic[j])
132
        ack = serial.read(1)
133
        if ack == CMD_ACK:
134
            exito = True
135
            break
136

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

    
143
    try:
144
        serial.write(CMD_STX)
145
        ident = serial.read(8)
146
    except:
147
        _r2_exit_programming_mode(radio)
148
        raise errors.RadioError("Error communicating with radio")
149

    
150
    # No idea yet what the next 7 bytes stand for
151
    # as long as they start with ACK we are fine
152
    if not ident.startswith(CMD_ACK):
153
        _r2_exit_programming_mode(radio)
154
        LOG.debug(util.hexprint(ident))
155
        raise errors.RadioError("Radio returned unknown identification string")
156

    
157
    try:
158
        serial.write(CMD_ACK)
159
        ack = serial.read(1)
160
    except:
161
        _r2_exit_programming_mode(radio)
162
        raise errors.RadioError("Error communicating with radio")
163

    
164
    if ack != CMD_ACK:
165
        _r2_exit_programming_mode(radio)
166
        raise errors.RadioError("Radio refused to enter programming mode")
167

    
168
    # the next 6 bytes represent the 6 digit password
169
    # they are somehow coded where '1' becomes x01 and 'a' becomes x25
170
    try:
171
        serial.write(CMD_ENQ)
172
        ack = serial.read(6)
173
    except:
174
        _r2_exit_programming_mode(radio)
175
        raise errors.RadioError("Error communicating with radio")
176

    
177
    # we will only read if no password is set
178
    if ack != "\xFF\xFF\xFF\xFF\xFF\xFF":
179
        _r2_exit_programming_mode(radio)
180
        raise errors.RadioError("Radio is password protected")
181
    try:
182
        serial.write(CMD_ACK)
183
        ack = serial.read(6)
184

    
185
    except:
186
        _r2_exit_programming_mode(radio)
187
        raise errors.RadioError("Error communicating with radio 2")
188

    
189
    if ack != CMD_ACK:
190
        _r2_exit_programming_mode(radio)
191
        raise errors.RadioError("Radio refused to enter programming mode 2")
192

    
193

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

    
201

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

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

    
209
    try:
210
        for j in range(0, len(cmd)):
211
            serial.write(cmd[j])
212

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

    
218
        block_data = response[4:]
219

    
220
        serial.write(CMD_ACK)
221
        ack = serial.read(1)
222
    except:
223
        _r2_exit_programming_mode(radio)
224
        raise errors.RadioError("Failed to read block at %04x" % block_addr)
225

    
226
    if ack != CMD_ACK:
227
        _r2_exit_programming_mode(radio)
228
        raise Exception("No ACK reading block %04x." % (block_addr))
229

    
230
    return block_data
231

    
232

    
233
def _r2_write_block(radio, block_addr, block_size):
234
    serial = radio.pipe
235

    
236
    cmd = struct.pack(">cHb", 'W', block_addr, block_size)
237
    data = radio.get_mmap()[block_addr:block_addr + block_size]
238

    
239
    LOG.debug("Writing block %04x..." % (block_addr))
240
    LOG.debug(util.hexprint(cmd + data))
241

    
242
    try:
243
        for j in range(0, len(cmd)):
244
            serial.write(cmd[j])
245
        for j in range(0, len(data)):
246
            serial.write(data[j])
247
        if serial.read(1) != CMD_ACK:
248
            raise Exception("No ACK")
249
    except:
250
        _r2_exit_programming_mode(radio)
251
        raise errors.RadioError("Failed to send block "
252
                                "%04x to radio" % block_addr)
253

    
254

    
255
def do_download(radio):
256
    LOG.debug("download")
257
    _r2_enter_programming_mode(radio)
258

    
259
    data = ""
260

    
261
    status = chirp_common.Status()
262
    status.msg = "Cloning from radio"
263

    
264
    status.cur = 0
265
    status.max = radio._memsize
266

    
267
    for addr in range(0, radio._memsize, radio._block_size):
268
        status.cur = addr + radio._block_size
269
        radio.status_fn(status)
270

    
271
        block = _r2_read_block(radio, addr, radio._block_size)
272
        data += block
273

    
274
        LOG.debug("Address: %04x" % addr)
275
        LOG.debug(util.hexprint(block))
276

    
277
    data += radio.MODEL.ljust(8)
278

    
279
    _r2_exit_programming_mode(radio)
280

    
281
    return memmap.MemoryMap(data)
282

    
283

    
284
def do_upload(radio):
285
    status = chirp_common.Status()
286
    status.msg = "Uploading to radio"
287

    
288
    _r2_enter_programming_mode(radio)
289

    
290
    status.cur = 0
291
    status.max = radio._memsize
292

    
293
    for start_addr, end_addr, block_size in radio._ranges:
294
        for addr in range(start_addr, end_addr, block_size):
295
            status.cur = addr + block_size
296
            radio.status_fn(status)
297
            _r2_write_block(radio, addr, block_size)
298

    
299
    _r2_exit_programming_mode(radio)
300

    
301

    
302
@directory.register
303
class RadioddityR2Radio(chirp_common.CloneModeRadio):
304
    """Radioddity R2"""
305
    VENDOR = "Radioddity"
306
    MODEL = "R2"
307
    BAUD_RATE = 9600
308

    
309
    # definitions on how to read StartAddr EndAddr BlockZize
310
    _ranges = [
311
               (0x0000, 0x01F8, 0x08),
312
               (0x01F8, 0x03F0, 0x08)
313
              ]
314
    _memsize = 0x03F0
315
    # never read more than 8 bytes at once
316
    _block_size = 0x08
317
    # frequency range is 400-470MHz
318
    _range = [400000000, 470000000]
319
    # maximum 16 channels
320
    _upper = 16
321

    
322
    def get_features(self):
323
        rf = chirp_common.RadioFeatures()
324
        rf.has_settings = True
325
        rf.has_bank = False
326
        rf.has_tuning_step = False
327
        rf.valid_tuning_steps = STEPS
328
        rf.has_name = False
329
        rf.has_offset = True
330
        rf.has_mode = True
331
        rf.has_dtcs = True
332
        rf.has_rx_dtcs = True
333
        rf.has_dtcs_polarity = True
334
        rf.has_ctone = True
335
        rf.has_cross = True
336
        rf.can_odd_split = False
337
        # FIXME: Is this right? The get_memory() has no checking for
338
        # deleted memories, but set_memory() used to reference a missing
339
        # variable likely copied from another driver
340
        rf.can_delete = False
341
        rf.valid_modes = MODE_LIST
342
        rf.valid_duplexes = ["", "-", "+", "off"]
343
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
344
        rf.valid_cross_modes = [
345
            "Tone->DTCS",
346
            "DTCS->Tone",
347
            "->Tone",
348
            "Tone->Tone",
349
            "->DTCS",
350
            "DTCS->",
351
            "DTCS->DTCS"]
352
        rf.valid_power_levels = POWER_LEVELS
353
        rf.valid_skips = []
354
        rf.valid_bands = [self._range]
355
        rf.memory_bounds = (1, self._upper)
356
        return rf
357

    
358
    def process_mmap(self):
359
        """Process the mem map into the mem object"""
360
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
361
        # to set the vars on the class to the correct ones
362

    
363
    def sync_in(self):
364
        """Download from radio"""
365
        try:
366
            data = do_download(self)
367
        except errors.RadioError:
368
            # Pass through any real errors we raise
369
            raise
370
        except:
371
            # If anything unexpected happens, make sure we raise
372
            # a RadioError and log the problem
373
            LOG.exception('Unexpected error during download')
374
            raise errors.RadioError('Unexpected error communicating '
375
                                    'with the radio')
376
        self._mmap = data
377
        self.process_mmap()
378

    
379
    def sync_out(self):
380
        """Upload to radio"""
381
        try:
382
            do_upload(self)
383
        except:
384
            # If anything unexpected happens, make sure we raise
385
            # a RadioError and log the problem
386
            LOG.exception('Unexpected error during upload')
387
            raise errors.RadioError('Unexpected error communicating '
388
                                    'with the radio')
389

    
390
    def get_raw_memory(self, number):
391
        return repr(self._memobj.memory[number - 1])
392

    
393
    def decode_tone(self, val):
394
        """Parse the tone data to decode from mem, it returns:
395
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
396
        if val.get_raw() == "\xFF\xFF":
397
            return '', None, None
398

    
399
        val = int(val)
400

    
401
        if val >= 12000:
402
            a = val - 12000
403
            return 'DTCS', a, 'R'
404

    
405
        elif val >= 8000:
406
            a = val - 8000
407
            return 'DTCS', a, 'N'
408

    
409
        else:
410
            a = val / 10.0
411
            return 'Tone', a, None
412

    
413
    def encode_tone(self, memval, mode, value, pol):
414
        """Parse the tone data to encode from UI to mem"""
415
        if mode == '':
416
            memval[0].set_raw(0xFF)
417
            memval[1].set_raw(0xFF)
418
        elif mode == 'Tone':
419
            memval.set_value(int(value * 10))
420
        elif mode == 'DTCS':
421
            flag = 0x80 if pol == 'N' else 0xC0
422
            memval.set_value(value)
423
            memval[1].set_bits(flag)
424
        else:
425
            raise Exception("Internal error: invalid mode `%s'" % mode)
426

    
427
    def get_memory(self, number):
428
        bitpos = (1 << ((number - 1) % 8))
429
        bytepos = ((number - 1) / 8)
430
        LOG.debug("bitpos %s" % bitpos)
431
        LOG.debug("bytepos %s" % bytepos)
432

    
433
        _mem = self._memobj.memory[number - 1]
434

    
435
        mem = chirp_common.Memory()
436

    
437
        mem.number = number
438

    
439
        mem.freq = int(_mem.rx_freq) * 10
440

    
441
        txfreq = int(_mem.tx_freq) * 10
442
        if txfreq == mem.freq:
443
            mem.duplex = ""
444
        elif txfreq == 0:
445
            mem.duplex = "off"
446
            mem.offset = 0
447
        # 166666665*10 is the equivalent for FF FF FF FF
448
        # stored in the TX field
449
        elif txfreq == 1666666650:
450
            mem.duplex = "off"
451
            mem.offset = 0
452
        elif txfreq < mem.freq:
453
            mem.duplex = "-"
454
            mem.offset = mem.freq - txfreq
455
        elif txfreq > mem.freq:
456
            mem.duplex = "+"
457
            mem.offset = txfreq - mem.freq
458

    
459
        # get bandwith FM or NFM
460
        mem.mode = MODE_LIST[_mem.mode]
461

    
462
        # tone data
463
        txtone = self.decode_tone(_mem.tx_tone)
464
        rxtone = self.decode_tone(_mem.rx_tone)
465
        chirp_common.split_tone_decode(mem, txtone, rxtone)
466

    
467
        mem.power = POWER_LEVELS[_mem.power]
468

    
469
        # add extra channel settings to the OTHER tab of the properties
470
        # extra settings are unfortunately inverted
471
        mem.extra = RadioSettingGroup("extra", "Extra")
472

    
473
        scanadd = RadioSetting("scanadd", "Scan Add",
474
                               RadioSettingValueBoolean(
475
                                   not bool(_mem.scanadd)))
476
        scanadd.set_doc("Add channel for scanning")
477
        mem.extra.append(scanadd)
478

    
479
        bclo = RadioSetting("bclo", "Busy Lockout",
480
                            RadioSettingValueBoolean(not bool(_mem.bclo)))
481
        bclo.set_doc("Busy Lockout")
482
        mem.extra.append(bclo)
483

    
484
        scramb = RadioSetting("scramb", "Scramble",
485
                              RadioSettingValueBoolean(not bool(_mem.scramb)))
486
        scramb.set_doc("Scramble Audio Signal")
487
        mem.extra.append(scramb)
488

    
489
        compand = RadioSetting("compand", "Compander",
490
                               RadioSettingValueBoolean(
491
                                   not bool(_mem.compand)))
492
        compand.set_doc("Compress Audio for TX")
493
        mem.extra.append(compand)
494

    
495
        return mem
496

    
497
    def set_memory(self, mem):
498
        bitpos = (1 << ((mem.number - 1) % 8))
499
        bytepos = ((mem.number - 1) / 8)
500
        LOG.debug("bitpos %s" % bitpos)
501
        LOG.debug("bytepos %s" % bytepos)
502

    
503
        # Get a low-level memory object mapped to the image
504
        _mem = self._memobj.memory[mem.number - 1]
505

    
506
        LOG.warning('This driver may be broken for deleted memories')
507
        if mem.empty:
508
            return
509

    
510
        _mem.rx_freq = mem.freq / 10
511

    
512
        if mem.duplex == "off":
513
            for i in range(0, 4):
514
                _mem.tx_freq[i].set_raw("\xFF")
515
        elif mem.duplex == "+":
516
            _mem.tx_freq = (mem.freq + mem.offset) / 10
517
        elif mem.duplex == "-":
518
            _mem.tx_freq = (mem.freq - mem.offset) / 10
519
        else:
520
            _mem.tx_freq = mem.freq / 10
521

    
522
        # power, default power is low
523
        if mem.power:
524
            _mem.power = POWER_LEVELS.index(mem.power)
525
        else:
526
            _mem.power = 0     # low
527

    
528
        # tone data
529
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
530
            chirp_common.split_tone_encode(mem)
531
        self.encode_tone(_mem.tx_tone, txmode, txtone, txpol)
532
        self.encode_tone(_mem.rx_tone, rxmode, rxtone, rxpol)
533

    
534
        _mem.mode = MODE_LIST.index(mem.mode)
535

    
536
        # extra settings are unfortunately inverted
537
        for setting in mem.extra:
538
            LOG.debug("@set_mem:", setting.get_name(), setting.value)
539
            setattr(_mem, setting.get_name(), not setting.value)
540

    
541
    def get_settings(self):
542
        _settings = self._memobj.settings
543
        basic = RadioSettingGroup("basic", "Basic Settings")
544
        top = RadioSettings(basic)
545

    
546
        rs = RadioSetting("settings.squelch", "Squelch Level",
547
                          RadioSettingValueInteger(0, 9, _settings.squelch))
548
        basic.append(rs)
549

    
550
        rs = RadioSetting("settings.timeout", "Timeout Timer",
551
                          RadioSettingValueList(
552
                              TIMEOUT_LIST, TIMEOUT_LIST[_settings.timeout]))
553

    
554
        basic.append(rs)
555

    
556
        rs = RadioSetting("settings.scanmode", "Scan Mode",
557
                          RadioSettingValueList(
558
                              SCANMODE_LIST,
559
                              SCANMODE_LIST[_settings.scanmode]))
560
        basic.append(rs)
561

    
562
        rs = RadioSetting("settings.voice", "Voice Prompts",
563
                          RadioSettingValueList(
564
                              VOICE_LIST, VOICE_LIST[_settings.voice]))
565
        basic.append(rs)
566

    
567
        rs = RadioSetting("settings.voxgain", "VOX Level",
568
                          RadioSettingValueList(
569
                              VOX_LIST, VOX_LIST[_settings.voxgain]))
570
        basic.append(rs)
571

    
572
        rs = RadioSetting("settings.voxdelay", "VOX Delay Time",
573
                          RadioSettingValueList(
574
                              VOXDELAY_LIST,
575
                              VOXDELAY_LIST[_settings.voxdelay]))
576
        basic.append(rs)
577

    
578
        rs = RadioSetting("settings.save", "Battery Save",
579
                          RadioSettingValueBoolean(_settings.save))
580
        basic.append(rs)
581

    
582
        rs = RadioSetting("settings.beep", "Beep Tone",
583
                          RadioSettingValueBoolean(_settings.beep))
584
        basic.append(rs)
585

    
586
        def _filter(name):
587
            filtered = ""
588
            for char in str(name):
589
                if char in VALID_CHARS:
590
                    filtered += char
591
                else:
592
                    filtered += " "
593
            return filtered
594

    
595
        return top
596

    
597
    def set_settings(self, settings):
598
        for element in settings:
599
            if not isinstance(element, RadioSetting):
600
                self.set_settings(element)
601
                continue
602
            else:
603
                try:
604
                    if "." in element.get_name():
605
                        bits = element.get_name().split(".")
606
                        obj = self._memobj
607
                        for bit in bits[:-1]:
608
                            obj = getattr(obj, bit)
609
                        setting = bits[-1]
610
                    else:
611
                        obj = self._memobj.settings
612
                        setting = element.get_name()
613

    
614
                    LOG.debug("Setting %s = %s" % (setting, element.value))
615
                    setattr(obj, setting, element.value)
616
                except Exception, e:
617
                    LOG.debug(element.get_name())
618
                    raise
619

    
620
    @classmethod
621
    def match_model(cls, filedata, filename):
622
        # This radio has always been post-metadata, so never do
623
        # old-school detection
624
        return False
(1-1/2)