-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSodaq_AT_Device.cpp
executable file
·408 lines (325 loc) · 9.55 KB
/
Sodaq_AT_Device.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
Copyright (c) 2013-2015 Sodaq. All rights reserved.
This file is part of Sodaq_nbIOT.
Sodaq_nbIOT is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or(at your option) any later version.
Sodaq_nbIOT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Sodaq_nbIOT. If not, see
<http://www.gnu.org/licenses/>.
*/
#include "Sodaq_AT_Device.h"
//#define DEBUG
#ifdef DEBUG
#define debugPrintLn(...) { if (!this->_disableDiag && this->_diagStream) this->_diagStream->println(__VA_ARGS__); }
#define debugPrint(...) { if (!this->_disableDiag && this->_diagStream) this->_diagStream->print(__VA_ARGS__); }
#warning "Debug mode is ON"
#else
#define debugPrintLn(...)
#define debugPrint(...)
#endif
#define CR "\r"
#define LF "\n"
#define CRLF "\r\n"
// TODO this needs to be set in the compiler directives. Find something else to do
#define SODAQ_AT_DEVICE_TERMINATOR CRLF
#ifndef SODAQ_AT_DEVICE_TERMINATOR
#warning "SODAQ_AT_DEVICE_TERMINATOR is not set"
#define SODAQ_AT_DEVICE_TERMINATOR CRLF
#endif
#define SODAQ_AT_DEVICE_TERMINATOR_LEN (sizeof(SODAQ_AT_DEVICE_TERMINATOR) - 1) // without the NULL terminator
#define SODAQ_AT_DEVICE_DEFAULT_INPUT_BUFFER_SIZE 250
// Constructor
Sodaq_AT_Device::Sodaq_AT_Device() :
_txEnablePin(-1),
_modemStream(0),
_diagStream(0),
_disableDiag(false),
_inputBufferSize(SODAQ_AT_DEVICE_DEFAULT_INPUT_BUFFER_SIZE),
_inputBuffer(0),
_onoff(0),
_baudRateChangeCallbackPtr(0),
_appendCommand(false),
_startOn(0)
{
this->_isBufferInitialized = false;
}
// Turns the modem on and returns true if successful.
bool Sodaq_AT_Device::on()
{
_startOn = millis();
if (!isOn()) {
if (_onoff) {
_onoff->on();
}
}
setTxPowerIfAvailable(true);
// wait for power up
bool timeout = true;
for (uint8_t i = 0; i < 15; i++) {
if (isAlive()) {
timeout = false;
break;
}
}
if (timeout) {
debugPrintLn("Error: No Reply from Modem");
return false;
}
return isOn(); // this essentially means isOn() && isAlive()
}
// Turns the modem off and returns true if successful.
bool Sodaq_AT_Device::off()
{
// No matter if it is on or off, turn it off.
if (_onoff) {
_onoff->off();
}
setTxPowerIfAvailable(false);
return !isOn();
}
// Returns true if the modem is on.
bool Sodaq_AT_Device::isOn() const
{
if (_onoff) {
return _onoff->isOn();
}
// No onoff. Let's assume it is on.
return true;
}
void Sodaq_AT_Device::setTxPowerIfAvailable(bool on)
{
if (_txEnablePin != -1) {
digitalWrite(_txEnablePin, on);
}
}
void Sodaq_AT_Device::writeProlog()
{
if (!_appendCommand) {
debugPrint(">> ");
_appendCommand = true;
}
}
// Write a byte, as binary data
size_t Sodaq_AT_Device::writeByte(uint8_t value)
{
return _modemStream->write(value);
}
size_t Sodaq_AT_Device::print(const String& buffer)
{
writeProlog();
debugPrint(buffer);
return _modemStream->print(buffer);
}
size_t Sodaq_AT_Device::print(const char buffer[])
{
writeProlog();
debugPrint(buffer);
return _modemStream->print(buffer);
}
size_t Sodaq_AT_Device::print(char value)
{
writeProlog();
debugPrint(value);
return _modemStream->print(value);
};
size_t Sodaq_AT_Device::print(unsigned char value, int base)
{
writeProlog();
debugPrint(value, base);
return _modemStream->print(value, base);
};
size_t Sodaq_AT_Device::print(int value, int base)
{
writeProlog();
debugPrint(value, base);
return _modemStream->print(value, base);
};
size_t Sodaq_AT_Device::print(unsigned int value, int base)
{
writeProlog();
debugPrint(value, base);
return _modemStream->print(value, base);
};
size_t Sodaq_AT_Device::print(long value, int base)
{
writeProlog();
debugPrint(value, base);
return _modemStream->print(value, base);
};
size_t Sodaq_AT_Device::print(unsigned long value, int base)
{
writeProlog();
debugPrint(value, base);
return _modemStream->print(value, base);
};
size_t Sodaq_AT_Device::println(const __FlashStringHelper* ifsh)
{
size_t n = print(ifsh);
n += println();
return n;
}
size_t Sodaq_AT_Device::println(const String& s)
{
size_t n = print(s);
n += println();
return n;
}
size_t Sodaq_AT_Device::println(const char c[])
{
size_t n = print(c);
n += println();
return n;
}
size_t Sodaq_AT_Device::println(char c)
{
size_t n = print(c);
n += println();
return n;
}
size_t Sodaq_AT_Device::println(unsigned char b, int base)
{
size_t i = print(b, base);
return i + println();
}
size_t Sodaq_AT_Device::println(int num, int base)
{
size_t i = print(num, base);
return i + println();
}
size_t Sodaq_AT_Device::println(unsigned int num, int base)
{
size_t i = print(num, base);
return i + println();
}
size_t Sodaq_AT_Device::println(long num, int base)
{
size_t i = print(num, base);
return i + println();
}
size_t Sodaq_AT_Device::println(unsigned long num, int base)
{
size_t i = print(num, base);
return i + println();
}
size_t Sodaq_AT_Device::println(double num, int digits)
{
writeProlog();
debugPrint(num, digits);
return _modemStream->println(num, digits);
}
size_t Sodaq_AT_Device::println(const Printable& x)
{
size_t i = print(x);
return i + println();
}
size_t Sodaq_AT_Device::println(void)
{
debugPrintLn();
size_t i = print('\r');
_appendCommand = false;
return i;
}
// Initializes the input buffer and makes sure it is only initialized once.
// Safe to call multiple times.
void Sodaq_AT_Device::initBuffer()
{
debugPrintLn("[initBuffer]");
// make sure the buffers are only initialized once
if (!_isBufferInitialized) {
this->_inputBuffer = static_cast<char*>(malloc(this->_inputBufferSize));
_isBufferInitialized = true;
}
}
// Sets the modem stream.
void Sodaq_AT_Device::setModemStream(Stream& stream)
{
this->_modemStream = &stream;
}
// Sets the optional tx enable pin.
void Sodaq_AT_Device::setTxEnablePin(int8_t txEnablePin)
{
_txEnablePin = txEnablePin;
if (_txEnablePin != -1) {
pinMode(_txEnablePin, OUTPUT);
digitalWrite(_txEnablePin, LOW);
}
}
// Returns a character from the modem stream if read within _timeout ms or -1 otherwise.
int Sodaq_AT_Device::timedRead(uint32_t timeout) const
{
int c;
uint32_t _startMillis = millis();
do {
c = _modemStream->read();
if (c >= 0) {
return c;
}
} while (millis() - _startMillis < timeout);
return -1; // -1 indicates timeout
}
// Fills the given "buffer" with characters read from the modem stream up to "length"
// maximum characters and until the "terminator" character is found or a character read
// times out (whichever happens first).
// The buffer does not contain the "terminator" character or a null terminator explicitly.
// Returns the number of characters written to the buffer, not including null terminator.
size_t Sodaq_AT_Device::readBytesUntil(char terminator, char* buffer, size_t length, uint32_t timeout)
{
if (length < 1) {
return 0;
}
size_t index = 0;
while (index < length) {
int c = timedRead(timeout);
if (c < 0 || c == terminator) {
break;
}
*buffer++ = static_cast<char>(c);
index++;
}
if (index < length) {
*buffer = '\0';
}
// TODO distinguise timeout from empty string?
// TODO return error for overflow?
return index; // return number of characters, not including null terminator
}
// Fills the given "buffer" with up to "length" characters read from the modem stream.
// It stops when a character read times out or "length" characters have been read.
// Returns the number of characters written to the buffer.
size_t Sodaq_AT_Device::readBytes(uint8_t* buffer, size_t length, uint32_t timeout)
{
size_t count = 0;
while (count < length) {
int c = timedRead(timeout);
if (c < 0) {
break;
}
*buffer++ = static_cast<uint8_t>(c);
count++;
}
// TODO distinguise timeout from empty string?
// TODO return error for overflow?
return count;
}
// Reads a line (up to the SODAQ_GSM_TERMINATOR) from the modem stream into the "buffer".
// The buffer is terminated with null.
// Returns the number of bytes read, not including the null terminator.
size_t Sodaq_AT_Device::readLn(char* buffer, size_t size, uint32_t timeout)
{
// Use size-1 to leave room for a string terminator
size_t len = readBytesUntil(SODAQ_AT_DEVICE_TERMINATOR[SODAQ_AT_DEVICE_TERMINATOR_LEN - 1], buffer, size - 1, timeout);
// check if the terminator is more than 1 characters, then check if the first character of it exists
// in the calculated position and terminate the string there
if ((SODAQ_AT_DEVICE_TERMINATOR_LEN > 1) && (buffer[len - (SODAQ_AT_DEVICE_TERMINATOR_LEN - 1)] == SODAQ_AT_DEVICE_TERMINATOR[0])) {
len -= SODAQ_AT_DEVICE_TERMINATOR_LEN - 1;
}
// terminate string, there should always be room for it (see size-1 above)
buffer[len] = '\0';
return len;
}