CHAILink SDK  Version 1.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
CLCm_TransportProtocol.h
Go to the documentation of this file.
1 //*********************************************************
2 // File: CLCm_TransportProtocol.h
3 // Project: CopperLan CHAILink SDK Common Part
4 // For Client & Server
5 // Version: 1.3
6 // Release Date: 2014/04/15
7 //*********************************************************
8 
9 /********************************************************************
10 Software License Agreement: CHAILink Client source code
11 
12 The software supplied herewith by Klavis Technology (the “Company”) is intended and
13 supplied to you, the Company’s customer, for use solely and exclusively on embedded
14 CopperLan products needing a CHAILink Client.
15  The software is owned by the Company and/or its supplier, and is protected
16  under applicable copyright laws. All rights are reserved. Any use in violation of the
17  foregoing restrictions may subject the user to criminal sanctions under applicable laws,
18 as well as to civil liability for the breach of the terms and conditions of this license.
19 
20 THIS SOFTWARE MUST REMAIN UNMODIFIED. NO WARRANTIES, WHETHER EXPRESS,
21  IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
22  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE
23 IN CASE OF SOURCE CODE CHANGE BY THE CUSTOMER. IN CASE OF SUCH CHANGE, THE
24 COMPANY SHALL NOT BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
25 OF ANY KIND.
26 ********************************************************************/
27 
28 
29 #ifndef __CLCM_TRANSPORTPROTOCOL_H__
30 #define __CLCM_TRANSPORTPROTOCOL_H__
31 
32 #include "CLCm_Message.h"
33 #include "CLCm_Transport.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
40 
41 //=============================================================================================
42 // Transport protocol: perform some checks
43 //=============================================================================================
44 
45 #if !defined(_CLTP_CLIENTMODE) && !defined(_CLTP_SERVERMODE)
46 #error '_CLTP_CLIENTMODE' or '_CLTP_SERVERMODE' must be defined!!
47 #endif
48 
49 //=============================================================================================
50 // Transport protocol map
51 // Standard Endianness for serialization is Little Endian
52 //=============================================================================================
53 
54 /* Frame Header Format
55  mm mv : frame start (2 * 8 bit) - mm m: Marker (0xFE 0xA_) v: version currently 1
56  cmdsize : Transport Command / Size word (16 bit)
57  : b15 b14 b13 b12 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0
58  c4 c3 c2 c1 c0 s10 s9 s8 s7 s6 s5 s4 s3 s2 s1 s0
59  c[4-0] : Command
60  s[10-0] : Payload size
61  PL : Payload (s bytes). buffer start pointer is and must be 4 bytes aligned
62 
63  Details about CHAILink Message Payload -> CLCm_Message.h
64 */
65 
66 //=============================================================================================
67 // Transport protocol: frame start defines (CLTP is for CHAILink Transport Protocol)
68 //=============================================================================================
69 #define CLTP_FRAMESTART_01 (0xFE) // marker byte 1
70 #define CLTP_FRAMESTART_02 (0xA1) // Marker byte 2 with version 1
71 #define CLTP_FRAMESTART_SIZE (2)
72 #define CLTP_FRAMESTART_POS (0)
73 
74 //=============================================================================================
75 // Transport protocol: command defines
76 //=============================================================================================
77 
78 // Commands sends in handshaking step:
79 //
80 // CLC side CLS side Remarks
81 // -------------------------------------------------------------------------------------------------------------------------------------
82 //
83 // CLTP_CMD_SETINITPARAMS -> Client send Init parameters (passthru ethernet, keepalive, CLTP FrameSize, CL FrameSize)
84 //
85 // <- CLTP_CMD_SETINITPARAMSACK Server respond with an error code : connection is established if errorcode = CLTP_ErrorCode_Success
86 //
87 
88 // Direction
89 #define CLTP_CMD_DIR_MASK (0x8000)
90 #define CLTP_CMD_DIR_TOCLIENT (0x0000)
91 #define CLTP_CMD_DIR_TOSERVER (0x8000)
92 
93 #define CLTP_CMD_OPCODE_MASK (0xF800)
94 #define CLTP_CMD_OPCODE_POS (11)
95 #define CLTP_CMD_PAYLOADSIZE_MASK (0x07FF)
96 
97 // Commands TPSS to TPCS
98 // TPCS: Transport Protocol Client Side
99 // TPSS: Transport Protocol Server Side
100 #define CLTP_CMD_SETINITPARAMSACK ((0 << CLTP_CMD_OPCODE_POS) | CLTP_CMD_DIR_TOCLIENT) // TPSS must send this command to confirm that it has well received init parameters from TPCS it contains 1 byte with error code
101 #define CLTP_CMD_KEEPALIVE ((1 << CLTP_CMD_OPCODE_POS) | CLTP_CMD_DIR_TOCLIENT) // If TPCS is configured to accept Keep Alive, TPSS sends periodically this command
102 #define CLTP_CMD_RESETCLIENTSTATE ((2 << CLTP_CMD_OPCODE_POS) | CLTP_CMD_DIR_TOCLIENT) // The server asks the client to reset its state
103 #define CLTP_CMD_CLIENTCLCMD ((3 << CLTP_CMD_OPCODE_POS) | CLTP_CMD_DIR_TOCLIENT) // Command sent to CHAILink Client (Notification or Asynchronous return)
104 
105 // Commands TPCS to TPSS
106 #define CLTP_CMD_SETINITPARAMS ((0 << CLTP_CMD_OPCODE_POS) | CLTP_CMD_DIR_TOSERVER) //
107 #define CLTP_CMD_ACKKEEPALIVE ((1 << CLTP_CMD_OPCODE_POS) | CLTP_CMD_DIR_TOSERVER) // Command sent when TPCS received _CLTP_CMD_KEEPALIVE from TPSS
108 #define CLTP_CMD_RESETSERVERSTATE ((2 << CLTP_CMD_OPCODE_POS) | CLTP_CMD_DIR_TOSERVER) // The Client asks the server to reset its state
109 #define CLTP_CMD_SERVERCLCMD ((3 << CLTP_CMD_OPCODE_POS) | CLTP_CMD_DIR_TOSERVER) // Payload is for CHAILink Server
110 
111 #define CLTP_CMD_SIZE (2)
112 #define CLTP_CMD_POS (CLTP_FRAMESTART_POS + CLTP_FRAMESTART_SIZE)
113 
114 //=============================================================================================
115 // Transport protocol: Client init parameters bits defines for _CLTP_CMD_SETINITPARAMS command
116 //=============================================================================================
117 #define CLTP_INITPARAMS_KEEPALIVE_MASK 0x01
118 #define CLTP_INITPARAMS_PASSTHRU_MASK 0x02
119 
120 //=============================================================================================
121 // Transport protocol: header defines
122 //=============================================================================================
123 #define CLTP_HEADERSIZE (CLTP_FRAMESTART_SIZE + CLTP_CMD_SIZE)
124 
125 //=============================================================================================
126 // Transport protocol: CRC defines
127 //=============================================================================================
128 #define CLTP_CRC_SIZE (1)
129 
130 //=============================================================================================
131 // Transport protocol: Global defines
132 //=============================================================================================
133 
134 // If server mode or if Ethernet Patssthru is enabled, force transport buffer to the maximum size.
135 #if defined(_CLTP_SERVERMODE) || defined(ENABLE_ETHERNET_PASSTHRU)
136 #define CLTPRT_PAYLOADMAXSIZE CLMSG_MESSAGEMAXSIZE
137 #else
138 #define CLTPRT_PAYLOADMAXSIZE CLMSG_MESSAGESIZE
139 #endif
140 
141 #define CLTPRT_FRAMEMAXSIZE (CLTPRT_PAYLOADMAXSIZE + CLTP_HEADERSIZE + CLTP_CRC_SIZE)
142 #define CLTPRT_FRAMEMINSIZE (CLMSG_MESSAGEMINSIZE + CLTP_HEADERSIZE + CLTP_CRC_SIZE)
143 
144 //=============================================================================================
145 // Transport protocol: action status
146 //=============================================================================================
147 // PS: 'CLTPCS' is for CHAILink Transport Protocol Client Status
148 typedef enum
149 {
150  CLTPCS_None = 0x0000,
151  CLTPCS_KeepAliveActive = 0x0001,
152  CLTPCS_TransportConnected = 0x0002,
153  CLTPCS_Connected = 0x0004
154 } CLTPCStatus;
155 
156 // PS: 'CLTPSS' is for CHAILink Transport Protocol Server Status
157 typedef enum
158 {
159  CLTPSS_None = 0x0000,
160  CLTPSS_KeepAliveActive = 0x0001,
161  CLTPSS_KeepAliveSent = 0x0002,
162  CLTPSS_Connected = 0x0004,
163  CLTPSS_EthernetEnabled = 0x0008
164 } CLTPSStatus;
165 
166 //=============================================================================================
167 // Transport protocol: parser status
168 //=============================================================================================
169 // PS: 'CLTPPS' is for CHAILink Transport Protocol Parser Status
170 typedef enum
171 {
172  CLTPPS_WaitingSync,
173  CLTPPS_ReceivingHeader,
174  CLTPPS_ReceivingPayload,
175  CLTPPS_CRC,
176  CLTPPS_FrameComplete
177 } ParserStatus;
178 
180 
182 typedef enum {
193 
197 typedef enum {
204 
206 
207 //=============================================================================================
208 // Transport protocol: timing
209 //=============================================================================================
210 
211 // After transport protocol (client side) has received its ID, it sends '_CLTP_CMD_SETINITPARAMS'
212 // command. If there is no answer after this timeout, transport protocol goes back to init step.
213 #define CLTP_WAITINITPARAMSACK_TIMEOUTMS 2000
214 
215 // Transport inactivity time after which server send keep alive to client (in milliseconds)
216 #define CLTP_CLSKEEPALIVE_DELAYBEFORESENDING 500
217 // Maximum delay waiting ACK from client after which server consider this client is down (in milliseconds)
218 #define CLTP_CLSKEEPALIVE_DELAYBEFORERESET 2000
219 // Maximum delay between two keep alive after which a client consider the server is down (in milliseconds)
220 #define CLTP_CLCKEEPALIVE_DELAYBEFORERESET 2000
221 
222 //=============================================================================================
223 // Transport protocol Interface : Callbacks for CLTP => CL communication
224 //=============================================================================================
225 
226 // CLTP_ERRORCB : Callback for error reporting from CLTP to CHAILink. Helpful for debugging
227 // bReason : [In] Error code. see CLTP_ERRORCODE_xxx defines
228 // pParam : [In] CHAILink callback param (CLTP_CALLBACKS.pParam)
229 typedef void (*CLTP_ERRORCB)( CLTP_ErrorCode reason, void * const pParam);
230 // CLTP_FRAMRECEIVEDCB : Callback for CHAILink frame reception
231 // pBuffer : [In] Pointer to the received CHAILink message
232 // wSize : [In] CHAILink message size
233 // pParam : [In] CHAILink callback param (CLTP_CALLBACKS.pParam)
234 typedef void (*CLTP_FRAMRECEIVEDCB)( CPBYTE * pBuffer, CPUINT16 wSize, void * const pParam);
235 // CLTP_CONNECTIONSTATECHANGEDCB : Callback for CLTP connection status change.
236 // fConnected : [In] TRUE if Peer is connected, FALSE if Peer is disconnected
237 // wData : [In] if fConnected is TRUE, wData contains wMaxMessageSize, the maximum peer payload size.
238 // if fConnected is FALSE, wData contains the disconnection reason see CLTP_RESETCODE_xxx
239 // pParam : [In] CHAILink callback param (CLTP_CALLBACKS.pParam)
240 typedef void (*CLTP_CONNECTIONSTATECHANGEDCB)( CPBOOLEAN fConnected, CPUINT16 wData, void * const pParam);
241 
242 
243 typedef void (*CLTP_LOCK)(CPBOOLEAN fLock, void * const pParam);
244 
245 typedef struct
246 {
247  CLTP_ERRORCB pfnErrorCB;
248  CLTP_FRAMRECEIVEDCB pfnReceiveCB;
249  CLTP_CONNECTIONSTATECHANGEDCB pfnConnectionStateChangedCB;
250 #if defined(_CLTP_SERVERMODE)
251  CLTP_LOCK pfnLock;
252 #endif
253  void * pParam;
254 }CLTP_Callbacks;
255 
256 
257 //=============================================================================================
258 // Transport protocol: working structures
259 //=============================================================================================
260 // Will contains decoded header data and parsing informations
261 typedef struct
262 {
263  volatile CPBYTE* pbFillingCursor; // Pointer where to save data from transport to arbIncomingFrame
264  CPUINT16 wCommand; // Frame's command
265  CPUINT16 wPayloadSize; // Frame's Payload size
266  CPUINT16 wStatus; // Parser Status
267 } CLTP_ParserData;
268 
269 #if defined(_CLTP_CLIENTMODE)
270 typedef struct
271 {
272  CPUINT32 dwConnectTimer;
273  CPUINT32 dwCLCInactivityTimer;
274  CLTPCStatus eStatus;
275 } CLTP_CLCVariables;
276 #endif
277 
278 #if defined(_CLTP_SERVERMODE)
279 typedef struct _CLTP_CLSVariables
280 {
281  volatile CPUINT32 dwKeepAliveTimer;
282  CLTPSStatus eStatus;
283 } CLTP_CLSVariables, *PCLTP_CLSVariables;
284 #endif
285 
286 // Will contains all necessary data to work on frame
287 typedef struct
288 {
289  CPBYTE *pIncomingFrame;
290  CPBYTE *pOutgoingFrame;
291  CLT_Interface transportInterface; // Interface with physical transport
292  CLTP_Callbacks Callbacks; // Callbacks to CHAILink for transport reset or error
293 
294  CLTP_ParserData ParserData; // Contains frame header information
295 #if defined(_CLTP_CLIENTMODE)
296  CLTP_CLCVariables CLCVariables; // Contains some specific variables for CHAILink Client
297 #elif defined(_CLTP_SERVERMODE)
298  CLTP_CLSVariables CLSVariables; // Contains some specific variables for CHAILink Server
299 #endif
300  CPBYTE arbIncomingFrameT[CLTPRT_PAYLOADMAXSIZE + 3]; // Buffer used to save frame being decoded
301  CPBYTE arbOutgoingFrameT[CLTPRT_PAYLOADMAXSIZE + 3]; // Buffer used to prepare frame before sending
302 } CLTP_Working;
303 
304 //=========================================================================
305 // Transport protocol: API
306 //=========================================================================
307 // CLTP_Init : Init the transport protocol
308 // pWorking : [In] CLTP working data
309 // pCB : [In] CHAILink callbacks for Transport protocol Interface
310 // pTransportInterface : [In] Transport interface
311 //
312 // Return : Error code (see CLTP_ErrorCode)
313 CLTP_ErrorCode CLTP_Init(CLTP_Working * const pWorking, CLTP_Callbacks const * const pCB, CLT_Interface const * const pTransportInterface);
314 
315 // CLTP_DoProcess : Give processing time to CLTP. Must be called periodically.
316 // pWorking : [In] CLTP working data
317 void CLTP_DoProcess(CLTP_Working * const pWorking);
318 
319 // CLTP_ProcessTransport : Give processing time only to transport DoProcess.
320 // pWorking : [In] CLTP working data
321 void CLTP_ProcessTransport(CLTP_Working * const pWorking);
322 
323 // CLTP_IsReady : Check if CLTP is ready (connected).
324 // pWorking : [In] CLTP working data
325 //
326 // Return : TRUE if Ready
327 CPBOOLEAN CLTP_IsReady(CLTP_Working * const pWorking);
328 
329 // CLTP_GetOutgoingBuffer : Get outgoing payload buffer pointer in CLTP working data. Used to build frame in place
330 #define CLTP_GetOutgoingBuffer(pWorking) ((pWorking)->pOutgoingFrame + CLTP_HEADERSIZE)
331 
332 // CLTP_WritePayload : Send a CHAILink frame to the peer (TPCS => TPCC or TPCC => TPCS).
333 // pWorking : [In] CLTP working data
334 // pbPayload : [In] Pointer to the CHAILink frame
335 // wPayloadSize : [In] CHAILink frame size in byte
336 // pwWritten : [out] number of bytes effectively written
337 //
338 // Returned : TRUE if write was successful
339 CPBOOLEAN CLTP_WritePayload(CLTP_Working * const pWorking, CPBYTE const * const pbPayload, CPUINT16 const wPayloadSize, CPUINT16 * const pwWritten);
340 
341 #if defined(_CLTP_CLIENTMODE)
342 // CLTP_SetKeepAlive : Set keep alive mode for TPCC. Default is Active
343 // This function must be called just after CLTP_Init.
344 // pWorking : [In] CLTP working data
345 // fActive : [In] Keep alive active or not.
346 void CLTP_SetKeepAlive(CLTP_Working * const pWorking, CPBOOLEAN const fActive);
347 #endif
348 
349 // CLTP_ResetPeerState : Send a Reset state command to the peer (TPCS => TPCC or TPCC => TPCS).
350 // This function also resets Transport protocol state and do not call Connection state change callback.
351 // pWorking : [In] CLTP working data
352 void CLTP_ResetPeerState(CLTP_Working * const pWorking);
353 
354 // CLTP_ComputeCRC : Compute a CRC-8.
355 // pData : [In] Pointer to the data buffer
356 // wDataSize : [In] The data buffer size
357 //
358 // Returned : The CRC-8 value
359 CPBYTE CLTP_ComputeCRC( CPBYTE const * const pData, CPUINT16 const wDataSize );
360 
362 
363 #ifdef __cplusplus
364 }
365 #endif
366 
367 #endif // __CLCM_TRANSPORTPROTOCOL_H__
Definition: CLCm_TransportProtocol.h:187
Definition: CLCm_TransportProtocol.h:189
Definition: CLCm_TransportProtocol.h:190
Definition: CLCm_TransportProtocol.h:184
Definition: CLCm_TransportProtocol.h:191
Definition: CLCm_TransportProtocol.h:185
Definition: CLCm_TransportProtocol.h:186
Definition: CLCm_TransportProtocol.h:188
CLTP_ResetCode
Transport protocol: reset codes TPCS: Transport Protocol Client Side TPSS: Transport Protocol Server ...
Definition: CLCm_TransportProtocol.h:197
Happens when a peer (TPCS or TPCC) asks to its counterpart to reset its state.
Definition: CLCm_TransportProtocol.h:200
Definition: CLCm_TransportProtocol.h:183
Happens when TPCC try to connect (INITPARAMS) while TPCS state is already connected. TPCS MUST reset its state.
Definition: CLCm_TransportProtocol.h:201
Happens when a keep alive error occurred in the TPCS or TPCC.
Definition: CLCm_TransportProtocol.h:199
unsigned short CPUINT16
Word Integer (16 bit unsigned)
Definition: PlatformTypes.h:45
unsigned long CPUINT32
Double Word Integer (32 bit unsigned)
Definition: PlatformTypes.h:48
CLTP_ErrorCode
Transport protocol: error codes.
Definition: CLCm_TransportProtocol.h:182
Happens when the TPCS does not receive keep alive within CLTP_CLCKEEPALIVE_DELAYBEFORERESET.
Definition: CLCm_TransportProtocol.h:198
CHAI Link Transport Interface.
Definition: CLCm_Transport.h:82
Happens when a peer (TPCS or TPCC) sent a command not understood by its counterpart. There is a sync error between TPCC and TPCS : state must be reset.
Definition: CLCm_TransportProtocol.h:202