Files
libcls/CanDataHandler.h
2024-02-05 19:49:37 +01:00

118 lines
3.8 KiB
C

/**
* @file CanDataHandler.h
* @brief CanDataHandler Module
*
* The CanDataHandler module is responsible for managing CAN data messages and event messages.
* It provides functionality to register CAN IDs for data and event messages, handle reception
* of these messages from FIFO buffers, insert data messages into an array, and retrieve stored
* data messages based on their CAN ID.
*
* The module maintains an internal data structure for data and event message slots, each of which
* is associated with a filter reference that points to a filter slot. A filter slot stores the
* CAN IDs and indicates the type of FIFO buffer and the number of free IDs in the slot.
*
* For event messages, a callback function can be registered to handle specific events associated
* with a CAN ID.
*/
#ifndef CANDATAHANDLER_H
#define CANDATAHANDLER_H
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "CLS_BSP.h"
// Define the maximum length of data for a Data Message
#define MAX_DATA_LENGTH 8
typedef uint16_t CanDataId;
// Typedef for the event callback function
typedef void (*EventCallback)(CanDataId canid, uint8_t* data, uint8_t len);
// type of fifo that this slot uses
typedef enum {
CANDATA_FIFOX_UNDEFIEND = CLS_CAN_FILTER_DISABLE,
CANDATA_FIFO0_DATA = CLS_CAN_FILTER_TO_RXFIFO0,
CANDATA_FIFO1_EVENT = CLS_CAN_FILTER_TO_RXFIFO1,
CANDATA_FIFOX_MANUAL = 0x0f,
} CanDataSlotFifo;
// count of free ids in the slot
typedef enum {
CANDATA_ALLFREE = 0x00,
CANDATA_FREE1 = 0x01,
CANDATA_FREE0 = 0x02,
CANDATA_FULL = 0x03,
CANDATA_MANUAL = 0x0f,
} CanDataSlotFree;
// Define s structer for a used filter slot
typedef struct {
CanDataSlotFree free; // Number of free IDs in the slot
CanDataSlotFifo fifo; // Type of FIFO that this slot uses
CanDataId id[2]; // Array to store the two CAN IDs associated with this filter slot
} CanDataFilterSlot;
// Define a structure for a Data Message
typedef struct {
CanDataId canid;
uint8_t data[MAX_DATA_LENGTH];
uint8_t data_length;
} CanDataMessage;
typedef struct
{
CanDataFilterSlot * ref; // Reference to the filter slot
uint8_t index; // Index of the used CAN ID in the filter slot's id array
} CanDataFilterRef;
typedef struct {
CanDataMessage msg;
CanDataFilterRef filter; // Filter reference
} CanDataMessageSlot;
// Define a structure for a Event Message
typedef struct {
CanDataId canid;
EventCallback eventCall;
CanDataFilterRef filter; // Filter reference
} CanDataEventSlot;
// reset all internal structures
// does not reset CAN HW
void CanDataHandler_init(void);
// Function to register a CAN ID for Data Messages
bool CanData_regDataMsg(CanDataId canid);
// Function to register a Set of ID for Data Messages using a Manual filter
size_t CanData_regDataManualMsg(const CanDataId* canid, size_t id_count, CLS_BSP_CAN_UniversalFilter * filter);
// Function to register a CAN ID for Event Messages and associate it with a callback function
bool CanData_regEventMsg(CanDataId canid, EventCallback event_callback);
// Function to remove a CAN ID from Event Messages Register
bool CanData_removeEvent(CanDataId canid);
// Function to handle reception of Data Messages from FIFO0
void CanData_canFifo0RxCallback(CanDataId canid, uint8_t* data, uint8_t len);
// Function to handle reception of Event Messages from FIFO1
void CanData_canFifo1RxCallback(CanDataId canid, uint8_t* data, uint8_t len);
// Function to insert a Data Message into the array
void CanData_insertDataMessage(CanDataId canid, uint8_t* data, uint8_t data_length);
// Function to read stored Data Message based on CAN ID
// Returns a const pointer to the stored data or NULL if not found
const CanDataMessage* CanData_getDataMessage(CanDataId canid);
#endif // CANDATAHANDLER_H