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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. struct videohub_commands *v = NULL;
  131. for (i = 0; i < ARRAY_SIZE(videohub_commands); i++) {
  132. if (!videohub_commands[i].txt)
  133. continue;
  134. if (strstr(cmd, videohub_commands[i].txt) == cmd) {
  135. v = &videohub_commands[i];
  136. break;
  137. }
  138. }
  139. if (!v) {
  140. if (!quiet) {
  141. fprintf(stderr, "WARNING: Videohub sent unknown command!\n");
  142. fprintf(stderr, " Please report this command to author's email: georgi@unixsol.org\n");
  143. fprintf(stderr, " You may use -q or --quiet to suppress the message.\n");
  144. fprintf(stderr, "---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  145. fprintf(stderr, "%s\n", cmd);
  146. fprintf(stderr, "---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  147. }
  148. return false;
  149. }
  150. v("verbose: Got '%s' command.\n", v->txt);
  151. bool cmd_response = false;
  152. char *p, *cmd_data = xstrdup( cmd + strlen(v->txt) + 2 ); // +2 to compensate for :\n at the end of the command
  153. // Split line by line
  154. char *line, *saveptr = NULL;
  155. for(i = 0, line = strtok_r(cmd_data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr), i++) {
  156. if (i == 0) { // Handle command response
  157. if (streq(line, "NAK"))
  158. return false;
  159. if (streq(line, "ACK")) {
  160. cmd_response = true;
  161. continue;
  162. }
  163. }
  164. if (i == 1 && cmd_response) { // The command must match
  165. if (strstr(line, v->txt) != line)
  166. return false;
  167. continue;
  168. }
  169. // Parse command data response looking like that: "[slot_pos] [slot_data]"
  170. bool valid_slot = false;
  171. unsigned int slot_pos = 0;
  172. char *slot_data = NULL;
  173. switch (v->cmd) {
  174. case CMD_INPUT_LABELS:
  175. case CMD_OUTPUT_LABELS:
  176. case CMD_VIDEO_OUTPUT_LOCKS:
  177. case CMD_VIDEO_OUTPUT_ROUTING:
  178. slot_data = strchr(line, ' ');
  179. if (slot_data) {
  180. slot_data[0] = '\0'; // Separate slot_pos from slot_data
  181. slot_data++;
  182. slot_pos = strtoul(line, NULL, 10);
  183. if (slot_pos < ARRAY_SIZE(data->inputs))
  184. valid_slot = true;
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. // Parse commands
  191. switch (v->cmd) {
  192. case CMD_PROTOCOL_PREAMBLE:
  193. if ((p = parse_text(line, "Version: ")))
  194. snprintf(data->device.protocol_ver, sizeof(data->device.protocol_ver), "%s", p);
  195. break;
  196. case CMD_VIDEOHUB_DEVICE:
  197. if ((p = parse_text(line, "Device present: ")))
  198. data->device.dev_present = streq(p, "true");
  199. if ((p = parse_text(line, "Model name: ")))
  200. snprintf(data->device.model_name, sizeof(data->device.model_name), "%s", p);
  201. if ((p = parse_text(line, "Unique ID: ")))
  202. snprintf(data->device.unique_id, sizeof(data->device.unique_id) , "%s", p);
  203. if ((p = parse_text(line, "Video inputs: ")))
  204. data->device.num_video_inputs = strtoul(p, NULL, 10);
  205. if ((p = parse_text(line, "Video processing units: ")))
  206. data->device.num_video_processing_units = strtoul(p, NULL, 10);
  207. if ((p = parse_text(line, "Video outputs: ")))
  208. data->device.num_video_outputs = strtoul(p, NULL, 10);
  209. if ((p = parse_text(line, "Video monitoring output: ")))
  210. data->device.num_video_monitoring_outputs = strtoul(p, NULL, 10);
  211. if ((p = parse_text(line, "Serial ports: ")))
  212. data->device.num_serial_ports = strtoul(p, NULL, 10);
  213. break;
  214. case CMD_INPUT_LABELS:
  215. if (valid_slot)
  216. snprintf(data->inputs[slot_pos].name, sizeof(data->inputs[slot_pos].name), "%s", slot_data);
  217. break;
  218. case CMD_OUTPUT_LABELS:
  219. if (valid_slot)
  220. snprintf(data->outputs[slot_pos].name, sizeof(data->inputs[slot_pos].name), "%s", slot_data);
  221. break;
  222. case CMD_VIDEO_OUTPUT_LOCKS:
  223. if (valid_slot)
  224. data->inputs[slot_pos].locked = slot_data[0] == 'L' ? true : false;
  225. break;
  226. case CMD_VIDEO_OUTPUT_ROUTING:
  227. if (valid_slot) {
  228. unsigned int dest_pos = strtoul(slot_data, NULL, 10);
  229. if (dest_pos < ARRAY_SIZE(data->outputs))
  230. data->inputs[slot_pos].routed_to = dest_pos;
  231. }
  232. break;
  233. case CMD_PING:
  234. // Do nothing, we just get ACK without any data
  235. break;
  236. }
  237. }
  238. free(cmd_data);
  239. /* Check if everything is within limits */
  240. switch (v->cmd) {
  241. case CMD_PROTOCOL_PREAMBLE:
  242. if (!streq(data->device.protocol_ver, "2.4")) {
  243. if (!quiet) {
  244. fprintf(stderr, "WARNING: Device protocol is %s but this program is tested with 2.4 only.\n",
  245. data->device.protocol_ver);
  246. fprintf(stderr, " Please report successes/failures to author's email: georgi@unixsol.org\n");
  247. fprintf(stderr, " You may use -q or --quiet to suppress the message.\n");
  248. }
  249. }
  250. break;
  251. case CMD_VIDEOHUB_DEVICE:
  252. if (!data->device.dev_present) {
  253. die("Device reports that it's not present.");
  254. }
  255. if (data->device.num_video_inputs > ARRAY_SIZE(data->inputs)) {
  256. die("The device supports %d inputs. Recompile the program with more MAX_INPUTS (currently %d)",
  257. data->device.num_video_inputs, MAX_INPUTS);
  258. }
  259. if (data->device.num_video_outputs > ARRAY_SIZE(data->outputs)) {
  260. die("The device supports %d outputs. Recompile the program with more MAX_OUTPUTS (currently %d)\n",
  261. data->device.num_video_outputs, MAX_OUTPUTS);
  262. }
  263. default:
  264. break;
  265. }
  266. return true;
  267. }