automotive-dlt
dlt-convert.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 /*******************************************************************************
28 ** **
29 ** SRC-MODULE: dlt-convert.cpp **
30 ** **
31 ** TARGET : linux **
32 ** **
33 ** PROJECT : DLT **
34 ** **
35 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
36 ** Markus Klein **
37 ** **
38 ** PURPOSE : **
39 ** **
40 ** REMARKS : **
41 ** **
42 ** PLATFORM DEPENDANT [yes/no]: yes **
43 ** **
44 ** TO BE CHANGED BY USER [yes/no]: no **
45 ** **
46 *******************************************************************************/
47 
48 /*******************************************************************************
49 ** Author Identity **
50 ********************************************************************************
51 ** **
52 ** Initials Name Company **
53 ** -------- ------------------------- ---------------------------------- **
54 ** aw Alexander Wenzel BMW **
55 ** mk Markus Klein Fraunhofer ESK **
56 *******************************************************************************/
57 
58 /*******************************************************************************
59 ** Author Identity **
60 ********************************************************************************
61 ** **
62 ** Initials Name Company **
63 ** -------- ------------------------- ---------------------------------- **
64 ** aw Alexander Wenzel BMW **
65 *******************************************************************************/
66 
67 /*******************************************************************************
68 ** Revision Control History **
69 *******************************************************************************/
70 
71 /*
72  * $LastChangedRevision: 1670 $
73  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
74  * $LastChangedBy$
75  Initials Date Comment
76  aw 13.01.2010 initial
77  */
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <unistd.h>
81 #include <string.h>
82 #include <ctype.h>
83 
84 #include <sys/stat.h>
85 #include <fcntl.h>
86 
87 #include <sys/uio.h> /* writev() */
88 
89 #include "dlt_common.h"
90 
91 #define DLT_CONVERT_TEXTBUFSIZE 10024 /* Size of buffer for text output */
92 
96 void usage()
97 {
98  char version[DLT_CONVERT_TEXTBUFSIZE];
99 
100  dlt_get_version(version,255);
101 
102  printf("Usage: dlt-convert [options] [commands] file1 [file2]\n");
103  printf("Read DLT files, print DLT messages as ASCII and store the messages again.\n");
104  printf("Use filters to filter DLT messages.\n");
105  printf("Use Ranges and Output file to cut DLT files.\n");
106  printf("Use two files and Output file to join DLT files.\n");
107  printf("%s \n", version);
108  printf("Commands:\n");
109  printf(" -h Usage\n");
110  printf(" -a Print DLT file; payload as ASCII\n");
111  printf(" -x Print DLT file; payload as hex\n");
112  printf(" -m Print DLT file; payload as hex and ASCII\n");
113  printf(" -s Print DLT file; only headers\n");
114  printf(" -o filename Output messages in new DLT file\n");
115  printf("Options:\n");
116  printf(" -v Verbose mode\n");
117  printf(" -c Count number of messages\n");
118  printf(" -f filename Enable filtering of messages\n");
119  printf(" -b number First messages to be handled\n");
120  printf(" -e number Last message to be handled\n");
121  printf(" -w Follow dlt file while file is increasing\n");
122 }
123 
127 int main(int argc, char* argv[])
128 {
129  int vflag = 0;
130  int cflag = 0;
131  int aflag = 0;
132  int sflag = 0;
133  int xflag = 0;
134  int mflag = 0;
135  int wflag = 0;
136  char *fvalue = 0;
137  char *bvalue = 0;
138  char *evalue = 0;
139  char *ovalue = 0;
140 
141  int index;
142  int c;
143 
144  DltFile file;
145  DltFilter filter;
146 
147  int ohandle=-1;
148 
149  int num, begin, end;
150 
151  char text[DLT_CONVERT_TEXTBUFSIZE];
152 
153  struct iovec iov[2];
154  int bytes_written;
155 
156  opterr = 0;
157 
158  while ((c = getopt (argc, argv, "vcashxmwf:b:e:o:")) != -1)
159  switch (c)
160  {
161  case 'v':
162  {
163  vflag = 1;
164  break;
165  }
166  case 'c':
167  {
168  cflag = 1;
169  break;
170  }
171  case 'a':
172  {
173  aflag = 1;
174  break;
175  }
176  case 's':
177  {
178  sflag = 1;
179  break;
180  }
181  case 'x':
182  {
183  xflag = 1;
184  break;
185  }
186  case 'm':
187  {
188  mflag = 1;
189  break;
190  }
191  case 'w':
192  {
193  wflag = 1;
194  break;
195  }
196  case 'h':
197  {
198  usage();
199  return -1;
200  }
201  case 'f':
202  {
203  fvalue = optarg;
204  break;
205  }
206  case 'b':
207  {
208  bvalue = optarg;
209  break;
210  }
211  case 'e':
212  {
213  evalue = optarg;
214  break;
215  }
216  case 'o':
217  {
218  ovalue = optarg;
219  break;
220  }
221  case '?':
222  {
223  if (optopt == 'f' || optopt == 'b' || optopt == 'e' || optopt == 'o')
224  {
225  fprintf (stderr, "Option -%c requires an argument.\n", optopt);
226  }
227  else if (isprint (optopt))
228  {
229  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
230  }
231  else
232  {
233  fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
234  }
235  /* unknown or wrong option used, show usage information and terminate */
236  usage();
237  return -1;
238  }
239  default:
240  {
241  abort();
242  return -1;//for parasoft
243  }
244  }
245 
246  /* initialise structure to use DLT file */
247  dlt_file_init(&file,vflag);
248 
249  /* first parse filter file if filter parameter is used */
250  if (fvalue)
251  {
252  if (dlt_filter_load(&filter,fvalue,vflag) < DLT_RETURN_OK)
253  {
254  dlt_file_free(&file,vflag);
255  return -1;
256  }
257 
258  dlt_file_set_filter(&file,&filter,vflag);
259  }
260 
261  if (ovalue)
262  {
263  ohandle = open(ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
264  if (ohandle == -1)
265  {
266  dlt_file_free(&file,vflag);
267  fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",ovalue);
268  return -1;
269  }
270 
271  }
272 
273  for (index = optind; index < argc; index++)
274  {
275  /* load, analyse data file and create index list */
276  if (dlt_file_open(&file,argv[index],vflag) >= DLT_RETURN_OK)
277  {
278  while (dlt_file_read(&file,vflag) >= DLT_RETURN_OK)
279  {
280  }
281  }
282 
283  if (aflag || sflag || xflag || mflag || ovalue)
284  {
285  if (bvalue)
286  {
287  begin = atoi(bvalue);
288  }
289  else
290  {
291  begin = 0;
292  }
293 
294  if (evalue && (wflag==0))
295  {
296  end = atoi(evalue);
297  }
298  else
299  {
300  end = file.counter-1;
301  }
302 
303  if (begin<0 || begin>=file.counter)
304  {
305  fprintf(stderr,"ERROR: Selected first message %d is out of range!\n",begin);
306  return -1;
307  }
308  if (end<0 || end>=file.counter || end<begin)
309  {
310  fprintf(stderr,"ERROR: Selected end message %d is out of range!\n",end);
311  return -1;
312  }
313  for (num = begin; num <= end ;num++)
314  {
315  dlt_file_message(&file,num,vflag);
316 
317  if (xflag)
318  {
319  printf("%d ",num);
321  }
322  else if (aflag)
323  {
324  printf("%d ",num);
325 
326  dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
327 
328  printf("%s ",text);
329 
331 
332  printf("[%s]\n",text);
333  }
334  else if (mflag)
335  {
336  printf("%d ",num);
338  }
339  else if (sflag)
340  {
341  printf("%d ",num);
342 
343  dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
344 
345  printf("%s \n",text);
346  }
347 
348  /* if file output enabled write message */
349  if (ovalue)
350  {
351  iov[0].iov_base = file.msg.headerbuffer;
352  iov[0].iov_len = file.msg.headersize;
353  iov[1].iov_base = file.msg.databuffer;
354  iov[1].iov_len = file.msg.datasize;
355 
356  bytes_written = writev(ohandle, iov, 2);
357  if (0 > bytes_written){
358  printf("in main: writev(ohandle, iov, 2); returned an error!" );
359  dlt_file_free(&file,vflag);
360  return -1;
361  }
362  }
363 
364  /* check for new messages if follow flag set */
365  if (wflag && num==end)
366  {
367  while (1)
368  {
369  while (dlt_file_read(&file,0)>=0)
370  {
371  }
372  if (end == (file.counter-1))
373  {
374  /* Sleep if no new message was received */
375  sleep(1);
376  }
377  else
378  {
379  /* set new end of log file and continue reading */
380  end = file.counter-1;
381  break;
382  }
383  }
384  }
385  }
386  }
387  if (cflag)
388  {
389  printf("Total number of messages: %d\n",file.counter_total);
390  if (file.filter)
391  {
392  printf("Filtered number of messages: %d\n",file.counter);
393  }
394  }
395  }
396  if (ovalue)
397  {
398  close(ohandle);
399  }
400  if (index == optind)
401  {
402  /* no file selected, show usage and terminate */
403  fprintf(stderr,"ERROR: No file selected\n");
404  usage();
405  return -1;
406  }
407 
408  dlt_file_free(&file,vflag);
409 
410  return 0;
411 }
int32_t datasize
Definition: dlt_common.h:423
DltReturnValue dlt_file_open(DltFile *file, const char *filename, int verbose)
Definition: dlt_common.c:1594
DltReturnValue dlt_file_message(DltFile *file, int index, int verbose)
Definition: dlt_common.c:1895
DltReturnValue dlt_file_set_filter(DltFile *file, DltFilter *filter, int verbose)
Definition: dlt_common.c:1331
DltReturnValue dlt_file_read(DltFile *file, int verbose)
Definition: dlt_common.c:1647
DltFilter * filter
Definition: dlt_common.h:659
#define DLT_CONVERT_TEXTBUFSIZE
Definition: dlt-convert.c:91
int32_t counter_total
Definition: dlt_common.h:650
DltReturnValue dlt_filter_load(DltFilter *filter, const char *filename, int verbose)
Definition: dlt_common.c:416
int32_t headersize
Definition: dlt_common.h:422
void usage()
Definition: dlt-convert.c:96
uint8_t headerbuffer[sizeof(DltStorageHeader)+sizeof(DltStandardHeader)+sizeof(DltStandardHeaderExtra)+sizeof(DltExtendedHeader)]
Definition: dlt_common.h:427
DltMessage msg
Definition: dlt_common.h:663
DltReturnValue dlt_file_init(DltFile *file, int verbose)
Definition: dlt_common.c:1305
DltReturnValue dlt_message_payload(DltMessage *msg, char *text, int textlength, int type, int verbose)
Definition: dlt_common.c:891
DltReturnValue dlt_file_free(DltFile *file, int verbose)
Definition: dlt_common.c:1942
int32_t counter
Definition: dlt_common.h:649
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
DltReturnValue dlt_message_header(DltMessage *msg, char *text, int textlength, int verbose)
Definition: dlt_common.c:710
DltReturnValue dlt_message_print_hex(DltMessage *message, char *text, uint32_t size, int verbose)
Definition: dlt_common.c:3310
#define DLT_OUTPUT_ASCII
Definition: dlt_common.h:271
uint8_t * databuffer
Definition: dlt_common.h:428
int main(int argc, char *argv[])
Definition: dlt-convert.c:127
DltReturnValue dlt_message_print_mixed_plain(DltMessage *message, char *text, uint32_t size, int verbose)
Definition: dlt_common.c:3340