-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSodaq_AT_Device.h
182 lines (140 loc) · 6.53 KB
/
Sodaq_AT_Device.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
/*
Copyright (c) 2015-2016 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/>.
*/
#ifndef _SODAQ_AT_DEVICE_h
#define _SODAQ_AT_DEVICE_h
#include <Arduino.h>
#include <stdint.h>
#include <Stream.h>
#include "Sodaq_OnOffBee.h"
// Response type (returned by readResponse() and parser methods).
enum ResponseTypes {
ResponseNotFound = 0,
ResponseOK = 1,
ResponseError = 2,
ResponsePrompt = 3,
ResponseTimeout = 4,
ResponseEmpty = 5,
ResponsePendingExtra = 6,
};
// IP type
typedef uint32_t IP_t;
// callback for changing the baudrate of the modem stream.
typedef void (*BaudRateChangeCallbackPtr)(uint32_t newBaudrate);
#define SODAQ_AT_DEVICE_DEFAULT_READ_MS 5000 // Used in readResponse()
class Sodaq_AT_Device
{
public:
// Constructor
Sodaq_AT_Device();
virtual ~Sodaq_AT_Device() {}
// Sets the onoff instance
void setOnOff(Sodaq_OnOffBee& onoff) { _onoff = &onoff; }
// Turns the modem on and returns true if successful.
bool on();
// Turns the modem off and returns true if successful.
bool off();
// Sets the optional "Diagnostics and Debug" stream.
void setDiag(Stream& stream) { _diagStream = &stream; }
void setDiag(Stream* stream) { _diagStream = stream; }
// Sets the size of the input buffer.
// Needs to be called before init().
void setInputBufferSize(size_t value) { this->_inputBufferSize = value; };
// Returns the default baud rate of the modem.
// To be used when initializing the modem stream for the first time.
virtual uint32_t getDefaultBaudrate() = 0;
// Enables the change of the baud rate to a higher speed when the modem is ready to do so.
// Needs a callback in the main application to re-initialize the stream.
void enableBaudrateChange(BaudRateChangeCallbackPtr callback) { _baudRateChangeCallbackPtr = callback; };
protected:
// The stream that communicates with the device.
Stream* _modemStream;
// The (optional) stream to show debug information.
Stream* _diagStream;
bool _disableDiag;
// The size of the input buffer. Equals SODAQ_GSM_MODEM_DEFAULT_INPUT_BUFFER_SIZE
// by default or (optionally) a user-defined value when using USE_DYNAMIC_BUFFER.
size_t _inputBufferSize;
// Flag to make sure the buffers are not allocated more than once.
bool _isBufferInitialized;
// The buffer used when reading from the modem. The space is allocated during init() via initBuffer().
char* _inputBuffer;
// The on-off pin power controller object.
Sodaq_OnOffBee* _onoff;
// The callback for requesting baudrate change of the modem stream.
BaudRateChangeCallbackPtr _baudRateChangeCallbackPtr;
// This flag keeps track if the next write is the continuation of the current command
// A Carriage Return will reset this flag.
bool _appendCommand;
// Keep track when connect started. Use this to record various status changes.
uint32_t _startOn;
// Initializes the input buffer and makes sure it is only initialized once.
// Safe to call multiple times.
void initBuffer();
// Returns true if the modem is ON (and replies to "AT" commands without timing out)
virtual bool isAlive() = 0;
// Returns true if the modem is on.
bool isOn() const;
// Sets the modem stream.
void setModemStream(Stream& stream);
// Returns a character from the modem stream if read within _timeout ms or -1 otherwise.
int timedRead(uint32_t timeout = 1000) const;
// 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 readBytesUntil(char terminator, char* buffer, size_t length, uint32_t timeout = 1000);
// 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 readBytes(uint8_t* buffer, size_t length, uint32_t timeout = 1000);
// Reads a line from the modem stream into the "buffer". The line terminator is not
// written into the buffer. The buffer is terminated with null.
// Returns the number of bytes read, not including the null terminator.
size_t readLn(char* buffer, size_t size, uint32_t timeout = 1000);
// Reads a line from the modem stream into the input buffer.
// Returns the number of bytes read.
size_t readLn() { return readLn(_inputBuffer, _inputBufferSize); };
// Write a byte
size_t writeByte(uint8_t value);
// Write the command prolog (just for debugging
void writeProlog();
size_t print(const __FlashStringHelper*);
size_t print(const String&);
size_t print(const char[]);
size_t print(char);
size_t print(unsigned char, int = DEC);
size_t print(int, int = DEC);
size_t print(unsigned int, int = DEC);
size_t print(long, int = DEC);
size_t print(unsigned long, int = DEC);
size_t print(double, int = 2);
size_t print(const Printable&);
size_t println(const __FlashStringHelper*);
size_t println(const String& s);
size_t println(const char[]);
size_t println(char);
size_t println(unsigned char, int = DEC);
size_t println(int, int = DEC);
size_t println(unsigned int, int = DEC);
size_t println(long, int = DEC);
size_t println(unsigned long, int = DEC);
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
virtual ResponseTypes readResponse(char* buffer, size_t size, size_t* outSize, uint32_t timeout = SODAQ_AT_DEVICE_DEFAULT_READ_MS) = 0;
};
#endif