Project

General

Profile

Bug #4249 » h777.py

Jim Unroe, 11/22/2016 01:53 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
SIDEKEYFUNCTION_LIST = ["Off", "Monitor", "Transmit Power", "Alarm"]
86
TIMEOUTTIMER_LIST = ["Off", "30 seconds", "60 seconds", "90 seconds",
87
                     "120 seconds", "150 seconds", "180 seconds",
88
                     "210 seconds", "240 seconds", "270 seconds",
89
                     "300 seconds"]
90
SCANMODE_LIST = ["Carrier", "Time"]
91

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

    
96

    
97
def _h777_enter_programming_mode(radio):
98
    serial = radio.pipe
99

    
100
    magic = "PROGRAM"
101
    try:
102
        serial.write("\x02")
103
        time.sleep(0.1)
104
        for j in range(0, len(magic)):
105
            time.sleep(0.005)
106
            serial.write(magic[j])
107
        ack = serial.read(1)
108
    except:
109
        raise errors.RadioError("Error communicating with radio")
110

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

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

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

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

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

    
135

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

    
143

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

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

    
151
    try:
152
        for j in range(0, len(cmd)):
153
            time.sleep(0.005)
154
            serial.write(cmd[j])
155

    
156
        response = serial.read(4 + BLOCK_SIZE)
157
        if response[:4] != expectedresponse:
158
            raise Exception("Error reading block %04x." % (block_addr))
159

    
160
        block_data = response[4:]
161

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

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

    
170
    return block_data
171

    
172

    
173
def _h777_write_block(radio, block_addr, block_size):
174
    serial = radio.pipe
175

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

    
179
    LOG.debug("Writing Data:")
180
    LOG.debug(util.hexprint(cmd + data))
181

    
182
    try:
183
        for j in range(0, len(cmd)):
184
            time.sleep(0.005)
185
            serial.write(cmd[j])
186
        for j in range(0, len(data)):
187
            time.sleep(0.005)
188
            serial.write(data[j])
189
        if serial.read(1) != CMD_ACK:
190
            raise Exception("No ACK")
191
    except:
192
        raise errors.RadioError("Failed to send block "
193
                                "to radio at %04x" % block_addr)
194

    
195

    
196
def do_download(radio):
197
    LOG.debug("download")
198
    _h777_enter_programming_mode(radio)
199

    
200
    data = ""
201

    
202
    status = chirp_common.Status()
203
    status.msg = "Cloning from radio"
204

    
205
    status.cur = 0
206
    status.max = radio._memsize
207

    
208
    for addr in range(0, radio._memsize, BLOCK_SIZE):
209
        status.cur = addr + BLOCK_SIZE
210
        radio.status_fn(status)
211

    
212
        block = _h777_read_block(radio, addr, BLOCK_SIZE)
213
        data += block
214

    
215
        LOG.debug("Address: %04x" % addr)
216
        LOG.debug(util.hexprint(block))
217

    
218
    _h777_exit_programming_mode(radio)
219

    
220
    return memmap.MemoryMap(data)
221

    
222

    
223
def do_upload(radio):
224
    status = chirp_common.Status()
225
    status.msg = "Uploading to radio"
226

    
227
    _h777_enter_programming_mode(radio)
228

    
229
    status.cur = 0
230
    status.max = radio._memsize
231

    
232
    for start_addr, end_addr in radio._ranges:
233
        for addr in range(start_addr, end_addr, BLOCK_SIZE):
234
            status.cur = addr + BLOCK_SIZE
235
            radio.status_fn(status)
236
            _h777_write_block(radio, addr, BLOCK_SIZE)
237

    
238
    _h777_exit_programming_mode(radio)
239

    
240

    
241
@directory.register
242
class H777Radio(chirp_common.CloneModeRadio):
243
    """HST H-777"""
244
    # VENDOR = "Heng Shun Tong (恒顺通)"
245
    # MODEL = "H-777"
246
    VENDOR = "Baofeng"
247
    MODEL = "BF-888"
248
    BAUD_RATE = 9600
249

    
250
    # This code currently requires that ranges start at 0x0000
251
    # and are continious. In the original program 0x0388 and 0x03C8
252
    # are only written (all bytes 0xFF), not read.
253
    # _ranges = [
254
    #       (0x0000, 0x0110),
255
    #       (0x02B0, 0x02C0),
256
    #       (0x0380, 0x03E0)
257
    #       ]
258
    # Memory starts looping at 0x1000... But not every 0x1000.
259

    
260
    _ranges = [
261
        (0x0000, 0x0110),
262
        (0x02B0, 0x02C0),
263
        (0x0380, 0x03E0),
264
    ]
265
    _memsize = 0x03E0
266

    
267
    def get_features(self):
268
        rf = chirp_common.RadioFeatures()
269
        rf.has_settings = True
270
        rf.valid_modes = ["NFM", "FM"]  # 12.5 KHz, 25 kHz.
271
        rf.valid_skips = ["", "S"]
272
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
273
        rf.valid_duplexes = ["", "-", "+", "split", "off"]
274
        rf.can_odd_split = True
275
        rf.has_rx_dtcs = True
276
        rf.has_ctone = True
277
        rf.has_cross = True
278
        rf.valid_cross_modes = [
279
            "Tone->Tone",
280
            "DTCS->",
281
            "->DTCS",
282
            "Tone->DTCS",
283
            "DTCS->Tone",
284
            "->Tone",
285
            "DTCS->DTCS"]
286
        rf.has_tuning_step = False
287
        rf.has_bank = False
288
        rf.has_name = False
289
        rf.memory_bounds = (1, 16)
290
        rf.valid_bands = [(400000000, 470000000)]
291
        rf.valid_power_levels = H777_POWER_LEVELS
292

    
293
        return rf
294

    
295
    def process_mmap(self):
296
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
297

    
298
    def sync_in(self):
299
        self._mmap = do_download(self)
300
        self.process_mmap()
301

    
302
    def sync_out(self):
303
        do_upload(self)
304

    
305
    def get_raw_memory(self, number):
306
        return repr(self._memobj.memory[number - 1])
307

    
308
    def _decode_tone(self, val):
309
        val = int(val)
310
        if val == 16665:
311
            return '', None, None
312
        elif val >= 12000:
313
            return 'DTCS', val - 12000, 'R'
314
        elif val >= 8000:
315
            return 'DTCS', val - 8000, 'N'
316
        else:
317
            return 'Tone', val / 10.0, None
318

    
319
    def _encode_tone(self, memval, mode, value, pol):
320
        if mode == '':
321
            memval[0].set_raw(0xFF)
322
            memval[1].set_raw(0xFF)
323
        elif mode == 'Tone':
324
            memval.set_value(int(value * 10))
325
        elif mode == 'DTCS':
326
            flag = 0x80 if pol == 'N' else 0xC0
327
            memval.set_value(value)
328
            memval[1].set_bits(flag)
329
        else:
330
            raise Exception("Internal error: invalid mode `%s'" % mode)
331

    
332
    def get_memory(self, number):
333
        _mem = self._memobj.memory[number - 1]
334

    
335
        mem = chirp_common.Memory()
336

    
337
        mem.number = number
338
        mem.freq = int(_mem.rxfreq) * 10
339

    
340
        # We'll consider any blank (i.e. 0MHz frequency) to be empty
341
        if mem.freq == 0:
342
            mem.empty = True
343
            return mem
344

    
345
        if _mem.rxfreq.get_raw() == "\xFF\xFF\xFF\xFF":
346
            mem.freq = 0
347
            mem.empty = True
348
            return mem
349

    
350
        if _mem.txfreq.get_raw() == "\xFF\xFF\xFF\xFF":
351
            mem.duplex = "off"
352
            mem.offset = 0
353
        elif int(_mem.rxfreq) == int(_mem.txfreq):
354
            mem.duplex = ""
355
            mem.offset = 0
356
        else:
357
            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
358
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
359

    
360
        mem.mode = not _mem.narrow and "FM" or "NFM"
361
        mem.power = H777_POWER_LEVELS[_mem.highpower]
362

    
363
        mem.skip = _mem.skip and "S" or ""
364

    
365
        txtone = self._decode_tone(_mem.txtone)
366
        rxtone = self._decode_tone(_mem.rxtone)
367
        chirp_common.split_tone_decode(mem, txtone, rxtone)
368

    
369
        mem.extra = RadioSettingGroup("Extra", "extra")
370
        rs = RadioSetting("bcl", "Busy Channel Lockout",
371
                          RadioSettingValueBoolean(not _mem.bcl))
372
        mem.extra.append(rs)
373
        rs = RadioSetting("beatshift", "Beat Shift(scramble)",
374
                          RadioSettingValueBoolean(not _mem.beatshift))
375
        mem.extra.append(rs)
376

    
377
        return mem
378

    
379
    def set_memory(self, mem):
380
        # Get a low-level memory object mapped to the image
381
        _mem = self._memobj.memory[mem.number - 1]
382

    
383
        if mem.empty:
384
            _mem.set_raw("\xFF" * (_mem.size() / 8))
385
            return
386

    
387
        _mem.rxfreq = mem.freq / 10
388

    
389
        if mem.duplex == "off":
390
            for i in range(0, 4):
391
                _mem.txfreq[i].set_raw("\xFF")
392
        elif mem.duplex == "split":
393
            _mem.txfreq = mem.offset / 10
394
        elif mem.duplex == "+":
395
            _mem.txfreq = (mem.freq + mem.offset) / 10
396
        elif mem.duplex == "-":
397
            _mem.txfreq = (mem.freq - mem.offset) / 10
398
        else:
399
            _mem.txfreq = mem.freq / 10
400

    
401
        txtone, rxtone = chirp_common.split_tone_encode(mem)
402
        self._encode_tone(_mem.txtone, *txtone)
403
        self._encode_tone(_mem.rxtone, *rxtone)
404

    
405
        _mem.narrow = 'N' in mem.mode
406
        _mem.highpower = mem.power == H777_POWER_LEVELS[1]
407
        _mem.skip = mem.skip == "S"
408

    
409
        for setting in mem.extra:
410
            # NOTE: Only two settings right now, both are inverted
411
            setattr(_mem, setting.get_name(), not int(setting.value))
412

    
413
        # When set to one, official programming software (BF-480) shows always
414
        # "WFM", even if we choose "NFM". Therefore, for compatibility
415
        # purposes, we will set these to zero.
416
        _mem.unknown1 = 0
417
        _mem.unknown2 = 0
418
        _mem.unknown3 = 0
419

    
420
    def get_settings(self):
421
        _settings = self._memobj.settings
422
        basic = RadioSettingGroup("basic", "Basic Settings")
423
        top = RadioSettings(basic)
424

    
425
        # TODO: Check that all these settings actually do what they
426
        # say they do.
427

    
428
        rs = RadioSetting("voiceprompt", "Voice prompt",
429
                          RadioSettingValueBoolean(_settings.voiceprompt))
430
        basic.append(rs)
431

    
432
        rs = RadioSetting("voicelanguage", "Voice language",
433
                          RadioSettingValueList(
434
                              VOICE_LIST,
435
                              VOICE_LIST[_settings.voicelanguage]))
436
        basic.append(rs)
437

    
438
        rs = RadioSetting("scan", "Scan",
439
                          RadioSettingValueBoolean(_settings.scan))
440
        basic.append(rs)
441

    
442
        rs = RadioSetting("settings2.scanmode", "Scan mode",
443
                          RadioSettingValueList(
444
                              SCANMODE_LIST,
445
                              SCANMODE_LIST[self._memobj.settings2.scanmode]))
446
        basic.append(rs)
447

    
448
        rs = RadioSetting("vox", "VOX",
449
                          RadioSettingValueBoolean(_settings.vox))
450
        basic.append(rs)
451

    
452
        rs = RadioSetting("voxlevel", "VOX level",
453
                          RadioSettingValueInteger(
454
                              1, 5, _settings.voxlevel + 1))
455
        basic.append(rs)
456

    
457
        rs = RadioSetting("voxinhibitonrx", "Inhibit VOX on receive",
458
                          RadioSettingValueBoolean(_settings.voxinhibitonrx))
459
        basic.append(rs)
460

    
461
        rs = RadioSetting("lowvolinhibittx", "Low voltage inhibit transmit",
462
                          RadioSettingValueBoolean(_settings.lowvolinhibittx))
463
        basic.append(rs)
464

    
465
        rs = RadioSetting("highvolinhibittx", "High voltage inhibit transmit",
466
                          RadioSettingValueBoolean(_settings.highvolinhibittx))
467
        basic.append(rs)
468

    
469
        rs = RadioSetting("alarm", "Alarm",
470
                          RadioSettingValueBoolean(_settings.alarm))
471
        basic.append(rs)
472

    
473
        # TODO: This should probably be called “FM Broadcast Band Radio”
474
        # or something. I'm not sure if the model actually has one though.
475
        rs = RadioSetting("fmradio", "FM function",
476
                          RadioSettingValueBoolean(_settings.fmradio))
477
        basic.append(rs)
478

    
479
        rs = RadioSetting("settings2.beep", "Beep",
480
                          RadioSettingValueBoolean(
481
                              self._memobj.settings2.beep))
482
        basic.append(rs)
483

    
484
        rs = RadioSetting("settings2.batterysaver", "Battery saver",
485
                          RadioSettingValueBoolean(
486
                              self._memobj.settings2.batterysaver))
487
        basic.append(rs)
488

    
489
        rs = RadioSetting("settings2.squelchlevel", "Squelch level",
490
                          RadioSettingValueInteger(
491
                              0, 9, self._memobj.settings2.squelchlevel))
492
        basic.append(rs)
493

    
494
        rs = RadioSetting("settings2.sidekeyfunction", "Side key function",
495
                          RadioSettingValueList(
496
                              SIDEKEYFUNCTION_LIST,
497
                              SIDEKEYFUNCTION_LIST[
498
                                  self._memobj.settings2.sidekeyfunction]))
499
        basic.append(rs)
500

    
501
        rs = RadioSetting("settings2.timeouttimer", "Timeout timer",
502
                          RadioSettingValueList(
503
                              TIMEOUTTIMER_LIST,
504
                              TIMEOUTTIMER_LIST[
505
                                  self._memobj.settings2.timeouttimer]))
506
        basic.append(rs)
507

    
508
        return top
509

    
510
    def set_settings(self, settings):
511
        for element in settings:
512
            if not isinstance(element, RadioSetting):
513
                self.set_settings(element)
514
                continue
515
            else:
516
                try:
517
                    if "." in element.get_name():
518
                        bits = element.get_name().split(".")
519
                        obj = self._memobj
520
                        for bit in bits[:-1]:
521
                            obj = getattr(obj, bit)
522
                        setting = bits[-1]
523
                    else:
524
                        obj = self._memobj.settings
525
                        setting = element.get_name()
526

    
527
                    if element.has_apply_callback():
528
                        LOG.debug("Using apply callback")
529
                        element.run_apply_callback()
530
                    elif setting == "voxlevel":
531
                        setattr(obj, setting, int(element.value) - 1)
532
                    else:
533
                        LOG.debug("Setting %s = %s" % (setting, element.value))
534
                        setattr(obj, setting, element.value)
535
                except Exception, e:
536
                    LOG.debug(element.get_name())
537
                    raise
538

    
539

    
540
class H777TestCase(unittest.TestCase):
541

    
542
    def setUp(self):
543
        self.driver = H777Radio(None)
544
        self.testdata = bitwise.parse("lbcd foo[2];",
545
                                      memmap.MemoryMap("\x00\x00"))
546

    
547
    def test_decode_tone_dtcs_normal(self):
548
        mode, value, pol = self.driver._decode_tone(8023)
549
        self.assertEqual('DTCS', mode)
550
        self.assertEqual(23, value)
551
        self.assertEqual('N', pol)
552

    
553
    def test_decode_tone_dtcs_rev(self):
554
        mode, value, pol = self.driver._decode_tone(12023)
555
        self.assertEqual('DTCS', mode)
556
        self.assertEqual(23, value)
557
        self.assertEqual('R', pol)
558

    
559
    def test_decode_tone_tone(self):
560
        mode, value, pol = self.driver._decode_tone(885)
561
        self.assertEqual('Tone', mode)
562
        self.assertEqual(88.5, value)
563
        self.assertEqual(None, pol)
564

    
565
    def test_decode_tone_none(self):
566
        mode, value, pol = self.driver._decode_tone(16665)
567
        self.assertEqual('', mode)
568
        self.assertEqual(None, value)
569
        self.assertEqual(None, pol)
570

    
571
    def test_encode_tone_dtcs_normal(self):
572
        self.driver._encode_tone(self.testdata.foo, 'DTCS', 23, 'N')
573
        self.assertEqual(8023, int(self.testdata.foo))
574

    
575
    def test_encode_tone_dtcs_rev(self):
576
        self.driver._encode_tone(self.testdata.foo, 'DTCS', 23, 'R')
577
        self.assertEqual(12023, int(self.testdata.foo))
578

    
579
    def test_encode_tone(self):
580
        self.driver._encode_tone(self.testdata.foo, 'Tone', 88.5, 'N')
581
        self.assertEqual(885, int(self.testdata.foo))
582

    
583
    def test_encode_tone_none(self):
584
        self.driver._encode_tone(self.testdata.foo, '', 67.0, 'N')
585
        self.assertEqual(16665, int(self.testdata.foo))
(2-2/9)