Skip to content

Commit 29e4b4d

Browse files
committed
fix(eppp_link): Minor cleanup
1 parent 81f3dfc commit 29e4b4d

File tree

5 files changed

+21
-178
lines changed

5 files changed

+21
-178
lines changed

components/eppp_link/eppp_link.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static esp_err_t init_master(struct eppp_config_spi_s *config, esp_netif_t *neti
357357
dev_cfg.cs_ena_pretrans = 0;
358358
dev_cfg.cs_ena_posttrans = 0;
359359
dev_cfg.duty_cycle_pos = 128;
360-
dev_cfg.input_delay_ns = 0;
360+
dev_cfg.input_delay_ns = 6;
361361
dev_cfg.pre_cb = NULL;
362362
dev_cfg.post_cb = NULL;
363363
dev_cfg.cs_ena_posttrans = 3;
@@ -480,6 +480,7 @@ static esp_err_t perform_transaction_slave(union transaction *t, struct eppp_han
480480

481481
esp_err_t eppp_add_channel(int nr, eppp_channel_fn_t *tx, const eppp_channel_fn_t rx)
482482
{
483+
483484
*tx = transmit_channel;
484485
s_rx = rx;
485486
return ESP_OK;
@@ -739,8 +740,8 @@ esp_netif_t *eppp_init(enum eppp_type role, eppp_config_t *config)
739740
}
740741
esp_netif_ppp_config_t netif_params;
741742
ESP_ERROR_CHECK(esp_netif_ppp_get_params(netif, &netif_params));
742-
netif_params.ppp_our_ip4_addr = config->ppp.our_ip4_addr;
743-
netif_params.ppp_their_ip4_addr = config->ppp.their_ip4_addr;
743+
netif_params.ppp_our_ip4_addr.addr = config->ppp.our_ip4_addr;
744+
netif_params.ppp_their_ip4_addr.addr = config->ppp.their_ip4_addr;
744745
netif_params.ppp_error_event_enabled = true;
745746
ESP_ERROR_CHECK(esp_netif_ppp_set_params(netif, &netif_params));
746747
#if CONFIG_EPPP_LINK_DEVICE_SPI

components/eppp_link/examples/rpc/client/main/app_main.c

+7-12
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,16 @@ static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
130130
}
131131
#endif // PING
132132

133+
/*
134+
* local netifs (wifi and ppp)
135+
*/
133136
static esp_netif_t *s_wifi_netif;
134137
static esp_netif_t *s_ppp_netif;
135138
static eppp_channel_fn_t s_tx;
136139

137140
static esp_err_t remote_wifi_receive(void *h, void *buffer, size_t len)
138141
{
139142
if (s_wifi_netif) {
140-
// printf("recv %d\n", len);
141143
return esp_netif_receive(s_wifi_netif, buffer, len, NULL);
142144
}
143145
return ESP_OK;
@@ -146,7 +148,6 @@ static esp_err_t remote_wifi_receive(void *h, void *buffer, size_t len)
146148
esp_err_t remote_wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buffer)
147149
{
148150
if (s_tx) {
149-
// printf("send %d\n", len);
150151
return s_tx(s_ppp_netif, buffer, len);
151152
}
152153
return ESP_OK;
@@ -160,9 +161,9 @@ static esp_err_t remote_wifi_transmit(void *h, void *buffer, size_t len)
160161
return ESP_OK;
161162
}
162163

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

168169
static void remote_wifi_netif(void)
@@ -178,7 +179,7 @@ static void remote_wifi_netif(void)
178179
esp_netif_config_t netif_config = {
179180
.base = ESP_NETIF_BASE_DEFAULT_WIFI_STA,
180181
.driver = wifi_driver_cfg,
181-
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
182+
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
182183
};
183184
s_wifi_netif = esp_netif_new(&netif_config);
184185
}
@@ -206,7 +207,7 @@ static void wifi_init(void *ctx)
206207
ESP_ERROR_CHECK(esp_wifi_remote_get_mac(WIFI_IF_STA, mac) );
207208

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

211212
esp_netif_action_start(s_wifi_netif, 0, 0, 0);
212213
ESP_ERROR_CHECK(esp_wifi_remote_connect() );
@@ -231,7 +232,7 @@ void app_main(void)
231232
#if CONFIG_EPPP_LINK_DEVICE_SPI
232233
config.transport = EPPP_TRANSPORT_SPI;
233234
config.task.priority = 14;
234-
config.spi.freq = 28000000;
235+
config.spi.freq = 40000000;
235236
#else
236237
config.transport = EPPP_TRANSPORT_UART;
237238
config.uart.tx_io = 10;
@@ -250,13 +251,7 @@ void app_main(void)
250251
return ;
251252
}
252253

253-
254254
xTaskCreate(&wifi_init, "initwifi", 8192, NULL, 18, NULL);
255-
// // Setup global DNS
256-
// esp_netif_dns_info_t dns;
257-
// dns.ip.u_addr.ip4.addr = esp_netif_htonl(CONFIG_EXAMPLE_GLOBAL_DNS);
258-
// dns.ip.type = ESP_IPADDR_TYPE_V4;
259-
// ESP_ERROR_CHECK(esp_netif_set_dns_info(eppp_netif, ESP_NETIF_DNS_MAIN, &dns));
260255

261256
#if CONFIG_EXAMPLE_IPERF
262257
esp_console_repl_t *repl = NULL;

components/eppp_link/examples/rpc/common/rpc.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ class RpcEngine {
4646

4747
template <typename T> esp_err_t send(api_id_t id, T *t)
4848
{
49-
// auto data = std::make_unique<RpcData<T>>(id);
5049
RpcData<T> req(id);
5150
size_t size;
5251
auto buf = req.marshall(t, size);
53-
ESP_LOGI("rpc", "Sending API id:%d", (int)id);
52+
ESP_LOGD("rpc", "Sending API id:%d", (int)id);
5453
ESP_LOG_BUFFER_HEXDUMP("rpc", buf, size, ESP_LOG_VERBOSE);
5554
int len = esp_tls_conn_write(tls_, buf, size);
5655
if (len <= 0) {

components/eppp_link/examples/rpc/server/main/server.cpp

+8-53
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <netdb.h>
1313
#include <errno.h>
1414
#include <memory>
15+
#include <esp_private/wifi.h>
1516
#include "esp_wifi.h"
1617
#include "rpc.hpp"
1718
#include "esp_wifi_remote.h"
@@ -69,62 +70,27 @@ const unsigned char prvtkey[] = "-----BEGIN PRIVATE KEY-----\n"
6970
"-----END PRIVATE KEY-----";
7071

7172

73+
extern "C" esp_err_t rpc_example_wifi_recv(void *buffer, uint16_t len, void *eb);
74+
7275
static esp_err_t perform()
7376
{
74-
// RpcHeader header{};
75-
// int len = esp_tls_conn_read(tls, (char *)&header, sizeof(header));
76-
// if (len <= 0) {
77-
// ESP_LOGE(TAG, "Failed to read data from the connection");
78-
// return ESP_FAIL;
79-
// }
8077
RpcEngine rpc(tls);
8178

8279
auto header = rpc.get_header();
8380

84-
// auto data = std::make_unique<RpcData<wifi_mode_t>>(SET_MODE);
85-
8681
switch (header.id) {
8782
case SET_MODE: {
8883
auto req = rpc.get_payload<wifi_mode_t>(SET_MODE, header);
89-
// auto data = std::make_unique<RpcData<wifi_mode_t>>(SET_MODE);
90-
// if (data->head.size != header.size) {
91-
// ESP_LOGE(TAG, "Data size mismatch problem! %d expected, %d given", (int)data->head.size, (int)header.size);
92-
// return ESP_FAIL;
93-
// }
94-
// len = esp_tls_conn_read(tls, (char *)data->value(), data->head.size);
95-
// if (len <= 0) {
96-
// ESP_LOGE(TAG, "Failed to read data from the connection");
97-
// return ESP_FAIL;
98-
// }
99-
// auto resp = std::make_unique<RpcData<esp_err_t>>(SET_MODE);
10084
auto ret = esp_wifi_set_mode(req);
10185
if (rpc.send(SET_MODE, &ret) != ESP_OK) {
10286
return ESP_FAIL;
10387
}
10488
break;
105-
//
106-
// ESP_LOGI(TAG, "esp_wifi_set_mode() returned %x", ret);
107-
// size_t size;
108-
// auto buf = resp->marshall(&ret, size);
109-
////
110-
//// resp->value_ = esp_wifi_set_mode(data->value_);
111-
//// ESP_LOGE(TAG, "size=%d", (int)data->size_);
112-
// ESP_LOG_BUFFER_HEXDUMP(TAG, buf, size, ESP_LOG_INFO);
113-
// len = esp_tls_conn_write(tls, buf, size);
114-
// if (len <= 0) {
115-
// ESP_LOGE(TAG, "Failed to write data to the connection");
116-
// return ESP_FAIL;
117-
// }
118-
11989
}
12090
case INIT: {
12191
auto req = rpc.get_payload<wifi_init_config_t>(INIT, header);
12292
req.osi_funcs = &g_wifi_osi_funcs;
12393
req.wpa_crypto_funcs = g_wifi_default_wpa_crypto_funcs;
124-
// ESP_LOG_BUFFER_HEXDUMP("cfg", &req, sizeof(req), ESP_LOG_WARN);
125-
// wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
126-
// ESP_LOG_BUFFER_HEXDUMP("cfg", &cfg, sizeof(cfg), ESP_LOG_WARN);
127-
12894
auto ret = esp_wifi_init(&req);
12995
if (rpc.send(INIT, &ret) != ESP_OK) {
13096
return ESP_FAIL;
@@ -144,6 +110,10 @@ static esp_err_t perform()
144110
return ESP_FAIL;
145111
}
146112

113+
// setup wifi callbacks now
114+
esp_wifi_internal_reg_rxcb(WIFI_IF_STA, rpc_example_wifi_recv);
115+
esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free);
116+
//
147117
auto ret = esp_wifi_start();
148118
if (rpc.send(START, &ret) != ESP_OK) {
149119
return ESP_FAIL;
@@ -170,25 +140,12 @@ static esp_err_t perform()
170140
}
171141
break;
172142
}
173-
174-
175143
}
176144
return ESP_OK;
177-
178-
// ESP_LOGW(TAG, "Data from the connection (size=%d)", len);
179-
// ESP_LOG_BUFFER_HEXDUMP(TAG, buf, len, ESP_LOG_WARN);
180-
// len = esp_tls_conn_write(tls,"ZDAR",4);
181-
// if (len <= 0) {
182-
// ESP_LOGE(TAG, "Failed to write data to the connection");
183-
// goto cleanup;
184-
// }
185-
186145
}
187146

188147
static void server(void *ctx)
189148
{
190-
char buf[512];
191-
192149
struct sockaddr_in dest_addr = {};
193150
int ret;
194151
int opt = 1;
@@ -241,13 +198,11 @@ static void server(void *ctx)
241198
goto exit;
242199
}
243200
ESP_LOGI(TAG, "Secure socket open");
244-
memset(buf, 0x00, sizeof(buf));
245201
while (perform() == ESP_OK) {}
246202

247-
248203
esp_tls_server_session_delete(tls);
249204
exit:
250-
vTaskDelete(NULL);
205+
vTaskDelete(nullptr);
251206

252207
}
253208

components/eppp_link/examples/rpc/server/main/station_example_main.c

+1-108
Original file line numberDiff line numberDiff line change
@@ -16,131 +16,30 @@
1616
#include "eppp_link.h"
1717
#include "esp_wifi_remote.h"
1818

19-
/* FreeRTOS event group to signal when we are connected*/
20-
static EventGroupHandle_t s_wifi_event_group;
21-
22-
/* The event group allows multiple bits for each event, but we only care about two events:
23-
* - we are connected to the AP with an IP
24-
* - we failed to connect after the maximum amount of retries */
25-
#define WIFI_CONNECTED_BIT BIT0
26-
#define WIFI_FAIL_BIT BIT1
27-
2819
static const char *TAG = "sta2pppos";
2920

30-
static int s_retry_num = 0;
31-
32-
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
33-
{
34-
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
35-
esp_wifi_connect();
36-
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
37-
if (s_retry_num < CONFIG_ESP_MAXIMUM_RETRY) {
38-
esp_wifi_connect();
39-
s_retry_num++;
40-
ESP_LOGI(TAG, "retry to connect to the AP");
41-
} else {
42-
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
43-
}
44-
ESP_LOGI(TAG, "connect to the AP fail");
45-
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
46-
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
47-
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
48-
s_retry_num = 0;
49-
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
50-
}
51-
}
52-
53-
void wifi_init_sta(void)
54-
{
55-
s_wifi_event_group = xEventGroupCreate();
56-
57-
ESP_ERROR_CHECK(esp_netif_init());
58-
59-
ESP_ERROR_CHECK(esp_event_loop_create_default());
60-
esp_netif_create_default_wifi_sta();
61-
62-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
63-
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
64-
65-
esp_event_handler_instance_t instance_any_id;
66-
esp_event_handler_instance_t instance_got_ip;
67-
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
68-
ESP_EVENT_ANY_ID,
69-
&event_handler,
70-
NULL,
71-
&instance_any_id));
72-
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
73-
IP_EVENT_STA_GOT_IP,
74-
&event_handler,
75-
NULL,
76-
&instance_got_ip));
77-
78-
wifi_config_t wifi_config = {
79-
.sta = {
80-
.ssid = CONFIG_ESP_WIFI_SSID,
81-
.password = CONFIG_ESP_WIFI_PASSWORD,
82-
},
83-
};
84-
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
85-
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
86-
ESP_ERROR_CHECK(esp_wifi_start() );
87-
88-
ESP_LOGI(TAG, "wifi_init_sta finished.");
89-
90-
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
91-
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
92-
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
93-
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
94-
pdFALSE,
95-
pdFALSE,
96-
portMAX_DELAY);
97-
98-
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
99-
* happened. */
100-
if (bits & WIFI_CONNECTED_BIT) {
101-
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
102-
CONFIG_ESP_WIFI_SSID, CONFIG_ESP_WIFI_PASSWORD);
103-
} else if (bits & WIFI_FAIL_BIT) {
104-
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
105-
CONFIG_ESP_WIFI_SSID, CONFIG_ESP_WIFI_PASSWORD);
106-
} else {
107-
ESP_LOGE(TAG, "UNEXPECTED EVENT");
108-
}
109-
}
110-
111-
112-
113-
114-
11521
esp_err_t server_init(void);
11622

11723
static eppp_channel_fn_t s_tx;
11824
static esp_netif_t *s_ppp_netif;
11925

12026
static esp_err_t netif_recv(void *h, void *buffer, size_t len)
12127
{
122-
// printf("recv %d\n", len);
123-
// ESP_LOG_BUFFER_HEXDUMP("cfg", buffer, len, ESP_LOG_WARN);
12428
return esp_wifi_internal_tx(WIFI_IF_STA, buffer, len);
12529
}
12630

127-
static esp_err_t wifi_recv(void *buffer, uint16_t len, void *eb)
31+
esp_err_t rpc_example_wifi_recv(void *buffer, uint16_t len, void *eb)
12832
{
129-
// printf("send %d\n", len);
13033
if (s_tx) {
131-
// printf("send %d\n", len);
13234
esp_err_t ret = s_tx(s_ppp_netif, buffer, len);
13335
esp_wifi_internal_free_rx_buffer(eb);
13436
return ret;
13537
}
13638
return ESP_OK;
13739
}
13840

139-
14041
void app_main(void)
14142
{
142-
143-
14443
//Initialize NVS
14544
esp_err_t ret = nvs_flash_init();
14645
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
@@ -159,13 +58,7 @@ void app_main(void)
15958
return ;
16059
}
16160

162-
// esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_recv);
163-
// esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free);
16461
eppp_add_channel(1, &s_tx, netif_recv);
16562

16663
server_init();
167-
vTaskDelay(pdMS_TO_TICKS(10000));
168-
esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_recv);
169-
esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free);
170-
17164
}

0 commit comments

Comments
 (0)