-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendmodem.c
144 lines (123 loc) · 2.63 KB
/
sendmodem.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <time.h>
#define BUFSIZE (65536+100)
unsigned char readbuf[BUFSIZE];
static struct termios term;
static struct termios gOriginalTTYAttrs;
int InitConn(int speed);
void SendCmd(int fd, void *buf, size_t size)
{
if(write(fd, buf, size) == -1) {
fprintf(stderr, "SendCmd error. %s\n", strerror(errno));
exit(1);
}
}
void SendStrCmd(int fd, char *buf)
{
fprintf(stderr,"Sending command to modem: %s\n",buf);
SendCmd(fd, buf, strlen(buf));
}
int ReadResp(int fd)
{
int len = 0;
struct timeval timeout;
int nfds = fd + 1;
fd_set readfds;
int select_ret;
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
// Wait a second
timeout.tv_sec = 1;
timeout.tv_usec = 500000;
fprintf(stderr,"-");
while (select_ret = select(nfds, &readfds, NULL, NULL, &timeout) > 0)
{
fprintf(stderr,".");
len += read(fd, readbuf + len, BUFSIZE - len);
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
}
if (len > 0) {
fprintf(stderr,"+\n");
}
readbuf[len] = 0;
fprintf(stderr,"%s",readbuf);
return len;
}
int InitConn(int speed)
{
int fd = open("/dev/tty.debug", O_RDWR | O_NOCTTY);
if(fd == -1) {
fprintf(stderr, "%i(%s)\n", errno, strerror(errno));
exit(1);
}
ioctl(fd, TIOCEXCL);
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &term);
gOriginalTTYAttrs = term;
cfmakeraw(&term);
cfsetspeed(&term, speed);
term.c_cflag = CS8 | CLOCAL | CREAD;
term.c_iflag = 0;
term.c_oflag = 0;
term.c_lflag = 0;
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &term);
return fd;
}
void CloseConn(int fd)
{
tcdrain(fd);
tcsetattr(fd, TCSANOW, &gOriginalTTYAttrs);
close(fd);
}
void SendAT(int fd)
{
char cmd[5];
// SendStrCmd(fd, "AT\r");
sprintf(cmd,"AT\r");
SendCmd(fd, cmd, strlen(cmd));
}
void AT(int fd)
{
fprintf(stderr, "Sending command to modem: AT\n");
SendAT(fd);
for (;;) {
if(ReadResp(fd) != 0) {
if(strstr((const char *)readbuf,"OK") != NULL)
{
break;
}
}
SendAT(fd);
}
}
int main(int argc, char **argv)
{
int fd;
char cmd[1024];
if(argc < 2)
{
fprintf(stderr,"usage: %s <at command>\n",argv[0]);
fprintf(stderr,"examples:\t%s \"AT+XSIMSTATE=1\"\n",argv[0]);
fprintf(stderr,"\t\t%s \"AT+XGENDATA\"\n",argv[0]);
fprintf(stderr,"\t\t%s \"AT+CLCK=\\\"SC\\\",2\"\n",argv[0]);
exit(1);
}
fd = InitConn(115200);
AT(fd);
sprintf(cmd,"%s\r",argv[1]);
SendStrCmd(fd,cmd);
ReadResp(fd);
CloseConn(fd);
return 0;
}