a

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
hydroguy45
Date:
Fri Nov 01 18:52:45 2019 +0000
Revision:
12:ea030e3181d3
Parent:
7:ca226305d28b
Cleaned up the header

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hydroguy45 7:ca226305d28b 1 /* Copyright (c) 2015 ARM Limited
hydroguy45 7:ca226305d28b 2 *
hydroguy45 7:ca226305d28b 3 * Licensed under the Apache License, Version 2.0 (the "License");
hydroguy45 7:ca226305d28b 4 * you may not use this file except in compliance with the License.
hydroguy45 7:ca226305d28b 5 * You may obtain a copy of the License at
hydroguy45 7:ca226305d28b 6 *
hydroguy45 7:ca226305d28b 7 * http://www.apache.org/licenses/LICENSE-2.0
hydroguy45 7:ca226305d28b 8 *
hydroguy45 7:ca226305d28b 9 * Unless required by applicable law or agreed to in writing, software
hydroguy45 7:ca226305d28b 10 * distributed under the License is distributed on an "AS IS" BASIS,
hydroguy45 7:ca226305d28b 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
hydroguy45 7:ca226305d28b 12 * See the License for the specific language governing permissions and
hydroguy45 7:ca226305d28b 13 * limitations under the License.
hydroguy45 7:ca226305d28b 14 *
hydroguy45 7:ca226305d28b 15 * @section DESCRIPTION
hydroguy45 7:ca226305d28b 16 *
hydroguy45 7:ca226305d28b 17 * Parser for the AT command syntax
hydroguy45 7:ca226305d28b 18 *
hydroguy45 7:ca226305d28b 19 */
hydroguy45 7:ca226305d28b 20
hydroguy45 7:ca226305d28b 21 #include "ATParser.h"
hydroguy45 7:ca226305d28b 22 #include "mbed_debug.h"
hydroguy45 7:ca226305d28b 23
hydroguy45 7:ca226305d28b 24
hydroguy45 7:ca226305d28b 25 // getc/putc handling with timeouts
hydroguy45 7:ca226305d28b 26 int ATParser::putc(char c)
hydroguy45 7:ca226305d28b 27 {
hydroguy45 7:ca226305d28b 28 Timer timer;
hydroguy45 7:ca226305d28b 29 timer.start();
hydroguy45 7:ca226305d28b 30
hydroguy45 7:ca226305d28b 31 while (true) {
hydroguy45 7:ca226305d28b 32 if (_serial->writeable()) {
hydroguy45 7:ca226305d28b 33 return _serial->putc(c);
hydroguy45 7:ca226305d28b 34 }
hydroguy45 7:ca226305d28b 35 if (timer.read_ms() > _timeout) {
hydroguy45 7:ca226305d28b 36 return -1;
hydroguy45 7:ca226305d28b 37 }
hydroguy45 7:ca226305d28b 38 }
hydroguy45 7:ca226305d28b 39 }
hydroguy45 7:ca226305d28b 40
hydroguy45 7:ca226305d28b 41 int ATParser::getc()
hydroguy45 7:ca226305d28b 42 {
hydroguy45 7:ca226305d28b 43 Timer timer;
hydroguy45 7:ca226305d28b 44 timer.start();
hydroguy45 7:ca226305d28b 45
hydroguy45 7:ca226305d28b 46 while (true) {
hydroguy45 7:ca226305d28b 47 if (_serial->readable()) {
hydroguy45 7:ca226305d28b 48 return _serial->getc();
hydroguy45 7:ca226305d28b 49 }
hydroguy45 7:ca226305d28b 50 if (timer.read_ms() > _timeout) {
hydroguy45 7:ca226305d28b 51 return -1;
hydroguy45 7:ca226305d28b 52 }
hydroguy45 7:ca226305d28b 53 }
hydroguy45 7:ca226305d28b 54 }
hydroguy45 7:ca226305d28b 55
hydroguy45 7:ca226305d28b 56 void ATParser::flush()
hydroguy45 7:ca226305d28b 57 {
hydroguy45 7:ca226305d28b 58 while (_serial->readable()) {
hydroguy45 7:ca226305d28b 59 _serial->getc();
hydroguy45 7:ca226305d28b 60 }
hydroguy45 7:ca226305d28b 61 }
hydroguy45 7:ca226305d28b 62
hydroguy45 7:ca226305d28b 63
hydroguy45 7:ca226305d28b 64 // read/write handling with timeouts
hydroguy45 7:ca226305d28b 65 int ATParser::write(const char *data, int size)
hydroguy45 7:ca226305d28b 66 {
hydroguy45 7:ca226305d28b 67 int i = 0;
hydroguy45 7:ca226305d28b 68 for ( ; i < size; i++) {
hydroguy45 7:ca226305d28b 69 if (putc(data[i]) < 0) {
hydroguy45 7:ca226305d28b 70 return -1;
hydroguy45 7:ca226305d28b 71 }
hydroguy45 7:ca226305d28b 72 }
hydroguy45 7:ca226305d28b 73 return i;
hydroguy45 7:ca226305d28b 74 }
hydroguy45 7:ca226305d28b 75
hydroguy45 7:ca226305d28b 76 int ATParser::read(char *data, int size)
hydroguy45 7:ca226305d28b 77 {
hydroguy45 7:ca226305d28b 78 int i = 0;
hydroguy45 7:ca226305d28b 79 for ( ; i < size; i++) {
hydroguy45 7:ca226305d28b 80 int c = getc();
hydroguy45 7:ca226305d28b 81 if (c < 0) {
hydroguy45 7:ca226305d28b 82 return -1;
hydroguy45 7:ca226305d28b 83 }
hydroguy45 7:ca226305d28b 84 data[i] = c;
hydroguy45 7:ca226305d28b 85 }
hydroguy45 7:ca226305d28b 86 return i;
hydroguy45 7:ca226305d28b 87 }
hydroguy45 7:ca226305d28b 88
hydroguy45 7:ca226305d28b 89
hydroguy45 7:ca226305d28b 90 // printf/scanf handling
hydroguy45 7:ca226305d28b 91 int ATParser::vprintf(const char *format, va_list args)
hydroguy45 7:ca226305d28b 92 {
hydroguy45 7:ca226305d28b 93 if (vsprintf(_buffer, format, args) < 0) {
hydroguy45 7:ca226305d28b 94 return false;
hydroguy45 7:ca226305d28b 95 }
hydroguy45 7:ca226305d28b 96 int i = 0;
hydroguy45 7:ca226305d28b 97 for ( ; _buffer[i]; i++) {
hydroguy45 7:ca226305d28b 98 if (putc(_buffer[i]) < 0) {
hydroguy45 7:ca226305d28b 99 return -1;
hydroguy45 7:ca226305d28b 100 }
hydroguy45 7:ca226305d28b 101 }
hydroguy45 7:ca226305d28b 102 return i;
hydroguy45 7:ca226305d28b 103 }
hydroguy45 7:ca226305d28b 104
hydroguy45 7:ca226305d28b 105 int ATParser::vscanf(const char *format, va_list args)
hydroguy45 7:ca226305d28b 106 {
hydroguy45 7:ca226305d28b 107 // Since format is const, we need to copy it into our buffer to
hydroguy45 7:ca226305d28b 108 // add the line's null terminator and clobber value-matches with asterisks.
hydroguy45 7:ca226305d28b 109 //
hydroguy45 7:ca226305d28b 110 // We just use the beginning of the buffer to avoid unnecessary allocations.
hydroguy45 7:ca226305d28b 111 int i = 0;
hydroguy45 7:ca226305d28b 112 int offset = 0;
hydroguy45 7:ca226305d28b 113
hydroguy45 7:ca226305d28b 114 while (format[i]) {
hydroguy45 7:ca226305d28b 115 if (format[i] == '%' && format[i+1] != '%' && format[i+1] != '*') {
hydroguy45 7:ca226305d28b 116 _buffer[offset++] = '%';
hydroguy45 7:ca226305d28b 117 _buffer[offset++] = '*';
hydroguy45 7:ca226305d28b 118 i++;
hydroguy45 7:ca226305d28b 119 } else {
hydroguy45 7:ca226305d28b 120 _buffer[offset++] = format[i++];
hydroguy45 7:ca226305d28b 121 }
hydroguy45 7:ca226305d28b 122 }
hydroguy45 7:ca226305d28b 123
hydroguy45 7:ca226305d28b 124 // Scanf has very poor support for catching errors
hydroguy45 7:ca226305d28b 125 // fortunately, we can abuse the %n specifier to determine
hydroguy45 7:ca226305d28b 126 // if the entire string was matched.
hydroguy45 7:ca226305d28b 127 _buffer[offset++] = '%';
hydroguy45 7:ca226305d28b 128 _buffer[offset++] = 'n';
hydroguy45 7:ca226305d28b 129 _buffer[offset++] = 0;
hydroguy45 7:ca226305d28b 130
hydroguy45 7:ca226305d28b 131 // To workaround scanf's lack of error reporting, we actually
hydroguy45 7:ca226305d28b 132 // make two passes. One checks the validity with the modified
hydroguy45 7:ca226305d28b 133 // format string that only stores the matched characters (%n).
hydroguy45 7:ca226305d28b 134 // The other reads in the actual matched values.
hydroguy45 7:ca226305d28b 135 //
hydroguy45 7:ca226305d28b 136 // We keep trying the match until we succeed or some other error
hydroguy45 7:ca226305d28b 137 // derails us.
hydroguy45 7:ca226305d28b 138 int j = 0;
hydroguy45 7:ca226305d28b 139
hydroguy45 7:ca226305d28b 140 while (true) {
hydroguy45 7:ca226305d28b 141 // Ran out of space
hydroguy45 7:ca226305d28b 142 if (j+1 >= _buffer_size - offset) {
hydroguy45 7:ca226305d28b 143 return false;
hydroguy45 7:ca226305d28b 144 }
hydroguy45 7:ca226305d28b 145 // Recieve next character
hydroguy45 7:ca226305d28b 146 int c = getc();
hydroguy45 7:ca226305d28b 147 if (c < 0) {
hydroguy45 7:ca226305d28b 148 return -1;
hydroguy45 7:ca226305d28b 149 }
hydroguy45 7:ca226305d28b 150 _buffer[offset + j++] = c;
hydroguy45 7:ca226305d28b 151 _buffer[offset + j] = 0;
hydroguy45 7:ca226305d28b 152
hydroguy45 7:ca226305d28b 153 // Check for match
hydroguy45 7:ca226305d28b 154 int count = -1;
hydroguy45 7:ca226305d28b 155 sscanf(_buffer+offset, _buffer, &count);
hydroguy45 7:ca226305d28b 156
hydroguy45 7:ca226305d28b 157 // We only succeed if all characters in the response are matched
hydroguy45 7:ca226305d28b 158 if (count == j) {
hydroguy45 7:ca226305d28b 159 // Store the found results
hydroguy45 7:ca226305d28b 160 vsscanf(_buffer+offset, format, args);
hydroguy45 7:ca226305d28b 161 return j;
hydroguy45 7:ca226305d28b 162 }
hydroguy45 7:ca226305d28b 163 }
hydroguy45 7:ca226305d28b 164 }
hydroguy45 7:ca226305d28b 165
hydroguy45 7:ca226305d28b 166
hydroguy45 7:ca226305d28b 167 // Command parsing with line handling
hydroguy45 7:ca226305d28b 168 bool ATParser::vsend(const char *command, va_list args)
hydroguy45 7:ca226305d28b 169 {
hydroguy45 7:ca226305d28b 170 // Create and send command
hydroguy45 7:ca226305d28b 171 if (vsprintf(_buffer, command, args) < 0) {
hydroguy45 7:ca226305d28b 172 return false;
hydroguy45 7:ca226305d28b 173 }
hydroguy45 7:ca226305d28b 174 for (int i = 0; _buffer[i]; i++) {
hydroguy45 7:ca226305d28b 175 if (putc(_buffer[i]) < 0) {
hydroguy45 7:ca226305d28b 176 return false;
hydroguy45 7:ca226305d28b 177 }
hydroguy45 7:ca226305d28b 178 }
hydroguy45 7:ca226305d28b 179
hydroguy45 7:ca226305d28b 180 // Finish with newline
hydroguy45 7:ca226305d28b 181 for (int i = 0; _delimiter[i]; i++) {
hydroguy45 7:ca226305d28b 182 if (putc(_delimiter[i]) < 0) {
hydroguy45 7:ca226305d28b 183 return false;
hydroguy45 7:ca226305d28b 184 }
hydroguy45 7:ca226305d28b 185 }
hydroguy45 7:ca226305d28b 186
hydroguy45 7:ca226305d28b 187 debug_if(dbg_on, "AT> %s\r\n", _buffer);
hydroguy45 7:ca226305d28b 188 return true;
hydroguy45 7:ca226305d28b 189 }
hydroguy45 7:ca226305d28b 190
hydroguy45 7:ca226305d28b 191 bool ATParser::vrecv(const char *response, va_list args)
hydroguy45 7:ca226305d28b 192 {
hydroguy45 7:ca226305d28b 193 // Iterate through each line in the expected response
hydroguy45 7:ca226305d28b 194 while (response[0]) {
hydroguy45 7:ca226305d28b 195 // Since response is const, we need to copy it into our buffer to
hydroguy45 7:ca226305d28b 196 // add the line's null terminator and clobber value-matches with asterisks.
hydroguy45 7:ca226305d28b 197 //
hydroguy45 7:ca226305d28b 198 // We just use the beginning of the buffer to avoid unnecessary allocations.
hydroguy45 7:ca226305d28b 199 int i = 0;
hydroguy45 7:ca226305d28b 200 int offset = 0;
hydroguy45 7:ca226305d28b 201
hydroguy45 7:ca226305d28b 202 while (response[i]) {
hydroguy45 7:ca226305d28b 203 if (memcmp(&response[i+1-_delim_size], _delimiter, _delim_size) == 0) {
hydroguy45 7:ca226305d28b 204 i++;
hydroguy45 7:ca226305d28b 205 break;
hydroguy45 7:ca226305d28b 206 } else if (response[i] == '%' && response[i+1] != '%' && response[i+1] != '*') {
hydroguy45 7:ca226305d28b 207 _buffer[offset++] = '%';
hydroguy45 7:ca226305d28b 208 _buffer[offset++] = '*';
hydroguy45 7:ca226305d28b 209 i++;
hydroguy45 7:ca226305d28b 210 } else {
hydroguy45 7:ca226305d28b 211 _buffer[offset++] = response[i++];
hydroguy45 7:ca226305d28b 212 }
hydroguy45 7:ca226305d28b 213 }
hydroguy45 7:ca226305d28b 214
hydroguy45 7:ca226305d28b 215 // Scanf has very poor support for catching errors
hydroguy45 7:ca226305d28b 216 // fortunately, we can abuse the %n specifier to determine
hydroguy45 7:ca226305d28b 217 // if the entire string was matched.
hydroguy45 7:ca226305d28b 218 _buffer[offset++] = '%';
hydroguy45 7:ca226305d28b 219 _buffer[offset++] = 'n';
hydroguy45 7:ca226305d28b 220 _buffer[offset++] = 0;
hydroguy45 7:ca226305d28b 221
hydroguy45 7:ca226305d28b 222 // To workaround scanf's lack of error reporting, we actually
hydroguy45 7:ca226305d28b 223 // make two passes. One checks the validity with the modified
hydroguy45 7:ca226305d28b 224 // format string that only stores the matched characters (%n).
hydroguy45 7:ca226305d28b 225 // The other reads in the actual matched values.
hydroguy45 7:ca226305d28b 226 //
hydroguy45 7:ca226305d28b 227 // We keep trying the match until we succeed or some other error
hydroguy45 7:ca226305d28b 228 // derails us.
hydroguy45 7:ca226305d28b 229 int j = 0;
hydroguy45 7:ca226305d28b 230
hydroguy45 7:ca226305d28b 231 while (true) {
hydroguy45 7:ca226305d28b 232 // Recieve next character
hydroguy45 7:ca226305d28b 233 int c = getc();
hydroguy45 7:ca226305d28b 234 if (c < 0) {
hydroguy45 7:ca226305d28b 235 return false;
hydroguy45 7:ca226305d28b 236 }
hydroguy45 7:ca226305d28b 237 _buffer[offset + j++] = c;
hydroguy45 7:ca226305d28b 238 _buffer[offset + j] = 0;
hydroguy45 7:ca226305d28b 239
hydroguy45 7:ca226305d28b 240 // Check for oob data
hydroguy45 7:ca226305d28b 241 for (int k = 0; k < _oobs.size(); k++) {
hydroguy45 7:ca226305d28b 242 if (j == _oobs[k].len && memcmp(
hydroguy45 7:ca226305d28b 243 _oobs[k].prefix, _buffer+offset, _oobs[k].len) == 0) {
hydroguy45 7:ca226305d28b 244 debug_if(dbg_on, "AT! %s\r\n", _oobs[k].prefix);
hydroguy45 7:ca226305d28b 245 _oobs[k].cb();
hydroguy45 7:ca226305d28b 246
hydroguy45 7:ca226305d28b 247 // oob may have corrupted non-reentrant buffer,
hydroguy45 7:ca226305d28b 248 // so we need to set it up again
hydroguy45 7:ca226305d28b 249 return vrecv(response, args);
hydroguy45 7:ca226305d28b 250 }
hydroguy45 7:ca226305d28b 251 }
hydroguy45 7:ca226305d28b 252
hydroguy45 7:ca226305d28b 253 // Check for match
hydroguy45 7:ca226305d28b 254 int count = -1;
hydroguy45 7:ca226305d28b 255 sscanf(_buffer+offset, _buffer, &count);
hydroguy45 7:ca226305d28b 256
hydroguy45 7:ca226305d28b 257 // We only succeed if all characters in the response are matched
hydroguy45 7:ca226305d28b 258 if (count == j) {
hydroguy45 7:ca226305d28b 259 debug_if(dbg_on, "AT= %s\r\n", _buffer+offset);
hydroguy45 7:ca226305d28b 260 // Reuse the front end of the buffer
hydroguy45 7:ca226305d28b 261 memcpy(_buffer, response, i);
hydroguy45 7:ca226305d28b 262 _buffer[i] = 0;
hydroguy45 7:ca226305d28b 263
hydroguy45 7:ca226305d28b 264 // Store the found results
hydroguy45 7:ca226305d28b 265 vsscanf(_buffer+offset, _buffer, args);
hydroguy45 7:ca226305d28b 266
hydroguy45 7:ca226305d28b 267 // Jump to next line and continue parsing
hydroguy45 7:ca226305d28b 268 response += i;
hydroguy45 7:ca226305d28b 269 break;
hydroguy45 7:ca226305d28b 270 }
hydroguy45 7:ca226305d28b 271
hydroguy45 7:ca226305d28b 272 // Clear the buffer when we hit a newline or ran out of space
hydroguy45 7:ca226305d28b 273 // running out of space usually means we ran into binary data
hydroguy45 7:ca226305d28b 274 if (j+1 >= _buffer_size - offset ||
hydroguy45 7:ca226305d28b 275 strcmp(&_buffer[offset + j-_delim_size], _delimiter) == 0) {
hydroguy45 7:ca226305d28b 276
hydroguy45 7:ca226305d28b 277 debug_if(dbg_on, "AT< %s", _buffer+offset);
hydroguy45 7:ca226305d28b 278 j = 0;
hydroguy45 7:ca226305d28b 279 }
hydroguy45 7:ca226305d28b 280 }
hydroguy45 7:ca226305d28b 281 }
hydroguy45 7:ca226305d28b 282
hydroguy45 7:ca226305d28b 283 return true;
hydroguy45 7:ca226305d28b 284 }
hydroguy45 7:ca226305d28b 285
hydroguy45 7:ca226305d28b 286
hydroguy45 7:ca226305d28b 287 // Mapping to vararg functions
hydroguy45 7:ca226305d28b 288 int ATParser::printf(const char *format, ...)
hydroguy45 7:ca226305d28b 289 {
hydroguy45 7:ca226305d28b 290 va_list args;
hydroguy45 7:ca226305d28b 291 va_start(args, format);
hydroguy45 7:ca226305d28b 292 int res = vprintf(format, args);
hydroguy45 7:ca226305d28b 293 va_end(args);
hydroguy45 7:ca226305d28b 294 return res;
hydroguy45 7:ca226305d28b 295 }
hydroguy45 7:ca226305d28b 296
hydroguy45 7:ca226305d28b 297 int ATParser::scanf(const char *format, ...)
hydroguy45 7:ca226305d28b 298 {
hydroguy45 7:ca226305d28b 299 va_list args;
hydroguy45 7:ca226305d28b 300 va_start(args, format);
hydroguy45 7:ca226305d28b 301 int res = vscanf(format, args);
hydroguy45 7:ca226305d28b 302 va_end(args);
hydroguy45 7:ca226305d28b 303 return res;
hydroguy45 7:ca226305d28b 304 }
hydroguy45 7:ca226305d28b 305
hydroguy45 7:ca226305d28b 306 bool ATParser::send(const char *command, ...)
hydroguy45 7:ca226305d28b 307 {
hydroguy45 7:ca226305d28b 308 va_list args;
hydroguy45 7:ca226305d28b 309 va_start(args, command);
hydroguy45 7:ca226305d28b 310 bool res = vsend(command, args);
hydroguy45 7:ca226305d28b 311 va_end(args);
hydroguy45 7:ca226305d28b 312 return res;
hydroguy45 7:ca226305d28b 313 }
hydroguy45 7:ca226305d28b 314
hydroguy45 7:ca226305d28b 315 bool ATParser::recv(const char *response, ...)
hydroguy45 7:ca226305d28b 316 {
hydroguy45 7:ca226305d28b 317 va_list args;
hydroguy45 7:ca226305d28b 318 va_start(args, response);
hydroguy45 7:ca226305d28b 319 bool res = vrecv(response, args);
hydroguy45 7:ca226305d28b 320 va_end(args);
hydroguy45 7:ca226305d28b 321 return res;
hydroguy45 7:ca226305d28b 322 }
hydroguy45 7:ca226305d28b 323
hydroguy45 7:ca226305d28b 324
hydroguy45 7:ca226305d28b 325 // oob registration
hydroguy45 7:ca226305d28b 326 void ATParser::oob(const char *prefix, Callback<void()> cb)
hydroguy45 7:ca226305d28b 327 {
hydroguy45 7:ca226305d28b 328 struct oob oob;
hydroguy45 7:ca226305d28b 329 oob.len = strlen(prefix);
hydroguy45 7:ca226305d28b 330 oob.prefix = prefix;
hydroguy45 7:ca226305d28b 331 oob.cb = cb;
hydroguy45 7:ca226305d28b 332 _oobs.push_back(oob);
hydroguy45 7:ca226305d28b 333 }