forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModem.cpp
303 lines (263 loc) · 8.89 KB
/
Modem.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
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
#include "Modem.h"
#define RESULT_OK "OK\r\n"
#define RESULT_ERROR "ERROR\r\n"
#define RESULT_DATA "DATA\r\n"
using namespace std;
/* -------------------------------------------------------------------------- */
ModemClass::ModemClass(int tx, int rx) : beginned(false), delete_serial(false), _timeout(MODEM_TIMEOUT), trim_results(true), read_by_size(false) {
/* -------------------------------------------------------------------------- */
_serial = new UART(tx,rx);
}
/* -------------------------------------------------------------------------- */
ModemClass::ModemClass(UART * serial) : beginned(false) , delete_serial(true) , _serial(serial), _timeout(MODEM_TIMEOUT), trim_results(true), read_by_size(false) {
/* -------------------------------------------------------------------------- */
}
/* -------------------------------------------------------------------------- */
ModemClass::~ModemClass() {
/* -------------------------------------------------------------------------- */
if(_serial != nullptr && !delete_serial){
delete _serial;
_serial = nullptr;
}
}
/* -------------------------------------------------------------------------- */
void ModemClass::begin(int badurate){
/* -------------------------------------------------------------------------- */
if(_serial != nullptr && !beginned) {
_serial->begin(badurate);
beginned = true;
string res = "";
_serial->flush();
modem.write(string(PROMPT(_SOFTRESETWIFI)),res, "%s" , CMD(_SOFTRESETWIFI));
}
}
/* -------------------------------------------------------------------------- */
void ModemClass::end(){
/* -------------------------------------------------------------------------- */
_serial->end();
}
/* -------------------------------------------------------------------------- */
bool ModemClass::passthrough(const uint8_t *data, size_t size) {
/* -------------------------------------------------------------------------- */
_serial->write(data,size);
bool res = false;
bool found = false;
string data_res = "";
unsigned long start_time = millis();
while(millis() - start_time < _timeout && !found){
while(_serial->available()){
char c = _serial->read();
data_res += c;
if(string::npos != data_res.rfind(RESULT_OK)){
found = true;
res = true;
break;
}
else if (string::npos != data_res.rfind(RESULT_ERROR)) {
found = true;
res = false;
break;
}
}
}
if(_serial_debug && _debug_level >= 2) {
_serial_debug->print(" ANSWER (passthrough): ");
_serial_debug->println(data_res.c_str());
if(res) {
_serial_debug->println(" Result: OK");
}
else {
_serial_debug->println(" Result: FAILED");
}
}
return res;
}
/* -------------------------------------------------------------------------- */
void ModemClass::write_nowait(const string &cmd, string &str, const char * fmt, ...) {
/* -------------------------------------------------------------------------- */
va_list va;
va_start (va, fmt);
vsnprintf((char *)tx_buff, MAX_BUFF_SIZE, fmt, va);
va_end (va);
if(_serial_debug && _debug_level >= 2) {
_serial_debug->print("REQUEST (passthrough): ");
_serial_debug->write(tx_buff,strlen((char *)tx_buff));
_serial_debug->println();
}
_serial->write(tx_buff,strlen((char *)tx_buff));
return;
}
/* -------------------------------------------------------------------------- */
bool ModemClass::write(const string &prompt, string &data_res, const char * fmt, ...){
/* -------------------------------------------------------------------------- */
data_res.clear();
va_list va;
va_start (va, fmt);
vsnprintf((char *)tx_buff, MAX_BUFF_SIZE, fmt, va);
va_end (va);
if(_serial_debug) {
_serial_debug->println();
_serial_debug->print("REQUEST: ");
_serial_debug->write(tx_buff,strlen((char *)tx_buff));
_serial_debug->println();
}
_serial->write(tx_buff,strlen((char *)tx_buff));
return buf_read(prompt,data_res);;
}
typedef enum {
IDLE,
WAIT_FOR_SIZE,
WAIT_FOR_DATA
} ReadBySizeSt_t;
/* -------------------------------------------------------------------------- */
bool ModemClass::read_by_size_finished(string &rx) {
/* -------------------------------------------------------------------------- */
bool rv = false;
static bool first_call = true;
static ReadBySizeSt_t st = IDLE;
static int data_to_be_received = 0;
static int data_received = 0;
if(first_call) {
first_call = false;
st = WAIT_FOR_SIZE;
}
switch(st) {
case IDLE:
break;
case WAIT_FOR_SIZE: {
int pos = rx.find("|");
int pos_space = rx.find(" ");
if(pos != string::npos && pos_space != string::npos) {
string n = rx.substr(pos_space,pos);
int to_be_rx = atoi(n.c_str());
if(to_be_rx <= 0) {
while( _serial->available() ){
_serial->read();
}
rv = true;
first_call = true;
st = IDLE;
}
else {
/* add 4 because OK\r\n is always added at the end of data */
data_to_be_received = to_be_rx + 4;
data_received = 0;
st = WAIT_FOR_DATA;
}
rx.clear();
}
}
break;
case WAIT_FOR_DATA:
data_received++;
if(data_received == data_to_be_received) {
rv = true;
first_call = true;
st = IDLE;
}
break;
default:
st = IDLE;
break;
}
return rv;
}
/* -------------------------------------------------------------------------- */
bool ModemClass::buf_read(const string &prompt, string &data_res) {
/* -------------------------------------------------------------------------- */
bool res = false;
bool found = false;
if(_serial_debug && _debug_level >= 1) {
_serial_debug->print("RAW: ");
}
unsigned long start_time = millis();
while((millis() - start_time < _timeout) && !found){
while( _serial->available() ){
char c = _serial->read();
data_res += c;
if(_serial_debug && _debug_level >= 1) {
_serial_debug->print(c);
}
if(read_by_size) {
if(read_by_size_finished(data_res)) {
found = true;
read_by_size = false;
res = true;
if(data_res.size() > 0) {
data_res = data_res.substr(0, data_res.length() - (sizeof(RESULT_OK) - 1));
}
else {
break;
}
}
}
else {
if(string::npos != data_res.rfind(RESULT_DATA)) {
data_res = data_res.substr(0, data_res.length() - (sizeof(RESULT_DATA) - 1));
if(prompt != DO_NOT_CHECK_CMD) {
if(removeAtBegin(data_res, prompt)) {
res = true;
found = true;
} else {
data_res.clear();
continue;
}
}
else {
res = true;
found = true;
}
break;
}
else if(string::npos != data_res.rfind(RESULT_OK)){
data_res = data_res.substr(0, data_res.length() - (sizeof(RESULT_OK) - 1) );
if(prompt != DO_NOT_CHECK_CMD) {
if(removeAtBegin(data_res, prompt)) {
res = true;
found = true;
} else {
data_res.clear();
continue;
}
}
else {
res = true;
found = true;
}
break;
}
else if (string::npos != data_res.rfind(RESULT_ERROR)) {
found = true;
data_res.substr(0, data_res.length() - (sizeof(RESULT_ERROR) - 1));
res = false;
break;
}
}
}
}
if(trim_results) {
trim(data_res);
}
trim_results = true;
read_by_size = false;
if(_serial_debug && _debug_level >= 1) {
_serial_debug->print("<-RAW END");
_serial_debug->println();
}
if(_serial_debug) {
_serial_debug->print(" ANSWER: ");
_serial_debug->println(data_res.c_str());
if(res) {
_serial_debug->println(" Result: OK");
}
else {
_serial_debug->println(" Result: FAILED");
}
}
return res;
}
#ifdef ARDUINO_UNOWIFIR4
ModemClass modem = ModemClass(&Serial2);
#else
ModemClass modem = ModemClass(D24,D25);
#endif