diff rvinterf/asyncshell/at.c @ 75:bbc41034f14c

fc-shell: added support for AT commands in one-shot mode
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 27 Oct 2016 02:13:38 +0000
parents d43d82cbfb85
children dd0247097097
line wrap: on
line diff
--- a/rvinterf/asyncshell/at.c	Thu Oct 27 00:18:49 2016 +0000
+++ b/rvinterf/asyncshell/at.c	Thu Oct 27 02:13:38 2016 +0000
@@ -3,15 +3,22 @@
  */
 
 #include <sys/types.h>
+#include <sys/errno.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
 #include <strings.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include "pktmux.h"
 #include "limits.h"
 #include "exitcodes.h"
 
+extern u_char rvi_msg[];
+extern int rvi_msg_len;
+extern int oneshot_nowait;
+extern int sock;
+
 send_string_to_ati(str)
 	char *str;
 {
@@ -44,3 +51,59 @@
 	ati_rx_control(1);
 	send_string_to_ati(arg);
 }
+
+oneshot_at_command(cmd)
+	char *cmd;
+{
+	fd_set fds;
+	int rc;
+
+	if (!oneshot_nowait) {
+		init();		/* to catch the response properly */
+		ati_rx_control(1);
+	}
+	rc = send_string_to_ati(cmd);
+	if (rc)
+		exit(rc);
+	if (oneshot_nowait)
+		exit(0);
+	/* wait for response */
+	for (;;) {
+		FD_ZERO(&fds);
+		FD_SET(sock, &fds);
+		rc = select(sock+1, &fds, 0, 0, 0);
+		if (rc < 0) {
+			if (errno == EINTR)
+				continue;
+			perror("select");
+			exit(ERROR_UNIX);
+		}
+		if (FD_ISSET(sock, &fds))
+			handle_rvinterf_input();
+	}
+}
+
+cmd_str_oneshot(argc, argv)
+	char **argv;
+{
+	return oneshot_at_command(argv[1]);
+}
+
+void
+oneshot_at_check_response()
+{
+	if (rvi_msg_len == 4 && !strncmp(rvi_msg + 2, "OK", 2))
+		exit(0);
+	if (rvi_msg_len == 4 && !strncmp(rvi_msg + 2, "> ", 2))
+		exit(0);
+	if (rvi_msg_len == 7 && !strncmp(rvi_msg + 2, "ERROR", 5))
+		exit(ERROR_TARGET);
+	if (rvi_msg_len == 6 && !strncmp(rvi_msg + 2, "BUSY", 4))
+		exit(ERROR_TARGET);
+	if (rvi_msg_len == 12 && !strncmp(rvi_msg + 2, "NO CARRIER", 10))
+		exit(ERROR_TARGET);
+	if (rvi_msg_len >= 12 && !strncmp(rvi_msg + 2, "+CME ERROR", 10))
+		exit(ERROR_TARGET);
+	if (rvi_msg_len >= 12 && !strncmp(rvi_msg + 2, "+CMS ERROR", 10))
+		exit(ERROR_TARGET);
+}