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

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