Skip to content

Commit

Permalink
fix(eppp_link): Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Feb 13, 2024
1 parent 81f3dfc commit 29e4b4d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 178 deletions.
7 changes: 4 additions & 3 deletions components/eppp_link/eppp_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ static esp_err_t init_master(struct eppp_config_spi_s *config, esp_netif_t *neti
dev_cfg.cs_ena_pretrans = 0;
dev_cfg.cs_ena_posttrans = 0;
dev_cfg.duty_cycle_pos = 128;
dev_cfg.input_delay_ns = 0;
dev_cfg.input_delay_ns = 6;
dev_cfg.pre_cb = NULL;
dev_cfg.post_cb = NULL;
dev_cfg.cs_ena_posttrans = 3;
Expand Down Expand Up @@ -480,6 +480,7 @@ static esp_err_t perform_transaction_slave(union transaction *t, struct eppp_han

esp_err_t eppp_add_channel(int nr, eppp_channel_fn_t *tx, const eppp_channel_fn_t rx)
{

*tx = transmit_channel;
s_rx = rx;
return ESP_OK;
Expand Down Expand Up @@ -739,8 +740,8 @@ esp_netif_t *eppp_init(enum eppp_type role, eppp_config_t *config)
}
esp_netif_ppp_config_t netif_params;
ESP_ERROR_CHECK(esp_netif_ppp_get_params(netif, &netif_params));
netif_params.ppp_our_ip4_addr = config->ppp.our_ip4_addr;
netif_params.ppp_their_ip4_addr = config->ppp.their_ip4_addr;
netif_params.ppp_our_ip4_addr.addr = config->ppp.our_ip4_addr;
netif_params.ppp_their_ip4_addr.addr = config->ppp.their_ip4_addr;
netif_params.ppp_error_event_enabled = true;
ESP_ERROR_CHECK(esp_netif_ppp_set_params(netif, &netif_params));
#if CONFIG_EPPP_LINK_DEVICE_SPI
Expand Down
19 changes: 7 additions & 12 deletions components/eppp_link/examples/rpc/client/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
}
#endif // PING

/*
* local netifs (wifi and ppp)
*/
static esp_netif_t *s_wifi_netif;
static esp_netif_t *s_ppp_netif;
static eppp_channel_fn_t s_tx;

static esp_err_t remote_wifi_receive(void *h, void *buffer, size_t len)
{
if (s_wifi_netif) {
// printf("recv %d\n", len);
return esp_netif_receive(s_wifi_netif, buffer, len, NULL);
}
return ESP_OK;
Expand All @@ -146,7 +148,6 @@ static esp_err_t remote_wifi_receive(void *h, void *buffer, size_t len)
esp_err_t remote_wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buffer)
{
if (s_tx) {
// printf("send %d\n", len);
return s_tx(s_ppp_netif, buffer, len);
}
return ESP_OK;
Expand All @@ -160,9 +161,9 @@ static esp_err_t remote_wifi_transmit(void *h, void *buffer, size_t len)
return ESP_OK;
}

// this is needed as the ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA config frees the eb on pbuf-free
static void wifi_free(void *h, void *buffer)
{
// printf("wifi_free %p\n", buffer);
}

static void remote_wifi_netif(void)
Expand All @@ -178,7 +179,7 @@ static void remote_wifi_netif(void)
esp_netif_config_t netif_config = {
.base = ESP_NETIF_BASE_DEFAULT_WIFI_STA,
.driver = wifi_driver_cfg,
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
};
s_wifi_netif = esp_netif_new(&netif_config);
}
Expand Down Expand Up @@ -206,7 +207,7 @@ static void wifi_init(void *ctx)
ESP_ERROR_CHECK(esp_wifi_remote_get_mac(WIFI_IF_STA, mac) );

esp_netif_set_mac(s_wifi_netif, mac);
vTaskDelay(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(1000)); // we're not supporting WIFI_EVENT yet, just play with delays for now

esp_netif_action_start(s_wifi_netif, 0, 0, 0);
ESP_ERROR_CHECK(esp_wifi_remote_connect() );
Expand All @@ -231,7 +232,7 @@ void app_main(void)
#if CONFIG_EPPP_LINK_DEVICE_SPI
config.transport = EPPP_TRANSPORT_SPI;
config.task.priority = 14;
config.spi.freq = 28000000;
config.spi.freq = 40000000;
#else
config.transport = EPPP_TRANSPORT_UART;
config.uart.tx_io = 10;
Expand All @@ -250,13 +251,7 @@ void app_main(void)
return ;
}


xTaskCreate(&wifi_init, "initwifi", 8192, NULL, 18, NULL);
// // Setup global DNS
// esp_netif_dns_info_t dns;
// dns.ip.u_addr.ip4.addr = esp_netif_htonl(CONFIG_EXAMPLE_GLOBAL_DNS);
// dns.ip.type = ESP_IPADDR_TYPE_V4;
// ESP_ERROR_CHECK(esp_netif_set_dns_info(eppp_netif, ESP_NETIF_DNS_MAIN, &dns));

#if CONFIG_EXAMPLE_IPERF
esp_console_repl_t *repl = NULL;
Expand Down
3 changes: 1 addition & 2 deletions components/eppp_link/examples/rpc/common/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ class RpcEngine {

template <typename T> esp_err_t send(api_id_t id, T *t)
{
// auto data = std::make_unique<RpcData<T>>(id);
RpcData<T> req(id);
size_t size;
auto buf = req.marshall(t, size);
ESP_LOGI("rpc", "Sending API id:%d", (int)id);
ESP_LOGD("rpc", "Sending API id:%d", (int)id);
ESP_LOG_BUFFER_HEXDUMP("rpc", buf, size, ESP_LOG_VERBOSE);
int len = esp_tls_conn_write(tls_, buf, size);
if (len <= 0) {
Expand Down
61 changes: 8 additions & 53 deletions components/eppp_link/examples/rpc/server/main/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <netdb.h>
#include <errno.h>
#include <memory>
#include <esp_private/wifi.h>
#include "esp_wifi.h"
#include "rpc.hpp"
#include "esp_wifi_remote.h"
Expand Down Expand Up @@ -69,62 +70,27 @@ const unsigned char prvtkey[] = "-----BEGIN PRIVATE KEY-----\n"
"-----END PRIVATE KEY-----";


extern "C" esp_err_t rpc_example_wifi_recv(void *buffer, uint16_t len, void *eb);

static esp_err_t perform()
{
// RpcHeader header{};
// int len = esp_tls_conn_read(tls, (char *)&header, sizeof(header));
// if (len <= 0) {
// ESP_LOGE(TAG, "Failed to read data from the connection");
// return ESP_FAIL;
// }
RpcEngine rpc(tls);

auto header = rpc.get_header();

// auto data = std::make_unique<RpcData<wifi_mode_t>>(SET_MODE);

switch (header.id) {
case SET_MODE: {
auto req = rpc.get_payload<wifi_mode_t>(SET_MODE, header);
// auto data = std::make_unique<RpcData<wifi_mode_t>>(SET_MODE);
// if (data->head.size != header.size) {
// ESP_LOGE(TAG, "Data size mismatch problem! %d expected, %d given", (int)data->head.size, (int)header.size);
// return ESP_FAIL;
// }
// len = esp_tls_conn_read(tls, (char *)data->value(), data->head.size);
// if (len <= 0) {
// ESP_LOGE(TAG, "Failed to read data from the connection");
// return ESP_FAIL;
// }
// auto resp = std::make_unique<RpcData<esp_err_t>>(SET_MODE);
auto ret = esp_wifi_set_mode(req);
if (rpc.send(SET_MODE, &ret) != ESP_OK) {
return ESP_FAIL;
}
break;
//
// ESP_LOGI(TAG, "esp_wifi_set_mode() returned %x", ret);
// size_t size;
// auto buf = resp->marshall(&ret, size);
////
//// resp->value_ = esp_wifi_set_mode(data->value_);
//// ESP_LOGE(TAG, "size=%d", (int)data->size_);
// ESP_LOG_BUFFER_HEXDUMP(TAG, buf, size, ESP_LOG_INFO);
// len = esp_tls_conn_write(tls, buf, size);
// if (len <= 0) {
// ESP_LOGE(TAG, "Failed to write data to the connection");
// return ESP_FAIL;
// }

}
case INIT: {
auto req = rpc.get_payload<wifi_init_config_t>(INIT, header);
req.osi_funcs = &g_wifi_osi_funcs;
req.wpa_crypto_funcs = g_wifi_default_wpa_crypto_funcs;
// ESP_LOG_BUFFER_HEXDUMP("cfg", &req, sizeof(req), ESP_LOG_WARN);
// wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
// ESP_LOG_BUFFER_HEXDUMP("cfg", &cfg, sizeof(cfg), ESP_LOG_WARN);

auto ret = esp_wifi_init(&req);
if (rpc.send(INIT, &ret) != ESP_OK) {
return ESP_FAIL;
Expand All @@ -144,6 +110,10 @@ static esp_err_t perform()
return ESP_FAIL;
}

// setup wifi callbacks now
esp_wifi_internal_reg_rxcb(WIFI_IF_STA, rpc_example_wifi_recv);
esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free);
//
auto ret = esp_wifi_start();
if (rpc.send(START, &ret) != ESP_OK) {
return ESP_FAIL;
Expand All @@ -170,25 +140,12 @@ static esp_err_t perform()
}
break;
}


}
return ESP_OK;

// ESP_LOGW(TAG, "Data from the connection (size=%d)", len);
// ESP_LOG_BUFFER_HEXDUMP(TAG, buf, len, ESP_LOG_WARN);
// len = esp_tls_conn_write(tls,"ZDAR",4);
// if (len <= 0) {
// ESP_LOGE(TAG, "Failed to write data to the connection");
// goto cleanup;
// }

}

static void server(void *ctx)
{
char buf[512];

struct sockaddr_in dest_addr = {};
int ret;
int opt = 1;
Expand Down Expand Up @@ -241,13 +198,11 @@ static void server(void *ctx)
goto exit;
}
ESP_LOGI(TAG, "Secure socket open");
memset(buf, 0x00, sizeof(buf));
while (perform() == ESP_OK) {}


esp_tls_server_session_delete(tls);
exit:
vTaskDelete(NULL);
vTaskDelete(nullptr);

}

Expand Down
109 changes: 1 addition & 108 deletions components/eppp_link/examples/rpc/server/main/station_example_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,131 +16,30 @@
#include "eppp_link.h"
#include "esp_wifi_remote.h"

/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;

/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1

static const char *TAG = "sta2pppos";

static int s_retry_num = 0;

static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < CONFIG_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG, "connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}

void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();

ESP_ERROR_CHECK(esp_netif_init());

ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));

wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_ESP_WIFI_SSID,
.password = CONFIG_ESP_WIFI_PASSWORD,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );

ESP_LOGI(TAG, "wifi_init_sta finished.");

/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);

/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
CONFIG_ESP_WIFI_SSID, CONFIG_ESP_WIFI_PASSWORD);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
CONFIG_ESP_WIFI_SSID, CONFIG_ESP_WIFI_PASSWORD);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}





esp_err_t server_init(void);

static eppp_channel_fn_t s_tx;
static esp_netif_t *s_ppp_netif;

static esp_err_t netif_recv(void *h, void *buffer, size_t len)
{
// printf("recv %d\n", len);
// ESP_LOG_BUFFER_HEXDUMP("cfg", buffer, len, ESP_LOG_WARN);
return esp_wifi_internal_tx(WIFI_IF_STA, buffer, len);
}

static esp_err_t wifi_recv(void *buffer, uint16_t len, void *eb)
esp_err_t rpc_example_wifi_recv(void *buffer, uint16_t len, void *eb)
{
// printf("send %d\n", len);
if (s_tx) {
// printf("send %d\n", len);
esp_err_t ret = s_tx(s_ppp_netif, buffer, len);
esp_wifi_internal_free_rx_buffer(eb);
return ret;
}
return ESP_OK;
}


void app_main(void)
{


//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
Expand All @@ -159,13 +58,7 @@ void app_main(void)
return ;
}

// esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_recv);
// esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free);
eppp_add_channel(1, &s_tx, netif_recv);

server_init();
vTaskDelay(pdMS_TO_TICKS(10000));
esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_recv);
esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free);

}

0 comments on commit 29e4b4d

Please sign in to comment.