Skip to content

Commit

Permalink
pkg/lwip: Add DEBUG output to lwip_sock_tcp()
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Dec 17, 2024
1 parent 68baf60 commit 7851f53
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include "lwip/api.h"
#include "lwip/opt.h"

#define ENABLE_DEBUG 0
#include "debug.h"

static inline void _tcp_sock_init(sock_tcp_t *sock, struct netconn *conn,
sock_tcp_queue_t *queue)
{
Expand Down Expand Up @@ -314,6 +317,9 @@ int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock,
ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len,
uint32_t timeout)
{
DEBUG("sock_tcp_read(sock, data, max_len=%u, timeout=%" PRIu32 ")\n",
(unsigned)max_len, timeout);

struct pbuf *buf;
ssize_t recvd = 0;
ssize_t res = 0;
Expand Down Expand Up @@ -343,6 +349,7 @@ ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len,

if ((timeout == 0) && !mbox_avail(&sock->base.conn->recvmbox.mbox)) {
mutex_unlock(&sock->mutex);
DEBUG_PUTS("sock_tcp_read(): -EAGAIN");
return -EAGAIN;
}

Expand All @@ -354,6 +361,7 @@ ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len,
else {
err_t err;
if ((err = netconn_recv_tcp_pbuf(sock->base.conn, &buf)) < 0) {
DEBUG("sock_tcp_read(): %d", (int)err);
switch (err) {
case ERR_ABRT:
res = -ECONNABORTED;
Expand Down Expand Up @@ -421,6 +429,17 @@ ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len,
#endif
netconn_set_nonblocking(sock->base.conn, false);
mutex_unlock(&sock->mutex);

DEBUG("sock_tcp_read(): %d\n", (int)res);
if (ENABLE_DEBUG && (res > 0)) {
DEBUG(" ");
unsigned bytes_to_print = (res > 8) ? 8 : res;
for (unsigned i = 0; i < bytes_to_print; i++) {
DEBUG(" %02X", (unsigned)((uint8_t *)data)[i]);
}
DEBUG_PUTS((res > 8) ? "..." : "");
}

return res;
}

Expand All @@ -429,6 +448,16 @@ ssize_t sock_tcp_write(sock_tcp_t *sock, const void *data, size_t len)
struct netconn *conn;
int res = 0;

DEBUG("sock_tcp_write(sock, data, %u)\n", (unsigned)len);
if (ENABLE_DEBUG) {
DEBUG(" ");
unsigned bytes_to_print = (len > 8) ? 8 : len;
for (unsigned i = 0; i < bytes_to_print; i++) {
DEBUG(" %02X", (unsigned)((uint8_t *)data)[i]);
}
DEBUG_PUTS((len > 8) ? "..." : "");
}

assert(sock != NULL);
assert((len == 0) || (data != NULL)); /* (len != 0) => (data != NULL) */
mutex_lock(&sock->mutex);
Expand All @@ -444,6 +473,8 @@ ssize_t sock_tcp_write(sock_tcp_t *sock, const void *data, size_t len)
NULL) so we can leave the mutex */
res = lwip_sock_send(conn, data, len, 0, NULL, NETCONN_TCP);

DEBUG("sock_tcp_write(): %d\n", (int)res);

return res;
}

Expand Down

0 comments on commit 7851f53

Please sign in to comment.