User | Revision | Line number | New contents of line |
geky |
0:c741e144517c
|
1
|
/* Copyright (c) 2015 ARM Limited
|
geky |
0:c741e144517c
|
2
|
*
|
geky |
0:c741e144517c
|
3
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
geky |
0:c741e144517c
|
4
|
* you may not use this file except in compliance with the License.
|
geky |
0:c741e144517c
|
5
|
* You may obtain a copy of the License at
|
geky |
0:c741e144517c
|
6
|
*
|
geky |
0:c741e144517c
|
7
|
* http://www.apache.org/licenses/LICENSE-2.0
|
geky |
0:c741e144517c
|
8
|
*
|
geky |
0:c741e144517c
|
9
|
* Unless required by applicable law or agreed to in writing, software
|
geky |
0:c741e144517c
|
10
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
geky |
0:c741e144517c
|
11
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
geky |
0:c741e144517c
|
12
|
* See the License for the specific language governing permissions and
|
geky |
0:c741e144517c
|
13
|
* limitations under the License.
|
geky |
0:c741e144517c
|
14
|
*
|
geky |
0:c741e144517c
|
15
|
* @section DESCRIPTION
|
geky |
0:c741e144517c
|
16
|
*
|
geky |
0:c741e144517c
|
17
|
* Parser for the AT command syntax
|
geky |
0:c741e144517c
|
18
|
*
|
geky |
0:c741e144517c
|
19
|
*/
|
geky |
0:c741e144517c
|
20
|
|
geky |
0:c741e144517c
|
21
|
#include "mbed.h"
|
geky |
1:66a14afe650a
|
22
|
#include <stdarg.h>
|
geky |
1:66a14afe650a
|
23
|
|
geky |
1:66a14afe650a
|
24
|
#include "BufferedSerial.h"
|
geky |
0:c741e144517c
|
25
|
|
geky |
0:c741e144517c
|
26
|
|
geky |
0:c741e144517c
|
27
|
/**
|
geky |
0:c741e144517c
|
28
|
* The ATParser class wraps information about the serial in use
|
geky |
0:c741e144517c
|
29
|
*/
|
geky |
0:c741e144517c
|
30
|
class ATParser {
|
geky |
0:c741e144517c
|
31
|
private:
|
geky |
0:c741e144517c
|
32
|
// Serial information
|
geky |
1:66a14afe650a
|
33
|
BufferedSerial *_serial;
|
geky |
0:c741e144517c
|
34
|
int _buffer_size;
|
geky |
0:c741e144517c
|
35
|
char *_buffer;
|
geky |
0:c741e144517c
|
36
|
int _timeout;
|
geky |
0:c741e144517c
|
37
|
|
geky |
2:4d68f546861c
|
38
|
// Parsing information
|
geky |
2:4d68f546861c
|
39
|
const char *_delimiter;
|
geky |
3:32915b9467d2
|
40
|
int _delim_size;
|
geky |
2:4d68f546861c
|
41
|
|
geky |
0:c741e144517c
|
42
|
// Helper methods for putc/getc with timeout
|
geky |
0:c741e144517c
|
43
|
int _putc(char c);
|
geky |
0:c741e144517c
|
44
|
int _getc();
|
geky |
0:c741e144517c
|
45
|
|
geky |
0:c741e144517c
|
46
|
// Flush used to clear serial connection
|
geky |
0:c741e144517c
|
47
|
void _flush();
|
geky |
0:c741e144517c
|
48
|
|
geky |
0:c741e144517c
|
49
|
// Helper methods for reading/writing lines with
|
geky |
0:c741e144517c
|
50
|
// timeout and buffer limitations
|
geky |
0:c741e144517c
|
51
|
bool _putline(const char *line);
|
geky |
1:66a14afe650a
|
52
|
bool _getline(char *line, int size);
|
geky |
0:c741e144517c
|
53
|
|
geky |
0:c741e144517c
|
54
|
public:
|
geky |
0:c741e144517c
|
55
|
/**
|
geky |
0:c741e144517c
|
56
|
* Constructor
|
geky |
0:c741e144517c
|
57
|
*
|
geky |
0:c741e144517c
|
58
|
* @param serial serial interface to use for AT commands
|
geky |
1:66a14afe650a
|
59
|
* @param buffer_size size of internal buffer for transaction
|
geky |
0:c741e144517c
|
60
|
* @param timeout timeout of the connection
|
geky |
1:66a14afe650a
|
61
|
* @param echo flag to indicate if an echo of sent characters should be expected
|
geky |
2:4d68f546861c
|
62
|
* @param delimiter string of characters to use as line delimiters
|
geky |
0:c741e144517c
|
63
|
*/
|
geky |
2:4d68f546861c
|
64
|
ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 3000,
|
geky |
2:4d68f546861c
|
65
|
const char *delimiter = "\r\n") :
|
geky |
0:c741e144517c
|
66
|
_serial(serial),
|
geky |
3:32915b9467d2
|
67
|
_buffer_size(buffer_size) {
|
geky |
0:c741e144517c
|
68
|
_buffer = new char[buffer_size];
|
geky |
3:32915b9467d2
|
69
|
setTimeout(timeout);
|
geky |
3:32915b9467d2
|
70
|
setDelimiter(delimiter);
|
geky |
0:c741e144517c
|
71
|
}
|
geky |
0:c741e144517c
|
72
|
|
geky |
0:c741e144517c
|
73
|
/**
|
geky |
0:c741e144517c
|
74
|
* Destructor
|
geky |
0:c741e144517c
|
75
|
*/
|
geky |
0:c741e144517c
|
76
|
~ATParser() {
|
geky |
0:c741e144517c
|
77
|
delete [] _buffer;
|
geky |
0:c741e144517c
|
78
|
}
|
geky |
0:c741e144517c
|
79
|
|
geky |
0:c741e144517c
|
80
|
/**
|
geky |
0:c741e144517c
|
81
|
* Allows timeout to be changed between commands
|
geky |
0:c741e144517c
|
82
|
*
|
geky |
0:c741e144517c
|
83
|
* @param timeout timeout of the connection
|
geky |
0:c741e144517c
|
84
|
*/
|
geky |
0:c741e144517c
|
85
|
void setTimeout(int timeout) {
|
geky |
0:c741e144517c
|
86
|
_timeout = timeout;
|
geky |
0:c741e144517c
|
87
|
}
|
geky |
2:4d68f546861c
|
88
|
|
geky |
2:4d68f546861c
|
89
|
/**
|
geky |
2:4d68f546861c
|
90
|
* Sets string of characters to use as line delimiters
|
geky |
2:4d68f546861c
|
91
|
*
|
geky |
2:4d68f546861c
|
92
|
* @param delimiter string of characters to use as line delimiters
|
geky |
2:4d68f546861c
|
93
|
*/
|
geky |
2:4d68f546861c
|
94
|
void setDelimiter(const char *delimiter) {
|
geky |
2:4d68f546861c
|
95
|
_delimiter = delimiter;
|
geky |
3:32915b9467d2
|
96
|
_delim_size = strlen(delimiter);
|
geky |
2:4d68f546861c
|
97
|
}
|
geky |
0:c741e144517c
|
98
|
|
geky |
0:c741e144517c
|
99
|
/**
|
geky |
2:4d68f546861c
|
100
|
* Issue AT commands
|
geky |
2:4d68f546861c
|
101
|
*
|
geky |
2:4d68f546861c
|
102
|
* Sends formatted command and waits for formatted response.
|
geky |
2:4d68f546861c
|
103
|
* Any recieved data that does not match the specified response
|
geky |
2:4d68f546861c
|
104
|
* is ignored until the timeout has passed.
|
geky |
2:4d68f546861c
|
105
|
*
|
geky |
2:4d68f546861c
|
106
|
* Both the command and response use format strings that are internally
|
geky |
2:4d68f546861c
|
107
|
* passed to printf and scanf respectively.
|
geky |
2:4d68f546861c
|
108
|
* @see printf
|
geky |
2:4d68f546861c
|
109
|
* @see scanf
|
geky |
2:4d68f546861c
|
110
|
*
|
geky |
2:4d68f546861c
|
111
|
* Commands are expected to be formatted with specified delimiters.
|
geky |
2:4d68f546861c
|
112
|
* Sent commands are appended with the delimiter, and responses are
|
geky |
2:4d68f546861c
|
113
|
* seperated by delimiters before attempting to parse.
|
geky |
1:66a14afe650a
|
114
|
*
|
geky |
1:66a14afe650a
|
115
|
* Here are some examples:
|
geky |
1:66a14afe650a
|
116
|
* @code
|
geky |
1:66a14afe650a
|
117
|
* at.command("AT", "OK");
|
geky |
1:66a14afe650a
|
118
|
* at.command("AT+CWMODE=%d", "OK", 3);
|
geky |
2:4d68f546861c
|
119
|
* at.command("AT+CWMODE?", "+CWMODE:%d\r\nOK", &result);
|
geky |
1:66a14afe650a
|
120
|
* @endcode
|
geky |
0:c741e144517c
|
121
|
*
|
geky |
0:c741e144517c
|
122
|
* @param command printf-like format string of command to send
|
geky |
0:c741e144517c
|
123
|
* @param response scanf-like format string of response to parse
|
geky |
0:c741e144517c
|
124
|
* @param ... all printf-like arguments to insert into command followed by
|
geky |
0:c741e144517c
|
125
|
* all scanf-like pointers to destinations for response values
|
geky |
1:66a14afe650a
|
126
|
* @return true only if response is successfully matched
|
geky |
0:c741e144517c
|
127
|
*/
|
geky |
0:c741e144517c
|
128
|
bool command(const char *command, const char *response, ...);
|
geky |
0:c741e144517c
|
129
|
};
|
geky |
1:66a14afe650a
|
130
|
|