Line data Source code
1 : /*! 2 : * @file CANMessageProcessor.cpp 3 : * @brief Implementation of the CANMessageProcessor class. 4 : * @version 0.1 5 : * @date 2025-01-31 6 : * @author Félix LE BIHAN (@Fle-bihh) 7 : * @author Tiago Pereira (@t-pereira06) 8 : * @author Ricardo Melo (@reomelo) 9 : * @author Michel Batista (@MicchelFAB) 10 : * 11 : * @details This file contains the implementation of the CANMessageProcessor 12 : * class, which processes CAN messages. 13 : * 14 : * @note This class is used to process CAN messages and call the appropriate 15 : * handler for each message. 16 : * 17 : * @warning Ensure that the MessageHandler type is properly defined. 18 : * 19 : * @see CANMessageProcessor.hpp for the class definition. 20 : * 21 : * @copyright Copyright (c) 2025 22 : */ 23 : 24 : #include "CANMessageProcessor.hpp" 25 : #include <stdexcept> 26 : 27 : /*! 28 : * @brief Construct a new CANMessageProcessor::CANMessageProcessor object 29 : * 30 : * @details This constructor initializes the CANMessageProcessor object. 31 : */ 32 25 : CANMessageProcessor::CANMessageProcessor() {} 33 : 34 : /*! 35 : * @brief Destroy the CANMessageProcessor::CANMessageProcessor object 36 : * 37 : * @param frameID 38 : * @param handler 39 : * @throws std::invalid_argument if the handler is null 40 : * @details This method registers a handler for a specific frame ID. 41 : */ 42 26 : void CANMessageProcessor::registerHandler(uint16_t frameID, 43 : MessageHandler handler) { 44 26 : if (!handler) { 45 1 : throw std::invalid_argument("Handler cannot be null"); 46 : } 47 25 : handlers[frameID] = handler; 48 25 : } 49 : 50 : /*! 51 : * @brief Process a CAN message 52 : * 53 : * @param frameID The frame ID of the message. 54 : * @param data The data of the message. 55 : * @throws std::runtime_error if no handler is registered for the frame ID. 56 : * @details This method processes a CAN message by calling the appropriate 57 : * handler for the frame ID. 58 : */ 59 8 : void CANMessageProcessor::processMessage(uint16_t frameID, 60 : const std::vector<uint8_t> &data) { 61 8 : auto it = handlers.find(frameID); 62 8 : if (it != handlers.end()) { 63 7 : it->second(data); 64 : } else { 65 2 : throw std::runtime_error("No handler registered for frame ID: " + 66 3 : std::to_string(frameID)); 67 : } 68 7 : }