2017-09-27 17:59:49 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
|
2021-04-18 15:38:36 -04:00
|
|
|
* Copyright (c) 2021 "Tobias Eydam" <tobiaseydam@hotmail.com>
|
2017-09-27 17:59:49 -04:00
|
|
|
*
|
|
|
|
* Based on the ESP IDF example code which is Public Domain / CC0
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
#include "esp_idf_version.h"
|
|
|
|
|
2023-01-13 00:04:07 -05:00
|
|
|
#if MICROPY_PY_NETWORK_LAN
|
2021-04-18 15:38:36 -04:00
|
|
|
|
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "esp_eth_mac.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_netif.h"
|
2023-01-13 00:04:07 -05:00
|
|
|
#if CONFIG_ETH_USE_SPI_ETHERNET
|
|
|
|
#include "driver/spi_master.h"
|
|
|
|
#endif
|
2017-09-27 17:59:49 -04:00
|
|
|
|
|
|
|
#include "modnetwork.h"
|
|
|
|
|
|
|
|
typedef struct _lan_if_obj_t {
|
2023-05-08 19:52:54 -04:00
|
|
|
base_if_obj_t base;
|
2017-09-27 17:59:49 -04:00
|
|
|
bool initialized;
|
2023-01-13 00:04:07 -05:00
|
|
|
int8_t mdc_pin;
|
|
|
|
int8_t mdio_pin;
|
2017-09-27 17:59:49 -04:00
|
|
|
int8_t phy_power_pin;
|
2023-01-13 00:04:07 -05:00
|
|
|
int8_t phy_cs_pin;
|
|
|
|
int8_t phy_int_pin;
|
2017-09-27 17:59:49 -04:00
|
|
|
uint8_t phy_addr;
|
|
|
|
uint8_t phy_type;
|
2021-04-18 15:38:36 -04:00
|
|
|
esp_eth_phy_t *phy;
|
|
|
|
esp_eth_handle_t eth_handle;
|
2017-09-27 17:59:49 -04:00
|
|
|
} lan_if_obj_t;
|
|
|
|
|
|
|
|
const mp_obj_type_t lan_if_type;
|
2023-05-08 19:52:54 -04:00
|
|
|
STATIC lan_if_obj_t lan_obj = {{{&lan_if_type}, ESP_IF_ETH, NULL}, false, false};
|
2021-04-18 15:38:36 -04:00
|
|
|
STATIC uint8_t eth_status = 0;
|
|
|
|
|
|
|
|
static void eth_event_handler(void *arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void *event_data) {
|
|
|
|
switch (event_id) {
|
|
|
|
case ETHERNET_EVENT_CONNECTED:
|
|
|
|
eth_status = ETH_CONNECTED;
|
|
|
|
ESP_LOGI("ethernet", "Ethernet Link Up");
|
|
|
|
break;
|
|
|
|
case ETHERNET_EVENT_DISCONNECTED:
|
|
|
|
eth_status = ETH_DISCONNECTED;
|
|
|
|
ESP_LOGI("ethernet", "Ethernet Link Down");
|
|
|
|
break;
|
|
|
|
case ETHERNET_EVENT_START:
|
|
|
|
eth_status = ETH_STARTED;
|
|
|
|
ESP_LOGI("ethernet", "Ethernet Started");
|
|
|
|
break;
|
|
|
|
case ETHERNET_EVENT_STOP:
|
|
|
|
eth_status = ETH_STOPPED;
|
|
|
|
ESP_LOGI("ethernet", "Ethernet Stopped");
|
|
|
|
break;
|
|
|
|
case IP_EVENT_ETH_GOT_IP:
|
|
|
|
eth_status = ETH_GOT_IP;
|
|
|
|
ESP_LOGI("ethernet", "Ethernet Got IP");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
lan_if_obj_t *self = &lan_obj;
|
|
|
|
|
|
|
|
if (self->initialized) {
|
|
|
|
return MP_OBJ_FROM_PTR(&lan_obj);
|
|
|
|
}
|
|
|
|
|
2022-12-24 10:52:55 -05:00
|
|
|
enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type,
|
2023-01-17 21:47:10 -05:00
|
|
|
ARG_spi, ARG_cs, ARG_int, ARG_ref_clk_mode, ARG_ref_clk };
|
2017-09-27 17:59:49 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2023-01-13 00:04:07 -05:00
|
|
|
{ MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2019-02-17 08:02:02 -05:00
|
|
|
{ MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2017-09-27 17:59:49 -04:00
|
|
|
{ MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
{ MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
|
2023-01-17 21:47:10 -05:00
|
|
|
{ MP_QSTR_spi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_int, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2022-12-24 10:52:55 -05:00
|
|
|
{ MP_QSTR_ref_clk_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
|
|
|
|
{ MP_QSTR_ref_clk, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2017-09-27 17:59:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
if (args[ARG_id].u_obj != mp_const_none) {
|
|
|
|
if (mp_obj_get_int(args[ARG_id].u_obj) != 0) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid LAN interface identifier"));
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 00:04:07 -05:00
|
|
|
#define GET_PIN(XXX) args[XXX].u_obj == mp_const_none ? -1 : machine_pin_get_id(args[XXX].u_obj);
|
|
|
|
|
|
|
|
self->mdc_pin = GET_PIN(ARG_mdc);
|
|
|
|
self->mdio_pin = GET_PIN(ARG_mdio);
|
|
|
|
self->phy_power_pin = GET_PIN(ARG_power);
|
|
|
|
self->phy_cs_pin = GET_PIN(ARG_cs);
|
|
|
|
self->phy_int_pin = GET_PIN(ARG_int);
|
2017-09-27 17:59:49 -04:00
|
|
|
|
|
|
|
if (args[ARG_phy_addr].u_int < 0x00 || args[ARG_phy_addr].u_int > 0x1f) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid phy address"));
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
2021-04-18 15:38:36 -04:00
|
|
|
self->phy_addr = args[ARG_phy_addr].u_int;
|
2017-09-27 17:59:49 -04:00
|
|
|
|
2022-12-24 03:37:02 -05:00
|
|
|
if (args[ARG_phy_type].u_int != PHY_LAN8710 &&
|
|
|
|
args[ARG_phy_type].u_int != PHY_LAN8720 &&
|
2021-04-18 15:38:36 -04:00
|
|
|
args[ARG_phy_type].u_int != PHY_IP101 &&
|
|
|
|
args[ARG_phy_type].u_int != PHY_RTL8201 &&
|
|
|
|
args[ARG_phy_type].u_int != PHY_KSZ8041 &&
|
2022-05-30 01:16:51 -04:00
|
|
|
args[ARG_phy_type].u_int != PHY_KSZ8081 &&
|
2023-01-13 00:04:07 -05:00
|
|
|
#if CONFIG_ETH_USE_SPI_ETHERNET
|
|
|
|
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
|
|
|
|
args[ARG_phy_type].u_int != PHY_KSZ8851SNL &&
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ETH_SPI_ETHERNET_DM9051
|
|
|
|
args[ARG_phy_type].u_int != PHY_DM9051 &&
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ETH_SPI_ETHERNET_W5500
|
|
|
|
args[ARG_phy_type].u_int != PHY_W5500 &&
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-04-18 15:38:36 -04:00
|
|
|
args[ARG_phy_type].u_int != PHY_DP83848) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid phy type"));
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
|
2023-05-08 19:52:54 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
eth_esp32_emac_config_t esp32_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
|
|
|
|
#endif
|
|
|
|
|
2023-01-13 00:04:07 -05:00
|
|
|
esp_eth_mac_t *mac = NULL;
|
|
|
|
|
2023-05-08 19:52:54 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
// Dynamic ref_clk configuration.
|
2022-12-24 10:52:55 -05:00
|
|
|
if (args[ARG_ref_clk_mode].u_int != -1) {
|
|
|
|
// Map the GPIO_MODE constants to EMAC_CLK constants.
|
2023-05-08 19:52:54 -04:00
|
|
|
esp32_config.clock_config.rmii.clock_mode =
|
2022-12-24 10:52:55 -05:00
|
|
|
args[ARG_ref_clk_mode].u_int == GPIO_MODE_INPUT ? EMAC_CLK_EXT_IN : EMAC_CLK_OUT;
|
|
|
|
}
|
|
|
|
if (args[ARG_ref_clk].u_obj != mp_const_none) {
|
2023-05-08 19:52:54 -04:00
|
|
|
esp32_config.clock_config.rmii.clock_gpio = machine_pin_get_id(args[ARG_ref_clk].u_obj);
|
2022-12-24 10:52:55 -05:00
|
|
|
}
|
|
|
|
#endif
|
2019-02-17 05:46:23 -05:00
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
|
|
|
|
phy_config.phy_addr = self->phy_addr;
|
|
|
|
phy_config.reset_gpio_num = self->phy_power_pin;
|
|
|
|
self->phy = NULL;
|
2017-09-27 17:59:49 -04:00
|
|
|
|
2023-01-13 00:04:07 -05:00
|
|
|
#if CONFIG_ETH_USE_SPI_ETHERNET
|
|
|
|
spi_device_handle_t spi_handle = NULL;
|
|
|
|
if (IS_SPI_PHY(args[ARG_phy_type].u_int)) {
|
|
|
|
spi_device_interface_config_t devcfg = {
|
|
|
|
.mode = 0,
|
|
|
|
.clock_speed_hz = MICROPY_PY_NETWORK_LAN_SPI_CLOCK_SPEED_MZ * 1000 * 1000,
|
|
|
|
.queue_size = 20,
|
|
|
|
.spics_io_num = self->phy_cs_pin,
|
|
|
|
};
|
|
|
|
switch (args[ARG_phy_type].u_int) {
|
|
|
|
#if CONFIG_ETH_SPI_ETHERNET_DM9051
|
|
|
|
case PHY_DM9051: {
|
|
|
|
devcfg.command_bits = 1;
|
|
|
|
devcfg.address_bits = 7;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ETH_SPI_ETHERNET_W5500
|
|
|
|
case PHY_W5500: {
|
|
|
|
devcfg.command_bits = 16;
|
|
|
|
devcfg.address_bits = 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
spi_host_device_t host = machine_hw_spi_get_host(args[ARG_spi].u_obj);
|
|
|
|
if (spi_bus_add_device(host, &devcfg, &spi_handle) != ESP_OK) {
|
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("spi_bus_add_device failed"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-27 17:59:49 -04:00
|
|
|
switch (args[ARG_phy_type].u_int) {
|
2023-01-13 00:04:07 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2022-12-24 03:37:02 -05:00
|
|
|
case PHY_LAN8710:
|
2017-09-27 17:59:49 -04:00
|
|
|
case PHY_LAN8720:
|
2023-05-08 19:52:54 -04:00
|
|
|
self->phy = esp_eth_phy_new_lan87xx(&phy_config);
|
2017-09-27 17:59:49 -04:00
|
|
|
break;
|
2020-04-20 10:51:11 -04:00
|
|
|
case PHY_IP101:
|
2021-04-18 15:38:36 -04:00
|
|
|
self->phy = esp_eth_phy_new_ip101(&phy_config);
|
|
|
|
break;
|
|
|
|
case PHY_RTL8201:
|
|
|
|
self->phy = esp_eth_phy_new_rtl8201(&phy_config);
|
|
|
|
break;
|
|
|
|
case PHY_DP83848:
|
|
|
|
self->phy = esp_eth_phy_new_dp83848(&phy_config);
|
|
|
|
break;
|
|
|
|
case PHY_KSZ8041:
|
2022-05-30 01:16:51 -04:00
|
|
|
case PHY_KSZ8081:
|
2023-05-08 19:52:54 -04:00
|
|
|
self->phy = esp_eth_phy_new_ksz80xx(&phy_config);
|
2022-05-30 01:16:51 -04:00
|
|
|
break;
|
|
|
|
#endif
|
2023-01-13 00:04:07 -05:00
|
|
|
#if CONFIG_ETH_USE_SPI_ETHERNET
|
|
|
|
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
|
|
|
|
case PHY_KSZ8851SNL: {
|
|
|
|
eth_ksz8851snl_config_t chip_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle);
|
|
|
|
chip_config.int_gpio_num = self->phy_int_pin;
|
|
|
|
mac = esp_eth_mac_new_ksz8851snl(&chip_config, &mac_config);
|
|
|
|
self->phy = esp_eth_phy_new_ksz8851snl(&phy_config);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ETH_SPI_ETHERNET_DM9051
|
|
|
|
case PHY_DM9051: {
|
|
|
|
eth_dm9051_config_t chip_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
|
|
|
|
chip_config.int_gpio_num = self->phy_int_pin;
|
|
|
|
mac = esp_eth_mac_new_dm9051(&chip_config, &mac_config);
|
|
|
|
self->phy = esp_eth_phy_new_dm9051(&phy_config);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_ETH_SPI_ETHERNET_W5500
|
|
|
|
case PHY_W5500: {
|
|
|
|
eth_w5500_config_t chip_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
|
|
|
|
chip_config.int_gpio_num = self->phy_int_pin;
|
|
|
|
mac = esp_eth_mac_new_w5500(&chip_config, &mac_config);
|
|
|
|
self->phy = esp_eth_phy_new_w5500(&phy_config);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
|
2023-01-13 00:04:07 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
if (!IS_SPI_PHY(args[ARG_phy_type].u_int)) {
|
|
|
|
if (self->mdc_pin == -1 || self->mdio_pin == -1) {
|
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("mdc and mdio must be specified"));
|
|
|
|
}
|
2023-05-08 19:52:54 -04:00
|
|
|
esp32_config.smi_mdc_gpio_num = self->mdc_pin;
|
|
|
|
esp32_config.smi_mdio_gpio_num = self->mdio_pin;
|
|
|
|
mac = esp_eth_mac_new_esp32(&esp32_config, &mac_config);
|
2023-01-13 00:04:07 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
if (esp_netif_init() != ESP_OK) {
|
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_init failed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
|
2023-05-08 19:52:54 -04:00
|
|
|
self->base.netif = esp_netif_new(&cfg);
|
2017-09-27 17:59:49 -04:00
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
if (esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL) != ESP_OK) {
|
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed"));
|
|
|
|
}
|
2017-09-27 17:59:49 -04:00
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
if (esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL) != ESP_OK) {
|
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed"));
|
2019-02-17 05:46:23 -05:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, self->phy);
|
|
|
|
|
|
|
|
esp_err_t esp_err = esp_eth_driver_install(&config, &self->eth_handle);
|
|
|
|
if (esp_err == ESP_OK) {
|
2023-06-30 23:54:44 -04:00
|
|
|
self->base.active = false;
|
2017-09-27 17:59:49 -04:00
|
|
|
self->initialized = true;
|
|
|
|
} else {
|
2021-04-18 15:38:36 -04:00
|
|
|
if (esp_err == ESP_ERR_INVALID_ARG) {
|
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with invalid argument"));
|
|
|
|
} else if (esp_err == ESP_ERR_NO_MEM) {
|
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed with no memory for driver"));
|
|
|
|
} else {
|
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_driver_install failed"));
|
|
|
|
}
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
2021-04-18 15:38:36 -04:00
|
|
|
|
2023-05-08 19:52:54 -04:00
|
|
|
if (esp_netif_attach(self->base.netif, esp_eth_new_netif_glue(self->eth_handle)) != ESP_OK) {
|
2021-04-18 15:38:36 -04:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_attach failed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
eth_status = ETH_INITIALIZED;
|
|
|
|
|
2017-09-27 17:59:49 -04:00
|
|
|
return MP_OBJ_FROM_PTR(&lan_obj);
|
|
|
|
}
|
2023-01-31 22:20:45 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(esp_network_get_lan_obj, 0, get_lan);
|
2017-09-27 17:59:49 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) {
|
|
|
|
lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
|
|
|
|
if (n_args > 1) {
|
|
|
|
if (mp_obj_is_true(args[1])) {
|
2023-06-30 23:54:44 -04:00
|
|
|
self->base.active = (esp_eth_start(self->eth_handle) == ESP_OK);
|
|
|
|
if (!self->base.active) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed"));
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
} else {
|
2023-06-30 23:54:44 -04:00
|
|
|
self->base.active = !(esp_eth_stop(self->eth_handle) == ESP_OK);
|
|
|
|
if (self->base.active) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 15:38:36 -04:00
|
|
|
|
2023-06-30 23:54:44 -04:00
|
|
|
return mp_obj_new_bool(self->base.active);
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
|
|
|
|
|
|
|
|
STATIC mp_obj_t lan_status(mp_obj_t self_in) {
|
2021-04-18 15:38:36 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(eth_status);
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);
|
|
|
|
|
|
|
|
STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) {
|
|
|
|
lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2023-06-30 23:54:44 -04:00
|
|
|
return self->base.active ? mp_obj_new_bool(self->phy->get_link(self->phy) == ETH_LINK_UP) : mp_const_false;
|
2017-09-27 17:59:49 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
|
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
STATIC mp_obj_t lan_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
|
|
|
if (n_args != 1 && kwargs->used != 0) {
|
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed"));
|
|
|
|
}
|
|
|
|
lan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
|
|
|
|
if (kwargs->used != 0) {
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kwargs->alloc; i++) {
|
|
|
|
if (mp_map_slot_is_filled(kwargs, i)) {
|
2021-12-10 07:28:46 -05:00
|
|
|
switch (mp_obj_str_get_qstr(kwargs->table[i].key)) {
|
|
|
|
case MP_QSTR_mac: {
|
2021-04-18 15:38:36 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ);
|
|
|
|
if (bufinfo.len != 6) {
|
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid buffer length"));
|
|
|
|
}
|
2023-01-13 00:04:07 -05:00
|
|
|
if (
|
|
|
|
(esp_eth_ioctl(self->eth_handle, ETH_CMD_S_MAC_ADDR, bufinfo.buf) != ESP_OK) ||
|
2023-05-08 19:52:54 -04:00
|
|
|
(esp_netif_set_mac(self->base.netif, bufinfo.buf) != ESP_OK)
|
2023-01-13 00:04:07 -05:00
|
|
|
) {
|
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("failed setting MAC address"));
|
|
|
|
}
|
2021-04-18 15:38:36 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_args != 2) {
|
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("can query only one param"));
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t val = mp_const_none;
|
|
|
|
|
2021-12-10 07:28:46 -05:00
|
|
|
switch (mp_obj_str_get_qstr(args[1])) {
|
|
|
|
case MP_QSTR_mac: {
|
2021-04-18 15:38:36 -04:00
|
|
|
uint8_t mac[6];
|
|
|
|
esp_eth_ioctl(self->eth_handle, ETH_CMD_G_MAC_ADDR, mac);
|
|
|
|
return mp_obj_new_bytes(mac, sizeof(mac));
|
|
|
|
}
|
esp32/modnetwork: Add support for SO_BINDTODEVICE socket option.
This implements support for SO_BINDTODEVICE, which allows telling a socket
to use a specific interface instead of lwIP automatically selecting one.
This allows devices that have multiple connections (for example cellular
over PPP in addition to WLAN) to explicitly choose which data is send over
which connection, which may have different reliability and or (mobile data)
costs associated with using them.
The used lwIP network stack already has support for this, so all that was
needed was to expose this functionality in MicroPython. This commit
exposes a new constant SO_BINDTODEVICE which can be set as an socket
option. As a value it expects the name of the interface to bind to. These
names can be retrieved using `.config('ifname')` implemented on each
interface type (including adding in this commit a `.config()` method to
PPP, which it didn't have before), which returns a string with the
interface name:
>>> import machine
>>> import network
>>> network.WLAN(network.AP_IF).config('ifname')
'lo0'
>>> wlan = network.WLAN(network.AP_IF)
>>> wlan.active(True) and wlan.config('ifname')
'ap1'
>>> wlan = network.WLAN(network.STA_IF)
>>> wlan.active(True) and wlan.config('ifname')
'st1'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp1'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp2'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp3'
Note that lo0 seems to be returned by lwIP if the interface is not yet
active. The method can also return None in the case of PPP where the
entire lwIP interface doesn't yet exist before being activated. Currently
no effort is made to unify those cases; it is expected that whatever we
receive from lwIP is valid.
When the socket option is set, this forces using a specific device:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, 'st1')
setsockopt will throw (OSError: [Errno 19] ENODEV) if the specified
interface does not exist.
Tested with LAN, WLAN, and PPP; can specify which interface should be used
and when testing with, for example, HTTP requests to ifconfig.co the
returned IP address confirms a specific interface was used.
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2023-07-04 09:36:37 -04:00
|
|
|
case MP_QSTR_ifname: {
|
|
|
|
val = esp_ifname(self->base.netif);
|
|
|
|
break;
|
|
|
|
}
|
2021-04-18 15:38:36 -04:00
|
|
|
default:
|
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(lan_config_obj, 1, lan_config);
|
|
|
|
|
2017-09-27 17:59:49 -04:00
|
|
|
STATIC const mp_rom_map_elem_t lan_if_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&lan_active_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&lan_isconnected_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) },
|
2021-04-18 15:38:36 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&lan_config_obj) },
|
2023-01-31 22:20:45 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
|
2017-09-27 17:59:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
lan_if_type,
|
|
|
|
MP_QSTR_LAN,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &lan_if_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2019-09-13 09:04:13 -04:00
|
|
|
|
2021-04-18 15:38:36 -04:00
|
|
|
#endif
|