uart/dte cleanup

This commit is contained in:
David Cermak
2021-04-18 19:14:22 +02:00
parent 7f8d27fcbc
commit 5e2b321c26
10 changed files with 103 additions and 120 deletions

View File

@ -12,13 +12,13 @@
static const char *TAG = "modem_console_helper"; static const char *TAG = "modem_console_helper";
ConsoleCommand::ConsoleCommand(const char* command, const char* help, std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f): ConsoleCommand::ConsoleCommand(const char* command, const char* help, const std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f):
func(std::move(f)) func(std::move(f))
{ {
RegisterCommand(command, help, args); RegisterCommand(command, help, args);
} }
void ConsoleCommand::RegisterCommand(const char* command, const char* help, std::vector<CommandArgs>& args) void ConsoleCommand::RegisterCommand(const char* command, const char* help, const std::vector<CommandArgs>& args)
{ {
assert(last_command <= MAX_REPEAT_NR); assert(last_command <= MAX_REPEAT_NR);
void * common_arg = nullptr; void * common_arg = nullptr;

View File

@ -53,7 +53,7 @@ public:
RegisterCommand(command, help, args); RegisterCommand(command, help, args);
} }
explicit ConsoleCommand(const char* command, const char* help, std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f); explicit ConsoleCommand(const char* command, const char* help, const std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f);
int get_count(int index); int get_count(int index);
template<typename T> int get_count_of(CommandArgs T::*member) { return get_count(index_arg(member)); } template<typename T> int get_count_of(CommandArgs T::*member) { return get_count(index_arg(member)); }
template<typename T> std::string get_string_of(CommandArgs T::*member) { return get_string(index_arg(member)); } template<typename T> std::string get_string_of(CommandArgs T::*member) { return get_string(index_arg(member)); }
@ -63,7 +63,7 @@ public:
int get_int(int index); int get_int(int index);
private: private:
void RegisterCommand(const char* command, const char* help, std::vector<CommandArgs>& args); void RegisterCommand(const char* command, const char* help, const std::vector<CommandArgs>& args);
template<typename T> static constexpr size_t index_arg(CommandArgs T::*member) template<typename T> static constexpr size_t index_arg(CommandArgs T::*member)
{ return ((uint8_t *)&((T*)nullptr->*member) - (uint8_t *)nullptr)/sizeof(CommandArgs); } { return ((uint8_t *)&((T*)nullptr->*member) - (uint8_t *)nullptr)/sizeof(CommandArgs); }
std::function<bool(ConsoleCommand *)> func; std::function<bool(ConsoleCommand *)> func;

View File

@ -30,7 +30,6 @@ static esp_console_repl_t *s_repl = nullptr;
using namespace esp_modem; using namespace esp_modem;
#define CHECK_ERR(cmd, success_action) do { \ #define CHECK_ERR(cmd, success_action) do { \
auto err = cmd; \ auto err = cmd; \
if (err == command_result::OK) { \ if (err == command_result::OK) { \
@ -49,9 +48,8 @@ extern "C" void app_main(void)
// init the DTE // init the DTE
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG(); esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
dte_config.pattern_queue_size = 100; dte_config.uart_config.event_task_stack_size = 4096;
dte_config.event_task_stack_size = 4096; dte_config.uart_config.event_task_priority = 15;
dte_config.event_task_priority = 15;
esp_netif_config_t ppp_netif_config = ESP_NETIF_DEFAULT_PPP(); esp_netif_config_t ppp_netif_config = ESP_NETIF_DEFAULT_PPP();
esp_netif_t *esp_netif = esp_netif_new(&ppp_netif_config); esp_netif_t *esp_netif = esp_netif_new(&ppp_netif_config);
@ -70,12 +68,11 @@ extern "C" void app_main(void)
modem_console_register_http(); modem_console_register_http();
modem_console_register_ping(); modem_console_register_ping();
const struct SetModeArgs { const struct SetModeArgs {
SetModeArgs(): mode(STR1, nullptr, nullptr, "<mode>", "PPP or CMD") {} SetModeArgs(): mode(STR1, nullptr, nullptr, "<mode>", "PPP or CMD") {}
CommandArgs mode; CommandArgs mode;
} set_mode_args; } set_mode_args;
ConsoleCommand SetModeParser("set_mode", "sets modem mode", &set_mode_args, sizeof(set_mode_args), [&](ConsoleCommand *c){ const ConsoleCommand SetModeParser("set_mode", "sets modem mode", &set_mode_args, sizeof(set_mode_args), [&](ConsoleCommand *c){
if (c->get_count_of(&SetModeArgs::mode)) { if (c->get_count_of(&SetModeArgs::mode)) {
auto mode = c->get_string_of(&SetModeArgs::mode); auto mode = c->get_string_of(&SetModeArgs::mode);
if (mode == "CMD") { if (mode == "CMD") {
@ -96,7 +93,7 @@ extern "C" void app_main(void)
SetPinArgs(): pin(STR1, nullptr, nullptr, "<pin>", "PIN") {} SetPinArgs(): pin(STR1, nullptr, nullptr, "<pin>", "PIN") {}
CommandArgs pin; CommandArgs pin;
} set_pin_args; } set_pin_args;
ConsoleCommand SetPinParser("set_pin", "sets SIM card PIN", &set_pin_args, sizeof(set_pin_args), [&](ConsoleCommand *c){ const ConsoleCommand SetPinParser("set_pin", "sets SIM card PIN", &set_pin_args, sizeof(set_pin_args), [&](ConsoleCommand *c){
if (c->get_count_of(&SetPinArgs::pin)) { if (c->get_count_of(&SetPinArgs::pin)) {
auto pin = c->get_string_of(&SetPinArgs::pin); auto pin = c->get_string_of(&SetPinArgs::pin);
ESP_LOGI(TAG, "Setting pin=%s...", pin.c_str()); ESP_LOGI(TAG, "Setting pin=%s...", pin.c_str());
@ -111,8 +108,8 @@ extern "C" void app_main(void)
return 0; return 0;
}); });
std::vector<CommandArgs> no_args; const std::vector<CommandArgs> no_args;
ConsoleCommand ReadPinArgs("read_pin", "checks if SIM is unlocked", no_args, [&](ConsoleCommand *c){ const ConsoleCommand ReadPinArgs("read_pin", "checks if SIM is unlocked", no_args, [&](ConsoleCommand *c){
bool pin_ok; bool pin_ok;
ESP_LOGI(TAG, "Checking pin..."); ESP_LOGI(TAG, "Checking pin...");
auto err = dce->read_pin(pin_ok); auto err = dce->read_pin(pin_ok);
@ -125,7 +122,7 @@ extern "C" void app_main(void)
return 0; return 0;
}); });
ConsoleCommand GetModuleName("get_module_name", "reads the module name", no_args, [&](ConsoleCommand *c){ const ConsoleCommand GetModuleName("get_module_name", "reads the module name", no_args, [&](ConsoleCommand *c){
std::string module_name; std::string module_name;
ESP_LOGI(TAG, "Reading module name..."); ESP_LOGI(TAG, "Reading module name...");
CHECK_ERR(dce->get_module_name(module_name), ESP_LOGI(TAG, "OK. Module name: %s", module_name.c_str())); CHECK_ERR(dce->get_module_name(module_name), ESP_LOGI(TAG, "OK. Module name: %s", module_name.c_str()));
@ -167,14 +164,15 @@ extern "C" void app_main(void)
CHECK_ERR(dce->get_signal_quality(rssi, ber), ESP_LOGI(TAG, "OK. rssi=%d, ber=%d", rssi, ber)); CHECK_ERR(dce->get_signal_quality(rssi, ber), ESP_LOGI(TAG, "OK. rssi=%d, ber=%d", rssi, ber));
}); });
signal_group exit_signal; signal_group exit_signal;
const ConsoleCommand ExitConsole("exit", "exit the console appliation", no_args, [&](ConsoleCommand *c){ const ConsoleCommand ExitConsole("exit", "exit the console application", no_args, [&](ConsoleCommand *c){
ESP_LOGI(TAG, "Exiting..."); ESP_LOGI(TAG, "Exiting...");
exit_signal.set(1); exit_signal.set(1);
s_repl->del(s_repl); s_repl->del(s_repl);
return true; return 0;
}); });
// start console REPL // start console REPL
ESP_ERROR_CHECK(esp_console_start_repl(s_repl)); ESP_ERROR_CHECK(esp_console_start_repl(s_repl));
ESP_LOGI(TAG, "Exiting...%d", esp_get_free_heap_size());
// wait till for exit // wait till for exit
exit_signal.wait_any(1, UINT32_MAX); exit_signal.wait_any(1, UINT32_MAX);
} }

View File

@ -141,17 +141,7 @@ extern "C" void app_main(void)
assert(esp_netif); assert(esp_netif);
auto dce = create_SIM7600_dce(&dce_config, uart_dte, esp_netif); auto dce = create_SIM7600_dce(&dce_config, uart_dte, esp_netif);
/// TEST
{
// std::string str;
// dce->set_mode(esp_modem::modem_mode::CMUX_MODE);
// while (1) {
// dce->get_imsi(str);
// std::cout << "Modem IMSI number:" << str << "|" << std::endl;
// }
}
// return;
//// TEST
dce->set_command_mode(); dce->set_command_mode();
std::string str; std::string str;

View File

@ -79,6 +79,11 @@ std::unique_ptr<DCE> create_SIM800_dce(const dce_config *config, std::shared_ptr
*/ */
std::unique_ptr<DCE> create_BG96_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif); std::unique_ptr<DCE> create_BG96_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif);
/**
* @brief Create generic DCE
*/
std::unique_ptr<DCE> create_generic_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif);
/** /**
* @} * @}
*/ */

View File

@ -24,6 +24,8 @@
#include "cxx_include/esp_modem_cmux.hpp" #include "cxx_include/esp_modem_cmux.hpp"
#include "cxx_include/esp_modem_types.hpp" #include "cxx_include/esp_modem_types.hpp"
struct esp_modem_dte_config;
namespace esp_modem { namespace esp_modem {
/** /**
@ -39,7 +41,7 @@ namespace esp_modem {
*/ */
class DTE : public CommandableIf { class DTE : public CommandableIf {
public: public:
explicit DTE(std::unique_ptr<Terminal> t); explicit DTE(const esp_modem_dte_config *config, std::unique_ptr<Terminal> t);
~DTE() = default; ~DTE() = default;
@ -97,7 +99,7 @@ public:
} }
command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms) override; command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms) override;
command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms, const char separator) override; command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms, char separator) override;
private: private:
@ -106,7 +108,7 @@ private:
[[nodiscard]] bool setup_cmux(); [[nodiscard]] bool setup_cmux();
static const size_t GOT_LINE = signal_group::bit0; static const size_t GOT_LINE = signal_group::bit0;
size_t buffer_size; size_t buffer_size{};
size_t consumed; size_t consumed;
std::unique_ptr<uint8_t[]> buffer; std::unique_ptr<uint8_t[]> buffer;
std::unique_ptr<Terminal> term; std::unique_ptr<Terminal> term;

View File

@ -40,7 +40,7 @@ typedef enum {
* @brief DTE configuration structure * @brief DTE configuration structure
* *
*/ */
struct esp_modem_dte_config { struct esp_modem_uart_term_config {
uart_port_t port_num; /*!< UART port number */ uart_port_t port_num; /*!< UART port number */
uart_word_length_t data_bits; /*!< Data bits of UART */ uart_word_length_t data_bits; /*!< Data bits of UART */
uart_stop_bits_t stop_bits; /*!< Stop bits of UART */ uart_stop_bits_t stop_bits; /*!< Stop bits of UART */
@ -53,36 +53,41 @@ struct esp_modem_dte_config {
int cts_io_num; /*!< CTS Pin Number */ int cts_io_num; /*!< CTS Pin Number */
int rx_buffer_size; /*!< UART RX Buffer Size */ int rx_buffer_size; /*!< UART RX Buffer Size */
int tx_buffer_size; /*!< UART TX Buffer Size */ int tx_buffer_size; /*!< UART TX Buffer Size */
int pattern_queue_size; /*!< UART Pattern Queue Size */
int event_queue_size; /*!< UART Event Queue Size */ int event_queue_size; /*!< UART Event Queue Size */
uint32_t event_task_stack_size; /*!< UART Event Task Stack size */ uint32_t event_task_stack_size; /*!< UART Event Task Stack size */
int event_task_priority; /*!< UART Event Task Priority */ int event_task_priority; /*!< UART Event Task Priority */
int line_buffer_size; /*!< Line buffer size for command mode */
}; };
struct esp_modem_dte_config {
size_t dte_buffer_size;
struct esp_modem_uart_term_config uart_config;
};
/** /**
* @brief ESP Modem DTE Default Configuration * @brief ESP Modem DTE Default Configuration
* *
*/ */
#define ESP_MODEM_DTE_DEFAULT_CONFIG() \ #define ESP_MODEM_DTE_DEFAULT_CONFIG() \
{ \ { \
.port_num = UART_NUM_1, \ .dte_buffer_size = 512, \
.data_bits = UART_DATA_8_BITS, \ .uart_config = { \
.stop_bits = UART_STOP_BITS_1, \ .port_num = UART_NUM_1, \
.parity = UART_PARITY_DISABLE, \ .data_bits = UART_DATA_8_BITS, \
.flow_control = ESP_MODEM_FLOW_CONTROL_NONE,\ .stop_bits = UART_STOP_BITS_1, \
.baud_rate = 115200, \ .parity = UART_PARITY_DISABLE, \
.tx_io_num = 25, \ .flow_control = ESP_MODEM_FLOW_CONTROL_NONE,\
.rx_io_num = 26, \ .baud_rate = 115200, \
.rts_io_num = 27, \ .tx_io_num = 25, \
.cts_io_num = 23, \ .rx_io_num = 26, \
.rx_buffer_size = 1024, \ .rts_io_num = 27, \
.tx_buffer_size = 512, \ .cts_io_num = 23, \
.pattern_queue_size = 20, \ .rx_buffer_size = 1024, \
.event_queue_size = 30, \ .tx_buffer_size = 512, \
.event_task_stack_size = 4096, \ .event_queue_size = 30, \
.event_task_priority = 5, \ .event_task_stack_size = 4096, \
.line_buffer_size = 512 \ .event_task_priority = 5, \
} \
} }
typedef struct esp_modem_dte_config esp_modem_dte_config_t; typedef struct esp_modem_dte_config esp_modem_dte_config_t;

View File

@ -32,7 +32,7 @@ static const char *TAG = "modem_api";
std::shared_ptr<DTE> create_uart_dte(const dte_config *config) { std::shared_ptr<DTE> create_uart_dte(const dte_config *config) {
TRY_CATCH_RET_NULL( TRY_CATCH_RET_NULL(
auto term = create_uart_terminal(config); auto term = create_uart_terminal(config);
return std::make_shared<DTE>(std::move(term)); return std::make_shared<DTE>(config, std::move(term));
) )
} }
@ -56,4 +56,8 @@ std::unique_ptr<DCE> create_BG96_dce(const dce_config *config, std::shared_ptr<D
return create_modem_dce(dce_factory::Modem::BG96, config, std::move(dte), netif); return create_modem_dce(dce_factory::Modem::BG96, config, std::move(dte), netif);
} }
std::unique_ptr<DCE> create_generic_dce(const dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif) {
return create_modem_dce(dce_factory::Modem::GenericModule, config, std::move(dte), netif);
}
} // namespace esp_modem } // namespace esp_modem

View File

@ -12,16 +12,15 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "cxx_include/esp_modem_dte.hpp"
#include <cstring> #include <cstring>
#include "esp_log.h" #include "esp_log.h"
#include "cxx_include/esp_modem_dte.hpp"
#include "esp_modem_config.h"
using namespace esp_modem; using namespace esp_modem;
const int DTE_BUFFER_SIZE = 1024; DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr<Terminal> terminal):
buffer_size(config->dte_buffer_size), consumed(0),
DTE::DTE(std::unique_ptr<Terminal> terminal):
buffer_size(DTE_BUFFER_SIZE), consumed(0),
buffer(std::make_unique<uint8_t[]>(buffer_size)), buffer(std::make_unique<uint8_t[]>(buffer_size)),
term(std::move(terminal)), command_term(term.get()), other_term(nullptr), term(std::move(terminal)), command_term(term.get()), other_term(nullptr),
mode(modem_mode::UNDEF) {} mode(modem_mode::UNDEF) {}
@ -56,6 +55,11 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui
return res; return res;
} }
command_result DTE::command(const std::string &cmd, got_line_cb got_line, uint32_t time_ms)
{
return command(cmd, got_line, time_ms, '\n');
}
bool DTE::setup_cmux() bool DTE::setup_cmux()
{ {
auto original_term = std::move(term); auto original_term = std::move(term);
@ -74,8 +78,3 @@ bool DTE::setup_cmux()
other_term = std::make_unique<CMuxInstance>(cmux_term, 1); other_term = std::make_unique<CMuxInstance>(cmux_term, 1);
return true; return true;
} }
command_result DTE::command(const std::string &cmd, got_line_cb got_line, uint32_t time_ms)
{
return command(cmd, got_line, time_ms, '\n');
}

View File

@ -17,13 +17,10 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_event.h"
#include "driver/uart.h" #include "driver/uart.h"
#include "esp_modem_config.h" #include "esp_modem_config.h"
#include "exception_stub.hpp" #include "exception_stub.hpp"
#define ESP_MODEM_EVENT_QUEUE_SIZE (16)
static const char *TAG = "uart_terminal"; static const char *TAG = "uart_terminal";
namespace esp_modem { namespace esp_modem {
@ -44,14 +41,12 @@ struct uart_resource {
uart_port_t port; /*!< UART port */ uart_port_t port; /*!< UART port */
QueueHandle_t event_queue; /*!< UART event queue handle */ QueueHandle_t event_queue; /*!< UART event queue handle */
int line_buffer_size; /*!< line buffer size in command mode */
int pattern_queue_size; /*!< UART pattern queue size */
}; };
struct uart_task { struct uart_task {
explicit uart_task(size_t stack_size, size_t priority, void *task_param, TaskFunction_t task_function) : explicit uart_task(size_t stack_size, size_t priority, void *task_param, TaskFunction_t task_function) :
task_handle(nullptr) { task_handle(nullptr) {
BaseType_t ret = xTaskCreate(task_function, "uart_task", 10000, task_param, priority, &task_handle); BaseType_t ret = xTaskCreate(task_function, "uart_task", stack_size, task_param, priority, &task_handle);
throw_if_false(ret == pdTRUE, "create uart event task failed"); throw_if_false(ret == pdTRUE, "create uart event task failed");
} }
@ -62,23 +57,8 @@ struct uart_task {
TaskHandle_t task_handle; /*!< UART event task handle */ TaskHandle_t task_handle; /*!< UART event task handle */
}; };
uart_resource::~uart_resource()
struct uart_event_loop { {
explicit uart_event_loop() : event_loop_hdl(nullptr) {
esp_event_loop_args_t loop_args = {};
loop_args.queue_size = ESP_MODEM_EVENT_QUEUE_SIZE;
loop_args.task_name = nullptr;
throw_if_esp_fail(esp_event_loop_create(&loop_args, &event_loop_hdl), "create event loop failed");
}
void run() { esp_event_loop_run(event_loop_hdl, pdMS_TO_TICKS(0)); }
~uart_event_loop() { if (event_loop_hdl) esp_event_loop_delete(event_loop_hdl); }
esp_event_loop_handle_t event_loop_hdl;
};
uart_resource::~uart_resource() {
if (port >= UART_NUM_0 && port < UART_NUM_MAX) { if (port >= UART_NUM_0 && port < UART_NUM_MAX) {
uart_driver_delete(port); uart_driver_delete(port);
} }
@ -86,54 +66,54 @@ uart_resource::~uart_resource() {
uart_resource::uart_resource(const esp_modem_dte_config *config) : uart_resource::uart_resource(const esp_modem_dte_config *config) :
port(-1) { port(-1)
{
esp_err_t res; esp_err_t res;
line_buffer_size = config->line_buffer_size;
/* Config UART */ /* Config UART */
uart_config_t uart_config = {}; uart_config_t uart_config = {};
uart_config.baud_rate = config->baud_rate; uart_config.baud_rate = config->uart_config.baud_rate;
uart_config.data_bits = config->data_bits; uart_config.data_bits = config->uart_config.data_bits;
uart_config.parity = config->parity; uart_config.parity = config->uart_config.parity;
uart_config.stop_bits = config->stop_bits; uart_config.stop_bits = config->uart_config.stop_bits;
uart_config.flow_ctrl = (config->flow_control == ESP_MODEM_FLOW_CONTROL_HW) ? UART_HW_FLOWCTRL_CTS_RTS uart_config.flow_ctrl = (config->uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) ? UART_HW_FLOWCTRL_CTS_RTS
: UART_HW_FLOWCTRL_DISABLE; : UART_HW_FLOWCTRL_DISABLE;
uart_config.source_clk = UART_SCLK_REF_TICK; uart_config.source_clk = UART_SCLK_REF_TICK;
throw_if_esp_fail(uart_param_config(config->port_num, &uart_config), "config uart parameter failed"); throw_if_esp_fail(uart_param_config(config->uart_config.port_num, &uart_config), "config uart parameter failed");
if (config->flow_control == ESP_MODEM_FLOW_CONTROL_HW) { if (config->uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
res = uart_set_pin(config->port_num, config->tx_io_num, config->rx_io_num, res = uart_set_pin(config->uart_config.port_num, config->uart_config.tx_io_num, config->uart_config.rx_io_num,
config->rts_io_num, config->cts_io_num); config->uart_config.rts_io_num, config->uart_config.cts_io_num);
} else { } else {
res = uart_set_pin(config->port_num, config->tx_io_num, config->rx_io_num, res = uart_set_pin(config->uart_config.port_num, config->uart_config.tx_io_num, config->uart_config.rx_io_num,
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
} }
throw_if_esp_fail(res, "config uart gpio failed"); throw_if_esp_fail(res, "config uart gpio failed");
/* Set flow control threshold */ /* Set flow control threshold */
if (config->flow_control == ESP_MODEM_FLOW_CONTROL_HW) { if (config->uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
res = uart_set_hw_flow_ctrl(config->port_num, UART_HW_FLOWCTRL_CTS_RTS, UART_FIFO_LEN - 8); res = uart_set_hw_flow_ctrl(config->uart_config.port_num, UART_HW_FLOWCTRL_CTS_RTS, UART_FIFO_LEN - 8);
} else if (config->flow_control == ESP_MODEM_FLOW_CONTROL_SW) { } else if (config->uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_SW) {
res = uart_set_sw_flow_ctrl(config->port_num, true, 8, UART_FIFO_LEN - 8); res = uart_set_sw_flow_ctrl(config->uart_config.port_num, true, 8, UART_FIFO_LEN - 8);
} }
throw_if_esp_fail(res, "config uart flow control failed"); throw_if_esp_fail(res, "config uart flow control failed");
/* Install UART driver and get event queue used inside driver */ /* Install UART driver and get event queue used inside driver */
res = uart_driver_install(config->port_num, config->rx_buffer_size, config->tx_buffer_size, res = uart_driver_install(config->uart_config.port_num, config->uart_config.rx_buffer_size, config->uart_config.tx_buffer_size,
config->event_queue_size, &(event_queue), 0); config->uart_config.event_queue_size, &(event_queue), 0);
throw_if_esp_fail(res, "install uart driver failed"); throw_if_esp_fail(res, "install uart driver failed");
throw_if_esp_fail(uart_set_rx_timeout(config->port_num, 1), "set rx timeout failed"); throw_if_esp_fail(uart_set_rx_timeout(config->uart_config.port_num, 1), "set rx timeout failed");
uart_set_rx_full_threshold(config->port_num, 64); uart_set_rx_full_threshold(config->uart_config.port_num, 64);
throw_if_esp_fail(res, "config uart pattern failed"); throw_if_esp_fail(res, "config uart pattern failed");
/* mark UART as initialized */ /* mark UART as initialized */
port = config->port_num; port = config->uart_config.port_num;
} }
class uart_terminal : public Terminal { class uart_terminal : public Terminal {
public: public:
explicit uart_terminal(const esp_modem_dte_config *config) : explicit uart_terminal(const esp_modem_dte_config *config) :
uart(config), event_loop(), signal(), uart(config), signal(),
task_handle(config->event_task_stack_size, config->event_task_priority, this, s_task) {} task_handle(config->uart_config.event_task_stack_size, config->uart_config.event_task_priority, this, s_task) {}
~uart_terminal() override = default; ~uart_terminal() override = default;
@ -168,12 +148,9 @@ private:
static const size_t TASK_STOP = BIT2; static const size_t TASK_STOP = BIT2;
static const size_t TASK_PARAMS = BIT3; static const size_t TASK_PARAMS = BIT3;
uart_resource uart; uart_resource uart;
uart_event_loop event_loop;
signal_group signal; signal_group signal;
uart_task task_handle; uart_task task_handle;
}; };
std::unique_ptr<Terminal> create_uart_terminal(const esp_modem_dte_config *config) { std::unique_ptr<Terminal> create_uart_terminal(const esp_modem_dte_config *config) {
@ -194,7 +171,6 @@ void uart_terminal::task() {
return; // exits to the static method where the task gets deleted return; // exits to the static method where the task gets deleted
} }
while (signal.is_any(TASK_START)) { while (signal.is_any(TASK_START)) {
event_loop.run();
if (uart.get_event(event, 100)) { if (uart.get_event(event, 100)) {
if (signal.is_any(TASK_PARAMS)) { if (signal.is_any(TASK_PARAMS)) {
on_data_priv = on_data; on_data_priv = on_data;
@ -202,10 +178,7 @@ void uart_terminal::task() {
} }
switch (event.type) { switch (event.type) {
case UART_DATA: case UART_DATA:
// ESP_LOGI(TAG, "UART_DATA");
// ESP_LOG_BUFFER_HEXDUMP("esp-modem-pattern: debug_data", esp_dte->buffer, length, ESP_LOG_DEBUG);
uart_get_buffered_data_len(uart.port, &len); uart_get_buffered_data_len(uart.port, &len);
// ESP_LOGI(TAG, "UART_DATA len=%d, on_data=%d", len, (bool)on_data);
if (len && on_data_priv) { if (len && on_data_priv) {
if (on_data_priv(nullptr, len)) { if (on_data_priv(nullptr, len)) {
on_data_priv = nullptr; on_data_priv = nullptr;
@ -214,23 +187,30 @@ void uart_terminal::task() {
break; break;
case UART_FIFO_OVF: case UART_FIFO_OVF:
ESP_LOGW(TAG, "HW FIFO Overflow"); ESP_LOGW(TAG, "HW FIFO Overflow");
if (on_error)
on_error(terminal_error::BUFFER_OVERFLOW);
uart.reset_events(); uart.reset_events();
break; break;
case UART_BUFFER_FULL: case UART_BUFFER_FULL:
ESP_LOGW(TAG, "Ring Buffer Full"); ESP_LOGW(TAG, "Ring Buffer Full");
if (on_error)
on_error(terminal_error::BUFFER_OVERFLOW);
uart.reset_events(); uart.reset_events();
break; break;
case UART_BREAK: case UART_BREAK:
ESP_LOGW(TAG, "Rx Break"); ESP_LOGW(TAG, "Rx Break");
if (on_error)
on_error(terminal_error::UNEXPECTED_CONTROL_FLOW);
break; break;
case UART_PARITY_ERR: case UART_PARITY_ERR:
ESP_LOGE(TAG, "Parity Error"); ESP_LOGE(TAG, "Parity Error");
if (on_error)
on_error(terminal_error::CHECKSUM_ERROR);
break; break;
case UART_FRAME_ERR: case UART_FRAME_ERR:
ESP_LOGE(TAG, "Frame Error"); ESP_LOGE(TAG, "Frame Error");
break; if (on_error)
case UART_PATTERN_DET: on_error(terminal_error::UNEXPECTED_CONTROL_FLOW);
ESP_LOGI(TAG, "UART_PATTERN_DET");
break; break;
default: default:
ESP_LOGW(TAG, "unknown uart event type: %d", event.type); ESP_LOGW(TAG, "unknown uart event type: %d", event.type);