From d7960620b6884e4d3d53960dd8702816787fb44a Mon Sep 17 00:00:00 2001 From: Teemu Kultala Date: Mon, 9 Apr 2018 13:46:40 +0300 Subject: [PATCH] cellular: ATHandler send delay --- features/cellular/framework/AT/ATHandler.cpp | 20 ++++++++++++++++++- features/cellular/framework/AT/ATHandler.h | 5 ++++- .../framework/AT/AT_CellularDevice.cpp | 7 ++++++- .../cellular/framework/AT/AT_CellularDevice.h | 2 ++ .../targets/TELIT/HE910/TELIT_HE910.cpp | 6 ++++++ .../targets/TELIT/HE910/TELIT_HE910.h | 4 ++++ 6 files changed, 41 insertions(+), 3 deletions(-) diff --git a/features/cellular/framework/AT/ATHandler.cpp b/features/cellular/framework/AT/ATHandler.cpp index eda2aa912a..4df55c90e2 100644 --- a/features/cellular/framework/AT/ATHandler.cpp +++ b/features/cellular/framework/AT/ATHandler.cpp @@ -24,6 +24,7 @@ #ifdef MBED_CONF_RTOS_PRESENT #include "rtos/Thread.h" #endif +#include "Kernel.h" using namespace mbed; using namespace events; @@ -58,7 +59,7 @@ static const uint8_t map_3gpp_errors[][2] = { { 146, 46 }, { 178, 65 }, { 179, 66 }, { 180, 48 }, { 181, 83 }, { 171, 49 }, }; -ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char *output_delimiter) : +ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay) : _nextATHandler(0), _fileHandle(fh), _queue(queue), @@ -68,6 +69,8 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char _oobs(NULL), _at_timeout(timeout), _previous_at_timeout(timeout), + _at_send_delay(send_delay), + _last_response_stop(0), _fh_sigio_set(false), _processing(false), _ref_count(1), @@ -893,6 +896,8 @@ void ATHandler::resp_stop() set_tag(&_resp_stop, OK); // Reset info resp prefix memset(_info_resp_prefix, 0, sizeof(_info_resp_prefix)); + + _last_response_stop = rtos::Kernel::get_ms_count(); } void ATHandler::information_response_stop() @@ -936,8 +941,21 @@ const char* ATHandler::mem_str(const char* dest, size_t dest_len, const char* sr void ATHandler::cmd_start(const char* cmd) { + + if (_at_send_delay) { + uint64_t current_time = rtos::Kernel::get_ms_count(); + uint64_t time_difference = current_time - _last_response_stop; + + if (time_difference < (uint64_t)_at_send_delay) { + wait_ms((uint64_t)_at_send_delay - time_difference); + tr_debug("AT wait %llu %llu", current_time, _last_response_stop); + } + } + tr_debug("AT> %s", cmd); + + if (_last_err != NSAPI_ERROR_OK) { return; } diff --git a/features/cellular/framework/AT/ATHandler.h b/features/cellular/framework/AT/ATHandler.h index 5410d6f709..ab6654ff6d 100644 --- a/features/cellular/framework/AT/ATHandler.h +++ b/features/cellular/framework/AT/ATHandler.h @@ -75,7 +75,7 @@ public: * @param timeout Timeout when reading for AT response * @param output_delimiter delimiter used when parsing at responses, "\r" should be used as output_delimiter */ - ATHandler(FileHandle *fh, events::EventQueue &queue, int timeout, const char *output_delimiter); + ATHandler(FileHandle *fh, events::EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay = 0); ~ATHandler(); /** Return used file handle. @@ -196,6 +196,9 @@ private: uint32_t _at_timeout; uint32_t _previous_at_timeout; + uint16_t _at_send_delay; + uint64_t _last_response_stop; + bool _fh_sigio_set; bool _processing; diff --git a/features/cellular/framework/AT/AT_CellularDevice.cpp b/features/cellular/framework/AT/AT_CellularDevice.cpp index 6eb88d9485..fe62761ccd 100644 --- a/features/cellular/framework/AT/AT_CellularDevice.cpp +++ b/features/cellular/framework/AT/AT_CellularDevice.cpp @@ -60,7 +60,7 @@ ATHandler* AT_CellularDevice::get_at_handler(FileHandle *fileHandle) atHandler = atHandler->_nextATHandler; } - atHandler = new ATHandler(fileHandle, _queue, _default_timeout, "\r"); + atHandler = new ATHandler(fileHandle, _queue, _default_timeout, "\r", get_send_delay()); if (atHandler) { if (_modem_debug_on) { atHandler->enable_debug(_modem_debug_on); @@ -225,6 +225,11 @@ void AT_CellularDevice::set_timeout(int timeout) } } +uint16_t AT_CellularDevice::get_send_delay() +{ + return 0; +} + void AT_CellularDevice::modem_debug_on(bool on) { _modem_debug_on = on; diff --git a/features/cellular/framework/AT/AT_CellularDevice.h b/features/cellular/framework/AT/AT_CellularDevice.h index a2d1171469..25616697f1 100644 --- a/features/cellular/framework/AT/AT_CellularDevice.h +++ b/features/cellular/framework/AT/AT_CellularDevice.h @@ -77,6 +77,8 @@ public: // CellularDevice virtual void set_timeout(int timeout); + virtual uint16_t get_send_delay(); + virtual void modem_debug_on(bool on); virtual NetworkStack *get_stack(); diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp index 89f4f985ef..51bbd34912 100644 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp +++ b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp @@ -58,3 +58,9 @@ CellularNetwork *TELIT_HE910::open_network(FileHandle *fh) } return _network; } + +uint16_t TELIT_HE910::get_send_delay() +{ + return DEFAULT_DELAY_BETWEEN_AT_COMMANDS; +} + diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.h b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.h index 2d66a21b1c..efba720495 100644 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.h +++ b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.h @@ -20,6 +20,9 @@ #include "AT_CellularDevice.h" +//the delay between sending AT commands +#define DEFAULT_DELAY_BETWEEN_AT_COMMANDS 20 + namespace mbed { class TELIT_HE910 : public AT_CellularDevice @@ -32,6 +35,7 @@ public: public: // from CellularDevice virtual CellularPower *open_power(FileHandle *fh); virtual CellularNetwork *open_network(FileHandle *fh); + virtual uint16_t get_send_delay(); }; } // namespace mbed #endif /* CELLULAR_TARGETS_TELIT_HE910_TELIT_HE910_H_ */