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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 <stddef.h>
  15. #include <string.h>
  16. #include "data.h"
  17. #include "cmd.h"
  18. #include "util.h"
  19. const char *videohub_commands_text[NUM_COMMANDS] = {
  20. [CMD_PROTOCOL_PREAMBLE] = "PROTOCOL PREAMBLE",
  21. [CMD_VIDEOHUB_DEVICE] = "VIDEOHUB DEVICE",
  22. [CMD_CONFIGURATION] = "CONFIGURATION",
  23. [CMD_END_PRELUDE] = "END PRELUDE",
  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_MONITORING_OUTPUT_LABELS] = "MONITORING OUTPUT LABELS",
  31. [CMD_MONITORING_OUTPUT_LOCKS] = "MONITORING OUTPUT LOCKS",
  32. [CMD_MONITORING_OUTPUT_ROUTING] = "VIDEO MONITORING OUTPUT ROUTING",
  33. [CMD_SERIAL_PORT_LABELS] = "SERIAL PORT LABELS",
  34. [CMD_SERIAL_PORT_ROUTING] = "SERIAL PORT ROUTING",
  35. [CMD_SERIAL_PORT_LOCKS] = "SERIAL PORT LOCKS",
  36. [CMD_SERIAL_PORT_STATUS] = "SERIAL PORT STATUS",
  37. [CMD_SERIAL_PORT_DIRECTIONS] = "SERIAL PORT DIRECTIONS",
  38. [CMD_PROCESSING_UNIT_ROUTING]= "PROCESSING UNIT ROUTING",
  39. [CMD_PROCESSING_UNIT_LOCKS] = "PROCESSING UNIT LOCKS",
  40. [CMD_FRAME_LABELS] = "FRAME LABELS",
  41. [CMD_FRAME_BUFFER_ROUTING] = "FRAME BUFFER ROUTING",
  42. [CMD_FRAME_BUFFER_LOCKS] = "FRAME BUFFER LOCKS",
  43. [CMD_ALARM_STATUS] = "ALARM STATUS",
  44. [CMD_PING] = "PING",
  45. [CMD_ACK] = "ACK",
  46. [CMD_NAK] = "NAK",
  47. };
  48. #define OFS(X) offsetof(struct videohub_data, X)
  49. struct videohub_commands videohub_commands[NUM_COMMANDS] = {
  50. [CMD_PROTOCOL_PREAMBLE] = { .cmd = CMD_PROTOCOL_PREAMBLE , .type = PARSE_CUSTOM },
  51. [CMD_VIDEOHUB_DEVICE] = { .cmd = CMD_VIDEOHUB_DEVICE , .type = PARSE_CUSTOM },
  52. [CMD_CONFIGURATION] = { .cmd = CMD_CONFIGURATION , .type = PARSE_CUSTOM },
  53. [CMD_INPUT_LABELS] = { .cmd = CMD_INPUT_LABELS , .type = PARSE_LABEL,
  54. .ports1 = OFS(inputs),
  55. .port_id1 = "video input",
  56. .opt_prefix = "in",
  57. },
  58. [CMD_OUTPUT_LABELS] = { .cmd = CMD_OUTPUT_LABELS , .type = PARSE_LABEL,
  59. .ports1 = OFS(outputs),
  60. .port_id1 = "video output",
  61. .opt_prefix = "out",
  62. },
  63. [CMD_VIDEO_OUTPUT_LOCKS] = { .cmd = CMD_VIDEO_OUTPUT_LOCKS , .type = PARSE_LOCK,
  64. .ports1 = OFS(outputs),
  65. .port_id1 = "video output",
  66. .opt_prefix = "out",
  67. },
  68. [CMD_VIDEO_OUTPUT_ROUTING] = { .cmd = CMD_VIDEO_OUTPUT_ROUTING, .type = PARSE_ROUTE,
  69. .ports1 = OFS(outputs),
  70. .ports2 = OFS(inputs),
  71. .port_id1 = "video output",
  72. .port_id2 = "video input",
  73. .opt_prefix = "out",
  74. },
  75. [CMD_VIDEO_INPUT_STATUS] = { .cmd = CMD_VIDEO_INPUT_STATUS , .type = PARSE_STATUS,
  76. .ports1 = OFS(inputs),
  77. .port_id1 = "video input",
  78. .opt_prefix = "in",
  79. },
  80. [CMD_VIDEO_OUTPUT_STATUS] = { .cmd = CMD_VIDEO_OUTPUT_STATUS , .type = PARSE_STATUS,
  81. .ports1 = OFS(outputs),
  82. .port_id1 = "video output",
  83. .opt_prefix = "out",
  84. },
  85. [CMD_MONITORING_OUTPUT_LABELS] = { .cmd = CMD_MONITORING_OUTPUT_LABELS , .type = PARSE_LABEL,
  86. .ports1 = OFS(mon_outputs),
  87. .port_id1 = "monitoring output",
  88. .opt_prefix = "mon",
  89. },
  90. [CMD_MONITORING_OUTPUT_LOCKS] = { .cmd = CMD_MONITORING_OUTPUT_LOCKS , .type = PARSE_LOCK,
  91. .ports1 = OFS(mon_outputs),
  92. .port_id1 = "monitoring output",
  93. .opt_prefix = "mon",
  94. },
  95. [CMD_MONITORING_OUTPUT_ROUTING] = { .cmd = CMD_MONITORING_OUTPUT_ROUTING, .type = PARSE_ROUTE,
  96. .ports1 = OFS(mon_outputs),
  97. .ports2 = OFS(inputs),
  98. .port_id1 = "monitoring output",
  99. .port_id2 = "video input",
  100. .opt_prefix = "mon",
  101. },
  102. [CMD_SERIAL_PORT_LABELS] = { .cmd = CMD_SERIAL_PORT_LABELS, .type = PARSE_LABEL,
  103. .ports1 = OFS(serial),
  104. .port_id1 = "serial",
  105. .opt_prefix = "ser",
  106. },
  107. [CMD_SERIAL_PORT_LOCKS] = { .cmd = CMD_SERIAL_PORT_LOCKS , .type = PARSE_LOCK,
  108. .ports1 = OFS(serial),
  109. .port_id1 = "serial",
  110. .opt_prefix = "ser",
  111. },
  112. [CMD_SERIAL_PORT_ROUTING] = { .cmd = CMD_SERIAL_PORT_ROUTING, .type = PARSE_ROUTE,
  113. .ports1 = OFS(serial),
  114. .ports2 = OFS(serial),
  115. .port_id1 = "serial",
  116. .port_id2 = "serial",
  117. .opt_prefix = "ser",
  118. .allow_disconnect = true,
  119. },
  120. [CMD_SERIAL_PORT_STATUS] = { .cmd = CMD_SERIAL_PORT_STATUS , .type = PARSE_STATUS,
  121. .ports1 = OFS(serial),
  122. .port_id1 = "serial",
  123. .opt_prefix = "ser",
  124. },
  125. [CMD_SERIAL_PORT_DIRECTIONS] = { .cmd = CMD_SERIAL_PORT_DIRECTIONS, .type = PARSE_DIR,
  126. .ports1 = OFS(serial),
  127. .port_id1 = "serial",
  128. .opt_prefix = "ser",
  129. },
  130. [CMD_PROCESSING_UNIT_ROUTING] = { .cmd = CMD_PROCESSING_UNIT_ROUTING, .type = PARSE_ROUTE,
  131. .ports1 = OFS(proc_units),
  132. .ports2 = OFS(inputs),
  133. .port_id1 = "proc unit",
  134. .port_id2 = "input",
  135. .opt_prefix = "pu",
  136. .allow_disconnect = true,
  137. },
  138. [CMD_PROCESSING_UNIT_LOCKS] = { .cmd = CMD_PROCESSING_UNIT_LOCKS, .type = PARSE_LOCK,
  139. .ports1 = OFS(proc_units),
  140. .port_id1 = "proc unit",
  141. .opt_prefix = "pu",
  142. },
  143. [CMD_FRAME_LABELS] = { .cmd = CMD_FRAME_LABELS, .type = PARSE_LABEL,
  144. .ports1 = OFS(frames),
  145. .port_id1 = "frame",
  146. .opt_prefix = "fr",
  147. },
  148. [CMD_FRAME_BUFFER_ROUTING] = { .cmd = CMD_FRAME_BUFFER_ROUTING, .type = PARSE_ROUTE,
  149. .ports1 = OFS(frames),
  150. .ports2 = OFS(outputs),
  151. .port_id1 = "frame",
  152. .port_id2 = "output",
  153. .opt_prefix = "fr",
  154. .allow_disconnect = true,
  155. },
  156. [CMD_FRAME_BUFFER_LOCKS] = { .cmd = CMD_FRAME_BUFFER_LOCKS, .type = PARSE_LOCK,
  157. .ports1 = OFS(frames),
  158. .port_id1 = "frame",
  159. .opt_prefix = "fr",
  160. },
  161. [CMD_ALARM_STATUS] = { .cmd = CMD_ALARM_STATUS , .type = PARSE_CUSTOM },
  162. [CMD_END_PRELUDE] = { .cmd = CMD_END_PRELUDE , .type = PARSE_NONE },
  163. [CMD_PING] = { .cmd = CMD_PING , .type = PARSE_NONE },
  164. [CMD_ACK] = { .cmd = CMD_ACK , .type = PARSE_NONE },
  165. [CMD_NAK] = { .cmd = CMD_NAK , .type = PARSE_NONE },
  166. };
  167. static char *parse_text(char *line, char *cmd) {
  168. char *parsed_text = strstr(line, cmd);
  169. if (parsed_text == line) {
  170. return parsed_text + strlen(cmd);
  171. }
  172. return NULL;
  173. }
  174. bool parse_command(struct videohub_data *d, char *cmd) {
  175. unsigned int i;
  176. bool ret = true;
  177. if (!strlen(cmd))
  178. return false;
  179. struct videohub_commands *v = NULL;
  180. const char *cmd_txt = NULL;
  181. for (i = 0; i < NUM_COMMANDS; i++) {
  182. const char *cmd_text = videohub_commands_text[i];
  183. if (strstr(cmd, cmd_text) == cmd) {
  184. v = &videohub_commands[i];
  185. cmd_txt = videohub_commands_text[i];
  186. break;
  187. }
  188. }
  189. if (!v) {
  190. if (cmd[0] == '\n') // Do not report 'empty' commands
  191. return false;
  192. q("WARNING: Videohub sent unknown command!\n");
  193. q(" Please report this command to author's email: georgi@unixsol.org\n");
  194. q(" You may use -q or --quiet to suppress the message.\n");
  195. q("---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  196. q("%s\n", cmd);
  197. q("---------8<-----------8<----------- cut here ---------8<------------8<---------\n");
  198. return false;
  199. }
  200. d("debug: Got '%s' command.\n", cmd_txt);
  201. if (debug > 1)
  202. d("----\n%s\n----\n", cmd);
  203. struct port_set *s_port = !v->ports1 ? NULL : (void *)d + v->ports1;
  204. struct port_set *d_port = !v->ports2 ? NULL : (void *)d + v->ports2;
  205. char *p, *cmd_data = xstrdup( cmd + strlen(cmd_txt) + 2 ); // +2 to compensate for :\n at the end of the command
  206. // Split line by line
  207. char *line, *saveptr = NULL;
  208. for(i = 0, line = strtok_r(cmd_data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr), i++) {
  209. // Parse command data response
  210. unsigned int port_num = 0;
  211. unsigned int dest_port_num = 0;
  212. char *port_data = NULL;
  213. if (v->type != PARSE_NONE && v->type != PARSE_CUSTOM) {
  214. port_data = strchr(line, ' ');
  215. if (!port_data)
  216. continue;
  217. port_data[0] = '\0'; // Separate port_num from port_data
  218. port_data++;
  219. port_num = strtoul(line, NULL, 10);
  220. if (port_num + 1 > s_port->num) {
  221. q("WARNING: %s: invalid %s port %u (valid 0..%u)\n", cmd_txt,
  222. v->port_id1, port_num, s_port->num - 1);
  223. continue;
  224. }
  225. }
  226. switch (v->type) {
  227. case PARSE_LABEL:
  228. snprintf(s_port->port[port_num].name, sizeof(s_port->port[port_num].name), "%s", port_data);
  229. break;
  230. case PARSE_STATUS:
  231. s_port->port[port_num].status = S_UNKNOWN;
  232. bool invalid_status = false;
  233. if (v->cmd == CMD_SERIAL_PORT_STATUS) {
  234. if (streq("RS422", port_data)) s_port->port[port_num].status = S_RS422;
  235. else if (streq("None", port_data)) s_port->port[port_num].status = S_NONE;
  236. else invalid_status = true;
  237. } else {
  238. if (streq("BNC", port_data)) s_port->port[port_num].status = S_BNC;
  239. else if (streq("Optical", port_data)) s_port->port[port_num].status = S_OPTICAL;
  240. else if (streq("None", port_data)) s_port->port[port_num].status = S_NONE;
  241. else if (streq("Thunderbolt", port_data)) s_port->port[port_num].status = S_THUNDERBOLT;
  242. else invalid_status = true;
  243. }
  244. if (invalid_status) {
  245. q("WARNING: %s command returned unknown status: '%s'\n", cmd_txt, port_data);
  246. q("Please report this line to author's email: georgi@unixsol.org\n");
  247. }
  248. break;
  249. case PARSE_DIR:
  250. s_port->port[port_num].direction = DIR_AUTO;
  251. if (streq("control", port_data)) s_port->port[port_num].direction = DIR_CONTROL;
  252. else if (streq("slave", port_data)) s_port->port[port_num].direction = DIR_SLAVE;
  253. else if (streq("auto", port_data)) s_port->port[port_num].direction = DIR_AUTO;
  254. else {
  255. q("WARNING: %s command returned unknown direction: '%s'\n", cmd_txt, port_data);
  256. q("Please report this line to author's email: georgi@unixsol.org\n");
  257. }
  258. break;
  259. case PARSE_ROUTE:
  260. dest_port_num = strtoul(port_data, NULL, 10);
  261. if (dest_port_num == NO_PORT) {
  262. if (v->allow_disconnect) {
  263. s_port->port[port_num].routed_to = dest_port_num;
  264. continue;
  265. } else {
  266. dest_port_num = port_num;
  267. }
  268. }
  269. if (dest_port_num + 1 > d_port->num) {
  270. q("WARNING: %s: invalid %s port %u (valid 0..%u)\n", cmd_txt,
  271. v->port_id2, dest_port_num, d_port->num - 1);
  272. continue;
  273. }
  274. s_port->port[port_num].routed_to = dest_port_num;
  275. break;
  276. case PARSE_LOCK:
  277. switch (port_data[0]) {
  278. case 'O': s_port->port[port_num].lock = PORT_LOCKED; break;
  279. case 'L': s_port->port[port_num].lock = PORT_LOCKED_OTHER; break;
  280. default : s_port->port[port_num].lock = PORT_UNLOCKED; break;
  281. }
  282. break;
  283. default: break;
  284. }
  285. // Parse custom commands
  286. switch (v->cmd) {
  287. case CMD_PROTOCOL_PREAMBLE:
  288. if ((p = parse_text(line, "Version: ")))
  289. snprintf(d->device.protocol_ver, sizeof(d->device.protocol_ver), "%s", p);
  290. break;
  291. case CMD_VIDEOHUB_DEVICE:
  292. if ((p = parse_text(line, "Device present: "))) {
  293. d->device.dev_present = streq(p, "true");
  294. d->device.needs_fw_update = streq(p, "needs_update");
  295. }
  296. else if ((p = parse_text(line, "Model name: ")))
  297. snprintf(d->device.model_name, sizeof(d->device.model_name), "%s", p);
  298. else if ((p = parse_text(line, "Friendly name: ")))
  299. snprintf(d->device.friendly_name, sizeof(d->device.friendly_name), "%s", p);
  300. else if ((p = parse_text(line, "Unique ID: ")))
  301. snprintf(d->device.unique_id, sizeof(d->device.unique_id) , "%s", p);
  302. else if ((p = parse_text(line, "Video inputs: ")))
  303. d->inputs.num = strtoul(p, NULL, 10);
  304. else if ((p = parse_text(line, "Video processing units: ")))
  305. d->proc_units.num = strtoul(p, NULL, 10);
  306. else if ((p = parse_text(line, "Video outputs: ")))
  307. d->outputs.num = strtoul(p, NULL, 10);
  308. else if ((p = parse_text(line, "Video monitoring outputs: ")))
  309. d->mon_outputs.num = strtoul(p, NULL, 10);
  310. else if ((p = parse_text(line, "Serial ports: "))) {
  311. d->serial.num = strtoul(p, NULL, 10);
  312. d->frames.num = d->serial.num;
  313. } else {
  314. q("WARNING: VIDEOHUB DEVICE command sent unknown line: '%s'\n", line);
  315. q("Please report this line to author's email: georgi@unixsol.org\n");
  316. }
  317. break;
  318. case CMD_CONFIGURATION:
  319. if ((p = parse_text(line, "Take Mode: "))) {
  320. d->device.conf_take_mode = streq(p, "true");
  321. }
  322. break;
  323. case CMD_ALARM_STATUS:
  324. p = strchr(line, ':');
  325. if (p) {
  326. char *alarm_name = line; // Use start of the line
  327. *p = '\0'; // Terminate the alarm name at :
  328. char *alarm_status = p + 2; // Set alarm value to point after ': '
  329. if (d->alarms.num + 1 < MAX_ALARMS) {
  330. snprintf(d->alarms.alarm[ d->alarms.num ].name , MAX_NAME_LEN, "%s", alarm_name);
  331. snprintf(d->alarms.alarm[ d->alarms.num ].status, MAX_ALARM_STATUS_LEN, "%s", alarm_status);
  332. d->alarms.num++;
  333. } else {
  334. q("WARNING: Too many alarms (> %d) increase MAX_ALARMS and recompile. Alarm '%s' value '%s' ignorred\n", d->alarms.num + 1, alarm_name, alarm_status);
  335. }
  336. }
  337. break;
  338. case CMD_NAK:
  339. ret = false;
  340. break;
  341. default: break;
  342. }
  343. }
  344. free(cmd_data);
  345. return ret;
  346. }
  347. int parse_text_buffer(struct videohub_data *d, char *_cmd_buffer) {
  348. int r, cmd_buffer_pos = 0, cmd_len = strlen(_cmd_buffer);
  349. char *cmd_buffer = xzalloc(cmd_len + 1);
  350. // Clean input
  351. char last_char = '\0';
  352. for (r = 0; r < cmd_len; r++) {
  353. if (_cmd_buffer[r] == '\r') continue; // Remove windows style line endings
  354. if (last_char == '\n' && _cmd_buffer[r] == ' ') continue; // Trim leading white space from each line
  355. cmd_buffer[cmd_buffer_pos++] = _cmd_buffer[r];
  356. last_char = _cmd_buffer[r];
  357. }
  358. // The buffer contains only one command, no splitting is needed
  359. if (!strstr(cmd_buffer, "\n\n")) {
  360. bool ret = parse_command(d, cmd_buffer);
  361. free(cmd_buffer);
  362. return ret;
  363. }
  364. // Split commands and parse them one by one
  365. int ok_commands = 0;
  366. char *buf_copy = xstrdup(cmd_buffer);
  367. char *newcmd, *cmd = buf_copy;
  368. while(1) {
  369. newcmd = strstr(cmd, "\n\n"); // Find next command
  370. if (!newcmd) {
  371. if (parse_command(d, cmd)) // Parse current command
  372. ok_commands++;
  373. break;
  374. }
  375. newcmd[0] = '\0'; // Terminate previous command
  376. if (parse_command(d, cmd)) // Parse previous command
  377. ok_commands++;
  378. cmd = newcmd + 2; // Advance cmd to the next command
  379. }
  380. free(buf_copy);
  381. free(cmd_buffer);
  382. return ok_commands;
  383. }
  384. // Try to find port with certain name, return 0 on not found, pos + 1 is found
  385. static int get_port_by_name(struct port_set *p, char *name) {
  386. unsigned int i;
  387. for(i = 0; i < p->num; i++) {
  388. if (streq(name, p->port[i].name)) {
  389. return i + 1;
  390. }
  391. }
  392. return 0;
  393. }
  394. // Return 0 on error, number otherwise if it can parse the whole input
  395. static unsigned int my_atoi(char *txt) {
  396. char *endptr = NULL;
  397. if (!txt)
  398. return 0;
  399. unsigned int ret = strtoul(txt, &endptr, 10);
  400. if (endptr == txt || *endptr)
  401. return 0;
  402. return ret;
  403. }
  404. static void init_port_number(struct vcmd_param *p, struct port_set *port, const char *port_id) {
  405. p->port_no = my_atoi(p->param);
  406. if (!port) {
  407. die("impossible! port == NULL");
  408. return;
  409. }
  410. if (p->port_no == 0 || p->port_no > port->num) {
  411. p->port_no = get_port_by_name(port, p->param);
  412. if (!p->port_no)
  413. die("Unknown %s port number/name: %s", port_id, p->param);
  414. }
  415. }
  416. void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
  417. struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
  418. struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
  419. if (e->cmd->type == PARSE_CUSTOM)
  420. return;
  421. // All command types needs parsing of the "source port"
  422. init_port_number(&e->p1, s_port, e->cmd->port_id1);
  423. // ROUTE type needs parsing of the "destination port"
  424. if (e->cmd->type == PARSE_ROUTE) {
  425. init_port_number(&e->p2, d_port, e->cmd->port_id2);
  426. }
  427. // Allow port_noX to be used as index into ->port[]
  428. e->p1.port_no -= 1;
  429. e->p2.port_no -= 1;
  430. if (e->clear_port)
  431. e->p2.port_no = NO_PORT;
  432. if (e->cmd->type == PARSE_LOCK) {
  433. e->lock = s_port->port[e->p1.port_no].lock;
  434. }
  435. }
  436. static char *dir2cmd(enum serial_dir dir) {
  437. switch (dir) {
  438. case DIR_CONTROL: return "control";
  439. case DIR_SLAVE : return "slave";
  440. case DIR_AUTO : return "auto";
  441. }
  442. return "auto";
  443. }
  444. static char *dir2txt(enum serial_dir dir) {
  445. switch (dir) {
  446. case DIR_CONTROL: return "IN (Workstation)";
  447. case DIR_SLAVE : return "OUT (Deck)";
  448. case DIR_AUTO : return "AUTO";
  449. }
  450. return "AUTO";
  451. }
  452. void format_cmd_text(struct vcmd_entry *e, char *buf, unsigned int bufsz) {
  453. if (e->cmd->cmd == CMD_VIDEOHUB_DEVICE) {
  454. snprintf(buf, bufsz, "%s:\n%s: %s\n\n", videohub_commands_text[e->cmd->cmd],
  455. e->p1.param, e->p2.param);
  456. return;
  457. }
  458. switch (e->cmd->type) {
  459. case PARSE_LABEL:
  460. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  461. e->p1.port_no, e->p2.param);
  462. break;
  463. case PARSE_LOCK:
  464. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  465. e->p1.port_no, e->do_lock ? "O" : (e->lock == PORT_LOCKED_OTHER ? "F" : "U"));
  466. break;
  467. case PARSE_ROUTE:
  468. snprintf(buf, bufsz, "%s:\n%u %d\n\n", videohub_commands_text[e->cmd->cmd],
  469. e->p1.port_no, (e->p2.port_no == NO_PORT ? -1 : (int)e->p2.port_no));
  470. break;
  471. case PARSE_DIR:
  472. snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
  473. e->p1.port_no, dir2cmd(e->direction));
  474. break;
  475. default: break;
  476. }
  477. }
  478. void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
  479. struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
  480. struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
  481. const char *prefix = "videohub: ";
  482. if (e->cmd->cmd == CMD_VIDEOHUB_DEVICE) {
  483. printf("%sset device \"%s\" to \"%s\"\n",
  484. prefix,
  485. e->p1.param,
  486. e->p2.param
  487. );
  488. return;
  489. }
  490. switch (e->cmd->type) {
  491. case PARSE_LABEL:
  492. printf("%srename %s %d \"%s\" to \"%s\"\n",
  493. prefix,
  494. e->cmd->port_id1,
  495. e->p1.port_no + 1, s_port->port[e->p1.port_no].name,
  496. e->p2.param
  497. );
  498. break;
  499. case PARSE_LOCK:
  500. printf("%s%s %s %d \"%s\"\n",
  501. prefix,
  502. e->do_lock ? "lock" : (e->lock == PORT_LOCKED_OTHER ? "force unlock" : "unlock"),
  503. e->cmd->port_id1,
  504. e->p1.port_no + 1, s_port->port[e->p1.port_no].name
  505. );
  506. break;
  507. case PARSE_ROUTE:
  508. if (e->p2.port_no == NO_PORT) {
  509. printf("%sdisconnect %s %d \"%s\"\n",
  510. prefix,
  511. e->cmd->port_id1,
  512. e->p1.port_no + 1, s_port->port[e->p1.port_no].name
  513. );
  514. break;
  515. }
  516. if (e->cmd->allow_disconnect) {
  517. printf("%sconnect %s %d \"%s\" to %s %d \"%s\"\n",
  518. prefix,
  519. e->cmd->port_id1,
  520. e->p1.port_no + 1, s_port->port[e->p1.port_no].name,
  521. e->cmd->port_id2,
  522. e->p2.port_no + 1, d_port->port [e->p2.port_no].name
  523. );
  524. break;
  525. }
  526. if (e->reversed_args) {
  527. printf("%sset %s %d \"%s\" to go out of %s %d \"%s\"\n",
  528. prefix,
  529. e->cmd->port_id2,
  530. e->p2.port_no + 1, d_port->port[e->p2.port_no].name,
  531. e->cmd->port_id1,
  532. e->p1.port_no + 1, s_port->port [e->p1.port_no].name
  533. );
  534. break;
  535. }
  536. printf("%sset %s %d \"%s\" to read from %s %d \"%s\"\n",
  537. prefix,
  538. e->cmd->port_id1,
  539. e->p1.port_no + 1, s_port->port[e->p1.port_no].name,
  540. e->cmd->port_id2,
  541. e->p2.port_no + 1, d_port->port [e->p2.port_no].name
  542. );
  543. break;
  544. case PARSE_DIR:
  545. printf("%sset %s %d \"%s\" direction to %s\n",
  546. prefix,
  547. e->cmd->port_id1,
  548. e->p1.port_no + 1, s_port->port[e->p1.port_no].name,
  549. dir2txt(e->direction)
  550. );
  551. break;
  552. default: break;
  553. }
  554. }