Skip to content

A few ESP32SPI fixes #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions adafruit_esp32spi/adafruit_esp32spi.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def __init__(
self._cs.direction = Direction.OUTPUT
self._ready.direction = Direction.INPUT
self._reset.direction = Direction.OUTPUT
# Only one TLS socket at a time is supported so track when we already have one.
self._tls_socket = None
if self._gpio0:
self._gpio0.direction = Direction.INPUT
self.reset()
Expand Down Expand Up @@ -330,11 +332,9 @@ def status(self):
(not found), WL_IDLE_STATUS, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED,
WL_CONNECTED, WL_CONNECT_FAILED, WL_CONNECTION_LOST, WL_DISCONNECTED,
WL_AP_LISTENING, WL_AP_CONNECTED, WL_AP_FAILED"""
if self._debug:
print("Connection status")
resp = self._send_command_get_response(_GET_CONN_STATUS_CMD)
if self._debug:
print("Conn status:", resp[0][0])
print("Connection status:", resp[0][0])
return resp[0][0] # one byte response

@property
Expand Down Expand Up @@ -623,7 +623,7 @@ def get_socket(self):
resp = self._send_command_get_response(_GET_SOCKET_CMD)
resp = resp[0][0]
if resp == 255:
raise RuntimeError("No sockets available")
raise OSError(23) # ENFILE - File table overflow
if self._debug:
print("Allocated socket #%d" % resp)
return resp
Expand All @@ -635,7 +635,9 @@ def socket_open(self, socket_num, dest, port, conn_mode=TCP_MODE):
(dest must be hostname for TLS_MODE!)"""
self._socknum_ll[0][0] = socket_num
if self._debug:
print("*** Open socket")
print("*** Open socket to", dest, port, conn_mode)
if conn_mode == ESP_SPIcontrol.TLS_MODE and self._tls_socket is not None:
raise OSError(23) # ENFILE - File table overflow
port_param = struct.pack(">H", port)
if isinstance(dest, str): # use the 5 arg version
dest = bytes(dest, "utf-8")
Expand All @@ -656,6 +658,8 @@ def socket_open(self, socket_num, dest, port, conn_mode=TCP_MODE):
)
if resp[0][0] != 1:
raise RuntimeError("Could not connect to remote server")
if conn_mode == ESP_SPIcontrol.TLS_MODE:
self._tls_socket = socket_num

def socket_status(self, socket_num):
"""Get the socket connection status, can be SOCKET_CLOSED, SOCKET_LISTEN,
Expand Down Expand Up @@ -706,6 +710,7 @@ def socket_write(self, socket_num, buffer, conn_mode=TCP_MODE):
return

if sent != len(buffer):
self.socket_close(socket_num)
raise RuntimeError(
"Failed to send %d bytes (sent %d)" % (len(buffer), sent)
)
Expand Down Expand Up @@ -766,9 +771,12 @@ def socket_close(self, socket_num):
if self._debug:
print("*** Closing socket #%d" % socket_num)
self._socknum_ll[0][0] = socket_num
resp = self._send_command_get_response(_STOP_CLIENT_TCP_CMD, self._socknum_ll)
if resp[0][0] != 1:
raise RuntimeError("Failed to close socket")
try:
self._send_command_get_response(_STOP_CLIENT_TCP_CMD, self._socknum_ll)
except RuntimeError:
pass
if socket_num == self._tls_socket:
self._tls_socket = None

def start_server(
self, port, socket_num, conn_mode=TCP_MODE, ip=None
Expand Down