aboutsummaryrefslogtreecommitdiff
path: root/main/sock.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-09 17:04:59 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-09 17:04:59 +0200
commit9263beca5c84f5f136c913439fe0557f1469e120 (patch)
tree71d8e697a25cbdf631f54664700dea2e61af1823 /main/sock.c
parentadc6e726f672aa7057992af54b286f7632b4fb07 (diff)
WIP TCP socket server
Diffstat (limited to 'main/sock.c')
-rw-r--r--main/sock.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/main/sock.c b/main/sock.c
new file mode 100644
index 0000000..f55be5b
--- /dev/null
+++ b/main/sock.c
@@ -0,0 +1,46 @@
+#include <pico/stdio.h>
+
+#include <lwip/init.h>
+#include <lwip/tcp.h>
+
+#include "init.h"
+
+#include "config.h"
+
+err_t recv_handler(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
+ if (p == NULL) return ERR_VAL;
+
+ printf("recv: %s\n", (char *) p->payload);
+
+ tcp_recved(pcb, p->len);
+ pbuf_free(p);
+ return ERR_OK;
+}
+
+err_t accept_handler(void *arg, struct tcp_pcb *pcb, err_t err) {
+ tcp_recv(pcb, recv_handler);
+
+ return ERR_OK;
+}
+
+void serve_task() {
+ await_init();
+
+ // TODO: why does this hang???
+ // printf("starting lwip...\n");
+ // lwip_init();
+
+ printf("starting server...\n");
+
+ struct tcp_pcb *pcb = tcp_new();
+ tcp_bind(pcb, IP_ADDR_ANY, CONF_SRV_PORT);
+ pcb = tcp_listen(pcb);
+
+ printf("listening on %s:%d\n", ip4addr_ntoa(netif_ip4_addr(netif_list)), CONF_SRV_PORT);
+
+ // connection accept callback
+ tcp_accept(pcb, accept_handler);
+
+ printf("server started!\n");
+}
+