mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-22 14:57:30 +02:00
experimental factory/builder
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
idf_component_register(SRCS "ap2pppos_example_main.c"
|
||||
"modem_board.c"
|
||||
INCLUDE_DIRS ".")
|
||||
"NetworkDCE.cpp"
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
#target_compile_features(${COMPONENT_LIB} PRIVATE cxx_std_17)
|
116
esp_modem/examples/ap_to_pppos/main/NetworkDCE.cpp
Normal file
116
esp_modem/examples/ap_to_pppos/main/NetworkDCE.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
//
|
||||
// Created by david on 3/25/21.
|
||||
//
|
||||
#include "cxx_include/esp_modem_dte.hpp"
|
||||
#include "esp_modem_config.h"
|
||||
#include "cxx_include/esp_modem_api.hpp"
|
||||
#include "cxx_include/esp_modem_dce_factory.hpp"
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
class NetModule;
|
||||
typedef DCE_T<MinimalModule> NetDCE;
|
||||
|
||||
class NetModule: public ModuleIf {
|
||||
public:
|
||||
explicit NetModule(std::shared_ptr<DTE> dte):
|
||||
dte(std::move(dte)) {}
|
||||
|
||||
bool setup_data_mode() override
|
||||
{
|
||||
set_command_mode(); // send in case we were in PPP mode, ignore potential failure
|
||||
bool is_pin_ok;
|
||||
if (read_pin(is_pin_ok) != command_result::OK)
|
||||
return false;
|
||||
if (!is_pin_ok) {
|
||||
if (set_pin(pin) != command_result::OK)
|
||||
return ESP_FAIL;
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
if (read_pin(is_pin_ok) != command_result::OK || !is_pin_ok)
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
PdpContext pdp(apn);
|
||||
if (set_pdp_context(pdp) != command_result::OK)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
if (mode == modem_mode::COMMAND_MODE) {
|
||||
return set_command_mode() == command_result::OK;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static esp_err_t init(esp_netif_t *netif, std::string apn_name, std::string pin_number)
|
||||
{
|
||||
apn = std::move(apn_name);
|
||||
pin = std::move(pin_number);
|
||||
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
|
||||
dte_config.event_task_stack_size = 4096;
|
||||
dte_config.rx_buffer_size = 16384;
|
||||
dte_config.tx_buffer_size = 2048;
|
||||
|
||||
// create
|
||||
auto uart_dte = create_uart_dte(&dte_config);
|
||||
esp_modem::DCE::Factory f(esp_modem::DCE::Modem::MinModule);
|
||||
MinimalModule* module;
|
||||
if (!f.build_module_T<MinimalModule>(module, uart_dte, netif)) {
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_modem::DCE::Factory f2(esp_modem::DCE::Modem::SIM7600);
|
||||
// std::shared_ptr<MinimalModule> dev;
|
||||
auto dev = f2.build_shared_module(uart_dte, netif);
|
||||
|
||||
// esp_modem::DCE::Builder<MinimalModule> factory(uart_dte, netif);
|
||||
// dce = factory.create(apn_name);
|
||||
// auto pdp = std::make_unique<PdpContext>(apn);
|
||||
// auto dev = std::make_shared<MinimalModule>(uart_dte, std::move(pdp));
|
||||
// auto dev = std::make_shared<MinimalModule>(uart_dte, nullptr);
|
||||
// dce = new DCE_T<MinimalModule>(uart_dte, module, netif);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void deinit() { delete dce; }
|
||||
static void start() { dce->set_data(); }
|
||||
static void stop() { dce->exit_data(); }
|
||||
|
||||
private:
|
||||
static NetDCE *dce;
|
||||
std::shared_ptr<DTE> dte;
|
||||
static std::string apn;
|
||||
static std::string pin;
|
||||
|
||||
template <typename ...T> command_result set_pdp_context(T&&... args) { return esp_modem::dce_commands::set_pdp_context(dte.get(),std::forward<T>(args)...); }
|
||||
template <typename ...T> command_result set_pin(T&&... args) { return esp_modem::dce_commands::set_pin(dte.get(),std::forward<T>(args)...); }
|
||||
template <typename ...T> command_result read_pin(T&&... args) { return esp_modem::dce_commands::read_pin(dte.get(),std::forward<T>(args)...); }
|
||||
command_result set_data_mode() { return esp_modem::dce_commands::set_data_mode(dte.get()); }
|
||||
command_result resume_data_mode() { return esp_modem::dce_commands::resume_data_mode(dte.get()); }
|
||||
command_result set_command_mode() { return esp_modem::dce_commands::set_command_mode(dte.get()); }
|
||||
};
|
||||
|
||||
NetDCE *NetModule::dce = nullptr;
|
||||
std::string NetModule::apn;
|
||||
std::string NetModule::pin;
|
||||
|
||||
extern "C" esp_err_t modem_init_network(esp_netif_t *netif)
|
||||
{
|
||||
return NetModule::init(netif, "internet", "1234");
|
||||
}
|
||||
|
||||
extern "C" void modem_start_network()
|
||||
{
|
||||
NetModule::start();
|
||||
}
|
||||
|
||||
extern "C" void modem_stop_network()
|
||||
{
|
||||
NetModule::stop();
|
||||
}
|
@ -12,7 +12,6 @@
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_modem.h"
|
||||
#include "lwip/lwip_napt.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
@ -35,6 +34,7 @@ static void on_modem_event(void *arg, esp_event_base_t event_base,
|
||||
if (event_id == IP_EVENT_PPP_GOT_IP) {
|
||||
esp_netif_dns_info_t dns_info;
|
||||
|
||||
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
esp_netif_t *netif = event->esp_netif;
|
||||
|
||||
@ -59,9 +59,6 @@ static void on_modem_event(void *arg, esp_event_base_t event_base,
|
||||
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
|
||||
ESP_LOGI(TAG, "Got IPv6 address " IPV6STR, IPV62STR(event->ip6_info.ip));
|
||||
}
|
||||
} else if (event_base == ESP_MODEM_EVENT) {
|
||||
ESP_LOGD(TAG, "Modem event! %d", event_id);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,7 +122,9 @@ void wifi_init_softap(void)
|
||||
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
|
||||
}
|
||||
|
||||
esp_modem_dce_t *sim7600_board_create(esp_modem_dce_config_t *config);
|
||||
esp_err_t modem_init_network(esp_netif_t *netif);
|
||||
void modem_start_network();
|
||||
void modem_stop_network();
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
@ -142,29 +141,32 @@ void app_main(void)
|
||||
event_group = xEventGroupCreate();
|
||||
|
||||
// init the DTE
|
||||
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
|
||||
dte_config.event_task_stack_size = 4096;
|
||||
dte_config.rx_buffer_size = 16384;
|
||||
dte_config.tx_buffer_size = 2048;
|
||||
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG("internet23");
|
||||
dce_config.populate_command_list = true;
|
||||
esp_netif_config_t ppp_netif_config = ESP_NETIF_DEFAULT_PPP();
|
||||
esp_netif_t *ppp_netif = esp_netif_new(&ppp_netif_config);
|
||||
assert(ppp_netif);
|
||||
|
||||
ESP_ERROR_CHECK(modem_init_network(ppp_netif));
|
||||
// vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
// esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
|
||||
// dte_config.event_task_stack_size = 4096;
|
||||
// dte_config.rx_buffer_size = 16384;
|
||||
// dte_config.tx_buffer_size = 2048;
|
||||
// esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG("internet23");
|
||||
// dce_config.populate_command_list = true;
|
||||
|
||||
|
||||
// Initialize esp-modem units, DTE, DCE, ppp-netif
|
||||
esp_modem_dte_t *dte = esp_modem_dte_new(&dte_config);
|
||||
esp_modem_dce_t *dce = sim7600_board_create(&dce_config);
|
||||
esp_netif_t *ppp_netif = esp_netif_new(&ppp_netif_config);
|
||||
|
||||
assert(ppp_netif);
|
||||
|
||||
ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, on_modem_event, ESP_EVENT_ANY_ID, dte));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, on_modem_event, dte));
|
||||
|
||||
ESP_ERROR_CHECK(esp_modem_default_attach(dte, dce, ppp_netif));
|
||||
|
||||
ESP_ERROR_CHECK(esp_modem_default_start(dte)); // use retry
|
||||
ESP_ERROR_CHECK(esp_modem_start_ppp(dte));
|
||||
// esp_modem_dte_t *dte = esp_modem_dte_new(&dte_config);
|
||||
// esp_modem_dce_t *dce = sim7600_board_create(&dce_config);
|
||||
//
|
||||
// ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, on_modem_event, ESP_EVENT_ANY_ID, dte));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, on_modem_event, NULL));
|
||||
//
|
||||
// ESP_ERROR_CHECK(esp_modem_default_attach(dte, dce, ppp_netif));
|
||||
//
|
||||
// ESP_ERROR_CHECK(esp_modem_default_start(dte)); // use retry
|
||||
// ESP_ERROR_CHECK(esp_modem_start_ppp(dte));
|
||||
modem_start_network();
|
||||
/* Wait for the first connection */
|
||||
EventBits_t bits;
|
||||
do {
|
||||
@ -186,8 +188,8 @@ void app_main(void)
|
||||
/* Provide recovery if disconnection of some kind registered */
|
||||
while (DISCONNECT_BIT&xEventGroupWaitBits(event_group, DISCONNECT_BIT, pdTRUE, pdFALSE, portMAX_DELAY)) {
|
||||
// restart the modem PPP mode
|
||||
ESP_ERROR_CHECK(esp_modem_stop_ppp(dte));
|
||||
ESP_ERROR_CHECK(esp_modem_start_ppp(dte));
|
||||
modem_stop_network();
|
||||
modem_start_network();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user