diff options
author | lonkaars <loek@pipeframe.xyz> | 2024-05-10 13:34:22 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2024-05-10 13:34:22 +0200 |
commit | 8a53d06efd517908f88f697a250059200e882c06 (patch) | |
tree | 7a259dfa35d77e4b27ee78c0ccd5145e07f05371 | |
parent | 9263beca5c84f5f136c913439fe0557f1469e120 (diff) |
fix TCP socket server
-rw-r--r-- | main/config.def.h | 3 | ||||
-rw-r--r-- | main/init.c | 4 | ||||
-rw-r--r-- | main/init.h | 10 | ||||
-rw-r--r-- | main/main.c | 14 | ||||
-rw-r--r-- | main/sock.c | 54 | ||||
-rw-r--r-- | main/sock.h | 2 |
6 files changed, 40 insertions, 47 deletions
diff --git a/main/config.def.h b/main/config.def.h index c6852ef..7fcaed9 100644 --- a/main/config.def.h +++ b/main/config.def.h @@ -4,8 +4,9 @@ // wifi credentials #define CONF_NET_SSID "network name" #define CONF_NET_PASS "network password" +#define CONF_NET_AUTH CYW43_AUTH_WPA2_AES_PSK // max duration (milliseconds) for establishing wifi connection -#define CONF_NET_CONN_TIMEOUT 10000 +#define CONF_NET_CONN_TIMEOUT 10e3 #include <cyw43_country.h> #define CONF_NET_COUNTRY CYW43_COUNTRY_NETHERLANDS diff --git a/main/init.c b/main/init.c index 014a5ad..dcd3d54 100644 --- a/main/init.c +++ b/main/init.c @@ -23,7 +23,7 @@ static void init_wifi() { // enable 'station' mode (connect to an access point instead of acting like one) cyw43_arch_enable_sta_mode(); - if (cyw43_arch_wifi_connect_timeout_ms(CONF_NET_SSID, CONF_NET_PASS, CYW43_AUTH_WPA2_AES_PSK, CONF_NET_CONN_TIMEOUT)) + if (cyw43_arch_wifi_connect_timeout_ms(CONF_NET_SSID, CONF_NET_PASS, CONF_NET_AUTH, CONF_NET_CONN_TIMEOUT)) panic("cyw43_arch_wifi_connect failed\n"); printf("connected to Wi-Fi\n"); @@ -33,8 +33,8 @@ static void init_wifi() { static void async_init() { init_cyw34(); - init_wifi(); // TODO: initialize i2c + init_wifi(); xEventGroupSetBits(init_complete, 1); diff --git a/main/init.h b/main/init.h index 97b2e20..de9023c 100644 --- a/main/init.h +++ b/main/init.h @@ -4,7 +4,7 @@ #include <event_groups.h> /** - * @brief init function complete event group handle + * \brief init function complete event group handle * * This is required to make sure the main task waits until initialization is * complete. Due to the combination of FreeRTOS + lwIP, the initialization @@ -12,25 +12,25 @@ * cyw43_arch_init functions make the pico hang indefinitely when used while * the task scheduler is not running. * - * @note `init_complete` only utilizes LSB, so `uxBitsToWaitFor` should always + * \note `init_complete` only utilizes LSB, so `uxBitsToWaitFor` should always * be set to *1* */ extern EventGroupHandle_t init_complete; /** - * @brief initialize all peripherals on the pico + * \brief initialize all peripherals on the pico * * This function only synchronously initializes the standard input/output (used * for `printf` and `panic`), and queues all other types of initialization in * the `init` task using FreeRTOS. * - * @note Tasks dependent on the wifi being initialized should use the + * \note Tasks dependent on the wifi being initialized should use the * `init_complete` event group to wait for initialization to complete! */ void init(); /** - * @brief block task until all initialization is complete + * \brief block task until all initialization is complete * * utility function, see above comments */ diff --git a/main/main.c b/main/main.c index a0a09a0..73b6708 100644 --- a/main/main.c +++ b/main/main.c @@ -8,10 +8,6 @@ #include "init.h" #include "sock.h" -// #include <lwip/sockets.h> -// #include <lwip/sys.h> -// #include <lwip/opt.h> - void blink_task() { await_init(); // `blink_task` uses GPIO @@ -23,20 +19,10 @@ void blink_task() { } } -void test_task() { - int i = 0; - while (true) { - // stdio is initialized synchronously, so no `await_init` is needed - printf("hello #%d...\n", ++i); - vTaskDelay(1000 / portTICK_PERIOD_MS); - } -} - int main() { init(); 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 index f55be5b..7064de7 100644 --- a/main/sock.c +++ b/main/sock.c @@ -1,46 +1,52 @@ #include <pico/stdio.h> -#include <lwip/init.h> -#include <lwip/tcp.h> +#include <lwip/opt.h> +#include <lwip/sys.h> +#include <lwip/api.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; +void recv_handler(struct netconn* conn, struct netbuf* buf) { + void *data; + uint16_t len; - printf("recv: %s\n", (char *) p->payload); + do { + netbuf_data(buf, &data, &len); + printf("got %d bytes!\n", len); + } while (netbuf_next(buf) >= 0); - tcp_recved(pcb, p->len); - pbuf_free(p); - return ERR_OK; + netbuf_delete(buf); } -err_t accept_handler(void *arg, struct tcp_pcb *pcb, err_t err) { - tcp_recv(pcb, recv_handler); +void accept_handler(struct netconn* conn) { + printf("new connection!\n"); - return ERR_OK; + struct netbuf* buf; + + while (netconn_recv(conn, &buf) == ERR_OK) + recv_handler(conn, buf); + + netconn_close(conn); + netconn_delete(conn); + + printf("connection closed!\n"); } 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); + struct netconn* conn = netconn_new(NETCONN_TCP); + netconn_bind(conn, IP_ADDR_ANY, CONF_SRV_PORT); + netconn_listen(conn); 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"); + while (1) { + struct netconn* incoming; + if (netconn_accept(conn, &incoming) == ERR_OK) + accept_handler(incoming); + } } diff --git a/main/sock.h b/main/sock.h index 66dc874..dd7fc61 100644 --- a/main/sock.h +++ b/main/sock.h @@ -1,5 +1,5 @@ #pragma once -/** @brief start listening for TCP socket requests */ +/** \brief start listening for TCP socket requests */ void serve_task(); |