Program Listing for File can_rx_handler.hpp

Return to documentation for file (src/tap/communication/can/can_rx_handler.hpp)

/*
 * Copyright (c) 2020-2021 Advanced Robotics at the University of Washington <robomstr@uw.edu>
 *
 * This file is part of Taproot.
 *
 * Taproot is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Taproot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Taproot.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef TAPROOT_CAN_RX_HANDLER_HPP_
#define TAPROOT_CAN_RX_HANDLER_HPP_

#include <cstdint>

#include "tap/util_macros.hpp"

#include "can_bus.hpp"

namespace modm::can
{
class Message;
}

namespace tap
{
class Drivers;
}

namespace tap::can
{
class CanRxListener;

class CanRxHandler
{
public:
    static constexpr uint16_t MIN_CAN_ID = 0x1E4;
    static constexpr uint16_t NUM_CAN_IDS = 64;
    static constexpr uint16_t MAX_CAN_ID = MIN_CAN_ID + NUM_CAN_IDS;

    CanRxHandler(Drivers* drivers);
    mockable ~CanRxHandler() = default;
    DISALLOW_COPY_AND_ASSIGN(CanRxHandler)


    static inline uint16_t lookupTableIndexForCanId(uint16_t canId)
    {
        if (canId < MIN_CAN_ID)
        {
            return NUM_CAN_IDS;
        }

        return canId - MIN_CAN_ID;
    }

    mockable void attachReceiveHandler(CanRxListener* const listener);

    mockable void pollCanData();

    mockable void removeReceiveHandler(const CanRxListener& rxListener);

protected:
    Drivers* drivers;

    CanRxListener* messageHandlerStoreCan1[NUM_CAN_IDS];

    CanRxListener* messageHandlerStoreCan2[NUM_CAN_IDS];

#if defined(PLATFORM_HOSTED) && defined(ENV_UNIT_TESTS)
public:
#endif

    void attachReceiveHandler(
        CanRxListener* const canRxListener,
        CanRxListener** messageHandlerStore);

    void processReceivedCanData(
        const modm::can::Message& rxMessage,
        CanRxListener* const* messageHandlerStore);

    void removeReceiveHandler(
        const CanRxListener& canRxListener,
        CanRxListener** messageHandlerStore);

    inline CanRxListener** getHandlerStore(CanBus bus)
    {
        return bus == CanBus::CAN_BUS1 ? messageHandlerStoreCan1 : messageHandlerStoreCan2;
    }
};  // class CanRxHandler

}  // namespace tap::can

#endif  // TAPROOT_CAN_RX_HANDLER_HPP_