1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# Copyright 2022 Dan Smith <chirp@f.danplanet.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import binascii
import os
import unittest
from unittest import mock
from chirp import directory
from chirp.drivers import alinco, alinco_dr735t
from chirp import memmap
class FakeAlincoSerial:
"""Behaves like a Serial with an Alinco radio connected"""
def __init__(self, image):
self.image = memmap.MemoryMapBytes(open(image, 'rb').read())
self.readbuf = []
self.ident = b''
def zero(self):
"""Zero the mmap before an upload"""
self.image = memmap.MemoryMapBytes(b'\x00' * len(self.image))
def _handle_rw_dr735tn(self, data):
if b'R' in data:
addr_idx = data.index(b'R')-4
elif b'W' in data:
addr_idx = data.index(b'W')-4
else:
raise Exception('Unable to handle RW command %r' % data)
addr = int(data[addr_idx: addr_idx+4], 16)
cmd = chr(data[addr_idx+4])
if cmd == 'R':
bin_chunk = self.image[addr:addr+64]
hex_chunk = binascii.hexlify(bin_chunk)
assert (len(hex_chunk) == 128)
self.readbuf.append(hex_chunk)
self.readbuf.append(b'\r\n')
if cmd == 'W':
hex_chunk = data[addr_idx+5:addr_idx+128+5].decode()
bin_chunk = binascii.unhexlify(hex_chunk)
self.image[addr] = bin_chunk
self.readbuf.append(b'OK\r\n')
def _handle_rw(self, data):
if b'R' in data:
cmdindex = data.index(b'R')
elif b'W' in data:
cmdindex = data.index(b'W')
else:
raise Exception('Unable to handle RW command %r' % data)
if cmdindex - 4 == 4:
block_size = 16
else:
block_size = 64
addr = int(data[4:cmdindex], 16)
cmd = data[cmdindex:cmdindex + 1]
if cmd == b'R':
chunk = binascii.hexlify(self.image[addr:addr + block_size])
if b'DJ175' in self.ident:
# The DJ175 uses 16-byte blocks, but without the colon
# separator
self.readbuf.append(b'%s\r\n' % chunk)
elif block_size == 64:
self.readbuf.append(b'\r\n%s\r\n' % chunk)
else:
self.readbuf.append(b'\r\n%04X:%s\r\n' % (addr, chunk))
elif cmd == b'W':
chunk = binascii.unhexlify(data[cmdindex + 1:].rstrip())
if len(chunk) != block_size:
raise Exception('Expected %i, got %i' % (block_size,
len(chunk)))
self.image[addr] = chunk
if block_size == 64:
self.readbuf.append(b'OK\r\n\r\n')
else:
raise Exception('Unable to handle command %s: %r' % (cmd, data))
def write(self, data):
"""Write to the radio (i.e. buffer some response for read)"""
# Serial port echo
self.readbuf.append(data)
ident_ack = b'OK\r\n\r\n'
if data.startswith(b'DR'):
self.ident = data.strip()
self.readbuf.append(ident_ack)
elif data.startswith(b'AL~DJ-G7'):
self.ident = data.strip()
self.readbuf.append(ident_ack)
elif data.startswith(b'DJ'):
self.ident = data.strip()
self.readbuf.append(ident_ack)
elif data.startswith(b'AL~F'):
self._handle_rw(data)
elif data.startswith(b'AL~E') and self.ident != b"DR735TN":
self.readbuf.append(b'OK' * 10)
elif data.startswith(b'AL~WHO'):
# radio responds with model to WHO
self.ident = b"DR735TN"
# expect back "DR735TN\r\nOK\r\n"
self.readbuf.append(self.ident+b"\r\n")
elif data.startswith(b'AL~DR735J'):
# this is somehow making the radio writeable...
self.readbuf.append(b'OK\r\n')
elif data.startswith(b'AL~RESET') and self.ident == b"DR735TN":
self.readbuf.append(b'OK\r\n')
elif data.startswith(b'AL~EEPEL'):
self._handle_rw_dr735tn(data)
else:
raise Exception('Unable to handle %r' % data)
def read(self, length):
"""Read from the radio (i.e. generate a radio's response)"""
if len(self.readbuf[0]) == length:
return self.readbuf.pop(0)
else:
raise Exception('Read of %i not match %i %r' % (
length, len(self.readbuf[0]), self.readbuf[0]))
class AlincoCloneTest(unittest.TestCase):
def _test_alinco_download(self, rclass, image):
pipe = FakeAlincoSerial(image)
radio = rclass(pipe)
radio.sync_in()
if not radio.NEEDS_COMPAT_SERIAL:
# Note that MemoryMap (the str-based compat class) is a subclass
# of MemoryMapBytes, so this must compare the type exactly.
self.assertEqual(type(radio._mmap), memmap.MemoryMapBytes)
def _test_alinco_upload(self, rclass, image):
pipe = FakeAlincoSerial(image)
pipe.zero()
radio = rclass(image)
radio.pipe = pipe
radio.sync_out()
ref_image = open(image, 'rb').read()
self.assertEqual(ref_image[0x200:rclass._memsize],
pipe.image.get_packed()[0x200:rclass._memsize])
def _test_alinco_roundtrip(self, rclass, image):
# Read from the radio and then write the same thing straight back,
# which is what actually happens in the field (#12611)
pipe = FakeAlincoSerial(image)
radio = rclass(None)
radio.pipe = pipe
with mock.patch('time.sleep'):
radio.sync_in()
pipe.zero()
with mock.patch('time.sleep'):
radio.sync_out()
ref_image = open(image, 'rb').read()
self.assertEqual(ref_image[0x200:rclass._memsize],
pipe.image.get_packed()[0x200:rclass._memsize])
def _test_alinco(self, rclass):
ident = directory.radio_class_id(rclass)
image = os.path.join(os.path.dirname(__file__),
'..', 'images',
'%s.img' % ident)
with mock.patch('time.sleep'):
# The round-trip goes first, as it is the flow that actually
# broke in the field (#12611)
self._test_alinco_roundtrip(rclass, image)
self._test_alinco_upload(rclass, image)
self._test_alinco_download(rclass, image)
def _test_roundtrip(self, rclass):
"""Run only the read-then-write-back test for @rclass"""
ident = directory.radio_class_id(rclass)
image = os.path.join(os.path.dirname(__file__),
'..', 'images',
'%s.img' % ident)
with mock.patch('time.sleep'):
self._test_alinco_roundtrip(rclass, image)
def test_djg7(self):
self._test_alinco(alinco.AlincoDJG7EG)
def test_dr235(self):
self._test_alinco(alinco.DR235Radio)
def test_dr735(self):
self._test_alinco(alinco_dr735t.AlincoDR735T)
def test_dj175(self):
# The 175 has a slightly different download string, so test it
# specifically
self._test_alinco(alinco.DJ175Radio)
def test_dj175_roundtrip(self):
# Uploading after a download used to fail with
# "encoding with 'hex' codec failed (TypeError: a bytes-like
# object is required, not 'str')" (#12611). This is kept as its
# own test so that the actual failure is reported, rather than
# tripping over the mmap type check in _test_alinco_download().
self._test_roundtrip(alinco.DJ175Radio)
def test_dr235_roundtrip(self):
self._test_roundtrip(alinco.DR235Radio)
def test_all_alinco_identify(self):
# Make sure all the alinco models have bytes for their _model
# and can identify properly
alincos = [x for x in directory.DRV_TO_RADIO.values()
if x.VENDOR == 'Alinco']
for rclass in alincos:
pipe = FakeAlincoSerial(__file__)
radio = rclass(None)
radio.pipe = pipe
radio._identify()
self.assertEqual(pipe.ident, radio._model)
|