#include #include #include #include "esp_err.h" #include "esp_event.h" #include "esp_log.h" #include "esp_netif.h" #include "esp_system.h" #include "esp_wifi.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "lwip/err.h" #include "lwip/sockets.h" #include "lwip/sys.h" #include "nvs_flash.h" #define UDP_PORT 23042 static const char *UDP_SERVER_TAG = "UDP_SERVER"; static void udp_server_task(void *pvParameters) { char rx_buffer[1024]; char addr_str[128]; int addr_family = (int)pvParameters; int ip_protocol = 0; struct sockaddr_in6 dest_addr; while (1) { if (addr_family == AF_INET) { struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr; dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY); dest_addr_ip4->sin_family = AF_INET; dest_addr_ip4->sin_port = htons(UDP_PORT); ip_protocol = IPPROTO_IP; } int sock = socket(addr_family, SOCK_DGRAM, ip_protocol); if (sock < 0) { ESP_LOGE(UDP_SERVER_TAG, "Unable to create socket: errno %d. sock: %d", errno, sock); break; } ESP_LOGI(UDP_SERVER_TAG, "Socket created"); // Set timeout struct timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 0; setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout); int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); if (err < 0) { ESP_LOGE(UDP_SERVER_TAG, "Socket unable to bind: errno %d. sock: %d", errno, sock); } ESP_LOGI(UDP_SERVER_TAG, "Socket bound, port %d", UDP_PORT); struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6 socklen_t socklen = sizeof(source_addr); while (1) { // ESP_LOGI(UDP_SERVER_TAG, "Waiting for data"); int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen); // Error occurred during receiving if (len < 0) { if (errno == EAGAIN) { continue; } ESP_LOGE(UDP_SERVER_TAG, "recvfrom failed: errno %d. len: %d", errno, len); } // Data received else { // Get the sender's ip address as string if (source_addr.ss_family == PF_INET) { inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1); rx_buffer[len] = 0; // Null-terminate whatever we received and treat // like a string... switch (rx_buffer[0]) { case 2: set_display_ambient_light_colors( ((uint16_t)rx_buffer[1] << 8 | (uint16_t)rx_buffer[2]), (uint8_t *)&(rx_buffer[3]), len - 3); break; default: ESP_LOGI(UDP_SERVER_TAG, "%s", rx_buffer); break; } int err = sendto(sock, rx_buffer, len, 0, (struct sockaddr *)&source_addr, sizeof(source_addr)); if (err < 0) { ESP_LOGI(UDP_SERVER_TAG, "Received %d bytes from %s:", len, addr_str); ESP_LOGE(UDP_SERVER_TAG, "Error occurred during sending: errno %d. sock: %d", errno, sock); break; } } } if (sock != -1 && len < 0) { ESP_LOGE(UDP_SERVER_TAG, "Shutting down socket and restarting..."); shutdown(sock, 0); close(sock); break; } } } vTaskDelete(NULL); } void udp_server_init(void) { ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); xTaskCreate(udp_server_task, "udp_server", 4096, (void *)AF_INET, 5, NULL); }