board/main/desktop.c

297 lines
8.1 KiB
C

#include <lwip/netdb.h>
#include <string.h>
#include <sys/param.h>
#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 "gui.c"
#include "light.c"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "nvs_flash.h"
#include "ui_input.c"
#define UDP_PORT 23042
static const char *UDP_SERVER_TAG = "UDP_SERVER";
static bool desktop_connected = false;
static uint64_t last_desktop_ping_at = 0;
static struct sockaddr *desktop_addr = NULL;
static int sock = -1;
static uint8_t tx_buffer[128];
typedef struct desktop_value {
uint8_t value;
uint8_t max;
uint8_t min;
} desktop_value_t;
static desktop_value_t display1_brightness = {
.value = 20,
.max = 100,
.min = 0,
};
static desktop_value_t display2_brightness = {
.value = 20,
.max = 100,
.min = 0,
};
static desktop_value_t computer_volume = {
.value = 20,
.max = 100,
.min = 0,
};
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_in dest_addr;
while (1) {
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(UDP_PORT);
ip_protocol = IPPROTO_IP;
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);
sock = -1;
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(UDP_SERVER_TAG, "Socket bound, port %d", UDP_PORT);
struct sockaddr 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,
&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
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 1:
last_desktop_ping_at = esp_timer_get_time();
ESP_LOGD(UDP_SERVER_TAG, "Received ping from %s", addr_str);
sendto(sock, rx_buffer, 1, 0, &source_addr, sizeof(source_addr));
desktop_addr = &source_addr;
break;
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;
case 3: // computer brightness changed
if (rx_buffer[1] == 0) {
display1_brightness.value = rx_buffer[2];
} else if (rx_buffer[1] == 1) {
display2_brightness.value = rx_buffer[2];
}
break;
case 4: // computer volume changed
computer_volume.value = rx_buffer[1];
break;
default:
ESP_LOG_BUFFER_HEXDUMP(UDP_SERVER_TAG, rx_buffer, len,
ESP_LOG_INFO);
break;
}
ESP_LOGD(UDP_SERVER_TAG, "Received %d bytes from %s:", len, addr_str);
}
if (sock != -1 && len < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Shutting down socket and restarting...");
shutdown(sock, 0);
close(sock);
break;
}
}
}
vTaskDelete(NULL);
}
static uint8_t desktop_change_value(desktop_value_t *target, int8_t delta) {
if (delta > 0) {
if ((target->max - target->value) > delta) {
target->value += delta;
} else {
target->value = target->max;
}
} else {
if ((target->value - target->min) > -delta) {
target->value += delta;
} else {
target->value = target->min;
}
}
return target->value;
}
static uint8_t change_display_brightness(uint8_t display_index, int8_t delta) {
if (desktop_addr == NULL) {
ESP_LOGW(UDP_SERVER_TAG, "No desktop connected");
return 0;
}
switch (display_index) {
case 0: {
uint8_t value = desktop_change_value(&display1_brightness, delta);
tx_buffer[0] = 3;
tx_buffer[1] = 0;
tx_buffer[2] = value;
int err =
sendto(sock, tx_buffer, 3, 0, desktop_addr, sizeof(*desktop_addr));
if (err < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to send: errno %d. sock: %d",
errno, sock);
}
gui_change_display_brightness(display_index, value);
return value;
}
case 1: {
uint8_t value = desktop_change_value(&display1_brightness, delta);
tx_buffer[0] = 3;
tx_buffer[1] = 1;
tx_buffer[2] = value;
int err =
sendto(sock, tx_buffer, 3, 0, desktop_addr, sizeof(*desktop_addr));
if (err < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to send: errno %d. sock: %d",
errno, sock);
}
gui_change_display_brightness(display_index, value);
return value;
}
default:
ESP_LOGE(UDP_SERVER_TAG, "Invalid display index: %d", display_index);
return 0;
}
}
static uint8_t change_volume(int8_t delta) {
if (desktop_addr == NULL) {
ESP_LOGW(UDP_SERVER_TAG, "No desktop connected");
return 0;
}
uint8_t value = desktop_change_value(&computer_volume, delta);
tx_buffer[0] = 4;
tx_buffer[1] = value;
int err = sendto(sock, tx_buffer, 2, 0, desktop_addr, sizeof(*desktop_addr));
if (err < 0) {
char addr_str[128];
inet_ntoa_r(((struct sockaddr_in *)desktop_addr)->sin_addr, addr_str,
sizeof(addr_str) - 1);
ESP_LOGI(UDP_SERVER_TAG, "addr: %s", addr_str);
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to send: errno %d. sock: %d", errno,
sock);
}
gui_change_volume_level(value);
return value;
}
static void desktop_watch_input_task(void *arg) {
s_ui_input_t input;
for (;;) {
if (xQueueReceive(ui_input_event, &input, portMAX_DELAY)) {
switch (input.key) {
case ui_input_key_display_0_brightness:
change_display_brightness(0, input.value);
break;
case ui_input_key_display_1_brightness:
change_display_brightness(1, input.value);
break;
case ui_input_key_computer_volume:
change_volume(input.value);
break;
default:
break;
}
}
}
}
static void change_desktop_connection_status(bool connected) {
desktop_connected = connected;
if (connected) {
gui_set_server_connected();
} else {
gui_set_server_disconnected();
}
}
static void desktop_online_check_task(void *pvParameters) {
while (1) {
if (esp_timer_get_time() - last_desktop_ping_at > 5000000) { // 2 seconds
change_desktop_connection_status(false);
} else {
change_desktop_connection_status(true);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void udp_server_init(void) {
xTaskCreate(udp_server_task, "udp_server", 4096, (void *)AF_INET, 5, NULL);
xTaskCreate(desktop_online_check_task, "desktop_online_check", 1024, NULL, 10,
NULL);
xTaskCreate(desktop_watch_input_task, "desktop_watch_input", 4096, NULL, 7,
NULL);
}