Skip to content

Commit cd81053

Browse files
author
brentru
committed
get_socket parses through CIPSTATUS socket states, returns socket if state is valid
1 parent e261580 commit cd81053

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

adafruit_fona/adafruit_fona.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,26 @@ def get_host_by_name(self, hostname):
528528

529529
### Socket API (TCP, UDP) ###
530530

531-
def get_socket(self, sockets):
532-
"""Returns the first avaliable (unused) socket
533-
by the socket module.
531+
def get_socket(self):
532+
"""Returns an avaliable socket (INITIAL or CLOSED state).
534533
535534
"""
536535
if self._debug:
537536
print("*** Allocating Socket")
538-
sock = 0
537+
538+
self._uart.write(b"AT+CIPSTATUS\r\n")
539+
self._read_line(100) # OK
540+
self._read_line(100) # table header
541+
539542
for sock in range(0, FONA_MAX_SOCKETS):
540-
if sock not in sockets:
543+
# parse and check for INITIAL client state
544+
self._read_line(100)
545+
self._parse_reply(b'C:', idx=5)
546+
if self._buf.strip('\"') == "INITIAL" or self._buf.strip('\"') == "CLOSED":
541547
break
542-
if self._debug:
543-
print("Allocated socket #", sock)
548+
# read out the rest of the responses
549+
for _ in range(sock, FONA_MAX_SOCKETS):
550+
self._read_line(100)
544551
return sock
545552

546553
def remote_ip(self, sock_num):

adafruit_fona/adafruit_fona_socket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def __init__(
103103
self._buffer = b""
104104
self._timeout = 0
105105

106-
self._socknum = _the_interface.get_socket(SOCKETS)
106+
self._socknum = _the_interface.get_socket()
107107
SOCKETS.append(self._socknum)
108108
self.settimeout(self._timeout)
109109

@@ -210,7 +210,7 @@ def recv(self, bufsize=0):
210210

211211
def readline(self):
212212
"""Attempt to return as many bytes as we can up to but not including '\r\n'"""
213-
# print("Socket readline")
213+
print("Socket readline")
214214
stamp = time.monotonic()
215215
while b"\r\n" not in self._buffer:
216216
# there's no line already in there, read some more

0 commit comments

Comments
 (0)