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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. static struct videohub_commands {
  102. enum vcmd cmd;
  103. const char *txt;
  104. } videohub_commands[] = {
  105. { CMD_PROTOCOL_PREAMBLE, "PROTOCOL PREAMBLE" },
  106. { CMD_VIDEOHUB_DEVICE, "VIDEOHUB DEVICE" },
  107. { CMD_INPUT_LABELS, "INPUT LABELS" },
  108. { CMD_OUTPUT_LABELS, "OUTPUT LABELS" },
  109. { CMD_VIDEO_OUTPUT_LOCKS, "VIDEO OUTPUT LOCKS" },
  110. { CMD_VIDEO_OUTPUT_ROUTING, "VIDEO OUTPUT ROUTING" },
  111. { CMD_PING, "PING" },
  112. { CMD_ACK, "ACK" },
  113. { CMD_NAK, "NAK" },
  114. };
  115. static const char *get_cmd_text(enum vcmd cmd) {
  116. unsigned int i;
  117. for (i = 0; i < ARRAY_SIZE(videohub_commands); i++) {
  118. if (videohub_commands[i].cmd == cmd)
  119. return videohub_commands[i].txt;
  120. }
  121. return "";
  122. }
  123. static char *parse_text(char *line, char *cmd) {
  124. char *parsed_text = strstr(line, cmd);
  125. if (parsed_text == line) {
  126. return parsed_text + strlen(cmd);
  127. }
  128. return NULL;
  129. }
  130. bool parse_command(struct videohub_data *data, char *cmd) {
  131. unsigned int i;
  132. bool ret = true;
  133. if (!strlen(cmd))
  134. return false;
  135. struct videohub_commands *v = NULL;
  136. for (i = 0; i < ARRAY_SIZE(videohub_commands); i++) {
  137. if (!videohub_commands[i].txt)
  138. continue;
  139. if (strstr(cmd, videohub_commands[i].txt) == cmd) {
  140. v = &videohub_commands[i];
  141. break;
  142. }
  143. }
  144. if (!v) {
  145. if (!quiet) {
  146. fprintf(stderr, "WARNING: Videohub sent unknown command!\n");
  147. fprintf(stderr, " Please report this command to author's email: georgi@unixsol.org\n");
  148. fprintf(stderr, " You may use -q or --quiet to suppress the message.\n");
  149. fprintf(stderr, "---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  150. fprintf(stderr, "%s\n", cmd);
  151. fprintf(stderr, "---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  152. }
  153. return false;
  154. }
  155. d("debug: Got '%s' command.\n", v->txt);
  156. if (debug > 1)
  157. d("----\n%s\n----\n", cmd);
  158. char *p, *cmd_data = xstrdup( cmd + strlen(v->txt) + 2 ); // +2 to compensate for :\n at the end of the command
  159. // Split line by line
  160. char *line, *saveptr = NULL;
  161. for(i = 0, line = strtok_r(cmd_data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr), i++) {
  162. // Parse command data response looking like that: "[slot_pos] [slot_data]"
  163. bool valid_slot = false;
  164. unsigned int slot_pos = 0;
  165. char *slot_data = NULL;
  166. switch (v->cmd) {
  167. case CMD_INPUT_LABELS:
  168. case CMD_OUTPUT_LABELS:
  169. case CMD_VIDEO_OUTPUT_LOCKS:
  170. case CMD_VIDEO_OUTPUT_ROUTING:
  171. slot_data = strchr(line, ' ');
  172. if (slot_data) {
  173. slot_data[0] = '\0'; // Separate slot_pos from slot_data
  174. slot_data++;
  175. slot_pos = strtoul(line, NULL, 10);
  176. if (slot_pos < ARRAY_SIZE(data->inputs))
  177. valid_slot = true;
  178. }
  179. break;
  180. default:
  181. break;
  182. }
  183. // Parse commands
  184. switch (v->cmd) {
  185. case CMD_PROTOCOL_PREAMBLE:
  186. if ((p = parse_text(line, "Version: ")))
  187. snprintf(data->device.protocol_ver, sizeof(data->device.protocol_ver), "%s", p);
  188. break;
  189. case CMD_VIDEOHUB_DEVICE:
  190. if ((p = parse_text(line, "Device present: "))) {
  191. data->device.dev_present = streq(p, "true");
  192. data->device.needs_fw_update = streq(p, "needs_update");
  193. }
  194. if ((p = parse_text(line, "Model name: ")))
  195. snprintf(data->device.model_name, sizeof(data->device.model_name), "%s", p);
  196. if ((p = parse_text(line, "Unique ID: ")))
  197. snprintf(data->device.unique_id, sizeof(data->device.unique_id) , "%s", p);
  198. if ((p = parse_text(line, "Video inputs: ")))
  199. data->device.num_video_inputs = strtoul(p, NULL, 10);
  200. if ((p = parse_text(line, "Video processing units: ")))
  201. data->device.num_video_processing_units = strtoul(p, NULL, 10);
  202. if ((p = parse_text(line, "Video outputs: ")))
  203. data->device.num_video_outputs = strtoul(p, NULL, 10);
  204. if ((p = parse_text(line, "Video monitoring output: ")))
  205. data->device.num_video_monitoring_outputs = strtoul(p, NULL, 10);
  206. if ((p = parse_text(line, "Serial ports: ")))
  207. data->device.num_serial_ports = strtoul(p, NULL, 10);
  208. break;
  209. case CMD_INPUT_LABELS:
  210. if (valid_slot)
  211. snprintf(data->inputs[slot_pos].name, sizeof(data->inputs[slot_pos].name), "%s", slot_data);
  212. break;
  213. case CMD_OUTPUT_LABELS:
  214. if (valid_slot)
  215. snprintf(data->outputs[slot_pos].name, sizeof(data->inputs[slot_pos].name), "%s", slot_data);
  216. break;
  217. case CMD_VIDEO_OUTPUT_LOCKS:
  218. if (valid_slot) {
  219. // L is lock owned by somebody else (set from other IP address)
  220. // O is lock owned by us (set from our IP address)
  221. if (slot_data[0] == 'L' || slot_data[0] == 'O') {
  222. data->outputs[slot_pos].locked = true;
  223. if (slot_data[0] == 'L')
  224. data->outputs[slot_pos].locked_other = true;
  225. else
  226. data->outputs[slot_pos].locked_other = false;
  227. } else {
  228. data->outputs[slot_pos].locked = false;
  229. }
  230. }
  231. break;
  232. case CMD_VIDEO_OUTPUT_ROUTING:
  233. if (valid_slot) {
  234. unsigned int dest_pos = strtoul(slot_data, NULL, 10);
  235. if (dest_pos < ARRAY_SIZE(data->inputs))
  236. data->outputs[slot_pos].routed_to = dest_pos;
  237. }
  238. break;
  239. case CMD_PING:
  240. case CMD_ACK:
  241. // Do nothing
  242. break;
  243. case CMD_NAK:
  244. ret = false;
  245. break;
  246. }
  247. }
  248. free(cmd_data);
  249. return ret;
  250. }
  251. int parse_text_buffer(struct videohub_data *data, char *cmd_buffer) {
  252. // The buffer contains only one command, no splitting is needed
  253. if (!strstr(cmd_buffer, "\n\n"))
  254. return parse_command(data, cmd_buffer);
  255. // Split commands and parse them one by one
  256. int ok_commands = 0;
  257. char *bcopy = xstrdup(cmd_buffer);
  258. char *newcmd, *cmd = bcopy;
  259. while(1) {
  260. newcmd = strstr(cmd, "\n\n"); // Find next command
  261. if (!newcmd) {
  262. if (parse_command(data, cmd)) // Parse current command
  263. ok_commands++;
  264. break;
  265. }
  266. newcmd[0] = '\0'; // Terminate previous command
  267. if (parse_command(data, cmd)) // Parse previous command
  268. ok_commands++;
  269. cmd = newcmd + 2; // Advance cmd to the next command
  270. }
  271. free(bcopy);
  272. return ok_commands;
  273. }