ATCmdParser.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Copyright (c) 2017 ARM Limited
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * @section DESCRIPTION
  16. *
  17. * Parser for the AT command syntax
  18. *
  19. */
  20. #ifndef MBED_ATCMDPARSER_H
  21. #define MBED_ATCMDPARSER_H
  22. #include "mbed.h"
  23. #include <cstdarg>
  24. #include "Callback.h"
  25. namespace mbed {
  26. /** \addtogroup platform */
  27. /** @{*/
  28. /**
  29. * \defgroup platform_ATCmdParser ATCmdParser class
  30. * @{
  31. */
  32. /**
  33. * Parser class for parsing AT commands
  34. *
  35. * Here are some examples:
  36. * @code
  37. * UARTSerial serial = UARTSerial(D1, D0);
  38. * ATCmdParser at = ATCmdParser(&serial, "\r\n");
  39. * int value;
  40. * char buffer[100];
  41. *
  42. * at.send("AT") && at.recv("OK");
  43. * at.send("AT+CWMODE=%d", 3) && at.recv("OK");
  44. * at.send("AT+CWMODE?") && at.recv("+CWMODE:%d\r\nOK", &value);
  45. * at.recv("+IPD,%d:", &value);
  46. * at.read(buffer, value);
  47. * at.recv("OK");
  48. * @endcode
  49. */
  50. class ATCmdParser : private NonCopyable<ATCmdParser> {
  51. private:
  52. // File handle
  53. // Not owned by ATCmdParser
  54. FileHandle *_fh;
  55. int _buffer_size;
  56. char *_buffer;
  57. int _timeout;
  58. // Parsing information
  59. const char *_output_delimiter;
  60. int _output_delim_size;
  61. char _in_prev;
  62. bool _dbg_on;
  63. bool _aborted;
  64. struct oob {
  65. unsigned len;
  66. const char *prefix;
  67. mbed::Callback<void()> cb;
  68. oob *next;
  69. };
  70. oob *_oobs;
  71. public:
  72. /**
  73. * Constructor
  74. *
  75. * @param fh A FileHandle to a digital interface to use for AT commands
  76. * @param output_delimiter end of command line termination
  77. * @param buffer_size size of internal buffer for transaction
  78. * @param timeout timeout of the connection
  79. * @param debug turns on/off debug output for AT commands
  80. */
  81. ATCmdParser(FileHandle *fh, const char *output_delimiter = "\r",
  82. int buffer_size = 256, int timeout = 8000, bool debug = false)
  83. : _fh(fh), _buffer_size(buffer_size), _in_prev(0), _oobs(NULL)
  84. {
  85. _buffer = new char[buffer_size];
  86. set_timeout(timeout);
  87. set_delimiter(output_delimiter);
  88. debug_on(debug);
  89. }
  90. /**
  91. * Destructor
  92. */
  93. ~ATCmdParser()
  94. {
  95. while (_oobs) {
  96. struct oob *oob = _oobs;
  97. _oobs = oob->next;
  98. delete oob;
  99. }
  100. delete[] _buffer;
  101. }
  102. /**
  103. * Allows timeout to be changed between commands
  104. *
  105. * @param timeout timeout of the connection
  106. */
  107. void set_timeout(int timeout)
  108. {
  109. _timeout = timeout;
  110. }
  111. /**
  112. * For backwards compatibility.
  113. * @deprecated Do not use this function. This function has been replaced with set_timeout for consistency.
  114. *
  115. * Please use set_timeout(int) API only from now on.
  116. * Allows timeout to be changed between commands
  117. *
  118. * @param timeout timeout of the connection
  119. */
  120. MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with set_timeout for consistency")
  121. void setTimeout(int timeout)
  122. {
  123. set_timeout(timeout);
  124. }
  125. /**
  126. * Sets string of characters to use as line delimiters
  127. *
  128. * @param output_delimiter string of characters to use as line delimiters
  129. */
  130. void set_delimiter(const char *output_delimiter)
  131. {
  132. _output_delimiter = output_delimiter;
  133. _output_delim_size = strlen(output_delimiter);
  134. }
  135. /**
  136. * For backwards compatibility.
  137. * @deprecated Do not use this function. This function has been replaced with set_delimiter for consistency.
  138. *
  139. * Please use set_delimiter(const char *) API only from now on.
  140. * Sets string of characters to use as line delimiters
  141. *
  142. * @param output_delimiter string of characters to use as line delimiters
  143. */
  144. MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with set_delimiter for consistency")
  145. void setDelimiter(const char *output_delimiter)
  146. {
  147. set_delimiter(output_delimiter);
  148. }
  149. /**
  150. * Allows traces from modem to be turned on or off
  151. *
  152. * @param on set as 1 to turn on traces and vice versa.
  153. */
  154. void debug_on(uint8_t on)
  155. {
  156. _dbg_on = (on) ? 1 : 0;
  157. }
  158. /**
  159. * For backwards compatibility.
  160. * @deprecated Do not use this function. This function has been replaced with debug_on for consistency.
  161. *
  162. * Allows traces from modem to be turned on or off
  163. *
  164. * @param on set as 1 to turn on traces and vice versa.
  165. */
  166. MBED_DEPRECATED_SINCE("mbed-os-5.5.0", "Replaced with debug_on for consistency")
  167. void debugOn(uint8_t on)
  168. {
  169. debug_on(on);
  170. }
  171. /**
  172. * Sends an AT command
  173. *
  174. * Sends a formatted command using printf style formatting
  175. * @see printf
  176. *
  177. * @param command printf-like format string of command to send which
  178. * is appended with a newline
  179. * @param ... all printf-like arguments to insert into command
  180. * @return true only if command is successfully sent
  181. */
  182. bool send(const char *command, ...) MBED_PRINTF_METHOD(1, 2);
  183. bool vsend(const char *command, va_list args);
  184. /**
  185. * Receive an AT response
  186. *
  187. * Receives a formatted response using scanf style formatting
  188. * @see scanf
  189. *
  190. * Responses are parsed line at a time.
  191. * Any received data that does not match the response is ignored until
  192. * a timeout occurs.
  193. *
  194. * @param response scanf-like format string of response to expect
  195. * @param ... all scanf-like arguments to extract from response
  196. * @return true only if response is successfully matched
  197. */
  198. bool recv(const char *response, ...) MBED_SCANF_METHOD(1, 2);
  199. bool vrecv(const char *response, va_list args);
  200. /**
  201. * Write a single byte to the underlying stream
  202. *
  203. * @param c The byte to write
  204. * @return The byte that was written or -1 during a timeout
  205. */
  206. int putc(char c);
  207. /**
  208. * Get a single byte from the underlying stream
  209. *
  210. * @return The byte that was read or -1 during a timeout
  211. */
  212. int getc();
  213. /**
  214. * Write an array of bytes to the underlying stream
  215. *
  216. * @param data the array of bytes to write
  217. * @param size number of bytes to write
  218. * @return number of bytes written or -1 on failure
  219. */
  220. int write(const char *data, int size);
  221. /**
  222. * Read an array of bytes from the underlying stream
  223. *
  224. * @param data the destination for the read bytes
  225. * @param size number of bytes to read
  226. * @return number of bytes read or -1 on failure
  227. */
  228. int read(char *data, int size);
  229. /**
  230. * Direct printf to underlying stream
  231. * @see printf
  232. *
  233. * @param format format string to pass to printf
  234. * @param ... arguments to printf
  235. * @return number of bytes written or -1 on failure
  236. */
  237. int printf(const char *format, ...) MBED_PRINTF_METHOD(1, 2);
  238. int vprintf(const char *format, va_list args);
  239. /**
  240. * Direct scanf on underlying stream
  241. * @see scanf
  242. *
  243. * @param format format string to pass to scanf
  244. * @param ... arguments to scanf
  245. * @return number of bytes read or -1 on failure
  246. */
  247. int scanf(const char *format, ...) MBED_SCANF_METHOD(1, 2);
  248. int vscanf(const char *format, va_list args);
  249. /**
  250. * Attach a callback for out-of-band data
  251. *
  252. * @param prefix string on when to initiate callback
  253. * @param func callback to call when string is read
  254. * @note out-of-band data is only processed during a scanf call
  255. */
  256. void oob(const char *prefix, mbed::Callback<void()> func);
  257. /**
  258. * Flushes the underlying stream
  259. */
  260. void flush();
  261. /**
  262. * Abort current recv
  263. *
  264. * Can be called from oob handler to interrupt the current
  265. * recv operation.
  266. */
  267. void abort();
  268. /**
  269. * Process out-of-band data
  270. *
  271. * Process out-of-band data in the receive buffer. This function
  272. * returns immediately if there is no data to process.
  273. *
  274. * @return true if oob data processed, false otherwise
  275. */
  276. bool process_oob(void);
  277. };
  278. /**@}*/
  279. /**@}*/
  280. } //namespace mbed
  281. #endif //MBED_ATCMDPARSER_H