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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * === Command parser ===
  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. /*
  19. Example response from videohub (on connect)
  20. ===========================================
  21. PROTOCOL PREAMBLE:
  22. Version: 2.4
  23. VIDEOHUB DEVICE:
  24. Device present: true
  25. Model name: Blackmagic Micro Videohub
  26. Unique ID: 7c2e0d021714
  27. Video inputs: 16
  28. Video processing units: 0
  29. Video outputs: 16
  30. Video monitoring outputs: 0
  31. Serial ports: 0
  32. INPUT LABELS:
  33. 0 Windows 1
  34. 1 Windows 2
  35. 2 Windows 3
  36. 3 Windows 4 HD
  37. 4 Input 5
  38. 5 Input 6
  39. 6 Input 7
  40. 7 Input 8
  41. 8 Input 9
  42. 9 Input 10
  43. 10 Input 11
  44. 11 DPlay1
  45. 12 DPlay2
  46. 13 Input 14
  47. 14 Input 15
  48. 15 Loopback
  49. OUTPUT LABELS:
  50. 0 Enc1 1
  51. 1 Enc1 2
  52. 2 Enc1 3
  53. 3 Enc1 4
  54. 4 Output 5
  55. 5 Output 6
  56. 6 Output 7
  57. 7 Output 8
  58. 8 Enc2 1
  59. 9 Output 10
  60. 10 Output 11
  61. 11 Denc
  62. 12 Output 13
  63. 13 Output 14
  64. 14 Output 15
  65. 15 Loopback
  66. VIDEO OUTPUT LOCKS:
  67. 0 L
  68. 1 L
  69. 2 L
  70. 3 L
  71. 4 U
  72. 5 U
  73. 6 U
  74. 7 U
  75. 8 L
  76. 9 U
  77. 10 U
  78. 11 L
  79. 12 L
  80. 13 O
  81. 14 U
  82. 15 O
  83. VIDEO OUTPUT ROUTING:
  84. 0 2
  85. 1 1
  86. 2 0
  87. 3 0
  88. 4 4
  89. 5 5
  90. 6 6
  91. 7 7
  92. 8 3
  93. 9 3
  94. 10 10
  95. 11 12
  96. 12 11
  97. 13 13
  98. 14 14
  99. 15 15
  100. */
  101. enum vcmd {
  102. CMD_PROTOCOL_PREAMBLE,
  103. CMD_VIDEOHUB_DEVICE,
  104. CMD_INPUT_LABELS,
  105. CMD_OUTPUT_LABELS,
  106. CMD_VIDEO_OUTPUT_LOCKS,
  107. CMD_VIDEO_OUTPUT_ROUTING,
  108. CMD_PING,
  109. };
  110. static struct videohub_commands {
  111. enum vcmd cmd;
  112. const char *txt;
  113. } videohub_commands[] = {
  114. { CMD_PROTOCOL_PREAMBLE, "PROTOCOL PREAMBLE" },
  115. { CMD_VIDEOHUB_DEVICE, "VIDEOHUB DEVICE" },
  116. { CMD_INPUT_LABELS, "INPUT LABELS" },
  117. { CMD_OUTPUT_LABELS, "OUTPUT LABELS" },
  118. { CMD_VIDEO_OUTPUT_LOCKS, "VIDEO OUTPUT LOCKS" },
  119. { CMD_VIDEO_OUTPUT_ROUTING, "VIDEO OUTPUT ROUTING" },
  120. { CMD_PING, "PING" },
  121. };
  122. static char *parse_text(char *line, char *cmd) {
  123. char *parsed_text = strstr(line, cmd);
  124. if (parsed_text == line) {
  125. return parsed_text + strlen(cmd);
  126. }
  127. return NULL;
  128. }
  129. bool parse_command(struct videohub_data *data, char *cmd) {
  130. unsigned int i;
  131. if (!strlen(cmd))
  132. return false;
  133. struct videohub_commands *v = NULL;
  134. for (i = 0; i < ARRAY_SIZE(videohub_commands); i++) {
  135. if (!videohub_commands[i].txt)
  136. continue;
  137. if (strstr(cmd, videohub_commands[i].txt) == cmd) {
  138. v = &videohub_commands[i];
  139. break;
  140. }
  141. }
  142. if (!v) {
  143. if (!quiet) {
  144. fprintf(stderr, "WARNING: Videohub sent unknown command!\n");
  145. fprintf(stderr, " Please report this command to author's email: georgi@unixsol.org\n");
  146. fprintf(stderr, " You may use -q or --quiet to suppress the message.\n");
  147. fprintf(stderr, "---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  148. fprintf(stderr, "%s\n", cmd);
  149. fprintf(stderr, "---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  150. }
  151. return false;
  152. }
  153. v("verbose: Got '%s' command.\n", v->txt);
  154. if (verbose > 1)
  155. v("----\n%s\n----\n", cmd);
  156. bool cmd_response = false;
  157. char *p, *cmd_data = xstrdup( cmd + strlen(v->txt) + 2 ); // +2 to compensate for :\n at the end of the command
  158. // Split line by line
  159. char *line, *saveptr = NULL;
  160. for(i = 0, line = strtok_r(cmd_data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr), i++) {
  161. if (i == 0) { // Handle command response
  162. if (streq(line, "NAK"))
  163. return false;
  164. if (streq(line, "ACK")) {
  165. cmd_response = true;
  166. continue;
  167. }
  168. }
  169. if (i == 1 && cmd_response) { // The command must match
  170. if (strstr(line, v->txt) != line)
  171. return false;
  172. continue;
  173. }
  174. // Parse command data response looking like that: "[slot_pos] [slot_data]"
  175. bool valid_slot = false;
  176. unsigned int slot_pos = 0;
  177. char *slot_data = NULL;
  178. switch (v->cmd) {
  179. case CMD_INPUT_LABELS:
  180. case CMD_OUTPUT_LABELS:
  181. case CMD_VIDEO_OUTPUT_LOCKS:
  182. case CMD_VIDEO_OUTPUT_ROUTING:
  183. slot_data = strchr(line, ' ');
  184. if (slot_data) {
  185. slot_data[0] = '\0'; // Separate slot_pos from slot_data
  186. slot_data++;
  187. slot_pos = strtoul(line, NULL, 10);
  188. if (slot_pos < ARRAY_SIZE(data->inputs))
  189. valid_slot = true;
  190. }
  191. break;
  192. default:
  193. break;
  194. }
  195. // Parse commands
  196. switch (v->cmd) {
  197. case CMD_PROTOCOL_PREAMBLE:
  198. if ((p = parse_text(line, "Version: ")))
  199. snprintf(data->device.protocol_ver, sizeof(data->device.protocol_ver), "%s", p);
  200. break;
  201. case CMD_VIDEOHUB_DEVICE:
  202. if ((p = parse_text(line, "Device present: "))) {
  203. data->device.dev_present = streq(p, "true");
  204. data->device.needs_fw_update = streq(p, "needs_update");
  205. }
  206. if ((p = parse_text(line, "Model name: ")))
  207. snprintf(data->device.model_name, sizeof(data->device.model_name), "%s", p);
  208. if ((p = parse_text(line, "Unique ID: ")))
  209. snprintf(data->device.unique_id, sizeof(data->device.unique_id) , "%s", p);
  210. if ((p = parse_text(line, "Video inputs: ")))
  211. data->device.num_video_inputs = strtoul(p, NULL, 10);
  212. if ((p = parse_text(line, "Video processing units: ")))
  213. data->device.num_video_processing_units = strtoul(p, NULL, 10);
  214. if ((p = parse_text(line, "Video outputs: ")))
  215. data->device.num_video_outputs = strtoul(p, NULL, 10);
  216. if ((p = parse_text(line, "Video monitoring output: ")))
  217. data->device.num_video_monitoring_outputs = strtoul(p, NULL, 10);
  218. if ((p = parse_text(line, "Serial ports: ")))
  219. data->device.num_serial_ports = strtoul(p, NULL, 10);
  220. break;
  221. case CMD_INPUT_LABELS:
  222. if (valid_slot)
  223. snprintf(data->inputs[slot_pos].name, sizeof(data->inputs[slot_pos].name), "%s", slot_data);
  224. break;
  225. case CMD_OUTPUT_LABELS:
  226. if (valid_slot)
  227. snprintf(data->outputs[slot_pos].name, sizeof(data->inputs[slot_pos].name), "%s", slot_data);
  228. break;
  229. case CMD_VIDEO_OUTPUT_LOCKS:
  230. if (valid_slot) {
  231. // L is lock owned by somebody else (set from other IP address)
  232. // O is lock owned by us (set from our IP address)
  233. if (slot_data[0] == 'L' || slot_data[0] == 'O') {
  234. data->outputs[slot_pos].locked = true;
  235. if (slot_data[0] == 'L')
  236. data->outputs[slot_pos].locked_other = true;
  237. else
  238. data->outputs[slot_pos].locked_other = false;
  239. } else {
  240. data->outputs[slot_pos].locked = false;
  241. }
  242. }
  243. break;
  244. case CMD_VIDEO_OUTPUT_ROUTING:
  245. if (valid_slot) {
  246. unsigned int dest_pos = strtoul(slot_data, NULL, 10);
  247. if (dest_pos < ARRAY_SIZE(data->outputs))
  248. data->outputs[slot_pos].routed_to = dest_pos;
  249. }
  250. break;
  251. case CMD_PING:
  252. // Do nothing, we just get ACK without any data
  253. break;
  254. }
  255. }
  256. free(cmd_data);
  257. /* Check if everything is within limits */
  258. switch (v->cmd) {
  259. case CMD_VIDEOHUB_DEVICE:
  260. if (!data->device.dev_present) {
  261. if (data->device.needs_fw_update) {
  262. die("Device reports that it needs firmware update.");
  263. }
  264. die("Device reports that it's not present.");
  265. }
  266. if (data->device.num_video_inputs > ARRAY_SIZE(data->inputs)) {
  267. die("The device supports %d inputs. Recompile the program with more MAX_INPUTS (currently %d)",
  268. data->device.num_video_inputs, MAX_INPUTS);
  269. }
  270. if (data->device.num_video_outputs > ARRAY_SIZE(data->outputs)) {
  271. die("The device supports %d outputs. Recompile the program with more MAX_OUTPUTS (currently %d)\n",
  272. data->device.num_video_outputs, MAX_OUTPUTS);
  273. }
  274. default:
  275. break;
  276. }
  277. return true;
  278. }
  279. int parse_text_buffer(struct videohub_data *data, char *cmd_buffer) {
  280. // The buffer contains only one command, no splitting is needed
  281. if (!strstr(cmd_buffer, "\n\n"))
  282. return parse_command(data, cmd_buffer);
  283. // Split commands and parse them one by one
  284. int ok_commands = 0;
  285. char *bcopy = xstrdup(cmd_buffer);
  286. char *newcmd, *cmd = bcopy;
  287. while(1) {
  288. newcmd = strstr(cmd, "\n\n"); // Find next command
  289. if (!newcmd) {
  290. if (parse_command(data, cmd)) // Parse current command
  291. ok_commands++;
  292. break;
  293. }
  294. newcmd[0] = '\0'; // Terminate previous command
  295. if (parse_command(data, cmd)) // Parse previous command
  296. ok_commands++;
  297. cmd = newcmd + 2; // Advance cmd to the next command
  298. }
  299. free(bcopy);
  300. return ok_commands;
  301. }