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