Skip to content

Commit

Permalink
bugfix: do not allow empty device name, breaks HomeKit / MDNS
Browse files Browse the repository at this point in the history
  • Loading branch information
dkerr64 committed Oct 26, 2024
1 parent 2cc9381 commit 8379dad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ build_flags =
-D ENABLE_CRASH_LOG
-D NTP_CLIENT
-D LEGACY_SETTINGS_MIGRATION
-D HOMEKIT_USE_IRAM
; -D CRASH_DEBUG
; -D CHUNK_WEB_PAGES
; -D DEBUG_UPDATER=Serial
monitor_filters = esp8266_exception_decoder
lib_deps =
https://github.com/dkerr64/Arduino-HomeKit-ESP8266.git#e7a0de256abd22e8bef878f037778f4f94d86efe
https://github.com/dkerr64/Arduino-HomeKit-ESP8266.git#1e95cd6e5d031b2b806176c34f3bc9a418277fd8
https://github.com/jgstroud/EspSaveCrash.git#cf2803abfa51a83c93548f2591d4564a47845a72
esphome/Improv@^1.2.3
https://github.com/ratgdo/espsoftwareserial.git#autobaud
Expand Down
24 changes: 19 additions & 5 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ char *make_rfc952(char *dest, const char *src, int size)
i++;
}
// remove dashes and periods from end of name
while (dest[i - 1] == '-' || dest[i - 1] == '.')
while (i > 0 && (dest[i - 1] == '-' || dest[i - 1] == '.'))
{
dest[--i] = 0;
}
Expand All @@ -101,9 +101,11 @@ void load_all_config_settings()
// to exceed available IRAM. We can adjust the LOG_BUFFER_SIZE (in log.h) if we
// need to make more space available for initialization.
userConfig = (userConfig_t *)malloc(sizeof(userConfig_t));
*userConfig = (userConfig_t){}; // Initializes with defaults defined in typedef.
IRAM_END("User config buffer allocated");
}
// Initialize with defaults.
*userConfig = (userConfig_t){}; // Initializes with defaults defined in typedef.
strlcpy(userConfig->deviceName, device_name, sizeof(userConfig->deviceName));
if (!read_config_from_file())
{
#ifdef LEGACY_SETTINGS_MIGRATION
Expand Down Expand Up @@ -182,10 +184,22 @@ void load_all_config_settings()
write_config_to_file();
}

// All user configuration loaded, set globals...
// Check we have a legal device name...
make_rfc952(device_name_rfc952, userConfig->deviceName, sizeof(device_name_rfc952));
if (strlen(device_name_rfc952) == 0)
{
// cannot have a empty device name, reset to default...
strlcpy(userConfig->deviceName, device_name, sizeof(userConfig->deviceName));
make_rfc952(device_name_rfc952, userConfig->deviceName, sizeof(device_name_rfc952));
}
else
{
// device name okay, copy it to our global
strlcpy(device_name, userConfig->deviceName, sizeof(device_name));
}

// Set rest of globals...
led.setIdleState((uint8_t)userConfig->ledIdleState);
strlcpy(device_name, userConfig->deviceName, sizeof(device_name));
make_rfc952(device_name_rfc952, device_name, sizeof(device_name_rfc952));
motionTriggers.asInt = userConfig->motionTriggers;
softAPmode = userConfig->softAPmode;
#ifdef NTP_CLIENT
Expand Down
2 changes: 1 addition & 1 deletion src/www/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ async function saveSettings() {
const LEDidle = (document.getElementById("LEDidle").checked) ? 1 : 0;
let rebootHours = Math.max(Math.min(parseInt(document.getElementById("rebootHours").value), 72), 0);
if (isNaN(rebootHours)) rebootHours = 0;
let newDeviceName = document.getElementById("newDeviceName").value.substring(0, 30);
let newDeviceName = document.getElementById("newDeviceName").value.substring(0, 30).trim();
if (newDeviceName.length == 0) newDeviceName = serverStatus.deviceName;
const wifiPhyMode = (document.getElementById("wifiPhyMode3").checked) ? '3'
: (document.getElementById("wifiPhyMode2").checked) ? '2'
Expand Down

0 comments on commit 8379dad

Please sign in to comment.