Skip to content

Commit 1f3e55d

Browse files
committed
feat(eppp): API to add channels
1 parent 6a49459 commit 1f3e55d

File tree

7 files changed

+88
-18
lines changed

7 files changed

+88
-18
lines changed

components/eppp_link/eppp_link.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,13 @@ void eppp_close(esp_netif_t *netif)
361361
eppp_deinit(netif);
362362
remove_handlers();
363363
}
364+
365+
esp_err_t eppp_add_channels(esp_netif_t *netif, int nr, eppp_channel_fn_t *tx, const eppp_channel_fn_t rx)
366+
{
367+
ESP_RETURN_ON_FALSE(netif != NULL && nr > 0 && nr < NR_OF_CHANNELS && tx != NULL && rx != NULL, ESP_ERR_INVALID_ARG, TAG, "Invalid arguments");
368+
struct eppp_handle *h = esp_netif_get_io_driver(netif);
369+
ESP_RETURN_ON_FALSE(h != NULL && h->channel_tx != NULL, ESP_ERR_INVALID_STATE, TAG, "Transport not initialized");
370+
*tx = h->channel_tx;
371+
h->channel_rx = rx;
372+
return ESP_OK;
373+
}

components/eppp_link/eppp_sdio.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define SDIO_PACKET_SIZE SDIO_ALIGN(MAX_SDIO_PAYLOAD + 4)
1212
#define PPP_SOF 0x7E
1313

14-
#define NR_OF_CHANNELS 2
1514

1615
// Interrupts and registers
1716
#define SLAVE_INTR 0

components/eppp_link/eppp_spi.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string.h>
77
#include <stdint.h>
88
#include <inttypes.h>
9+
#include "sdkconfig.h"
910
#include "esp_log.h"
1011
#include "esp_netif.h"
1112
#include "esp_check.h"
@@ -24,18 +25,21 @@
2425

2526
#define MAX_PAYLOAD 1500
2627
#define MIN_TRIGGER_US 20
27-
#define SPI_HEADER_MAGIC 0x1234
28+
#define PPP_SOF 0x7E
29+
#define SPI_HEADER_MAGIC PPP_SOF
2830
#define SPI_ALIGN(size) (((size) + 3U) & ~(3U))
2931
#define TRANSFER_SIZE SPI_ALIGN((MAX_PAYLOAD + 6))
3032
#define NEXT_TRANSACTION_SIZE(a,b) (((a)>(b))?(a):(b)) /* next transaction: whichever is bigger */
3133

3234
struct packet {
3335
size_t len;
3436
uint8_t *data;
37+
int channel;
3538
};
3639

3740
struct header {
38-
uint16_t magic;
41+
uint8_t magic;
42+
uint8_t channel;
3943
uint16_t size;
4044
uint16_t next_size;
4145
uint16_t check;
@@ -65,12 +69,10 @@ struct eppp_spi {
6569
esp_timer_handle_t timer;
6670
};
6771

68-
static esp_err_t transmit(void *h, void *buffer, size_t len)
72+
static esp_err_t transmit_generic(struct eppp_spi *handle, int channel, void *buffer, size_t len)
6973
{
70-
struct eppp_handle *common = h;
71-
struct eppp_spi *handle = __containerof(common, struct eppp_spi, parent);;
72-
#if CONFIG_EPPP_LINK_DEVICE_SPI
73-
struct packet buf = { };
74+
75+
struct packet buf = { .channel = channel };
7476
uint8_t *current_buffer = buffer;
7577
size_t remaining = len;
7678
do { // TODO(IDF-9194): Refactor this loop to allocate only once and perform
@@ -100,14 +102,24 @@ static esp_err_t transmit(void *h, void *buffer, size_t len)
100102
}
101103
gpio_set_level(handle->gpio_intr, 0);
102104
}
103-
104-
#elif CONFIG_EPPP_LINK_DEVICE_UART
105-
ESP_LOG_BUFFER_HEXDUMP("ppp_uart_send", buffer, len, ESP_LOG_WARN);
106-
uart_write_bytes(handle->uart_port, buffer, len);
107-
#endif // DEVICE UART or SPI
108105
return ESP_OK;
109106
}
110107

108+
static esp_err_t transmit(void *h, void *buffer, size_t len)
109+
{
110+
struct eppp_handle *handle = h;
111+
struct eppp_spi *spi_handle = __containerof(handle, struct eppp_spi, parent);;
112+
return transmit_generic(spi_handle, 0, buffer, len);
113+
}
114+
115+
static esp_err_t transmit_channel(esp_netif_t *netif, int channel, void *buffer, size_t len)
116+
{
117+
// printf("transmit_channel %d %p %zu\n", channel, buffer, len);
118+
struct eppp_handle *handle = esp_netif_get_io_driver(netif);
119+
struct eppp_spi *spi_handle = __containerof(handle, struct eppp_spi, parent);;
120+
return transmit_generic(spi_handle, channel, buffer, len);
121+
}
122+
111123
static void IRAM_ATTR timer_callback(void *arg)
112124
{
113125
struct eppp_spi *h = arg;
@@ -339,6 +351,7 @@ esp_err_t eppp_perform(esp_netif_t *netif)
339351
if (h->outbound.len <= h->transaction_size && allow_test_tx == false) {
340352
// sending outbound
341353
head->size = h->outbound.len;
354+
head->channel = h->outbound.channel;
342355
if (h->outbound.len > 0) {
343356
memcpy(out_buf + sizeof(struct header), h->outbound.data, h->outbound.len);
344357
free(h->outbound.data);
@@ -355,6 +368,7 @@ esp_err_t eppp_perform(esp_netif_t *netif)
355368
} else {
356369
// outbound is bigger, need to transmit in another transaction (keep this empty)
357370
head->size = 0;
371+
head->channel = 0;
358372
}
359373
next_tx_size = head->next_size = h->outbound.len;
360374
head->magic = SPI_HEADER_MAGIC;
@@ -367,17 +381,25 @@ esp_err_t eppp_perform(esp_netif_t *netif)
367381
}
368382
head = (void *)in_buf;
369383
uint16_t check = esp_rom_crc16_le(0, in_buf, sizeof(struct header) - sizeof(uint16_t));
370-
if (check != head->check || head->magic != SPI_HEADER_MAGIC) {
384+
if (check != head->check || head->magic != SPI_HEADER_MAGIC || head->channel >= NR_OF_CHANNELS) {
371385
h->transaction_size = 0; // need to start with HEADER only transaction
372386
if (allow_test_tx) {
373387
return ESP_OK;
374388
}
375-
ESP_LOGE(TAG, "Wrong checksum or magic");
389+
ESP_LOGE(TAG, "Wrong checksum, magic, or channel: %x %x %x", check, head->magic, head->channel);
376390
return ESP_FAIL;
377391
}
378392
if (head->size > 0) {
379393
ESP_LOG_BUFFER_HEXDUMP(TAG, in_buf + sizeof(struct header), head->size, ESP_LOG_VERBOSE);
380-
esp_netif_receive(netif, in_buf + sizeof(struct header), head->size, NULL);
394+
if (head->channel == 0) {
395+
esp_netif_receive(netif, in_buf + sizeof(struct header), head->size, NULL);
396+
} else {
397+
// printf("channel receive %d\n", head->channel);
398+
// printf("channel_rx %p\n", h->parent.channel_rx);
399+
if (h->parent.channel_rx) {
400+
h->parent.channel_rx(netif, head->channel, in_buf + sizeof(struct header), head->size);
401+
}
402+
}
381403
}
382404
h->transaction_size = NEXT_TRANSACTION_SIZE(next_tx_size, head->next_size);
383405
return ESP_OK;
@@ -413,6 +435,7 @@ eppp_transport_handle_t eppp_spi_init(struct eppp_config_spi_s *config)
413435
__attribute__((unused)) esp_err_t ret = ESP_OK;
414436
struct eppp_spi *h = calloc(1, sizeof(struct eppp_spi));
415437
ESP_RETURN_ON_FALSE(h, NULL, TAG, "Failed to allocate eppp_handle");
438+
h->parent.channel_tx = transmit_channel;
416439
h->is_master = config->is_master;
417440
h->parent.base.post_attach = post_attach;
418441
h->out_queue = xQueueCreate(CONFIG_EPPP_LINK_PACKET_QUEUE_SIZE, sizeof(struct packet));

components/eppp_link/eppp_transport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
#pragma once
77
#include "esp_netif_types.h"
88

9+
#define NR_OF_CHANNELS 2
10+
911
struct eppp_handle {
1012
esp_netif_driver_base_t base;
1113
eppp_type_t role;
1214
bool stop;
1315
bool exited;
1416
bool netif_stop;
17+
#if NR_OF_CHANNELS > 0
18+
eppp_channel_fn_t channel_tx;
19+
eppp_channel_fn_t channel_rx;
20+
#endif
1521
};
1622

1723
esp_err_t eppp_check_connection(esp_netif_t *netif);

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,15 @@ static void mqtt_app_start(void)
8787
esp_mqtt_client_start(client);
8888
}
8989
#endif // MQTT
90-
90+
static esp_err_t recv(esp_netif_t *netif, int nr, void *buffer, size_t len)
91+
{
92+
printf("recv %d %p %zu\n", nr, buffer, len);
93+
return ESP_OK;
94+
}
9195

9296
void app_main(void)
9397
{
98+
static eppp_channel_fn_t transmit = NULL;
9499
ESP_LOGI(TAG, "[APP] Startup..");
95100
ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
96101
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
@@ -156,6 +161,11 @@ void app_main(void)
156161
// start console REPL
157162
ESP_ERROR_CHECK(console_cmd_start());
158163

164+
eppp_add_channels(eppp_netif, 1, &transmit, recv);
165+
while (1) {
166+
transmit(eppp_netif, 1, "Hi three", 3);
167+
vTaskDelay(1000 / portTICK_PERIOD_MS);
168+
}
159169
#if CONFIG_EXAMPLE_MQTT
160170
mqtt_app_start();
161171
#endif

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,18 @@ void init_network_interface(void)
114114

115115
#endif // SoC WiFi capable chip
116116

117+
static esp_err_t recv(esp_netif_t *netif, int nr, void *buffer, size_t len)
118+
{
119+
char data[32];
120+
if (len < sizeof(data)) {
121+
memcpy(data, buffer, len);
122+
printf("Received #[%d] %s\n", nr, data);
123+
}
124+
return ESP_OK;
125+
}
117126
void app_main(void)
118127
{
128+
static eppp_channel_fn_t transmit = NULL;
119129
//Initialize NVS
120130
esp_err_t ret = nvs_flash_init();
121131
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
@@ -153,6 +163,14 @@ void app_main(void)
153163
ESP_LOGE(TAG, "Failed to setup connection");
154164
return ;
155165
}
156-
vTaskDelay(10000 / portTICK_PERIOD_MS);
157166
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+
}
172+
while (1) {
173+
transmit(eppp_netif, 1, "SLAve three", 3);
174+
vTaskDelay(1000 / portTICK_PERIOD_MS);
175+
}
158176
}

components/eppp_link/include/eppp_link.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,7 @@ esp_err_t eppp_netif_start(esp_netif_t *netif);
197197
void eppp_netif_deinit(esp_netif_t *netif);
198198

199199
esp_err_t eppp_perform(esp_netif_t *netif);
200+
201+
typedef esp_err_t (*eppp_channel_fn_t)(esp_netif_t *netif, int nr, void *buffer, size_t len);
202+
203+
esp_err_t eppp_add_channels(esp_netif_t *netif, int nr, eppp_channel_fn_t *tx, const eppp_channel_fn_t rx);

0 commit comments

Comments
 (0)