Skip to content

Commit b0881c9

Browse files
committedMar 2, 2024
registerCanListener should be idempotent
1 parent 720a71d commit b0881c9

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed
 

‎firmware/controllers/can/can_listener.h

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class CanListener {
3636
return m_next;
3737
}
3838

39+
bool hasNext() const {
40+
return m_next;
41+
}
42+
3943
// Return true if the provided frame should be accepted for processing by the listener.
4044
// Override if you need more complex logic than comparing to a single ID.
4145
virtual bool acceptFrame(const CANRxFrame& frame) const {

‎firmware/controllers/can/can_rx.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,23 @@ static void printPacket(CanBusIndex busIndex, const CANRxFrame &rx) {
8484

8585
volatile float canMap = 0;
8686

87-
CanListener *canListeners_head = nullptr;
87+
struct CanListenerTailSentinel : public CanListener {
88+
CanListenerTailSentinel()
89+
: CanListener(0)
90+
{
91+
}
92+
93+
bool acceptFrame(const CANRxFrame&) const override {
94+
return false;
95+
}
96+
97+
void decodeFrame(const CANRxFrame&, efitick_t) override {
98+
// nothing to do
99+
}
100+
};
101+
102+
static CanListenerTailSentinel tailSentinel;
103+
CanListener *canListeners_head = &tailSentinel;
88104

89105
void serviceCanSubscribers(const CANRxFrame &frame, efitick_t nowNt) {
90106
CanListener *current = canListeners_head;
@@ -95,8 +111,11 @@ void serviceCanSubscribers(const CANRxFrame &frame, efitick_t nowNt) {
95111
}
96112

97113
void registerCanListener(CanListener& listener) {
98-
listener.setNext(canListeners_head);
99-
canListeners_head = &listener;
114+
// If the listener already has a next, it's already registered
115+
if (!listener.hasNext()) {
116+
listener.setNext(canListeners_head);
117+
canListeners_head = &listener;
118+
}
100119
}
101120

102121
void registerCanSensor(CanSensorBase& sensor) {

0 commit comments

Comments
 (0)