automotive-dlt
dlt-example-user.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-example-user.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 #include <netdb.h>
70 #include <ctype.h>
71 #include <stdio.h> /* for printf() and fprintf() */
72 #include <stdlib.h> /* for atoi() and exit() */
73 #include <string.h> /* for memset() */
74 #include <unistd.h> /* for close() */
75 
76 #include "dlt.h"
77 #include "dlt_common.h" /* for dlt_get_version() */
78 
79 int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length);
80 void dlt_user_log_level_changed_callback(char context_id[DLT_ID_SIZE],uint8_t log_level,uint8_t trace_status);
81 
83 
84 
87 void usage()
88 {
89  char version[255];
90 
91  dlt_get_version(version,255);
92 
93  printf("Usage: dlt-example-user [options] message\n");
94  printf("Generate DLT messages and store them to file or send them to daemon.\n");
95  printf("%s \n", version);
96  printf("Options:\n");
97  printf(" -d delay Milliseconds to wait between sending messages (Default: 500)\n");
98  printf(" -f filename Use local log file instead of sending to daemon\n");
99  printf(" -n count Number of messages to be generated (Default: 10)\n");
100  printf(" -g Switch to non-verbose mode (Default: verbose mode)\n");
101  printf(" -a Enable local printing of DLT messages (Default: disabled)\n");
102  printf(" -k Send marker message\n");
103  printf(" -m mode Set log mode 0=off,1=external,2=internal,3=both\n");
104  printf(" -l level Set log level to <level>, level=-1..6\n");
105  printf(" -t timeout Set timeout when sending messages at exit, in ms (Default: 10000 = 10sec)\n");
106 #ifdef DLT_TEST_ENABLE
107  printf(" -c Corrupt user header\n");
108  printf(" -s size Corrupt message size\n");
109  printf(" -z size Size of message\n");
110 #endif /* DLT_TEST_ENABLE */
111 }
112 
116 int main(int argc, char* argv[])
117 {
118  int gflag = 0;
119  int aflag = 0;
120  int kflag = 0;
121 #ifdef DLT_TEST_ENABLE
122  int cflag = 0;
123  char *svalue = 0;
124  char *zvalue = 0;
125 #endif /* DLT_TEST_ENABLE */
126  char *dvalue = 0;
127  char *fvalue = 0;
128  char *nvalue = 0;
129  char *mvalue = 0;
130  char *message = 0;
131  int lvalue = DLT_LOG_WARN;
132  char *tvalue = 0;
133 
134  int index;
135  int c;
136 
137  char *text;
138  int num,maxnum;
139  int delay;
140 
141  int state=-1,newstate;
142 
143  opterr = 0;
144 #ifdef DLT_TEST_ENABLE
145  while ((c = getopt (argc, argv, "vgakcd:f:n:m:z:s:l:t:")) != -1)
146 #else
147  while ((c = getopt (argc, argv, "vgakd:f:n:m:l:t:")) != -1)
148 #endif /* DLT_TEST_ENABLE */
149  {
150  switch (c)
151  {
152  case 'g':
153  {
154  gflag = 1;
155  break;
156  }
157  case 'a':
158  {
159  aflag = 1;
160  break;
161  }
162  case 'k':
163  {
164  kflag = 1;
165  break;
166  }
167 #ifdef DLT_TEST_ENABLE
168  case 'c':
169  {
170  cflag = 1;
171  break;
172  }
173  case 's':
174  {
175  svalue = optarg;
176  break;
177  }
178  case 'z':
179  {
180  zvalue = optarg;
181  break;
182  }
183 #endif /* DLT_TEST_ENABLE */
184  case 'd':
185  {
186  dvalue = optarg;
187  break;
188  }
189  case 'f':
190  {
191  fvalue = optarg;
192  break;
193  }
194  case 'n':
195  {
196  nvalue = optarg;
197  break;
198  }
199  case 'm':
200  {
201  mvalue = optarg;
202  break;
203  }
204  case 'l':
205  {
206  lvalue = atoi(optarg);
207  break;
208  }
209  case 't':
210  {
211  tvalue = optarg;
212  break;
213  }
214  case '?':
215  {
216  if (optopt == 'd' || optopt == 'f' || optopt == 'n'|| optopt == 'l' || optopt == 't')
217  {
218  fprintf (stderr, "Option -%c requires an argument.\n", optopt);
219  }
220  else if (isprint (optopt))
221  {
222  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
223  }
224  else
225  {
226  fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
227  }
228 
229  /* unknown or wrong option used, show usage information and terminate */
230  usage();
231  return -1;
232  }
233  default:
234  {
235  abort ();
236  break;//for parasoft
237  }
238  }
239  }
240 
241  for (index = optind; index < argc; index++)
242  {
243  message = argv[index];
244  }
245 
246  if (message == 0)
247  {
248  /* no message, show usage and terminate */
249  fprintf(stderr,"ERROR: No message selected\n");
250  usage();
251  return -1;
252  }
253 
254  if (fvalue)
255  {
256  /* DLT is initialized automatically, except another output target will be used */
257  if (dlt_init_file(fvalue)<0) /* log to file */
258  {
259  return -1;
260  }
261  }
262 
265  dlt_with_ecu_id(1);
267 
268  DLT_REGISTER_APP("LOG","Test Application for Logging");
269  DLT_REGISTER_CONTEXT(mycontext,"TEST","Test Context for Logging");
270 
273 
274  text = message;
275 
276  if(mvalue)
277  {
278  printf("Set log mode to %d\n",atoi(mvalue));
279  dlt_set_log_mode(atoi(mvalue));
280  }
281 
282 
283  if (gflag)
284  {
286  }
287 
288  if (aflag)
289  {
291  }
292 
293  if (kflag)
294  {
295  DLT_LOG_MARKER();
296  }
297 
298  if (nvalue)
299  {
300  maxnum = atoi(nvalue);
301  }
302  else
303  {
304  maxnum = 10;
305  }
306 
307  if (dvalue)
308  {
309  delay = atoi(dvalue) * 1000;
310  }
311  else
312  {
313  delay = 500 * 1000;
314  }
315 
316  if (tvalue)
317  {
318  dlt_set_resend_timeout_atexit(atoi(tvalue));
319  }
320 
321  if (gflag)
322  {
323  /* DLT messages to test Fibex non-verbose description: dlt-example-non-verbose.xml */
328  DLT_LOG_ID(mycontext,DLT_LOG_INFO,14,DLT_STRING("DEAD BEEF"));
329  }
330 
331 #ifdef DLT_TEST_ENABLE
332  if (cflag)
333  {
334  dlt_user_test_corrupt_user_header(1);
335  }
336  if (svalue)
337  {
338  dlt_user_test_corrupt_message_size(1,atoi(svalue));
339  }
340  if (zvalue)
341  {
342  char* buffer = malloc(atoi(zvalue));
343  if(buffer==0)
344  {
345  /* no message, show usage and terminate */
346  fprintf(stderr,"Cannot allocate buffer memory!\n");
347  return -1;
348  }
349  DLT_LOG(mycontext,DLT_LOG_WARN,DLT_STRING(text),DLT_RAW(buffer,atoi(zvalue)));
350  free(buffer);
351  }
352 #endif /* DLT_TEST_ENABLE */
353 
354  for (num=0;num<maxnum;num++)
355  {
356  printf("Send %d %s\n",num,text);
357 
358  newstate = dlt_get_log_state();
359  if(state!=newstate)
360  {
361  state = newstate;
362  if(state == -1) {
363  printf("Client unknown state!\n");
364  }
365  else if(state == 0) {
366  printf("Client disconnected!\n");
367  }
368  else if(state == 1) {
369  printf("Client connected!\n");
370  }
371  }
372 
373  if (gflag)
374  {
375  /* Non-verbose mode */
376  DLT_LOG_ID(mycontext,lvalue,num,DLT_INT(num),DLT_STRING(text));
377  }
378  else
379  {
380  /* Verbose mode */
381  DLT_LOG(mycontext,lvalue,DLT_INT(num),DLT_STRING(text));
382  }
383 
384  if (delay>0)
385  {
386  usleep(delay);
387  }
388  }
389 
390  sleep(1);
391 
393 
395 
396  return 0;
397 
398 }
399 
400 int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length)
401 {
402  char text[1024];
403  DLT_LOG(mycontext, DLT_LOG_INFO, DLT_STRING("Injection: "), DLT_UINT32(service_id));
404  printf("Injection %d, Length=%d \n",service_id,length);
405  if (length>0)
406  {
407  dlt_print_mixed_string(text,1024,data,length,0);
408  DLT_LOG(mycontext, DLT_LOG_INFO, DLT_STRING("Data: "), DLT_STRING(text));
409  printf("%s \n", text);
410  }
411 
412  return 0;
413 }
414 
415 void dlt_user_log_level_changed_callback(char context_id[DLT_ID_SIZE],uint8_t log_level,uint8_t trace_status)
416 {
417  char text[5];
418  text[4]=0;
419 
420  memcpy(text,context_id,DLT_ID_SIZE);
421 
422  printf("Log level changed of context %s, LogLevel=%u, TraceState=%u \n",text,log_level,trace_status);
423 }
424 
#define DLT_RAW(BUF, LEN)
int dlt_set_resend_timeout_atexit(uint32_t timeout_in_milliseconds)
Definition: dlt_user.c:1301
#define DLT_UNREGISTER_APP()
DltReturnValue dlt_verbose_mode(void)
Definition: dlt_user.c:3178
#define DLT_ID_SIZE
Definition: dlt_common.h:204
#define DLT_UINT16(UINT_VAR)
DLT_DECLARE_CONTEXT(mycontext)
void usage()
Definition: dlt-control.c:190
#define DLT_NONVERBOSE_MODE()
#define DLT_REGISTER_INJECTION_CALLBACK(CONTEXT, SERVICEID, CALLBACK)
#define DLT_INT(INT_VAR)
DltReturnValue dlt_print_mixed_string(char *text, int textlength, uint8_t *ptr, int size, int html)
Definition: dlt_common.c:153
DltReturnValue dlt_with_session_id(int8_t with_session_id)
Definition: dlt_user.c:3229
DltReturnValue dlt_init_file(const char *name)
Definition: dlt_user.c:350
#define DLT_LOG_MARKER()
int dlt_get_log_state()
Definition: dlt_user.c:1276
#define DLT_FLOAT32(FLOAT32_VAR)
void dlt_user_log_level_changed_callback(char context_id[DLT_ID_SIZE], uint8_t log_level, uint8_t trace_status)
static char * service_id[]
Definition: dlt_common.c:85
static char data[kDataSize]
Definition: city-test.cc:40
#define DLT_LOG_ID(CONTEXT, LOGLEVEL, MSGID, ARGS...)
#define DLT_REGISTER_APP(APPID, DESCRIPTION)
#define DLT_STRING(TEXT)
unsigned char buffer[BUFFER_SIZE]
Buffer for dlt file transfer. The size is defined by BUFFER_SIZE.
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
#define DLT_UNREGISTER_CONTEXT(CONTEXT)
#define DLT_UINT8(UINT_VAR)
#define DLT_ENABLE_LOCAL_PRINT()
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length)
DltReturnValue dlt_with_timestamp(int8_t with_timestamp)
Definition: dlt_user.c:3246
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
int main(int argc, char *argv[])
DltContext mycontext
DltReturnValue dlt_set_log_mode(DltUserLogMode mode)
Definition: dlt_user.c:1281
#define DLT_UINT32(UINT_VAR)
#define DLT_REGISTER_LOG_LEVEL_CHANGED_CALLBACK(CONTEXT, CALLBACK)
DltReturnValue dlt_with_ecu_id(int8_t with_ecu_id)
Definition: dlt_user.c:3263