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

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