changeset 345:cc207d81c05f

fcup-atinterf: implemented wakeup of sleeping targets
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 04 Feb 2018 16:05:11 +0000
parents e0260c2982b6
children 99471c57155a
files uptools/atinterf/fcup-atinterf.c
diffstat 1 files changed, 42 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/uptools/atinterf/fcup-atinterf.c	Sun Feb 04 08:23:54 2018 +0000
+++ b/uptools/atinterf/fcup-atinterf.c	Sun Feb 04 16:05:11 2018 +0000
@@ -1,4 +1,5 @@
 #include <sys/types.h>
+#include <sys/time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -7,8 +8,6 @@
 
 extern int target_fd;
 
-int wakeup_after_sec = 7;
-
 FILE *target_rd;
 char response[515];
 
@@ -48,6 +47,18 @@
 	}
 }
 
+single_char_to_target(ch)
+{
+	char buf = ch;
+	int cc;
+
+	cc = write(target_fd, &buf, 1);
+	if (cc != 1) {
+		printf("Etarget write error\n");
+		exit(1);
+	}
+}
+
 collect_target_response()
 {
 	char *nl;
@@ -110,9 +121,29 @@
 	}
 }
 
+wakeup_at()
+{
+	single_char_to_target('A');
+	usleep(50000);
+	single_char_to_target('T');
+	usleep(50000);
+	single_char_to_target('\r');
+	collect_target_response();
+	if (response[0] && strcmp(response, "AT")) {
+badresp:	printf("Ebad response to wakeup AT command\n");
+		exit(1);
+	}
+	collect_target_response();
+	if (strcmp(response, "OK"))
+		goto badresp;
+}
+
 main(argc, argv)
 	char **argv;
 {
+	int wakeup_after_sec = 7;
+	struct timeval curtime, last_time, timediff;
+
 	if (argc < 3 || argc > 4) {
 		fprintf(stderr,
 			"usage: %s ttyport baudrate [wakeup-after-sec]\n",
@@ -121,15 +152,16 @@
 	}
 	open_serial_port(argv[1]);
 	set_fixed_baudrate(argv[2]);
-	set_serial_nonblock(0);
 	if (argc > 3)
 		wakeup_after_sec = strtoul(argv[3], 0, 0);
 
+	set_serial_nonblock(0);
 	target_rd = fdopen(target_fd, "r");
 	if (!target_rd) {
 		perror("fdopen");
 		exit(1);
 	}
+	bzero(&last_time, sizeof(struct timeval));
 
 	while (read_command_input(command)) {
 		if (!strcasecmp(command, "c+m")) {
@@ -140,6 +172,13 @@
 				break;
 		} else
 			cmd_with_msg = 0;
+		if (wakeup_after_sec) {
+			gettimeofday(&curtime, 0);
+			timersub(&curtime, &last_time, &timediff);
+			if (timediff.tv_sec >= wakeup_after_sec)
+				wakeup_at();
+			bcopy(&curtime, &last_time, sizeof(struct timeval));
+		}
 		execute_command();
 		fflush(stdout);
 	}