Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
f4329a85eThis file is pinned to the selected release. Download the original to open it in your design tool.
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2025 Jibril Sharafi
#pragma once
#include <AdvancedLogger.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include "issue_logic.h" // lib/issue_logic - pure lifecycle + predicates (host-tested)
#include "constants.h"
#include "structs.h"
// Device issue registry (issue #145). One periodic tick derives every issue
// from current runtime facts (counters, flags, RTC state) through the pure
// predicates in lib/issue_logic - nothing is persisted, so a stale issue is
// impossible by construction. Modules never call into the registry; the
// dependency arrow points one way, registry -> facts.
// Task
#define ISSUE_REGISTRY_TASK_NAME "issue_reg_task"
#define ISSUE_REGISTRY_TASK_STACK_SIZE (6 * 1024)
#define ISSUE_REGISTRY_TASK_PRIORITY 1
#define ISSUE_REGISTRY_TICK_INTERVAL (5 * 1000)
// Instance table
#define ISSUE_MAX_INSTANCES 32
#define ISSUE_MESSAGE_BUFFER_SIZE 192 // Longest catalog message (channel mismatch with label) is ~160 chars
#define ISSUE_GLOBAL_SCOPE 255 // channel value for globally-scoped codes
// Hysteresis (windows are one tick long, ~5 s). The thresholds encode durations,
// so they scale with the tick: the "over X s" messages derive from streak * tick.
#define ISSUE_STREAK_TO_RAISE 12 // ~1 min of sustained bad evidence
#define ISSUE_CONNECTIVITY_STREAK_TO_RAISE 24 // ~2 min: rides out boot/reconnect blips
// InfluxDB uses sparse evidence (one window per attempt due to exponential backoff):
// streak counts failed attempts, not 5s ticks. 3 consecutive failures = issue raised.
#define ISSUE_INFLUXDB_STREAK_TO_RAISE 3
// Per-code thresholds
#define ISSUE_MISMATCH_MIN_EVIDENCE_READS 4
#define ISSUE_MISMATCH_MIN_CLAMPED_FRACTION 0.9f
#define ISSUE_HEAP_LOW_THRESHOLD_BYTES (25 * 1024)
#define ISSUE_LITTLEFS_USED_FRACTION_RAISE 0.90f
#define ISSUE_LITTLEFS_USED_FRACTION_CLEAR 0.85f
#define ISSUE_VOLTAGE_TOLERANCE_FRACTION 0.10f
#define ISSUE_FREQUENCY_TOLERANCE_HZ 1.0f
#define ISSUE_ADE7953_FAILURES_PER_WINDOW 7 // failures in one ~5 s window to count as bad
#define ISSUE_NTP_BOOT_GRACE_MS (5 * 60 * 1000)
#define ISSUE_TEMPERATURE_RAISE_CELSIUS 70.0f
#define ISSUE_TEMPERATURE_CLEAR_CELSIUS 65.0f
namespace IssueRegistry
{
void begin();
void stop();
// REST support. issuesToJson fills {"issues":[...]}; clients derive any counts.
bool issuesToJson(JsonDocument &doc);
// Acknowledge one instance (channel = ISSUE_GLOBAL_SCOPE for global codes).
// Returns false if the code string is unknown or no such instance exists.
bool ack(const char *codeStr, uint8_t channel);
// Acknowledge every visible instance. Returns the number acked.
uint32_t ackAll();
// Number of currently-active instances (ActiveUnacked + ActiveAcked).
uint32_t activeCount();
// Observer hook: invoked from the registry task on any state transition or
// ack so a consumer (e.g. the issues shadow) can refresh its mirror. The
// callback must be cheap and non-blocking - set a flag, never publish.
using ChangeCallback = void (*)();
void setChangeCallback(ChangeCallback cb);
// Task information
TaskInfo getTaskInfo();
}