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

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