Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ethernet IP Config #14385

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions tasmota/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,13 @@ typedef struct {
uint8_t weight_change; // E9F
uint8_t web_color2[2][3]; // EA0 Needs to be on integer / 3 distance from web_color

#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32
uint8_t free_ea6[10]; // EA6
uint32_t eth_ipv4_address[5]; // EB0
uint8_t free_ec4[2]; // EC4
#else
uint8_t free_ea6[32]; // EA6
#endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point but this results in issues in the future where these bits are used for all processor types. I suggest to move it to the 32-bit register area at the bottom just above F9C and reserve it fixed for all processor types. No ifdefs needed.


uint8_t shift595_device_count; // EC6
uint8_t sta_config; // EC7
Expand Down Expand Up @@ -853,8 +859,8 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu
uint16_t http_init : 1;
uint16_t shutter_moved : 1;
uint16_t shutter_moving : 1;
uint16_t spare11 : 1;
uint16_t spare12 : 1;
uint16_t eth_connected : 1;
uint16_t eth_disconnected : 1;
uint16_t spare13 : 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an issue with shutters here. I don't like the shutter solution and I will see if this can be solved first to merge your PR.

In short, ifdef USE_SHUTTERS changes the amount of bits tested based on the assumptions the last two bits are for shutter status. This changes with your implemntation.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bits are not persistent so I suggest the following:

  • Insert your entries just above the http_init bit.
  • Increment both MAX_RULES_FLAG by two.
  • Update RulesEvery50ms() by insert your rule text above the HTTP entries. So they become 8 and 9. HTTP becomes 10 and the optional shutter ones become 11 and 12.

uint16_t spare14 : 1;
uint16_t spare15 : 1;
Expand Down
28 changes: 21 additions & 7 deletions tasmota/support_command.ino
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,26 @@ void CmndStatus(void)
}

if ((0 == payload) || (5 == payload)) {
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS5_NETWORK "\":{\"" D_CMND_HOSTNAME "\":\"%s\",\"" D_CMND_IPADDRESS "\":\"%_I\",\""
D_JSON_GATEWAY "\":\"%_I\",\"" D_JSON_SUBNETMASK "\":\"%_I\",\"" D_JSON_DNSSERVER "1\":\"%_I\",\"" D_JSON_DNSSERVER "2\":\"%_I\",\""
D_JSON_MAC "\":\"%s\",\"" D_CMND_WEBSERVER "\":%d,\"HTTP_API\":%d,\"" D_CMND_WIFICONFIG "\":%d,\"" D_CMND_WIFIPOWER "\":%s}}"),
NetworkHostname(), (uint32_t)NetworkAddress(),
Settings->ipv4_address[1], Settings->ipv4_address[2], Settings->ipv4_address[3], Settings->ipv4_address[4],
NetworkMacAddress().c_str(), Settings->webserver, Settings->flag5.disable_referer_chk, Settings->sta_config, WifiGetOutputPower().c_str());
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS5_NETWORK "\":{\"" D_CMND_HOSTNAME "\":\"%s\",\""
D_CMND_IPADDRESS "\":\"%_I\",\"" D_JSON_GATEWAY "\":\"%_I\",\"" D_JSON_SUBNETMASK "\":\"%_I\",\""
D_JSON_DNSSERVER "1\":\"%_I\",\"" D_JSON_DNSSERVER "2\":\"%_I\",\""
D_JSON_MAC "\":\"%s\",\""
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
"Eth" D_CMND_HOSTNAME "\":\"%s\",\""
"Eth" D_CMND_IPADDRESS "\":\"%_I\",\"Eth" D_JSON_GATEWAY "\":\"%_I\",\"Eth" D_JSON_SUBNETMASK "\":\"%_I\",\""
"Eth" D_JSON_DNSSERVER "1\":\"%_I\",\"Eth" D_JSON_DNSSERVER "2\":\"%_I\",\""
"Eth" D_JSON_MAC "\":\"%s\",\""
#endif
D_CMND_WEBSERVER "\":%d,\"HTTP_API\":%d,\"" D_CMND_WIFICONFIG "\":%d,\"" D_CMND_WIFIPOWER "\":%s}}"),
TasmotaGlobal.hostname,
(uint32_t)WiFi.localIP(), Settings->ipv4_address[1], Settings->ipv4_address[2], Settings->ipv4_address[3], Settings->ipv4_address[4],
WiFi.macAddress().c_str(),
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
EthernetHostname(),
(uint32_t)EthernetLocalIP(), Settings->eth_ipv4_address[1], Settings->eth_ipv4_address[2], Settings->eth_ipv4_address[3], Settings->eth_ipv4_address[4],
EthernetMacAddress().c_str(),
#endif
Settings->webserver, Settings->flag5.disable_referer_chk, Settings->sta_config, WifiGetOutputPower().c_str());
CmndStatusResponse(5);
}

Expand Down Expand Up @@ -1635,7 +1649,7 @@ void CmndIpAddress(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) {
char network_address[22];
ext_snprintf_P(network_address, sizeof(network_address), PSTR(" (%_I)"), (uint32_t)NetworkAddress());
ext_snprintf_P(network_address, sizeof(network_address), PSTR(" (%_I)"), (uint32_t)WiFi.localIP());
if (!XdrvMailbox.usridx) {
ResponseClear();
for (uint32_t i = 0; i < 5; i++) {
Expand Down
6 changes: 5 additions & 1 deletion tasmota/xdrv_01_webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,11 @@ bool HttpCheckPriviledgedAccess(bool autorequestauth = true)
referer.toUpperCase();
String hostname = NetworkHostname();
hostname.toUpperCase();
if ((referer.indexOf(hostname) == 7) || (referer.indexOf(NetworkAddress().toString()) == 7)) {
if ((referer.indexOf(hostname) == 7) || (referer.indexOf(WiFi.localIP().toString()) == 7)
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
|| (referer.indexOf(EthernetLocalIP().toString()) == 7)
#endif
) {
return true;
}
}
Expand Down
12 changes: 10 additions & 2 deletions tasmota/xdrv_02_9_mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,16 @@ void MqttConnected(void) {
Response_P(PSTR("{\"Info2\":{\"" D_JSON_WEBSERVER_MODE "\":\"%s\",\"" D_CMND_HOSTNAME "\":\"%s\",\"" D_CMND_IPADDRESS "\":\"%s\",\"IPv6Address\":\"%s\"}}"),
(2 == Settings->webserver) ? PSTR(D_ADMIN) : PSTR(D_USER), NetworkHostname(), NetworkAddress().toString().c_str(), WifiGetIPv6().c_str(), Settings->flag5.mqtt_info_retain);
#else
Response_P(PSTR("{\"Info2\":{\"" D_JSON_WEBSERVER_MODE "\":\"%s\",\"" D_CMND_HOSTNAME "\":\"%s\",\"" D_CMND_IPADDRESS "\":\"%s\"}}"),
(2 == Settings->webserver) ? PSTR(D_ADMIN) : PSTR(D_USER), NetworkHostname(), NetworkAddress().toString().c_str(), Settings->flag5.mqtt_info_retain);
Response_P(PSTR("{\"Info2\":{\"" D_JSON_WEBSERVER_MODE "\":\"%s\",\"" D_CMND_HOSTNAME "\":\"%s\",\"" D_CMND_IPADDRESS "\":\"%s\""
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
",\"Eth" D_CMND_HOSTNAME "\":\"%s\",\"Eth" D_CMND_IPADDRESS "\":\"%s\""
#endif
"}}"),
(2 == Settings->webserver) ? PSTR(D_ADMIN) : PSTR(D_USER), TasmotaGlobal.hostname, WiFi.localIP().toString().c_str(),
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
EthernetHostname(), (uint32_t)EthernetLocalIP().toString().c_str(),
#endif
Settings->flag5.mqtt_info_retain);
#endif // LWIP_IPV6 = 1
MqttPublishPrefixTopicRulesProcess_P(TELE, PSTR(D_RSLT_INFO "2"), Settings->flag5.mqtt_info_retain);
}
Expand Down
4 changes: 4 additions & 0 deletions tasmota/xdrv_10_rules.ino
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,10 @@ void RulesEvery50ms(void)
case 9: strncpy_P(json_event, PSTR("{\"SHUTTER\":{\"Moved\":1}}"), sizeof(json_event)); break;
case 10: strncpy_P(json_event, PSTR("{\"SHUTTER\":{\"Moving\":1}}"), sizeof(json_event)); break;
#endif // USE_SHUTTER
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
case 11: strncpy_P(json_event, PSTR("{\"ETH\":{\"Connected\":1}}"), sizeof(json_event)); break;
case 12: strncpy_P(json_event, PSTR("{\"ETH\":{\"Disconnected\":1}}"), sizeof(json_event)); break;
#endif
}
if (json_event[0]) {
RulesProcessEvent(json_event);
Expand Down
4 changes: 2 additions & 2 deletions tasmota/xdrv_58_range_extender.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
To use this, add the following to your user_config_override.h
#define USE_WIFI_RANGE_EXTENDER

Additionally, for the ESP8266, PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH must be
Additionally, for the ESP8266, PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH must be
set in your build options.
For example, in your platfromio_tasmota_cenv.ini, you will need an entry such as:
[env:tasmota-rangeextender]
build_flags = ${common.build_flags}
-D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH

For the ESP32, the arduino-esp32 library must be at least version 2, with
For the ESP32, the arduino-esp32 library must be at least version 2, with
CONFIG_LWIP_IP_FORWARD option set, and optionally CONFIG_LWIP_IPV4_NAPT.

If you want to support NAPT (removing the need for routes on a core router):
Expand Down
60 changes: 52 additions & 8 deletions tasmota/xdrv_82_esp32_ethernet.ino
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,22 @@ void EthernetEvent(WiFiEvent_t event) {
case ARDUINO_EVENT_ETH_CONNECTED:
AddLog(LOG_LEVEL_INFO, PSTR("ETH: " D_CONNECTED " at %dMbps%s"),
ETH.linkSpeed(), (ETH.fullDuplex()) ? " Full Duplex" : "");
TasmotaGlobal.rules_flag.eth_connected = 1;
break;
case ARDUINO_EVENT_ETH_GOT_IP:
AddLog(LOG_LEVEL_DEBUG, PSTR("ETH: Mac %s, IPAddress %_I, Hostname %s"),
ETH.macAddress().c_str(), (uint32_t)ETH.localIP(), eth_hostname);
Settings->ipv4_address[1] = (uint32_t)ETH.gatewayIP();
Settings->ipv4_address[2] = (uint32_t)ETH.subnetMask();
Settings->ipv4_address[3] = (uint32_t)ETH.dnsIP();
Settings->ipv4_address[4] = (uint32_t)ETH.dnsIP(1);
Settings->eth_ipv4_address[1] = (uint32_t)ETH.gatewayIP();
Settings->eth_ipv4_address[2] = (uint32_t)ETH.subnetMask();
Settings->eth_ipv4_address[3] = (uint32_t)ETH.dnsIP();
Settings->eth_ipv4_address[4] = (uint32_t)ETH.dnsIP(1);
TasmotaGlobal.global_state.eth_down = 0;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
AddLog(LOG_LEVEL_INFO, PSTR("ETH: Disconnected"));
TasmotaGlobal.global_state.eth_down = 1;
TasmotaGlobal.rules_flag.eth_disconnected = 1;

break;
case ARDUINO_EVENT_ETH_STOP:
AddLog(LOG_LEVEL_DEBUG, PSTR("ETH: Stopped"));
Expand Down Expand Up @@ -140,7 +143,13 @@ void EthernetInit(void) {
int eth_mdio = Pin(GPIO_ETH_PHY_MDIO);
if (!ETH.begin(Settings->eth_address, eth_power, eth_mdc, eth_mdio, (eth_phy_type_t)Settings->eth_type, (eth_clock_mode_t)Settings->eth_clk_mode)) {
AddLog(LOG_LEVEL_DEBUG, PSTR("ETH: Bad PHY type or init error"));
return;
};

if (Settings->eth_ipv4_address[0]) {
// (IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2);
ETH.config(Settings->eth_ipv4_address[0], Settings->eth_ipv4_address[1], Settings->eth_ipv4_address[2], Settings->eth_ipv4_address[3], Settings->eth_ipv4_address[4]); // Set static IP
}
}

IPAddress EthernetLocalIP(void) {
Expand All @@ -159,15 +168,28 @@ String EthernetMacAddress(void) {
* Commands
\*********************************************************************************************/

#define D_CMND_ETHADDRESS "EthAddress"
#define D_CMND_ETHTYPE "EthType"
#define D_CMND_ETHADDRESS "EthAddress"
#define D_CMND_ETHTYPE "EthType"
#define D_CMND_ETHCLOCKMODE "EthClockMode"
#define D_CMND_ETHIPADDRESS "Eth" D_CMND_IPADDRESS
#define D_CMND_ETHGATEWAY "Eth" D_JSON_GATEWAY
#define D_CMND_ETHNETMASK "Eth" D_JSON_SUBNETMASK
#define D_CMND_ETHDNS "Eth" D_JSON_DNSSERVER

const char kEthernetCommands[] PROGMEM = "|" // No prefix
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually use "Eth" as prefix before the first "|"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do in a minute
Is there also an way got extract a sub command to avoid the array below?

D_CMND_ETHERNET "|" D_CMND_ETHADDRESS "|" D_CMND_ETHTYPE "|" D_CMND_ETHCLOCKMODE;
D_CMND_ETHERNET "|" D_CMND_ETHADDRESS "|" D_CMND_ETHTYPE "|" D_CMND_ETHCLOCKMODE "|"
D_CMND_ETHIPADDRESS "|" D_CMND_ETHGATEWAY "|" D_CMND_ETHNETMASK "|" D_CMND_ETHDNS;

const char *EthIpParams[] = {
PSTR(D_CMND_ETHIPADDRESS), PSTR(D_CMND_ETHGATEWAY), PSTR(D_CMND_ETHNETMASK),
PSTR(D_CMND_ETHDNS "1"), PSTR(D_CMND_ETHDNS "2")
};

void (* const EthernetCommand[])(void) PROGMEM = {
&CmndEthernet, &CmndEthAddress, &CmndEthType, &CmndEthClockMode };
&CmndEthernet, &CmndEthAddress, &CmndEthType, &CmndEthClockMode,
&CmndEthIpAddr, &CmndEthGateway, &CmndEthNetmask, &CmndEthDns
};


void CmndEthernet(void)
{
Expand Down Expand Up @@ -205,6 +227,28 @@ void CmndEthClockMode(void)
ResponseCmndNumber(Settings->eth_clk_mode);
}

void CmndEthIpConfig(int param_id)
{
if (3 == param_id && 2 == XdrvMailbox.index)
param_id = 4;
char network_address[22] = {0};
if (0 == param_id)
ext_snprintf_P(network_address, sizeof(network_address), PSTR(" (%_I)"), (uint32_t)ETH.localIP());

uint32_t ipv4_address;
if (ParseIPv4(&ipv4_address, XdrvMailbox.data)) {
Settings->eth_ipv4_address[param_id] = ipv4_address;
}
Response_P(PSTR("{\"%s\":\"%_I%s\"}"), EthIpParams[param_id], Settings->eth_ipv4_address[param_id], (0 == param_id)?network_address:"");

// TasmotaGlobal.restart_flag = 2; // Wifi IpAddress doesn't do that so ?
}

void CmndEthIpAddr(void) { CmndEthIpConfig(0); }
void CmndEthGateway(void) { CmndEthIpConfig(1); }
void CmndEthNetmask(void) { CmndEthIpConfig(2); }
void CmndEthDns(void) { CmndEthIpConfig(3); }

/*********************************************************************************************\
* Interface
\*********************************************************************************************/
Expand Down