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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. int timeout = 15;
  29. static struct videohub_data maindata;
  30. static int show_info = 1;
  31. static int show_monitor = 0;
  32. static int show_backup = 0;
  33. static int show_list = 0;
  34. static char *test_data;
  35. enum list_actions {
  36. action_list_device = (1 << 0),
  37. action_list_vinputs = (1 << 1),
  38. action_list_voutputs = (1 << 2),
  39. action_list_moutputs = (1 << 3),
  40. action_list_serial = (1 << 4),
  41. action_list_proc_units = (1 << 5),
  42. action_list_frames = (1 << 6),
  43. };
  44. static const char *program_id = PROGRAM_NAME " Version: " VERSION " Git: " GIT_VER;
  45. static const char short_options[] = "h:p:T:qdHVimbt:";
  46. static const struct option long_options[] = {
  47. { "host", required_argument, NULL, 'h' },
  48. { "port", required_argument, NULL, 'p' },
  49. { "test-input", required_argument, NULL, 'T' },
  50. { "quiet", no_argument, NULL, 'q' },
  51. { "debug", no_argument, NULL, 'd' },
  52. { "help", no_argument, NULL, 'H' },
  53. { "version", no_argument, NULL, 'V' },
  54. { "info", no_argument, NULL, 'i' },
  55. { "monitor", no_argument, NULL, 'm' },
  56. { "backup", no_argument, NULL, 'b' },
  57. { "timeout", required_argument, NULL, 't' },
  58. { "list-device", no_argument, NULL, 901 },
  59. { "list-inputs", no_argument, NULL, 902 },
  60. { "list-vinputs", no_argument, NULL, 902 },
  61. { "list-outputs", no_argument, NULL, 903 },
  62. { "list-voutputs", no_argument, NULL, 903 },
  63. { "list-monitoring", no_argument, NULL, 904 },
  64. { "list-moutputs", no_argument, NULL, 904 },
  65. { "list-serial", no_argument, NULL, 905 },
  66. { "list-proc-units", no_argument, NULL, 906 },
  67. { "list-frames", no_argument, NULL, 907 },
  68. { "set-name", required_argument, NULL, 950 },
  69. { "in-name", required_argument, NULL, 1001 },
  70. { "in-output", required_argument, NULL, 1002 },
  71. { "in-monitor", required_argument, NULL, 1003 },
  72. { "vi-name", required_argument, NULL, 1001 },
  73. { "vi-output", required_argument, NULL, 1002 },
  74. { "vi-monitor", required_argument, NULL, 1003 },
  75. { "out-name", required_argument, NULL, 2001 },
  76. { "out-input", required_argument, NULL, 2002 },
  77. { "out-route", required_argument, NULL, 2002 }, // Alias
  78. { "out-lock", required_argument, NULL, 2003 },
  79. { "out-unlock", required_argument, NULL, 2004 },
  80. { "vo-name", required_argument, NULL, 2001 },
  81. { "vo-input", required_argument, NULL, 2002 },
  82. { "vo-route", required_argument, NULL, 2002 }, // Alias
  83. { "vo-lock", required_argument, NULL, 2003 },
  84. { "vo-unlock", required_argument, NULL, 2004 },
  85. { "mon-name", required_argument, NULL, 3001 },
  86. { "mon-input", required_argument, NULL, 3002 },
  87. { "mon-route", required_argument, NULL, 3002 }, // Alias
  88. { "mon-lock", required_argument, NULL, 3003 },
  89. { "mon-unlock", required_argument, NULL, 3004 },
  90. { "mo-name", required_argument, NULL, 3001 },
  91. { "mo-input", required_argument, NULL, 3002 },
  92. { "mo-route", required_argument, NULL, 3002 }, // Alias
  93. { "mo-lock", required_argument, NULL, 3003 },
  94. { "mo-unlock", required_argument, NULL, 3004 },
  95. { "ser-name", required_argument, NULL, 4001 },
  96. { "ser-input", required_argument, NULL, 4002 },
  97. { "ser-connect", required_argument, NULL, 4002 }, // Alias
  98. { "ser-route", required_argument, NULL, 4002 }, // Alias
  99. { "ser-lock", required_argument, NULL, 4003 },
  100. { "ser-unlock", required_argument, NULL, 4004 },
  101. { "ser-dir", required_argument, NULL, 4005 },
  102. { "ser-clear", required_argument, NULL, 4006 },
  103. { "se-name", required_argument, NULL, 4001 },
  104. { "se-input", required_argument, NULL, 4002 },
  105. { "se-connect", required_argument, NULL, 4002 }, // Alias
  106. { "se-route", required_argument, NULL, 4002 }, // Alias
  107. { "se-lock", required_argument, NULL, 4003 },
  108. { "se-unlock", required_argument, NULL, 4004 },
  109. { "se-dir", required_argument, NULL, 4005 },
  110. { "se-clear", required_argument, NULL, 4006 },
  111. { "pu-input", required_argument, NULL, 5001 },
  112. { "pu-connect", required_argument, NULL, 5001 }, // Alias
  113. { "pu-route", required_argument, NULL, 5001 }, // Alias
  114. { "pu-lock", required_argument, NULL, 5002 },
  115. { "pu-unlock", required_argument, NULL, 5003 },
  116. { "pu-clear", required_argument, NULL, 5004 },
  117. { "fr-name", required_argument, NULL, 6001 },
  118. { "fr-output", required_argument, NULL, 6002 },
  119. { "fr-connect", required_argument, NULL, 6002 }, // Alias
  120. { "fr-input", required_argument, NULL, 6002 }, // Alias
  121. { "fr-route", required_argument, NULL, 6002 }, // Alias
  122. { "fr-lock", required_argument, NULL, 6003 },
  123. { "fr-unlock", required_argument, NULL, 6004 },
  124. { "fr-clear", required_argument, NULL, 6006 },
  125. { 0, 0, 0, 0 }
  126. };
  127. static void show_help(struct videohub_data *data) {
  128. printf("%s\n", program_id);
  129. printf("\n");
  130. printf(" Usage: " PROGRAM_NAME " --host <host> [..commands..]\n");
  131. printf("\n");
  132. printf("Main options:\n");
  133. printf(" -h --host <host> | Set device host name.\n");
  134. printf(" -p --port <port_number> | Set device port (default: 9990).\n");
  135. printf(" -t --timeout <secs> | Set connect/read timeout. Default: %d\n", timeout);
  136. printf("\n");
  137. printf("Commands:\n");
  138. printf(" -i --info | Show full device info (default command).\n");
  139. printf(" . This command is shows the equivalent of\n");
  140. printf(" . running all --list-XXX commands.\n");
  141. printf(" -m --monitor | Display real-time config changes monitor.\n");
  142. printf(" -b --backup | Show the command line that will restore\n");
  143. printf(" . the device to the current configuration.\n");
  144. printf("\n");
  145. printf(" --list-device | Display device info.\n");
  146. printf(" --list-inputs | List device input ports.\n");
  147. printf(" --list-outputs | List device output ports.\n");
  148. printf(" --list-monitor | List device monitoring outputs.\n");
  149. printf(" --list-serial | List device serial ports.\n");
  150. printf(" --list-proc-units | List device processing units.\n");
  151. printf(" --list-frames | List device frame buffers.\n");
  152. printf("\n");
  153. printf(" --set-name <name> | Set the device \"friendly name\".\n");
  154. printf("\n");
  155. printf("Inputs configuration:\n");
  156. printf(" --in-name <in_X> <name> | Set input port X name.\n");
  157. printf(" --in-output <in_X> <out_Y> | Route input X to output Y.\n");
  158. printf(" --in-monitor <in_X> <mout_Y> | Route input X to monitor port Y.\n");
  159. printf("\n");
  160. printf("Outputs configuration:\n");
  161. printf(" --out-name <out_X> <name> | Set output port X name.\n");
  162. printf(" --out-input <out_X> <in_Y> | Connect output X to input Y.\n");
  163. printf(" --out-lock <out_X> | Lock output port X.\n");
  164. printf(" --out-unlock <out_X> | Unlock output port X.\n");
  165. printf("\n");
  166. printf("Monitoring outputs configuration:\n");
  167. printf(" --mon-name <mout_X> <name> | Set monitoring port X name.\n");
  168. printf(" --mon-input <mout_X> <in_Y> | Connect monitoring X to input Y.\n");
  169. printf(" --mon-lock <mout_X> | Lock monitoring port X.\n");
  170. printf(" --mon-unlock <mout_X> | Unlock monitoring port X.\n");
  171. printf("\n");
  172. printf("Serial ports configuration:\n");
  173. printf(" --ser-name <ser_X> <name> | Set serial port X name.\n");
  174. printf(" --ser-connect <ser_X> <ser_Y> | Connect serial X to serial Y.\n");
  175. printf(" --ser-clear <ser_X> | Disconnect serial port X from serial Y.\n");
  176. printf(" --ser-lock <ser_X> | Lock serial port X.\n");
  177. printf(" --ser-unlock <ser_X> | Unlock serial port X.\n");
  178. printf(" --ser-dir <ser_X> <dir> | Set serial port X direction.\n");
  179. printf(" . <dir> can be 'auto', 'in' or 'out'.\n");
  180. printf("\n");
  181. printf("Processing units configuration:\n");
  182. printf(" --pu-input <pu_X> <in_Y> | Connect processing unit X to input Y.\n");
  183. printf(" --pu-clear <pu_X> | Disconnect unit X from input Y.\n");
  184. printf(" --pu-lock <pu_X> | Lock processing unit X.\n");
  185. printf(" --pu-unlock <pu_X> | Unlock processing unit X.\n");
  186. printf("\n");
  187. printf("Frames configuration:\n");
  188. printf(" --fr-name <fr_X> <name> | Set frame X name.\n");
  189. printf(" --fr-output <fr_X> <out_Y> | Output frame X to output Y.\n");
  190. printf(" --fr-clear <fr_X> | Stop outputing frame X to the output.\n");
  191. printf(" --fr-lock <fr_X> | Lock frame X.\n");
  192. printf(" --fr-unlock <rf_X> | Unlock frame X.\n");
  193. printf("\n");
  194. printf("Misc options:\n");
  195. printf(" -T --test-input <file> | Read commands from <file>.\n");
  196. printf(" -d --debug | Increase logging verbosity.\n");
  197. printf(" -q --quiet | Suppress warnings.\n");
  198. printf(" -H --help | Show help screen.\n");
  199. printf(" -V --version | Show program version.\n");
  200. printf("\n");
  201. }
  202. static int num_parsed_cmds = 0;
  203. static struct run_cmds {
  204. struct vcmd_entry entry[MAX_RUN_CMDS];
  205. } parsed_cmds;
  206. static void check_num_parsed_cmds(void) {
  207. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  208. die("No more than %u commands are supported.", num_parsed_cmds);
  209. }
  210. static void parse_cmd2(int argc, char **argv, enum vcmd vcmd) {
  211. check_num_parsed_cmds();
  212. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  213. if (optind == argc || argv[optind - 1][0] == '-' || argv[optind][0] == '-') {
  214. fprintf(stderr, "%s: option '%s' requires two arguments\n", argv[0], argv[optind - 2]);
  215. exit(EXIT_FAILURE);
  216. }
  217. c->cmd = &videohub_commands[vcmd];
  218. c->p1.param = argv[optind - 1];
  219. c->p2.param = argv[optind];
  220. if (vcmd == CMD_SERIAL_PORT_DIRECTIONS) {
  221. if (strcasecmp("in", c->p2.param) == 0) c->direction = DIR_CONTROL;
  222. else if (strcasecmp("out", c->p2.param) == 0) c->direction = DIR_SLAVE;
  223. else if (strcasecmp("auto", c->p2.param) == 0) c->direction = DIR_AUTO;
  224. else die("Invalid serial port direction '%s'. Allowed are: in, out, auto.", c->p2.param);
  225. }
  226. num_parsed_cmds++;
  227. }
  228. static void parse_cmd2s(int argc, char **argv, enum vcmd vcmd) {
  229. check_num_parsed_cmds();
  230. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  231. c->cmd = &videohub_commands[vcmd];
  232. c->p1.param = optarg;
  233. c->p2.param = "1"; // Fake
  234. c->clear_port = true;
  235. num_parsed_cmds++;
  236. }
  237. static void parse_cmd1(int argc, char **argv, enum vcmd vcmd, bool do_lock) {
  238. check_num_parsed_cmds();
  239. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  240. c->cmd = &videohub_commands[vcmd];
  241. c->p1.param = argv[optind - 1];
  242. c->do_lock = do_lock;
  243. num_parsed_cmds++;
  244. }
  245. static void set_device_option(char *setting, char *value) {
  246. check_num_parsed_cmds();
  247. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  248. c->cmd = &videohub_commands[CMD_VIDEOHUB_DEVICE];
  249. c->p1.param = setting;
  250. c->p2.param = value;
  251. num_parsed_cmds++;
  252. }
  253. static void switch_cmd_args(void) {
  254. char *p1, *p2;
  255. parsed_cmds.entry[num_parsed_cmds-1].reversed_args = 1;
  256. p1 = parsed_cmds.entry[num_parsed_cmds-1].p1.param;
  257. p2 = parsed_cmds.entry[num_parsed_cmds-1].p2.param;
  258. parsed_cmds.entry[num_parsed_cmds-1].p1.param = p2;
  259. parsed_cmds.entry[num_parsed_cmds-1].p2.param = p1;
  260. }
  261. static void parse_options(struct videohub_data *data, int argc, char **argv) {
  262. int j, err = 0;
  263. // Check environment
  264. data->dev_host = getenv("VIDEOHUB_HOST");
  265. data->dev_port = getenv("VIDEOHUB_PORT");
  266. // Set defaults
  267. if (!data->dev_port)
  268. data->dev_port = "9990";
  269. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  270. if (j == '?') // Invalid parameter
  271. exit(EXIT_FAILURE);
  272. switch (j) {
  273. case 'h': // --host
  274. data->dev_host = optarg;
  275. break;
  276. case 'p': // --port
  277. data->dev_port = optarg;
  278. break;
  279. case 't': // --timeout
  280. timeout = strtoimax(optarg, NULL, 0);
  281. break;
  282. case 'T': { // --test-input
  283. struct stat st;
  284. FILE *f;
  285. if (stat(optarg, &st) != 0)
  286. die("Can't stat %s: %s", optarg, strerror(errno));
  287. f = fopen(optarg, "r");
  288. if (!f)
  289. die("Can't open %s: %s", optarg, strerror(errno));
  290. test_data = xzalloc(st.st_size + 1);
  291. if (fread(test_data, st.st_size, 1, f) < 1)
  292. die("Can't read from %s: %s", optarg, strerror(errno));
  293. fclose(f);
  294. data->dev_host = "sdi-matrix";
  295. break;
  296. }
  297. case 'd': // --debug
  298. debug++;
  299. if (debug)
  300. quiet = 0; // Disable quiet
  301. break;
  302. case 'q': // --quiet
  303. quiet = !quiet;
  304. break;
  305. case 'i': // --info
  306. show_info = 1;
  307. break;
  308. case 'm': // --monitor
  309. show_monitor = 1;
  310. break;
  311. case 'b': // --backup
  312. show_backup = 1;
  313. break;
  314. case 901: show_list |= action_list_device; break; // --list-device
  315. case 902: show_list |= action_list_vinputs; break; // --list-vinputs
  316. case 903: show_list |= action_list_voutputs; break; // --list-voutputs
  317. case 904: show_list |= action_list_moutputs; break; // --list-moutputs
  318. case 905: show_list |= action_list_serial; break; // --list-serial
  319. case 906: show_list |= action_list_proc_units; break; // --list-proc-units
  320. case 907: show_list |= action_list_frames; break; // --list-frames
  321. case 950: set_device_option("Friendly name", optarg); break; // --set-name
  322. case 1001: parse_cmd2(argc, argv, CMD_INPUT_LABELS); break; // --in-name
  323. case 1002: parse_cmd2(argc, argv, CMD_VIDEO_OUTPUT_ROUTING); switch_cmd_args(); break; // --in-output
  324. case 1003: parse_cmd2(argc, argv, CMD_MONITORING_OUTPUT_ROUTING); switch_cmd_args(); break; // --in-monitor
  325. case 2001: parse_cmd2(argc, argv, CMD_OUTPUT_LABELS); break; // --out-name
  326. case 2002: parse_cmd2(argc, argv, CMD_VIDEO_OUTPUT_ROUTING); break; // --out-input
  327. case 2003: parse_cmd1(argc, argv, CMD_VIDEO_OUTPUT_LOCKS, true); break; // --out-lock
  328. case 2004: parse_cmd1(argc, argv, CMD_VIDEO_OUTPUT_LOCKS, false); break; // --out-unlock
  329. case 3001: parse_cmd2(argc, argv, CMD_MONITORING_OUTPUT_LABELS); break; // --mon-name
  330. case 3002: parse_cmd2(argc, argv, CMD_MONITORING_OUTPUT_ROUTING); break; // --mon-input
  331. case 3003: parse_cmd1(argc, argv, CMD_MONITORING_OUTPUT_LOCKS, true); break; // --mon-lock
  332. case 3004: parse_cmd1(argc, argv, CMD_MONITORING_OUTPUT_LOCKS, false); break; // --mon-unlock
  333. case 4001: parse_cmd2(argc, argv, CMD_SERIAL_PORT_LABELS); break; // --ser-name
  334. case 4002: parse_cmd2(argc, argv, CMD_SERIAL_PORT_ROUTING); break; // --ser-input
  335. case 4003: parse_cmd1(argc, argv, CMD_SERIAL_PORT_LOCKS, true); break; // --ser-lock
  336. case 4004: parse_cmd1(argc, argv, CMD_SERIAL_PORT_LOCKS, false); break; // --ser-unlock
  337. case 4005: parse_cmd2(argc, argv, CMD_SERIAL_PORT_DIRECTIONS); break; // --ser-dir
  338. case 4006: parse_cmd2s(argc, argv, CMD_SERIAL_PORT_ROUTING); break; // --ser-clear
  339. case 5001: parse_cmd2(argc, argv, CMD_PROCESSING_UNIT_ROUTING); break; // --pu-input
  340. case 5002: parse_cmd1(argc, argv, CMD_PROCESSING_UNIT_LOCKS, true); break; // --pu-lock
  341. case 5003: parse_cmd1(argc, argv, CMD_PROCESSING_UNIT_LOCKS, false); break; // --pu-unlock
  342. case 5004: parse_cmd2s(argc, argv, CMD_PROCESSING_UNIT_ROUTING); break; // --pu-clear
  343. case 6001: parse_cmd2(argc, argv, CMD_FRAME_LABELS); break; // --fr-name
  344. case 6002: parse_cmd2(argc, argv, CMD_FRAME_BUFFER_ROUTING); break; // --fr-input
  345. case 6003: parse_cmd1(argc, argv, CMD_FRAME_BUFFER_LOCKS, true); break; // --fr-lock
  346. case 6004: parse_cmd1(argc, argv, CMD_FRAME_BUFFER_LOCKS, false); break; // --fr-unlock
  347. case 6005: parse_cmd2s(argc, argv, CMD_FRAME_BUFFER_ROUTING); break; // --fr-clear
  348. case 'H': // --help
  349. show_help(data);
  350. exit(EXIT_SUCCESS);
  351. case 'V': // --version
  352. printf("%s\n", program_id);
  353. exit(EXIT_SUCCESS);
  354. }
  355. }
  356. if (!data->dev_host || !strtoul(data->dev_port, NULL, 10) || !timeout || timeout > 60)
  357. err = 1;
  358. if (err) {
  359. show_help(data);
  360. if (!data->dev_host)
  361. fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
  362. if (!timeout || timeout > 60)
  363. fprintf(stderr, "ERROR: invalid timeout value, must be between 1 and 60\n");
  364. exit(EXIT_FAILURE);
  365. }
  366. d("Device address: %s:%s\n", data->dev_host, data->dev_port);
  367. }
  368. static void check_number_of_ports(struct port_set *p) {
  369. if (p->num > ARRAY_SIZE(p->port))
  370. die("The device supports %d ports. Increase MAX_PORTS (%lu) and recompile the program.",
  371. p->num, (unsigned long)ARRAY_SIZE(p->port));
  372. }
  373. static void reset_routed_to(struct port_set *p) {
  374. unsigned int i;
  375. for (i = 0; i < ARRAY_SIZE(p->port); i++)
  376. p->port[i].routed_to = NO_PORT;
  377. }
  378. static void print_device_full(struct videohub_data *d) {
  379. print_device_info(d);
  380. print_device_video_inputs(d);
  381. print_device_video_outputs(d);
  382. print_device_monitoring_outputs(d);
  383. print_device_serial_ports(d);
  384. print_device_processing_units(d);
  385. print_device_frame_buffers(d);
  386. fflush(stdout);
  387. }
  388. static int read_device_command_stream(struct videohub_data *d) {
  389. int ret, ncommands = 0;
  390. char buf[8192 + 1];
  391. if (test_data)
  392. return 0;
  393. memset(buf, 0, sizeof(buf));
  394. while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, timeout, 0, 1)) >= 0) {
  395. ncommands += parse_text_buffer(d, buf);
  396. memset(buf, 0, sizeof(buf));
  397. }
  398. return ncommands;
  399. }
  400. static void send_device_command(struct videohub_data *d, char *cmd_buffer) {
  401. if (!test_data)
  402. fdwrite(d->dev_fd, cmd_buffer, strlen(cmd_buffer));
  403. else
  404. parse_text_buffer(d, cmd_buffer);
  405. }
  406. int main(int argc, char **argv) {
  407. struct videohub_data *data = &maindata;
  408. parse_options(data, argc, argv);
  409. set_log_io_errors(0);
  410. if (!test_data) {
  411. data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
  412. if (data->dev_fd < 0)
  413. exit(EXIT_FAILURE);
  414. }
  415. reset_routed_to(&data->inputs);
  416. reset_routed_to(&data->outputs);
  417. reset_routed_to(&data->serial);
  418. reset_routed_to(&data->proc_units);
  419. reset_routed_to(&data->frames);
  420. read_device_command_stream(data);
  421. if (test_data)
  422. parse_text_buffer(data, test_data);
  423. if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
  424. die("The device does not respond correctly. Is it Videohub?");
  425. if (strstr(data->device.protocol_ver, "2.") != data->device.protocol_ver)
  426. q("WARNING: Device protocol is %s but this program is tested with 2.x only.\n",
  427. data->device.protocol_ver);
  428. if (!data->device.dev_present) {
  429. if (data->device.needs_fw_update) {
  430. die("Device reports that it needs firmware update.");
  431. }
  432. die("Device reports that it is not present.");
  433. }
  434. check_number_of_ports(&data->inputs);
  435. check_number_of_ports(&data->outputs);
  436. check_number_of_ports(&data->mon_outputs);
  437. check_number_of_ports(&data->serial);
  438. check_number_of_ports(&data->proc_units);
  439. check_number_of_ports(&data->frames);
  440. if (num_parsed_cmds) {
  441. unsigned int i;
  442. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  443. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  444. if (!ve->p1.param)
  445. continue;
  446. prepare_cmd_entry(data, &parsed_cmds.entry[i]);
  447. }
  448. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  449. char cmd_buffer[1024];
  450. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  451. if (!ve->p1.param)
  452. continue;
  453. format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
  454. if (strlen(cmd_buffer)) {
  455. show_cmd(data, ve);
  456. send_device_command(data, cmd_buffer);
  457. read_device_command_stream(data);
  458. }
  459. }
  460. // Show the result after commands
  461. if (test_data)
  462. print_device_full(data);
  463. } else if (show_monitor) {
  464. while (1) {
  465. int sleeps = 0;
  466. printf("\e[2J\e[H"); // Clear screen
  467. time_t now = time(NULL);
  468. struct tm *tm = localtime(&now);
  469. printf("Last update: %s\n", asctime(tm));
  470. print_device_info(data);
  471. print_device_video_inputs(data);
  472. print_device_video_outputs(data);
  473. fflush(stdout);
  474. do {
  475. usleep(500000);
  476. if (++sleeps >= 20)
  477. send_device_command(data, "PING:\n\n");
  478. } while (read_device_command_stream(data) == 0);
  479. }
  480. } else if (show_list) {
  481. if (show_list & action_list_device) print_device_info(data);
  482. if (show_list & action_list_vinputs) print_device_video_inputs(data);
  483. if (show_list & action_list_voutputs) print_device_video_outputs(data);
  484. if (show_list & action_list_moutputs) print_device_monitoring_outputs(data);
  485. if (show_list & action_list_serial) print_device_serial_ports(data);
  486. if (show_list & action_list_proc_units) print_device_processing_units(data);
  487. if (show_list & action_list_frames) print_device_frame_buffers(data);
  488. fflush(stdout);
  489. } else if (show_backup) {
  490. print_device_backup(data);
  491. } else if (show_info) {
  492. print_device_full(data);
  493. }
  494. shutdown_fd(&data->dev_fd);
  495. free(test_data);
  496. return 0;
  497. }