-
Notifications
You must be signed in to change notification settings - Fork 0
/
SARA_R4_modem.h
277 lines (243 loc) · 9.16 KB
/
SARA_R4_modem.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
272
273
274
275
276
277
#include "ChallengerLTE.h"
#include "SerialDebug.h"
#include "StringStuff.h"
#include "Arduino_secrets.h"
#define RESPONSE_BUFFER_SIZE (500)
class SARA_R4_Class
{
public:
int RawReceiveBufferIndex = 0;
char RawReceiveBuffer[RESPONSE_BUFFER_SIZE];
char ResponseBuffer[RESPONSE_BUFFER_SIZE];
char ExpectedResponsePrefixBuffer[RESPONSE_BUFFER_SIZE];
int CMD_STATE = 0;
int CMD_ID = 0;
bool CMD_OK = 0;
bool CMD_ERROR = 0;
void AT_SET(const char command[150])
{
//set typically only returns OK
CMD_STATE = 1;
++CMD_ID;
SARA_SERIAL_PORT.printf("AT%s\r",command);
CMD_OK = 0;
CMD_ERROR = 0;
ExpectedResponsePrefixBuffer[0] = 0;
}
void AT_READ(const char command[150])
{
//read means you want some kind of response back
//often but not always the command has a ? at the end of it.
CMD_STATE = 1;
++CMD_ID;
SARA_SERIAL_PORT.printf("AT%s\r",command);
CMD_OK = 0;
CMD_ERROR = 0;
if (command[0] == '+') {
ExpectedResponsePrefixBuffer[0] = '+';
size_t x = 1;
char c = 0;
do {
c = command[x];
if (isalpha(c)) {
ExpectedResponsePrefixBuffer[x] = c;
} else {
ExpectedResponsePrefixBuffer[x] = 0;
break; //do-while
}
++x;
} while (c != 0);
} else {
ExpectedResponsePrefixBuffer[0] = 0;
}
}
void HOLOGRAM(int TCPsocket, const char message[150], const char topics[150])
{
//requires very specific JSON formatting for unique key, message data, and topic.
// //refer to HOLOGRAM docs for how to bracket multiple topics, if needed.
CMD_STATE = 1;
++CMD_ID;
CMD_OK = 0;
CMD_ERROR = 0;
strcpy(ExpectedResponsePrefixBuffer, "+USOWR"); //includes the null char
size_t asciilen = 0;
char ascii_str[300];
char hex_str[300];
char key[] = SECRET_HOLOGRAM_DEVICE_KEY;
snprintf(ascii_str, 300, "{\"k\":\"%s\",\"d\":\"%s\",\"t\":\"%s\"}", key, message, topics);
//Serial.printf("%s\n",ascii_str);
//A terminating null character is automatically appended after the content written by snprintf
asciilen = base16encode(hex_str, sizeof(hex_str), ascii_str, sizeof(ascii_str));
SARA_SERIAL_PORT.printf("AT+USOWR=%1i,%zu,\"%s\"\r", TCPsocket, asciilen, hex_str);
//Serial.printf("AT+USOWR=%1i,%zu,\"%s\"\r\n", TCPsocket, asciilen, hex_str);
}
// SARA Response/Result Listener
uint32_t WHITESPACE_CMDSTART = 0; //for curiosity sake
uint32_t RESPONSE_OVERFLOWS = 0;
uint32_t AT_COMMAND_ECHOES = 0;
int listenerState = 0;
void ResponseListener(bool modem_quit)
{
int TERM_byte; //it is not lost on me that an int is being used to store a byte.
int SARA_byte;
int SARA_delta;
switch(listenerState) {
case 0:
//not actively dealing with a command.
if (CMD_STATE == 1) {
//command is being sent to SARA
memset(ResponseBuffer, 0, sizeof(ResponseBuffer));
listenerState = 1;
} else if (SARA_SERIAL_PORT.available() > 0) {
//SARA is saying something but we didn't ask.
//just echo to terminal.
while ( (SARA_delta=SARA_SERIAL_PORT.available()) > 0) {
SARA_byte = SARA_SERIAL_PORT.read();
TERM_ECHO(SARA_byte);
}
} else if (Serial.available() > 0) {
//need to kick over to terminal send.
listenerState = 10;
} else if (modem_quit) {
TERM_P("Quitting because Button C was pressed\n");
SARA_SERIAL_PORT.printf("AT+CPWROFF\r");
}
break;
case 1:
//(re)initialize raw receive buffer.
//we are expecting an OK or an ERROR.
//but we can still get other complications like URC or IRC codes from SARA.
WHITESPACE_CMDSTART = 0;
RawReceiveBufferIndex = 0;
memset(RawReceiveBuffer, 0, sizeof(RawReceiveBuffer));
listenerState = 2;
//no break; FALL THRU INTENTIONAL
case 2:
//first thing is get rid of the beginning control codes and spaces and actually get an alphanumeric code
//but the timing of all of this is asynchronous so we could get nothing for a while.
while ( (SARA_delta=SARA_SERIAL_PORT.available()) > 0) {
SARA_byte = SARA_SERIAL_PORT.read();
//TERM_ECHO(SARA_byte);
if (SARA_byte > 32) {
RawReceiveBuffer[RawReceiveBufferIndex++] = SARA_byte;
listenerState = 3;
break; //while
} else {
++WHITESPACE_CMDSTART;
}
}
break;
case 3:
while ( (SARA_delta=SARA_SERIAL_PORT.available()) > 0) {
SARA_byte = SARA_SERIAL_PORT.read();
//TERM_ECHO(SARA_byte);
if (SARA_byte != 13) {
if (SARA_byte == 10) {
//read of one line is complete.
RawReceiveBuffer[RawReceiveBufferIndex] = 0;
listenerState = 4;
break; //while
} else {
RawReceiveBuffer[RawReceiveBufferIndex++] = SARA_byte;
if (RawReceiveBufferIndex >= RESPONSE_BUFFER_SIZE) {
RawReceiveBufferIndex = RESPONSE_BUFFER_SIZE-1;
RawReceiveBuffer[RawReceiveBufferIndex] = 0;
listenerState = 9;
break; //while
}
}
}
}
break;
case 4:
if (doesCsvStringBeginWith("OK", RawReceiveBuffer, sizeof(RawReceiveBuffer))) {
//(strstr(RawReceiveBuffer, "OK") != NULL)
//check for OK
CMD_OK = 1;
CMD_STATE = 2;
listenerState = 0;
} else if (strstr(RawReceiveBuffer, "ERROR") != NULL) {
//check for ERROR anywhere in the response.
//this is about 99% solved. this could fail on really bizarre edge cases.
CMD_ERROR = 1;
CMD_STATE = 3;
listenerState = 0;
TERM_P(RawReceiveBuffer);
} else if (RawReceiveBuffer[0] == '+') {
//check for starting with +, which is a response to a question, URC or IRC.
//is this the response we were expecting?
if (doesCsvStringBeginWith(ExpectedResponsePrefixBuffer, RawReceiveBuffer, sizeof(RawReceiveBuffer))) {
size_t x = 0;
do {
ResponseBuffer[x] = RawReceiveBuffer[x];
} while (ResponseBuffer[x++] != 0);
//TERM_P("Copied Expected Response from AT Read Command");
} else {
TERM_P("Unexpected Response to AT Read Command: ");
TERM_P(RawReceiveBuffer);
//TODO: probably need to process this
}
listenerState = 1;
} else if (doesCsvStringBeginWith("AT", RawReceiveBuffer, sizeof(RawReceiveBuffer))) {
//old (strstr(RawReceiveBuffer, "AT") != NULL)
//check for starting with AT, meaning command is being echoed back. Do nothing with it. go back for another line.
++AT_COMMAND_ECHOES;
listenerState = 1;
} else {
//LOL I dunno
//ATI does whatever but ends with OK
listenerState = 1;
}
break;
case 9:
++RESPONSE_OVERFLOWS;
CMD_ERROR = 1;
CMD_STATE = 4;
listenerState = 0;
break;
case 10:
TERM_byte = -1;
while (Serial.available() > 0) {
TERM_byte = Serial.read();
SARA_SERIAL_PORT.write(TERM_byte);
//Serial.print(TERM_byte, HEX);
//Serial.print(" ");
//a carriage return marks the end of the message from the terminal
if (TERM_byte == 13) {
listenerState = 0;
break;
}
}
break;
}
}
#define SYSTEM_MNO_PROF 100
#define DEFAULT_PS_ENABLED 0
void modem_init()
{
// Power on modem
SARA_SERIAL_PORT.setFIFOSize(200);
if (!Challenger2040LTE.doPowerOn()) {
TERM_P("Failed to start the modem properly !");
while(1);
} else {
TERM_P("Modem started !");
delay(1000);
// Read current modem MNO profile
int mnoprof = Challenger2040LTE.getMNOProfile();
if (mnoprof < 0) {
TERM_P("Failed setting reading the MNO profile !");
while(1);
}
// if current modem MNO profile differs from system default change it
if (mnoprof != SYSTEM_MNO_PROF) {
TERM_P("Current modem MNO profile differs from system default !");
if(Challenger2040LTE.setMNOProfile(100)) {
TERM_P("Suceeded setting new MNO profile !");
} else {
TERM_P("Failed setting new MNO profile !");
}
}
}
}
};