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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_PING] = "PING",
  32. [CMD_ACK] = "ACK",
  33. [CMD_NAK] = "NAK",
  34. };
  35. #define OFS(X) offsetof(struct videohub_data, X)
  36. struct videohub_commands videohub_commands[NUM_COMMANDS] = {
  37. [CMD_PROTOCOL_PREAMBLE] = { .cmd = CMD_PROTOCOL_PREAMBLE , .type = PARSE_CUSTOM },
  38. [CMD_VIDEOHUB_DEVICE] = { .cmd = CMD_VIDEOHUB_DEVICE , .type = PARSE_CUSTOM },
  39. [CMD_INPUT_LABELS] = { .cmd = CMD_INPUT_LABELS , .type = PARSE_LABEL,
  40. .ports1 = OFS(inputs),
  41. .port_id1 = "video input",
  42. .opt_prefix = "vi",
  43. },
  44. [CMD_OUTPUT_LABELS] = { .cmd = CMD_OUTPUT_LABELS , .type = PARSE_LABEL,
  45. .ports1 = OFS(outputs),
  46. .port_id1 = "video output",
  47. .opt_prefix = "vo",
  48. },
  49. [CMD_VIDEO_OUTPUT_LOCKS] = { .cmd = CMD_VIDEO_OUTPUT_LOCKS , .type = PARSE_LOCK,
  50. .ports1 = OFS(outputs),
  51. .port_id1 = "video output",
  52. .opt_prefix = "vo",
  53. },
  54. [CMD_VIDEO_OUTPUT_ROUTING] = { .cmd = CMD_VIDEO_OUTPUT_ROUTING, .type = PARSE_ROUTE,
  55. .ports1 = OFS(outputs),
  56. .ports2 = OFS(inputs),
  57. .port_id1 = "video output",
  58. .port_id2 = "video input",
  59. .opt_prefix = "vo",
  60. },
  61. [CMD_VIDEO_INPUT_STATUS] = { .cmd = CMD_VIDEO_INPUT_STATUS , .type = PARSE_STATUS,
  62. .ports1 = OFS(inputs),
  63. .port_id1 = "video input",
  64. .opt_prefix = "vi",
  65. },
  66. [CMD_VIDEO_OUTPUT_STATUS] = { .cmd = CMD_VIDEO_OUTPUT_STATUS , .type = PARSE_STATUS,
  67. .ports1 = OFS(outputs),
  68. .port_id1 = "video output",
  69. .opt_prefix = "vo",
  70. },
  71. [CMD_MONITORING_OUTPUT_LABELS] = { .cmd = CMD_MONITORING_OUTPUT_LABELS , .type = PARSE_LABEL,
  72. .ports1 = OFS(mon_outputs),
  73. .port_id1 = "monitoring output",
  74. .opt_prefix = "mo",
  75. },
  76. [CMD_MONITORING_OUTPUT_LOCKS] = { .cmd = CMD_MONITORING_OUTPUT_LOCKS , .type = PARSE_LOCK,
  77. .ports1 = OFS(mon_outputs),
  78. .port_id1 = "monitoring output",
  79. .opt_prefix = "mo",
  80. },
  81. [CMD_MONITORING_OUTPUT_ROUTING] = { .cmd = CMD_MONITORING_OUTPUT_ROUTING, .type = PARSE_ROUTE,
  82. .ports1 = OFS(mon_outputs),
  83. .ports2 = OFS(inputs),
  84. .port_id1 = "monitoring output",
  85. .port_id2 = "video input",
  86. .opt_prefix = "mo",
  87. },
  88. [CMD_PING] = { .cmd = CMD_PING , .type = PARSE_NONE },
  89. [CMD_ACK] = { .cmd = CMD_ACK , .type = PARSE_NONE },
  90. [CMD_NAK] = { .cmd = CMD_NAK , .type = PARSE_NONE },
  91. };
  92. static char *parse_text(char *line, char *cmd) {
  93. char *parsed_text = strstr(line, cmd);
  94. if (parsed_text == line) {
  95. return parsed_text + strlen(cmd);
  96. }
  97. return NULL;
  98. }
  99. bool parse_command(struct videohub_data *d, char *cmd) {
  100. unsigned int i;
  101. bool ret = true;
  102. if (!strlen(cmd))
  103. return false;
  104. struct videohub_commands *v = NULL;
  105. const char *cmd_txt = NULL;
  106. for (i = 0; i < NUM_COMMANDS; i++) {
  107. const char *cmd_text = videohub_commands_text[i];
  108. if (strstr(cmd, cmd_text) == cmd) {
  109. v = &videohub_commands[i];
  110. cmd_txt = videohub_commands_text[i];
  111. break;
  112. }
  113. }
  114. if (!v) {
  115. q("WARNING: Videohub sent unknown command!\n");
  116. q(" Please report this command to author's email: georgi@unixsol.org\n");
  117. q(" You may use -q or --quiet to suppress the message.\n");
  118. q("---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  119. q("%s\n", cmd);
  120. q("---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  121. return false;
  122. }
  123. d("debug: Got '%s' command.\n", cmd_txt);
  124. if (debug > 1)
  125. d("----\n%s\n----\n", cmd);
  126. struct port_set *s_port = !v->ports1 ? NULL : (void *)d + v->ports1;
  127. struct port_set *d_port = !v->ports2 ? NULL : (void *)d + v->ports2;
  128. char *p, *cmd_data = xstrdup( cmd + strlen(cmd_txt) + 2 ); // +2 to compensate for :\n at the end of the command
  129. // Split line by line
  130. char *line, *saveptr = NULL;
  131. for(i = 0, line = strtok_r(cmd_data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr), i++) {
  132. // Parse command data response
  133. unsigned int port_num = 0;
  134. unsigned int dest_port_num = 0;
  135. char *port_data = NULL;
  136. if (v->type != PARSE_NONE && v->type != PARSE_CUSTOM) {
  137. port_data = strchr(line, ' ');
  138. if (!port_data)
  139. continue;
  140. port_data[0] = '\0'; // Separate port_num from port_data
  141. port_data++;
  142. port_num = strtoul(line, NULL, 10);
  143. if (port_num + 1 > s_port->num) {
  144. q("WARNING: %s: invalid %s port %u (valid 0..%u)\n", cmd_txt,
  145. v->port_id1, port_num, s_port->num - 1);
  146. continue;
  147. }
  148. }
  149. switch (v->type) {
  150. case PARSE_LABEL:
  151. snprintf(s_port->port[port_num].name, sizeof(s_port->port[port_num].name), "%s", port_data);
  152. break;
  153. case PARSE_STATUS:
  154. snprintf(s_port->port[port_num].status, sizeof(s_port->port[port_num].status), "%s", port_data);
  155. break;
  156. case PARSE_ROUTE:
  157. dest_port_num = strtoul(port_data, NULL, 10);
  158. if (dest_port_num + 1 > d_port->num) {
  159. q("WARNING: %s: invalid %s port %u (valid 0..%u)\n", cmd_txt,
  160. v->port_id2, dest_port_num, d_port->num - 1);
  161. continue;
  162. }
  163. s_port->port[port_num].routed_to = dest_port_num;
  164. break;
  165. case PARSE_LOCK:
  166. switch (port_data[0]) {
  167. case 'O': s_port->port[port_num].lock = PORT_LOCKED; break;
  168. case 'L': s_port->port[port_num].lock = PORT_LOCKED_OTHER; break;
  169. default : s_port->port[port_num].lock = PORT_UNLOCKED; break;
  170. }
  171. break;
  172. default: break;
  173. }
  174. // Parse custom commands
  175. switch (v->cmd) {
  176. case CMD_PROTOCOL_PREAMBLE:
  177. if ((p = parse_text(line, "Version: ")))
  178. snprintf(d->device.protocol_ver, sizeof(d->device.protocol_ver), "%s", p);
  179. break;
  180. case CMD_VIDEOHUB_DEVICE:
  181. if ((p = parse_text(line, "Device present: "))) {
  182. d->device.dev_present = streq(p, "true");
  183. d->device.needs_fw_update = streq(p, "needs_update");
  184. }
  185. if ((p = parse_text(line, "Model name: ")))
  186. snprintf(d->device.model_name, sizeof(d->device.model_name), "%s", p);
  187. if ((p = parse_text(line, "Unique ID: ")))
  188. snprintf(d->device.unique_id, sizeof(d->device.unique_id) , "%s", p);
  189. if ((p = parse_text(line, "Video inputs: ")))
  190. d->inputs.num = strtoul(p, NULL, 10);
  191. if ((p = parse_text(line, "Video processing units: ")))
  192. d->device.num_video_processing_units = strtoul(p, NULL, 10);
  193. if ((p = parse_text(line, "Video outputs: ")))
  194. d->outputs.num = strtoul(p, NULL, 10);
  195. if ((p = parse_text(line, "Video monitoring outputs: ")))
  196. d->mon_outputs.num = strtoul(p, NULL, 10);
  197. if ((p = parse_text(line, "Serial ports: ")))
  198. d->device.num_serial_ports = strtoul(p, NULL, 10);
  199. break;
  200. case CMD_NAK:
  201. ret = false;
  202. break;
  203. default: break;
  204. }
  205. }
  206. free(cmd_data);
  207. return ret;
  208. }
  209. int parse_text_buffer(struct videohub_data *d, char *cmd_buffer) {
  210. // The buffer contains only one command, no splitting is needed
  211. if (!strstr(cmd_buffer, "\n\n"))
  212. return parse_command(d, cmd_buffer);
  213. // Split commands and parse them one by one
  214. int ok_commands = 0;
  215. char *buf_copy = xstrdup(cmd_buffer);
  216. char *newcmd, *cmd = buf_copy;
  217. while(1) {
  218. newcmd = strstr(cmd, "\n\n"); // Find next command
  219. if (!newcmd) {
  220. if (parse_command(d, cmd)) // Parse current command
  221. ok_commands++;
  222. break;
  223. }
  224. newcmd[0] = '\0'; // Terminate previous command
  225. if (parse_command(d, cmd)) // Parse previous command
  226. ok_commands++;
  227. cmd = newcmd + 2; // Advance cmd to the next command
  228. }
  229. free(buf_copy);
  230. return ok_commands;
  231. }
  232. // Try to find port with certain name, return 0 on not found, pos + 1 is found
  233. static int get_port_by_name(struct port_set *p, char *name) {
  234. unsigned int i;
  235. for(i = 0; i < p->num; i++) {
  236. if (streq(name, p->port[i].name)) {
  237. return i + 1;
  238. }
  239. }
  240. return 0;
  241. }
  242. // Return 0 on error, number otherwise if it can parse the whole input
  243. static unsigned int my_atoi(char *txt) {
  244. char *endptr = NULL;
  245. if (!txt)
  246. return 0;
  247. unsigned int ret = strtoul(txt, &endptr, 10);
  248. if (endptr == txt || *endptr)
  249. return 0;
  250. return ret;
  251. }
  252. void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
  253. struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
  254. struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
  255. // All command types needs parsing of the "source port"
  256. e->port_no1 = my_atoi(e->param1);
  257. if (e->port_no1 == 0 || e->port_no1 > s_port->num) {
  258. e->port_no1 = get_port_by_name(s_port, e->param1);
  259. if (!e->port_no1)
  260. die("Unknown %s port number/name: %s", e->cmd->port_id1, e->param1);
  261. }
  262. // ROUTE type needs parsing of the "destination port"
  263. if (e->cmd->type == PARSE_ROUTE) {
  264. e->port_no2 = my_atoi(e->param2);
  265. if (e->port_no2 == 0 || e->port_no2 > d_port->num) {
  266. e->port_no2 = get_port_by_name(d_port, e->param2);
  267. if (!e->port_no2)
  268. die("Unknown %s port number/name: %s", e->cmd->port_id2, e->param2);
  269. }
  270. }
  271. if (e->cmd->type == PARSE_LOCK) {
  272. e->lock = s_port->port[e->port_no1 - 1].lock;
  273. }
  274. }
  275. void format_cmd_text(struct vcmd_entry *e, char *buf, unsigned int bufsz) {
  276. switch (e->cmd->type) {
  277. case PARSE_LABEL:
  278. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  279. e->port_no1 - 1, e->param2);
  280. break;
  281. case PARSE_LOCK:
  282. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  283. e->port_no1 - 1, e->do_lock ? "O" : (e->lock == PORT_LOCKED_OTHER ? "F" : "U"));
  284. break;
  285. case PARSE_ROUTE:
  286. snprintf(buf, bufsz, "%s:\n%u %u\n\n", videohub_commands_text[e->cmd->cmd],
  287. e->port_no1 - 1, e->port_no2 - 1);
  288. break;
  289. default: break;
  290. }
  291. }
  292. void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
  293. struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
  294. struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
  295. const char *prefix = "videohub: ";
  296. switch (e->cmd->type) {
  297. case PARSE_LABEL:
  298. printf("%srename %s %d \"%s\" to \"%s\"\n",
  299. prefix,
  300. e->cmd->port_id1,
  301. e->port_no1, s_port->port[e->port_no1 - 1].name,
  302. e->param2
  303. );
  304. break;
  305. case PARSE_LOCK:
  306. printf("%s%s %s %d \"%s\"\n",
  307. prefix,
  308. e->do_lock ? "lock" : (e->lock == PORT_LOCKED_OTHER ? "force unlock" : "unlock"),
  309. e->cmd->port_id1,
  310. e->port_no1, s_port->port[e->port_no1 - 1].name
  311. );
  312. break;
  313. case PARSE_ROUTE:
  314. printf("%sset %s %d \"%s\" to read from %s %d \"%s\"\n",
  315. prefix,
  316. e->cmd->port_id1,
  317. e->port_no1, s_port->port[e->port_no1 - 1].name,
  318. e->cmd->port_id2,
  319. e->port_no2, d_port->port [e->port_no2 - 1].name
  320. );
  321. break;
  322. default: break;
  323. }
  324. }