Skip to content

Commit

Permalink
Merge pull request #134 from codm/mac-hostname
Browse files Browse the repository at this point in the history
Default Hostnames Derived From MAC
  • Loading branch information
xyzroe authored Dec 7, 2024
2 parents 5cc603b + 180ea4b commit cb57751
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
20 changes: 16 additions & 4 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ void loadVpnConfig(VpnConfigStruct &config)

config.hnEnable = preferences.getBool(hnEnableKey, false);
strlcpy(config.hnJoinCode, preferences.getString(hnJoinCodeKey).c_str(), sizeof(config.hnJoinCode));
strlcpy(config.hnHostName, preferences.getString(hnHostNameKey, String(vars.deviceId)).c_str(), sizeof(config.hnHostName));
strlcpy(config.hnDashUrl, preferences.getString(hnDashUrlKey, "default").c_str(), sizeof(config.hnDashUrl));

preferences.end();
Expand Down Expand Up @@ -330,7 +329,6 @@ void loadMqttConfig(MqttConfigStruct &config)
config.port = preferences.getInt(portKey, 1883);
strlcpy(config.user, preferences.getString(userKey, "").c_str(), sizeof(config.user));
strlcpy(config.pass, preferences.getString(passKey, "").c_str(), sizeof(config.pass));
strlcpy(config.topic, preferences.getString(topicKey, String(vars.deviceId)).c_str(), sizeof(config.topic));
// config.retain = preferences.getBool(retain, false); // If needed
config.updateInt = preferences.getInt(updateIntKey, 60);
config.discovery = preferences.getBool(discoveryKey, true);
Expand Down Expand Up @@ -378,6 +376,7 @@ void saveSystemConfig(const SystemConfigStruct &config)
preferences.end();
}


void loadSystemConfig(SystemConfigStruct &config)
{
preferences.begin(systemConfigKey, true);
Expand All @@ -396,7 +395,6 @@ void loadSystemConfig(SystemConfigStruct &config)
config.disableLedUSB = preferences.getBool(disableLedUSBKey, false);
config.disableLedPwr = preferences.getBool(disableLedPwrKey, false);
config.refreshLogs = preferences.getInt(refreshLogsKey, 2);
strlcpy(config.hostname, preferences.getString(hostnameKey, "XZG").c_str(), sizeof(config.hostname)); /// to do add def host name!!
strlcpy(config.timeZone, preferences.getString(timeZoneKey, NTP_TIME_ZONE).c_str(), sizeof(config.timeZone));
strlcpy(config.ntpServ1, preferences.getString(ntpServ1Key, NTP_SERV_1).c_str(), sizeof(config.ntpServ1));
strlcpy(config.ntpServ2, preferences.getString(ntpServ2Key, NTP_SERV_2).c_str(), sizeof(config.ntpServ2));
Expand All @@ -418,6 +416,20 @@ void loadSystemConfig(SystemConfigStruct &config)
preferences.end();
}

void writeDeviceId(SystemConfigStruct &sysConfig, VpnConfigStruct &vpnConfig, MqttConfigStruct &mqttConfig)
{
preferences.begin(systemConfigKey, true);
strlcpy(sysConfig.hostname, preferences.getString(hostnameKey, String(vars.deviceId)).c_str(), sizeof(sysConfig.hostname));
preferences.end();
preferences.begin(vpnConfigKey, true);
strlcpy(vpnConfig.hnHostName, preferences.getString(hnHostNameKey, String(vars.deviceId)).c_str(), sizeof(vpnConfig.hnHostName));
preferences.end();
preferences.begin(mqttConfigKey, true);
strlcpy(mqttConfig.topic, preferences.getString(topicKey, String(vars.deviceId)).c_str(), sizeof(mqttConfig.topic));
preferences.end();
LOGD("Sysconfig hostname: %s", sysConfig.hostname);
}

/*
enum API_PAGE_t : uint8_t
{
Expand Down Expand Up @@ -1475,4 +1487,4 @@ bool loadFileConfigWg()
*/

/* Previous firmware read config support. end */
/* Previous firmware read config support. end */
4 changes: 3 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ bool loadFileConfigHW();
void saveHwConfig(const ThisConfigStruct &config);
void loadHwConfig(ThisConfigStruct &config);

void writeDeviceId(SystemConfigStruct &sysConfig, VpnConfigStruct &vpnConfig, MqttConfigStruct &mqttConfig);

/* Previous firmware read config support. start */
/*
bool loadFileSystemVar();
Expand Down Expand Up @@ -409,4 +411,4 @@ enum updInfoType : uint8_t
{
UPD_ESP,
UPD_ZB
};
};
51 changes: 20 additions & 31 deletions src/etc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,37 +320,26 @@ void usbModeSet(usbMode mode)
//}
}

void getDeviceID(char *arr)
void writeDefaultDeviceId(char *arr, bool ethernet)
{
uint64_t mac = ESP.getEfuseMac(); // Retrieve the MAC address
uint8_t a = 0; // Initialize variables to store the results
uint8_t b = 0;

// Apply XOR operation to each byte of the MAC address to obtain unique values for a and b
a ^= (mac >> (8 * 0)) & 0xFF;
a ^= (mac >> (8 * 1)) & 0xFF;
a ^= (mac >> (8 * 2)) & 0xFF;
a ^= (mac >> (8 * 3)) & 0xFF;
b ^= (mac >> (8 * 4)) & 0xFF;
b ^= (mac >> (8 * 5)) & 0xFF;

char buf[MAX_DEV_ID_LONG];

// Format a and b into buf as hexadecimal values, ensuring two-digit representation for each
sprintf(buf, "%02x%02x", a, b);

// Convert each character in buf to upper case
for (uint8_t cnt = 0; buf[cnt] != '\0'; cnt++)
{
buf[cnt] = toupper(buf[cnt]);
}

// Form the final string including the board name and the processed MAC address

// sprintf(arr, "%s-%s", hwConfig.board, buf);

String devicePref = "XZG"; // hwConfig.board
snprintf(arr, MAX_DEV_ID_LONG + 1, "%s-%s", devicePref.c_str(), buf);
char id_str[MAX_DEV_ID_LONG] = "XZG-";
const size_t id_str_len = strlen(id_str);
union MacAddress {
uint8_t bytes[8];
uint64_t value;
} mac = {};

if (ethernet) {
ETH.macAddress(mac.bytes);
}
else {
mac.value = ESP.getEfuseMac();
}
snprintf(&id_str[id_str_len],
MAX_DEV_ID_LONG - id_str_len,
"%02X%02X",
mac.bytes[4], mac.bytes[5]);
memcpy(arr, id_str, MAX_DEV_ID_LONG);
}

/*void writeDefaultConfig(const char *path, DynamicJsonDocument &doc)
Expand Down Expand Up @@ -1355,4 +1344,4 @@ void firstUpdCheck()
checkFileSys();
}
}
}
}
4 changes: 2 additions & 2 deletions src/etc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void zigbeeRestart();

void usbModeSet(usbMode mode);

void getDeviceID(char *arr);
void writeDefaultDeviceId(char *arr, bool ethernet);
//void writeDefaultConfig(const char *path, DynamicJsonDocument &doc);

#define TIMEOUT_FACTORY_RESET 3
Expand Down Expand Up @@ -71,4 +71,4 @@ struct FirmwareInfo {
String sha;
};

#endif // ETC_H
#endif // ETC_H
11 changes: 7 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,12 +604,13 @@ void networkStart()
tmrNetworkOverseer.attach(overseerInterval, handleTmrNetworkOverseer);
}
WiFi.onEvent(NetworkEvent);
if (networkCfg.ethEnable)
if (networkCfg.ethEnable){
initLan();
}
if (networkCfg.wifiEnable)
connectWifi();
//}

//}
// if (!systemCfg.disableWeb && ((systemCfg.workMode != WORK_MODE_USB) || systemCfg.keepWeb))
// updWeb = true; // handle web server
if (!systemCfg.disableWeb)
Expand Down Expand Up @@ -709,8 +710,6 @@ void setup()

initNVS();

getDeviceID(vars.deviceId); // need for mqtt, vpn, mdns, wifi ap and so on

loadSystemConfig(systemCfg);
loadNetworkConfig(networkCfg);
loadVpnConfig(vpnCfg);
Expand Down Expand Up @@ -784,6 +783,10 @@ void setup()

networkStart();

writeDefaultDeviceId(vars.deviceId, networkCfg.ethEnable); // need for mqtt, vpn, mdns, wifi ap and so on
writeDeviceId(systemCfg, vpnCfg, mqttCfg);


/*while (WiFi.status() != WL_CONNECTED && !vars.connectedEther)
{
delay(1000);
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AUTO GENERATED FILE
#ifndef VERSION
#define VERSION "20241001"
#define VERSION "20241207"
#endif

0 comments on commit cb57751

Please sign in to comment.