videohubctrl can be used to control Blackmagic Design Videohub SDI router device over the network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmd.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * === Commands processing ===
  3. *
  4. * Blackmagic Design Videohub control application
  5. * Copyright (C) 2014 Unix Solutions Ltd.
  6. * Written by Georgi Chorbadzhiyski
  7. *
  8. * Released under MIT license.
  9. * See LICENSE-MIT.txt for license terms.
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stddef.h>
  15. #include <string.h>
  16. #include "data.h"
  17. #include "cmd.h"
  18. #include "util.h"
  19. const char *videohub_commands_text[NUM_COMMANDS] = {
  20. [CMD_PROTOCOL_PREAMBLE] = "PROTOCOL PREAMBLE",
  21. [CMD_VIDEOHUB_DEVICE] = "VIDEOHUB DEVICE",
  22. [CMD_INPUT_LABELS] = "INPUT LABELS",
  23. [CMD_OUTPUT_LABELS] = "OUTPUT LABELS",
  24. [CMD_VIDEO_OUTPUT_LOCKS] = "VIDEO OUTPUT LOCKS",
  25. [CMD_VIDEO_OUTPUT_ROUTING] = "VIDEO OUTPUT ROUTING",
  26. [CMD_VIDEO_INPUT_STATUS] = "VIDEO INPUT STATUS",
  27. [CMD_VIDEO_OUTPUT_STATUS] = "VIDEO OUTPUT STATUS",
  28. [CMD_MONITORING_OUTPUT_LABELS] = "MONITORING OUTPUT LABELS",
  29. [CMD_MONITORING_OUTPUT_LOCKS] = "MONITORING OUTPUT LOCKS",
  30. [CMD_MONITORING_OUTPUT_ROUTING] = "VIDEO MONITORING OUTPUT ROUTING",
  31. [CMD_SERIAL_PORT_LABELS] = "SERIAL PORT LABELS",
  32. [CMD_SERIAL_PORT_ROUTING] = "SERIAL PORT ROUTING",
  33. [CMD_SERIAL_PORT_LOCKS] = "SERIAL PORT LOCKS",
  34. [CMD_SERIAL_PORT_STATUS] = "SERIAL PORT STATUS",
  35. [CMD_SERIAL_PORT_DIRECTIONS] = "SERIAL PORT DIRECTIONS",
  36. [CMD_PING] = "PING",
  37. [CMD_ACK] = "ACK",
  38. [CMD_NAK] = "NAK",
  39. };
  40. #define OFS(X) offsetof(struct videohub_data, X)
  41. struct videohub_commands videohub_commands[NUM_COMMANDS] = {
  42. [CMD_PROTOCOL_PREAMBLE] = { .cmd = CMD_PROTOCOL_PREAMBLE , .type = PARSE_CUSTOM },
  43. [CMD_VIDEOHUB_DEVICE] = { .cmd = CMD_VIDEOHUB_DEVICE , .type = PARSE_CUSTOM },
  44. [CMD_INPUT_LABELS] = { .cmd = CMD_INPUT_LABELS , .type = PARSE_LABEL,
  45. .ports1 = OFS(inputs),
  46. .port_id1 = "video input",
  47. .opt_prefix = "vi",
  48. },
  49. [CMD_OUTPUT_LABELS] = { .cmd = CMD_OUTPUT_LABELS , .type = PARSE_LABEL,
  50. .ports1 = OFS(outputs),
  51. .port_id1 = "video output",
  52. .opt_prefix = "vo",
  53. },
  54. [CMD_VIDEO_OUTPUT_LOCKS] = { .cmd = CMD_VIDEO_OUTPUT_LOCKS , .type = PARSE_LOCK,
  55. .ports1 = OFS(outputs),
  56. .port_id1 = "video output",
  57. .opt_prefix = "vo",
  58. },
  59. [CMD_VIDEO_OUTPUT_ROUTING] = { .cmd = CMD_VIDEO_OUTPUT_ROUTING, .type = PARSE_ROUTE,
  60. .ports1 = OFS(outputs),
  61. .ports2 = OFS(inputs),
  62. .port_id1 = "video output",
  63. .port_id2 = "video input",
  64. .opt_prefix = "vo",
  65. },
  66. [CMD_VIDEO_INPUT_STATUS] = { .cmd = CMD_VIDEO_INPUT_STATUS , .type = PARSE_STATUS,
  67. .ports1 = OFS(inputs),
  68. .port_id1 = "video input",
  69. .opt_prefix = "vi",
  70. },
  71. [CMD_VIDEO_OUTPUT_STATUS] = { .cmd = CMD_VIDEO_OUTPUT_STATUS , .type = PARSE_STATUS,
  72. .ports1 = OFS(outputs),
  73. .port_id1 = "video output",
  74. .opt_prefix = "vo",
  75. },
  76. [CMD_MONITORING_OUTPUT_LABELS] = { .cmd = CMD_MONITORING_OUTPUT_LABELS , .type = PARSE_LABEL,
  77. .ports1 = OFS(mon_outputs),
  78. .port_id1 = "monitoring output",
  79. .opt_prefix = "mo",
  80. },
  81. [CMD_MONITORING_OUTPUT_LOCKS] = { .cmd = CMD_MONITORING_OUTPUT_LOCKS , .type = PARSE_LOCK,
  82. .ports1 = OFS(mon_outputs),
  83. .port_id1 = "monitoring output",
  84. .opt_prefix = "mo",
  85. },
  86. [CMD_MONITORING_OUTPUT_ROUTING] = { .cmd = CMD_MONITORING_OUTPUT_ROUTING, .type = PARSE_ROUTE,
  87. .ports1 = OFS(mon_outputs),
  88. .ports2 = OFS(inputs),
  89. .port_id1 = "monitoring output",
  90. .port_id2 = "video input",
  91. .opt_prefix = "mo",
  92. },
  93. [CMD_SERIAL_PORT_LABELS] = { .cmd = CMD_SERIAL_PORT_LABELS, .type = PARSE_LABEL,
  94. .ports1 = OFS(serial),
  95. .port_id1 = "serial",
  96. .opt_prefix = "se",
  97. },
  98. [CMD_SERIAL_PORT_LOCKS] = { .cmd = CMD_SERIAL_PORT_LOCKS , .type = PARSE_LOCK,
  99. .ports1 = OFS(serial),
  100. .port_id1 = "serial",
  101. .opt_prefix = "se",
  102. },
  103. [CMD_SERIAL_PORT_ROUTING] = { .cmd = CMD_SERIAL_PORT_ROUTING, .type = PARSE_ROUTE,
  104. .ports1 = OFS(serial),
  105. .ports2 = OFS(serial),
  106. .port_id1 = "serial",
  107. .port_id2 = "serial",
  108. .opt_prefix = "se",
  109. },
  110. [CMD_SERIAL_PORT_STATUS] = { .cmd = CMD_SERIAL_PORT_STATUS , .type = PARSE_STATUS,
  111. .ports1 = OFS(serial),
  112. .port_id1 = "serial",
  113. .opt_prefix = "se",
  114. },
  115. [CMD_SERIAL_PORT_DIRECTIONS] = { .cmd = CMD_SERIAL_PORT_DIRECTIONS, .type = PARSE_DIR,
  116. .ports1 = OFS(serial),
  117. .port_id1 = "serial",
  118. .opt_prefix = "se",
  119. },
  120. [CMD_PING] = { .cmd = CMD_PING , .type = PARSE_NONE },
  121. [CMD_ACK] = { .cmd = CMD_ACK , .type = PARSE_NONE },
  122. [CMD_NAK] = { .cmd = CMD_NAK , .type = PARSE_NONE },
  123. };
  124. static char *parse_text(char *line, char *cmd) {
  125. char *parsed_text = strstr(line, cmd);
  126. if (parsed_text == line) {
  127. return parsed_text + strlen(cmd);
  128. }
  129. return NULL;
  130. }
  131. bool parse_command(struct videohub_data *d, char *cmd) {
  132. unsigned int i;
  133. bool ret = true;
  134. if (!strlen(cmd))
  135. return false;
  136. struct videohub_commands *v = NULL;
  137. const char *cmd_txt = NULL;
  138. for (i = 0; i < NUM_COMMANDS; i++) {
  139. const char *cmd_text = videohub_commands_text[i];
  140. if (strstr(cmd, cmd_text) == cmd) {
  141. v = &videohub_commands[i];
  142. cmd_txt = videohub_commands_text[i];
  143. break;
  144. }
  145. }
  146. if (!v) {
  147. q("WARNING: Videohub sent unknown command!\n");
  148. q(" Please report this command to author's email: georgi@unixsol.org\n");
  149. q(" You may use -q or --quiet to suppress the message.\n");
  150. q("---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  151. q("%s\n", cmd);
  152. q("---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  153. return false;
  154. }
  155. d("debug: Got '%s' command.\n", cmd_txt);
  156. if (debug > 1)
  157. d("----\n%s\n----\n", cmd);
  158. struct port_set *s_port = !v->ports1 ? NULL : (void *)d + v->ports1;
  159. struct port_set *d_port = !v->ports2 ? NULL : (void *)d + v->ports2;
  160. char *p, *cmd_data = xstrdup( cmd + strlen(cmd_txt) + 2 ); // +2 to compensate for :\n at the end of the command
  161. // Split line by line
  162. char *line, *saveptr = NULL;
  163. for(i = 0, line = strtok_r(cmd_data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr), i++) {
  164. // Parse command data response
  165. unsigned int port_num = 0;
  166. unsigned int dest_port_num = 0;
  167. char *port_data = NULL;
  168. if (v->type != PARSE_NONE && v->type != PARSE_CUSTOM) {
  169. port_data = strchr(line, ' ');
  170. if (!port_data)
  171. continue;
  172. port_data[0] = '\0'; // Separate port_num from port_data
  173. port_data++;
  174. port_num = strtoul(line, NULL, 10);
  175. if (port_num + 1 > s_port->num) {
  176. q("WARNING: %s: invalid %s port %u (valid 0..%u)\n", cmd_txt,
  177. v->port_id1, port_num, s_port->num - 1);
  178. continue;
  179. }
  180. }
  181. switch (v->type) {
  182. case PARSE_LABEL:
  183. snprintf(s_port->port[port_num].name, sizeof(s_port->port[port_num].name), "%s", port_data);
  184. break;
  185. case PARSE_STATUS:
  186. s_port->port[port_num].status = S_UNKNOWN;
  187. bool invalid_status = false;
  188. if (v->cmd == CMD_SERIAL_PORT_STATUS) {
  189. if (streq("RS422", port_data)) s_port->port[port_num].status = S_RS422;
  190. else if (streq("None", port_data)) s_port->port[port_num].status = S_NONE;
  191. else invalid_status = true;
  192. } else {
  193. if (streq("BNC", port_data)) s_port->port[port_num].status = S_BNC;
  194. else if (streq("Optical", port_data)) s_port->port[port_num].status = S_OPTICAL;
  195. else if (streq("None", port_data)) s_port->port[port_num].status = S_NONE;
  196. else if (streq("Thunderbolt", port_data)) s_port->port[port_num].status = S_THUNDERBOLT;
  197. else invalid_status = true;
  198. }
  199. if (invalid_status) {
  200. q("WARNING: %s command returned unknown status: '%s'\n", cmd_txt, port_data);
  201. q("Please report this line to author's email: georgi@unixsol.org\n");
  202. }
  203. break;
  204. case PARSE_DIR:
  205. s_port->port[port_num].direction = DIR_AUTO;
  206. if (streq("control", port_data)) s_port->port[port_num].direction = DIR_CONTROL;
  207. else if (streq("slave", port_data)) s_port->port[port_num].direction = DIR_SLAVE;
  208. else if (streq("auto", port_data)) s_port->port[port_num].direction = DIR_AUTO;
  209. else {
  210. q("WARNING: %s command returned unknown direction: '%s'\n", cmd_txt, port_data);
  211. q("Please report this line to author's email: georgi@unixsol.org\n");
  212. }
  213. break;
  214. case PARSE_ROUTE:
  215. dest_port_num = strtoul(port_data, NULL, 10);
  216. if (dest_port_num == NO_PORT) {
  217. // Only serial port routing can be disabled with -1
  218. if (v->cmd == CMD_SERIAL_PORT_ROUTING) {
  219. s_port->port[port_num].routed_to = dest_port_num;
  220. continue;
  221. } else {
  222. dest_port_num = port_num;
  223. }
  224. }
  225. if (dest_port_num + 1 > d_port->num) {
  226. q("WARNING: %s: invalid %s port %u (valid 0..%u)\n", cmd_txt,
  227. v->port_id2, dest_port_num, d_port->num - 1);
  228. continue;
  229. }
  230. s_port->port[port_num].routed_to = dest_port_num;
  231. break;
  232. case PARSE_LOCK:
  233. switch (port_data[0]) {
  234. case 'O': s_port->port[port_num].lock = PORT_LOCKED; break;
  235. case 'L': s_port->port[port_num].lock = PORT_LOCKED_OTHER; break;
  236. default : s_port->port[port_num].lock = PORT_UNLOCKED; break;
  237. }
  238. break;
  239. default: break;
  240. }
  241. // Parse custom commands
  242. switch (v->cmd) {
  243. case CMD_PROTOCOL_PREAMBLE:
  244. if ((p = parse_text(line, "Version: ")))
  245. snprintf(d->device.protocol_ver, sizeof(d->device.protocol_ver), "%s", p);
  246. break;
  247. case CMD_VIDEOHUB_DEVICE:
  248. if ((p = parse_text(line, "Device present: "))) {
  249. d->device.dev_present = streq(p, "true");
  250. d->device.needs_fw_update = streq(p, "needs_update");
  251. }
  252. else if ((p = parse_text(line, "Model name: ")))
  253. snprintf(d->device.model_name, sizeof(d->device.model_name), "%s", p);
  254. else if ((p = parse_text(line, "Unique ID: ")))
  255. snprintf(d->device.unique_id, sizeof(d->device.unique_id) , "%s", p);
  256. else if ((p = parse_text(line, "Video inputs: ")))
  257. d->inputs.num = strtoul(p, NULL, 10);
  258. else if ((p = parse_text(line, "Video processing units: ")))
  259. d->device.num_video_processing_units = strtoul(p, NULL, 10);
  260. else if ((p = parse_text(line, "Video outputs: ")))
  261. d->outputs.num = strtoul(p, NULL, 10);
  262. else if ((p = parse_text(line, "Video monitoring outputs: ")))
  263. d->mon_outputs.num = strtoul(p, NULL, 10);
  264. else if ((p = parse_text(line, "Serial ports: ")))
  265. d->serial.num = strtoul(p, NULL, 10);
  266. else {
  267. q("WARNING: VIDEOHUB DEVICE command sent unknown line: '%s'\n", line);
  268. q("Please report this line to author's email: georgi@unixsol.org\n");
  269. }
  270. break;
  271. case CMD_NAK:
  272. ret = false;
  273. break;
  274. default: break;
  275. }
  276. }
  277. free(cmd_data);
  278. return ret;
  279. }
  280. int parse_text_buffer(struct videohub_data *d, char *cmd_buffer) {
  281. // The buffer contains only one command, no splitting is needed
  282. if (!strstr(cmd_buffer, "\n\n"))
  283. return parse_command(d, cmd_buffer);
  284. // Split commands and parse them one by one
  285. int ok_commands = 0;
  286. char *buf_copy = xstrdup(cmd_buffer);
  287. char *newcmd, *cmd = buf_copy;
  288. while(1) {
  289. newcmd = strstr(cmd, "\n\n"); // Find next command
  290. if (!newcmd) {
  291. if (parse_command(d, cmd)) // Parse current command
  292. ok_commands++;
  293. break;
  294. }
  295. newcmd[0] = '\0'; // Terminate previous command
  296. if (parse_command(d, cmd)) // Parse previous command
  297. ok_commands++;
  298. cmd = newcmd + 2; // Advance cmd to the next command
  299. }
  300. free(buf_copy);
  301. return ok_commands;
  302. }
  303. // Try to find port with certain name, return 0 on not found, pos + 1 is found
  304. static int get_port_by_name(struct port_set *p, char *name) {
  305. unsigned int i;
  306. for(i = 0; i < p->num; i++) {
  307. if (streq(name, p->port[i].name)) {
  308. return i + 1;
  309. }
  310. }
  311. return 0;
  312. }
  313. // Return 0 on error, number otherwise if it can parse the whole input
  314. static unsigned int my_atoi(char *txt) {
  315. char *endptr = NULL;
  316. if (!txt)
  317. return 0;
  318. unsigned int ret = strtoul(txt, &endptr, 10);
  319. if (endptr == txt || *endptr)
  320. return 0;
  321. return ret;
  322. }
  323. void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
  324. struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
  325. struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
  326. // All command types needs parsing of the "source port"
  327. e->port_no1 = my_atoi(e->param1);
  328. if (e->port_no1 == 0 || e->port_no1 > s_port->num) {
  329. e->port_no1 = get_port_by_name(s_port, e->param1);
  330. if (!e->port_no1)
  331. die("Unknown %s port number/name: %s", e->cmd->port_id1, e->param1);
  332. }
  333. // ROUTE type needs parsing of the "destination port"
  334. if (e->cmd->type == PARSE_ROUTE) {
  335. e->port_no2 = my_atoi(e->param2);
  336. if (e->port_no2 == 0 || e->port_no2 > d_port->num) {
  337. e->port_no2 = get_port_by_name(d_port, e->param2);
  338. if (!e->port_no2)
  339. die("Unknown %s port number/name: %s", e->cmd->port_id2, e->param2);
  340. }
  341. }
  342. // Allow port_noX to be used as index into ->port[]
  343. e->port_no1 -= 1;
  344. e->port_no2 -= 1;
  345. if (e->clear_port)
  346. e->port_no2 = NO_PORT;
  347. if (e->cmd->type == PARSE_LOCK) {
  348. e->lock = s_port->port[e->port_no1].lock;
  349. }
  350. }
  351. static char *dir2cmd(enum serial_dir dir) {
  352. switch (dir) {
  353. case DIR_CONTROL: return "control";
  354. case DIR_SLAVE : return "slave";
  355. case DIR_AUTO : return "auto";
  356. }
  357. return "auto";
  358. }
  359. static char *dir2txt(enum serial_dir dir) {
  360. switch (dir) {
  361. case DIR_CONTROL: return "IN (Workstation)";
  362. case DIR_SLAVE : return "OUT (Deck)";
  363. case DIR_AUTO : return "AUTO";
  364. }
  365. return "AUTO";
  366. }
  367. void format_cmd_text(struct vcmd_entry *e, char *buf, unsigned int bufsz) {
  368. switch (e->cmd->type) {
  369. case PARSE_LABEL:
  370. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  371. e->port_no1, e->param2);
  372. break;
  373. case PARSE_LOCK:
  374. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  375. e->port_no1, e->do_lock ? "O" : (e->lock == PORT_LOCKED_OTHER ? "F" : "U"));
  376. break;
  377. case PARSE_ROUTE:
  378. snprintf(buf, bufsz, "%s:\n%u %d\n\n", videohub_commands_text[e->cmd->cmd],
  379. e->port_no1, (e->port_no2 == NO_PORT ? -1 : (int)e->port_no2));
  380. break;
  381. case PARSE_DIR:
  382. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  383. e->port_no1, dir2cmd(e->direction));
  384. break;
  385. default: break;
  386. }
  387. }
  388. void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
  389. struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
  390. struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
  391. const char *prefix = "videohub: ";
  392. switch (e->cmd->type) {
  393. case PARSE_LABEL:
  394. printf("%srename %s %d \"%s\" to \"%s\"\n",
  395. prefix,
  396. e->cmd->port_id1,
  397. e->port_no1 + 1, s_port->port[e->port_no1].name,
  398. e->param2
  399. );
  400. break;
  401. case PARSE_LOCK:
  402. printf("%s%s %s %d \"%s\"\n",
  403. prefix,
  404. e->do_lock ? "lock" : (e->lock == PORT_LOCKED_OTHER ? "force unlock" : "unlock"),
  405. e->cmd->port_id1,
  406. e->port_no1 + 1, s_port->port[e->port_no1].name
  407. );
  408. break;
  409. case PARSE_ROUTE:
  410. if (e->port_no2 == NO_PORT) {
  411. printf("%sdisconnect %s %d \"%s\"\n",
  412. prefix,
  413. e->cmd->port_id1,
  414. e->port_no1 + 1, s_port->port[e->port_no1].name
  415. );
  416. break;
  417. }
  418. if (e->cmd->cmd == CMD_SERIAL_PORT_ROUTING) {
  419. printf("%sconnect %s %d \"%s\" to %s %d \"%s\"\n",
  420. prefix,
  421. e->cmd->port_id1,
  422. e->port_no1 + 1, s_port->port[e->port_no1].name,
  423. e->cmd->port_id2,
  424. e->port_no2 + 1, d_port->port [e->port_no2].name
  425. );
  426. break;
  427. }
  428. printf("%sset %s %d \"%s\" to read from %s %d \"%s\"\n",
  429. prefix,
  430. e->cmd->port_id1,
  431. e->port_no1 + 1, s_port->port[e->port_no1].name,
  432. e->cmd->port_id2,
  433. e->port_no2 + 1, d_port->port [e->port_no2].name
  434. );
  435. break;
  436. case PARSE_DIR:
  437. printf("%sset %s %d \"%s\" direction to %s\n",
  438. prefix,
  439. e->cmd->port_id1,
  440. e->port_no1 + 1, s_port->port[e->port_no1].name,
  441. dir2txt(e->direction)
  442. );
  443. break;
  444. default: break;
  445. }
  446. }