Browse Source

Add -t / --timeout option

Georgi Chorbadzhiyski 3 years 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
 Main options:
42
 Main options:
43
  -h --host <host>           | Set device host name.
43
  -h --host <host>           | Set device host name.
44
  -p --port <port_number>    | Set device port (default: 9990).
44
  -p --port <port_number>    | Set device port (default: 9990).
45
+ -t --timeout <secs>        | Set connect/read timeout. Default: 15
45
 
46
 
46
 Commands:
47
 Commands:
47
  -i --info                  | Show full device info (default command).
48
  -i --info                  | Show full device info (default command).

+ 2
- 1
net.c View File

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

+ 4
- 0
videohubctrl.1 View File

28
 \fB\-p\fR, \fB\-\-port\fR <port>
28
 \fB\-p\fR, \fB\-\-port\fR <port>
29
 Set the device port. You can set \fBVIDEOHUB_PORT\fR environment
29
 Set the device port. You can set \fBVIDEOHUB_PORT\fR environment
30
 variable instead of using this option. The default port is \fB9990\fR.
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
 .SH COMMANDS
35
 .SH COMMANDS
32
 .PP
36
 .PP
33
 .TP
37
 .TP

+ 11
- 3
videohubctrl.c View File

29
 
29
 
30
 int debug;
30
 int debug;
31
 int quiet;
31
 int quiet;
32
+int timeout = 15;
32
 
33
 
33
 static struct videohub_data maindata;
34
 static struct videohub_data maindata;
34
 static int show_info = 1;
35
 static int show_info = 1;
49
 
50
 
50
 static const char *program_id = PROGRAM_NAME " Version: " VERSION " Git: " GIT_VER;
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
 static const struct option long_options[] = {
55
 static const struct option long_options[] = {
55
 	{ "host",				required_argument, NULL, 'h' },
56
 	{ "host",				required_argument, NULL, 'h' },
62
 	{ "info",				no_argument,       NULL, 'i' },
63
 	{ "info",				no_argument,       NULL, 'i' },
63
 	{ "monitor",			no_argument,       NULL, 'm' },
64
 	{ "monitor",			no_argument,       NULL, 'm' },
64
 	{ "backup",				no_argument,       NULL, 'b' },
65
 	{ "backup",				no_argument,       NULL, 'b' },
66
+	{ "timeout",			required_argument, NULL, 't' },
65
 
67
 
66
 	{ "list-device",		no_argument,       NULL, 901 },
68
 	{ "list-device",		no_argument,       NULL, 901 },
67
 	{ "list-inputs",		no_argument,       NULL, 902 },
69
 	{ "list-inputs",		no_argument,       NULL, 902 },
148
 	printf("Main options:\n");
150
 	printf("Main options:\n");
149
 	printf(" -h --host <host>           | Set device host name.\n");
151
 	printf(" -h --host <host>           | Set device host name.\n");
150
 	printf(" -p --port <port_number>    | Set device port (default: 9990).\n");
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
 	printf("\n");
154
 	printf("\n");
152
 	printf("Commands:\n");
155
 	printf("Commands:\n");
153
 	printf(" -i --info                  | Show full device info (default command).\n");
156
 	printf(" -i --info                  | Show full device info (default command).\n");
299
 			case 'p': // --port
302
 			case 'p': // --port
300
 				data->dev_port = optarg;
303
 				data->dev_port = optarg;
301
 				break;
304
 				break;
305
+			case 't': // --timeout
306
+				timeout = strtoimax(optarg, NULL, 0);
307
+				break;
302
 			case 'T': { // --test-input
308
 			case 'T': { // --test-input
303
 				struct stat st;
309
 				struct stat st;
304
 				FILE *f;
310
 				FILE *f;
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
 		err = 1;
384
 		err = 1;
379
 
385
 
380
 	if (err) {
386
 	if (err) {
381
 		show_help(data);
387
 		show_help(data);
382
 		if (!data->dev_host)
388
 		if (!data->dev_host)
383
 			fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
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
 		exit(EXIT_FAILURE);
392
 		exit(EXIT_FAILURE);
385
 	}
393
 	}
386
 
394
 
416
 	if (test_data)
424
 	if (test_data)
417
 		return 0;
425
 		return 0;
418
 	memset(buf, 0, sizeof(buf));
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
 		ncommands += parse_text_buffer(d, buf);
428
 		ncommands += parse_text_buffer(d, buf);
421
 		memset(buf, 0, sizeof(buf));
429
 		memset(buf, 0, sizeof(buf));
422
 	}
430
 	}

Loading…
Cancel
Save