Project

General

Profile

Bug #8197 » h777_reworked_timeout.py

Jim Unroe, 12/17/2020 06:48 PM

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

    
17
import time
18
import os
19
import struct
20
import unittest
21
import logging
22

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

    
29
LOG = logging.getLogger(__name__)
30

    
31
MEM_FORMAT = """
32
#seekto 0x0010;
33
struct {
34
    lbcd rxfreq[4];
35
    lbcd txfreq[4];
36
    lbcd rxtone[2];
37
    lbcd txtone[2];
38
    u8 unknown3:1,
39
       unknown2:1,
40
       unknown1:1,
41
       skip:1,
42
       highpower:1,
43
       narrow:1,
44
       beatshift:1,
45
       bcl:1;
46
    u8 unknown4[3];
47
} memory[16];
48
#seekto 0x02B0;
49
struct {
50
    u8 voiceprompt;
51
    u8 voicelanguage;
52
    u8 scan;
53
    u8 vox;
54
    u8 voxlevel;
55
    u8 voxinhibitonrx;
56
    u8 lowvolinhibittx;
57
    u8 highvolinhibittx;
58
    u8 alarm;
59
    u8 fmradio;
60
} settings;
61
#seekto 0x03C0;
62
struct {
63
    u8 unused:6,
64
       batterysaver:1,
65
       beep:1;
66
    u8 squelchlevel;
67
    u8 sidekeyfunction;
68
    u8 timeouttimer;
69
    u8 unused2[3];
70
    u8 unused3:7,
71
       scanmode:1;
72
} settings2;
73
"""
74

    
75
CMD_ACK = "\x06"
76
BLOCK_SIZE = 0x08
77
UPLOAD_BLOCKS = [range(0x0000, 0x0110, 8),
78
                 range(0x02b0, 0x02c0, 8),
79
                 range(0x0380, 0x03e0, 8)]
80

    
81
# TODO: Is it 1 watt?
82
H777_POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=1.00),
83
                     chirp_common.PowerLevel("High", watts=5.00)]
84
VOICE_LIST = ["English", "Chinese"]
85
TIMEOUTTIMER_LIST = ["Off", "30 seconds", "60 seconds", "90 seconds",
86
                     "120 seconds", "150 seconds", "180 seconds",
87
                     "210 seconds", "240 seconds", "270 seconds",
88
                     "300 seconds"]
89
SCANMODE_LIST = ["Carrier", "Time"]
90

    
91
SETTING_LISTS = {
92
    "voice": VOICE_LIST,
93
}
94

    
95

    
96
def _h777_enter_programming_mode(radio):
97
    serial = radio.pipe
98
    # At least one version of the Baofeng BF-888S has a consistent
99
    # ~0.33s delay between sending the first five bytes of the
100
    # version data and the last three bytes. We need to raise the
101
    # timeout so that the read doesn't finish early.
102
    serial.timeout = 0.5
103

    
104
    try:
105
        serial.write("\x02")
106
        time.sleep(0.1)
107
        serial.write("PROGRAM")
108
        ack = serial.read(1)
109
    except:
110
        raise errors.RadioError("Error communicating with radio")
111

    
112
    if not ack:
113
        raise errors.RadioError("No response from radio")
114
    elif ack != CMD_ACK:
115
        raise errors.RadioError("Radio refused to enter programming mode")
116

    
117
    try:
118
        serial.write("\x02")
119
        ident = serial.read(8)
120
    except:
121
        raise errors.RadioError("Error communicating with radio")
122

    
123
    if not ident.startswith("P3107"):
124
        LOG.debug(util.hexprint(ident))
125
        raise errors.RadioError("Radio returned unknown identification string")
126

    
127
    try:
128
        serial.write(CMD_ACK)
129
        ack = serial.read(1)
130
    except:
131
        raise errors.RadioError("Error communicating with radio")
132

    
133
    if ack != CMD_ACK:
134
        raise errors.RadioError("Radio refused to enter programming mode")
135

    
136

    
137
def _h777_exit_programming_mode(radio):
138
    serial = radio.pipe
139
    try:
140
        serial.write("E")
141
    except:
142
        raise errors.RadioError("Radio refused to exit programming mode")
143

    
144

    
145
def _h777_read_block(radio, block_addr, block_size):
146
    serial = radio.pipe
147

    
148
    cmd = struct.pack(">cHb", 'R', block_addr, BLOCK_SIZE)
149
    expectedresponse = "W" + cmd[1:]
150
    LOG.debug("Reading block %04x..." % (block_addr))
151

    
152
    try:
153
        serial.write(cmd)
154
        response = serial.read(4 + BLOCK_SIZE)
155
        if response[:4] != expectedresponse:
156
            raise Exception("Error reading block %04x." % (block_addr))
157

    
158
        block_data = response[4:]
159

    
160
        serial.write(CMD_ACK)
161
        ack = serial.read(1)
162
    except:
163
        raise errors.RadioError("Failed to read block at %04x" % block_addr)
164

    
165
    if ack != CMD_ACK:
166
        raise Exception("No ACK reading block %04x." % (block_addr))
167

    
168
    return block_data
169

    
170

    
171
def _h777_write_block(radio, block_addr, block_size):
172
    serial = radio.pipe
173

    
174
    cmd = struct.pack(">cHb", 'W', block_addr, BLOCK_SIZE)
175
    data = radio.get_mmap()[block_addr:block_addr + 8]
176

    
177
    LOG.debug("Writing Data:")
178
    LOG.debug(util.hexprint(cmd + data))
179

    
180
    try:
181
        serial.write(cmd + data)
182
        if serial.read(1) != CMD_ACK:
183
            raise Exception("No ACK")
184
    except:
185
        raise errors.RadioError("Failed to send block "
186
                                "to radio at %04x" % block_addr)
187

    
188

    
189
def do_download(radio):
190
    LOG.debug("download")
191
    _h777_enter_programming_mode(radio)
192

    
193
    data = ""
194

    
195
    status = chirp_common.Status()
196
    status.msg = "Cloning from radio"
197

    
198
    status.cur = 0
199
    status.max = radio._memsize
200

    
201
    for addr in range(0, radio._memsize, BLOCK_SIZE):
202
        status.cur = addr + BLOCK_SIZE
203
        radio.status_fn(status)
204

    
205
        block = _h777_read_block(radio, addr, BLOCK_SIZE)
206
        data += block
207

    
208
        LOG.debug("Address: %04x" % addr)
209
        LOG.debug(util.hexprint(block))
210

    
211
    _h777_exit_programming_mode(radio)
212

    
213
    return memmap.MemoryMap(data)
214

    
215

    
216
def do_upload(radio):
217
    status = chirp_common.Status()
218
    status.msg = "Uploading to radio"
219

    
220
    _h777_enter_programming_mode(radio)
221

    
222
    status.cur = 0
223
    status.max = radio._memsize
224

    
225
    for start_addr, end_addr in radio._ranges:
226
        for addr in range(start_addr, end_addr, BLOCK_SIZE):
227
            status.cur = addr + BLOCK_SIZE
228
            radio.status_fn(status)
229
            _h777_write_block(radio, addr, BLOCK_SIZE)
230

    
231
    _h777_exit_programming_mode(radio)
232

    
233

    
234
class ArcshellAR5(chirp_common.Alias):
235
    VENDOR = 'Arcshell'
236
    MODEL = 'AR-5'
237

    
238

    
239
class ArcshellAR6(chirp_common.Alias):
240
    VENDOR = 'Arcshell'
241
    MODEL = 'AR-6'
242

    
243

    
244
class GV8SAlias(chirp_common.Alias):
245
    VENDOR = 'Greaval'
246
    MODEL = 'GV-8S'
247

    
248

    
249
class GV9SAlias(chirp_common.Alias):
250
    VENDOR = 'Greaval'
251
    MODEL = 'GV-9S'
252

    
253

    
254
class A8SAlias(chirp_common.Alias):
255
    VENDOR = 'Ansoko'
256
    MODEL = 'A-8S'
257

    
258

    
259
class TenwayTW325Alias(chirp_common.Alias):
260
    VENDOR = 'Tenway'
261
    MODEL = 'TW-325'
262

    
263

    
264
class RetevisH777Alias(chirp_common.Alias):
265
    VENDOR = 'Retevis'
266
    MODEL = 'H777'
267

    
268

    
269
@directory.register
270
class H777Radio(chirp_common.CloneModeRadio):
271
    """HST H-777"""
272
    # VENDOR = "Heng Shun Tong (恒顺通)"
273
    # MODEL = "H-777"
274
    VENDOR = "Baofeng"
275
    MODEL = "BF-888"
276
    BAUD_RATE = 9600
277

    
278
    ALIASES = [ArcshellAR5, ArcshellAR6, GV8SAlias, GV9SAlias, A8SAlias,
279
               TenwayTW325Alias, RetevisH777Alias]
280
    SIDEKEYFUNCTION_LIST = ["Off", "Monitor", "Transmit Power", "Alarm"]
281

    
282
    # This code currently requires that ranges start at 0x0000
283
    # and are continious. In the original program 0x0388 and 0x03C8
284
    # are only written (all bytes 0xFF), not read.
285
    # _ranges = [
286
    #       (0x0000, 0x0110),
287
    #       (0x02B0, 0x02C0),
288
    #       (0x0380, 0x03E0)
289
    #       ]
290
    # Memory starts looping at 0x1000... But not every 0x1000.
291

    
292
    _ranges = [
293
        (0x0000, 0x0110),
294
        (0x0380, 0x03E0),
295
        (0x02B0, 0x02C0),
296
    ]
297
    _memsize = 0x03E0
298
    _has_fm = True
299
    _has_sidekey = True
300

    
301
    def get_features(self):
302
        rf = chirp_common.RadioFeatures()
303
        rf.has_settings = True
304
        rf.valid_modes = ["NFM", "FM"]  # 12.5 KHz, 25 kHz.
305
        rf.valid_skips = ["", "S"]
306
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
307
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
308
        rf.can_odd_split = True
309
        rf.has_rx_dtcs = True
310
        rf.has_ctone = True
311
        rf.has_cross = True
312
        rf.valid_cross_modes = [
313
            "Tone->Tone",
314
            "DTCS->",
315
            "->DTCS",
316
            "Tone->DTCS",
317
            "DTCS->Tone",
318
            "->Tone",
319
            "DTCS->DTCS"]
320
        rf.has_tuning_step = False
321
        rf.has_bank = False
322
        rf.has_name = False
323
        rf.memory_bounds = (1, 16)
324
        rf.valid_bands = [(400000000, 470000000)]
325
        rf.valid_power_levels = H777_POWER_LEVELS
326
        rf.valid_tuning_steps = [2.5, 5.0, 6.25, 10.0, 12.5, 15.0, 20.0, 25.0,
327
                                 50.0, 100.0]
328

    
329
        return rf
330

    
331
    def process_mmap(self):
332
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
333

    
334
    def sync_in(self):
335
        self._mmap = do_download(self)
336
        self.process_mmap()
337

    
338
    def sync_out(self):
339
        do_upload(self)
340

    
341
    def get_raw_memory(self, number):
342
        return repr(self._memobj.memory[number - 1])
343

    
344
    def _decode_tone(self, val):
345
        val = int(val)
346
        if val == 16665:
347
            return '', None, None
348
        elif val >= 12000:
349
            return 'DTCS', val - 12000, 'R'
350
        elif val >= 8000:
351
            return 'DTCS', val - 8000, 'N'
352
        else:
353
            return 'Tone', val / 10.0, None
354

    
355
    def _encode_tone(self, memval, mode, value, pol):
356
        if mode == '':
357
            memval[0].set_raw(0xFF)
358
            memval[1].set_raw(0xFF)
359
        elif mode == 'Tone':
360
            memval.set_value(int(value * 10))
361
        elif mode == 'DTCS':
362
            flag = 0x80 if pol == 'N' else 0xC0
363
            memval.set_value(value)
364
            memval[1].set_bits(flag)
365
        else:
366
            raise Exception("Internal error: invalid mode `%s'" % mode)
367

    
368
    def get_memory(self, number):
369
        _mem = self._memobj.memory[number - 1]
370

    
371
        mem = chirp_common.Memory()
372

    
373
        mem.number = number
374
        mem.freq = int(_mem.rxfreq) * 10
375

    
376
        # We'll consider any blank (i.e. 0MHz frequency) to be empty
377
        if mem.freq == 0:
378
            mem.empty = True
379
            return mem
380

    
381
        if _mem.rxfreq.get_raw() == "\xFF\xFF\xFF\xFF":
382
            mem.freq = 0
383
            mem.empty = True
384
            return mem
385

    
386
        if _mem.txfreq.get_raw() == "\xFF\xFF\xFF\xFF":
387
            mem.duplex = "off"
388
            mem.offset = 0
389
        elif int(_mem.rxfreq) == int(_mem.txfreq):
390
            mem.duplex = ""
391
            mem.offset = 0
392
        else:
393
            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
394
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
395

    
396
        mem.mode = not _mem.narrow and "FM" or "NFM"
397
        mem.power = H777_POWER_LEVELS[_mem.highpower]
398

    
399
        mem.skip = _mem.skip and "S" or ""
400

    
401
        txtone = self._decode_tone(_mem.txtone)
402
        rxtone = self._decode_tone(_mem.rxtone)
403
        chirp_common.split_tone_decode(mem, txtone, rxtone)
404

    
405
        mem.extra = RadioSettingGroup("Extra", "extra")
406
        rs = RadioSetting("bcl", "Busy Channel Lockout",
407
                          RadioSettingValueBoolean(not _mem.bcl))
408
        mem.extra.append(rs)
409
        rs = RadioSetting("beatshift", "Beat Shift(scramble)",
410
                          RadioSettingValueBoolean(not _mem.beatshift))
411
        mem.extra.append(rs)
412

    
413
        return mem
414

    
415
    def set_memory(self, mem):
416
        # Get a low-level memory object mapped to the image
417
        _mem = self._memobj.memory[mem.number - 1]
418

    
419
        if mem.empty:
420
            _mem.set_raw("\xFF" * (_mem.size() / 8))
421
            return
422

    
423
        _mem.rxfreq = mem.freq / 10
424

    
425
        if mem.duplex == "off":
426
            for i in range(0, 4):
427
                _mem.txfreq[i].set_raw("\xFF")
428
        elif mem.duplex == "split":
429
            _mem.txfreq = mem.offset / 10
430
        elif mem.duplex == "+":
431
            _mem.txfreq = (mem.freq + mem.offset) / 10
432
        elif mem.duplex == "-":
433
            _mem.txfreq = (mem.freq - mem.offset) / 10
434
        else:
435
            _mem.txfreq = mem.freq / 10
436

    
437
        txtone, rxtone = chirp_common.split_tone_encode(mem)
438
        self._encode_tone(_mem.txtone, *txtone)
439
        self._encode_tone(_mem.rxtone, *rxtone)
440

    
441
        _mem.narrow = 'N' in mem.mode
442
        _mem.highpower = mem.power == H777_POWER_LEVELS[1]
443
        _mem.skip = mem.skip == "S"
444

    
445
        for setting in mem.extra:
446
            # NOTE: Only two settings right now, both are inverted
447
            setattr(_mem, setting.get_name(), not int(setting.value))
448

    
449
        # When set to one, official programming software (BF-480) shows always
450
        # "WFM", even if we choose "NFM". Therefore, for compatibility
451
        # purposes, we will set these to zero.
452
        _mem.unknown1 = 0
453
        _mem.unknown2 = 0
454
        _mem.unknown3 = 0
455

    
456
    def get_settings(self):
457
        _settings = self._memobj.settings
458
        basic = RadioSettingGroup("basic", "Basic Settings")
459
        top = RadioSettings(basic)
460

    
461
        # TODO: Check that all these settings actually do what they
462
        # say they do.
463

    
464
        rs = RadioSetting("voiceprompt", "Voice prompt",
465
                          RadioSettingValueBoolean(_settings.voiceprompt))
466
        basic.append(rs)
467

    
468
        rs = RadioSetting("voicelanguage", "Voice language",
469
                          RadioSettingValueList(
470
                              VOICE_LIST,
471
                              VOICE_LIST[_settings.voicelanguage]))
472
        basic.append(rs)
473

    
474
        rs = RadioSetting("scan", "Scan",
475
                          RadioSettingValueBoolean(_settings.scan))
476
        basic.append(rs)
477

    
478
        rs = RadioSetting("settings2.scanmode", "Scan mode",
479
                          RadioSettingValueList(
480
                              SCANMODE_LIST,
481
                              SCANMODE_LIST[self._memobj.settings2.scanmode]))
482
        basic.append(rs)
483

    
484
        rs = RadioSetting("vox", "VOX",
485
                          RadioSettingValueBoolean(_settings.vox))
486
        basic.append(rs)
487

    
488
        rs = RadioSetting("voxlevel", "VOX level",
489
                          RadioSettingValueInteger(
490
                              1, 5, _settings.voxlevel + 1))
491
        basic.append(rs)
492

    
493
        rs = RadioSetting("voxinhibitonrx", "Inhibit VOX on receive",
494
                          RadioSettingValueBoolean(_settings.voxinhibitonrx))
495
        basic.append(rs)
496

    
497
        rs = RadioSetting("lowvolinhibittx", "Low voltage inhibit transmit",
498
                          RadioSettingValueBoolean(_settings.lowvolinhibittx))
499
        basic.append(rs)
500

    
501
        rs = RadioSetting("highvolinhibittx", "High voltage inhibit transmit",
502
                          RadioSettingValueBoolean(_settings.highvolinhibittx))
503
        basic.append(rs)
504

    
505
        rs = RadioSetting("alarm", "Alarm",
506
                          RadioSettingValueBoolean(_settings.alarm))
507
        basic.append(rs)
508

    
509
        # TODO: This should probably be called “FM Broadcast Band Radio”
510
        # or something. I'm not sure if the model actually has one though.
511
        if self._has_fm:
512
            rs = RadioSetting("fmradio", "FM function",
513
                              RadioSettingValueBoolean(_settings.fmradio))
514
            basic.append(rs)
515

    
516
        rs = RadioSetting("settings2.beep", "Beep",
517
                          RadioSettingValueBoolean(
518
                              self._memobj.settings2.beep))
519
        basic.append(rs)
520

    
521
        rs = RadioSetting("settings2.batterysaver", "Battery saver",
522
                          RadioSettingValueBoolean(
523
                              self._memobj.settings2.batterysaver))
524
        basic.append(rs)
525

    
526
        rs = RadioSetting("settings2.squelchlevel", "Squelch level",
527
                          RadioSettingValueInteger(
528
                              0, 9, self._memobj.settings2.squelchlevel))
529
        basic.append(rs)
530

    
531
        if self._has_sidekey:
532
            rs = RadioSetting("settings2.sidekeyfunction", "Side key function",
533
                              RadioSettingValueList(
534
                                  self.SIDEKEYFUNCTION_LIST,
535
                                  self.SIDEKEYFUNCTION_LIST[
536
                                      self._memobj.settings2.sidekeyfunction]))
537
            basic.append(rs)
538

    
539
        rs = RadioSetting("settings2.timeouttimer", "Timeout timer",
540
                          RadioSettingValueList(
541
                              TIMEOUTTIMER_LIST,
542
                              TIMEOUTTIMER_LIST[
543
                                  self._memobj.settings2.timeouttimer]))
544
        basic.append(rs)
545

    
546
        return top
547

    
548
    def set_settings(self, settings):
549
        for element in settings:
550
            if not isinstance(element, RadioSetting):
551
                self.set_settings(element)
552
                continue
553
            else:
554
                try:
555
                    if "." in element.get_name():
556
                        bits = element.get_name().split(".")
557
                        obj = self._memobj
558
                        for bit in bits[:-1]:
559
                            obj = getattr(obj, bit)
560
                        setting = bits[-1]
561
                    else:
562
                        obj = self._memobj.settings
563
                        setting = element.get_name()
564

    
565
                    if element.has_apply_callback():
566
                        LOG.debug("Using apply callback")
567
                        element.run_apply_callback()
568
                    elif setting == "voxlevel":
569
                        setattr(obj, setting, int(element.value) - 1)
570
                    else:
571
                        LOG.debug("Setting %s = %s" % (setting, element.value))
572
                        setattr(obj, setting, element.value)
573
                except Exception, e:
574
                    LOG.debug(element.get_name())
575
                    raise
576

    
577

    
578
class H777TestCase(unittest.TestCase):
579

    
580
    def setUp(self):
581
        self.driver = H777Radio(None)
582
        self.testdata = bitwise.parse("lbcd foo[2];",
583
                                      memmap.MemoryMap("\x00\x00"))
584

    
585
    def test_decode_tone_dtcs_normal(self):
586
        mode, value, pol = self.driver._decode_tone(8023)
587
        self.assertEqual('DTCS', mode)
588
        self.assertEqual(23, value)
589
        self.assertEqual('N', pol)
590

    
591
    def test_decode_tone_dtcs_rev(self):
592
        mode, value, pol = self.driver._decode_tone(12023)
593
        self.assertEqual('DTCS', mode)
594
        self.assertEqual(23, value)
595
        self.assertEqual('R', pol)
596

    
597
    def test_decode_tone_tone(self):
598
        mode, value, pol = self.driver._decode_tone(885)
599
        self.assertEqual('Tone', mode)
600
        self.assertEqual(88.5, value)
601
        self.assertEqual(None, pol)
602

    
603
    def test_decode_tone_none(self):
604
        mode, value, pol = self.driver._decode_tone(16665)
605
        self.assertEqual('', mode)
606
        self.assertEqual(None, value)
607
        self.assertEqual(None, pol)
608

    
609
    def test_encode_tone_dtcs_normal(self):
610
        self.driver._encode_tone(self.testdata.foo, 'DTCS', 23, 'N')
611
        self.assertEqual(8023, int(self.testdata.foo))
612

    
613
    def test_encode_tone_dtcs_rev(self):
614
        self.driver._encode_tone(self.testdata.foo, 'DTCS', 23, 'R')
615
        self.assertEqual(12023, int(self.testdata.foo))
616

    
617
    def test_encode_tone(self):
618
        self.driver._encode_tone(self.testdata.foo, 'Tone', 88.5, 'N')
619
        self.assertEqual(885, int(self.testdata.foo))
620

    
621
    def test_encode_tone_none(self):
622
        self.driver._encode_tone(self.testdata.foo, '', 67.0, 'N')
623
        self.assertEqual(16665, int(self.testdata.foo))
624

    
625

    
626
@directory.register
627
class ROGA2SRadio(H777Radio):
628
    VENDOR = "Radioddity"
629
    MODEL = "GA-2S"
630
    _has_fm = False
631
    SIDEKEYFUNCTION_LIST = ["Off", "Monitor", "Unused", "Alarm"]
632

    
633
    @classmethod
634
    def match_model(cls, filedata, filename):
635
        # This model is only ever matched via metadata
636
        return False
(9-9/9)