Small project to display some OBD values from the Toyota GT86/ Subaru BRZ/ Scion FRS on an OLED display.
Dependencies: Adafruit_GFX MODSERIAL mbed-rtos mbed
Diff: IsoTpHandler.h
- Revision:
- 0:6b1f6139fb25
- Child:
- 1:ca506b88b1d6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IsoTpHandler.h Tue Apr 22 14:51:04 2014 +0000 @@ -0,0 +1,85 @@ +#ifndef ISO_TP_HANDLER +#define ISO_TP_HANDLER + +#include "mbed.h" + +/** + * http://en.wikipedia.org/wiki/ISO_15765-2 + */ +class IsoTpHandler +{ +private: + class State + { + public: + virtual ~State() {} + virtual void processInput(const CANMessage* message, IsoTpHandler* context) const = 0; + virtual void onEnter(IsoTpHandler* context) const = 0; + virtual void onLeave(IsoTpHandler* context) const = 0; + }; + + /** + * No special packet expected + */ + class IdleState : public State + { + public: + IdleState(); + + virtual void processInput(const CANMessage* message, IsoTpHandler* context) const; + virtual void onEnter(IsoTpHandler* context) const; + virtual void onLeave(IsoTpHandler* context) const; + }; + + /** + * Expect packets of type "consecutive frame" + */ + class ConsequtiveTransferState : public State + { + public: + ConsequtiveTransferState(); + + virtual void processInput(const CANMessage* message, IsoTpHandler* context) const; + virtual void onEnter(IsoTpHandler* context) const; + virtual void onLeave(IsoTpHandler* context) const; + }; +public: + IsoTpHandler(CAN* canInterface); + + void processCanMessage(const CANMessage* message); + + void handle_decoded_packet(const uint8_t* data, uint16_t length); + + /** + * + * \param[in] data Always 6 bytes. + */ + void init_consequtive_reading(uint16_t messageSize, const uint8_t* data); + uint8_t getExpectedIndex() const; + void incrementExpectedIndex(); + /** + * \retval \c True if the state should be switched. + * \retval \c False if the state not change. + */ + bool appendReceivedData(const uint8_t* data, uint8_t length); + + void setState(const State* state); + + static const IdleState idleState; + static const ConsequtiveTransferState consequtiveTransferState; + + static bool isValidIsoTpPacket(const CANMessage* message); + +private: + const State* m_state; + + CAN* m_canInterface; + + uint8_t m_messageBuffer[256]; + uint16_t m_expectedMessageSize; + uint16_t m_currentMessageSize; + uint8_t m_expectedIndex; +}; + + +#endif //ISO_TP_HANDLER \ No newline at end of file