Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c0f4017

Browse files
committedJan 12, 2024
fix(eppp_link): Fix stuff
1 parent 982c0d7 commit c0f4017

File tree

10 files changed

+209
-14
lines changed

10 files changed

+209
-14
lines changed
 

‎components/eppp_link/eppp_link.c

+52-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static EventGroupHandle_t s_event_group = NULL;
3030
static const char *TAG = "eppp_link";
3131
static int s_retry_num = 0;
3232
static int s_eppp_netif_count = 0; // used as a suffix for the netif key
33+
static eppp_channel_fn_t s_rx = NULL;
3334

3435
struct eppp_handle {
3536
#if CONFIG_EPPP_LINK_DEVICE_SPI
@@ -52,8 +53,28 @@ struct eppp_handle {
5253
struct packet {
5354
size_t len;
5455
uint8_t *data;
56+
int channel;
5557
};
5658

59+
static esp_err_t transmit_channel(void *netif, void *buffer, size_t len)
60+
{
61+
struct eppp_handle *h = esp_netif_get_io_driver(netif);
62+
struct packet buf = { .len = len };
63+
buf.channel = 1;
64+
buf.data = malloc(len);
65+
if (buf.data == NULL) {
66+
ESP_LOGE(TAG, "Failed to allocate packet");
67+
return ESP_FAIL;
68+
}
69+
memcpy(buf.data, buffer, len);
70+
BaseType_t ret = xQueueSend(h->out_queue, &buf, pdMS_TO_TICKS(10));
71+
if (ret != pdTRUE) {
72+
ESP_LOGE(TAG, "Failed to queue packet to slave!");
73+
return ESP_FAIL;
74+
}
75+
return ESP_OK;
76+
}
77+
5778
static esp_err_t transmit(void *h, void *buffer, size_t len)
5879
{
5980
struct eppp_handle *handle = h;
@@ -230,6 +251,10 @@ static void on_ip_event(void *arg, esp_event_base_t base, int32_t event_id, void
230251
{
231252
ip_event_got_ip_t *event = (ip_event_got_ip_t *)data;
232253
esp_netif_t *netif = event->esp_netif;
254+
if (event_id == IP_EVENT_STA_GOT_IP) {
255+
ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s(%s)\" address: " IPSTR, esp_netif_get_desc(netif),
256+
esp_netif_get_ifkey(netif), IP2STR(&event->ip_info.ip));
257+
}
233258
int netif_cnt = get_netif_num(netif);
234259
if (netif_cnt < 0) {
235260
return;
@@ -275,6 +300,7 @@ struct header {
275300
} __attribute__((packed));
276301
};
277302
uint8_t magic;
303+
uint8_t channel;
278304
uint8_t checksum;
279305
} __attribute__((packed));
280306

@@ -452,6 +478,13 @@ static esp_err_t perform_transaction_slave(union transaction *t, struct eppp_han
452478
return spi_slave_transmit(h->spi_host, &t->slave, portMAX_DELAY);
453479
}
454480

481+
esp_err_t eppp_add_channel(int nr, eppp_channel_fn_t *tx, const eppp_channel_fn_t rx)
482+
{
483+
*tx = transmit_channel;
484+
s_rx = rx;
485+
return ESP_OK;
486+
}
487+
455488
esp_err_t eppp_perform(esp_netif_t *netif)
456489
{
457490
static WORD_ALIGNED_ATTR uint8_t out_buf[TRANSFER_SIZE] = {};
@@ -482,8 +515,10 @@ esp_err_t eppp_perform(esp_netif_t *netif)
482515
head->magic = FRAME_OUT_CTRL_EX;
483516
head->size = 0;
484517
head->checksum = 0;
518+
head->channel = 0;
485519
BaseType_t tx_queue_stat = xQueueReceive(h->out_queue, &buf, 0);
486520
if (tx_queue_stat == pdTRUE && buf.data) {
521+
head->channel = buf.channel;
487522
if (buf.len > SHORT_PAYLOAD) {
488523
head->magic = FRAME_OUT_CTRL;
489524
head->size = buf.len;
@@ -521,7 +556,14 @@ esp_err_t eppp_perform(esp_netif_t *netif)
521556
return ESP_FAIL;
522557
}
523558
if (head->magic == FRAME_IN_CTRL_EX && head->short_size > 0) {
524-
esp_netif_receive(netif, in_buf + sizeof(struct header), head->short_size, NULL);
559+
if (head->channel == 0) {
560+
esp_netif_receive(netif, in_buf + sizeof(struct header), head->short_size, NULL);
561+
} else {
562+
ESP_LOGE(TAG, "Got channel %d size %d", head->channel, head->short_size);
563+
if (s_rx != NULL) {
564+
s_rx(netif, in_buf + sizeof(struct header), head->short_size);
565+
}
566+
}
525567
}
526568
size_t in_long_payload = 0;
527569
if (head->magic == FRAME_IN_CTRL) {
@@ -537,6 +579,7 @@ esp_err_t eppp_perform(esp_netif_t *netif)
537579
head->magic = FRAME_OUT_DATA;
538580
head->size = out_long_payload;
539581
head->checksum = 0;
582+
head->channel = buf.channel;
540583
for (int i = 0; i < sizeof(struct header) - 1; ++i) {
541584
head->checksum += out_buf[i];
542585
}
@@ -570,7 +613,14 @@ esp_err_t eppp_perform(esp_netif_t *netif)
570613

571614
if (head->size > 0) {
572615
ESP_LOG_BUFFER_HEXDUMP(TAG, in_buf + sizeof(struct header), head->size, ESP_LOG_VERBOSE);
573-
esp_netif_receive(netif, in_buf + sizeof(struct header), head->size, NULL);
616+
if (head->channel == 0) {
617+
esp_netif_receive(netif, in_buf + sizeof(struct header), head->size, NULL);
618+
} else {
619+
ESP_LOGE(TAG, "Got channel %d size %d", head->channel, head->size);
620+
if (s_rx != NULL) {
621+
s_rx(netif, in_buf + sizeof(struct header), head->size);
622+
}
623+
}
574624
}
575625
return ESP_OK;
576626
}

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

+70-2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,59 @@ static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
130130
}
131131
#endif // PING
132132

133+
static esp_netif_t *s_wifi_netif;
134+
static esp_netif_t *s_ppp_netif;
135+
static eppp_channel_fn_t s_tx;
136+
137+
static esp_err_t remote_wifi_receive(void *h, void *buffer, size_t len)
138+
{
139+
if (s_wifi_netif) {
140+
printf("recv %d\n", len);
141+
return esp_netif_receive(s_wifi_netif, buffer, len, NULL);
142+
}
143+
return ESP_OK;
144+
}
145+
146+
esp_err_t remote_wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buffer)
147+
{
148+
if (s_tx) {
149+
printf("send %d\n", len);
150+
return s_tx(s_ppp_netif, buffer, len);
151+
}
152+
return ESP_OK;
153+
}
154+
155+
static esp_err_t remote_wifi_transmit(void *h, void *buffer, size_t len)
156+
{
157+
if (s_tx) {
158+
return s_tx(s_ppp_netif, buffer, len);
159+
}
160+
return ESP_OK;
161+
}
162+
163+
static void wifi_free(void *h, void *buffer)
164+
{
165+
printf("wifi_free %p\n", buffer);
166+
}
167+
168+
static void remote_wifi_netif(void)
169+
{
170+
esp_netif_driver_ifconfig_t driver_cfg = {
171+
.handle = (void *)1,
172+
.transmit = remote_wifi_transmit,
173+
.transmit_wrap = remote_wifi_transmit_wrap,
174+
.driver_free_rx_buffer = wifi_free
175+
176+
};
177+
const esp_netif_driver_ifconfig_t *wifi_driver_cfg = &driver_cfg;
178+
esp_netif_config_t netif_config = {
179+
.base = ESP_NETIF_BASE_DEFAULT_WIFI_STA,
180+
.driver = wifi_driver_cfg,
181+
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
182+
};
183+
s_wifi_netif = esp_netif_new(&netif_config);
184+
}
185+
133186
void app_main(void)
134187
{
135188
ESP_LOGI(TAG, "[APP] Startup..");
@@ -152,14 +205,21 @@ void app_main(void)
152205
config.uart.rx_io = 11;
153206
config.uart.baud = 2000000;
154207
#endif
155-
esp_netif_t *eppp_netif = eppp_connect(&config);
156-
if (eppp_netif == NULL) {
208+
s_ppp_netif = eppp_connect(&config);
209+
if (s_ppp_netif == NULL) {
157210
ESP_LOGE(TAG, "Failed to connect");
158211
return ;
159212
}
213+
eppp_add_channel(1, &s_tx, remote_wifi_receive);
214+
remote_wifi_netif();
215+
if (s_wifi_netif == NULL) {
216+
ESP_LOGE(TAG, "Failed to create wifi netif");
217+
return ;
218+
}
160219

161220
client_init();
162221

222+
163223
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
164224
ESP_LOG_BUFFER_HEXDUMP("cfg", &cfg, sizeof(cfg), ESP_LOG_WARN);
165225
ESP_ERROR_CHECK(esp_wifi_remote_init(&cfg));
@@ -176,8 +236,16 @@ void app_main(void)
176236
ESP_ERROR_CHECK(esp_wifi_remote_set_config(WIFI_IF_STA, &wifi_config) );
177237
ESP_ERROR_CHECK(esp_wifi_remote_start() );
178238
vTaskDelay(pdMS_TO_TICKS(1000));
239+
uint8_t mac[6];
240+
ESP_ERROR_CHECK(esp_wifi_remote_get_mac(WIFI_IF_STA, mac) );
241+
242+
esp_netif_set_mac(s_wifi_netif, mac);
243+
vTaskDelay(pdMS_TO_TICKS(1000));
244+
245+
esp_netif_action_start(s_wifi_netif, 0, 0, 0);
179246
ESP_ERROR_CHECK(esp_wifi_remote_connect() );
180247

248+
esp_netif_action_connected(s_wifi_netif, 0, 0, 0);
181249
// // Setup global DNS
182250
// esp_netif_dns_info_t dns;
183251
// dns.ip.u_addr.ip4.addr = esp_netif_htonl(CONFIG_EXAMPLE_GLOBAL_DNS);

‎components/eppp_link/examples/rpc/client/main/client.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,19 @@ extern "C" esp_err_t esp_wifi_remote_connect(void)
174174
auto header = rpc.get_header();
175175
return rpc.get_payload<esp_err_t>(CONNECT, header);
176176
}
177+
178+
extern "C" esp_err_t esp_wifi_remote_get_mac(wifi_interface_t ifx, uint8_t mac[6])
179+
{
180+
RpcEngine rpc(tls);
181+
182+
if (rpc.send(GET_MAC, &ifx) != ESP_OK) {
183+
return ESP_FAIL;
184+
}
185+
auto header = rpc.get_header();
186+
auto ret = rpc.get_payload<esp_wifi_remote_mac_t>(GET_MAC, header);
187+
ESP_LOG_BUFFER_HEXDUMP("MAC", ret.mac, 6, ESP_LOG_INFO);
188+
189+
memcpy(mac, ret.mac, 6);
190+
return ret.err;
191+
192+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# This file was generated using idf.py save-defconfig. It can be edited manually.
22
# Espressif IoT Development Framework (ESP-IDF) 5.3.0 Project Minimal Configuration
33
#
4-
CONFIG_UART_ISR_IN_IRAM=y
4+
CONFIG_IDF_TARGET="esp32s3"
5+
CONFIG_ESP_WIFI_SSID="Cermakowifi"
6+
CONFIG_ESP_WIFI_PASSWORD="MojeNovaUfka"
7+
CONFIG_ESP_TLS_INSECURE=y
8+
CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y
9+
CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
510
CONFIG_LWIP_PPP_SUPPORT=y
11+
CONFIG_LWIP_PPP_SERVER_SUPPORT=y
612
CONFIG_LWIP_PPP_VJ_HEADER_COMPRESSION=n
7-
CONFIG_LWIP_PPP_DEBUG_ON=y
813
CONFIG_EPPP_LINK_DEVICE_SPI=y
14+
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096

‎components/eppp_link/examples/rpc/common/esp_wifi_remote.h

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ struct esp_wifi_remote_config {
1616
wifi_config_t conf;
1717
};
1818

19+
struct esp_wifi_remote_mac_t {
20+
esp_err_t err;
21+
uint8_t mac[6];
22+
};
23+
24+
1925
esp_err_t esp_wifi_remote_set_config(wifi_interface_t interface, wifi_config_t *conf);
2026

2127
esp_err_t esp_wifi_remote_set_mode(wifi_mode_t mode);
@@ -26,6 +32,8 @@ esp_err_t esp_wifi_remote_start(void);
2632

2733
esp_err_t esp_wifi_remote_connect(void);
2834

35+
esp_err_t esp_wifi_remote_get_mac(wifi_interface_t ifx, uint8_t mac[6]);
36+
2937
#ifdef __cplusplus
3038
}
3139
#endif

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

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef enum api_id {
1111
SET_CONFIG,
1212
START,
1313
CONNECT,
14+
GET_MAC
1415
} api_id_t;
1516

1617
struct RpcHeader {

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ static esp_err_t perform()
119119
}
120120
case INIT: {
121121
auto req = rpc.get_payload<wifi_init_config_t>(INIT, header);
122-
ESP_LOG_BUFFER_HEXDUMP("cfg", &req, sizeof(req), ESP_LOG_WARN);
123-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
124-
ESP_LOG_BUFFER_HEXDUMP("cfg", &cfg, sizeof(cfg), ESP_LOG_WARN);
122+
req.osi_funcs = &g_wifi_osi_funcs;
123+
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);
125127

126128
auto ret = esp_wifi_init(&req);
127129
if (rpc.send(INIT, &ret) != ESP_OK) {
@@ -141,6 +143,7 @@ static esp_err_t perform()
141143
if (header.size != 0) {
142144
return ESP_FAIL;
143145
}
146+
144147
auto ret = esp_wifi_start();
145148
if (rpc.send(START, &ret) != ESP_OK) {
146149
return ESP_FAIL;
@@ -151,12 +154,22 @@ static esp_err_t perform()
151154
if (header.size != 0) {
152155
return ESP_FAIL;
153156
}
157+
154158
auto ret = esp_wifi_connect();
155159
if (rpc.send(CONNECT, &ret) != ESP_OK) {
156160
return ESP_FAIL;
157161
}
158162
break;
159163
}
164+
case GET_MAC: {
165+
auto req = rpc.get_payload<wifi_interface_t>(GET_MAC, header);
166+
esp_wifi_remote_mac_t resp = {};
167+
resp.err = esp_wifi_get_mac(req, resp.mac);
168+
if (rpc.send(GET_MAC, &resp) != ESP_OK) {
169+
return ESP_FAIL;
170+
}
171+
break;
172+
}
160173

161174

162175
}

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <string.h>
8+
#include <esp_private/wifi.h>
89
#include "freertos/FreeRTOS.h"
910
#include "freertos/event_groups.h"
1011
#include "esp_system.h"
@@ -113,6 +114,29 @@ void wifi_init_sta(void)
113114

114115
esp_err_t server_init(void);
115116

117+
static eppp_channel_fn_t s_tx;
118+
static esp_netif_t *s_ppp_netif;
119+
120+
static esp_err_t netif_recv(void *h, void *buffer, size_t len)
121+
{
122+
// printf("recv %d\n", len);
123+
ESP_LOG_BUFFER_HEXDUMP("cfg", buffer, len, ESP_LOG_WARN);
124+
return esp_wifi_internal_tx(WIFI_IF_STA, buffer, len);
125+
}
126+
127+
static esp_err_t wifi_recv(void *buffer, uint16_t len, void *eb)
128+
{
129+
printf("send %d\n", len);
130+
if (s_tx) {
131+
printf("send %d\n", len);
132+
esp_err_t ret = s_tx(s_ppp_netif, buffer, len);
133+
esp_wifi_internal_free_rx_buffer(eb);
134+
return ret;
135+
}
136+
return ESP_OK;
137+
}
138+
139+
116140
void app_main(void)
117141
{
118142

@@ -129,11 +153,19 @@ void app_main(void)
129153
ESP_ERROR_CHECK(esp_event_loop_create_default());
130154
eppp_config_t config = EPPP_DEFAULT_SERVER_CONFIG();
131155
config.transport = EPPP_TRANSPORT_SPI;
132-
esp_netif_t *eppp_netif = eppp_listen(&config);
133-
if (eppp_netif == NULL) {
156+
s_ppp_netif = eppp_listen(&config);
157+
if (s_ppp_netif == NULL) {
134158
ESP_LOGE(TAG, "Failed to setup connection");
135159
return ;
136160
}
137161

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);
164+
eppp_add_channel(1, &s_tx, netif_recv);
165+
138166
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+
139171
}

‎components/eppp_link/examples/rpc/server/sdkconfig.defaults

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
CONFIG_UART_ISR_IN_IRAM=y
2-
CONFIG_LWIP_IP_FORWARD=y
3-
CONFIG_LWIP_IPV4_NAPT=y
41
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096
52
CONFIG_LWIP_PPP_SUPPORT=y
63
CONFIG_LWIP_PPP_SERVER_SUPPORT=y

‎components/eppp_link/include/eppp_link.h

+4
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ esp_err_t eppp_netif_stop(esp_netif_t *netif, TickType_t stop_timeout);
103103
esp_err_t eppp_netif_start(esp_netif_t *netif);
104104

105105
esp_err_t eppp_perform(esp_netif_t *netif);
106+
107+
typedef esp_err_t (*eppp_channel_fn_t)(void *h, void *buffer, size_t len);
108+
109+
esp_err_t eppp_add_channel(int nr, eppp_channel_fn_t *tx, const eppp_channel_fn_t rx);

0 commit comments

Comments
 (0)
Please sign in to comment.