3 * Copyright (C) 2012 BMW AG
5 * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
7 * Contributions are licensed to the GENIVI Alliance under one or more
8 * Contribution License Agreements.
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/.
16 * \author Alexander Wenzel <alexander.aw.wenzel@bmw.de> BMW 2011-2012
19 * For further information see http://www.genivi.org/.
24 /*******************************************************************************
26 ** SRC-MODULE: dlt_common.c **
32 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
39 ** PLATFORM DEPENDANT [yes/no]: yes **
41 ** TO BE CHANGED BY USER [yes/no]: no **
43 *******************************************************************************/
45 /*******************************************************************************
47 ********************************************************************************
49 ** Initials Name Company **
50 ** -------- ------------------------- ---------------------------------- **
51 ** aw Alexander Wenzel BMW **
52 ** mk Markus Klein Fraunhofer ESK **
53 *******************************************************************************/
55 /*******************************************************************************
56 ** Revision Control History **
57 *******************************************************************************/
60 * $LastChangedRevision: 1670 $
61 * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
67 #include <stdlib.h> /* for malloc(), free() */
68 #include <string.h> /* for strlen(), memcmp(), memmove() */
69 #include <time.h> /* for localtime(), strftime() */
70 #include <limits.h> /* for NAME_MAX */
72 #include "dlt_common.h"
73 #include "dlt_common_cfg.h"
75 #include "dlt_version.h"
77 #if defined (__WIN32__) || defined (_MSC_VER)
78 #include <winsock2.h> /* for socket(), connect(), send(), and recv() */
80 #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
82 #include <time.h> /* for clock_gettime() */
85 #if defined (_MSC_VER)
88 #include <unistd.h> /* for read(), close() */
89 #include <sys/time.h> /* for gettimeofday() */
92 #if defined (__MSDOS__) || defined (_MSC_VER)
93 #pragma warning(disable : 4996) /* Switch off C4996 warnings */
98 static char str[DLT_COMMON_BUFFER_LENGTH];
100 const char dltSerialHeader[DLT_ID_SIZE] = { 'D','L','S',1 };
101 char dltSerialHeaderChar[DLT_ID_SIZE] = { 'D','L','S',1 };
103 /* internal logging parameters */
104 static int logging_mode = 0;
105 static int logging_level = 6;
106 static char logging_filename[NAME_MAX + 1] = "";
107 static FILE *logging_handle = 0;
109 char *message_type[] = {"log","app_trace","nw_trace","control","","","",""};
110 char *log_info[] = {"","fatal","error","warn","info","debug","verbose","","","","","","","","",""};
111 char *trace_type[] = {"","variable","func_in","func_out","state","vfb","","","","","","","","","",""};
112 char *nw_trace_type[] = {"","ipc","can","flexray","most","vfb","","","","","","","","","",""};
113 char *control_type[] = {"","request","response","time","","","","","","","","","","","",""};
114 static char *service_id[] = {"","set_log_level","set_trace_status","get_log_info","get_default_log_level","store_config","reset_to_factory_default",
115 "set_com_interface_status","set_com_interface_max_bandwidth","set_verbose_mode","set_message_filtering","set_timing_packets",
116 "get_local_time","use_ecu_id","use_session_id","use_timestamp","use_extended_header","set_default_log_level","set_default_trace_status",
117 "get_software_version","message_buffer_overflow"
119 static char *return_type[] = {"ok","not_supported","error","","","","","","no_matching_context_id"};
121 /* internal function definitions */
122 int dlt_buffer_get(DltBuffer *buf,unsigned char *data, int max_size,int delete);
123 int dlt_buffer_reset(DltBuffer *buf);
124 int dlt_buffer_increase_size(DltBuffer *buf);
125 int dlt_buffer_minimize_size(DltBuffer *buf);
126 void dlt_buffer_write_block(DltBuffer *buf,int *write, const unsigned char *data,unsigned int size);
127 void dlt_buffer_read_block(DltBuffer *buf,int *read,unsigned char *data,unsigned int size);
129 void dlt_print_hex(uint8_t *ptr,int size)
138 for (num=0;num<size;num++)
145 printf("%.2x",((uint8_t*)ptr)[num]);
149 int dlt_print_hex_string(char *text,int textlength,uint8_t *ptr,int size)
153 if ((ptr==0) || (text==0) || (textlength<=0) || (size<0))
158 /* Length 3: AB_ , A is first digit of hex number, B is second digit of hex number, _ is space */
159 if (textlength<(size*3))
161 dlt_log(LOG_ERR, "String does not fit binary data!\n");
165 for (num=0;num<size;num++)
173 sprintf(text,"%.2x",((uint8_t*)ptr)[num]);
174 text+=2; /* 2 chars */
180 int dlt_print_mixed_string(char *text,int textlength,uint8_t *ptr,int size,int html)
182 int required_size = 0;
185 if ((ptr==0) || (text==0) || (textlength<=0) || (size<0))
190 /* Check maximum required size and do a length check */
193 required_size=(DLT_COMMON_HEX_LINELEN+(2*DLT_COMMON_HEX_CHARS+(DLT_COMMON_HEX_CHARS-1))+DLT_COMMON_CHARLEN+DLT_COMMON_HEX_CHARS+DLT_COMMON_CHARLEN) *
194 ((size/DLT_COMMON_HEX_CHARS) + 1);
195 /* Example: (8 chars line number + (2*16 chars + 15 spaces) + space + 16 ascii chars + CR) *
196 ((size/16) lines + extra line for the rest) */
200 required_size=(DLT_COMMON_HEX_LINELEN+(2*DLT_COMMON_HEX_CHARS+(DLT_COMMON_HEX_CHARS-1))+DLT_COMMON_CHARLEN+DLT_COMMON_HEX_CHARS+4*DLT_COMMON_CHARLEN) *
201 ((size/DLT_COMMON_HEX_CHARS) + 1);
202 /* Example: (8 chars line number + (2*16 chars + 15 spaces) + space + 16 ascii chars + 4 [HTML CR: <BR>]) *
203 ((size/16) lines + extra line for the rest) */
206 if (textlength<required_size)
208 sprintf(str, "String does not fit mixed data (available=%d, required=%d) !\n", textlength, required_size);
209 dlt_log(LOG_ERR, str);
213 /* print full lines */
214 for (lines=0; lines< (size / DLT_COMMON_HEX_CHARS); lines++)
217 sprintf(text,"%.6x: ",lines * DLT_COMMON_HEX_CHARS);
218 text+=DLT_COMMON_HEX_LINELEN; /* 'XXXXXX: ' */
221 /* It is not required to decrement textlength, as it was already checked, that
222 there is enough space for the complete output */
223 dlt_print_hex_string(text,textlength,(uint8_t*)(ptr+(lines*DLT_COMMON_HEX_CHARS)),DLT_COMMON_HEX_CHARS);
224 text+=((2*DLT_COMMON_HEX_CHARS)+(DLT_COMMON_HEX_CHARS-1)); /* 32 characters + 15 spaces */
227 text+=DLT_COMMON_CHARLEN;
230 /* It is not required to decrement textlength, as it was already checked, that
231 there is enough space for the complete output */
232 dlt_print_char_string(&text,textlength,(uint8_t*)(ptr+(lines*DLT_COMMON_HEX_CHARS)),DLT_COMMON_HEX_CHARS);
237 text+=DLT_COMMON_CHARLEN;
241 sprintf(text,"<BR>");
242 text+=(4*DLT_COMMON_CHARLEN);
246 /* print partial line */
247 rest = size % DLT_COMMON_HEX_CHARS;
252 sprintf(text,"%.6x: ", (size / DLT_COMMON_HEX_CHARS) * DLT_COMMON_HEX_CHARS);
253 text+=DLT_COMMON_HEX_LINELEN; /* 'XXXXXX: ' */
256 /* It is not required to decrement textlength, as it was already checked, that
257 there is enough space for the complete output */
258 dlt_print_hex_string(text,textlength,(uint8_t*)(ptr+ ((size / DLT_COMMON_HEX_CHARS) * DLT_COMMON_HEX_CHARS)),rest);
259 text+=2*rest+(rest-1);
261 for (i=0;i<(DLT_COMMON_HEX_CHARS-rest);i++)
264 text+=(3*DLT_COMMON_CHARLEN);
268 text+=DLT_COMMON_CHARLEN;
271 /* It is not required to decrement textlength, as it was already checked, that
272 there is enough space for the complete output */
273 dlt_print_char_string(&text,textlength,(uint8_t*)(ptr+ ((size / DLT_COMMON_HEX_CHARS) * DLT_COMMON_HEX_CHARS)),rest);
279 int dlt_print_char_string(char **text,int textlength,uint8_t *ptr,int size)
288 if ((ptr==0) || (*text==0) || (textlength<=0) || (size<0))
293 if (textlength< size)
295 dlt_log(LOG_ERR, "String does not fit character data!\n");
299 for (num=0;num<size;num++)
301 if ( (((char*)ptr)[num]<DLT_COMMON_ASCII_CHAR_SPACE) || (((char*)ptr)[num]>DLT_COMMON_ASCII_CHAR_TILDE) )
307 /* replace < with . */
308 if (((char*)ptr)[num]!=DLT_COMMON_ASCII_CHAR_LT)
310 sprintf(*text,"%c",((char *)ptr)[num]);
323 void dlt_print_id(char *text,const char *id)
332 /* Initialize text */
333 for (i=0; i<DLT_ID_SIZE; i++)
338 text[DLT_ID_SIZE] = 0;
340 len = ((strlen(id)<=DLT_ID_SIZE)?strlen(id):DLT_ID_SIZE);
343 for (i=0; i<len; i++)
347 void dlt_set_id(char *id,const char *text)
396 void dlt_clean_string(char *text,int length)
405 for (num=0;num<length;num++)
407 if (text[num]=='\r' || text[num]=='\n')
414 int dlt_filter_init(DltFilter *filter,int verbose)
416 PRINT_FUNCTION_VERBOSE(verbose);
428 int dlt_filter_free(DltFilter *filter,int verbose)
430 PRINT_FUNCTION_VERBOSE(verbose);
440 int dlt_filter_load(DltFilter *filter,const char *filename,int verbose)
443 char str1[DLT_COMMON_BUFFER_LENGTH];
444 char apid[DLT_ID_SIZE],ctid[DLT_ID_SIZE];
446 PRINT_FUNCTION_VERBOSE(verbose);
453 handle = fopen(filename,"r");
456 sprintf(str,"Filter file %s cannot be opened!\n",filename);
457 dlt_log(LOG_ERR, str);
464 while (!feof(handle))
467 if (fscanf(handle,"%s",str1)!=1)
476 if (strcmp(str1,"----")==0)
482 dlt_set_id(apid,str1);
486 if (fscanf(handle,"%s",str1)!=1)
494 printf(" %s\r\n",str1);
495 if (strcmp(str1,"----")==0)
501 dlt_set_id(ctid,str1);
504 if (filter->counter<DLT_FILTER_MAX)
506 dlt_filter_add(filter,apid,ctid,verbose);
510 sprintf(str, "Maximum number (%d) of allowed filters reached!\n", DLT_FILTER_MAX);
511 dlt_log(LOG_ERR, str);
521 int dlt_filter_save(DltFilter *filter,const char *filename,int verbose)
525 char buf[DLT_COMMON_BUFFER_LENGTH];
527 PRINT_FUNCTION_VERBOSE(verbose);
534 handle = fopen(filename,"w");
537 sprintf(str,"Filter file %s cannot be opened!\n",filename);
538 dlt_log(LOG_ERR, str);
542 for (num=0;num<filter->counter;num++)
544 if (filter->apid[num][0]==0)
546 fprintf(handle,"---- ");
550 dlt_print_id(buf,filter->apid[num]);
551 fprintf(handle,"%s ",buf);
553 if (filter->ctid[num][0]==0)
555 fprintf(handle,"---- ");
559 dlt_print_id(buf,filter->ctid[num]);
560 fprintf(handle,"%s ",buf);
569 int dlt_filter_find(DltFilter *filter,const char *apid,const char *ctid, int verbose)
573 PRINT_FUNCTION_VERBOSE(verbose);
575 if ((filter==0) || (apid==0))
580 for (num=0; num<filter->counter; num++)
582 if (memcmp(filter->apid[num],apid,DLT_ID_SIZE)==0)
584 /* apid matches, now check for ctid */
587 /* check if empty ctid matches */
588 //if (memcmp(filter->ctid[num],"",DLT_ID_SIZE)==0)//coverity complains here about Out-of-bounds access.
589 char empty_ctid[DLT_ID_SIZE]="";
590 if (memcmp(filter->ctid[num],empty_ctid,DLT_ID_SIZE)==0)
597 if (memcmp(filter->ctid[num],ctid,DLT_ID_SIZE)==0)
605 return -1; /* Not found */
608 int dlt_filter_add(DltFilter *filter,const char *apid,const char *ctid, int verbose)
610 PRINT_FUNCTION_VERBOSE(verbose);
612 if ((filter==0) || (apid==0))
617 if (filter->counter >= DLT_FILTER_MAX)
619 dlt_log(LOG_ERR, "Maximum numbers of allowed filters reached!\n");
623 /* add each filter (apid, ctid) only once to filter array */
624 if (dlt_filter_find(filter,apid,ctid,verbose)<0)
626 /* filter not found, so add it to filter array */
627 if (filter->counter<DLT_FILTER_MAX)
629 dlt_set_id(filter->apid[filter->counter],apid);
630 dlt_set_id(filter->ctid[filter->counter],(ctid?ctid:""));
641 int dlt_filter_delete(DltFilter *filter,const char *apid,const char *ctid, int verbose)
646 PRINT_FUNCTION_VERBOSE(verbose);
648 if ((filter==0) || (apid==0))
653 if (filter->counter>0)
655 /* Get first occurence of apid and ctid in filter array */
656 for (j=0; j<filter->counter; j++)
658 if ((memcmp(filter->apid[j],apid,DLT_ID_SIZE)==0) &&
659 (memcmp(filter->ctid[j],ctid,DLT_ID_SIZE)==0)
671 /* Copy from j+1 til end to j til end-1 */
673 dlt_set_id(filter->apid[j],"");
674 dlt_set_id(filter->ctid[j],"");
676 for (k=j; k<(filter->counter-1); k++)
678 dlt_set_id(filter->apid[k],filter->apid[k+1]);
679 dlt_set_id(filter->ctid[k],filter->ctid[k+1]);
690 int dlt_message_init(DltMessage *msg,int verbose)
692 PRINT_FUNCTION_VERBOSE(verbose);
699 /* initalise structure parameters */
704 msg->databuffersize = 0;
706 msg->storageheader = 0;
707 msg->standardheader = 0;
708 msg->extendedheader = 0;
710 msg->found_serialheader = 0;
715 int dlt_message_free(DltMessage *msg,int verbose)
717 PRINT_FUNCTION_VERBOSE(verbose);
723 /* delete databuffer if exists */
726 free(msg->databuffer);
728 msg->databuffersize = 0;
734 int dlt_message_header(DltMessage *msg,char *text,int textlength,int verbose)
736 return dlt_message_header_flags(msg,text,textlength,DLT_HEADER_SHOW_ALL,verbose);
739 int dlt_message_header_flags(DltMessage *msg,char *text,int textlength,int flags, int verbose)
741 struct tm * timeinfo;
742 char buffer [DLT_COMMON_BUFFER_LENGTH];
744 PRINT_FUNCTION_VERBOSE(verbose);
746 if ((msg==0) || (text==0) || (textlength<=0))
751 if ((flags<DLT_HEADER_SHOW_NONE) || (flags>DLT_HEADER_SHOW_ALL))
758 if ((flags & DLT_HEADER_SHOW_TIME) == DLT_HEADER_SHOW_TIME)
760 /* print received time */
761 timeinfo = localtime ((const time_t*)(&(msg->storageheader->seconds)));
765 strftime (buffer,sizeof(buffer),"%Y/%m/%d %H:%M:%S",timeinfo);
766 sprintf(text,"%s.%.6d ",buffer,msg->storageheader->microseconds);
770 if ((flags & DLT_HEADER_SHOW_TMSTP) == DLT_HEADER_SHOW_TMSTP)
772 /* print timestamp if available */
773 if ( DLT_IS_HTYP_WTMS(msg->standardheader->htyp) )
775 sprintf(text+strlen(text),"%10u ",msg->headerextra.tmsp);
779 sprintf(text+strlen(text),"---------- ");
783 if ((flags & DLT_HEADER_SHOW_MSGCNT) == DLT_HEADER_SHOW_MSGCNT)
785 /* print message counter */
786 sprintf(text+strlen(text),"%.3d ",msg->standardheader->mcnt);
789 if ((flags & DLT_HEADER_SHOW_ECUID) == DLT_HEADER_SHOW_ECUID)
791 /* print ecu id, use header extra if available, else storage header value */
792 if ( DLT_IS_HTYP_WEID(msg->standardheader->htyp) )
794 dlt_print_id(text+strlen(text),msg->headerextra.ecu);
798 dlt_print_id(text+strlen(text),msg->storageheader->ecu);
802 /* print app id and context id if extended header available, else '----' */#
803 if ((flags & DLT_HEADER_SHOW_APID) == DLT_HEADER_SHOW_APID)
805 sprintf(text+strlen(text)," ");
806 if ((DLT_IS_HTYP_UEH(msg->standardheader->htyp)) && (msg->extendedheader->apid[0]!=0))
808 dlt_print_id(text+strlen(text),msg->extendedheader->apid);
812 sprintf(text+strlen(text),"----");
815 sprintf(text+strlen(text)," ");
818 if ((flags & DLT_HEADER_SHOW_CTID) == DLT_HEADER_SHOW_CTID)
820 if ((DLT_IS_HTYP_UEH(msg->standardheader->htyp)) && (msg->extendedheader->ctid[0]!=0))
822 dlt_print_id(text+strlen(text),msg->extendedheader->ctid);
826 sprintf(text+strlen(text),"----");
829 sprintf(text+strlen(text)," ");
832 /* print info about message type and length */
833 if (DLT_IS_HTYP_UEH(msg->standardheader->htyp))
835 if ((flags & DLT_HEADER_SHOW_MSGTYPE) == DLT_HEADER_SHOW_MSGTYPE)
837 sprintf(text+strlen(text),"%s",message_type[DLT_GET_MSIN_MSTP(msg->extendedheader->msin)]);
838 sprintf(text+strlen(text)," ");
841 if ((flags & DLT_HEADER_SHOW_MSGSUBTYPE) == DLT_HEADER_SHOW_MSGSUBTYPE)
843 if ((DLT_GET_MSIN_MSTP(msg->extendedheader->msin))==DLT_TYPE_LOG)
845 sprintf(text+strlen(text),"%s",log_info[DLT_GET_MSIN_MTIN(msg->extendedheader->msin)]);
848 if ((DLT_GET_MSIN_MSTP(msg->extendedheader->msin))==DLT_TYPE_APP_TRACE)
850 sprintf(text+strlen(text),"%s",trace_type[DLT_GET_MSIN_MTIN(msg->extendedheader->msin)]);
853 if ((DLT_GET_MSIN_MSTP(msg->extendedheader->msin))==DLT_TYPE_NW_TRACE)
855 sprintf(text+strlen(text),"%s",nw_trace_type[DLT_GET_MSIN_MTIN(msg->extendedheader->msin)]);
858 if ((DLT_GET_MSIN_MSTP(msg->extendedheader->msin))==DLT_TYPE_CONTROL)
860 sprintf(text+strlen(text),"%s",control_type[DLT_GET_MSIN_MTIN(msg->extendedheader->msin)]);
863 sprintf(text+strlen(text)," ");
866 if ((flags & DLT_HEADER_SHOW_VNVSTATUS) == DLT_HEADER_SHOW_VNVSTATUS)
868 /* print verbose status pf message */
869 if (DLT_IS_MSIN_VERB(msg->extendedheader->msin))
871 sprintf(text+strlen(text),"V");
875 sprintf(text+strlen(text),"N");
878 sprintf(text+strlen(text)," ");
881 if ((flags & DLT_HEADER_SHOW_NOARG) == DLT_HEADER_SHOW_NOARG)
883 /* print number of arguments */
884 sprintf(text+strlen(text),"%d", msg->extendedheader->noar);
890 if ((flags & DLT_HEADER_SHOW_MSGTYPE) == DLT_HEADER_SHOW_MSGTYPE)
892 sprintf(text+strlen(text),"--- ");
895 if ((flags & DLT_HEADER_SHOW_MSGSUBTYPE) == DLT_HEADER_SHOW_MSGSUBTYPE)
897 sprintf(text+strlen(text),"--- ");
900 if ((flags & DLT_HEADER_SHOW_VNVSTATUS) == DLT_HEADER_SHOW_VNVSTATUS)
902 sprintf(text+strlen(text),"N ");
905 if ((flags & DLT_HEADER_SHOW_NOARG) == DLT_HEADER_SHOW_NOARG)
907 sprintf(text+strlen(text),"-");
914 int dlt_message_payload(DltMessage *msg,char *text,int textlength,int type,int verbose)
916 uint32_t id=0,id_tmp=0;
922 /* Pointer to ptr and datalength */
924 int32_t *pdatalength;
929 uint32_t type_info=0,type_info_tmp=0;
931 PRINT_FUNCTION_VERBOSE(verbose);
933 if ((msg==0) || (text==0))
940 dlt_log(LOG_ERR, "String does not fit binary data!\n");
944 /* start with empty string */
947 /* print payload only as hex */
948 if (type==DLT_OUTPUT_HEX)
950 return dlt_print_hex_string(text,textlength,msg->databuffer,msg->datasize);
953 /* print payload as mixed */
954 if (type==DLT_OUTPUT_MIXED_FOR_PLAIN)
956 return dlt_print_mixed_string(text,textlength,msg->databuffer,msg->datasize,0);
959 if (type==DLT_OUTPUT_MIXED_FOR_HTML)
961 return dlt_print_mixed_string(text,textlength,msg->databuffer,msg->datasize,1);
964 ptr = msg->databuffer;
965 datalength = msg->datasize;
967 /* Pointer to ptr and datalength */
969 pdatalength = &datalength;
971 /* non-verbose mode */
973 /* print payload as hex */
974 if (DLT_MSG_IS_NONVERBOSE(msg))
977 DLT_MSG_READ_VALUE(id_tmp,ptr,datalength,uint32_t);
978 id=DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp);
980 if (textlength<((datalength*3)+20))
982 dlt_log(LOG_ERR, "String does not fit binary data!\n");
986 /* process message id / service id */
987 if (DLT_MSG_IS_CONTROL(msg))
989 if (id > 0 && id <= DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW)
991 sprintf(text+strlen(text),"%s",service_id[id]); /* service id */
995 if (!(DLT_MSG_IS_CONTROL_TIME(msg)))
997 sprintf(text+strlen(text),"service(%u)",id); /* service id */
1003 sprintf(text+strlen(text),", ");
1008 sprintf(text+strlen(text),"%u, ",id); /* message id */
1011 /* process return value */
1012 if (DLT_MSG_IS_CONTROL_RESPONSE(msg))
1016 DLT_MSG_READ_VALUE(retval,ptr,datalength,uint8_t); /* No endian conversion necessary */
1017 if ( (retval<3) || (retval==8))
1019 sprintf(text+strlen(text),"%s",return_type[retval]);
1023 sprintf(text+strlen(text),"%.2x",retval);
1028 sprintf(text+strlen(text),", ");
1033 if (type==DLT_OUTPUT_ASCII_LIMITED)
1035 ret=dlt_print_hex_string(text+strlen(text),textlength-strlen(text),ptr,
1036 (datalength>DLT_COMMON_ASCII_LIMIT_MAX_CHARS?DLT_COMMON_ASCII_LIMIT_MAX_CHARS:datalength));
1037 if ((datalength>DLT_COMMON_ASCII_LIMIT_MAX_CHARS) &&
1038 ((textlength-strlen(text))>4))
1040 sprintf(text+strlen(text)," ...");
1045 ret=dlt_print_hex_string(text+strlen(text),textlength-strlen(text),ptr,datalength);
1051 /* At this point, it is ensured that a extended header is available */
1057 for (num=0;num<(int)(msg->extendedheader->noar);num++)
1061 sprintf(text+strlen(text)," ");
1064 /* first read the type info of the argument */
1065 DLT_MSG_READ_VALUE(type_info_tmp,ptr,datalength,uint32_t);
1066 type_info=DLT_ENDIAN_GET_32(msg->standardheader->htyp, type_info_tmp);
1068 /* print out argument */
1069 if (dlt_message_argument_print(msg, type_info, pptr, pdatalength, text, textlength, -1, 0)==-1)
1078 int dlt_message_filter_check(DltMessage *msg,DltFilter *filter,int verbose)
1080 /* check the filters if message is used */
1084 PRINT_FUNCTION_VERBOSE(verbose);
1086 if ((msg==0) || (filter==0))
1091 if ((filter->counter==0) || (!(DLT_IS_HTYP_UEH(msg->standardheader->htyp))))
1093 /* no filter is set, or no extended header is available, so do as filter is matching */
1097 for (num=0;num<filter->counter;num++)
1099 /* check each filter if it matches */
1100 if ((DLT_IS_HTYP_UEH(msg->standardheader->htyp)) &&
1101 (filter->apid[num][0]==0 || memcmp(filter->apid[num],msg->extendedheader->apid,DLT_ID_SIZE)==0) &&
1102 (filter->ctid[num][0]==0 || memcmp(filter->ctid[num],msg->extendedheader->ctid,DLT_ID_SIZE)==0) )
1112 int dlt_message_read(DltMessage *msg,uint8_t *buffer,unsigned int length,int resync, int verbose)
1116 PRINT_FUNCTION_VERBOSE(verbose);
1118 if ((msg==0) || (buffer==0) || (length<=0))
1123 /* initialize resync_offset */
1124 msg->resync_offset=0;
1126 /* check if message contains serial header, smaller than standard header */
1127 if (length<sizeof(dltSerialHeader))
1129 /* dlt_log(LOG_ERR, "Length smaller than serial header!\n"); */
1133 if (memcmp(buffer,dltSerialHeader,sizeof(dltSerialHeader)) == 0)
1135 /* serial header found */
1136 msg->found_serialheader = 1;
1137 buffer += sizeof(dltSerialHeader);
1138 length -= sizeof(dltSerialHeader);
1142 /* serial header not found */
1143 msg->found_serialheader = 0;
1146 /* resync if necessary */
1147 msg->resync_offset=0;
1151 if (memcmp(buffer+msg->resync_offset,dltSerialHeader,sizeof(dltSerialHeader)) == 0)
1153 /* serial header found */
1154 msg->found_serialheader = 1;
1155 buffer += sizeof(dltSerialHeader);
1156 length -= sizeof(dltSerialHeader);
1160 msg->resync_offset++;
1162 while ((sizeof(dltSerialHeader)+msg->resync_offset)<=length);
1164 /* Set new start offset */
1165 if (msg->resync_offset>0)
1167 /* Resyncing connection */
1168 buffer+=msg->resync_offset;
1169 length-=msg->resync_offset;
1174 /* check that standard header fits buffer */
1175 if (length<sizeof(DltStandardHeader))
1177 /* dlt_log(LOG_ERR, "Length smaller than standard header!\n"); */
1180 memcpy(msg->headerbuffer+sizeof(DltStorageHeader),buffer,sizeof(DltStandardHeader));
1182 /* set ptrs to structures */
1183 msg->storageheader = (DltStorageHeader*) msg->headerbuffer;
1184 msg->standardheader = (DltStandardHeader*) (msg->headerbuffer + sizeof(DltStorageHeader));
1186 /* calculate complete size of headers */
1187 extra_size = DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp)+(DLT_IS_HTYP_UEH(msg->standardheader->htyp) ? sizeof(DltExtendedHeader) : 0);
1188 msg->headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + extra_size;
1189 msg->datasize = DLT_BETOH_16(msg->standardheader->len) - (msg->headersize - sizeof(DltStorageHeader));
1193 sprintf(str,"Buffer length: %d\n",length);
1194 dlt_log(LOG_INFO, str);
1198 sprintf(str,"Header Size: %d\n",msg->headersize);
1199 dlt_log(LOG_INFO, str);
1203 sprintf(str,"Data Size: %d\n",msg->datasize);
1204 dlt_log(LOG_INFO, str);
1207 /* check data size */
1208 if (msg->datasize < 0)
1210 sprintf(str,"Plausibility check failed. Complete message size too short (%d)!\n",msg->datasize);
1211 dlt_log(LOG_ERR, str);
1215 /* load standard header extra parameters and Extended header if used */
1218 if (length < (msg->headersize - sizeof(DltStorageHeader)))
1223 memcpy(msg->headerbuffer+sizeof(DltStorageHeader)+sizeof(DltStandardHeader),buffer+sizeof(DltStandardHeader),extra_size);
1225 /* set extended header ptr and get standard header extra parameters */
1226 if (DLT_IS_HTYP_UEH(msg->standardheader->htyp))
1228 msg->extendedheader = (DltExtendedHeader*) (msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
1229 DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp));
1233 msg->extendedheader = 0;
1236 dlt_message_get_extraparameters(msg,verbose);
1239 /* check if payload fits length */
1240 if (length < (msg->headersize - sizeof(DltStorageHeader) + msg->datasize))
1242 /* dlt_log(LOG_ERR,"length does not fit!\n"); */
1246 /* free last used memory for buffer */
1247 if (msg->databuffer)
1249 if (msg->datasize>msg->databuffersize){
1250 free(msg->databuffer);
1251 msg->databuffer=(uint8_t *)malloc(msg->datasize);
1252 msg->databuffersize = msg->datasize;
1255 /* get new memory for buffer */
1256 msg->databuffer = (uint8_t *)malloc(msg->datasize);
1257 msg->databuffersize = msg->datasize;
1259 if (msg->databuffer == 0)
1261 sprintf(str,"Cannot allocate memory for payload buffer of size %d!\n",msg->datasize);
1262 dlt_log(LOG_ERR, str);
1266 /* load payload data from buffer */
1267 memcpy(msg->databuffer,buffer+(msg->headersize-sizeof(DltStorageHeader)),msg->datasize);
1272 int dlt_message_get_extraparameters(DltMessage *msg,int verbose)
1274 PRINT_FUNCTION_VERBOSE(verbose);
1281 if (DLT_IS_HTYP_WEID(msg->standardheader->htyp))
1283 memcpy(msg->headerextra.ecu,msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader),DLT_ID_SIZE);
1286 if (DLT_IS_HTYP_WSID(msg->standardheader->htyp))
1288 memcpy(&(msg->headerextra.seid),msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
1289 + (DLT_IS_HTYP_WEID(msg->standardheader->htyp) ? DLT_SIZE_WEID : 0), DLT_SIZE_WSID);
1290 msg->headerextra.seid = DLT_BETOH_32(msg->headerextra.seid);
1293 if (DLT_IS_HTYP_WTMS(msg->standardheader->htyp))
1295 memcpy(&(msg->headerextra.tmsp),msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
1296 + (DLT_IS_HTYP_WEID(msg->standardheader->htyp) ? DLT_SIZE_WEID : 0)
1297 + (DLT_IS_HTYP_WSID(msg->standardheader->htyp) ? DLT_SIZE_WSID : 0),DLT_SIZE_WTMS);
1298 msg->headerextra.tmsp = DLT_BETOH_32(msg->headerextra.tmsp);
1304 int dlt_message_set_extraparameters(DltMessage *msg,int verbose)
1306 PRINT_FUNCTION_VERBOSE(verbose);
1313 if (DLT_IS_HTYP_WEID(msg->standardheader->htyp))
1315 memcpy(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader),msg->headerextra.ecu,DLT_ID_SIZE);
1318 if (DLT_IS_HTYP_WSID(msg->standardheader->htyp))
1320 msg->headerextra.seid = DLT_HTOBE_32(msg->headerextra.seid);
1321 memcpy(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
1322 + (DLT_IS_HTYP_WEID(msg->standardheader->htyp) ? DLT_SIZE_WEID : 0), &(msg->headerextra.seid),DLT_SIZE_WSID);
1325 if (DLT_IS_HTYP_WTMS(msg->standardheader->htyp))
1327 msg->headerextra.tmsp = DLT_HTOBE_32(msg->headerextra.tmsp);
1328 memcpy(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
1329 + (DLT_IS_HTYP_WEID(msg->standardheader->htyp) ? DLT_SIZE_WEID : 0)
1330 + (DLT_IS_HTYP_WSID(msg->standardheader->htyp) ? DLT_SIZE_WSID : 0), &(msg->headerextra.tmsp),DLT_SIZE_WTMS);
1336 int dlt_file_init(DltFile *file,int verbose)
1338 PRINT_FUNCTION_VERBOSE(verbose);
1345 /* initalise structure parameters */
1348 file->counter_total = 0;
1352 file->filter_counter = 0;
1353 file->file_position = 0;
1357 file->error_messages = 0;
1359 return dlt_message_init(&(file->msg),verbose);
1362 int dlt_file_set_filter(DltFile *file,DltFilter *filter,int verbose)
1364 PRINT_FUNCTION_VERBOSE(verbose);
1372 file->filter = filter;
1377 int dlt_file_read_header(DltFile *file,int verbose)
1379 PRINT_FUNCTION_VERBOSE(verbose);
1386 /* load header from file */
1387 if (fread(file->msg.headerbuffer,sizeof(DltStorageHeader)+sizeof(DltStandardHeader),1,file->handle)!=1)
1389 if (!feof(file->handle))
1391 dlt_log(LOG_ERR, "Cannot read header from file!\n");
1396 /* set ptrs to structures */
1397 file->msg.storageheader = (DltStorageHeader*) file->msg.headerbuffer;
1398 file->msg.standardheader = (DltStandardHeader*) (file->msg.headerbuffer + sizeof(DltStorageHeader));
1400 /* check id of storage header */
1401 if (dlt_check_storageheader(file->msg.storageheader)==0)
1403 dlt_log(LOG_ERR, "DLT storage header pattern not found!\n");
1407 /* calculate complete size of headers */
1408 file->msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
1409 DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp) + (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp) ? sizeof(DltExtendedHeader) : 0);
1410 file->msg.datasize = DLT_BETOH_16(file->msg.standardheader->len) + sizeof(DltStorageHeader) - file->msg.headersize;
1413 sprintf(str,"Header Size: %d\n",file->msg.headersize);
1414 dlt_log(LOG_INFO, str);
1418 sprintf(str,"Data Size: %d\n",file->msg.datasize);
1419 dlt_log(LOG_INFO, str);
1422 /* check data size */
1423 if (file->msg.datasize < 0)
1425 sprintf(str,"Plausibility check failed. Complete message size too short! (%d)\n", file->msg.datasize);
1426 dlt_log(LOG_ERR, str);
1433 int dlt_file_read_header_raw(DltFile *file,int resync,int verbose)
1435 char dltSerialHeaderBuffer[DLT_ID_SIZE];
1437 PRINT_FUNCTION_VERBOSE(verbose);
1444 /* check if serial header exists, ignore if found */
1445 if (fread(dltSerialHeaderBuffer,sizeof(dltSerialHeaderBuffer),1,file->handle)!=1)
1447 /* cannot read serial header, not enough data available in file */
1448 if (!feof(file->handle))
1450 dlt_log(LOG_ERR, "Cannot read header from file!\n");
1454 if (memcmp(dltSerialHeaderBuffer,dltSerialHeader,sizeof(dltSerialHeader)) == 0)
1456 /* serial header found */
1457 /* nothing to do continue reading */
1462 /* serial header not found */
1465 /* increase error counter */
1466 file->error_messages++;
1468 /* resync to serial header */
1471 memmove(dltSerialHeaderBuffer,dltSerialHeaderBuffer+1,sizeof(dltSerialHeader)-1);
1472 if (fread(dltSerialHeaderBuffer+3,1,1,file->handle)!=1)
1474 /* cannot read any data, perhaps end of file reached */
1477 if (memcmp(dltSerialHeaderBuffer,dltSerialHeader,sizeof(dltSerialHeader)) == 0)
1479 /* serial header synchronised */
1486 /* go back to last file position */
1487 if (0 != fseek(file->handle,file->file_position,SEEK_SET))
1492 /* load header from file */
1493 if (fread(file->msg.headerbuffer+sizeof(DltStorageHeader),sizeof(DltStandardHeader),1,file->handle)!=1)
1495 if (!feof(file->handle))
1497 dlt_log(LOG_ERR, "Cannot read header from file!\n");
1502 /* set ptrs to structures */
1503 file->msg.storageheader = (DltStorageHeader*) file->msg.headerbuffer; // this points now to a empty storage header (filled with '0')
1504 file->msg.standardheader = (DltStandardHeader*) (file->msg.headerbuffer + sizeof(DltStorageHeader));
1506 /* Skip storage header field, fill this field with '0' */
1507 memset(file->msg.storageheader,0,sizeof(DltStorageHeader));
1509 /* Set storage header */
1510 dlt_set_storageheader(file->msg.storageheader,DLT_COMMON_DUMMY_ECUID);
1512 /* no check for storage header id*/
1514 /* calculate complete size of headers */
1515 file->msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
1516 DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp) + (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp) ? sizeof(DltExtendedHeader) : 0);
1517 file->msg.datasize = DLT_BETOH_16(file->msg.standardheader->len) + sizeof(DltStorageHeader) - file->msg.headersize;
1520 sprintf(str,"Header Size: %d\n",file->msg.headersize);
1521 dlt_log(LOG_INFO, str);
1525 sprintf(str,"Data Size: %d\n",file->msg.datasize);
1526 dlt_log(LOG_INFO, str);
1529 /* check data size */
1530 if (file->msg.datasize < 0)
1532 sprintf(str,"Plausibility check failed. Complete message size too short! (%d)\n", file->msg.datasize);
1533 dlt_log(LOG_ERR, str);
1540 int dlt_file_read_header_extended(DltFile *file, int verbose)
1542 PRINT_FUNCTION_VERBOSE(verbose);
1549 /* load standard header extra parameters if used */
1550 if (DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp))
1552 if (fread(file->msg.headerbuffer+sizeof(DltStorageHeader)+sizeof(DltStandardHeader),
1553 DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp),
1556 dlt_log(LOG_ERR, "Cannot read standard header extra parameters from file!\n");
1560 dlt_message_get_extraparameters(&(file->msg),verbose);
1563 /* load Extended header if used */
1564 if (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp)==0)
1566 /* there is nothing to be loaded */
1570 if (fread(file->msg.headerbuffer+sizeof(DltStorageHeader)+sizeof(DltStandardHeader)+DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp),
1571 (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp) ? sizeof(DltExtendedHeader) : 0),
1574 dlt_log(LOG_ERR, "Cannot read extended header from file!\n");
1578 /* set extended header ptr */
1579 if (DLT_IS_HTYP_UEH(file->msg.standardheader->htyp))
1581 file->msg.extendedheader = (DltExtendedHeader*) (file->msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
1582 DLT_STANDARD_HEADER_EXTRA_SIZE(file->msg.standardheader->htyp));
1586 file->msg.extendedheader = 0;
1592 int dlt_file_read_data(DltFile *file, int verbose)
1594 PRINT_FUNCTION_VERBOSE(verbose);
1601 /* free last used memory for buffer */
1602 if (file->msg.databuffer && (file->msg.databuffersize < file->msg.datasize))
1604 free(file->msg.databuffer);
1605 file->msg.databuffer=0;
1608 if (file->msg.databuffer == 0){
1609 /* get new memory for buffer */
1610 file->msg.databuffer = (uint8_t *)malloc(file->msg.datasize);
1611 file->msg.databuffersize = file->msg.datasize;
1614 if (file->msg.databuffer == 0)
1616 sprintf(str,"Cannot allocate memory for payload buffer of size %d!\n",file->msg.datasize);
1617 dlt_log(LOG_ERR, str);
1621 /* load payload data from file */
1622 if (fread(file->msg.databuffer,file->msg.datasize,1,file->handle)!=1)
1624 if (file->msg.datasize!=0)
1626 sprintf(str,"Cannot read payload data from file of size %d!\n",file->msg.datasize);
1627 dlt_log(LOG_ERR, str);
1635 int dlt_file_open(DltFile *file,const char *filename,int verbose)
1637 PRINT_FUNCTION_VERBOSE(verbose);
1644 /* reset counters */
1646 file->counter_total = 0;
1648 file->file_position = 0;
1649 file->file_length = 0;
1650 file->error_messages = 0;
1654 fclose(file->handle);
1658 file->handle = fopen(filename,"rb");
1659 if (file->handle == 0)
1661 snprintf(str, DLT_COMMON_BUFFER_LENGTH - 1 ,"File %s cannot be opened!\n",filename);
1663 dlt_log(LOG_ERR, str);
1667 if (0 != fseek(file->handle,0,SEEK_END))
1669 sprintf(str,"dlt_file_open: Seek failed to 0,SEEK_END");
1670 dlt_log(LOG_ERR, str);
1673 file->file_length = ftell(file->handle);
1675 if (0 != fseek(file->handle,0,SEEK_SET))
1677 sprintf(str,"dlt_file_open: Seek failed to 0,SEEK_SET");
1678 dlt_log(LOG_ERR, str);
1684 /* print file length */
1685 sprintf(str,"File is %lu bytes long\n",file->file_length);
1686 dlt_log(LOG_INFO, str);
1691 int dlt_file_read(DltFile *file,int verbose)
1698 sprintf(str,"%s: Message %d:\n",__func__, file->counter_total);
1699 dlt_log(LOG_INFO, str);
1707 /* allocate new memory for index if number of messages exceeds a multiple of DLT_COMMON_INDEX_ALLOC (e.g.: 1000) */
1708 if (file->counter % DLT_COMMON_INDEX_ALLOC == 0)
1710 ptr = (long *) malloc(((file->counter/DLT_COMMON_INDEX_ALLOC) + 1) * DLT_COMMON_INDEX_ALLOC * sizeof(long));
1719 memcpy(ptr,file->index,file->counter * sizeof(long));
1725 /* set to end of last succesful read message, because of conflicting calls to dlt_file_read and dlt_file_message */
1726 if (0 != fseek(file->handle,file->file_position,SEEK_SET))
1728 sprintf(str,"Seek failed to file_position %ld \n",file->file_position);
1729 dlt_log(LOG_ERR, str);
1733 /* get file position at start of DLT message */
1736 sprintf(str,"Position in file: %ld\n",file->file_position);
1737 dlt_log(LOG_INFO, str);
1741 if (dlt_file_read_header(file,verbose)<0)
1743 /* go back to last position in file */
1744 fseek(file->handle,file->file_position,SEEK_SET);
1750 /* read the extended header if filter is enabled and extended header exists */
1751 if (dlt_file_read_header_extended(file, verbose)<0)
1753 /* go back to last position in file */
1754 if (0 != fseek(file->handle,file->file_position,SEEK_SET))
1756 sprintf(str,"Seek to last file pos failed!\n");
1757 dlt_log(LOG_ERR, str);
1762 /* check the filters if message is used */
1763 if (dlt_message_filter_check(&(file->msg),file->filter,verbose) == 1)
1765 /* filter matched, consequently store current message */
1766 /* store index pointer to message position in DLT file */
1767 file->index[file->counter] = file->file_position;
1769 file->position = file->counter - 1;
1774 /* skip payload data */
1775 if (fseek(file->handle,file->msg.datasize,SEEK_CUR)!=0)
1777 /* go back to last position in file */
1778 sprintf(str,"Seek failed to skip payload data of size %d!\n",file->msg.datasize);
1779 dlt_log(LOG_ERR, str);
1781 if (0 != fseek(file->handle,file->file_position,SEEK_SET))
1783 sprintf(str,"Seek back also failed!\n");
1784 dlt_log(LOG_ERR, str);
1792 /* filter is disabled */
1793 /* skip additional header parameters and payload data */
1794 if (fseek(file->handle,file->msg.headersize - sizeof(DltStorageHeader) - sizeof(DltStandardHeader) + file->msg.datasize,SEEK_CUR))
1797 sprintf(str,"Seek failed to skip extra header and payload data from file of size %d!\n",
1798 file->msg.headersize - sizeof(DltStorageHeader) - sizeof(DltStandardHeader) + file->msg.datasize);
1799 dlt_log(LOG_ERR, str);
1800 /* go back to last position in file */
1801 if (fseek(file->handle,file->file_position,SEEK_SET))
1803 sprintf(str,"Seek back also failed!\n");
1804 dlt_log(LOG_ERR, str);
1809 /* store index pointer to message position in DLT file */
1810 file->index[file->counter] = file->file_position;
1812 file->position = file->counter - 1;
1817 /* increase total message counter */
1818 file->counter_total++;
1820 /* store position to next message */
1821 file->file_position = ftell(file->handle);
1826 int dlt_file_read_raw(DltFile *file,int resync, int verbose)
1833 sprintf(str,"%s: Message %d:\n",__func__, file->counter_total);
1834 dlt_log(LOG_INFO, str);
1840 /* allocate new memory for index if number of messages exceeds a multiple of DLT_COMMON_INDEX_ALLOC (e.g.: 1000) */
1841 if (file->counter % DLT_COMMON_INDEX_ALLOC == 0)
1843 ptr = (long *) malloc(((file->counter/DLT_COMMON_INDEX_ALLOC) + 1) * DLT_COMMON_INDEX_ALLOC * sizeof(long));
1852 memcpy(ptr,file->index,file->counter * sizeof(long));
1858 /* set to end of last successful read message, because of conflicting calls to dlt_file_read and dlt_file_message */
1859 if (0 != fseek(file->handle,file->file_position,SEEK_SET))
1862 /* get file position at start of DLT message */
1865 sprintf(str,"Position in file: %ld\n",file->file_position);
1866 dlt_log(LOG_INFO, str);
1870 if (dlt_file_read_header_raw(file,resync,verbose)<0)
1872 /* go back to last position in file */
1873 if (0!= fseek(file->handle,file->file_position,SEEK_SET))
1875 sprintf(str,"dlt_file_read_raw, fseek failed 1\n");
1876 dlt_log(LOG_ERR, str);
1881 /* read the extended header if filter is enabled and extended header exists */
1882 if (dlt_file_read_header_extended(file, verbose)<0)
1884 /* go back to last position in file */
1885 if (0 != fseek(file->handle,file->file_position,SEEK_SET))
1887 sprintf(str,"dlt_file_read_raw, fseek failed 2\n");
1888 dlt_log(LOG_ERR, str);
1893 if (dlt_file_read_data(file,verbose)<0)
1895 /* go back to last position in file */
1896 if (0 != fseek(file->handle,file->file_position,SEEK_SET))
1898 sprintf(str,"dlt_file_read_raw, fseek failed 3\n");
1899 dlt_log(LOG_ERR, str);
1904 /* store index pointer to message position in DLT file */
1905 file->index[file->counter] = file->file_position;
1907 file->position = file->counter - 1;
1911 /* increase total message counter */
1912 file->counter_total++;
1914 /* store position to next message */
1915 file->file_position = ftell(file->handle);
1920 int dlt_file_close(DltFile *file,int verbose)
1922 PRINT_FUNCTION_VERBOSE(verbose);
1931 fclose(file->handle);
1939 int dlt_file_message(DltFile *file,int index,int verbose)
1941 PRINT_FUNCTION_VERBOSE(verbose);
1948 /* check if message is in range */
1949 if (index >= file->counter)
1951 sprintf(str,"Message %d out of range!\r\n",index);
1952 dlt_log(LOG_ERR, str);
1956 /* seek to position in file */
1957 if (fseek(file->handle,file->index[index],SEEK_SET)!=0)
1959 sprintf(str,"Seek to message %d to position %ld failed!\r\n",index,file->index[index]);
1960 dlt_log(LOG_ERR, str);
1964 /* read all header and payload */
1965 if (dlt_file_read_header(file,verbose)<0)
1970 if (dlt_file_read_header_extended(file,verbose)<0)
1975 if (dlt_file_read_data(file,verbose)<0)
1980 /* set current position in file */
1981 file->position = index;
1986 int dlt_file_free(DltFile *file,int verbose)
1988 PRINT_FUNCTION_VERBOSE(verbose);
1995 /* delete index lost if exists */
2005 fclose(file->handle);
2009 return dlt_message_free(&(file->msg),verbose);
2012 void dlt_log_set_level(int level)
2014 logging_level = level;
2017 void dlt_log_set_filename(const char *filename)
2019 strncpy(logging_filename,filename,NAME_MAX);
2023 void dlt_log_init(int mode)
2025 logging_mode = mode;
2027 if(logging_mode == 2)
2029 /* internal logging to file */
2030 logging_handle = fopen(logging_filename,"w");
2031 if (logging_handle == 0)
2033 printf("Internal log file %s cannot be opened!\n",logging_filename);
2039 void dlt_log_free(void)
2041 if(logging_mode == 2) {
2042 fclose(logging_handle);
2046 int dlt_log(int prio, char *s)
2048 char logfmtstring[DLT_COMMON_BUFFER_LENGTH];
2054 if(logging_level<prio)
2063 strncpy(logfmtstring,"DLT| EMERGENCY: %s",sizeof(logfmtstring));
2068 strncpy(logfmtstring,"DLT| ALERT: %s",sizeof(logfmtstring));
2073 strncpy(logfmtstring,"DLT| CRITICAL: %s",sizeof(logfmtstring));
2078 strncpy(logfmtstring,"DLT| ERROR: %s",sizeof(logfmtstring));
2083 strncpy(logfmtstring,"DLT| WARNING: %s",sizeof(logfmtstring));
2088 strncpy(logfmtstring,"DLT| NOTICE: %s",sizeof(logfmtstring));
2093 strncpy(logfmtstring,"DLT| INFO: %s",sizeof(logfmtstring));
2098 strncpy(logfmtstring,"DLT| DEBUG: %s",sizeof(logfmtstring));
2103 strncpy(logfmtstring,"DLT| %s",sizeof(logfmtstring));
2108 switch(logging_mode)
2112 printf(logfmtstring, s);
2116 #if !defined (__WIN32__) && !defined(_MSC_VER)
2117 openlog("DLT",LOG_PID,LOG_DAEMON);
2118 syslog(prio, logfmtstring, s);
2124 if(logging_handle) {
2125 fprintf(logging_handle,logfmtstring, s);
2126 fflush(logging_handle);
2134 int dlt_receiver_init(DltReceiver *receiver,int fd, int buffersize)
2141 receiver->lastBytesRcvd = 0;
2142 receiver->bytesRcvd = 0;
2143 receiver->totalBytesRcvd = 0;
2144 receiver->buffersize = buffersize;
2146 receiver->buffer = (char*)malloc(receiver->buffersize);
2148 if (receiver->buffer == 0)
2155 receiver->buf = receiver->buffer;
2161 int dlt_receiver_free(DltReceiver *receiver)
2169 if (receiver->buffer)
2171 free(receiver->buffer);
2174 receiver->buffer = 0;
2181 int dlt_receiver_receive_socket(DltReceiver *receiver)
2188 if (receiver->buffer==0)
2193 receiver->buf = (char *)receiver->buffer;
2194 receiver->lastBytesRcvd = receiver->bytesRcvd;
2196 /* wait for data from socket */
2197 if ((receiver->bytesRcvd = recv(receiver->fd, receiver->buf + receiver->lastBytesRcvd, receiver->buffersize - receiver->lastBytesRcvd , 0)) <= 0)
2199 receiver->bytesRcvd = 0;
2201 return receiver->bytesRcvd;
2204 receiver->totalBytesRcvd += receiver->bytesRcvd;
2205 receiver->bytesRcvd += receiver->lastBytesRcvd;
2207 return receiver->bytesRcvd;
2211 int dlt_receiver_receive_fd(DltReceiver *receiver)
2218 if (receiver->buffer==0)
2223 receiver->buf = (char *)receiver->buffer;
2224 receiver->lastBytesRcvd = receiver->bytesRcvd;
2226 /* wait for data from fd */
2227 if ((receiver->bytesRcvd = read(receiver->fd, receiver->buf + receiver->lastBytesRcvd, receiver->buffersize - receiver->lastBytesRcvd)) <= 0)
2229 receiver->bytesRcvd = 0;
2231 return receiver->bytesRcvd;
2234 receiver->totalBytesRcvd += receiver->bytesRcvd;
2235 receiver->bytesRcvd += receiver->lastBytesRcvd;
2237 return receiver->bytesRcvd;
2240 int dlt_receiver_remove(DltReceiver *receiver,int size)
2247 if (receiver->buf==0)
2252 if (size > receiver->bytesRcvd || size <= 0)
2254 receiver->buf = receiver->buf + receiver->bytesRcvd;
2255 receiver->bytesRcvd=0;
2259 receiver->bytesRcvd = receiver->bytesRcvd - size;
2260 receiver->buf = receiver->buf + size;
2265 int dlt_receiver_move_to_begin(DltReceiver *receiver)
2272 if ((receiver->buffer==0) || (receiver->buf==0))
2277 if ((receiver->buffer!=receiver->buf) && (receiver->bytesRcvd!=0))
2279 memmove(receiver->buffer,receiver->buf,receiver->bytesRcvd);
2285 int dlt_set_storageheader(DltStorageHeader *storageheader, const char *ecu)
2288 #if !defined(_MSC_VER)
2292 if (storageheader==0)
2297 /* get time of day */
2298 #if defined(_MSC_VER)
2299 time(&(storageheader->seconds));
2301 gettimeofday(&tv, NULL);
2304 /* prepare storage header */
2305 storageheader->pattern[0] = 'D';
2306 storageheader->pattern[1] = 'L';
2307 storageheader->pattern[2] = 'T';
2308 storageheader->pattern[3] = 0x01;
2310 dlt_set_id(storageheader->ecu,ecu);
2312 /* Set current time */
2313 #if defined(_MSC_VER)
2314 storageheader->microseconds = 0;
2316 storageheader->seconds = (time_t)tv.tv_sec; /* value is long */
2317 storageheader->microseconds = (int32_t)tv.tv_usec; /* value is long */
2323 int dlt_check_storageheader(DltStorageHeader *storageheader)
2325 if (storageheader==0)
2330 return ((storageheader->pattern[0] == 'D') &&
2331 (storageheader->pattern[1] == 'L') &&
2332 (storageheader->pattern[2] == 'T') &&
2333 (storageheader->pattern[3] == 1));
2347 int dlt_buffer_init_static_server(DltBuffer *buf, const unsigned char *ptr, uint32_t size)
2350 DltBufferHead *head;
2353 buf->shm = (unsigned char *)ptr;
2354 buf->min_size = size;
2355 buf->max_size = size;
2359 head = (DltBufferHead*)buf->shm;
2363 buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
2364 buf->size = buf->min_size - sizeof(DltBufferHead);
2367 memset(buf->mem,0,buf->size);
2369 snprintf(str,sizeof(str),"Buffer: Size %d\n",buf->size);
2370 dlt_log(LOG_INFO, str);
2375 int dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char *ptr, uint32_t size)
2380 buf->shm = (unsigned char *)ptr;
2381 buf->min_size = size;
2382 buf->max_size = size;
2386 buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
2387 buf->size = buf->min_size - sizeof(DltBufferHead);
2389 snprintf(str,sizeof(str),"Buffer: Size %d\n",buf->size);
2390 dlt_log(LOG_INFO, str);
2395 int dlt_buffer_init_dynamic(DltBuffer *buf, uint32_t min_size, uint32_t max_size,uint32_t step_size)
2397 //Do not DLT_SEM_LOCK inside here!
2399 DltBufferHead *head;
2402 buf->min_size = min_size;
2403 buf->max_size = max_size;
2404 buf->step_size = step_size;
2407 buf->shm = malloc(buf->min_size);
2408 if(buf->shm == NULL) {
2409 snprintf(str,sizeof(str),"Buffer: Cannot allocate %d bytes\n",buf->min_size);
2410 dlt_log(LOG_EMERG, str);
2415 head = (DltBufferHead*)buf->shm;
2419 buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
2420 buf->size = buf->min_size - sizeof(DltBufferHead);
2423 memset(buf->mem,0,buf->size);
2425 //snprintf(str,sizeof(str),"Buffer: Size %d bytes\n",buf->size);
2426 //dlt_log(LOG_INFO, str);
2431 int dlt_buffer_free_static(DltBuffer *buf)
2434 // buffer not initialised
2435 dlt_log(LOG_ERR,"Buffer: Buffer not initialised\n");
2436 return -1; /* ERROR */
2442 int dlt_buffer_free_dynamic(DltBuffer *buf)
2445 // buffer not initialised
2446 dlt_log(LOG_ERR,"Buffer: Buffer not initialised\n");
2447 return -1; /* ERROR */
2455 void dlt_buffer_write_block(DltBuffer *buf,int *write, const unsigned char *data,unsigned int size)
2457 if((int)(*write+size) <= buf->size) {
2459 memcpy(buf->mem+*write,data,size);
2464 memcpy(buf->mem+*write, data, buf->size-*write);
2465 memcpy(buf->mem, data+buf->size-*write, size-buf->size+*write);
2466 *write += size-buf->size;
2470 void dlt_buffer_read_block(DltBuffer *buf,int *read,unsigned char *data,unsigned int size)
2472 if((int)(*read+size) <= buf->size) {
2474 memcpy(data,buf->mem+*read,size);
2479 memcpy(data, buf->mem+*read, buf->size-*read);
2480 memcpy(data+buf->size-*read, buf->mem, size-buf->size+*read);
2481 *read += size-buf->size;
2485 int dlt_buffer_increase_size(DltBuffer *buf)
2487 DltBufferHead *head,*new_head;
2488 unsigned char *new_ptr;
2491 if(buf->step_size==0) {
2492 /* cannot increase size */
2497 if((buf->size + sizeof(DltBufferHead) + buf->step_size) > buf->max_size) {
2498 /* max size reached, do not increase */
2502 /* allocate new buffer */
2503 new_ptr = malloc(buf->size + sizeof(DltBufferHead) + buf->step_size);
2504 if(new_ptr == NULL) {
2505 snprintf(str,sizeof(str),"Buffer: Cannot increase size because allocate %d bytes failed\n",buf->min_size);
2506 dlt_log(LOG_WARNING, str);
2511 head = (DltBufferHead*)buf->shm;
2512 new_head = (DltBufferHead*)new_ptr;
2513 if(head->read < head->write) {
2514 memcpy(new_ptr+sizeof(DltBufferHead) , buf->mem+head->read , head->write-head->read);
2516 new_head->write = head->write-head->read;
2517 new_head->count = head->count;
2520 memcpy(new_ptr+sizeof(DltBufferHead) , buf->mem+head->read , buf->size-head->read);
2521 memcpy(new_ptr+sizeof(DltBufferHead)+buf->size-head->read , buf->mem , head->write);
2523 new_head->write = buf->size-head->read+head->write;
2524 new_head->count = head->count;
2532 buf->mem = new_ptr+sizeof(DltBufferHead);
2533 buf->size += buf->step_size;
2535 snprintf(str,sizeof(str),"Buffer: Size increased to %d bytes\n",buf->size+sizeof(DltBufferHead));
2536 dlt_log(LOG_INFO, str);
2541 int dlt_buffer_minimize_size(DltBuffer *buf)
2543 unsigned char *new_ptr;
2545 if((buf->size + sizeof(DltBufferHead)) == buf->min_size)
2547 /* already minimized */
2551 /* allocate new buffer */
2552 new_ptr = malloc(buf->min_size);
2553 if(new_ptr == NULL) {
2554 snprintf(str,sizeof(str),"Buffer: Cannot set to min size of %d bytes\n",buf->min_size);
2555 dlt_log(LOG_WARNING, str);
2564 buf->mem = new_ptr+sizeof(DltBufferHead);
2565 buf->size = buf->min_size - sizeof(DltBufferHead);
2567 /* reset pointers and counters */
2568 ((int*)(buf->shm))[0] = 0; // pointer to write memory
2569 ((int*)(buf->shm))[1] = 0; // pointer to read memory
2570 ((int*)(buf->shm))[2] = 0; // number of packets
2573 memset(buf->mem,0,buf->size);
2575 dlt_log(LOG_INFO,"Buffer: Buffer minimized.\n");
2580 int dlt_buffer_reset(DltBuffer *buf)
2582 dlt_log(LOG_ERR,"Buffer: Buffer reset triggered.\n");
2584 /* reset pointers and counters */
2585 ((int*)(buf->shm))[0] = 0; // pointer to write memory
2586 ((int*)(buf->shm))[1] = 0; // pointer to read memory
2587 ((int*)(buf->shm))[2] = 0; // number of packets
2590 memset(buf->mem,0,buf->size);
2595 int dlt_buffer_push(DltBuffer *buf,const unsigned char *data,unsigned int size)
2597 return dlt_buffer_push3(buf,data,size,0,0,0,0);
2600 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)
2603 int write, read, count;
2604 DltBufferBlockHead head;
2607 // buffer not initialised
2608 dlt_log(LOG_ERR,"Buffer: Buffer not initialised\n");
2609 return -1; /* ERROR */
2612 // get current write pointer
2613 write = ((int*)(buf->shm))[0];
2614 read = ((int*)(buf->shm))[1];
2615 count = ((int*)(buf->shm))[2];
2618 if((read>buf->size) || (write>buf->size))
2620 dlt_log(LOG_ERR,"Buffer: Pointer out of range\n");
2621 dlt_buffer_reset(buf);
2625 // calculate free size
2627 free_size = read - write;
2628 else if(count && (write == read))
2631 free_size = buf->size - write + read;
2634 if(free_size < (int)(sizeof(DltBufferBlockHead)+size1+size2+size3)) {
2635 // try to increase size if possible
2636 if(dlt_buffer_increase_size(buf)) {
2637 /* increase size is not possible */
2638 dlt_log(LOG_ERR,"Buffer: Buffer is full\n");
2642 write = ((int*)(buf->shm))[0];
2643 read = ((int*)(buf->shm))[1];
2647 strcpy(head.head,DLT_BUFFER_HEAD);
2649 head.size = size1+size2+size3;
2652 dlt_buffer_write_block(buf,&write,(unsigned char*)&head,sizeof(DltBufferBlockHead));
2653 if(size1) dlt_buffer_write_block(buf,&write,data1,size1);
2654 if(size2) dlt_buffer_write_block(buf,&write,data2,size2);
2655 if(size3) dlt_buffer_write_block(buf,&write,data3,size3);
2657 // update global shm pointers
2658 ((int*)(buf->shm))[0] = write; // set new write pointer
2659 ((int*)(buf->shm))[2] += 1; // increase counter
2665 int dlt_buffer_get(DltBuffer *buf,unsigned char *data, int max_size,int delete)
2668 int write, read, count;
2669 char head_compare[] = DLT_BUFFER_HEAD;
2670 DltBufferBlockHead head;
2673 // shm not initialised
2674 dlt_log(LOG_ERR,"Buffer: SHM not initialised\n");
2675 return -1; /* ERROR */
2678 // get current write pointer
2679 write = ((int*)(buf->shm))[0];
2680 read = ((int*)(buf->shm))[1];
2681 count = ((int*)(buf->shm))[2];
2684 if((read>buf->size) || (write>buf->size) || (count<0))
2686 dlt_log(LOG_ERR,"Buffer: Pointer out of range\n");
2687 dlt_buffer_reset(buf);
2691 // check if data is in there
2695 dlt_log(LOG_ERR,"Buffer: SHM should be empty, but is not\n");
2696 dlt_buffer_reset(buf);
2701 // calculate used size
2703 used_size = write - read;
2705 used_size = buf->size - read + write;
2708 if(used_size < (int)(sizeof(DltBufferBlockHead))) {
2709 dlt_log(LOG_ERR,"Buffer: Size check 1 failed\n");
2710 dlt_buffer_reset(buf);
2715 dlt_buffer_read_block(buf,&read,(unsigned char*)&head,sizeof(DltBufferBlockHead));
2718 if(memcmp((unsigned char*)(head.head),head_compare,sizeof(head_compare))!=0)
2720 dlt_log(LOG_ERR,"Buffer: Header head check failed\n");
2721 dlt_buffer_reset(buf);
2724 if(head.status != 2)
2726 dlt_log(LOG_ERR,"Buffer: Header status check failed\n");
2727 dlt_buffer_reset(buf);
2731 // second check size
2732 if(used_size < (int)(sizeof(DltBufferBlockHead)+head.size)) {
2733 dlt_log(LOG_ERR,"Buffer: Size check 2 failed\n");
2734 dlt_buffer_reset(buf);
2739 if(max_size && (head.size > max_size)) {
2740 dlt_log(LOG_ERR,"Buffer: Size check 3 failed\n");
2741 // nothing to do but data does not fit provided buffer
2744 if(data && max_size)
2747 dlt_buffer_read_block(buf,&read,data,head.size);
2751 // update buffer pointers
2752 ((int*)(buf->shm))[1] = read; // set new read pointer
2758 if( (read+head.size) <= buf->size)
2759 ((int*)(buf->shm))[1] = read+head.size; // set new read pointer
2761 ((int*)(buf->shm))[1] = read+head.size-buf->size; // set new read pointer
2765 ((int*)(buf->shm))[2] -= 1; // decrease counter
2767 if(((int*)(buf->shm))[2] == 0)
2769 // try to minimize size
2770 dlt_buffer_minimize_size(buf);
2774 return head.size; // OK
2777 int dlt_buffer_pull(DltBuffer *buf,unsigned char *data, int max_size)
2779 return dlt_buffer_get(buf,data,max_size,1);
2782 int dlt_buffer_copy(DltBuffer *buf,unsigned char *data, int max_size)
2784 return dlt_buffer_get(buf,data,max_size,0);
2787 int dlt_buffer_remove(DltBuffer *buf)
2789 return dlt_buffer_get(buf,0,0,1);
2792 void dlt_buffer_info(DltBuffer *buf)
2796 snprintf(str,sizeof(str),"Buffer: Available size: %d\n",buf->size);
2797 dlt_log(LOG_INFO, str);
2798 snprintf(str,sizeof(str),"Buffer: Buffer full start address: %lX\n",(unsigned long)buf->shm);
2799 dlt_log(LOG_INFO, str);
2800 snprintf(str,sizeof(str),"Buffer: Buffer start address: %lX\n",(unsigned long)buf->mem);
2801 dlt_log(LOG_INFO, str);
2805 void dlt_buffer_status(DltBuffer *buf)
2807 int write, read, count;
2810 write = ((int*)(buf->shm))[0];
2811 read = ((int*)(buf->shm))[1];
2812 count = ((int*)(buf->shm))[2];
2814 snprintf(str,sizeof(str),"Buffer: Write: %d\n",write);
2815 dlt_log(LOG_INFO, str);
2816 snprintf(str,sizeof(str),"Buffer: Read: %d\n",read);
2817 dlt_log(LOG_INFO, str);
2818 snprintf(str,sizeof(str),"Buffer: Count: %d\n",count);
2819 dlt_log(LOG_INFO, str);
2822 int dlt_buffer_get_total_size(DltBuffer *buf)
2824 return buf->max_size;
2827 int dlt_buffer_get_used_size(DltBuffer *buf)
2829 int write, read, count;
2831 write = ((int*)(buf->shm))[0];
2832 read = ((int*)(buf->shm))[1];
2833 count = ((int*)(buf->shm))[2];
2839 return (write - read);
2841 return (buf->size - read + write);
2844 int dlt_buffer_get_message_count(DltBuffer *buf)
2846 return ((int*)(buf->shm))[2];
2849 #if !defined (__WIN32__)
2851 int dlt_setup_serial(int fd, speed_t speed)
2853 #if !defined (__WIN32__) && !defined(_MSC_VER)
2854 struct termios config;
2861 if (tcgetattr(fd, &config) < 0)
2866 /* Input flags - Turn off input processing
2867 convert break to null byte, no CR to NL translation,
2868 no NL to CR translation, don't mark parity errors or breaks
2869 no input parity check, don't strip high bit off,
2870 no XON/XOFF software flow control
2872 config.c_iflag &= ~(IGNBRK | BRKINT | ICRNL |
2873 INLCR | PARMRK | INPCK | ISTRIP | IXON);
2875 /* Output flags - Turn off output processing
2876 no CR to NL translation, no NL to CR-NL translation,
2877 no NL to CR translation, no column 0 CR suppression,
2878 no Ctrl-D suppression, no fill characters, no case mapping,
2879 no local output processing
2881 config.c_oflag &= ~(OCRNL | ONLCR | ONLRET |
2882 ONOCR | ONOEOT| OFILL | OLCUC | OPOST);
2886 /* No line processing:
2887 echo off, echo newline off, canonical mode off,
2888 extended input processing off, signal chars off
2890 config.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
2892 /* Turn off character processing
2893 clear current char size mask, no parity checking,
2894 no output processing, force 8 bit input
2896 config.c_cflag &= ~(CSIZE | PARENB);
2897 config.c_cflag |= CS8;
2899 /* One input byte is enough to return from read()
2900 Inter-character timer off
2902 config.c_cc[VMIN] = 1;
2903 config.c_cc[VTIME] = 0;
2905 /* Communication speed (simple version, using the predefined
2908 if (cfsetispeed(&config, speed) < 0 || cfsetospeed(&config, speed) < 0)
2913 /* Finally, apply the configuration
2915 if (tcsetattr(fd, TCSAFLUSH, &config) < 0)
2926 speed_t dlt_convert_serial_speed(int baudrate)
2928 #if !defined (__WIN32__) && !defined(_MSC_VER)
3098 void dlt_get_version(char *buf)
3100 sprintf(buf,"DLT Package Version: %s %s, Package Revision: %s, build on %s %s\n%s %s %s %s\n",
3101 _DLT_PACKAGE_VERSION, _DLT_PACKAGE_VERSION_STATE, _DLT_PACKAGE_REVISION, __DATE__ , __TIME__,_DLT_SYSTEMD_ENABLE,_DLT_SYSTEMD_WATCHDOG_ENABLE,_DLT_TEST_ENABLE,_DLT_SHM_ENABLE );
3104 void dlt_get_major_version(char *buf)
3106 sprintf(buf,"%s",_DLT_PACKAGE_MAJOR_VERSION);
3109 void dlt_get_minor_version(char *buf)
3111 sprintf(buf,"%s",_DLT_PACKAGE_MINOR_VERSION);
3115 uint32_t dlt_uptime(void)
3118 #if defined (__WIN32__) || defined(_MSC_VER)
3120 return (uint32_t)(GetTickCount()*10); /* GetTickCount() return DWORD */
3125 if (clock_gettime(CLOCK_MONOTONIC,&ts)==0)
3127 return ( (uint32_t)ts.tv_sec*10000 + (uint32_t)ts.tv_nsec/100000 );// in 0.1 ms = 100 us
3138 int dlt_message_print_header(DltMessage *message, char *text, uint32_t size, int verbose)
3140 if ((message==0) || (text==0))
3145 dlt_message_header(message,text,size,verbose);
3146 printf("%s\n",text);
3151 int dlt_message_print_hex(DltMessage *message, char *text, uint32_t size, int verbose)
3153 if ((message==0) || (text==0))
3158 dlt_message_header(message,text,size,verbose);
3160 dlt_message_payload(message,text,size,DLT_OUTPUT_HEX,verbose);
3161 printf("[%s]\n",text);
3166 int dlt_message_print_ascii(DltMessage *message, char *text, uint32_t size, int verbose)
3168 if ((message==0) || (text==0))
3173 dlt_message_header(message,text,size,verbose);
3175 dlt_message_payload(message,text,size,DLT_OUTPUT_ASCII,verbose);
3176 printf("[%s]\n",text);
3181 int dlt_message_print_mixed_plain(DltMessage *message, char *text, uint32_t size, int verbose)
3183 if ((message==0) || (text==0))
3188 dlt_message_header(message,text,size,verbose);
3189 printf("%s \n",text);
3190 dlt_message_payload(message,text,size,DLT_OUTPUT_MIXED_FOR_PLAIN,verbose);
3191 printf("[%s]\n",text);
3196 int dlt_message_print_mixed_html(DltMessage *message, char *text, uint32_t size, int verbose)
3198 if ((message==0) || (text==0))
3203 dlt_message_header(message,text,size,verbose);
3204 printf("%s \n",text);
3205 dlt_message_payload(message,text,size,DLT_OUTPUT_MIXED_FOR_HTML,verbose);
3206 printf("[%s]\n",text);
3211 int dlt_message_argument_print(DltMessage *msg,uint32_t type_info,uint8_t **ptr,int32_t *datalength,char *text,int textlength,int byteLength,int __attribute__((unused)) verbose)
3213 int16_t length=0,length_tmp=0; /* the macro can set this variable to -1 */
3214 uint16_t length2=0,length2_tmp=0,length3=0,length3_tmp=0;
3217 uint16_t value16u=0,value16u_tmp=0;
3218 uint32_t value32u=0,value32u_tmp=0;
3219 uint64_t value64u=0,value64u_tmp=0;
3222 int16_t value16i=0,value16i_tmp=0;
3223 int32_t value32i=0,value32i_tmp=0;
3224 int64_t value64i=0,value64i_tmp=0;
3226 float32_t value32f=0,value32f_tmp=0;
3227 int32_t value32f_tmp_int32i=0,value32f_tmp_int32i_swaped=0;
3228 float64_t value64f=0,value64f_tmp=0;
3229 int64_t value64f_tmp_int64i=0,value64f_tmp_int64i_swaped=0;
3231 uint32_t quantisation_tmp=0;
3233 if ( (type_info & DLT_TYPE_INFO_STRG) && (((type_info & DLT_TYPE_INFO_SCOD) == DLT_SCOD_ASCII) || ((type_info & DLT_TYPE_INFO_SCOD) == DLT_SCOD_UTF8)) )
3236 /* string type or utf8-encoded string type */
3239 DLT_MSG_READ_VALUE(length_tmp,*ptr,*datalength,uint16_t);
3242 length=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length_tmp);
3246 length=(int16_t)byteLength;
3249 if (type_info & DLT_TYPE_INFO_VARI)
3251 DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
3254 length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
3255 if((*datalength)<length2)
3258 *datalength-=length2;
3261 DLT_MSG_READ_STRING((text+strlen(text)),*ptr,*datalength,length);
3266 else if (type_info & DLT_TYPE_INFO_BOOL)
3269 if (type_info & DLT_TYPE_INFO_VARI)
3271 DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
3274 length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
3275 if((*datalength)<length2)
3278 *datalength-=length2;
3281 DLT_MSG_READ_VALUE(value8u,*ptr,*datalength,uint8_t); /* No endian conversion necessary */
3284 sprintf(text+strlen(text),"%d",value8u);
3286 else if ((type_info & DLT_TYPE_INFO_SINT) || (type_info & DLT_TYPE_INFO_UINT))
3288 /* signed or unsigned argument received */
3289 if (type_info & DLT_TYPE_INFO_VARI)
3291 DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
3294 length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
3295 DLT_MSG_READ_VALUE(length3_tmp,*ptr,*datalength,uint16_t);
3298 length3=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length3_tmp);
3299 if((*datalength)<length2)
3302 *datalength-=length2;
3303 if((*datalength)<length3)
3306 *datalength-=length3;
3308 if (type_info & DLT_TYPE_INFO_FIXP)
3310 //compiler warning: variable ‘quantisation_tmp’ set but not used [-Wunused-but-set-variable], but: DLT_MSG_READ_VALUE wants a parameter, "0" does not work
3311 DLT_MSG_READ_VALUE(quantisation_tmp,*ptr,*datalength,uint32_t);
3315 switch ( type_info & DLT_TYPE_INFO_TYLE)
3318 case DLT_TYLE_16BIT:
3319 case DLT_TYLE_32BIT:
3327 case DLT_TYLE_64BIT:
3335 case DLT_TYLE_128BIT:
3337 if((*datalength)<16)
3349 switch ( type_info & DLT_TYPE_INFO_TYLE)
3353 if (type_info & DLT_TYPE_INFO_SINT)
3356 DLT_MSG_READ_VALUE(value8i,*ptr,*datalength,int8_t); /* No endian conversion necessary */
3359 sprintf(text+strlen(text),"%d",value8i);
3364 DLT_MSG_READ_VALUE(value8u,*ptr,*datalength,uint8_t); /* No endian conversion necessary */
3367 sprintf(text+strlen(text),"%d",value8u);
3371 case DLT_TYLE_16BIT:
3373 if (type_info & DLT_TYPE_INFO_SINT)
3377 DLT_MSG_READ_VALUE(value16i_tmp,*ptr,*datalength,int16_t);
3380 value16i=DLT_ENDIAN_GET_16(msg->standardheader->htyp, value16i_tmp);
3381 sprintf(text+strlen(text),"%hd",value16i);
3387 DLT_MSG_READ_VALUE(value16u_tmp,*ptr,*datalength,uint16_t);
3390 value16u=DLT_ENDIAN_GET_16(msg->standardheader->htyp, value16u_tmp);
3391 sprintf(text+strlen(text),"%hu",value16u);
3395 case DLT_TYLE_32BIT:
3397 if (type_info & DLT_TYPE_INFO_SINT)
3401 DLT_MSG_READ_VALUE(value32i_tmp,*ptr,*datalength,int32_t);
3404 value32i=DLT_ENDIAN_GET_32(msg->standardheader->htyp, (uint32_t)value32i_tmp);
3405 sprintf(text+strlen(text),"%d",value32i);
3411 DLT_MSG_READ_VALUE(value32u_tmp,*ptr,*datalength,uint32_t);
3414 value32u=DLT_ENDIAN_GET_32(msg->standardheader->htyp, value32u_tmp);
3415 sprintf(text+strlen(text),"%u",value32u);
3419 case DLT_TYLE_64BIT:
3421 if (type_info & DLT_TYPE_INFO_SINT)
3425 DLT_MSG_READ_VALUE(value64i_tmp,*ptr,*datalength,int64_t);
3428 value64i=DLT_ENDIAN_GET_64(msg->standardheader->htyp, (uint64_t)value64i_tmp);
3429 #if defined (__WIN32__) && !defined(_MSC_VER)
3430 sprintf(text+strlen(text),"%I64d",value64i);
3432 sprintf(text+strlen(text),"%lld",value64i);
3439 DLT_MSG_READ_VALUE(value64u_tmp,*ptr,*datalength,uint64_t);
3442 value64u=DLT_ENDIAN_GET_64(msg->standardheader->htyp, value64u_tmp);
3443 #if defined (__WIN32__) && !defined(_MSC_VER)
3444 sprintf(text+strlen(text),"%I64u",value64u);
3446 sprintf(text+strlen(text),"%llu",value64u);
3451 case DLT_TYLE_128BIT:
3453 if (*datalength>=16)
3454 dlt_print_hex_string(text+strlen(text),textlength,*ptr,16);
3455 if((*datalength)<16)
3467 else if (type_info & DLT_TYPE_INFO_FLOA)
3469 /* float data argument */
3470 if (type_info & DLT_TYPE_INFO_VARI)
3472 DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
3475 length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
3476 DLT_MSG_READ_VALUE(length3_tmp,*ptr,*datalength,uint16_t);
3479 length3=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length3_tmp);
3480 if((*datalength)<length2)
3483 *datalength-=length2;
3484 if((*datalength)<length3)
3487 *datalength-=length3;
3489 switch ( type_info & DLT_TYPE_INFO_TYLE)
3494 dlt_print_hex_string(text+strlen(text),textlength,*ptr,1);
3501 case DLT_TYLE_16BIT:
3504 dlt_print_hex_string(text+strlen(text),textlength,*ptr,2);
3511 case DLT_TYLE_32BIT:
3513 if (sizeof(float32_t)==4)
3517 value32f_tmp_int32i=0;
3518 value32f_tmp_int32i_swaped=0;
3519 DLT_MSG_READ_VALUE(value32f_tmp,*ptr,*datalength,float32_t);
3522 memcpy(&value32f_tmp_int32i,&value32f_tmp,sizeof(float32_t));
3523 value32f_tmp_int32i_swaped=DLT_ENDIAN_GET_32(msg->standardheader->htyp, (uint32_t)value32f_tmp_int32i);
3524 memcpy(&value32f,&value32f_tmp_int32i_swaped,sizeof(float32_t));
3525 sprintf(text+strlen(text),"%g",value32f);
3529 dlt_log(LOG_ERR, "Invalid size of float32_t\n");
3534 case DLT_TYLE_64BIT:
3536 if (sizeof(float64_t)==8)
3540 value64f_tmp_int64i=0;
3541 value64f_tmp_int64i_swaped=0;
3542 DLT_MSG_READ_VALUE(value64f_tmp,*ptr,*datalength,float64_t);
3545 memcpy(&value64f_tmp_int64i,&value64f_tmp,sizeof(float64_t));
3546 value64f_tmp_int64i_swaped=DLT_ENDIAN_GET_64(msg->standardheader->htyp, (uint64_t)value64f_tmp_int64i);
3547 memcpy(&value64f,&value64f_tmp_int64i_swaped,sizeof(float64_t));
3549 sprintf(text+strlen(text),"ILLEGAL");
3551 sprintf(text+strlen(text),"%g",value64f);
3556 dlt_log(LOG_ERR, "Invalid size of float64_t\n");
3561 case DLT_TYLE_128BIT:
3563 if (*datalength>=16)
3564 dlt_print_hex_string(text+strlen(text),textlength,*ptr,16);
3565 if((*datalength)<16)
3578 else if (type_info & DLT_TYPE_INFO_RAWD)
3580 /* raw data argument */
3581 DLT_MSG_READ_VALUE(length_tmp,*ptr,*datalength,uint16_t);
3584 length=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length_tmp);
3585 if (type_info & DLT_TYPE_INFO_VARI)
3587 DLT_MSG_READ_VALUE(length2_tmp,*ptr,*datalength,uint16_t);
3590 length2=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length2_tmp);
3591 if((*datalength)<length2)
3594 *datalength-=length2;
3596 if((*datalength)<length)
3598 dlt_print_hex_string(text+strlen(text),textlength,*ptr,length);
3600 *datalength-=length;
3602 else if (type_info & DLT_TYPE_INFO_TRAI)
3604 /* trace info argument */
3605 DLT_MSG_READ_VALUE(length_tmp,*ptr,*datalength,uint16_t);
3608 length=DLT_ENDIAN_GET_16(msg->standardheader->htyp, length_tmp);
3609 DLT_MSG_READ_STRING((text+strlen(text)),*ptr,*datalength,length);
3620 dlt_log(LOG_ERR, "Payload of DLT message corrupted\n");