Files
esp-protocols/components/esp_modem/src/esp_modem_netif.cpp

127 lines
4.5 KiB
C++
Raw Normal View History

2021-03-29 19:34:45 +02:00
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
2021-02-26 20:23:29 +01:00
//
2021-03-29 19:34:45 +02:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
2021-02-26 20:23:29 +01:00
//
2021-03-29 19:34:45 +02:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-02-26 20:23:29 +01:00
#include <memory>
#include <utility>
#include <inttypes.h>
2021-02-26 20:23:29 +01:00
#include <esp_log.h>
#include <esp_event.h>
2022-09-09 09:23:56 +02:00
#include "esp_idf_version.h"
#include "cxx_include/esp_modem_netif.hpp"
2021-02-26 20:23:29 +01:00
#include "cxx_include/esp_modem_dte.hpp"
#include "esp_netif_ppp.h"
2021-03-29 19:34:45 +02:00
namespace esp_modem {
2021-02-26 20:23:29 +01:00
void Netif::on_ppp_changed(void *arg, esp_event_base_t event_base,
2021-06-01 10:21:51 +02:00
int32_t event_id, void *event_data)
{
2021-03-29 19:34:45 +02:00
auto *ppp = static_cast<Netif *>(arg);
2021-02-26 20:23:29 +01:00
if (event_id < NETIF_PP_PHASE_OFFSET) {
ESP_LOGI("esp_modem_netif", "PPP state changed event %" PRId32, event_id);
2021-02-26 20:23:29 +01:00
// only notify the modem on state/error events, ignoring phase transitions
2021-03-04 20:19:18 +01:00
ppp->signal.set(PPP_EXIT);
2021-02-26 20:23:29 +01:00
}
}
2021-06-01 10:21:51 +02:00
esp_err_t Netif::esp_modem_dte_transmit(void *h, void *buffer, size_t len)
{
2021-03-29 19:34:45 +02:00
auto *ppp = static_cast<Netif *>(h);
2021-03-04 20:19:18 +01:00
if (ppp->signal.is_any(PPP_STARTED)) {
if (ppp->ppp_dte && ppp->ppp_dte->write((uint8_t *) buffer, len) > 0) {
2021-03-04 20:19:18 +01:00
return ESP_OK;
}
2021-02-26 20:23:29 +01:00
}
return ESP_FAIL;
}
2021-06-01 10:21:51 +02:00
esp_err_t Netif::esp_modem_post_attach(esp_netif_t *esp_netif, void *args)
{
2021-03-29 19:34:45 +02:00
auto d = (ppp_netif_driver *) args;
esp_netif_driver_ifconfig_t driver_ifconfig = {};
driver_ifconfig.transmit = Netif::esp_modem_dte_transmit;
2021-03-29 19:34:45 +02:00
driver_ifconfig.handle = (void *) d->ppp;
2021-02-26 20:23:29 +01:00
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
2021-05-19 23:00:28 +08:00
esp_netif_ppp_config_t ppp_config = { .ppp_phase_event_enabled = true, // assuming phase enabled, as earlier IDFs
2021-06-01 10:21:51 +02:00
.ppp_error_event_enabled = false
}; // don't provide cfg getters so we enable both events
2022-09-09 09:23:56 +02:00
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
2021-04-15 09:46:28 +02:00
esp_netif_ppp_get_params(esp_netif, &ppp_config);
2021-05-19 23:00:28 +08:00
#endif // ESP-IDF >= v4.4
2021-03-04 20:19:18 +01:00
if (!ppp_config.ppp_error_event_enabled) {
2021-02-26 20:23:29 +01:00
ppp_config.ppp_error_event_enabled = true;
esp_netif_ppp_set_params(esp_netif, &ppp_config);
2021-03-04 20:19:18 +01:00
}
2021-02-26 20:23:29 +01:00
return ESP_OK;
}
2021-06-01 10:21:51 +02:00
void Netif::receive(uint8_t *data, size_t len)
{
2021-03-04 20:19:18 +01:00
if (signal.is_any(PPP_STARTED)) {
esp_netif_receive(driver.base.netif, data, len, nullptr);
}
2021-02-27 09:32:14 +01:00
}
2021-02-26 20:23:29 +01:00
2021-03-29 19:34:45 +02:00
Netif::Netif(std::shared_ptr<DTE> e, esp_netif_t *ppp_netif) :
2021-06-01 10:21:51 +02:00
ppp_dte(std::move(e)), netif(ppp_netif)
{
2021-02-26 20:23:29 +01:00
driver.base.netif = ppp_netif;
2021-03-04 20:19:18 +01:00
driver.ppp = this;
2021-02-26 20:23:29 +01:00
driver.base.post_attach = esp_modem_post_attach;
ESP_MODEM_THROW_IF_ERROR(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, (void *) this));
ESP_MODEM_THROW_IF_ERROR(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, ppp_netif));
ESP_MODEM_THROW_IF_ERROR(
2021-06-01 10:21:51 +02:00
esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_LOST_IP, esp_netif_action_disconnected, ppp_netif));
ESP_MODEM_THROW_IF_ERROR(esp_netif_attach(ppp_netif, &driver));
2021-02-26 20:23:29 +01:00
}
2021-02-27 09:32:14 +01:00
2021-06-01 10:21:51 +02:00
void Netif::start()
{
ppp_dte->set_read_cb([this](uint8_t *data, size_t len) -> bool {
receive(data, len);
2021-03-04 20:19:18 +01:00
return false;
});
2021-03-30 08:25:16 +02:00
esp_netif_action_start(driver.base.netif, nullptr, 0, nullptr);
2021-03-04 20:19:18 +01:00
signal.set(PPP_STARTED);
2021-03-03 20:35:08 +01:00
}
2021-06-01 10:21:51 +02:00
void Netif::stop()
{
2021-03-03 20:35:08 +01:00
esp_netif_action_stop(driver.base.netif, nullptr, 0, nullptr);
2021-03-04 20:19:18 +01:00
signal.clear(PPP_STARTED);
2021-03-29 19:34:45 +02:00
}
2021-06-01 10:21:51 +02:00
Netif::~Netif()
{
2021-04-15 14:59:30 +02:00
if (signal.is_any(PPP_STARTED)) {
esp_netif_action_stop(driver.base.netif, nullptr, 0, nullptr);
signal.clear(PPP_STARTED);
signal.wait(PPP_EXIT, 30000);
}
2021-03-29 19:34:45 +02:00
esp_event_handler_unregister(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed);
esp_event_handler_unregister(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected);
esp_event_handler_unregister(IP_EVENT, IP_EVENT_PPP_LOST_IP, esp_netif_action_disconnected);
}
2021-03-03 20:35:08 +01:00
2021-06-01 10:21:51 +02:00
void Netif::wait_until_ppp_exits()
{
2021-04-04 22:15:46 +02:00
signal.wait(PPP_EXIT, 30000);
}
2021-03-29 19:34:45 +02:00
} // namespace esp_modem