registerCanListener should be idempotent

This commit is contained in:
Matthew Kennedy 2024-03-02 15:53:57 -08:00 committed by rusefi
parent 26452365c8
commit 4f39b22be6
2 changed files with 26 additions and 3 deletions

View File

@ -36,6 +36,10 @@ public:
return m_next;
}
bool hasNext() const {
return m_next;
}
// 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 {

View File

@ -55,7 +55,23 @@ static void printPacket(const size_t busIndex, const CANRxFrame &rx) {
volatile float canMap = 0;
CanListener *canListeners_head = nullptr;
struct CanListenerTailSentinel : public CanListener {
CanListenerTailSentinel()
: CanListener(0)
{
}
bool acceptFrame(const CANRxFrame&) const override {
return false;
}
void decodeFrame(const CANRxFrame&, efitick_t) override {
// nothing to do
}
};
static CanListenerTailSentinel tailSentinel;
CanListener *canListeners_head = &tailSentinel;
void serviceCanSubscribers(const CANRxFrame &frame, efitick_t nowNt) {
CanListener *current = canListeners_head;
@ -71,8 +87,11 @@ void serviceCanSubscribers(const CANRxFrame &frame, efitick_t nowNt) {
}
void registerCanListener(CanListener& listener) {
listener.setNext(canListeners_head);
canListeners_head = &listener;
// If the listener already has a next, it's already registered
if (!listener.hasNext()) {
listener.setNext(canListeners_head);
canListeners_head = &listener;
}
}
void registerCanSensor(CanSensorBase& sensor) {