/ components / esp_eth / src / esp_eth_netif_glue.c
esp_eth_netif_glue.c
  1  // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2  //
  3  // Licensed under the Apache License, Version 2.0 (the "License");
  4  // you may not use this file except in compliance with the License.
  5  // You may obtain a copy of the License at
  6  //
  7  //     http://www.apache.org/licenses/LICENSE-2.0
  8  //
  9  // Unless required by applicable law or agreed to in writing, software
 10  // distributed under the License is distributed on an "AS IS" BASIS,
 11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  // See the License for the specific language governing permissions and
 13  // limitations under the License.
 14  #include <stdlib.h>
 15  #include "esp_netif.h"
 16  #include "esp_eth.h"
 17  #include "esp_eth_netif_glue.h"
 18  #include "esp_event.h"
 19  #include "esp_log.h"
 20  
 21  const static char *TAG = "esp_eth.netif.glue";
 22  
 23  typedef struct {
 24      esp_netif_driver_base_t base;
 25      esp_eth_handle_t eth_driver;
 26  } esp_eth_netif_glue_t;
 27  
 28  static esp_err_t eth_input_to_netif(esp_eth_handle_t eth_handle, uint8_t *buffer, uint32_t length, void *priv)
 29  {
 30      return esp_netif_receive((esp_netif_t *)priv, buffer, length, NULL);
 31  }
 32  
 33  static esp_err_t esp_eth_post_attach(esp_netif_t *esp_netif, void *args)
 34  {
 35      uint8_t eth_mac[6];
 36      esp_eth_netif_glue_t *glue = (esp_eth_netif_glue_t *)args;
 37      glue->base.netif = esp_netif;
 38  
 39      esp_eth_update_input_path(glue->eth_driver, eth_input_to_netif, esp_netif);
 40  
 41      // set driver related config to esp-netif
 42      esp_netif_driver_ifconfig_t driver_ifconfig = {
 43          .handle =  glue->eth_driver,
 44          .transmit = esp_eth_transmit,
 45          .driver_free_rx_buffer = NULL
 46      };
 47  
 48      ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig));
 49      esp_eth_ioctl(glue->eth_driver, ETH_CMD_G_MAC_ADDR, eth_mac);
 50      ESP_LOGI(TAG, "%02x:%02x:%02x:%02x:%02x:%02x", eth_mac[0], eth_mac[1],
 51               eth_mac[2], eth_mac[3], eth_mac[4], eth_mac[5]);
 52  
 53      esp_netif_set_mac(esp_netif, eth_mac);
 54      ESP_LOGI(TAG, "ethernet attached to netif");
 55  
 56      return ESP_OK;
 57  }
 58  
 59  void *esp_eth_new_netif_glue(esp_eth_handle_t eth_hdl)
 60  {
 61      esp_eth_netif_glue_t *glue = calloc(1, sizeof(esp_eth_netif_glue_t));
 62      if (!glue) {
 63          ESP_LOGE(TAG, "create netif glue failed");
 64          return NULL;
 65      }
 66      glue->eth_driver = eth_hdl;
 67      glue->base.post_attach = esp_eth_post_attach;
 68      esp_eth_increase_reference(eth_hdl);
 69      return &glue->base;
 70  }
 71  
 72  esp_err_t esp_eth_del_netif_glue(void *g)
 73  {
 74      esp_eth_netif_glue_t *glue = (esp_eth_netif_glue_t *)g;
 75      esp_eth_decrease_reference(glue->eth_driver);
 76      free(glue);
 77      return ESP_OK;
 78  }
 79  
 80  esp_err_t esp_eth_clear_default_handlers(void *esp_netif)
 81  {
 82      if (!esp_netif) {
 83          ESP_LOGE(TAG, "esp-netif handle can't be null");
 84          return ESP_ERR_INVALID_ARG;
 85      }
 86      esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_START, esp_netif_action_start);
 87      esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_STOP, esp_netif_action_stop);
 88      esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, esp_netif_action_connected);
 89      esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, esp_netif_action_disconnected);
 90      esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, esp_netif_action_got_ip);
 91  
 92      return ESP_OK;
 93  }
 94  
 95  esp_err_t esp_eth_set_default_handlers(void *esp_netif)
 96  {
 97      esp_err_t ret;
 98  
 99      if (!esp_netif) {
100          ESP_LOGE(TAG, "esp-netif handle can't be null");
101          return ESP_ERR_INVALID_ARG;
102      }
103  
104      ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, esp_netif_action_start, esp_netif);
105      if (ret != ESP_OK) {
106          goto fail;
107      }
108  
109      ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_STOP, esp_netif_action_stop, esp_netif);
110      if (ret != ESP_OK) {
111          goto fail;
112      }
113  
114      ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, esp_netif_action_connected, esp_netif);
115      if (ret != ESP_OK) {
116          goto fail;
117      }
118  
119      ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, esp_netif_action_disconnected, esp_netif);
120      if (ret != ESP_OK) {
121          goto fail;
122      }
123  
124      ret = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, esp_netif_action_got_ip, esp_netif);
125      if (ret != ESP_OK) {
126          goto fail;
127      }
128  
129      return ESP_OK;
130  
131  fail:
132      esp_eth_clear_default_handlers(esp_netif);
133      return ret;
134  }