mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-22 14:57:30 +02:00
DCE factory implementation
This commit is contained in:
@ -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(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);
|
||||
|
@ -3,9 +3,9 @@
|
||||
|
||||
#include "cxx_include/esp_modem_netif.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 {
|
||||
public:
|
||||
@ -36,6 +36,8 @@ public:
|
||||
|
||||
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)
|
||||
{
|
||||
@ -49,11 +51,13 @@ protected:
|
||||
std::shared_ptr<DTE> dte;
|
||||
std::shared_ptr<SpecificModule> module;
|
||||
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> {
|
||||
public:
|
||||
|
||||
@ -69,4 +73,5 @@ public:
|
||||
|
||||
#undef ESP_MODEM_DECLARE_DCE_COMMAND
|
||||
|
||||
};
|
||||
};
|
||||
//#endif
|
@ -5,9 +5,7 @@
|
||||
#ifndef 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 {
|
||||
namespace esp_modem::dce_factory {
|
||||
|
||||
using config = esp_modem_dce_config;
|
||||
|
||||
@ -32,7 +30,7 @@ using config = esp_modem_dce_config;
|
||||
template <typename T, typename ...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;
|
||||
}
|
||||
|
||||
@ -48,7 +46,7 @@ using config = esp_modem_dce_config;
|
||||
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");
|
||||
}
|
||||
@ -60,100 +58,29 @@ using config = esp_modem_dce_config;
|
||||
|
||||
~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");
|
||||
}
|
||||
|
||||
// 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>
|
||||
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);
|
||||
}
|
||||
|
||||
template<typename Ptr>
|
||||
bool create(Ptr &t, esp_modem_dce_config *config)
|
||||
template<typename DceT, typename Ptr>
|
||||
bool create(Ptr &t, const esp_modem_dce_config *config)
|
||||
{
|
||||
if (dte == nullptr)
|
||||
return false;
|
||||
if (module == nullptr) {
|
||||
if (!create_module(module, config)) {
|
||||
ESP_LOGE("builder", "Failed to build module");
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
ESP_LOGI("builder", "create_module passed");
|
||||
return FactoryHelper::make<DCE_T<T>>(t, std::move(dte), std::move(module), netif);
|
||||
return FactoryHelper::make<DceT>(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:
|
||||
std::shared_ptr<DTE> dte;
|
||||
std::shared_ptr<T> module;
|
||||
@ -166,64 +93,33 @@ using config = esp_modem_dce_config;
|
||||
BG96,
|
||||
MinModule
|
||||
};
|
||||
|
||||
class Factory {
|
||||
public:
|
||||
explicit Factory(Modem modem): m(modem) {}
|
||||
|
||||
template <typename T, typename U, typename ...Args>
|
||||
static bool build_module_T(U &t, config *cfg, Args&&... args)
|
||||
template <typename T, typename ...Args>
|
||||
static std::unique_ptr<DCE> build_unique(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(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;
|
||||
return build_generic_DCE<DCE, std::unique_ptr<DCE>, T>(cfg, std::forward<Args>(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)...);
|
||||
}
|
||||
|
||||
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)...);
|
||||
return build_generic_DCE<DCE, DCE*, T>(cfg, std::forward<Args>(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)...);
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
case Modem::SIM800:
|
||||
@ -238,9 +134,59 @@ using config = esp_modem_dce_config;
|
||||
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:
|
||||
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
|
||||
|
@ -13,7 +13,7 @@ class GenericModule: public ModuleIf {
|
||||
public:
|
||||
explicit GenericModule(std::shared_ptr<DTE> dte, std::unique_ptr<PdpContext> 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
|
||||
{
|
||||
@ -56,7 +56,7 @@ protected:
|
||||
class SIM7600: public GenericModule {
|
||||
using GenericModule::GenericModule;
|
||||
public:
|
||||
// command_result get_module_name(std::string& name) override;
|
||||
command_result get_module_name(std::string& name) override;
|
||||
};
|
||||
|
||||
class SIM800: public GenericModule {
|
||||
|
@ -11,7 +11,6 @@
|
||||
class DTE;
|
||||
class Netif;
|
||||
|
||||
//struct ppp_netif_driver;
|
||||
struct ppp_netif_driver {
|
||||
esp_netif_driver_base_t base;
|
||||
Netif *ppp;
|
||||
|
@ -19,7 +19,7 @@ typedef enum esp_modem_dce_mode
|
||||
ESP_MODEM_MODE_DATA,
|
||||
} 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);
|
||||
esp_err_t esp_modem_set_mode(esp_modem_dce_t * dce, esp_modem_dce_mode_t mode);
|
||||
|
Reference in New Issue
Block a user