Browse Source

Add -t / --timeout option

Georgi Chorbadzhiyski 8 months ago
parent
commit
d8974177cb
4 changed files with 18 additions and 4 deletions
  1. 1
    0
      README
  2. 2
    1
      net.c
  3. 4
    0
      videohubctrl.1
  4. 11
    3
      videohubctrl.c

+ 1
- 0
README View File

@@ -42,6 +42,7 @@ of supported command line parameters:
42 42
 Main options:
43 43
  -h --host <host>           | Set device host name.
44 44
  -p --port <port_number>    | Set device port (default: 9990).
45
+ -t --timeout <secs>        | Set connect/read timeout. Default: 15
45 46
 
46 47
 Commands:
47 48
  -i --info                  | Show full device info (default command).

+ 2
- 1
net.c View File

@@ -26,6 +26,7 @@
26 26
 #include "libfuncs/libfuncs.h"
27 27
 
28 28
 int ai_family = AF_UNSPEC;
29
+extern int timeout;
29 30
 
30 31
 static char *my_inet_ntop(int family, struct sockaddr *addr, char *dest, int dest_len) {
31 32
 	struct sockaddr_in  *addr_v4 = (struct sockaddr_in  *)addr;
@@ -69,7 +70,7 @@ int connect_client(int socktype, const char *hostname, const char *service) {
69 70
 		sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
70 71
 		if (sockfd > -1) {
71 72
 			my_inet_ntop(res->ai_family, res->ai_addr, str_addr, sizeof(str_addr));
72
-			if (do_connect(sockfd, res->ai_addr, res->ai_addrlen, 1000) < 0) {
73
+			if (do_connect(sockfd, res->ai_addr, res->ai_addrlen, timeout * 1000) < 0) {
73 74
 				fprintf(stderr, "ERROR: Cant connect to server %s port %s (addr=%s) | %s\n",
74 75
 					hostname, service, str_addr, strerror(errno));
75 76
 				close(sockfd);

+ 4
- 0
videohubctrl.1 View File

@@ -28,6 +28,10 @@ this option.
28 28
 \fB\-p\fR, \fB\-\-port\fR <port>
29 29
 Set the device port. You can set \fBVIDEOHUB_PORT\fR environment
30 30
 variable instead of using this option. The default port is \fB9990\fR.
31
+.TP
32
+\fB\-t\fR, \fB\-\-timeout <seconds>\fR
33
+Set the timeout in seconds for connect and read. The default is 15 seconds,
34
+min vlue is 1 and max value is 60
31 35
 .SH COMMANDS
32 36
 .PP
33 37
 .TP

+ 11
- 3
videohubctrl.c View File

@@ -29,6 +29,7 @@
29 29
 
30 30
 int debug;
31 31
 int quiet;
32
+int timeout = 15;
32 33
 
33 34
 static struct videohub_data maindata;
34 35
 static int show_info = 1;
@@ -49,7 +50,7 @@ enum list_actions {
49 50
 
50 51
 static const char *program_id = PROGRAM_NAME " Version: " VERSION " Git: " GIT_VER;
51 52
 
52
-static const char short_options[] = "h:p:T:qdHVimb";
53
+static const char short_options[] = "h:p:T:qdHVimbt:";
53 54
 
54 55
 static const struct option long_options[] = {
55 56
 	{ "host",				required_argument, NULL, 'h' },
@@ -62,6 +63,7 @@ static const struct option long_options[] = {
62 63
 	{ "info",				no_argument,       NULL, 'i' },
63 64
 	{ "monitor",			no_argument,       NULL, 'm' },
64 65
 	{ "backup",				no_argument,       NULL, 'b' },
66
+	{ "timeout",			required_argument, NULL, 't' },
65 67
 
66 68
 	{ "list-device",		no_argument,       NULL, 901 },
67 69
 	{ "list-inputs",		no_argument,       NULL, 902 },
@@ -148,6 +150,7 @@ static void show_help(struct videohub_data *data) {
148 150
 	printf("Main options:\n");
149 151
 	printf(" -h --host <host>           | Set device host name.\n");
150 152
 	printf(" -p --port <port_number>    | Set device port (default: 9990).\n");
153
+	printf(" -t --timeout <secs>        | Set connect/read timeout. Default: %d\n", timeout);
151 154
 	printf("\n");
152 155
 	printf("Commands:\n");
153 156
 	printf(" -i --info                  | Show full device info (default command).\n");
@@ -299,6 +302,9 @@ static void parse_options(struct videohub_data *data, int argc, char **argv) {
299 302
 			case 'p': // --port
300 303
 				data->dev_port = optarg;
301 304
 				break;
305
+			case 't': // --timeout
306
+				timeout = strtoimax(optarg, NULL, 0);
307
+				break;
302 308
 			case 'T': { // --test-input
303 309
 				struct stat st;
304 310
 				FILE *f;
@@ -374,13 +380,15 @@ static void parse_options(struct videohub_data *data, int argc, char **argv) {
374 380
 		}
375 381
 	}
376 382
 
377
-	if (!data->dev_host || !strtoul(data->dev_port, NULL, 10))
383
+	if (!data->dev_host || !strtoul(data->dev_port, NULL, 10) || !timeout || timeout > 60)
378 384
 		err = 1;
379 385
 
380 386
 	if (err) {
381 387
 		show_help(data);
382 388
 		if (!data->dev_host)
383 389
 			fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
390
+		if (!timeout || timeout > 60)
391
+			fprintf(stderr, "ERROR: invalid timeout value, must be between 1 and 60\n");
384 392
 		exit(EXIT_FAILURE);
385 393
 	}
386 394
 
@@ -416,7 +424,7 @@ static int read_device_command_stream(struct videohub_data *d) {
416 424
 	if (test_data)
417 425
 		return 0;
418 426
 	memset(buf, 0, sizeof(buf));
419
-	while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 1)) >= 0) {
427
+	while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, timeout, 0, 1)) >= 0) {
420 428
 		ncommands += parse_text_buffer(d, buf);
421 429
 		memset(buf, 0, sizeof(buf));
422 430
 	}

Loading…
Cancel
Save