Skip to content

Commit

Permalink
Merge pull request #132 from lora-aprs/hostname_update
Browse files Browse the repository at this point in the history
fix hostname and static ip settings
  • Loading branch information
peterus authored Dec 18, 2021
2 parents 3d6dded + f1e64a6 commit 4bd2b91
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 34 deletions.
17 changes: 11 additions & 6 deletions data/is-cfg.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{
"callsign": "NOCALL-10",
"network": {
"hostname": "NOCALL-10",
"DHCP": true,
"staticIP": "192.0.2.100",
"subnet": "255.255.255.0",
"gateway": "192.0.2.1",
"dns1": "192.0.2.1",
"dns2": "192.0.2.2"
"static": {
"ip": "192.168.0.100",
"subnet": "255.255.255.0",
"gateway": "192.168.0.1",
"dns1": "192.168.0.1",
"dns2": "192.168.0.2"
},
"hostname": {
"overwrite": false,
"name": "NOCALL-10"
}
},
"wifi": {
"active": true,
Expand Down
9 changes: 6 additions & 3 deletions src/TaskEth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ void WiFiEvent(WiFiEvent_t event) {
break;
case SYSTEM_EVENT_ETH_CONNECTED:
logPrintlnI("ETH Connected");
ETH.setHostname("esp32-ethernet");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
logPrintI("Hostname: ");
Expand Down Expand Up @@ -105,9 +104,13 @@ bool EthTask::setup(System &system) {
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK);

if (!system.getUserConfig()->network.DHCP) {
ETH.config(system.getUserConfig()->network.staticIP, system.getUserConfig()->network.gateway, system.getUserConfig()->network.subnet, system.getUserConfig()->network.dns1, system.getUserConfig()->network.dns2);
ETH.config(system.getUserConfig()->network.static_.ip, system.getUserConfig()->network.static_.gateway, system.getUserConfig()->network.static_.subnet, system.getUserConfig()->network.static_.dns1, system.getUserConfig()->network.static_.dns2);
}
if (system.getUserConfig()->network.hostname.overwrite) {
ETH.setHostname(system.getUserConfig()->network.hostname.name.c_str());
} else {
ETH.setHostname(system.getUserConfig()->callsign.c_str());
}
ETH.setHostname(system.getUserConfig()->network.hostname.c_str());
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion src/TaskOTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ bool OTATask::setup(System &system) {
else if (error == OTA_END_ERROR)
logPrintlnE("End Failed");
});
_ota.setHostname(system.getUserConfig()->callsign.c_str());
if (system.getUserConfig()->network.hostname.overwrite) {
_ota.setHostname(system.getUserConfig()->network.hostname.name.c_str());
} else {
_ota.setHostname(system.getUserConfig()->callsign.c_str());
}
_stateInfo = "";
return true;
}
Expand Down
8 changes: 6 additions & 2 deletions src/TaskWifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ bool WifiTask::setup(System &system) {
WiFi.mode(WIFI_STA);

WiFi.onEvent(WiFiEvent);
WiFi.setHostname(system.getUserConfig()->callsign.c_str());
if (system.getUserConfig()->network.hostname.overwrite) {
WiFi.setHostname(system.getUserConfig()->network.hostname.name.c_str());
} else {
WiFi.setHostname(system.getUserConfig()->callsign.c_str());
}

if (!system.getUserConfig()->network.DHCP) {
WiFi.config(system.getUserConfig()->network.staticIP, system.getUserConfig()->network.gateway, system.getUserConfig()->network.subnet, system.getUserConfig()->network.dns1, system.getUserConfig()->network.dns2);
WiFi.config(system.getUserConfig()->network.static_.ip, system.getUserConfig()->network.static_.gateway, system.getUserConfig()->network.static_.subnet, system.getUserConfig()->network.static_.dns1, system.getUserConfig()->network.static_.dns2);
}

for (Configuration::Wifi::AP ap : system.getUserConfig()->wifi.APs) {
Expand Down
42 changes: 27 additions & 15 deletions src/project_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ void ProjectConfigurationManagement::readProjectConfiguration(DynamicJsonDocumen
if (data.containsKey("callsign"))
conf.callsign = data["callsign"].as<String>();

if (data.containsKey("network") && data["network"].containsKey("DHCP")) {
conf.network.hostname = data["network"]["hostname"].as<String>();
conf.network.DHCP = data["network"]["DHCP"];
conf.network.staticIP.fromString(data["network"]["staticIP"].as<String>());
conf.network.subnet.fromString(data["network"]["subnet"].as<String>());
conf.network.gateway.fromString(data["network"]["gateway"].as<String>());
conf.network.dns1.fromString(data["network"]["dns1"].as<String>());
conf.network.dns2.fromString(data["network"]["dns2"].as<String>());
if (data.containsKey("network")) {
conf.network.DHCP = data["network"]["DHCP"] | false;
if (data["network"].containsKey("static")) {
if (data["network"]["static"].containsKey("ip"))
conf.network.static_.ip.fromString(data["network"]["static"]["ip"].as<String>());
if (data["network"]["static"].containsKey("subnet"))
conf.network.static_.subnet.fromString(data["network"]["static"]["subnet"].as<String>());
if (data["network"]["static"].containsKey("gateway"))
conf.network.static_.gateway.fromString(data["network"]["static"]["gateway"].as<String>());
if (data["network"]["static"].containsKey("dns1"))
conf.network.static_.dns1.fromString(data["network"]["static"]["dns1"].as<String>());
if (data["network"]["static"].containsKey("dns2"))
conf.network.static_.dns2.fromString(data["network"]["static"]["dns2"].as<String>());
}
if (data["network"].containsKey("hostname")) {
conf.network.hostname.overwrite = data["network"]["hostname"]["overwrite"] | false;
if (data["network"]["hostname"].containsKey("name"))
conf.network.hostname.name = data["network"]["hostname"]["name"].as<String>();
}
}

conf.wifi.active = data["wifi"]["active"];
Expand Down Expand Up @@ -77,13 +88,14 @@ void ProjectConfigurationManagement::writeProjectConfiguration(Configuration &co
data["callsign"] = conf.callsign;

if (!conf.network.DHCP) {
data["network"]["hostname"] = conf.network.hostname;
data["network"]["DHCP"] = conf.network.DHCP;
data["network"]["staticIP"] = conf.network.staticIP.toString();
data["network"]["subnet"] = conf.network.subnet.toString();
data["network"]["gateway"] = conf.network.gateway.toString();
data["network"]["dns1"] = conf.network.dns1.toString();
data["network"]["dns2"] = conf.network.dns2.toString();
data["network"]["DHCP"] = conf.network.DHCP;
data["network"]["static"]["ip"] = conf.network.static_.ip.toString();
data["network"]["static"]["subnet"] = conf.network.static_.subnet.toString();
data["network"]["static"]["gateway"] = conf.network.static_.gateway.toString();
data["network"]["static"]["dns1"] = conf.network.static_.dns1.toString();
data["network"]["static"]["dns2"] = conf.network.static_.dns2.toString();
data["network"]["hostname"]["overwrite"] = conf.network.hostname.overwrite;
data["network"]["hostname"]["name"] = conf.network.hostname.name;
}

data["wifi"]["active"] = conf.wifi.active;
Expand Down
25 changes: 18 additions & 7 deletions src/project_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@

class Configuration {
public:
class Network {
class Static {
public:
Network() : DHCP(true) {
}

String hostname;
bool DHCP;
IPAddress staticIP;
IPAddress ip;
IPAddress subnet;
IPAddress gateway;
IPAddress dns1;
IPAddress dns2;
};

class Hostname {
public:
bool overwrite;
String name;
};

class Network {
public:
Network() : DHCP(true) {
}

bool DHCP;
Static static_;
Hostname hostname;
};

class Wifi {
public:
Wifi() : active(true) {
Expand Down

0 comments on commit 4bd2b91

Please sign in to comment.