summaryrefslogtreecommitdiffstats
path: root/K2LABI/ABI/ABI.h
blob: 791bb3b729df2757f94255bd08bc6c3a6a2f099c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#include "windows-adapter.h"
#include "K2LABI.h"
#include "K2LIPC.h"
#include "SLock.h"
#include "ABIFilter.h"
#include "SEvent.h"
#define MAX_CONTEXT_OBJECT 256

using namespace K2L::Automotive::ABI;
using namespace K2L::Automotive::IPC;

class CABI : public IABI, public IIPCListener
{
public:
    CABI();
    ~CABI();

    int   Init                 (AbiIpcDevice* pInitData);
    int   Init                 (AbiIpcDevice* pInitData, int prio);
    int   Deinit               (void);
    void  Start                (IABIListener* pListener);
    void  Stop                 (void);
    void  RegisterBusContext   (Bus bus, void* context);
    void  UnregisterBusContext (Bus bus);
    void* GetBusContext        (Bus bus);
    bool  IsSpyMode            (void);
    void  SetSpyMode           (bool bSpyMode);
    BYTE* SendRequest(BYTE ifc, BYTE function, BYTE* data, int len, int& resultLen, BYTE& errorCode);
    BYTE* SendRequest(BYTE ifc, BYTE function, BYTE* data, int len, int& resultLen, BYTE& errorCode, int timeout);
    void EndRequest();

    void AddIdIntervalFilter    (Bus bus, unsigned short handle, unsigned int minId, unsigned int maxId);
    void AddMaskedIdFilter      (Bus bus, unsigned short handle, unsigned int id, unsigned int mask);
    void RemoveFilter           (unsigned short handle);
    
    void OnMessage(IIpc* pIpc, BYTE channel, BYTE errorCode, BYTE* pPacket, int nLen);
    void OnError(IIpc* pIpc);
    void OnMessageLost(IIpc* pIpc);
public:
    //Will be accessed by the IPC receive callback, which is in static context
    IIpc*         m_pIpcCommands;
    IIpc*         m_pIpcEvents;

private:
    inline void OnCommandResponse(IIpc* pIpc, BYTE channel, BYTE errorCode, BYTE* pPacket, int nLen);
    inline void OnEvent(IIpc* pIpc, BYTE channel, BYTE errorCode, BYTE* pPacket, int nLen);

    inline void DecodeCommon(BYTE* pPacket, int nLen, BYTE channel);
    inline void DecodeCanChannels(BYTE* pPacket, int nLen, Bus bus, BYTE protocol, BYTE eventType);
    inline void DecodeMostChannels(BYTE* pPacket, int nLen, Bus bus, BYTE protocol, BYTE eventType);
    inline void DecodeLinChannels(BYTE* pPacket, int nLen, Bus bus, BYTE protocol, BYTE eventType);
    inline void DecodeFlexRayChannels(BYTE* pPacket, int nLen, Bus bus, BYTE protocol, BYTE eventType);
    inline void DecodeRelayChannels(BYTE* pPacket, int nLen, Bus bus, BYTE protocol, BYTE eventType);
    inline void DecodeMostEthernetChannels(BYTE* pPacket, int nLen, Bus bus, BYTE protocol, BYTE eventType);
    
    inline int Init();
    inline BYTE ReadByte(BYTE*& pBuffer);
    inline unsigned short ReadUInt16(BYTE*& pBuffer);
    inline unsigned long ReadUInt32(BYTE*& pBuffer);
    inline unsigned long long ReadUInt64(BYTE*& pBuffer);
private:
    AbiIpcDevice* m_abiDevice;

    IABIListener* m_pAbiListener;
    bool          m_bStarted;
    bool          m_bSpyMode;
    void*         m_objectMap[MAX_CONTEXT_OBJECT];

    CEvent        m_requestEvent;
    CLock         m_lock;

    BYTE          m_errorCode;
    BYTE*         m_pResult;
    int           m_nResultLength;
    
    CABIFilter    m_filter;

    DWORD         m_dwUniqueId;
};