not all listeners are sensors (#2514)

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2021-03-31 19:51:49 -07:00 committed by GitHub
parent e2b1195b56
commit c637307238
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 38 deletions

View File

@ -17,12 +17,14 @@
#define CAN_TIMEOUT MS2NT(100)
class Logging;
class CanListener;
class CanSensorBase;
#if EFI_CAN_SUPPORT
void processCanRxMessage(const CANRxFrame& msg, Logging* logger, efitick_t nowNt);
#endif // EFI_CAN_SUPPORT
void registerCanListener(CanListener& listener);
void registerCanSensor(CanSensorBase& sensor);
// Indicate that an ack response was received from the wideband bootloader

View File

@ -0,0 +1,45 @@
/**
* @file can_listener.h
*
* @date March 31, 2021
* @author Matthew Kennedy, (c) 2021
*/
#pragma once
#include "can.h"
class CanListener {
public:
CanListener(uint32_t eid)
: m_eid(eid)
{
}
CanListener* processFrame(const CANRxFrame& frame, efitick_t nowNt) {
if (CAN_EID(frame) == m_eid) {
decodeFrame(frame, nowNt);
}
return m_next;
}
uint32_t getEid() {
return m_eid;
}
void setNext(CanListener* next) {
m_next = next;
}
virtual CanListener* request() {
return m_next;
}
protected:
virtual void decodeFrame(const CANRxFrame& frame, efitick_t nowNt) = 0;
CanListener* m_next = nullptr;
private:
const uint32_t m_eid;
};

View File

@ -80,19 +80,23 @@ static void printPacket(const CANRxFrame &rx, Logging *logger) {
volatile float canMap = 0;
CanSensorBase *cansensors_head = nullptr;
CanListener *canListeners_head = nullptr;
void serviceCanSubscribers(const CANRxFrame &frame, efitick_t nowNt) {
CanSensorBase *current = cansensors_head;
CanListener *current = canListeners_head;
while (current) {
current = current->processFrame(frame, nowNt);
}
}
void registerCanSensor(CanSensorBase &sensor) {
sensor.setNext(cansensors_head);
cansensors_head = &sensor;
void registerCanListener(CanListener& listener) {
listener.setNext(canListeners_head);
canListeners_head = &listener;
}
void registerCanSensor(CanSensorBase& sensor) {
registerCanListener(sensor);
sensor.Register();
}

View File

@ -20,7 +20,7 @@
EXTERN_ENGINE;
extern CanSensorBase* cansensors_head;
extern CanListener* canListeners_head;
CanWrite::CanWrite()
: PeriodicController("CAN TX", PRIO_CAN_TX, 50)
@ -35,7 +35,7 @@ void CanWrite::PeriodicTask(efitime_t nowNt) {
sendCanVerbose();
}
CanSensorBase* current = cansensors_head;
CanListener* current = canListeners_head;
while (current) {
current = current->request();

View File

@ -13,46 +13,20 @@
#include "can_msg_tx.h"
#include "obd2.h"
#include "can.h"
#include "can_listener.h"
/**
* Sensor which reads it's value from CAN
*/
class CanSensorBase : public StoredValueSensor {
class CanSensorBase : public StoredValueSensor, public CanListener {
public:
CanSensorBase(uint32_t eid, SensorType type, efitick_t timeout)
: StoredValueSensor(type, timeout)
, m_eid(eid)
, CanListener(eid)
{
}
virtual CanSensorBase* request() {
return m_next;
}
void showInfo(Logging* logger, const char* sensorName) const override;
CanSensorBase* processFrame(const CANRxFrame& frame, efitick_t nowNt) {
if (CAN_EID(frame) == m_eid) {
decodeFrame(frame, nowNt);
}
return m_next;
}
uint32_t getEid() {
return m_eid;
}
void setNext(CanSensorBase* next) {
m_next = next;
}
protected:
virtual void decodeFrame(const CANRxFrame& frame, efitick_t nowNt) = 0;
CanSensorBase* m_next = nullptr;
private:
const uint32_t m_eid;
};
template <typename TStorage, int TScale>
@ -105,7 +79,7 @@ public:
setValidValue(fValue, nowNt);
}
CanSensorBase* request() override {
CanListener* request() override {
{
CanTxMessage msg(OBD_TEST_REQUEST);
msg[0] = _OBD_2;
@ -115,7 +89,7 @@ public:
// let's sleep on write update after each OBD request, this would give read thread a chance to read response
// todo: smarter logic of all this with with semaphore not just sleep
chThdSleepMilliseconds(300);
return m_next;
return CanListener::request();
}
int PID;