mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-29 18:27:31 +02:00
esp-modem(DCE-Factory): Minor corrections per code review
This commit is contained in:
committed by
David Cermak
parent
3332c27978
commit
dc64f862c4
@ -85,7 +85,7 @@ protected:
|
||||
|
||||
/**
|
||||
* @brief Common abstraction of the modem DCE, specialized by the GenericModule which is a parent class for the supported
|
||||
* defices and most common modems, as well.
|
||||
* devices and most common modems, as well.
|
||||
*/
|
||||
class DCE : public DCE_T<GenericModule> {
|
||||
public:
|
||||
|
@ -13,6 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include "esp_log.h"
|
||||
|
||||
/**
|
||||
* @defgroup ESP_MODEM_DCE_FACTORY
|
||||
@ -30,62 +31,67 @@ using config = ::esp_modem_dce_config;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Helper class for creating a uder define pointer in a specific way, either as a plain pointer, shared_ptr or unique_ptr
|
||||
* @brief Helper class for creating a user define pointer in a specific way, either as a plain pointer, shared_ptr or unique_ptr
|
||||
*/
|
||||
class FactoryHelper {
|
||||
public:
|
||||
static std::unique_ptr<PdpContext> create_pdp_context(std::string &apn);
|
||||
|
||||
template <typename T, typename Ptr, typename ...Args>
|
||||
static auto make(Args&&... args) -> typename std::enable_if<std::is_same<Ptr, T*>::value, T*>::type
|
||||
template <typename T, typename T_Ptr, typename ...Args>
|
||||
static auto make(Args&&... args) -> typename std::enable_if<std::is_same<T_Ptr, T*>::value, T*>::type
|
||||
{
|
||||
return new T(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename T, typename Ptr, typename ...Args>
|
||||
static auto make(Args&&... args) -> typename std::enable_if<std::is_same<Ptr, std::shared_ptr<T>>::value, std::shared_ptr<T>>::type
|
||||
template <typename T, typename T_Ptr, typename ...Args>
|
||||
static auto make(Args&&... args) -> typename std::enable_if<std::is_same<T_Ptr, std::shared_ptr<T>>::value, std::shared_ptr<T>>::type
|
||||
{
|
||||
return std::make_shared<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename T, typename Ptr = std::unique_ptr<T>, typename ...Args>
|
||||
static auto make(Args&&... args) -> typename std::enable_if<std::is_same<Ptr, std::unique_ptr<T>>::value, std::unique_ptr<T>>::type
|
||||
template <typename T, typename T_Ptr = std::unique_ptr<T>, typename ...Args>
|
||||
static auto make(Args&&... args) -> typename std::enable_if<std::is_same<T_Ptr, std::unique_ptr<T>>::value, std::unique_ptr<T>>::type
|
||||
{
|
||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Builder class for building a DCE_T<Module> in a specific way, either form a Module object or by default from the DTE and netif
|
||||
* @brief Builder class for building a DCE_T<Module> in a specific way, either from a Module object or by default from the DTE and netif
|
||||
*
|
||||
* @throws
|
||||
* - esp_modem::esp_err_exception on invalid arguments
|
||||
* - std::bad_alloc if failed to allocate
|
||||
*/
|
||||
template<typename Module>
|
||||
template<typename T_Module>
|
||||
class Builder {
|
||||
static_assert(std::is_base_of<ModuleIf, Module>::value, "Builder must be used only for Module classes");
|
||||
static_assert(std::is_base_of<ModuleIf, T_Module>::value, "Builder must be used only for Module classes");
|
||||
public:
|
||||
Builder(std::shared_ptr<DTE> x, esp_netif_t* esp_netif): dte(std::move(x)), device(nullptr), netif(esp_netif)
|
||||
Builder(std::shared_ptr<DTE> dte, esp_netif_t* esp_netif): dte(std::move(dte)), device(nullptr), netif(esp_netif)
|
||||
{
|
||||
throw_if_false(netif != nullptr, "Null netif");
|
||||
}
|
||||
|
||||
Builder(std::shared_ptr<DTE> dte, esp_netif_t* esp_netif, std::shared_ptr<Module> dev): dte(std::move(dte)), device(std::move(dev)), netif(esp_netif)
|
||||
Builder(std::shared_ptr<DTE> dte, esp_netif_t* esp_netif, std::shared_ptr<T_Module> dev): dte(std::move(dte)), device(std::move(dev)), netif(esp_netif)
|
||||
{
|
||||
throw_if_false(netif != nullptr, "Null netif");
|
||||
}
|
||||
|
||||
~Builder()
|
||||
{
|
||||
throw_if_false(device == nullptr, "module was captured or created but never used");
|
||||
if (device == nullptr) {
|
||||
ESP_LOGE("dce_factory::~Builder", "module was captured or created but never used");
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Ptr>
|
||||
auto create_module(const esp_modem_dce_config *config) -> Ptr
|
||||
template<typename T_Ptr>
|
||||
auto create_module(const esp_modem_dce_config *config) -> T_Ptr
|
||||
{
|
||||
return FactoryHelper::make<Module, Ptr>(dte, config);
|
||||
return FactoryHelper::make<T_Module, T_Ptr>(dte, config);
|
||||
}
|
||||
|
||||
template<typename DceT, typename Ptr>
|
||||
auto create(const esp_modem_dce_config *config) -> Ptr
|
||||
template<typename T_Dce, typename T_Ptr>
|
||||
auto create(const esp_modem_dce_config *config) -> T_Ptr
|
||||
{
|
||||
if (dte == nullptr)
|
||||
return nullptr;
|
||||
@ -94,19 +100,19 @@ public:
|
||||
if (device == nullptr)
|
||||
return nullptr;
|
||||
}
|
||||
return FactoryHelper::make<DceT, Ptr>(std::move(dte), std::move(device), netif);
|
||||
return FactoryHelper::make<T_Dce, T_Ptr>(std::move(dte), std::move(device), netif);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<DTE> dte;
|
||||
std::shared_ptr<Module> device;
|
||||
std::shared_ptr<T_Module> device;
|
||||
esp_netif_t *netif;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Specific modem choice when creating by the Factory
|
||||
*/
|
||||
enum class Modem {
|
||||
enum class ModemType {
|
||||
GenericModule, /*!< Default generic module with the most common commands */
|
||||
SIM7600, /*!< Derived from the GenericModule, specifics applied to SIM7600 model */
|
||||
BG96, /*!< Derived from the GenericModule, specifics applied to BG69 model */
|
||||
@ -119,7 +125,7 @@ enum class Modem {
|
||||
*/
|
||||
class Factory {
|
||||
public:
|
||||
explicit Factory(Modem modem): m(modem) {}
|
||||
explicit Factory(ModemType modem): m(modem) {}
|
||||
|
||||
/**
|
||||
* @brief Create a default unique_ptr DCE in a specific way (from the module)
|
||||
@ -129,10 +135,10 @@ public:
|
||||
* @param args typically a DTE object and a netif handle for PPP network
|
||||
* @return unique_ptr DCE of the created DCE on success
|
||||
*/
|
||||
template <typename Module, typename ...Args>
|
||||
template <typename T_Module, typename ...Args>
|
||||
static std::unique_ptr<DCE> build_unique(const config *cfg, Args&&... args)
|
||||
{
|
||||
return build_generic_DCE<Module, DCE, std::unique_ptr<DCE>>(cfg, std::forward<Args>(args)...);
|
||||
return build_generic_DCE<T_Module, DCE, std::unique_ptr<DCE>>(cfg, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -143,17 +149,17 @@ public:
|
||||
* @param args typically a DTE object and a netif handle for PPP network
|
||||
* @return DCE pointer the created DCE on success
|
||||
*/
|
||||
template <typename Module, typename ...Args>
|
||||
template <typename T_Module, typename ...Args>
|
||||
static DCE* build(const config *cfg, Args&&... args)
|
||||
{
|
||||
return build_generic_DCE<Module, DCE>(cfg, std::forward<Args>(args)...);
|
||||
return build_generic_DCE<T_Module, DCE>(cfg, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
||||
template <typename Module, typename ...Args>
|
||||
static std::shared_ptr<Module> build_shared_module(const config *cfg, Args&&... args)
|
||||
template <typename T_Module, typename ...Args>
|
||||
static std::shared_ptr<T_Module> build_shared_module(const config *cfg, Args&&... args)
|
||||
{
|
||||
return build_module_T<Module>(cfg, std::forward<Args>(args)...);
|
||||
return build_module_T<T_Module>(cfg, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
||||
@ -161,13 +167,13 @@ public:
|
||||
std::shared_ptr<GenericModule> build_shared_module(const config *cfg, Args&&... args)
|
||||
{
|
||||
switch (m) {
|
||||
case Modem::SIM800:
|
||||
case ModemType::SIM800:
|
||||
return build_shared_module<SIM800>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::SIM7600:
|
||||
case ModemType::SIM7600:
|
||||
return build_shared_module<SIM7600>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::BG96:
|
||||
case ModemType::BG96:
|
||||
return build_shared_module<BG96>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::GenericModule:
|
||||
case ModemType::GenericModule:
|
||||
return build_shared_module<GenericModule>(cfg, std::forward<Args>(args)...);
|
||||
default:
|
||||
break;
|
||||
@ -186,13 +192,13 @@ public:
|
||||
std::unique_ptr<DCE> build_unique(const config *cfg, Args&&... args)
|
||||
{
|
||||
switch (m) {
|
||||
case Modem::SIM800:
|
||||
case ModemType::SIM800:
|
||||
return build_unique<SIM800>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::SIM7600:
|
||||
case ModemType::SIM7600:
|
||||
return build_unique<SIM7600>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::BG96:
|
||||
case ModemType::BG96:
|
||||
return build_unique<BG96>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::GenericModule:
|
||||
case ModemType::GenericModule:
|
||||
return build_unique<GenericModule>(cfg, std::forward<Args>(args)...);
|
||||
default:
|
||||
break;
|
||||
@ -204,13 +210,13 @@ public:
|
||||
DCE* build(const config *cfg, Args&&... args)
|
||||
{
|
||||
switch (m) {
|
||||
case Modem::SIM800:
|
||||
case ModemType::SIM800:
|
||||
return build<SIM800>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::SIM7600:
|
||||
case ModemType::SIM7600:
|
||||
return build<SIM7600>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::BG96:
|
||||
case ModemType::BG96:
|
||||
return build<BG96>(cfg, std::forward<Args>(args)...);
|
||||
case Modem::GenericModule:
|
||||
case ModemType::GenericModule:
|
||||
return build<GenericModule>(cfg, std::forward<Args>(args)...);
|
||||
default:
|
||||
break;
|
||||
@ -219,22 +225,22 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
Modem m;
|
||||
ModemType m;
|
||||
|
||||
protected:
|
||||
template <typename Module, typename Ptr = std::shared_ptr<Module>, typename ...Args>
|
||||
template <typename T_Module, typename Ptr = std::shared_ptr<T_Module>, typename ...Args>
|
||||
static Ptr build_module_T(const config *cfg, Args&&... args)
|
||||
{
|
||||
Builder<Module> b(std::forward<Args>(args)...);
|
||||
Builder<T_Module> b(std::forward<Args>(args)...);
|
||||
return b.template create_module<Ptr>(cfg);
|
||||
}
|
||||
|
||||
|
||||
template <typename Module, typename Dce = DCE_T<Module>, typename DcePtr = Dce*, typename ...Args>
|
||||
static DcePtr build_generic_DCE(const config *cfg, Args&&... args)
|
||||
template <typename T_Module, typename T_Dce = DCE_T<T_Module>, typename T_DcePtr = T_Dce*, typename ...Args>
|
||||
static auto build_generic_DCE(const config *cfg, Args&&... args) -> T_DcePtr
|
||||
{
|
||||
Builder<Module> b(std::forward<Args>(args)...);
|
||||
return b.template create<Dce, DcePtr>(cfg);
|
||||
Builder<T_Module> b(std::forward<Args>(args)...);
|
||||
return b.template create<T_Dce, T_DcePtr>(cfg);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -29,14 +29,14 @@
|
||||
#define BOOL_OUT(param, name) bool& _ARG(param, name)
|
||||
#define INT_OUT(param, name) int& _ARG(param, name)
|
||||
|
||||
#define STRUCT_OUT(struct_name, x) struct_name& x
|
||||
#define STRUCT_OUT(struct_name, p1) struct_name& p1
|
||||
#else
|
||||
#define STRING_IN(param, name) const char* _ARG(param, name)
|
||||
#define STRING_OUT(param, name) char* _ARG(param, name)
|
||||
#define BOOL_IN(param, name) const bool _ARG(param, name)
|
||||
#define BOOL_OUT(param, name) bool* _ARG(param, name)
|
||||
#define INT_OUT(param, name) int* _ARG(param, name)
|
||||
#define STRUCT_OUT(struct_name, x) struct struct_name* x
|
||||
#define STRUCT_OUT(struct_name, p1) struct struct_name* p1
|
||||
#endif
|
||||
|
||||
#define DECLARE_ALL_COMMAND_APIS(...) \
|
||||
@ -50,7 +50,7 @@ ESP_MODEM_DECLARE_DCE_COMMAND(sync, command_result, 0) \
|
||||
* @param[out] name module name
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_operator_name, command_result, 1, STRING_OUT(x, name)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_operator_name, command_result, 1, STRING_OUT(p1, name)) \
|
||||
\
|
||||
/**
|
||||
* @brief Stores current user profile
|
||||
@ -63,28 +63,28 @@ ESP_MODEM_DECLARE_DCE_COMMAND(store_profile, command_result, 0) \
|
||||
* @param[in] pin Pin
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/\
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_pin, command_result, 1, STRING_IN(x, pin)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_pin, command_result, 1, STRING_IN(p1, pin)) \
|
||||
\
|
||||
/**
|
||||
* @brief Checks if the SIM needs a PIN
|
||||
* @param[out] pin_ok true if the SIM card doesn't need a PIN to unlock
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(read_pin, command_result, 1, BOOL_OUT(x, pin_ok)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(read_pin, command_result, 1, BOOL_OUT(p1, pin_ok)) \
|
||||
\
|
||||
/**
|
||||
* @brief Sets echo mode
|
||||
* @param[in] echo_on true if echo mode on (repeats the commands)
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_echo, command_result, 1, BOOL_IN(x, echo_on)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_echo, command_result, 1, BOOL_IN(p1, echo_on)) \
|
||||
\
|
||||
/**
|
||||
* @brief Sets the Txt or Pdu mode for SMS (only txt is supported)
|
||||
* @param[in] txt true if txt mode
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(sms_txt_mode, command_result, 1, BOOL_IN(x, txt)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(sms_txt_mode, command_result, 1, BOOL_IN(p1, txt)) \
|
||||
\
|
||||
/**
|
||||
* @brief Sets the default (GSM) charater set
|
||||
@ -98,7 +98,7 @@ ESP_MODEM_DECLARE_DCE_COMMAND(sms_character_set, command_result, 0) \
|
||||
* @param[in] message Text message to be sent
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(send_sms, command_result, 2, STRING_IN(x, number), STRING_IN(y, message)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(send_sms, command_result, 2, STRING_IN(p1, number), STRING_IN(p2, message)) \
|
||||
\
|
||||
/**
|
||||
* @brief Resumes data mode (Switches back to th data mode, which was temporarily suspended)
|
||||
@ -108,10 +108,10 @@ ESP_MODEM_DECLARE_DCE_COMMAND(resume_data_mode, command_result, 0) \
|
||||
\
|
||||
/**
|
||||
* @brief Sets php context
|
||||
* @param[in] x PdP context struct to setup modem cellular connection
|
||||
* @param[in] p1 PdP context struct to setup modem cellular connection
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_pdp_context, command_result, 1, STRUCT_OUT(PdpContext, x)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_pdp_context, command_result, 1, STRUCT_OUT(PdpContext, p1)) \
|
||||
\
|
||||
/**
|
||||
* @brief Switches to the command mode
|
||||
@ -130,21 +130,21 @@ ESP_MODEM_DECLARE_DCE_COMMAND(set_cmux, command_result, 0) \
|
||||
* @param[out] imsi Module's IMSI number
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_imsi, command_result, 1, STRING_OUT(x, imsi)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_imsi, command_result, 1, STRING_OUT(p1, imsi)) \
|
||||
\
|
||||
/**
|
||||
* @brief Reads the IMEI number
|
||||
* @param[out] imei Module's IMEI number
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_imei, command_result, 1, STRING_OUT(x, imei)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_imei, command_result, 1, STRING_OUT(p1, imei)) \
|
||||
\
|
||||
/**
|
||||
* @brief Reads the module name
|
||||
* @param[out] name module name
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_module_name, command_result, 1, STRING_OUT(x, name)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_module_name, command_result, 1, STRING_OUT(p1, name)) \
|
||||
\
|
||||
/**
|
||||
* @brief Sets the modem to data mode
|
||||
@ -158,7 +158,7 @@ ESP_MODEM_DECLARE_DCE_COMMAND(set_data_mode, command_result, 0) \
|
||||
* @param[out] ber channel bit error rate
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_signal_quality, command_result, 2, INT_OUT(x, rssi), INT_OUT(y, ber)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_signal_quality, command_result, 2, INT_OUT(p1, rssi), INT_OUT(p2, ber)) \
|
||||
\
|
||||
/**
|
||||
* @brief Sets HW control flow
|
||||
@ -166,7 +166,7 @@ ESP_MODEM_DECLARE_DCE_COMMAND(get_signal_quality, command_result, 2, INT_OUT(x,
|
||||
* @param[in] dte_flow 0=none, 2=CTS hw flow control of DTE
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_flow_control, command_result, 2, INT_IN(x, dce_flow), INT_IN(y, dte_flow)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_flow_control, command_result, 2, INT_IN(p1, dce_flow), INT_IN(p2, dte_flow)) \
|
||||
\
|
||||
/**
|
||||
* @brief Hangs up current data call
|
||||
@ -181,7 +181,7 @@ ESP_MODEM_DECLARE_DCE_COMMAND(hang_up, command_result, 0) \
|
||||
* @param[out] bcl 1-100% battery capacity, -1-Not available
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_battery_status, command_result, 3, INT_OUT(x, voltage), INT_OUT(y, bcs), INT_OUT(z, bcl)) \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_battery_status, command_result, 3, INT_OUT(p1, voltage), INT_OUT(p2, bcs), INT_OUT(p3, bcl)) \
|
||||
\
|
||||
/**
|
||||
* @brief Power down the module
|
||||
@ -193,14 +193,14 @@ ESP_MODEM_DECLARE_DCE_COMMAND(power_down, command_result, 0) \
|
||||
* @brief Reset the module
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(reset, command_result, 0)\
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(reset, command_result, 0) \
|
||||
\
|
||||
/**
|
||||
* @brief Configures the baudrate
|
||||
* @param[in] baud Desired baud rate of the DTE
|
||||
* @return OK, FAIL or TIMEOUT
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_baud, command_result, 1, INT_IN(x, baud))
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_baud, command_result, 1, INT_IN(p1, baud))
|
||||
|
||||
|
||||
#ifdef GENERATE_DOCS
|
||||
|
Reference in New Issue
Block a user