DCE factory implementation

This commit is contained in:
David Cermak
2021-03-28 20:22:44 +02:00
parent e98e6c42f6
commit 30bde53d46
13 changed files with 156 additions and 189 deletions

View File

@ -8,15 +8,26 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
using namespace esp_modem;
using namespace esp_modem::dce_factory;
class NetModule; class NetModule;
typedef DCE_T<NetModule> NetDCE; typedef DCE_T<NetModule> NetDCE;
class NetDCE_Factory: public Factory {
public:
template <typename T, typename ...Args>
static DCE_T<T>* create(const config *cfg, Args&&... args)
{
return build_generic_DCE< /* Object to create */ DCE_T<T>, /* vanilla pointer */ DCE_T<T> *, /* module */ T>
(cfg, std::forward<Args>(args)...);
}
};
class NetModule: public ModuleIf { class NetModule: public ModuleIf {
public: public:
explicit NetModule(std::shared_ptr<DTE> dte, std::unique_ptr<PdpContext> pdp): explicit NetModule(std::shared_ptr<DTE> dte, const esp_modem_dce_config *cfg):
dte(std::move(dte)) {} dte(std::move(dte)), apn(std::string(cfg->apn)) {}
explicit NetModule(std::shared_ptr<DTE> dte, esp_modem_dce_config *cfg):
dte(std::move(dte)) {}
bool setup_data_mode() override bool setup_data_mode() override
{ {
@ -26,10 +37,10 @@ public:
return false; return false;
if (!is_pin_ok) { if (!is_pin_ok) {
if (set_pin(pin) != command_result::OK) if (set_pin(pin) != command_result::OK)
return ESP_FAIL; return false;
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
if (read_pin(is_pin_ok) != command_result::OK || !is_pin_ok) if (read_pin(is_pin_ok) != command_result::OK || !is_pin_ok)
return ESP_FAIL; return false;
} }
PdpContext pdp(apn); PdpContext pdp(apn);
@ -51,36 +62,20 @@ public:
return false; return false;
} }
static esp_err_t init(esp_netif_t *netif, std::string apn_name, std::string pin_number) static esp_err_t init(esp_netif_t *netif, const std::string& apn, std::string pin_number)
{ {
apn = std::move(apn_name); // configure
pin = std::move(pin_number); pin = std::move(pin_number);
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.event_task_stack_size = 4096; dte_config.event_task_stack_size = 4096;
dte_config.rx_buffer_size = 16384; dte_config.rx_buffer_size = 16384;
dte_config.tx_buffer_size = 2048; dte_config.tx_buffer_size = 2048;
esp_modem_dce_config dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(apn.c_str());
// create // create DTE and minimal network DCE
auto uart_dte = create_uart_dte(&dte_config); auto uart_dte = create_uart_dte(&dte_config);
// NetModule* module; dce = NetDCE_Factory::create<NetModule>(&dce_config, uart_dte, netif);
// if (!f.build_module_T<NetModule>(module, uart_dte, netif)) { return dce == nullptr ? ESP_FAIL : ESP_OK;
// return ESP_OK;
// }
// esp_modem::DCE::Factory f2(esp_modem::DCE::Modem::SIM7600);
esp_modem::DCE::config dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG("internet");
// std::shared_ptr<MinimalModule> dev;
// auto dev = f2.build_shared_module(&dce_config, uart_dte, netif);
// auto module = esp_modem::DCE::Factory::build_shared_module<NetModule>(&dce_config, uart_dte, netif);
dce = esp_modem::DCE::Factory::build<NetModule>(&dce_config, uart_dte, netif);
// esp_modem::DCE::Builder<MinimalModule> factory(uart_dte, netif);
// dce = factory.create(apn_name);
// auto pdp = std::make_unique<PdpContext>(apn);
// auto dev = std::make_shared<MinimalModule>(uart_dte, std::move(pdp));
// auto dev = std::make_shared<MinimalModule>(uart_dte, nullptr);
// dce = new DCE_T<MinimalModule>(uart_dte, module, netif);
return ESP_OK;
} }
static void deinit() { delete dce; } static void deinit() { delete dce; }
@ -90,19 +85,18 @@ public:
private: private:
static NetDCE *dce; static NetDCE *dce;
std::shared_ptr<DTE> dte; std::shared_ptr<DTE> dte;
static std::string apn; std::string apn;
static std::string pin; static std::string pin;
template <typename ...T> command_result set_pdp_context(T&&... args) { return esp_modem::dce_commands::set_pdp_context(dte.get(),std::forward<T>(args)...); } template <typename ...T> command_result set_pdp_context(T&&... args) { return dce_commands::set_pdp_context(dte.get(),std::forward<T>(args)...); }
template <typename ...T> command_result set_pin(T&&... args) { return esp_modem::dce_commands::set_pin(dte.get(),std::forward<T>(args)...); } template <typename ...T> command_result set_pin(T&&... args) { return dce_commands::set_pin(dte.get(),std::forward<T>(args)...); }
template <typename ...T> command_result read_pin(T&&... args) { return esp_modem::dce_commands::read_pin(dte.get(),std::forward<T>(args)...); } template <typename ...T> command_result read_pin(T&&... args) { return dce_commands::read_pin(dte.get(),std::forward<T>(args)...); }
command_result set_data_mode() { return esp_modem::dce_commands::set_data_mode(dte.get()); } command_result set_data_mode() { return dce_commands::set_data_mode(dte.get()); }
command_result resume_data_mode() { return esp_modem::dce_commands::resume_data_mode(dte.get()); } command_result resume_data_mode() { return dce_commands::resume_data_mode(dte.get()); }
command_result set_command_mode() { return esp_modem::dce_commands::set_command_mode(dte.get()); } command_result set_command_mode() { return dce_commands::set_command_mode(dte.get()); }
}; };
NetDCE *NetModule::dce = nullptr; NetDCE *NetModule::dce = nullptr;
std::string NetModule::apn;
std::string NetModule::pin; std::string NetModule::pin;
extern "C" esp_err_t modem_init_network(esp_netif_t *netif) extern "C" esp_err_t modem_init_network(esp_netif_t *netif)

View File

@ -350,8 +350,10 @@ extern "C" void app_main(void)
esp_netif_t *esp_netif = esp_netif_new(&ppp_netif_config); esp_netif_t *esp_netif = esp_netif_new(&ppp_netif_config);
assert(esp_netif); assert(esp_netif);
auto uart_dte = create_uart_dte(&dte_config); auto uart_dte = create_uart_dte(&dte_config);
std::string apn = "internet";
auto dce = create_SIM7600_dce(uart_dte, esp_netif, apn); esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG("internet");
auto dce = create_SIM7600_dce(&dce_config, uart_dte, esp_netif);
assert(dce != NULL); assert(dce != NULL);
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();

View File

@ -22,4 +22,6 @@ std::unique_ptr<DCE> create_generic_dce_from_module(const std::shared_ptr<DTE>&
std::unique_ptr<DCE> create_SIM7600_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SIM7600>& dev, esp_netif_t *netif); std::unique_ptr<DCE> create_SIM7600_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SIM7600>& dev, esp_netif_t *netif);
std::unique_ptr<DCE> create_SIM7600_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn); std::unique_ptr<DCE> create_SIM7600_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif);
std::unique_ptr<DCE> create_SIM800_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif);
std::unique_ptr<DCE> create_BG96_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif);

View File

@ -3,9 +3,9 @@
#include "cxx_include/esp_modem_netif.hpp" #include "cxx_include/esp_modem_netif.hpp"
#include "cxx_include/esp_modem_dce_module.hpp" #include "cxx_include/esp_modem_dce_module.hpp"
#include "generate/esp_modem_command_declare.inc" //#include "generate/esp_modem_command_declare.inc"
namespace esp_modem::DCE { namespace esp_modem::dce_factory {
class Modes { class Modes {
public: public:
@ -36,6 +36,8 @@ public:
void set_cmux() { set_mode(modem_mode::CMUX_MODE); } void set_cmux() { set_mode(modem_mode::CMUX_MODE); }
ModuleIf* get_module() { return module.get(); }
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)
{ {
@ -49,11 +51,13 @@ protected:
std::shared_ptr<DTE> dte; std::shared_ptr<DTE> dte;
std::shared_ptr<SpecificModule> module; std::shared_ptr<SpecificModule> module;
Netif netif; Netif netif;
esp_modem::DCE::Modes mode; esp_modem::dce_factory::Modes mode;
}; };
//typedef DCE_T<GenericModule> DCE;
//
//#if 0
class DCE: public DCE_T<GenericModule> { class DCE: public DCE_T<GenericModule> {
public: public:
@ -69,4 +73,5 @@ public:
#undef ESP_MODEM_DECLARE_DCE_COMMAND #undef ESP_MODEM_DECLARE_DCE_COMMAND
}; };
//#endif

View File

@ -5,9 +5,7 @@
#ifndef AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP #ifndef AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP
#define AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP #define AP_TO_PPPOS_ESP_MODEM_DCE_FACTORY_HPP
#include <esp_log.h> namespace esp_modem::dce_factory {
namespace esp_modem::DCE {
using config = esp_modem_dce_config; using config = esp_modem_dce_config;
@ -32,7 +30,7 @@ using config = esp_modem_dce_config;
template <typename T, typename ...Args> template <typename T, typename ...Args>
static bool make(std::unique_ptr<T> &t, Args&&... args) static bool make(std::unique_ptr<T> &t, Args&&... args)
{ {
t = std::unique_ptr<T>(std::forward<Args>(args)...); t = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
return true; return true;
} }
@ -48,7 +46,7 @@ using config = esp_modem_dce_config;
throw_if_false(netif != nullptr, "Cannot create default PPP netif"); throw_if_false(netif != nullptr, "Cannot create default PPP netif");
} }
Builder(std::shared_ptr<DTE> dte, esp_netif_t* esp_netif): dte(std::move(dte)), module(nullptr), netif(esp_netif) Builder(std::shared_ptr<DTE> x, esp_netif_t* esp_netif): dte(std::move(x)), module(nullptr), netif(esp_netif)
{ {
throw_if_false(netif != nullptr, "Null netif"); throw_if_false(netif != nullptr, "Null netif");
} }
@ -60,100 +58,29 @@ using config = esp_modem_dce_config;
~Builder() ~Builder()
{ {
// throw_if_false(netif == nullptr, "Netif created but never used");
// throw_if_false(dte.use_count() == 0, "dte was captured but never used");
throw_if_false(module == nullptr, "module was captured or created but never used"); throw_if_false(module == nullptr, "module was captured or created but never used");
} }
// DCE_T<T>* create(std::string &apn)
// {
// return create_dce(dte, netif, apn);
// }
// template<typename Ptr>
// bool create_module(Ptr &t, std::string &apn)
// {
// auto pdp = FactoryHelper::create_pdp_context(apn);
// return FactoryHelper::make<T>(t, std::move(dte), std::move(pdp));
// }
template<typename Ptr> template<typename Ptr>
bool create_module(Ptr &t, esp_modem_dce_config *config) bool create_module(Ptr &t, const esp_modem_dce_config *config)
{ {
return FactoryHelper::make<T>(t, dte, config); return FactoryHelper::make<T>(t, dte, config);
} }
template<typename Ptr> template<typename DceT, typename Ptr>
bool create(Ptr &t, esp_modem_dce_config *config) bool create(Ptr &t, const esp_modem_dce_config *config)
{ {
if (dte == nullptr) if (dte == nullptr)
return false; return false;
if (module == nullptr) { if (module == nullptr) {
if (!create_module(module, config)) { if (!create_module(module, config)) {
ESP_LOGE("builder", "Failed to build module");
return false; return false;
} }
} }
ESP_LOGI("builder", "create_module passed"); return FactoryHelper::make<DceT>(t, std::move(dte), std::move(module), netif);
return FactoryHelper::make<DCE_T<T>>(t, std::move(dte), std::move(module), netif);
} }
// bool create_module(std::shared_ptr<T> &t, std::string &apn)
// {
// auto pdp = FactoryHelper::create_pdp_context(apn);
// t = std::make_shared<T>(std::move(dte), std::move(pdp));
// return true;
// }
// template<typename U, typename V>
// U create_device(std::string &apn)
// {
// auto pdp = FactoryHelper::create_pdp_context(apn);
// return FactoryHelper::make<V>(std::move(dte), std::move(pdp));
// }
//
// T* create_dev(std::string &apn)
// {
// auto pdp = FactoryHelper::create_pdp_context(apn);
// return new T(std::move(dte), std::move(pdp));
// }
//
// std::shared_ptr<T> create_shared_dev(std::string &apn)
// {
// auto pdp = FactoryHelper::create_pdp_context(apn);
// return std::make_shared<T>(std::move(dte), std::move(pdp));
// }
//
// static DCE_T<T>* create_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<T>& dev, esp_netif_t *netif)
// {
// return new DCE_T<T>(dte, dev, netif);
// }
//
// static std::unique_ptr<DCE_T<T>> create_unique_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<T>& dev, esp_netif_t *netif)
// {
// return std::unique_ptr<DCE_T<T>>(new DCE_T<T>(dte, dev, netif));
// }
// static std::shared_ptr<T> create_module(const std::shared_ptr<DTE>& dte, std::string &apn)
// {
// auto pdp = FactoryHelper::create_pdp_context(apn);
// return std::make_shared<T>(dte, std::move(pdp));
// }
//
// static std::unique_ptr<DCE_T<T>> create_unique_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn)
// {
// auto module = create_module(dte, apn);
// return create_unique_dce_from_module(dte, std::move(module), netif);
// }
//
// static DCE_T<T>* create_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn)
// {
// auto module = create_module(dte, apn);
// return create_dce_from_module(dte, std::move(module), netif);
// }
private: private:
std::shared_ptr<DTE> dte; std::shared_ptr<DTE> dte;
std::shared_ptr<T> module; std::shared_ptr<T> module;
@ -166,64 +93,33 @@ using config = esp_modem_dce_config;
BG96, BG96,
MinModule MinModule
}; };
class Factory { class Factory {
public: public:
explicit Factory(Modem modem): m(modem) {} explicit Factory(Modem modem): m(modem) {}
template <typename T, typename U, typename ...Args> template <typename T, typename ...Args>
static bool build_module_T(U &t, config *cfg, Args&&... args) static std::unique_ptr<DCE> build_unique(const config *cfg, Args&&... args)
{ {
Builder<T> b(std::forward<Args>(args)...); return build_generic_DCE<DCE, std::unique_ptr<DCE>, T>(cfg, std::forward<Args>(args)...);
return b.create_module(t, cfg);
}
template <typename T, typename U, typename ...Args>
static T build_module_T(config *cfg, Args&&... args)
{
T module;
if (build_module_T<U>(module, cfg, std::forward<Args>(args)...))
return module;
return nullptr;
}
template <typename T, typename U, typename ...Args>
static bool build_T(U &t, config *cfg, Args&&... args)
{
Builder<T> b(std::forward<Args>(args)...);
return b.create(t, cfg);
}
template <typename T, typename U, typename ...Args>
static T build_T(config *cfg, Args&&... args)
{
T dce;
if (build_T<U>(dce, cfg, std::forward<Args>(args)...))
return dce;
return nullptr;
} }
template <typename T, typename ...Args> template <typename T, typename ...Args>
static std::unique_ptr<DCE_T<T>> build_unique(config *cfg, Args&&... args) static DCE* build(const config *cfg, Args&&... args)
{ {
return build_T<std::unique_ptr<DCE_T<T>>, T>(cfg, std::forward<Args>(args)...); return build_generic_DCE<DCE, DCE*, T>(cfg, std::forward<Args>(args)...);
}
template <typename T, typename ...Args>
static DCE_T<T>* build(config *cfg, Args&&... args)
{
return build_T<DCE_T<T>*, T>(cfg, std::forward<Args>(args)...);
} }
template <typename T, typename ...Args> template <typename T, typename ...Args>
static std::shared_ptr<T> build_shared_module(config *cfg, Args&&... args) static std::shared_ptr<T> build_shared_module(const config *cfg, Args&&... args)
{ {
return build_module_T<std::shared_ptr<T>, T>(cfg, std::forward<Args>(args)...); return build_module_T<std::shared_ptr<T>, T>(cfg, std::forward<Args>(args)...);
} }
template <typename ...Args> template <typename ...Args>
std::shared_ptr<GenericModule> build_shared_module(config *cfg, Args&&... args) std::shared_ptr<GenericModule> build_shared_module(const config *cfg, Args&&... args)
{ {
switch (m) { switch (m) {
case Modem::SIM800: case Modem::SIM800:
@ -238,9 +134,59 @@ using config = esp_modem_dce_config;
return nullptr; return nullptr;
} }
template <typename ...Args>
std::unique_ptr<DCE> build_unique(const config *cfg, Args&&... args)
{
switch (m) {
case Modem::SIM800:
return build_unique<SIM800>(cfg, std::forward<Args>(args)...);
case Modem::SIM7600:
return build_unique<SIM7600>(cfg, std::forward<Args>(args)...);
case Modem::BG96:
return build_unique<BG96>(cfg, std::forward<Args>(args)...);
case Modem::MinModule:
break;
}
return nullptr;
}
private: private:
Modem m; Modem m;
template <typename T, typename U, typename ...Args>
static bool build_module_T(U &t, const config *cfg, Args&&... args)
{
Builder<T> b(std::forward<Args>(args)...);
return b.create_module(t, cfg);
}
template <typename T, typename U, typename ...Args>
static T build_module_T(const config *cfg, Args&&... args)
{
T module;
if (build_module_T<U>(module, cfg, std::forward<Args>(args)...))
return module;
return nullptr;
}
template <typename Dce, typename DcePtr, typename Module, typename ...Args>
static bool build_generic_DCE(DcePtr &t, const config *cfg, Args&&... args)
{
Builder<Module> b(std::forward<Args>(args)...);
return b.template create<Dce>(t, cfg);
}
protected:
template <typename Dce, typename DcePtr, typename Module, typename ...Args>
static DcePtr build_generic_DCE(const config *cfg, Args&&... args)
{
DcePtr dce;
if (build_generic_DCE<Dce, DcePtr, Module>(dce, cfg, std::forward<Args>(args)...))
return dce;
return nullptr;
}
}; };
} // esp_modem } // esp_modem

View File

@ -13,7 +13,7 @@ class GenericModule: public ModuleIf {
public: public:
explicit GenericModule(std::shared_ptr<DTE> dte, std::unique_ptr<PdpContext> pdp): explicit GenericModule(std::shared_ptr<DTE> dte, std::unique_ptr<PdpContext> pdp):
dte(std::move(dte)), pdp(std::move(pdp)) {} dte(std::move(dte)), pdp(std::move(pdp)) {}
explicit GenericModule(std::shared_ptr<DTE> dte, esp_modem_dce_config* config); explicit GenericModule(std::shared_ptr<DTE> dte, const esp_modem_dce_config* config);
bool setup_data_mode() override bool setup_data_mode() override
{ {
@ -56,7 +56,7 @@ protected:
class SIM7600: public GenericModule { class SIM7600: public GenericModule {
using GenericModule::GenericModule; using GenericModule::GenericModule;
public: public:
// command_result get_module_name(std::string& name) override; command_result get_module_name(std::string& name) override;
}; };
class SIM800: public GenericModule { class SIM800: public GenericModule {

View File

@ -11,7 +11,6 @@
class DTE; class DTE;
class Netif; class Netif;
//struct ppp_netif_driver;
struct ppp_netif_driver { struct ppp_netif_driver {
esp_netif_driver_base_t base; esp_netif_driver_base_t base;
Netif *ppp; Netif *ppp;

View File

@ -19,7 +19,7 @@ typedef enum esp_modem_dce_mode
ESP_MODEM_MODE_DATA, ESP_MODEM_MODE_DATA,
} esp_modem_dce_mode_t; } esp_modem_dce_mode_t;
esp_modem_dce_t *esp_modem_new(const esp_modem_dte_config_t *config, esp_netif_t *netif, const char* apn); esp_modem_dce_t *esp_modem_new(const esp_modem_dte_config_t *dte_config, const esp_modem_dce_config_t *dce_config, esp_netif_t *netif);
void esp_modem_destroy(esp_modem_dce_t * dce); void esp_modem_destroy(esp_modem_dce_t * dce);
esp_err_t esp_modem_set_mode(esp_modem_dce_t * dce, esp_modem_dce_mode_t mode); esp_err_t esp_modem_set_mode(esp_modem_dce_t * dce, esp_modem_dce_mode_t mode);

View File

@ -5,11 +5,13 @@
#include "uart_terminal.hpp" #include "uart_terminal.hpp"
#include "esp_log.h" #include "esp_log.h"
#include "cxx_include/esp_modem_api.hpp" #include "cxx_include/esp_modem_api.hpp"
#include "cxx_include/esp_modem_dce_factory.hpp"
#include "esp_modem_api.h" #include "esp_modem_api.h"
#include "esp_modem_config.h" #include "esp_modem_config.h"
#include "exception_stub.hpp" #include "exception_stub.hpp"
struct PdpContext; struct PdpContext;
using namespace esp_modem;
static const char *TAG = "modem_api"; static const char *TAG = "modem_api";
@ -36,19 +38,37 @@ std::unique_ptr<DCE> create_generic_dce_from_module(const std::shared_ptr<DTE>&
) )
} }
//std::unique_ptr<DCE<SIM7600>> create_SIM7600_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<SIM7600>& dev, esp_netif_t *netif) //std::unique_ptr<DCE> create_SIM7600_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn)
//{ //{
// return create_dce<SIM7600>(dte, dev, netif); // auto pdp = std::make_unique<PdpContext>(apn);
// auto dev = std::make_shared<SIM7600>(dte, std::move(pdp));
//
// return create_generic_dce_from_module(dte, std::move(dev), netif);
//} //}
std::unique_ptr<DCE> create_SIM7600_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn)
{
auto pdp = std::make_unique<PdpContext>(apn);
auto dev = std::make_shared<SIM7600>(dte, std::move(pdp));
return create_generic_dce_from_module(dte, std::move(dev), netif); static inline std::unique_ptr<DCE> create_modem_dce(dce_factory::Modem m, const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif)
{
dce_factory::Factory f(m);
TRY_CATCH_RET_NULL(
return f.build_unique(config, std::move(dte), netif);
)
} }
std::unique_ptr<DCE> create_SIM7600_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif)
{
return create_modem_dce(dce_factory::Modem::SIM7600, config, std::move(dte), netif);
}
std::unique_ptr<DCE> create_SIM800_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif)
{
return create_modem_dce(dce_factory::Modem::SIM800, config, std::move(dte), netif);
}
std::unique_ptr<DCE> create_BG96_dce(const esp_modem_dce_config *config, std::shared_ptr<DTE> dte, esp_netif_t *netif)
{
return create_modem_dce(dce_factory::Modem::BG96, config, std::move(dte), netif);
}
// //
// C API definitions // C API definitions
@ -73,14 +93,13 @@ static inline esp_err_t command_response_to_esp_err(command_result res)
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
extern "C" esp_modem_dce_t *esp_modem_new(const esp_modem_dte_config_t *config, esp_netif_t *netif, const char* apn) extern "C" esp_modem_dce_t *esp_modem_new(const esp_modem_dte_config_t *dte_config, const esp_modem_dce_config_t *dce_config, esp_netif_t *netif)
{ {
auto dce_wrap = new (std::nothrow) esp_modem_dce_wrap; auto dce_wrap = new (std::nothrow) esp_modem_dce_wrap;
if (dce_wrap == nullptr) if (dce_wrap == nullptr)
return nullptr; return nullptr;
std::string apn_str(apn); auto dte = create_uart_dte(dte_config);
auto dte = create_uart_dte(config); auto dce = create_SIM7600_dce(dce_config, dte, netif);
auto dce = create_SIM7600_dce(dte, netif, apn_str);
dce_wrap->modem_type = esp_modem_dce_wrap::MODEM_SIM7600; dce_wrap->modem_type = esp_modem_dce_wrap::MODEM_SIM7600;
dce_wrap->dce_ptr = dce.release(); dce_wrap->dce_ptr = dce.release();
return dce_wrap; return dce_wrap;

View File

@ -5,7 +5,7 @@
#include "cxx_include/esp_modem_dte.hpp" #include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_dce_module.hpp" #include "cxx_include/esp_modem_dce_module.hpp"
#include "cxx_include/esp_modem_command_library.hpp" #include "cxx_include/esp_modem_command_library.hpp"
#include <string.h> #include <cstring>
// TODO: Remove iostream // TODO: Remove iostream
#include <iostream> #include <iostream>
@ -17,7 +17,7 @@ static inline command_result generic_command(CommandableIf* t, const std::string
const std::string& fail_phrase, uint32_t timeout_ms) const std::string& fail_phrase, uint32_t timeout_ms)
{ {
std::cout << command << std::endl; std::cout << command << std::endl;
return t->command(command.c_str(), [&](uint8_t *data, size_t len) { return t->command(command, [&](uint8_t *data, size_t len) {
std::string response((char*)data, len); std::string response((char*)data, len);
std::cout << response << std::endl; std::cout << response << std::endl;
@ -33,7 +33,7 @@ static inline command_result generic_command(CommandableIf* t, const std::string
static inline command_result generic_get_string(CommandableIf* 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; std::cout << command << std::endl;
return t->command(command.c_str(), [&](uint8_t *data, size_t len) { return t->command(command, [&](uint8_t *data, size_t len) {
size_t pos = 0; size_t pos = 0;
std::string response((char*)data, len); std::string response((char*)data, len);
while ((pos = response.find('\n')) != std::string::npos) { while ((pos = response.find('\n')) != std::string::npos) {

View File

@ -6,7 +6,7 @@
#include "cxx_include/esp_modem_dce.hpp" #include "cxx_include/esp_modem_dce.hpp"
#include "esp_log.h" #include "esp_log.h"
namespace esp_modem::DCE { namespace esp_modem::dce_factory {
bool Modes::set(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m) bool Modes::set(DTE *dte, ModuleIf *device, Netif &netif, modem_mode m)

View File

@ -24,7 +24,7 @@ std::shared_ptr<SIM7600> create_SIM7600_module(const std::shared_ptr<DTE>& dte,
#include "cxx_include/esp_modem_api.hpp" #include "cxx_include/esp_modem_api.hpp"
#include "cxx_include/esp_modem_dce_factory.hpp" #include "cxx_include/esp_modem_dce_factory.hpp"
namespace esp_modem::DCE { namespace esp_modem::dce_factory {
std::unique_ptr<PdpContext> FactoryHelper::create_pdp_context(std::string &apn) { std::unique_ptr<PdpContext> FactoryHelper::create_pdp_context(std::string &apn) {
return std::unique_ptr<PdpContext>(); return std::unique_ptr<PdpContext>();
} }

View File

@ -5,7 +5,7 @@
#include "cxx_include/esp_modem_dce_module.hpp" #include "cxx_include/esp_modem_dce_module.hpp"
#include "generate/esp_modem_command_declare.inc" #include "generate/esp_modem_command_declare.inc"
GenericModule::GenericModule(std::shared_ptr<DTE> dte, esp_modem_dce_config *config): GenericModule::GenericModule(std::shared_ptr<DTE> dte, const esp_modem_dce_config *config):
dte(std::move(dte)), pdp(std::make_unique<PdpContext>(config->apn)) {} dte(std::move(dte)), pdp(std::make_unique<PdpContext>(config->apn)) {}
@ -23,11 +23,11 @@ DECLARE_ALL_COMMAND_APIS(return_type name(...) { forwards to esp_modem::dce_comm
//command_result SIM7600::get_module_name(std::string& name) command_result SIM7600::get_module_name(std::string& name)
//{ {
// name = "7600"; name = "7600";
// return command_result::OK; return command_result::OK;
//} }
command_result SIM800::get_module_name(std::string& name) command_result SIM800::get_module_name(std::string& name)
{ {