From 904cf94648d69435b2062d7929d450f58cfb8533 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 7 Mar 2021 19:43:45 +0100 Subject: [PATCH] Implement perfect forwarding for command lib --- esp_modem/CMakeLists.txt | 8 +- .../simple_cxx_client/main/simple_client.cpp | 29 ++++-- .../include/cxx_include/esp_modem_api.hpp | 15 ++- .../include/cxx_include/esp_modem_cmux.hpp | 45 +++++++++ .../cxx_include/esp_modem_command_library.hpp | 27 +++++ .../include/cxx_include/esp_modem_dce.hpp | 63 +++++++++--- .../cxx_include/esp_modem_dce_commands.hpp | 40 -------- .../cxx_include/esp_modem_dce_commands_if.hpp | 9 -- .../cxx_include/esp_modem_dce_module.hpp | 70 +++++++++++++ .../cxx_include/esp_modem_device_factory.hpp | 5 - .../include/cxx_include/esp_modem_dte.hpp | 99 ++----------------- .../{ppp_netif.hpp => esp_modem_netif.hpp} | 8 +- ...l_objects.hpp => esp_modem_primitives.hpp} | 8 +- .../cxx_include/esp_modem_terminal.hpp | 43 ++++++++ .../include/cxx_include/esp_modem_types.hpp | 43 ++++++++ .../cxx_include/esp_modem_uart_terminal.hpp | 10 -- esp_modem/include/esp_modem_api.h | 33 +++++++ .../generate/esp_modem_command_declare.inc | 73 ++++++++++++++ .../uart_terminal.hpp | 0 esp_modem/src/esp_modem_api.cpp | 80 ++++++++++++++- .../esp_modem_command_library.cpp} | 70 +++++-------- esp_modem/src/esp_modem_dce.cpp | 33 ------- esp_modem/src/esp_modem_device.cpp | 46 --------- esp_modem/src/esp_modem_device_factory.cpp | 10 -- esp_modem/src/esp_modem_dte.cpp | 5 +- esp_modem/src/esp_modem_factory.cpp | 21 ++++ esp_modem/src/esp_modem_modules.cpp | 23 +++++ .../{ppp_netif.cpp => esp_modem_netif.cpp} | 5 +- esp_modem/src/esp_modem_uart.cpp | 3 - 29 files changed, 586 insertions(+), 338 deletions(-) create mode 100644 esp_modem/include/cxx_include/esp_modem_cmux.hpp create mode 100644 esp_modem/include/cxx_include/esp_modem_command_library.hpp delete mode 100644 esp_modem/include/cxx_include/esp_modem_dce_commands.hpp delete mode 100644 esp_modem/include/cxx_include/esp_modem_dce_commands_if.hpp create mode 100644 esp_modem/include/cxx_include/esp_modem_dce_module.hpp delete mode 100644 esp_modem/include/cxx_include/esp_modem_device_factory.hpp rename esp_modem/include/cxx_include/{ppp_netif.hpp => esp_modem_netif.hpp} (83%) rename esp_modem/include/cxx_include/{terminal_objects.hpp => esp_modem_primitives.hpp} (93%) create mode 100644 esp_modem/include/cxx_include/esp_modem_terminal.hpp create mode 100644 esp_modem/include/cxx_include/esp_modem_types.hpp delete mode 100644 esp_modem/include/cxx_include/esp_modem_uart_terminal.hpp create mode 100644 esp_modem/include/esp_modem_api.h create mode 100644 esp_modem/include/generate/esp_modem_command_declare.inc rename esp_modem/{include/cxx_include => private_include}/uart_terminal.hpp (100%) rename esp_modem/{include/cxx_include/esp_modem_commands.hpp => src/esp_modem_command_library.cpp} (65%) delete mode 100644 esp_modem/src/esp_modem_dce.cpp delete mode 100644 esp_modem/src/esp_modem_device.cpp delete mode 100644 esp_modem/src/esp_modem_device_factory.cpp create mode 100644 esp_modem/src/esp_modem_factory.cpp create mode 100644 esp_modem/src/esp_modem_modules.cpp rename esp_modem/src/{ppp_netif.cpp => esp_modem_netif.cpp} (97%) diff --git a/esp_modem/CMakeLists.txt b/esp_modem/CMakeLists.txt index 1724bf949..ef441d9cd 100644 --- a/esp_modem/CMakeLists.txt +++ b/esp_modem/CMakeLists.txt @@ -9,12 +9,12 @@ set(srcs "src/esp_modem.c" "src/esp_sim7600.c" "src/esp_bg96.c" "src/esp_modem_dte.cpp" - "src/ppp_netif.cpp" + "src/esp_modem_netif.cpp" "src/esp_modem_api.cpp" - "src/esp_modem_dce.cpp" - "src/esp_modem_device.cpp" - "src/esp_modem_device_factory.cpp" + "src/esp_modem_factory.cpp" "src/esp_modem_cmux.cpp" + "src/esp_modem_command_library.cpp" + "src/esp_modem_modules.cpp" "src/esp_modem_uart.cpp") set(include_dirs "include") diff --git a/esp_modem/examples/simple_cxx_client/main/simple_client.cpp b/esp_modem/examples/simple_cxx_client/main/simple_client.cpp index 3f17ca6af..e426019ee 100644 --- a/esp_modem/examples/simple_cxx_client/main/simple_client.cpp +++ b/esp_modem/examples/simple_cxx_client/main/simple_client.cpp @@ -16,7 +16,7 @@ #include "esp_modem_netif.h" #include "esp_log.h" #include "cxx_include/esp_modem_dte.hpp" -#include "cxx_include/uart_terminal.hpp" + #include "cxx_include/esp_modem_api.hpp" #define BROKER_URL "mqtt://mqtt.eclipseprojects.io" @@ -266,7 +266,7 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con uint8_t data[32] = {}; int actual_len = 0; auto uart_dte = create_uart_dte(dte_config); - uart_dte->set_mode(dte_mode::UNDEF); + uart_dte->set_mode(modem_mode::UNDEF); uart_dte->command("+++", [&](uint8_t *data, size_t len) { std::string response((char*)data, len); ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)data); @@ -293,14 +293,15 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con assert(esp_netif); std::string apn = "internet"; - auto device = create_device(uart_dte, apn); - bool pin_ok = true; - if (device->read_pin(pin_ok) == command_result::OK && !pin_ok) { - throw_if_false(device->set_pin("1234") == command_result::OK, "Cannot set PIN!"); - } +// auto device = create_generic_module(uart_dte, apn); + auto device = create_SIM7600_module(uart_dte, apn); +// bool pin_ok = true; +// if (device->read_pin(pin_ok) == command_result::OK && !pin_ok) { +// throw_if_false(device->set_pin("1234") == command_result::OK, "Cannot set PIN!"); +// } // -// std::string number; + std::string number; // std::cout << "----" << std::endl; // device->get_imsi(number); // std::cout << "----" << std::endl; @@ -312,7 +313,14 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con // device->get_module_name(number); // std::cout << "|" << number << "|" << std::endl; // std::cout << "----" << std::endl; - auto my_dce = create_dce(uart_dte, device, esp_netif); +// auto my_dce = create_generic_module_dce(uart_dte, device, esp_netif); + auto my_dce = create_SIM7600_dce_from_module(uart_dte, device, esp_netif); + my_dce->get_module_name(number); + std::cout << "|" << number << "|" << std::endl; + bool pin_ok = true; + if (my_dce->read_pin(pin_ok) == command_result::OK && !pin_ok) { + throw_if_false(my_dce->set_pin("1234") == command_result::OK, "Cannot set PIN!"); + } // return; // my_dce->set_cmux(); @@ -326,6 +334,9 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con // uart_dte->send_cmux_command(2, "AT+CPIN?"); // return; + my_dce->get_module_name(number); + my_dce->set_data_mode(); + my_dce->command("AT+CPIN?\r", [&](uint8_t *data, size_t len) { std::string response((char*)data, len); ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)data); diff --git a/esp_modem/include/cxx_include/esp_modem_api.hpp b/esp_modem/include/cxx_include/esp_modem_api.hpp index b6c5dd24d..047c4d034 100644 --- a/esp_modem/include/cxx_include/esp_modem_api.hpp +++ b/esp_modem/include/cxx_include/esp_modem_api.hpp @@ -2,13 +2,24 @@ #include #include "cxx_include/esp_modem_dce.hpp" -#include "cxx_include/esp_modem_device_factory.hpp" +#include "cxx_include/esp_modem_dce_module.hpp" class DTE; +class GenericModule; +class SIM7600; +class SIM800; +class BG96; struct dte_config; typedef struct esp_netif_obj esp_netif_t; std::shared_ptr create_uart_dte(const dte_config *config); -std::unique_ptr create_dce(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif); +std::shared_ptr create_generic_module(const std::shared_ptr& dte, std::string &apn); +std::shared_ptr create_SIM7600_module(const std::shared_ptr& dte, std::string &apn); + +std::unique_ptr> create_generic_dce_from_module(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif); +std::unique_ptr> create_SIM7600_dce_from_module(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif); + + +std::unique_ptr> create_SIM7600_dce(const std::shared_ptr& dte, esp_netif_t *netif, std::string &apn); diff --git a/esp_modem/include/cxx_include/esp_modem_cmux.hpp b/esp_modem/include/cxx_include/esp_modem_cmux.hpp new file mode 100644 index 000000000..95ba4e9ef --- /dev/null +++ b/esp_modem/include/cxx_include/esp_modem_cmux.hpp @@ -0,0 +1,45 @@ +// +// Created by david on 3/8/21. +// + +#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP +#define SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP + +enum class cmux_state { + INIT, + HEADER, + PAYLOAD, + FOOTER, + RECOVER, +}; + +class CMUXedTerminal: public Terminal { +public: + explicit CMUXedTerminal(std::unique_ptr t, std::unique_ptr b, size_t buff_size): + term(std::move(t)), buffer(std::move(b)) {} + ~CMUXedTerminal() override = default; + void setup_cmux(); + void set_data_cb(std::function f) override {} + int write(uint8_t *data, size_t len) override; + int read(uint8_t *data, size_t len) override { return term->read(data, len); } + void start() override; + void stop() override {} +private: + static bool process_cmux_recv(size_t len); + void send_sabm(size_t i); + std::unique_ptr term; + cmux_state state; + uint8_t dlci; + uint8_t type; + size_t payload_len; + uint8_t frame_header[6]; + size_t frame_header_offset; + size_t buffer_size; + size_t consumed; + std::unique_ptr buffer; + bool on_cmux(size_t len); + +}; + + +#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CMUX_HPP diff --git a/esp_modem/include/cxx_include/esp_modem_command_library.hpp b/esp_modem/include/cxx_include/esp_modem_command_library.hpp new file mode 100644 index 000000000..9d7d823f7 --- /dev/null +++ b/esp_modem/include/cxx_include/esp_modem_command_library.hpp @@ -0,0 +1,27 @@ +// +// Created by david on 3/2/21. +// + +#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP +#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP + +#include "esp_modem_dte.hpp" +#include "esp_modem_dce_module.hpp" +#include "esp_modem_types.hpp" +#include "generate/esp_modem_command_declare.inc" + +namespace esp_modem { +namespace dce_commands { + +#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \ + return_type name(CommandableIf *t, ## __VA_ARGS__); + + DECLARE_ALL_COMMAND_APIS(forwards name(...) { device->name(...); } ) + +#undef ESP_MODEM_DECLARE_DCE_COMMAND + +} // dce_commands +} // esp_modem + + +#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_LIBRARY_HPP diff --git a/esp_modem/include/cxx_include/esp_modem_dce.hpp b/esp_modem/include/cxx_include/esp_modem_dce.hpp index 9102e32db..9fa6b6f50 100644 --- a/esp_modem/include/cxx_include/esp_modem_dce.hpp +++ b/esp_modem/include/cxx_include/esp_modem_dce.hpp @@ -1,28 +1,65 @@ #pragma once -#include "cxx_include/esp_modem_dce_commands_if.hpp" +#include "cxx_include/esp_modem_netif.hpp" +#include "generate/esp_modem_command_declare.inc" +template class DCE { + static_assert(std::is_base_of::value, "DCE must be instantiated with Module class only"); public: - explicit DCE(const std::shared_ptr& d, std::shared_ptr device, esp_netif_t * netif); + explicit DCE(const std::shared_ptr& dte, std::shared_ptr device, esp_netif_t * netif): + dce_dte(dte), device(std::move(device)), ppp_netif(dte, netif) + { } void set_data() { -// command("AT+CGDCONT=1,\"IP\",\"internet\"\r", [&](uint8_t *data, size_t len) { -// return command_result::OK; -// }, 1000); -// command("ATD*99***1#\r", [&](uint8_t *data, size_t len) { -// return command_result::OK; -// }, 1000); device->setup_data_mode(); - device->set_mode(dte_mode::DATA_MODE); - dce_dte->set_mode(dte_mode::DATA_MODE); + device->set_mode(modem_mode::DATA_MODE); + dce_dte->set_mode(modem_mode::DATA_MODE); ppp_netif.start(); } - void exit_data(); + void exit_data() { + ppp_netif.stop(); + device->set_mode(modem_mode::COMMAND_MODE); +// uint8_t* data; + dce_dte->set_data_cb([&](size_t len) -> bool { +// auto actual_len = dce_dte->read(&data, 64); +// ESP_LOG_BUFFER_HEXDUMP("esp-modem: debug_data", data, actual_len, ESP_LOG_INFO); + return false; + }); + ppp_netif.wait_until_ppp_exits(); + dce_dte->set_data_cb(nullptr); + dce_dte->set_mode(modem_mode::COMMAND_MODE); + } + +#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \ + template \ + return_type name(Agrs&&... args) \ + { \ + return device->name(std::forward(args)...); \ + } + + DECLARE_ALL_COMMAND_APIS(forwards name(...) { device->name(...); } ) + +#undef ESP_MODEM_DECLARE_DCE_COMMAND + + +// template +// command_result get_module_name(Params&&... params) +// { +// return device->get_module_name(std::forward(params)...); +// } +// template +// command_result set_data_mode(Params&&... params) +// { +// return device->set_data_mode(std::forward(params)...); +// } + + command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) { return dce_dte->command(command, got_line, time_ms); } - void set_cmux(); + void set_cmux() { device->set_mode(modem_mode::CMUX_MODE); + dce_dte->set_mode(modem_mode::CMUX_MODE); } private: std::shared_ptr dce_dte; - std::shared_ptr device; + std::shared_ptr device; PPP ppp_netif; }; \ No newline at end of file diff --git a/esp_modem/include/cxx_include/esp_modem_dce_commands.hpp b/esp_modem/include/cxx_include/esp_modem_dce_commands.hpp deleted file mode 100644 index 74e6564f5..000000000 --- a/esp_modem/include/cxx_include/esp_modem_dce_commands.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "cxx_include/esp_modem_dce_commands_if.hpp" -#include -#include - -struct PdpContext { - PdpContext(std::string& apn): context_id(1), protocol_type("IP"), apn(apn) {} - size_t context_id; - std::string protocol_type; - std::string apn; -}; - -enum class command_result; -class DTE; - -class Device: public DeviceIf { -public: - explicit Device(std::shared_ptr dte, std::unique_ptr pdp): - dte(std::move(dte)), pdp(std::move(pdp)) {} - bool setup_data_mode() override; - bool set_mode(dte_mode mode) override; - - command_result set_echo(bool on); - command_result set_data_mode(); - command_result resume_data_mode(); - command_result set_pdp_context(PdpContext& pdp_context); - command_result set_command_mode(); - command_result set_cmux(); - command_result get_imsi(std::string& imsi_number); - command_result set_pin(const std::string& pin); - command_result read_pin(bool& pin_ok); - command_result get_imei(std::string& imei); - command_result get_module_name(std::string& imei); - -private: - std::shared_ptr dte; - std::unique_ptr pdp; -}; - - diff --git a/esp_modem/include/cxx_include/esp_modem_dce_commands_if.hpp b/esp_modem/include/cxx_include/esp_modem_dce_commands_if.hpp deleted file mode 100644 index b2d5d0841..000000000 --- a/esp_modem/include/cxx_include/esp_modem_dce_commands_if.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -enum class dte_mode; - -class DeviceIf { -public: - virtual bool setup_data_mode() = 0; - virtual bool set_mode(dte_mode mode) = 0; -}; \ No newline at end of file diff --git a/esp_modem/include/cxx_include/esp_modem_dce_module.hpp b/esp_modem/include/cxx_include/esp_modem_dce_module.hpp new file mode 100644 index 000000000..1b048ea4f --- /dev/null +++ b/esp_modem/include/cxx_include/esp_modem_dce_module.hpp @@ -0,0 +1,70 @@ +#pragma once +#include +#include +#include "generate/esp_modem_command_declare.inc" +#include "cxx_include/esp_modem_command_library.hpp" +#include "cxx_include/esp_modem_types.hpp" + +enum class command_result; +class DTE; + +class GenericModule: public ModuleIf { +public: + explicit GenericModule(std::shared_ptr dte, std::unique_ptr pdp): + dte(std::move(dte)), pdp(std::move(pdp)) {} + + bool setup_data_mode() override + { + if (set_echo(false) != command_result::OK) + return false; + if (set_pdp_context(*pdp) != command_result::OK) + return false; + return true; + } + + bool set_mode(modem_mode mode) override + { + if (mode == modem_mode::DATA_MODE) { + if (set_data_mode() != command_result::OK) + return resume_data_mode() == command_result::OK; + return true; + } else if (mode == modem_mode::COMMAND_MODE) { + return set_command_mode() == command_result::OK; + } else if (mode == modem_mode::CMUX_MODE) { + return set_cmux() == command_result::OK; + } + return true; + } + + +#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \ + template \ + return_type name(Agrs&&... args) { return esp_modem::dce_commands::name(dte.get(), std::forward(args)...); } + + DECLARE_ALL_COMMAND_APIS(return_type name(...); ) + +#undef ESP_MODEM_DECLARE_DCE_COMMAND + + +protected: + std::shared_ptr dte; + std::unique_ptr pdp; +}; + +// Definitions of other modules +class SIM7600: public GenericModule { + using GenericModule::GenericModule; +public: + command_result get_module_name(std::string& name); +}; + +class SIM800: public GenericModule { + using GenericModule::GenericModule; +public: + command_result get_module_name(std::string& name); +}; + +class BG96: public GenericModule { +public: + command_result get_module_name(std::string& name); +}; diff --git a/esp_modem/include/cxx_include/esp_modem_device_factory.hpp b/esp_modem/include/cxx_include/esp_modem_device_factory.hpp deleted file mode 100644 index 093dc3deb..000000000 --- a/esp_modem/include/cxx_include/esp_modem_device_factory.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include "cxx_include/esp_modem_dce_commands.hpp" -//std::shared_ptr create_device(const std::shared_ptr& dte, std::string &apn); -std::shared_ptr create_device(const std::shared_ptr& dte, std::string &apn); \ No newline at end of file diff --git a/esp_modem/include/cxx_include/esp_modem_dte.hpp b/esp_modem/include/cxx_include/esp_modem_dte.hpp index 34c3c8fb0..5987e38fd 100644 --- a/esp_modem/include/cxx_include/esp_modem_dte.hpp +++ b/esp_modem/include/cxx_include/esp_modem_dte.hpp @@ -1,6 +1,5 @@ #ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP #define SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP - #include #include #include @@ -8,94 +7,16 @@ #include #include #include "esp_err.h" -#include "terminal_objects.hpp" -#include "ppp_netif.hpp" -#include +#include "cxx_include/esp_modem_primitives.hpp" +#include "cxx_include/esp_modem_terminal.hpp" +#include "cxx_include/esp_modem_cmux.hpp" +#include "cxx_include/esp_modem_types.hpp" -enum class terminal_error { - BUFFER_OVERFLOW, - CHECKSUM_ERROR, - UNEXPECTED_CONTROL_FLOW, -}; - -class Terminal { -public: - virtual ~Terminal() = default; - virtual void set_data_cb(std::function f) { on_data = std::move(f); } - void set_error_cb(std::function f) { on_error = std::move(f); } - virtual int write(uint8_t *data, size_t len) = 0; - virtual int read(uint8_t *data, size_t len) = 0; - virtual void start() = 0; - virtual void stop() = 0; - -protected: - std::function on_data; - std::function on_error; -}; - - -enum class cmux_state { - INIT, - HEADER, - PAYLOAD, - FOOTER, - RECOVER, -}; - -class CMUXedTerminal: public Terminal { -public: - explicit CMUXedTerminal(std::unique_ptr t, std::unique_ptr b, size_t buff_size): - term(std::move(t)), buffer(std::move(b)) {} - ~CMUXedTerminal() override = default; - void setup_cmux(); - void set_data_cb(std::function f) override {} - int write(uint8_t *data, size_t len) override; - int read(uint8_t *data, size_t len) override { return term->read(data, len); } - void start() override; - void stop() override {} -private: - static bool process_cmux_recv(size_t len); - void send_sabm(size_t i); - std::unique_ptr term; - cmux_state state; - uint8_t dlci; - uint8_t type; - size_t payload_len; - uint8_t frame_header[6]; - size_t frame_header_offset; - size_t buffer_size; - size_t consumed; - std::unique_ptr buffer; - bool on_cmux(size_t len); - -}; - - -enum class dte_mode { - UNDEF, - COMMAND_MODE, - DATA_MODE, - CMUX_MODE -}; - -enum class command_result { - OK, - FAIL, - TIMEOUT -}; const int DTE_BUFFER_SIZE = 1024; -struct CMUXHelper { - void send_sabm(size_t dlci); -}; - - - -typedef std::function got_line_cb; - -class DTE { +class DTE: public CommandableIf { public: explicit DTE(std::unique_ptr t); ~DTE() = default; @@ -120,15 +41,15 @@ public: // std::shared_ptr get_buffer() { return buffer;} void start() { term->start(); } void data_mode_closed() { term->stop(); } - void set_mode(dte_mode m) { + void set_mode(modem_mode m) { term->start(); mode = m; - if (m == dte_mode::DATA_MODE) { + if (m == modem_mode::DATA_MODE) { term->set_data_cb(on_data); - } else if (m == dte_mode::CMUX_MODE) { + } else if (m == modem_mode::CMUX_MODE) { setup_cmux(); } } - command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms); + command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) override; // std::shared_ptr buffer; void send_cmux_command(uint8_t i, const std::string& command); @@ -145,7 +66,7 @@ private: std::unique_ptr buffer; std::unique_ptr term; // got_line_cb on_line; - dte_mode mode; + modem_mode mode; signal_group signal; std::function on_data; diff --git a/esp_modem/include/cxx_include/ppp_netif.hpp b/esp_modem/include/cxx_include/esp_modem_netif.hpp similarity index 83% rename from esp_modem/include/cxx_include/ppp_netif.hpp rename to esp_modem/include/cxx_include/esp_modem_netif.hpp index 956162400..f283f0281 100644 --- a/esp_modem/include/cxx_include/ppp_netif.hpp +++ b/esp_modem/include/cxx_include/esp_modem_netif.hpp @@ -2,11 +2,11 @@ // Created by david on 2/26/21. // -#ifndef SIMPLE_CXX_CLIENT_PPP_NETIF_HPP -#define SIMPLE_CXX_CLIENT_PPP_NETIF_HPP +#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP +#define SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP #include "esp_netif.h" -#include "cxx_include/terminal_objects.hpp" +#include "cxx_include/esp_modem_primitives.hpp" class DTE; class PPP; @@ -42,4 +42,4 @@ private: }; -#endif //SIMPLE_CXX_CLIENT_PPP_NETIF_HPP +#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_NETIF_HPP diff --git a/esp_modem/include/cxx_include/terminal_objects.hpp b/esp_modem/include/cxx_include/esp_modem_primitives.hpp similarity index 93% rename from esp_modem/include/cxx_include/terminal_objects.hpp rename to esp_modem/include/cxx_include/esp_modem_primitives.hpp index 66223400b..793a0615a 100644 --- a/esp_modem/include/cxx_include/terminal_objects.hpp +++ b/esp_modem/include/cxx_include/esp_modem_primitives.hpp @@ -2,8 +2,8 @@ // Created by david on 2/26/21. // -#ifndef SIMPLE_CXX_CLIENT_TERMINAL_OBJECTS_HPP -#define SIMPLE_CXX_CLIENT_TERMINAL_OBJECTS_HPP +#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP +#define SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "freertos/semphr.h" @@ -49,7 +49,7 @@ static inline void throw_if_esp_fail(esp_err_t err) struct Lock { explicit Lock(): lock(nullptr) { - lock = xSemaphoreCreateMutex(); + lock = xSemaphoreCreateRecursiveMutex(); throw_if_false(lock != nullptr, "create signal event group failed"); } ~Lock() { vSemaphoreDelete(lock); } @@ -111,4 +111,4 @@ struct signal_group { EventGroupHandle_t event_group; }; -#endif //SIMPLE_CXX_CLIENT_TERMINAL_OBJECTS_HPP +#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_PRIMITIVES_HPP diff --git a/esp_modem/include/cxx_include/esp_modem_terminal.hpp b/esp_modem/include/cxx_include/esp_modem_terminal.hpp new file mode 100644 index 000000000..8cce80618 --- /dev/null +++ b/esp_modem/include/cxx_include/esp_modem_terminal.hpp @@ -0,0 +1,43 @@ +// +// Created by david on 3/3/21. +// + +#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP +#define SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP + + +#include +#include +#include +#include +#include +#include +#include "esp_err.h" +#include "esp_modem_primitives.hpp" + + +enum class terminal_error { + BUFFER_OVERFLOW, + CHECKSUM_ERROR, + UNEXPECTED_CONTROL_FLOW, +}; + +class Terminal { +public: + virtual ~Terminal() = default; + virtual void set_data_cb(std::function f) { on_data = std::move(f); } + void set_error_cb(std::function f) { on_error = std::move(f); } + virtual int write(uint8_t *data, size_t len) = 0; + virtual int read(uint8_t *data, size_t len) = 0; + virtual void start() = 0; + virtual void stop() = 0; + virtual size_t max_virtual_terms() { return 1; } + +protected: + std::function on_data; + std::function on_error; +}; + + + +#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_TERMINAL_HPP diff --git a/esp_modem/include/cxx_include/esp_modem_types.hpp b/esp_modem/include/cxx_include/esp_modem_types.hpp new file mode 100644 index 000000000..87ddbce35 --- /dev/null +++ b/esp_modem/include/cxx_include/esp_modem_types.hpp @@ -0,0 +1,43 @@ +// +// Created by david on 3/8/21. +// + +#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP +#define SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP + + +enum class modem_mode { + UNDEF, + COMMAND_MODE, + DATA_MODE, + CMUX_MODE +}; + +enum class command_result { + OK, + FAIL, + TIMEOUT +}; + +typedef std::function got_line_cb; + +struct PdpContext { + explicit PdpContext(std::string& apn): context_id(1), protocol_type("IP"), apn(apn) {} + size_t context_id; + std::string protocol_type; + std::string apn; +}; + + +class CommandableIf { +public: + virtual command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) = 0; +}; + +class ModuleIf { +public: + virtual bool setup_data_mode() = 0; + virtual bool set_mode(modem_mode mode) = 0; +}; + +#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_TYPES_HPP diff --git a/esp_modem/include/cxx_include/esp_modem_uart_terminal.hpp b/esp_modem/include/cxx_include/esp_modem_uart_terminal.hpp deleted file mode 100644 index 84c4c7b3a..000000000 --- a/esp_modem/include/cxx_include/esp_modem_uart_terminal.hpp +++ /dev/null @@ -1,10 +0,0 @@ -// -// Created by david on 3/3/21. -// - -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_UART_TERMINAL_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_UART_TERMINAL_HPP - - - -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_UART_TERMINAL_HPP diff --git a/esp_modem/include/esp_modem_api.h b/esp_modem/include/esp_modem_api.h new file mode 100644 index 000000000..6482e35b1 --- /dev/null +++ b/esp_modem/include/esp_modem_api.h @@ -0,0 +1,33 @@ +// +// Created by david on 3/8/21. +// + +#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_API_H +#define SIMPLE_CXX_CLIENT_ESP_MODEM_API_H +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct esp_modem_dce_wrap esp_modem_t; + +esp_modem_t *esp_modem_new(const dte_config *config, esp_netif_t *netif, const char* apn); + +void esp_modem_destroy(esp_modem_t * dce); + + +#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \ + esp_err_t esp_modem_ ## name(esp_modem_t *dce, ##__VA_ARGS__); + +DECLARE_ALL_COMMAND_APIS(declares esp_modem_(esp_modem_t * dce, ...);) + +#undef ESP_MODEM_DECLARE_DCE_COMMAND + + +#ifdef __cplusplus +} +#endif + + +#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_API_H diff --git a/esp_modem/include/generate/esp_modem_command_declare.inc b/esp_modem/include/generate/esp_modem_command_declare.inc new file mode 100644 index 000000000..13b17dc9b --- /dev/null +++ b/esp_modem/include/generate/esp_modem_command_declare.inc @@ -0,0 +1,73 @@ +// +// Created by david on 3/7/21. +// + +#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC +#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC + +#ifdef __cplusplus +#define STRING_IN const std::string& +#define STRING_OUT std::string& +#else +#define STRING_IN const char* +#define STRING_OUT char* +#endif + + +#define ESP_MODEM_DEFINE_DCE_COMMAND(...) ESP_MODEM_DECLARE_DCE_COMMAND(##__VA_ARGS__) +#define DEFINE_ALL_COMMAND_APIS() DECLARE_ALL_COMMAND_APIS() + +#define DECLARE_ALL_COMMAND_APIS(...) \ +/** + * @brief Sends the supplied PIN code + * + * @param pin Pin + */ \ + \ +ESP_MODEM_DECLARE_DCE_COMMAND(set_pin, command_result, TEMPLATE_ARG, MUX_ARG, STRING_IN) \ +/** + * @brief Checks if the SIM needs a PIN + * + * @param[out] pin_ok Pin + */ \ +ESP_MODEM_DECLARE_DCE_COMMAND(read_pin, command_result, TEMPLATE_ARG, MUX_ARG, bool& pin_ok) \ + \ +ESP_MODEM_DECLARE_DCE_COMMAND(set_echo, command_result, TEMPLATE_ARG, MUX_ARG, bool on) \ + \ +ESP_MODEM_DECLARE_DCE_COMMAND(resume_data_mode, command_result, TEMPLATE_ARG, MUX_ARG) \ + \ +ESP_MODEM_DECLARE_DCE_COMMAND(set_pdp_context, command_result, TEMPLATE_ARG, MUX_ARG, PdpContext& pdp_context) \ + \ +ESP_MODEM_DECLARE_DCE_COMMAND(set_command_mode, command_result, TEMPLATE_ARG, MUX_ARG) \ + \ +ESP_MODEM_DECLARE_DCE_COMMAND(set_cmux, command_result, TEMPLATE_ARG, MUX_ARG) \ + \ +ESP_MODEM_DECLARE_DCE_COMMAND(get_imsi, command_result, TEMPLATE_ARG, MUX_ARG, std::string& imsi_number) \ + \ +ESP_MODEM_DECLARE_DCE_COMMAND(get_imei, command_result, TEMPLATE_ARG, MUX_ARG, std::string& imsi_number) \ + \ +/** + * @brief Reads the module name + * + * @param[out] module name + */ \ +ESP_MODEM_DECLARE_DCE_COMMAND(get_module_name, command_result, TEMPLATE_ARG, MUX_ARG, std::string& name) \ +\ +/** + * @brief Sets the modem to data mode + * + */ \ +ESP_MODEM_DECLARE_DCE_COMMAND(set_data_mode, command_result, TEMPLATE_ARG, MUX_ARG) + +// --- DCE command documentation starts here --- +#ifdef GENERATE_DOCS +// gcc -E -CC -P -DGENERATE_DOCS esp_modem_command_declare.inc | sed -n '/DCE command documentation/,//p' + +#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \ +return_type name (__VA_ARGS__); + +DECLARE_ALL_COMMAND_APIS() +#endif + + +#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMAND_DECLARE_INC diff --git a/esp_modem/include/cxx_include/uart_terminal.hpp b/esp_modem/private_include/uart_terminal.hpp similarity index 100% rename from esp_modem/include/cxx_include/uart_terminal.hpp rename to esp_modem/private_include/uart_terminal.hpp diff --git a/esp_modem/src/esp_modem_api.cpp b/esp_modem/src/esp_modem_api.cpp index 29edb6a64..89a2982bc 100644 --- a/esp_modem/src/esp_modem_api.cpp +++ b/esp_modem/src/esp_modem_api.cpp @@ -2,9 +2,10 @@ // Created by david on 3/3/21. // #include "cxx_include/esp_modem_dte.hpp" -#include "cxx_include/uart_terminal.hpp" +#include "uart_terminal.hpp" #include "esp_log.h" #include "cxx_include/esp_modem_api.hpp" +#include "esp_modem_api.h" static const char *TAG = "dce_factory"; struct PdpContext; @@ -12,7 +13,6 @@ struct PdpContext; std::shared_ptr create_uart_dte(const dte_config *config) { try { -// auto term = std::make_unique(std::make_unique(config)); auto term = create_uart_terminal(config); return std::make_shared(std::move(term)); } catch (std::bad_alloc& e) { @@ -27,10 +27,11 @@ std::shared_ptr create_uart_dte(const dte_config *config) } -std::unique_ptr create_dce(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif) +template +std::unique_ptr> create_dce(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif) { try { - return std::make_unique(dte, dev, netif); + return std::make_unique>(dte, dev, netif); } catch (std::bad_alloc& e) { ESP_LOGE(TAG, "Out of memory"); return nullptr; @@ -42,3 +43,74 @@ std::unique_ptr create_dce(const std::shared_ptr& dte, const std::shar } } +std::unique_ptr> create_generic_dce_from_module(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif) +{ + return create_dce(dte, dev, netif); +} + +std::unique_ptr> create_SIM7600_dce_from_module(const std::shared_ptr& dte, const std::shared_ptr& dev, esp_netif_t *netif) +{ + return create_dce(dte, dev, netif); +} + +std::unique_ptr> create_SIM7600_dce(const std::shared_ptr& dte, esp_netif_t *netif, std::string &apn) +{ + auto pdp = std::make_unique(apn); + auto dev = std::make_shared(dte, std::move(pdp)); + + return create_dce(dte, std::move(dev), netif); +} + + +// +// C API definitions + + +struct esp_modem_dce_wrap // need to mimic the polymorphic dispatch as CPP uses templated dispatch +{ + enum esp_modem_t { MODEM_SIM7600, MODEM_SIM800, MODEM_BG96 } modem_type; + void * dce_ptr; +}; + +static inline esp_err_t command_response_to_esp_err(command_result res) +{ + switch (res) { + case command_result::OK: + return ESP_OK; + case command_result::FAIL: + return ESP_FAIL; + case command_result::TIMEOUT: + return ESP_ERR_TIMEOUT; + } + return ESP_ERR_INVALID_ARG; +} + +extern "C" esp_modem_t *esp_modem_new(const dte_config *config, esp_netif_t *netif, const char* apn) +{ + auto dce_wrap = new (std::nothrow) esp_modem_dce_wrap; + if (dce_wrap == nullptr) + return nullptr; + std::string apn_str(apn); + auto dte = create_uart_dte(config); + auto dce = create_SIM7600_dce(dte, netif, apn_str); + dce_wrap->modem_type = esp_modem_dce_wrap::MODEM_SIM7600; + dce_wrap->dce_ptr = dce.release(); + return dce_wrap; +} + +extern "C" void esp_modem_destroy(esp_modem_t * dce) +{ + assert(dce->modem_type == esp_modem_dce_wrap::MODEM_SIM7600); + auto dce_sim7600 = static_cast*>(dce->dce_ptr); + delete dce_sim7600; + delete dce; +} + +extern "C" esp_err_t esp_modem_read_pin(esp_modem_t * dce, bool &x) +{ + assert(dce->modem_type == esp_modem_dce_wrap::MODEM_SIM7600); + auto dce_sim7600 = static_cast*>(dce->dce_ptr); + return command_response_to_esp_err(dce_sim7600->read_pin(x)); +} + +// define all commands manually, as we have to convert char* strings to CPP strings and references to \ No newline at end of file diff --git a/esp_modem/include/cxx_include/esp_modem_commands.hpp b/esp_modem/src/esp_modem_command_library.cpp similarity index 65% rename from esp_modem/include/cxx_include/esp_modem_commands.hpp rename to esp_modem/src/esp_modem_command_library.cpp index 5c928f0a8..51b419db4 100644 --- a/esp_modem/include/cxx_include/esp_modem_commands.hpp +++ b/esp_modem/src/esp_modem_command_library.cpp @@ -1,24 +1,18 @@ // -// Created by david on 3/2/21. +// Created by david on 3/8/21. // -#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_COMMANDS_HPP -#define SIMPLE_CXX_CLIENT_ESP_MODEM_COMMANDS_HPP - -#include "esp_modem_dte.hpp" -#include "esp_modem_dce_commands.hpp" +#include "cxx_include/esp_modem_dte.hpp" +#include "cxx_include/esp_modem_dce_module.hpp" +#include "cxx_include/esp_modem_command_library.hpp" #include -enum class command_result; - - - +// TODO: Remove iostream #include -namespace esp_modem { -namespace dce_commands { +namespace esp_modem::dce_commands { -template static inline command_result generic_command(T t, const std::string& command, +static inline command_result generic_command(CommandableIf* t, const std::string& command, const std::string& pass_phrase, const std::string& fail_phrase, uint32_t timeout_ms) { @@ -35,16 +29,8 @@ template static inline command_result generic_command(T t, const st return command_result::TIMEOUT; }, timeout_ms); } -static inline void strip_cr_lf_tail(char *str, uint32_t len) -{ - if (str[len - 2] == '\r') { - str[len - 2] = '\0'; - } else if (str[len - 1] == '\r') { - str[len - 1] = '\0'; - } -} -template static inline command_result generic_get_string(T t, const std::string& command, std::string& output, uint32_t timeout_ms) +static inline command_result generic_get_string(CommandableIf* t, const std::string& command, std::string& output, uint32_t timeout_ms) { std::cout << command << std::endl; return t->command(command.c_str(), [&](uint8_t *data, size_t len) { @@ -62,13 +48,7 @@ template static inline command_result generic_get_string(T t, const } else if (token.find("ERROR") != std::string::npos) { return command_result::FAIL; } else if (token.length() > 2) { -// response.resize(response.find('\r')); -// response.erase(std::find(response.begin(), response.end(), '\r'), response.end()); -// std::cout << "|" << output << "|" << std::endl; -// const std::string& out(response); - output = token; //.substr(0,-1); - std::cout << "|" << token << "|" << std::endl; -// std::cout << output << std::endl; + output = token; } response = response.substr(pos+1); } @@ -76,42 +56,42 @@ template static inline command_result generic_get_string(T t, const }, timeout_ms); } -template static inline command_result generic_command_common(T t, std::string command) +static inline command_result generic_command_common(CommandableIf* t, std::string command) { return generic_command(t, command, "OK", "ERROR", 500); } -template command_result sync(T t) +command_result sync(CommandableIf* t) { return generic_command_common(t, "AT\r"); } -template command_result set_echo(T t, bool on) +command_result set_echo(CommandableIf* t, bool on) { if (on) return generic_command_common(t, "ATE1\r"); return generic_command_common(t, "ATE0\r"); } -template command_result set_pdp_context(T t, PdpContext& pdp) +command_result set_pdp_context(CommandableIf* t, PdpContext& pdp) { std::string pdp_command = "AT+CGDCONT=" + std::to_string(pdp.context_id) + ",\"" + pdp.protocol_type + "\",\"" + pdp.apn + "\"\r"; return generic_command_common(t, pdp_command); } -template command_result set_data_mode(T t) +command_result set_data_mode(CommandableIf* t) { return generic_command(t, "ATD*99##\r", "CONNECT", "ERROR", 5000); } -template command_result resume_data_mode(T t) +command_result resume_data_mode(CommandableIf* t) { return generic_command(t, "ATO\r", "CONNECT", "ERROR", 5000); } -template command_result set_command_mode(T t) +command_result set_command_mode(CommandableIf* t) { std::cout << "Sending +++" << std::endl; return t->command("+++", [&](uint8_t *data, size_t len) { @@ -128,27 +108,27 @@ template command_result set_command_mode(T t) }, 5000); } -template command_result get_imsi(T t, std::string& imsi_number) +command_result get_imsi(CommandableIf* t, std::string& imsi_number) { return generic_get_string(t, "AT+CIMI\r", imsi_number, 5000); } -template command_result get_imei(T t, std::string& out) +command_result get_imei(CommandableIf* t, std::string& out) { return generic_get_string(t, "AT+CGSN\r", out, 5000); } -template command_result get_module_name(T t, std::string& out) +command_result get_module_name(CommandableIf* t, std::string& out) { return generic_get_string(t, "AT+CGMM\r", out, 5000); } -template command_result set_cmux(T t) +command_result set_cmux(CommandableIf* t) { return generic_command_common(t, "AT+CMUX=0\r"); } -template command_result read_pin(T t, bool& pin_ok) +command_result read_pin(CommandableIf* t, bool& pin_ok) { std::cout << "Sending read_pin" << std::endl; return t->command("AT+CPIN?\r", [&](uint8_t *data, size_t len) { @@ -167,15 +147,11 @@ template command_result read_pin(T t, bool& pin_ok) }, 5000); } -template command_result set_pin(T t, const std::string& pin) +command_result set_pin(CommandableIf* t, const std::string& pin) { std::cout << "Sending set_pin" << std::endl; std::string set_pin_command = "AT+CPIN=" + pin + "\r"; return generic_command_common(t, set_pin_command); } -} // dce_commands -} // esp_modem - - -#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_COMMANDS_HPP +} // esp_modem::dce_commands \ No newline at end of file diff --git a/esp_modem/src/esp_modem_dce.cpp b/esp_modem/src/esp_modem_dce.cpp deleted file mode 100644 index 9efb60c2f..000000000 --- a/esp_modem/src/esp_modem_dce.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "cxx_include/esp_modem_dte.hpp" -#include "cxx_include/esp_modem_dce.hpp" -#include - -#include -#include "esp_log.h" - -DCE::DCE(const std::shared_ptr& dte, std::shared_ptr device, esp_netif_t * netif): - dce_dte(dte), device(std::move(device)), ppp_netif(dte, netif) -{ } - -void DCE::exit_data() { - ppp_netif.stop(); - device->set_mode(dte_mode::COMMAND_MODE); - uint8_t* data; - dce_dte->set_data_cb([&](size_t len) -> bool { - auto actual_len = dce_dte->read(&data, 64); - ESP_LOG_BUFFER_HEXDUMP("esp-modem: debug_data", data, actual_len, ESP_LOG_INFO); - return false; - }); - ppp_netif.wait_until_ppp_exits(); - dce_dte->set_data_cb(nullptr); - dce_dte->set_mode(dte_mode::COMMAND_MODE); -} - -void DCE::set_cmux() -{ - device->set_mode(dte_mode::CMUX_MODE); - dce_dte->set_mode(dte_mode::CMUX_MODE); -// auto t = dce_dte->detach(); -// auto cmux = std::make_unique(std::move(t), dce_dte->get_buffer()); -// dce_dte->attach(std::move(cmux)); -} \ No newline at end of file diff --git a/esp_modem/src/esp_modem_device.cpp b/esp_modem/src/esp_modem_device.cpp deleted file mode 100644 index ad684d38d..000000000 --- a/esp_modem/src/esp_modem_device.cpp +++ /dev/null @@ -1,46 +0,0 @@ -// -// Created by david on 3/3/21. -// - -#include "cxx_include/esp_modem_dce_commands.hpp" -#include "cxx_include/esp_modem_dte.hpp" - -#include "cxx_include/esp_modem_commands.hpp" - - - -bool Device::setup_data_mode() { - if (set_echo(false) != command_result::OK) - return false; - if (set_pdp_context(*pdp) != command_result::OK) - return false; -// if (set_data_mode() != command_result::OK) -// return false; - return true; -} - -bool Device::set_mode(dte_mode mode) { - if (mode == dte_mode::DATA_MODE) { - if (set_data_mode() != command_result::OK) - return resume_data_mode() == command_result::OK; - return true; - } else if (mode == dte_mode::COMMAND_MODE) { - return set_command_mode() == command_result::OK; - } else if (mode == dte_mode::CMUX_MODE) { - return set_cmux() == command_result::OK; - } - return true; -} - - -command_result Device::set_echo(bool on) { return esp_modem::dce_commands::set_echo(dte, on); } -command_result Device::set_data_mode() { return esp_modem::dce_commands::set_data_mode(dte); } -command_result Device::resume_data_mode() { return esp_modem::dce_commands::resume_data_mode(dte); } -command_result Device::set_pdp_context(PdpContext& pdp_context) { return esp_modem::dce_commands::set_pdp_context(dte.get(), pdp_context); } -command_result Device::set_command_mode() { return esp_modem::dce_commands::set_command_mode(dte); } -command_result Device::set_cmux() { return esp_modem::dce_commands::set_cmux(dte); } -command_result Device::get_imsi(std::string& imsi_number) { return esp_modem::dce_commands::get_imsi(dte, imsi_number); } -command_result Device::set_pin(const std::string& pin) { return esp_modem::dce_commands::set_pin(dte, pin); } -command_result Device::read_pin(bool& pin_ok) { return esp_modem::dce_commands::read_pin(dte, pin_ok); } -command_result Device::get_imei(std::string &imei) { return esp_modem::dce_commands::get_imei(dte, imei); } -command_result Device::get_module_name(std::string &name) { return esp_modem::dce_commands::get_module_name(dte, name); } diff --git a/esp_modem/src/esp_modem_device_factory.cpp b/esp_modem/src/esp_modem_device_factory.cpp deleted file mode 100644 index 2fd49a573..000000000 --- a/esp_modem/src/esp_modem_device_factory.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "cxx_include/esp_modem_dce_commands.hpp" -#include "cxx_include/esp_modem_dte.hpp" - -//std::shared_ptr create_device(const std::shared_ptr& dte, std::string &apn) -std::shared_ptr create_device(const std::shared_ptr& dte, std::string &apn) -{ - auto pdp = std::make_unique(apn); - return std::make_shared(dte, std::move(pdp)); -} - diff --git a/esp_modem/src/esp_modem_dte.cpp b/esp_modem/src/esp_modem_dte.cpp index 73a013467..2aa467df1 100644 --- a/esp_modem/src/esp_modem_dte.cpp +++ b/esp_modem/src/esp_modem_dte.cpp @@ -5,10 +5,11 @@ DTE::DTE(std::unique_ptr terminal): buffer_size(DTE_BUFFER_SIZE), consumed(0), buffer(std::make_unique(buffer_size)), - term(std::move(terminal)), mode(dte_mode::UNDEF) {} + term(std::move(terminal)), mode(modem_mode::UNDEF) {} -command_result DTE::command(const std::string& command, got_line_cb got_line, uint32_t time_ms) +command_result DTE::command(const std::string &command, got_line_cb got_line, uint32_t time_ms) { +// assert(term_id < term->max_virtual_terms()); Scoped l(lock); command_result res = command_result::TIMEOUT; term->write((uint8_t *)command.c_str(), command.length()); diff --git a/esp_modem/src/esp_modem_factory.cpp b/esp_modem/src/esp_modem_factory.cpp new file mode 100644 index 000000000..e33a48158 --- /dev/null +++ b/esp_modem/src/esp_modem_factory.cpp @@ -0,0 +1,21 @@ +#include +#include "cxx_include/esp_modem_types.hpp" +#include "cxx_include/esp_modem_dte.hpp" +#include "cxx_include/esp_modem_dce_module.hpp" + +template +std::shared_ptr create_device(const std::shared_ptr& dte, std::string &apn) +{ + auto pdp = std::make_unique(apn); + return std::make_shared(dte, std::move(pdp)); +} + +std::shared_ptr create_generic_module(const std::shared_ptr& dte, std::string &apn) +{ + return create_device(dte, apn); +} + +std::shared_ptr create_SIM7600_module(const std::shared_ptr& dte, std::string &apn) +{ + return create_device(dte, apn); +} diff --git a/esp_modem/src/esp_modem_modules.cpp b/esp_modem/src/esp_modem_modules.cpp new file mode 100644 index 000000000..5becd9e50 --- /dev/null +++ b/esp_modem/src/esp_modem_modules.cpp @@ -0,0 +1,23 @@ +// +// Created by david on 3/8/21. +// + +#include "cxx_include/esp_modem_dce_module.hpp" + +command_result SIM7600::get_module_name(std::string& name) +{ + name = "7600"; + return command_result::OK; +} + +command_result SIM800::get_module_name(std::string& name) +{ + name = "800L"; + return command_result::OK; +} + +command_result BG96::get_module_name(std::string& name) +{ + name = "BG96"; + return command_result::OK; +} diff --git a/esp_modem/src/ppp_netif.cpp b/esp_modem/src/esp_modem_netif.cpp similarity index 97% rename from esp_modem/src/ppp_netif.cpp rename to esp_modem/src/esp_modem_netif.cpp index 407c70e35..a0ab9b331 100644 --- a/esp_modem/src/ppp_netif.cpp +++ b/esp_modem/src/esp_modem_netif.cpp @@ -5,15 +5,12 @@ #include #include #include -#include "cxx_include/ppp_netif.hpp" +#include "cxx_include/esp_modem_netif.hpp" #include "cxx_include/esp_modem_dte.hpp" #include "esp_netif_ppp.h" #include -//struct ppp_netif_driver { -// esp_netif_driver_base_t base; -//}; void PPP::on_ppp_changed(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) diff --git a/esp_modem/src/esp_modem_uart.cpp b/esp_modem/src/esp_modem_uart.cpp index 6c497e523..743bb1800 100644 --- a/esp_modem/src/esp_modem_uart.cpp +++ b/esp_modem/src/esp_modem_uart.cpp @@ -177,11 +177,8 @@ std::unique_ptr create_uart_terminal(const dte_config *config) ESP_LOGE(TAG, "%s", e.what()); return nullptr; } - } - - void uart_terminal::task() { std::function on_data_priv = nullptr;