Added missing license header to two files
[profile/ivi/dlt-daemon.git] / src / console / dlt-receive.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-receive.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 **                      Revision Control History                              **
72 *******************************************************************************/
73
74 /*
75  * $LastChangedRevision: 1670 $
76  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
77  * $LastChangedBy$
78  Initials    Date         Comment
79  aw          13.01.2010   initial
80  */
81
82 #include <ctype.h>      /* for isprint() */
83 #include <stdlib.h>     /* for atoi() */
84 #include <sys/stat.h>   /* for S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH */
85 #include <fcntl.h>      /* for open() */
86 #include <sys/uio.h>    /* for writev() */
87
88 #include "dlt_client.h"
89
90 #define DLT_RECEIVE_TEXTBUFSIZE 10024  /* Size of buffer for text output */
91
92 #define DLT_RECEIVE_ECU_ID "RECV"
93
94 /* Function prototypes */
95 int dlt_receive_message_callback(DltMessage *message, void *data);
96
97 typedef struct {
98     int aflag;
99     int sflag;
100     int xflag;
101     int mflag;
102     int vflag;
103     int yflag;
104     char *ovalue;
105     char *fvalue;
106     char *evalue;
107     int bvalue;
108     char ecuid[4];
109     int ohandle;
110     DltFile file;
111     DltFilter filter;
112 } DltReceiveData;
113
114 /**
115  * Print usage information of tool.
116  */
117 void usage()
118 {
119     char version[255];
120
121     dlt_get_version(version);
122
123     printf("Usage: dlt-receive [options] hostname/serial_device_name\n");
124     printf("Receive DLT messages from DLT daemon and print or store the messages.\n");
125     printf("Use filters to filter received messages.\n");
126     printf("%s \n", version);
127     printf("Options:\n");
128     printf("  -a            Print DLT messages; payload as ASCII\n");
129     printf("  -x            Print DLT messages; payload as hex\n");
130     printf("  -m            Print DLT messages; payload as hex and ASCII\n");
131     printf("  -s            Print DLT messages; only headers\n");
132     printf("  -v            Verbose mode\n");
133     printf("  -h            Usage\n");
134     printf("  -y            Serial device mode\n");
135     printf("  -b baudrate   Serial device baudrate (Default: 115200)\n");
136     printf("  -e ecuid      Set ECU ID (Default: RECV)\n");
137     printf("  -o filename   Output messages in new DLT file\n");
138     printf("  -f filename   Enable filtering of messages\n");
139 }
140
141 /**
142  * Main function of tool.
143  */
144 int main(int argc, char* argv[])
145 {
146     DltClient      dltclient;
147     DltReceiveData dltdata;
148     int c;
149     int index;
150
151     /* Initialize dltdata */
152     dltdata.aflag = 0;
153     dltdata.sflag = 0;
154     dltdata.xflag = 0;
155     dltdata.mflag = 0;
156     dltdata.vflag = 0;
157     dltdata.yflag = 0;
158     dltdata.ovalue = 0;
159     dltdata.fvalue = 0;
160     dltdata.evalue = 0;
161     dltdata.bvalue = 0;
162     dltdata.ohandle=-1;
163
164     /* Fetch command line arguments */
165     opterr = 0;
166
167     while ((c = getopt (argc, argv, "vashyxmf:o:e:b:")) != -1)
168         switch (c)
169         {
170         case 'v':
171                         {
172                 dltdata.vflag = 1;
173                 break;
174                         }
175         case 'a':
176                         {
177                 dltdata.aflag = 1;
178                 break;
179                         }
180         case 's':
181                         {
182                 dltdata.sflag = 1;
183                 break;
184                         }
185         case 'x':
186                         {
187                 dltdata.xflag = 1;
188                 break;
189                         }
190         case 'm':
191                         {
192                 dltdata.mflag = 1;
193                 break;
194                         }
195         case 'h':
196                         {
197                 usage();
198                 return -1;
199                         }
200         case 'y':
201                         {
202                 dltdata.yflag = 1;
203                 break;
204                         }
205         case 'f':
206                         {
207                 dltdata.fvalue = optarg;
208                 break;
209                         }
210         case 'o':
211                         {
212                 dltdata.ovalue = optarg;
213                 break;
214                         }
215         case 'e':
216                         {
217                 dltdata.evalue = optarg;
218                 break;
219                         }
220         case 'b':
221                         {
222                 dltdata.bvalue = atoi(optarg);
223                 break;
224                         }
225         case '?':
226                         {
227                         if (optopt == 'o' || optopt == 'f')
228                                 {
229                             fprintf (stderr, "Option -%c requires an argument.\n", optopt);
230                         }
231                                 else if (isprint (optopt))
232                                 {
233                             fprintf (stderr, "Unknown option `-%c'.\n", optopt);
234                         }
235                                 else
236                                 {
237                             fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
238                                 }
239                         /* unknown or wrong option used, show usage information and terminate */
240                         usage();
241                         return -1;
242                         }
243         default:
244                         {
245                 abort ();
246                         }
247         }
248
249     /* Initialize DLT Client */
250     dlt_client_init(&dltclient, dltdata.vflag);
251
252     /* Register callback to be called when message was received */
253     dlt_client_register_message_callback(dlt_receive_message_callback);
254
255     /* Setup DLT Client structure */
256     dltclient.serial_mode = dltdata.yflag;
257
258     if (dltclient.serial_mode==0)
259     {
260         for (index = optind; index < argc; index++)
261         {
262             dltclient.servIP = argv[index];
263         }
264
265         if (dltclient.servIP == 0)
266         {
267             /* no hostname selected, show usage and terminate */
268             fprintf(stderr,"ERROR: No hostname selected\n");
269             usage();
270             dlt_client_cleanup(&dltclient,dltdata.vflag);
271             return -1;
272         }
273     }
274     else
275     {
276         for (index = optind; index < argc; index++)
277         {
278             dltclient.serialDevice = argv[index];
279         }
280
281         if (dltclient.serialDevice == 0)
282         {
283             /* no serial device name selected, show usage and terminate */
284             fprintf(stderr,"ERROR: No serial device name specified\n");
285             usage();
286             return -1;
287         }
288
289                 dlt_client_setbaudrate(&dltclient,dltdata.bvalue);
290     }
291
292     /* initialise structure to use DLT file */
293     dlt_file_init(&(dltdata.file),dltdata.vflag);
294
295     /* first parse filter file if filter parameter is used */
296     dlt_filter_init(&(dltdata.filter),dltdata.vflag);
297
298     if (dltdata.fvalue)
299     {
300         if (dlt_filter_load(&(dltdata.filter),dltdata.fvalue,dltdata.vflag)<0)
301         {
302             dlt_file_free(&(dltdata.file),dltdata.vflag);
303             return -1;
304         }
305
306         dlt_file_set_filter(&(dltdata.file),&(dltdata.filter),dltdata.vflag);
307     }
308
309     /* open DLT output file */
310     if (dltdata.ovalue)
311     {
312         dltdata.ohandle = open(dltdata.ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
313
314         if (dltdata.ohandle == -1)
315         {
316             dlt_file_free(&(dltdata.file),dltdata.vflag);
317             fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",dltdata.ovalue);
318             return -1;
319         }
320     }
321
322     if (dltdata.evalue)
323         {
324         dlt_set_id(dltdata.ecuid,dltdata.evalue);
325     }
326         else
327         {
328         dlt_set_id(dltdata.ecuid,DLT_RECEIVE_ECU_ID);
329         }
330
331     /* Connect to TCP socket or open serial device */
332     if (dlt_client_connect(&dltclient, dltdata.vflag)!=-1)
333     {
334
335         /* Dlt Client Main Loop */
336         dlt_client_main_loop(&dltclient, &dltdata, dltdata.vflag);
337
338         /* Dlt Client Cleanup */
339         dlt_client_cleanup(&dltclient,dltdata.vflag);
340     }
341
342     /* dlt-receive cleanup */
343     if (dltdata.ovalue)
344         {
345         close(dltdata.ohandle);
346         }
347
348     dlt_file_free(&(dltdata.file),dltdata.vflag);
349
350     dlt_filter_free(&(dltdata.filter),dltdata.vflag);
351
352     return 0;
353 }
354
355 int dlt_receive_message_callback(DltMessage *message, void *data)
356 {
357         DltReceiveData *dltdata;
358     static char text[DLT_RECEIVE_TEXTBUFSIZE];
359
360         struct iovec iov[2];
361     int bytes_written;
362
363     if ((message==0) || (data==0))
364         {
365         return -1;
366         }
367
368     dltdata = (DltReceiveData*)data;
369
370     /* prepare storage header */
371     if (DLT_IS_HTYP_WEID(message->standardheader->htyp))
372     {
373         dlt_set_storageheader(message->storageheader,message->headerextra.ecu);
374     }
375     else
376     {
377         dlt_set_storageheader(message->storageheader,dltdata->ecuid);
378     }
379
380     if ((dltdata->fvalue==0) || (dltdata->fvalue && dlt_message_filter_check(message,&(dltdata->filter),dltdata->vflag)==1))
381     {
382         /* if no filter set or filter is matching display message */
383         if (dltdata->xflag)
384         {
385             dlt_message_print_hex(message,text,DLT_RECEIVE_TEXTBUFSIZE,dltdata->vflag);
386         }
387         else if (dltdata->aflag)
388         {
389
390             dlt_message_header(message,text,DLT_RECEIVE_TEXTBUFSIZE,dltdata->vflag);
391
392             printf("%s ",text);
393
394             dlt_message_payload(message,text,DLT_RECEIVE_TEXTBUFSIZE,DLT_OUTPUT_ASCII,dltdata->vflag);
395
396             printf("[%s]\n",text);
397         }
398         else if (dltdata->mflag)
399         {
400             dlt_message_print_mixed_plain(message,text,DLT_RECEIVE_TEXTBUFSIZE,dltdata->vflag);
401         }
402         else if (dltdata->sflag)
403         {
404
405             dlt_message_header(message,text,DLT_RECEIVE_TEXTBUFSIZE,dltdata->vflag);
406
407             printf("%s \n",text);
408         }
409
410         /* if file output enabled write message */
411         if (dltdata->ovalue)
412         {
413             iov[0].iov_base = message->headerbuffer;
414             iov[0].iov_len = message->headersize;
415             iov[1].iov_base = message->databuffer;
416             iov[1].iov_len = message->datasize;
417
418             bytes_written = writev(dltdata->ohandle, iov, 2);
419         }
420     }
421
422     return 0;
423 }