/*
 * can_port.h
 *
 *  Created on: Sep 16, 2025
 *      Author: herli
 */

#ifndef SRC_CAN_LIBS_CAN_PORT_H_
#define SRC_CAN_LIBS_CAN_PORT_H_

#include <stdint.h>
#include <stdbool.h>
#include "stm32g4xx_hal.h"   // adjust family include if needed
#include "can_manager.h"     // uses the generic CAN manager API
#include "can_db_symbols.h"

/* Initialize the CAN port glue (provide your HAL handle).
 * You can call this before or after HAL_FDCAN_Start(), but call can_port_boot()
 * only after the CAN is started and bus-active.
 */
void can_port_init(FDCAN_HandleTypeDef *hfdcan);

/* Sends all TX messages flagged with send_on_boot == 1 in can_db.c */
void can_port_boot(void);

/* Drain and process all frames from a FIFO (0 or 1). Call from your HAL Rx callbacks. */
void can_port_handle_rx_fifo(uint32_t fifo_index);

size_t can_port_required_std_filters(void);

HAL_StatusTypeDef can_port_apply_rx_filters(FDCAN_HandleTypeDef *hfdcan);

/* New: send by message DEF */
static inline bool can_port_send_msg_def(const CanMessageDef *msg) {
    return can_manager_send_msg(msg);
}

/* Raw (rarely needed) */
bool can_port_send_raw(uint16_t id, const uint8_t data[8], uint8_t dlc);

bool CAN_Service(void);                   // drain queue; returns true if any sent
size_t CAN_TxQueueDepth(void);            // how many frames waiting


#endif /* SRC_CAN_LIBS_CAN_PORT_H_ */
