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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. if (streq("BNC", port_data)) s_port->port[port_num].status = S_BNC;
  188. else if (streq("Optical", port_data)) s_port->port[port_num].status = S_OPTICAL;
  189. else 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 if (streq("Thunderbolt", port_data)) s_port->port[port_num].status = S_THUNDERBOLT;
  192. break;
  193. case PARSE_DIR:
  194. s_port->port[port_num].direction = DIR_AUTO;
  195. if (streq("control", port_data)) s_port->port[port_num].direction = DIR_CONTROL;
  196. else if (streq("slave", port_data)) s_port->port[port_num].direction = DIR_SLAVE;
  197. break;
  198. case PARSE_ROUTE:
  199. dest_port_num = strtoul(port_data, NULL, 10);
  200. if (dest_port_num == NO_PORT) {
  201. // Only serial port routing can be disabled with -1
  202. if (v->cmd == CMD_SERIAL_PORT_ROUTING) {
  203. s_port->port[port_num].routed_to = dest_port_num;
  204. continue;
  205. } else {
  206. dest_port_num = port_num;
  207. }
  208. }
  209. if (dest_port_num + 1 > d_port->num) {
  210. q("WARNING: %s: invalid %s port %u (valid 0..%u)\n", cmd_txt,
  211. v->port_id2, dest_port_num, d_port->num - 1);
  212. continue;
  213. }
  214. s_port->port[port_num].routed_to = dest_port_num;
  215. break;
  216. case PARSE_LOCK:
  217. switch (port_data[0]) {
  218. case 'O': s_port->port[port_num].lock = PORT_LOCKED; break;
  219. case 'L': s_port->port[port_num].lock = PORT_LOCKED_OTHER; break;
  220. default : s_port->port[port_num].lock = PORT_UNLOCKED; break;
  221. }
  222. break;
  223. default: break;
  224. }
  225. // Parse custom commands
  226. switch (v->cmd) {
  227. case CMD_PROTOCOL_PREAMBLE:
  228. if ((p = parse_text(line, "Version: ")))
  229. snprintf(d->device.protocol_ver, sizeof(d->device.protocol_ver), "%s", p);
  230. break;
  231. case CMD_VIDEOHUB_DEVICE:
  232. if ((p = parse_text(line, "Device present: "))) {
  233. d->device.dev_present = streq(p, "true");
  234. d->device.needs_fw_update = streq(p, "needs_update");
  235. }
  236. else if ((p = parse_text(line, "Model name: ")))
  237. snprintf(d->device.model_name, sizeof(d->device.model_name), "%s", p);
  238. else if ((p = parse_text(line, "Unique ID: ")))
  239. snprintf(d->device.unique_id, sizeof(d->device.unique_id) , "%s", p);
  240. else if ((p = parse_text(line, "Video inputs: ")))
  241. d->inputs.num = strtoul(p, NULL, 10);
  242. else if ((p = parse_text(line, "Video processing units: ")))
  243. d->device.num_video_processing_units = strtoul(p, NULL, 10);
  244. else if ((p = parse_text(line, "Video outputs: ")))
  245. d->outputs.num = strtoul(p, NULL, 10);
  246. else if ((p = parse_text(line, "Video monitoring outputs: ")))
  247. d->mon_outputs.num = strtoul(p, NULL, 10);
  248. else if ((p = parse_text(line, "Serial ports: ")))
  249. d->serial.num = strtoul(p, NULL, 10);
  250. else {
  251. q("WARNING: VIDEOHUB DEVICE command sent unknown line: '%s'\n", line);
  252. q("Please report this line to author's email: georgi@unixsol.org\n");
  253. }
  254. break;
  255. case CMD_NAK:
  256. ret = false;
  257. break;
  258. default: break;
  259. }
  260. }
  261. free(cmd_data);
  262. return ret;
  263. }
  264. int parse_text_buffer(struct videohub_data *d, char *cmd_buffer) {
  265. // The buffer contains only one command, no splitting is needed
  266. if (!strstr(cmd_buffer, "\n\n"))
  267. return parse_command(d, cmd_buffer);
  268. // Split commands and parse them one by one
  269. int ok_commands = 0;
  270. char *buf_copy = xstrdup(cmd_buffer);
  271. char *newcmd, *cmd = buf_copy;
  272. while(1) {
  273. newcmd = strstr(cmd, "\n\n"); // Find next command
  274. if (!newcmd) {
  275. if (parse_command(d, cmd)) // Parse current command
  276. ok_commands++;
  277. break;
  278. }
  279. newcmd[0] = '\0'; // Terminate previous command
  280. if (parse_command(d, cmd)) // Parse previous command
  281. ok_commands++;
  282. cmd = newcmd + 2; // Advance cmd to the next command
  283. }
  284. free(buf_copy);
  285. return ok_commands;
  286. }
  287. // Try to find port with certain name, return 0 on not found, pos + 1 is found
  288. static int get_port_by_name(struct port_set *p, char *name) {
  289. unsigned int i;
  290. for(i = 0; i < p->num; i++) {
  291. if (streq(name, p->port[i].name)) {
  292. return i + 1;
  293. }
  294. }
  295. return 0;
  296. }
  297. // Return 0 on error, number otherwise if it can parse the whole input
  298. static unsigned int my_atoi(char *txt) {
  299. char *endptr = NULL;
  300. if (!txt)
  301. return 0;
  302. unsigned int ret = strtoul(txt, &endptr, 10);
  303. if (endptr == txt || *endptr)
  304. return 0;
  305. return ret;
  306. }
  307. void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
  308. struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
  309. struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
  310. // All command types needs parsing of the "source port"
  311. e->port_no1 = my_atoi(e->param1);
  312. if (e->port_no1 == 0 || e->port_no1 > s_port->num) {
  313. e->port_no1 = get_port_by_name(s_port, e->param1);
  314. if (!e->port_no1)
  315. die("Unknown %s port number/name: %s", e->cmd->port_id1, e->param1);
  316. }
  317. // ROUTE type needs parsing of the "destination port"
  318. if (e->cmd->type == PARSE_ROUTE) {
  319. e->port_no2 = my_atoi(e->param2);
  320. if (e->port_no2 == 0 || e->port_no2 > d_port->num) {
  321. e->port_no2 = get_port_by_name(d_port, e->param2);
  322. if (!e->port_no2)
  323. die("Unknown %s port number/name: %s", e->cmd->port_id2, e->param2);
  324. }
  325. }
  326. // Allow port_noX to be used as index into ->port[]
  327. e->port_no1 -= 1;
  328. e->port_no2 -= 1;
  329. if (e->clear_port)
  330. e->port_no2 = NO_PORT;
  331. if (e->cmd->type == PARSE_LOCK) {
  332. e->lock = s_port->port[e->port_no1].lock;
  333. }
  334. }
  335. static char *dir2cmd(enum serial_dir dir) {
  336. switch (dir) {
  337. case DIR_CONTROL: return "control";
  338. case DIR_SLAVE : return "slave";
  339. case DIR_AUTO : return "auto";
  340. }
  341. return "auto";
  342. }
  343. static char *dir2txt(enum serial_dir dir) {
  344. switch (dir) {
  345. case DIR_CONTROL: return "IN (Workstation)";
  346. case DIR_SLAVE : return "OUT (Deck)";
  347. case DIR_AUTO : return "AUTO";
  348. }
  349. return "AUTO";
  350. }
  351. void format_cmd_text(struct vcmd_entry *e, char *buf, unsigned int bufsz) {
  352. switch (e->cmd->type) {
  353. case PARSE_LABEL:
  354. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  355. e->port_no1, e->param2);
  356. break;
  357. case PARSE_LOCK:
  358. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  359. e->port_no1, e->do_lock ? "O" : (e->lock == PORT_LOCKED_OTHER ? "F" : "U"));
  360. break;
  361. case PARSE_ROUTE:
  362. snprintf(buf, bufsz, "%s:\n%u %d\n\n", videohub_commands_text[e->cmd->cmd],
  363. e->port_no1, (e->port_no2 == NO_PORT ? -1 : (int)e->port_no2));
  364. break;
  365. case PARSE_DIR:
  366. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  367. e->port_no1, dir2cmd(e->direction));
  368. break;
  369. default: break;
  370. }
  371. }
  372. void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
  373. struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
  374. struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
  375. const char *prefix = "videohub: ";
  376. switch (e->cmd->type) {
  377. case PARSE_LABEL:
  378. printf("%srename %s %d \"%s\" to \"%s\"\n",
  379. prefix,
  380. e->cmd->port_id1,
  381. e->port_no1 + 1, s_port->port[e->port_no1].name,
  382. e->param2
  383. );
  384. break;
  385. case PARSE_LOCK:
  386. printf("%s%s %s %d \"%s\"\n",
  387. prefix,
  388. e->do_lock ? "lock" : (e->lock == PORT_LOCKED_OTHER ? "force unlock" : "unlock"),
  389. e->cmd->port_id1,
  390. e->port_no1 + 1, s_port->port[e->port_no1].name
  391. );
  392. break;
  393. case PARSE_ROUTE:
  394. if (e->port_no2 == NO_PORT) {
  395. printf("%sdisconnect %s %d \"%s\"\n",
  396. prefix,
  397. e->cmd->port_id1,
  398. e->port_no1 + 1, s_port->port[e->port_no1].name
  399. );
  400. break;
  401. }
  402. if (e->cmd->cmd == CMD_SERIAL_PORT_ROUTING) {
  403. printf("%sconnect %s %d \"%s\" to %s %d \"%s\"\n",
  404. prefix,
  405. e->cmd->port_id1,
  406. e->port_no1 + 1, s_port->port[e->port_no1].name,
  407. e->cmd->port_id2,
  408. e->port_no2 + 1, d_port->port [e->port_no2].name
  409. );
  410. break;
  411. }
  412. printf("%sset %s %d \"%s\" to read from %s %d \"%s\"\n",
  413. prefix,
  414. e->cmd->port_id1,
  415. e->port_no1 + 1, s_port->port[e->port_no1].name,
  416. e->cmd->port_id2,
  417. e->port_no2 + 1, d_port->port [e->port_no2].name
  418. );
  419. break;
  420. case PARSE_DIR:
  421. printf("%sset %s %d \"%s\" direction to %s\n",
  422. prefix,
  423. e->cmd->port_id1,
  424. e->port_no1 + 1, s_port->port[e->port_no1].name,
  425. dir2txt(e->direction)
  426. );
  427. break;
  428. default: break;
  429. }
  430. }