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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. switch (slot_data[0]) {
  147. case 'O': data->outputs[slot_pos].lock = PORT_LOCKED; break;
  148. case 'L': data->outputs[slot_pos].lock = PORT_LOCKED_OTHER; break;
  149. default : data->outputs[slot_pos].lock = PORT_UNLOCKED; break;
  150. }
  151. }
  152. break;
  153. case CMD_VIDEO_OUTPUT_ROUTING:
  154. if (valid_slot) {
  155. unsigned int dest_pos = strtoul(slot_data, NULL, 10);
  156. if (dest_pos < ARRAY_SIZE(data->inputs))
  157. data->outputs[slot_pos].routed_to = dest_pos;
  158. }
  159. break;
  160. case CMD_PING:
  161. case CMD_ACK:
  162. // Do nothing
  163. break;
  164. case CMD_NAK:
  165. ret = false;
  166. break;
  167. }
  168. }
  169. free(cmd_data);
  170. return ret;
  171. }
  172. int parse_text_buffer(struct videohub_data *data, char *cmd_buffer) {
  173. // The buffer contains only one command, no splitting is needed
  174. if (!strstr(cmd_buffer, "\n\n"))
  175. return parse_command(data, cmd_buffer);
  176. // Split commands and parse them one by one
  177. int ok_commands = 0;
  178. char *buf_copy = xstrdup(cmd_buffer);
  179. char *newcmd, *cmd = buf_copy;
  180. while(1) {
  181. newcmd = strstr(cmd, "\n\n"); // Find next command
  182. if (!newcmd) {
  183. if (parse_command(data, cmd)) // Parse current command
  184. ok_commands++;
  185. break;
  186. }
  187. newcmd[0] = '\0'; // Terminate previous command
  188. if (parse_command(data, cmd)) // Parse previous command
  189. ok_commands++;
  190. cmd = newcmd + 2; // Advance cmd to the next command
  191. }
  192. free(buf_copy);
  193. return ok_commands;
  194. }
  195. // Try to find input/output with certain name, return 0 on not found, pos + 1 is found
  196. static int search_video_output_name(struct videohub_data *d, char *name) {
  197. unsigned int i;
  198. for(i = 0; i < d->device.num_video_outputs; i++) {
  199. if (streq(name, d->outputs[i].name)) {
  200. return i + 1;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int search_video_input_name(struct videohub_data *d, char *name) {
  206. unsigned int i;
  207. for(i = 0; i < d->device.num_video_inputs; i++) {
  208. if (streq(name, d->inputs[i].name)) {
  209. return i + 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. // Return 0 on error, number otherwise if it can parse the whole input
  215. static unsigned int my_atoi(char *txt) {
  216. char *endptr = NULL;
  217. if (!txt)
  218. return 0;
  219. unsigned int ret = strtoul(txt, &endptr, 10);
  220. if (endptr == txt || *endptr)
  221. return 0;
  222. return ret;
  223. }
  224. void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
  225. e->port_no1 = my_atoi(e->param1);
  226. e->port_no2 = my_atoi(e->param2);
  227. switch (e->cmd) {
  228. case CMD_INPUT_LABELS:
  229. if (e->port_no1 == 0 || e->port_no1 > d->device.num_video_inputs) {
  230. e->port_no1 = search_video_input_name(d, e->param1);
  231. if (!e->port_no1)
  232. die("Unknown input port number/name: %s", e->param1);
  233. }
  234. break;
  235. case CMD_OUTPUT_LABELS:
  236. case CMD_VIDEO_OUTPUT_LOCKS:
  237. if (e->port_no1 == 0 || e->port_no1 > d->device.num_video_outputs) {
  238. e->port_no1 = search_video_output_name(d, e->param1);
  239. if (!e->port_no1)
  240. die("Unknown output port number/name: %s", e->param1);
  241. }
  242. e->lock = d->outputs[e->port_no1 - 1].lock;
  243. break;
  244. case CMD_VIDEO_OUTPUT_ROUTING:
  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. }
  250. if (e->port_no2 == 0 || e->port_no2 > d->device.num_video_inputs) {
  251. e->port_no2 = search_video_input_name(d, e->param2);
  252. if (!e->port_no2)
  253. die("Unknown input port number/name: %s", e->param2);
  254. }
  255. break;
  256. case CMD_VIDEO_INPUT_STATUS:
  257. case CMD_VIDEO_OUTPUT_STATUS:
  258. case CMD_PROTOCOL_PREAMBLE:
  259. case CMD_VIDEOHUB_DEVICE:
  260. case CMD_PING:
  261. case CMD_ACK:
  262. case CMD_NAK:
  263. break;
  264. }
  265. }
  266. void format_cmd_text(struct vcmd_entry *e, char *buf, unsigned int bufsz) {
  267. switch (e->cmd) {
  268. case CMD_INPUT_LABELS:
  269. snprintf(buf, bufsz, "%s:\n%u %s\n\n", get_cmd_text(e->cmd),
  270. e->port_no1 - 1, e->param2);
  271. break;
  272. case CMD_OUTPUT_LABELS:
  273. snprintf(buf, bufsz, "%s:\n%u %s\n\n", get_cmd_text(e->cmd),
  274. e->port_no1 - 1, e->param2);
  275. break;
  276. case CMD_VIDEO_OUTPUT_LOCKS:
  277. snprintf(buf, bufsz, "%s:\n%u %s\n\n", get_cmd_text(e->cmd),
  278. e->port_no1 - 1, e->do_lock ? "O" : (e->lock == PORT_LOCKED_OTHER ? "F" : "U"));
  279. break;
  280. case CMD_VIDEO_OUTPUT_ROUTING:
  281. snprintf(buf, bufsz, "%s:\n%u %u\n\n", get_cmd_text(e->cmd),
  282. e->port_no1 - 1, e->port_no2 - 1);
  283. break;
  284. case CMD_VIDEO_INPUT_STATUS:
  285. case CMD_VIDEO_OUTPUT_STATUS:
  286. case CMD_PROTOCOL_PREAMBLE:
  287. case CMD_VIDEOHUB_DEVICE:
  288. case CMD_PING:
  289. case CMD_ACK:
  290. case CMD_NAK:
  291. break;
  292. }
  293. }
  294. void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
  295. const char *prefix = "videohub: ";
  296. switch (e->cmd) {
  297. case CMD_INPUT_LABELS:
  298. printf("%srename video input %d - \"%s\" to \"%s\"\n",
  299. prefix,
  300. e->port_no1, d->inputs[e->port_no1 - 1].name,
  301. e->param2
  302. );
  303. break;
  304. case CMD_OUTPUT_LABELS:
  305. printf("%srename video output %d - \"%s\" to \"%s\"\n",
  306. prefix,
  307. e->port_no1, d->outputs[e->port_no1 - 1].name,
  308. e->param2
  309. );
  310. break;
  311. case CMD_VIDEO_OUTPUT_LOCKS:
  312. printf("%s%s video output %d - \"%s\"\n",
  313. prefix,
  314. e->do_lock ? "lock" : (e->lock == PORT_LOCKED_OTHER ? "force unlock" : "unlock"),
  315. e->port_no1, d->outputs[e->port_no1 - 1].name
  316. );
  317. break;
  318. case CMD_VIDEO_OUTPUT_ROUTING:
  319. printf("%sset video output %d \"%s\" to read from input %d \"%s\"\n",
  320. prefix,
  321. e->port_no1, d->outputs[e->port_no1 - 1].name,
  322. e->port_no2, d->inputs [e->port_no2 - 1].name
  323. );
  324. break;
  325. case CMD_VIDEO_INPUT_STATUS:
  326. case CMD_VIDEO_OUTPUT_STATUS:
  327. case CMD_PROTOCOL_PREAMBLE:
  328. case CMD_VIDEOHUB_DEVICE:
  329. case CMD_PING:
  330. case CMD_ACK:
  331. case CMD_NAK:
  332. break;
  333. }
  334. }