functional factory work

This commit is contained in:
David Cermak
2021-03-28 13:38:20 +02:00
parent 1ef6141936
commit e98e6c42f6
8 changed files with 179 additions and 157 deletions

View File

@ -15,6 +15,8 @@ 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, std::unique_ptr<PdpContext> pdp):
dte(std::move(dte)) {} dte(std::move(dte)) {}
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
{ {
@ -60,14 +62,17 @@ public:
// create // create
auto uart_dte = create_uart_dte(&dte_config); auto uart_dte = create_uart_dte(&dte_config);
esp_modem::DCE::Factory f(esp_modem::DCE::Modem::MinModule); // NetModule* module;
NetModule* module; // if (!f.build_module_T<NetModule>(module, uart_dte, netif)) {
if (!f.build_module_T<NetModule>(module, uart_dte, netif)) { // return ESP_OK;
return ESP_OK; // }
} // esp_modem::DCE::Factory f2(esp_modem::DCE::Modem::SIM7600);
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; // std::shared_ptr<MinimalModule> dev;
auto dev = f2.build_shared_module(uart_dte, netif); // 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); // esp_modem::DCE::Builder<MinimalModule> factory(uart_dte, netif);
// dce = factory.create(apn_name); // dce = factory.create(apn_name);

View File

@ -5,8 +5,11 @@
#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 { namespace esp_modem::DCE {
using config = esp_modem_dce_config;
class FactoryHelper { class FactoryHelper {
public: public:
@ -33,23 +36,6 @@ namespace esp_modem::DCE {
return true; return true;
} }
// template <typename T, typename ...Args>
// static T* make(Args&&... args)
// {
// return new T(std::forward<Args>(args)...);
// }
// template <typename T, typename ...Args>
// static std::shared_ptr<T> make(Args&&... args)
// {
// return std::make_shared<T>(std::forward<Args>(args)...);
// }
// template <typename T, typename ...Args>
// static std::unique_ptr<T> make(Args&&... args)
// {
// return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
// }
}; };
template<typename T> template<typename T>
@ -74,23 +60,45 @@ namespace esp_modem::DCE {
~Builder() ~Builder()
{ {
throw_if_false(netif == nullptr, "Netif created but never used"); // throw_if_false(netif == nullptr, "Netif created but never used");
throw_if_false(dte == nullptr, "dte was captured 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) // 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)
{ {
return create_dce(dte, netif, apn); return FactoryHelper::make<T>(t, dte, config);
} }
template<typename U> template<typename Ptr>
bool create_module(U &t, std::string &apn) bool create(Ptr &t, esp_modem_dce_config *config)
{ {
auto pdp = FactoryHelper::create_pdp_context(apn); if (dte == nullptr)
// t = new T(std::move(dte), std::move(pdp)); return false;
return FactoryHelper::make<T>(t, std::move(dte), std::move(pdp)); if (module == nullptr) {
return true; 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);
}
// bool create_module(std::shared_ptr<T> &t, std::string &apn) // bool create_module(std::shared_ptr<T> &t, std::string &apn)
// { // {
@ -99,52 +107,52 @@ namespace esp_modem::DCE {
// return true; // return true;
// } // }
template<typename U, typename V> // template<typename U, typename V>
U create_device(std::string &apn) // U create_device(std::string &apn)
{ // {
auto pdp = FactoryHelper::create_pdp_context(apn); // auto pdp = FactoryHelper::create_pdp_context(apn);
return FactoryHelper::make<V>(std::move(dte), std::move(pdp)); // 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));
// }
T* create_dev(std::string &apn) // static std::shared_ptr<T> create_module(const std::shared_ptr<DTE>& dte, std::string &apn)
{ // {
auto pdp = FactoryHelper::create_pdp_context(apn); // auto pdp = FactoryHelper::create_pdp_context(apn);
return new T(std::move(dte), std::move(pdp)); // return std::make_shared<T>(dte, std::move(pdp));
} // }
//
std::shared_ptr<T> create_shared_dev(std::string &apn) // static std::unique_ptr<DCE_T<T>> create_unique_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn)
{ // {
auto pdp = FactoryHelper::create_pdp_context(apn); // auto module = create_module(dte, apn);
return std::make_shared<T>(std::move(dte), std::move(pdp)); // return create_unique_dce_from_module(dte, std::move(module), netif);
} // }
//
static DCE_T<T>* create_dce_from_module(const std::shared_ptr<DTE>& dte, const std::shared_ptr<T>& dev, esp_netif_t *netif) // static DCE_T<T>* create_dce(const std::shared_ptr<DTE>& dte, esp_netif_t *netif, std::string &apn)
{ // {
return new DCE_T<T>(dte, dev, netif); // auto module = create_module(dte, apn);
} // return create_dce_from_module(dte, std::move(module), 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;
@ -162,93 +170,72 @@ namespace esp_modem::DCE {
public: public:
explicit Factory(Modem modem): m(modem) {} explicit Factory(Modem modem): m(modem) {}
template <typename T, typename ...Args>
T* create(Builder<T> b, std::string apn)
{
return b.create_dev(apn);
}
template <typename T, typename ...Args>
std::shared_ptr<T> create(Builder<T> b, std::string apn)
{
return b.create_shared_dev(apn);
}
template <typename T, typename U, typename ...Args> template <typename T, typename U, typename ...Args>
// std::shared_ptr<T> build_module_T(Args&&... args) static bool build_module_T(U &t, config *cfg, Args&&... args)
// bool build_module_T(std::shared_ptr<T> &t, Args&&... args)
bool build_module_T(U &t, Args&&... args)
{ {
Builder<T> b(std::forward<Args>(args)...); Builder<T> b(std::forward<Args>(args)...);
std::string apn = "internet"; return b.create_module(t, cfg);
return b.create_module(t, apn);
// return b.create_shared_dev(apn);
} }
// template <typename T, typename ...Args>
// T* build_module_T(Args&&... args)
// {
// Builder<T> b(std::forward<Args>(args)...);
// std::string apn = "internet";
// return b.create_dev(apn);
// }
template <typename T, typename U, typename ...Args> template <typename T, typename U, typename ...Args>
T build_module_T(Args&&... args) static T build_module_T(config *cfg, Args&&... args)
{ {
// Builder<U> b(std::forward<Args>(args)...);
// std::string apn = "internet";
// return b.template create_device<T>(apn);
T module; T module;
if (build_module_T<U>(module, std::forward<Args>(args)...)) if (build_module_T<U>(module, cfg, std::forward<Args>(args)...))
return module; return module;
return nullptr; return nullptr;
} }
template <typename T, typename ...Args> template <typename T, typename U, typename ...Args>
T build_module_xxT(Args&&... args) static bool build_T(U &t, config *cfg, Args&&... args)
{ {
T generic_module = nullptr; Builder<T> b(std::forward<Args>(args)...);
switch (m) { return b.create(t, cfg);
case Modem::MinModule:
break;
case Modem::SIM7600: {
SIM7600 *module;
if (build_module_T<SIM7600>(module, std::forward<Args>(args)...))
generic_module = module;
break;
} }
case Modem::SIM800: {
SIM800 *module; template <typename T, typename U, typename ...Args>
if (build_module_T<SIM800>(module, std::forward<Args>(args)...)) static T build_T(config *cfg, Args&&... args)
generic_module = module; {
break; T dce;
} if (build_T<U>(dce, cfg, std::forward<Args>(args)...))
case Modem::BG96: { return dce;
BG96 *module; return nullptr;
if (build_module_T<BG96>(module, std::forward<Args>(args)...))
generic_module = module;
break;
}
}
return generic_module;
} }
template <typename T, typename ...Args> template <typename T, typename ...Args>
std::shared_ptr<T> build_shared_module_specific(Args&&... args) static std::unique_ptr<DCE_T<T>> build_unique(config *cfg, Args&&... args)
{ {
return build_module_T<std::shared_ptr<T>, T>(std::forward<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)...);
}
template <typename T, typename ...Args>
static std::shared_ptr<T> build_shared_module(config *cfg, 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(Args&&... args) std::shared_ptr<GenericModule> build_shared_module(config *cfg, Args&&... args)
{ {
// Builder<GenericModule> b(std::forward<Args>(args)...); switch (m) {
// std::string apn = "internet"; case Modem::SIM800:
// return b.template create_device<GenericModule, std::shared_ptr<GenericModule>>(apn); return build_shared_module<SIM800>(cfg, std::forward<Args>(args)...);
case Modem::SIM7600:
return build_shared_module_specific<SIM7600>(std::forward<Args>(args)...); return build_shared_module<SIM7600>(cfg, std::forward<Args>(args)...);
case Modem::BG96:
return build_shared_module<BG96>(cfg, std::forward<Args>(args)...);
case Modem::MinModule:
break;
}
return nullptr;
} }

View File

@ -4,6 +4,7 @@
#include "generate/esp_modem_command_declare.inc" #include "generate/esp_modem_command_declare.inc"
#include "cxx_include/esp_modem_command_library.hpp" #include "cxx_include/esp_modem_command_library.hpp"
#include "cxx_include/esp_modem_types.hpp" #include "cxx_include/esp_modem_types.hpp"
#include "esp_modem_dce_config.h"
enum class command_result; enum class command_result;
class DTE; class DTE;
@ -12,6 +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);
bool setup_data_mode() override bool setup_data_mode() override
{ {
@ -64,6 +66,7 @@ public:
}; };
class BG96: public GenericModule { class BG96: public GenericModule {
using GenericModule::GenericModule;
public: public:
command_result get_module_name(std::string& name) override; command_result get_module_name(std::string& name) override;
}; };

View File

@ -23,12 +23,12 @@ typedef std::function<command_result(uint8_t *data, size_t len)> got_line_cb;
struct PdpContext { struct PdpContext {
explicit PdpContext(std::string& apn): context_id(1), protocol_type("IP"), apn(apn) {} explicit PdpContext(std::string& apn): context_id(1), protocol_type("IP"), apn(apn) {}
explicit PdpContext(const char *apn): context_id(1), protocol_type("IP"), apn(apn) {}
size_t context_id; size_t context_id;
std::string protocol_type; std::string protocol_type;
std::string apn; std::string apn;
}; };
class CommandableIf { class CommandableIf {
public: public:
virtual command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) = 0; virtual command_result command(const std::string& command, got_line_cb got_line, uint32_t time_ms) = 0;

View File

@ -5,6 +5,7 @@
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H #ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H
#define SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H #define SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H
#include "driver/uart.h" #include "driver/uart.h"
#include "esp_modem_dce_config.h"
/** /**
* @brief Modem flow control type * @brief Modem flow control type
@ -61,17 +62,6 @@ struct esp_modem_dte_config {
.line_buffer_size = 512 \ .line_buffer_size = 512 \
} }
struct esp_modem_dce_config {
const char* apn;
};
#define ESP_MODEM_DCE_DEFAULT_CONFIG(APN) \
{ \
.apn = APN \
}
typedef struct esp_modem_dte_config esp_modem_dte_config_t; typedef struct esp_modem_dte_config esp_modem_dte_config_t;
typedef struct esp_modem_dce_config esp_modem_dce_config_t;
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H #endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H

View File

@ -0,0 +1,21 @@
//
// Created by david on 3/28/21.
//
#ifndef AP_TO_PPPOS_ESP_MODEM_DCE_CONFIG_H
struct esp_modem_dce_config {
const char* apn;
};
#define ESP_MODEM_DCE_DEFAULT_CONFIG(APN) \
{ \
.apn = APN \
}
typedef struct esp_modem_dce_config esp_modem_dce_config_t;
#define AP_TO_PPPOS_ESP_MODEM_DCE_CONFIG_H
#endif //AP_TO_PPPOS_ESP_MODEM_DCE_CONFIG_H

View File

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

View File

@ -5,6 +5,10 @@
#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):
dte(std::move(dte)), pdp(std::make_unique<PdpContext>(config->apn)) {}
#define ARGS0 #define ARGS0
#define ARGS1 , x #define ARGS1 , x
#define _ARGS(x) ARGS ## x #define _ARGS(x) ARGS ## x
@ -18,6 +22,7 @@ DECLARE_ALL_COMMAND_APIS(return_type name(...) { forwards to esp_modem::dce_comm
#undef ESP_MODEM_DECLARE_DCE_COMMAND #undef ESP_MODEM_DECLARE_DCE_COMMAND
//command_result SIM7600::get_module_name(std::string& name) //command_result SIM7600::get_module_name(std::string& name)
//{ //{
// name = "7600"; // name = "7600";