INF2004-Project v0.1
 
Loading...
Searching...
No Matches
wifi.c File Reference

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"
Include dependency graph for wifi.c:

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...
 

Detailed Description

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.

Author
Jurgen Tan
Version
0.1
Date
2023-10-29

Function Documentation

◆ wifi_run_tcp_server_test()

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.

Note
The code contains a note that assumes certain conditions have been met before the function can be executed. These conditions include the inclusion of necessary libraries and header files, definition of macros, structs and functions, allocation of memory, setup of hardware and network configurations, granting of permissions, opening of necessary ports, implementation of error handling, and cleanup.

References wifi_tcp_server::is_complete.

Here is the caller graph for this function:

◆ wifi_tcp_server_recv()

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.

Parameters
[in]p_argPointer to the TCP server state structure.
[in]p_tpcbPointer to the TCP protocol control block (Unused).
[in]p_bufPointer to the received data buffer.
[in]errError code (Unused).
Returns
err_t Error code.

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().

Here is the call graph for this function:

◆ 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.

Parameters
[in]p_argA void pointer to the wifi_tcp_server_t struct.
[in]p_tpcbA pointer to the tcp_pcb struct.
Returns
err_t value indicating success or failure.

References wifi_tcp_server::buffer_sent, DEBUG_PRINT, wifi_tcp_server::sent_len, and WIFI_BUFFER_SIZE.

Here is the caller graph for this function: