This repository has been archived by the owner on Dec 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModem_SIM900.c
318 lines (287 loc) · 7.43 KB
/
Modem_SIM900.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "net_modem.h"
/* Modem driver states */
enum Modem_State {
Modem_Idle,
Modem_Error,
Modem_Ready,
Modem_Init,
Modem_Listen,
Modem_Dial,
Modem_Online,
Modem_Hangup
};
/* Modem driver local variables */
static struct {
Modem_Request_t callback;
enum Modem_State state;
uint8_t step;
bool dial_mode;
const char *init_string;
const char *dialnum;
} drv;
/**
\fn MODEM_STATUS Modem_Initialize (Modem_Request_t cb_request, const char *init_string))
\brief Initialize Modem Device.
\param[in] cb_request Pointer to \ref Modem_Request
\param[in] init_string Pointer to modem initialization string
\return execution status \ref MODEM_STATUS
*/
static MODEM_STATUS Initialize (Modem_Request_t cb_request, const char *init_string) {
if (cb_request == NULL) {
return MODEM_ERROR;
}
drv.callback = cb_request;
drv.init_string = init_string;
drv.state = Modem_Idle;
drv.step = 0;
drv.dialnum = NULL;
return MODEM_OK;
}
/**
\fn MODEM_STATUS Modem_Uninitialize (void)
\brief De-initialize Modem Device.
\return execution status \ref MODEM_STATUS
*/
static MODEM_STATUS Uninitialize (void) {
drv.state = Modem_Idle;
return MODEM_OK;
}
/**
\fn MODEM_STATUS Modem_Listen (void)
\brief Start Modem Device listening mode.
\return execution status \ref MODEM_STATUS
*/
static MODEM_STATUS Listen (void) {
if (drv.state > Modem_Ready) {
return MODEM_ERROR;
}
drv.dialnum = NULL;
drv.dial_mode = false;
drv.state = Modem_Init;
drv.step = 0;
drv.callback (NULL, NULL, 100, 0);
return MODEM_OK;
}
/**
\fn MODEM_STATUS Modem_Dial (const char *dial_num)
\brief Start dialing target number.
\param[in] dial_num Target number, a null-terminated string
\return execution status \ref MODEM_STATUS
*/
static MODEM_STATUS Dial (const char *dial_num) {
if ((dial_num == NULL) || drv.state > Modem_Ready) {
return MODEM_ERROR;
}
drv.dialnum = dial_num;
drv.dial_mode = true;
drv.state = Modem_Init;
drv.step = 0;
drv.callback (NULL, NULL, 100, 0);
return MODEM_OK;
}
/**
\fn MODEM_STATUS Modem_Hangup (void)
\brief Hangup the line.
\return execution status \ref MODEM_STATUS
*/
static MODEM_STATUS Hangup (void) {
if (drv.state <= Modem_Ready) {
return MODEM_OK;
}
drv.state = Modem_Hangup;
drv.step = 0;
/* Wait 3 seconds */
drv.callback (NULL, NULL, 3000, 0);
return MODEM_OK;
}
/**
\fn bool Modem_IsOnline (void)
\brief Check if modem is online.
\return - \b true Modem is Online
- \b false Modem is Offline
*/
static bool IsOnline (void) {
if (drv.state == Modem_Online) {
return true;
}
return false;
}
/**
\fn void Modem_Notify (MODEM_EVENT event)
\brief Notify the Modem Driver of completion event.
\param[in] event notification event \ref MODEM_EVENT
\return None.
*/
static void Notify (MODEM_EVENT event) {
switch (drv.state) {
case Modem_Init:
switch (drv.step) {
case 0:
drv.callback("AT\r\n", "OK", 2000, 2);
drv.step = 1;
break;
case 1:
if (event != MODEM_EVENT_OK) {
/* Modem handshake failed */
drv.state = Modem_Error;
break;
}
/* 'ATE0' = No echo, return word responses */
drv.callback ("ATE0\r\n", "OK", 2000, 2);
drv.step = 2;
break;
case 2:
if (event != MODEM_EVENT_OK) {
/* ATE0 command failed */
drv.state = Modem_Error;
break;
}
drv.callback ("ATZ\r\n", "OK", 5000, 2);
drv.step = 3;
break;
case 3:
if (event != MODEM_EVENT_OK) {
/* Modem reset failed */
drv.state = Modem_Error;
break;
}
if (drv.init_string != NULL) {
/* Send custom init string */
drv.callback (drv.init_string, "OK", 3000, 2);
} else {
drv.callback (NULL, NULL, 100, 0);
}
drv.step = 4;
break;
case 4:
if (event != MODEM_EVENT_OK && drv.init_string != NULL) {
/* Custom init string failed */
drv.state = Modem_Error;
break;
}
drv.state = drv.dial_mode ? Modem_Dial : Modem_Listen;
drv.callback (NULL, NULL, 100, 0);
drv.step = 0;
break;
}
break;
case Modem_Dial:
switch (drv.step) {
case 0:
drv.callback ("ATD", NULL, 0, 0);
drv.callback (drv.dialnum, NULL, 0, 0);
drv.callback ("\r\n", "CONNECT", 30000, 0);
drv.step = 1;
break;
case 1:
if (event != MODEM_EVENT_OK) {
/* Connect timeout error */
drv.callback ("ATH\r\n", "OK", 2000, 2);
drv.step = 2;
break;
}
drv.callback (NULL, "NO CARRIER", 0, 0);
drv.state = Modem_Online;
break;
case 2:
if (event != MODEM_EVENT_OK) {
/* 'ATH' command failed */
drv.state = Modem_Error;
break;
}
drv.state = Modem_Ready;
break;
}
break;
case Modem_Listen:
switch (drv.step) {
case 0:
/* Wait for 'RING' response */
drv.callback (NULL, "RING", 0, 0);
drv.step = 1;
break;
case 1:
/* 'RING' response received */
drv.callback ("ATA\r\n", "CONNECT", 30000, 0);
drv.step = 2;
break;
case 2:
if (event != MODEM_EVENT_OK) {
/* Connect timeout error */
drv.callback ("ATH\r\n", "OK", 2000, 2);
drv.step = 3;
break;
}
drv.callback (NULL, "NO CARRIER", 0, 0);
drv.state = Modem_Online;
break;
case 3:
if (event != MODEM_EVENT_OK) {
/* 'ATH' command failed, reset modem */
drv.state = Modem_Init;
}
/* 'ATH' command ok */
drv.callback (NULL, NULL, 100, 0);
drv.step = 0;
break;
}
break;
case Modem_Online:
/* 'NO CARRIER' response received */
if (drv.dial_mode) {
drv.state = Modem_Ready;
break;
}
drv.state = Modem_Listen;
drv.callback (NULL, NULL, 100, 0);
drv.step = 0;
break;
case Modem_Hangup:
switch (drv.step) {
case 0:
/* Delay of 3 sec has expired */
drv.callback ("+++", "", 2000, 0);
drv.step = 1;
break;
case 1:
drv.callback ("ATH\r\n", "OK\r\n", 3000, 2);
drv.step = 2;
break;
case 2:
if (event != MODEM_EVENT_OK) {
/* 'ATH' command failed */
if (drv.dial_mode) {
drv.state = Modem_Error;
break;
}
/* Reset modem for listen mode */
drv.state = Modem_Init;
drv.callback (NULL, NULL, 100, 0);
drv.step = 0;
break;
}
/* 'ATH' command ok */
if (drv.dial_mode) {
drv.state = Modem_Ready;
break;
}
drv.state = Modem_Listen;
drv.callback (NULL, NULL, 100, 0);
drv.step = 0;
break;
}
break;
default:
break;
}
}
/* Modem Driver Control Block */
DRIVER_MODEM Driver_MODEM = {
Initialize,
Uninitialize,
Listen,
Dial,
Hangup,
IsOnline,
Notify
};