File: icom_clone_simulator.py

package info (click to toggle)
chirp 1%3A20221106%2Bpy3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,144 kB
  • sloc: python: 119,334; ansic: 296; sh: 184; makefile: 41
file content (195 lines) | stat: -rw-r--r-- 6,544 bytes parent folder | download | duplicates (2)
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
# Copyright 2019 Dan Smith <dsmith@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/>.

from builtins import bytes

import struct

from chirp.drivers import icf

import logging

LOG = logging.getLogger(__name__)


import sys
l = logging.getLogger()
l.level = logging.ERROR
l.addHandler(logging.StreamHandler(sys.stdout))

class FakeIcomRadio(object):
    def __init__(self, radio, mapfile=None):
        self._buffer = bytes(b'')
        self._radio = radio
        if not mapfile:
            self._memory = bytes(b'\x00') * radio.get_memsize()
        else:
            self.load_from_file(mapfile)

    def load_from_file(self, filename):
        with open(filename, 'rb') as f:
            self._memory = bytes(f.read())
        LOG.debug('Initialized %i bytes from %s' % (len(self._memory),
                                                    filename))

    def read(self, count):
        """read() from radio, so here we synthesize responses"""
        chunk = self._buffer[:count]
        self._buffer = self._buffer[count:]
        return chunk

    def queue(self, data):
        # LOG.debug('Queuing: %r' % data)
        self._buffer += data

    def make_response(self, cmd, payload):
        return bytes([
            0xFE, 0xFE,
            0xEF,  # Radio
            0xEE,  # PC
            cmd,
        ]) + payload + bytes([0xFD])

    @property
    def address_fmt(self):
        if self._radio.get_memsize() > 0x10000:
            return 'I'
        else:
            return 'H'

    def do_clone_out(self):
        LOG.debug('Clone from radio started')
        size = 16
        for addr in range(0, self._radio.get_memsize(), size):
            if len(self._memory[addr:]) < 4:
                # IC-W32E has an off-by-one hack for detection,
                # which will cause us to send a short one-byte
                # block of garbage, unlike the real radio. So,
                # if we get to the end and have a few bytes
                # left, don't be stupid.
                break
            header = bytes(struct.pack('>%sB' % self.address_fmt,
                                       addr, size))
            #LOG.debug('Header for %02x@%04x: %r' % (
            #    size, addr, header))
            chunk = []
            cs = 0
            for byte in header:
                chunk.extend(x for x in bytes(b'%02X' % byte))
                cs += byte
            #LOG.debug('Chunk so far: %r' % chunk)
            for byte in self._memory[addr:addr + size]:
                chunk.extend(x for x in bytes(b'%02X' % byte))
                cs += byte
            #LOG.debug('Chunk is %r' % chunk)

            vx = ((cs ^ 0xFFFF) + 1) & 0xFF
            chunk.extend(x for x in bytes(b'%02X' % vx))
            self.queue(self.make_response(icf.CMD_CLONE_DAT, bytes(chunk)))
            #LOG.debug('Stopping after first frame')
            #break
        self.queue(self.make_response(icf.CMD_CLONE_END, bytes([])))

    def do_clone_in(self):
        LOG.debug('Clone to radio started')
        self._memory = bytes(b'')

    def do_clone_data(self, payload_hex):
        if self.address_fmt == 'I':
            header_len = 5
        else:
            header_len = 3

        def hex_to_byte(hexchars):
            return int('%s%s' % (chr(hexchars[0]), chr(hexchars[1])), 16)

        payload_bytes = bytes([hex_to_byte(payload_hex[i:i+2])
                               for i in range(0, len(payload_hex), 2)])

        addr, size = struct.unpack('>%sB' % self.address_fmt, payload_bytes[:header_len])
        data = payload_bytes[header_len:-1]
        csum = payload_bytes[-1]

        #addr_hex = payload[0:size_offset]
        #size_hex = payload[size_offset:size_offset + 2]
        #data_hex = payload[size_offset + 2:-2]
        #csum_hex = payload[-2:]


        #addr = hex_to_byte(addr_hex[0:2]) << 8 | hex_to_byte(addr_hex[2:4])
        #size = hex_to_byte(size_hex)
        #csum = hex_to_byte(csum_hex)

        #data = []
        #for i in range(0, len(data_hex), 2):
        #    data.append(hex_to_byte(data_hex[i:i+2]))

        if len(data) != size:
            LOG.debug('Invalid frame size: expected %i, but got %i' % (
                size, len(data)))

        expected_addr = len(self._memory)
        if addr < expected_addr:
            LOG.debug('Frame goes back to %04x from %04x' % (addr,
                                                             expected_addr))
        if len(self._memory) != addr:
            LOG.debug('Filling gap between %04x and %04x' % (expected_addr,
                                                             addr))
            self._memory += (bytes(b'\x00') * (addr - expected_addr))

        # FIXME: Check checksum

        self._memory += data

    def write(self, data):
        """write() to radio, so here we process requests"""

        assert isinstance(data, bytes), 'Bytes required, %s received' % data.__class__

        if data[:12] == (bytes(b'\xFE') * 12):
            LOG.debug('Got hispeed kicker')
            data = data[12:]
            if data[2] == 0xFE:
                return

        src = data[2]
        dst = data[3]
        cmd = data[4]
        payload = data[5:-1]
        end = data[-1]

        LOG.debug('Received command: %r' % cmd)
        LOG.debug('  Full frame: %r' % data)

        model = self._radio.get_model() + bytes(b'\x00' * 20)

        if cmd == 0xE0:  # Ident
            # FIXME
            self.queue(self.make_response(0x01,  # Model
                                          model))
        elif cmd == icf.CMD_CLONE_OUT:
            self.do_clone_out()
        elif cmd == icf.CMD_CLONE_IN:
            self.do_clone_in()
        elif cmd == icf.CMD_CLONE_DAT:
            self.do_clone_data(payload)
        else:
            LOG.debug('Unknown command %i' % cmd)
            self.queue(self.make_response(0x00, bytes([0x01])))

        return len(data)

    def flush(self):
        return