42 lines
include/signal_router.h
Declares SignalRouter and the CRITICAL_THRESHOLD routing contract.
// SignalRouter: dispatches DSP hardware signals to handlers based on priority.
#pragma once
#include <cstdint>
#include <functional>
 
// Priority threshold for critical signal routing.
// Signals with priority strictly less than CRITICAL_THRESHOLD go to the critical handler.
// Signals with priority >= CRITICAL_THRESHOLD go to the standard event queue.
static constexpr uint8_t CRITICAL_THRESHOLD = 5;
 
struct Signal {
  uint8_t  priority; // signal urgency: 0 = highest urgency, 255 = lowest
  uint16_t sourceId; // hardware source channel identifier
  uint32_t payload;  // signal-specific data word
};
 
using HandlerFn = std::function<void(const Signal&)>;
 
// Routes incoming hardware signals to one of two registered handlers.
// Both handlers are invoked synchronously from route().
class SignalRouter {
public:
  // Parameters:
  //   onCritical - invoked for signals with priority < CRITICAL_THRESHOLD
  //   onStandard - invoked for signals with priority >= CRITICAL_THRESHOLD
  // Returns: new SignalRouter instance
  explicit SignalRouter(HandlerFn onCritical, HandlerFn onStandard);
 
  // Routes the signal to the appropriate handler based on its priority.
  // Parameters:
  //   sig - the hardware signal to dispatch
  // Returns: void
  void route(const Signal& sig);
 
  // Returns the cumulative number of signals routed since construction.
  uint32_t totalRouted() const { return totalRouted_; }
 
private:
  HandlerFn onCritical_; // handler for priority < CRITICAL_THRESHOLD
  HandlerFn onStandard_; // handler for priority >= CRITICAL_THRESHOLD
  uint32_t  totalRouted_ = 0; // cumulative count of all dispatched signals
};