diff options
author | lonkaars <loek@pipeframe.xyz> | 2024-05-09 17:04:59 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2024-05-09 17:04:59 +0200 |
commit | 9263beca5c84f5f136c913439fe0557f1469e120 (patch) | |
tree | 71d8e697a25cbdf631f54664700dea2e61af1823 | |
parent | adc6e726f672aa7057992af54b286f7632b4fb07 (diff) |
WIP TCP socket server
-rw-r--r-- | main/CMakeLists.txt | 1 | ||||
-rw-r--r-- | main/init.c | 2 | ||||
-rw-r--r-- | main/main.c | 2 | ||||
-rw-r--r-- | main/sock.c | 46 | ||||
-rw-r--r-- | main/sock.h | 5 |
5 files changed, 56 insertions, 0 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 123d1b7..7b8d567 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -15,6 +15,7 @@ pico_sdk_init() add_executable(main main.c init.c + sock.c ) pico_enable_stdio_usb(main 1) diff --git a/main/init.c b/main/init.c index 616cfea..014a5ad 100644 --- a/main/init.c +++ b/main/init.c @@ -26,6 +26,8 @@ static void init_wifi() { if (cyw43_arch_wifi_connect_timeout_ms(CONF_NET_SSID, CONF_NET_PASS, CYW43_AUTH_WPA2_AES_PSK, CONF_NET_CONN_TIMEOUT)) panic("cyw43_arch_wifi_connect failed\n"); + printf("connected to Wi-Fi\n"); + // TODO: announce hostname(?) } diff --git a/main/main.c b/main/main.c index c97a808..a0a09a0 100644 --- a/main/main.c +++ b/main/main.c @@ -6,6 +6,7 @@ #include "config.h" #include "init.h" +#include "sock.h" // #include <lwip/sockets.h> // #include <lwip/sys.h> @@ -36,6 +37,7 @@ int main() { xTaskCreate((TaskFunction_t) blink_task, "blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); xTaskCreate((TaskFunction_t) test_task, "test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); + xTaskCreate((TaskFunction_t) serve_task, "serve", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL); vTaskStartScheduler(); } 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"); +} + diff --git a/main/sock.h b/main/sock.h new file mode 100644 index 0000000..66dc874 --- /dev/null +++ b/main/sock.h @@ -0,0 +1,5 @@ +#pragma once + +/** @brief start listening for TCP socket requests */ +void serve_task(); + |