-
Notifications
You must be signed in to change notification settings - Fork 0
/
modem.c
324 lines (265 loc) · 8.25 KB
/
modem.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
319
320
321
322
323
324
#include "modem.h"
#include "uart.h"
#include "circular_buffer.h"
#include <string.h>
#define MODEM_LOG_LENGTH 96
#define MODEM_SYNC_RETRIALS 10
char modem_buffer[MODEM_LOG_LENGTH];
char modem_log[MODEM_LOG_LENGTH];
size_t modem_ret;
PMCU_Error modem_read(char *buffer) {
PMCU_Error error;
char *cp_buf;
cp_buf = buffer;
if ((error = uart_match_string(UART_A0, "\r\n", MODEM_COMMAND_TIMEOUT)) != PMCU_OK) {
return error;
}
while (1) {
uart_read(UART_A0, (uint8_t *) buffer, MODEM_COMMAND_TIMEOUT);
if (*buffer == '\r') {
break;
}
buffer++;
}
uart_read(UART_A0, (uint8_t *) buffer, MODEM_COMMAND_TIMEOUT);
if (*buffer != '\n') {
return UART_UNEXPECTED_BYTE_ERROR;
}
*buffer = '\0';
strcpy(modem_log, "MODEM AT< ");
strcat(modem_log, cp_buf);
PMCU_log(modem_log);
return PMCU_OK;
}
PMCU_Error modem_read_and_expect(const char *expected) {
modem_read(modem_buffer);
if (strcmp(modem_buffer, expected) != 0) {
return SIM800L_UNEXPECTED_RESPONSE_ERROR;
} else {
return PMCU_OK;
}
}
void modem_execute(const char *cmd) {
size_t i;
i = 0;
while (cmd[i] != '\0') {
uart_write(UART_A0, cmd[i]);
i++;
}
strcpy(modem_log, "MODEM AT> ");
strcat(modem_log, cmd);
PMCU_log(modem_log);
uart_write(UART_A0, '\r');
}
PMCU_Error modem_sync() {
uint8_t tmp;
/*
* IMPORTANT:
* Wait some time before sending any command!
* If during power up SIM800L receives a command will send out
* RDY, Call Ready, SIM Ready messages. We want to skip them!
*/
__delay_cycles(12880000 * 6);
/* Try to exit AT+CIPSEND state if was in */
uart_write(UART_A0, 27);
/* Wait for SIM800L to respond OK to AT command */
while (1) {
modem_execute("AT");
if (uart_read_until_string(UART_A0, "OK\r\n", NULL, NULL, 3) != PMCU_OK) {
continue;
}
break;
}
modem_execute("ATZ");
uart_read_until_string(UART_A0, "OK\r\n", NULL, NULL, MODEM_COMMAND_TIMEOUT);
/* Force ATE0 */
while (1) {
modem_execute("ATE0");
if ((pmcu_error = uart_read_until_string(UART_A0, "\r", NULL, NULL, MODEM_COMMAND_TIMEOUT)) != PMCU_OK) {
continue;
}
if ((pmcu_error = uart_match_string(UART_A0, "\r\nOK\r\n", MODEM_COMMAND_TIMEOUT)) != PMCU_OK) {
continue;
}
break;
}
/* Force AT+CMEE=2 */
while (1) {
modem_execute("AT+CMEE=2");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
continue;
}
break;
}
/* Force AT+CIPSPRT=1 */
while (1) {
modem_execute("AT+CIPSPRT=1");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
continue;
}
break;
}
/* Wait until find an available network */
while (1) {
__delay_cycles(12288000 * 3);
modem_execute("AT+CREG?");
if ((pmcu_error = modem_read(modem_buffer)) != PMCU_OK) {
continue;
}
tmp = strcmp(modem_buffer, "+CREG: 0,1") != 0 && strcmp(modem_buffer, "+CREG: 0,5") != 0;
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
continue;
}
if (tmp) {
continue;
}
break;
}
/* Wait a few seconds to settle */
__delay_cycles(12288000 * 3);
return PMCU_OK;
}
PMCU_Error modem_get_imei(char *imei) {
modem_execute("AT+CGSN");
if ((pmcu_error = modem_read(imei)) != PMCU_OK) {
return pmcu_error;
}
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
return PMCU_OK;
}
PMCU_Error modem_get_location(char *location) {
modem_execute("AT+CIPGSMLOC=1,1");
if ((pmcu_error = modem_read(location)) != PMCU_OK) {
return pmcu_error;
}
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
return PMCU_OK;
}
PMCU_Error modem_gprs_attach() {
/* Force AT+CGATT=1 */
while (1) {
modem_execute("AT+CGATT=1");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
continue;
}
break;
}
// ************************************************** bearer profile creation for at+cipgsmloc
// closes previous opened bearer profile (if any)
modem_execute("AT+SAPBR=0,1");
if ((pmcu_error = modem_read(modem_buffer)) != PMCU_OK) {
return pmcu_error;
}
// sets the connection type to gprs
modem_execute("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
// sets apn to "TM"
modem_execute("AT+SAPBR=3,1,\"APN\",\"TM\"");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
// opens the created bearer profile
modem_execute("AT+SAPBR=1,1");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
// queries the created bearer
modem_execute("AT+SAPBR=2,1");
if ((pmcu_error = modem_read(modem_buffer)) != PMCU_OK) {
return pmcu_error;
}
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
// ************************************************** pdp context creation for tcp/ip network
// deactivates current pdp context
modem_execute("AT+CIPSHUT");
if ((pmcu_error = modem_read_and_expect("SHUT OK")) != PMCU_OK) {
return pmcu_error;
}
// sets single ip connection
modem_execute("AT+CIPMUX=0");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
// starts task, sets apn, empty username and password
modem_execute("AT+CSTT=\"TM\",\"\",\"\"");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
// brings up wireless connection
modem_execute("AT+CIICR");
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
// gets pdp ip address (should be equal to bearer profile's one)
modem_execute("AT+CIFSR");
if ((pmcu_error = modem_read(modem_buffer)) != PMCU_OK) {
return pmcu_error;
}
return PMCU_OK;
}
/*
int modem_gprs_detach() {
// Closes bearer profile
modem_execute("AT+SAPBR=0,1");
modem_read(modem_log, MODEM_LOG_LENGTH);
// Deactivates PDP context
modem_execute("AT+CIPSHUT");
modem_read(modem_log, MODEM_LOG_LENGTH);
// Deattaches GPRS
modem_execute("AT+CGATT=0");
modem_read(modem_log, MODEM_LOG_LENGTH);
return MODEM_OK;
}
*/
PMCU_Error modem_tcp_connect(const char *host, const char *port) {
modem_execute("AT+CIPCLOSE");
if ((pmcu_error = modem_read(modem_buffer)) != PMCU_OK) {
return pmcu_error;
}
// connects to the tcp server
strcpy(modem_buffer, "AT+CIPSTART=\"TCP\",\"");
strcat(modem_buffer, host);
strcat(modem_buffer, "\",\"");
strcat(modem_buffer, port);
strcat(modem_buffer, "\"");
modem_execute(modem_buffer);
if ((pmcu_error = modem_read_and_expect("OK")) != PMCU_OK) {
return pmcu_error;
}
if ((pmcu_error = modem_read_and_expect("CONNECT OK")) != PMCU_OK) {
return pmcu_error;
}
return PMCU_OK;
}
PMCU_Error modem_tcp_send(const uint8_t *buffer, size_t buffer_length) {
modem_execute("AT+CIPSEND");
if ((pmcu_error = uart_read_until_string(UART_A0, "> ", NULL, NULL, MODEM_COMMAND_TIMEOUT)) != PMCU_OK) {
return pmcu_error;
}
uart_write_buffer(UART_A0, buffer, buffer_length);
uart_write(UART_A0, 0x1a); // ctrl+z
if ((pmcu_error = modem_read(modem_buffer)) != PMCU_OK) {
return pmcu_error;
}
if (strcmp(modem_buffer, "SEND OK") != 0 && strcmp(modem_buffer, "OK") != 0) { // for some strange reason could return OK
return SIM800L_UNEXPECTED_RESPONSE_ERROR;
}
return PMCU_OK;
}
inline PMCU_Error modem_tcp_recv(uint8_t *buffer, size_t buffer_length) {
uart_read_buffer(UART_A0, buffer, buffer_length, 0);
return PMCU_OK;
}
PMCU_Error modem_tcp_disconnect() {
modem_execute("AT+CIPCLOSE");
pmcu_error = modem_read(modem_buffer);
return pmcu_error;
}