2021-03-31 19:51:49 -07:00
|
|
|
/**
|
|
|
|
* @file can_listener.h
|
|
|
|
*
|
|
|
|
* @date March 31, 2021
|
|
|
|
* @author Matthew Kennedy, (c) 2021
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "can.h"
|
|
|
|
|
|
|
|
class CanListener {
|
|
|
|
public:
|
2021-12-01 08:13:36 -08:00
|
|
|
CanListener(uint32_t id)
|
|
|
|
: m_id(id)
|
2021-03-31 19:51:49 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CanListener* processFrame(const CANRxFrame& frame, efitick_t nowNt) {
|
2022-03-14 16:09:01 -07:00
|
|
|
if (acceptFrame(frame)) {
|
2021-03-31 19:51:49 -07:00
|
|
|
decodeFrame(frame, nowNt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_next;
|
|
|
|
}
|
|
|
|
|
2021-12-01 08:13:36 -08:00
|
|
|
uint32_t getId() {
|
|
|
|
return m_id;
|
2021-03-31 19:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void setNext(CanListener* next) {
|
|
|
|
m_next = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual CanListener* request() {
|
|
|
|
return m_next;
|
|
|
|
}
|
|
|
|
|
2022-03-14 16:09:01 -07:00
|
|
|
// Return true if the provided frame should be accepted for processing by the listener.
|
|
|
|
// Override if you need more complex logic than comparing to a single ID.
|
|
|
|
virtual bool acceptFrame(const CANRxFrame& frame) const {
|
|
|
|
return CAN_ID(frame) == m_id;
|
|
|
|
}
|
|
|
|
|
2021-03-31 19:51:49 -07:00
|
|
|
protected:
|
|
|
|
virtual void decodeFrame(const CANRxFrame& frame, efitick_t nowNt) = 0;
|
2022-01-07 13:04:30 -08:00
|
|
|
|
2022-01-15 20:02:11 -08:00
|
|
|
private:
|
2022-03-14 16:09:01 -07:00
|
|
|
CanListener* m_next = nullptr;
|
|
|
|
|
2021-12-01 08:13:36 -08:00
|
|
|
const uint32_t m_id;
|
2021-03-31 19:51:49 -07:00
|
|
|
};
|