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

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