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

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