forked from espressif/esp-protocols
esp-modem: c++ dce added
This commit is contained in:
@@ -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 {
|
||||
auto term = std::make_unique<dte>(std::make_unique<uart_terminal>(config));
|
||||
// term->start();
|
||||
// auto term = std::make_unique<dte>(std::make_unique<uart_terminal>(config));
|
||||
auto term = std::make_shared<dte>(std::make_unique<uart_terminal>(config));
|
||||
return term;
|
||||
} catch (std::bad_alloc& e) {
|
||||
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()
|
||||
{
|
||||
@@ -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);
|
||||
consumed += 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)) {
|
||||
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);
|
||||
consumed = 0;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
dce::dce(std::shared_ptr<dte> e, esp_netif_t * netif):
|
||||
_dte(std::move(e)), ppp_netif(e, netif)
|
||||
{ }
|
||||
|
||||
|
||||
@@ -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