sta-core/include/sta/can/subscribable.hpp
2023-02-02 22:22:14 +01:00

103 lines
2.3 KiB
C++

/**
* @file
* @brief Subscription interface for CAN controller drivers.
*/
#ifndef STA_CORE_CAN_SUBSCRIBABLE_HPP
#define STA_CORE_CAN_SUBSCRIBABLE_HPP
#include <sta/can/filter.hpp>
#include <sta/can/headers.hpp>
namespace sta
{
/**
* @defgroup sta_core_can_sub Subscription
* @ingroup sta_core_can
* @brief Subscription interface for CAN controller drivers.
* @{
*/
/**
* @brief Callback for handling received frames.
*
* @param header Frame header
* @param buffer Frame payload buffer
*/
using CanRxCallback = void (*) (const CanRxHeader & header, const uint8_t * buffer);
/**
* @brief Filter configuration and message handler.
*/
struct CanFilterConfig
{
CanFilter filter; /**< Filter handled by callback */
CanRxCallback callback; /**< Callback for message handling */
};
/**
* @brief CAN controller with subscriptions.
*
* @tparam T Implementation of CanController interface
*/
template <typename T>
class SubscribableCanController : public T
{
public:
using T::T;
// Subscriptions
//
/**
* @brief Subscribe to specific message types.
*
* @param subscriptions Array of message filters and handlers
* @param num Number of array entries
*/
bool subscribe(const CanFilterConfig * subscriptions, uint8_t num);
/**
* @brief Subscribe to all messages.
*
* @param callback Called when message is received
* @param fifo FIFO used for received messages
*/
void subscribeAll(CanRxCallback callback, uint8_t fifo);
/**
* @brief Unsubscribe from all messages.
*
* No more messages will be received.
*/
void unsubscribeAll();
/**
* @brief Read message from RX FIFO and notify subscriber.
*/
void receiveAndNotify(uint8_t fifo);
/**
* @brief Process pending frames from RX FIFOs.
*/
void processMessages();
private:
CanRxCallback filterCallbacks_[T::MAX_FILTER_COUNT]; /**< Callbacks for RX filters */
};
/** @} */
} // namespace sta
#include <sta/can/subscribable.tpp>
#endif // STA_CORE_CAN_SUBSCRIBABLE_HPP