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

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