/
libsms_modem.c
414 lines (357 loc) · 9.32 KB
/
libsms_modem.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
SMS Server Tools
Copyright (C) 2000 Stefan Frings
This program is free software unless you got it under another license directly
from the author. You can redistribute it and/or modify it under the terms of
the GNU General Public License as published by the Free Software Foundation.
Either version 2 of the License, or (at your option) any later version.
http://www.isis.de/members/~s.frings
mailto:s.frings@mail.isis.de
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/ioctl.h>
#ifdef __sun
#include <sys/filio.h>
#endif
#include "libsms_modem.h"
#include "../../core/dprint.h"
#define MAX_BUF 2048
#define CDS_HDR "\r\n+CDS:"
#define CDS_HDR_LEN (strlen(CDS_HDR))
#define optz(_n,_l) (buf+buf_len-(((_n)+(_l)>buf_len)?buf_len:(_n)+(_l)))
/* global variables */
cds_report cds_report_func;
int put_command( struct modem *mdm, char* cmd, int cmd_len, char* answer,
int max, int timeout,char* exp_end)
{
static char buf[MAX_BUF];
static int buf_len = 0;
char* pos;
char* foo;
char* ptr;
char* to_move;
char* answer_s;
char* answer_e;
int timeoutcounter;
int available;
int status;
int exp_end_len;
int n;
/* check if fd is "clean" for reading */
timeoutcounter = 0;
ioctl(mdm->fd,TIOCMGET,&status);
while (!(status & TIOCM_CTS))
{
usleep( READ_SLEEP );
timeoutcounter++;
ioctl(mdm->fd,TIOCMGET,&status);
if (timeoutcounter>=timeout) {
LM_INFO("Modem is not clear to send\n");
return 0;
}
}
#ifdef SHOW_SMS_MODEM_COMMAND
LM_DBG("-<%d>-->[%.*s] \n",cmd_len,cmd_len,cmd);
#endif
/* send the command to the modem */
if (write(mdm->fd,cmd,cmd_len)<0) {
LM_ERR("write error: %s\n", strerror(errno));
}
tcdrain(mdm->fd);
/* read from the modem */
exp_end_len = exp_end?strlen(exp_end):0;
answer_s = buf;
answer_e = 0;
to_move = 0;
do
{
/* try to read some bytes */
ioctl(mdm->fd,FIONREAD,&available);
/* how many bytes are available to read? */
if (available<1) /* if 0 then wait a little bit and retry */
{
usleep( READ_SLEEP );
timeoutcounter++;
ioctl(mdm->fd,FIONREAD,&available);
}
if (available>0)
{
/* How many bytes do I want to read maximum?
Not more than buffer size. And how many bytes are available? */
n = (available>MAX_BUF-buf_len-1)?MAX_BUF-buf_len-1:available;
/* read data */
n = read( mdm->fd, buf+buf_len, n);
if (n<0) {
LM_ERR("error reading from modem: %s\n", strerror(errno));
goto error;
}
if (n) {
buf_len += n;
buf[buf_len] = 0;
//LM_DBG("read = [%s]\n",buf+buf_len-n);
foo = pos = 0;
if ( (!exp_end && ((pos=strstr(optz(n,4),"OK\r\n"))
|| (foo=strstr(optz(n,5),"ERROR"))))
|| (exp_end && (pos=strstr(optz(n,exp_end_len),exp_end)) )) {
/* we found the end */
//LM_DBG("end found = %s\n",
// (foo?"ERROR":(exp_end?exp_end:"OK")));
/* for ERROR we still have to read EOL */
if (!foo || (foo=strstr(foo+5,"\r\n"))) {
answer_e = foo?foo+2:(pos+(exp_end?exp_end_len:4));
timeoutcounter = timeout;
}
}
}
}
/* repeat until timeout */
}while (timeoutcounter<timeout);
if (!answer_e)
answer_e = buf+buf_len;
/* CDS report is activ? */
if (sms_report_type==CDS_REPORT) {
to_move = 0;
ptr = buf;
/* do we have a CDS reply inside? */
while ( (pos=strstr(ptr,CDS_HDR)) ) {
if (pos!=ptr) { /* here we have the command response */
answer_s = ptr;
}
/* look for the end of CDS response */
ptr = pos+CDS_HDR_LEN;
for( n=0 ; n<2&&(foo=strstr(ptr,"\r\n")) ; ptr=foo+2,n++ );
if (n<2) { /* we haven't read the entire CDS response */
LM_DBG("CDS end not found!\n");
to_move = pos;
ptr = buf + buf_len;
}else{
/* process the CDS */
LM_DBG("CDS=[%.*s]\n",(int)(ptr-pos),pos);
cds_report_func(mdm,pos,ptr-pos);
}
}
if ((*ptr)) {
answer_s = ptr;
ptr = answer_e;
}
if (ptr!=buf+buf_len)
to_move = ptr;
}
/* copy the response in answer buffer - as much as fits */
if (answer && max) {
n = max-1<answer_e-answer_s?max-1:answer_e-answer_s;
memcpy(answer,answer_s,n);
answer[n] = 0;
}
/* shift left the remaining data into the buffer - if needs */
if (sms_report_type==CDS_REPORT && to_move) {
buf_len = buf_len - (to_move-buf);
memcpy(buf,to_move,buf_len);
buf[buf_len] = 0;
LM_DBG("buffer shifted left=[%d][%s]\n",buf_len,buf);
} else {
buf_len = 0;
}
#ifdef SHOW_SMS_MODEM_COMMAND
LM_DBG("<-[%s] \n",answer);
#endif
return answer_e-answer_s;
error:
return 0;
}
/* setup serial port */
int setmodemparams( struct modem *mdm )
{
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = mdm->baudrate | CRTSCTS | CS8 | CLOCAL | CREAD | O_NDELAY;
//uncomment next line to disable hardware handshake
//newtio.c_cflag &= ~CRTSCTS;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 1;
newtio.c_cc[VMIN] = 0;
tcflush(mdm->fd, TCIOFLUSH);
tcsetattr(mdm->fd,TCSANOW,&newtio);
return 0;
}
int initmodem(struct modem *mdm, cds_report cds_report_f)
{
char command[MAX_CHAR_BUF+12];
char answer[100];
int retries=0;
int success=0;
int clen=0;
int n;
LM_INFO("init modem %s on %s.\n",mdm->name,mdm->device);
if (mdm->pin[0]) {
/* Checking if modem needs PIN */
put_command(mdm,"AT+CPIN?\r",9,answer,sizeof(answer),50,0);
if (strstr(answer,"+CPIN: SIM PIN")) {
LM_INFO("Modem needs PIN, entering PIN...\n");
clen=sprintf(command,"AT+CPIN=\"%s\"\r",mdm->pin);
put_command(mdm,command,clen,answer,sizeof(answer),100,0);
put_command(mdm,"AT+CPIN?\r",9,answer,sizeof(answer),50,0);
if (!strstr(answer,"+CPIN: READY")) {
if (strstr(answer,"+CPIN: SIM PIN")) {
LM_ERR("Modem did not accept this PIN\n");
goto error;
} else if (strstr(answer,"+CPIN: SIM PUK")) {
LM_ERR("YourPIN is locked! Unlock it manually!\n");
goto error;
} else {
goto error;
}
}
LM_INFO("PIN Ready!\n");
sleep(5);
}
}
if (mdm->mode==MODE_DIGICOM)
success=1;
else {
LM_INFO("Checking if Modem is registered to the network\n");
success=0;
retries=0;
do
{
retries++;
put_command(mdm,"AT+CREG?\r",9,answer,sizeof(answer),100,0);
if (strchr(answer,'1') )
{
LM_INFO("Modem is registered to the network\n");
success=1;
} else if (strchr(answer,'2')) {
// added by bogdan
LM_WARN("Modems seems to try to reach the network!"
" Let's wait a little bit\n");
retries--;
sleep(2);
} else if (strchr(answer,'5')) {
// added by Thomas Stoeckel
LM_INFO("Modem is registered to a roaming partner network\n");
success=1;
} else if (strstr(answer,"ERROR")) {
LM_WARN("Ignoring that modem does not support +CREG command.\n");
success=1;
} else {
LM_NOTICE("Waiting 2 sec. before retrying\n");
sleep(2);
}
}while ((success==0)&&(retries<20));
}
if (success==0) {
LM_ERR("Modem is not registered to the network\n");
goto error;
}
for( n=0 ; n<2+2*(sms_report_type==CDS_REPORT) ; n++) {
/* build the command */
switch (n) {
case 0:
strcpy(command,"AT+CMGF=0\r");
command[8]+=(mdm->mode==MODE_ASCII || mdm->mode==MODE_DIGICOM);
clen = 10;
break;
case 1:
strcpy(command,"AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0\r");
clen = 33;
break;
case 2:
strcpy(command,"AT+CSMP=49,167,0,241\r");
clen = 21;
break;
case 3:
strcpy(command,"AT+CNMI=1,1,0,1,0\r");
clen = 18;
break;
}
/* send it to modem */
retries=0;
success=0;
do {
retries++;
/*querying the modem*/
put_command(mdm,command,clen,answer,sizeof(answer),100,0);
/*dealing with the answer*/
if (strstr(answer,"ERROR")) {
LM_NOTICE("Waiting 1 sec. before to retrying\n");
sleep(1);
} else
success=1;
}while ((success==0)&&(retries<3));
/* have we succeeded? */
if (success==0) {
LM_ERR("cmd [%.*s] returned ERROR\n", clen-1,command);
goto error;
}
} /* end for */
if ( sms_report_type==CDS_REPORT && !cds_report_f) {
LM_ERR("no CDS_REPORT function given\n");
goto error;
}
cds_report_func = cds_report_f;
if (mdm->smsc[0]) {
LM_INFO("Changing SMSC to \"%s\"\n",mdm->smsc);
setsmsc(mdm,mdm->smsc);
}
return 0;
error:
return -1;
}
int checkmodem(struct modem *mdm)
{
char answer[500];
/* Checking if modem needs PIN */
put_command(mdm,"AT+CPIN?\r",9,answer,sizeof(answer),50,0);
if (!strstr(answer,"+CPIN: READY")) {
LM_WARN("modem wants the PIN again!\n");
goto reinit;
}
if (mdm->mode!=MODE_DIGICOM) {
put_command(mdm,"AT+CREG?\r",9,answer,sizeof(answer),100,0);
if (!strchr(answer,'1') ) {
LM_WARN("Modem is not registered to the network\n");
goto reinit;
}
}
return 1;
reinit:
LM_WARN("re -init the modem!!\n");
initmodem(mdm,cds_report_func);
return -1;
}
int setsmsc(struct modem *mdm, char *smsc)
{
char command[100];
char answer[50];
int clen;
if (smsc && smsc[0]) {
clen=sprintf(command,"AT+CSCA=\"+%s\"\r",smsc);
put_command(mdm,command,clen,answer,sizeof(answer),50,0);
}
return 0;
}
int openmodem( struct modem *mdm)
{
mdm->fd = open(mdm->device, O_RDWR | O_NOCTTY );
if (mdm->fd <0)
return -1;
tcgetattr(mdm->fd,&(mdm->oldtio));
return 0;
}
int closemodem(struct modem *mdm)
{
tcsetattr(mdm->fd,TCSANOW,&(mdm->oldtio));
close(mdm->fd);
return 0;
}