cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 1 | #! python |
| 2 | # |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 3 | # This module implements a RFC2217 compatible client. RF2217 descibes a |
| 4 | # protocol to access serial ports over TCP/IP and allows setting the baud rate, |
| 5 | # modem control lines etc. |
| 6 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 7 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 8 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 9 | # |
| 10 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 11 | |
| 12 | # TODO: |
| 13 | # - setting control line -> answer is not checked (had problems with one of the |
| 14 | # severs). consider implementing a compatibility mode flag to make check |
| 15 | # conditional |
| 16 | # - write timeout not implemented at all |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 17 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 18 | # ########################################################################### |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 19 | # observations and issues with servers |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 20 | # =========================================================================== |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 21 | # sredird V2.2.1 |
| 22 | # - http://www.ibiblio.org/pub/Linux/system/serial/ sredird-2.2.2.tar.gz |
| 23 | # - does not acknowledge SET_CONTROL (RTS/DTR) correctly, always responding |
| 24 | # [105 1] instead of the actual value. |
| 25 | # - SET_BAUDRATE answer contains 4 extra null bytes -> probably for larger |
| 26 | # numbers than 2**32? |
| 27 | # - To get the signature [COM_PORT_OPTION 0] has to be sent. |
| 28 | # - run a server: while true; do nc -l -p 7000 -c "sredird debug /dev/ttyUSB0 /var/lock/sredir"; done |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 29 | # =========================================================================== |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 30 | # telnetcpcd (untested) |
| 31 | # - http://ftp.wayne.edu/kermit/sredird/telnetcpcd-1.09.tar.gz |
| 32 | # - To get the signature [COM_PORT_OPTION] w/o data has to be sent. |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 33 | # =========================================================================== |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 34 | # ser2net |
| 35 | # - does not negotiate BINARY or COM_PORT_OPTION for his side but at least |
| 36 | # acknowledges that the client activates these options |
| 37 | # - The configuration may be that the server prints a banner. As this client |
| 38 | # implementation does a flushInput on connect, this banner is hidden from |
| 39 | # the user application. |
| 40 | # - NOTIFY_MODEMSTATE: the poll interval of the server seems to be one |
| 41 | # second. |
| 42 | # - To get the signature [COM_PORT_OPTION 0] has to be sent. |
| 43 | # - run a server: run ser2net daemon, in /etc/ser2net.conf: |
| 44 | # 2000:telnet:0:/dev/ttyS0:9600 remctl banner |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 45 | # ########################################################################### |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 46 | |
| 47 | # How to identify ports? pySerial might want to support other protocols in the |
| 48 | # future, so lets use an URL scheme. |
| 49 | # for RFC2217 compliant servers we will use this: |
Chris Liechti | 142ae56 | 2015-08-23 01:11:06 +0200 | [diff] [blame] | 50 | # rfc2217://<host>:<port>[?option[&option...]] |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 51 | # |
| 52 | # options: |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 53 | # - "logging" set log level print diagnostic messages (e.g. "logging=debug") |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 54 | # - "ign_set_control": do not look at the answers to SET_CONTROL |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 55 | # - "poll_modem": issue NOTIFY_MODEMSTATE requests when CTS/DTR/RI/CD is read. |
| 56 | # Without this option it expects that the server sends notifications |
| 57 | # automatically on change (which most servers do and is according to the |
| 58 | # RFC). |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 59 | # the order of the options is not relevant |
| 60 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 61 | import logging |
Chris Liechti | c4bca9e | 2015-08-07 14:40:41 +0200 | [diff] [blame] | 62 | import socket |
| 63 | import struct |
| 64 | import threading |
| 65 | import time |
| 66 | try: |
| 67 | import urlparse |
| 68 | except ImportError: |
| 69 | import urllib.parse as urlparse |
Chris Liechti | d214600 | 2015-08-04 16:57:16 +0200 | [diff] [blame] | 70 | try: |
| 71 | import Queue |
| 72 | except ImportError: |
| 73 | import queue as Queue |
| 74 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 75 | import serial |
Chris Liechti | 8f6d3d0 | 2016-08-31 00:46:50 +0200 | [diff] [blame] | 76 | from serial.serialutil import SerialBase, SerialException, to_bytes, \ |
| 77 | iterbytes, portNotOpenError, Timeout |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 78 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 79 | # port string is expected to be something like this: |
| 80 | # rfc2217://host:port |
| 81 | # host may be an IP or including domain, whatever. |
| 82 | # port is 0...65535 |
| 83 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 84 | # map log level names to constants. used in from_url() |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 85 | LOGGER_LEVELS = { |
Chris Liechti | cb6ce1b | 2016-02-02 01:53:56 +0100 | [diff] [blame] | 86 | 'debug': logging.DEBUG, |
| 87 | 'info': logging.INFO, |
| 88 | 'warning': logging.WARNING, |
| 89 | 'error': logging.ERROR, |
| 90 | } |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 91 | |
| 92 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 93 | # telnet protocol characters |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 94 | SE = b'\xf0' # Subnegotiation End |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 95 | NOP = b'\xf1' # No Operation |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 96 | DM = b'\xf2' # Data Mark |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 97 | BRK = b'\xf3' # Break |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 98 | IP = b'\xf4' # Interrupt process |
| 99 | AO = b'\xf5' # Abort output |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 100 | AYT = b'\xf6' # Are You There |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 101 | EC = b'\xf7' # Erase Character |
| 102 | EL = b'\xf8' # Erase Line |
| 103 | GA = b'\xf9' # Go Ahead |
| 104 | SB = b'\xfa' # Subnegotiation Begin |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 105 | WILL = b'\xfb' |
| 106 | WONT = b'\xfc' |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 107 | DO = b'\xfd' |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 108 | DONT = b'\xfe' |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 109 | IAC = b'\xff' # Interpret As Command |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 110 | IAC_DOUBLED = b'\xff\xff' |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 111 | |
| 112 | # selected telnet options |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 113 | BINARY = b'\x00' # 8-bit data path |
| 114 | ECHO = b'\x01' # echo |
| 115 | SGA = b'\x03' # suppress go ahead |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 116 | |
| 117 | # RFC2217 |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 118 | COM_PORT_OPTION = b'\x2c' |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 119 | |
| 120 | # Client to Access Server |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 121 | SET_BAUDRATE = b'\x01' |
| 122 | SET_DATASIZE = b'\x02' |
| 123 | SET_PARITY = b'\x03' |
| 124 | SET_STOPSIZE = b'\x04' |
| 125 | SET_CONTROL = b'\x05' |
| 126 | NOTIFY_LINESTATE = b'\x06' |
| 127 | NOTIFY_MODEMSTATE = b'\x07' |
| 128 | FLOWCONTROL_SUSPEND = b'\x08' |
| 129 | FLOWCONTROL_RESUME = b'\x09' |
| 130 | SET_LINESTATE_MASK = b'\x0a' |
| 131 | SET_MODEMSTATE_MASK = b'\x0b' |
| 132 | PURGE_DATA = b'\x0c' |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 133 | |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 134 | SERVER_SET_BAUDRATE = b'\x65' |
| 135 | SERVER_SET_DATASIZE = b'\x66' |
| 136 | SERVER_SET_PARITY = b'\x67' |
| 137 | SERVER_SET_STOPSIZE = b'\x68' |
| 138 | SERVER_SET_CONTROL = b'\x69' |
| 139 | SERVER_NOTIFY_LINESTATE = b'\x6a' |
| 140 | SERVER_NOTIFY_MODEMSTATE = b'\x6b' |
| 141 | SERVER_FLOWCONTROL_SUSPEND = b'\x6c' |
| 142 | SERVER_FLOWCONTROL_RESUME = b'\x6d' |
| 143 | SERVER_SET_LINESTATE_MASK = b'\x6e' |
| 144 | SERVER_SET_MODEMSTATE_MASK = b'\x6f' |
| 145 | SERVER_PURGE_DATA = b'\x70' |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 146 | |
| 147 | RFC2217_ANSWER_MAP = { |
| 148 | SET_BAUDRATE: SERVER_SET_BAUDRATE, |
| 149 | SET_DATASIZE: SERVER_SET_DATASIZE, |
| 150 | SET_PARITY: SERVER_SET_PARITY, |
| 151 | SET_STOPSIZE: SERVER_SET_STOPSIZE, |
| 152 | SET_CONTROL: SERVER_SET_CONTROL, |
| 153 | NOTIFY_LINESTATE: SERVER_NOTIFY_LINESTATE, |
| 154 | NOTIFY_MODEMSTATE: SERVER_NOTIFY_MODEMSTATE, |
| 155 | FLOWCONTROL_SUSPEND: SERVER_FLOWCONTROL_SUSPEND, |
| 156 | FLOWCONTROL_RESUME: SERVER_FLOWCONTROL_RESUME, |
| 157 | SET_LINESTATE_MASK: SERVER_SET_LINESTATE_MASK, |
| 158 | SET_MODEMSTATE_MASK: SERVER_SET_MODEMSTATE_MASK, |
| 159 | PURGE_DATA: SERVER_PURGE_DATA, |
| 160 | } |
| 161 | |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 162 | SET_CONTROL_REQ_FLOW_SETTING = b'\x00' # Request Com Port Flow Control Setting (outbound/both) |
| 163 | SET_CONTROL_USE_NO_FLOW_CONTROL = b'\x01' # Use No Flow Control (outbound/both) |
| 164 | SET_CONTROL_USE_SW_FLOW_CONTROL = b'\x02' # Use XON/XOFF Flow Control (outbound/both) |
| 165 | SET_CONTROL_USE_HW_FLOW_CONTROL = b'\x03' # Use HARDWARE Flow Control (outbound/both) |
| 166 | SET_CONTROL_REQ_BREAK_STATE = b'\x04' # Request BREAK State |
| 167 | SET_CONTROL_BREAK_ON = b'\x05' # Set BREAK State ON |
| 168 | SET_CONTROL_BREAK_OFF = b'\x06' # Set BREAK State OFF |
| 169 | SET_CONTROL_REQ_DTR = b'\x07' # Request DTR Signal State |
| 170 | SET_CONTROL_DTR_ON = b'\x08' # Set DTR Signal State ON |
| 171 | SET_CONTROL_DTR_OFF = b'\x09' # Set DTR Signal State OFF |
| 172 | SET_CONTROL_REQ_RTS = b'\x0a' # Request RTS Signal State |
| 173 | SET_CONTROL_RTS_ON = b'\x0b' # Set RTS Signal State ON |
| 174 | SET_CONTROL_RTS_OFF = b'\x0c' # Set RTS Signal State OFF |
| 175 | SET_CONTROL_REQ_FLOW_SETTING_IN = b'\x0d' # Request Com Port Flow Control Setting (inbound) |
| 176 | SET_CONTROL_USE_NO_FLOW_CONTROL_IN = b'\x0e' # Use No Flow Control (inbound) |
| 177 | SET_CONTROL_USE_SW_FLOW_CONTOL_IN = b'\x0f' # Use XON/XOFF Flow Control (inbound) |
| 178 | SET_CONTROL_USE_HW_FLOW_CONTOL_IN = b'\x10' # Use HARDWARE Flow Control (inbound) |
| 179 | SET_CONTROL_USE_DCD_FLOW_CONTROL = b'\x11' # Use DCD Flow Control (outbound/both) |
| 180 | SET_CONTROL_USE_DTR_FLOW_CONTROL = b'\x12' # Use DTR Flow Control (inbound) |
| 181 | SET_CONTROL_USE_DSR_FLOW_CONTROL = b'\x13' # Use DSR Flow Control (outbound/both) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 182 | |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 183 | LINESTATE_MASK_TIMEOUT = 128 # Time-out Error |
| 184 | LINESTATE_MASK_SHIFTREG_EMPTY = 64 # Transfer Shift Register Empty |
| 185 | LINESTATE_MASK_TRANSREG_EMPTY = 32 # Transfer Holding Register Empty |
| 186 | LINESTATE_MASK_BREAK_DETECT = 16 # Break-detect Error |
| 187 | LINESTATE_MASK_FRAMING_ERROR = 8 # Framing Error |
| 188 | LINESTATE_MASK_PARTIY_ERROR = 4 # Parity Error |
| 189 | LINESTATE_MASK_OVERRUN_ERROR = 2 # Overrun Error |
| 190 | LINESTATE_MASK_DATA_READY = 1 # Data Ready |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 191 | |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 192 | MODEMSTATE_MASK_CD = 128 # Receive Line Signal Detect (also known as Carrier Detect) |
| 193 | MODEMSTATE_MASK_RI = 64 # Ring Indicator |
| 194 | MODEMSTATE_MASK_DSR = 32 # Data-Set-Ready Signal State |
| 195 | MODEMSTATE_MASK_CTS = 16 # Clear-To-Send Signal State |
| 196 | MODEMSTATE_MASK_CD_CHANGE = 8 # Delta Receive Line Signal Detect |
| 197 | MODEMSTATE_MASK_RI_CHANGE = 4 # Trailing-edge Ring Detector |
| 198 | MODEMSTATE_MASK_DSR_CHANGE = 2 # Delta Data-Set-Ready |
| 199 | MODEMSTATE_MASK_CTS_CHANGE = 1 # Delta Clear-To-Send |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 200 | |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 201 | PURGE_RECEIVE_BUFFER = b'\x01' # Purge access server receive data buffer |
| 202 | PURGE_TRANSMIT_BUFFER = b'\x02' # Purge access server transmit data buffer |
Chris Liechti | 70ca49c | 2016-02-07 23:54:03 +0100 | [diff] [blame] | 203 | PURGE_BOTH_BUFFERS = b'\x03' # Purge both the access server receive data |
| 204 | # buffer and the access server transmit data buffer |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 205 | |
| 206 | |
| 207 | RFC2217_PARITY_MAP = { |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 208 | serial.PARITY_NONE: 1, |
| 209 | serial.PARITY_ODD: 2, |
| 210 | serial.PARITY_EVEN: 3, |
| 211 | serial.PARITY_MARK: 4, |
| 212 | serial.PARITY_SPACE: 5, |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 213 | } |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 214 | RFC2217_REVERSE_PARITY_MAP = dict((v, k) for k, v in RFC2217_PARITY_MAP.items()) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 215 | |
| 216 | RFC2217_STOPBIT_MAP = { |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 217 | serial.STOPBITS_ONE: 1, |
| 218 | serial.STOPBITS_ONE_POINT_FIVE: 3, |
| 219 | serial.STOPBITS_TWO: 2, |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 220 | } |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 221 | RFC2217_REVERSE_STOPBIT_MAP = dict((v, k) for k, v in RFC2217_STOPBIT_MAP.items()) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 222 | |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 223 | # Telnet filter states |
| 224 | M_NORMAL = 0 |
| 225 | M_IAC_SEEN = 1 |
| 226 | M_NEGOTIATE = 2 |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 227 | |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 228 | # TelnetOption and TelnetSubnegotiation states |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 229 | REQUESTED = 'REQUESTED' |
| 230 | ACTIVE = 'ACTIVE' |
| 231 | INACTIVE = 'INACTIVE' |
| 232 | REALLY_INACTIVE = 'REALLY_INACTIVE' |
| 233 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 234 | |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 235 | class TelnetOption(object): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 236 | """Manage a single telnet option, keeps track of DO/DONT WILL/WONT.""" |
| 237 | |
Chris Liechti | 70ca49c | 2016-02-07 23:54:03 +0100 | [diff] [blame] | 238 | def __init__(self, connection, name, option, send_yes, send_no, ack_yes, |
| 239 | ack_no, initial_state, activation_callback=None): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 240 | """\ |
| 241 | Initialize option. |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 242 | :param connection: connection used to transmit answers |
| 243 | :param name: a readable name for debug outputs |
| 244 | :param send_yes: what to send when option is to be enabled. |
| 245 | :param send_no: what to send when option is to be disabled. |
| 246 | :param ack_yes: what to expect when remote agrees on option. |
| 247 | :param ack_no: what to expect when remote disagrees on option. |
| 248 | :param initial_state: options initialized with REQUESTED are tried to |
| 249 | be enabled on startup. use INACTIVE for all others. |
| 250 | """ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 251 | self.connection = connection |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 252 | self.name = name |
| 253 | self.option = option |
| 254 | self.send_yes = send_yes |
| 255 | self.send_no = send_no |
| 256 | self.ack_yes = ack_yes |
| 257 | self.ack_no = ack_no |
| 258 | self.state = initial_state |
| 259 | self.active = False |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 260 | self.activation_callback = activation_callback |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 261 | |
| 262 | def __repr__(self): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 263 | """String for debug outputs""" |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 264 | return "{o.name}:{o.active}({o.state})".format(o=self) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 265 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 266 | def process_incoming(self, command): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 267 | """\ |
| 268 | A DO/DONT/WILL/WONT was received for this option, update state and |
| 269 | answer when needed. |
| 270 | """ |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 271 | if command == self.ack_yes: |
| 272 | if self.state is REQUESTED: |
| 273 | self.state = ACTIVE |
| 274 | self.active = True |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 275 | if self.activation_callback is not None: |
| 276 | self.activation_callback() |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 277 | elif self.state is ACTIVE: |
| 278 | pass |
| 279 | elif self.state is INACTIVE: |
| 280 | self.state = ACTIVE |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 281 | self.connection.telnet_send_option(self.send_yes, self.option) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 282 | self.active = True |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 283 | if self.activation_callback is not None: |
| 284 | self.activation_callback() |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 285 | elif self.state is REALLY_INACTIVE: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 286 | self.connection.telnet_send_option(self.send_no, self.option) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 287 | else: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 288 | raise ValueError('option in illegal state {!r}'.format(self)) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 289 | elif command == self.ack_no: |
| 290 | if self.state is REQUESTED: |
| 291 | self.state = INACTIVE |
| 292 | self.active = False |
| 293 | elif self.state is ACTIVE: |
| 294 | self.state = INACTIVE |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 295 | self.connection.telnet_send_option(self.send_no, self.option) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 296 | self.active = False |
| 297 | elif self.state is INACTIVE: |
| 298 | pass |
| 299 | elif self.state is REALLY_INACTIVE: |
| 300 | pass |
| 301 | else: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 302 | raise ValueError('option in illegal state {!r}'.format(self)) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 303 | |
| 304 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 305 | class TelnetSubnegotiation(object): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 306 | """\ |
| 307 | A object to handle subnegotiation of options. In this case actually |
| 308 | sub-sub options for RFC 2217. It is used to track com port options. |
| 309 | """ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 310 | |
| 311 | def __init__(self, connection, name, option, ack_option=None): |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 312 | if ack_option is None: |
| 313 | ack_option = option |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 314 | self.connection = connection |
| 315 | self.name = name |
| 316 | self.option = option |
| 317 | self.value = None |
| 318 | self.ack_option = ack_option |
| 319 | self.state = INACTIVE |
| 320 | |
| 321 | def __repr__(self): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 322 | """String for debug outputs.""" |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 323 | return "{sn.name}:{sn.state}".format(sn=self) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 324 | |
| 325 | def set(self, value): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 326 | """\ |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 327 | Request a change of the value. a request is sent to the server. if |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 328 | the client needs to know if the change is performed he has to check the |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 329 | state of this object. |
| 330 | """ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 331 | self.value = value |
| 332 | self.state = REQUESTED |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 333 | self.connection.rfc2217_send_subnegotiation(self.option, self.value) |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 334 | if self.connection.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 335 | self.connection.logger.debug("SB Requesting {} -> {!r}".format(self.name, self.value)) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 336 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 337 | def is_ready(self): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 338 | """\ |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 339 | Check if answer from server has been received. when server rejects |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 340 | the change, raise a ValueError. |
| 341 | """ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 342 | if self.state == REALLY_INACTIVE: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 343 | raise ValueError("remote rejected value for option {!r}".format(self.name)) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 344 | return self.state == ACTIVE |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 345 | # add property to have a similar interface as TelnetOption |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 346 | active = property(is_ready) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 347 | |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 348 | def wait(self, timeout=3): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 349 | """\ |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 350 | Wait until the subnegotiation has been acknowledged or timeout. It |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 351 | can also throw a value error when the answer from the server does not |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 352 | match the value sent. |
| 353 | """ |
Chris Liechti | 8f6d3d0 | 2016-08-31 00:46:50 +0200 | [diff] [blame] | 354 | timeout_timer = Timeout(timeout) |
| 355 | while not timeout_timer.expired(): |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 356 | time.sleep(0.05) # prevent 100% CPU load |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 357 | if self.is_ready(): |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 358 | break |
| 359 | else: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 360 | raise SerialException("timeout while waiting for option {!r}".format(self.name)) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 361 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 362 | def check_answer(self, suboption): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 363 | """\ |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 364 | Check an incoming subnegotiation block. The parameter already has |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 365 | cut off the header like sub option number and com port option value. |
| 366 | """ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 367 | if self.value == suboption[:len(self.value)]: |
| 368 | self.state = ACTIVE |
| 369 | else: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 370 | # error propagation done in is_ready |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 371 | self.state = REALLY_INACTIVE |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 372 | if self.connection.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 373 | self.connection.logger.debug("SB Answer {} -> {!r} -> {}".format(self.name, suboption, self.state)) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 374 | |
| 375 | |
Chris Liechti | ef6b7b4 | 2015-08-06 22:19:26 +0200 | [diff] [blame] | 376 | class Serial(SerialBase): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 377 | """Serial port implementation for RFC 2217 remote serial ports.""" |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 378 | |
| 379 | BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 380 | 9600, 19200, 38400, 57600, 115200) |
| 381 | |
Chris Liechti | bb5341b | 2015-10-31 23:31:26 +0100 | [diff] [blame] | 382 | def __init__(self, *args, **kwargs): |
Chris Liechti | bb5341b | 2015-10-31 23:31:26 +0100 | [diff] [blame] | 383 | self._thread = None |
| 384 | self._socket = None |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 385 | self._linestate = 0 |
| 386 | self._modemstate = None |
Chris Liechti | f019725 | 2016-09-01 19:48:03 +0200 | [diff] [blame] | 387 | self._modemstate_timeout = Timeout(-1) |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 388 | self._remote_suspend_flow = False |
| 389 | self._write_lock = None |
| 390 | self.logger = None |
| 391 | self._ignore_set_control_answer = False |
| 392 | self._poll_modem_state = False |
| 393 | self._network_timeout = 3 |
| 394 | self._telnet_options = None |
| 395 | self._rfc2217_port_settings = None |
| 396 | self._rfc2217_options = None |
| 397 | self._read_buffer = None |
Chris Liechti | 8903261 | 2016-12-14 19:05:49 +0100 | [diff] [blame] | 398 | super(Serial, self).__init__(*args, **kwargs) # must be last call in case of auto-open |
Chris Liechti | bb5341b | 2015-10-31 23:31:26 +0100 | [diff] [blame] | 399 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 400 | def open(self): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 401 | """\ |
| 402 | Open port with current settings. This may throw a SerialException |
| 403 | if the port cannot be opened. |
| 404 | """ |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 405 | self.logger = None |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 406 | self._ignore_set_control_answer = False |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 407 | self._poll_modem_state = False |
cliechti | dfe2d27 | 2009-08-10 22:19:41 +0000 | [diff] [blame] | 408 | self._network_timeout = 3 |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 409 | if self._port is None: |
| 410 | raise SerialException("Port must be configured before it can be used.") |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 411 | if self.is_open: |
cliechti | 8f69e70 | 2011-03-19 00:22:32 +0000 | [diff] [blame] | 412 | raise SerialException("Port is already open.") |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 413 | try: |
Chris Liechti | 43b3b10 | 2016-06-07 21:31:47 +0200 | [diff] [blame] | 414 | self._socket = socket.create_connection(self.from_url(self.portstr), timeout=5) # XXX good value? |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 415 | self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 416 | except Exception as msg: |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 417 | self._socket = None |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 418 | raise SerialException("Could not open port {}: {}".format(self.portstr, msg)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 419 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 420 | # use a thread save queue as buffer. it also simplifies implementing |
| 421 | # the read timeout |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 422 | self._read_buffer = Queue.Queue() |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 423 | # to ensure that user writes does not interfere with internal |
| 424 | # telnet/rfc2217 options establish a lock |
| 425 | self._write_lock = threading.Lock() |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 426 | # name the following separately so that, below, a check can be easily done |
| 427 | mandadory_options = [ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 428 | TelnetOption(self, 'we-BINARY', BINARY, WILL, WONT, DO, DONT, INACTIVE), |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 429 | TelnetOption(self, 'we-RFC2217', COM_PORT_OPTION, WILL, WONT, DO, DONT, REQUESTED), |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 430 | ] |
| 431 | # all supported telnet options |
| 432 | self._telnet_options = [ |
cliechti | a29275e | 2009-08-03 00:08:04 +0000 | [diff] [blame] | 433 | TelnetOption(self, 'ECHO', ECHO, DO, DONT, WILL, WONT, REQUESTED), |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 434 | TelnetOption(self, 'we-SGA', SGA, WILL, WONT, DO, DONT, REQUESTED), |
| 435 | TelnetOption(self, 'they-SGA', SGA, DO, DONT, WILL, WONT, REQUESTED), |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 436 | TelnetOption(self, 'they-BINARY', BINARY, DO, DONT, WILL, WONT, INACTIVE), |
| 437 | TelnetOption(self, 'they-RFC2217', COM_PORT_OPTION, DO, DONT, WILL, WONT, REQUESTED), |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 438 | ] + mandadory_options |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 439 | # RFC 2217 specific states |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 440 | # COM port settings |
| 441 | self._rfc2217_port_settings = { |
| 442 | 'baudrate': TelnetSubnegotiation(self, 'baudrate', SET_BAUDRATE, SERVER_SET_BAUDRATE), |
| 443 | 'datasize': TelnetSubnegotiation(self, 'datasize', SET_DATASIZE, SERVER_SET_DATASIZE), |
| 444 | 'parity': TelnetSubnegotiation(self, 'parity', SET_PARITY, SERVER_SET_PARITY), |
| 445 | 'stopsize': TelnetSubnegotiation(self, 'stopsize', SET_STOPSIZE, SERVER_SET_STOPSIZE), |
Chris Liechti | cb6ce1b | 2016-02-02 01:53:56 +0100 | [diff] [blame] | 446 | } |
cliechti | cb20a4f | 2011-04-25 02:25:54 +0000 | [diff] [blame] | 447 | # There are more subnegotiation objects, combine all in one dictionary |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 448 | # for easy access |
| 449 | self._rfc2217_options = { |
| 450 | 'purge': TelnetSubnegotiation(self, 'purge', PURGE_DATA, SERVER_PURGE_DATA), |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 451 | 'control': TelnetSubnegotiation(self, 'control', SET_CONTROL, SERVER_SET_CONTROL), |
Chris Liechti | cb6ce1b | 2016-02-02 01:53:56 +0100 | [diff] [blame] | 452 | } |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 453 | self._rfc2217_options.update(self._rfc2217_port_settings) |
| 454 | # cache for line and modem states that the server sends to us |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 455 | self._linestate = 0 |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 456 | self._modemstate = None |
Chris Liechti | f019725 | 2016-09-01 19:48:03 +0200 | [diff] [blame] | 457 | self._modemstate_timeout = Timeout(-1) |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 458 | # RFC 2217 flow control between server and client |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 459 | self._remote_suspend_flow = False |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 460 | |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 461 | self.is_open = True |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 462 | self._thread = threading.Thread(target=self._telnet_read_loop) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 463 | self._thread.setDaemon(True) |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 464 | self._thread.setName('pySerial RFC 2217 reader thread for {}'.format(self._port)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 465 | self._thread.start() |
| 466 | |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 467 | try: # must clean-up if open fails |
| 468 | # negotiate Telnet/RFC 2217 -> send initial requests |
| 469 | for option in self._telnet_options: |
| 470 | if option.state is REQUESTED: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 471 | self.telnet_send_option(option.send_yes, option.option) |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 472 | # now wait until important options are negotiated |
Chris Liechti | 8f6d3d0 | 2016-08-31 00:46:50 +0200 | [diff] [blame] | 473 | timeout = Timeout(self._network_timeout) |
| 474 | while not timeout.expired(): |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 475 | time.sleep(0.05) # prevent 100% CPU load |
| 476 | if sum(o.active for o in mandadory_options) == sum(o.state != INACTIVE for o in mandadory_options): |
| 477 | break |
| 478 | else: |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 479 | raise SerialException( |
| 480 | "Remote does not seem to support RFC2217 or BINARY mode {!r}".format(mandadory_options)) |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 481 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 482 | self.logger.info("Negotiated options: {}".format(self._telnet_options)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 483 | |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 484 | # fine, go on, set RFC 2271 specific things |
| 485 | self._reconfigure_port() |
| 486 | # all things set up get, now a clean start |
| 487 | if not self._dsrdtr: |
| 488 | self._update_dtr_state() |
| 489 | if not self._rtscts: |
| 490 | self._update_rts_state() |
| 491 | self.reset_input_buffer() |
| 492 | self.reset_output_buffer() |
| 493 | except: |
| 494 | self.close() |
| 495 | raise |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 496 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 497 | def _reconfigure_port(self): |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 498 | """Set communication parameters on opened port.""" |
| 499 | if self._socket is None: |
| 500 | raise SerialException("Can only operate on open ports") |
| 501 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 502 | # if self._timeout != 0 and self._interCharTimeout is not None: |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 503 | # XXX |
| 504 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 505 | if self._write_timeout is not None: |
| 506 | raise NotImplementedError('write_timeout is currently not supported') |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 507 | # XXX |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 508 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 509 | # Setup the connection |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 510 | # to get good performance, all parameter changes are sent first... |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 511 | if not 0 < self._baudrate < 2 ** 32: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 512 | raise ValueError("invalid baudrate: {!r}".format(self._baudrate)) |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 513 | self._rfc2217_port_settings['baudrate'].set(struct.pack(b'!I', self._baudrate)) |
| 514 | self._rfc2217_port_settings['datasize'].set(struct.pack(b'!B', self._bytesize)) |
| 515 | self._rfc2217_port_settings['parity'].set(struct.pack(b'!B', RFC2217_PARITY_MAP[self._parity])) |
| 516 | self._rfc2217_port_settings['stopsize'].set(struct.pack(b'!B', RFC2217_STOPBIT_MAP[self._stopbits])) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 517 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 518 | # and now wait until parameters are active |
| 519 | items = self._rfc2217_port_settings.values() |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 520 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 521 | self.logger.debug("Negotiating settings: {}".format(items)) |
Chris Liechti | 8f6d3d0 | 2016-08-31 00:46:50 +0200 | [diff] [blame] | 522 | timeout = Timeout(self._network_timeout) |
| 523 | while not timeout.expired(): |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 524 | time.sleep(0.05) # prevent 100% CPU load |
| 525 | if sum(o.active for o in items) == len(items): |
| 526 | break |
| 527 | else: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 528 | raise SerialException("Remote does not accept parameter change (RFC2217): {!r}".format(items)) |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 529 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 530 | self.logger.info("Negotiated settings: {}".format(items)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 531 | |
| 532 | if self._rtscts and self._xonxoff: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 533 | raise ValueError('xonxoff and rtscts together are not supported') |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 534 | elif self._rtscts: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 535 | self.rfc2217_set_control(SET_CONTROL_USE_HW_FLOW_CONTROL) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 536 | elif self._xonxoff: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 537 | self.rfc2217_set_control(SET_CONTROL_USE_SW_FLOW_CONTROL) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 538 | else: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 539 | self.rfc2217_set_control(SET_CONTROL_USE_NO_FLOW_CONTROL) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 540 | |
| 541 | def close(self): |
| 542 | """Close port""" |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 543 | self.is_open = False |
| 544 | if self._socket: |
| 545 | try: |
| 546 | self._socket.shutdown(socket.SHUT_RDWR) |
| 547 | self._socket.close() |
| 548 | except: |
| 549 | # ignore errors. |
| 550 | pass |
| 551 | if self._thread: |
| 552 | self._thread.join(7) # XXX more than socket timeout |
| 553 | self._thread = None |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 554 | # in case of quick reconnects, give the server some time |
| 555 | time.sleep(0.3) |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 556 | self._socket = None |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 557 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 558 | def from_url(self, url): |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 559 | """\ |
| 560 | extract host and port from an URL string, other settings are extracted |
| 561 | an stored in instance |
| 562 | """ |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 563 | parts = urlparse.urlsplit(url) |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 564 | if parts.scheme != "rfc2217": |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 565 | raise SerialException( |
| 566 | 'expected a string in the form ' |
| 567 | '"rfc2217://<host>:<port>[?option[&option...]]": ' |
| 568 | 'not starting with rfc2217:// ({!r})'.format(parts.scheme)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 569 | try: |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 570 | # process options now, directly altering self |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 571 | for option, values in urlparse.parse_qs(parts.query, True).items(): |
| 572 | if option == 'logging': |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 573 | logging.basicConfig() # XXX is that good to call it here? |
| 574 | self.logger = logging.getLogger('pySerial.rfc2217') |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 575 | self.logger.setLevel(LOGGER_LEVELS[values[0]]) |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 576 | self.logger.debug('enabled logging') |
| 577 | elif option == 'ign_set_control': |
| 578 | self._ignore_set_control_answer = True |
| 579 | elif option == 'poll_modem': |
| 580 | self._poll_modem_state = True |
| 581 | elif option == 'timeout': |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 582 | self._network_timeout = float(values[0]) |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 583 | else: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 584 | raise ValueError('unknown option: {!r}'.format(option)) |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 585 | if not 0 <= parts.port < 65536: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 586 | raise ValueError("port not in range 0...65535") |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 587 | except ValueError as e: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 588 | raise SerialException( |
| 589 | 'expected a string in the form ' |
| 590 | '"rfc2217://<host>:<port>[?option[&option...]]": {}'.format(e)) |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 591 | return (parts.hostname, parts.port) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 592 | |
| 593 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 594 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 595 | @property |
| 596 | def in_waiting(self): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 597 | """Return the number of bytes currently in the input buffer.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 598 | if not self.is_open: |
| 599 | raise portNotOpenError |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 600 | return self._read_buffer.qsize() |
| 601 | |
| 602 | def read(self, size=1): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 603 | """\ |
| 604 | Read size bytes from the serial port. If a timeout is set it may |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 605 | return less characters as requested. With no timeout it will block |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 606 | until the requested number of bytes is read. |
| 607 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 608 | if not self.is_open: |
| 609 | raise portNotOpenError |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 610 | data = bytearray() |
| 611 | try: |
Chris Liechti | 129aca6 | 2016-12-21 03:19:30 +0100 | [diff] [blame] | 612 | timeout = Timeout(self._timeout) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 613 | while len(data) < size: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 614 | if self._thread is None: |
| 615 | raise SerialException('connection failed (reader thread died)') |
Chris Liechti | 129aca6 | 2016-12-21 03:19:30 +0100 | [diff] [blame] | 616 | data += self._read_buffer.get(True, timeout.time_left()) |
| 617 | if timeout.expired(): |
| 618 | break |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 619 | except Queue.Empty: # -> timeout |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 620 | pass |
| 621 | return bytes(data) |
| 622 | |
| 623 | def write(self, data): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 624 | """\ |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 625 | Output the given byte string over the serial port. Can block if the |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 626 | connection is blocked. May raise SerialException if the connection is |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 627 | closed. |
| 628 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 629 | if not self.is_open: |
| 630 | raise portNotOpenError |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 631 | with self._write_lock: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 632 | try: |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 633 | self._socket.sendall(to_bytes(data).replace(IAC, IAC_DOUBLED)) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 634 | except socket.error as e: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 635 | raise SerialException("connection failed (socket error): {}".format(e)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 636 | return len(data) |
| 637 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 638 | def reset_input_buffer(self): |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 639 | """Clear input buffer, discarding all that is in the buffer.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 640 | if not self.is_open: |
| 641 | raise portNotOpenError |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 642 | self.rfc2217_send_purge(PURGE_RECEIVE_BUFFER) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 643 | # empty read buffer |
| 644 | while self._read_buffer.qsize(): |
| 645 | self._read_buffer.get(False) |
| 646 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 647 | def reset_output_buffer(self): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 648 | """\ |
| 649 | Clear output buffer, aborting the current output and |
| 650 | discarding all that is in the buffer. |
| 651 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 652 | if not self.is_open: |
| 653 | raise portNotOpenError |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 654 | self.rfc2217_send_purge(PURGE_TRANSMIT_BUFFER) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 655 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 656 | def _update_break_state(self): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 657 | """\ |
| 658 | Set break: Controls TXD. When active, to transmitting is |
| 659 | possible. |
| 660 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 661 | if not self.is_open: |
| 662 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 663 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 664 | self.logger.info('set BREAK to {}'.format('active' if self._break_state else 'inactive')) |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 665 | if self._break_state: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 666 | self.rfc2217_set_control(SET_CONTROL_BREAK_ON) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 667 | else: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 668 | self.rfc2217_set_control(SET_CONTROL_BREAK_OFF) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 669 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 670 | def _update_rts_state(self): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 671 | """Set terminal status line: Request To Send.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 672 | if not self.is_open: |
| 673 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 674 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 675 | self.logger.info('set RTS to {}'.format('active' if self._rts_state else 'inactive')) |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 676 | if self._rts_state: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 677 | self.rfc2217_set_control(SET_CONTROL_RTS_ON) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 678 | else: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 679 | self.rfc2217_set_control(SET_CONTROL_RTS_OFF) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 680 | |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 681 | def _update_dtr_state(self): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 682 | """Set terminal status line: Data Terminal Ready.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 683 | if not self.is_open: |
| 684 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 685 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 686 | self.logger.info('set DTR to {}'.format('active' if self._dtr_state else 'inactive')) |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 687 | if self._dtr_state: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 688 | self.rfc2217_set_control(SET_CONTROL_DTR_ON) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 689 | else: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 690 | self.rfc2217_set_control(SET_CONTROL_DTR_OFF) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 691 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 692 | @property |
| 693 | def cts(self): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 694 | """Read terminal status line: Clear To Send.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 695 | if not self.is_open: |
| 696 | raise portNotOpenError |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 697 | return bool(self.get_modem_state() & MODEMSTATE_MASK_CTS) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 698 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 699 | @property |
| 700 | def dsr(self): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 701 | """Read terminal status line: Data Set Ready.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 702 | if not self.is_open: |
| 703 | raise portNotOpenError |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 704 | return bool(self.get_modem_state() & MODEMSTATE_MASK_DSR) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 705 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 706 | @property |
| 707 | def ri(self): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 708 | """Read terminal status line: Ring Indicator.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 709 | if not self.is_open: |
| 710 | raise portNotOpenError |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 711 | return bool(self.get_modem_state() & MODEMSTATE_MASK_RI) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 712 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 713 | @property |
| 714 | def cd(self): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 715 | """Read terminal status line: Carrier Detect.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 716 | if not self.is_open: |
| 717 | raise portNotOpenError |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 718 | return bool(self.get_modem_state() & MODEMSTATE_MASK_CD) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 719 | |
| 720 | # - - - platform specific - - - |
| 721 | # None so far |
| 722 | |
| 723 | # - - - RFC2217 specific - - - |
| 724 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 725 | def _telnet_read_loop(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 726 | """Read loop for the socket.""" |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 727 | mode = M_NORMAL |
| 728 | suboption = None |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 729 | try: |
Chris Liechti | 39d5217 | 2015-10-25 22:53:51 +0100 | [diff] [blame] | 730 | while self.is_open: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 731 | try: |
| 732 | data = self._socket.recv(1024) |
| 733 | except socket.timeout: |
| 734 | # just need to get out of recv form time to time to check if |
| 735 | # still alive |
| 736 | continue |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 737 | except socket.error as e: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 738 | # connection fails -> terminate loop |
cliechti | cb20a4f | 2011-04-25 02:25:54 +0000 | [diff] [blame] | 739 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 740 | self.logger.debug("socket error in reader thread: {}".format(e)) |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 741 | break |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 742 | if not data: |
| 743 | break # lost connection |
Chris Liechti | f99cd5c | 2015-08-13 22:54:16 +0200 | [diff] [blame] | 744 | for byte in iterbytes(data): |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 745 | if mode == M_NORMAL: |
| 746 | # interpret as command or as data |
| 747 | if byte == IAC: |
| 748 | mode = M_IAC_SEEN |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 749 | else: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 750 | # store data in read buffer or sub option buffer |
| 751 | # depending on state |
| 752 | if suboption is not None: |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 753 | suboption += byte |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 754 | else: |
| 755 | self._read_buffer.put(byte) |
| 756 | elif mode == M_IAC_SEEN: |
| 757 | if byte == IAC: |
| 758 | # interpret as command doubled -> insert character |
| 759 | # itself |
cliechti | f325c03 | 2009-12-25 16:09:49 +0000 | [diff] [blame] | 760 | if suboption is not None: |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 761 | suboption += IAC |
cliechti | f325c03 | 2009-12-25 16:09:49 +0000 | [diff] [blame] | 762 | else: |
| 763 | self._read_buffer.put(IAC) |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 764 | mode = M_NORMAL |
| 765 | elif byte == SB: |
| 766 | # sub option start |
| 767 | suboption = bytearray() |
| 768 | mode = M_NORMAL |
| 769 | elif byte == SE: |
| 770 | # sub option end -> process it now |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 771 | self._telnet_process_subnegotiation(bytes(suboption)) |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 772 | suboption = None |
| 773 | mode = M_NORMAL |
| 774 | elif byte in (DO, DONT, WILL, WONT): |
| 775 | # negotiation |
| 776 | telnet_command = byte |
| 777 | mode = M_NEGOTIATE |
| 778 | else: |
| 779 | # other telnet commands |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 780 | self._telnet_process_command(byte) |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 781 | mode = M_NORMAL |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 782 | elif mode == M_NEGOTIATE: # DO, DONT, WILL, WONT was received, option now following |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 783 | self._telnet_negotiate_option(telnet_command, byte) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 784 | mode = M_NORMAL |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 785 | finally: |
| 786 | self._thread = None |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 787 | if self.logger: |
| 788 | self.logger.debug("read thread terminated") |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 789 | |
| 790 | # - incoming telnet commands and options |
| 791 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 792 | def _telnet_process_command(self, command): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 793 | """Process commands other than DO, DONT, WILL, WONT.""" |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 794 | # Currently none. RFC2217 only uses negotiation and subnegotiation. |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 795 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 796 | self.logger.warning("ignoring Telnet command: {!r}".format(command)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 797 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 798 | def _telnet_negotiate_option(self, command, option): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 799 | """Process incoming DO, DONT, WILL, WONT.""" |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 800 | # check our registered telnet options and forward command to them |
| 801 | # they know themselves if they have to answer or not |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 802 | known = False |
| 803 | for item in self._telnet_options: |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 804 | # can have more than one match! as some options are duplicated for |
| 805 | # 'us' and 'them' |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 806 | if item.option == option: |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 807 | item.process_incoming(command) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 808 | known = True |
| 809 | if not known: |
| 810 | # handle unknown options |
| 811 | # only answer to positive requests and deny them |
| 812 | if command == WILL or command == DO: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 813 | self.telnet_send_option((DONT if command == WILL else WONT), option) |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 814 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 815 | self.logger.warning("rejected Telnet option: {!r}".format(option)) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 816 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 817 | def _telnet_process_subnegotiation(self, suboption): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 818 | """Process subnegotiation, the data between IAC SB and IAC SE.""" |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 819 | if suboption[0:1] == COM_PORT_OPTION: |
| 820 | if suboption[1:2] == SERVER_NOTIFY_LINESTATE and len(suboption) >= 3: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 821 | self._linestate = ord(suboption[2:3]) # ensure it is a number |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 822 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 823 | self.logger.info("NOTIFY_LINESTATE: {}".format(self._linestate)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 824 | elif suboption[1:2] == SERVER_NOTIFY_MODEMSTATE and len(suboption) >= 3: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 825 | self._modemstate = ord(suboption[2:3]) # ensure it is a number |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 826 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 827 | self.logger.info("NOTIFY_MODEMSTATE: {}".format(self._modemstate)) |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 828 | # update time when we think that a poll would make sense |
Chris Liechti | f019725 | 2016-09-01 19:48:03 +0200 | [diff] [blame] | 829 | self._modemstate_timeout.restart(0.3) |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 830 | elif suboption[1:2] == FLOWCONTROL_SUSPEND: |
| 831 | self._remote_suspend_flow = True |
| 832 | elif suboption[1:2] == FLOWCONTROL_RESUME: |
| 833 | self._remote_suspend_flow = False |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 834 | else: |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 835 | for item in self._rfc2217_options.values(): |
| 836 | if item.ack_option == suboption[1:2]: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 837 | #~ print "processing COM_PORT_OPTION: %r" % list(suboption[1:]) |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 838 | item.check_answer(bytes(suboption[2:])) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 839 | break |
| 840 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 841 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 842 | self.logger.warning("ignoring COM_PORT_OPTION: {!r}".format(suboption)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 843 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 844 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 845 | self.logger.warning("ignoring subnegotiation: {!r}".format(suboption)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 846 | |
| 847 | # - outgoing telnet commands and options |
| 848 | |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 849 | def _internal_raw_write(self, data): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 850 | """internal socket write with no data escaping. used to send telnet stuff.""" |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 851 | with self._write_lock: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 852 | self._socket.sendall(data) |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 853 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 854 | def telnet_send_option(self, action, option): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 855 | """Send DO, DONT, WILL, WONT.""" |
Greg Bowser | 77ca290 | 2016-11-22 13:04:16 -0500 | [diff] [blame] | 856 | self._internal_raw_write(IAC + action + option) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 857 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 858 | def rfc2217_send_subnegotiation(self, option, value=b''): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 859 | """Subnegotiation of RFC2217 parameters.""" |
cliechti | f325c03 | 2009-12-25 16:09:49 +0000 | [diff] [blame] | 860 | value = value.replace(IAC, IAC_DOUBLED) |
Greg Bowser | 77ca290 | 2016-11-22 13:04:16 -0500 | [diff] [blame] | 861 | self._internal_raw_write(IAC + SB + COM_PORT_OPTION + option + value + IAC + SE) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 862 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 863 | def rfc2217_send_purge(self, value): |
| 864 | """\ |
| 865 | Send purge request to the remote. |
| 866 | (PURGE_RECEIVE_BUFFER / PURGE_TRANSMIT_BUFFER / PURGE_BOTH_BUFFERS) |
| 867 | """ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 868 | item = self._rfc2217_options['purge'] |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 869 | item.set(value) # transmit desired purge type |
| 870 | item.wait(self._network_timeout) # wait for acknowledge from the server |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 871 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 872 | def rfc2217_set_control(self, value): |
| 873 | """transmit change of control line to remote""" |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 874 | item = self._rfc2217_options['control'] |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 875 | item.set(value) # transmit desired control type |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 876 | if self._ignore_set_control_answer: |
| 877 | # answers are ignored when option is set. compatibility mode for |
cliechti | cb20a4f | 2011-04-25 02:25:54 +0000 | [diff] [blame] | 878 | # servers that answer, but not the expected one... (or no answer |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame] | 879 | # at all) i.e. sredird |
| 880 | time.sleep(0.1) # this helps getting the unit tests passed |
| 881 | else: |
cliechti | dfe2d27 | 2009-08-10 22:19:41 +0000 | [diff] [blame] | 882 | item.wait(self._network_timeout) # wait for acknowledge from the server |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 883 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 884 | def rfc2217_flow_server_ready(self): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 885 | """\ |
| 886 | check if server is ready to receive data. block for some time when |
| 887 | not. |
| 888 | """ |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 889 | #~ if self._remote_suspend_flow: |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 890 | #~ wait--- |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 891 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 892 | def get_modem_state(self): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 893 | """\ |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 894 | get last modem state (cached value. If value is "old", request a new |
| 895 | one. This cache helps that we don't issue to many requests when e.g. all |
gesorthy | b5766f2 | 2017-03-23 08:35:14 +0100 | [diff] [blame] | 896 | status lines, one after the other is queried by the user (CTS, DSR |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 897 | etc.) |
| 898 | """ |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 899 | # active modem state polling enabled? is the value fresh enough? |
Chris Liechti | f019725 | 2016-09-01 19:48:03 +0200 | [diff] [blame] | 900 | if self._poll_modem_state and self._modemstate_timeout.expired(): |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 901 | if self.logger: |
| 902 | self.logger.debug('polling modem state') |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 903 | # when it is older, request an update |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 904 | self.rfc2217_send_subnegotiation(NOTIFY_MODEMSTATE) |
Chris Liechti | 8f6d3d0 | 2016-08-31 00:46:50 +0200 | [diff] [blame] | 905 | timeout = Timeout(self._network_timeout) |
| 906 | while not timeout.expired(): |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 907 | time.sleep(0.05) # prevent 100% CPU load |
| 908 | # when expiration time is updated, it means that there is a new |
| 909 | # value |
Chris Liechti | f019725 | 2016-09-01 19:48:03 +0200 | [diff] [blame] | 910 | if not self._modemstate_timeout.expired(): |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 911 | break |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 912 | else: |
| 913 | if self.logger: |
| 914 | self.logger.warning('poll for modem state failed') |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 915 | # even when there is a timeout, do not generate an error just |
| 916 | # return the last known value. this way we can support buggy |
| 917 | # servers that do not respond to polls, but send automatic |
| 918 | # updates. |
| 919 | if self._modemstate is not None: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 920 | if self.logger: |
| 921 | self.logger.debug('using cached modem state') |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 922 | return self._modemstate |
| 923 | else: |
| 924 | # never received a notification from the server |
cliechti | 8fb119c | 2009-08-05 23:39:45 +0000 | [diff] [blame] | 925 | raise SerialException("remote sends no NOTIFY_MODEMSTATE") |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 926 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 927 | |
cliechti | 595ed5b | 2009-08-10 01:43:32 +0000 | [diff] [blame] | 928 | ############################################################################# |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 929 | # The following is code that helps implementing an RFC 2217 server. |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 930 | |
cliechti | 8ccc2ff | 2009-08-05 12:44:46 +0000 | [diff] [blame] | 931 | class PortManager(object): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 932 | """\ |
| 933 | This class manages the state of Telnet and RFC 2217. It needs a serial |
cliechti | cb20a4f | 2011-04-25 02:25:54 +0000 | [diff] [blame] | 934 | instance and a connection to work with. Connection is expected to implement |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 935 | a (thread safe) write function, that writes the string to the network. |
| 936 | """ |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 937 | |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 938 | def __init__(self, serial_port, connection, logger=None): |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 939 | self.serial = serial_port |
| 940 | self.connection = connection |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 941 | self.logger = logger |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 942 | self._client_is_rfc2217 = False |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 943 | |
| 944 | # filter state machine |
| 945 | self.mode = M_NORMAL |
| 946 | self.suboption = None |
| 947 | self.telnet_command = None |
| 948 | |
| 949 | # states for modem/line control events |
| 950 | self.modemstate_mask = 255 |
| 951 | self.last_modemstate = None |
| 952 | self.linstate_mask = 0 |
| 953 | |
| 954 | # all supported telnet options |
| 955 | self._telnet_options = [ |
| 956 | TelnetOption(self, 'ECHO', ECHO, WILL, WONT, DO, DONT, REQUESTED), |
| 957 | TelnetOption(self, 'we-SGA', SGA, WILL, WONT, DO, DONT, REQUESTED), |
| 958 | TelnetOption(self, 'they-SGA', SGA, DO, DONT, WILL, WONT, INACTIVE), |
| 959 | TelnetOption(self, 'we-BINARY', BINARY, WILL, WONT, DO, DONT, INACTIVE), |
| 960 | TelnetOption(self, 'they-BINARY', BINARY, DO, DONT, WILL, WONT, REQUESTED), |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 961 | TelnetOption(self, 'we-RFC2217', COM_PORT_OPTION, WILL, WONT, DO, DONT, REQUESTED, self._client_ok), |
| 962 | TelnetOption(self, 'they-RFC2217', COM_PORT_OPTION, DO, DONT, WILL, WONT, INACTIVE, self._client_ok), |
Chris Liechti | cb6ce1b | 2016-02-02 01:53:56 +0100 | [diff] [blame] | 963 | ] |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 964 | |
| 965 | # negotiate Telnet/RFC2217 -> send initial requests |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 966 | if self.logger: |
| 967 | self.logger.debug("requesting initial Telnet/RFC 2217 options") |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 968 | for option in self._telnet_options: |
| 969 | if option.state is REQUESTED: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 970 | self.telnet_send_option(option.send_yes, option.option) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 971 | # issue 1st modem state notification |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 972 | |
| 973 | def _client_ok(self): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 974 | """\ |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 975 | callback of telnet option. It gets called when option is activated. |
| 976 | This one here is used to detect when the client agrees on RFC 2217. A |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 977 | flag is set so that other functions like check_modem_lines know if the |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 978 | client is OK. |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 979 | """ |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 980 | # The callback is used for we and they so if one party agrees, we're |
| 981 | # already happy. it seems not all servers do the negotiation correctly |
| 982 | # and i guess there are incorrect clients too.. so be happy if client |
| 983 | # answers one or the other positively. |
| 984 | self._client_is_rfc2217 = True |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 985 | if self.logger: |
| 986 | self.logger.info("client accepts RFC 2217") |
cliechti | 8fb119c | 2009-08-05 23:39:45 +0000 | [diff] [blame] | 987 | # this is to ensure that the client gets a notification, even if there |
| 988 | # was no change |
| 989 | self.check_modem_lines(force_notification=True) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 990 | |
| 991 | # - outgoing telnet commands and options |
| 992 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 993 | def telnet_send_option(self, action, option): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 994 | """Send DO, DONT, WILL, WONT.""" |
Greg Bowser | 77ca290 | 2016-11-22 13:04:16 -0500 | [diff] [blame] | 995 | self.connection.write(IAC + action + option) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 996 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 997 | def rfc2217_send_subnegotiation(self, option, value=b''): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 998 | """Subnegotiation of RFC 2217 parameters.""" |
cliechti | f325c03 | 2009-12-25 16:09:49 +0000 | [diff] [blame] | 999 | value = value.replace(IAC, IAC_DOUBLED) |
Greg Bowser | 77ca290 | 2016-11-22 13:04:16 -0500 | [diff] [blame] | 1000 | self.connection.write(IAC + SB + COM_PORT_OPTION + option + value + IAC + SE) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1001 | |
| 1002 | # - check modem lines, needs to be called periodically from user to |
| 1003 | # establish polling |
| 1004 | |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 1005 | def check_modem_lines(self, force_notification=False): |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1006 | """\ |
| 1007 | read control lines from serial port and compare the last value sent to remote. |
| 1008 | send updates on changes. |
| 1009 | """ |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1010 | modemstate = ( |
gesorthy | b5766f2 | 2017-03-23 08:35:14 +0100 | [diff] [blame] | 1011 | (self.serial.cts and MODEMSTATE_MASK_CTS) | |
| 1012 | (self.serial.dsr and MODEMSTATE_MASK_DSR) | |
| 1013 | (self.serial.ri and MODEMSTATE_MASK_RI) | |
| 1014 | (self.serial.cd and MODEMSTATE_MASK_CD)) |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 1015 | # check what has changed |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 1016 | deltas = modemstate ^ (self.last_modemstate or 0) # when last is None -> 0 |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 1017 | if deltas & MODEMSTATE_MASK_CTS: |
| 1018 | modemstate |= MODEMSTATE_MASK_CTS_CHANGE |
| 1019 | if deltas & MODEMSTATE_MASK_DSR: |
| 1020 | modemstate |= MODEMSTATE_MASK_DSR_CHANGE |
| 1021 | if deltas & MODEMSTATE_MASK_RI: |
| 1022 | modemstate |= MODEMSTATE_MASK_RI_CHANGE |
| 1023 | if deltas & MODEMSTATE_MASK_CD: |
| 1024 | modemstate |= MODEMSTATE_MASK_CD_CHANGE |
| 1025 | # if new state is different and the mask allows this change, send |
cliechti | 86b593e | 2009-08-05 16:28:12 +0000 | [diff] [blame] | 1026 | # notification. suppress notifications when client is not rfc2217 |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 1027 | if modemstate != self.last_modemstate or force_notification: |
cliechti | 8fb119c | 2009-08-05 23:39:45 +0000 | [diff] [blame] | 1028 | if (self._client_is_rfc2217 and (modemstate & self.modemstate_mask)) or force_notification: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1029 | self.rfc2217_send_subnegotiation( |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 1030 | SERVER_NOTIFY_MODEMSTATE, |
| 1031 | to_bytes([modemstate & self.modemstate_mask])) |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1032 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1033 | self.logger.info("NOTIFY_MODEMSTATE: {}".format(modemstate)) |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 1034 | # save last state, but forget about deltas. |
| 1035 | # otherwise it would also notify about changing deltas which is |
| 1036 | # probably not very useful |
| 1037 | self.last_modemstate = modemstate & 0xf0 |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1038 | |
cliechti | 32c1033 | 2009-08-05 13:23:43 +0000 | [diff] [blame] | 1039 | # - outgoing data escaping |
| 1040 | |
| 1041 | def escape(self, data): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 1042 | """\ |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 1043 | This generator function is for the user. All outgoing data has to be |
cliechti | cb20a4f | 2011-04-25 02:25:54 +0000 | [diff] [blame] | 1044 | properly escaped, so that no IAC character in the data stream messes up |
| 1045 | the Telnet state machine in the server. |
cliechti | 32c1033 | 2009-08-05 13:23:43 +0000 | [diff] [blame] | 1046 | |
| 1047 | socket.sendall(escape(data)) |
| 1048 | """ |
Chris Liechti | b15dc05 | 2015-10-19 23:17:16 +0200 | [diff] [blame] | 1049 | for byte in iterbytes(data): |
cliechti | 32c1033 | 2009-08-05 13:23:43 +0000 | [diff] [blame] | 1050 | if byte == IAC: |
| 1051 | yield IAC |
| 1052 | yield IAC |
| 1053 | else: |
| 1054 | yield byte |
| 1055 | |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1056 | # - incoming data filter |
| 1057 | |
| 1058 | def filter(self, data): |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 1059 | """\ |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 1060 | Handle a bunch of incoming bytes. This is a generator. It will yield |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 1061 | all characters not of interest for Telnet/RFC 2217. |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1062 | |
| 1063 | The idea is that the reader thread pushes data from the socket through |
| 1064 | this filter: |
| 1065 | |
| 1066 | for byte in filter(socket.recv(1024)): |
| 1067 | # do things like CR/LF conversion/whatever |
| 1068 | # and write data to the serial port |
| 1069 | serial.write(byte) |
| 1070 | |
| 1071 | (socket error handling code left as exercise for the reader) |
| 1072 | """ |
Chris Liechti | f99cd5c | 2015-08-13 22:54:16 +0200 | [diff] [blame] | 1073 | for byte in iterbytes(data): |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1074 | if self.mode == M_NORMAL: |
| 1075 | # interpret as command or as data |
| 1076 | if byte == IAC: |
| 1077 | self.mode = M_IAC_SEEN |
| 1078 | else: |
| 1079 | # store data in sub option buffer or pass it to our |
| 1080 | # consumer depending on state |
| 1081 | if self.suboption is not None: |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 1082 | self.suboption += byte |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1083 | else: |
| 1084 | yield byte |
| 1085 | elif self.mode == M_IAC_SEEN: |
| 1086 | if byte == IAC: |
| 1087 | # interpret as command doubled -> insert character |
| 1088 | # itself |
cliechti | f325c03 | 2009-12-25 16:09:49 +0000 | [diff] [blame] | 1089 | if self.suboption is not None: |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 1090 | self.suboption += byte |
cliechti | f325c03 | 2009-12-25 16:09:49 +0000 | [diff] [blame] | 1091 | else: |
| 1092 | yield byte |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1093 | self.mode = M_NORMAL |
| 1094 | elif byte == SB: |
| 1095 | # sub option start |
| 1096 | self.suboption = bytearray() |
| 1097 | self.mode = M_NORMAL |
| 1098 | elif byte == SE: |
| 1099 | # sub option end -> process it now |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1100 | self._telnet_process_subnegotiation(bytes(self.suboption)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1101 | self.suboption = None |
| 1102 | self.mode = M_NORMAL |
| 1103 | elif byte in (DO, DONT, WILL, WONT): |
| 1104 | # negotiation |
| 1105 | self.telnet_command = byte |
| 1106 | self.mode = M_NEGOTIATE |
| 1107 | else: |
| 1108 | # other telnet commands |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1109 | self._telnet_process_command(byte) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1110 | self.mode = M_NORMAL |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 1111 | elif self.mode == M_NEGOTIATE: # DO, DONT, WILL, WONT was received, option now following |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1112 | self._telnet_negotiate_option(self.telnet_command, byte) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1113 | self.mode = M_NORMAL |
| 1114 | |
| 1115 | # - incoming telnet commands and options |
| 1116 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1117 | def _telnet_process_command(self, command): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 1118 | """Process commands other than DO, DONT, WILL, WONT.""" |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1119 | # Currently none. RFC2217 only uses negotiation and subnegotiation. |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1120 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1121 | self.logger.warning("ignoring Telnet command: {!r}".format(command)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1122 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1123 | def _telnet_negotiate_option(self, command, option): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 1124 | """Process incoming DO, DONT, WILL, WONT.""" |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1125 | # check our registered telnet options and forward command to them |
| 1126 | # they know themselves if they have to answer or not |
| 1127 | known = False |
| 1128 | for item in self._telnet_options: |
| 1129 | # can have more than one match! as some options are duplicated for |
| 1130 | # 'us' and 'them' |
| 1131 | if item.option == option: |
| 1132 | item.process_incoming(command) |
| 1133 | known = True |
| 1134 | if not known: |
| 1135 | # handle unknown options |
| 1136 | # only answer to positive requests and deny them |
| 1137 | if command == WILL or command == DO: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1138 | self.telnet_send_option((DONT if command == WILL else WONT), option) |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1139 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1140 | self.logger.warning("rejected Telnet option: {!r}".format(option)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1141 | |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1142 | def _telnet_process_subnegotiation(self, suboption): |
cliechti | 044d866 | 2009-08-11 21:40:31 +0000 | [diff] [blame] | 1143 | """Process subnegotiation, the data between IAC SB and IAC SE.""" |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1144 | if suboption[0:1] == COM_PORT_OPTION: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1145 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1146 | self.logger.debug('received COM_PORT_OPTION: {!r}'.format(suboption)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1147 | if suboption[1:2] == SET_BAUDRATE: |
| 1148 | backup = self.serial.baudrate |
| 1149 | try: |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 1150 | (baudrate,) = struct.unpack(b"!I", suboption[2:6]) |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 1151 | if baudrate != 0: |
| 1152 | self.serial.baudrate = baudrate |
Chris Liechti | d214600 | 2015-08-04 16:57:16 +0200 | [diff] [blame] | 1153 | except ValueError as e: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1154 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1155 | self.logger.error("failed to set baud rate: {}".format(e)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1156 | self.serial.baudrate = backup |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 1157 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1158 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1159 | self.logger.info("{} baud rate: {}".format('set' if baudrate else 'get', self.serial.baudrate)) |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1160 | self.rfc2217_send_subnegotiation(SERVER_SET_BAUDRATE, struct.pack(b"!I", self.serial.baudrate)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1161 | elif suboption[1:2] == SET_DATASIZE: |
| 1162 | backup = self.serial.bytesize |
| 1163 | try: |
Chris Liechti | 142ae56 | 2015-08-23 01:11:06 +0200 | [diff] [blame] | 1164 | (datasize,) = struct.unpack(b"!B", suboption[2:3]) |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 1165 | if datasize != 0: |
| 1166 | self.serial.bytesize = datasize |
Chris Liechti | d214600 | 2015-08-04 16:57:16 +0200 | [diff] [blame] | 1167 | except ValueError as e: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1168 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1169 | self.logger.error("failed to set data size: {}".format(e)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1170 | self.serial.bytesize = backup |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 1171 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1172 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1173 | self.logger.info("{} data size: {}".format('set' if datasize else 'get', self.serial.bytesize)) |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1174 | self.rfc2217_send_subnegotiation(SERVER_SET_DATASIZE, struct.pack(b"!B", self.serial.bytesize)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1175 | elif suboption[1:2] == SET_PARITY: |
| 1176 | backup = self.serial.parity |
| 1177 | try: |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 1178 | parity = struct.unpack(b"!B", suboption[2:3])[0] |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 1179 | if parity != 0: |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 1180 | self.serial.parity = RFC2217_REVERSE_PARITY_MAP[parity] |
Chris Liechti | d214600 | 2015-08-04 16:57:16 +0200 | [diff] [blame] | 1181 | except ValueError as e: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1182 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1183 | self.logger.error("failed to set parity: {}".format(e)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1184 | self.serial.parity = backup |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 1185 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1186 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1187 | self.logger.info("{} parity: {}".format('set' if parity else 'get', self.serial.parity)) |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1188 | self.rfc2217_send_subnegotiation( |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 1189 | SERVER_SET_PARITY, |
| 1190 | struct.pack(b"!B", RFC2217_PARITY_MAP[self.serial.parity])) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1191 | elif suboption[1:2] == SET_STOPSIZE: |
| 1192 | backup = self.serial.stopbits |
| 1193 | try: |
Chris Liechti | 01587b1 | 2015-08-05 02:39:32 +0200 | [diff] [blame] | 1194 | stopbits = struct.unpack(b"!B", suboption[2:3])[0] |
cliechti | eada4fd | 2013-07-31 16:26:07 +0000 | [diff] [blame] | 1195 | if stopbits != 0: |
| 1196 | self.serial.stopbits = RFC2217_REVERSE_STOPBIT_MAP[stopbits] |
Chris Liechti | d214600 | 2015-08-04 16:57:16 +0200 | [diff] [blame] | 1197 | except ValueError as e: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1198 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1199 | self.logger.error("failed to set stop bits: {}".format(e)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1200 | self.serial.stopbits = backup |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame] | 1201 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1202 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1203 | self.logger.info("{} stop bits: {}".format('set' if stopbits else 'get', self.serial.stopbits)) |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1204 | self.rfc2217_send_subnegotiation( |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 1205 | SERVER_SET_STOPSIZE, |
| 1206 | struct.pack(b"!B", RFC2217_STOPBIT_MAP[self.serial.stopbits])) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1207 | elif suboption[1:2] == SET_CONTROL: |
| 1208 | if suboption[2:3] == SET_CONTROL_REQ_FLOW_SETTING: |
| 1209 | if self.serial.xonxoff: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1210 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_USE_SW_FLOW_CONTROL) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1211 | elif self.serial.rtscts: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1212 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_USE_HW_FLOW_CONTROL) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1213 | else: |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1214 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_USE_NO_FLOW_CONTROL) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1215 | elif suboption[2:3] == SET_CONTROL_USE_NO_FLOW_CONTROL: |
| 1216 | self.serial.xonxoff = False |
| 1217 | self.serial.rtscts = False |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1218 | if self.logger: |
| 1219 | self.logger.info("changed flow control to None") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1220 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_USE_NO_FLOW_CONTROL) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1221 | elif suboption[2:3] == SET_CONTROL_USE_SW_FLOW_CONTROL: |
| 1222 | self.serial.xonxoff = True |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1223 | if self.logger: |
| 1224 | self.logger.info("changed flow control to XON/XOFF") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1225 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_USE_SW_FLOW_CONTROL) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1226 | elif suboption[2:3] == SET_CONTROL_USE_HW_FLOW_CONTROL: |
| 1227 | self.serial.rtscts = True |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1228 | if self.logger: |
| 1229 | self.logger.info("changed flow control to RTS/CTS") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1230 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_USE_HW_FLOW_CONTROL) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1231 | elif suboption[2:3] == SET_CONTROL_REQ_BREAK_STATE: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1232 | if self.logger: |
| 1233 | self.logger.warning("requested break state - not implemented") |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 1234 | pass # XXX needs cached value |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1235 | elif suboption[2:3] == SET_CONTROL_BREAK_ON: |
gesorthy | b5766f2 | 2017-03-23 08:35:14 +0100 | [diff] [blame] | 1236 | self.serial.break_condition = True |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1237 | if self.logger: |
| 1238 | self.logger.info("changed BREAK to active") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1239 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_BREAK_ON) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1240 | elif suboption[2:3] == SET_CONTROL_BREAK_OFF: |
gesorthy | b5766f2 | 2017-03-23 08:35:14 +0100 | [diff] [blame] | 1241 | self.serial.break_condition = False |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1242 | if self.logger: |
| 1243 | self.logger.info("changed BREAK to inactive") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1244 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_BREAK_OFF) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1245 | elif suboption[2:3] == SET_CONTROL_REQ_DTR: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1246 | if self.logger: |
| 1247 | self.logger.warning("requested DTR state - not implemented") |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 1248 | pass # XXX needs cached value |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1249 | elif suboption[2:3] == SET_CONTROL_DTR_ON: |
gesorthy | b5766f2 | 2017-03-23 08:35:14 +0100 | [diff] [blame] | 1250 | self.serial.dtr = True |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1251 | if self.logger: |
| 1252 | self.logger.info("changed DTR to active") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1253 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_DTR_ON) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1254 | elif suboption[2:3] == SET_CONTROL_DTR_OFF: |
gesorthy | b5766f2 | 2017-03-23 08:35:14 +0100 | [diff] [blame] | 1255 | self.serial.dtr = False |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1256 | if self.logger: |
| 1257 | self.logger.info("changed DTR to inactive") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1258 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_DTR_OFF) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1259 | elif suboption[2:3] == SET_CONTROL_REQ_RTS: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1260 | if self.logger: |
| 1261 | self.logger.warning("requested RTS state - not implemented") |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 1262 | pass # XXX needs cached value |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1263 | #~ self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_RTS_ON) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1264 | elif suboption[2:3] == SET_CONTROL_RTS_ON: |
gesorthy | b5766f2 | 2017-03-23 08:35:14 +0100 | [diff] [blame] | 1265 | self.serial.rts = True |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1266 | if self.logger: |
| 1267 | self.logger.info("changed RTS to active") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1268 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_RTS_ON) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1269 | elif suboption[2:3] == SET_CONTROL_RTS_OFF: |
gesorthy | b5766f2 | 2017-03-23 08:35:14 +0100 | [diff] [blame] | 1270 | self.serial.rts = False |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1271 | if self.logger: |
| 1272 | self.logger.info("changed RTS to inactive") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1273 | self.rfc2217_send_subnegotiation(SERVER_SET_CONTROL, SET_CONTROL_RTS_OFF) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1274 | #~ elif suboption[2:3] == SET_CONTROL_REQ_FLOW_SETTING_IN: |
| 1275 | #~ elif suboption[2:3] == SET_CONTROL_USE_NO_FLOW_CONTROL_IN: |
| 1276 | #~ elif suboption[2:3] == SET_CONTROL_USE_SW_FLOW_CONTOL_IN: |
| 1277 | #~ elif suboption[2:3] == SET_CONTROL_USE_HW_FLOW_CONTOL_IN: |
| 1278 | #~ elif suboption[2:3] == SET_CONTROL_USE_DCD_FLOW_CONTROL: |
| 1279 | #~ elif suboption[2:3] == SET_CONTROL_USE_DTR_FLOW_CONTROL: |
| 1280 | #~ elif suboption[2:3] == SET_CONTROL_USE_DSR_FLOW_CONTROL: |
| 1281 | elif suboption[1:2] == NOTIFY_LINESTATE: |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 1282 | # client polls for current state |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1283 | self.rfc2217_send_subnegotiation( |
Chris Liechti | 920917b | 2016-02-17 18:26:16 +0100 | [diff] [blame] | 1284 | SERVER_NOTIFY_LINESTATE, |
| 1285 | to_bytes([0])) # sorry, nothing like that implemented |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1286 | elif suboption[1:2] == NOTIFY_MODEMSTATE: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1287 | if self.logger: |
| 1288 | self.logger.info("request for modem state") |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 1289 | # client polls for current state |
| 1290 | self.check_modem_lines(force_notification=True) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1291 | elif suboption[1:2] == FLOWCONTROL_SUSPEND: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1292 | if self.logger: |
| 1293 | self.logger.info("suspend") |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1294 | self._remote_suspend_flow = True |
| 1295 | elif suboption[1:2] == FLOWCONTROL_RESUME: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1296 | if self.logger: |
| 1297 | self.logger.info("resume") |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1298 | self._remote_suspend_flow = False |
| 1299 | elif suboption[1:2] == SET_LINESTATE_MASK: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 1300 | self.linstate_mask = ord(suboption[2:3]) # ensure it is a number |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1301 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1302 | self.logger.info("line state mask: 0x{:02x}".format(self.linstate_mask)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1303 | elif suboption[1:2] == SET_MODEMSTATE_MASK: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 1304 | self.modemstate_mask = ord(suboption[2:3]) # ensure it is a number |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1305 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1306 | self.logger.info("modem state mask: 0x{:02x}".format(self.modemstate_mask)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1307 | elif suboption[1:2] == PURGE_DATA: |
| 1308 | if suboption[2:3] == PURGE_RECEIVE_BUFFER: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 1309 | self.serial.reset_input_buffer() |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1310 | if self.logger: |
| 1311 | self.logger.info("purge in") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1312 | self.rfc2217_send_subnegotiation(SERVER_PURGE_DATA, PURGE_RECEIVE_BUFFER) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1313 | elif suboption[2:3] == PURGE_TRANSMIT_BUFFER: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 1314 | self.serial.reset_output_buffer() |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1315 | if self.logger: |
| 1316 | self.logger.info("purge out") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1317 | self.rfc2217_send_subnegotiation(SERVER_PURGE_DATA, PURGE_TRANSMIT_BUFFER) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1318 | elif suboption[2:3] == PURGE_BOTH_BUFFERS: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 1319 | self.serial.reset_input_buffer() |
| 1320 | self.serial.reset_output_buffer() |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1321 | if self.logger: |
| 1322 | self.logger.info("purge both") |
Chris Liechti | 277220e | 2016-02-18 23:27:52 +0100 | [diff] [blame] | 1323 | self.rfc2217_send_subnegotiation(SERVER_PURGE_DATA, PURGE_BOTH_BUFFERS) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1324 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1325 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1326 | self.logger.error("undefined PURGE_DATA: {!r}".format(list(suboption[2:]))) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1327 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1328 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1329 | self.logger.error("undefined COM_PORT_OPTION: {!r}".format(list(suboption[1:]))) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1330 | else: |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 1331 | if self.logger: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1332 | self.logger.warning("unknown subnegotiation: {!r}".format(suboption)) |
cliechti | 130d1f0 | 2009-08-04 02:10:58 +0000 | [diff] [blame] | 1333 | |
| 1334 | |
| 1335 | # simple client test |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 1336 | if __name__ == '__main__': |
| 1337 | import sys |
| 1338 | s = Serial('rfc2217://localhost:7000', 115200) |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1339 | sys.stdout.write('{}\n'.format(s)) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 1340 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 1341 | sys.stdout.write("write...\n") |
Chris Liechti | b4cda3a | 2015-08-08 17:12:08 +0200 | [diff] [blame] | 1342 | s.write(b"hello\n") |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 1343 | s.flush() |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 1344 | sys.stdout.write("read: {}\n".format(s.read(5))) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 1345 | s.close() |