Skip to content

Commit

Permalink
Merge pull request #755 from MartinMueller2003/main
Browse files Browse the repository at this point in the history
Added support for manually configured Primary and secondary DNS addresses used while in Static IP mode.
  • Loading branch information
forkineye authored Mar 19, 2024
2 parents 778d635 + a5efce9 commit b65d9a2
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 68 deletions.
3 changes: 2 additions & 1 deletion ESPixelStick/ESPixelStick.ino
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void setup()
// TestHeap(uint32_t(10));
// DEBUG_V("");
FileMgr.Begin();

#ifdef SUPPORT_CONFIG_MERGE
if(FileMgr.FlashFileExists (RestoredConfigFileName))
{
// DEBUG_V("Setting Restored Config flag to true");
Expand All @@ -183,6 +183,7 @@ void setup()
{
// DEBUG_V("Setting Restored Config flag to false");
}
#endif // def SUPPORT_CONFIG_MERGE
// Load configuration from the File System and set Hostname
// TestHeap(uint32_t(15));
// DEBUG_V(String("LoadConfig Heap: ") + String(ESP.getFreeHeap()));
Expand Down
2 changes: 2 additions & 0 deletions ESPixelStick/src/ConstNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const CN_PROGMEM char CN_Default [] = "Default";
const CN_PROGMEM char CN_device [] = "device";
const CN_PROGMEM char CN_dhcp [] = "dhcp";
const CN_PROGMEM char CN_Disabled [] = "Disabled";
const CN_PROGMEM char CN_dnsp [] = "dnsp";
const CN_PROGMEM char CN_dnss [] = "dnss";
const CN_PROGMEM char CN_DMX [] = "DMX";
const CN_PROGMEM char CN_Dotfseq [] = ".fseq";
const CN_PROGMEM char CN_Dotjson [] = ".json";
Expand Down
2 changes: 2 additions & 0 deletions ESPixelStick/src/ConstNames.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ extern const CN_PROGMEM char CN_device [];
extern const CN_PROGMEM char CN_dhcp [];
extern const CN_PROGMEM char CN_Default [];
extern const CN_PROGMEM char CN_Disabled [];
extern const CN_PROGMEM char CN_dnsp [];
extern const CN_PROGMEM char CN_dnss [];
extern const CN_PROGMEM char CN_Dotfseq [];
extern const CN_PROGMEM char CN_Dotjson [];
extern const CN_PROGMEM char CN_Dotpl [];
Expand Down
60 changes: 46 additions & 14 deletions ESPixelStick/src/input/InputMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,14 @@ void c_InputMgr::CreateNewConfig ()
// do we create a clean config or do we merge from a restored config?
if(RestoredConfig)
{
// DEBUG_V("Merge a Restored Config");
DEBUG_V("Merge a Restored Input Config");
// read the existing file and add to it as needed
FileMgr.ReadFlashFile(ConfigFileName, JsonConfigDoc);
}

if(!JsonConfigDoc.containsKey(CN_input_config))
{
DEBUG_V("Add missing input config fields");
JsonConfigDoc.createNestedObject(CN_input_config);
}
JsonObject JsonConfig = JsonConfigDoc[CN_input_config];
Expand Down Expand Up @@ -746,16 +747,16 @@ void c_InputMgr::ProcessButtonActions (c_ExternalInput::InputValue_t value)
} // ProcessButtonActions

//-----------------------------------------------------------------------------
/*
check the contents of the config and send
the proper portion of the config to the currently instantiated channels
*/
bool c_InputMgr::ProcessJsonConfig (JsonObject & jsonConfig)
bool c_InputMgr::FindJsonChannelConfig (JsonObject& jsonConfig,
e_InputChannelIds ChanId,
JsonObject& ChanConfig)
{
// DEBUG_START;
bool Response = false;
// DEBUG_V ();

// DEBUG_V ("InputDataBufferSize: " + String (InputDataBufferSize));
// extern void PrettyPrint(JsonObject & jsonStuff, String Name);
// PrettyPrint(jsonConfig, "ProcessJsonConfig");

do // once
{
Expand Down Expand Up @@ -805,21 +806,52 @@ bool c_InputMgr::ProcessJsonConfig (JsonObject & jsonConfig)
JsonObject InputChannelArray = InputChannelMgrData[CN_channels];
// DEBUG_V ("");

// get access to the channel config
if (false == InputChannelArray.containsKey (String (ChanId)))
{
// if not, flag an error and stop processing
logcon (String (F ("No Input Settings Found for Channel '")) + ChanId + String (F ("'. Using Defaults")));
continue;
}
ChanConfig = InputChannelArray[String(ChanId)];
// DEBUG_V ();

// all went well
Response = true;

} while (false);

// DEBUG_END;
return Response;

} // FindChannelJsonConfig

//-----------------------------------------------------------------------------
/*
check the contents of the config and send
the proper portion of the config to the currently instantiated channels
*/
bool c_InputMgr::ProcessJsonConfig (JsonObject & jsonConfig)
{
// DEBUG_START;
bool Response = false;

// DEBUG_V ("InputDataBufferSize: " + String (InputDataBufferSize));

do // once
{
// for each Input channel
for (uint32_t ChannelIndex = uint32_t (InputChannelId_Start);
ChannelIndex < uint32_t (InputChannelId_End);
ChannelIndex++)
{
// get access to the channel config
if (false == InputChannelArray.containsKey (String (ChannelIndex)))
JsonObject InputChannelConfig;
// DEBUG_V ("");
if(!FindJsonChannelConfig(jsonConfig, e_InputChannelIds(ChannelIndex), InputChannelConfig))
{
// if not, flag an error and stop processing
logcon (String (F ("No Input Settings Found for Channel '")) + ChannelIndex + String (F ("'. Using Defaults")));
DEBUG_V("Did not find the desired channel configuration");
continue;
}
JsonObject InputChannelConfig = InputChannelArray[String (ChannelIndex)];
// DEBUG_V ("");

// set a default value for channel type
uint32_t ChannelType = uint32_t (InputType_Default);
setFromJSON (ChannelType, InputChannelConfig, CN_type);
Expand Down
1 change: 1 addition & 0 deletions ESPixelStick/src/input/InputMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class c_InputMgr
void CreateJsonConfig (JsonObject & jsonConfig);
bool ProcessJsonChannelConfig (JsonObject & jsonConfig, uint32_t ChannelIndex);
bool InputTypeIsAllowedOnChannel (e_InputType type, e_InputChannelIds ChannelId);
bool FindJsonChannelConfig (JsonObject& jsonConfig, e_InputChannelIds ChanId, JsonObject& ChanConfig);

String ConfigFileName;
bool rebootNeeded = false;
Expand Down
16 changes: 14 additions & 2 deletions ESPixelStick/src/network/EthernetDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ void c_EthernetDriver::GetConfig (JsonObject& json)
json[CN_ip] = ip.toString ();
json[CN_netmask] = netmask.toString ();
json[CN_gateway] = gateway.toString ();
json[CN_dnsp] = primaryDns.toString ();
json[CN_dnss] = secondaryDns.toString ();
json[CN_dhcp] = UseDhcp;

json[CN_type] = phy_type;
Expand Down Expand Up @@ -321,10 +323,14 @@ bool c_EthernetDriver::SetConfig (JsonObject & json)
String sIP = ip.toString ();
String sGateway = gateway.toString ();
String sNetmask = netmask.toString ();
String sDnsp = primaryDns.toString ();
String sDnss = secondaryDns.toString ();

ConfigChanged |= setFromJSON (sIP, json, CN_ip);
ConfigChanged |= setFromJSON (sNetmask, json, CN_netmask);
ConfigChanged |= setFromJSON (sGateway, json, CN_gateway);
ConfigChanged |= setFromJSON (sDnsp, json, CN_dnsp);
ConfigChanged |= setFromJSON (sDnss, json, CN_dnss);
ConfigChanged |= setFromJSON (UseDhcp, json, CN_dhcp);

ConfigChanged |= setFromJSON (phy_addr, json, CN_addr);
Expand All @@ -339,7 +345,9 @@ bool c_EthernetDriver::SetConfig (JsonObject & json)
ip.fromString (sIP);
gateway.fromString (sGateway);
netmask.fromString (sNetmask);

primaryDns.fromString (sDnsp);
secondaryDns.fromString (sDnss);

// DEBUG_V (String (" sip: ") + ip.toString ());
// DEBUG_V (String ("sgateway: ") + gateway.toString ());
// DEBUG_V (String ("snetmask: ") + netmask.toString ());
Expand Down Expand Up @@ -397,10 +405,14 @@ void c_EthernetDriver::SetUpIp ()
// DEBUG_V ("netmask: " + netmask.toString ());
// DEBUG_V ("gateway: " + gateway.toString ());

if(primaryDns == INADDR_NONE)
{
primaryDns = gateway;
}
// We didn't use DNS, so just set it to our configured gateway
// https://github.com/espressif/arduino-esp32/issues/5733 - add delay
delay(100);
ETH.config (ip, gateway, netmask, gateway);
ETH.config (ip, gateway, netmask, primaryDns, secondaryDns);

logcon (F ("Connecting to Ethernet with Static IP"));

Expand Down
2 changes: 2 additions & 0 deletions ESPixelStick/src/network/EthernetDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class c_EthernetDriver
IPAddress ip = INADDR_NONE;
IPAddress netmask = INADDR_NONE;
IPAddress gateway = INADDR_NONE;
IPAddress primaryDns = INADDR_NONE;
IPAddress secondaryDns = INADDR_NONE;
bool UseDhcp = true;
uint32_t phy_addr = DEFAULT_ETH_ADDR;
gpio_num_t power_pin = DEFAULT_ETH_POWER_PIN;
Expand Down
22 changes: 21 additions & 1 deletion ESPixelStick/src/network/WiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,16 @@ void c_WiFiDriver::GetConfig (JsonObject& json)
json[CN_netmask] = Temp.toString ();
Temp = gateway;
json[CN_gateway] = Temp.toString ();
Temp = primaryDns;
json[CN_dnsp] = Temp.toString ();
Temp = secondaryDns;
json[CN_dnss] = Temp.toString ();
#else
json[CN_ip] = ip.toString ();
json[CN_netmask] = netmask.toString ();
json[CN_gateway] = gateway.toString ();
json[CN_dnsp] = primaryDns.toString ();
json[CN_dnss] = secondaryDns.toString ();
#endif // !def ARDUINO_ARCH_ESP8266

json[CN_StayInApMode] = StayInApMode;
Expand Down Expand Up @@ -481,11 +487,16 @@ bool c_WiFiDriver::SetConfig (JsonObject & json)
String sIp = ip.toString ();
String sGateway = gateway.toString ();
String sNetmask = netmask.toString ();
String sdnsp = primaryDns.toString ();
String sdnss = secondaryDns.toString ();

ConfigChanged |= setFromJSON (ssid, json, CN_ssid);
ConfigChanged |= setFromJSON (passphrase, json, CN_passphrase);
ConfigChanged |= setFromJSON (sIp, json, CN_ip);
ConfigChanged |= setFromJSON (sNetmask, json, CN_netmask);
ConfigChanged |= setFromJSON (sGateway, json, CN_gateway);
ConfigChanged |= setFromJSON (sdnsp, json, CN_dnsp);
ConfigChanged |= setFromJSON (sdnss, json, CN_dnss);
ConfigChanged |= setFromJSON (UseDhcp, json, CN_dhcp);
ConfigChanged |= setFromJSON (ap_ssid, json, CN_ap_ssid);
ConfigChanged |= setFromJSON (ap_passphrase, json, CN_ap_passphrase);
Expand All @@ -507,6 +518,8 @@ bool c_WiFiDriver::SetConfig (JsonObject & json)
ip.fromString (sIp);
gateway.fromString (sGateway);
netmask.fromString (sNetmask);
primaryDns.fromString (sdnsp);
secondaryDns.fromString (sdnss);

if((passphrase.length() < 8) && (passphrase.length() > 0))
{
Expand Down Expand Up @@ -591,8 +604,15 @@ void c_WiFiDriver::SetUpIp ()
// correct IP is already set
break;
}

// use gateway if primary DNS is not defined
if(primaryDns == INADDR_NONE)
{
primaryDns = gateway;
}

// We didn't use DNS, so just set it to our configured gateway
WiFi.config (ip, gateway, netmask, gateway);
WiFi.config (ip, gateway, netmask, primaryDns, secondaryDns);

logcon (F ("Using Static IP"));

Expand Down
8 changes: 5 additions & 3 deletions ESPixelStick/src/network/WiFiDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ class c_WiFiDriver
String passphrase;
String ap_ssid;
String ap_passphrase;
IPAddress ip = IPAddress ((uint32_t)0);
IPAddress netmask = IPAddress ((uint32_t)0);
IPAddress gateway = IPAddress ((uint32_t)0);
IPAddress ip = INADDR_NONE;
IPAddress netmask = INADDR_NONE;
IPAddress gateway = INADDR_NONE;
IPAddress primaryDns = INADDR_NONE;
IPAddress secondaryDns = INADDR_NONE;
bool UseDhcp = true;
uint8_t ap_channelNumber = 1;
bool ap_fallbackIsEnabled = true;
Expand Down
Loading

0 comments on commit b65d9a2

Please sign in to comment.