-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_sms_receive.c
143 lines (128 loc) · 3.1 KB
/
client_sms_receive.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<fcntl.h>
#include<unistd.h>
#include<limits.h>
#include<signal.h>
#include"client_protocol.h"
/*
* We are fetching SMS the simple way - asking FONA for all messages and then
* deleting the ones recently read. The application will have to handle the
* messages once read (like print them out).
* I do this instead of subscribing to +CMTI because I do not want to miss one
* due to client downtime. Fetching messages which may have come in during
* downtime would be the same - by simply asking for all outstanding unread
* messages.
*/
static int in_fd, out_fd;
static char *buf;
static char *qbuf;
static int query = 0;
void stop_sms_query();
int fetch_sms();
void stop_sms_query() {
query = 0;
}
int fetch_sms() {
char *temp;
char *found, *tofree;
int messages = 0;
int i, j;
if(buf != NULL) {
free(buf);
buf = NULL;
}
if(qbuf != NULL) {
free(qbuf);
qbuf = NULL;
}
buf = calloc(PIPE_BUF, sizeof(char));
qbuf = calloc(PIPE_BUF, sizeof(char));
/* Set text mode */
write(out_fd, "AT+CMGF=1\r\n", 11);
sleep(1);
read(in_fd, buf, PIPE_BUF);
if(strstr(buf, "OK") == NULL) {
fprintf(stderr, "[Error] Couldn't set text mode (%s)\n", buf);
return 1;
}
/* Get number of messages in storage */
write(out_fd, "AT+CPMS?\r\n", 10);
read(in_fd, buf, PIPE_BUF);
temp = strndup(buf, strlen(buf));
tofree = temp;
found = strsep(&temp, ",");
found = strsep(&temp, ",");
if(found != NULL) {
messages = atoi(found);
for(i=0; i<messages; i++) {
memset(buf, 0, PIPE_BUF);
sprintf(qbuf, "AT+CMGR=%d\r\n", i+1);
write(out_fd, qbuf, strlen(qbuf));
sleep(1);
read(in_fd, buf, PIPE_BUF);
if(strstr(buf, "CMGR") != NULL) {
free(tofree);
temp = strndup(buf, strlen(buf));
tofree = temp;
// Contact number is #4
for(j=0; j<4; j++) {
found = strsep(&temp, "\"");
}
printf("Contact: %s\n", found);
// Date/Time is #8
for(j=0; j<4; j++) {
found = strsep(&temp, "\"");
}
printf("Time: %s\n", found);
// Message is #9
found = strsep(&temp, "\"");
printf("%s\n", found);
// Delete the message from FONA
memset(qbuf, 0, PIPE_BUF);
memset(buf, 0, PIPE_BUF);
sprintf(qbuf, "AT+CMGD=%d\r\n", i+1);
write(out_fd, qbuf, strlen(qbuf));
sleep(1);
read(in_fd, buf, PIPE_BUF);
if(strstr(buf, "OK") == NULL) {
fprintf(stderr, "[Error] Deleting message from FONA\n");
}
}
}
}
memset (buf, 0, PIPE_BUF);
/* Set PDU mode */
write(out_fd, "AT+CMGF=0\r\n", 11);
read(in_fd, buf, PIPE_BUF);
if(strstr(buf, "OK") == NULL) {
fprintf(stderr, "[Error] Couldn't set pdu mode\n");
return 1;
}
free(tofree);
free(qbuf);
free(buf);
buf = NULL;
qbuf = NULL;
return 0;
}
int main() {
signal(SIGINT, stop_sms_query);
init_fone_client();
query = 1;
while(1) {
if(send_hello(&in_fd, &out_fd) != 0) {
fprintf(stderr, "[Error] Failed to converse with server\n");
break;
}
fetch_sms();
if(send_finish(&in_fd, &out_fd) != 0) {
fprintf(stderr, "[Error] Failed to finish conversation with server\n");
break;
}
if(query == 0) {
break;
}
sleep(10);
}
}