Skip to content

Commit

Permalink
registerCanListener should be idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Mar 2, 2024
1 parent 720a71d commit b0881c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions firmware/controllers/can/can_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class CanListener {
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 {
Expand Down
25 changes: 22 additions & 3 deletions firmware/controllers/can/can_rx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,23 @@ static void printPacket(CanBusIndex 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;
Expand All @@ -95,8 +111,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) {
Expand Down

0 comments on commit b0881c9

Please sign in to comment.