Skip to content

Commit

Permalink
Refactorize ethernet part
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-github committed Sep 3, 2024
1 parent 86d33bd commit b93e24b
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 91 deletions.
18 changes: 18 additions & 0 deletions docs/esp3dcnf.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ AP_IP = 192.168.0.1
#AP channel 1~14
AP_channel = 11

#Ethernet STA IP Mode DHCP / STATIC
ETH_STA_IP_mode = DHCP

#Ethernet STA static IP
ETH_STA_IP = 192.168.0.2

#Ethernet STA static gateway
ETH_STA_GW = 192.168.0.1

#Ethernet STA static mask
ETH_STA_MSK = 255.255.255.0

#Ethernet STA static dns
ETH_STA_DNS = 192.168.0.1

#Ethernet fallback mode BT, OFF
eth_sta_fallback = OFF

[services]
#Active or not Serial Bridge Yes / No
Serial_Bridge_active = Yes
Expand Down
45 changes: 41 additions & 4 deletions esp3d/src/core/commands/ESP400.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ const char* FallbackValues[] = {"0"
#endif // BLUETOOTH_FEATURE
};

const char* EthFallbackValues[] = {"0"
#ifdef BLUETOOTH_FEATURE
,
"3"
#endif // BLUETOOTH_FEATURE
};
const char* EthFallbackLabels[] = {"none"
#ifdef BLUETOOTH_FEATURE
,
"bt"
#endif // BLUETOOTH_FEATURE
};

const char* FirmwareLabels[] = {"Unknown", "Grbl", "Marlin", "Smoothieware",
"Repetier"};

Expand Down Expand Up @@ -204,6 +217,30 @@ void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) {
dispatchSetting(json, "network/network", ESP_BOOT_RADIO_STATE, "radio_boot",
YesNoValues, YesNoLabels, sizeof(YesNoValues) / sizeof(char*),
-1, -1, -1, NULL, true, target, requestId);
#if defined(ETH_FEATURE)
// Ethernet STA IP mode
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_IP_MODE, "ip mode", IpModeValues,
IpModeLabels, sizeof(IpModeLabels) / sizeof(char*), -1, -1,
-1, nullptr, true, target, requestId);
// Ethernet STA static IP
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_IP_VALUE, "ip", nullptr, nullptr,
-1, -1, -1, -1, nullptr, true, target, requestId);

// Ethernet STA static Gateway
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_GATEWAY_VALUE, "gw", nullptr,
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
// Ethernet STA static Mask
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_MASK_VALUE, "msk", nullptr,
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
// Ethernet STA static DNS
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_DNS_VALUE, "DNS", nullptr,
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);
// Ethernet Sta fallback mode
dispatchSetting(json, "network/eth-sta", ESP_ETH_STA_FALLBACK_MODE,
"sta fallback mode", EthFallbackValues, EthFallbackLabels,
sizeof(EthFallbackValues) / sizeof(char*), -1, -1, -1, nullptr,
true, target, requestId);
#endif // ETH_FEATURE
#ifdef WIFI_FEATURE
// STA SSID network/sta
dispatchSetting(json, "network/sta", ESP_STA_SSID, "SSID", nullptr, nullptr,
Expand All @@ -214,7 +251,7 @@ void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) {
nullptr, 64, 8, 0, -1, nullptr, true, target, requestId);

#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
#if defined(WIFI_FEATURE)
// STA IP mode
dispatchSetting(json, "network/sta", ESP_STA_IP_MODE, "ip mode", IpModeValues,
IpModeLabels, sizeof(IpModeLabels) / sizeof(char*), -1, -1,
Expand All @@ -233,15 +270,15 @@ void ESP3DCommands::ESP400(int cmd_params_pos, ESP3DMessage* msg) {
dispatchSetting(json, "network/sta", ESP_STA_DNS_VALUE, "DNS", nullptr,
nullptr, -1, -1, -1, -1, nullptr, true, target, requestId);

#endif // WIFI_FEATURE || ETH_FEATURE
#endif // WIFI_FEATURE

#if defined(WIFI_FEATURE) || defined(ETH_FEATURE) || defined(BT_FEATURE)
#if defined(WIFI_FEATURE)
// Sta fallback mode
dispatchSetting(json, "network/sta", ESP_STA_FALLBACK_MODE,
"sta fallback mode", FallbackValues, FallbackLabels,
sizeof(FallbackValues) / sizeof(char*), -1, -1, -1, nullptr,
true, target, requestId);
#endif // WIFI_FEATURE || ETH_FEATURE || BT_FEATURE
#endif // WIFI_FEATURE
#if defined(WIFI_FEATURE)
// AP SSID network/ap
dispatchSetting(json, "network/ap", ESP_AP_SSID, "SSID", nullptr, nullptr, 32,
Expand Down
6 changes: 6 additions & 0 deletions esp3d/src/core/commands/ESP420.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ void ESP3DCommands::ESP420(int cmd_params_pos, ESP3DMessage* msg) {
return;
}
// IP mode
esp3d_log_d("IP mode %d", NetConfig::isIPModeDHCP(ESP_ETH_STA));
tmpstr = (NetConfig::isIPModeDHCP(ESP_ETH_STA)) ? "dhcp" : "static";
if (!dispatchIdValue(json, "ip mode", tmpstr.c_str(), target, requestId,
false)) {
Expand Down Expand Up @@ -426,6 +427,11 @@ void ESP3DCommands::ESP420(int cmd_params_pos, ESP3DMessage* msg) {
false)) {
return;
}
} else {
if (!dispatchIdValue(json, "ethernet", "OFF", target, requestId,
false)) {
return;
}
}
#endif // ETH_FEATURE
#if defined(WIFI_FEATURE)
Expand Down
60 changes: 55 additions & 5 deletions esp3d/src/core/esp3d_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
#define MIN_SERVER_ADDRESS_LENGTH 0

// default byte values
#ifdef ETH_FEATURE
#define DEFAULT_ETH_STA_FALLBACK_MODE STRING(ESP_NO_NETWORK)
#else
#define DEFAULT_ETH_STA_FALLBACK_MODE STRING(ESP_NO_NETWORK)
#endif // ETH_FEATURE

#ifdef WIFI_FEATURE
#define DEFAULT_STA_FALLBACK_MODE STRING(ESP_AP_SETUP)
#if defined(STATION_WIFI_SSID) && defined(STATION_WIFI_PASSWORD)
Expand Down Expand Up @@ -192,6 +198,12 @@ uint16_t ESP3DSettingsData[] = {ESP_RADIO_MODE,
ESP_STA_DNS_VALUE,
ESP_AP_IP_VALUE,
ESP_STA_IP_MODE,
ESP_ETH_STA_FALLBACK_MODE,
ESP_ETH_STA_IP_VALUE,
ESP_ETH_STA_GATEWAY_VALUE,
ESP_ETH_STA_MASK_VALUE,
ESP_ETH_STA_DNS_VALUE,
ESP_ETH_STA_IP_MODE,
ESP_SETTINGS_VERSION,
ESP_NOTIFICATION_TYPE,
ESP_CALIBRATION,
Expand Down Expand Up @@ -240,7 +252,6 @@ uint16_t ESP3DSettingsData[] = {ESP_RADIO_MODE,
ESP_FTP_DATA_PASSIVE_PORT,
ESP_WEBDAV_PORT,
ESP_SERIAL_BRIDGE_BAUD

};
#if defined(SD_DEVICE)
const uint8_t SupportedSPIDivider[] = {1, 2, 4, 6, 8, 16, 32};
Expand Down Expand Up @@ -962,12 +973,19 @@ bool ESP3DSettings::isValidByteSetting(uint8_t value,
return true;
}
break;
case ESP_STA_IP_MODE:
#ifdef ETH_FEATURE
case ESP_ETH_STA_IP_MODE:
if (value == DHCP_MODE || value == STATIC_IP_MODE) {
return true;
}
break;
#endif // ETH_FEATURE
#if defined(WIFI_FEATURE)
case ESP_STA_IP_MODE:
if (value == DHCP_MODE || value == STATIC_IP_MODE) {
return true;
}
break;
case ESP_AP_CHANNEL:
for (uint8_t i = 0; i < SupportedApChannelsSize; i++) {
if (value == SupportedApChannels[i]) {
Expand Down Expand Up @@ -1014,19 +1032,30 @@ bool ESP3DSettings::isValidByteSetting(uint8_t value,
// 0 means no timeout so it is ok to have 0
return true;
break;
case ESP_STA_FALLBACK_MODE:
#ifdef ETH_FEATURE
case ESP_ETH_STA_FALLBACK_MODE:
if (value == ESP_NO_NETWORK
#if defined(BT_FEATURE)
|| value == ESP_BT
#endif // BT_FEATURE
) {
return true;
}
break;
#endif // ETH_FEATURE
#if defined(WIFI_FEATURE)
case ESP_STA_FALLBACK_MODE:
if (value == ESP_NO_NETWORK

|| value == ESP_AP_SETUP
#endif // WIFI_FEATURE
#if defined(BT_FEATURE)
|| value == ESP_BT
#endif // BT_FEATURE
) {
return true;
}
break;

#endif // WIFI_FEATURE
default:
return false;
}
Expand Down Expand Up @@ -1119,8 +1148,10 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr(
case ESP_SECURE_SERIAL:
case ESP_BOOT_RADIO_STATE:
case ESP_STA_FALLBACK_MODE:
case ESP_ETH_STA_FALLBACK_MODE:
case ESP_SERIAL_BRIDGE_ON:
case ESP_STA_IP_MODE:
case ESP_ETH_STA_IP_MODE:
setting.type = ESP3DSettingType::byte_t; // byte
break;

Expand All @@ -1146,6 +1177,10 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr(
case ESP_STA_GATEWAY_VALUE:
case ESP_STA_MASK_VALUE:
case ESP_STA_DNS_VALUE:
case ESP_ETH_STA_IP_VALUE:
case ESP_ETH_STA_GATEWAY_VALUE:
case ESP_ETH_STA_MASK_VALUE:
case ESP_ETH_STA_DNS_VALUE:
case ESP_AP_IP_VALUE:

setting.type = ESP3DSettingType::ip_t; // ip = 4 bytes
Expand Down Expand Up @@ -1199,11 +1234,17 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr(
case ESP_WEBDAV_ON:
case ESP_SECURE_SERIAL:
case ESP_BOOT_RADIO_STATE:
case ESP_ETH_STA_FALLBACK_MODE:
case ESP_STA_FALLBACK_MODE:
case ESP_SERIAL_BRIDGE_ON:
case ESP_ETH_STA_IP_MODE:
case ESP_STA_IP_MODE:
setting.size = 1; // 1 byte
break;
case ESP_ETH_STA_IP_VALUE:
case ESP_ETH_STA_GATEWAY_VALUE:
case ESP_ETH_STA_MASK_VALUE:
case ESP_ETH_STA_DNS_VALUE:
case ESP_STA_IP_VALUE:
case ESP_STA_GATEWAY_VALUE:
case ESP_STA_MASK_VALUE:
Expand Down Expand Up @@ -1280,6 +1321,8 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr(

// default value of setting in string
switch (index) {

case ESP_ETH_STA_IP_MODE:
case ESP_STA_IP_MODE:
setting.default_val = DEFAULT_STA_IP_MODE;
break;
Expand Down Expand Up @@ -1352,6 +1395,9 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr(
case ESP_BOOT_RADIO_STATE:
setting.default_val = DEFAULT_BOOT_RADIO_STATE;
break;
case ESP_ETH_STA_FALLBACK_MODE:
setting.default_val = DEFAULT_ETH_STA_FALLBACK_MODE;
break;
case ESP_STA_FALLBACK_MODE:
setting.default_val = DEFAULT_STA_FALLBACK_MODE;
break;
Expand Down Expand Up @@ -1403,15 +1449,19 @@ const ESP3DSettingDescription *ESP3DSettings::getSettingPtr(
case ESP_TIME_ZONE:
setting.default_val = DEFAULT_TIME_ZONE;
break;
case ESP_ETH_STA_IP_VALUE:
case ESP_STA_IP_VALUE:
setting.default_val = DEFAULT_STA_IP_VALUE;
break;
case ESP_ETH_STA_GATEWAY_VALUE:
case ESP_STA_GATEWAY_VALUE:
setting.default_val = DEFAULT_STA_GATEWAY_VALUE;
break;
case ESP_ETH_STA_MASK_VALUE:
case ESP_STA_MASK_VALUE:
setting.default_val = DEFAULT_STA_MASK_VALUE;
break;
case ESP_ETH_STA_DNS_VALUE:
case ESP_STA_DNS_VALUE:
setting.default_val = DEFAULT_STA_DNS_VALUE;
break;
Expand Down
38 changes: 32 additions & 6 deletions esp3d/src/include/esp3d_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ typedef uint ESP3DSettingIndex;
#define ESP_SETUP 1192 // 1 byte = flag
// #define FREE 1193 // 1 byte = flag
// #define FREE 1194 // 1 byte = flag
// #define FREE 1195 // 1 byte = flag
#define ESP_ETH_STA_FALLBACK_MODE 1195 // 1 byte = flag
#define ESP_FTP_CTRL_PORT 1196 // 4 bytes = int
#define ESP_FTP_DATA_ACTIVE_PORT 1200 // 4 bytes = int
#define ESP_FTP_DATA_PASSIVE_PORT 1204 // 4 bytes = int
Expand All @@ -133,9 +133,13 @@ typedef uint ESP3DSettingIndex;
#define ESP_BOOT_RADIO_STATE 1221 // 1 byte = flag
#define ESP_STA_FALLBACK_MODE 1222 // 1 byte = flag
#define ESP_SERIAL_BRIDGE_ON 1223 // 1 byte = flag
// #define FREE 1224 // 1 byte = flag
#define ESP_ETH_STA_IP_MODE 1224 // 1 byte = flag
#define ESP_SERIAL_BRIDGE_BAUD 1225 // 4 bytes= int
#define ESP_TIME_ZONE 1229 // 7 bytes 6+1 = string
#define ESP_ETH_STA_IP_VALUE 1237 // 4 bytes xxx.xxx.xxx.xxx
#define ESP_ETH_STA_MASK_VALUE 1240 // 4 bytes xxx.xxx.xxx.xxx
#define ESP_ETH_STA_GATEWAY_VALUE 1244 // 4 bytes xxx.xxx.xxx.xxx
#define ESP_ETH_STA_DNS_VALUE 1248 // 4 bytes xxx.xxx.xxx.xxx

// Hidden password
#define HIDDEN_PASSWORD "********"
Expand Down Expand Up @@ -313,14 +317,36 @@ typedef uint ESP3DSettingIndex;
#define MODE_ETH_CLOCK_GPIO16_OUT 2
#define MODE_ETH_CLOCK_GPIO17_OUT 3

// Ethernet type (Check ETH.h eth_phy_type_t)
// Ethernet type (Check ETH.h eth_phy_type_t because it enum with #ifdef CONFIG_....)
#define TYPE_ETH_PHY_LAN8720 0
#define TYPE_ETH_PHY_TLK110 1
#define TYPE_ETH_PHY_RTL8201 2
#define TYPE_ETH_PHY_DP83848 3
#define TYPE_ETH_PHY_DM9051 4
#define TYPE_ETH_PHY_KSZ8041 5
#define TYPE_ETH_PHY_KSZ8081 6
#define TYPE_ETH_PHY_KSZ8041 4
#define TYPE_ETH_PHY_KSZ8081 5
#define TYPE_ETH_PHY_DM9051 6
#define TYPE_ETH_PHY_W5500 7
#define TYPE_ETH_PHY_KSZ8851 8

//SPI pis for ethernet
#ifndef ETH_PHY_CS
#define ETH_PHY_CS 15
#endif // ETH_PHY_CS
#ifndef ETH_PHY_IRQ
#define ETH_PHY_IRQ 4
#endif // ETH_PHY_IRQ
#ifndef ETH_PHY_RST
#define ETH_PHY_RST 5
#endif // ETH_PHY_RST
#ifndef ETH_SPI_SCK
#define ETH_SPI_SCK 14
#endif // ETH_SPI_SCK
#ifndef ETH_SPI_MISO
#define ETH_SPI_MISO 12
#endif // ETH_SPI_MISO
#ifndef ETH_SPI_MOSI
#define ETH_SPI_MOSI 13
#endif // ETH_SPI_MOSI

// Host path
#define ESP3D_HOST_PATH "/"
Expand Down
Loading

0 comments on commit b93e24b

Please sign in to comment.