Project

General

Profile

Bug #10616 » ft2800.py

0cf683d6 - Dan Smith, 06/04/2023 01:05 PM

 
1
# Copyright 2011 Dan Smith <dsmith@danplanet.com>
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 3 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 logging
18

    
19
from chirp import util, memmap, chirp_common, bitwise, directory, errors
20
from chirp.drivers.yaesu_clone import YaesuCloneModeRadio
21

    
22
LOG = logging.getLogger(__name__)
23

    
24
CHUNK_SIZE = 16
25

    
26

    
27
def _send(s, data):
28
    for i in range(0, len(data), CHUNK_SIZE):
29
        chunk = data[i:i+CHUNK_SIZE]
30
        s.write(chunk)
31
        echo = s.read(len(chunk))
32
        if chunk != echo:
33
            raise Exception("Failed to read echo chunk")
34

    
35
IDBLOCK = b"\x0c\x01\x41\x33\x35\x02\x00\xb8"
36
TRAILER = b"\x0c\x02\x41\x33\x35\x00\x00\xb7"
37
ACK = b"\x0C\x06\x00"
38

    
39

    
40
def _download(radio):
41
    data = b""
42
    attempts = 30
43
    for _i in range(0, attempts):
44
        data = radio.pipe.read(8)
45
        if data == IDBLOCK:
46
            break
47
        LOG.debug('Download attempt %i received %i: %s',
48
                  _i, len(data), util.hexprint(data))
49
        if radio.status_fn:
50
            status = chirp_common.Status()
51
            status.max = 1
52
            status.cur = 0
53
            status.msg = "Waiting for radio (%i)" % (
54
                attempts - (_i + 1))
55
            radio.status_fn(status)
56

    
57
    LOG.debug("Header:\n%s" % util.hexprint(data))
58

    
59
    if len(data) != 8:
60
        raise Exception("Failed to read header")
61

    
62
    _send(radio.pipe, ACK)
63

    
64
    data = b""
65

    
66
    while len(data) < radio._block_sizes[1]:
67
        time.sleep(0.1)
68
        chunk = radio.pipe.read(38)
69
        LOG.debug("Got: %i:\n%s" % (len(chunk), util.hexprint(chunk)))
70
        if len(chunk) == 8:
71
            LOG.debug("END?")
72
        elif len(chunk) != 38:
73
            LOG.debug("Should fail?")
74
            break
75
            # raise Exception("Failed to get full data block")
76
        else:
77
            cs = 0
78
            for byte in chunk[:-1]:
79
                cs += byte
80
            if chunk[-1] != (cs & 0xFF):
81
                raise Exception("Block failed checksum!")
82

    
83
            data += chunk[5:-1]
84

    
85
        _send(radio.pipe, ACK)
86
        if radio.status_fn:
87
            status = chirp_common.Status()
88
            status.max = radio._block_sizes[1]
89
            status.cur = len(data)
90
            status.msg = "Cloning from radio"
91
            radio.status_fn(status)
92

    
93
    LOG.debug("Total: %i" % len(data))
94

    
95
    return memmap.MemoryMapBytes(data)
96

    
97

    
98
def _upload(radio):
99
    for _i in range(0, 10):
100
        data = radio.pipe.read(256)
101
        if not data:
102
            break
103
        LOG.debug("What is this garbage?\n%s" % util.hexprint(data))
104

    
105
    _send(radio.pipe, IDBLOCK)
106
    time.sleep(1)
107
    ack = radio.pipe.read(300)
108
    LOG.debug("Ack was (%i):\n%s" % (len(ack), util.hexprint(ack)))
109
    if ack != ACK:
110
        raise Exception("Radio did not ack ID")
111

    
112
    block = 0
113
    while block < (radio.get_memsize() // 32):
114
        data = b"\x0C\x03\x00\x00" + bytes([block])
115
        data += radio.get_mmap()[block*32:(block+1)*32]
116
        cs = 0
117
        for byte in data:
118
            cs += byte
119
        data += bytes([cs & 0xFF])
120

    
121
        LOG.debug("Writing block %i:\n%s" % (block, util.hexprint(data)))
122

    
123
        _send(radio.pipe, data)
124
        time.sleep(0.1)
125
        ack = radio.pipe.read(3)
126
        if ack != ACK:
127
            raise Exception("Radio did not ack block %i" % block)
128

    
129
        if radio.status_fn:
130
            status = chirp_common.Status()
131
            status.max = radio._block_sizes[1]
132
            status.cur = block * 32
133
            status.msg = "Cloning to radio"
134
            radio.status_fn(status)
135
        block += 1
136

    
137
    _send(radio.pipe, TRAILER)
138

    
139
MEM_FORMAT = """
140
struct {
141
  bbcd freq[4];
142
  u8 unknown1[4];
143
  bbcd offset[2];
144
  u8 unknown2[2];
145
  u8 pskip:1,
146
     skip:1,
147
     unknown3:1,
148
     isnarrow:1,
149
     power:2,
150
     duplex:2;
151
  u8 unknown4:6,
152
     tmode:2;
153
  u8 tone;
154
  u8 dtcs;
155
} memory[200];
156

    
157
#seekto 0x0E00;
158
struct {
159
  char name[6];
160
} names[200];
161
"""
162

    
163
MODES = ["FM", "NFM"]
164
TMODES = ["", "Tone", "TSQL", "DTCS"]
165
DUPLEX = ["", "-", "+", ""]
166
POWER_LEVELS = [chirp_common.PowerLevel("Hi", watts=65),
167
                chirp_common.PowerLevel("Mid", watts=25),
168
                chirp_common.PowerLevel("Low2", watts=10),
169
                chirp_common.PowerLevel("Low1", watts=5),
170
                ]
171
CHARSET = chirp_common.CHARSET_UPPER_NUMERIC + "()+-=*/???|_"
172

    
173

    
174
@directory.register
175
class FT2800Radio(YaesuCloneModeRadio):
176
    """Yaesu FT-2800"""
177
    VENDOR = "Yaesu"
178
    MODEL = "FT-2800M"
179

    
180
    _block_sizes = [8, 7680]
181
    _memsize = 7680
182

    
183
    @classmethod
184
    def get_prompts(cls):
185
        rp = chirp_common.RadioPrompts()
186
        rp.pre_download = _(
187
            "1. Turn radio off.\n"
188
            "2. Connect cable\n"
189
            "3. Press and hold in the MHz, Low, and D/MR keys on the radio "
190
            "while turning it on\n"
191
            "4. <b>After clicking OK</b>, "
192
            "press the MHz key on the radio to send"
193
            " image.\n"
194
            "    (\"TX\" will appear on the LCD). \n")
195
        rp.pre_upload = _(
196
            "1. Turn radio off.\n"
197
            "2. Connect cable\n"
198
            "3. Press and hold in the MHz, Low, and D/MR keys on the radio "
199
            "while turning it on\n"
200
            "4. Press the Low key on the radio "
201
            "(\"RX\" will appear on the LCD).\n"
202
            "5. Click OK.")
203
        return rp
204

    
205
    def get_features(self):
206
        rf = chirp_common.RadioFeatures()
207

    
208
        rf.memory_bounds = (0, 199)
209

    
210
        rf.has_ctone = False
211
        rf.has_tuning_step = False
212
        rf.has_dtcs_polarity = False
213
        rf.has_bank = False
214

    
215
        rf.valid_tuning_steps = [5.0, 10.0, 12.5, 15.0,
216
                                 20.0, 25.0, 50.0, 100.0]
217
        rf.valid_modes = MODES
218
        rf.valid_tmodes = TMODES
219
        rf.valid_bands = [(137000000, 174000000)]
220
        rf.valid_power_levels = POWER_LEVELS
221
        rf.valid_duplexes = DUPLEX
222
        rf.valid_skips = ["", "S", "P"]
223
        rf.valid_name_length = 6
224
        rf.valid_characters = CHARSET
225

    
226
        return rf
227

    
228
    def sync_in(self):
229
        self.pipe.parity = "E"
230
        self.pipe.timeout = 1
231
        start = time.time()
232
        try:
233
            self._mmap = _download(self)
234
        except errors.RadioError:
235
            raise
236
        except Exception as e:
237
            LOG.exception('Failed download')
238
            raise errors.RadioError("Failed to communicate with radio: %s" % e)
239
        LOG.info("Downloaded in %.2f sec" % (time.time() - start))
240
        self.process_mmap()
241

    
242
    def sync_out(self):
243
        self.pipe.timeout = 1
244
        self.pipe.parity = "E"
245
        start = time.time()
246
        try:
247
            _upload(self)
248
        except errors.RadioError:
249
            raise
250
        except Exception as e:
251
            LOG.exception('Failed upload')
252
            raise errors.RadioError("Failed to communicate with radio: %s" % e)
253
        LOG.info("Uploaded in %.2f sec" % (time.time() - start))
254

    
255
    def process_mmap(self):
256
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
257

    
258
    def get_raw_memory(self, number):
259
        return repr(self._memobj.memory[number])
260

    
261
    def get_memory(self, number):
262
        _mem = self._memobj.memory[number]
263
        _nam = self._memobj.names[number]
264
        mem = chirp_common.Memory()
265

    
266
        mem.number = number
267

    
268
        if _mem.get_raw()[0] == "\xFF":
269
            mem.empty = True
270
            return mem
271

    
272
        mem.freq = int(_mem.freq) * 10
273
        mem.offset = int(_mem.offset) * 100000
274
        mem.duplex = DUPLEX[_mem.duplex]
275
        mem.tmode = TMODES[_mem.tmode]
276
        mem.rtone = chirp_common.TONES[_mem.tone]
277
        mem.dtcs = chirp_common.DTCS_CODES[_mem.dtcs]
278
        mem.name = str(_nam.name).rstrip()
279
        mem.mode = _mem.isnarrow and "NFM" or "FM"
280
        mem.skip = _mem.pskip and "P" or _mem.skip and "S" or ""
281
        mem.power = POWER_LEVELS[_mem.power]
282

    
283
        return mem
284

    
285
    def set_memory(self, mem):
286
        _mem = self._memobj.memory[mem.number]
287
        _nam = self._memobj.names[mem.number]
288

    
289
        if mem.empty:
290
            _mem.set_raw("\xFF" * (_mem.size() // 8))
291
            return
292

    
293
        if _mem.get_raw()[0] == "\xFF":
294
            # Empty -> Non-empty, so initialize
295
            _mem.set_raw("\x00" * (_mem.size() // 8))
296

    
297
        _mem.freq = mem.freq / 10
298
        _mem.offset = mem.offset / 100000
299
        _mem.duplex = DUPLEX.index(mem.duplex)
300
        _mem.tmode = TMODES.index(mem.tmode)
301
        _mem.tone = chirp_common.TONES.index(mem.rtone)
302
        _mem.dtcs = chirp_common.DTCS_CODES.index(mem.dtcs)
303
        _mem.isnarrow = MODES.index(mem.mode)
304
        _mem.pskip = mem.skip == "P"
305
        _mem.skip = mem.skip == "S"
306
        if mem.power:
307
            _mem.power = POWER_LEVELS.index(mem.power)
308
        else:
309
            _mem.power = 0
310

    
311
        _nam.name = mem.name.ljust(6)[:6]
312

    
313
    @classmethod
314
    def match_model(cls, filedata, filename):
315
        return len(filedata) == cls._memsize
(33-33/41)