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

Header file for WiFi module. This file contains the function prototypes and data structures for the WiFi module. The module provides a TCP server that can be initialized and started using the functions wifi_tcp_server_begin_init() and wifi_tcp_server_begin(). The TCP server uses lwIP library for network communication and has a buffer size of 2048 bytes. More...

#include <stdio.h>
#include "lwip/pbuf.h"
#include "lwip/tcp.h"
Include dependency graph for wifi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  wifi_tcp_server
 Struct representing a TCP server connection. More...
 

Macros

#define DEBUG_PRINT(...)   printf(__VA_ARGS__)
 Macro for printing debug messages. More...
 
#define WIFI_BUFFER_SIZE   2048
 Buffer size for messages sent out via wifi.
 
#define WIFI_MAX_MESSAGE_LENGTH   1024
 Maximum length of a received message.
 
#define WIFI_POLL_TIME_S   20
 Poll time in seconds.
 
#define WIFI_TCP_PORT   4242
 Port to listen on.
 

Typedefs

typedef struct wifi_tcp_server wifi_tcp_server_t
 Struct representing a TCP server connection. More...
 

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

Header file for WiFi module. This file contains the function prototypes and data structures for the WiFi module. The module provides a TCP server that can be initialized and started using the functions wifi_tcp_server_begin_init() and wifi_tcp_server_begin(). The TCP server uses lwIP library for network communication and has a buffer size of 2048 bytes.

Author
Jurgen Tan
Version
0.1
Date
2023-11-28

Macro Definition Documentation

◆ DEBUG_PRINT

#define DEBUG_PRINT (   ...)    printf(__VA_ARGS__)

Macro for printing debug messages.

Parameters
...Variable number of arguments to be printed.

Typedef Documentation

◆ wifi_tcp_server_t

Struct representing a TCP server connection.

Note
The struct contains information about a TCP server connection, including the server and client PCBs, flags indicating whether the connection is complete, buffers for sent and received data, and various counters for tracking the state of the connection.

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: