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

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