This file contains the implementation of a TCP server using lwIP. The TCP server is initialized using tcp_server_init() and closed using tcp_server_close(). The server sends data to the client using wifi_tcp_server_send_data() and receives data from the client using wifi_tcp_server_recv(). Callback functions for sent data and connection result are defined in tcp_server_sent() and tcp_server_result() respectively. More...
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/cdefs.h>
#include "pico/cyw43_arch.h"
#include "cyw43_configport.h"
#include "cyw43_ll.h"
#include "lwip/ip4_addr.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/arch.h"
#include "lwip/err.h"
#include "lwip/pbuf.h"
#include "lwip/tcp.h"
#include "lwip/tcpbase.h"
#include "pico/time.h"
#include "drivers/wifi/wifi.h"
Functions | |
void | wifi_run_tcp_server_test (void) |
Runs a TCP server and opens it. Then, it enters a loop to check for Wi-Fi driver or lwIP work that needs to be done. If using pico_cyw43_arch_poll, it polls periodically from the main loop to check for work. Otherwise, it sleeps for 1000ms. More... | |
void | wifi_tcp_server_begin (void) |
Begins a TCP server by connecting to Wi-Fi with the given SSID and password. | |
void | wifi_tcp_server_begin_init (void) |
Initializes the TCP server by initializing standard input/output, initializing the CYW43 architecture, and enabling station mode. | |
err_t | wifi_tcp_server_recv (void *p_arg, __unused struct tcp_pcb *p_tpcb, struct pbuf *p_buf, __unused err_t err) |
This function is called by lwIP when data is received from a TCP client. It receives the data and checks if a complete message has been received (e.g., terminated by a newline character). If a complete message has been received, it null-terminates the received data, prints it to the console, and resets the receive buffer. More... | |
err_t | wifi_tcp_server_send_data (void *p_arg, struct tcp_pcb *p_tpcb) |
This function is a callback from lwIP, so cyw43_arch_lwip_begin is not required. However, you can use this method to cause an assertion in debug mode if this method is called when cyw43_arch_lwip_begin IS needed. More... | |
This file contains the implementation of a TCP server using lwIP. The TCP server is initialized using tcp_server_init() and closed using tcp_server_close(). The server sends data to the client using wifi_tcp_server_send_data() and receives data from the client using wifi_tcp_server_recv(). Callback functions for sent data and connection result are defined in tcp_server_sent() and tcp_server_result() respectively.
void wifi_run_tcp_server_test | ( | void | ) |
Runs a TCP server and opens it. Then, it enters a loop to check for Wi-Fi driver or lwIP work that needs to be done. If using pico_cyw43_arch_poll, it polls periodically from the main loop to check for work. Otherwise, it sleeps for 1000ms.
References wifi_tcp_server::is_complete.
err_t wifi_tcp_server_recv | ( | void * | p_arg, |
__unused struct tcp_pcb * | p_tpcb, | ||
struct pbuf * | p_buf, | ||
__unused err_t | err | ||
) |
This function is called by lwIP when data is received from a TCP client. It receives the data and checks if a complete message has been received (e.g., terminated by a newline character). If a complete message has been received, it null-terminates the received data, prints it to the console, and resets the receive buffer.
[in] | p_arg | Pointer to the TCP server state structure. |
[in] | p_tpcb | Pointer to the TCP protocol control block (Unused). |
[in] | p_buf | Pointer to the received data buffer. |
[in] | err | Error code (Unused). |
References wifi_tcp_server::buffer_recv, wifi_tcp_server::p_client_pcb, wifi_tcp_server::recv_len, WIFI_BUFFER_SIZE, and wifi_tcp_server_send_data().
err_t wifi_tcp_server_send_data | ( | void * | p_arg, |
struct tcp_pcb * | p_tpcb | ||
) |
This function is a callback from lwIP, so cyw43_arch_lwip_begin is not required. However, you can use this method to cause an assertion in debug mode if this method is called when cyw43_arch_lwip_begin IS needed.
[in] | p_arg | A void pointer to the wifi_tcp_server_t struct. |
[in] | p_tpcb | A pointer to the tcp_pcb struct. |
References wifi_tcp_server::buffer_sent, DEBUG_PRINT, wifi_tcp_server::sent_len, and WIFI_BUFFER_SIZE.