automotive-dlt
dlt-test-stress-client.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-test-stress-client.c **
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 ** Revision Control History **
60 *******************************************************************************/
61 
62 /*
63  * $LastChangedRevision: 1670 $
64  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
65  * $LastChangedBy$
66  Initials Date Comment
67  aw 13.01.2010 initial
68  */
69 
70 #include <ctype.h> /* for isprint() */
71 #include <stdlib.h> /* for atoi() */
72 #include <sys/stat.h> /* for S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH */
73 #include <fcntl.h> /* for open() */
74 #include <string.h> /* for strcmp() */
75 #include <sys/uio.h> /* for writev() */
76 
77 #include "dlt_client.h"
78 #include "dlt_protocol.h"
79 #include "dlt_user.h"
80 
81 #define DLT_TESTCLIENT_TEXTBUFSIZE 10024 /* Size of buffer for text output */
82 #define DLT_TESTCLIENT_ECU_ID "ECU1"
83 
84 #define DLT_TESTCLIENT_NUM_TESTS 7
85 
86 /* Function prototypes */
88 
89 typedef struct
90 {
91  int aflag;
92  int sflag;
93  int xflag;
94  int mflag;
95  int vflag;
96  int yflag;
97  char *ovalue;
98  char *fvalue;
99  char *tvalue;
100  char *evalue;
101  int nvalue;
102  int bvalue;
103 
104  char ecuid[4];
105  int ohandle;
106 
107  DltFile file;
108  DltFilter filter;
109 
110  int running_test;
111 
112  int test_counter_macro[DLT_TESTCLIENT_NUM_TESTS];
113  int test_counter_function[DLT_TESTCLIENT_NUM_TESTS];
114 
115  int tests_passed;
116  int tests_failed;
117 
118  int sock;
119 
120  // test values
121  unsigned long bytes_received;
122  unsigned long time_elapsed;
126 
128 
132 void usage()
133 {
134  char version[255];
135 
136  dlt_get_version(version,255);
137 
138  printf("Usage: dlt-test-stress-client [options] hostname/serial_device_name\n");
139  printf("Test against received data from dlt-test-stress-user.\n");
140  printf("%s \n", version);
141  printf("Options:\n");
142  printf(" -a Print DLT messages; payload as ASCII\n");
143  printf(" -x Print DLT messages; payload as hex\n");
144  printf(" -m Print DLT messages; payload as hex and ASCII\n");
145  printf(" -s Print DLT messages; only headers\n");
146  printf(" -v Verbose mode\n");
147  printf(" -h Usage\n");
148  printf(" -y Serial device mode\n");
149  printf(" -b baudrate Serial device baudrate (Default: 115200)\n");
150  printf(" -e ecuid Set ECU ID (Default: ECU1)\n");
151  printf(" -o filename Output messages in new DLT file\n");
152  printf(" -f filename Enable filtering of messages\n");
153  printf(" -n messages Number of messages to be received per test(Default: 10000)\n");
154 }
155 
159 int main(int argc, char* argv[])
160 {
161  DltClient dltclient;
162  DltTestclientData dltdata;
163  int c,i;
164  int index;
165 
166  /* Initialize dltdata */
167  dltdata.aflag = 0;
168  dltdata.sflag = 0;
169  dltdata.xflag = 0;
170  dltdata.mflag = 0;
171  dltdata.vflag = 0;
172  dltdata.yflag = 0;
173  dltdata.ovalue = 0;
174  dltdata.fvalue = 0;
175  dltdata.evalue = 0;
176  dltdata.bvalue = 0;
177  dltdata.nvalue = 10000;
178  dltdata.ohandle= -1;
179 
180  dltdata.running_test = 0;
181 
182  for (i=0;i<DLT_TESTCLIENT_NUM_TESTS;i++)
183  {
184  dltdata.test_counter_macro[i]=0;
185  dltdata.test_counter_function[i]=0;
186  }
187 
188  dltdata.tests_passed = 0;
189  dltdata.tests_failed = 0;
190 
191  dltdata.bytes_received = 0;
192  dltdata.time_elapsed = dlt_uptime();
193 
194  dltdata.last_value = 0;
195  dltdata.count_received_messages = 0;
196  dltdata.count_not_received_messages = 0;
197 
198 
199  dltdata.sock = -1;
200 
201  /* Fetch command line arguments */
202  opterr = 0;
203 
204  while ((c = getopt (argc, argv, "vashyxmf:o:e:b:n:")) != -1)
205  {
206  switch (c)
207  {
208  case 'v':
209  {
210  dltdata.vflag = 1;
211  break;
212  }
213  case 'a':
214  {
215  dltdata.aflag = 1;
216  break;
217  }
218  case 's':
219  {
220  dltdata.sflag = 1;
221  break;
222  }
223  case 'x':
224  {
225  dltdata.xflag = 1;
226  break;
227  }
228  case 'm':
229  {
230  dltdata.mflag = 1;
231  break;
232  }
233  case 'h':
234  {
235  usage();
236  return -1;
237  }
238  case 'y':
239  {
240  dltdata.yflag = 1;
241  break;
242  }
243  case 'f':
244  {
245  dltdata.fvalue = optarg;
246  break;
247  }
248  case 'o':
249  {
250  dltdata.ovalue = optarg;
251  break;
252  }
253  case 'e':
254  {
255  dltdata.evalue = optarg;
256  break;
257  }
258  case 'b':
259  {
260  dltdata.bvalue = atoi(optarg);
261  break;
262  }
263  case 'n':
264  {
265  dltdata.nvalue = atoi(optarg);
266  break;
267  }
268  case '?':
269  {
270  if (optopt == 'o' || optopt == 'f' || optopt == 't')
271  {
272  fprintf (stderr, "Option -%c requires an argument.\n", optopt);
273  }
274  else if (isprint (optopt))
275  {
276  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
277  }
278  else
279  {
280  fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
281  }
282  /* unknown or wrong option used, show usage information and terminate */
283  usage();
284  return -1;
285  }
286  default:
287  {
288  abort ();
289  return -1;//for parasoft
290  }
291  }
292  }
293 
294  /* Initialize DLT Client */
295  dlt_client_init(&dltclient, dltdata.vflag);
296 
297  /* Register callback to be called when message was received */
299 
300  /* Setup DLT Client structure */
301  dltclient.mode = dltdata.yflag;
302 
303  if (dltclient.mode==0)
304  {
305  for (index = optind; index < argc; index++)
306  {
307  if(dlt_client_set_server_ip(&dltclient, argv[index]) == -1)
308  {
309  fprintf(stderr,"set server ip didn't succeed\n");
310  return -1;
311  }
312  }
313 
314  if (dltclient.servIP == 0)
315  {
316  /* no hostname selected, show usage and terminate */
317  fprintf(stderr,"ERROR: No hostname selected\n");
318  usage();
319  dlt_client_cleanup(&dltclient,dltdata.vflag);
320  return -1;
321  }
322  }
323  else
324  {
325  for (index = optind; index < argc; index++)
326  {
327  if(dlt_client_set_serial_device(&dltclient, argv[index]) == -1)
328  {
329  fprintf(stderr,"set serial device didn't succeed\n");
330  return -1;
331  }
332  }
333 
334  if (dltclient.serialDevice == 0)
335  {
336  /* no serial device name selected, show usage and terminate */
337  fprintf(stderr,"ERROR: No serial device name specified\n");
338  usage();
339  return -1;
340  }
341 
342  dlt_client_setbaudrate(&dltclient,dltdata.bvalue);
343  }
344 
345  /* initialise structure to use DLT file */
346  dlt_file_init(&(dltdata.file),dltdata.vflag);
347 
348  /* first parse filter file if filter parameter is used */
349  dlt_filter_init(&(dltdata.filter),dltdata.vflag);
350 
351  if (dltdata.fvalue)
352  {
353  if (dlt_filter_load(&(dltdata.filter),dltdata.fvalue,dltdata.vflag) < DLT_RETURN_OK)
354  {
355  dlt_file_free(&(dltdata.file),dltdata.vflag);
356  return -1;
357  }
358 
359  dlt_file_set_filter(&(dltdata.file),&(dltdata.filter),dltdata.vflag);
360  }
361 
362  /* open DLT output file */
363  if (dltdata.ovalue)
364  {
365  dltdata.ohandle = open(dltdata.ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
366 
367  if (dltdata.ohandle == -1)
368  {
369  dlt_file_free(&(dltdata.file),dltdata.vflag);
370  fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",dltdata.ovalue);
371  return -1;
372  }
373  }
374 
375  if (dltdata.evalue)
376  {
377  dlt_set_id(dltdata.ecuid,dltdata.evalue);
378  }
379  else
380  {
382  }
383 
384  /* Connect to TCP socket or open serial device */
385  if (dlt_client_connect(&dltclient, dltdata.vflag) != DLT_RETURN_ERROR)
386  {
387  dltdata.sock = dltclient.sock;
388 
389  /* Dlt Client Main Loop */
390  dlt_client_main_loop(&dltclient, &dltdata, dltdata.vflag);
391 
392  /* Dlt Client Cleanup */
393  dlt_client_cleanup(&dltclient,dltdata.vflag);
394  }
395 
396  /* dlt-receive cleanup */
397  if (dltdata.ovalue)
398  {
399  close(dltdata.ohandle);
400  }
401 
402  dlt_file_free(&(dltdata.file),dltdata.vflag);
403 
404  dlt_filter_free(&(dltdata.filter),dltdata.vflag);
405 
406  return 0;
407 }
408 
410 {
411  static char text[DLT_TESTCLIENT_TEXTBUFSIZE];
412  DltTestclientData *dltdata;
413 
414  uint32_t type_info, type_info_tmp;
415  int16_t length,length_tmp; /* the macro can set this variable to -1 */
416  uint8_t *ptr;
417  int32_t datalength;
418  int32_t value;
419  uint32_t value_tmp = 0;
420 
421  struct iovec iov[2];
422  int bytes_written;
423 
424  if ((message==0) || (data==0))
425  {
426  return -1;
427  }
428 
429  dltdata = (DltTestclientData*)data;
430 
431  /* prepare storage header */
432  if (DLT_IS_HTYP_WEID(message->standardheader->htyp))
433  {
434  dlt_set_storageheader(message->storageheader,message->headerextra.ecu);
435  }
436  else
437  {
438  dlt_set_storageheader(message->storageheader,dltdata->ecuid);
439  }
440 
441  if ((dltdata->fvalue==0) || (dltdata->fvalue && dlt_message_filter_check(message,&(dltdata->filter),dltdata->vflag) == DLT_RETURN_TRUE))
442  {
443 
444  //dlt_message_header(message,text,sizeof(text),dltdata->vflag);
445  if (dltdata->aflag)
446  {
447  //printf("%s ",text);
448  }
449  //dlt_message_payload(message,text,sizeof(text),DLT_OUTPUT_ASCII,dltdata->vflag);
450  if (dltdata->aflag)
451  {
452  //printf("[%s]\n",text);
453  }
454 
455  /* do something here */
456 
457  // Count number of received bytes
458  dltdata->bytes_received += message->datasize+message->headersize-sizeof(DltStorageHeader);
459 
460  // print number of received bytes
461  if((dlt_uptime() - dltdata->time_elapsed) > 10000)
462  {
463  printf("Received %lu Bytes/s\n",dltdata->bytes_received);
464  //printf("Received %lu Bytes received\n",dltdata->bytes_received);
465  dltdata->time_elapsed = dlt_uptime();
466  dltdata->bytes_received = 0;
467  }
468 
469  /* Extended header */
470  if (DLT_IS_HTYP_UEH(message->standardheader->htyp))
471  {
472  /* Log message */
473  if ((DLT_GET_MSIN_MSTP(message->extendedheader->msin))==DLT_TYPE_LOG)
474  {
475  /* Verbose */
476  if (DLT_IS_MSIN_VERB(message->extendedheader->msin))
477  {
478  /* 2 arguments */
479  if (message->extendedheader->noar==2)
480  {
481  /* verbose mode */
482  type_info=0;
483  type_info_tmp=0;
484  length=0;
485  length_tmp=0; /* the macro can set this variable to -1 */
486 
487  ptr = message->databuffer;
488  datalength = message->datasize;
489 
490  /* first read the type info of the first argument: must be string */
491  DLT_MSG_READ_VALUE(type_info_tmp,ptr,datalength,uint32_t);
492  type_info=DLT_ENDIAN_GET_32(message->standardheader->htyp, type_info_tmp);
493 
494  if (type_info & DLT_TYPE_INFO_SINT)
495  {
496  /* read value */
497  DLT_MSG_READ_VALUE(value_tmp,ptr,datalength,int32_t);
498  value=DLT_ENDIAN_GET_32(message->standardheader->htyp, value_tmp);
499  //printf("%d\n",value);
500 
501  if(value < dltdata->last_value)
502  {
503  if(dltdata->nvalue == dltdata->count_received_messages)
504  printf("PASSED: %d Msg received, %d not received\n",dltdata->count_received_messages,dltdata->count_not_received_messages);
505  else
506  printf("FAILED: %d Msg received, %d not received\n",dltdata->count_received_messages,dltdata->count_not_received_messages);
507 
508  dltdata->last_value = 0;
509  dltdata->count_received_messages = 0;
510  dltdata->count_not_received_messages = value -1;
511  }
512  else
513  {
514  dltdata->count_not_received_messages += value - dltdata->last_value -1;
515  }
516  dltdata->last_value = value;
517  dltdata->count_received_messages++;
518 
519  if (length>=0)
520  {
521  ptr+=length;
522  datalength-=length;
523 
524  /* read type of second argument: must be raw */
525  DLT_MSG_READ_VALUE(type_info_tmp,ptr,datalength,uint32_t);
526  type_info=DLT_ENDIAN_GET_32(message->standardheader->htyp, type_info_tmp);
527 
528  if (type_info & DLT_TYPE_INFO_RAWD)
529  {
530  /* get length of raw data block */
531  DLT_MSG_READ_VALUE(length_tmp,ptr,datalength,uint16_t);
532  length=DLT_ENDIAN_GET_16(message->standardheader->htyp, length_tmp);
533 
534  if ((length>=0) && (length==datalength))
535  {
536  //printf("Raw data found in payload, length=");
537  //printf("%d, datalength=%d \n", length, datalength);
538  dltdata->test_counter_macro[3]++;
539  }
540  }
541  }
542  }
543  }
544  }
545  }
546  }
547 
548  /* if no filter set or filter is matching display message */
549  if (dltdata->xflag)
550  {
552  }
553  else if (dltdata->mflag)
554  {
556  }
557  else if (dltdata->sflag)
558  {
559  dlt_message_print_header(message,text,sizeof(text),dltdata->vflag);
560  }
561 
562  /* if file output enabled write message */
563  if (dltdata->ovalue)
564  {
565  iov[0].iov_base = message->headerbuffer;
566  iov[0].iov_len = message->headersize;
567  iov[1].iov_base = message->databuffer;
568  iov[1].iov_len = message->datasize;
569 
570  bytes_written = writev(dltdata->ohandle, iov, 2);
571  if (0 > bytes_written){
572  printf("dlt_testclient_message_callback, error when: writev(dltdata->ohandle, iov, 2) \n");
573  return -1;
574  }
575  }
576  }
577 
578  return 0;
579 }
int32_t datasize
Definition: dlt_common.h:423
#define DLT_ENDIAN_GET_32(htyp, x)
Definition: dlt_common.h:165
int sock
Definition: dlt_client.h:92
DltReturnValue dlt_message_print_header(DltMessage *message, char *text, uint32_t size, int verbose)
Definition: dlt_common.c:3297
#define DLT_TYPE_INFO_SINT
Definition: dlt_protocol.h:150
DltStorageHeader * storageheader
Definition: dlt_common.h:432
#define DLT_TESTCLIENT_TEXTBUFSIZE
void dlt_set_id(char *id, const char *text)
Definition: dlt_common.c:324
void dlt_client_register_message_callback(int(*registerd_callback)(DltMessage *message, void *data))
Definition: dlt_client.c:105
DltReturnValue dlt_file_set_filter(DltFile *file, DltFilter *filter, int verbose)
Definition: dlt_common.c:1331
DltReturnValue dlt_client_main_loop(DltClient *client, void *data, int verbose)
Definition: dlt_client.c:326
int test_counter_macro[DLT_TESTCLIENT_NUM_TESTS]
int dlt_client_set_server_ip(DltClient *client, char *ipaddr)
Definition: dlt_client.c:839
void usage()
int dlt_testclient_message_callback(DltMessage *message, void *data)
DltReturnValue dlt_message_filter_check(DltMessage *msg, DltFilter *filter, int verbose)
Definition: dlt_common.c:1057
#define DLT_MSG_READ_VALUE(dst, src, length, type)
Definition: dlt_common.h:278
#define DLT_GET_MSIN_MSTP(msin)
Definition: dlt_protocol.h:108
DltReturnValue dlt_client_setbaudrate(DltClient *client, int baudrate)
Definition: dlt_client.c:827
DltReturnValue dlt_filter_init(DltFilter *filter, int verbose)
Definition: dlt_common.c:390
DltExtendedHeader * extendedheader
Definition: dlt_common.h:435
#define DLT_TESTCLIENT_ECU_ID
int dlt_client_set_serial_device(DltClient *client, char *serial_device)
Definition: dlt_client.c:850
#define DLT_IS_MSIN_VERB(msin)
Definition: dlt_protocol.h:107
#define DLT_ENDIAN_GET_16(htyp, x)
Definition: dlt_common.h:164
#define DLT_TESTCLIENT_NUM_TESTS
static char data[kDataSize]
Definition: city-test.cc:40
DltReturnValue dlt_set_storageheader(DltStorageHeader *storageheader, const char *ecu)
Definition: dlt_common.c:2315
#define DLT_IS_HTYP_UEH(htyp)
Definition: dlt_protocol.h:89
DltReturnValue dlt_client_init(DltClient *client, int verbose)
Definition: dlt_client.c:133
DltReturnValue dlt_filter_free(DltFilter *filter, int verbose)
Definition: dlt_common.c:404
DltReturnValue dlt_client_cleanup(DltClient *client, int verbose)
Definition: dlt_client.c:301
uint32_t dlt_uptime(void)
Definition: dlt_common.c:3274
DltReturnValue dlt_client_connect(DltClient *client, int verbose)
Definition: dlt_client.c:168
DltReturnValue dlt_filter_load(DltFilter *filter, const char *filename, int verbose)
Definition: dlt_common.c:416
int32_t headersize
Definition: dlt_common.h:422
int test_counter_function[DLT_TESTCLIENT_NUM_TESTS]
uint8_t headerbuffer[sizeof(DltStorageHeader)+sizeof(DltStandardHeader)+sizeof(DltStandardHeaderExtra)+sizeof(DltExtendedHeader)]
Definition: dlt_common.h:427
#define DLT_IS_HTYP_WEID(htyp)
Definition: dlt_protocol.h:91
DltStandardHeaderExtra headerextra
Definition: dlt_common.h:434
DltReturnValue dlt_file_init(DltFile *file, int verbose)
Definition: dlt_common.c:1305
#define DLT_TYPE_LOG
Definition: dlt_protocol.h:114
DltReturnValue dlt_file_free(DltFile *file, int verbose)
Definition: dlt_common.c:1942
DltStandardHeader * standardheader
Definition: dlt_common.h:433
#define DLT_TYPE_INFO_RAWD
Definition: dlt_protocol.h:155
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
DltReturnValue dlt_message_print_hex(DltMessage *message, char *text, uint32_t size, int verbose)
Definition: dlt_common.c:3310
char * servIP
Definition: dlt_client.h:93
uint8_t * databuffer
Definition: dlt_common.h:428
char * serialDevice
Definition: dlt_client.h:95
int main(int argc, char *argv[])
DltClientMode mode
Definition: dlt_client.h:99
DltReturnValue dlt_message_print_mixed_plain(DltMessage *message, char *text, uint32_t size, int verbose)
Definition: dlt_common.c:3340