comparison 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
comparison
equal deleted inserted replaced
126:815e4c59162e 127:f062c32a5116
1 /* 1 /*
2 * Main module for themwi-mgw. 2 * Main module for themwi-mgw.
3 */ 3 */
4 4
5 #include <sys/types.h> 5 #include <sys/types.h>
6 #include <sys/time.h>
6 #include <sys/errno.h> 7 #include <sys/errno.h>
7 #include <stdio.h> 8 #include <stdio.h>
8 #include <stdint.h> 9 #include <stdint.h>
9 #include <stdlib.h> 10 #include <stdlib.h>
10 #include <string.h> 11 #include <string.h>
11 #include <strings.h> 12 #include <strings.h>
12 #include <signal.h> 13 #include <signal.h>
13 #include <syslog.h> 14 #include <syslog.h>
14 #include <unistd.h> 15 #include <unistd.h>
15 16
17 extern int dtmf_timer_running;
18 extern struct timeval dtmf_next_time;
19
16 fd_set select_for_read; 20 fd_set select_for_read;
17 void (*select_handlers[FD_SETSIZE])(); 21 void (*select_handlers[FD_SETSIZE])();
18 void *select_data[FD_SETSIZE]; 22 void *select_data[FD_SETSIZE];
23 struct timeval cur_event_time;
19 24
20 static int max_fd; 25 static int max_fd;
21 26
22 update_max_fd(newfd) 27 update_max_fd(newfd)
23 { 28 {
32 37
33 main(argc, argv) 38 main(argc, argv)
34 char **argv; 39 char **argv;
35 { 40 {
36 fd_set fds; 41 fd_set fds;
42 struct timeval timeout;
37 int cc, i; 43 int cc, i;
38 44
39 openlog("themwi-mgw", 0, LOG_LOCAL5); 45 openlog("themwi-mgw", 0, LOG_LOCAL5);
40 read_config_file(); 46 read_config_file();
41 dtmf_init_sample_arrays(); 47 dtmf_init_sample_arrays();
45 } 51 }
46 signal(SIGPIPE, SIG_IGN); 52 signal(SIGPIPE, SIG_IGN);
47 /* main select loop */ 53 /* main select loop */
48 for (;;) { 54 for (;;) {
49 bcopy(&select_for_read, &fds, sizeof(fd_set)); 55 bcopy(&select_for_read, &fds, sizeof(fd_set));
50 cc = select(max_fd+1, &fds, 0, 0, 0); 56 if (dtmf_timer_running) {
57 if (timercmp(&dtmf_next_time, &cur_event_time, >))
58 timersub(&dtmf_next_time, &cur_event_time,
59 &timeout);
60 else
61 timerclear(&timeout);
62 cc = select(max_fd+1, &fds, 0, 0, &timeout);
63 } else
64 cc = select(max_fd+1, &fds, 0, 0, 0);
51 if (cc < 0) { 65 if (cc < 0) {
52 if (errno == EINTR) 66 if (errno == EINTR)
53 continue; 67 continue;
54 syslog(LOG_CRIT, "select: %m"); 68 syslog(LOG_CRIT, "select: %m");
55 exit(1); 69 exit(1);
56 } 70 }
71 gettimeofday(&cur_event_time, 0);
57 for (i = 0; cc && i <= max_fd; i++) { 72 for (i = 0; cc && i <= max_fd; i++) {
58 if (FD_ISSET(i, &fds)) { 73 if (FD_ISSET(i, &fds)) {
59 select_handlers[i](i, select_data[i]); 74 select_handlers[i](i, select_data[i]);
60 cc--; 75 cc--;
61 } 76 }
62 } 77 }
78 if (dtmf_timer_running &&
79 !timercmp(&cur_event_time, &dtmf_next_time, <))
80 dtmf_timer_process();
63 free_deleted_endpoints(); 81 free_deleted_endpoints();
64 } 82 }
65 } 83 }