aboutsummaryrefslogtreecommitdiff
path: root/src/modem.c
blob: 5b0daba77f76c63bb0090f396bfbe061a8419b99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * modem.c
 * (c) 2019, Jonas Gunz, jonasgunz.de
 * <Description>
 * <License>
*/

#include "modem.h"

int modem_accept_wait(int fd)
{
	if(!fd)
		return 1;

	int buffsize = 128;
	char buff[ buffsize + 1];
	buff[buffsize] = '\0';
	
	int ret;		//for retturn values
	struct pollfd fds;	//poll struct
	int cnt = 0;		//read byte counter

	fds.fd = fd;
	fds.events = POLLIN;

	modem_command(fd, _AT_ECHO_OFF, 1000);
	modem_command(fd, _AT_MUTE, 1000);
	modem_command(fd, _AT_RESET_ON_DTR, 1000);

#ifdef _MODEM_WAIT_RING
	while ( 1 ) { //wait for RING
		ret = poll(&fds, 1, 2000); //poll in 2s interval
		usleep(5000);
		if(ret) {
			cnt = read ( fd, buff, 128 );
			if(strstr(buff, "RING"))
				break;
		}
	}
#else
#warning "Wait for RING disabled"
#endif
	printf("Modem RINGING\n");
	int ok = 5;
	int timeout = 60000;
	write (fd, _AT_ANSWER, strlen(_AT_ANSWER)); //Accept incoming call
	cnt = 0;
	while(1)
	{
		ret = poll(&fds, 1, timeout); //Connection timeout 1 minute
		usleep(5000); //Wait for data
		if(!ret)
			break;

		if(cnt >= 128)
			break;

		cnt += read (fd, &buff[cnt - 1], 128 - cnt + 1);

		printf("%s\n", buff);

		if(strstr(buff, "CONNECT")){
			ok = 0;
			break;
		}
		else if(strstr(buff, "NO CARRIER")) //Don't timeout on error
			break;
	}

	if(poll(&fds, 1, 1)) //empty the buffer
			read(fd, &buff, 128);

	return ok;
}

int modem_command(int fd, char* cmd, int timeout_ms)
{
	int buffsize = 128;
	char buff[ buffsize + 1];
	buff[buffsize] = '\0';
	
	int ret;		//for retturn values
	struct pollfd fds;	//poll struct
	int cnt = 0;		//read byte counter

	fds.fd = fd;
	fds.events = POLLIN;

	write(fd, cmd, strlen(cmd) );

	int ok = 1;
	while(1) {
		ret = poll(&fds, 1, timeout_ms);
		usleep(5000); //Wait for data to fully come in
		if (ret)
			cnt += read(fd, &buff[cnt - 1], 128 - cnt + 1);
		else 
			break;

		if(cnt >= 128)
			break;


		if( strstr(buff, "OK") ) {
			ok = 0;
			break;
		}
	}

	if(poll(&fds, 1, 1)) //Empty the buffer
			read(fd, &buff, 128);

	printf("Command %s: %s\n", cmd, ok ? "FAIL" : "OK");

	return ok;
}

int modem_run(int fd, int argc, char* argv[])
{
	int in[2];
	int out[2];

	//1: write, 0: read
	if(pipe(in) == -1)
		return 1;
	
	if(pipe(out) == -1)
		return 1;
	
	pid_t pid = fork_run(in[0], out[1], out[1], argc, argv);//fork();
	
	if (pid < 0) {//error
		return 2;
	}

	int buffsize = 128;
	char buff[ buffsize + 1];
	buff[buffsize] = '\0';

	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;
				}

				if(try_write(in[1], buff, cnt, 100)) {
					printf("Consecutive write errors while writing to STDIN.\n");
					break;
				}
			}
		}

		if(ret < 0)
			break;

		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;
}