diff --git a/lwipopts.h b/lwipopts.h new file mode 100644 index 0000000..3cc083c --- /dev/null +++ b/lwipopts.h @@ -0,0 +1,92 @@ +#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H +#define _LWIPOPTS_EXAMPLE_COMMONH_H + + +// Common settings used in most of the pico_w examples +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details) + +// allow override in some examples +#ifndef NO_SYS +#define NO_SYS 1 +#endif +// allow override in some examples +#ifndef LWIP_SOCKET +#define LWIP_SOCKET 0 +#endif +#if PICO_CYW43_ARCH_POLL +#define MEM_LIBC_MALLOC 1 +#else +// MEM_LIBC_MALLOC is incompatible with non polling versions +#define MEM_LIBC_MALLOC 0 +#endif +#define MEM_ALIGNMENT 4 +#ifndef MEM_SIZE +#define MEM_SIZE 4000 +#endif +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 24 +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (8 * TCP_MSS) +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) +#define LWIP_NETIF_STATUS_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETCONN 0 +#define MEM_STATS 0 +#define SYS_STATS 0 +#define MEMP_STATS 0 +#define LINK_STATS 0 +// #define ETH_PAD_SIZE 2 +#define LWIP_CHKSUM_ALGORITHM 3 +#define LWIP_DHCP 1 +#define LWIP_IPV4 1 +#define LWIP_TCP 1 +#define LWIP_UDP 1 +#define LWIP_DNS 1 +#define LWIP_TCP_KEEPALIVE 1 +#define LWIP_NETIF_TX_SINGLE_PBUF 1 +#define DHCP_DOES_ARP_CHECK 0 +#define LWIP_DHCP_DOES_ACD_CHECK 0 + +#ifndef NDEBUG +#define LWIP_DEBUG 1 +#define LWIP_STATS 1 +#define LWIP_STATS_DISPLAY 1 +#endif + +#define ETHARP_DEBUG LWIP_DBG_OFF +#define NETIF_DEBUG LWIP_DBG_OFF +#define PBUF_DEBUG LWIP_DBG_OFF +#define API_LIB_DEBUG LWIP_DBG_OFF +#define API_MSG_DEBUG LWIP_DBG_OFF +#define SOCKETS_DEBUG LWIP_DBG_OFF +#define ICMP_DEBUG LWIP_DBG_OFF +#define INET_DEBUG LWIP_DBG_OFF +#define IP_DEBUG LWIP_DBG_OFF +#define IP_REASS_DEBUG LWIP_DBG_OFF +#define RAW_DEBUG LWIP_DBG_OFF +#define MEM_DEBUG LWIP_DBG_OFF +#define MEMP_DEBUG LWIP_DBG_OFF +#define SYS_DEBUG LWIP_DBG_OFF +#define TCP_DEBUG LWIP_DBG_OFF +#define TCP_INPUT_DEBUG LWIP_DBG_OFF +#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF +#define TCP_RTO_DEBUG LWIP_DBG_OFF +#define TCP_CWND_DEBUG LWIP_DBG_OFF +#define TCP_WND_DEBUG LWIP_DBG_OFF +#define TCP_FR_DEBUG LWIP_DBG_OFF +#define TCP_QLEN_DEBUG LWIP_DBG_OFF +#define TCP_RST_DEBUG LWIP_DBG_OFF +#define UDP_DEBUG LWIP_DBG_OFF +#define TCPIP_DEBUG LWIP_DBG_OFF +#define PPP_DEBUG LWIP_DBG_OFF +#define SLIP_DEBUG LWIP_DBG_OFF +#define DHCP_DEBUG LWIP_DBG_OFF + +#endif /* __LWIPOPTS_H__ */ diff --git a/main.c b/main.c new file mode 100644 index 0000000..dee34ed --- /dev/null +++ b/main.c @@ -0,0 +1,172 @@ +// Necessary includes from the Pico SDK +#include +#include +#include +#include +#include + +#define CLIENT +//#define SERVER +#define STA_MODE +//#define AP_MODE + +#ifdef STA_MODE + +#define SSID "thinkpad-T440p" +#define PASSWORD "CDuKaka2000!" + +#endif // STA_MODE + +#ifdef AP_MODE + +#define AP_SSID "PicoAP" +#define AP_PASSWORD "PicoPassword!" +#define AP_CHANNEL 3 + +#endif // AP_MODE + +#ifdef CLIENT + +#define CLIENT_PORT 4243 + +static void udp_recv_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) +{ + puts("Callback"); + + if(p->len == 1) + { + printf("a:%d\n", ((uint8_t *)p->payload)[0]); + } + pbuf_free(p); +} + +#endif // CLIENT + +int main() +{ + stdio_init_all(); + + sleep_ms(2000); + + puts("Pi Starting..."); + + if(cyw43_arch_init_with_country(CYW43_COUNTRY_FRANCE)) + { + puts("CYW43 arch init failed"); + return -1; + } + + if(cyw43_wifi_pm(&cyw43_state, CYW43_NO_POWERSAVE_MODE)) + { + puts("CYW43 no powersave mode failed"); + return -1; + } + + puts("CYW43 arch initialized"); + +#ifdef STA_MODE + + cyw43_arch_enable_sta_mode(); + + for(int error_code = 1; error_code; ) + { + printf("WiFi connection attempt to \"%s\"\n", SSID); + + error_code = cyw43_arch_wifi_connect_timeout_ms(SSID, PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 10000); + + if(error_code) + { + const char *error_message; + + switch(error_code) + { + case PICO_ERROR_TIMEOUT: + error_message = "Timeout reached"; + break; + + case PICO_ERROR_BADAUTH: + error_message = "Bad password"; + break; + + case PICO_ERROR_CONNECT_FAILED: + error_message = "Some reason"; + break; + } + + printf("Connection failed : %s\n", error_message); + } + } + + puts("WiFi connected"); + +#endif // STA_MODE + +#ifdef AP_MODE + + cyw43_arch_enable_ap_mode(AP_SSID, AP_PASSWORD, CYW43_AUTH_WPA2_AES_PSK); + + cyw43_wifi_ap_set_channel(&cyw43_state, AP_CHANNEL); + + puts("AP mode enabled"); + +#endif //AP_MODE + +#ifdef CLIENT + + struct udp_pcb *pcb = udp_new(); + + if(!pcb) + { + puts("UDP pcb init failed"); + return -1; + } + + udp_recv(pcb, udp_recv_callback, NULL); + + if(udp_bind(pcb, IP4_ADDR_ANY, CLIENT_PORT)) + { + puts("UDP pcb bind failed"); + return -1; + } + +#endif // CLIENT + + // ========================== Main loop ========================== + while(true) + { + cyw43_arch_poll(); + +#ifdef STA_MODE + int ret = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA); + if(ret != CYW43_LINK_JOIN) + { + const char *status_message; + switch(ret) + { + case CYW43_LINK_DOWN: + status_message = "WiFi down"; + break; + + case CYW43_LINK_JOIN: + status_message = "WiFi up"; + break; + + case CYW43_LINK_FAIL: + status_message = "WiFi connection failed"; + break; + + case CYW43_LINK_NONET: + status_message = "Network not found"; + break; + + case CYW43_LINK_BADAUTH: + status_message = "Bad auth"; + break; + } + puts(status_message); + } +#endif // STA_MODE + + tight_loop_contents(); + } +}