summaryrefslogtreecommitdiffstats
path: root/K2LABI/IPC/K2LIPC.h
blob: e959dbffef65a5dac39a2237719ff788e6dac855 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*=== K2L GmbH ===============================================================*/
/**
\file
            This file contains the API of the \ref IPC interface.

\ingroup    IPC

\par        COPYRIGHT (c) 2000-2011 by K2L GmbH
            All rights reserved.
*/
/*==============================================================================
            Alle Rechte an der Software verbleiben bei der K2L GmbH.

            Die K2L GmbH raeumt dem Lizenznehmer nach vollstaendiger
            Bezahlung der Lizenzgebuehr ein nicht ausschliessliches,
            unwiderrufliches, nicht uebertragbares, zeitlich und geografisch
            nicht beschraenktes Recht zur Nutzung der jeweils
            auftragsgegenstaendlichen Software innerhalb des Lizenznehmers ein.

            Die Weitergabe des Nutzungsrechts durch den Lizenznehmer ist auf
            dessen Zulieferer beschraenkt. Die Zulieferer sind zu verpflichten,
            die Software nur im Rahmen von Projekten fuer den Lizenznehmer
            zu verwenden; weitere Rechte der Zulieferer sind auszuschliessen.
===HEADER END=================================================================*/
//@{

#pragma once

/// \defgroup IPC Interprocess communication
/// The IPC group ...
namespace K2L
{
    namespace Automotive
    {
        namespace IPC
        {

class IIpc;

/// Identifies the head of the packet.
#define ABI_IPC_PACKET_HEAD     0x04

/// Identifies the IPC channel 7.
#define ABI_IPC_CHANNEL7        0x07

/// Identifies the IPC channel 8.
#define ABI_IPC_CHANNEL8        0x08

#pragma pack(push, 1)
/// \brief Class holding all informations to create a IPC message.
/// \ingroup IPC
class CIpcPacket
{
public:
    /// The header of the IPC packet.
    BYTE m_head;

    /// The channel of the IPC packet.
    BYTE m_channel;

    /// The length of the IPC packet.
    DWORD m_length;

    /// The error code of the IPC packet.
    BYTE m_errorCode;

    /// Checksum of the IPC header.
    BYTE m_hdrCrc;
};
#pragma pack(pop)

#pragma pack(push, 1)
/// \brief Class holding all informations to create a IPC request.
/// \ingroup IPC
class CRequest
{
public:
    /// Message processing group in the ABI device.
    BYTE m_interface;

    /// Function id of the request.
    BYTE m_function;

    /// Request Id.
    DWORD m_requestId;
};
#pragma pack(pop)

/*============================================================================*/
/// \brief Interface of IPC event listener.
/// \note Abstract base class. So it has to be implemented.
/// \ingroup IPC
/*============================================================================*/
class K2LABI_API IIPCListener
{
public:
    virtual ~IIPCListener() {}

    /// Called if packet arrived.
    /// \param[in] pIpc Pointer to IPC that has occured event.
    /// \param[in] pPacket Pointer to data package.
    /// \param[in] nLen Length of package.
    virtual void OnMessage(IIpc* pIpc, BYTE channel, BYTE errorCode, BYTE* pPacket, int nLen) = 0;

    /// Called if error occured.
    /// \param[in] pIpc Pointer to IPC that has occured event.
    virtual void OnError(IIpc* pIpc) = 0;
    
    /// Called if message lost caused by an overflow.
    /// \param[in] pIpc Pointer to IPC that has occured event.
    virtual void OnMessageLost(IIpc* pIpc) = 0;
};

/*============================================================================*/
/// \brief Interface of interprocess connection.
/// \ingroup IPC
/*============================================================================*/
class K2LABI_API IIpc
{
public:
    virtual ~IIpc() {}

    /// Initializes IPC connection.
    /// \param[in] Device to connect.
    /// \param[in] Event listener.
    /// \return 0 if operation successful.
    /// \return !0 if operation failed.
    virtual int Init(IIPCListener* pListener) = 0;

    /// Deinitializes IPC connection.
    /// \return 0 if operation successful.
    /// \return !0 if operation failed.
    virtual int Deinit() = 0;

    /// Sends a request to the device.
    /// \param[in] Namespace ID of command.
    /// \param[in] Function ID of command.
    /// \param[in] Pointer to data that should be send.
    /// \param[in] Length of data.
    /// \param[out] Length of result.
    /// \param[out] Error code of operation.
    /// \return Response of request. NULL if no response returned.
    virtual int FormatRequest(BYTE channel, BYTE ifaceId, BYTE funId, DWORD dwSenderID, const BYTE *pData, DWORD dataLen, BYTE* pResult, int nResultLen, int* nResultLenNeeded) = 0;
    
    /// Decodes a IPC message from the buffer.
    /// \param[in] pPipe Pipe to check if an overflow occurr.
    /// \param[in] pBuffer Pointer to the first byte of message that should be decoded.
    /// \param[in] nLen Length of message.
    virtual void Decode(BYTE* pBuffer, DWORD nLen) = 0;

    /// Gets a pointer to the registered event listener.
    /// \return Pointer to event listener.
    virtual IIPCListener* GetListener() = 0;
};

/*============================================================================*/
/// \brief Factory to create IPC connections.
/// \ingroup IPC
/*============================================================================*/
class K2LABI_API CIpcFactory
{
public:
    /// Create new IPC connection.
    /// \return Pointer to new IPC connection.
    static IIpc* CreateIpc();

    /// Destroy IPC connection.
    /// \param[in] Pointer to IPC connection that should be destroied.
    static void DestroyIpc(IIpc* pIpc);
};
        }
    }
}

/*============================================================================*/
/* C interface                                                                */
/*============================================================================*/

extern "C"
{
#define IPC_HANDLE HANDLE

typedef void (*FCT_OnMessageEvent)    (IPC_HANDLE, BYTE, BYTE, BYTE*, int);
typedef void (*FCT_OnEvent)    (IPC_HANDLE);

K2LABI_API IPC_HANDLE CreateIPC             ();
K2LABI_API void       DestroyIPC            (IPC_HANDLE hIpc);

K2LABI_API int        IPCInit               (IPC_HANDLE hIpc, FCT_OnMessageEvent callback, FCT_OnEvent error, FCT_OnEvent overflow);
K2LABI_API int        IPCDeinit             (IPC_HANDLE hIpc);
K2LABI_API int        IPCFormatRequest      (IPC_HANDLE hIpc, BYTE channel, BYTE ifaceId, BYTE funId, DWORD dwSenderID, const BYTE *pReqData, DWORD dataLen, BYTE* pResult, int nResultLen, int* nNeededResultLen);
K2LABI_API void       IPCDecode             (IPC_HANDLE hIpc, BYTE* pData, int nDataLen);
K2LABI_API void       IPCDecodeFlow         (IPC_HANDLE hIpc, HANDLE hReaderPipe, BYTE* pData, int nDataLen);
}

//@}
/*============================================================================*/
/* END OF FILE                                                                */
/*============================================================================*/