diff --git a/tashtalkd/daemon/__init__.py b/tashtalkd/daemon/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tashtalkd/daemon/protocol/ltoudp.py b/tashtalkd/daemon/protocol/ltoudp.py new file mode 100644 index 0000000..565d984 --- /dev/null +++ b/tashtalkd/daemon/protocol/ltoudp.py @@ -0,0 +1,99 @@ +from itertools import chain +import logging +import os +import select +import socket +import struct +import time + +from ..util import hexdump_lines, TashTalkReceiver, TashTalkTransmitter, NodeIdSet + + +LTOUDP_GROUP = '239.192.76.84' # the last two octets spell 'LT' +LTOUDP_PORT = 1954 + + +class LtoudpDaemon: + + def __init__(self, serial_obj): + self.serial_obj = serial_obj + self.receiver = None + self.sender = None + self.socket = None + self.sender_id = None + self.node_id_set = None + + def initialize(self): + '''Set up the serial and socket connections the daemon will use.''' + + self.receiver = TashTalkReceiver(self.serial_obj) + self.sender = TashTalkTransmitter(self.serial_obj) + self.sender.initialize() + + self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + self.socket.bind((LTOUDP_GROUP, LTOUDP_PORT)) + self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 1) + self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, + struct.pack('=4sL', socket.inet_aton(LTOUDP_GROUP), socket.INADDR_ANY)) + #TODO add an option for this instead of forcing INADDR_ANY? + + self.sender_id = struct.pack('>L', os.getpid()) + self.node_id_set = NodeIdSet(self.sender) + + def service_tashtalk(self): + '''Called when there is data from TashTalk that could finish one or more frames.''' + + for frame in self.receiver.get_frames(): + + frame_dest = frame[0] + frame_type = frame[2] + if frame_type in (0x84, 0x85): + logging.debug('not retransmitting RTS/CTS frame') + continue + if frame_type == 0x81 and frame_dest in self.node_id_set: + logging.debug('not retransmitting ENQ frame that TashTalk has already responded to') + continue + + self.socket.sendto(self.sender_id + frame[:-2], (LTOUDP_GROUP, LTOUDP_PORT)) + + def service_udp(self): + '''Called when there is a UDP datagram to be parsed and potentially forwarded.''' + + data, sender_addr = self.socket.recvfrom(65507) + + if logging.getLogger().isEnabledFor(logging.DEBUG): + logging.debug('\n'.join(chain(('received UDP datagram from %s:' % str(sender_addr),), + (line for line in hexdump_lines(data))))) + + if len(data) < 7: + logging.info('ignoring invalid too-small UDP datagram') + return + + if data[0:4] == self.sender_id: #TODO check sender_addr too + logging.info('ignoring echoed UDP datagram') + return + + frame = data[4:] + frame_dest = frame[0] + frame_src = frame[1] + frame_type = frame[2] + if not (frame_type == 0x81 and frame_dest == frame_src): self.node_id_set.touch(frame_src) + + self.sender.send_frame(frame) + + def run(self): + '''Run the daemon.''' + + last_check_expiration = time.monotonic() + + while True: + rlist, wlist, xlist = select.select((self.receiver, self.socket), (), (), 10) + if self.receiver in rlist: self.service_tashtalk() + if self.socket in rlist: self.service_udp() + + now = time.monotonic() + if now - last_check_expiration >= 10: + self.node_id_set.check_expiration() + last_check_expiration = now diff --git a/tashtalkd/daemon/util/__init__.py b/tashtalkd/daemon/util/__init__.py new file mode 100644 index 0000000..99764d6 --- /dev/null +++ b/tashtalkd/daemon/util/__init__.py @@ -0,0 +1,3 @@ +from .receiver import TashTalkReceiver +from .transmitter import TashTalkTransmitter +from .misc import hexdump_lines, NodeIdSet diff --git a/tashtalkd/daemon/util/lt_crc.py b/tashtalkd/daemon/util/lt_crc.py new file mode 100644 index 0000000..dc47062 --- /dev/null +++ b/tashtalkd/daemon/util/lt_crc.py @@ -0,0 +1,54 @@ +'''Code for calculating CRCs ('frame check sequences') in the bizarre way that SDLC defined and LocalTalk followed.''' + + +LT_CRC_LUT = ( + 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, 0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, + 0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876, + 0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5, + 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974, + 0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB, 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3, + 0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72, + 0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, + 0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738, 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70, + 0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7, 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF, + 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036, 0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, + 0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5, 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD, + 0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C, + 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB, + 0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232, 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A, + 0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9, + 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78, +) + + +class CrcCalculator: + '''Utility class to calculate the CRC of a frame.''' + + def __init__(self): + self.reset() + + def reset(self): + '''Reset the CRC calculator as though no data had been fed into it.''' + self.reg = 0xFFFF + + def feed_byte(self, byte): + '''Feed a single byte (an integer between 0 and 255) into the CRC calculator.''' + index = (self.reg & 0xFF) ^ byte + self.reg = LT_CRC_LUT[index] ^ (self.reg >> 8) + + def feed(self, data): + '''Feed a bytes-like object into the CRC calculator.''' + for byte in data: + self.feed_byte(byte) + + def byte1(self): + '''Returns the first byte of the frame CRC.''' + return (self.reg & 0xFF) ^ 0xFF + + def byte2(self): + '''Returns the second byte of the frame CRC.''' + return (self.reg >> 8) ^ 0xFF + + def is_okay(self): + '''If the CRC has been fed into the calculator and is correct, this will return True.''' + return True if self.reg == 61624 else False # this is the binary constant on B-22 of Inside Appletalk, but backwards diff --git a/tashtalkd/daemon/util/misc.py b/tashtalkd/daemon/util/misc.py new file mode 100644 index 0000000..1215c46 --- /dev/null +++ b/tashtalkd/daemon/util/misc.py @@ -0,0 +1,39 @@ +'''Miscellaneous functions and classes.''' + +import time + + +def hexdump_lines(data): + '''Utility function to make a hex dump of a bytes-like object.''' + for index in range(0, len(data), 16): + line_data = data[index:index+16] + yield '%06X %-47s %-16s' % (index, + ' '.join(('%02X' % i) for i in line_data), + ''.join((chr(i) if 32 <= i < 127 else '.') for i in line_data)) + + +class NodeIdSet: + + def __init__(self, sender, timeout=600): + self.sender = sender + self.timeout = timeout + self.node_ids = {} + + def touch(self, node_id): + if node_id in self.node_ids: + self.node_ids[node_id] = time.monotonic() + else: + self.node_ids[node_id] = time.monotonic() + self.sender.set_node_ids(self.node_ids.keys()) + + def check_expiration(self): + expired = set() + now = time.monotonic() + for node_id, last_time in self.node_ids.items(): + if now - last_time >= self.timeout: expired.add(node_id) + if expired: + for node_id in expired: self.node_ids.pop(node_id) + self.sender.set_node_ids(self.node_ids.keys()) + + def __contains__(self, item): + return True if item in self.node_ids else False diff --git a/tashtalkd/daemon/util/receiver.py b/tashtalkd/daemon/util/receiver.py new file mode 100644 index 0000000..3da1a83 --- /dev/null +++ b/tashtalkd/daemon/util/receiver.py @@ -0,0 +1,76 @@ +'''Code to abstract TashTalk receiver side.''' + +from collections import deque +from itertools import chain +import logging + +from .lt_crc import CrcCalculator +from .misc import hexdump_lines + + +class TashTalkReceiver: + '''Abstracted TashTalk receiver that deals in frames rather than raw serial port data.''' + + def __init__(self, serial_obj): + self.serial_obj = serial_obj + self.crc = CrcCalculator() + self.next_frame = deque() + self.escape_on = False + self.frames = deque() + + def _reset(self): + self.crc.reset() + self.next_frame.clear() + self.escape_on = False + + def _feed(self, data): + for byte in data: + if self.escape_on: + if byte == 0xFF: # literal 0 byte + self.next_frame.append(0) + self.crc.feed_byte(0) + elif byte == 0xFD: # Frame Delimiter + if self.crc.is_okay() and len(self.next_frame) >= 5: + frame = bytes(self.next_frame) + if logging.getLogger().isEnabledFor(logging.DEBUG): + logging.debug('\n'.join(chain(('TashTalk received good frame:',), (line for line in hexdump_lines(frame))))) + self.frames.append(frame) + else: + if logging.getLogger().isEnabledFor(logging.INFO): + frame = bytes(self.next_frame) + logging.info('\n'.join(chain(('TashTalk received bad frame:',), (line for line in hexdump_lines(frame))))) + self._reset() + elif byte == 0xFE: # Framing Error + if logging.getLogger().isEnabledFor(logging.INFO): + frame = bytes(self.next_frame) + logging.info('\n'.join(chain(('TashTalk detected framing error:',), (line for line in hexdump_lines(frame))))) + self._reset() + elif byte == 0xFA: # Frame Aborted + if logging.getLogger().isEnabledFor(logging.INFO): + frame = bytes(self.next_frame) + logging.info('\n'.join(chain(('TashTalk detected aborted frame:',), (line for line in hexdump_lines(frame))))) + self._reset() + self.escape_on = False + elif byte == 0x00: + self.escape_on = True + else: + self.next_frame.append(byte) + self.crc.feed_byte(byte) + + def _service_serial_port(self): + while True: + data = self.serial_obj.read(16384) + if not data: break + logging.debug('read %d bytes from TashTalk', len(data)) + self._feed(data) + + def get_frames(self): + '''Get data from TashTalk and yield any frames that were completed since the last time this was called.''' + self._service_serial_port() + for frame in self.frames: + yield frame + self.frames.clear() + + def fileno(self): + '''This function makes it possible to pass a receiver into select.select, at least on POSIX platforms.''' + return self.serial_obj.fileno() diff --git a/tashtalkd/daemon/util/transmitter.py b/tashtalkd/daemon/util/transmitter.py new file mode 100644 index 0000000..f63b59a --- /dev/null +++ b/tashtalkd/daemon/util/transmitter.py @@ -0,0 +1,54 @@ +'''Code to abstract TashTalk transmitter side.''' + +from itertools import chain +import logging + +from .lt_crc import CrcCalculator +from .misc import hexdump_lines + + +class TashTalkTransmitter: + '''Abstracted TashTalk transmitter that deals in commands rather than raw serial port data.''' + + def __init__(self, serial_obj): + self.serial_obj = serial_obj + + def initialize(self): + '''Initialize TashTalk by bringing it to a known-good state and setting it not to respond to any node IDs.''' + self.known_state() + self.set_node_ids(()) + + def known_state(self): + '''Return TashTalk to the known state where it's awaiting a command byte.''' + self.serial_obj.write(b'\x00' * 1024) + logging.debug('return to known state sent to TashTalk') + + def send_frame(self, frame): + '''Accept a frame without CRC bytes, check it for validity, and send it to TashTalk with the correct CRC bytes.''' + if len(frame) < 3: + raise ValueError('frame must be 3 bytes minimum') + elif len(frame) == 3 and not (frame[2] & 0x80): + raise ValueError('a 3-byte frame must be a control frame') + elif (frame[2] & 0x80) and not len(frame) == 3: + raise ValueError('a control frame must be 3 bytes in length') + elif len(frame) == 4: + raise ValueError('invalid frame length') + elif len(frame) > 603: + raise ValueError('frame may be 603 bytes maximum') + elif len(frame) >= 5 and (((frame[3] & 0x3) << 8) | frame[4]) != len(frame) - 3: + raise ValueError('frame length does not match frame length field') + crc = CrcCalculator() + crc.feed(frame) + frame += bytes((crc.byte1(), crc.byte2())) + self.serial_obj.write(b'\x01' + frame) + if logging.getLogger().isEnabledFor(logging.DEBUG): + logging.debug('\n'.join(chain(('frame sent to TashTalk:',), (line for line in hexdump_lines(frame))))) + + def set_node_ids(self, ids): + '''Set the node IDs for which TashTalk should respond to RTS, CTS, and ENQ control frames.''' + id_bitmap = bytearray(32) + for id in ids: + byte_num, bit_num = divmod(id, 8) + id_bitmap[byte_num] |= (1 << bit_num) + self.serial_obj.write(b'\x02' + id_bitmap) + logging.debug('ID bitmap sent to TashTalk: %s', ', '.join(hex(id) for id in ids) if ids else '(empty)') diff --git a/tashtalkd/tashtalkd b/tashtalkd/tashtalkd new file mode 100644 index 0000000..26cc5e5 --- /dev/null +++ b/tashtalkd/tashtalkd @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# 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 . + + +import argparse +import logging +import sys + +import serial + +from daemon.protocol.ltoudp import LtoudpDaemon + + +def main(argv): + + parser = argparse.ArgumentParser(description='Userspace daemon for TashTalk.') + parser.add_argument('--device', '-d', metavar='DEVICE', required=True, help='serial device where TashTalk is connected') + parser.add_argument('--verbose', '-v', action='count', default=0, help='increase verbosity of logging') + args = parser.parse_args(argv[1:]) + + logging.basicConfig(level=logging.DEBUG if args.verbose > 1 else logging.INFO if args.verbose else logging.WARNING, + format='[%(levelname)s] %(message)s') + + serial_obj = serial.Serial(port=args.device, baudrate=1000000, timeout=0, rtscts=True) + daemon = LtoudpDaemon(serial_obj) + daemon.initialize() + daemon.run() + + +if __name__ == '__main__': sys.exit(main(sys.argv))