diff mgw/main.c @ 127:f062c32a5116

mgw: implement DTMF
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 01 Oct 2022 20:31:15 -0800
parents 815e4c59162e
children a6eb2de277f6
line wrap: on
line diff
--- a/mgw/main.c	Sat Oct 01 17:09:51 2022 -0800
+++ b/mgw/main.c	Sat Oct 01 20:31:15 2022 -0800
@@ -3,6 +3,7 @@
  */
 
 #include <sys/types.h>
+#include <sys/time.h>
 #include <sys/errno.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -13,9 +14,13 @@
 #include <syslog.h>
 #include <unistd.h>
 
+extern int dtmf_timer_running;
+extern struct timeval dtmf_next_time;
+
 fd_set select_for_read;
 void (*select_handlers[FD_SETSIZE])();
 void *select_data[FD_SETSIZE];
+struct timeval cur_event_time;
 
 static int max_fd;
 
@@ -34,6 +39,7 @@
 	char **argv;
 {
 	fd_set fds;
+	struct timeval timeout;
 	int cc, i;
 
 	openlog("themwi-mgw", 0, LOG_LOCAL5);
@@ -47,19 +53,31 @@
 	/* main select loop */
 	for (;;) {
 		bcopy(&select_for_read, &fds, sizeof(fd_set));
-		cc = select(max_fd+1, &fds, 0, 0, 0);
+		if (dtmf_timer_running) {
+			if (timercmp(&dtmf_next_time, &cur_event_time, >))
+				timersub(&dtmf_next_time, &cur_event_time,
+					 &timeout);
+			else
+				timerclear(&timeout);
+			cc = select(max_fd+1, &fds, 0, 0, &timeout);
+		} else
+			cc = select(max_fd+1, &fds, 0, 0, 0);
 		if (cc < 0) {
 			if (errno == EINTR)
 				continue;
 			syslog(LOG_CRIT, "select: %m");
 			exit(1);
 		}
+		gettimeofday(&cur_event_time, 0);
 		for (i = 0; cc && i <= max_fd; i++) {
 			if (FD_ISSET(i, &fds)) {
 				select_handlers[i](i, select_data[i]);
 				cc--;
 			}
 		}
+		if (dtmf_timer_running &&
+		    !timercmp(&cur_event_time, &dtmf_next_time, <))
+			dtmf_timer_process();
 		free_deleted_endpoints();
 	}
 }