Skip to content

Commit

Permalink
fix(eppp_link): Use CRC instead of checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Jan 29, 2024
1 parent f336fbf commit c891b47
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion components/eppp_link/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ESP PPP Link component (eppp_link)

The component provides a general purpose connectivity engine between two microcontrollers, one acting as PPP server (slave), the other one as PPP client (host).
This component could be used for extending network using physical serial connection. Applications could vary from providing PRC engine for multiprocessor solutions to serial connection to POSIX machine. Typical application is a WiFi connectivity provider for chips that do not have WiFi
This component could be used for extending network using physical serial connection. Applications could vary from providing PRC engine for multiprocessor solutions to serial connection to POSIX machine. This uses a standard PPP protocol to negotiate IP addresses and networking, so standard PPP toolset could be used, e.g. a `pppd` service on linux. Typical application is a WiFi connectivity provider for chips that do not have WiFi

## Typical application

Expand Down
58 changes: 27 additions & 31 deletions components/eppp_link/eppp_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "driver/spi_slave.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include "esp_rom_crc.h"
#elif CONFIG_EPPP_LINK_DEVICE_UART
#include "driver/uart.h"
#endif
Expand Down Expand Up @@ -69,16 +70,16 @@ static esp_err_t transmit(void *h, void *buffer, size_t len)
buf.data = malloc(batch);
if (buf.data == NULL) {
ESP_LOGE(TAG, "Failed to allocate packet");
return ESP_FAIL;
return ESP_ERR_NO_MEM;
}
buf.len = batch;
remaining -= batch;
memcpy(buf.data, current_buffer, batch);
current_buffer += batch;
BaseType_t ret = xQueueSend(handle->out_queue, &buf, pdMS_TO_TICKS(10));
BaseType_t ret = xQueueSend(handle->out_queue, &buf, 0);
if (ret != pdTRUE) {
ESP_LOGE(TAG, "Failed to queue packet to slave!");
return ESP_FAIL;
return ESP_ERR_NO_MEM;
}
} while (remaining > 0);
#elif CONFIG_EPPP_LINK_DEVICE_UART
Expand Down Expand Up @@ -173,6 +174,9 @@ static esp_netif_t *netif_init(eppp_type_t role)
ESP_LOGE(TAG, "Failed to create esp_netif");
#if CONFIG_EPPP_LINK_DEVICE_SPI
vQueueDelete(h->out_queue);
if (h->ready_semaphore) {
vSemaphoreDelete(h->ready_semaphore);
}
#endif
free(h);
return NULL;
Expand All @@ -181,13 +185,13 @@ static esp_netif_t *netif_init(eppp_type_t role)

}

esp_err_t eppp_netif_stop(esp_netif_t *netif, TickType_t stop_timeout)
esp_err_t eppp_netif_stop(esp_netif_t *netif, int stop_timeout_ms)
{
esp_netif_action_disconnected(netif, 0, 0, 0);
esp_netif_action_stop(netif, 0, 0, 0);
struct eppp_handle *h = esp_netif_get_io_driver(netif);
for (int wait = 0; wait < 100; wait++) {
vTaskDelay(stop_timeout / 100);
vTaskDelay(pdMS_TO_TICKS(stop_timeout_ms) / 100);
if (h->netif_stop) {
break;
}
Expand Down Expand Up @@ -260,7 +264,9 @@ static void on_ip_event(void *arg, esp_event_base_t base, int32_t event_id, void

#if CONFIG_EPPP_LINK_DEVICE_SPI

#define TRANSFER_SIZE (MAX_PAYLOAD + 4)
#define SPI_ALIGN(size) (((size) + 3U) & ~(3U))

#define TRANSFER_SIZE SPI_ALIGN((MAX_PAYLOAD + 4))
#define SHORT_PAYLOAD (48)
#define CONTROL_SIZE (SHORT_PAYLOAD + 4)

Expand All @@ -282,7 +288,7 @@ struct header {
} __attribute__((packed));
};
uint8_t magic;
uint8_t checksum;
uint8_t crc;
} __attribute__((packed));

static void IRAM_ATTR gpio_isr_handler(void *arg)
Expand Down Expand Up @@ -322,7 +328,7 @@ static esp_err_t init_master(struct eppp_config_spi_s *config, esp_netif_t *neti
bus_cfg.sclk_io_num = config->sclk;
bus_cfg.quadwp_io_num = -1;
bus_cfg.quadhd_io_num = -1;
bus_cfg.max_transfer_sz = 2000;
bus_cfg.max_transfer_sz = TRANSFER_SIZE;
bus_cfg.flags = 0;
bus_cfg.intr_flags = 0;

Expand Down Expand Up @@ -435,15 +441,15 @@ typedef esp_err_t (*perform_transaction_t)(union transaction *t, struct eppp_han

static void set_transaction_master(union transaction *t, size_t len, const void *tx_buffer, void *rx_buffer, int gpio_intr)
{
t->master.length = len * 8;
t->master.length = SPI_ALIGN(len) * 8;
t->master.tx_buffer = tx_buffer;
t->master.rx_buffer = rx_buffer;
}

static void set_transaction_slave(union transaction *t, size_t len, const void *tx_buffer, void *rx_buffer, int gpio_intr)
{
t->slave.user = (void *)gpio_intr;
t->slave.length = len * 8;
t->slave.length = SPI_ALIGN(len) * 8;
t->slave.tx_buffer = tx_buffer;
t->slave.rx_buffer = rx_buffer;
}
Expand Down Expand Up @@ -488,7 +494,7 @@ esp_err_t eppp_perform(esp_netif_t *netif)
size_t out_long_payload = 0;
head->magic = FRAME_OUT_CTRL_EX;
head->size = 0;
head->checksum = 0;
head->crc = 0;
BaseType_t tx_queue_stat = xQueueReceive(h->out_queue, &buf, 0);
if (tx_queue_stat == pdTRUE && buf.data) {
if (buf.len > SHORT_PAYLOAD) {
Expand All @@ -506,20 +512,15 @@ esp_err_t eppp_perform(esp_netif_t *netif)
}
memset(&t, 0, sizeof(t));
set_transaction(&t, CONTROL_SIZE, out_buf, in_buf, h->gpio_intr);
for (int i = 0; i < sizeof(struct header) - 1; ++i) {
head->checksum += out_buf[i];
}
head->crc = esp_rom_crc8_le(0, out_buf, sizeof(struct header) - 1);
esp_err_t ret = perform_transaction(&t, h);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "spi_device_transmit failed");
return ESP_FAIL;
}
head = (void *)in_buf;
uint8_t checksum = 0;
for (int i = 0; i < sizeof(struct header) - 1; ++i) {
checksum += in_buf[i];
}
if (checksum != head->checksum) {
uint8_t crc = esp_rom_crc8_le(0, in_buf, sizeof(struct header) - 1);
if (crc != head->crc) {
ESP_LOGE(TAG, "Wrong checksum");
return ESP_FAIL;
}
Expand All @@ -543,10 +544,8 @@ esp_err_t eppp_perform(esp_netif_t *netif)
head = (void *)out_buf;
head->magic = FRAME_OUT_DATA;
head->size = out_long_payload;
head->checksum = 0;
for (int i = 0; i < sizeof(struct header) - 1; ++i) {
head->checksum += out_buf[i];
}
head->crc = 0;
head->crc = esp_rom_crc8_le(0, out_buf, sizeof(struct header) - 1);
if (head->size > 0) {
memcpy(out_buf + sizeof(struct header), buf.data, buf.len);
// ESP_LOG_BUFFER_HEXDUMP(TAG, out_buf + sizeof(struct header), head->size, ESP_LOG_INFO);
Expand All @@ -562,11 +561,8 @@ esp_err_t eppp_perform(esp_netif_t *netif)
return ESP_FAIL;
}
head = (void *)in_buf;
checksum = 0;
for (int i = 0; i < sizeof(struct header) - 1; ++i) {
checksum += in_buf[i];
}
if (checksum != head->checksum) {
crc = esp_rom_crc8_le(0, in_buf, sizeof(struct header) - 1);
if (crc != head->crc) {
ESP_LOGE(TAG, "Wrong checksum");
return ESP_FAIL;
}
Expand Down Expand Up @@ -708,7 +704,7 @@ esp_netif_t *eppp_init(eppp_type_t role, eppp_config_t *config)
return netif;
}

esp_netif_t *eppp_open(eppp_type_t role, eppp_config_t *config, TickType_t connect_timeout)
esp_netif_t *eppp_open(eppp_type_t role, eppp_config_t *config, int connect_timeout_ms)
{
#if CONFIG_EPPP_LINK_DEVICE_UART
if (config->transport != EPPP_TRANSPORT_UART) {
Expand Down Expand Up @@ -759,7 +755,7 @@ esp_netif_t *eppp_open(eppp_type_t role, eppp_config_t *config, TickType_t conne
return NULL;
}
ESP_LOGI(TAG, "Waiting for IP address %d", netif_cnt);
EventBits_t bits = xEventGroupWaitBits(s_event_group, CONNECT_BITS << (netif_cnt * 2), pdFALSE, pdFALSE, connect_timeout);
EventBits_t bits = xEventGroupWaitBits(s_event_group, CONNECT_BITS << (netif_cnt * 2), pdFALSE, pdFALSE, pdMS_TO_TICKS(connect_timeout_ms));
if (bits & (CONNECTION_FAILED << (netif_cnt * 2))) {
ESP_LOGE(TAG, "Connection failed!");
eppp_close(netif);
Expand All @@ -782,7 +778,7 @@ esp_netif_t *eppp_listen(eppp_config_t *config)
void eppp_close(esp_netif_t *netif)
{
struct eppp_handle *h = esp_netif_get_io_driver(netif);
if (eppp_netif_stop(netif, pdMS_TO_TICKS(60000)) != ESP_OK) {
if (eppp_netif_stop(netif, 60000) != ESP_OK) {
ESP_LOGE(TAG, "Network didn't exit cleanly");
}
h->stop = true;
Expand Down
20 changes: 13 additions & 7 deletions components/eppp_link/include/eppp_link.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
.sclk = 12, \
.cs = 10, \
.intr = 2, \
.freq = 20*1000*1000, \
.freq = 16*1000*1000, \
.input_delay_ns = 0, \
.cs_ena_pretrans = 0, \
.cs_ena_posttrans = 0, \
}, \
.uart = { \
.port = 1, \
Expand All @@ -32,8 +35,8 @@
.priority = 18, \
}, \
. ppp = { \
.our_ip4_addr = our_ip, \
.their_ip4_addr = their_ip, \
.our_ip4_addr.addr = our_ip, \
.their_ip4_addr.addr = their_ip, \
} \
}

Expand Down Expand Up @@ -62,6 +65,9 @@ typedef struct eppp_config_t {
int cs;
int intr;
int freq;
int input_delay_ns;
int cs_ena_pretrans;
int cs_ena_posttrans;
} spi;

struct eppp_config_uart_s {
Expand All @@ -80,8 +86,8 @@ typedef struct eppp_config_t {
} task;

struct eppp_config_pppos_s {
uint32_t our_ip4_addr;
uint32_t their_ip4_addr;
esp_ip4_addr_t our_ip4_addr;
esp_ip4_addr_t their_ip4_addr;
} ppp;

} eppp_config_t;
Expand All @@ -96,9 +102,9 @@ esp_netif_t *eppp_init(eppp_type_t role, eppp_config_t *config);

void eppp_deinit(esp_netif_t *netif);

esp_netif_t *eppp_open(eppp_type_t role, eppp_config_t *config, TickType_t connect_timeout);
esp_netif_t *eppp_open(eppp_type_t role, eppp_config_t *config, int connect_timeout_ms);

esp_err_t eppp_netif_stop(esp_netif_t *netif, TickType_t stop_timeout);
esp_err_t eppp_netif_stop(esp_netif_t *netif, int stop_timeout_ms);

esp_err_t eppp_netif_start(esp_netif_t *netif);

Expand Down

0 comments on commit c891b47

Please sign in to comment.