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

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