/** * @file can_tp.h * @brief CAN Transport Protocol (ISO 15765-2) implementation */ #ifndef CAN_TP_H #define CAN_TP_H #include #include #include "kernel.h" #include "can_driver.h" /* CAN TP Configuration */ #define CAN_TP_MAX_CONNECTIONS 8 #define CAN_TP_MAX_PAYLOAD_SIZE 4096 #define CAN_TP_DEFAULT_TIMEOUT_MS 1000 #define CAN_TP_STMIN_DEFAULT 10 /* Minimum separation time in ms */ #define CAN_TP_BS_DEFAULT 8 /* Block size */ /* CAN TP Addressing Formats */ typedef enum { CAN_TP_ADDRESSING_NORMAL = 0, /* Standard 11-bit or 29-bit */ CAN_TP_ADDRESSING_NORMAL_FIXED = 1, /* Fixed addressing */ CAN_TP_ADDRESSING_EXTENDED = 2, /* Extended addressing */ CAN_TP_ADDRESSING_MIXED = 3 /* Mixed addressing */ } CanTpAddressingFormat_t; /* CAN TP Frame Types */ typedef enum { CAN_TP_FRAME_SINGLE = 0, /* Single Frame */ CAN_TP_FRAME_FIRST = 1, /* First Frame */ CAN_TP_FRAME_CONSECUTIVE = 2, /* Consecutive Frame */ CAN_TP_FRAME_FLOW_CONTROL = 3 /* Flow Control */ } CanTpFrameType_t; /* CAN TP Flow Control Status */ typedef enum { CAN_TP_FC_CONTINUE = 0, /* Continue to send */ CAN_TP_FC_WAIT = 1, /* Wait */ CAN_TP_FC_OVERFLOW = 2 /* Overflow/Abort */ } CanTpFlowControlStatus_t; /* CAN TP States */ typedef enum { CAN_TP_IDLE = 0, CAN_TP_SEND_IN_PROGRESS = 1, CAN_TP_RECEIVE_IN_PROGRESS = 2, CAN_TP_WAIT_FLOW_CONTROL = 3, CAN_TP_WAIT_CONSECUTIVE = 4, CAN_TP_TIMEOUT = 5, CAN_TP_ERROR = 6 } CanTpState_t; /* CAN TP Message Structure */ typedef struct { uint32_t message_id; uint8_t* data; uint16_t length; uint8_t addressing_format; uint32_t source_address; uint32_t target_address; } CanTpMessage_t; /* CAN TP Connection */ typedef struct { uint8_t connection_id; CanTpState_t state; CanTpMessage_t current_message; uint16_t current_index; uint8_t sequence_number; uint8_t block_counter; uint32_t timeout_timer; uint8_t stmin; uint8_t block_size; bool is_sender; Semaphore_t flow_control_semaphore; Semaphore_t complete_semaphore; Mutex_t connection_mutex; } CanTpConnection_t; /* CAN TP Configuration */ typedef struct { CanTpAddressingFormat_t addressing_format; uint32_t source_address; uint32_t target_address; uint32_t timeout_ms; uint8_t stmin; uint8_t block_size; bool padding_enabled; uint8_t padding_byte; } CanTpConfig_t; /* CAN TP Callbacks */ typedef void (*CanTpMessageReceivedCallback_t)(const CanTpMessage_t* message); typedef void (*CanTpMessageSentCallback_t)(uint8_t connection_id, bool success); typedef void (*CanTpErrorCallback_t)(uint8_t connection_id, uint32_t error_code); /* CAN TP Functions */ KernelStatus_t can_tp_init(const CanTpConfig_t* config); KernelStatus_t can_tp_deinit(void); KernelStatus_t can_tp_send_message(const CanTpMessage_t* message, uint32_t timeout_ms); KernelStatus_t can_tp_receive_message(CanTpMessage_t* message, uint32_t timeout_ms); KernelStatus_t can_tp_register_callbacks( CanTpMessageReceivedCallback_t rx_callback, CanTpMessageSentCallback_t tx_callback, CanTpErrorCallback_t error_callback); void can_tp_process_rx_indication(const CanMessage_t* can_message); void can_tp_process_tx_confirmation(uint8_t mailbox); void can_tp_main_function(void); /* Periodic processing */ CanTpState_t can_tp_get_state(uint8_t connection_id); #endif /* CAN_TP_H */