|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <string.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <inttypes.h> |
| 9 | +#include "esp_log.h" |
| 10 | +#include "esp_netif.h" |
| 11 | +#include "esp_check.h" |
| 12 | +#include "esp_event.h" |
| 13 | +#include "esp_mac.h" |
| 14 | +#include "eppp_link.h" |
| 15 | +#include "eppp_transport.h" |
| 16 | +#include "esp_eth_driver.h" |
| 17 | +#include "ethernet_init.h" |
| 18 | +#include "esp_eth_spec.h" |
| 19 | + |
| 20 | +typedef struct header { |
| 21 | + uint8_t dst[ETH_ADDR_LEN]; |
| 22 | + uint8_t src[ETH_ADDR_LEN]; |
| 23 | + uint16_t len; |
| 24 | +} __attribute__((packed)) header_t; |
| 25 | + |
| 26 | +static const char *TAG = "eppp_ethernet"; |
| 27 | +static bool s_is_connected = false; |
| 28 | +static esp_eth_handle_t *s_eth_handles = NULL; |
| 29 | +static uint8_t s_out_buffer[ETH_MAX_PACKET_SIZE]; |
| 30 | +static uint8_t s_their_mac[ETH_ADDR_LEN]; |
| 31 | +static uint8_t s_our_mac[ETH_ADDR_LEN]; |
| 32 | + |
| 33 | +static void event_handler(void *arg, esp_event_base_t event_base, |
| 34 | + int32_t event_id, void *event_data) |
| 35 | +{ |
| 36 | + switch (event_id) { |
| 37 | + case ETHERNET_EVENT_CONNECTED: |
| 38 | + ESP_LOGI(TAG, "Ethernet Link Up"); |
| 39 | + s_is_connected = true; |
| 40 | + break; |
| 41 | + case ETHERNET_EVENT_DISCONNECTED: |
| 42 | + ESP_LOGI(TAG, "Ethernet Link Down"); |
| 43 | + s_is_connected = false; |
| 44 | + break; |
| 45 | + case ETHERNET_EVENT_START: |
| 46 | + ESP_LOGI(TAG, "Ethernet Started"); |
| 47 | + break; |
| 48 | + case ETHERNET_EVENT_STOP: |
| 49 | + ESP_LOGI(TAG, "Ethernet Stopped"); |
| 50 | + break; |
| 51 | + default: |
| 52 | + break; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +static esp_err_t receive(esp_eth_handle_t h, uint8_t *buffer, uint32_t len, void *netif) |
| 57 | +{ |
| 58 | + header_t *head = (header_t *)buffer; |
| 59 | + size_t packet_len = head->len; |
| 60 | + if (len >= packet_len) { |
| 61 | + esp_err_t ret = esp_netif_receive(netif, buffer + ETH_HEADER_LEN, packet_len, NULL); |
| 62 | + free(buffer); |
| 63 | + return ret; |
| 64 | + } |
| 65 | + return ESP_FAIL; |
| 66 | +} |
| 67 | + |
| 68 | +esp_err_t eppp_transport_init(eppp_config_t *config, esp_netif_t *esp_netif) |
| 69 | +{ |
| 70 | + uint8_t eth_port_cnt = 0; |
| 71 | + ESP_ERROR_CHECK(ethernet_init_all(&s_eth_handles, ð_port_cnt)); |
| 72 | + if (eth_port_cnt > 1) { |
| 73 | + ESP_LOGW(TAG, "multiple Ethernet devices detected, the first initialized is to be used!"); |
| 74 | + } |
| 75 | + ESP_ERROR_CHECK(esp_eth_update_input_path(s_eth_handles[0], receive, esp_netif)); |
| 76 | + sscanf(CONFIG_EPPP_LINK_ETHERNET_OUR_ADDRESS, "%2" PRIu8 ":%2" PRIu8 ":%2" PRIi8 ":%2" PRIu8 ":%2" PRIu8 ":%2" PRIu8, |
| 77 | + &s_our_mac[0], &s_our_mac[1], &s_our_mac[2], &s_our_mac[3], &s_our_mac[4], &s_our_mac[5]); |
| 78 | + |
| 79 | + sscanf(CONFIG_EPPP_LINK_ETHERNET_THEIR_ADDRESS, "%2" PRIu8 ":%2" PRIu8 ":%2" PRIi8 ":%2" PRIu8 ":%2" PRIu8 ":%2" PRIu8, |
| 80 | + &s_their_mac[0], &s_their_mac[1], &s_their_mac[2], &s_their_mac[3], &s_their_mac[4], &s_their_mac[5]); |
| 81 | + esp_eth_ioctl(s_eth_handles[0], ETH_CMD_S_MAC_ADDR, s_our_mac); |
| 82 | + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL)); |
| 83 | + ESP_ERROR_CHECK(esp_eth_start(s_eth_handles[0])); |
| 84 | + return ESP_OK; |
| 85 | +} |
| 86 | + |
| 87 | +void eppp_transport_deinit(void) |
| 88 | +{ |
| 89 | + ethernet_deinit_all(s_eth_handles); |
| 90 | +} |
| 91 | + |
| 92 | +esp_err_t eppp_transport_tx(void *h, void *buffer, size_t len) |
| 93 | +{ |
| 94 | + if (!s_is_connected) { |
| 95 | + return ESP_OK; |
| 96 | + } |
| 97 | + // setup Ethernet header |
| 98 | + header_t *head = (header_t *)s_out_buffer; |
| 99 | + memcpy(head->dst, s_their_mac, ETH_ADDR_LEN); |
| 100 | + memcpy(head->src, s_our_mac, ETH_ADDR_LEN); |
| 101 | + head->len = len; |
| 102 | + // handle frame size: ETH_MIN_PAYLOAD_LEN <= len <= ETH_MAX_PAYLOAD_LEN |
| 103 | + size_t frame_payload_len = len < ETH_MIN_PAYLOAD_LEN ? ETH_MIN_PAYLOAD_LEN : len; |
| 104 | + if (frame_payload_len > ETH_MAX_PAYLOAD_LEN) { |
| 105 | + return ESP_FAIL; |
| 106 | + } |
| 107 | + memcpy(s_out_buffer + ETH_HEADER_LEN, buffer, len); |
| 108 | + return esp_eth_transmit(s_eth_handles[0], s_out_buffer, frame_payload_len + ETH_HEADER_LEN); |
| 109 | +} |
0 commit comments