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 | # |
| 10 | # (C) 2001-2009 Chris Liechti <cliechti@gmx.net> |
| 11 | # this is distributed under a free software license, see license.txt |
| 12 | |
| 13 | # TODO: |
| 14 | # - setting control line -> answer is not checked (had problems with one of the |
| 15 | # severs). consider implementing a compatibility mode flag to make check |
| 16 | # conditional |
| 17 | # - write timeout not implemented at all |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 18 | |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 19 | ############################################################################## |
| 20 | # observations and issues with servers |
| 21 | #============================================================================= |
| 22 | # sredird V2.2.1 |
| 23 | # - http://www.ibiblio.org/pub/Linux/system/serial/ sredird-2.2.2.tar.gz |
| 24 | # - does not acknowledge SET_CONTROL (RTS/DTR) correctly, always responding |
| 25 | # [105 1] instead of the actual value. |
| 26 | # - SET_BAUDRATE answer contains 4 extra null bytes -> probably for larger |
| 27 | # numbers than 2**32? |
| 28 | # - To get the signature [COM_PORT_OPTION 0] has to be sent. |
| 29 | # - run a server: while true; do nc -l -p 7000 -c "sredird debug /dev/ttyUSB0 /var/lock/sredir"; done |
| 30 | #============================================================================= |
| 31 | # telnetcpcd (untested) |
| 32 | # - http://ftp.wayne.edu/kermit/sredird/telnetcpcd-1.09.tar.gz |
| 33 | # - To get the signature [COM_PORT_OPTION] w/o data has to be sent. |
| 34 | #============================================================================= |
| 35 | # ser2net |
| 36 | # - does not negotiate BINARY or COM_PORT_OPTION for his side but at least |
| 37 | # acknowledges that the client activates these options |
| 38 | # - The configuration may be that the server prints a banner. As this client |
| 39 | # implementation does a flushInput on connect, this banner is hidden from |
| 40 | # the user application. |
| 41 | # - NOTIFY_MODEMSTATE: the poll interval of the server seems to be one |
| 42 | # second. |
| 43 | # - To get the signature [COM_PORT_OPTION 0] has to be sent. |
| 44 | # - run a server: run ser2net daemon, in /etc/ser2net.conf: |
| 45 | # 2000:telnet:0:/dev/ttyS0:9600 remctl banner |
| 46 | ############################################################################## |
| 47 | |
| 48 | # How to identify ports? pySerial might want to support other protocols in the |
| 49 | # future, so lets use an URL scheme. |
| 50 | # for RFC2217 compliant servers we will use this: |
| 51 | # rfc2217://<host>:<port>[/option[/option...]] |
| 52 | # |
| 53 | # options: |
| 54 | # - "debug" print diagnostic messages |
| 55 | # - "ign_set_control": do not look at the answers to SET_CONTROL |
| 56 | # the order of the options is not relevant |
| 57 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 58 | from serialutil import * |
| 59 | import time |
| 60 | import struct |
| 61 | import socket |
| 62 | import threading |
| 63 | import Queue |
| 64 | |
| 65 | def to_bytes(seq): |
| 66 | b = bytearray() |
| 67 | for item in seq: |
| 68 | b.append(item) |
| 69 | return bytes(b) |
| 70 | |
| 71 | # port string is expected to be something like this: |
| 72 | # rfc2217://host:port |
| 73 | # host may be an IP or including domain, whatever. |
| 74 | # port is 0...65535 |
| 75 | |
| 76 | # telnet protocol characters |
| 77 | IAC = to_bytes([255]) # Interpret As Command |
| 78 | DONT = to_bytes([254]) |
| 79 | DO = to_bytes([253]) |
| 80 | WONT = to_bytes([252]) |
| 81 | WILL = to_bytes([251]) |
| 82 | IAC_DOUBLED = to_bytes([IAC, IAC]) |
| 83 | |
| 84 | SE = to_bytes([240]) # Subnegotiation End |
| 85 | NOP = to_bytes([241]) # No Operation |
| 86 | DM = to_bytes([242]) # Data Mark |
| 87 | BRK = to_bytes([243]) # Break |
| 88 | IP = to_bytes([244]) # Interrupt process |
| 89 | AO = to_bytes([245]) # Abort output |
| 90 | AYT = to_bytes([246]) # Are You There |
| 91 | EC = to_bytes([247]) # Erase Character |
| 92 | EL = to_bytes([248]) # Erase Line |
| 93 | GA = to_bytes([249]) # Go Ahead |
| 94 | SB = to_bytes([250]) # Subnegotiation Begin |
| 95 | |
| 96 | # selected telnet options |
| 97 | BINARY = to_bytes([0]) # 8-bit data path |
| 98 | ECHO = to_bytes([1]) # echo |
| 99 | SGA = to_bytes([3]) # suppress go ahead |
| 100 | |
| 101 | # RFC2217 |
| 102 | COM_PORT_OPTION = to_bytes([44]) |
| 103 | |
| 104 | # Client to Access Server |
| 105 | #~ SIGNATURE text text |
| 106 | SET_BAUDRATE = to_bytes([1]) |
| 107 | SET_DATASIZE = to_bytes([2]) |
| 108 | SET_PARITY = to_bytes([3]) |
| 109 | SET_STOPSIZE = to_bytes([4]) |
| 110 | SET_CONTROL = to_bytes([5]) |
| 111 | NOTIFY_LINESTATE = to_bytes([6]) |
| 112 | NOTIFY_MODEMSTATE = to_bytes([7]) |
| 113 | FLOWCONTROL_SUSPEND = to_bytes([8]) |
| 114 | FLOWCONTROL_RESUME = to_bytes([9]) |
| 115 | SET_LINESTATE_MASK = to_bytes([10]) |
| 116 | SET_MODEMSTATE_MASK = to_bytes([11]) |
| 117 | PURGE_DATA = to_bytes([12]) |
| 118 | |
| 119 | SERVER_SET_BAUDRATE = to_bytes([101]) |
| 120 | SERVER_SET_DATASIZE = to_bytes([102]) |
| 121 | SERVER_SET_PARITY = to_bytes([103]) |
| 122 | SERVER_SET_STOPSIZE = to_bytes([104]) |
| 123 | SERVER_SET_CONTROL = to_bytes([105]) |
| 124 | SERVER_NOTIFY_LINESTATE = to_bytes([106]) |
| 125 | SERVER_NOTIFY_MODEMSTATE = to_bytes([107]) |
| 126 | SERVER_FLOWCONTROL_SUSPEND = to_bytes([108]) |
| 127 | SERVER_FLOWCONTROL_RESUME = to_bytes([109]) |
| 128 | SERVER_SET_LINESTATE_MASK = to_bytes([110]) |
| 129 | SERVER_SET_MODEMSTATE_MASK = to_bytes([111]) |
| 130 | SERVER_PURGE_DATA = to_bytes([112]) |
| 131 | |
| 132 | RFC2217_ANSWER_MAP = { |
| 133 | SET_BAUDRATE: SERVER_SET_BAUDRATE, |
| 134 | SET_DATASIZE: SERVER_SET_DATASIZE, |
| 135 | SET_PARITY: SERVER_SET_PARITY, |
| 136 | SET_STOPSIZE: SERVER_SET_STOPSIZE, |
| 137 | SET_CONTROL: SERVER_SET_CONTROL, |
| 138 | NOTIFY_LINESTATE: SERVER_NOTIFY_LINESTATE, |
| 139 | NOTIFY_MODEMSTATE: SERVER_NOTIFY_MODEMSTATE, |
| 140 | FLOWCONTROL_SUSPEND: SERVER_FLOWCONTROL_SUSPEND, |
| 141 | FLOWCONTROL_RESUME: SERVER_FLOWCONTROL_RESUME, |
| 142 | SET_LINESTATE_MASK: SERVER_SET_LINESTATE_MASK, |
| 143 | SET_MODEMSTATE_MASK: SERVER_SET_MODEMSTATE_MASK, |
| 144 | PURGE_DATA: SERVER_PURGE_DATA, |
| 145 | } |
| 146 | |
| 147 | SET_CONTROL_REQ_FLOW_SETTING = to_bytes([0]) # Request Com Port Flow Control Setting (outbound/both) |
| 148 | SET_CONTROL_USE_NO_FLOW_CONTROL = to_bytes([1]) # Use No Flow Control (outbound/both) |
| 149 | SET_CONTROL_USE_SW_FLOW_CONTROL = to_bytes([2]) # Use XON/XOFF Flow Control (outbound/both) |
| 150 | SET_CONTROL_USE_HW_FLOW_CONTROL = to_bytes([3]) # Use HARDWARE Flow Control (outbound/both) |
| 151 | SET_CONTROL_REQ_BREAK_STATE = to_bytes([4]) # Request BREAK State |
| 152 | SET_CONTROL_BREAK_ON = to_bytes([5]) # Set BREAK State ON |
| 153 | SET_CONTROL_BREAK_OFF = to_bytes([6]) # Set BREAK State OFF |
| 154 | SET_CONTROL_REQ_DTR = to_bytes([7]) # Request DTR Signal State |
| 155 | SET_CONTROL_DTR_ON = to_bytes([8]) # Set DTR Signal State ON |
| 156 | SET_CONTROL_DTR_OFF = to_bytes([9]) # Set DTR Signal State OFF |
| 157 | SET_CONTROL_REQ_RTS = to_bytes([10]) # Request RTS Signal State |
| 158 | SET_CONTROL_RTS_ON = to_bytes([11]) # Set RTS Signal State ON |
| 159 | SET_CONTROL_RTS_OFF = to_bytes([12]) # Set RTS Signal State OFF |
| 160 | SET_CONTROL_REQ_FLOW_SETTING_IN = to_bytes([13]) # Request Com Port Flow Control Setting (inbound) |
| 161 | SET_CONTROL_USE_NO_FLOW_CONTROL_IN = to_bytes([14]) # Use No Flow Control (inbound) |
| 162 | SET_CONTROL_USE_SW_FLOW_CONTOL_IN = to_bytes([15]) # Use XON/XOFF Flow Control (inbound) |
| 163 | SET_CONTROL_USE_HW_FLOW_CONTOL_IN = to_bytes([16]) # Use HARDWARE Flow Control (inbound) |
| 164 | SET_CONTROL_USE_DCD_FLOW_CONTROL = to_bytes([17]) # Use DCD Flow Control (outbound/both) |
| 165 | SET_CONTROL_USE_DTR_FLOW_CONTROL = to_bytes([18]) # Use DTR Flow Control (inbound) |
| 166 | SET_CONTROL_USE_DSR_FLOW_CONTROL = to_bytes([19]) # Use DSR Flow Control (outbound/both) |
| 167 | |
| 168 | LINESTATE_MASK_TIMEOUT = 128 # Time-out Error |
| 169 | LINESTATE_MASK_SHIFTREG_EMPTY = 64 # Transfer Shift Register Empty |
| 170 | LINESTATE_MASK_TRANSREG_EMPTY = 32 # Transfer Holding Register Empty |
| 171 | LINESTATE_MASK_BREAK_DETECT = 16 # Break-detect Error |
| 172 | LINESTATE_MASK_FRAMING_ERROR = 8 # Framing Error |
| 173 | LINESTATE_MASK_PARTIY_ERROR = 4 # Parity Error |
| 174 | LINESTATE_MASK_OVERRUN_ERROR = 2 # Overrun Error |
| 175 | LINESTATE_MASK_DATA_READY = 1 # Data Ready |
| 176 | |
| 177 | MODEMSTATE_MASK_CD = 128 # Receive Line Signal Detect (also known as Carrier Detect) |
| 178 | MODEMSTATE_MASK_RI = 64 # Ring Indicator |
| 179 | MODEMSTATE_MASK_DSR = 32 # Data-Set-Ready Signal State |
| 180 | MODEMSTATE_MASK_CTS = 16 # Clear-To-Send Signal State |
| 181 | MODEMSTATE_MASK_CD_CHANGE = 8 # Delta Receive Line Signal Detect |
| 182 | MODEMSTATE_MASK_RI_CHANGE = 4 # Trailing-edge Ring Detector |
| 183 | MODEMSTATE_MASK_DSR_CHANGE = 2 # Delta Data-Set-Ready |
| 184 | MODEMSTATE_MASK_CTS_CHANGE = 1 # Delta Clear-To-Send |
| 185 | |
| 186 | PURGE_RECEIVE_BUFFER = to_bytes([1]) # Purge access server receive data buffer |
| 187 | PURGE_TRANSMIT_BUFFER = to_bytes([2]) # Purge access server transmit data buffer |
| 188 | PURGE_BOTH_BUFFERS = to_bytes([3]) # Purge both the access server receive data buffer and the access server transmit data buffer |
| 189 | |
| 190 | |
| 191 | RFC2217_PARITY_MAP = { |
| 192 | PARITY_NONE: 1, |
| 193 | PARITY_ODD: 2, |
| 194 | PARITY_EVEN: 3, |
| 195 | PARITY_MARK: 4, |
| 196 | PARITY_SPACE: 5, |
| 197 | } |
| 198 | |
| 199 | RFC2217_STOPBIT_MAP = { |
| 200 | STOPBITS_ONE: 1, |
| 201 | STOPBITS_ONE_POINT_FIVE: 3, |
| 202 | STOPBITS_TWO: 2, |
| 203 | } |
| 204 | |
| 205 | TELNET_ACTION_SUCCESS_MAP = { |
| 206 | DO: (DO, WILL), |
| 207 | WILL: (DO, WILL), |
| 208 | DONT: (DONT, WONT), |
| 209 | WONT: (DONT, WONT), |
| 210 | } |
| 211 | |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 212 | REQUESTED = 'REQUESTED' |
| 213 | ACTIVE = 'ACTIVE' |
| 214 | INACTIVE = 'INACTIVE' |
| 215 | REALLY_INACTIVE = 'REALLY_INACTIVE' |
| 216 | |
| 217 | class TelnetOption(object): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 218 | """Manage a single telnet option, keeps track of DO/DONT WILL/WONT.""" |
| 219 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 220 | def __init__(self, connection, name, option, send_yes, send_no, ack_yes, ack_no, initial_state): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 221 | """Init option. |
| 222 | :param connection: connection used to transmit answers |
| 223 | :param name: a readable name for debug outputs |
| 224 | :param send_yes: what to send when option is to be enabled. |
| 225 | :param send_no: what to send when option is to be disabled. |
| 226 | :param ack_yes: what to expect when remote agrees on option. |
| 227 | :param ack_no: what to expect when remote disagrees on option. |
| 228 | :param initial_state: options initialized with REQUESTED are tried to |
| 229 | be enabled on startup. use INACTIVE for all others. |
| 230 | """ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 231 | self.connection = connection |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 232 | self.name = name |
| 233 | self.option = option |
| 234 | self.send_yes = send_yes |
| 235 | self.send_no = send_no |
| 236 | self.ack_yes = ack_yes |
| 237 | self.ack_no = ack_no |
| 238 | self.state = initial_state |
| 239 | self.active = False |
| 240 | |
| 241 | def __repr__(self): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 242 | """String for debug outputs""" |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 243 | return "%s:%s(%s)" % (self.name, self.active, self.state) |
| 244 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 245 | def process_incoming(self, command): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 246 | """A DO/DONT/WILL/WONT was received for this option, update state and |
| 247 | answer when needed.""" |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 248 | if command == self.ack_yes: |
| 249 | if self.state is REQUESTED: |
| 250 | self.state = ACTIVE |
| 251 | self.active = True |
| 252 | elif self.state is ACTIVE: |
| 253 | pass |
| 254 | elif self.state is INACTIVE: |
| 255 | self.state = ACTIVE |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 256 | self.connection.telnetSendOption(self.send_yes, self.option) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 257 | self.active = True |
| 258 | elif self.state is REALLY_INACTIVE: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 259 | self.connection.telnetSendOption(self.send_no, self.option) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 260 | else: |
| 261 | raise ValueError('option in illegal state %r' % self) |
| 262 | elif command == self.ack_no: |
| 263 | if self.state is REQUESTED: |
| 264 | self.state = INACTIVE |
| 265 | self.active = False |
| 266 | elif self.state is ACTIVE: |
| 267 | self.state = INACTIVE |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 268 | self.connection.telnetSendOption(self.send_no, self.option) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 269 | self.active = False |
| 270 | elif self.state is INACTIVE: |
| 271 | pass |
| 272 | elif self.state is REALLY_INACTIVE: |
| 273 | pass |
| 274 | else: |
| 275 | raise ValueError('option in illegal state %r' % self) |
| 276 | |
| 277 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 278 | class TelnetSubnegotiation(object): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 279 | """A object to handle subnegotiation of options. In this case actually |
| 280 | sub-sub options for RFC2217. It is used to track com port options.""" |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 281 | |
| 282 | def __init__(self, connection, name, option, ack_option=None): |
| 283 | if ack_option is None: ack_option = option |
| 284 | self.connection = connection |
| 285 | self.name = name |
| 286 | self.option = option |
| 287 | self.value = None |
| 288 | self.ack_option = ack_option |
| 289 | self.state = INACTIVE |
| 290 | |
| 291 | def __repr__(self): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 292 | """String for debug outputs""" |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 293 | return "%s:%s" % (self.name, self.state) |
| 294 | |
| 295 | def set(self, value): |
| 296 | """request a change of the value. a request is sent to the server. if |
| 297 | the client needs to know if the change is performed he has to check the |
| 298 | state of this object.""" |
| 299 | self.value = value |
| 300 | self.state = REQUESTED |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 301 | self.connection.rfc2217SendSubnegotiation(self.option, self.value) |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 302 | if self.connection.debug_output: |
| 303 | print "SB Requesting %s -> %r" % (self.name, self.value) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 304 | |
| 305 | def isReady(self): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 306 | """check if answer from server has been received. when server rejects |
| 307 | the change, raise a ValueError.""" |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 308 | if self.state == REALLY_INACTIVE: |
| 309 | raise ValueError("remote rejected value for option %r" % (self.name)) |
| 310 | return self.state == ACTIVE |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 311 | # add property to have a similar interface as TelnetOption |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 312 | active = property(isReady) |
| 313 | |
| 314 | def wait(self, timeout): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 315 | """wait until the subnegotiation has been acknowledged or timeout. It |
| 316 | can also throw a value error when the answer from the server does not |
| 317 | match the value sent.""" |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 318 | timeout_time = time.time() + 3 |
| 319 | while time.time() < timeout_time: |
| 320 | time.sleep(0.05) # prevent 100% CPU load |
| 321 | if self.isReady(): |
| 322 | break |
| 323 | else: |
| 324 | raise SerialException("timeout while waiting for option %r" % (self.name)) |
| 325 | |
| 326 | def checkAnswer(self, suboption): |
| 327 | """check an incoming subnegotiation block. the parameter already has |
| 328 | cut off the header like sub option number and com port option value""" |
| 329 | if self.value == suboption[:len(self.value)]: |
| 330 | self.state = ACTIVE |
| 331 | else: |
| 332 | # error propagation done in isReady |
| 333 | self.state = REALLY_INACTIVE |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 334 | if self.connection.debug_output: |
| 335 | print "SB Answer %s -> %r -> %s" % (self.name, suboption, self.state) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 336 | |
| 337 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 338 | class RFC2217Serial(SerialBase): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 339 | """Serial port implementation for RFC2217 remote serial ports.""" |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 340 | |
| 341 | BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 342 | 9600, 19200, 38400, 57600, 115200) |
| 343 | |
| 344 | def open(self): |
| 345 | """Open port with current settings. This may throw a SerialException |
| 346 | if the port cannot be opened.""" |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 347 | self.debug_output = False |
| 348 | self._ignore_set_control_answer = False |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 349 | if self._port is None: |
| 350 | raise SerialException("Port must be configured before it can be used.") |
| 351 | try: |
| 352 | self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 353 | self._socket.connect(self.fromURL(self.portstr)) |
| 354 | except Exception, msg: |
| 355 | self._socket = None |
| 356 | raise SerialException("Could not open port %s: %s" % (self.portstr, msg)) |
| 357 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 358 | self._socket.settimeout(5) # XXX good value? |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 359 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 360 | # use a thread save queue as buffer. it also simplifies implementing |
| 361 | # the read timeout |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 362 | self._read_buffer = Queue.Queue() |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 363 | # to ensure that user writes does not interfere with internal |
| 364 | # telnet/rfc2217 options establish a lock |
| 365 | self._write_lock = threading.Lock() |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 366 | # name the following separately so that, below, a check can be easily done |
| 367 | mandadory_options = [ |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 368 | TelnetOption(self, 'we-BINARY', BINARY, WILL, WONT, DO, DONT, INACTIVE), |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 369 | TelnetOption(self, 'we-RFC2217', COM_PORT_OPTION, WILL, WONT, DO, DONT, REQUESTED), |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 370 | ] |
| 371 | # all supported telnet options |
| 372 | self._telnet_options = [ |
cliechti | a29275e | 2009-08-03 00:08:04 +0000 | [diff] [blame] | 373 | TelnetOption(self, 'ECHO', ECHO, DO, DONT, WILL, WONT, REQUESTED), |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 374 | TelnetOption(self, 'we-SGA', SGA, WILL, WONT, DO, DONT, REQUESTED), |
| 375 | TelnetOption(self, 'they-SGA', SGA, DO, DONT, WILL, WONT, REQUESTED), |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 376 | TelnetOption(self, 'they-BINARY', BINARY, DO, DONT, WILL, WONT, INACTIVE), |
| 377 | TelnetOption(self, 'they-RFC2217', COM_PORT_OPTION, DO, DONT, WILL, WONT, REQUESTED), |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 378 | ] + mandadory_options |
| 379 | # RFC2217 specific states |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 380 | # COM port settings |
| 381 | self._rfc2217_port_settings = { |
| 382 | 'baudrate': TelnetSubnegotiation(self, 'baudrate', SET_BAUDRATE, SERVER_SET_BAUDRATE), |
| 383 | 'datasize': TelnetSubnegotiation(self, 'datasize', SET_DATASIZE, SERVER_SET_DATASIZE), |
| 384 | 'parity': TelnetSubnegotiation(self, 'parity', SET_PARITY, SERVER_SET_PARITY), |
| 385 | 'stopsize': TelnetSubnegotiation(self, 'stopsize', SET_STOPSIZE, SERVER_SET_STOPSIZE), |
| 386 | } |
| 387 | # There are more subnegotiation object, combine all in one dictionary |
| 388 | # for easy access |
| 389 | self._rfc2217_options = { |
| 390 | 'purge': TelnetSubnegotiation(self, 'purge', PURGE_DATA, SERVER_PURGE_DATA), |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 391 | 'control': TelnetSubnegotiation(self, 'control', SET_CONTROL, SERVER_SET_CONTROL), |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 392 | } |
| 393 | self._rfc2217_options.update(self._rfc2217_port_settings) |
| 394 | # cache for line and modem states that the server sends to us |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 395 | self._linestate = 0 |
| 396 | self._modemstate = 0 |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 397 | # RFC2217 flow control between server and client |
| 398 | self._remote_suspend_flow = False |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 399 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 400 | self._thread = threading.Thread(target=self._telnetReadLoop) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 401 | self._thread.setDaemon(True) |
| 402 | self._thread.setName('pySerial RFC2217 reader thread for %s' % (self._port,)) |
| 403 | self._thread.start() |
| 404 | |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 405 | # negotiate Telnet/RFC2217 -> send initial requests |
| 406 | for option in self._telnet_options: |
| 407 | if option.state is REQUESTED: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 408 | self.telnetSendOption(option.send_yes, option.option) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 409 | # now wait until important options are negotiated |
| 410 | timeout_time = time.time() + 3 |
| 411 | while time.time() < timeout_time: |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 412 | time.sleep(0.05) # prevent 100% CPU load |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 413 | if sum(o.active for o in mandadory_options) == len(mandadory_options): |
| 414 | break |
| 415 | else: |
| 416 | raise SerialException("Remote does not seem to support RFC2217 or BINARY mode %r" % mandadory_options) |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 417 | if self.debug_output: |
| 418 | print self._telnet_options |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 419 | |
| 420 | # fine, go on, set RFC2271 specific things |
| 421 | self._reconfigurePort() |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 422 | # all things set up get, now a clean start |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 423 | self._isOpen = True |
| 424 | if not self._rtscts: |
| 425 | self.setRTS(True) |
| 426 | self.setDTR(True) |
| 427 | self.flushInput() |
| 428 | self.flushOutput() |
| 429 | |
| 430 | def _reconfigurePort(self): |
| 431 | """Set communication parameters on opened port.""" |
| 432 | if self._socket is None: |
| 433 | raise SerialException("Can only operate on open ports") |
| 434 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 435 | # if self._timeout != 0 and self._interCharTimeout is not None: |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 436 | # XXX |
| 437 | |
| 438 | if self._writeTimeout is not None: |
| 439 | raise NotImplementedError('writeTimeout is currently not supported') |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 440 | # XXX |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 441 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 442 | # Setup the connection |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 443 | # to get good performance, all parameter changes are sent first... |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 444 | if not isinstance(self._baudrate, (int, long)) or not 0 < self._baudrate < 2**32: |
| 445 | raise ValueError("invalid baudrate: %r" % (self._baudrate)) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 446 | self._rfc2217_port_settings['baudrate'].set(struct.pack('!I', self._baudrate)) |
| 447 | self._rfc2217_port_settings['datasize'].set(struct.pack('!B', self._bytesize)) |
| 448 | self._rfc2217_port_settings['parity'].set(struct.pack('!B', RFC2217_PARITY_MAP[self._parity])) |
| 449 | self._rfc2217_port_settings['stopsize'].set(struct.pack('!B', RFC2217_STOPBIT_MAP[self._stopbits])) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 450 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 451 | # and now wait until parameters are active |
| 452 | items = self._rfc2217_port_settings.values() |
| 453 | timeout_time = time.time() + 3 |
| 454 | while time.time() < timeout_time: |
| 455 | time.sleep(0.05) # prevent 100% CPU load |
| 456 | if sum(o.active for o in items) == len(items): |
| 457 | break |
| 458 | else: |
| 459 | raise SerialException("Remote does not accept parameter change (RFC2217): %r" % items) |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 460 | if self.debug_output: |
| 461 | print items |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 462 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 463 | |
| 464 | if self._rtscts and self._xonxoff: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 465 | raise ValueError('xonxoff and rtscts together are not supported') |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 466 | elif self._rtscts: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 467 | self.rfc2217SetControl(SET_CONTROL_USE_HW_FLOW_CONTROL) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 468 | elif self._xonxoff: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 469 | self.rfc2217SetControl(SET_CONTROL_USE_SW_FLOW_CONTROL) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 470 | else: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 471 | self.rfc2217SetControl(SET_CONTROL_USE_NO_FLOW_CONTROL) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 472 | |
| 473 | def close(self): |
| 474 | """Close port""" |
| 475 | if self._isOpen: |
| 476 | if self._socket: |
| 477 | try: |
| 478 | self._socket.shutdown(socket.SHUT_RDWR) |
| 479 | self._socket.close() |
| 480 | except: |
| 481 | # ignore errors. |
| 482 | pass |
| 483 | self._socket = None |
| 484 | if self._thread: |
| 485 | self._thread.join() |
| 486 | self._isOpen = False |
| 487 | # in case of quick reconnects, give the server some time |
| 488 | time.sleep(0.3) |
| 489 | |
| 490 | def makeDeviceName(self, port): |
| 491 | raise SerialException("there is no sensible way to turn numbers into URLs") |
| 492 | |
| 493 | def fromURL(self, url): |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 494 | """extract host and port from an URL string""" |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 495 | if url.lower().startswith("rfc2217://"): url = url[10:] |
| 496 | try: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 497 | # is there a "path" (our options)? |
| 498 | if '/' in url: |
| 499 | # cut away options |
| 500 | url, options = url.split('/', 1) |
| 501 | # process options now, directly altering self |
| 502 | for option in options.split('/'): |
| 503 | if option == 'debug': |
| 504 | self.debug_output = True |
| 505 | elif option == 'ign_set_control': |
| 506 | self._ignore_set_control_answer = True |
| 507 | else: |
| 508 | raise ValueError('unknown option: %r' % (option,)) |
| 509 | # get host and port |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 510 | host, port = url.split(':', 1) # may raise ValueError because of unpacking |
| 511 | port = int(port) # and this if it's not a number |
| 512 | if not 0 <= port < 65536: raise ValueError("port not in range 0...65535") |
| 513 | except ValueError, e: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 514 | 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] | 515 | return (host, port) |
| 516 | |
| 517 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 518 | |
| 519 | def inWaiting(self): |
| 520 | """Return the number of characters currently in the input buffer.""" |
| 521 | if not self._isOpen: raise portNotOpenError |
| 522 | return self._read_buffer.qsize() |
| 523 | |
| 524 | def read(self, size=1): |
| 525 | """Read size bytes from the serial port. If a timeout is set it may |
| 526 | return less characters as requested. With no timeout it will block |
| 527 | until the requested number of bytes is read.""" |
| 528 | if not self._isOpen: raise portNotOpenError |
| 529 | data = bytearray() |
| 530 | try: |
| 531 | while len(data) < size: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 532 | if self._thread is None: |
| 533 | raise SerialException('connection failed (reader thread died)') |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 534 | data.append(self._read_buffer.get(True, self._timeout)) |
| 535 | except Queue.Empty: # -> timeout |
| 536 | pass |
| 537 | return bytes(data) |
| 538 | |
| 539 | def write(self, data): |
| 540 | """Output the given string over the serial port. Can block if the |
| 541 | connection is blocked. May raise SerialException if the connection is |
| 542 | closed.""" |
| 543 | if not self._isOpen: raise portNotOpenError |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 544 | self._write_lock.acquire() |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 545 | try: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 546 | try: |
| 547 | self._socket.sendall(data.replace(IAC, IAC_DOUBLED)) |
| 548 | except socket.error, e: |
| 549 | raise SerialException("socket connection failed: %s" % e) # XXX what exception if socket connection fails |
| 550 | finally: |
| 551 | self._write_lock.release() |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 552 | return len(data) |
| 553 | |
| 554 | def flushInput(self): |
| 555 | """Clear input buffer, discarding all that is in the buffer.""" |
| 556 | if not self._isOpen: raise portNotOpenError |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 557 | self.rfc2217SendPurge(PURGE_RECEIVE_BUFFER) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 558 | # empty read buffer |
| 559 | while self._read_buffer.qsize(): |
| 560 | self._read_buffer.get(False) |
| 561 | |
| 562 | def flushOutput(self): |
| 563 | """Clear output buffer, aborting the current output and |
| 564 | discarding all that is in the buffer.""" |
| 565 | if not self._isOpen: raise portNotOpenError |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 566 | self.rfc2217SendPurge(PURGE_TRANSMIT_BUFFER) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 567 | |
| 568 | def sendBreak(self, duration=0.25): |
| 569 | """Send break condition. Timed, returns to idle state after given |
| 570 | duration.""" |
| 571 | if not self._isOpen: raise portNotOpenError |
| 572 | self.setBreak(True) |
| 573 | time.sleep(duration) |
| 574 | self.setBreak(False) |
| 575 | |
| 576 | def setBreak(self, level=True): |
| 577 | """Set break: Controls TXD. When active, to transmitting is |
| 578 | possible.""" |
| 579 | if not self._isOpen: raise portNotOpenError |
| 580 | if level: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 581 | self.rfc2217SetControl(SET_CONTROL_BREAK_ON) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 582 | else: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 583 | self.rfc2217SetControl(SET_CONTROL_BREAK_OFF) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 584 | |
| 585 | def setRTS(self, level=True): |
| 586 | """Set terminal status line: Request To Send""" |
| 587 | if not self._isOpen: raise portNotOpenError |
| 588 | if level: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 589 | self.rfc2217SetControl(SET_CONTROL_RTS_ON) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 590 | else: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 591 | self.rfc2217SetControl(SET_CONTROL_RTS_OFF) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 592 | |
| 593 | def setDTR(self, level=True): |
| 594 | """Set terminal status line: Data Terminal Ready""" |
| 595 | if not self._isOpen: raise portNotOpenError |
| 596 | if level: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 597 | self.rfc2217SetControl(SET_CONTROL_DTR_ON) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 598 | else: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 599 | self.rfc2217SetControl(SET_CONTROL_DTR_OFF) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 600 | |
| 601 | def getCTS(self): |
| 602 | """Read terminal status line: Clear To Send""" |
| 603 | if not self._isOpen: raise portNotOpenError |
| 604 | return bool(self._modemstate & MODEMSTATE_MASK_CTS) |
| 605 | |
| 606 | def getDSR(self): |
| 607 | """Read terminal status line: Data Set Ready""" |
| 608 | if not self._isOpen: raise portNotOpenError |
| 609 | return bool(self._modemstate & MODEMSTATE_MASK_DSR) |
| 610 | |
| 611 | def getRI(self): |
| 612 | """Read terminal status line: Ring Indicator""" |
| 613 | if not self._isOpen: raise portNotOpenError |
| 614 | return bool(self._modemstate & MODEMSTATE_MASK_RI) |
| 615 | |
| 616 | def getCD(self): |
| 617 | """Read terminal status line: Carrier Detect""" |
| 618 | if not self._isOpen: raise portNotOpenError |
| 619 | return bool(self._modemstate & MODEMSTATE_MASK_CD) |
| 620 | |
| 621 | # - - - platform specific - - - |
| 622 | # None so far |
| 623 | |
| 624 | # - - - RFC2217 specific - - - |
| 625 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 626 | def _telnetReadLoop(self): |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 627 | """read loop for the socket""" |
| 628 | M_NORMAL = 0 |
| 629 | M_IAC_SEEN = 1 |
| 630 | M_NEGOTIATE = 2 |
| 631 | mode = M_NORMAL |
| 632 | suboption = None |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 633 | try: |
| 634 | while self._socket is not None: |
| 635 | try: |
| 636 | data = self._socket.recv(1024) |
| 637 | except socket.timeout: |
| 638 | # just need to get out of recv form time to time to check if |
| 639 | # still alive |
| 640 | continue |
| 641 | except socket.error: |
| 642 | # connection fails -> terminate loop |
| 643 | break |
| 644 | for byte in data: |
| 645 | if mode == M_NORMAL: |
| 646 | # interpret as command or as data |
| 647 | if byte == IAC: |
| 648 | mode = M_IAC_SEEN |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 649 | else: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 650 | # store data in read buffer or sub option buffer |
| 651 | # depending on state |
| 652 | if suboption is not None: |
| 653 | suboption.append(byte) |
| 654 | else: |
| 655 | self._read_buffer.put(byte) |
| 656 | elif mode == M_IAC_SEEN: |
| 657 | if byte == IAC: |
| 658 | # interpret as command doubled -> insert character |
| 659 | # itself |
| 660 | self._read_buffer.put(IAC) |
| 661 | mode = M_NORMAL |
| 662 | elif byte == SB: |
| 663 | # sub option start |
| 664 | suboption = bytearray() |
| 665 | mode = M_NORMAL |
| 666 | elif byte == SE: |
| 667 | # sub option end -> process it now |
| 668 | self._telnetProcessSubnegotiation(bytes(suboption)) |
| 669 | suboption = None |
| 670 | mode = M_NORMAL |
| 671 | elif byte in (DO, DONT, WILL, WONT): |
| 672 | # negotiation |
| 673 | telnet_command = byte |
| 674 | mode = M_NEGOTIATE |
| 675 | else: |
| 676 | # other telnet commands |
| 677 | self._telnetProcessCommand(byte) |
| 678 | mode = M_NORMAL |
| 679 | elif mode == M_NEGOTIATE: # DO, DONT, WILL, WONT was received, option now following |
| 680 | self._telnetNegotiateOption(telnet_command, byte) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 681 | mode = M_NORMAL |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 682 | finally: |
| 683 | self._thread = None |
| 684 | if self.debug_output: |
| 685 | print "read thread terminated" |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 686 | |
| 687 | # - incoming telnet commands and options |
| 688 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 689 | def _telnetProcessCommand(self, command): |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 690 | """Process commands other than DO, DONT, WILL, WONT""" |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 691 | # Currently none. RFC2217 only uses negotiation and subnegotiation. |
| 692 | #~ print "_telnetProcessCommand %r" % ord(command) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 693 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 694 | def _telnetNegotiateOption(self, command, option): |
| 695 | """Process incoming DO, DONT, WILL, WONT""" |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 696 | # check our registered telnet options and forward command to them |
| 697 | # they know themselves if they have to answer or not |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 698 | known = False |
| 699 | for item in self._telnet_options: |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 700 | # can have more than one match! as some options are duplicated for |
| 701 | # 'us' and 'them' |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 702 | if item.option == option: |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 703 | item.process_incoming(command) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 704 | known = True |
| 705 | if not known: |
| 706 | # handle unknown options |
| 707 | # only answer to positive requests and deny them |
| 708 | if command == WILL or command == DO: |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 709 | self.telnetSendOption((command == WILL and DONT or WONT), option) |
cliechti | ac20532 | 2009-08-02 20:40:21 +0000 | [diff] [blame] | 710 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 711 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 712 | def _telnetProcessSubnegotiation(self, suboption): |
| 713 | """Process subnegotiation, the data between IAC SB and IAC SE""" |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 714 | if suboption[0:1] == COM_PORT_OPTION: |
| 715 | if suboption[1:2] == SERVER_NOTIFY_LINESTATE and len(suboption) >= 3: |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 716 | self._linestate = ord(suboption[2:3]) # ensure it is a number |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 717 | if self.debug_output: |
| 718 | print "NOTIFY_LINESTATE: %s" % self._linestate |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 719 | elif suboption[1:2] == SERVER_NOTIFY_MODEMSTATE and len(suboption) >= 3: |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 720 | self._modemstate = ord(suboption[2:3]) # ensure it is a number |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 721 | if self.debug_output: |
| 722 | print "NOTIFY_MODEMSTATE: %s" % self._modemstate |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 723 | elif suboption[1:2] == FLOWCONTROL_SUSPEND: |
| 724 | self._remote_suspend_flow = True |
| 725 | elif suboption[1:2] == FLOWCONTROL_RESUME: |
| 726 | self._remote_suspend_flow = False |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 727 | else: |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 728 | for item in self._rfc2217_options.values(): |
| 729 | if item.ack_option == suboption[1:2]: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 730 | #~ print "processing COM_PORT_OPTION: %r" % list(suboption[1:]) |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 731 | item.checkAnswer(bytes(suboption[2:])) |
| 732 | break |
| 733 | else: |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 734 | if self.debug_output: |
| 735 | print "ignoring COM_PORT_OPTION: %r" % list(suboption[1:]) |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 736 | #~ print "_telnetProcessSubnegotiation COM_PORT_OPTION %r" % suboption[1:] |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 737 | else: |
| 738 | pass |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 739 | #~ print "_telnetProcessSubnegotiation unknown %r" % suboption |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 740 | |
| 741 | # - outgoing telnet commands and options |
| 742 | |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 743 | def _internal_raw_write(self, data): |
| 744 | """internal socket write with no data escaping. used to send telnet stuff""" |
| 745 | self._write_lock.acquire() |
| 746 | try: |
| 747 | self._socket.sendall(data) |
| 748 | finally: |
| 749 | self._write_lock.release() |
| 750 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 751 | def telnetSendOption(self, action, option): |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 752 | """Send DO, DONT, WILL, WONT""" |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 753 | self._internal_raw_write(to_bytes([IAC, action, option])) |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 754 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 755 | def rfc2217SendSubnegotiation(self, option, value=[]): |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 756 | """Subnegotiation of RFC2217 parameters""" |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 757 | 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] | 758 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 759 | def rfc2217SendPurge(self, value): |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 760 | item = self._rfc2217_options['purge'] |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 761 | item.set(value) # transmit desired purge type |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 762 | item.wait(3) # wait for acknowledge from the server |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 763 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 764 | def rfc2217SetControl(self, value): |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 765 | item = self._rfc2217_options['control'] |
| 766 | item.set(value) # transmit desired purge type |
| 767 | if self._ignore_set_control_answer: |
| 768 | # answers are ignored when option is set. compatibility mode for |
| 769 | # servers that answers, but not the expected ones... (or no answer |
| 770 | # at all) i.e. sredird |
| 771 | time.sleep(0.1) # this helps getting the unit tests passed |
| 772 | else: |
| 773 | item.wait(3) # wait for acknowledge from the server |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 774 | |
cliechti | 1ef7e3e | 2009-08-03 02:38:43 +0000 | [diff] [blame] | 775 | def rfc2217FlowServerReady(self): |
cliechti | 672d029 | 2009-08-03 02:01:57 +0000 | [diff] [blame] | 776 | """check if server is ready to receive data. block for some time when |
| 777 | not""" |
| 778 | #~ if self._remote_suspend_flow: |
| 779 | #~ wait--- |
| 780 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 781 | |
| 782 | # assemble Serial class with the platform specific implementation and the base |
| 783 | # for file-like behavior. for Python 2.6 and newer, that provide the new I/O |
| 784 | # library, derive from io.RawIOBase |
| 785 | try: |
| 786 | import io |
| 787 | except ImportError: |
| 788 | # classic version with our own file-like emulation |
| 789 | class Serial(RFC2217Serial, FileLike): |
| 790 | pass |
| 791 | else: |
| 792 | # io library present |
| 793 | class Serial(RFC2217Serial, io.RawIOBase): |
| 794 | pass |
| 795 | |
| 796 | |
cliechti | 81c5476 | 2009-08-03 23:53:27 +0000 | [diff] [blame^] | 797 | # simple test |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 798 | if __name__ == '__main__': |
| 799 | import sys |
| 800 | s = Serial('rfc2217://localhost:7000', 115200) |
| 801 | sys.stdout.write('%s\n' % s) |
| 802 | |
cliechti | 2b929b7 | 2009-08-02 23:49:02 +0000 | [diff] [blame] | 803 | #~ s.baudrate = 1898 |
| 804 | |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 805 | sys.stdout.write("write...\n") |
| 806 | s.write("hello\n") |
| 807 | s.flush() |
cliechti | 8099bed | 2009-08-01 23:59:18 +0000 | [diff] [blame] | 808 | sys.stdout.write("read: %s\n" % s.read(5)) |
| 809 | |
| 810 | #~ s.baudrate = 19200 |
| 811 | #~ s.databits = 7 |
| 812 | s.close() |