Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ATHandler.h Source File

ATHandler.h

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef AT_HANDLER_H_
00019 #define AT_HANDLER_H_
00020 
00021 #include "platform/mbed_retarget.h"
00022 
00023 #include "events/EventQueue.h"
00024 #include "nsapi_types.h"
00025 
00026 #include "Callback.h"
00027 
00028 #include <cstdarg>
00029 
00030 #include "UARTSerial.h"
00031 
00032 /**
00033  * If application calls associated FileHandle only from single thread context
00034   * then locking between AT command and response is not needed. However,
00035   * note that many cellular functions are called indirectly, for example with the socket API.
00036   * If you are unsure, then AT_HANDLER_MUTEX must be defined.
00037   */
00038 #define AT_HANDLER_MUTEX
00039 
00040 #if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT
00041 #include "ConditionVariable.h"
00042 #endif
00043 
00044 namespace mbed {
00045 
00046 class FileHandle;
00047 
00048 extern const char *OK;
00049 extern const char *CRLF;
00050 
00051 #define BUFF_SIZE 32
00052 
00053 /* AT Error types enumeration */
00054 enum DeviceErrorType {
00055     DeviceErrorTypeNoError = 0,
00056     DeviceErrorTypeError,       // AT ERROR
00057     DeviceErrorTypeErrorCMS,    // AT ERROR CMS
00058     DeviceErrorTypeErrorCME     // AT ERROR CME
00059 };
00060 
00061 /** AT response error with error code and type */
00062 struct device_err_t {
00063     DeviceErrorType errType;
00064     int errCode;
00065 };
00066 
00067 /// Class for sending AT commands and parsing AT responses.
00068 class ATHandler {
00069 
00070 public:
00071     /** Constructor
00072      *
00073      *  @param fh               file handle used for reading AT responses and writing AT commands
00074      *  @param queue            Event queue used to transfer sigio events to this thread
00075      *  @param timeout          Timeout when reading for AT response
00076      *  @param output_delimiter delimiter used when parsing at responses, "\r" should be used as output_delimiter
00077      *  @param send_delay       the minimum delay in ms between the end of last response and the beginning of a new command
00078      */
00079     ATHandler(FileHandle *fh, events::EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay = 0);
00080     virtual ~ATHandler();
00081 
00082     /** Return used file handle.
00083      *
00084      *  @return used file handle
00085      */
00086     FileHandle *get_file_handle();
00087 
00088     /** Get a new ATHandler instance, and update the linked list. Once the use of the ATHandler
00089      *  has finished, call to close() has to be made
00090      *
00091      *  @param fileHandle       filehandle used for reading AT responses and writing AT commands.
00092      *                          If there is already an ATHandler with the same fileHandle pointer,
00093      *                          then a pointer to that ATHandler instance will be returned with
00094      *                          that ATHandler's queue, timeout, delimiter, send_delay and debug_on
00095      *                          values
00096      *  @param queue            Event queue used to transfer sigio events to this thread
00097      *  @param timeout          Timeout when reading for AT response
00098      *  @param delimiter        delimiter used when parsing at responses, "\r" should be used as output_delimiter
00099      *  @param send_delay       the minimum delay in ms between the end of last response and the beginning of a new command
00100      *  @param debug_on         Set true to enable debug traces
00101      *  @return                 NULL, if fileHandle is not set, or a pointer to an existing ATHandler, if the fileHandle is
00102      *                          already in use. Otherwise a pointer to a new ATHandler instance is returned
00103      */
00104     static ATHandler *get_instance(FileHandle *fileHandle, events::EventQueue &queue, uint32_t timeout,
00105                                    const char *delimiter, uint16_t send_delay, bool debug_on);
00106 
00107     /** Close and delete the current ATHandler instance, if the reference count to it is 0.
00108      *  Close() can be only called, if the ATHandler was opened with get_instance()
00109      *
00110      *  @return NSAPI_ERROR_OK on success, NSAPI_ERROR_PARAMETER on failure
00111      */
00112     nsapi_error_t close();
00113 
00114     /** Locks the mutex for file handle if AT_HANDLER_MUTEX is defined.
00115      */
00116     void lock();
00117 
00118     /** Unlocks the mutex for file handle if AT_HANDLER_MUTEX is defined.
00119      */
00120     void unlock();
00121 
00122     /** Locks the mutex for file handle if AT_HANDLER_MUTEX is defined and returns the last error.
00123      *
00124      * @return last error that happened when parsing AT responses
00125      */
00126     nsapi_error_t unlock_return_error();
00127 
00128     /** Set callback function for URC
00129      *
00130      *  @param prefix   URC text to look for, e.g. "+CMTI:". Maximum length is BUFF_SIZE.
00131      *  @param callback function to call on prefix, or 0 to remove callback
00132      */
00133     void set_urc_handler(const char *prefix, Callback<void()> callback);
00134 
00135     ATHandler *_nextATHandler; // linked list
00136 
00137     /** returns the last error while parsing AT responses.
00138      *
00139      *  @return last error
00140      */
00141     nsapi_error_t get_last_error() const;
00142 
00143     /** returns the last device error while parsing AT responses. Actually AT error (CME/CMS).
00144      *
00145      *  @return last error struct device_err_t
00146      */
00147     device_err_t get_last_device_error() const;
00148 
00149     /** Increase reference count. Used for counting references to this instance.
00150      *  Note that this should be used with care, if the ATHandler was taken into use
00151      *  with get_instance()
00152      */
00153     void inc_ref_count();
00154 
00155     /** Decrease reference count. Used for counting references to this instance.
00156      *  Note that this should be used with care, if the ATHandler was taken into use
00157      *  with get_instance()
00158      */
00159     void dec_ref_count();
00160 
00161     /** Get the current reference count. Used for counting references to this instance.
00162      *
00163      *  @return current reference count
00164      */
00165     int get_ref_count();
00166 
00167     /** Set timeout in milliseconds for AT commands
00168      *
00169      *  @param timeout_milliseconds  Timeout in milliseconds
00170      *  @param default_timeout       Store as default timeout
00171      */
00172     void set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout = false);
00173 
00174     /** Set timeout in milliseconds for all ATHandlers in the _atHandlers list
00175      *
00176      *  @param timeout_milliseconds  Timeout in milliseconds
00177      *  @param default_timeout       Store as default timeout
00178      */
00179     static void set_at_timeout_list(uint32_t timeout_milliseconds, bool default_timeout = false);
00180 
00181     /** Restore timeout to previous timeout. Handy if there is a need to change timeout temporarily.
00182      */
00183     void restore_at_timeout();
00184 
00185     /** Clear pending error flag. By default, error is cleared only in lock().
00186      */
00187     void clear_error();
00188 
00189     /**
00190      * Flushes the underlying stream
00191      */
00192     void flush();
00193 
00194     /** Tries to find oob's from the AT response. Call the urc callback if one is found.
00195      */
00196     void process_oob();
00197 
00198     /** Set file handle, which is used for reading AT responses and writing AT commands
00199      *
00200      *  @param fh file handle used for reading AT responses and writing AT commands
00201      */
00202     void set_file_handle(FileHandle *fh);
00203 
00204     /** Set is file handle usable. Some situations like after going to data mode, file handle is not usable anymore.
00205      *  Any items in queue are not to be processed.
00206      *
00207      *  @param usable true for usable filehandle
00208      */
00209     void set_is_filehandle_usable(bool usable);
00210 
00211     /** Synchronize AT command and response handling to modem.
00212      *
00213      *  @param timeout_ms ATHandler timeout when trying to sync. Will be restored when function returns.
00214      *  @return true is synchronization was successful, false in case of failure
00215      */
00216     bool sync(int timeout_ms);
00217 
00218     /** Sets the delay to be applied before sending any AT command.
00219      *
00220      *  @param send_delay the minimum delay in ms between the end of last response and the beginning of a new command
00221      */
00222     void set_send_delay(uint16_t send_delay);
00223 
00224     /** Sets UARTSerial filehandle to given baud rate
00225      *
00226      *  @param baud_rate
00227      */
00228     void set_baud(int baud_rate);
00229 
00230 protected:
00231     void event();
00232 #if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT
00233     rtos::Mutex _fileHandleMutex;
00234     rtos::ConditionVariable _oobCv;
00235 #endif
00236     FileHandle *_fileHandle;
00237 private:
00238     /** Remove urc handler from linked list of urc's
00239      *
00240      *  @param prefix   Register urc prefix for callback. Urc could be for example "+CMTI: "
00241      */
00242     void remove_urc_handler(const char *prefix);
00243 
00244     void set_error(nsapi_error_t err);
00245 
00246     events::EventQueue &_queue;
00247     nsapi_error_t _last_err;
00248     int _last_3gpp_error;
00249     device_err_t  _last_at_err;
00250     uint16_t _oob_string_max_length;
00251     char *_output_delimiter;
00252 
00253     struct oob_t {
00254         const char *prefix;
00255         int prefix_len;
00256         Callback<void()> cb;
00257         oob_t *next;
00258     };
00259     oob_t *_oobs;
00260     uint32_t _at_timeout;
00261     uint32_t _previous_at_timeout;
00262 
00263     uint16_t _at_send_delay;
00264     uint64_t _last_response_stop;
00265 
00266     int32_t _ref_count;
00267     bool _is_fh_usable;
00268 
00269     static ATHandler *_atHandlers;
00270 
00271     //*************************************
00272 public:
00273 
00274     /** Starts the command writing by clearing the last error and writing the given command.
00275      *  In case of failure when writing, the last error is set to NSAPI_ERROR_DEVICE_ERROR.
00276      *
00277      *  @param cmd  AT command to be written to modem
00278      */
00279     virtual void cmd_start(const char *cmd);
00280 
00281     /**
00282      * @brief cmd_start_stop Starts an AT command, writes given variadic arguments and stops the command. Use this
00283      *        command when you need multiple response parameters to be handled.
00284      *        NOTE: Does not lock ATHandler for process!
00285      *
00286      * @param cmd AT command in form +<CMD> (will be used also in response reading, no extra chars allowed)
00287      * @param cmd_chr Char to be added to specific AT command: '?', '=' or ''. Will be used as such so '=1' is valid as well.
00288      * @param format Format string for variadic arguments to be added to AT command; No separator needed.
00289      *        Use %d for integer, %s for string and %b for byte string (requires 2 arguments: string and length)
00290      */
00291     void cmd_start_stop(const char *cmd, const char *cmd_chr, const char *format = "", ...);
00292 
00293     /**
00294      * @brief at_cmd_str Send an AT command and read a single string response. Locks and unlocks ATHandler for operation
00295      * @param cmd AT command in form +<CMD> (will be used also in response reading, no extra chars allowed)
00296      * @param cmd_chr Char to be added to specific AT command: '?', '=' or ''. Will be used as such so '=1' is valid as well.
00297      * @param resp_buf Response buffer
00298      * @param resp_buf_size Response buffer size
00299      * @param format Format string for variadic arguments to be added to AT command; No separator needed.
00300      *        Use %d for integer, %s for string and %b for byte string (requires 2 arguments: string and length)
00301      * @return last error that happened when parsing AT responses
00302      */
00303     nsapi_error_t at_cmd_str(const char *cmd, const char *cmd_chr, char *resp_buf, size_t resp_buf_size, const char *format = "", ...);
00304 
00305     /**
00306      * @brief at_cmd_int Send an AT command and read a single integer response. Locks and unlocks ATHandler for operation
00307      * @param cmd AT command in form +<CMD> (will be used also in response reading, no extra chars allowed)
00308      * @param cmd_chr Char to be added to specific AT command: '?', '=' or ''. Will be used as such so '=1' is valid as well.
00309      * @param resp Integer to hold response
00310      * @param format Format string for variadic arguments to be added to AT command; No separator needed.
00311      *        Use %d for integer, %s for string and %b for byte string (requires 2 arguments: string and length)
00312      * @return last error that happened when parsing AT responses
00313      */
00314     nsapi_error_t at_cmd_int(const char *cmd, const char *cmd_chr, int &resp, const char *format = "", ...);
00315 
00316     /**
00317      * @brief at_cmd_discard Send an AT command and read and discard a response. Locks and unlocks ATHandler for operation
00318      * @param cmd AT command in form +<CMD> (will be used also in response reading, no extra chars allowed)
00319      * @param cmd_chr Char to be added to specific AT command: '?', '=' or ''. Will be used as such so '=1' is valid as well.
00320      * @param format Format string for variadic arguments to be added to AT command; No separator needed.
00321      *        Use %d for integer, %s for string and %b for byte string (requires 2 arguments: string and length)
00322      * @return last error that happened when parsing AT responses
00323      */
00324     nsapi_error_t at_cmd_discard(const char *cmd, const char *cmd_chr, const char *format = "", ...);
00325 
00326 public:
00327 
00328     /** Writes integer type AT command subparameter. Starts with the delimiter if not the first param after cmd_start.
00329      *  In case of failure when writing, the last error is set to NSAPI_ERROR_DEVICE_ERROR.
00330      *
00331      *   @param param int to be written to modem as AT command subparameter
00332      */
00333     void write_int(int32_t param);
00334 
00335     /** Writes string type AT command subparamater. Quotes are added to surround the given string.
00336      *  Starts with the delimiter if not the first param after cmd_start.
00337      *  In case of failure when writing, the last error is set to NSAPI_ERROR_DEVICE_ERROR.
00338      *
00339      *  @param param string to be written to modem as AT command subparameter
00340      *  @param useQuotations flag indicating whether the string should be included in quotation marks
00341      */
00342     void write_string(const char *param,  bool useQuotations = true);
00343 
00344     /** Stops the AT command by writing command-line terminator CR to mark command as finished.
00345      */
00346     void cmd_stop();
00347 
00348     /** Stops the AT command by writing command-line terminator CR to mark command as finished and reads the OK/ERROR response.
00349      *
00350      */
00351     void cmd_stop_read_resp();
00352 
00353     /** Write bytes without any subparameter delimiters, such as comma.
00354      *  In case of failure when writing, the last error is set to NSAPI_ERROR_DEVICE_ERROR.
00355      *
00356      *  @param data bytes to be written to modem
00357      *  @param len  length of data string
00358      *
00359      *  @return     number of characters successfully written
00360      */
00361     size_t write_bytes(const uint8_t *data, size_t len);
00362 
00363     /** Sets the stop tag for the current scope (response/information response/element)
00364      *  Parameter's reading routines will stop the reading when such tag is found and will set the found flag.
00365      *  Consume routines will read everything until such tag is found.
00366      *
00367      *  @param stop_tag_seq string to be set as stop tag
00368      */
00369     void set_stop_tag(const char *stop_tag_seq);
00370 
00371     /** Sets the delimiter between parameters or between elements of the information response.
00372      *  Parameter's reading routines will stop when such char is read.
00373      *
00374      *  @param delimiter char to be set as _delimiter
00375      */
00376     void set_delimiter(char delimiter);
00377 
00378     /** Sets the delimiter to default value defined by DEFAULT_DELIMITER.
00379      */
00380     void set_default_delimiter();
00381 
00382     /** Defines behaviour for using or ignoring the delimiter within an AT command
00383      *
00384      *  @param use_delimiter indicating if delimiter should be used or not
00385      */
00386     void use_delimiter(bool use_delimiter);
00387 
00388     /** Consumes the reading buffer up to the delimiter or stop_tag
00389      *
00390      *  @param count number of parameters to be skipped
00391      */
00392     void skip_param(uint32_t count = 1);
00393 
00394     /** Consumes the given length from the reading buffer
00395      *
00396      *  @param len length to be consumed from reading buffer
00397      *  @param count number of parameters to be skipped
00398      */
00399     void skip_param(ssize_t len, uint32_t count);
00400 
00401     /** Reads given number of bytes from receiving buffer without checking any subparameter delimiters, such as comma.
00402      *
00403      *  @param buf output buffer for the read
00404      *  @param len maximum number of bytes to read
00405      *  @return number of successfully read bytes or -1 in case of error
00406      */
00407     ssize_t read_bytes(uint8_t *buf, size_t len);
00408 
00409     /** Reads chars from reading buffer. Terminates with null. Skips the quotation marks.
00410      *  Stops on delimiter or stop tag.
00411      *
00412      *  @param str output buffer for the read
00413      *  @param size maximum number of chars to output including NULL
00414      *  @param read_even_stop_tag if true then try to read even if the stop tag was found previously
00415      *  @return length of output string or -1 in case of read timeout before delimiter or stop tag is found
00416      */
00417     ssize_t read_string(char *str, size_t size, bool read_even_stop_tag = false);
00418 
00419     /** Reads chars representing hex ascii values and converts them to the corresponding chars.
00420      *  For example: "4156" to "AV".
00421      *  Terminates with null. Skips the quotation marks.
00422      *  Stops on delimiter or stop tag.
00423      *
00424      *  @param str output buffer for the read
00425      *  @param size maximum number of chars to output
00426      *  @return length of output string or -1 in case of read timeout before delimiter or stop tag is found
00427      */
00428     ssize_t read_hex_string(char *str, size_t size);
00429 
00430     /** Converts contained chars to their hex ascii value and writes the resulting string to the file handle
00431      *  For example: "AV" to "4156".
00432      *
00433      *  @param str input buffer to be converted to hex ascii
00434      *  @param size of the input param str
00435      */
00436     void write_hex_string(char *str, size_t size);
00437 
00438     /** Reads as string and converts result to integer. Supports only non-negative integers.
00439      *
00440      *  @return the non-negative integer or -1 in case of error.
00441      */
00442     int32_t read_int();
00443 
00444     /**  This looks for necessary matches: prefix, OK, ERROR, URCs and sets the correct scope.
00445      *
00446      *  @param prefix string to be matched from receiving buffer. If not NULL and match succeeds, then scope
00447      *          will be set as information response(info_type)
00448      *  @param stop flag to indicate if we go to information response scope or not.
00449      *        (needed when nothing is expected to be received anymore after the prefix match:
00450      *         sms case: "> ", bc95 reboot case)
00451      */
00452     void resp_start(const char *prefix = NULL, bool stop = false);
00453 
00454     /**  Ends all scopes starting from current scope.
00455      *   Consumes everything until the scope's stop tag is found, then
00456      *   goes to next scope until response scope is ending.
00457      *   URC match is checked during response scope ending,
00458      *   for every new line / CRLF.
00459      *
00460      *
00461      *   Possible sequence:
00462      *   element scope -> information response scope -> response scope
00463      */
00464     void resp_stop();
00465 
00466     /**  Looks for matching the prefix given to resp_start() call.
00467      *   If needed, it ends the scope of a previous information response.
00468      *   Sets the information response scope if new prefix is found and response scope if prefix is not found.
00469      *
00470      *  @return true if prefix defined for information response is not empty string and is found,
00471      *          false otherwise.
00472      */
00473     bool info_resp();
00474 
00475     /**  Looks for matching the start tag.
00476      *   If needed, it ends the scope of a previous element.
00477      *   Sets the element scope if start tag is found and information response scope if start tag is not found.
00478      *
00479      *  @param start_tag tag to be matched to begin parsing an element of an information response
00480      *  @return true if new element is found, false otherwise
00481      */
00482     bool info_elem(char start_tag);
00483 
00484     /**  Consumes the received content until current stop tag is found.
00485      *
00486      *  @return true if stop tag is found, false otherwise
00487      */
00488     bool consume_to_stop_tag();
00489 
00490     /** Return the last 3GPP error code.
00491      *  @return last 3GPP error code
00492      */
00493     int get_3gpp_error();
00494 
00495 public: // just for debugging
00496     /**
00497      * AT debugging, when enabled will print all data read and written,
00498      * non-printable chars are printed as "[%d]".
00499      *
00500      * AT debug can be enabled at compile time using MBED_CONF_CELLULAR_DEBUG_AT flag or at runtime
00501      * calling set_debug(). Note that MBED_CONF_MBED_TRACE_ENABLE must also be enabled.
00502      *
00503      *  @param debug_on Enable/disable debugging
00504      */
00505     void set_debug(bool debug_on);
00506 
00507     /**
00508      * Get degug state set by @ref set_debug
00509      *
00510      *  @return current state of debug
00511      */
00512     bool get_debug() const;
00513 
00514     /** Set debug_on for all ATHandlers in the _atHandlers list
00515      *
00516      *  @param debug_on Set true to enable debug traces
00517      */
00518     static void set_debug_list(bool debug_on);
00519 
00520 private:
00521 
00522     // should fit any prefix and int
00523     char _recv_buff[BUFF_SIZE];
00524     // reading position
00525     size_t _recv_len;
00526     // reading length
00527     size_t _recv_pos;
00528 
00529     // resp_type: the part of the response that doesn't include the information response (+CMD1,+CMD2..)
00530     //            ends with OK or (CME)(CMS)ERROR
00531     // info_type: the information response part of the response: starts with +CMD1 and ends with CRLF
00532     //            information response contains parameters or subsets of parameters (elements), both separated by comma
00533     // elem_type: subsets of parameters that are part of information response, its parameters are separated by comma
00534     enum ScopeType {RespType, InfoType, ElemType, NotSet};
00535     void set_scope(ScopeType scope_type);
00536     ScopeType _current_scope;
00537 
00538     struct tag_t {
00539         char tag[7];
00540         size_t len;
00541         bool found;
00542     };
00543 
00544     // tag to stop response scope
00545     tag_t _resp_stop;
00546     // tag to stop information response scope
00547     tag_t _info_stop;
00548     // tag to stop element scope
00549     tag_t _elem_stop;
00550     // reference to the stop tag of current scope (resp/info/elem)
00551     tag_t *_stop_tag;
00552 
00553     // delimiter between parameters and also used for delimiting elements of information response
00554     char _delimiter;
00555     // set true on prefix match -> indicates start of an information response or of an element
00556     bool _prefix_matched;
00557     // set true on urc match
00558     bool _urc_matched;
00559     // set true on (CME)(CMS)ERROR
00560     bool _error_found;
00561     // Max length of OK,(CME)(CMS)ERROR and URCs
00562     size_t _max_resp_length;
00563 
00564     // prefix set during resp_start and used to try matching possible information responses
00565     char _info_resp_prefix[BUFF_SIZE];
00566     bool _debug_on;
00567     bool _cmd_start;
00568     bool _use_delimiter;
00569 
00570     // time when a command or an URC processing was started
00571     uint64_t _start_time;
00572     // eventqueue event id
00573     int _event_id;
00574 
00575     char _cmd_buffer[BUFF_SIZE];
00576 
00577 private:
00578     //Handles the arguments from given variadic list
00579     void handle_args(const char *format, std::va_list list);
00580 
00581     //Starts an AT command based on given parameters
00582     void handle_start(const char *cmd, const char *cmd_chr);
00583 
00584     //Checks that ATHandler does not have a pending error condition and filehandle is usable
00585     bool ok_to_proceed();
00586 
00587 private:
00588     // Gets char from receiving buffer.
00589     // Resets and fills the buffer if all are already read (receiving position equals receiving length).
00590     // Returns a next char or -1 on failure (also sets error flag)
00591     int get_char();
00592     // Sets to 0 the reading position, reading length and the whole buffer content.
00593     void reset_buffer();
00594     // Reading position set to 0 and buffer's unread content moved to beginning
00595     void rewind_buffer();
00596     // Calculate remaining time for polling based on request start time and AT timeout.
00597     // Returns 0 or time in ms for polling.
00598     int poll_timeout(bool wait_for_timeout = true);
00599     // Reads from serial to receiving buffer.
00600     // Returns true on successful read OR false on timeout.
00601     bool fill_buffer(bool wait_for_timeout = true);
00602 
00603     void set_tag(tag_t *tag_dest, const char *tag_seq);
00604 
00605     // Rewinds the receiving buffer and compares it against given str.
00606     bool match(const char *str, size_t size);
00607     // Iterates URCs and checks if they match the receiving buffer content.
00608     // If URC match sets the scope to information response and after urc's cb returns
00609     // finishes the information response scope(consumes to CRLF).
00610     bool match_urc();
00611     // Checks if any of the error strings are matching the receiving buffer content.
00612     bool match_error();
00613     // Checks if current char in buffer matches ch and consumes it,
00614     // if no match lets the buffer unchanged.
00615     bool consume_char(char ch);
00616     // Consumes the received content until tag is found.
00617     // Consumes the tag only if consume_tag flag is true.
00618     bool consume_to_tag(const char *tag, bool consume_tag);
00619     // Checks if receiving buffer contains OK, ERROR, URC or given prefix.
00620     void resp(const char *prefix, bool check_urc);
00621 
00622 
00623     ScopeType get_scope();
00624 
00625     // Consumes to information response stop tag which is CRLF. Sets scope to response.
00626     void information_response_stop();
00627     // Consumes to element stop tag. Sets scope to information response
00628     void information_response_element_stop();
00629 
00630     // Reads the error code if expected and sets it as last error.
00631     void at_error(bool error_code, DeviceErrorType error_type);
00632 
00633     /** Convert AT error code to 3GPP error codes
00634      *  @param err AT error code read from CME/CMS ERROR responses
00635      *  @param error_type error type (CMS/CME/ERROR)
00636      */
00637     void set_3gpp_error(int err, DeviceErrorType error_type);
00638 
00639     bool check_cmd_send();
00640     size_t write(const void *data, size_t len);
00641 
00642     /** Finds occurrence of one char buffer inside another char buffer.
00643      *
00644      * @param dest                  destination char buffer
00645      * @param dest_len              length of dest
00646      * @param src                   string to be searched for
00647      * @param src_len               length of string to be searched for
00648      *
00649      * @return pointer to first occurrence of src in dest
00650      */
00651     const char *mem_str(const char *dest, size_t dest_len, const char *src, size_t src_len);
00652 
00653     // check is urc is already added
00654     bool find_urc_handler(const char *prefix);
00655 
00656     // print contents of a buffer to trace log
00657     enum ATType {
00658         AT_ERR,
00659         AT_RX,
00660         AT_TX
00661     };
00662     void debug_print(const char *p, int len, ATType type);
00663 };
00664 
00665 } // namespace mbed
00666 
00667 #endif //AT_HANDLER_H_