From a882dbfaa2a878f9fb7c11e361e76e192ef27951 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Fri, 12 Jul 2019 00:54:27 +0200 Subject: Some refactoring --- src/modem.c | 108 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 57 insertions(+), 51 deletions(-) (limited to 'src/modem.c') diff --git a/src/modem.c b/src/modem.c index a5644a7..0776bed 100644 --- a/src/modem.c +++ b/src/modem.c @@ -24,7 +24,7 @@ int modem_accept_wait(int fd) modem_command(fd, _AT_MUTE, 1000); modem_command(fd, _AT_RESET_ON_DTR, 1000); -#ifdef _MODEM_WAIT_RING //DONT wait for ring +#ifdef _MODEM_WAIT_RING while ( 1 ) { //wait for RING ret = poll(&fds, 1, 2000); //poll in 2s interval usleep(5000); @@ -44,7 +44,7 @@ int modem_accept_wait(int fd) cnt = 0; while(1) { - ret = poll(&fds, 1, 60000); //Connection timeout 1 minute + ret = poll(&fds, 1, timeout); //Connection timeout 1 minute usleep(5000); //Wait for data if(!ret) break; @@ -121,9 +121,9 @@ int modem_run(int fd, int argc, char* argv[]) if(pipe(out) == -1) return 1; - pid_t pid = fork(); + pid_t pid = fork_run(in[0], out[1], out[1], argc, argv);//fork(); - if(pid == 0) {//child +/* if(pid == 0) {//child close (in[1]); close (out[0]); @@ -142,60 +142,66 @@ int modem_run(int fd, int argc, char* argv[]) printf("EXEC ERROR %i: %s\r\n", errno, strerror(errno)); exit(1); - } - else if (pid < 0) {//error + }*/ + if (pid < 0) {//error return 2; } - else {//parent - int buffsize = 128; - char buff[ buffsize ]; - - close (in[0]); - close (out[1]); - - //setup poll to listen for input - struct pollfd fds[2]; - fds[0].fd = out[0]; - fds[0].events = POLLIN; - fds[1].fd = fd; - fds[1].events = POLLIN; - - printf("Forked with PID %i\n", pid); - - while(1) - { - int ret = poll (fds, 2, 100); - usleep(5000); //Wait for data to fully come in, helps prevent truncation of status returns which helps with parsing - if ( fds[0].revents & POLLIN ) { - int cnt = read (out[0], buff, buffsize); - if(cnt) { - //replace +++ to not trigger modem command mode - char *str = strstr(buff, "+++"); - if(str) - str[1] = '*'; - - write(fd, buff, cnt); + + int buffsize = 128; + char buff[ buffsize ]; + + close (in[0]); + close (out[1]); + + //setup poll to listen for input + struct pollfd fds[2]; + fds[0].fd = out[0]; + fds[0].events = POLLIN; + fds[1].fd = fd; + fds[1].events = POLLIN; + + printf("Forked with PID %i\n", pid); + + while(1) + { + int ret = poll (fds, 2, 100); + usleep(5000); //Wait for data to fully come in, helps prevent truncation of status returns which helps with parsing + if ( fds[0].revents & POLLIN ) { + int cnt = read (out[0], buff, buffsize); + if(cnt) { + //replace +++ to not trigger modem command mode + char *str = strstr(buff, "+++"); + if(str) + str[1] = '*'; + + if(try_write(fd, buff, cnt, 100)) { + printf("Consecutive write errors while writing to serial device.\n"); + break; } } - if ( fds[1].revents & POLLIN ) { - int cnt = read (fd, buff, buffsize); - if(cnt) { - //search for modem error message - char *str = strstr(buff, "NO CARRIER"); - if(str){ //Exit if message found - kill(pid,SIGTERM); - break; - } - write(in[1], buff, cnt); + } + if ( fds[1].revents & POLLIN ) { + int cnt = read (fd, buff, buffsize); + if(cnt) { + //search for modem error message + char *str = strstr(buff, "NO CARRIER"); + if(str){ //Exit if message found + kill(pid,SIGTERM); + break; } - } - if(kill(pid,0)) //Check if child is still alive, if not return. - break; + if(try_write(in[1], buff, cnt, 100)) { + printf("Consecutive write errors while writing to STDIN.\n"); + break; + } + } } - printf("Connection closed.\n"); - close(fd); //Auto closes connection - return 0; + if(kill(pid,0)) //Check if child is still alive, if not return. + break; } + + printf("Connection closed.\n"); + close(fd); //Auto closes connection + return 0; } -- cgit v1.2.3