tcp test
This commit is contained in:
34
src/main.c
34
src/main.c
@@ -1,24 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
#include <stdbool.h>
|
||||
#include <net/net.h>
|
||||
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
|
||||
// Initialise the Wi-Fi chip
|
||||
if (cyw43_arch_init()) {
|
||||
printf("Wi-Fi init failed\n");
|
||||
return -1;
|
||||
net_creds_t creds = {
|
||||
.ssid = "5",
|
||||
.password = "nevem123"
|
||||
};
|
||||
|
||||
net_server_t* server = Net_Init(&creds);
|
||||
if (!server) {
|
||||
printf("Failed to initialize network\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Example to turn on the Pico W LED
|
||||
bool led_on = true;
|
||||
if (!Net_Listen(server)) {
|
||||
printf("Failed to start server\n");
|
||||
Net_Destroy(server);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Net_Destroy(server);
|
||||
|
||||
|
||||
while (true) {
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_on);
|
||||
led_on = !led_on; // Toggle LED state
|
||||
sleep_ms(1000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
117
src/net/net.c
Normal file
117
src/net/net.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <net/net.h>
|
||||
#include <lwip/tcp.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char* link_status_to_string(int status) {
|
||||
switch (status) {
|
||||
case CYW43_LINK_DOWN:
|
||||
return "LINK_DOWN";
|
||||
case CYW43_LINK_JOIN:
|
||||
return "LINK_JOIN";
|
||||
case CYW43_LINK_NOIP:
|
||||
return "LINK_NOIP";
|
||||
case CYW43_LINK_UP:
|
||||
return "LINK_UP";
|
||||
case CYW43_LINK_FAIL:
|
||||
return "LINK_FAIL";
|
||||
case CYW43_LINK_NONET:
|
||||
return "LINK_NONET";
|
||||
case CYW43_LINK_BADAUTH:
|
||||
return "LINK_BADAUTH";
|
||||
default:
|
||||
return "LINK_UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
net_server_t* Net_Init(net_creds_t* creds) {
|
||||
if (!creds) {
|
||||
printf("Invalid credentials\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (creds->ssid[0] == '\0') {
|
||||
printf("SSID is empty\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Init Wi-Fi
|
||||
if (cyw43_arch_init()) {
|
||||
printf("Wi-Fi init failed\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cyw43_arch_enable_sta_mode();
|
||||
|
||||
printf("Connecting to SSID: %s\n", creds->ssid);
|
||||
|
||||
int rc = cyw43_arch_wifi_connect_timeout_ms(
|
||||
creds->ssid,
|
||||
creds->password,
|
||||
CYW43_AUTH_WPA2_AES_PSK,
|
||||
15000
|
||||
);
|
||||
|
||||
if (rc) {
|
||||
int link = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
|
||||
printf("Wi-Fi connection failed rc=%d status=%s(%d)\n", rc, link_status_to_string(link), link);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int link = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
|
||||
printf("Wi-Fi connected status=%s(%d)\n", link_status_to_string(link), link);
|
||||
|
||||
net_server_t* svr = (net_server_t*)malloc(sizeof(net_server_t));
|
||||
if (!svr) {
|
||||
printf("Failed to allocate server memory\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(svr, 0, sizeof(net_server_t)); // Zero out
|
||||
|
||||
return svr;
|
||||
}
|
||||
|
||||
bool Net_Listen(net_server_t* server) {
|
||||
if (!server) {
|
||||
printf("Server not initialized\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
struct tcp_pcb* pcb = tcp_new();
|
||||
if (!pcb) {
|
||||
printf("Failed to create TCP PCB\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
err_t err = tcp_bind(pcb, IP_ADDR_ANY, server->port);
|
||||
if (err != ERR_OK) {
|
||||
printf("Failed to bind TCP PCB: %d\n", err);
|
||||
tcp_close(pcb);
|
||||
return false;
|
||||
}
|
||||
|
||||
struct tcp_pcb* listen_pcb = tcp_listen_with_backlog(pcb, server->maxClients);
|
||||
if (!listen_pcb) {
|
||||
printf("Failed to listen on TCP PCB\n");
|
||||
tcp_close(pcb);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Turn on LED
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true);
|
||||
|
||||
while (true) {
|
||||
// TODO: Accept clients and handle data
|
||||
sleep_ms(10);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Net_Destroy(net_server_t* server) {
|
||||
if (server) {
|
||||
// TODO: Disconnect gracefully
|
||||
free(server);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user