Skip to content

Commit 2c3d845

Browse files
committed
[ot] python/qemu: ot.spi.spi_device: add support for Unix sockets
Signed-off-by: Emmanuel Blot <eblot@rivosinc.com>
1 parent 019c8c3 commit 2c3d845

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

python/qemu/ot/spi/spi_device.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from binascii import hexlify
1010
from logging import getLogger
1111
from select import POLLIN, poll as spoll
12-
from socket import (create_connection, socket, IPPROTO_TCP, TCP_NODELAY,
13-
SHUT_RDWR, timeout as LegacyTimeoutError)
12+
from socket import (AF_UNIX, IPPROTO_TCP, TCP_NODELAY, SHUT_RDWR, SOCK_STREAM,
13+
create_connection, socket, timeout as LegacyTimeoutError)
1414
from struct import calcsize as scalc, pack as spack
1515
from time import sleep, time as now
1616
from typing import NamedTuple, Optional, Union
@@ -34,6 +34,10 @@ class SpiDevice:
3434
"""Default allowed timeout to complete an exchange with the remote target.
3535
"""
3636

37+
CONN_TIMEOUT = 1.0
38+
"""Maximum time to connect to the remote peer.
39+
"""
40+
3741
POLL_TIMEOUT = 0.05
3842
"""Maximum time to wait on a blocking operation.
3943
"""
@@ -88,22 +92,46 @@ def __init__(self):
8892
self._rev_rx = False
8993
self._rev_tx = False
9094

91-
def connect(self, host: str, port: int) -> None:
92-
"""Open a TCP connection to the remote host.
95+
def connect(self, host: str, port: Optional[int] = None) -> None:
96+
"""Open a connection to the remote host.
9397
94-
@param host the host name
95-
@paran port the TCP port
98+
@param host the host name or the connection string
99+
@param port the TCP port
96100
"""
97101
if self._socket:
98102
raise RuntimeError('Cannot open multiple comm port at once')
99-
try:
100-
self._socket = create_connection((host, port), timeout=self.TIMEOUT)
101-
except OSError:
102-
self._log.fatal('Cannot connect to %s:%d', host, port)
103-
raise
104-
# use poll
105-
self._socket.settimeout(None)
106-
self._socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
103+
if port is not None:
104+
try:
105+
self._socket = create_connection((host, port),
106+
timeout=self.CONN_TIMEOUT)
107+
self._socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
108+
except OSError:
109+
self._log.fatal('Cannot connect to %s:%d', host, port)
110+
raise
111+
else:
112+
sock_args = host.split(':')
113+
try:
114+
if sock_args[0] == 'tcp':
115+
try:
116+
host, port = sock_args[1:]
117+
except ValueError as exc:
118+
raise ValueError('TCP port not specified') from exc
119+
self._socket = create_connection((host, int(port)),
120+
timeout=self.TIMEOUT)
121+
self._socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
122+
elif sock_args[0] == 'unix':
123+
self._socket = socket(AF_UNIX, SOCK_STREAM)
124+
self._socket.settimeout(self.CONN_TIMEOUT)
125+
self._socket.connect(sock_args[1])
126+
else:
127+
raise ValueError(f'Unsupported connection string: {host}')
128+
except OSError:
129+
if port:
130+
self._log.fatal('Cannot connect to %s:%d', host, port)
131+
else:
132+
self._log.fatal('Cannot connect to %s', host)
133+
raise
134+
self._socket.settimeout(None) # use poll
107135

108136
def disconnect(self) -> None:
109137
"""Close the communication socket."""

0 commit comments

Comments
 (0)