Project

General

Profile

Bug #9810 » th350(steps added).py

Jim Unroe, 04/01/2022 11:07 PM

 
1
# Copyright 2019 Zhaofeng Li <hello@zhaofeng.li>
2
# Copyright 2013 Dan Smith <dsmith@danplanet.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 struct
18
import logging
19
from math import floor
20
from chirp import chirp_common, directory, bitwise, memmap, errors, util
21
from chirp.drivers.uvb5 import BaofengUVB5
22

    
23
LOG = logging.getLogger(__name__)
24

    
25
mem_format = """
26
struct memory {
27
  lbcd freq[4];
28
  lbcd offset[4];
29
  u8 unknown1:2,
30
     txpol:1,
31
     rxpol:1,
32
     compander:1,
33
     scrambler:1,
34
     unknown2:2;
35
  u8 rxtoneb;
36
  u8 rxtonea;
37
  u8 txtoneb;
38
  u8 txtonea;
39
  u8 pttid:1,
40
     scanadd:1,
41
     isnarrow:1,
42
     bcl:1,
43
     highpower:1,
44
     revfreq:1,
45
     duplex:2;
46
  u8 unknown[2];
47
};
48

    
49
#seekto 0x0000;
50
char ident[32];
51
u8 blank[16];
52
struct memory vfo1;
53
struct memory channels[128];
54
#seekto 0x0840;
55
struct memory vfo3;
56
struct memory vfo2;
57

    
58
#seekto 0x09D0;
59
u16 fm_presets[16];
60

    
61
#seekto 0x0A30;
62
struct {
63
  u8 name[5];
64
} names[128];
65

    
66
#seekto 0x0D30;
67
struct {
68
  u8 squelch;
69
  u8 freqmode_ab:1,
70
     save_funct:1,
71
     backlight:1,
72
     beep_tone_disabled:1,
73
     roger:1,
74
     tdr:1,
75
     scantype:2;
76
  u8 language:1,
77
     workmode_b:1,
78
     workmode_a:1,
79
     workmode_fm:1,
80
     voice_prompt:1,
81
     fm:1,
82
     pttid:2;
83
  u8 unknown_0:5,
84
     timeout:3;
85
  u8 mdf_b:2,
86
     mdf_a:2,
87
     unknown_1:2,
88
     txtdr:2;
89
  u8 unknown_2:4,
90
     ste_disabled:1,
91
     unknown_3:2,
92
     sidetone:1;
93
  u8 vox;
94
  u8 unk1;
95
  u8 mem_chan_a;
96
  u16 fm_vfo;
97
  u8 unk4;
98
  u8 unk5;
99
  u8 mem_chan_b;
100
  u8 unk6;
101
  u8 last_menu; // number of last menu item accessed
102
} settings;
103

    
104
#seekto 0x0D50;
105
struct {
106
  u8 code[6];
107
} pttid;
108

    
109
#seekto 0x0F30;
110
struct {
111
  lbcd lower_vhf[2];
112
  lbcd upper_vhf[2];
113
  lbcd lower_uhf[2];
114
  lbcd upper_uhf[2];
115
} limits;
116

    
117
#seekto 0x0FF0;
118
struct {
119
  u8 vhfsquelch0;
120
  u8 vhfsquelch1;
121
  u8 vhfsquelch2;
122
  u8 vhfsquelch3;
123
  u8 vhfsquelch4;
124
  u8 vhfsquelch5;
125
  u8 vhfsquelch6;
126
  u8 vhfsquelch7;
127
  u8 vhfsquelch8;
128
  u8 vhfsquelch9;
129
  u8 unknown1[6];
130
  u8 uhfsquelch0;
131
  u8 uhfsquelch1;
132
  u8 uhfsquelch2;
133
  u8 uhfsquelch3;
134
  u8 uhfsquelch4;
135
  u8 uhfsquelch5;
136
  u8 uhfsquelch6;
137
  u8 uhfsquelch7;
138
  u8 uhfsquelch8;
139
  u8 uhfsquelch9;
140
  u8 unknown2[6];
141
  u8 vhfhipwr0;
142
  u8 vhfhipwr1;
143
  u8 vhfhipwr2;
144
  u8 vhfhipwr3;
145
  u8 vhfhipwr4;
146
  u8 vhfhipwr5;
147
  u8 vhfhipwr6;
148
  u8 vhfhipwr7;
149
  u8 vhflopwr0;
150
  u8 vhflopwr1;
151
  u8 vhflopwr2;
152
  u8 vhflopwr3;
153
  u8 vhflopwr4;
154
  u8 vhflopwr5;
155
  u8 vhflopwr6;
156
  u8 vhflopwr7;
157
  u8 uhfhipwr0;
158
  u8 uhfhipwr1;
159
  u8 uhfhipwr2;
160
  u8 uhfhipwr3;
161
  u8 uhfhipwr4;
162
  u8 uhfhipwr5;
163
  u8 uhfhipwr6;
164
  u8 uhfhipwr7;
165
  u8 uhflopwr0;
166
  u8 uhflopwr1;
167
  u8 uhflopwr2;
168
  u8 uhflopwr3;
169
  u8 uhflopwr4;
170
  u8 uhflopwr5;
171
  u8 uhflopwr6;
172
  u8 uhflopwr7;
173
} test;
174
"""
175

    
176

    
177
def do_ident(radio):
178
    radio.pipe.timeout = 3
179
    radio.pipe.write("\x05TROGRAM")
180
    for x in xrange(10):
181
        ack = radio.pipe.read(1)
182
        if ack == '\x06':
183
            break
184
    else:
185
        raise errors.RadioError("Radio did not ack programming mode")
186
    radio.pipe.write("\x02")
187
    ident = radio.pipe.read(8)
188
    LOG.debug(util.hexprint(ident))
189
    if not ident.startswith('HKT511'):
190
        raise errors.RadioError("Unsupported model")
191
    radio.pipe.write("\x06")
192
    ack = radio.pipe.read(1)
193
    if ack != "\x06":
194
        raise errors.RadioError("Radio did not ack ident")
195

    
196

    
197
def do_status(radio, direction, addr):
198
    status = chirp_common.Status()
199
    status.msg = "Cloning %s radio" % direction
200
    status.cur = addr
201
    status.max = 0x1000
202
    radio.status_fn(status)
203

    
204

    
205
def do_download(radio):
206
    do_ident(radio)
207
    data = "TH350 Radio Program data v1.08\x00\x00"
208
    data += ("\x00" * 16)
209
    firstack = None
210
    for i in range(0, 0x1000, 16):
211
        frame = struct.pack(">cHB", "R", i, 16)
212
        radio.pipe.write(frame)
213
        result = radio.pipe.read(20)
214
        if frame[1:4] != result[1:4]:
215
            LOG.debug(util.hexprint(result))
216
            raise errors.RadioError("Invalid response for address 0x%04x" % i)
217
        data += result[4:]
218
        do_status(radio, "from", i)
219

    
220
    return memmap.MemoryMap(data)
221

    
222

    
223
def do_upload(radio):
224
    do_ident(radio)
225
    data = radio._mmap[0x0030:]
226

    
227
    for i in range(0, 0x1000, 16):
228
        frame = struct.pack(">cHB", "W", i, 16)
229
        frame += data[i:i + 16]
230
        radio.pipe.write(frame)
231
        ack = radio.pipe.read(1)
232
        if ack != "\x06":
233
            # UV-B5/UV-B6 radios with 27 menus do not support service settings
234
            # and will stop ACKing when the upload reaches 0x0F10
235
            if i == 0x0F10:
236
                # must be a radio with 27 menus detected - stop upload
237
                break
238
            else:
239
                LOG.debug("Radio NAK'd block at address 0x%04x" % i)
240
                raise errors.RadioError(
241
                    "Radio NAK'd block at address 0x%04x" % i)
242
        LOG.debug("Radio ACK'd block at address 0x%04x" % i)
243
        do_status(radio, "to", i)
244

    
245

    
246
DUPLEX = ["", "-", "+"]
247
CHARSET = "0123456789- ABCDEFGHIJKLMNOPQRSTUVWXYZ/_+*"
248
POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=1),
249
                chirp_common.PowerLevel("High", watts=5)]
250

    
251

    
252
@directory.register
253
class Th350Radio(BaofengUVB5):
254
    """TYT TH-350"""
255
    VENDOR = "TYT"
256
    MODEL = "TH-350"
257
    BAUD_RATE = 9600
258
    SPECIALS = {
259
        "VFO1": -3,
260
        "VFO2": -2,
261
        "VFO3": -1,
262
    }
263

    
264
    _memsize = 0x1000
265

    
266
    @classmethod
267
    def get_prompts(cls):
268
        rp = chirp_common.RadioPrompts()
269
        rp.experimental = \
270
            ("This TYT TH-350 driver is an alpha version. "
271
             "Proceed with Caution and backup your data. "
272
             "Always confirm the correctness of your settings with the "
273
             "official programmer.")
274
        return rp
275

    
276
    def get_features(self):
277
        rf = chirp_common.RadioFeatures()
278
        rf.has_settings = True
279
        rf.has_cross = True
280
        rf.has_rx_dtcs = True
281
        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
282
        rf.valid_cross_modes = ["Tone->Tone", "Tone->DTCS", "DTCS->Tone",
283
                                "->Tone", "->DTCS", "DTCS->", "DTCS->DTCS"]
284
        rf.valid_duplexes = DUPLEX + ["split"]
285
        rf.can_odd_split = True
286
        rf.valid_skips = ["", "S"]
287
        rf.valid_characters = CHARSET
288
        rf.valid_name_length = 5
289
        rf.valid_bands = [(130000000, 175000000),
290
                          (220000000, 269000000),
291
                          (400000000, 520000000)]
292
        rf.valid_modes = ["FM", "NFM"]
293
        rf.valid_special_chans = self.SPECIALS.keys()
294
        rf.valid_power_levels = POWER_LEVELS
295
        rf.valid_tuning_steps = [5.0, 6.25, 10.0, 12.5, 20.0, 25.0]
296
        rf.has_ctone = True
297
        rf.has_bank = False
298
        rf.has_tuning_step = False
299
        rf.memory_bounds = (1, 128)
300
        return rf
301

    
302
    def sync_in(self):
303
        try:
304
            self._mmap = do_download(self)
305
        except errors.RadioError:
306
            raise
307
        except Exception, e:
308
            raise errors.RadioError("Failed to communicate with radio: %s" % e)
309
        self.process_mmap()
310

    
311
    def sync_out(self):
312
        try:
313
            do_upload(self)
314
        except errors.RadioError:
315
            raise
316
        except Exception, e:
317
            raise errors.RadioError("Failed to communicate with radio: %s" % e)
318

    
319
    def process_mmap(self):
320
        self._memobj = bitwise.parse(mem_format, self._mmap)
321

    
322
    def _decode_tone(self, _mem, which):
323
        def _get(field):
324
            return getattr(_mem, "%s%s" % (which, field))
325

    
326
        tonea, toneb = _get('tonea'), _get('toneb')
327

    
328
        if tonea == 0xff:
329
            mode = val = pol = None
330
        elif tonea >= 0x80:
331
            # DTCS
332
            # D754N -> 0x87 0x54
333
            # D754I -> 0xc7 0x54
334
            # Yes. Decimal digits as hex. You're seeing that right.
335
            # No idea why TYT engineers would do something like that.
336
            pold = tonea / 16
337
            if pold not in [0x8, 0xc]:
338
                LOG.warn("Bug: tone is %04x %04x" % (tonea, toneb))
339
                mode = val = pol = None
340
            else:
341
                mode = 'DTCS'
342
                val = (tonea % 16) * 100 + \
343
                    toneb / 16 * 10 + \
344
                    (toneb % 16)
345
                pol = 'N' if pold == 8 else 'R'
346
        else:
347
            # Tone
348
            # 107.2 -> 0x10 0x72. Seriously.
349
            mode = 'Tone'
350
            val = tonea / 16 * 100 + \
351
                (tonea % 16) * 10 + \
352
                toneb / 16 + \
353
                float(toneb % 16) / 10
354
            pol = None
355

    
356
        return mode, val, pol
357

    
358
    def _encode_tone(self, _mem, which, mode, val, pol):
359
        def _set(field, value):
360
            setattr(_mem, "%s%s" % (which, field), value)
361

    
362
        if mode == "Tone":
363
            tonea = int(
364
                        floor(val / 100) * 16 +
365
                        floor(val / 10) % 10
366
                    )
367
            toneb = int(
368
                        floor(val % 10) * 16 +
369
                        floor(val * 10) % 10
370
                    )
371
        elif mode == "DTCS":
372
            tonea = (0x80 if pol == 'N' else 0xc0) + \
373
                val / 100
374
            toneb = (val / 10) % 10 * 16 + \
375
                val % 10
376
        else:
377
            tonea = toneb = 0xff
378

    
379
        _set('tonea', tonea)
380
        _set('toneb', toneb)
381

    
382
    def _get_memobjs(self, number):
383
        if isinstance(number, str):
384
            return (getattr(self._memobj, number.lower()), None)
385
        elif number < 0:
386
            for k, v in self.SPECIALS.items():
387
                if number == v:
388
                    return (getattr(self._memobj, k.lower()), None)
389
        else:
390
            return (self._memobj.channels[number - 1],
391
                    self._memobj.names[number - 1].name)
392

    
393
    @classmethod
394
    def match_model(cls, filedata, filename):
395
        return (filedata.startswith("TH350 Radio Program data") and
396
                len(filedata) == (cls._memsize + 0x30))
(2-2/2)