forked from itead/ITEADSW_GSM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GSM.h
271 lines (218 loc) · 7.9 KB
/
GSM.h
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
#ifndef GSM_H
#define GSM_H
//#define UNO
#define MEGA
#include <SoftwareSerial.h>
#include <inttypes.h>
#include "WideTextFinder.h"
#define ctrlz 26 //Ascii character for ctr+z. End of a SMS.
#define cr 13 //Ascii character for carriage return.
#define lf 10 //Ascii character for line feed.
#define ctrlz 26 //Ascii character for ctr+z. End of a SMS.
#define cr 13 //Ascii character for carriage return.
#define lf 10 //Ascii character for line feed.
#define GSM_LIB_VERSION 308 // library version X.YY (e.g. 1.00)
#define DEBUG_ON
#ifdef MEGA
#include "HWSerial.h"
#endif
// if defined - debug print is enabled with possibility to print out
// debug texts to the terminal program
//#define DEBUG_PRINT
// if defined - debug print is enabled with possibility to print out
// the data recived from gsm module
//#define DEBUG_GSMRX
// if defined - debug LED is enabled, otherwise debug LED is disabled
//#define DEBUG_LED_ENABLED
// if defined - SMSs are not send(are finished by the character 0x1b
// which causes that SMS are not send)
// by this way it is possible to develop program without paying for the SMSs
//#define DEBUG_SMS_ENABLED
// pins definition
#define GSM_ON 8 // connect GSM Module turn ON to pin 77
#define GSM_RESET 9 // connect GSM Module RESET to pin 35
//#define DTMF_OUTPUT_ENABLE 71 // connect DTMF Output Enable not used
#define DTMF_DATA_VALID 14 // connect DTMF Data Valid to pin 14
#define DTMF_DATA0 72 // connect DTMF Data0 to pin 72
#define DTMF_DATA1 73 // connect DTMF Data1 to pin 73
#define DTMF_DATA2 74 // connect DTMF Data2 to pin 74
#define DTMF_DATA3 75 // connect DTMF Data3 to pin 75
// length for the internal communication buffer
#define COMM_BUF_LEN 200
// some constants for the IsRxFinished() method
#define RX_NOT_STARTED 0
#define RX_ALREADY_STARTED 1
// some constants for the InitParam() method
#define PARAM_SET_0 0
#define PARAM_SET_1 1
// DTMF signal is NOT valid
//#define DTMF_NOT_VALID 0x10
// status bits definition
#define STATUS_NONE 0
#define STATUS_INITIALIZED 1
#define STATUS_REGISTERED 2
#define STATUS_USER_BUTTON_ENABLE 4
// GPRS status
#define CHECK_AND_OPEN 0
#define CLOSE_AND_REOPEN 1
// Common string used
#define str_ok "OK" //string to reduce stack usage
#define str_at "AT" //string to reduce stack usage
// SMS type
// use by method IsSMSPresent()
enum sms_type_enum {
SMS_UNREAD,
SMS_READ,
SMS_ALL,
SMS_LAST_ITEM
};
enum comm_line_status_enum {
// CLS like CommunicationLineStatus
CLS_FREE, // line is free - not used by the communication and can be used
CLS_ATCMD, // line is used by AT commands, includes also time for response
CLS_DATA, // for the future - line is used in the CSD or GPRS communication
CLS_LAST_ITEM
};
enum rx_state_enum {
RX_NOT_FINISHED = 0, // not finished yet
RX_FINISHED, // finished, some character was received
RX_FINISHED_STR_RECV, // finished and expected string received
RX_FINISHED_STR_NOT_RECV, // finished, but expected string not received
RX_TMOUT_ERR, // finished, no character received
// initial communication tmout occurred
RX_LAST_ITEM
};
enum at_resp_enum {
AT_RESP_ERR_NO_RESP = -1, // nothing received
AT_RESP_ERR_DIF_RESP = 0, // response_string is different from the response
AT_RESP_OK = 1, // response_string was included in the response
AT_RESP_LAST_ITEM
};
enum registration_ret_val_enum {
REG_NOT_REGISTERED = 0,
REG_REGISTERED,
REG_NO_RESPONSE,
REG_COMM_LINE_BUSY,
REG_LAST_ITEM
};
enum call_ret_val_enum {
CALL_NONE = 0,
CALL_INCOM_VOICE,
CALL_ACTIVE_VOICE,
CALL_INCOM_VOICE_AUTH,
CALL_INCOM_VOICE_NOT_AUTH,
CALL_INCOM_DATA_AUTH,
CALL_INCOM_DATA_NOT_AUTH,
CALL_ACTIVE_DATA,
CALL_OTHERS,
CALL_NO_RESPONSE,
CALL_COMM_LINE_BUSY,
CALL_LAST_ITEM
};
enum getsms_ret_val_enum {
GETSMS_NO_SMS = 0,
GETSMS_UNREAD_SMS,
GETSMS_READ_SMS,
GETSMS_OTHER_SMS,
GETSMS_NOT_AUTH_SMS,
GETSMS_AUTH_SMS,
GETSMS_LAST_ITEM
};
class GSM {
public:
enum GSM_st_e { ERROR, IDLE, READY, ATTACHED, TCPSERVERWAIT, TCPCONNECTEDSERVER, TCPCONNECTEDCLIENT };
byte comm_buf[COMM_BUF_LEN+1]; // communication buffer +1 for 0x00 termination
void InitParam (byte group);
private:
int _status;
byte comm_line_status;
// global status - bits are used for representation of states
byte module_status;
// variables connected with communication buffer
byte *p_comm_buf; // pointer to the communication buffer
byte comm_buf_len; // num. of characters in the buffer
byte rx_state; // internal state of rx state machine
uint16_t start_reception_tmout; // max tmout for starting reception
uint16_t interchar_tmout; // previous time in msec.
unsigned long prev_time; // previous time in msec.
// last value of speaker volume
byte last_speaker_volume;
char InitSMSMemory(void);
protected:
#ifdef MEGA
HWSerial _cell;
#endif
#ifdef UNO
SoftwareSerial _cell;
#endif
int isIP(const char* cadena);
public:
#ifdef UNO
WideTextFinder _tf;
#endif
inline void setStatus(GSM_st_e status) {
_status = status;
}
GSM();
inline int getStatus() {
return _status;
};
virtual int begin(long baud_rate);
inline void SetCommLineStatus(byte new_status) {
comm_line_status = new_status;
};
inline byte GetCommLineStatus(void) {
return comm_line_status;
};
void RxInit(uint16_t start_comm_tmout, uint16_t max_interchar_tmout);
byte IsRxFinished(void);
byte IsStringReceived(char const *compare_string);
byte WaitResp(uint16_t start_comm_tmout, uint16_t max_interchar_tmout);
byte WaitResp(uint16_t start_comm_tmout, uint16_t max_interchar_tmout,
char const *expected_resp_string);
char SendATCmdWaitResp(char const *AT_cmd_string,
uint16_t start_comm_tmout, uint16_t max_interchar_tmout,
char const *response_string,
byte no_of_attempts);
char SendATCmdWaitResp(const __FlashStringHelper *AT_cmd_string,
uint16_t start_comm_tmout, uint16_t max_interchar_tmout,
char const *response_string,
byte no_of_attempts);
void Echo(byte state);
//-----------------------
// turns off/on the speaker
void SetSpeaker(byte off_on);
// checks if module is registered in the GSM network
// must be called regularly
byte CheckRegistration(void);
// User button methods
inline byte IsUserButtonEnable(void) {
return (module_status & STATUS_USER_BUTTON_ENABLE);
};
inline void DisableUserButton(void) {
module_status &= ~STATUS_USER_BUTTON_ENABLE;
};
inline void EnableUserButton(void) {
module_status |= STATUS_USER_BUTTON_ENABLE;
};
byte IsUserButtonPushed(void);
// Phonebook's methods
char GetPhoneNumber(byte position, char *phone_number);
char WritePhoneNumber(byte position, char *phone_number);
char DelPhoneNumber(byte position);
char ComparePhoneNumber(byte position, char *phone_number);
// returns registration state
byte IsRegistered(void);
// returns whether complete initialization was made
byte IsInitialized(void);
//-----------------------
// debug methods
#ifdef DEBUG_LED_ENABLED
void BlinkDebugLED (byte num_of_blink);
#endif
#ifdef DEBUG_PRINT
void DebugPrint(const char *string_to_print, byte last_debug_print);
void DebugPrint(int number_to_print, byte last_debug_print);
#endif
};
#endif