Skip to content

Commit 44746e9

Browse files
committed
one more feedback
1 parent 6d464f4 commit 44746e9

1 file changed

Lines changed: 78 additions & 35 deletions

File tree

targets/ChibiOS/_WiFi/inventek/sockets_ism43362.cpp

Lines changed: 78 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,45 @@ static bool IsValidSocket(SOCK_SOCKET socket)
115115
return socket >= 0 && socket < ISM43362_MAX_SOCKETS && s_sockets[socket].inUse;
116116
}
117117

118+
// Checks whether there's data available to read right now: cached in peekBuf, or (TCP only - see
119+
// SOCK_ioctl(SOCK_FIONREAD) for why UDP can't be probed this way) discovered via a fresh
120+
// non-blocking probe read. Sets *closed if the probe finds the peer has closed the connection.
121+
static bool Ism43362_HasDataAvailable(Ism43362SocketState &state, int socket, bool *closed)
122+
{
123+
*closed = false;
124+
125+
if (state.peekBufPos < state.peekBufLen)
126+
{
127+
return true;
128+
}
129+
130+
if (state.protocol != WIFI_TCP_PROTOCOL)
131+
{
132+
return false;
133+
}
134+
135+
state.peekBufPos = 0;
136+
state.peekBufLen = 0;
137+
138+
uint16_t receivedLen = 0;
139+
WIFI_Status_t status =
140+
WIFI_ReceiveData((uint8_t)socket, state.peekBuf, ISM43362_RX_LOOKAHEAD_SIZE, &receivedLen, 0);
141+
142+
if (status == WIFI_STATUS_SOCKET_CLOSED)
143+
{
144+
*closed = true;
145+
return true;
146+
}
147+
148+
if (status == WIFI_STATUS_OK && receivedLen > 0)
149+
{
150+
state.peekBufLen = receivedLen;
151+
return true;
152+
}
153+
154+
return false;
155+
}
156+
118157
// extracts IPv4 address (network byte order) and port (host byte order) from a SOCK_sockaddr
119158
static void GetIPv4AddressAndPort(const struct SOCK_sockaddr *address, uint8_t ipAddr[4], uint16_t *port)
120159
{
@@ -408,42 +447,18 @@ int SOCK_ioctl(SOCK_SOCKET socket, int cmd, int *data)
408447

409448
Ism43362SocketState &state = s_sockets[socket];
410449

411-
if (state.peekBufPos < state.peekBufLen)
412-
{
413-
*data = state.peekBufLen - state.peekBufPos;
414-
return 0;
415-
}
416-
417-
// Restricted to TCP: SOCK_recvfrom() (UDP) doesn't drain peekBuf, so caching a datagram
418-
// here would make it silently disappear from a subsequent ReceiveFrom() call. TCP is a
419-
// byte stream (no datagram framing to lose), and SOCK_recv() already knows to serve from
420-
// this buffer first.
421-
if (state.protocol != WIFI_TCP_PROTOCOL)
450+
bool closed = false;
451+
if (Ism43362_HasDataAvailable(state, socket, &closed))
422452
{
423-
*data = 0;
424-
return 0;
425-
}
426-
427-
// No "peek without consuming" AT command exists, so checking for data means actually
428-
// reading with a near-instant timeout (0 -> 1ms). Read a whole lookahead buffer's worth
429-
// (not 1 byte) so SOCK_recv() can serve large chunks from RAM instead of one round trip
430-
// per byte.
431-
state.peekBufPos = 0;
432-
state.peekBufLen = 0;
433-
434-
uint16_t receivedLen = 0;
435-
WIFI_Status_t status =
436-
WIFI_ReceiveData((uint8_t)socket, state.peekBuf, ISM43362_RX_LOOKAHEAD_SIZE, &receivedLen, 0);
437-
438-
if (status == WIFI_STATUS_SOCKET_CLOSED)
439-
{
440-
state.connected = false;
441-
*data = 0;
442-
}
443-
else if (status == WIFI_STATUS_OK && receivedLen > 0)
444-
{
445-
state.peekBufLen = receivedLen;
446-
*data = receivedLen;
453+
if (closed)
454+
{
455+
state.connected = false;
456+
*data = 0;
457+
}
458+
else
459+
{
460+
*data = state.peekBufLen - state.peekBufPos;
461+
}
447462
}
448463
else
449464
{
@@ -558,8 +573,36 @@ int SOCK_select(
558573
for (unsigned int i = 0; i < count; i++)
559574
{
560575
int socket = snapshot[i];
576+
bool ready = false;
561577

562578
if (IsValidSocket(socket) && s_sockets[socket].connected)
579+
{
580+
Ism43362SocketState &state = s_sockets[socket];
581+
582+
if (state.protocol == WIFI_TCP_PROTOCOL)
583+
{
584+
// only report readable when data is actually cached/available or the peer
585+
// closed - not just because the socket is connected (a caller would then
586+
// call SOCK_recv() and block for ISM43362_SOCKET_TIMEOUT for nothing)
587+
bool closed = false;
588+
if (Ism43362_HasDataAvailable(state, socket, &closed))
589+
{
590+
ready = true;
591+
if (closed)
592+
{
593+
state.connected = false;
594+
}
595+
}
596+
}
597+
else
598+
{
599+
// no non-consuming peek exists for UDP (see SOCK_ioctl(SOCK_FIONREAD)) - keep
600+
// reporting ready whenever connected
601+
ready = true;
602+
}
603+
}
604+
605+
if (ready)
563606
{
564607
readyCount++;
565608
SOCK_FD_SET(socket, readfds);

0 commit comments

Comments
 (0)