Merge branch 'develop'
[profile/ivi/dlt-daemon.git] / include / dlt / dlt_common.h
1 /*
2  * Dlt- Diagnostic Log and Trace
3  * @licence app begin@
4  *
5  * Copyright (C) 2011, BMW AG - Alexander Wenzel <alexander.wenzel@bmw.de>
6  * 
7  * This program is free software; you can redistribute it and/or modify it under the terms of the 
8  * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even 
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General 
11  * Public License, version 2.1, for more details.
12  * 
13  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along 
14  * with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
15  * 
16  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may 
17  * also be applicable to programs even in cases in which the program is not a library in the technical sense.
18  * 
19  * Linking DLT statically or dynamically with other modules is making a combined work based on DLT. You may 
20  * license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to 
21  * license your linked modules under the GNU Lesser General Public License, version 2.1, you 
22  * may use the program under the following exception.
23  * 
24  * As a special exception, the copyright holders of DLT give you permission to combine DLT 
25  * with software programs or libraries that are released under any license unless such a combination is not
26  * permitted by the license of such a software program or library. You may copy and distribute such a 
27  * system following the terms of the GNU Lesser General Public License, version 2.1, including this
28  * special exception, for DLT and the licenses of the other code concerned.
29  * 
30  * Note that people who make modified versions of DLT are not obligated to grant this special exception 
31  * for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, 
32  * version 2.1, gives permission to release a modified version without this exception; this exception 
33  * also makes it possible to release a modified version which carries forward this exception.
34  *
35  * @licence end@
36  */
37
38
39 /*******************************************************************************
40 **                                                                            **
41 **  SRC-MODULE: dlt_common.h                                                  **
42 **                                                                            **
43 **  TARGET    : linux                                                         **
44 **                                                                            **
45 **  PROJECT   : DLT                                                           **
46 **                                                                            **
47 **  AUTHOR    : Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
48 **              Markus Klein                                                  **
49 **                                                                            **
50 **  PURPOSE   :                                                               **
51 **                                                                            **
52 **  REMARKS   :                                                               **
53 **                                                                            **
54 **  PLATFORM DEPENDANT [yes/no]: yes                                          **
55 **                                                                            **
56 **  TO BE CHANGED BY USER [yes/no]: no                                        **
57 **                                                                            **
58 *******************************************************************************/
59
60 /*******************************************************************************
61 **                      Author Identity                                       **
62 ********************************************************************************
63 **                                                                            **
64 ** Initials     Name                       Company                            **
65 ** --------     -------------------------  ---------------------------------- **
66 **  aw          Alexander Wenzel           BMW                                **
67 **  mk          Markus Klein               Fraunhofer ESK                     **
68 *******************************************************************************/
69
70 /*******************************************************************************
71 **                      Revision Control History                              **
72 *******************************************************************************/
73
74 /*
75  * $LastChangedRevision: 1670 $
76  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
77  * $LastChangedBy$
78  Initials    Date         Comment
79  aw          13.01.2010   initial
80  */
81 #ifndef DLT_COMMON_H
82 #define DLT_COMMON_H
83
84 /**
85   \defgroup commonapi DLT Common API
86   \addtogroup commonapi
87   \{
88 */
89
90 #include <stdio.h>
91
92 #if !defined(_MSC_VER)
93 #include <unistd.h>
94 #include <time.h>
95 #endif
96
97 #if !defined (__WIN32__) && !defined(_MSC_VER)
98 #include <termios.h>
99 #endif
100
101 #include "dlt_types.h"
102 #include "dlt_protocol.h"
103
104 #if !defined (PACKED)
105 #define PACKED __attribute__((aligned(1),packed))
106 #endif
107
108 #if defined (__MSDOS__) || defined (_MSC_VER)
109 /* set instead /Zp8 flag in Visual C++ configuration */
110 #undef PACKED
111 #define PACKED
112 #endif
113
114 /*
115  * Macros to swap the byte order.
116  */
117 #define DLT_SWAP_64(value) ((((uint64_t)DLT_SWAP_32((value)&0xffffffffull))<<32) | (DLT_SWAP_32((value)>>32)))
118
119 #define DLT_SWAP_16(value) ((((value) >> 8)&0xff) | (((value) << 8)&0xff00))
120 #define DLT_SWAP_32(value) ((((value) >> 24)&0xff) | (((value) << 8)&0xff0000) | (((value) >> 8)&0xff00) | (((value) << 24)&0xff000000))
121
122 /* Set Big Endian and Little Endian to a initial value, if not defined */
123 #ifndef LITTLE_ENDIAN
124 #define LITTLE_ENDIAN 1234
125 #endif
126
127 #ifndef BIG_ENDIAN
128 #define BIG_ENDIAN    4321
129 #endif
130
131 /* If byte order is not defined, default to little endian */
132 #ifndef BYTE_ORDER
133 #define BYTE_ORDER LITTLE_ENDIAN
134 #endif
135
136 /* Check for byte-order */
137 #if (BYTE_ORDER==BIG_ENDIAN)
138 /* #warning "Big Endian Architecture!" */
139 #define DLT_HTOBE_16(x) ((x))
140 #define DLT_HTOLE_16(x) DLT_SWAP_16((x))
141 #define DLT_BETOH_16(x) ((x))
142 #define DLT_LETOH_16(x) DLT_SWAP_16((x))
143
144 #define DLT_HTOBE_32(x) ((x))
145 #define DLT_HTOLE_32(x) DLT_SWAP_32((x))
146 #define DLT_BETOH_32(x) ((x))
147 #define DLT_LETOH_32(x) DLT_SWAP_32((x))
148
149 #define DLT_HTOBE_64(x) ((x))
150 #define DLT_HTOLE_64(x) DLT_SWAP_64((x))
151 #define DLT_BETOH_64(x) ((x))
152 #define DLT_LETOH_64(x) DLT_SWAP_64((x))
153 #else
154 /* #warning "Litte Endian Architecture!" */
155 #define DLT_HTOBE_16(x) DLT_SWAP_16((x))
156 #define DLT_HTOLE_16(x) ((x))
157 #define DLT_BETOH_16(x) DLT_SWAP_16((x))
158 #define DLT_LETOH_16(x) ((x))
159
160 #define DLT_HTOBE_32(x) DLT_SWAP_32((x))
161 #define DLT_HTOLE_32(x) ((x))
162 #define DLT_BETOH_32(x) DLT_SWAP_32((x))
163 #define DLT_LETOH_32(x) ((x))
164
165 #define DLT_HTOBE_64(x) DLT_SWAP_64((x))
166 #define DLT_HTOLE_64(x) ((x))
167 #define DLT_BETOH_64(x) DLT_SWAP_64((x))
168 #define DLT_LETOH_64(x) ((x))
169 #endif
170
171 #define DLT_ENDIAN_GET_16(htyp,x) ((((htyp) & DLT_HTYP_MSBF)>0)?DLT_BETOH_16(x):DLT_LETOH_16(x))
172 #define DLT_ENDIAN_GET_32(htyp,x) ((((htyp) & DLT_HTYP_MSBF)>0)?DLT_BETOH_32(x):DLT_LETOH_32(x))
173 #define DLT_ENDIAN_GET_64(htyp,x) ((((htyp) & DLT_HTYP_MSBF)>0)?DLT_BETOH_64(x):DLT_LETOH_64(x))
174
175 #if defined (__WIN32__) || defined (_MSC_VER)
176 #define LOG_EMERG       0
177 #define LOG_ALERT       1
178 #define LOG_CRIT        2
179 #define LOG_ERR         3
180 #define LOG_WARNING     4
181 #define LOG_NOTICE      5
182 #define LOG_INFO        6
183 #define LOG_DEBUG       7
184
185 #define LOG_PID         0x01
186 #define LOG_DAEMON      (3<<3)
187 #endif
188
189 /**
190  * The standard TCP Port used for DLT daemon
191  */
192 #define DLT_DAEMON_TCP_PORT 3490
193
194 /**
195  * The size of a DLT ID
196  */
197 #define DLT_ID_SIZE 4
198
199 #define DLT_SIZE_WEID DLT_ID_SIZE
200 #define DLT_SIZE_WSID (sizeof(uint32_t))
201 #define DLT_SIZE_WTMS (sizeof(uint32_t))
202
203 /**
204  * Get the size of extra header parameters, depends on htyp.
205  */
206 #define DLT_STANDARD_HEADER_EXTRA_SIZE(htyp) ( (DLT_IS_HTYP_WEID(htyp) ? DLT_SIZE_WEID : 0) + (DLT_IS_HTYP_WSID(htyp) ? DLT_SIZE_WSID : 0) + (DLT_IS_HTYP_WTMS(htyp) ? DLT_SIZE_WTMS : 0) )
207
208
209 #if defined (__MSDOS__) || defined (_MSC_VER)
210 #define __func__ __FUNCTION__
211 #endif
212
213 #define PRINT_FUNCTION_VERBOSE(_verbose)  \
214 { \
215         static char _strbuf[255]; \
216     \
217     if(_verbose) \
218     { \
219                 sprintf(_strbuf, "%s()\n",__func__); \
220                 dlt_log(LOG_INFO, _strbuf); \
221     } \
222 }
223
224 #ifndef NULL
225 #define NULL (char*)0
226 #endif
227
228 #define DLT_MSG_IS_CONTROL(MSG)          ((DLT_IS_HTYP_UEH((MSG)->standardheader->htyp)) && \
229                                           (DLT_GET_MSIN_MSTP((MSG)->extendedheader->msin)==DLT_TYPE_CONTROL))
230
231 #define DLT_MSG_IS_CONTROL_REQUEST(MSG)  ((DLT_IS_HTYP_UEH((MSG)->standardheader->htyp)) && \
232                                           (DLT_GET_MSIN_MSTP((MSG)->extendedheader->msin)==DLT_TYPE_CONTROL) && \
233                                           (DLT_GET_MSIN_MTIN((MSG)->extendedheader->msin)==DLT_CONTROL_REQUEST))
234
235 #define DLT_MSG_IS_CONTROL_RESPONSE(MSG) ((DLT_IS_HTYP_UEH((MSG)->standardheader->htyp)) && \
236                                           (DLT_GET_MSIN_MSTP((MSG)->extendedheader->msin)==DLT_TYPE_CONTROL) && \
237                                           (DLT_GET_MSIN_MTIN((MSG)->extendedheader->msin)==DLT_CONTROL_RESPONSE))
238
239 #define DLT_MSG_IS_CONTROL_TIME(MSG)     ((DLT_IS_HTYP_UEH((MSG)->standardheader->htyp)) && \
240                                           (DLT_GET_MSIN_MSTP((MSG)->extendedheader->msin)==DLT_TYPE_CONTROL) && \
241                                           (DLT_GET_MSIN_MTIN((MSG)->extendedheader->msin)==DLT_CONTROL_TIME))
242
243 #define DLT_MSG_IS_NW_TRACE(MSG)         ((DLT_IS_HTYP_UEH((MSG)->standardheader->htyp)) && \
244                                                                               (DLT_GET_MSIN_MSTP((MSG)->extendedheader->msin)==DLT_TYPE_NW_TRACE))
245
246 #define DLT_MSG_IS_TRACE_MOST(MSG)       ((DLT_IS_HTYP_UEH((MSG)->standardheader->htyp)) && \
247                                                                               (DLT_GET_MSIN_MSTP((MSG)->extendedheader->msin)==DLT_TYPE_NW_TRACE) && \
248                                                                               (DLT_GET_MSIN_MTIN((MSG)->extendedheader->msin)==DLT_NW_TRACE_MOST))
249
250 #define DLT_MSG_IS_NONVERBOSE(MSG)       (!(DLT_IS_HTYP_UEH((MSG)->standardheader->htyp)) || \
251                                           ((DLT_IS_HTYP_UEH((MSG)->standardheader->htyp)) && (!(DLT_IS_MSIN_VERB((MSG)->extendedheader->msin)))))
252
253 /*
254
255  * Definitions of DLT message buffer overflow
256  */
257 #define DLT_MESSAGE_BUFFER_NO_OVERFLOW     0x00 /**< Buffer overflow has not occured */
258 #define DLT_MESSAGE_BUFFER_OVERFLOW        0x01 /**< Buffer overflow has occured */
259
260 /*
261  * Definition of DLT output variants
262  */
263 #define DLT_OUTPUT_HEX              1
264 #define DLT_OUTPUT_ASCII            2
265 #define DLT_OUTPUT_MIXED_FOR_PLAIN  3
266 #define DLT_OUTPUT_MIXED_FOR_HTML   4
267 #define DLT_OUTPUT_ASCII_LIMITED    5
268
269 #define DLT_FILTER_MAX 30 /**< Maximum number of filters */
270
271 #define DLT_MSG_READ_VALUE(dst,src,length,type) \
272     { \
273     if((length<0) || ((length)<((int32_t)sizeof(type)))) \
274         { length = -1; } \
275     else \
276         { dst = *((type*)src);src+=sizeof(type);length-=sizeof(type); } \
277     }
278
279 #define DLT_MSG_READ_ID(dst,src,length) \
280     { \
281     if((length<0) || ((length)<DLT_ID_SIZE)) \
282         { length = -1; } \
283     else \
284         { memcpy(dst,src,DLT_ID_SIZE);src+=DLT_ID_SIZE;length-=DLT_ID_SIZE; } \
285     }
286
287 #define DLT_MSG_READ_STRING(dst,src,maxlength,length) \
288     { \
289     if(((maxlength)<0) || ((length)<0) || ((maxlength)<(length))) \
290         { maxlength = -1; } \
291     else \
292         { memcpy(dst,src,length);dlt_clean_string(dst,length);dst[length]=0; \
293           src+=length;maxlength-=length; } \
294     }
295
296 #define DLT_MSG_READ_NULL(src,maxlength,length) \
297     { \
298     if(((maxlength)<0) || ((length)<0) || ((maxlength)<(length))) \
299         { length = -1; } \
300     else \
301         { src+=length;maxlength-=length; } \
302     }
303
304 #define DLT_HEADER_SHOW_NONE       0x0000
305 #define DLT_HEADER_SHOW_TIME       0x0001
306 #define DLT_HEADER_SHOW_TMSTP      0x0002
307 #define DLT_HEADER_SHOW_MSGCNT     0x0004
308 #define DLT_HEADER_SHOW_ECUID      0x0008
309 #define DLT_HEADER_SHOW_APID       0x0010
310 #define DLT_HEADER_SHOW_CTID       0x0020
311 #define DLT_HEADER_SHOW_MSGTYPE    0x0040
312 #define DLT_HEADER_SHOW_MSGSUBTYPE 0x0080
313 #define DLT_HEADER_SHOW_VNVSTATUS  0x0100
314 #define DLT_HEADER_SHOW_NOARG      0x0200
315 #define DLT_HEADER_SHOW_ALL        0xFFFF
316
317 /**
318  * The definition of the serial header containing the characters "DLS" + 0x01.
319  */
320 extern const char dltSerialHeader[DLT_ID_SIZE];
321
322 /**
323  * The definition of the serial header containing the characters "DLS" + 0x01 as char.
324  */
325 extern char dltSerialHeaderChar[DLT_ID_SIZE];
326
327 /**
328
329  * The type of a DLT ID (context id, application id, etc.)
330  */
331 typedef char ID4[DLT_ID_SIZE];
332
333 /**
334  * The structure of the DLT file storage header. This header is used before each stored DLT message.
335  */
336 typedef struct
337 {
338     char pattern[DLT_ID_SIZE];          /**< This pattern should be DLT0x01 */
339     time_t seconds;                                 /**< seconds since 1.1.1970 */
340     int32_t microseconds;                       /**< Microseconds */
341     char ecu[DLT_ID_SIZE];                      /**< The ECU id is added, if it is not already in the DLT message itself */
342 } PACKED DltStorageHeader;
343
344 /**
345  * The structure of the DLT standard header. This header is used in each DLT message.
346  */
347 typedef struct
348 {
349     uint8_t htyp;           /**< This parameter contains several informations, see definitions below */
350     uint8_t mcnt;           /**< The message counter is increased with each sent DLT message */
351     uint16_t len;           /**< Length of the complete message, without storage header */
352 } PACKED DltStandardHeader;
353
354 /**
355  * The structure of the DLT extra header parameters. Each parameter is sent only if enabled in htyp.
356  */
357 typedef struct
358 {
359     char ecu[DLT_ID_SIZE];       /**< ECU id */
360     uint32_t seid;     /**< Session number */
361     uint32_t tmsp;     /**< Timestamp since system start in 0.1 milliseconds */
362 } PACKED DltStandardHeaderExtra;
363
364 /**
365  * The structure of the DLT extended header. This header is only sent if enabled in htyp parameter.
366  */
367 typedef struct
368 {
369     uint8_t msin;          /**< messsage info */
370     uint8_t noar;          /**< number of arguments */
371     char apid[DLT_ID_SIZE];          /**< application id */
372     char ctid[DLT_ID_SIZE];          /**< context id */
373 } PACKED DltExtendedHeader;
374
375 /**
376  * The structure to organise the DLT messages.
377  * This structure is used by the corresponding functions.
378  */
379 typedef struct sDltMessage
380 {
381     /* flags */
382     int8_t found_serialheader;
383
384     /* offsets */
385     int32_t resync_offset;
386
387     /* size parameters */
388     int32_t headersize;    /**< size of complete header including storage header */
389     int32_t datasize;      /**< size of complete payload */
390
391     /* buffer for current loaded message */
392     uint8_t headerbuffer[sizeof(DltStorageHeader)+
393                          sizeof(DltStandardHeader)+sizeof(DltStandardHeaderExtra)+sizeof(DltExtendedHeader)]; /**< buffer for loading complete header */
394     uint8_t *databuffer;         /**< buffer for loading payload */
395
396     /* header values of current loaded message */
397     DltStorageHeader       *storageheader;  /**< pointer to storage header of current loaded header */
398     DltStandardHeader      *standardheader; /**< pointer to standard header of current loaded header */
399     DltStandardHeaderExtra headerextra;     /**< extra parameters of current loaded header */
400     DltExtendedHeader      *extendedheader; /**< pointer to extended of current loaded header */
401 } DltMessage;
402
403 /**
404  * The structure of the DLT Service Get Log Info.
405  */
406 typedef struct
407 {
408     uint32_t service_id;            /**< service ID */
409     uint8_t options;                /**< type of request */
410     char apid[DLT_ID_SIZE];                   /**< application id */
411     char ctid[DLT_ID_SIZE];                   /**< context id */
412     char com[DLT_ID_SIZE];                    /**< communication interface */
413 } PACKED DltServiceGetLogInfoRequest;
414
415 /**
416  * The structure of the DLT Service Set Log Level.
417  */
418 typedef struct
419 {
420     uint32_t service_id;            /**< service ID */
421     char apid[DLT_ID_SIZE];                   /**< application id */
422     char ctid[DLT_ID_SIZE];                   /**< context id */
423     uint8_t log_level;              /**< log level to be set */
424     char com[DLT_ID_SIZE];                    /**< communication interface */
425 } PACKED DltServiceSetLogLevel;
426
427 /**
428  * The structure of the DLT Service Set Default Log Level.
429  */
430 typedef struct
431 {
432     uint32_t service_id;            /**< service ID */
433     uint8_t log_level;              /**< default log level to be set */
434     char com[DLT_ID_SIZE];                    /**< communication interface */
435 } PACKED DltServiceSetDefaultLogLevel;
436
437 /**
438  * The structure of the DLT Service Set Verbose Mode
439  */
440 typedef struct
441 {
442     uint32_t service_id;            /**< service ID */
443     uint8_t new_status;             /**< new status to be set */
444 } PACKED DltServiceSetVerboseMode;
445
446 /**
447  * The structure of the DLT Service Set Communication Interface Status
448  */
449 typedef struct
450 {
451     uint32_t service_id;            /**< service ID */
452     char com[DLT_ID_SIZE];                    /**< communication interface */
453     uint8_t new_status;             /**< new status to be set */
454 } PACKED DltServiceSetCommunicationInterfaceStatus;
455
456 /**
457  * The structure of the DLT Service Set Communication Maximum Bandwidth
458  */
459 typedef struct
460 {
461     uint32_t service_id;            /**< service ID */
462     char com[DLT_ID_SIZE];                    /**< communication interface */
463     uint32_t max_bandwidth;         /**< maximum bandwith */
464 } PACKED DltServiceSetCommunicationMaximumBandwidth;
465
466 typedef struct
467 {
468     uint32_t service_id;            /**< service ID */
469     uint8_t status;                 /**< reponse status */
470 } PACKED DltServiceResponse;
471
472 typedef struct
473 {
474     uint32_t service_id;            /**< service ID */
475     uint8_t status;                 /**< reponse status */
476     uint8_t log_level;              /**< log level */
477 } PACKED DltServiceGetDefaultLogLevelResponse;
478
479 typedef struct
480 {
481     uint32_t service_id;            /**< service ID */
482     uint8_t status;                 /**< reponse status */
483     uint8_t overflow;               /**< overflow status */
484 } PACKED DltServiceMessageBufferOverflowResponse;
485
486 typedef struct
487 {
488     uint32_t service_id;            /**< service ID */
489     uint8_t  status;                /**< reponse status */
490     uint32_t length;                /**< length of following payload */
491     /* char [] payload */
492 } PACKED DltServiceGetSoftwareVersionResponse;
493
494 /**
495  * Structure to store filter parameters.
496  * ID are maximal four characters. Unused values are filled with zeros.
497  * If every value as filter is valid, the id should be empty by having only zero values.
498  */
499 typedef struct
500 {
501     char apid[DLT_FILTER_MAX][DLT_ID_SIZE]; /**< application id */
502     char ctid[DLT_FILTER_MAX][DLT_ID_SIZE]; /**< context id */
503     int  counter;                           /**< number of filters */
504 } DltFilter;
505
506 /**
507  * The structure to organise the access to DLT files.
508  * This structure is used by the corresponding functions.
509  */
510 typedef struct sDltFile
511 {
512     /* file handle and index for fast access */
513     FILE *handle;      /**< file handle of opened DLT file */
514     long *index;       /**< file positions of all DLT messages for fast access to file, only filtered messages */
515
516     /* size parameters */
517     int32_t counter;       /**< number of messages in DLT file with filter */
518     int32_t counter_total; /**< number of messages in DLT file without filter */
519     int32_t position;      /**< current index to message parsed in DLT file starting at 0 */
520     long file_length;  /**< length of the file */
521     long file_position; /**< current position in the file */
522
523     /* error counters */
524     int32_t error_messages; /**< number of incomplete DLT messages found during file parsing */
525
526     /* filter parameters */
527     DltFilter *filter;  /**< pointer to filter list. Zero if no filter is set. */
528     int32_t filter_counter; /**< number of filter set */
529
530     /* current loaded message */
531     DltMessage msg;     /**< pointer to message */
532
533 } DltFile;
534
535 /**
536  * The structure is used to organise the receiving of data
537  * including buffer handling.
538  * This structure is used by the corresponding functions.
539  */
540 typedef struct
541 {
542     int32_t lastBytesRcvd;    /**< bytes received in last receive call */
543     int32_t bytesRcvd;        /**< received bytes */
544     int32_t totalBytesRcvd;   /**< total number of received bytes */
545     char *buffer;         /**< pointer to receiver buffer */
546     char *buf;            /**< pointer to position within receiver buffer */
547     int fd;               /**< connection handle */
548     int32_t buffersize;       /**< size of receiver buffer */
549 } DltReceiver;
550
551 typedef struct
552 {
553     char*   buffer;         /**< Ptr. to buffer */
554     uint32_t    size;       /**< Maximum size of buffer */
555     uint32_t    pos_write;  /**< current writing position in bytes*/
556     uint32_t    pos_read;   /**< current reading position in bytes*/
557     uint32_t    count;      /**< nr. of entries */
558     uint32_t    minimum_size;   /**< minimum value for the buffer  */
559     uint32_t    increasing_size;        /**< increasing value for the buffer    */
560     uint32_t    maximum_size;   /**< maximum value for the buffer*/
561     
562 } DltRingBuffer;
563
564 #ifdef __cplusplus
565 extern "C"
566 {
567 #endif
568
569     /**
570      * Helper function to print a byte array in hex.
571      * @param ptr pointer to the byte array.
572      * @param size number of bytes to be printed.
573      */
574     void dlt_print_hex(uint8_t *ptr,int size);
575     /**
576      * Helper function to print a byte array in hex into a string.
577      * @param text pointer to a ASCII string, in which the text is written
578      * @param textlength maximal size of text buffer
579      * @param ptr pointer to the byte array.
580      * @param size number of bytes to be printed.
581      * @return negative value if there was an error
582      */
583     int dlt_print_hex_string(char *text,int textlength,uint8_t *ptr,int size);
584     /**
585      * Helper function to print a byte array in hex and ascii into a string.
586      * @param text pointer to a ASCII string, in which the text is written
587      * @param textlength maximal size of text buffer
588      * @param ptr pointer to the byte array.
589      * @param size number of bytes to be printed.
590      * @param html output is html? 0 - false, 1 - true
591      * @return negative value if there was an error
592      */
593     int dlt_print_mixed_string(char *text,int textlength,uint8_t *ptr,int size,int html);
594     /**
595      * Helper function to print a byte array in ascii into a string.
596      * @param text pointer to a ASCII string, in which the text is written
597      * @param textlength maximal size of text buffer
598      * @param ptr pointer to the byte array.
599      * @param size number of bytes to be printed.
600      * @return negative value if there was an error
601      */
602     int dlt_print_char_string(char **text,int textlength,uint8_t *ptr,int size);
603
604     /**
605      * Helper function to print an id.
606      * @param text pointer to ASCII string where to write the id
607      * @param id four byte char array as used in DLT mesages as IDs.
608      */
609     void dlt_print_id(char *text,const char *id);
610
611     /**
612      * Helper function to set an ID parameter.
613      * @param id four byte char array as used in DLT mesages as IDs.
614      * @param text string to be copied into char array.
615      */
616     void dlt_set_id(char *id,const char *text);
617
618     /**
619      * Helper function to remove not nice to print characters, e.g. NULL or carage return.
620      * @param text pointer to string to be cleaned.
621      * @param length length of string excluding terminating zero.
622      */
623     void dlt_clean_string(char *text,int length);
624
625     /**
626      * Initialise the filter list.
627      * This function must be called before using further dlt filter.
628      * @param filter pointer to structure of organising DLT filter
629      * @param verbose if set to true verbose information is printed out.
630      * @return negative value if there was an error
631      */
632     int dlt_filter_init(DltFilter *filter,int verbose);
633     /**
634      * Free the used memory by the organising structure of filter.
635      * @param filter pointer to structure of organising DLT filter
636      * @param verbose if set to true verbose information is printed out.
637      * @return negative value if there was an error
638      */
639     int dlt_filter_free(DltFilter *filter,int verbose);
640     /**
641      * Load filter list from file.
642      * @param filter pointer to structure of organising DLT filter
643      * @param filename filename to load filters from
644      * @param verbose if set to true verbose information is printed out.
645      * @return negative value if there was an error
646      */
647     int dlt_filter_load(DltFilter *filter,const char *filename,int verbose);
648     /**
649      * Save filter list to file.
650      * @param filter pointer to structure of organising DLT filter
651      * @param filename filename to load filters from
652      * @param verbose if set to true verbose information is printed out.
653      * @return negative value if there was an error
654      */
655     int dlt_filter_save(DltFilter *filter,const char *filename,int verbose);
656     /**
657      * Find index of filter in filter list
658      * @param filter pointer to structure of organising DLT filter
659      * @param apid application id to be found in filter list
660      * @param ctid context id to be found in filter list
661      * @param verbose if set to true verbose information is printed out.
662      * @return negative value if there was an error (or not found), else return index of filter
663      */
664     int dlt_filter_find(DltFilter *filter,const char *apid,const char *ctid, int verbose);
665     /**
666      * Add new filter to filter list.
667      * @param filter pointer to structure of organising DLT filter
668      * @param apid application id to be added to filter list (must always be set).
669      * @param ctid context id to be added to filter list. empty equals don't care.
670      * @param verbose if set to true verbose information is printed out.
671      * @return negative value if there was an error
672      */
673     int dlt_filter_add(DltFilter *filter,const char *apid,const char *ctid,int verbose);
674     /**
675      * Delete filter from filter list
676      * @param filter pointer to structure of organising DLT filter
677      * @param apid application id to be deleted from filter list
678      * @param ctid context id to be deleted from filter list
679      * @param verbose if set to true verbose information is printed out.
680      * @return negative value if there was an error
681      */
682     int dlt_filter_delete(DltFilter *filter,const char *apid,const char *ctid,int verbose);
683
684     /**
685      * Initialise the structure used to access a DLT message.
686      * This function must be called before using further dlt_message functions.
687      * @param msg pointer to structure of organising access to DLT messages
688      * @param verbose if set to true verbose information is printed out.
689      * @return negative value if there was an error
690      */
691     int dlt_message_init(DltMessage *msg,int verbose);
692     /**
693      * Free the used memory by the organising structure of file.
694      * @param msg pointer to structure of organising access to DLT messages
695      * @param verbose if set to true verbose information is printed out.
696      * @return negative value if there was an error
697      */
698     int dlt_message_free(DltMessage *msg,int verbose);
699     /**
700      * Print Header into an ASCII string.
701      * This function calls dlt_message_header_flags() with flags=DLT_HEADER_SHOW_ALL
702      * @param msg pointer to structure of organising access to DLT messages
703      * @param text pointer to a ASCII string, in which the header is written
704      * @param textlength maximal size of text buffer
705      * @param verbose if set to true verbose information is printed out.
706      * @return negative value if there was an error
707      */
708     int dlt_message_header(DltMessage *msg,char *text,int textlength,int verbose);
709     /**
710      * Print Header into an ASCII string, selective.
711      * @param msg pointer to structure of organising access to DLT messages
712      * @param text pointer to a ASCII string, in which the header is written
713      * @param textlength maximal size of text buffer
714      * @param flags select, bit-field to select, what should be printed (DLT_HEADER_SHOW_...)
715      * @param verbose if set to true verbose information is printed out.
716      * @return negative value if there was an error
717      */
718     int dlt_message_header_flags(DltMessage *msg,char *text,int textlength,int flags, int verbose);
719     /**
720      * Print Payload into an ASCII string.
721      * @param msg pointer to structure of organising access to DLT messages
722      * @param text pointer to a ASCII string, in which the header is written
723      * @param textlength maximal size of text buffer
724      * @param type 1 = payload as hex, 2 = payload as ASCII.
725      * @param verbose if set to true verbose information is printed out.
726      * @return negative value if there was an error
727      */
728     int dlt_message_payload(DltMessage *msg,char *text,int textlength,int type,int verbose);
729     /**
730      * Check if message is filtered or not. All filters are applied (logical OR).
731      * @param msg pointer to structure of organising access to DLT messages
732      * @param filter pointer to filter
733      * @param verbose if set to true verbose information is printed out.
734      * @return 1 = filter matches, 0 = filter does not match, negative value if there was an error
735      */
736     int dlt_message_filter_check(DltMessage *msg,DltFilter *filter,int verbose);
737
738     /**
739      * Read message from memory buffer.
740      * Message in buffer has no storage header.
741      * @param msg pointer to structure of organising access to DLT messages
742      * @param buffer pointer to memory buffer
743      * @param length length of message in buffer
744      * @param resync if set to true resync to serial header is enforced
745      * @param verbose if set to true verbose information is printed out.
746      * @return negative value if there was an error
747      */
748     int dlt_message_read(DltMessage *msg,uint8_t *buffer,unsigned int length,int resync,int verbose);
749
750     /**
751      * Get standard header extra parameters
752      * @param msg pointer to structure of organising access to DLT messages
753      * @param verbose if set to true verbose information is printed out.
754      * @return negative value if there was an error
755      */
756     int dlt_message_get_extraparameters(DltMessage *msg,int verbose);
757
758     /**
759      * Set standard header extra parameters
760      * @param msg pointer to structure of organising access to DLT messages
761      * @param verbose if set to true verbose information is printed out.
762      * @return negative value if there was an error
763      */
764     int dlt_message_set_extraparameters(DltMessage *msg,int verbose);
765
766     /**
767      * Initialise the structure used to access a DLT file.
768      * This function must be called before using further dlt_file functions.
769      * @param file pointer to structure of organising access to DLT file
770      * @param verbose if set to true verbose information is printed out.
771      * @return negative value if there was an error
772      */
773     int dlt_file_init(DltFile *file,int verbose);
774     /**
775      * Set a list to filters.
776      * This function should be called before loading a DLT file, if filters should be used.
777      * A filter list is an array of filters. Several filters are combined logically by or operation.
778      * The filter list is not copied, so take care to keep list in memory.
779      * @param file pointer to structure of organising access to DLT file
780      * @param filter pointer to filter list array
781      * @param verbose if set to true verbose information is printed out.
782      * @return negative value if there was an error
783      */
784     int dlt_file_set_filter(DltFile *file,DltFilter *filter,int verbose);
785     /**
786      * Initialising loading a DLT file.
787      * @param file pointer to structure of organising access to DLT file
788      * @param filename filename of DLT file
789      * @param verbose if set to true verbose information is printed out.
790      * @return negative value if there was an error
791      */
792     int dlt_file_open(DltFile *file,const char *filename,int verbose);
793     /**
794      * Find next message in the DLT file and parse them.
795      * This function finds the next message in the DLT file.
796      * If a filter is set, the filter list is used.
797      * @param file pointer to structure of organising access to DLT file
798      * @param verbose if set to true verbose information is printed out.
799      * @return 0 = message does not match filter, 1 = message was read, negative value if there was an error
800      */
801     int dlt_file_read(DltFile *file,int verbose);
802     /**
803      * Find next message in the DLT file in RAW format (without storage header) and parse them.
804      * This function finds the next message in the DLT file.
805      * If a filter is set, the filter list is used.
806      * @param file pointer to structure of organising access to DLT file
807      * @param resync Resync to serial header when set to true
808      * @param verbose if set to true verbose information is printed out.
809      * @return 0 = message does not match filter, 1 = message was read, negative value if there was an error
810      */
811     int dlt_file_read_raw(DltFile *file,int resync,int verbose);
812     /**
813      * Closing loading a DLT file.
814      * @param file pointer to structure of organising access to DLT file
815      * @param verbose if set to true verbose information is printed out.
816      * @return negative value if there was an error
817      */
818     int dlt_file_close(DltFile *file,int verbose);
819     /**
820      * Load standard header of a message from file
821      * @param file pointer to structure of organising access to DLT file
822      * @param verbose if set to true verbose information is printed out.
823      * @return negative value if there was an error
824      */
825     int dlt_file_read_header(DltFile *file,int verbose);
826     /**
827      * Load standard header of a message from file in RAW format (without storage header)
828      * @param file pointer to structure of organising access to DLT file
829      * @param resync Resync to serial header when set to true
830      * @param verbose if set to true verbose information is printed out.
831      * @return negative value if there was an error
832      */
833     int dlt_file_read_header_raw(DltFile *file,int resync,int verbose);
834     /**
835      * Load, if available in message, extra standard header fields and
836      * extended header of a message from file
837      * (dlt_file_read_header() must have been called before this call!)
838      * @param file pointer to structure of organising access to DLT file
839      * @param verbose if set to true verbose information is printed out.
840      * @return negative value if there was an error
841      */
842     int dlt_file_read_header_extended(DltFile *file, int verbose);
843     /**
844      * Load payload of a message from file
845      * (dlt_file_read_header() must have been called before this call!)
846      * @param file pointer to structure of organising access to DLT file
847      * @param verbose if set to true verbose information is printed out.
848      * @return negative value if there was an error
849      */
850     int dlt_file_read_data(DltFile *file, int verbose);
851     /**
852      * Load headers and payload of a message selected by the index.
853      * If filters are set, index is based on the filtered list.
854      * @param file pointer to structure of organising access to DLT file
855      * @param index position of message in the files beginning from zero
856      * @param verbose if set to true verbose information is printed out.
857      * @return number of messages loaded, negative value if there was an error
858      */
859     int dlt_file_message(DltFile *file,int index,int verbose);
860     /**
861      * Free the used memory by the organising structure of file.
862      * @param file pointer to structure of organising access to DLT file
863      * @param verbose if set to true verbose information is printed out.
864      * @return negative value if there was an error
865      */
866     int dlt_file_free(DltFile *file,int verbose);
867
868     /**
869      * Initialize (external) logging facility
870      * @param mode positive, if syslog should be used; zero for console output
871      */
872     void dlt_log_init(int mode);
873     /**
874      * Log ASCII string with null-termination to (external) logging facility
875      * @param prio priority (see syslog() call)
876      * @param s Pointer to ASCII string with null-termination
877      * @return negative value if there was an error
878      */
879     int dlt_log(int prio, char *s);
880     /**
881      * De-Initialize (external) logging facility
882      */
883     void dlt_log_free(void);
884
885     /**
886      * Initialising a dlt receiver structure
887      * @param receiver pointer to dlt receiver structure
888      * @param _fd handle to file/socket/fifo, fram which the data should be received
889      * @param _buffersize size of data buffer for storing the received data
890      * @return negative value if there was an error
891      */
892     int dlt_receiver_init(DltReceiver *receiver,int _fd, int _buffersize);
893     /**
894      * De-Initialize a dlt receiver structure
895      * @param receiver pointer to dlt receiver structure
896      * @return negative value if there was an error
897      */
898     int dlt_receiver_free(DltReceiver *receiver);
899     /**
900      * Receive data from socket using the dlt receiver structure
901      * @param receiver pointer to dlt receiver structure
902      * @return negative value if there was an error
903      */
904     int dlt_receiver_receive_socket(DltReceiver *receiver);
905     /**
906      * Receive data from file/fifo using the dlt receiver structure
907      * @param receiver pointer to dlt receiver structure
908      * @return negative value if there was an error
909      */
910     int dlt_receiver_receive_fd(DltReceiver *receiver);
911     /**
912      * Remove a specific size of bytes from the received data
913      * @param receiver pointer to dlt receiver structure
914      * @param size amount of bytes to be removed
915      * @return negative value if there was an error
916      */
917     int dlt_receiver_remove(DltReceiver *receiver,int size);
918     /**
919      * Move data from last receive call to front of receive buffer
920      * @param receiver pointer to dlt receiver structure
921      * @return negative value if there was an error
922      */
923     int dlt_receiver_move_to_begin(DltReceiver *receiver);
924
925     /**
926      * Fill out storage header of a dlt message
927      * @param storageheader pointer to storage header of a dlt message
928      * @param ecu name of ecu to be set in storage header
929      * @return negative value if there was an error
930      */
931     int dlt_set_storageheader(DltStorageHeader *storageheader, const char *ecu);
932     /**
933      * Check if a storage header contains its marker
934      * @param storageheader pointer to storage header of a dlt message
935      * @return 0 no, 1 yes, negative value if there was an error
936      */
937     int dlt_check_storageheader(DltStorageHeader *storageheader);
938
939     /**
940      * Initialize ringbuffer of with a maximum size of size
941      * @param dltbuf Pointer to ringbuffer structure
942      * @param size Maximum size of buffer in bytes
943      * @return negative value if there was an error
944      */
945     int dlt_ringbuffer_init(DltRingBuffer *dltbuf, uint32_t size, uint32_t increase_size, uint32_t max_size);
946
947     /**
948      * Release and free memory used by ringbuffer
949      * @param dltbuf Pointer to ringbuffer structure
950      * @return negative value if there was an error
951      */
952     int dlt_ringbuffer_free(DltRingBuffer *dltbuf);
953
954     /**
955      * Write one entry to ringbuffer
956      * @param dltbuf Pointer to ringbuffer structure
957      * @param data Pointer to data to be written to ringbuffer
958      * @param size Size of data in bytes to be written to ringbuffer
959      * @return negative value if there was an error
960      */
961     int dlt_ringbuffer_put(DltRingBuffer *dltbuf, void *data, uint32_t size);
962
963     /**
964      * Write one entry given as 3 chunks to ringbuffer
965      * @param dltbuf Pointer to ringbuffer structure
966      * @param data1 Pointer to data1 to be written to ringbuffer
967      * @param size1 Size of data1 in bytes to be written to ringbuffer
968      * @param data2 Pointer to data2 to be written to ringbuffer
969      * @param size2 Size of data2 in bytes to be written to ringbuffer
970      * @param data3 Pointer to data3 to be written to ringbuffer
971      * @param size3 Size of data3 in bytes to be written to ringbuffer
972      * @return negative value if there was an error
973      */
974     int dlt_ringbuffer_put3(DltRingBuffer *dltbuf, void *data1, uint32_t size1, void *data2, uint32_t size2, void *data3, uint32_t size3);
975
976          /**
977      * This method writes the size of message
978      * @param dltbuf Pointer to ringbuffer structure
979      * @param data_size Pointer to size of data to be written
980      * @param unit_size Size of uint32_t   
981      */
982         void dlt_ringbuffer_putMessageSize(DltRingBuffer *dltbuf,uint32_t * data_size, uint32_t unit_size);
983         
984          /**
985      * This method reads the size of a message 
986      * @param dltbuf Pointer to ringbuffer structure
987      * @param unit_size Size of uint32_t 
988      * @return size of the message  
989      */
990         int dlt_ringbuffer_getMessageSize(DltRingBuffer *dltbuf, uint32_t unit_size);
991     /**
992      * Read one entry from ringbuffer
993      * @param dltbuf Pointer to ringbuffer structure
994      * @param data Pointer to data read from ringbuffer
995      * @param size Size of read data in bytes from ringbuffer
996      * @return negative value if there was an error
997      */
998     int dlt_ringbuffer_get(DltRingBuffer *dltbuf, void *data, size_t *size);
999
1000     /**
1001      * Helper function: Skip one readable entry in ringbuffer
1002      * @param dltbuf Pointer to ringbuffer structure
1003      * @return negative value if there was an error
1004      */
1005     int dlt_ringbuffer_get_skip(DltRingBuffer *dltbuf);
1006
1007     /**
1008      * Helper function: Get free space in bytes for writting between write and read position
1009      * @param dltbuf Pointer to ringbuffer structure
1010      * @param freespace Free Space in bytes for writting is returned
1011      * @return negative value if there was an error
1012      */
1013     int dlt_ringbuffer_freespacewrite(DltRingBuffer *dltbuf, uint32_t *freespace);
1014
1015     /**
1016      * Helper function: Check free space and if necessary discard entries, so that at least
1017      * reqspace bytes are available for writting
1018      * @param dltbuf Pointer to ringbuffer structure
1019      * @param reqspace Requested space for writting in bytes
1020      * @return negative value if there was an error
1021      */
1022     int dlt_ringbuffer_checkandfreespace(DltRingBuffer *dltbuf, uint32_t reqspace);
1023
1024 #if !defined (__WIN32__)
1025
1026     /**
1027      * Helper function: Setup serial connection
1028      * @param fd File descriptor of serial tty device
1029      * @param speed Serial line speed, as defined in termios.h
1030      * @return negative value if there was an error
1031      */
1032     int dlt_setup_serial(int fd, speed_t speed);
1033
1034     /**
1035      * Helper function: Convert serial line baudrate (as number) to line speed (as defined in termios.h)
1036      * @param baudrate Serial line baudrate (as number)
1037      * @return Serial line speed, as defined in termios.h
1038      */
1039     speed_t dlt_convert_serial_speed(int baudrate);
1040
1041     /**
1042      * Print dlt version and dlt svn version to buffer
1043      * @param buf Pointer to buffer
1044      */
1045     void dlt_get_version(char *buf);
1046
1047 #endif
1048
1049     /* Function prototypes which should be used only internally */
1050     /*                                                          */
1051
1052     /**
1053      * Common part of initialisation
1054      * @return negative value if there was an error
1055      */
1056     int dlt_init_common(void);
1057
1058     /**
1059      * Return the uptime of the system in 0.1 ms resolution
1060      * @return 0 if there was an error
1061      */
1062     uint32_t dlt_uptime(void);
1063
1064     /**
1065      * Print header of a DLT message
1066      * @param message pointer to structure of organising access to DLT messages
1067      * @param text pointer to a ASCII string, in which the header is written
1068      * @param size maximal size of text buffer
1069      * @param verbose if set to true verbose information is printed out.
1070      * @return negative value if there was an error
1071      */
1072     int dlt_message_print_header(DltMessage *message, char *text, uint32_t size, int verbose);
1073
1074     /**
1075      * Print payload of a DLT message as Hex-Output
1076      * @param message pointer to structure of organising access to DLT messages
1077      * @param text pointer to a ASCII string, in which the output is written
1078      * @param size maximal size of text buffer
1079      * @param verbose if set to true verbose information is printed out.
1080      * @return negative value if there was an error
1081      */
1082     int dlt_message_print_hex(DltMessage *message, char *text, uint32_t size, int verbose);
1083
1084     /**
1085      * Print payload of a DLT message as ASCII-Output
1086      * @param message pointer to structure of organising access to DLT messages
1087      * @param text pointer to a ASCII string, in which the output is written
1088      * @param size maximal size of text buffer
1089      * @param verbose if set to true verbose information is printed out.
1090      * @return negative value if there was an error
1091      */
1092     int dlt_message_print_ascii(DltMessage *message, char *text, uint32_t size, int verbose);
1093
1094     /**
1095      * Print payload of a DLT message as Mixed-Ouput (Hex and ASCII), for plain text output
1096      * @param message pointer to structure of organising access to DLT messages
1097      * @param text pointer to a ASCII string, in which the output is written
1098      * @param size maximal size of text buffer
1099      * @param verbose if set to true verbose information is printed out.
1100      * @return negative value if there was an error
1101      */
1102     int dlt_message_print_mixed_plain(DltMessage *message, char *text, uint32_t size, int verbose);
1103
1104     /**
1105      * Print payload of a DLT message as Mixed-Ouput (Hex and ASCII), for HTML text output
1106      * @param message pointer to structure of organising access to DLT messages
1107      * @param text pointer to a ASCII string, in which the output is written
1108      * @param size maximal size of text buffer
1109      * @param verbose if set to true verbose information is printed out.
1110      * @return negative value if there was an error
1111      */
1112     int dlt_message_print_mixed_html(DltMessage *message, char *text, uint32_t size, int verbose);
1113
1114     /**
1115      * Decode and print a argument of a DLT message
1116      * @param msg pointer to structure of organising access to DLT messages
1117      * @param type_info Type of argument
1118      * @param ptr pointer to pointer to data (pointer to data is changed within this function)
1119      * @param datalength pointer to datalength (datalength is changed within this function)
1120      * @param text pointer to a ASCII string, in which the output is written
1121      * @param textlength maximal size of text buffer
1122      * @param byteLength If argument is a string, and this value is 0 or greater, this value will be taken as string length
1123      * @param verbose if set to true verbose information is printed out.
1124      * @return negative value if there was an error
1125      */
1126     int dlt_message_argument_print(DltMessage *msg,uint32_t type_info,uint8_t **ptr,int32_t *datalength,char *text,int textlength,int byteLength,int verbose);
1127
1128 #ifdef __cplusplus
1129 }
1130 #endif
1131
1132 /**
1133   \}
1134 */
1135
1136 #endif /* DLT_COMMON_H */