forked from DFRobot/DFRobot_SIM808
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim808.cpp
198 lines (173 loc) · 5.32 KB
/
sim808.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
/*
* sim808.cpp
* A library for DFRobot's SIM808 GPS/GPRS/GSM Shield
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
*
* @author [Jason](jason.ling@dfrobot.com)
* @version V1.0
* @date 2016-09-23
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "sim808.h"
//SoftwareSerial *serialSIM808 = NULL;
Stream *serialSIM808 = NULL;
/*
void sim808_init(void * uart_device, uint32_t baud)
{
serialSIM808 = (SoftwareSerial*)uart_device;
serialSIM808->begin(baud);
}
*/
void sim808_init(void * uart_device, char num)
{
if(num)
serialSIM808 = (HardwareSerial*)uart_device;
else
serialSIM808 = (SoftwareSerial*)uart_device;
}
int sim808_check_readable()
{
return serialSIM808->available();
}
int sim808_wait_readable (int wait_time)
{
unsigned long timerStart;
int dataLen = 0;
timerStart = millis();
while((unsigned long) (millis() - timerStart) > wait_time * 1000UL) {
delay(500);
dataLen = sim808_check_readable();
if(dataLen > 0){
break;
}
}
return dataLen;
}
void sim808_flush_serial()
{
while(sim808_check_readable()){
serialSIM808->read();
}
}
void sim808_read_buffer(char *buffer, int count, unsigned int timeout, unsigned int chartimeout)
{
int i = 0;
unsigned long timerStart, prevChar;
timerStart = millis();
prevChar = 0;
while(1) {
while (sim808_check_readable()) {
char c = serialSIM808->read();
prevChar = millis();
buffer[i++] = c;
if(i >= count)break;
}
if(i >= count)break;
if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
break;
}
//If interchar Timeout => return FALSE. So we can return sooner from this function. Not DO it if we dont recieve at least one char (prevChar <> 0)
if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) {
break;
}
}
}
void sim808_clean_buffer(char *buffer, int count)
{
for(int i=0; i < count; i++) {
buffer[i] = '\0';
}
}
//HACERR quitar esta funcion ?
void sim808_send_byte(uint8_t data)
{
serialSIM808->write(data);
}
void sim808_send_char(const char c)
{
serialSIM808->write(c);
}
void sim808_send_cmd(const char* cmd)
{
for(uint16_t i=0; i<strlen(cmd); i++)
{
sim808_send_byte(cmd[i]);
}
}
void sim808_send_cmd(const __FlashStringHelper* cmd)
{
int i = 0;
const char *ptr = (const char *) cmd;
while (pgm_read_byte(ptr + i) != 0x00) {
sim808_send_byte(pgm_read_byte(ptr + i++));
}
}
void sim808_send_cmd_P(const char* cmd)
{
while (pgm_read_byte(cmd) != 0x00)
sim808_send_byte(pgm_read_byte(cmd++));
}
boolean sim808_send_AT(void)
{
return sim808_check_with_cmd(F("AT\r\n"),"OK",CMD);
}
void sim808_send_End_Mark(void)
{
sim808_send_byte((char)26);
}
boolean sim808_wait_for_resp(const char* resp, DataType type, unsigned int timeout, unsigned int chartimeout)
{
int len = strlen(resp);
int sum = 0;
unsigned long timerStart, prevChar; //prevChar is the time when the previous Char has been read.
timerStart = millis();
prevChar = 0;
while(1) {
if(sim808_check_readable()) {
char c = serialSIM808->read();
prevChar = millis();
sum = (c==resp[sum]) ? sum+1 : 0;
if(sum == len)break;
}
if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
return false;
}
//If interchar Timeout => return FALSE. So we can return sooner from this function.
if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) {
return false;
}
}
//If is a CMD, we will finish to read buffer.
if(type == CMD) sim808_flush_serial();
return true;
}
boolean sim808_check_with_cmd(const char* cmd, const char *resp, DataType type, unsigned int timeout, unsigned int chartimeout)
{
sim808_send_cmd(cmd);
return sim808_wait_for_resp(resp,type,timeout,chartimeout);
}
//HACERR que tambien la respuesta pueda ser FLASH STRING
boolean sim808_check_with_cmd(const __FlashStringHelper* cmd, const char *resp, DataType type, unsigned int timeout, unsigned int chartimeout)
{
sim808_send_cmd(cmd);
return sim808_wait_for_resp(resp,type,timeout,chartimeout);
}