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