Files

82 lines
2.6 KiB
C++

/**
* Link Receiver Module (Inst. Cluster)
*
* Receives v2 frames from Main CTRL on two transports:
* - UART1 (byte stream, framed here with a state machine)
* - ESP-NOW (whole frames, delivered by EspNowReceiver)
* Both feed processFrame(). DATA frames are deduplicated on packet_id so a frame
* that arrives on both paths is applied once; CONFIG frames update the cached
* link_config_t and bump a sequence number the main loop watches.
*
* The namespace keeps its historical name; it is the link receiver for both paths.
*/
#ifndef UART_RECEIVER_H
#define UART_RECEIVER_H
#include <Arduino.h>
#include "protocol/automotive_data.h"
typedef enum {
LINK_SOURCE_NONE = 0,
LINK_SOURCE_UART = 1,
LINK_SOURCE_ESPNOW = 2
} link_source_t;
namespace UARTReceiver {
typedef struct {
uint32_t packets_received; // DATA frames applied (after dedupe)
uint32_t packets_uart; // valid DATA frames seen on UART
uint32_t packets_espnow; // valid DATA frames seen on ESP-NOW
uint32_t duplicates; // DATA frames dropped as already applied
uint32_t config_received; // CONFIG frames accepted
uint32_t checksum_errors;
uint32_t framing_errors;
uint32_t version_errors; // frames from a different PACKET_VERSION
uint32_t timeout_errors;
uint32_t last_packet_time; // millis() of the last applied DATA frame
uint32_t last_uart_time; // millis() of the last valid UART frame
uint32_t last_espnow_time; // millis() of the last valid ESP-NOW frame
uint32_t bytes_received; // UART bytes
link_source_t last_source; // transport of the last applied DATA frame
} uart_rx_stats_t;
/**
* Create the mutex and the UART1 receive task on Core 0.
*/
bool init();
/**
* Validate and apply one complete frame (start..end). Safe to call from the
* ESP-NOW receive callback or the UART task.
*/
void processFrame(const uint8_t* frame, size_t len, link_source_t source);
automotive_data_t getLatestData();
uart_rx_stats_t getStats();
/**
* True if a DATA frame was applied within timeout_ms, on either transport.
*/
bool isConnected(uint32_t timeout_ms = 1000);
/**
* True if the given transport delivered a valid frame within timeout_ms.
*/
bool isSourceAlive(link_source_t source, uint32_t timeout_ms = 1000);
/**
* Latest CONFIG payload from A (defaults until one arrives) and a sequence
* number that increments each time a new one is accepted.
*/
link_config_t getConfig();
uint32_t getConfigSeq();
void resetStats();
uint32_t getTimeSinceLastPacket();
} // namespace UARTReceiver
#endif // UART_RECEIVER_H