Project

General

Profile

Bug #1193 » h777.py

Source with the added prints. - Daniel Hjort, 04/12/2014 09:07 AM

 
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

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

    
28
DEBUG = os.getenv("CHIRP_DEBUG") and True or False
29

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

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

    
80
# TODO: Is it 1 watt?
81
H777_POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=1.00),
82
                     chirp_common.PowerLevel("High", watts=5.00)]
83
VOICE_LIST = ["English", "Chinese"]
84
SIDEKEYFUNCTION_LIST = ["Off", "Monitor", "Transmit Power", "Alarm"]
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
def _h777_enter_programming_mode(radio):
96
    serial = radio.pipe
97

    
98
    try:
99
        serial.write("\x02")
100
        time.sleep(0.1)
101
        serial.write("PROGRAM")
102
        ack = serial.read(1)
103
    except:
104
        raise errors.RadioError("Error communicating with radio")
105

    
106
    if not ack:
107
        raise errors.RadioError("No response from radio")
108
    elif ack != CMD_ACK:
109
        ack = serial.read(1)
110

    
111
    if ack != CMD_ACK:
112
        raise errors.RadioError("Radio refused to enter programming mode")
113

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

    
120
    if not ident.startswith("P3107"):
121
        print util.hexprint(ident)
122
        raise errors.RadioError("Radio returned unknown identification string")
123

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

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

    
133
def _h777_exit_programming_mode(radio):
134
    
135
    print("Begin _h777_exit_programming_mode")
136
    serial = radio.pipe
137
    print("After serial = radio.pipe")
138
    try:
139
        serial.write("E")
140
        print("After serial.write(\"E\")")
141
    except:
142
        raise errors.RadioError("Radio refused to exit programming mode")
143
    print("End _h777_exit_programming_mode")
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
    if DEBUG:
151
        print("Reading block %04x..." % (block_addr))
152

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

    
159
        block_data = response[4:]
160

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

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

    
169
    return block_data
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
    if DEBUG:
178
        print("Writing Data:")
179
        print util.hexprint(cmd + data)
180

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

    
189
def do_download(radio):
190
    print "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
        if DEBUG:
209
            print "Address: %04x" % addr
210
            print util.hexprint(block)
211

    
212
    _h777_exit_programming_mode(radio)
213

    
214
    return memmap.MemoryMap(data)
215

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

    
220
    print("Before calling _h777_enter_programming_mode")
221
    _h777_enter_programming_mode(radio)
222
    print("After calling _h777_enter_programming_mode")
223

    
224
    status.cur = 0
225
    status.max = radio._memsize
226

    
227
    for start_addr, end_addr in radio._ranges:
228
        for addr in range(start_addr, end_addr, BLOCK_SIZE):
229
            status.cur = addr + BLOCK_SIZE
230
            radio.status_fn(status)
231
            print("Before calling _h777_write_block")
232
            _h777_write_block(radio, addr, BLOCK_SIZE)
233
            print("After calling _h777_write_block")
234

    
235
    print("Before calling _h777_exit_programming_mode")
236
    _h777_exit_programming_mode(radio)
237
    print("After calling _h777_exit_programming_mode")
238

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

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

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

    
265
    def get_features(self):
266
        rf = chirp_common.RadioFeatures()
267
        rf.has_settings = True
268
        rf.valid_modes = ["NFM", "FM"]  # 12.5 KHz, 25 kHz.
269
        rf.valid_skips = ["", "S"]
270
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
271
        rf.has_rx_dtcs = True
272
        rf.has_ctone = True
273
        rf.has_cross = True
274
        rf.has_tuning_step = False
275
        rf.has_bank = False
276
        rf.has_name = False
277
        rf.memory_bounds = (1, 16)
278
        rf.valid_bands = [(400000000, 470000000)]
279
        rf.valid_power_levels = H777_POWER_LEVELS
280

    
281
        return rf
282

    
283
    def process_mmap(self):
284
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
285

    
286
    def sync_in(self):
287
        self._mmap = do_download(self)
288
        self.process_mmap()
289

    
290
    def sync_out(self):
291
        do_upload(self)
292

    
293
    def get_raw_memory(self, number):
294
        return repr(self._memobj.memory[number - 1])
295

    
296
    def _decode_tone(self, val):
297
        val = int(val)
298
        if val == 16665:
299
            return '', None, None
300
        elif val >= 12000:
301
            return 'DTCS', val - 12000, 'R'
302
        elif val >= 8000:
303
            return 'DTCS', val - 8000, 'N'
304
        else:
305
            return 'Tone', val / 10.0, None
306

    
307
    def _encode_tone(self, memval, mode, value, pol):
308
        if mode == '':
309
            memval[0].set_raw(0xFF)
310
            memval[1].set_raw(0xFF)
311
        elif mode == 'Tone':
312
            memval.set_value(int(value * 10))
313
        elif mode == 'DTCS':
314
            flag = 0x80 if pol == 'N' else 0xC0
315
            memval.set_value(value)
316
            memval[1].set_bits(flag)
317
        else:
318
            raise Exception("Internal error: invalid mode `%s'" % mode)
319

    
320
    def get_memory(self, number):
321
        _mem = self._memobj.memory[number - 1]
322

    
323
        mem = chirp_common.Memory()
324

    
325
        mem.number = number
326
        mem.freq = int(_mem.rxfreq) * 10
327

    
328
        # We'll consider any blank (i.e. 0MHz frequency) to be empty
329
        if mem.freq == 0:
330
            mem.empty = True
331
            return mem
332

    
333
        if _mem.rxfreq.get_raw() == "\xFF\xFF\xFF\xFF":
334
            mem.freq = 0
335
            mem.empty = True
336
            return mem
337

    
338
        if int(_mem.rxfreq) == int(_mem.txfreq):
339
            mem.duplex = ""
340
            mem.offset = 0
341
        else:
342
            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
343
            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
344

    
345
        mem.mode = not _mem.narrow and "FM" or "NFM"
346
        mem.power = H777_POWER_LEVELS[_mem.highpower]
347

    
348
        mem.skip = _mem.skip and "S" or ""
349

    
350
        txtone = self._decode_tone(_mem.txtone)
351
        rxtone = self._decode_tone(_mem.rxtone)
352
        chirp_common.split_tone_decode(mem, txtone, rxtone)
353

    
354
        mem.extra = RadioSettingGroup("Extra", "extra")
355
        rs = RadioSetting("bcl", "Busy Channel Lockout",
356
                          RadioSettingValueBoolean(not _mem.bcl))
357
        mem.extra.append(rs)
358
        rs = RadioSetting("beatshift", "Beat Shift(scramble)",
359
                          RadioSettingValueBoolean(not _mem.beatshift))
360
        mem.extra.append(rs)
361

    
362
        return mem
363

    
364
    def set_memory(self, mem):
365
        # Get a low-level memory object mapped to the image
366
        _mem = self._memobj.memory[mem.number - 1]
367

    
368
        if mem.empty:
369
            _mem.set_raw("\xFF" * (_mem.size() / 8))
370
            return
371

    
372
        _mem.rxfreq = mem.freq / 10
373

    
374
        if mem.duplex == "off":
375
            for i in range(0, 4):
376
                _mem.txfreq[i].set_raw("\xFF")
377
        elif mem.duplex == "split":
378
            _mem.txfreq = mem.offset / 10
379
        elif mem.duplex == "+":
380
            _mem.txfreq = (mem.freq + mem.offset) / 10
381
        elif mem.duplex == "-":
382
            _mem.txfreq = (mem.freq - mem.offset) / 10
383
        else:
384
            _mem.txfreq = mem.freq / 10
385

    
386
        txtone, rxtone = chirp_common.split_tone_encode(mem)
387
        self._encode_tone(_mem.txtone, *txtone)
388
        self._encode_tone(_mem.rxtone, *rxtone)
389

    
390
        _mem.narrow = 'N' in mem.mode
391
        _mem.highpower = mem.power == H777_POWER_LEVELS[1]
392
        _mem.skip = mem.skip == "S"
393

    
394
        for setting in mem.extra:
395
            # NOTE: Only two settings right now, both are inverted
396
            setattr(_mem, setting.get_name(), not setting.value)
397

    
398
    def get_settings(self):
399
        _settings = self._memobj.settings
400
        basic = RadioSettingGroup("basic", "Basic Settings")
401

    
402
        # TODO: Check that all these settings actually do what they
403
        # say they do.
404

    
405
        rs = RadioSetting("voiceprompt", "Voice prompt",
406
                          RadioSettingValueBoolean(_settings.voiceprompt))
407
        basic.append(rs)
408

    
409
        rs = RadioSetting("voicelanguage", "Voice language",
410
                          RadioSettingValueList(VOICE_LIST,
411
                              VOICE_LIST[_settings.voicelanguage]))
412
        basic.append(rs)
413

    
414
        rs = RadioSetting("scan", "Scan",
415
                          RadioSettingValueBoolean(_settings.scan))
416
        basic.append(rs)
417

    
418
        rs = RadioSetting("settings2.scanmode", "Scan mode",
419
                          RadioSettingValueList(SCANMODE_LIST,
420
                          SCANMODE_LIST[self._memobj.settings2.scanmode]))
421
        basic.append(rs)
422

    
423
        rs = RadioSetting("vox", "VOX",
424
                          RadioSettingValueBoolean(_settings.vox))
425
        basic.append(rs)
426

    
427
        rs = RadioSetting("voxlevel", "VOX level",
428
                          RadioSettingValueInteger(
429
                              1, 5, _settings.voxlevel + 1))
430
        basic.append(rs)
431

    
432
        rs = RadioSetting("voxinhibitonrx", "Inhibit VOX on receive",
433
                          RadioSettingValueBoolean(_settings.voxinhibitonrx))
434
        basic.append(rs)
435

    
436
        rs = RadioSetting("lowvolinhibittx", "Low voltage inhibit transmit",
437
                          RadioSettingValueBoolean(_settings.lowvolinhibittx))
438
        basic.append(rs)
439

    
440
        rs = RadioSetting("highvolinhibittx", "High voltage inhibit transmit",
441
                          RadioSettingValueBoolean(_settings.highvolinhibittx))
442
        basic.append(rs)
443

    
444
        rs = RadioSetting("alarm", "Alarm",
445
                          RadioSettingValueBoolean(_settings.alarm))
446
        basic.append(rs)
447

    
448
        # TODO: This should probably be called “FM Broadcast Band Radio”
449
        # or something. I'm not sure if the model actually has one though.
450
        rs = RadioSetting("fmradio", "FM function",
451
                          RadioSettingValueBoolean(_settings.fmradio))
452
        basic.append(rs)
453

    
454
        rs = RadioSetting("settings2.beep", "Beep",
455
                          RadioSettingValueBoolean(
456
                              self._memobj.settings2.beep))
457
        basic.append(rs)
458

    
459
        rs = RadioSetting("settings2.batterysaver", "Battery saver",
460
                          RadioSettingValueBoolean(
461
                              self._memobj.settings2.batterysaver))
462
        basic.append(rs)
463

    
464
        rs = RadioSetting("settings2.squelchlevel", "Squelch level",
465
                          RadioSettingValueInteger(0, 9,
466
                              self._memobj.settings2.squelchlevel))
467
        basic.append(rs)
468

    
469
        rs = RadioSetting("settings2.sidekeyfunction", "Side key function",
470
                          RadioSettingValueList(SIDEKEYFUNCTION_LIST,
471
                          SIDEKEYFUNCTION_LIST[
472
                              self._memobj.settings2.sidekeyfunction]))
473
        basic.append(rs)
474

    
475
        rs = RadioSetting("settings2.timeouttimer", "Timeout timer",
476
                          RadioSettingValueList(TIMEOUTTIMER_LIST,
477
                          TIMEOUTTIMER_LIST[
478
                              self._memobj.settings2.timeouttimer]))
479
        basic.append(rs)
480

    
481
        return basic
482

    
483
    def set_settings(self, settings):
484
        for element in settings:
485
            if not isinstance(element, RadioSetting):
486
                self.set_settings(element)
487
                continue
488
            else:
489
                try:
490
                    if "." in element.get_name():
491
                        bits = element.get_name().split(".")
492
                        obj = self._memobj
493
                        for bit in bits[:-1]:
494
                            obj = getattr(obj, bit)
495
                        setting = bits[-1]
496
                    else:
497
                        obj = self._memobj.settings
498
                        setting = element.get_name()
499

    
500
                    if element.has_apply_callback():
501
                        print "Using apply callback"
502
                        element.run_apply_callback()
503
                    elif setting == "voxlevel":
504
                        setattr(obj, setting, int(element.value) - 1)
505
                    else:
506
                        print "Setting %s = %s" % (setting, element.value)
507
                        setattr(obj, setting, element.value)
508
                except Exception, e:
509
                    print element.get_name()
510
                    raise
511

    
512
class H777TestCase(unittest.TestCase):
513
    def setUp(self):
514
        self.driver = H777Radio(None)
515
        self.testdata = bitwise.parse("lbcd foo[2];",
516
                                      memmap.MemoryMap("\x00\x00"))
517

    
518
    def test_decode_tone_dtcs_normal(self):
519
        mode, value, pol = self.driver._decode_tone(8023)
520
        self.assertEqual('DTCS', mode)
521
        self.assertEqual(23, value)
522
        self.assertEqual('N', pol)
523

    
524
    def test_decode_tone_dtcs_rev(self):
525
        mode, value, pol = self.driver._decode_tone(12023)
526
        self.assertEqual('DTCS', mode)
527
        self.assertEqual(23, value)
528
        self.assertEqual('R', pol)
529

    
530
    def test_decode_tone_tone(self):
531
        mode, value, pol = self.driver._decode_tone(885)
532
        self.assertEqual('Tone', mode)
533
        self.assertEqual(88.5, value)
534
        self.assertEqual(None, pol)
535

    
536
    def test_decode_tone_none(self):
537
        mode, value, pol = self.driver._decode_tone(16665)
538
        self.assertEqual('', mode)
539
        self.assertEqual(None, value)
540
        self.assertEqual(None, pol)
541

    
542
    def test_encode_tone_dtcs_normal(self):
543
        self.driver._encode_tone(self.testdata.foo, 'DTCS', 23, 'N')
544
        self.assertEqual(8023, int(self.testdata.foo))
545

    
546
    def test_encode_tone_dtcs_rev(self):
547
        self.driver._encode_tone(self.testdata.foo, 'DTCS', 23, 'R')
548
        self.assertEqual(12023, int(self.testdata.foo))
549

    
550
    def test_encode_tone(self):
551
        self.driver._encode_tone(self.testdata.foo, 'Tone', 88.5, 'N')
552
        self.assertEqual(885, int(self.testdata.foo))
553

    
554
    def test_encode_tone_none(self):
555
        self.driver._encode_tone(self.testdata.foo, '', 67.0, 'N')
556
        self.assertEqual(16665, int(self.testdata.foo))
(2-2/2)