// DMX definitions and DMX bridges (see bridge_dmx.h). // /dmx-definitions {name, manufacturer, model, mode, channels: [names], defaults: {name: value}}: the format of the // rig sim's room "dmx_definitions". The sim's two presets are seeded when there are none. // /dmx-bridges {name, universes: [{id, name, number, outputs: [...], fixtures: [...]}]}. A fixture without a // MAC gets a new one (locally administered, unique). Saving a running bridge applies at once; // deleting one also takes it off the running bridges. #include #include #include #include #include "api_util.h" #include "bridge.h" #include "dmx_wire.h" #include "esp_random.h" #include "store.h" #if CONFIG_IDF_TARGET_LINUX #include "bridge_dmx.h" #endif #define NAME_MAX_LEN 64 #define UNIVERSES_MAX 32 #define OUTPUTS_MAX 8 #define FIXTURES_MAX 128 // per universe /* ------------------------------------------------------------ definitions */ static const char *const BCC145[] = { "Dimmer", "Strobe", "Red", "Green", "Blue", "White", "Macro", "Speed" }; static const char *const MOVER_43[] = { "Pan", "Pan Fine", "Tilt", "Tilt Fine", "Pan/Tilt Speed", "Lens Rotate", "Zoom", "Focus", "Center Brightness", "Center Strobe", "Red 1", "Green 1", "Blue 1", "White 1", "Red 2", "Green 2", "Blue 2", "White 2", "Red 3", "Green 3", "Blue 3", "White 3", "Red 4", "Green 4", "Blue 4", "White 4", "Red 5", "Green 5", "Blue 5", "White 5", "Red 6", "Green 6", "Blue 6", "White 6", "Red 7", "Green 7", "Blue 7", "White 7", "Colour Temperature", "Colour Macro", "Static Effect", "Dynamic Effect", "Reset", }; static cJSON *make_def(const char *name, const char *maker, const char *model, const char *mode, const char *const *chans, size_t n) { cJSON *d = cJSON_CreateObject(); cJSON_AddStringToObject(d, "name", name); cJSON_AddStringToObject(d, "manufacturer", maker); cJSON_AddStringToObject(d, "model", model); cJSON_AddStringToObject(d, "mode", mode); cJSON_AddItemToObject(d, "channels", cJSON_CreateStringArray(chans, (int)n)); cJSON_AddObjectToObject(d, "defaults"); return d; } void dmx_seed_definitions(void) { if (store_coll("dmx_definition")->child) { return; } char id[16]; api_add("dmx_definition", make_def("beamZ BCC145", "beamZ", "BCC145", "8 Channel", BCC145, 8), id, sizeof(id)); api_add("dmx_definition", make_def("Small Eye 7 Mover", "Small Eye 7", "Mover", "43 Channel", MOVER_43, sizeof(MOVER_43) / sizeof(MOVER_43[0])), id, sizeof(id)); store_save("dmx_definition"); } // Trimmed string field into buf; "" when missing static void text(const cJSON *obj, const char *key, char *buf, size_t len) { const cJSON *v = cJSON_GetObjectItem(obj, key); const char *s = cJSON_IsString(v) ? v->valuestring : ""; while (isspace((unsigned char)*s)) { s++; } snprintf(buf, len, "%s", s); for (int i = (int)strlen(buf) - 1; i >= 0 && isspace((unsigned char)buf[i]); i--) { buf[i] = 0; } } static bool int_of(const cJSON *v, int *out) { if (cJSON_IsNumber(v) && v->valuedouble == (int)v->valuedouble) { *out = (int)v->valuedouble; return true; } if (cJSON_IsString(v) && v->valuestring[0]) { char *end; long n = strtol(v->valuestring, &end, 10); if (!*end) { *out = (int)n; return true; } } return false; } static cJSON *validate_def(const cJSON *data, char *err, size_t elen) { cJSON *d = cJSON_CreateObject(); char buf[NAME_MAX_LEN]; text(data, "name", buf, sizeof(buf)); if (!buf[0]) { snprintf(err, elen, "name is required"); goto fail; } cJSON_AddStringToObject(d, "name", buf); static const char *const META[] = { "manufacturer", "model", "mode" }; for (size_t i = 0; i < 3; i++) { text(data, META[i], buf, sizeof(buf)); cJSON_AddStringToObject(d, META[i], buf); } const cJSON *chans = cJSON_GetObjectItem(data, "channels"); if (!cJSON_IsArray(chans) || !cJSON_GetArraySize(chans) || cJSON_GetArraySize(chans) > DMX_CHANNELS) { snprintf(err, elen, "channels must be a list of 1 to %d names", DMX_CHANNELS); goto fail; } cJSON *out = cJSON_AddArrayToObject(d, "channels"); const cJSON *c; cJSON_ArrayForEach(c, chans) { char name[48] = ""; if (cJSON_IsString(c)) { const char *s = c->valuestring; while (isspace((unsigned char)*s)) { s++; } snprintf(name, sizeof(name), "%s", s); for (int i = (int)strlen(name) - 1; i >= 0 && isspace((unsigned char)name[i]); i--) { name[i] = 0; } } if (!name[0]) { snprintf(err, elen, "channel %d has no name", cJSON_GetArraySize(out) + 1); goto fail; } cJSON_AddItemToArray(out, cJSON_CreateString(name)); } cJSON *defs = cJSON_AddObjectToObject(d, "defaults"); const cJSON *dv; cJSON_ArrayForEach(dv, cJSON_GetObjectItem(data, "defaults")) { int v; if (!int_of(dv, &v) || v < 0 || v > 255) { snprintf(err, elen, "default of %.40s must be 0 to 255", dv->string); goto fail; } cJSON_AddNumberToObject(defs, dv->string, v); } return d; fail: cJSON_Delete(d); return NULL; } // The bridges and fixtures using definition def_id: a description of the first, or false static bool def_in_use(const char *def_id, char *who, size_t n) { const cJSON *b; cJSON_ArrayForEach(b, store_coll("dmx_bridge")) { const cJSON *u; cJSON_ArrayForEach(u, cJSON_GetObjectItem(b, "universes")) { const cJSON *f; cJSON_ArrayForEach(f, cJSON_GetObjectItem(u, "fixtures")) { const cJSON *d = cJSON_GetObjectItem(f, "definition"); if (cJSON_IsString(d) && !strcmp(d->valuestring, def_id)) { const cJSON *fn = cJSON_GetObjectItem(f, "name"), *bn = cJSON_GetObjectItem(b, "name"); snprintf(who, n, "fixture %s of bridge %s", cJSON_IsString(fn) ? fn->valuestring : "?", cJSON_IsString(bn) ? bn->valuestring : b->string); return true; } } } } return false; } static void reload_bridges_using(const char *def_id) { #if CONFIG_IDF_TARGET_LINUX const cJSON *b; cJSON_ArrayForEach(b, store_coll("dmx_bridge")) { const cJSON *u; bool uses = false; cJSON_ArrayForEach(u, cJSON_GetObjectItem(b, "universes")) { const cJSON *f; cJSON_ArrayForEach(f, cJSON_GetObjectItem(u, "fixtures")) { const cJSON *d = cJSON_GetObjectItem(f, "definition"); uses = uses || (cJSON_IsString(d) && !strcmp(d->valuestring, def_id)); } } if (uses) { bridge_dmx_reload(b->string); } } #else (void)def_id; #endif } bool dmx_definitions_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) { const int method = req->method; cJSON *all = store_coll("dmx_definition"); char err[128]; if (method == HTTP_GET || n > 2) { return false; // reads (and unknown actions) are the generic collection's } if (n == 1 && method == HTTP_POST) { cJSON *body = api_body(req); if (!body) { *ret = ESP_OK; return true; } cJSON *d = validate_def(body, err, sizeof(err)); cJSON_Delete(body); if (!d) { *ret = api_error(req, "400 Bad Request", err); return true; } char id[16]; api_add("dmx_definition", d, id, sizeof(id)); store_save("dmx_definition"); *ret = api_json(req, "201 Created", d, false); return true; } if (n != 2) { return false; } cJSON *rec = cJSON_GetObjectItem(all, seg[1]); if (!rec) { *ret = api_error(req, "404 Not Found", "DMX definition not found"); return true; } if (method == HTTP_DELETE) { char who[160]; if (def_in_use(seg[1], who, sizeof(who))) { snprintf(err, sizeof(err), "the definition is in use (%.100s)", who); *ret = api_error(req, "409 Conflict", err); return true; } cJSON_DeleteItemFromObject(all, seg[1]); store_save("dmx_definition"); *ret = api_message(req, "200 OK", "Deleted"); return true; } if (method != HTTP_PUT) { return false; } 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 *d = validate_def(merged, err, sizeof(err)); cJSON_Delete(merged); if (!d) { *ret = api_error(req, "400 Bad Request", err); return true; } // A fixture must still fit in its universe with the new channel count int nch = cJSON_GetArraySize(cJSON_GetObjectItem(d, "channels")); const cJSON *b; cJSON_ArrayForEach(b, store_coll("dmx_bridge")) { const cJSON *u; cJSON_ArrayForEach(u, cJSON_GetObjectItem(b, "universes")) { const cJSON *f; cJSON_ArrayForEach(f, cJSON_GetObjectItem(u, "fixtures")) { const cJSON *dd = cJSON_GetObjectItem(f, "definition"); const cJSON *a = cJSON_GetObjectItem(f, "address"); if (cJSON_IsString(dd) && !strcmp(dd->valuestring, seg[1]) && cJSON_IsNumber(a) && a->valueint + nch - 1 > DMX_CHANNELS) { cJSON_Delete(d); snprintf(err, sizeof(err), "%d channels from address %d of a fixture using it pass channel 512", nch, a->valueint); *ret = api_error(req, "400 Bad Request", err); return true; } } } } api_set(d, "id", cJSON_CreateString(seg[1])); cJSON_ReplaceItemInObject(all, seg[1], d); store_save("dmx_definition"); reload_bridges_using(seg[1]); *ret = api_json(req, "200 OK", d, false); return true; } /* ---------------------------------------------------------------- bridges */ static bool mac_taken(const char *mac, const char *self_bridge, const cJSON *mine) { // Fixtures of the other bridges const cJSON *b; cJSON_ArrayForEach(b, store_coll("dmx_bridge")) { if (self_bridge && !strcmp(b->string, self_bridge)) { continue; } const cJSON *u; cJSON_ArrayForEach(u, cJSON_GetObjectItem(b, "universes")) { const cJSON *f; cJSON_ArrayForEach(f, cJSON_GetObjectItem(u, "fixtures")) { const cJSON *m = cJSON_GetObjectItem(f, "mac"); if (cJSON_IsString(m) && !strcmp(m->valuestring, mac)) { return true; } } } } // This bridge's fixtures checked so far const cJSON *u; cJSON_ArrayForEach(u, cJSON_GetObjectItem(mine, "universes")) { const cJSON *f; cJSON_ArrayForEach(f, cJSON_GetObjectItem(u, "fixtures")) { const cJSON *m = cJSON_GetObjectItem(f, "mac"); if (cJSON_IsString(m) && !strcmp(m->valuestring, mac)) { return true; } } } // A real driver with that MAC (a device that is not a DMX fixture) const cJSON *dev = cJSON_GetObjectItem(store_coll("device"), mac); return dev && !cJSON_IsTrue(cJSON_GetObjectItem(dev, "dmx_fixture")); } static void new_mac(char out[13], const char *self_bridge, const cJSON *mine) { do { uint8_t m[6]; uint32_t a = esp_random(), b = esp_random(); m[0] = 0x02; // locally administered, unicast m[1] = a; m[2] = a >> 8; m[3] = a >> 16; m[4] = b; m[5] = b >> 8; snprintf(out, 13, "%02x%02x%02x%02x%02x%02x", m[0], m[1], m[2], m[3], m[4], m[5]); } while (mac_taken(out, self_bridge, mine)); } static cJSON *validate_output(const cJSON *o, int number, char *err, size_t elen) { cJSON *out = cJSON_CreateObject(); char type[16], buf[96]; text(o, "type", type, sizeof(type)); for (char *c = type; *c; c++) { *c = (char)tolower((unsigned char)*c); } if (!type[0]) { snprintf(type, sizeof(type), "artnet"); } if (strcmp(type, "artnet") && strcmp(type, "sacn") && strcmp(type, "dmx_serial") && strcmp(type, "sim")) { snprintf(err, elen, "output type must be artnet, sacn, dmx_serial or sim"); goto fail; } cJSON_AddStringToObject(out, "type", type); // sim: the universe goes to the rig simulator inside this process (no host or port; Art-Net numbering) bool serial = !strcmp(type, "dmx_serial"), sacn = !strcmp(type, "sacn"), sim = !strcmp(type, "sim"); text(o, "host", buf, sizeof(buf)); cJSON_AddStringToObject(out, "host", serial || sim ? "" : buf); const cJSON *pv = cJSON_GetObjectItem(o, "port"); int port; if (serial || sim || !pv || cJSON_IsNull(pv) || (cJSON_IsString(pv) && !pv->valuestring[0])) { cJSON_AddNullToObject(out, "port"); } else if (!int_of(pv, &port) || port < 1 || port > 65535) { snprintf(err, elen, "port must be 1 to 65535"); goto fail; } else { cJSON_AddNumberToObject(out, "port", port); } const cJSON *uv = cJSON_GetObjectItem(o, "universe"); int uni = number; if (!uv || cJSON_IsNull(uv) || (cJSON_IsString(uv) && !uv->valuestring[0])) { cJSON_AddNullToObject(out, "universe"); } else if (!int_of(uv, &uni)) { snprintf(err, elen, "an output's universe must be a number"); goto fail; } else { cJSON_AddNumberToObject(out, "universe", uni); } if (!serial && sacn && (uni < 1 || uni > 63999)) { snprintf(err, elen, "sACN universe must be 1 to 63999 (this output sends %d)", uni); goto fail; } if (!serial && !sacn && (uni < 0 || uni > 32767)) { snprintf(err, elen, "%s universe must be 0 to 32767 (this output sends %d)", sim ? "Simulator" : "Art-Net", uni); goto fail; } text(o, "serial_port", buf, sizeof(buf)); if (serial && !buf[0]) { snprintf(err, elen, "serial_port is required for dmx_serial"); goto fail; } cJSON_AddStringToObject(out, "serial_port", serial ? buf : ""); text(o, "dmx_mode", buf, sizeof(buf)); if (!buf[0]) { snprintf(buf, sizeof(buf), "enttec_pro"); } if (strcmp(buf, "enttec_pro") && strcmp(buf, "open_dmx")) { snprintf(err, elen, "dmx_mode must be enttec_pro or open_dmx"); goto fail; } cJSON_AddStringToObject(out, "dmx_mode", buf); cJSON_AddBoolToObject(out, "enabled", !cJSON_IsFalse(cJSON_GetObjectItem(o, "enabled"))); return out; fail: cJSON_Delete(out); return NULL; } // The normalised record, or NULL with err set. self: the record's id (NULL for a new one). static cJSON *validate_bridge(const cJSON *data, const char *self, char *err, size_t elen) { cJSON *doc = cJSON_CreateObject(); char buf[NAME_MAX_LEN]; text(data, "name", buf, sizeof(buf)); if (!buf[0]) { snprintf(err, elen, "name is required"); goto fail; } cJSON_AddStringToObject(doc, "name", buf); const cJSON *ulist = cJSON_GetObjectItem(data, "universes"); if (ulist && !cJSON_IsNull(ulist) && !cJSON_IsArray(ulist)) { snprintf(err, elen, "universes must be a list"); goto fail; } if (cJSON_GetArraySize(ulist) > UNIVERSES_MAX) { snprintf(err, elen, "a bridge has at most %d universes", UNIVERSES_MAX); goto fail; } cJSON *unis = cJSON_AddArrayToObject(doc, "universes"); const cJSON *defs = store_coll("dmx_definition"); int next_u = 1, next_f = 1; // ids already given keep; new ones count up past the largest const cJSON *u; cJSON_ArrayForEach(u, ulist) { char id[16]; text(u, "id", id, sizeof(id)); if (id[0] == 'u' && atoi(id + 1) >= next_u) { next_u = atoi(id + 1) + 1; } const cJSON *f; cJSON_ArrayForEach(f, cJSON_GetObjectItem(u, "fixtures")) { text(f, "id", id, sizeof(id)); if (id[0] == 'f' && atoi(id + 1) >= next_f) { next_f = atoi(id + 1) + 1; } } } int ui = 0; cJSON_ArrayForEach(u, ulist) { ui++; cJSON *nu = cJSON_CreateObject(); cJSON_AddItemToArray(unis, nu); char id[16]; text(u, "id", id, sizeof(id)); if (!id[0]) { snprintf(id, sizeof(id), "u%d", next_u++); } cJSON_AddStringToObject(nu, "id", id); text(u, "name", buf, sizeof(buf)); if (!buf[0]) { snprintf(buf, sizeof(buf), "Universe %d", ui); } cJSON_AddStringToObject(nu, "name", buf); int number = ui - 1; const cJSON *nv = cJSON_GetObjectItem(u, "number"); if (nv && !cJSON_IsNull(nv) && (!int_of(nv, &number) || number < 0 || number > 63999)) { snprintf(err, elen, "universe %s: number must be 0 to 63999", buf); goto fail; } cJSON_AddNumberToObject(nu, "number", number); const cJSON *olist = cJSON_GetObjectItem(u, "outputs"); if (cJSON_GetArraySize(olist) > OUTPUTS_MAX) { snprintf(err, elen, "universe %s: at most %d outputs", buf, OUTPUTS_MAX); goto fail; } cJSON *outs = cJSON_AddArrayToObject(nu, "outputs"); const cJSON *o; cJSON_ArrayForEach(o, olist) { char oerr[96]; cJSON *no = validate_output(o, number, oerr, sizeof(oerr)); if (!no) { snprintf(err, elen, "universe %.40s: %s", buf, oerr); goto fail; } cJSON_AddItemToArray(outs, no); } const cJSON *flist = cJSON_GetObjectItem(u, "fixtures"); if (cJSON_GetArraySize(flist) > FIXTURES_MAX) { snprintf(err, elen, "universe %s: at most %d fixtures", buf, FIXTURES_MAX); goto fail; } cJSON *fixtures = cJSON_AddArrayToObject(nu, "fixtures"); uint8_t owner[DMX_CHANNELS + 1] = { 0 }; // which fixture (1-based, in this universe) holds each channel int fi = 0; const cJSON *f; cJSON_ArrayForEach(f, flist) { fi++; cJSON *nf = cJSON_CreateObject(); char fid[16], fname[NAME_MAX_LEN], def_id[24]; text(f, "id", fid, sizeof(fid)); if (!fid[0]) { snprintf(fid, sizeof(fid), "f%d", next_f++); } text(f, "name", fname, sizeof(fname)); if (!fname[0]) { cJSON_Delete(nf); snprintf(err, elen, "universe %s: fixture %d needs a name", buf, fi); goto fail; } text(f, "definition", def_id, sizeof(def_id)); if (!def_id[0] && cJSON_IsNumber(cJSON_GetObjectItem(f, "definition"))) { snprintf(def_id, sizeof(def_id), "%d", cJSON_GetObjectItem(f, "definition")->valueint); } const cJSON *def = cJSON_GetObjectItem(defs, def_id); if (!def) { cJSON_Delete(nf); snprintf(err, elen, "fixture %s: no such DMX definition", fname); goto fail; } int nch = cJSON_GetArraySize(cJSON_GetObjectItem(def, "channels")); int addr; if (!int_of(cJSON_GetObjectItem(f, "address"), &addr) || addr < 1 || addr > DMX_CHANNELS) { cJSON_Delete(nf); snprintf(err, elen, "fixture %s: address must be 1 to 512", fname); goto fail; } if (addr + nch - 1 > DMX_CHANNELS) { cJSON_Delete(nf); snprintf(err, elen, "fixture %s: %d channels from address %d pass channel 512", fname, nch, addr); goto fail; } for (int c = addr; c < addr + nch; c++) { if (owner[c]) { const cJSON *other = cJSON_GetArrayItem(fixtures, owner[c] - 1); cJSON_Delete(nf); snprintf(err, elen, "fixture %s: channel %d is already used by %s", fname, c, cJSON_GetObjectItem(other, "name")->valuestring); goto fail; } owner[c] = (uint8_t)fi; } const cJSON *mv = cJSON_GetObjectItem(f, "mac"); char mac[13]; if (!mv || cJSON_IsNull(mv) || (cJSON_IsString(mv) && !mv->valuestring[0])) { new_mac(mac, self, doc); } else if (!api_mac(mv, mac)) { cJSON_Delete(nf); snprintf(err, elen, "fixture %s: mac must be 12 hex digits", fname); goto fail; } else { int first = (int)strtol((char[3]){ mac[0], mac[1], 0 }, NULL, 16); if ((first & 1) || !strcmp(mac, "000000000000")) { cJSON_Delete(nf); snprintf(err, elen, "fixture %s: mac must be a unicast address, not zero", fname); goto fail; } if (mac_taken(mac, self, doc)) { cJSON_Delete(nf); snprintf(err, elen, "fixture %s: mac %s is already used by another fixture or driver", fname, mac); goto fail; } } cJSON_AddStringToObject(nf, "id", fid); cJSON_AddStringToObject(nf, "name", fname); cJSON_AddStringToObject(nf, "mac", mac); cJSON_AddStringToObject(nf, "definition", def_id); cJSON_AddNumberToObject(nf, "address", addr); cJSON_AddItemToArray(fixtures, nf); } } return doc; fail: cJSON_Delete(doc); return NULL; } bool dmx_bridges_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) { const int method = req->method; cJSON *all = store_coll("dmx_bridge"); char err[160]; if (method == HTTP_GET || n > 2) { return false; } if (n == 1 && method == HTTP_POST) { cJSON *body = api_body(req); if (!body) { *ret = ESP_OK; return true; } cJSON *doc = validate_bridge(body, NULL, err, sizeof(err)); cJSON_Delete(body); if (!doc) { *ret = api_error(req, "400 Bad Request", err); return true; } char id[16]; api_add("dmx_bridge", doc, id, sizeof(id)); store_save("dmx_bridge"); *ret = api_json(req, "201 Created", doc, false); return true; } if (n != 2) { return false; } cJSON *rec = cJSON_GetObjectItem(all, seg[1]); if (!rec) { *ret = api_error(req, "404 Not Found", "DMX bridge not found"); return true; } char addr[BRIDGE_ADDR_MAX]; snprintf(addr, sizeof(addr), "dmx:%s", seg[1]); if (method == HTTP_DELETE) { cJSON_DeleteItemFromObject(all, seg[1]); store_save("dmx_bridge"); api_bridges_remove(addr); *ret = api_message(req, "200 OK", "Deleted"); return true; } if (method != HTTP_PUT) { return false; } 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_bridge(merged, seg[1], 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("dmx_bridge"); #if CONFIG_IDF_TARGET_LINUX bridge_dmx_reload(seg[1]); #endif *ret = api_json(req, "200 OK", doc, false); return true; }