Christian Taedcke / Mbed 2 deprecated ObdDisplay

Dependencies:   Adafruit_GFX MODSERIAL mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IsoTpHandler.h Source File

IsoTpHandler.h

00001 #ifndef ISO_TP_HANDLER
00002 #define ISO_TP_HANDLER
00003 
00004 #include "mbed.h"
00005 
00006 /**
00007  * http://en.wikipedia.org/wiki/ISO_15765-2
00008  */
00009 class IsoTpHandler
00010 {
00011 private:
00012 
00013     /**
00014      * Represents a state.
00015      */
00016     class State
00017     {
00018     public:
00019     
00020         /**
00021          * Empty destructor.
00022          */
00023         virtual ~State() {}
00024         
00025         /**
00026          * Processes the received can message.
00027          *
00028          * \param[in] message This can message it processed.
00029          * \param context This state machine (context) is used.
00030          */
00031         virtual void processInput(const CANMessage* message, IsoTpHandler* context) const = 0;
00032         
00033         /**
00034          * This method is called when this state is entered.
00035          * \param context This state machine (context) is used.
00036          */
00037         virtual void onEnter(IsoTpHandler* context) const = 0;
00038         
00039         /**
00040          * This method is called when leaving this state.
00041          * \param context This state machine (context) is used.
00042          */
00043         virtual void onLeave(IsoTpHandler* context) const = 0;
00044     };
00045     
00046     /**
00047      * No special packet expected 
00048      */
00049     class IdleState : public State
00050     {
00051     public:
00052         IdleState();
00053         
00054         virtual void processInput(const CANMessage* message, IsoTpHandler* context) const;
00055         virtual void onEnter(IsoTpHandler* context) const;
00056         virtual void onLeave(IsoTpHandler* context) const;
00057     };
00058     
00059     /**
00060      * Expect packets of type "consecutive frame"
00061      */
00062     class ConsequtiveTransferState : public State
00063     {
00064     public:
00065         ConsequtiveTransferState();
00066         
00067         virtual void processInput(const CANMessage* message, IsoTpHandler* context) const;
00068         virtual void onEnter(IsoTpHandler* context) const;
00069         virtual void onLeave(IsoTpHandler* context) const;
00070     };
00071 public:
00072 
00073     /**
00074      * Constructor
00075      *
00076      * \param[in] canInterface The can interface the packets should be sent to. It must not be NULL.
00077      */
00078     IsoTpHandler(CAN* canInterface);
00079     
00080     /**
00081      * Processes the given can message.
00082      *
00083      * This is the main method. It must be called for every received IsoTp can packet.
00084      * It updates the internal state and sends the can response.
00085      *
00086      * \param[in] message The received can message. It must not be NULL.
00087      */  
00088     void processCanMessage(const CANMessage* message);
00089     
00090     /**
00091      * This method is called when a complete Iso Tp message was received.
00092      *
00093      * Currently the packet is only printed out.
00094      * Later a user callback must be executed from here.
00095      *
00096      * \param[in] data The content of the Iso Tp message.
00097      * \param[in] length The amount of bytes in data.
00098      */
00099     void handle_decoded_packet(const uint8_t* data, uint16_t length);
00100         
00101     /**
00102      *
00103      * \param[in] messageSize Total bytes included in the consequtive transfer. 
00104      * \param[in] data Always 6 bytes.
00105      */
00106     void init_consequtive_reading (uint16_t messageSize, const uint8_t* data);
00107     
00108     /**
00109      * Returns the next expected index value of the can packet.
00110      *
00111      * \return The next expected index value.
00112      */
00113     uint8_t getExpectedIndex() const;
00114     
00115     /**
00116      * Increments the expected index.
00117      *
00118      * This method ensures an automatic wrap around from 15 to 0.
00119      */
00120     void incrementExpectedIndex();
00121     
00122     /**
00123      * Appends the given data to the internal consequtive transfer buffer.
00124      *
00125      * \retval \c True if the state should be switched.
00126      * \retval \c False if the state not change.
00127      */
00128     bool appendReceivedData(const uint8_t* data, uint8_t length);
00129 
00130     /**
00131      * Modifies the internal state.
00132      *
00133      * Should not be used externally.
00134      *
00135      * \param[in] The new state.
00136      */
00137     void setState(const State* state);
00138 
00139     static const IdleState idleState;
00140     static const ConsequtiveTransferState consequtiveTransferState;
00141     
00142     static bool isValidIsoTpPacket(const CANMessage* message);
00143 
00144 private:
00145 
00146     /** The current state. */
00147     const State* m_state;
00148     
00149     /** The used can interface. */
00150     CAN* m_canInterface;
00151     
00152     /** This buffer must be able to store the maximum message size. */
00153     uint8_t m_messageBuffer[256];
00154     
00155     /** The expected size of the current message. */
00156     uint16_t m_expectedMessageSize;
00157     
00158     /** The current size of the message that is currently received. */
00159     uint16_t m_currentMessageSize;
00160     
00161     /** Stores the index that is expected in the next can packet. */
00162     uint8_t m_expectedIndex;
00163 };
00164     
00165 
00166 #endif //ISO_TP_HANDLER