esp-modem(Docs): Update documentation and minor fixes

This commit is contained in:
David Čermák
2021-05-26 16:41:19 +08:00
committed by David Cermak
parent 28433de4ad
commit e0e65856f0
24 changed files with 139 additions and 350 deletions

View File

@ -31,9 +31,16 @@ menu "Example Configuration"
help
Set APN (Access Point Name), a logical name to choose data network
config EXAMPLE_NEED_SIM_PIN
bool "SIM PIN needed"
default n
help
Enable to set SIM PIN before starting the example
config EXAMPLE_SIM_PIN
string "Set SIM PIN"
default "1234"
depends on EXAMPLE_NEED_SIM_PIN
help
Pin to unlock the SIM

View File

@ -93,7 +93,6 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,
void wifi_init_softap(void)
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

View File

@ -19,40 +19,28 @@ using namespace esp_modem;
using namespace esp_modem::dce_factory;
class NetModule;
typedef DCE_T<NetModule> NetDCE;
/**
* @brief Local network object used to setup PPP network
*/
class PPPNetwork {
public:
esp_err_t init(esp_netif_t *netif, const std::string& apn, const std::string &pin_number);
void deinit();
NetDCE * get_dce();
private:
NetDCE *dce;
};
using NetDCE = DCE_T<NetModule>;
/**
* @brief The PPP network is a singleton, allocate statically here
*/
static PPPNetwork ppp_network;
/**
* @brief Custom factory for creating NetDCE and NetModule
*/
class NetDCE_Factory: public Factory {
public:
template <typename T, typename ...Args>
static DCE_T<T>* create(const config *cfg, Args&&... args)
template <typename Module, typename ...Args>
static DCE_T<Module>* create(const config *cfg, Args&&... args)
{
return build_generic_DCE<T>(cfg, std::forward<Args>(args)...);
return build_generic_DCE<Module>(cfg, std::forward<Args>(args)...);
}
template <typename T, typename ...Args>
static std::shared_ptr<T> create_module(const config *cfg, Args&&... args)
template <typename Module, typename ...Args>
static std::shared_ptr<Module> create_module(const config *cfg, Args&&... args)
{
return build_shared_module<T>(cfg, std::forward<Args>(args)...);
return build_shared_module<Module>(cfg, std::forward<Args>(args)...);
}
};
@ -67,7 +55,7 @@ public:
explicit NetModule(std::shared_ptr<DTE> dte, const esp_modem_dce_config *cfg):
dte(std::move(dte)), apn(std::string(cfg->apn)) {}
bool setup_data_mode() override
[[nodiscard]] bool setup_data_mode() override
{
PdpContext pdp(apn);
if (set_pdp_context(pdp) != command_result::OK)
@ -77,18 +65,20 @@ public:
bool set_mode(modem_mode mode) override
{
if (mode == modem_mode::DATA_MODE) {
if (set_data_mode() != command_result::OK)
return resume_data_mode() == command_result::OK;
return true;
switch (mode) {
case esp_modem::modem_mode::DATA_MODE:
if (set_data_mode() != command_result::OK) {
return resume_data_mode() == command_result::OK;
}
return true;
case esp_modem::modem_mode::COMMAND_MODE:
return set_command_mode() == command_result::OK;
default:
return false;
}
if (mode == modem_mode::COMMAND_MODE) {
return set_command_mode() == command_result::OK;
}
return false;
}
bool init(const std::string& pin)
[[maybe_unused]] bool init_sim(const std::string& pin)
{
// switch to command mode (in case we were in PPP mode)
static_cast<void>(set_command_mode()); // ignore the potential failure, as we might be in command mode after startup
@ -118,22 +108,33 @@ private:
};
esp_err_t PPPNetwork::init(esp_netif_t *netif, const std::string& apn, const std::string &pin_number)
/**
* @brief Implement the C-API for the AP-2-PPP functionality
*/
namespace {
/**
* @brief Local network object used to setup PPP network
*/
NetDCE *dce = nullptr;
extern "C" esp_err_t modem_init_network(esp_netif_t *netif)
{
// configure
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
dte_config.uart_config.rx_buffer_size = 16384;
dte_config.uart_config.tx_buffer_size = 2048;
esp_modem_dce_config dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(apn.c_str());
esp_modem_dce_config dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_PPP_APN);
// create DTE and minimal network DCE
auto uart_dte = create_uart_dte(&dte_config);
// create the specific device (and initialize it)
auto dev = NetDCE_Factory::create_module<NetModule>(&dce_config, uart_dte, netif);
if (!dev->init(pin_number))
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
if (!dev->init_sim(CONFIG_EXAMPLE_SIM_PIN))
return ESP_FAIL;
#endif
// now create the DCE from our already existent device
dce = NetDCE_Factory::create<NetModule>(&dce_config, uart_dte, netif, dev);
if (dce == nullptr)
@ -141,36 +142,20 @@ esp_err_t PPPNetwork::init(esp_netif_t *netif, const std::string& apn, const std
return ESP_OK;
}
void PPPNetwork::deinit()
extern "C" void modem_start_network()
{
dce->set_mode(esp_modem::modem_mode::DATA_MODE);
}
extern "C" void modem_stop_network()
{
dce->set_mode(esp_modem::modem_mode::COMMAND_MODE);
}
extern "C" void modem_deinit_network()
{
free(dce);
dce = nullptr;
}
NetDCE *PPPNetwork::get_dce()
{
return dce;
}
/**
* @brief Implement the C-API for the AP-2-PPP functionality
*/
extern "C" esp_err_t modem_init_network(esp_netif_t *netif)
{
return ppp_network.init(netif, CONFIG_EXAMPLE_MODEM_PPP_APN, CONFIG_EXAMPLE_SIM_PIN);
}
extern "C" void modem_start_network()
{
ppp_network.get_dce()->set_mode(esp_modem::modem_mode::DATA_MODE);
}
extern "C" void modem_stop_network()
{
ppp_network.get_dce()->set_mode(esp_modem::modem_mode::COMMAND_MODE);
}
extern "C" void modem_deinit_network()
{
ppp_network.deinit();
}
}