cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 2 | # |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 3 | # redirect data from a TCP/IP connection to a serial port and vice versa |
| 4 | # using RFC 2217 |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 5 | # |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 6 | # (C) 2009-2015 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 7 | # |
| 8 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 9 | |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 10 | import logging |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 11 | import os |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 12 | import socket |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 13 | import sys |
| 14 | import time |
| 15 | import threading |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 16 | import serial |
| 17 | import serial.rfc2217 |
| 18 | |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 19 | class Redirector(object): |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 20 | def __init__(self, serial_instance, socket, debug=False): |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 21 | self.serial = serial_instance |
| 22 | self.socket = socket |
| 23 | self._write_lock = threading.Lock() |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 24 | self.rfc2217 = serial.rfc2217.PortManager( |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 25 | self.serial, |
| 26 | self, |
| 27 | logger = logging.getLogger('rfc2217.server') if debug else None |
| 28 | ) |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 29 | self.log = logging.getLogger('redirector') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 30 | |
| 31 | def statusline_poller(self): |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 32 | self.log.debug('status line poll thread started') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 33 | while self.alive: |
| 34 | time.sleep(1) |
| 35 | self.rfc2217.check_modem_lines() |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 36 | self.log.debug('status line poll thread terminated') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 37 | |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 38 | def shortcircuit(self): |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 39 | """connect the serial port to the TCP port by copying everything |
| 40 | from one side to the other""" |
| 41 | self.alive = True |
| 42 | self.thread_read = threading.Thread(target=self.reader) |
| 43 | self.thread_read.setDaemon(True) |
| 44 | self.thread_read.setName('serial->socket') |
| 45 | self.thread_read.start() |
| 46 | self.thread_poll = threading.Thread(target=self.statusline_poller) |
| 47 | self.thread_poll.setDaemon(True) |
| 48 | self.thread_poll.setName('status line poll') |
| 49 | self.thread_poll.start() |
| 50 | self.writer() |
| 51 | |
| 52 | def reader(self): |
| 53 | """loop forever and copy serial->socket""" |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 54 | self.log.debug('reader thread started') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 55 | while self.alive: |
| 56 | try: |
| 57 | data = self.serial.read(1) # read one, blocking |
| 58 | n = self.serial.inWaiting() # look if there is more |
| 59 | if n: |
| 60 | data = data + self.serial.read(n) # and get as much as possible |
| 61 | if data: |
| 62 | # escape outgoing data when needed (Telnet IAC (0xff) character) |
| 63 | data = serial.to_bytes(self.rfc2217.escape(data)) |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 64 | with self._write_lock: |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 65 | self.socket.sendall(data) # send it over TCP |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 66 | except socket.error as msg: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 67 | self.log.error('%s' % (msg,)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 68 | # probably got disconnected |
| 69 | break |
| 70 | self.alive = False |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 71 | self.log.debug('reader thread terminated') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 72 | |
| 73 | def write(self, data): |
| 74 | """thread safe socket write with no data escaping. used to send telnet stuff""" |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 75 | with self._write_lock: |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 76 | self.socket.sendall(data) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 77 | |
| 78 | def writer(self): |
| 79 | """loop forever and copy socket->serial""" |
| 80 | while self.alive: |
| 81 | try: |
| 82 | data = self.socket.recv(1024) |
| 83 | if not data: |
| 84 | break |
| 85 | self.serial.write(serial.to_bytes(self.rfc2217.filter(data))) |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 86 | except socket.error as msg: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 87 | self.log.error('%s' % (msg,)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 88 | # probably got disconnected |
| 89 | break |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 90 | self.stop() |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 91 | |
| 92 | def stop(self): |
| 93 | """Stop copying""" |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 94 | self.log.debug('stopping') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 95 | if self.alive: |
| 96 | self.alive = False |
| 97 | self.thread_read.join() |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 98 | self.thread_poll.join() |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | if __name__ == '__main__': |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 102 | import argparse |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 103 | |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 104 | parser = argparse.ArgumentParser( |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 105 | description = "RFC 2217 Serial to Network (TCP/IP) redirector.", |
| 106 | epilog = """\ |
| 107 | NOTE: no security measures are implemented. Anyone can remotely connect |
| 108 | to this service over the network. |
| 109 | |
| 110 | Only one connection at once is supported. When the connection is terminated |
| 111 | it waits for the next connect. |
| 112 | """) |
| 113 | |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 114 | parser.add_argument('SERIALPORT') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 115 | |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 116 | parser.add_argument('-p', '--localport', |
| 117 | type=int, |
| 118 | help='local TCP port, default: %(default)s', |
| 119 | metavar='TCPPORT', |
| 120 | default=2217 |
| 121 | ) |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 122 | |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 123 | parser.add_argument('-v', '--verbose', |
| 124 | dest='verbosity', |
| 125 | action='count', |
| 126 | help='print more diagnostic messages (option can be given multiple times)', |
| 127 | default=0 |
| 128 | ) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 129 | |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 130 | args = parser.parse_args() |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 131 | |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 132 | if args.verbosity > 3: |
| 133 | args.verbosity = 3 |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 134 | level = ( |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 135 | logging.WARNING, |
| 136 | logging.INFO, |
| 137 | logging.DEBUG, |
| 138 | logging.NOTSET, |
| 139 | )[args.verbosity] |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 140 | logging.basicConfig(level=logging.INFO) |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 141 | logging.getLogger('root').setLevel(logging.INFO) |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 142 | logging.getLogger('rfc2217').setLevel(level) |
| 143 | |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 144 | # connect to serial port |
Chris Liechti | cd42db9 | 2015-08-17 03:24:34 +0200 | [diff] [blame^] | 145 | ser = serial.serial_for_url(args.SERIALPORT, do_not_open=True) |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 146 | ser.timeout = 3 # required so that the reader thread can exit |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 147 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 148 | logging.info("RFC 2217 TCP/IP to Serial redirector - type Ctrl-C / BREAK to quit") |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 149 | |
| 150 | try: |
| 151 | ser.open() |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 152 | except serial.SerialException as e: |
| 153 | logging.error("Could not open serial port %s: %s" % (ser.name, e)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 154 | sys.exit(1) |
| 155 | |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 156 | logging.info("Serving serial port: %s" % (ser.name,)) |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 157 | settings = ser.getSettingsDict() |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 158 | # reset control line as no _remote_ "terminal" has been connected yet |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 159 | ser.setDTR(False) |
| 160 | ser.setRTS(False) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 161 | |
| 162 | srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 163 | srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 164 | srv.bind(('', args.localport)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 165 | srv.listen(1) |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 166 | logging.info("TCP/IP port: %s" % (args.localport,)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 167 | while True: |
| 168 | try: |
| 169 | connection, addr = srv.accept() |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 170 | logging.info('Connected by %s:%s' % (addr[0], addr[1])) |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 171 | connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 172 | ser.setRTS(True) |
| 173 | ser.setDTR(True) |
| 174 | # enter network <-> serial loop |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 175 | r = Redirector( |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 176 | ser, |
| 177 | connection, |
| 178 | args.verbosity > 0 |
| 179 | ) |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 180 | try: |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 181 | r.shortcircuit() |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 182 | finally: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 183 | logging.info('Disconnected') |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 184 | r.stop() |
| 185 | connection.close() |
| 186 | ser.setDTR(False) |
| 187 | ser.setRTS(False) |
Chris Liechti | 34290f4 | 2015-08-17 03:08:24 +0200 | [diff] [blame] | 188 | # Restore port settings (may have been changed by RFC 2217 |
| 189 | # capable client) |
| 190 | ser.applySettingsDict(settings) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 191 | except KeyboardInterrupt: |
| 192 | break |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 193 | except socket.error as msg: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 194 | logging.error('%s' % (msg,)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 195 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 196 | logging.info('--- exit ---') |