mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-18 04:52:18 +02:00
functional factory work
This commit is contained in:
@ -15,6 +15,8 @@ class NetModule: public ModuleIf {
|
||||
public:
|
||||
explicit NetModule(std::shared_ptr<DTE> dte, std::unique_ptr<PdpContext> pdp):
|
||||
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
|
||||
{
|
||||
@ -60,14 +62,17 @@ public:
|
||||
|
||||
// create
|
||||
auto uart_dte = create_uart_dte(&dte_config);
|
||||
esp_modem::DCE::Factory f(esp_modem::DCE::Modem::MinModule);
|
||||
NetModule* module;
|
||||
if (!f.build_module_T<NetModule>(module, uart_dte, netif)) {
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_modem::DCE::Factory f2(esp_modem::DCE::Modem::SIM7600);
|
||||
// NetModule* module;
|
||||
// if (!f.build_module_T<NetModule>(module, uart_dte, netif)) {
|
||||
// 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(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);
|
||||
// dce = factory.create(apn_name);
|
||||
|
@ -5,8 +5,11 @@
|
||||
#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 {
|
||||
|
||||
using config = esp_modem_dce_config;
|
||||
|
||||
class FactoryHelper {
|
||||
public:
|
||||
@ -33,23 +36,6 @@ namespace esp_modem::DCE {
|
||||
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>
|
||||
@ -74,24 +60,46 @@ namespace esp_modem::DCE {
|
||||
|
||||
~Builder()
|
||||
{
|
||||
throw_if_false(netif == nullptr, "Netif created but never used");
|
||||
throw_if_false(dte == nullptr, "dte was captured but never used");
|
||||
// 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)
|
||||
// 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>
|
||||
bool create_module(U &t, std::string &apn)
|
||||
template<typename Ptr>
|
||||
bool create(Ptr &t, esp_modem_dce_config *config)
|
||||
{
|
||||
auto pdp = FactoryHelper::create_pdp_context(apn);
|
||||
// t = new T(std::move(dte), std::move(pdp));
|
||||
return FactoryHelper::make<T>(t, std::move(dte), std::move(pdp));
|
||||
return true;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
// bool create_module(std::shared_ptr<T> &t, std::string &apn)
|
||||
// {
|
||||
// auto pdp = FactoryHelper::create_pdp_context(apn);
|
||||
@ -99,52 +107,52 @@ namespace esp_modem::DCE {
|
||||
// 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));
|
||||
}
|
||||
// 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));
|
||||
// }
|
||||
|
||||
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);
|
||||
}
|
||||
// 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;
|
||||
@ -162,93 +170,72 @@ namespace esp_modem::DCE {
|
||||
public:
|
||||
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>
|
||||
// std::shared_ptr<T> build_module_T(Args&&... args)
|
||||
// bool build_module_T(std::shared_ptr<T> &t, Args&&... args)
|
||||
bool build_module_T(U &t, Args&&... args)
|
||||
static bool build_module_T(U &t, config *cfg, Args&&... args)
|
||||
{
|
||||
Builder<T> b(std::forward<Args>(args)...);
|
||||
std::string apn = "internet";
|
||||
return b.create_module(t, apn);
|
||||
// return b.create_shared_dev(apn);
|
||||
return b.create_module(t, cfg);
|
||||
}
|
||||
// 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>
|
||||
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;
|
||||
if (build_module_T<U>(module, std::forward<Args>(args)...))
|
||||
if (build_module_T<U>(module, cfg, std::forward<Args>(args)...))
|
||||
return module;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename T, typename ...Args>
|
||||
T build_module_xxT(Args&&... args)
|
||||
template <typename T, typename U, typename ...Args>
|
||||
static bool build_T(U &t, config *cfg, Args&&... args)
|
||||
{
|
||||
T generic_module = nullptr;
|
||||
switch (m) {
|
||||
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;
|
||||
if (build_module_T<SIM800>(module, std::forward<Args>(args)...))
|
||||
generic_module = module;
|
||||
break;
|
||||
}
|
||||
case Modem::BG96: {
|
||||
BG96 *module;
|
||||
if (build_module_T<BG96>(module, std::forward<Args>(args)...))
|
||||
generic_module = module;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return generic_module;
|
||||
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>
|
||||
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>
|
||||
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)...);
|
||||
// std::string apn = "internet";
|
||||
// return b.template create_device<GenericModule, std::shared_ptr<GenericModule>>(apn);
|
||||
|
||||
return build_shared_module_specific<SIM7600>(std::forward<Args>(args)...);
|
||||
switch (m) {
|
||||
case Modem::SIM800:
|
||||
return build_shared_module<SIM800>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::SIM7600:
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "generate/esp_modem_command_declare.inc"
|
||||
#include "cxx_include/esp_modem_command_library.hpp"
|
||||
#include "cxx_include/esp_modem_types.hpp"
|
||||
#include "esp_modem_dce_config.h"
|
||||
|
||||
enum class command_result;
|
||||
class DTE;
|
||||
@ -12,6 +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);
|
||||
|
||||
bool setup_data_mode() override
|
||||
{
|
||||
@ -64,6 +66,7 @@ public:
|
||||
};
|
||||
|
||||
class BG96: public GenericModule {
|
||||
using GenericModule::GenericModule;
|
||||
public:
|
||||
command_result get_module_name(std::string& name) override;
|
||||
};
|
||||
|
@ -23,12 +23,12 @@ typedef std::function<command_result(uint8_t *data, size_t len)> got_line_cb;
|
||||
|
||||
struct PdpContext {
|
||||
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;
|
||||
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;
|
||||
|
@ -5,6 +5,7 @@
|
||||
#ifndef SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H
|
||||
#define SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H
|
||||
#include "driver/uart.h"
|
||||
#include "esp_modem_dce_config.h"
|
||||
|
||||
/**
|
||||
* @brief Modem flow control type
|
||||
@ -61,17 +62,6 @@ struct esp_modem_dte_config {
|
||||
.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_dce_config esp_modem_dce_config_t;
|
||||
|
||||
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_CONFIG_H
|
||||
|
21
esp_modem/include/esp_modem_dce_config.h
Normal file
21
esp_modem/include/esp_modem_dce_config.h
Normal 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
|
@ -19,3 +19,14 @@ std::shared_ptr<SIM7600> create_SIM7600_module(const std::shared_ptr<DTE>& dte,
|
||||
{
|
||||
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>();
|
||||
}
|
||||
|
||||
}
|
@ -5,6 +5,10 @@
|
||||
#include "cxx_include/esp_modem_dce_module.hpp"
|
||||
#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 ARGS1 , 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
|
||||
|
||||
|
||||
|
||||
//command_result SIM7600::get_module_name(std::string& name)
|
||||
//{
|
||||
// name = "7600";
|
||||
|
Reference in New Issue
Block a user