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.8KB

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