2026-Propulsion/Wifi.c

164 lines
4.7 KiB
C

#include "Wifi.h"
#include <string.h>
#include "pico/cyw43_arch.h"
#include "lwip/etharp.h"
#include "lwip/ethip6.h"
#include "lwip/dns.h"
#include "lwip/igmp.h"
#include "lwip/tcpip.h"
#include "netif/ethernet.h"
#include <pico/lwip_nosys.h>
// Si le fichier n'existe pas, créez-le à partir du modèle "wifi_settings.h.default"
#include "wifi_settings.h"
#define BEACON_MSG_LEN_MAX 500
#define UDP_CLIENT_PORT 1234
struct udp_pcb* pcb;
struct udp_pcb * pcb_r;
ip_addr_t my_ip_addr, host_ip_addr;
char udp_buffer_r[UDP_BUFFER_LENGTH];
unsigned int udp_buffer_r_index = 0;
void udp_receive_put_in_buffer(char c){
if(udp_buffer_r_index + 1< UDP_BUFFER_LENGTH){
udp_buffer_r[udp_buffer_r_index] = c;
udp_buffer_r_index++;
}
}
/// @brief Copie les données reçues par UDP dans le tampon et renvoie le nombre de données copiées
/// @param tampon qui va contenir les données
/// @return Nombre de données dans le tampon
int udp_receive_get_from_buffer(char* tampon){
unsigned int taille_tampon;
if( udp_buffer_r_index > 0){
taille_tampon = udp_buffer_r_index;
memcpy(tampon, udp_buffer_r, taille_tampon);
udp_buffer_r_index = 0;
return taille_tampon;
}
return 0;
}
void udp_receive_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){
for(int i=0; i < p->len; i++){
udp_receive_put_in_buffer(((char*)p->payload)[i]);
}
pbuf_free(p);
}
int UDP_client_init(void){
pcb_r = udp_new();
if(pcb_r == NULL)
{
puts("Error creating UDP client");
return -1;
}
udp_recv(pcb_r, udp_receive_callback, NULL);
err_t err = udp_bind(pcb_r, IP_ADDR_ANY, UDP_CLIENT_PORT);
if(err)
{
printf("Error binding UDP client: %d\n", err);
return -1;
}
printf("UDP client started on port %d\n", UDP_CLIENT_PORT);
}
int Wifi_init(void){
if (cyw43_arch_init()) {
printf("failed to initialise\n");
return 1;
}
cyw43_arch_enable_sta_mode();
printf("Connecting to Wi-Fi...\n");
while (cyw43_arch_wifi_connect_timeout_ms(MY_WIFI_SSID, MY_WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("failed to connect.\n");
}
printf("Connected.\n");
pcb = udp_new();
ipaddr_aton(BEACON_TARGET, &my_ip_addr);
UDP_client_init();
return 0;
}
void Wifi_udp_send_data(char * message, unsigned int size){
static int counter = 0;
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, BEACON_MSG_LEN_MAX+1, PBUF_RAM);
char *req = (char *)p->payload;
memset(req, 0, BEACON_MSG_LEN_MAX+1);
if(size <BEACON_MSG_LEN_MAX){
memcpy(req, message, size);
}else{
printf("Error: size >= BEACON_MSG_LEN_MAX\n");
}
err_t er = udp_sendto(pcb, p, &my_ip_addr, 1234);
pbuf_free(p);
if (er != ERR_OK) {
printf("Failed to send UDP packet! error=%d", er);
} else {
// printf("Sent packet %d\n", counter);
counter++;
}
// Note in practice for this simple UDP transmitter,
// the end result for both background and poll is the same
#if PICO_CYW43_ARCH_POLL
// if you are using pico_cyw43_arch_poll, then you must poll periodically from your
// main loop (not from a timer) to check for Wi-Fi driver or lwIP work that needs to be done.
cyw43_arch_poll();
#else
// if you are not using pico_cyw43_arch_poll, then WiFI driver and lwIP work
// is done via interrupt in the background. This sleep is just an example of some (blocking)
// work you might be doing.
// sleep_ms(BEACON_INTERVAL_MS);
#endif
}
void Wifi_udp_send_string(char * message){
static int counter = 0;
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, BEACON_MSG_LEN_MAX+1, PBUF_RAM);
char *req = (char *)p->payload;
memset(req, 0, BEACON_MSG_LEN_MAX+1);
snprintf(req, BEACON_MSG_LEN_MAX, "%s", message);
err_t er = udp_sendto(pcb, p, &my_ip_addr, 1234);
//er |= udp_sendto(pcb, p, &my_ip_addr, 1234);
pbuf_free(p);
if (er != ERR_OK) {
printf("Failed to send UDP packet! error=%d", er);
} else {
// printf("Sent packet %d\n", counter);
counter++;
}
// Note in practice for this simple UDP transmitter,
// the end result for both background and poll is the same
#if PICO_CYW43_ARCH_POLL
// if you are using pico_cyw43_arch_poll, then you must poll periodically from your
// main loop (not from a timer) to check for Wi-Fi driver or lwIP work that needs to be done.
cyw43_arch_poll();
#else
// if you are not using pico_cyw43_arch_poll, then WiFI driver and lwIP work
// is done via interrupt in the background. This sleep is just an example of some (blocking)
// work you might be doing.
// sleep_ms(BEACON_INTERVAL_MS);
#endif
}