2 * Dlt Client console utilities - Diagnostic Log and Trace
5 * Copyright (C) 2011, BMW AG - Alexander Wenzel <alexander.wenzel@bmw.de>
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.
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>.
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.
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.
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.
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.
39 /*******************************************************************************
41 ** SRC-MODULE: dlt-receive.cpp **
47 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
54 ** PLATFORM DEPENDANT [yes/no]: yes **
56 ** TO BE CHANGED BY USER [yes/no]: no **
58 *******************************************************************************/
60 /*******************************************************************************
62 ********************************************************************************
64 ** Initials Name Company **
65 ** -------- ------------------------- ---------------------------------- **
66 ** aw Alexander Wenzel BMW **
67 ** mk Markus Klein Fraunhofer ESK **
68 *******************************************************************************/
70 /*******************************************************************************
71 ** Revision Control History **
72 *******************************************************************************/
75 * $LastChangedRevision: 1670 $
76 * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
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() */
88 #include "dlt_client.h"
90 #define DLT_RECEIVE_TEXTBUFSIZE 10024 /* Size of buffer for text output */
92 #define DLT_RECEIVE_ECU_ID "RECV"
94 /* Function prototypes */
95 int dlt_receive_message_callback(DltMessage *message, void *data);
115 * Print usage information of tool.
121 dlt_get_version(version);
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");
142 * Main function of tool.
144 int main(int argc, char* argv[])
147 DltReceiveData dltdata;
151 /* Initialize dltdata */
164 /* Fetch command line arguments */
167 while ((c = getopt (argc, argv, "vashyxmf:o:e:b:")) != -1)
207 dltdata.fvalue = optarg;
212 dltdata.ovalue = optarg;
217 dltdata.evalue = optarg;
222 dltdata.bvalue = atoi(optarg);
227 if (optopt == 'o' || optopt == 'f')
229 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
231 else if (isprint (optopt))
233 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
237 fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
239 /* unknown or wrong option used, show usage information and terminate */
249 /* Initialize DLT Client */
250 dlt_client_init(&dltclient, dltdata.vflag);
252 /* Register callback to be called when message was received */
253 dlt_client_register_message_callback(dlt_receive_message_callback);
255 /* Setup DLT Client structure */
256 dltclient.serial_mode = dltdata.yflag;
258 if (dltclient.serial_mode==0)
260 for (index = optind; index < argc; index++)
262 dltclient.servIP = argv[index];
265 if (dltclient.servIP == 0)
267 /* no hostname selected, show usage and terminate */
268 fprintf(stderr,"ERROR: No hostname selected\n");
270 dlt_client_cleanup(&dltclient,dltdata.vflag);
276 for (index = optind; index < argc; index++)
278 dltclient.serialDevice = argv[index];
281 if (dltclient.serialDevice == 0)
283 /* no serial device name selected, show usage and terminate */
284 fprintf(stderr,"ERROR: No serial device name specified\n");
289 dlt_client_setbaudrate(&dltclient,dltdata.bvalue);
292 /* initialise structure to use DLT file */
293 dlt_file_init(&(dltdata.file),dltdata.vflag);
295 /* first parse filter file if filter parameter is used */
296 dlt_filter_init(&(dltdata.filter),dltdata.vflag);
300 if (dlt_filter_load(&(dltdata.filter),dltdata.fvalue,dltdata.vflag)<0)
302 dlt_file_free(&(dltdata.file),dltdata.vflag);
306 dlt_file_set_filter(&(dltdata.file),&(dltdata.filter),dltdata.vflag);
309 /* open DLT output file */
312 dltdata.ohandle = open(dltdata.ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
314 if (dltdata.ohandle == -1)
316 dlt_file_free(&(dltdata.file),dltdata.vflag);
317 fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",dltdata.ovalue);
324 dlt_set_id(dltdata.ecuid,dltdata.evalue);
328 dlt_set_id(dltdata.ecuid,DLT_RECEIVE_ECU_ID);
331 /* Connect to TCP socket or open serial device */
332 if (dlt_client_connect(&dltclient, dltdata.vflag)!=-1)
335 /* Dlt Client Main Loop */
336 dlt_client_main_loop(&dltclient, &dltdata, dltdata.vflag);
338 /* Dlt Client Cleanup */
339 dlt_client_cleanup(&dltclient,dltdata.vflag);
342 /* dlt-receive cleanup */
345 close(dltdata.ohandle);
348 dlt_file_free(&(dltdata.file),dltdata.vflag);
350 dlt_filter_free(&(dltdata.filter),dltdata.vflag);
355 int dlt_receive_message_callback(DltMessage *message, void *data)
357 DltReceiveData *dltdata;
358 static char text[DLT_RECEIVE_TEXTBUFSIZE];
363 if ((message==0) || (data==0))
368 dltdata = (DltReceiveData*)data;
370 /* prepare storage header */
371 if (DLT_IS_HTYP_WEID(message->standardheader->htyp))
373 dlt_set_storageheader(message->storageheader,message->headerextra.ecu);
377 dlt_set_storageheader(message->storageheader,dltdata->ecuid);
380 if ((dltdata->fvalue==0) || (dltdata->fvalue && dlt_message_filter_check(message,&(dltdata->filter),dltdata->vflag)==1))
382 /* if no filter set or filter is matching display message */
385 dlt_message_print_hex(message,text,DLT_RECEIVE_TEXTBUFSIZE,dltdata->vflag);
387 else if (dltdata->aflag)
390 dlt_message_header(message,text,DLT_RECEIVE_TEXTBUFSIZE,dltdata->vflag);
394 dlt_message_payload(message,text,DLT_RECEIVE_TEXTBUFSIZE,DLT_OUTPUT_ASCII,dltdata->vflag);
396 printf("[%s]\n",text);
398 else if (dltdata->mflag)
400 dlt_message_print_mixed_plain(message,text,DLT_RECEIVE_TEXTBUFSIZE,dltdata->vflag);
402 else if (dltdata->sflag)
405 dlt_message_header(message,text,DLT_RECEIVE_TEXTBUFSIZE,dltdata->vflag);
407 printf("%s \n",text);
410 /* if file output enabled write message */
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;
418 bytes_written = writev(dltdata->ohandle, iov, 2);