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 14KB

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