21#include "platform/mbed_retarget.h"
22#include "platform/mbed_chrono.h"
24#include "events/EventQueue.h"
25#include "netsocket/nsapi_types.h"
28#include "rtos/Kernel.h"
32#include "drivers/BufferedSerial.h"
40#define AT_HANDLER_MUTEX
42#if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT
43#include "rtos/ConditionVariable.h"
51extern const char *CRLF;
55 DeviceErrorTypeNoError = 0,
57 DeviceErrorTypeErrorCMS,
58 DeviceErrorTypeErrorCME
63 DeviceErrorType errType;
89 ATHandler(
FileHandle *fh,
events::EventQueue &queue, mbed::chrono::milliseconds_u32 timeout,
const char *output_delimiter, std::chrono::duration<uint16_t, std::milli> send_delay = std::chrono::milliseconds(0));
137 void set_at_timeout(uint32_t timeout_milliseconds,
bool default_timeout =
false);
144 void set_at_timeout(mbed::chrono::milliseconds_u32 timeout,
bool default_timeout =
false);
182 bool sync(std::chrono::duration<int, std::milli> timeout);
213 void cmd_start_stop(
const char *cmd,
const char *cmd_chr,
const char *format =
"", ...);
225 nsapi_error_t at_cmd_str(
const char *cmd,
const char *cmd_chr,
char *resp_buf,
size_t resp_buf_size,
const char *format =
"", ...);
344 ssize_t
read_string(
char *str,
size_t size,
bool read_even_stop_tag =
false);
380 void resp_start(
const char *prefix = NULL,
bool stop =
false);
481 void inc_ref_count();
487 void dec_ref_count();
499 void remove_urc_handler(
const char *prefix);
504 void handle_args(
const char *format, std::va_list list);
507 void handle_start(
const char *cmd,
const char *cmd_chr);
510 bool ok_to_proceed();
519 void rewind_buffer();
522 int poll_timeout(
bool wait_for_timeout =
true);
525 bool fill_buffer(
bool wait_for_timeout =
true);
527 void set_tag(tag_t *tag_dest,
const char *tag_seq);
530 bool match(
const char *str,
size_t size);
539 bool consume_char(
char ch);
542 bool consume_to_tag(
const char *tag,
bool consume_tag);
544 void resp(
const char *prefix,
bool check_urc);
546 void set_scope(ScopeType scope_type);
548 ScopeType get_scope();
551 void information_response_stop();
553 void information_response_element_stop();
556 void at_error(
bool error_code, DeviceErrorType error_type);
562 void set_3gpp_error(
int err, DeviceErrorType error_type);
564 bool check_cmd_send();
565 size_t write(
const void *data,
size_t len);
576 const char *mem_str(
const char *dest,
size_t dest_len,
const char *src,
size_t src_len);
579 bool find_urc_handler(
const char *prefix);
588 void debug_print(
const char *p,
int len, ATType type);
592#if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT
596 FileHandle *_fileHandle;
600 int _last_3gpp_error;
601 device_err_t _last_at_err;
602 uint16_t _oob_string_max_length;
603 char *_output_delimiter;
606 mbed::chrono::milliseconds_u32 _at_timeout;
607 mbed::chrono::milliseconds_u32 _previous_at_timeout;
609 std::chrono::duration<uint16_t, std::milli> _at_send_delay;
610 rtos::Kernel::Clock::time_point _last_response_stop;
616 char _recv_buff[MBED_CONF_CELLULAR_AT_HANDLER_BUFFER_SIZE];
622 ScopeType _current_scope;
636 bool _prefix_matched;
642 size_t _max_resp_length;
645 char _info_resp_prefix[MBED_CONF_CELLULAR_AT_HANDLER_BUFFER_SIZE];
651 rtos::Kernel::Clock::time_point _start_time;
655 char _cmd_buffer[MBED_CONF_CELLULAR_AT_HANDLER_BUFFER_SIZE];
Class for sending AT commands and parsing AT responses.
void set_default_delimiter()
Sets the delimiter to default value defined by DEFAULT_DELIMITER.
bool consume_to_stop_tag()
Consumes the received content until current stop tag is found.
void set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout=false)
Set timeout in milliseconds for AT commands.
ssize_t read_bytes(uint8_t *buf, size_t len)
Reads given number of bytes from receiving buffer without checking any subparameter delimiters,...
int32_t read_int()
Reads as string and converts result to integer.
ssize_t read_string(char *str, size_t size, bool read_even_stop_tag=false)
Reads chars from reading buffer.
void use_delimiter(bool use_delimiter)
Defines behaviour for using or ignoring the delimiter within an AT command.
bool sync(std::chrono::duration< int, std::milli > timeout)
Synchronize AT command and response handling to modem.
ssize_t read_hex_string(char *str, size_t size)
Reads chars representing hex ascii values and converts them to the corresponding chars.
void cmd_stop()
Stops the AT command by writing command-line terminator CR to mark command as finished.
void set_stop_tag(const char *stop_tag_seq)
Sets the stop tag for the current scope (response/information response/element) Parameter's reading r...
nsapi_error_t unlock_return_error()
Locks the mutex for file handle if AT_HANDLER_MUTEX is defined and returns the last error.
nsapi_error_t at_cmd_str(const char *cmd, const char *cmd_chr, char *resp_buf, size_t resp_buf_size, const char *format="",...)
at_cmd_str Send an AT command and read a single string response.
bool sync(int timeout_ms)
Synchronize AT command and response handling to modem.
nsapi_error_t at_cmd_int(const char *cmd, const char *cmd_chr, int &resp, const char *format="",...)
at_cmd_int Send an AT command and read a single integer response.
nsapi_error_t at_cmd_discard(const char *cmd, const char *cmd_chr, const char *format="",...)
at_cmd_discard Send an AT command and read and discard a response.
void set_baud(int baud_rate)
Sets BufferedSerial filehandle to given baud rate.
void set_is_filehandle_usable(bool usable)
Set is file handle usable.
void skip_param(ssize_t len, uint32_t count)
Consumes the given length from the reading buffer.
bool get_debug() const
Get degug state set by set_debug.
ATHandler(FileHandle *fh, events::EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay=0)
Constructor.
void set_at_timeout(mbed::chrono::milliseconds_u32 timeout, bool default_timeout=false)
Set timeout in milliseconds for AT commands.
device_err_t get_last_device_error() const
returns the last device error while parsing AT responses.
void set_debug(bool debug_on)
AT debugging, when enabled will print all data read and written, non-printable chars are printed as "...
size_t write_bytes(const uint8_t *data, size_t len)
Write bytes without any subparameter delimiters, such as comma.
bool info_resp()
Looks for matching the prefix given to resp_start() call.
void write_int(int32_t param)
Writes integer type AT command subparameter.
void write_hex_string(const char *str, size_t size, bool quote_string=true)
Converts contained chars to their hex ascii value and writes the resulting string to the file handle ...
void skip_param_bytes(ssize_t len, uint32_t count)
Consumes the given length from the reading buffer even if the stop tag has been found.
bool consume_to_stop_tag_even_found()
Consumes the received content until current stop tag is found even if stop tag has been found previou...
void set_send_delay(uint16_t send_delay)
Sets the delay to be applied before sending any AT command.
FileHandle * get_file_handle()
Return used file handle.
int get_3gpp_error()
Return the last 3GPP error code.
void cmd_start(const char *cmd)
Starts the command writing by clearing the last error and writing the given command.
void unlock()
Unlocks the mutex for file handle if AT_HANDLER_MUTEX is defined.
void lock()
Locks the mutex for file handle if AT_HANDLER_MUTEX is defined.
void resp_stop()
Ends all scopes starting from current scope.
nsapi_error_t get_last_error() const
returns the last error while parsing AT responses.
void restore_at_timeout()
Restore timeout to previous timeout.
void set_urc_handler(const char *prefix, Callback< void()> callback)
Set callback function for URC.
void process_oob()
Tries to find oob's from the AT response.
void cmd_start_stop(const char *cmd, const char *cmd_chr, const char *format="",...)
cmd_start_stop Starts an AT command, writes given variadic arguments and stops the command.
void skip_param(uint32_t count=1)
Consumes the reading buffer up to the delimiter or stop_tag.
void flush()
Flushes the underlying stream.
bool info_elem(char start_tag)
Looks for matching the start tag.
void set_delimiter(char delimiter)
Sets the delimiter between parameters or between elements of the information response.
void write_string(const char *param, bool useQuotations=true)
Writes string type AT command subparamater.
void cmd_stop_read_resp()
Stops the AT command by writing command-line terminator CR to mark command as finished and reads the ...
void resp_start(const char *prefix=NULL, bool stop=false)
This looks for necessary matches: prefix, OK, ERROR, URCs and sets the correct scope.
void clear_error()
Clear pending error flag.
ATHandler(FileHandle *fh, events::EventQueue &queue, mbed::chrono::milliseconds_u32 timeout, const char *output_delimiter, std::chrono::duration< uint16_t, std::milli > send_delay=std::chrono::milliseconds(0))
Constructor.
Callback class based on template specialization.
The ConditionVariable class is a synchronization primitive that allows threads to wait until a partic...
The Mutex class is used to synchronize the execution of threads.
signed int nsapi_error_t
Type used to represent error codes.
AT response error with error code and type.