feat(mqtt): add link diagnostics and improve network logging
This commit is contained in:
1 parent
366b086713
commit
3e3eec8d5d
11 files changed
+317
-30
No files matched your search
@@ -15,20 +15,39 @@ enum class NetworkMedium : uint8_t {
|
||||
WiFi,
|
||||
};
|
||||
|
||||
enum class NetworkDiagnosticReason : uint8_t {
|
||||
EthernetActive,
|
||||
EthernetInitFailed,
|
||||
EthernetLinkUnknown,
|
||||
EthernetLinkDown,
|
||||
EthernetAwaitingIp,
|
||||
EthernetStabilizing,
|
||||
SwitchingLocked,
|
||||
EthernetReady,
|
||||
};
|
||||
|
||||
namespace NetworkPolicy {
|
||||
|
||||
struct MQTTTransitionActions {
|
||||
bool disconnect_slots;
|
||||
bool stop_started_slots;
|
||||
bool retry_disconnected_slots_now;
|
||||
bool reset_reconnect_backoff;
|
||||
};
|
||||
|
||||
static constexpr MQTTTransitionActions mqttActions(NetworkTransition transition) {
|
||||
return {
|
||||
transition == NetworkTransition::Down || transition == NetworkTransition::Switched,
|
||||
transition == NetworkTransition::Up || transition == NetworkTransition::Switched,
|
||||
transition == NetworkTransition::Up || transition == NetworkTransition::Switched,
|
||||
};
|
||||
}
|
||||
|
||||
static constexpr const char* mediumName(NetworkMedium medium) {
|
||||
return medium == NetworkMedium::Ethernet ? "ethernet"
|
||||
: medium == NetworkMedium::WiFi ? "wifi"
|
||||
: "none";
|
||||
}
|
||||
|
||||
struct AutomaticSelectionInput {
|
||||
NetworkMedium selected;
|
||||
bool ethernet_connected;
|
||||
@@ -46,6 +65,58 @@ static constexpr uint32_t kEthernetDownGraceMs = 3000;
|
||||
static constexpr uint32_t kEthernetFailbackStableMs = 10000;
|
||||
static constexpr uint32_t kNtpRetryMs = 30000;
|
||||
|
||||
static inline NetworkDiagnosticReason automaticDiagnosticReason(
|
||||
bool ethernet_initialized, bool ethernet_link_known,
|
||||
bool ethernet_link_up, bool ethernet_connected, NetworkMedium selected,
|
||||
bool switching_locked, uint32_t ethernet_stable_ms) {
|
||||
if (!ethernet_initialized) {
|
||||
return NetworkDiagnosticReason::EthernetInitFailed;
|
||||
}
|
||||
if (!ethernet_link_known) {
|
||||
return NetworkDiagnosticReason::EthernetLinkUnknown;
|
||||
}
|
||||
if (!ethernet_link_up) {
|
||||
return NetworkDiagnosticReason::EthernetLinkDown;
|
||||
}
|
||||
if (!ethernet_connected) {
|
||||
return NetworkDiagnosticReason::EthernetAwaitingIp;
|
||||
}
|
||||
if (selected == NetworkMedium::Ethernet) {
|
||||
return NetworkDiagnosticReason::EthernetActive;
|
||||
}
|
||||
if (switching_locked) {
|
||||
return NetworkDiagnosticReason::SwitchingLocked;
|
||||
}
|
||||
if (selected == NetworkMedium::WiFi &&
|
||||
ethernet_stable_ms < kEthernetFailbackStableMs) {
|
||||
return NetworkDiagnosticReason::EthernetStabilizing;
|
||||
}
|
||||
return NetworkDiagnosticReason::EthernetReady;
|
||||
}
|
||||
|
||||
static inline const char* diagnosticReasonName(
|
||||
NetworkDiagnosticReason reason) {
|
||||
switch (reason) {
|
||||
case NetworkDiagnosticReason::EthernetActive:
|
||||
return "ethernet-active";
|
||||
case NetworkDiagnosticReason::EthernetInitFailed:
|
||||
return "ethernet-init-failed";
|
||||
case NetworkDiagnosticReason::EthernetLinkUnknown:
|
||||
return "ethernet-link-unknown";
|
||||
case NetworkDiagnosticReason::EthernetLinkDown:
|
||||
return "ethernet-link-down";
|
||||
case NetworkDiagnosticReason::EthernetAwaitingIp:
|
||||
return "ethernet-awaiting-ip";
|
||||
case NetworkDiagnosticReason::EthernetStabilizing:
|
||||
return "ethernet-stabilizing";
|
||||
case NetworkDiagnosticReason::SwitchingLocked:
|
||||
return "switching-locked";
|
||||
case NetworkDiagnosticReason::EthernetReady:
|
||||
return "ethernet-ready";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
static constexpr bool ntpPendingAfterConnectivitySample(
|
||||
bool synced, bool pending, bool was_connected, bool connected) {
|
||||
return pending || (!synced && connected && !was_connected);
|
||||
@@ -64,6 +135,16 @@ static constexpr NetworkMedium bootSelection(bool ethernet_connected,
|
||||
: NetworkMedium::None;
|
||||
}
|
||||
|
||||
// Once the Ethernet controller has initialized, allow the entire boot probe
|
||||
// window for link negotiation and DHCP. PHY carrier is useful diagnostic data,
|
||||
// but it must not shorten the advertised deadline when carrier reporting lags.
|
||||
static constexpr bool ethernetBootProbePending(bool ethernet_initialized,
|
||||
bool ethernet_connected,
|
||||
uint32_t elapsed_ms,
|
||||
uint32_t wait_ms) {
|
||||
return ethernet_initialized && !ethernet_connected && elapsed_ms < wait_ms;
|
||||
}
|
||||
|
||||
static inline NetworkMedium automaticSelection(
|
||||
const AutomaticSelectionInput& input) {
|
||||
if (input.switching_locked) return input.selected;
|
||||
|
||||
Reference in new issue
Block a user