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.

videohubctrl.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Blackmagic Design Videohub control application
  3. * Copyright (C) 2014 Unix Solutions Ltd.
  4. * Written by Georgi Chorbadzhiyski
  5. *
  6. * Released under MIT license.
  7. * See LICENSE-MIT.txt for license terms.
  8. *
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <getopt.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include "data.h"
  20. #include "cmd.h"
  21. #include "net.h"
  22. #include "util.h"
  23. #include "display.h"
  24. #include "version.h"
  25. #include "libfuncs/libfuncs.h"
  26. int debug;
  27. int quiet;
  28. static struct videohub_data maindata;
  29. static int show_info = 1;
  30. static int show_monitor = 0;
  31. static int show_backup = 0;
  32. static int show_list = 0;
  33. static char *test_data;
  34. enum list_actions {
  35. action_list_device = (1 << 0),
  36. action_list_vinputs = (1 << 1),
  37. action_list_voutputs = (1 << 2),
  38. action_list_moutputs = (1 << 3),
  39. action_list_serial = (1 << 4),
  40. action_list_proc_units = (1 << 5),
  41. };
  42. static const char *program_id = PROGRAM_NAME " Version: " VERSION " Git: " GIT_VER;
  43. static const char short_options[] = "h:p:T:qdHVimb";
  44. static const struct option long_options[] = {
  45. { "host", required_argument, NULL, 'h' },
  46. { "port", required_argument, NULL, 'p' },
  47. { "test-input", required_argument, NULL, 'T' },
  48. { "quiet", no_argument, NULL, 'q' },
  49. { "debug", no_argument, NULL, 'd' },
  50. { "help", no_argument, NULL, 'H' },
  51. { "version", no_argument, NULL, 'V' },
  52. { "info", no_argument, NULL, 'i' },
  53. { "monitor", no_argument, NULL, 'm' },
  54. { "backup", no_argument, NULL, 'b' },
  55. { "list-device", no_argument, NULL, 901 },
  56. { "list-vinputs", no_argument, NULL, 902 },
  57. { "list-voutputs", no_argument, NULL, 903 },
  58. { "list-moutputs", no_argument, NULL, 904 },
  59. { "list-serial", no_argument, NULL, 905 },
  60. { "vi-name", required_argument, NULL, 1001 },
  61. { "vo-name", required_argument, NULL, 2001 },
  62. { "vo-input", required_argument, NULL, 2002 },
  63. { "vo-route", required_argument, NULL, 2002 }, // Alias of --vo-input
  64. { "vo-lock", required_argument, NULL, 2003 },
  65. { "vo-unlock", required_argument, NULL, 2004 },
  66. { "mo-name", required_argument, NULL, 3001 },
  67. { "mo-input", required_argument, NULL, 3002 },
  68. { "mo-route", required_argument, NULL, 3002 }, // Alias of --mo-input
  69. { "mo-lock", required_argument, NULL, 3003 },
  70. { "mo-unlock", required_argument, NULL, 3004 },
  71. { "se-name", required_argument, NULL, 4001 },
  72. { "se-input", required_argument, NULL, 4002 },
  73. { "se-connect", required_argument, NULL, 4002 }, // Alias of --se-input
  74. { "se-route", required_argument, NULL, 4002 }, // Alias of --se-input
  75. { "se-lock", required_argument, NULL, 4003 },
  76. { "se-unlock", required_argument, NULL, 4004 },
  77. { "se-dir", required_argument, NULL, 4005 },
  78. { "se-clear", required_argument, NULL, 4006 },
  79. { "pu-input", required_argument, NULL, 5001 },
  80. { "pu-connect", required_argument, NULL, 5001 }, // Alias of --pu-input
  81. { "pu-route", required_argument, NULL, 5001 }, // Alias of --pu-input
  82. { "pu-lock", required_argument, NULL, 5002 },
  83. { "pu-unlock", required_argument, NULL, 5003 },
  84. { "pu-clear", required_argument, NULL, 5004 },
  85. { 0, 0, 0, 0 }
  86. };
  87. static void show_help(struct videohub_data *data) {
  88. printf("%s\n", program_id);
  89. printf("\n");
  90. printf(" Usage: " PROGRAM_NAME " --host <host> [..commands..]\n");
  91. printf("\n");
  92. printf("Main options:\n");
  93. printf(" -h --host <host> | Set device host name.\n");
  94. printf(" -p --port <port_number> | Set device port (default: 9990).\n");
  95. printf("\n");
  96. printf("Commands:\n");
  97. printf(" -i --info | Show full device info (default command).\n");
  98. printf(" . This command is shows the equivalent of\n");
  99. printf(" . running all --list-XXX commands.\n");
  100. printf(" -m --monitor | Display real-time config changes monitor.\n");
  101. printf(" -b --backup | Show the command line that will restore\n");
  102. printf(" . the device to the current configuration.\n");
  103. printf("\n");
  104. printf(" --list-device | Display device info.\n");
  105. printf(" --list-vinputs | List device video inputs.\n");
  106. printf(" --list-voutputs | List device video outputs.\n");
  107. printf(" --list-moutputs | List device monitoring outputs.\n");
  108. printf(" --list-serial | List device serial ports.\n");
  109. printf(" --list-proc-units | List device processing units.\n");
  110. printf("\n");
  111. printf("Video inputs configuration:\n");
  112. printf(" --vi-name <in_X> <name> | Set video input port X name.\n");
  113. printf("\n");
  114. printf("Video outputs configuration:\n");
  115. printf(" --vo-name <out_X> <name> | Set video output port X name.\n");
  116. printf(" --vo-input <out_X> <in_Y> | Connect video output X to input Y.\n");
  117. printf(" --vo-lock <out_X> | Lock output port X.\n");
  118. printf(" --vo-unlock <out_X> | Unlock output port X.\n");
  119. printf("\n");
  120. printf("Monitoring outputs configuration:\n");
  121. printf(" --mo-name <mout_X> <name> | Set monitoring port X name.\n");
  122. printf(" --mo-input <mout_X> <in_Y> | Connect monitoring X to input Y.\n");
  123. printf(" --mo-lock <mout_X> | Lock monitoring port X.\n");
  124. printf(" --mo-unlock <mout_X> | Unlock monitoring port X.\n");
  125. printf("\n");
  126. printf("Serial ports configuration:\n");
  127. printf(" --se-name <ser_X> <name> | Set serial port X name.\n");
  128. printf(" --se-connect <ser_X> <ser_Y> | Connect serial X to serial Y.\n");
  129. printf(" --se-clear <ser_X> | Disconnect serial port X from serial Y.\n");
  130. printf(" --se-lock <ser_X> | Lock serial port X.\n");
  131. printf(" --se-unlock <ser_X> | Unlock serial port X.\n");
  132. printf(" --se-dir <ser_X> <dir> | Set serial port X direction.\n");
  133. printf(" . <dir> can be 'auto', 'in' or 'out'.\n");
  134. printf("\n");
  135. printf("Processing units configuration:\n");
  136. printf(" --pu-input <pu_X> <in_Y> | Connect processing unit X to input Y.\n");
  137. printf(" --pu-clear <pu_X> | Disconnect unit X from input Y.\n");
  138. printf(" --pu-lock <pu_X> | Lock processing unit X.\n");
  139. printf(" --pu-unlock <pu_X> | Unlock processing unit X.\n");
  140. printf("\n");
  141. printf("Misc options:\n");
  142. printf(" -T --test-input <file> | Read commands from <file>.\n");
  143. printf(" -d --debug | Increase logging verbosity.\n");
  144. printf(" -q --quiet | Suppress warnings.\n");
  145. printf(" -H --help | Show help screen.\n");
  146. printf(" -V --version | Show program version.\n");
  147. printf("\n");
  148. }
  149. static int num_parsed_cmds = 0;
  150. static struct run_cmds {
  151. struct vcmd_entry entry[MAX_RUN_CMDS];
  152. } parsed_cmds;
  153. static void check_num_parsed_cmds(void) {
  154. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  155. die("No more than %u commands are supported.", num_parsed_cmds);
  156. }
  157. static void parse_cmd2(int argc, char **argv, enum vcmd vcmd) {
  158. check_num_parsed_cmds();
  159. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  160. if (optind == argc || argv[optind - 1][0] == '-' || argv[optind][0] == '-') {
  161. fprintf(stderr, "%s: option '%s' requires two arguments\n", argv[0], argv[optind - 2]);
  162. exit(EXIT_FAILURE);
  163. }
  164. c->cmd = &videohub_commands[vcmd];
  165. c->param1 = argv[optind - 1];
  166. c->param2 = argv[optind];
  167. if (vcmd == CMD_SERIAL_PORT_DIRECTIONS) {
  168. if (strcasecmp("in", c->param2) == 0) c->direction = DIR_CONTROL;
  169. else if (strcasecmp("out", c->param2) == 0) c->direction = DIR_SLAVE;
  170. else if (strcasecmp("auto", c->param2) == 0) c->direction = DIR_AUTO;
  171. else die("Invalid serial port direction '%s'. Allowed are: in, out, auto.", c->param2);
  172. }
  173. num_parsed_cmds++;
  174. }
  175. static void parse_cmd2s(int argc, char **argv, enum vcmd vcmd) {
  176. check_num_parsed_cmds();
  177. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  178. c->cmd = &videohub_commands[vcmd];
  179. c->param1 = optarg;
  180. c->param2 = "1"; // Fake
  181. c->clear_port = true;
  182. num_parsed_cmds++;
  183. }
  184. static void parse_cmd1(int argc, char **argv, enum vcmd vcmd, bool do_lock) {
  185. check_num_parsed_cmds();
  186. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  187. c->cmd = &videohub_commands[vcmd];
  188. c->param1 = argv[optind - 1];
  189. c->do_lock = do_lock;
  190. num_parsed_cmds++;
  191. }
  192. static void parse_options(struct videohub_data *data, int argc, char **argv) {
  193. int j, err = 0;
  194. // Check environment
  195. data->dev_host = getenv("VIDEOHUB_HOST");
  196. data->dev_port = getenv("VIDEOHUB_PORT");
  197. // Set defaults
  198. if (!data->dev_port)
  199. data->dev_port = "9990";
  200. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  201. if (j == '?') // Invalid parameter
  202. exit(EXIT_FAILURE);
  203. switch (j) {
  204. case 'h': // --host
  205. data->dev_host = optarg;
  206. break;
  207. case 'p': // --port
  208. data->dev_port = optarg;
  209. break;
  210. case 'T': { // --test-input
  211. struct stat st;
  212. FILE *f;
  213. if (stat(optarg, &st) != 0)
  214. die("Can't stat %s: %s", optarg, strerror(errno));
  215. f = fopen(optarg, "r");
  216. if (!f)
  217. die("Can't open %s: %s", optarg, strerror(errno));
  218. test_data = xzalloc(st.st_size + 1);
  219. if (fread(test_data, st.st_size, 1, f) < 1)
  220. die("Can't read from %s: %s", optarg, strerror(errno));
  221. fclose(f);
  222. data->dev_host = "sdi-matrix";
  223. break;
  224. }
  225. case 'd': // --debug
  226. debug++;
  227. if (debug)
  228. quiet = 0; // Disable quiet
  229. break;
  230. case 'q': // --quiet
  231. quiet = !quiet;
  232. break;
  233. case 'i': // --info
  234. show_info = 1;
  235. break;
  236. case 'm': // --monitor
  237. show_monitor = 1;
  238. break;
  239. case 'b': // --backup
  240. show_backup = 1;
  241. break;
  242. case 901: show_list |= action_list_device; break; // --list-device
  243. case 902: show_list |= action_list_vinputs; break; // --list-vinputs
  244. case 903: show_list |= action_list_voutputs; break; // --list-voutputs
  245. case 904: show_list |= action_list_moutputs; break; // --list-moutputs
  246. case 905: show_list |= action_list_serial; break; // --list-serial
  247. case 1001: parse_cmd2(argc, argv, CMD_INPUT_LABELS); break; // --vi-name
  248. case 2001: parse_cmd2(argc, argv, CMD_OUTPUT_LABELS); break; // --vo-name
  249. case 2002: parse_cmd2(argc, argv, CMD_VIDEO_OUTPUT_ROUTING); break; // --vo-input
  250. case 2003: parse_cmd1(argc, argv, CMD_VIDEO_OUTPUT_LOCKS, true); break; // --vo-lock
  251. case 2004: parse_cmd1(argc, argv, CMD_VIDEO_OUTPUT_LOCKS, false); break; // --vo-unlock
  252. case 3001: parse_cmd2(argc, argv, CMD_MONITORING_OUTPUT_LABELS); break; // --mo-name
  253. case 3002: parse_cmd2(argc, argv, CMD_MONITORING_OUTPUT_ROUTING); break; // --mo-input
  254. case 3003: parse_cmd1(argc, argv, CMD_MONITORING_OUTPUT_LOCKS, true); break; // --mo-lock
  255. case 3004: parse_cmd1(argc, argv, CMD_MONITORING_OUTPUT_LOCKS, false); break; // --mo-unlock
  256. case 4001: parse_cmd2(argc, argv, CMD_SERIAL_PORT_LABELS); break; // --se-name
  257. case 4002: parse_cmd2(argc, argv, CMD_SERIAL_PORT_ROUTING); break; // --se-input
  258. case 4003: parse_cmd1(argc, argv, CMD_SERIAL_PORT_LOCKS, true); break; // --se-lock
  259. case 4004: parse_cmd1(argc, argv, CMD_SERIAL_PORT_LOCKS, false); break; // --se-unlock
  260. case 4005: parse_cmd2(argc, argv, CMD_SERIAL_PORT_DIRECTIONS); break; // --se-dir
  261. case 4006: parse_cmd2s(argc, argv, CMD_SERIAL_PORT_ROUTING); break; // --se-clear
  262. case 5001: parse_cmd2(argc, argv, CMD_PROCESSING_UNIT_ROUTING); break; // --pu-input
  263. case 5002: parse_cmd1(argc, argv, CMD_PROCESSING_UNIT_LOCKS, true); break; // --pu-lock
  264. case 5003: parse_cmd1(argc, argv, CMD_PROCESSING_UNIT_LOCKS, false); break; // --pu-unlock
  265. case 5004: parse_cmd2s(argc, argv, CMD_PROCESSING_UNIT_ROUTING); break; // --pu-clear
  266. case 'H': // --help
  267. show_help(data);
  268. exit(EXIT_SUCCESS);
  269. case 'V': // --version
  270. printf("%s\n", program_id);
  271. exit(EXIT_SUCCESS);
  272. }
  273. }
  274. if (!data->dev_host || !strtoul(data->dev_port, NULL, 10))
  275. err = 1;
  276. if (err) {
  277. show_help(data);
  278. if (!data->dev_host)
  279. fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
  280. exit(EXIT_FAILURE);
  281. }
  282. d("Device address: %s:%s\n", data->dev_host, data->dev_port);
  283. }
  284. static void check_number_of_ports(struct port_set *p) {
  285. if (p->num > ARRAY_SIZE(p->port))
  286. die("The device supports %d ports. Increase MAX_PORTS (%lu) and recompile the program.",
  287. p->num, ARRAY_SIZE(p->port));
  288. }
  289. static void reset_routed_to(struct port_set *p) {
  290. unsigned int i;
  291. for (i = 0; i < ARRAY_SIZE(p->port); i++)
  292. p->port[i].routed_to = NO_PORT;
  293. }
  294. static void print_device_full(struct videohub_data *d) {
  295. print_device_info(d);
  296. print_device_video_inputs(d);
  297. print_device_video_outputs(d);
  298. print_device_monitoring_outputs(d);
  299. print_device_serial_ports(d);
  300. print_device_processing_units(d);
  301. fflush(stdout);
  302. }
  303. static int read_device_command_stream(struct videohub_data *d) {
  304. int ret, ncommands = 0;
  305. char buf[8192 + 1];
  306. if (test_data)
  307. return 0;
  308. memset(buf, 0, sizeof(buf));
  309. while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
  310. ncommands += parse_text_buffer(d, buf);
  311. memset(buf, 0, sizeof(buf));
  312. }
  313. return ncommands;
  314. }
  315. static void send_device_command(struct videohub_data *d, char *cmd_buffer) {
  316. if (!test_data)
  317. fdwrite(d->dev_fd, cmd_buffer, strlen(cmd_buffer));
  318. else
  319. parse_text_buffer(d, cmd_buffer);
  320. }
  321. int main(int argc, char **argv) {
  322. struct videohub_data *data = &maindata;
  323. parse_options(data, argc, argv);
  324. set_log_io_errors(0);
  325. if (!test_data) {
  326. data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
  327. if (data->dev_fd < 0)
  328. exit(EXIT_FAILURE);
  329. }
  330. reset_routed_to(&data->serial);
  331. reset_routed_to(&data->proc_units);
  332. read_device_command_stream(data);
  333. if (test_data)
  334. parse_text_buffer(data, test_data);
  335. if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
  336. die("The device does not respond correctly. Is it Videohub?");
  337. if (strstr(data->device.protocol_ver, "2.") != data->device.protocol_ver)
  338. die("Device protocol is %s but this program supports 2.x only.\n",
  339. data->device.protocol_ver);
  340. if (!data->device.dev_present) {
  341. if (data->device.needs_fw_update) {
  342. die("Device reports that it needs firmware update.");
  343. }
  344. die("Device reports that it is not present.");
  345. }
  346. check_number_of_ports(&data->inputs);
  347. check_number_of_ports(&data->outputs);
  348. check_number_of_ports(&data->mon_outputs);
  349. check_number_of_ports(&data->serial);
  350. check_number_of_ports(&data->proc_units);
  351. if (num_parsed_cmds) {
  352. unsigned int i;
  353. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  354. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  355. if (!ve->param1)
  356. continue;
  357. prepare_cmd_entry(data, &parsed_cmds.entry[i]);
  358. }
  359. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  360. char cmd_buffer[1024];
  361. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  362. if (!ve->param1)
  363. continue;
  364. format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
  365. if (strlen(cmd_buffer)) {
  366. show_cmd(data, ve);
  367. send_device_command(data, cmd_buffer);
  368. read_device_command_stream(data);
  369. }
  370. }
  371. // Show the result after commands
  372. if (test_data)
  373. print_device_full(data);
  374. } else if (show_monitor) {
  375. while (1) {
  376. int sleeps = 0;
  377. printf("\e[2J\e[H"); // Clear screen
  378. time_t now = time(NULL);
  379. struct tm *tm = localtime(&now);
  380. printf("Last update: %s\n", asctime(tm));
  381. print_device_info(data);
  382. print_device_video_inputs(data);
  383. print_device_video_outputs(data);
  384. fflush(stdout);
  385. do {
  386. usleep(500000);
  387. if (++sleeps >= 20)
  388. send_device_command(data, "PING:\n\n");
  389. } while (read_device_command_stream(data) == 0);
  390. }
  391. } else if (show_list) {
  392. if (show_list & action_list_device) print_device_info(data);
  393. if (show_list & action_list_vinputs) print_device_video_inputs(data);
  394. if (show_list & action_list_voutputs) print_device_video_outputs(data);
  395. if (show_list & action_list_moutputs) print_device_monitoring_outputs(data);
  396. if (show_list & action_list_serial) print_device_serial_ports(data);
  397. if (show_list & action_list_proc_units) print_device_processing_units(data);
  398. fflush(stdout);
  399. } else if (show_backup) {
  400. print_device_backup(data);
  401. } else if (show_info) {
  402. print_device_full(data);
  403. }
  404. shutdown_fd(&data->dev_fd);
  405. free(test_data);
  406. return 0;
  407. }