Skip to content

Commit 125492e

Browse files
committed
fix(eppp): uart changes + simple channel operations
1 parent d129f7f commit 125492e

File tree

5 files changed

+105
-35
lines changed

5 files changed

+105
-35
lines changed

components/eppp_link/eppp_spi.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ static esp_err_t transmit(void *h, void *buffer, size_t len)
115115
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
116116
static esp_err_t transmit_channel(esp_netif_t *netif, int channel, void *buffer, size_t len)
117117
{
118-
// printf("transmit_channel %d %p %zu\n", channel, buffer, len);
119118
struct eppp_handle *handle = esp_netif_get_io_driver(netif);
120119
struct eppp_spi *spi_handle = __containerof(handle, struct eppp_spi, parent);;
121120
return transmit_generic(spi_handle, channel, buffer, len);

components/eppp_link/eppp_uart.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ struct eppp_uart {
2929

3030
struct header {
3131
uint8_t magic;
32+
uint8_t channel;
3233
uint8_t check;
3334
uint16_t size;
3435
} __attribute__((packed));
3536

36-
static esp_err_t transmit(void *h, void *buffer, size_t len)
37+
static esp_err_t transmit_generic(struct eppp_uart *handle, int channel, void *buffer, size_t len)
3738
{
38-
struct eppp_handle *common = h;
39-
struct eppp_uart *handle = __containerof(common, struct eppp_uart, parent);
4039
#ifndef CONFIG_EPPP_LINK_USES_PPP
4140
static uint8_t out_buf[PACKET_SIZE] = {};
4241
struct header *head = (void *)out_buf;
4342
head->magic = HEADER_MAGIC;
4443
head->check = 0;
44+
head->channel = channel;
4545
head->size = len;
46-
head->check = HEADER_MAGIC + len + (len >> 8);
46+
head->check = HEADER_MAGIC + len + (len >> 8) + channel;
4747
memcpy(out_buf + sizeof(struct header), buffer, len);
4848
ESP_LOG_BUFFER_HEXDUMP("ppp_uart_send", out_buf, len + sizeof(struct header), ESP_LOG_DEBUG);
4949
uart_write_bytes(handle->uart_port, out_buf, len + sizeof(struct header));
@@ -54,6 +54,22 @@ static esp_err_t transmit(void *h, void *buffer, size_t len)
5454
return ESP_OK;
5555
}
5656

57+
static esp_err_t transmit(void *h, void *buffer, size_t len)
58+
{
59+
struct eppp_handle *handle = h;
60+
struct eppp_uart *uart_handle = __containerof(handle, struct eppp_uart, parent);
61+
return transmit_generic(uart_handle, 0, buffer, len);
62+
}
63+
64+
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
65+
static esp_err_t transmit_channel(esp_netif_t *netif, int channel, void *buffer, size_t len)
66+
{
67+
struct eppp_handle *handle = esp_netif_get_io_driver(netif);
68+
struct eppp_uart *uart_handle = __containerof(handle, struct eppp_uart, parent);
69+
return transmit_generic(uart_handle, channel, buffer, len);
70+
}
71+
#endif
72+
5773
static esp_err_t init_uart(struct eppp_uart *h, struct eppp_config_uart_s *config)
5874
{
5975
h->uart_port = config->port;
@@ -78,7 +94,7 @@ static void deinit_uart(struct eppp_uart *h)
7894
}
7995

8096
#ifndef CONFIG_EPPP_LINK_USES_PPP
81-
static void process_packet(esp_netif_t *netif, uint8_t *buffer, size_t len)
97+
static void process_packet(esp_netif_t *netif, struct eppp_uart *h, uint8_t *buffer, size_t len)
8298
{
8399
static uint8_t in_buf[PACKET_SIZE + UART_BUF_SIZE] = {};
84100
static size_t current_offset = 0;
@@ -104,7 +120,7 @@ static void process_packet(esp_netif_t *netif, uint8_t *buffer, size_t len)
104120
continue;
105121
}
106122

107-
uint8_t calculated_check = HEADER_MAGIC + head->size + (head->size >> 8);
123+
uint8_t calculated_check = HEADER_MAGIC + head->size + (head->size >> 8) + head->channel;
108124
if (head->check != calculated_check) {
109125
ESP_LOGW(TAG, "Checksum mismatch: expected 0x%04x, got 0x%04x", calculated_check, head->check);
110126
// Discard this header
@@ -115,6 +131,7 @@ static void process_packet(esp_netif_t *netif, uint8_t *buffer, size_t len)
115131

116132
// Check if we have the complete packet
117133
uint16_t payload_size = head->size;
134+
int channel = head->channel;
118135
size_t total_packet_size = sizeof(struct header) + payload_size;
119136

120137
if (payload_size > MAX_PAYLOAD) {
@@ -131,8 +148,16 @@ static void process_packet(esp_netif_t *netif, uint8_t *buffer, size_t len)
131148
break;
132149
}
133150

134-
// Got a complete packet, pass it to network
135-
esp_netif_receive(netif, in_buf + sizeof(struct header), payload_size, NULL);
151+
// Got a complete packet, pass it to the network (or to a custom channel handler)
152+
if (channel == 0) {
153+
esp_netif_receive(netif, in_buf + sizeof(struct header), payload_size, NULL);
154+
} else {
155+
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
156+
if (h->parent.channel_rx) {
157+
h->parent.channel_rx(netif, channel, in_buf + sizeof(struct header), payload_size);
158+
}
159+
#endif
160+
}
136161

137162
// Move any remaining data to the beginning of the buffer
138163
if (current_offset > total_packet_size) {
@@ -166,9 +191,10 @@ esp_err_t eppp_perform(esp_netif_t *netif)
166191
len = uart_read_bytes(h->uart_port, buffer, UART_BUF_SIZE, 0);
167192
ESP_LOG_BUFFER_HEXDUMP("ppp_uart_recv", buffer, len, ESP_LOG_DEBUG);
168193
#ifdef CONFIG_EPPP_LINK_USES_PPP
194+
// TODO: cannot use PPP and channels at the same time
169195
esp_netif_receive(netif, buffer, len, NULL);
170196
#else
171-
process_packet(netif, buffer, len);
197+
process_packet(netif, h, buffer, len);
172198
#endif
173199
}
174200
} else {
@@ -198,6 +224,9 @@ eppp_transport_handle_t eppp_uart_init(struct eppp_config_uart_s *config)
198224
__attribute__((unused)) esp_err_t ret = ESP_OK;
199225
struct eppp_uart *h = calloc(1, sizeof(struct eppp_uart));
200226
ESP_RETURN_ON_FALSE(h, NULL, TAG, "Failed to allocate eppp_handle");
227+
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
228+
h->parent.channel_tx = transmit_channel;
229+
#endif
201230
h->parent.base.post_attach = post_attach;
202231
ESP_GOTO_ON_ERROR(init_uart(h, config), err, TAG, "Failed to init UART");
203232
return &h->parent;

components/eppp_link/examples/host/main/app_main.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414
#include "esp_netif.h"
1515
#include "eppp_link.h"
1616
#include "esp_log.h"
17+
#include "esp_check.h"
1718
#include "mqtt_client.h"
1819
#include "console_ping.h"
20+
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
21+
#if __has_include("esp_wifi_remote.h")
22+
#include "esp_wifi_remote.h"
23+
#define HAVE_WIFI_REMOTE
24+
#endif
25+
#endif
26+
1927

2028
void register_iperf(void);
2129

@@ -87,19 +95,43 @@ static void mqtt_app_start(void)
8795
esp_mqtt_client_start(client);
8896
}
8997
#endif // MQTT
98+
9099
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
100+
typedef enum {
101+
UNKNOWN,
102+
INIT,
103+
CONNECTED,
104+
DISCONNECTED,
105+
ERROR,
106+
} state_t;
107+
static state_t s_state = UNKNOWN;
108+
static eppp_channel_fn_t s_transmit[CHANNEL_ID_MAX] = {NULL};
91109
static esp_err_t recv(esp_netif_t *netif, int nr, void *buffer, size_t len)
92110
{
93-
printf("recv %d %p %zu\n", nr, buffer, len);
111+
char data[32];
112+
if (len < sizeof(data)) {
113+
memcpy(data, buffer, len);
114+
printf("Received [%d] %*s\n", (int)len, (int)len, data);
115+
return ESP_OK;
116+
}
94117
return ESP_OK;
95118
}
119+
120+
static void task_transmit(void *arg)
121+
{
122+
int counter = 0;
123+
esp_netif_t *eppp_netif = (esp_netif_t *)arg;
124+
while (1) {
125+
char data[32];
126+
sprintf(data, "Message from host #%d", counter++);
127+
s_transmit(eppp_netif, 1, data, strlen(data));
128+
vTaskDelay(1000 / portTICK_PERIOD_MS);
129+
}
130+
}
96131
#endif
97132

98133
void app_main(void)
99134
{
100-
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
101-
static eppp_channel_fn_t transmit = NULL;
102-
#endif
103135
ESP_LOGI(TAG, "[APP] Startup..");
104136
ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
105137
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
@@ -123,8 +155,8 @@ void app_main(void)
123155
config.spi.freq = 4000000;
124156
#elif CONFIG_EPPP_LINK_DEVICE_UART
125157
config.transport = EPPP_TRANSPORT_UART;
126-
config.uart.tx_io = 17;
127-
config.uart.rx_io = 18;
158+
config.uart.tx_io = 4;
159+
config.uart.rx_io = 5;
128160
// config.uart.baud = CONFIG_EXAMPLE_UART_BAUDRATE;
129161
#elif CONFIG_EPPP_LINK_DEVICE_ETH
130162
config.transport = EPPP_TRANSPORT_ETHERNET;
@@ -166,11 +198,9 @@ void app_main(void)
166198
ESP_ERROR_CHECK(console_cmd_start());
167199

168200
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
169-
eppp_add_channels(eppp_netif, 1, &transmit, recv);
170-
while (1) {
171-
transmit(eppp_netif, 1, "Hi three", 3);
172-
vTaskDelay(1000 / portTICK_PERIOD_MS);
173-
}
201+
ESP_RETURN_ON_FALSE(eppp_add_channels(eppp_netif, 1, &s_transmit, recv) == ESP_OK,, TAG, "Failed to add channels");
202+
ESP_RETURN_ON_FALSE(s_transmit,, TAG, "Channel tx function is not set");
203+
xTaskCreate(task_transmit, "task_transmit", 2048, eppp_netif, 5, NULL);
174204
#endif
175205
#if CONFIG_EXAMPLE_MQTT
176206
mqtt_app_start();
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
dependencies:
2-
espressif/iperf-cmd: "^0.1.1"
2+
espressif/iperf-cmd: ^0.1.1
33
espressif/eppp_link:
4-
version: "*"
5-
override_path: "../../.."
4+
version: '*'
5+
override_path: ../../..
66
console_cmd_ping:
7-
version: "*"
7+
version: '*'
8+
espressif/esp_wifi_remote:
9+
version: '*'
10+
override_path: /home/david/repos/esp-wifi-remote/components/esp_wifi_remote

components/eppp_link/examples/slave/main/eppp_slave.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "esp_wifi.h"
1212
#include "esp_event.h"
1313
#include "esp_log.h"
14+
#include "esp_check.h"
1415
#include "nvs_flash.h"
1516
#include "eppp_link.h"
1617

@@ -114,18 +115,24 @@ void init_network_interface(void)
114115

115116
#endif // SoC WiFi capable chip
116117

118+
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
117119
static esp_err_t recv(esp_netif_t *netif, int nr, void *buffer, size_t len)
118120
{
119121
char data[32];
120122
if (len < sizeof(data)) {
121123
memcpy(data, buffer, len);
122-
printf("Received #[%d] %s\n", nr, data);
124+
printf("Received [%d] %*s\n", (int)len, (int)len, data);
125+
return ESP_OK;
123126
}
124-
return ESP_OK;
127+
return ESP_ERR_INVALID_SIZE;
125128
}
129+
#endif // CONFIG_EPPP_LINK_CHANNELS_SUPPORT
130+
126131
void app_main(void)
127132
{
133+
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
128134
static eppp_channel_fn_t transmit = NULL;
135+
#endif // CONFIG_EPPP_LINK_CHANNELS_SUPPORT
129136
//Initialize NVS
130137
esp_err_t ret = nvs_flash_init();
131138
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
@@ -151,8 +158,8 @@ void app_main(void)
151158
config.spi.freq = 1000000;
152159
#elif CONFIG_EPPP_LINK_DEVICE_UART
153160
config.transport = EPPP_TRANSPORT_UART;
154-
config.uart.tx_io = 18;
155-
config.uart.rx_io = 17;
161+
config.uart.tx_io = 4;
162+
config.uart.rx_io = 5;
156163
// config.uart.baud = CONFIG_EXAMPLE_UART_BAUDRATE;
157164
#elif CONFIG_EPPP_LINK_DEVICE_SDIO
158165
config.transport = EPPP_TRANSPORT_SDIO;
@@ -164,13 +171,15 @@ void app_main(void)
164171
return ;
165172
}
166173
ESP_ERROR_CHECK(esp_netif_napt_enable(eppp_netif));
167-
ret = eppp_add_channels(eppp_netif, 1, &transmit, recv);
168-
if (ret != ESP_OK) {
169-
ESP_LOGE(TAG, "Failed to add channels");
170-
return ;
171-
}
174+
#ifdef CONFIG_EPPP_LINK_CHANNELS_SUPPORT
175+
ESP_RETURN_ON_FALSE(eppp_add_channels(eppp_netif, 1, &transmit, recv) == ESP_OK,, TAG, "Failed to add channels");
176+
ESP_RETURN_ON_FALSE(transmit,, TAG, "Channel tx function is not set");
177+
int counter = 0;
172178
while (1) {
173-
transmit(eppp_netif, 1, "SLAve three", 3);
179+
char data[32];
180+
sprintf(data, "Message from slave #%d", counter++);
181+
transmit(eppp_netif, 1, data, strlen(data));
174182
vTaskDelay(1000 / portTICK_PERIOD_MS);
175183
}
184+
#endif // CONFIG_EPPP_LINK_CHANNELS_SUPPORT
176185
}

0 commit comments

Comments
 (0)