Initial Import
[profile/ivi/automotive-dlt.git] / src / console / dlt-convert.c
1 /*
2  * Dlt Client console utilities - Diagnostic Log and Trace
3  * @licence app begin@
4  *
5  * Copyright (C) 2011, BMW AG - Alexander Wenzel <alexander.wenzel@bmw.de>
6  * 
7  * This program is free software; you can redistribute it and/or modify it under the terms of the 
8  * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even 
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General 
11  * Public License, version 2.1, for more details.
12  * 
13  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along 
14  * with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
15  * 
16  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may 
17  * also be applicable to programs even in cases in which the program is not a library in the technical sense.
18  * 
19  * Linking DLT statically or dynamically with other modules is making a combined work based on DLT. You may 
20  * license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to 
21  * license your linked modules under the GNU Lesser General Public License, version 2.1, you 
22  * may use the program under the following exception.
23  * 
24  * As a special exception, the copyright holders of DLT give you permission to combine DLT 
25  * with software programs or libraries that are released under any license unless such a combination is not
26  * permitted by the license of such a software program or library. You may copy and distribute such a 
27  * system following the terms of the GNU Lesser General Public License, version 2.1, including this
28  * special exception, for DLT and the licenses of the other code concerned.
29  * 
30  * Note that people who make modified versions of DLT are not obligated to grant this special exception 
31  * for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, 
32  * version 2.1, gives permission to release a modified version without this exception; this exception 
33  * also makes it possible to release a modified version which carries forward this exception.
34  *
35  * @licence end@
36  */
37
38
39 /*******************************************************************************
40 **                                                                            **
41 **  SRC-MODULE: dlt-convert.cpp                                               **
42 **                                                                            **
43 **  TARGET    : linux                                                         **
44 **                                                                            **
45 **  PROJECT   : DLT                                                           **
46 **                                                                            **
47 **  AUTHOR    : Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
48 **              Markus Klein                                                  **
49 **                                                                            **
50 **  PURPOSE   :                                                               **
51 **                                                                            **
52 **  REMARKS   :                                                               **
53 **                                                                            **
54 **  PLATFORM DEPENDANT [yes/no]: yes                                          **
55 **                                                                            **
56 **  TO BE CHANGED BY USER [yes/no]: no                                        **
57 **                                                                            **
58 *******************************************************************************/
59
60 /*******************************************************************************
61 **                      Author Identity                                       **
62 ********************************************************************************
63 **                                                                            **
64 ** Initials     Name                       Company                            **
65 ** --------     -------------------------  ---------------------------------- **
66 **  aw          Alexander Wenzel           BMW                                **
67 **  mk          Markus Klein               Fraunhofer ESK                     **
68 *******************************************************************************/
69
70 /*******************************************************************************
71 **                      Author Identity                                       **
72 ********************************************************************************
73 **                                                                            **
74 ** Initials     Name                       Company                            **
75 ** --------     -------------------------  ---------------------------------- **
76 **  aw          Alexander Wenzel           BMW                                **
77 *******************************************************************************/
78
79 /*******************************************************************************
80 **                      Revision Control History                              **
81 *******************************************************************************/
82
83 /*
84  * $LastChangedRevision: 1670 $
85  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
86  * $LastChangedBy$
87  Initials    Date         Comment
88  aw          13.01.2010   initial
89  */
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <unistd.h>
93 #include <string.h>
94 #include <ctype.h>
95
96 #include <sys/stat.h>
97 #include <fcntl.h>
98
99 #include <sys/uio.h> /* writev() */
100
101 #include "dlt_common.h"
102
103 #define DLT_CONVERT_TEXTBUFSIZE  10024   /* Size of buffer for text output */
104
105 /**
106  * Print usage information of tool.
107  */
108 void usage()
109 {
110     char version[DLT_CONVERT_TEXTBUFSIZE];
111
112     dlt_get_version(version);
113
114     printf("Usage: dlt-convert [options] [commands] file1 [file2]\n");
115     printf("Read DLT files, print DLT messages as ASCII and store the messages again.\n");
116     printf("Use filters to filter DLT messages.\n");
117     printf("Use Ranges and Output file to cut DLT files.\n");
118     printf("Use two files and Output file to join DLT files.\n");
119     printf("%s \n", version);
120     printf("Commands:\n");
121     printf("  -h            Usage\n");
122     printf("  -a            Print DLT file; payload as ASCII\n");
123     printf("  -x            Print DLT file; payload as hex\n");
124     printf("  -m            Print DLT file; payload as hex and ASCII\n");
125     printf("  -s            Print DLT file; only headers\n");
126     printf("  -o filename   Output messages in new DLT file\n");
127     printf("Options:\n");
128     printf("  -v            Verbose mode\n");
129     printf("  -c            Count number of messages\n");
130     printf("  -f filename   Enable filtering of messages\n");
131     printf("  -b number     First messages to be handled\n");
132     printf("  -e number     Last message to be handled\n");
133     printf("  -w            Follow dlt file while file is increasing\n");
134 }
135
136 /**
137  * Main function of tool.
138  */
139 int main(int argc, char* argv[])
140 {
141     int vflag = 0;
142     int cflag = 0;
143     int aflag = 0;
144     int sflag = 0;
145     int xflag = 0;
146     int mflag = 0;
147     int wflag = 0;
148     char *fvalue = 0;
149     char *bvalue = 0;
150     char *evalue = 0;
151     char *ovalue = 0;
152
153     int index;
154     int c;
155
156         DltFile file;
157         DltFilter filter;
158
159         int ohandle=-1;
160
161         int num, begin, end;
162
163         char text[DLT_CONVERT_TEXTBUFSIZE];
164
165         struct iovec iov[2];
166     int bytes_written;
167
168     opterr = 0;
169
170     while ((c = getopt (argc, argv, "vcashxmwf:b:e:o:")) != -1)
171         switch (c)
172         {
173         case 'v':
174                         {
175                 vflag = 1;
176                 break;
177                         }
178         case 'c':
179                         {
180                 cflag = 1;
181                 break;
182                         }
183         case 'a':
184                         {
185                 aflag = 1;
186                 break;
187                         }
188         case 's':
189                         {
190                 sflag = 1;
191                 break;
192                         }
193         case 'x':
194                         {
195                 xflag = 1;
196                 break;
197                         }
198         case 'm':
199                         {
200                 mflag = 1;
201                 break;
202                         }
203         case 'w':
204                         {
205                 wflag = 1;
206                 break;
207                         }
208         case 'h':
209                         {
210                 usage();
211                 return -1;
212                         }
213         case 'f':
214                         {
215                 fvalue = optarg;
216                 break;
217                         }
218         case 'b':
219                         {
220                 bvalue = optarg;
221                 break;
222                         }
223         case 'e':
224                         {
225                 evalue = optarg;
226                 break;
227                         }
228         case 'o':
229                         {
230                 ovalue = optarg;
231                 break;
232                         }
233         case '?':
234                         {
235                         if (optopt == 'f' || optopt == 'b' || optopt == 'e' || optopt == 'o')
236                                 {
237                             fprintf (stderr, "Option -%c requires an argument.\n", optopt);
238                                 }
239                                 else if (isprint (optopt))
240                                 {
241                             fprintf (stderr, "Unknown option `-%c'.\n", optopt);
242                                 }
243                                 else
244                                 {
245                             fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
246                                 }
247                         /* unknown or wrong option used, show usage information and terminate */
248                         usage();
249                         return -1;
250                         }
251         default:
252                         {
253                 abort();
254                         }
255         }
256
257     /* initialise structure to use DLT file */
258     dlt_file_init(&file,vflag);
259
260     /* first parse filter file if filter parameter is used */
261     if (fvalue)
262     {
263         if (dlt_filter_load(&filter,fvalue,vflag)<0)
264         {
265             dlt_file_free(&file,vflag);
266             return -1;
267         }
268
269         dlt_file_set_filter(&file,&filter,vflag);
270     }
271
272     if (ovalue)
273     {
274         ohandle = open(ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
275         if (ohandle == -1)
276         {
277             dlt_file_free(&file,vflag);
278             fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",ovalue);
279             return -1;
280         }
281
282     }
283
284     for (index = optind; index < argc; index++)
285     {
286         /* load, analyse data file and create index list */
287         if (dlt_file_open(&file,argv[index],vflag)>=0)
288         {
289             while (dlt_file_read(&file,vflag)>=0)
290             {
291             }
292         }
293
294         if (aflag || sflag || xflag || mflag || ovalue)
295         {
296             if (bvalue)
297                         {
298                 begin = atoi(bvalue);
299             }
300                         else
301                         {
302                 begin = 0;
303                         }
304
305             if (evalue && (wflag==0))
306                         {
307                 end = atoi(evalue);
308             }
309                         else
310                         {
311                 end = file.counter-1;
312                         }
313
314             if (begin<0 || begin>=file.counter)
315             {
316                 fprintf(stderr,"ERROR: Selected first message %d is out of range!\n",begin);
317                 return -1;
318             }
319             if (end<0 || end>=file.counter || end<begin)
320             {
321                 fprintf(stderr,"ERROR: Selected end message %d is out of range!\n",end);
322                 return -1;
323             }
324             for (num = begin; num <= end ;num++)
325             {
326                 dlt_file_message(&file,num,vflag);
327
328                 if (xflag)
329                 {
330                     printf("%d ",num);
331                     dlt_message_print_hex(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
332                 }
333                 else if (aflag)
334                 {
335                     printf("%d ",num);
336
337                     dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
338
339                     printf("%s ",text);
340
341                     dlt_message_payload(&file.msg,text,DLT_CONVERT_TEXTBUFSIZE,DLT_OUTPUT_ASCII,vflag);
342
343                     printf("[%s]\n",text);
344                 }
345                 else if (mflag)
346                 {
347                     printf("%d ",num);
348                     dlt_message_print_mixed_plain(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
349                 }
350                 else if (sflag)
351                 {
352                     printf("%d ",num);
353
354                     dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
355
356                     printf("%s \n",text);
357                 }
358
359                 /* if file output enabled write message */
360                 if (ovalue)
361                 {
362                     iov[0].iov_base = file.msg.headerbuffer;
363                     iov[0].iov_len = file.msg.headersize;
364                     iov[1].iov_base = file.msg.databuffer;
365                     iov[1].iov_len = file.msg.datasize;
366
367                     bytes_written = writev(ohandle, iov, 2);
368                 }
369
370                 /* check for new messages if follow flag set */
371                 if (wflag && num==end)
372                 {
373                     while (1)
374                     {
375                         while (dlt_file_read(&file,0)>=0)
376                         {
377                         }
378                         if (end == (file.counter-1))
379                                                 {
380                             /* Sleep if no new message was received */
381                             sleep(1);
382                         }
383                                                 else
384                         {
385                             /* set new end of log file and continue reading */
386                             end = file.counter-1;
387                             break;
388                         }
389                     }
390                 }
391             }
392         }
393         if (cflag)
394         {
395             printf("Total number of messages: %d\n",file.counter_total);
396             if (file.filter)
397                         {
398                 printf("Filtered number of messages: %d\n",file.counter);
399                         }
400         }
401     }
402     if (ovalue)
403     {
404         close(ohandle);
405     }
406     if (index == optind)
407     {
408         /* no file selected, show usage and terminate */
409         fprintf(stderr,"ERROR: No file selected\n");
410         usage();
411         return -1;
412     }
413
414     dlt_file_free(&file,vflag);
415
416     return 0;
417 }