/* * Art-Net and DDP received for the simulated rig's network fixtures: see sim_net.h. * The Art-Net side follows the qlcplus stage simulator (technicalkiwi/qlcplus simulator/artnet.py). */ #include "sim_net.h" #include #include #include #include #include "esp_log.h" #include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "freertos/task.h" #include "lwip/sockets.h" static const char *TAG = "sim_net"; #define ARTNET_PORT 6454 #define OP_POLL 0x2000 #define OP_POLL_REPLY 0x2100 #define OP_DMX 0x5000 #define MAX_UNIVERSES 64 #define TICK_MS 25 // 40 frames a second to the page typedef struct { int uni; // -1: unused uint8_t data[512]; bool dirty; uint32_t packets; } universe_t; typedef struct { uint16_t port; // 0: unused int fd; char error[64]; uint8_t rgb[SIM_NET_DDP_PIXELS * 3]; size_t len; // bytes written so far (the highest offset + length) bool dirty; uint32_t packets; char last_from[16]; } ddp_t; static SemaphoreHandle_t s_mu; static universe_t s_uni[MAX_UNIVERSES]; static ddp_t s_ddp[SIM_NET_MAX_DDP_PORTS]; static uint16_t s_want[SIM_NET_MAX_DDP_PORTS]; // the ports the room asks for static int s_nwant; static bool s_want_changed; static int s_art_fd = -1, s_art_port = ARTNET_PORT; static char s_art_error[64], s_art_from[16]; static uint32_t s_art_packets, s_art_polls, s_local_packets; static int udp_bind(uint16_t port, char *err, size_t errn) { int fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { snprintf(err, errn, "socket: errno %d", errno); return -1; } int one = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); #ifdef SO_REUSEPORT setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); #endif setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(port), .sin_addr.s_addr = htonl(INADDR_ANY) }; if (bind(fd, (struct sockaddr *)&a, sizeof(a)) != 0) { snprintf(err, errn, "bind %u: errno %d", port, errno); close(fd); return -1; } fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); err[0] = 0; return fd; } static universe_t *universe_slot(int uni) { universe_t *free_slot = NULL; for (int i = 0; i < MAX_UNIVERSES; i++) { if (s_uni[i].uni == uni) { return &s_uni[i]; } if (s_uni[i].uni < 0 && !free_slot) { free_slot = &s_uni[i]; } } if (free_slot) { free_slot->uni = uni; memset(free_slot->data, 0, sizeof(free_slot->data)); } return free_slot; } // The address this machine uses toward the peer (the IP an ArtPollReply names) static void local_ip_toward(const struct sockaddr_in *peer, uint8_t ip[4]) { static const uint8_t LOOP[4] = { 127, 0, 0, 1 }; memcpy(ip, LOOP, 4); int fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { return; } struct sockaddr_in to = *peer, me; socklen_t n = sizeof(me); if (connect(fd, (struct sockaddr *)&to, sizeof(to)) == 0 && getsockname(fd, (struct sockaddr *)&me, &n) == 0) { memcpy(ip, &me.sin_addr.s_addr, 4); } close(fd); } static void poll_reply(const struct sockaddr_in *from) { uint8_t pkt[239] = { 0 }; memcpy(pkt, "Art-Net\0", 8); pkt[8] = OP_POLL_REPLY & 0xFF; pkt[9] = OP_POLL_REPLY >> 8; pkt[11] = 14; local_ip_toward(from, pkt + 12); pkt[16] = ARTNET_PORT & 0xFF; pkt[17] = ARTNET_PORT >> 8; pkt[19] = 1; pkt[25] = 0xD0; memcpy(pkt + 26, "led-sim", 7); // short name (17 bytes from 26) memcpy(pkt + 44, "LED controller simulated rig", 28); // long name (63 bytes from 44) pkt[173] = 1; // NumPorts pkt[174] = 0x80; // port 0 outputs DMX pkt[182] = 0x80; // GoodOutput: data is being output sendto(s_art_fd, pkt, sizeof(pkt), 0, (const struct sockaddr *)from, sizeof(*from)); } static void artnet_rx(const uint8_t *p, int n, const struct sockaddr_in *from) { if (n < 10 || memcmp(p, "Art-Net\0", 8) != 0) { return; } int op = p[8] | (p[9] << 8); if (op == OP_POLL) { s_art_polls++; poll_reply(from); return; } if (op != OP_DMX || n < 18) { return; } int uni = p[14] | ((p[15] & 0x7F) << 8); int len = (p[16] << 8) | p[17]; if (len < 2 || len > 512 || 18 + len > n) { return; } xSemaphoreTake(s_mu, portMAX_DELAY); universe_t *u = universe_slot(uni); if (u) { memcpy(u->data, p + 18, (size_t)len); u->dirty = true; u->packets++; } s_art_packets++; inet_ntop(AF_INET, &from->sin_addr, s_art_from, sizeof(s_art_from)); xSemaphoreGive(s_mu); } static void ddp_rx(ddp_t *d, const uint8_t *p, int n, const struct sockaddr_in *from) { if (n < 10 || (p[0] & 0xC0) != 0x40) { return; } int hdr = (p[0] & 0x10) ? 14 : 10; // the timecode flag adds 4 bytes uint32_t off = ((uint32_t)p[4] << 24) | ((uint32_t)p[5] << 16) | ((uint32_t)p[6] << 8) | p[7]; uint32_t len = ((uint32_t)p[8] << 8) | p[9]; if (n < hdr || len > (uint32_t)(n - hdr) || off >= sizeof(d->rgb)) { return; } if (off + len > sizeof(d->rgb)) { len = sizeof(d->rgb) - off; } xSemaphoreTake(s_mu, portMAX_DELAY); memcpy(d->rgb + off, p + hdr, len); if (off + len > d->len) { d->len = off + len; } d->dirty = true; d->packets++; inet_ntop(AF_INET, &from->sin_addr, d->last_from, sizeof(d->last_from)); xSemaphoreGive(s_mu); } // Bring the DDP sockets in line with the ports the room asks for static void sync_ddp(void) { xSemaphoreTake(s_mu, portMAX_DELAY); if (!s_want_changed) { xSemaphoreGive(s_mu); return; } s_want_changed = false; uint16_t want[SIM_NET_MAX_DDP_PORTS]; int nwant = s_nwant; memcpy(want, s_want, sizeof(want)); for (int i = 0; i < SIM_NET_MAX_DDP_PORTS; i++) { // close the ports no longer wanted bool keep = false; for (int k = 0; k < nwant; k++) { keep |= s_ddp[i].port && s_ddp[i].port == want[k]; } if (s_ddp[i].port && !keep) { if (s_ddp[i].fd >= 0) { close(s_ddp[i].fd); } ESP_LOGI(TAG, "DDP: stopped listening on %u", s_ddp[i].port); memset(&s_ddp[i], 0, sizeof(s_ddp[i])); s_ddp[i].fd = -1; } } for (int k = 0; k < nwant; k++) { // open the new ones bool have = false; for (int i = 0; i < SIM_NET_MAX_DDP_PORTS; i++) { have |= s_ddp[i].port == want[k]; } for (int i = 0; !have && i < SIM_NET_MAX_DDP_PORTS; i++) { if (!s_ddp[i].port) { s_ddp[i].port = want[k]; s_ddp[i].fd = udp_bind(want[k], s_ddp[i].error, sizeof(s_ddp[i].error)); if (s_ddp[i].fd >= 0) { ESP_LOGI(TAG, "DDP: listening on UDP %u", want[k]); } else { ESP_LOGW(TAG, "DDP: %s", s_ddp[i].error); } have = true; } } } xSemaphoreGive(s_mu); } // Send what changed since the last tick to the page static void flush(void) { static uint8_t frame[1 + 6 + SIM_NET_DDP_PIXELS * 3]; for (int i = 0; i < MAX_UNIVERSES; i++) { xSemaphoreTake(s_mu, portMAX_DELAY); bool send = s_uni[i].uni >= 0 && s_uni[i].dirty; if (send) { frame[0] = 1; frame[1] = s_uni[i].uni & 0xFF; frame[2] = s_uni[i].uni >> 8; memcpy(frame + 3, s_uni[i].data, 512); s_uni[i].dirty = false; } xSemaphoreGive(s_mu); if (send) { sim_rig_page_send(frame, 3 + 512); } } for (int i = 0; i < SIM_NET_MAX_DDP_PORTS; i++) { xSemaphoreTake(s_mu, portMAX_DELAY); size_t len = s_ddp[i].len; bool send = s_ddp[i].port && s_ddp[i].dirty; if (send) { frame[0] = 2; frame[1] = s_ddp[i].port & 0xFF; frame[2] = s_ddp[i].port >> 8; memset(frame + 3, 0, 4); // offset 0: the whole buffer memcpy(frame + 7, s_ddp[i].rgb, len); s_ddp[i].dirty = false; } xSemaphoreGive(s_mu); if (send) { sim_rig_page_send(frame, 7 + len); } } } static void net_task(void *arg) { (void)arg; static uint8_t buf[1500]; int64_t last = esp_timer_get_time(); // not xTaskGetTickCount: the desktop build's FreeRTOS has none for (;;) { sync_ddp(); fd_set rd; FD_ZERO(&rd); int top = -1; if (s_art_fd >= 0) { FD_SET(s_art_fd, &rd); top = s_art_fd; } for (int i = 0; i < SIM_NET_MAX_DDP_PORTS; i++) { if (s_ddp[i].fd >= 0 && s_ddp[i].port) { FD_SET(s_ddp[i].fd, &rd); top = s_ddp[i].fd > top ? s_ddp[i].fd : top; } } struct timeval tv = { .tv_sec = 0, .tv_usec = 5000 }; int ready = top >= 0 ? select(top + 1, &rd, NULL, NULL, &tv) : 0; if (top < 0) { vTaskDelay(pdMS_TO_TICKS(TICK_MS)); } if (ready > 0) { struct sockaddr_in from; socklen_t fl; int n; if (s_art_fd >= 0 && FD_ISSET(s_art_fd, &rd)) { while (fl = sizeof(from), (n = recvfrom(s_art_fd, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fl)) > 0) { artnet_rx(buf, n, &from); } } for (int i = 0; i < SIM_NET_MAX_DDP_PORTS; i++) { if (s_ddp[i].fd >= 0 && s_ddp[i].port && FD_ISSET(s_ddp[i].fd, &rd)) { while (fl = sizeof(from), (n = recvfrom(s_ddp[i].fd, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fl)) > 0) { ddp_rx(&s_ddp[i], buf, n, &from); } } } } else { vTaskDelay(1); // let the other tasks in (select blocks the host thread, not the scheduler) } int64_t now = esp_timer_get_time(); if (now - last >= TICK_MS * 1000) { last = now; flush(); } } } void sim_net_start(void) { if (s_mu) { return; } s_mu = xSemaphoreCreateMutex(); for (int i = 0; i < MAX_UNIVERSES; i++) { s_uni[i].uni = -1; } for (int i = 0; i < SIM_NET_MAX_DDP_PORTS; i++) { s_ddp[i].fd = -1; } const char *env = getenv("LED_SIM_ARTNET_PORT"); if (env && env[0]) { s_art_port = atoi(env); } if (s_art_port > 0 && s_art_port < 65536) { s_art_fd = udp_bind((uint16_t)s_art_port, s_art_error, sizeof(s_art_error)); if (s_art_fd >= 0) { ESP_LOGI(TAG, "Art-Net: listening on UDP %d", s_art_port); } else { ESP_LOGW(TAG, "Art-Net: %s (set LED_SIM_ARTNET_PORT to use another port)", s_art_error); } } else { snprintf(s_art_error, sizeof(s_art_error), "off (LED_SIM_ARTNET_PORT=0)"); } if (xTaskCreate(net_task, "sim_net", 8192, NULL, 4, NULL) != pdPASS) { ESP_LOGE(TAG, "no task: Art-Net and DDP fixtures stay dark"); } } void sim_net_put_universe(int uni, const uint8_t data[512]) { if (!s_mu || uni < 0 || uni > 32767) { return; } xSemaphoreTake(s_mu, portMAX_DELAY); universe_t *u = universe_slot(uni); if (u) { memcpy(u->data, data, 512); u->dirty = true; u->packets++; } s_local_packets++; xSemaphoreGive(s_mu); } void sim_net_set_ddp_ports(const uint16_t *ports, int n) { if (!s_mu) { return; } xSemaphoreTake(s_mu, portMAX_DELAY); int k = 0; for (int i = 0; i < n && k < SIM_NET_MAX_DDP_PORTS; i++) { bool dup = !ports[i]; for (int j = 0; j < k; j++) { dup |= s_want[j] == ports[i]; } if (!dup) { s_want[k++] = ports[i]; } } s_nwant = k; s_want_changed = true; // sync_ddp only opens and closes the ports that differ xSemaphoreGive(s_mu); } static void add_hex(cJSON *o, const char *key, const uint8_t *p, size_t n) { char *h = malloc(n * 2 + 1); if (!h) { return; } for (size_t i = 0; i < n; i++) { snprintf(h + i * 2, 3, "%02x", p[i]); } h[n * 2] = 0; cJSON_AddStringToObject(o, key, h); free(h); } cJSON *sim_net_status(bool with_data) { cJSON *o = cJSON_CreateObject(); if (!s_mu) { return o; } xSemaphoreTake(s_mu, portMAX_DELAY); cJSON *a = cJSON_AddObjectToObject(o, "artnet"); cJSON_AddNumberToObject(a, "port", s_art_port); cJSON_AddBoolToObject(a, "listening", s_art_fd >= 0); if (s_art_error[0]) { cJSON_AddStringToObject(a, "error", s_art_error); } cJSON_AddNumberToObject(a, "packets", s_art_packets); cJSON_AddNumberToObject(a, "polls", s_art_polls); cJSON_AddNumberToObject(a, "local_packets", s_local_packets); // from the controller's DMX bridges cJSON_AddStringToObject(a, "last_from", s_art_from); cJSON *us = cJSON_AddObjectToObject(a, "universes"); for (int i = 0; i < MAX_UNIVERSES; i++) { if (s_uni[i].uni < 0) { continue; } char k[12]; snprintf(k, sizeof(k), "%d", s_uni[i].uni); cJSON *u = cJSON_AddObjectToObject(us, k); cJSON_AddNumberToObject(u, "packets", s_uni[i].packets); if (with_data) { add_hex(u, "data", s_uni[i].data, 512); } } cJSON *d = cJSON_AddObjectToObject(o, "ddp"); for (int i = 0; i < SIM_NET_MAX_DDP_PORTS; i++) { if (!s_ddp[i].port) { continue; } char k[12]; snprintf(k, sizeof(k), "%u", s_ddp[i].port); cJSON *p = cJSON_AddObjectToObject(d, k); cJSON_AddBoolToObject(p, "listening", s_ddp[i].fd >= 0); if (s_ddp[i].error[0]) { cJSON_AddStringToObject(p, "error", s_ddp[i].error); } cJSON_AddNumberToObject(p, "packets", s_ddp[i].packets); cJSON_AddNumberToObject(p, "pixels", (double)(s_ddp[i].len / 3)); cJSON_AddStringToObject(p, "last_from", s_ddp[i].last_from); if (with_data) { add_hex(p, "data", s_ddp[i].rgb, s_ddp[i].len); } } xSemaphoreGive(s_mu); return o; }