// Pixel outputs: the v1 app's models/output.py and controllers/output.py. Network fixtures (DDP, // Art-Net, sACN) are reached over Ethernet; USB-DMX (dmx_serial) records are kept but this board // has no USB-DMX adapter to drive. #include #include #include #include #include #include "api_util.h" #include "esp_mac.h" #include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "lwip/netdb.h" #include "lwip/sockets.h" #include "store.h" static const char *const PROTOCOLS[] = { "ddp", "artnet", "sacn", "dmx_serial" }; static const char *const COLOR_ORDERS[] = { "rgb", "rbg", "grb", "gbr", "brg", "bgr" }; static const char *const DMX_MODES[] = { "enttec_pro", "open_dmx" }; #define COUNT(a) (sizeof(a) / sizeof((a)[0])) #define UNIVERSE_BYTES 510 // 170 RGB pixels per DMX universe #define DDP_MAX_DATA 1440 static bool one_of(const char *s, const char *const *list, size_t n) { for (size_t i = 0; i < n; i++) { if (!strcmp(s, list[i])) { return true; } } return false; } static cJSON *defaults(void) { return cJSON_Parse("{\"name\":\"\",\"protocol\":\"ddp\",\"host\":\"\",\"port\":null,\"universe\":null,\"channel\":1," "\"serial_port\":\"\",\"dmx_mode\":\"enttec_pro\",\"pixel_count\":60,\"pixel_offset\":0," "\"color_order\":\"rgb\",\"reverse\":false,\"brightness\":255,\"fps\":40,\"enabled\":true}"); } // Lower-cased, trimmed copy of a string field ("" when missing) static void text(const cJSON *doc, const char *key, char *buf, size_t len, bool lower) { const cJSON *v = cJSON_GetObjectItem(doc, key); const char *s = cJSON_IsString(v) ? v->valuestring : ""; while (isspace((unsigned char)*s)) { s++; } snprintf(buf, len, "%s", s); for (int i = strlen(buf) - 1; i >= 0 && isspace((unsigned char)buf[i]); i--) { buf[i] = 0; } for (char *c = buf; lower && *c; c++) { *c = tolower((unsigned char)*c); } } // Integer field in [lo, hi]; numeric strings are accepted. Writes the error into err. static bool int_field(cJSON *doc, const char *key, int lo, int hi, bool allow_null, char *err, size_t elen) { const cJSON *v = cJSON_GetObjectItem(doc, key); if (!v || cJSON_IsNull(v) || (cJSON_IsString(v) && !v->valuestring[0])) { if (allow_null) { api_set(doc, key, cJSON_CreateNull()); return true; } snprintf(err, elen, "%s is required", key); return false; } char *end = NULL; double d = cJSON_IsNumber(v) ? v->valuedouble : (cJSON_IsString(v) ? strtod(v->valuestring, &end) : 0.5); if ((end && *end) || d != (long)d) { snprintf(err, elen, "%s must be an integer", key); return false; } if (d < lo || d > hi) { snprintf(err, elen, "%s must be between %d and %d", key, lo, hi); return false; } api_set(doc, key, cJSON_CreateNumber((long)d)); return true; } // Defaults, then data; returns the normalised record or NULL with err set static cJSON *validate(const cJSON *data, char *err, size_t elen) { cJSON *doc = defaults(); api_merge(doc, data); char buf[64]; text(doc, "name", buf, sizeof(buf), false); if (!buf[0]) { snprintf(err, elen, "name is required"); goto fail; } api_set(doc, "name", cJSON_CreateString(buf)); char proto[16]; text(doc, "protocol", proto, sizeof(proto), true); if (!one_of(proto, PROTOCOLS, COUNT(PROTOCOLS))) { snprintf(err, elen, "protocol must be one of: ddp, artnet, sacn, dmx_serial"); goto fail; } api_set(doc, "protocol", cJSON_CreateString(proto)); text(doc, "host", buf, sizeof(buf), false); api_set(doc, "host", cJSON_CreateString(buf)); if (!strcmp(proto, "ddp") && !buf[0]) { snprintf(err, elen, "host is required for ddp"); goto fail; } text(doc, "serial_port", buf, sizeof(buf), false); api_set(doc, "serial_port", cJSON_CreateString(buf)); bool dmx = !strcmp(proto, "dmx_serial"); if (dmx) { if (!buf[0]) { snprintf(err, elen, "serial_port is required for dmx_serial"); goto fail; } text(doc, "dmx_mode", buf, sizeof(buf), true); if (!one_of(buf[0] ? buf : "enttec_pro", DMX_MODES, COUNT(DMX_MODES))) { snprintf(err, elen, "dmx_mode must be one of: enttec_pro, open_dmx"); goto fail; } api_set(doc, "dmx_mode", cJSON_CreateString(buf[0] ? buf : "enttec_pro")); } bool sacn = !strcmp(proto, "sacn"); if (!int_field(doc, "port", 1, 65535, true, err, elen) || !int_field(doc, "universe", sacn ? 1 : 0, sacn ? 63999 : 0x7FFF, true, err, elen) || !int_field(doc, "channel", 1, 512, false, err, elen) || !int_field(doc, "pixel_count", 1, 100000, false, err, elen) || !int_field(doc, "pixel_offset", 0, 100000, false, err, elen) || !int_field(doc, "brightness", 0, 255, false, err, elen) || !int_field(doc, "fps", 1, 120, false, err, elen)) { goto fail; } if (dmx) { int ch = cJSON_GetObjectItem(doc, "channel")->valueint; int max_px = (512 - ch + 1) / 3; if (cJSON_GetObjectItem(doc, "pixel_count")->valueint > max_px) { snprintf(err, elen, "pixel_count must be at most %d for one DMX universe from channel %d", max_px, ch); goto fail; } } text(doc, "color_order", buf, sizeof(buf), true); if (!one_of(buf[0] ? buf : "rgb", COLOR_ORDERS, COUNT(COLOR_ORDERS))) { snprintf(err, elen, "color_order must be one of: rgb, rbg, grb, gbr, brg, bgr"); goto fail; } api_set(doc, "color_order", cJSON_CreateString(buf[0] ? buf : "rgb")); api_set(doc, "reverse", cJSON_CreateBool(cJSON_IsTrue(cJSON_GetObjectItem(doc, "reverse")))); api_set(doc, "enabled", cJSON_CreateBool(!cJSON_IsFalse(cJSON_GetObjectItem(doc, "enabled")))); return doc; fail: cJSON_Delete(doc); return NULL; } /* ------------------------------------------------------------ test frames */ typedef struct { char name[64], proto[16], host[64], order[4]; int port, universe, pixels, brightness, fps, id; int sock; struct sockaddr_in to; uint8_t seq; uint8_t cid[16]; int frames, packets, bytes, errors; char last_error[96]; } out_t; static void fail(out_t *o, const char *what) { o->errors++; snprintf(o->last_error, sizeof(o->last_error), "%s: errno %d", what, errno); } static bool resolve(out_t *o, int universe_for_sacn) { memset(&o->to, 0, sizeof(o->to)); o->to.sin_family = AF_INET; o->to.sin_port = htons(o->port); if (!o->host[0]) { if (!strcmp(o->proto, "sacn")) { // multicast group of the universe o->to.sin_addr.s_addr = htonl(0xEFFF0000 | (universe_for_sacn & 0xFFFF)); } else { o->to.sin_addr.s_addr = htonl(INADDR_BROADCAST); } return true; } if (inet_aton(o->host, &o->to.sin_addr)) { return true; } struct addrinfo hints = { .ai_family = AF_INET, .ai_socktype = SOCK_DGRAM }, *res = NULL; if (getaddrinfo(o->host, NULL, &hints, &res) != 0 || !res) { o->errors++; snprintf(o->last_error, sizeof(o->last_error), "cannot resolve %s", o->host); return false; } o->to.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr; freeaddrinfo(res); return true; } static void send_pkt(out_t *o, const uint8_t *pkt, size_t len) { if (sendto(o->sock, pkt, len, 0, (struct sockaddr *)&o->to, sizeof(o->to)) < 0) { fail(o, "send"); } else { o->packets++; o->bytes += len; } } static void send_ddp(out_t *o, const uint8_t *data, size_t len) { static uint8_t pkt[10 + DDP_MAX_DATA]; o->seq = o->seq % 15 + 1; for (size_t off = 0; off < len; off += DDP_MAX_DATA) { size_t n = len - off < DDP_MAX_DATA ? len - off : DDP_MAX_DATA; bool last = off + n >= len; uint8_t h[10] = { 0x40 | (last ? 0x01 : 0), o->seq, 0x0B, 1, off >> 24, off >> 16, off >> 8, off, n >> 8, n }; memcpy(pkt, h, 10); memcpy(pkt + 10, data + off, n); send_pkt(o, pkt, 10 + n); } } static void send_artnet(out_t *o, const uint8_t *data, size_t len) { static uint8_t pkt[18 + UNIVERSE_BYTES + 1]; o->seq = o->seq % 255 + 1; int base = o->universe; for (size_t off = 0, u = 0; off < len; off += UNIVERSE_BYTES, u++) { size_t n = len - off < UNIVERSE_BYTES ? len - off : UNIVERSE_BYTES; size_t dl = n + (n & 1); dl = dl < 2 ? 2 : dl; int uni = base + u; memcpy(pkt, "Art-Net\0", 8); uint8_t h[10] = { 0x00, 0x50, 0, 14, o->seq, 0, uni & 0xFF, (uni >> 8) & 0x7F, dl >> 8, dl & 0xFF }; memcpy(pkt + 8, h, 10); memset(pkt + 18, 0, dl); memcpy(pkt + 18, data + off, n); send_pkt(o, pkt, 18 + dl); } } static void put16(uint8_t *p, unsigned v) { p[0] = v >> 8; p[1] = v; } static void send_sacn(out_t *o, const uint8_t *data, size_t len) { static uint8_t pkt[126 + UNIVERSE_BYTES]; o->seq++; for (size_t off = 0, u = 0; off < len; off += UNIVERSE_BYTES, u++) { size_t n = len - off < UNIVERSE_BYTES ? len - off : UNIVERSE_BYTES; int uni = o->universe + u; size_t total = 126 + n; memset(pkt, 0, 126); put16(pkt, 0x0010); memcpy(pkt + 4, "ASC-E1.17\0\0\0", 12); put16(pkt + 16, 0x7000 | (total - 16)); pkt[21] = 0x04; memcpy(pkt + 22, o->cid, 16); put16(pkt + 38, 0x7000 | (total - 38)); pkt[43] = 0x02; snprintf((char *)pkt + 44, 64, "led-controller %.48s", o->name); pkt[108] = 100; pkt[111] = o->seq; put16(pkt + 113, uni); put16(pkt + 115, 0x7000 | (total - 115)); pkt[117] = 0x02; pkt[118] = 0xA1; put16(pkt + 121, 0x0001); put16(pkt + 123, n + 1); memcpy(pkt + 126, data + off, n); if (!o->host[0]) { o->to.sin_addr.s_addr = htonl(0xEFFF0000 | (uni & 0xFFFF)); } send_pkt(o, pkt, total); } } static void send_frame(out_t *o, const uint8_t *frame, size_t len) { if (!strcmp(o->proto, "ddp")) { send_ddp(o, frame, len); } else if (!strcmp(o->proto, "artnet")) { send_artnet(o, frame, len); } else { send_sacn(o, frame, len); } o->frames++; } // Solid colour with the output's brightness and colour order applied static void fill(const out_t *o, uint8_t *frame, const uint8_t rgb[3]) { uint8_t c[3]; for (int k = 0; k < 3; k++) { int src = o->order[k] == 'r' ? 0 : o->order[k] == 'g' ? 1 : 2; c[k] = (rgb[src] * o->brightness + 127) / 255; } for (int p = 0; p < o->pixels; p++) { memcpy(frame + 3 * p, c, 3); } } static cJSON *run_test(out_t *o, double step_s) { static const uint8_t STEPS[][3] = { { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 }, { 255, 255, 255 } }; static const uint8_t BLACK[3] = { 0, 0, 0 }; size_t len = (size_t)o->pixels * 3; uint8_t *frame = malloc(len); o->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (o->sock < 0) { fail(o, "socket"); } else { int on = 1; setsockopt(o->sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); } if (frame && o->sock >= 0 && resolve(o, o->universe)) { int64_t gap_us = 1000000 / o->fps; for (size_t s = 0; s < COUNT(STEPS); s++) { fill(o, frame, STEPS[s]); int64_t end = esp_timer_get_time() + (int64_t)(step_s * 1e6); do { send_frame(o, frame, len); TickType_t t = pdMS_TO_TICKS(gap_us / 1000); vTaskDelay(t ? t : 1); } while (esp_timer_get_time() < end); } fill(o, frame, BLACK); send_frame(o, frame, len); } if (o->sock >= 0) { close(o->sock); } free(frame); cJSON *st = cJSON_CreateObject(); cJSON_AddStringToObject(st, "name", o->name); cJSON_AddStringToObject(st, "protocol", o->proto); cJSON_AddStringToObject(st, "kind", "frame"); cJSON_AddNumberToObject(st, "pixel_count", o->pixels); cJSON_AddNumberToObject(st, "frames", o->frames); cJSON_AddNumberToObject(st, "packets", o->packets); cJSON_AddNumberToObject(st, "bytes", o->bytes); cJSON_AddNumberToObject(st, "errors", o->errors); cJSON_AddItemToObject(st, "last_error", o->last_error[0] ? cJSON_CreateString(o->last_error) : cJSON_CreateNull()); return st; } static int num(const cJSON *rec, const char *key, int dflt) { const cJSON *v = cJSON_GetObjectItem(rec, key); return cJSON_IsNumber(v) ? v->valueint : dflt; } // POST /outputs//test {step_s}: solid red, green, blue and white for step_s each, then black static esp_err_t test_output(httpd_req_t *req, const cJSON *rec, const char *id) { cJSON *body = api_body_opt(req); if (!body) { return ESP_OK; } const cJSON *sv = cJSON_GetObjectItem(body, "step_s"); double step_s = cJSON_IsNumber(sv) ? sv->valuedouble : 1.0; step_s = step_s < 0.1 ? 0.1 : (step_s > 5 ? 5 : step_s); cJSON_Delete(body); out_t *o = calloc(1, sizeof(*o)); if (!o) { return api_error(req, "500 Internal Server Error", "out of memory"); } text(rec, "name", o->name, sizeof(o->name), false); text(rec, "protocol", o->proto, sizeof(o->proto), true); text(rec, "host", o->host, sizeof(o->host), false); text(rec, "color_order", o->order, sizeof(o->order), true); if (strlen(o->order) != 3) { strcpy(o->order, "rgb"); } if (!strcmp(o->proto, "dmx_serial")) { free(o); return api_error(req, "503 Service Unavailable", "Test failed: this board has no USB-DMX adapter"); } bool sacn = !strcmp(o->proto, "sacn"); o->port = num(rec, "port", !strcmp(o->proto, "ddp") ? 4048 : sacn ? 5568 : 6454); o->universe = num(rec, "universe", sacn ? 1 : 0); o->pixels = num(rec, "pixel_count", 60); o->brightness = num(rec, "brightness", 255); o->fps = num(rec, "fps", 40); uint8_t mac[6]; esp_read_mac(mac, ESP_MAC_ETH); memcpy(o->cid, mac, 6); snprintf((char *)o->cid + 6, 10, "%.9s", id); store_unlock(); // frames go out for several seconds; keep the sequencer running cJSON *st = run_test(o, step_s); store_lock(); free(o); cJSON *r = cJSON_CreateObject(); cJSON_AddStringToObject(r, "message", "Test sent"); cJSON_AddItemToObject(r, "status", st); return api_json(req, "200 OK", r, true); } bool outputs_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) { cJSON *all = store_coll("output"); const int method = req->method; char err[96]; if (n == 1 && method == HTTP_POST) { cJSON *body = api_body(req); if (!body) { *ret = ESP_OK; return true; } cJSON *doc = validate(body, err, sizeof(err)); cJSON_Delete(body); if (!doc) { *ret = api_error(req, "400 Bad Request", err); return true; } char id[16]; api_add("output", doc, id, sizeof(id)); store_save("output"); *ret = api_json(req, "201 Created", doc, false); return true; } if (n < 2 || (method != HTTP_PUT && !(n == 3 && method == HTTP_POST))) { return false; } const cJSON *rec = cJSON_GetObjectItem(all, seg[1]); if (!rec) { *ret = api_error(req, "404 Not Found", "Output not found"); return true; } if (n == 3) { *ret = !strcmp(seg[2], "test") ? test_output(req, rec, seg[1]) : api_error(req, "404 Not Found", "unknown action"); return true; } cJSON *body = api_body(req); if (!body) { *ret = ESP_OK; return true; } cJSON *merged = cJSON_Duplicate(rec, true); api_merge(merged, body); cJSON_Delete(body); cJSON *doc = validate(merged, err, sizeof(err)); cJSON_Delete(merged); if (!doc) { *ret = api_error(req, "400 Bad Request", err); return true; } api_set(doc, "id", cJSON_CreateString(seg[1])); cJSON_ReplaceItemInObject(all, seg[1], doc); store_save("output"); *ret = api_json(req, "200 OK", doc, false); return true; }