Browse Source

Add support for reading commands from text file using -T / --test-input.

Georgi Chorbadzhiyski 9 years ago
parent
commit
e7d8e15880
6 changed files with 137 additions and 102 deletions
  1. 2
    0
      ChangeLog
  2. 4
    3
      README
  3. 0
    90
      cmd.c
  4. 84
    0
      vh_test_commands.txt
  5. 5
    0
      videohubctrl.1
  6. 42
    9
      videohubctrl.c

+ 2
- 0
ChangeLog View File

@@ -3,6 +3,8 @@
3 3
  * Add videohubctrl(1) man page.
4 4
  * Add all, install, uninstall and help Makefile targets.
5 5
  * Display where each input is being routed.
6
+ * Add support for testing the program using pre-recorded commands
7
+   in text file by using -T / --test-input option.
6 8
 
7 9
 2014-11-28 : Version 0.3
8 10
  * Rename -v / --verbose parameter to -d / --debug.

+ 4
- 3
README View File

@@ -69,6 +69,7 @@ Video input/output configuration:
69 69
    For <in_X/out_X/in_Y> you may use port number or port name.
70 70
 
71 71
 Misc options:
72
+ -T --test-input <file>     | Read commands from <file>.
72 73
  -d --debug                 | Increase logging verbosity.
73 74
  -q --quiet                 | Suppress warnings.
74 75
  -H --help                  | Show help screen.
@@ -136,10 +137,11 @@ Example usage
136 137
 Example output
137 138
 ==============
138 139
 
139
-Here is how videohubctrl output looks like:
140
+Here is how videohubctrl output looks like using the commands recorded
141
+in vh_test_commands.txt file.
140 142
 
141 143
 |----------------------------------------------------------------------|
142
-gf@gf:~/git/videohubctrl$ ./videohubctrl --host sdi-matrix
144
+$ videohubctrl --test vh_test_commands.txt
143 145
 Device info
144 146
   -----------------------------------------------------------
145 147
   | Device address             | sdi-matrix                 |
@@ -197,7 +199,6 @@ Video outputs
197 199
   | 16 | O | Loopback                 | Loopback                 |
198 200
   ----------------------------------------------------------------
199 201
 
200
-gf@gf:~/git/videohubctrl$
201 202
 |----------------------------------------------------------------------|
202 203
 
203 204
 Video outputs Legend

+ 0
- 90
cmd.c View File

@@ -18,96 +18,6 @@
18 18
 #include "cmd.h"
19 19
 #include "util.h"
20 20
 
21
-/*
22
-Example response from videohub (on connect)
23
-===========================================
24
-PROTOCOL PREAMBLE:
25
-Version: 2.4
26
-
27
-VIDEOHUB DEVICE:
28
-Device present: true
29
-Model name: Blackmagic Micro Videohub
30
-Unique ID: 7c2e0d021714
31
-Video inputs: 16
32
-Video processing units: 0
33
-Video outputs: 16
34
-Video monitoring outputs: 0
35
-Serial ports: 0
36
-
37
-INPUT LABELS:
38
-0 Windows 1
39
-1 Windows 2
40
-2 Windows 3
41
-3 Windows 4 HD
42
-4 Input 5
43
-5 Input 6
44
-6 Input 7
45
-7 Input 8
46
-8 Input 9
47
-9 Input 10
48
-10 Input 11
49
-11 DPlay1
50
-12 DPlay2
51
-13 Input 14
52
-14 Input 15
53
-15 Loopback
54
-
55
-OUTPUT LABELS:
56
-0 Enc1 1
57
-1 Enc1 2
58
-2 Enc1 3
59
-3 Enc1 4
60
-4 Output 5
61
-5 Output 6
62
-6 Output 7
63
-7 Output 8
64
-8 Enc2 1
65
-9 Output 10
66
-10 Output 11
67
-11 Denc
68
-12 Output 13
69
-13 Output 14
70
-14 Output 15
71
-15 Loopback
72
-
73
-VIDEO OUTPUT LOCKS:
74
-0 L
75
-1 L
76
-2 L
77
-3 L
78
-4 U
79
-5 U
80
-6 U
81
-7 U
82
-8 L
83
-9 U
84
-10 U
85
-11 L
86
-12 L
87
-13 O
88
-14 U
89
-15 O
90
-
91
-VIDEO OUTPUT ROUTING:
92
-0 2
93
-1 1
94
-2 0
95
-3 0
96
-4 4
97
-5 5
98
-6 6
99
-7 7
100
-8 3
101
-9 3
102
-10 10
103
-11 12
104
-12 11
105
-13 13
106
-14 14
107
-15 15
108
-
109
-*/
110
-
111 21
 static struct videohub_commands {
112 22
 	enum vcmd	cmd;
113 23
 	const char	*txt;

+ 84
- 0
vh_test_commands.txt View File

@@ -0,0 +1,84 @@
1
+PROTOCOL PREAMBLE:
2
+Version: 2.4
3
+
4
+VIDEOHUB DEVICE:
5
+Device present: true
6
+Model name: Blackmagic Micro Videohub
7
+Unique ID: 7c2e0d021714
8
+Video inputs: 16
9
+Video processing units: 0
10
+Video outputs: 16
11
+Video monitoring outputs: 0
12
+Serial ports: 0
13
+
14
+INPUT LABELS:
15
+0 Windows 1
16
+1 Windows 2
17
+2 Windows 3
18
+3 Windows 4 HD
19
+4 Input 5
20
+5 Input 6
21
+6 Input 7
22
+7 Input 8
23
+8 Input 9
24
+9 Input 10
25
+10 Input 11
26
+11 DPlay1
27
+12 DPlay2
28
+13 Input 14
29
+14 Input 15
30
+15 Loopback
31
+
32
+OUTPUT LABELS:
33
+0 Enc1 1
34
+1 Enc1 2
35
+2 Enc1 3
36
+3 Enc1 4
37
+4 Output 5
38
+5 Output 6
39
+6 Output 7
40
+7 Output 8
41
+8 Enc2 1
42
+9 Output 10
43
+10 Output 11
44
+11 Denc
45
+12 Output 13
46
+13 Output 14
47
+14 Output 15
48
+15 Loopback
49
+
50
+VIDEO OUTPUT LOCKS:
51
+0 L
52
+1 L
53
+2 L
54
+3 L
55
+4 U
56
+5 U
57
+6 U
58
+7 U
59
+8 L
60
+9 U
61
+10 U
62
+11 L
63
+12 O
64
+13 U
65
+14 U
66
+15 O
67
+
68
+VIDEO OUTPUT ROUTING:
69
+0 2
70
+1 1
71
+2 0
72
+3 0
73
+4 4
74
+5 5
75
+6 6
76
+7 3
77
+8 3
78
+9 9
79
+10 10
80
+11 12
81
+12 11
82
+13 13
83
+14 14
84
+15 15

+ 5
- 0
videohubctrl.1 View File

@@ -76,6 +76,11 @@ port would be forcefully unlocked.
76 76
 .SH MISC OPTIONS
77 77
 .PP
78 78
 .TP
79
+\fB\-T\fR, \fB\-\-test\fR <file>
80
+Read commands from the <file> instead of connecting to a real
81
+device. This allows testing the program without having access
82
+to a device.
83
+.TP
79 84
 \fB\-d\fR, \fB\-\-debug\fR
80 85
 Enable debugging output. Use this option more times to increase
81 86
 the verbosity.

+ 42
- 9
videohubctrl.c View File

@@ -13,6 +13,10 @@
13 13
 #include <string.h>
14 14
 #include <getopt.h>
15 15
 #include <unistd.h>
16
+#include <sys/types.h>
17
+#include <sys/stat.h>
18
+#include <unistd.h>
19
+#include <errno.h>
16 20
 
17 21
 #include "data.h"
18 22
 #include "cmd.h"
@@ -31,6 +35,7 @@ static int show_info = 1;
31 35
 static int show_monitor = 0;
32 36
 static int show_backup = 0;
33 37
 static int show_list = 0;
38
+static char *test_data;
34 39
 
35 40
 enum list_actions {
36 41
 	action_list_device		= (1 << 0),
@@ -40,11 +45,12 @@ enum list_actions {
40 45
 
41 46
 static const char *program_id = PROGRAM_NAME " Version: " VERSION " Git: " GIT_VER;
42 47
 
43
-static const char short_options[] = "h:p:qdHVimb";
48
+static const char short_options[] = "h:p:T:qdHVimb";
44 49
 
45 50
 static const struct option long_options[] = {
46 51
 	{ "host",				required_argument, NULL, 'h' },
47 52
 	{ "port",				required_argument, NULL, 'p' },
53
+	{ "test-input",			required_argument, NULL, 'T' },
48 54
 	{ "quiet",				no_argument,       NULL, 'q' },
49 55
 	{ "debug",				no_argument,       NULL, 'd' },
50 56
 	{ "help",				no_argument,       NULL, 'H' },
@@ -96,6 +102,7 @@ static void show_help(struct videohub_data *data) {
96 102
 	printf("   For <in_X/out_X/in_Y> you may use port number or port name.\n");
97 103
 	printf("\n");
98 104
 	printf("Misc options:\n");
105
+	printf(" -T --test-input <file>     | Read commands from <file>.\n");
99 106
 	printf(" -d --debug                 | Increase logging verbosity.\n");
100 107
 	printf(" -q --quiet                 | Suppress warnings.\n");
101 108
 	printf(" -H --help                  | Show help screen.\n");
@@ -127,6 +134,21 @@ static void parse_options(struct videohub_data *data, int argc, char **argv) {
127 134
 			case 'p': // --port
128 135
 				data->dev_port = optarg;
129 136
 				break;
137
+			case 'T': { // --test-input
138
+				struct stat st;
139
+				FILE *f;
140
+				if (stat(optarg, &st) != 0)
141
+					die("Can't stat %s: %s", optarg, strerror(errno));
142
+				f = fopen(optarg, "r");
143
+				if (!f)
144
+					die("Can't open %s: %s", optarg, strerror(errno));
145
+				test_data = xzalloc(st.st_size);
146
+				if (fread(test_data, st.st_size, 1, f) < 1)
147
+					die("Can't read from %s: %s", optarg, strerror(errno));
148
+				fclose(f);
149
+				data->dev_host = "sdi-matrix";
150
+				break;
151
+			}
130 152
 			case 'd': // --debug
131 153
 				debug++;
132 154
 				if (debug)
@@ -202,6 +224,8 @@ static void parse_options(struct videohub_data *data, int argc, char **argv) {
202 224
 static int read_device_command_stream(struct videohub_data *d) {
203 225
 	int ret, ncommands = 0;
204 226
 	char buf[8192 + 1];
227
+	if (test_data)
228
+		return 0;
205 229
 	memset(buf, 0, sizeof(buf));
206 230
 	while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
207 231
 		ncommands += parse_text_buffer(d, buf);
@@ -210,18 +234,28 @@ static int read_device_command_stream(struct videohub_data *d) {
210 234
 	return ncommands;
211 235
 }
212 236
 
237
+static void send_device_command(struct videohub_data *d, char *cmd_buffer) {
238
+	if (!test_data)
239
+		fdwrite(d->dev_fd, cmd_buffer, strlen(cmd_buffer));
240
+}
241
+
213 242
 int main(int argc, char **argv) {
214 243
 	struct videohub_data *data = &maindata;
215 244
 
216 245
 	parse_options(data, argc, argv);
217 246
 	set_log_io_errors(0);
218 247
 
219
-	data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
220
-	if (data->dev_fd < 0)
221
-		exit(EXIT_FAILURE);
248
+	if (!test_data) {
249
+		data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
250
+		if (data->dev_fd < 0)
251
+			exit(EXIT_FAILURE);
252
+	}
222 253
 
223 254
 	read_device_command_stream(data);
224 255
 
256
+	if (test_data)
257
+		parse_text_buffer(data, test_data);
258
+
225 259
 	if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
226 260
 		die("The device does not respond correctly. Is it Videohub?");
227 261
 
@@ -261,7 +295,7 @@ int main(int argc, char **argv) {
261 295
 			format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
262 296
 			if (strlen(cmd_buffer)) {
263 297
 				show_cmd(data, ve);
264
-				fdwrite(data->dev_fd, cmd_buffer, strlen(cmd_buffer));
298
+				send_device_command(data, cmd_buffer);
265 299
 				read_device_command_stream(data);
266 300
 			}
267 301
 		}
@@ -278,10 +312,8 @@ int main(int argc, char **argv) {
278 312
 			fflush(stdout);
279 313
 			do {
280 314
 				usleep(500000);
281
-				if (++sleeps >= 20) {
282
-					char *ping_cmd = "PING:\n\n";
283
-					fdwrite(data->dev_fd, ping_cmd, strlen(ping_cmd));
284
-				}
315
+				if (++sleeps >= 20)
316
+					send_device_command(data, "PING:\n\n");
285 317
 			} while (read_device_command_stream(data) == 0);
286 318
 		}
287 319
 	} else if (show_list) {
@@ -299,6 +331,7 @@ int main(int argc, char **argv) {
299 331
 	}
300 332
 
301 333
 	shutdown_fd(&data->dev_fd);
334
+	free(test_data);
302 335
 
303 336
 	return 0;
304 337
 }

Loading…
Cancel
Save