mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-18 04:52:18 +02:00
Add license headers, update namespaces
This commit is contained in:
@ -1,6 +1,17 @@
|
||||
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Created by david on 2/26/21.
|
||||
// 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
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <esp_log.h>
|
||||
@ -9,45 +20,35 @@
|
||||
#include "cxx_include/esp_modem_dte.hpp"
|
||||
#include "esp_netif_ppp.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace esp_modem {
|
||||
|
||||
void Netif::on_ppp_changed(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
Netif *ppp = (Netif*)arg;
|
||||
// DTE *e = (DTE*)arg;
|
||||
std::cout << "on_ppp_changed " << std::endl;
|
||||
int32_t event_id, void *event_data) {
|
||||
auto *ppp = static_cast<Netif *>(arg);
|
||||
ESP_LOGW("TAG", "PPP state changed event %d", event_id);
|
||||
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
|
||||
ppp->signal.set(PPP_EXIT);
|
||||
// e->notify_ppp_exit();
|
||||
// e->data_mode_closed();
|
||||
// esp_modem_notify_ppp_netif_closed(dte);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t Netif::esp_modem_dte_transmit(void *h, void *buffer, size_t len)
|
||||
{
|
||||
Netif *ppp = (Netif*)h;
|
||||
esp_err_t Netif::esp_modem_dte_transmit(void *h, void *buffer, size_t len) {
|
||||
auto *ppp = static_cast<Netif *>(h);
|
||||
if (ppp->signal.is_any(PPP_STARTED)) {
|
||||
// std::cout << "sending data " << len << std::endl;
|
||||
if (ppp->ppp_dte->write((uint8_t*)buffer, len) > 0) {
|
||||
if (ppp->ppp_dte->write((uint8_t *) buffer, len) > 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
esp_err_t Netif::esp_modem_post_attach(esp_netif_t * esp_netif, void * args)
|
||||
{
|
||||
auto d = (ppp_netif_driver*)args;
|
||||
esp_netif_driver_ifconfig_t driver_ifconfig = { };
|
||||
esp_err_t Netif::esp_modem_post_attach(esp_netif_t *esp_netif, void *args) {
|
||||
auto d = (ppp_netif_driver *) args;
|
||||
esp_netif_driver_ifconfig_t driver_ifconfig = {};
|
||||
driver_ifconfig.transmit = Netif::esp_modem_dte_transmit;
|
||||
driver_ifconfig.handle = (void*)d->ppp;
|
||||
std::cout << "esp_modem_post_attach " << std::endl;
|
||||
driver_ifconfig.handle = (void *) d->ppp;
|
||||
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
|
||||
@ -59,32 +60,28 @@ esp_err_t Netif::esp_modem_post_attach(esp_netif_t * esp_netif, void * args)
|
||||
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, 0));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void Netif::receive(uint8_t *data, size_t len)
|
||||
{
|
||||
void Netif::receive(uint8_t *data, size_t len) {
|
||||
if (signal.is_any(PPP_STARTED)) {
|
||||
// std::cout << "received data " << len << std::endl;
|
||||
esp_netif_receive(driver.base.netif, data, len, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
Netif::Netif(std::shared_ptr<DTE> e, esp_netif_t *ppp_netif):
|
||||
ppp_dte(std::move(e)), netif(ppp_netif)
|
||||
{
|
||||
Netif::Netif(std::shared_ptr<DTE> e, esp_netif_t *ppp_netif) :
|
||||
ppp_dte(std::move(e)), netif(ppp_netif) {
|
||||
driver.base.netif = ppp_netif;
|
||||
driver.ppp = this;
|
||||
driver.base.post_attach = esp_modem_post_attach;
|
||||
throw_if_esp_fail(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, (void*)this));
|
||||
throw_if_esp_fail(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, (void *) this));
|
||||
throw_if_esp_fail(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, ppp_netif));
|
||||
throw_if_esp_fail(esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_LOST_IP, esp_netif_action_disconnected, ppp_netif));
|
||||
throw_if_esp_fail(
|
||||
esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_LOST_IP, esp_netif_action_disconnected, ppp_netif));
|
||||
throw_if_esp_fail(esp_netif_attach(ppp_netif, &driver));
|
||||
}
|
||||
|
||||
void Netif::start()
|
||||
{
|
||||
void Netif::start() {
|
||||
ppp_dte->set_data_cb([this](size_t len) -> bool {
|
||||
uint8_t *data;
|
||||
auto actual_len = ppp_dte->read(&data, len);
|
||||
@ -95,10 +92,15 @@ void Netif::start()
|
||||
signal.set(PPP_STARTED);
|
||||
}
|
||||
|
||||
void Netif::stop()
|
||||
{
|
||||
std::cout << "esp_netif_action_stop " << std::endl;
|
||||
void Netif::stop() {
|
||||
esp_netif_action_stop(driver.base.netif, nullptr, 0, nullptr);
|
||||
signal.clear(PPP_STARTED);
|
||||
}
|
||||
|
||||
}
|
||||
Netif::~Netif() {
|
||||
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);
|
||||
}
|
||||
|
||||
} // namespace esp_modem
|
Reference in New Issue
Block a user