Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:02dd72d1d465 1 /* Copyright (c) 2017 ARM Limited
borlanic 0:02dd72d1d465 2 *
borlanic 0:02dd72d1d465 3 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:02dd72d1d465 4 * you may not use this file except in compliance with the License.
borlanic 0:02dd72d1d465 5 * You may obtain a copy of the License at
borlanic 0:02dd72d1d465 6 *
borlanic 0:02dd72d1d465 7 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:02dd72d1d465 8 *
borlanic 0:02dd72d1d465 9 * Unless required by applicable law or agreed to in writing, software
borlanic 0:02dd72d1d465 10 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:02dd72d1d465 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:02dd72d1d465 12 * See the License for the specific language governing permissions and
borlanic 0:02dd72d1d465 13 * limitations under the License.
borlanic 0:02dd72d1d465 14 *
borlanic 0:02dd72d1d465 15 * @section DESCRIPTION
borlanic 0:02dd72d1d465 16 *
borlanic 0:02dd72d1d465 17 * Parser for the AT command syntax
borlanic 0:02dd72d1d465 18 *
borlanic 0:02dd72d1d465 19 */
borlanic 0:02dd72d1d465 20
borlanic 0:02dd72d1d465 21 #include "ATCmdParser.h"
borlanic 0:02dd72d1d465 22 #include "mbed_poll.h"
borlanic 0:02dd72d1d465 23 #include "mbed_debug.h"
borlanic 0:02dd72d1d465 24
borlanic 0:02dd72d1d465 25 #ifdef LF
borlanic 0:02dd72d1d465 26 #undef LF
borlanic 0:02dd72d1d465 27 #define LF 10
borlanic 0:02dd72d1d465 28 #else
borlanic 0:02dd72d1d465 29 #define LF 10
borlanic 0:02dd72d1d465 30 #endif
borlanic 0:02dd72d1d465 31
borlanic 0:02dd72d1d465 32 #ifdef CR
borlanic 0:02dd72d1d465 33 #undef CR
borlanic 0:02dd72d1d465 34 #define CR 13
borlanic 0:02dd72d1d465 35 #else
borlanic 0:02dd72d1d465 36 #define CR 13
borlanic 0:02dd72d1d465 37 #endif
borlanic 0:02dd72d1d465 38
borlanic 0:02dd72d1d465 39 // getc/putc handling with timeouts
borlanic 0:02dd72d1d465 40 int ATCmdParser::putc(char c)
borlanic 0:02dd72d1d465 41 {
borlanic 0:02dd72d1d465 42 pollfh fhs;
borlanic 0:02dd72d1d465 43 fhs.fh = _fh;
borlanic 0:02dd72d1d465 44 fhs.events = POLLOUT;
borlanic 0:02dd72d1d465 45
borlanic 0:02dd72d1d465 46 int count = poll(&fhs, 1, _timeout);
borlanic 0:02dd72d1d465 47 if (count > 0 && (fhs.revents & POLLOUT)) {
borlanic 0:02dd72d1d465 48 return _fh->write(&c, 1) == 1 ? 0 : -1;
borlanic 0:02dd72d1d465 49 } else {
borlanic 0:02dd72d1d465 50 return -1;
borlanic 0:02dd72d1d465 51 }
borlanic 0:02dd72d1d465 52 }
borlanic 0:02dd72d1d465 53
borlanic 0:02dd72d1d465 54 int ATCmdParser::getc()
borlanic 0:02dd72d1d465 55 {
borlanic 0:02dd72d1d465 56 pollfh fhs;
borlanic 0:02dd72d1d465 57 fhs.fh = _fh;
borlanic 0:02dd72d1d465 58 fhs.events = POLLIN;
borlanic 0:02dd72d1d465 59
borlanic 0:02dd72d1d465 60 int count = poll(&fhs, 1, _timeout);
borlanic 0:02dd72d1d465 61 if (count > 0 && (fhs.revents & POLLIN)) {
borlanic 0:02dd72d1d465 62 unsigned char ch;
borlanic 0:02dd72d1d465 63 return _fh->read(&ch, 1) == 1 ? ch : -1;
borlanic 0:02dd72d1d465 64 } else {
borlanic 0:02dd72d1d465 65 return -1;
borlanic 0:02dd72d1d465 66 }
borlanic 0:02dd72d1d465 67 }
borlanic 0:02dd72d1d465 68
borlanic 0:02dd72d1d465 69 void ATCmdParser::flush()
borlanic 0:02dd72d1d465 70 {
borlanic 0:02dd72d1d465 71 while (_fh->readable()) {
borlanic 0:02dd72d1d465 72 unsigned char ch;
borlanic 0:02dd72d1d465 73 _fh->read(&ch, 1);
borlanic 0:02dd72d1d465 74 }
borlanic 0:02dd72d1d465 75 }
borlanic 0:02dd72d1d465 76
borlanic 0:02dd72d1d465 77
borlanic 0:02dd72d1d465 78 // read/write handling with timeouts
borlanic 0:02dd72d1d465 79 int ATCmdParser::write(const char *data, int size)
borlanic 0:02dd72d1d465 80 {
borlanic 0:02dd72d1d465 81 int i = 0;
borlanic 0:02dd72d1d465 82 for ( ; i < size; i++) {
borlanic 0:02dd72d1d465 83 if (putc(data[i]) < 0) {
borlanic 0:02dd72d1d465 84 return -1;
borlanic 0:02dd72d1d465 85 }
borlanic 0:02dd72d1d465 86 }
borlanic 0:02dd72d1d465 87 return i;
borlanic 0:02dd72d1d465 88 }
borlanic 0:02dd72d1d465 89
borlanic 0:02dd72d1d465 90 int ATCmdParser::read(char *data, int size)
borlanic 0:02dd72d1d465 91 {
borlanic 0:02dd72d1d465 92 int i = 0;
borlanic 0:02dd72d1d465 93 for ( ; i < size; i++) {
borlanic 0:02dd72d1d465 94 int c = getc();
borlanic 0:02dd72d1d465 95 if (c < 0) {
borlanic 0:02dd72d1d465 96 return -1;
borlanic 0:02dd72d1d465 97 }
borlanic 0:02dd72d1d465 98 data[i] = c;
borlanic 0:02dd72d1d465 99 }
borlanic 0:02dd72d1d465 100 return i;
borlanic 0:02dd72d1d465 101 }
borlanic 0:02dd72d1d465 102
borlanic 0:02dd72d1d465 103
borlanic 0:02dd72d1d465 104 // printf/scanf handling
borlanic 0:02dd72d1d465 105 int ATCmdParser::vprintf(const char *format, va_list args)
borlanic 0:02dd72d1d465 106 {
borlanic 0:02dd72d1d465 107
borlanic 0:02dd72d1d465 108 if (vsprintf(_buffer, format, args) < 0) {
borlanic 0:02dd72d1d465 109 return false;
borlanic 0:02dd72d1d465 110 }
borlanic 0:02dd72d1d465 111
borlanic 0:02dd72d1d465 112 int i = 0;
borlanic 0:02dd72d1d465 113 for ( ; _buffer[i]; i++) {
borlanic 0:02dd72d1d465 114 if (putc(_buffer[i]) < 0) {
borlanic 0:02dd72d1d465 115 return -1;
borlanic 0:02dd72d1d465 116 }
borlanic 0:02dd72d1d465 117 }
borlanic 0:02dd72d1d465 118 return i;
borlanic 0:02dd72d1d465 119 }
borlanic 0:02dd72d1d465 120
borlanic 0:02dd72d1d465 121 int ATCmdParser::vscanf(const char *format, va_list args)
borlanic 0:02dd72d1d465 122 {
borlanic 0:02dd72d1d465 123 // Since format is const, we need to copy it into our buffer to
borlanic 0:02dd72d1d465 124 // add the line's null terminator and clobber value-matches with asterisks.
borlanic 0:02dd72d1d465 125 //
borlanic 0:02dd72d1d465 126 // We just use the beginning of the buffer to avoid unnecessary allocations.
borlanic 0:02dd72d1d465 127 int i = 0;
borlanic 0:02dd72d1d465 128 int offset = 0;
borlanic 0:02dd72d1d465 129
borlanic 0:02dd72d1d465 130 while (format[i]) {
borlanic 0:02dd72d1d465 131 if (format[i] == '%' && format[i+1] != '%' && format[i+1] != '*') {
borlanic 0:02dd72d1d465 132 _buffer[offset++] = '%';
borlanic 0:02dd72d1d465 133 _buffer[offset++] = '*';
borlanic 0:02dd72d1d465 134 i++;
borlanic 0:02dd72d1d465 135 } else {
borlanic 0:02dd72d1d465 136 _buffer[offset++] = format[i++];
borlanic 0:02dd72d1d465 137 }
borlanic 0:02dd72d1d465 138 }
borlanic 0:02dd72d1d465 139
borlanic 0:02dd72d1d465 140 // Scanf has very poor support for catching errors
borlanic 0:02dd72d1d465 141 // fortunately, we can abuse the %n specifier to determine
borlanic 0:02dd72d1d465 142 // if the entire string was matched.
borlanic 0:02dd72d1d465 143 _buffer[offset++] = '%';
borlanic 0:02dd72d1d465 144 _buffer[offset++] = 'n';
borlanic 0:02dd72d1d465 145 _buffer[offset++] = 0;
borlanic 0:02dd72d1d465 146
borlanic 0:02dd72d1d465 147 // To workaround scanf's lack of error reporting, we actually
borlanic 0:02dd72d1d465 148 // make two passes. One checks the validity with the modified
borlanic 0:02dd72d1d465 149 // format string that only stores the matched characters (%n).
borlanic 0:02dd72d1d465 150 // The other reads in the actual matched values.
borlanic 0:02dd72d1d465 151 //
borlanic 0:02dd72d1d465 152 // We keep trying the match until we succeed or some other error
borlanic 0:02dd72d1d465 153 // derails us.
borlanic 0:02dd72d1d465 154 int j = 0;
borlanic 0:02dd72d1d465 155
borlanic 0:02dd72d1d465 156 while (true) {
borlanic 0:02dd72d1d465 157 // Ran out of space
borlanic 0:02dd72d1d465 158 if (j+1 >= _buffer_size - offset) {
borlanic 0:02dd72d1d465 159 return false;
borlanic 0:02dd72d1d465 160 }
borlanic 0:02dd72d1d465 161 // Receive next character
borlanic 0:02dd72d1d465 162 int c = getc();
borlanic 0:02dd72d1d465 163 if (c < 0) {
borlanic 0:02dd72d1d465 164 return -1;
borlanic 0:02dd72d1d465 165 }
borlanic 0:02dd72d1d465 166 _buffer[offset + j++] = c;
borlanic 0:02dd72d1d465 167 _buffer[offset + j] = 0;
borlanic 0:02dd72d1d465 168
borlanic 0:02dd72d1d465 169 // Check for match
borlanic 0:02dd72d1d465 170 int count = -1;
borlanic 0:02dd72d1d465 171 sscanf(_buffer+offset, _buffer, &count);
borlanic 0:02dd72d1d465 172
borlanic 0:02dd72d1d465 173 // We only succeed if all characters in the response are matched
borlanic 0:02dd72d1d465 174 if (count == j) {
borlanic 0:02dd72d1d465 175 // Store the found results
borlanic 0:02dd72d1d465 176 vsscanf(_buffer+offset, format, args);
borlanic 0:02dd72d1d465 177 return j;
borlanic 0:02dd72d1d465 178 }
borlanic 0:02dd72d1d465 179 }
borlanic 0:02dd72d1d465 180 }
borlanic 0:02dd72d1d465 181
borlanic 0:02dd72d1d465 182
borlanic 0:02dd72d1d465 183 // Command parsing with line handling
borlanic 0:02dd72d1d465 184 bool ATCmdParser::vsend(const char *command, va_list args)
borlanic 0:02dd72d1d465 185 {
borlanic 0:02dd72d1d465 186 // Create and send command
borlanic 0:02dd72d1d465 187 if (vsprintf(_buffer, command, args) < 0) {
borlanic 0:02dd72d1d465 188 return false;
borlanic 0:02dd72d1d465 189 }
borlanic 0:02dd72d1d465 190
borlanic 0:02dd72d1d465 191 for (int i = 0; _buffer[i]; i++) {
borlanic 0:02dd72d1d465 192 if (putc(_buffer[i]) < 0) {
borlanic 0:02dd72d1d465 193 return false;
borlanic 0:02dd72d1d465 194 }
borlanic 0:02dd72d1d465 195 }
borlanic 0:02dd72d1d465 196
borlanic 0:02dd72d1d465 197 // Finish with newline
borlanic 0:02dd72d1d465 198 for (size_t i = 0; _output_delimiter[i]; i++) {
borlanic 0:02dd72d1d465 199 if (putc(_output_delimiter[i]) < 0) {
borlanic 0:02dd72d1d465 200 return false;
borlanic 0:02dd72d1d465 201 }
borlanic 0:02dd72d1d465 202 }
borlanic 0:02dd72d1d465 203
borlanic 0:02dd72d1d465 204 debug_if(_dbg_on, "AT> %s\n", _buffer);
borlanic 0:02dd72d1d465 205 return true;
borlanic 0:02dd72d1d465 206 }
borlanic 0:02dd72d1d465 207
borlanic 0:02dd72d1d465 208 bool ATCmdParser::vrecv(const char *response, va_list args)
borlanic 0:02dd72d1d465 209 {
borlanic 0:02dd72d1d465 210 restart:
borlanic 0:02dd72d1d465 211 _aborted = false;
borlanic 0:02dd72d1d465 212 // Iterate through each line in the expected response
borlanic 0:02dd72d1d465 213 while (response[0]) {
borlanic 0:02dd72d1d465 214 // Since response is const, we need to copy it into our buffer to
borlanic 0:02dd72d1d465 215 // add the line's null terminator and clobber value-matches with asterisks.
borlanic 0:02dd72d1d465 216 //
borlanic 0:02dd72d1d465 217 // We just use the beginning of the buffer to avoid unnecessary allocations.
borlanic 0:02dd72d1d465 218 int i = 0;
borlanic 0:02dd72d1d465 219 int offset = 0;
borlanic 0:02dd72d1d465 220 bool whole_line_wanted = false;
borlanic 0:02dd72d1d465 221
borlanic 0:02dd72d1d465 222 while (response[i]) {
borlanic 0:02dd72d1d465 223 if (response[i] == '%' && response[i+1] != '%' && response[i+1] != '*') {
borlanic 0:02dd72d1d465 224 _buffer[offset++] = '%';
borlanic 0:02dd72d1d465 225 _buffer[offset++] = '*';
borlanic 0:02dd72d1d465 226 i++;
borlanic 0:02dd72d1d465 227 } else {
borlanic 0:02dd72d1d465 228 _buffer[offset++] = response[i++];
borlanic 0:02dd72d1d465 229 // Find linebreaks, taking care not to be fooled if they're in a %[^\n] conversion specification
borlanic 0:02dd72d1d465 230 if (response[i - 1] == '\n' && !(i >= 3 && response[i-3] == '[' && response[i-2] == '^')) {
borlanic 0:02dd72d1d465 231 whole_line_wanted = true;
borlanic 0:02dd72d1d465 232 break;
borlanic 0:02dd72d1d465 233 }
borlanic 0:02dd72d1d465 234 }
borlanic 0:02dd72d1d465 235 }
borlanic 0:02dd72d1d465 236
borlanic 0:02dd72d1d465 237 // Scanf has very poor support for catching errors
borlanic 0:02dd72d1d465 238 // fortunately, we can abuse the %n specifier to determine
borlanic 0:02dd72d1d465 239 // if the entire string was matched.
borlanic 0:02dd72d1d465 240 _buffer[offset++] = '%';
borlanic 0:02dd72d1d465 241 _buffer[offset++] = 'n';
borlanic 0:02dd72d1d465 242 _buffer[offset++] = 0;
borlanic 0:02dd72d1d465 243
borlanic 0:02dd72d1d465 244 debug_if(_dbg_on, "AT? %s\n", _buffer);
borlanic 0:02dd72d1d465 245 // To workaround scanf's lack of error reporting, we actually
borlanic 0:02dd72d1d465 246 // make two passes. One checks the validity with the modified
borlanic 0:02dd72d1d465 247 // format string that only stores the matched characters (%n).
borlanic 0:02dd72d1d465 248 // The other reads in the actual matched values.
borlanic 0:02dd72d1d465 249 //
borlanic 0:02dd72d1d465 250 // We keep trying the match until we succeed or some other error
borlanic 0:02dd72d1d465 251 // derails us.
borlanic 0:02dd72d1d465 252 int j = 0;
borlanic 0:02dd72d1d465 253
borlanic 0:02dd72d1d465 254 while (true) {
borlanic 0:02dd72d1d465 255 // Receive next character
borlanic 0:02dd72d1d465 256 int c = getc();
borlanic 0:02dd72d1d465 257 if (c < 0) {
borlanic 0:02dd72d1d465 258 debug_if(_dbg_on, "AT(Timeout)\n");
borlanic 0:02dd72d1d465 259 return false;
borlanic 0:02dd72d1d465 260 }
borlanic 0:02dd72d1d465 261 // Simplify newlines (borrowed from retarget.cpp)
borlanic 0:02dd72d1d465 262 if ((c == CR && _in_prev != LF) ||
borlanic 0:02dd72d1d465 263 (c == LF && _in_prev != CR)) {
borlanic 0:02dd72d1d465 264 _in_prev = c;
borlanic 0:02dd72d1d465 265 c = '\n';
borlanic 0:02dd72d1d465 266 } else if ((c == CR && _in_prev == LF) ||
borlanic 0:02dd72d1d465 267 (c == LF && _in_prev == CR)) {
borlanic 0:02dd72d1d465 268 _in_prev = c;
borlanic 0:02dd72d1d465 269 // onto next character
borlanic 0:02dd72d1d465 270 continue;
borlanic 0:02dd72d1d465 271 } else {
borlanic 0:02dd72d1d465 272 _in_prev = c;
borlanic 0:02dd72d1d465 273 }
borlanic 0:02dd72d1d465 274 _buffer[offset + j++] = c;
borlanic 0:02dd72d1d465 275 _buffer[offset + j] = 0;
borlanic 0:02dd72d1d465 276
borlanic 0:02dd72d1d465 277 // Check for oob data
borlanic 0:02dd72d1d465 278 for (struct oob *oob = _oobs; oob; oob = oob->next) {
borlanic 0:02dd72d1d465 279 if ((unsigned)j == oob->len && memcmp(
borlanic 0:02dd72d1d465 280 oob->prefix, _buffer+offset, oob->len) == 0) {
borlanic 0:02dd72d1d465 281 debug_if(_dbg_on, "AT! %s\n", oob->prefix);
borlanic 0:02dd72d1d465 282 oob->cb();
borlanic 0:02dd72d1d465 283
borlanic 0:02dd72d1d465 284 if (_aborted) {
borlanic 0:02dd72d1d465 285 debug_if(_dbg_on, "AT(Aborted)\n");
borlanic 0:02dd72d1d465 286 return false;
borlanic 0:02dd72d1d465 287 }
borlanic 0:02dd72d1d465 288 // oob may have corrupted non-reentrant buffer,
borlanic 0:02dd72d1d465 289 // so we need to set it up again
borlanic 0:02dd72d1d465 290 goto restart;
borlanic 0:02dd72d1d465 291 }
borlanic 0:02dd72d1d465 292 }
borlanic 0:02dd72d1d465 293
borlanic 0:02dd72d1d465 294 // Check for match
borlanic 0:02dd72d1d465 295 int count = -1;
borlanic 0:02dd72d1d465 296 if (whole_line_wanted && c != '\n') {
borlanic 0:02dd72d1d465 297 // Don't attempt scanning until we get delimiter if they included it in format
borlanic 0:02dd72d1d465 298 // This allows recv("Foo: %s\n") to work, and not match with just the first character of a string
borlanic 0:02dd72d1d465 299 // (scanf does not itself match whitespace in its format string, so \n is not significant to it)
borlanic 0:02dd72d1d465 300 } else {
borlanic 0:02dd72d1d465 301 sscanf(_buffer+offset, _buffer, &count);
borlanic 0:02dd72d1d465 302 }
borlanic 0:02dd72d1d465 303
borlanic 0:02dd72d1d465 304 // We only succeed if all characters in the response are matched
borlanic 0:02dd72d1d465 305 if (count == j) {
borlanic 0:02dd72d1d465 306 debug_if(_dbg_on, "AT= %s\n", _buffer+offset);
borlanic 0:02dd72d1d465 307 // Reuse the front end of the buffer
borlanic 0:02dd72d1d465 308 memcpy(_buffer, response, i);
borlanic 0:02dd72d1d465 309 _buffer[i] = 0;
borlanic 0:02dd72d1d465 310
borlanic 0:02dd72d1d465 311 // Store the found results
borlanic 0:02dd72d1d465 312 vsscanf(_buffer+offset, _buffer, args);
borlanic 0:02dd72d1d465 313
borlanic 0:02dd72d1d465 314 // Jump to next line and continue parsing
borlanic 0:02dd72d1d465 315 response += i;
borlanic 0:02dd72d1d465 316 break;
borlanic 0:02dd72d1d465 317 }
borlanic 0:02dd72d1d465 318
borlanic 0:02dd72d1d465 319 // Clear the buffer when we hit a newline or ran out of space
borlanic 0:02dd72d1d465 320 // running out of space usually means we ran into binary data
borlanic 0:02dd72d1d465 321 if (c == '\n' || j+1 >= _buffer_size - offset) {
borlanic 0:02dd72d1d465 322 debug_if(_dbg_on, "AT< %s", _buffer+offset);
borlanic 0:02dd72d1d465 323 j = 0;
borlanic 0:02dd72d1d465 324 }
borlanic 0:02dd72d1d465 325 }
borlanic 0:02dd72d1d465 326 }
borlanic 0:02dd72d1d465 327
borlanic 0:02dd72d1d465 328 return true;
borlanic 0:02dd72d1d465 329 }
borlanic 0:02dd72d1d465 330
borlanic 0:02dd72d1d465 331 // Mapping to vararg functions
borlanic 0:02dd72d1d465 332 int ATCmdParser::printf(const char *format, ...)
borlanic 0:02dd72d1d465 333 {
borlanic 0:02dd72d1d465 334 va_list args;
borlanic 0:02dd72d1d465 335 va_start(args, format);
borlanic 0:02dd72d1d465 336 int res = vprintf(format, args);
borlanic 0:02dd72d1d465 337 va_end(args);
borlanic 0:02dd72d1d465 338 return res;
borlanic 0:02dd72d1d465 339 }
borlanic 0:02dd72d1d465 340
borlanic 0:02dd72d1d465 341 int ATCmdParser::scanf(const char *format, ...)
borlanic 0:02dd72d1d465 342 {
borlanic 0:02dd72d1d465 343 va_list args;
borlanic 0:02dd72d1d465 344 va_start(args, format);
borlanic 0:02dd72d1d465 345 int res = vscanf(format, args);
borlanic 0:02dd72d1d465 346 va_end(args);
borlanic 0:02dd72d1d465 347 return res;
borlanic 0:02dd72d1d465 348 }
borlanic 0:02dd72d1d465 349
borlanic 0:02dd72d1d465 350 bool ATCmdParser::send(const char *command, ...)
borlanic 0:02dd72d1d465 351 {
borlanic 0:02dd72d1d465 352 va_list args;
borlanic 0:02dd72d1d465 353 va_start(args, command);
borlanic 0:02dd72d1d465 354 bool res = vsend(command, args);
borlanic 0:02dd72d1d465 355 va_end(args);
borlanic 0:02dd72d1d465 356 return res;
borlanic 0:02dd72d1d465 357 }
borlanic 0:02dd72d1d465 358
borlanic 0:02dd72d1d465 359 bool ATCmdParser::recv(const char *response, ...)
borlanic 0:02dd72d1d465 360 {
borlanic 0:02dd72d1d465 361 va_list args;
borlanic 0:02dd72d1d465 362 va_start(args, response);
borlanic 0:02dd72d1d465 363 bool res = vrecv(response, args);
borlanic 0:02dd72d1d465 364 va_end(args);
borlanic 0:02dd72d1d465 365 return res;
borlanic 0:02dd72d1d465 366 }
borlanic 0:02dd72d1d465 367
borlanic 0:02dd72d1d465 368 // oob registration
borlanic 0:02dd72d1d465 369 void ATCmdParser::oob(const char *prefix, Callback<void()> cb)
borlanic 0:02dd72d1d465 370 {
borlanic 0:02dd72d1d465 371 struct oob *oob = new struct oob;
borlanic 0:02dd72d1d465 372 oob->len = strlen(prefix);
borlanic 0:02dd72d1d465 373 oob->prefix = prefix;
borlanic 0:02dd72d1d465 374 oob->cb = cb;
borlanic 0:02dd72d1d465 375 oob->next = _oobs;
borlanic 0:02dd72d1d465 376 _oobs = oob;
borlanic 0:02dd72d1d465 377 }
borlanic 0:02dd72d1d465 378
borlanic 0:02dd72d1d465 379 void ATCmdParser::abort()
borlanic 0:02dd72d1d465 380 {
borlanic 0:02dd72d1d465 381 _aborted = true;
borlanic 0:02dd72d1d465 382 }
borlanic 0:02dd72d1d465 383
borlanic 0:02dd72d1d465 384 bool ATCmdParser::process_oob()
borlanic 0:02dd72d1d465 385 {
borlanic 0:02dd72d1d465 386 if (!_fh->readable()) {
borlanic 0:02dd72d1d465 387 return false;
borlanic 0:02dd72d1d465 388 }
borlanic 0:02dd72d1d465 389
borlanic 0:02dd72d1d465 390 int i = 0;
borlanic 0:02dd72d1d465 391 while (true) {
borlanic 0:02dd72d1d465 392 // Receive next character
borlanic 0:02dd72d1d465 393 int c = getc();
borlanic 0:02dd72d1d465 394 if (c < 0) {
borlanic 0:02dd72d1d465 395 return false;
borlanic 0:02dd72d1d465 396 }
borlanic 0:02dd72d1d465 397 // Simplify newlines (borrowed from retarget.cpp)
borlanic 0:02dd72d1d465 398 if ((c == CR && _in_prev != LF) ||
borlanic 0:02dd72d1d465 399 (c == LF && _in_prev != CR)) {
borlanic 0:02dd72d1d465 400 _in_prev = c;
borlanic 0:02dd72d1d465 401 c = '\n';
borlanic 0:02dd72d1d465 402 } else if ((c == CR && _in_prev == LF) ||
borlanic 0:02dd72d1d465 403 (c == LF && _in_prev == CR)) {
borlanic 0:02dd72d1d465 404 _in_prev = c;
borlanic 0:02dd72d1d465 405 // onto next character
borlanic 0:02dd72d1d465 406 continue;
borlanic 0:02dd72d1d465 407 } else {
borlanic 0:02dd72d1d465 408 _in_prev = c;
borlanic 0:02dd72d1d465 409 }
borlanic 0:02dd72d1d465 410 _buffer[i++] = c;
borlanic 0:02dd72d1d465 411 _buffer[i] = 0;
borlanic 0:02dd72d1d465 412
borlanic 0:02dd72d1d465 413 // Check for oob data
borlanic 0:02dd72d1d465 414 struct oob *oob = _oobs;
borlanic 0:02dd72d1d465 415 while (oob) {
borlanic 0:02dd72d1d465 416 if (i == (int)oob->len && memcmp(
borlanic 0:02dd72d1d465 417 oob->prefix, _buffer, oob->len) == 0) {
borlanic 0:02dd72d1d465 418 debug_if(_dbg_on, "AT! %s\r\n", oob->prefix);
borlanic 0:02dd72d1d465 419 oob->cb();
borlanic 0:02dd72d1d465 420 return true;
borlanic 0:02dd72d1d465 421 }
borlanic 0:02dd72d1d465 422 oob = oob->next;
borlanic 0:02dd72d1d465 423 }
borlanic 0:02dd72d1d465 424
borlanic 0:02dd72d1d465 425 // Clear the buffer when we hit a newline or ran out of space
borlanic 0:02dd72d1d465 426 // running out of space usually means we ran into binary data
borlanic 0:02dd72d1d465 427 if (((i+1) >= _buffer_size) || (c == '\n')) {
borlanic 0:02dd72d1d465 428 debug_if(_dbg_on, "AT< %s", _buffer);
borlanic 0:02dd72d1d465 429 i = 0;
borlanic 0:02dd72d1d465 430 }
borlanic 0:02dd72d1d465 431 }
borlanic 0:02dd72d1d465 432 }
borlanic 0:02dd72d1d465 433
borlanic 0:02dd72d1d465 434