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-convert.cpp **
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 /*******************************************************************************
57 ********************************************************************************
59 ** Initials Name Company **
60 ** -------- ------------------------- ---------------------------------- **
61 ** aw Alexander Wenzel BMW **
62 *******************************************************************************/
64 /*******************************************************************************
65 ** Revision Control History **
66 *******************************************************************************/
69 * $LastChangedRevision: 1670 $
70 * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
84 #include <sys/uio.h> /* writev() */
86 #include "dlt_common.h"
88 #define DLT_CONVERT_TEXTBUFSIZE 10024 /* Size of buffer for text output */
91 * Print usage information of tool.
95 char version[DLT_CONVERT_TEXTBUFSIZE];
97 dlt_get_version(version,255);
99 printf("Usage: dlt-convert [options] [commands] file1 [file2]\n");
100 printf("Read DLT files, print DLT messages as ASCII and store the messages again.\n");
101 printf("Use filters to filter DLT messages.\n");
102 printf("Use Ranges and Output file to cut DLT files.\n");
103 printf("Use two files and Output file to join DLT files.\n");
104 printf("%s \n", version);
105 printf("Commands:\n");
106 printf(" -h Usage\n");
107 printf(" -a Print DLT file; payload as ASCII\n");
108 printf(" -x Print DLT file; payload as hex\n");
109 printf(" -m Print DLT file; payload as hex and ASCII\n");
110 printf(" -s Print DLT file; only headers\n");
111 printf(" -o filename Output messages in new DLT file\n");
112 printf("Options:\n");
113 printf(" -v Verbose mode\n");
114 printf(" -c Count number of messages\n");
115 printf(" -f filename Enable filtering of messages\n");
116 printf(" -b number First messages to be handled\n");
117 printf(" -e number Last message to be handled\n");
118 printf(" -w Follow dlt file while file is increasing\n");
122 * Main function of tool.
124 int main(int argc, char* argv[])
148 char text[DLT_CONVERT_TEXTBUFSIZE];
155 while ((c = getopt (argc, argv, "vcashxmwf:b:e:o:")) != -1)
220 if (optopt == 'f' || optopt == 'b' || optopt == 'e' || optopt == 'o')
222 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
224 else if (isprint (optopt))
226 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
230 fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
232 /* unknown or wrong option used, show usage information and terminate */
239 return -1;//for parasoft
243 /* initialise structure to use DLT file */
244 dlt_file_init(&file,vflag);
246 /* first parse filter file if filter parameter is used */
249 if (dlt_filter_load(&filter,fvalue,vflag)<0)
251 dlt_file_free(&file,vflag);
255 dlt_file_set_filter(&file,&filter,vflag);
260 ohandle = open(ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
263 dlt_file_free(&file,vflag);
264 fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",ovalue);
270 for (index = optind; index < argc; index++)
272 /* load, analyse data file and create index list */
273 if (dlt_file_open(&file,argv[index],vflag)>=0)
275 while (dlt_file_read(&file,vflag)>=0)
280 if (aflag || sflag || xflag || mflag || ovalue)
284 begin = atoi(bvalue);
291 if (evalue && (wflag==0))
297 end = file.counter-1;
300 if (begin<0 || begin>=file.counter)
302 fprintf(stderr,"ERROR: Selected first message %d is out of range!\n",begin);
305 if (end<0 || end>=file.counter || end<begin)
307 fprintf(stderr,"ERROR: Selected end message %d is out of range!\n",end);
310 for (num = begin; num <= end ;num++)
312 dlt_file_message(&file,num,vflag);
317 dlt_message_print_hex(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
323 dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
327 dlt_message_payload(&file.msg,text,DLT_CONVERT_TEXTBUFSIZE,DLT_OUTPUT_ASCII,vflag);
329 printf("[%s]\n",text);
334 dlt_message_print_mixed_plain(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
340 dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
342 printf("%s \n",text);
345 /* if file output enabled write message */
348 iov[0].iov_base = file.msg.headerbuffer;
349 iov[0].iov_len = file.msg.headersize;
350 iov[1].iov_base = file.msg.databuffer;
351 iov[1].iov_len = file.msg.datasize;
353 bytes_written = writev(ohandle, iov, 2);
354 if (0 > bytes_written){
355 printf("in main: writev(ohandle, iov, 2); returned an error!" );
356 dlt_file_free(&file,vflag);
361 /* check for new messages if follow flag set */
362 if (wflag && num==end)
366 while (dlt_file_read(&file,0)>=0)
369 if (end == (file.counter-1))
371 /* Sleep if no new message was received */
376 /* set new end of log file and continue reading */
377 end = file.counter-1;
386 printf("Total number of messages: %d\n",file.counter_total);
389 printf("Filtered number of messages: %d\n",file.counter);
399 /* no file selected, show usage and terminate */
400 fprintf(stderr,"ERROR: No file selected\n");
405 dlt_file_free(&file,vflag);