Skip to content

Commit fce466b

Browse files
authored
Merge pull request #114 from tannewt/esp32spi_fixes
A few ESP32SPI fixes
2 parents 18b3f89 + 2cc5615 commit fce466b

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

100755100644
+16-8
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def __init__(
152152
self._cs.direction = Direction.OUTPUT
153153
self._ready.direction = Direction.INPUT
154154
self._reset.direction = Direction.OUTPUT
155+
# Only one TLS socket at a time is supported so track when we already have one.
156+
self._tls_socket = None
155157
if self._gpio0:
156158
self._gpio0.direction = Direction.INPUT
157159
self.reset()
@@ -330,11 +332,9 @@ def status(self):
330332
(not found), WL_IDLE_STATUS, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED,
331333
WL_CONNECTED, WL_CONNECT_FAILED, WL_CONNECTION_LOST, WL_DISCONNECTED,
332334
WL_AP_LISTENING, WL_AP_CONNECTED, WL_AP_FAILED"""
333-
if self._debug:
334-
print("Connection status")
335335
resp = self._send_command_get_response(_GET_CONN_STATUS_CMD)
336336
if self._debug:
337-
print("Conn status:", resp[0][0])
337+
print("Connection status:", resp[0][0])
338338
return resp[0][0] # one byte response
339339

340340
@property
@@ -623,7 +623,7 @@ def get_socket(self):
623623
resp = self._send_command_get_response(_GET_SOCKET_CMD)
624624
resp = resp[0][0]
625625
if resp == 255:
626-
raise RuntimeError("No sockets available")
626+
raise OSError(23) # ENFILE - File table overflow
627627
if self._debug:
628628
print("Allocated socket #%d" % resp)
629629
return resp
@@ -635,7 +635,9 @@ def socket_open(self, socket_num, dest, port, conn_mode=TCP_MODE):
635635
(dest must be hostname for TLS_MODE!)"""
636636
self._socknum_ll[0][0] = socket_num
637637
if self._debug:
638-
print("*** Open socket")
638+
print("*** Open socket to", dest, port, conn_mode)
639+
if conn_mode == ESP_SPIcontrol.TLS_MODE and self._tls_socket is not None:
640+
raise OSError(23) # ENFILE - File table overflow
639641
port_param = struct.pack(">H", port)
640642
if isinstance(dest, str): # use the 5 arg version
641643
dest = bytes(dest, "utf-8")
@@ -656,6 +658,8 @@ def socket_open(self, socket_num, dest, port, conn_mode=TCP_MODE):
656658
)
657659
if resp[0][0] != 1:
658660
raise RuntimeError("Could not connect to remote server")
661+
if conn_mode == ESP_SPIcontrol.TLS_MODE:
662+
self._tls_socket = socket_num
659663

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

708712
if sent != len(buffer):
713+
self.socket_close(socket_num)
709714
raise RuntimeError(
710715
"Failed to send %d bytes (sent %d)" % (len(buffer), sent)
711716
)
@@ -766,9 +771,12 @@ def socket_close(self, socket_num):
766771
if self._debug:
767772
print("*** Closing socket #%d" % socket_num)
768773
self._socknum_ll[0][0] = socket_num
769-
resp = self._send_command_get_response(_STOP_CLIENT_TCP_CMD, self._socknum_ll)
770-
if resp[0][0] != 1:
771-
raise RuntimeError("Failed to close socket")
774+
try:
775+
self._send_command_get_response(_STOP_CLIENT_TCP_CMD, self._socknum_ll)
776+
except RuntimeError:
777+
pass
778+
if socket_num == self._tls_socket:
779+
self._tls_socket = None
772780

773781
def start_server(
774782
self, port, socket_num, conn_mode=TCP_MODE, ip=None

0 commit comments

Comments
 (0)