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

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