jcs
/subtext
/amendments
/157
serial: Keep track of RINGs and if no connection after 45 secs, reset
jcs made amendment 157 over 3 years ago
--- serial.c	Thu Jun 16 21:51:58 2022
+++ serial.c	Tue Jun 21 13:37:54 2022
@@ -36,6 +36,13 @@
 #include "subtext.h"
 #include "util.h"
 
+/*
+ * The number of seconds after a RING that we'll allow a connection to
+ * establish and a session to start, before we hang up and reset the
+ * modem.
+ */
+#define SERIAL_CONNECT_TIMEOUT	45
+
 static bool serial_initialized = false;
 static short serial_out_refnum = 0, serial_in_refnum = 0;
 static char serial_input_internal_buf[1024];
@@ -48,6 +55,7 @@ struct serial_node {
 	unsigned char obuf[512];
 	unsigned char ibuf[64];
 	unsigned short ibuflen;
+	unsigned long last_ring;
 	struct session *session;
 } the_serial_node;
 
@@ -144,6 +152,7 @@ serial_reset(void)
 	the_serial_node.connected = false;
 	the_serial_node.obuflen = 0;
 	the_serial_node.ibuflen = 0;
+	the_serial_node.last_ring = 0;
 	
 	serial_input_buf_len = 0;
 }
@@ -290,16 +299,25 @@ serial_idle(void)
 #if 0
 	SerStatus(serial_in_refnum, &status);
 	if (status.ctsHold != 0)
-		session_log(session, "CTS set");
+		logger_printf(logger, "[modem] CTS set");
 	if (status.xOffHold != 0)
-		session_log(session, "XOFF set");
+		logger_printf(logger, "[modem] XOFF set");
 	if (status.cumErrs != 0)
-		session_log(session, "Reported errors %d", status.cumErrs);
+		logger_printf(logger, "[modem] Reported errors %d",
+		  status.cumErrs);
 #endif
 
 	if (the_serial_node.connected)
 		return;
 
+	if (the_serial_node.last_ring != 0 &&
+	  (Time - the_serial_node.last_ring > SERIAL_CONNECT_TIMEOUT)) {
+		logger_printf(logger, "[modem] Timed out establishing a "
+		  "connection, resetting");
+		serial_reset();
+		return;
+	}
+	
 	if ((line = serial_get_line(false)) == NULL)
 		return;
 
@@ -319,6 +337,8 @@ serial_idle(void)
 		if (sscanf(line, "CONNECT %ld/%n", &baud, &count) == 1 &&
 		 count > 0)
 			the_serial_node.session->tspeed = baud;
+	} else if (strstr(line, "RING") != NULL) {
+		the_serial_node.last_ring = Time;
 	}
 }