-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore.cpp
230 lines (201 loc) · 6.18 KB
/
core.cpp
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
/*----------------------------------------------------
Name: core.cpp
Platform: Arduino
Autor: SunDro
Date: 2015
Copyright: See license.txt file
Description:
Library of SunDroid v0.2-Nightlie
----------------------------------------------------*/
#include <stdio.h>
#include <Suli.h>
#include "core.h"
GPRS* GPRS::inst;
/*----------------------------------------------------
Create GPRS instance */
GPRS::GPRS(PIN_T tx, PIN_T rx, uint32_t baudRate, const char* apn, const char* userName, const char* passWord):gprsSerial(tx,rx)
{
inst = this;
m_apn = apn;
user = userName;
pass = passWord;
sim900_init(&gprsSerial, -1, baudRate);
}
/*----------------------------------------------------
initialize GPRS module */
int GPRS::GPRS::init(void)
{
if(sim900_check_with_cmd("AT\r\n","OK",DEFAULT_TIMEOUT,CMD)) return -1;
if(sim900_check_with_cmd("AT+CFUN=1\r\n","OK",DEFAULT_TIMEOUT,CMD)) return -1;
if(sim_status()) return -1;
return 0;
}
/*----------------------------------------------------
Check sim card satus */
int GPRS::sim_status(void)
{
char gprsBuffer[32];
int count = 0;
sim900_clean_buffer(gprsBuffer,32);
while(count < 3)
{
sim900_send_cmd("AT+CPIN?\r\n");
sim900_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) break;
count++;
suli_delay_ms(300);
}
if(count == 3) return -1;
return 0;
}
/*----------------------------------------------------
Send message */
int GPRS::send_message(char *num, char *data)
{
char cmd[32];
if(0 != sim900_check_with_cmd("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT,CMD)) return -1;
suli_delay_ms(500);
snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", num);
if(0 != sim900_check_with_cmd(cmd,">",DEFAULT_TIMEOUT,CMD)) return -1;
suli_delay_ms(1000);
sim900_send_cmd(data);
suli_delay_ms(500);
sim900_send_End_Mark();
return 0;
}
/*----------------------------------------------------
Check message is unread or not */
int GPRS::is_message_unread()
{
char gprsBuffer[64];
char *p,*s;
/* List of unread sms */
sim900_send_cmd("AT+CMGL=\"REC UNREAD\",1\r\n");
sim900_clean_buffer(gprsBuffer,64);
sim900_read_buffer(gprsBuffer,64,DEFAULT_TIMEOUT);
if(NULL != (s = strstr(gprsBuffer,"OK"))) return 0;
else
{
if(NULL != (s = strstr(gprsBuffer,"+CMGL:")))
{
p = strstr((char *)gprsBuffer,":");
if (p != NULL)
{
sim900_wait_for_resp("OK", DEFAULT_TIMEOUT, CMD);
return atoi(p+1);
}
}
else return 0;
}
return -1;
}
/*----------------------------------------------------
Read message and change status to readed */
int GPRS::read_message(int smsIndex, char *msg, int length, char *phone, char *date)
{
int i = 0;
char gprsBuffer[100 + length];
char cmd[16];
char *p,*p2,*s;
sim900_check_with_cmd("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
suli_delay_ms(1000);
sprintf(cmd,"AT+CMGR=%d\r\n",smsIndex);
sim900_send_cmd(cmd);
sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
if(NULL != ( s = strstr(gprsBuffer,"+CMGR:")))
{
p = strstr(s,",");
p2 = p + 2;
p = strstr((char *)(p2), "\"");
if (NULL != p)
{
i = 0;
while (p2 < p) phone[i++] = *(p2++);
phone[i] = '\0';
}
p = strstr((char *)(p2),",");
p2 = p + 1;
p = strstr((char *)(p2), ",");
p2 = p + 2;
p = strstr((char *)(p2), "\"");
if (NULL != p)
{
i = 0;
while (p2 < p) date[i++] = *(p2++);
date[i] = '\0';
}
if(NULL != ( s = strstr(s,"\r\n")))
{
i = 0;
p = s + 2;
while((*p != '\r')&&(i < length-1)) msg[i++] = *(p++);
msg[i] = '\0';
}
}
return 0;
}
/*----------------------------------------------------
Read message */
int GPRS::read_message(int smsIndex, char *msg,int length)
{
int i = 0;
char gprsBuffer[100];
char cmd[16];
char *p,*s;
sim900_check_with_cmd("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
suli_delay_ms(1000);
sprintf(cmd,"AT+CMGR=%d\r\n",smsIndex);
sim900_send_cmd(cmd);
sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
if(NULL != ( s = strstr(gprsBuffer,"+CMGR:")))
{
if(NULL != ( s = strstr(s,"\r\n")))
{
p = s + 2;
while((*p != '\r')&&(i < length-1)) msg[i++] = *(p++);
msg[i] = '\0';
}
}
return 0;
}
/*----------------------------------------------------
Delete message */
int GPRS::delete_message(int index)
{
char cmd[16];
snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
if(0 != sim900_check_with_cmd(cmd,"OK",DEFAULT_TIMEOUT,CMD)) return -1;
return 0;
}
/*----------------------------------------------------
Make call */
int GPRS::make_call(char *num)
{
char cmd[24];
if(0 != sim900_check_with_cmd("AT+COLP=1\r\n","OK",DEFAULT_TIMEOUT,CMD)) return -1;
suli_delay_ms(1000);
sprintf(cmd,"ATD%s;\r\n", num);
sim900_send_cmd(cmd);
return 0;
}
/*----------------------------------------------------
Answer to call */
int GPRS::answer(void)
{
sim900_send_cmd("ATA\r\n");
return 0;
}
/*----------------------------------------------------
Check if GPRS module is readable or not */
int GPRS::readable(void)
{
return sim900_check_readable();
}
/*----------------------------------------------------
Check few times if GPRS module is readable or not */
int GPRS::wait_readable(int wait_time)
{
return sim900_wait_readable(wait_time);
}