changeset 341:692dbc4c2f07

fcup-atinterf program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 04 Feb 2018 07:56:21 +0000
parents 88fb194b4b61
children c6165837d141
files .hgignore uptools/atinterf/Makefile uptools/atinterf/fcup-atinterf.c
diffstat 3 files changed, 166 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Feb 04 06:20:55 2018 +0000
+++ b/.hgignore	Sun Feb 04 07:56:21 2018 +0000
@@ -59,4 +59,5 @@
 ^toolchain/newlib-2\.0\.0/
 ^toolchain/newlib-build/
 
+^uptools/atinterf/fcup-atinterf$
 ^uptools/sms-pdu-decode/sms-pdu-decode$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uptools/atinterf/Makefile	Sun Feb 04 07:56:21 2018 +0000
@@ -0,0 +1,18 @@
+CC=	gcc
+CFLAGS=	-O2
+PROG=	fcup-atinterf
+LIB=	../../libserial/libserial.a
+OBJS=	${PROG}.o ${LIB}
+INSTBIN=/opt/freecalypso/bin
+
+all:	${PROG}
+
+${PROG}:	${OBJS}
+	${CC} ${CFLAGS} -o $@ ${OBJS}
+
+install:
+	mkdir -p ${INSTBIN}
+	install -c ${PROG} ${INSTBIN}
+
+clean:
+	rm -f ${PROG} *.o *errs *.out
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uptools/atinterf/fcup-atinterf.c	Sun Feb 04 07:56:21 2018 +0000
@@ -0,0 +1,147 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+
+extern int target_fd;
+
+int wakeup_after_sec = 7;
+
+FILE *target_rd;
+char response[515];
+
+char command[513], message[513];
+int cmd_with_msg;
+
+read_command_input(buf)
+	char *buf;
+{
+	char *nl;
+
+	if (!fgets(buf, 513, stdin))
+		return(0);
+	nl = index(buf, '\n');
+	if (!nl) {
+		printf("Ecommand or message is too long\n");
+		exit(1);
+	}
+	*nl = '\0';
+	return(1);
+}
+
+send_to_target(cmd, term)
+	char *cmd;
+{
+	char *endp;
+	int len, cc;
+
+	endp = index(cmd, '\0');
+	*endp = term;
+	len = endp - cmd + 1;
+	cc = write(target_fd, cmd, len);
+	*endp = '\0';
+	if (cc != len) {
+		printf("Etarget write error\n");
+		exit(1);
+	}
+}
+
+collect_target_response()
+{
+	char *nl;
+
+	if (!fgets(response, 515, target_rd)) {
+		printf("Etarget read error\n");
+		exit(1);
+	}
+	nl = index(response, '\n');
+	if (!nl) {
+		printf("Etarget response is too long\n");
+		exit(1);
+	}
+	while (nl > response && nl[-1] == '\r')
+		nl--;
+	*nl = '\0';
+}
+
+execute_command()
+{
+	int c;
+
+	send_to_target(command, '\r');
+	collect_target_response();
+	if (strcmp(command, response)) {
+		printf("Efailed to get echo of command\n");
+		exit(1);
+	}
+	if (cmd_with_msg) {
+		if ((c = getc(target_rd)) != '>') {
+			ungetc(c, target_rd);
+			collect_target_response();
+			printf("F%s\n", response);
+			return;
+		}
+		if ((c = getc(target_rd)) != ' ') {
+			ungetc(c, target_rd);
+			collect_target_response();
+			printf("F%s\n", response);
+			return;
+		}
+		send_to_target(message, '\033');
+		collect_target_response();
+		if (strcmp(message, response)) {
+			printf("Efailed to get echo of message\n");
+			exit(1);
+		}
+	}
+	for (;;) {
+		collect_target_response();
+		if (!strcmp(response, "OK") || !strcmp(response, "ERROR") ||
+		    !strcmp(response, "BUSY") ||
+		    !strcmp(response, "NO CARRIER") ||
+		    !strncmp(response, "+CME ERROR", 10) ||
+		    !strncmp(response, "+CMS ERROR", 10)) {
+			printf("F%s\n", response);
+			return;
+		}
+		printf("I%s\n", response);
+	}
+}
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc < 3 || argc > 4) {
+		fprintf(stderr,
+			"usage: %s ttyport baudrate [wakeup-after-sec]\n",
+			argv[0]);
+		exit(1);
+	}
+	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);
+
+	target_rd = fdopen(target_fd, "r");
+	if (!target_rd) {
+		perror("fdopen");
+		exit(1);
+	}
+
+	while (read_command_input(command)) {
+		if (!strcasecmp(command, "c+m")) {
+			cmd_with_msg = 1;
+			if (!read_command_input(command))
+				break;
+			if (!read_command_input(message))
+				break;
+		} else
+			cmd_with_msg = 0;
+		execute_command();
+		fflush(stdout);
+	}
+	exit(0);
+}