mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-19 05:22:21 +02:00
esp-modem: c++ dce added
This commit is contained in:
@ -8,7 +8,8 @@ set(srcs "src/esp_modem.c"
|
|||||||
"src/esp_sim800.c"
|
"src/esp_sim800.c"
|
||||||
"src/esp_sim7600.c"
|
"src/esp_sim7600.c"
|
||||||
"src/esp_bg96.c"
|
"src/esp_bg96.c"
|
||||||
"src/esp_modem_dte.cpp")
|
"src/esp_modem_dte.cpp"
|
||||||
|
"src/ppp_netif.cpp")
|
||||||
|
|
||||||
set(include_dirs "include")
|
set(include_dirs "include")
|
||||||
|
|
||||||
|
@ -264,13 +264,31 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con
|
|||||||
int actual_len = 0;
|
int actual_len = 0;
|
||||||
auto ddd = create_dte(dte_config);
|
auto ddd = create_dte(dte_config);
|
||||||
ddd->set_mode(dte_mode::UNDEF);
|
ddd->set_mode(dte_mode::UNDEF);
|
||||||
ddd->send_command("AT\r", [&](uint8_t *data, size_t len) {
|
ddd->send_command("AT+CPIN?\r", [&](uint8_t *data, size_t len) {
|
||||||
std::string response((char*)data, len);
|
std::string response((char*)data, len);
|
||||||
ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)data);
|
ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)data);
|
||||||
std::cout << response << std::endl;
|
std::cout << response << std::endl;
|
||||||
return true;
|
return true;
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
|
// ddd->send_command("AT+CPIN=1234\r", [&](uint8_t *data, size_t len) {
|
||||||
|
// std::string response((char*)data, len);
|
||||||
|
// ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)data);
|
||||||
|
// std::cout << response << std::endl;
|
||||||
|
// return true;
|
||||||
|
// }, 1000);
|
||||||
|
esp_netif_t *esp_netif = esp_netif_new(ppp_config);
|
||||||
|
assert(esp_netif);
|
||||||
|
|
||||||
|
auto my_dce = create_dce(ddd, esp_netif);
|
||||||
|
|
||||||
|
ddd->send_command("AT+COPS=?\r", [&](uint8_t *data, size_t len) {
|
||||||
|
std::string response((char*)data, len);
|
||||||
|
ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)data);
|
||||||
|
std::cout << response << std::endl;
|
||||||
|
return true;
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
// auto uart = create_uart_terminal(dte_config);
|
// auto uart = create_uart_terminal(dte_config);
|
||||||
// uart->set_data_cb([&](size_t len){
|
// uart->set_data_cb([&](size_t len){
|
||||||
// actual_len = uart->read(data, 32);
|
// actual_len = uart->read(data, 32);
|
||||||
@ -300,7 +318,7 @@ static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_con
|
|||||||
assert(dce != NULL);
|
assert(dce != NULL);
|
||||||
|
|
||||||
/* create netif object */
|
/* create netif object */
|
||||||
esp_netif_t *esp_netif = esp_netif_new(ppp_config);
|
// esp_netif_t *esp_netif = esp_netif_new(ppp_config);
|
||||||
assert(esp_netif);
|
assert(esp_netif);
|
||||||
#if !defined(CONFIG_EXAMPLE_MODEM_PPP_AUTH_NONE) && (defined(CONFIG_LWIP_PPP_PAP_SUPPORT) || defined(CONFIG_LWIP_PPP_CHAP_SUPPORT))
|
#if !defined(CONFIG_EXAMPLE_MODEM_PPP_AUTH_NONE) && (defined(CONFIG_LWIP_PPP_PAP_SUPPORT) || defined(CONFIG_LWIP_PPP_CHAP_SUPPORT))
|
||||||
#if CONFIG_LWIP_PPP_PAP_SUPPORT
|
#if CONFIG_LWIP_PPP_PAP_SUPPORT
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
#include "terminal_objects.hpp"
|
#include "terminal_objects.hpp"
|
||||||
|
#include "ppp_netif.hpp"
|
||||||
|
|
||||||
|
|
||||||
enum class terminal_error {
|
enum class terminal_error {
|
||||||
@ -62,10 +62,14 @@ typedef std::function<bool(uint8_t *data, size_t len)> got_line_cb;
|
|||||||
|
|
||||||
class dte {
|
class dte {
|
||||||
public:
|
public:
|
||||||
explicit dte(std::unique_ptr<terminal> terminal);
|
explicit dte(std::unique_ptr<terminal> t);
|
||||||
~dte() = default;
|
~dte() = default;
|
||||||
// void set_line_cb(got_line f) { on_line_cb = std::move(f); }
|
// void set_line_cb(got_line f) { on_line_cb = std::move(f); }
|
||||||
|
int write(uint8_t *data, size_t len) { return term->write(data, len); }
|
||||||
|
int read(uint8_t *data, size_t len) { return term->read(data, len); }
|
||||||
|
|
||||||
|
void start() { term->start(); }
|
||||||
|
void data_mode_closed() { term->stop(); }
|
||||||
void set_mode(dte_mode m) { term->start(); mode = m; }
|
void set_mode(dte_mode m) { term->start(); mode = m; }
|
||||||
bool send_command(const std::string& command, got_line_cb got_line, uint32_t time_ms);
|
bool send_command(const std::string& command, got_line_cb got_line, uint32_t time_ms);
|
||||||
|
|
||||||
@ -81,6 +85,14 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class dce {
|
||||||
|
public:
|
||||||
|
explicit dce(std::shared_ptr<dte> d, esp_netif_t * netif);
|
||||||
|
private:
|
||||||
|
std::shared_ptr<dte> _dte;
|
||||||
|
ppp ppp_netif;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP
|
#endif //SIMPLE_CXX_CLIENT_ESP_MODEM_DTE_HPP
|
||||||
|
30
esp_modem/include/cxx_include/ppp_netif.hpp
Normal file
30
esp_modem/include/cxx_include/ppp_netif.hpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
//
|
||||||
|
// Created by david on 2/26/21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SIMPLE_CXX_CLIENT_PPP_NETIF_HPP
|
||||||
|
#define SIMPLE_CXX_CLIENT_PPP_NETIF_HPP
|
||||||
|
|
||||||
|
#include "esp_netif.h"
|
||||||
|
|
||||||
|
class dte;
|
||||||
|
|
||||||
|
//struct ppp_netif_driver;
|
||||||
|
struct ppp_netif_driver {
|
||||||
|
esp_netif_driver_base_t base;
|
||||||
|
dte *e;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class ppp {
|
||||||
|
public:
|
||||||
|
explicit ppp(std::shared_ptr<dte> e, esp_netif_t *netif);
|
||||||
|
|
||||||
|
private:
|
||||||
|
esp_netif_t *netif;
|
||||||
|
std::shared_ptr<dte> _dte;
|
||||||
|
struct ppp_netif_driver driver;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //SIMPLE_CXX_CLIENT_PPP_NETIF_HPP
|
@ -12,6 +12,9 @@ std::unique_ptr<terminal> create_uart_terminal(const struct dte_config *config);
|
|||||||
|
|
||||||
class dte;
|
class dte;
|
||||||
|
|
||||||
std::unique_ptr<dte> create_dte(const struct dte_config *config);
|
std::shared_ptr<dte> create_dte(const struct dte_config *config);
|
||||||
|
|
||||||
|
std::unique_ptr<dce> create_dce(const std::shared_ptr<dte>& e, esp_netif_t *netif);
|
||||||
|
|
||||||
|
|
||||||
#endif //SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP
|
#endif //SIMPLE_CXX_CLIENT_UART_TERMINAL_HPP
|
||||||
|
@ -219,11 +219,11 @@ std::unique_ptr<terminal> create_uart_terminal(const struct dte_config *config)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<dte> create_dte(const struct dte_config *config)
|
std::shared_ptr<dte> create_dte(const struct dte_config *config)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
auto term = std::make_unique<dte>(std::make_unique<uart_terminal>(config));
|
// auto term = std::make_unique<dte>(std::make_unique<uart_terminal>(config));
|
||||||
// term->start();
|
auto term = std::make_shared<dte>(std::make_unique<uart_terminal>(config));
|
||||||
return term;
|
return term;
|
||||||
} catch (std::bad_alloc& e) {
|
} catch (std::bad_alloc& e) {
|
||||||
ESP_LOGE(TAG, "Out of memory");
|
ESP_LOGE(TAG, "Out of memory");
|
||||||
@ -237,6 +237,21 @@ std::unique_ptr<dte> create_dte(const struct dte_config *config)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<dce> create_dce(const std::shared_ptr<dte>& e, esp_netif_t *netif)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return std::make_unique<dce>(e, netif);
|
||||||
|
} catch (std::bad_alloc& e) {
|
||||||
|
ESP_LOGE(TAG, "Out of memory");
|
||||||
|
return nullptr;
|
||||||
|
} catch (esp_err_exception& e) {
|
||||||
|
esp_err_t err = e.get_err_t();
|
||||||
|
ESP_LOGE(TAG, "Error occurred during UART term init: %d", err);
|
||||||
|
ESP_LOGE(TAG, "%s", e.what());
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void uart_terminal::task()
|
void uart_terminal::task()
|
||||||
{
|
{
|
||||||
@ -321,14 +336,18 @@ bool dte::send_command(const std::string& command, got_line_cb got_line, uint32_
|
|||||||
auto actual_len = term->read(data, data_to_read);
|
auto actual_len = term->read(data, data_to_read);
|
||||||
consumed += actual_len;
|
consumed += actual_len;
|
||||||
if (memchr(data, '\n', actual_len)) {
|
if (memchr(data, '\n', actual_len)) {
|
||||||
ESP_LOGI("in the lambda", "FOUND");
|
ESP_LOGD("in the lambda", "FOUND");
|
||||||
if (got_line(buffer.get(), consumed)) {
|
if (got_line(buffer.get(), consumed)) {
|
||||||
signal.set(GOT_LINE);
|
signal.set(GOT_LINE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ESP_LOGI("in the lambda", "len=%d data %s", len, (char*)buffer.get());
|
|
||||||
});
|
});
|
||||||
auto res = signal.wait(GOT_LINE, time_ms);
|
auto res = signal.wait(GOT_LINE, time_ms);
|
||||||
consumed = 0;
|
consumed = 0;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dce::dce(std::shared_ptr<dte> e, esp_netif_t * netif):
|
||||||
|
_dte(std::move(e)), ppp_netif(e, netif)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
69
esp_modem/src/ppp_netif.cpp
Normal file
69
esp_modem/src/ppp_netif.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
//
|
||||||
|
// Created by david on 2/26/21.
|
||||||
|
//
|
||||||
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
|
#include <esp_log.h>
|
||||||
|
#include <esp_event.h>
|
||||||
|
#include "cxx_include/ppp_netif.hpp"
|
||||||
|
#include "cxx_include/esp_modem_dte.hpp"
|
||||||
|
#include "esp_netif_ppp.h"
|
||||||
|
|
||||||
|
|
||||||
|
//struct ppp_netif_driver {
|
||||||
|
// esp_netif_driver_base_t base;
|
||||||
|
//};
|
||||||
|
|
||||||
|
static void on_ppp_changed(void *arg, esp_event_base_t event_base,
|
||||||
|
int32_t event_id, void *event_data)
|
||||||
|
{
|
||||||
|
dte *e = (dte*)arg;
|
||||||
|
if (event_id < NETIF_PP_PHASE_OFFSET) {
|
||||||
|
ESP_LOGI("TAG", "PPP state changed event %d", event_id);
|
||||||
|
// only notify the modem on state/error events, ignoring phase transitions
|
||||||
|
e->data_mode_closed();
|
||||||
|
// esp_modem_notify_ppp_netif_closed(dte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t esp_modem_dte_transmit(void *h, void *buffer, size_t len)
|
||||||
|
{
|
||||||
|
dte *e = (dte*)h;
|
||||||
|
if (e->write((uint8_t*)buffer, len) > 0) {
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static esp_err_t esp_modem_post_attach(esp_netif_t * esp_netif, void * args)
|
||||||
|
{
|
||||||
|
ppp_netif_driver *d = (ppp_netif_driver*)args;
|
||||||
|
esp_netif_driver_ifconfig_t driver_ifconfig = { };
|
||||||
|
driver_ifconfig.transmit = esp_modem_dte_transmit;
|
||||||
|
driver_ifconfig.handle = (void*)d->e;
|
||||||
|
|
||||||
|
d->base.netif = esp_netif;
|
||||||
|
ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig));
|
||||||
|
// check if PPP error events are enabled, if not, do enable the error occurred/state changed
|
||||||
|
// to notify the modem layer when switching modes
|
||||||
|
esp_netif_ppp_config_t ppp_config;
|
||||||
|
esp_netif_ppp_get_params(esp_netif, &ppp_config);
|
||||||
|
if (!ppp_config.ppp_error_event_enabled) {
|
||||||
|
ppp_config.ppp_error_event_enabled = true;
|
||||||
|
esp_netif_ppp_set_params(esp_netif, &ppp_config);
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, (void*)d->e));
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ppp::ppp(std::shared_ptr<dte> e, esp_netif_t *ppp_netif):
|
||||||
|
netif(ppp_netif), _dte(std::move(e))
|
||||||
|
{
|
||||||
|
driver.base.netif = ppp_netif;
|
||||||
|
driver.e = this->_dte.get();
|
||||||
|
driver.base.post_attach = esp_modem_post_attach;
|
||||||
|
throw_if_esp_fail(esp_netif_attach(ppp_netif, &driver));
|
||||||
|
}
|
Reference in New Issue
Block a user