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