Skip to content

Commit

Permalink
debug: rework boot info and terminal commands
Browse files Browse the repository at this point in the history
- reduce info lines on boot
- more compact `info` command. show versions, modules and crash info when there is one
- add `storage` command to display flash layout (experimental)
- display full chip id aka MAC, including the oui
- fix telnet never printing the crash data b/c telnet in not yet authorized
- fix eeprom size not reflecting the space used by the backup sectors
- use static flash strings when possible for the fw info
  • Loading branch information
mcspr committed Apr 7, 2021
1 parent 0ee55ba commit 7ea7355
Show file tree
Hide file tree
Showing 18 changed files with 655 additions and 958 deletions.
1,004 changes: 352 additions & 652 deletions code/espurna/board.cpp

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions code/espurna/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ BOARD MODULE
#include <Arduino.h>

const String& getChipId();
const String& getFullChipId();
const String& getIdentifier();

String getEspurnaModules();
String getEspurnaOTAModules();
String getEspurnaSensors();
const char* getEspurnaModules();
const char* getEspurnaSensors();
const char* getEspurnaWebUI();

String getEspurnaWebUI();

bool isEspurnaCore();

int getBoardId();
void boardSetup();
10 changes: 10 additions & 0 deletions code/espurna/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ COMPATIBILITY BETWEEN 2.3.0 and latest versions

#include "espurna.h"

// -----------------------------------------------------------------------------

inline constexpr bool isEspurnaCore() {
#if defined(ESPURNA_CORE) || defined(ESPURNA_CORE_WEBUI)
return true;
#else
return false;
#endif
}

// -----------------------------------------------------------------------------
// Core version 2.4.2 and higher changed the cont_t structure to a pointer:
// https://github.com/esp8266/Arduino/commit/5d5ea92a4d004ab009d5f642629946a0cb8893dd#diff-3fa12668b289ccb95b7ab334833a4ba8L35
Expand Down
41 changes: 27 additions & 14 deletions code/espurna/crash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ Copyright (C) 2019-2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com
#include <stdarg.h>

#include "system.h"
#include "rtcmem.h"
#include "storage_eeprom.h"

constexpr uint32_t EmptyTimestamp { 0xffffffff };

bool _save_crash_enabled = true;

size_t crashReservedSize() {
Expand Down Expand Up @@ -101,8 +104,7 @@ extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack
* Clears crash info CRASH_TIME value, later checked in crashDump()
*/
void crashClear() {
uint32_t crash_time = 0xFFFFFFFF;
eepromPut(EepromCrashBegin + SAVE_CRASH_CRASH_TIME, crash_time);
eepromPut(EepromCrashBegin + SAVE_CRASH_CRASH_TIME, EmptyTimestamp);
eepromCommit();
}

Expand All @@ -119,14 +121,14 @@ void _crashDump(Print& print, bool check) {
uint32_t crash_time;
eepromGet(EepromCrashBegin + SAVE_CRASH_CRASH_TIME, crash_time);

bool crash_time_erased = ((crash_time == 0) || (crash_time == 0xFFFFFFFF));
bool crash_time_erased = ((crash_time == 0) || (crash_time == EmptyTimestamp));
if (check && crash_time_erased) {
return;
}

uint8_t reason = eepromRead(EepromCrashBegin + SAVE_CRASH_RESTART_REASON);
if (!crash_time_erased) {
snprintf_P(buffer, sizeof(buffer), PSTR("\nLatest crash was at %lu ms after boot\n"), crash_time);
snprintf_P(buffer, sizeof(buffer), PSTR("\nlatest crash was at %lu ms after boot\n"), crash_time);
print.print(buffer);
}

Expand Down Expand Up @@ -214,12 +216,7 @@ void _crashDump(Print& print, bool check) {
#if TERMINAL_SUPPORT

void _crashTerminalCommand(const terminal::CommandContext& ctx) {
if ((ctx.argc == 2) && (ctx.argv[1].equals(F("force")))) {
crashForceDump(ctx.output);
} else {
crashDump(ctx.output);
crashClear();
}
crashForceDump(ctx.output);
terminalOK(ctx);
}

Expand All @@ -235,14 +232,30 @@ void crashDump(Print& print) {
_crashDump(print, true);
}

void crashResetReason(Print& print) {
auto reason = customResetReason();
bool custom { CustomResetReason::None != reason };
print.printf_P(PSTR("last reset reason: %s\n"), custom
? customResetReasonToPayload(reason).c_str()
: ESP.getResetReason().c_str());

if (!custom) {
print.printf_P(PSTR("extra info: %s\n"), ESP.getResetInfo().c_str());
}

crashDump(print);
}

void crashSetup() {
if (!rtcmemStatus()) {
crashClear();
}

#if TERMINAL_SUPPORT
terminalRegisterCommand(F("CRASH"), _crashTerminalCommand);
#endif
#if TERMINAL_SUPPORT
terminalRegisterCommand(F("CRASH"), _crashTerminalCommand);
#endif

_save_crash_enabled = getSetting("sysCrashSave", 1 == SAVE_CRASH_ENABLED);

}

#endif // DEBUG_SUPPORT
1 change: 1 addition & 0 deletions code/espurna/crash.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ constexpr size_t CrashTraceReservedSize = CrashReservedSize - SAVE_CRASH_STACK_T

size_t crashReservedSize();

void crashResetReason(Print&);
void crashForceDump(Print&);
void crashDump(Print&);
void crashClear();
Expand Down
8 changes: 5 additions & 3 deletions code/espurna/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ char _udp_syslog_header[64];

bool _debug_enabled = false;


// -----------------------------------------------------------------------------
// printf-like debug methods
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -376,8 +375,11 @@ bool _debugHeartbeat(heartbeat::Mask mask) {
if (mask & heartbeat::Report::Uptime)
DEBUG_MSG_P(PSTR("[MAIN] Uptime: %s\n"), getUptime().c_str());

if (mask & heartbeat::Report::Freeheap)
infoHeapStats();
if (mask & heartbeat::Report::Freeheap) {
auto stats = systemHeapStats();
DEBUG_MSG_P(PSTR("[MAIN] %5u / %5u bytes available (%5u contiguous)\n"),
stats.available, systemInitialFreeHeap(), stats.usable);
}

if ((mask & heartbeat::Report::Vcc) && (ADC_MODE_VALUE == ADC_VCC))
DEBUG_MSG_P(PSTR("[MAIN] Power: %lu mV\n"), ESP.getVcc());
Expand Down
16 changes: 8 additions & 8 deletions code/espurna/homeassistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Device {
Strings(const Strings&) = delete;

Strings(Strings&&) = default;
Strings(String&& prefix_, String&& name_, const String& identifier_, const String& version_, const String& manufacturer_, const String& device_) :
Strings(String&& prefix_, String&& name_, const String& identifier_, const char* version_, const char* manufacturer_, const char* device_) :
prefix(std::move(prefix_)),
name(std::move(name_)),
identifier(identifier_),
Expand All @@ -80,9 +80,9 @@ class Device {
String prefix;
String name;
String identifier;
String version;
String manufacturer;
String device;
const char* version;
const char* manufacturer;
const char* device;
};

using StringsPtr = std::unique_ptr<Strings>;
Expand All @@ -96,7 +96,7 @@ class Device {
Device(const Device&) = delete;

Device(Device&&) = default;
Device(String&& prefix, String&& name, const String& identifier, const String& version, const String& manufacturer, const String& device) :
Device(String&& prefix, String&& name, const String& identifier, const char* version, const char* manufacturer, const char* device) :
_strings(std::make_unique<Strings>(std::move(prefix), std::move(name), identifier, version, manufacturer, device)),
_buffer(std::make_unique<Buffer>()),
_root(_buffer->createObject())
Expand All @@ -105,9 +105,9 @@ class Device {
ids.add(_strings->identifier.c_str());

_root["name"] = _strings->name.c_str();
_root["sw"] = _strings->version.c_str();
_root["mf"] = _strings->manufacturer.c_str();
_root["mdl"] = _strings->device.c_str();
_root["sw"] = _strings->version;
_root["mf"] = _strings->manufacturer;
_root["mdl"] = _strings->device;
}

const String& name() const {
Expand Down
5 changes: 2 additions & 3 deletions code/espurna/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ void setup() {
}
setBoardName();

// Show welcome message and system configuration
info(true);

boardSetup();
wifiSetup();
otaSetup();

Expand Down Expand Up @@ -290,6 +288,7 @@ void setup() {
if (_loop_delay != loop_delay) {
setSetting("loopDelay", _loop_delay);
}

}

void loop() {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ bool _mqttHeartbeat(heartbeat::Mask mask) {
mqttSend(MQTT_TOPIC_APP, APP_NAME);

if (mask & heartbeat::Report::Version)
mqttSend(MQTT_TOPIC_VERSION, getVersion().c_str());
mqttSend(MQTT_TOPIC_VERSION, getVersion());

if (mask & heartbeat::Report::Board)
mqttSend(MQTT_TOPIC_BOARD, getBoardName().c_str());
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/storage_eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void eepromSetup();
extern EEPROM_Rotate EEPROMr;

inline unsigned long eepromSpace() {
return EEPROMr.reserved() * SPI_FLASH_SEC_SIZE;
return EEPROMr.size() * SPI_FLASH_SEC_SIZE;
}

inline void eepromClear() {
Expand Down
14 changes: 6 additions & 8 deletions code/espurna/telnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Updated to use WiFiServer and support reverse connections by Niek van der Maas <
#include <vector>

#include "board.h"
#include "crash.h"
#include "terminal.h"
#include "ws.h"

#if TELNET_SERVER == TELNET_SERVER_ASYNC
Expand Down Expand Up @@ -360,14 +362,6 @@ void _telnetNotifyConnected(unsigned char i) {

DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);

// If there is no terminal support automatically dump info and crash data
#if DEBUG_SUPPORT
#if not TERMINAL_SUPPORT
crashDump(terminalDefaultStream());
crashClear();
#endif
#endif

if (!isEspurnaCore()) {
_telnetClientsAuth[i] = !_telnetAuth;
if (_telnetAuth) {
Expand All @@ -381,6 +375,10 @@ void _telnetNotifyConnected(unsigned char i) {
_telnetClientsAuth[i] = true;
}

#if DEBUG_SUPPORT
crashResetReason(terminalDefaultStream());
#endif

}

#if TELNET_SERVER == TELNET_SERVER_WIFISERVER
Expand Down
Loading

0 comments on commit 7ea7355

Please sign in to comment.