|
@@ -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
|
}
|