automotive-dlt
dlt-example-user-func.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-func.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 ** 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 
83 
87 void usage()
88 {
89  char version[255];
90 
91  dlt_get_version(version,255);
92 
93  printf("Usage: dlt-example-user-func [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 }
103 
107 int main(int argc, char* argv[])
108 {
109  int gflag = 0;
110  int aflag = 0;
111  char *dvalue = 0;
112  char *fvalue = 0;
113  char *nvalue = 0;
114  char *message = 0;
115 
116  int index;
117  int c;
118  char *text;
119  int num,maxnum;
120  int delay;
121 
122  opterr = 0;
123 
124  while ((c = getopt (argc, argv, "vgad:f:n:")) != -1)
125  {
126  switch (c)
127  {
128  case 'g':
129  {
130  gflag = 1;
131  break;
132  }
133  case 'a':
134  {
135  aflag = 1;
136  break;
137  }
138  case 'd':
139  {
140  dvalue = optarg;
141  break;
142  }
143  case 'f':
144  {
145  fvalue = optarg;
146  break;
147  }
148  case 'n':
149  {
150  nvalue = optarg;
151  break;
152  }
153  case '?':
154  {
155  if (optopt == 'd' || optopt == 'f' || optopt == 'n')
156  {
157  fprintf (stderr, "Option -%c requires an argument.\n", optopt);
158  }
159  else if (isprint (optopt))
160  {
161  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
162  }
163  else
164  {
165  fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
166  }
167 
168  /* unknown or wrong option used, show usage information and terminate */
169  usage();
170  return -1;
171  }
172  default:
173  {
174  abort ();
175  return -1;//for parasoft
176  }
177  }
178  }
179 
180  for (index = optind; index < argc; index++)
181  {
182  message = argv[index];
183  }
184 
185  if (message == 0)
186  {
187  /* no message, show usage and terminate */
188  fprintf(stderr,"ERROR: No message selected\n");
189  usage();
190  return -1;
191  }
192 
193  if (fvalue)
194  {
195  /* DLT is intialised automatically, except another output target will be used */
196  if (dlt_init_file(fvalue)<0) /* log to file */
197  {
198  return -1;
199  }
200  }
201 
202  dlt_register_app("LOG","Test Application for Logging");
203 
204  dlt_register_context(&mycontext,"TEST","Test Context for Logging");
205 
207 
208  text = message;
209 
210  if (gflag)
211  {
213  }
214 
215  if (aflag)
216  {
218  }
219 
220  if (nvalue)
221  {
222  maxnum = atoi(nvalue);
223  }
224  else
225  {
226  maxnum = 10;
227  }
228 
229  if (dvalue)
230  {
231  delay = atoi(dvalue) * 1000;
232  }
233  else
234  {
235  delay = 500 * 1000;
236  }
237 
238  if (gflag)
239  {
240  /* DLT messages to test Fibex non-verbose description: dlt-example-non-verbose.xml */
241  if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,10)>0)
242  {
243  dlt_user_log_write_finish(&mycontextdata);
244  }
245  if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,11)>0)
246  {
247  dlt_user_log_write_uint16(&mycontextdata,1011);
248  dlt_user_log_write_finish(&mycontextdata);
249  }
250  if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,12)>0)
251  {
252  dlt_user_log_write_uint32(&mycontextdata,1012);
253  dlt_user_log_write_uint32(&mycontextdata,1013);
254  dlt_user_log_write_finish(&mycontextdata);
255  }
256  if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,13)>0)
257  {
258  dlt_user_log_write_uint8(&mycontextdata,123);
259  dlt_user_log_write_float32(&mycontextdata,1.12);
260  dlt_user_log_write_finish(&mycontextdata);
261  }
262  if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_INFO,14)>0)
263  {
264  dlt_user_log_write_string(&mycontextdata,"DEAD BEEF");
265  dlt_user_log_write_finish(&mycontextdata);
266  }
267  }
268 
269  for (num=0;num<maxnum;num++)
270  {
271  printf("Send %d %s\n",num,text);
272 
273  if (gflag)
274  {
275  /* Non-verbose mode */
276  if (dlt_user_log_write_start_id(&mycontext,&mycontextdata,DLT_LOG_WARN,num)>0)
277  {
278  dlt_user_log_write_int(&mycontextdata,num);
279  dlt_user_log_write_string(&mycontextdata,text);
280  dlt_user_log_write_finish(&mycontextdata);
281  }
282  }
283  else
284  {
285  /* Verbose mode */
286  if (dlt_user_log_write_start(&mycontext,&mycontextdata,DLT_LOG_WARN)>0)
287  {
288  dlt_user_log_write_int(&mycontextdata,num);
289  dlt_user_log_write_string(&mycontextdata,text);
290  dlt_user_log_write_finish(&mycontextdata);
291  }
292  }
293 
294  if (delay>0)
295  {
296  usleep(delay);
297  }
298  }
299 
300  dlt_unregister_context(&mycontext);
301 
303 
304  return 0;
305 }
306 
307 int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length)
308 {
309  char text[1024];
310 
311  printf("Injection %d, Length=%d \n",service_id,length);
312  if (length>0)
313  {
314  dlt_print_mixed_string(text,1024,data,length,0);
315  printf("%s \n", text);
316  }
317 
318  return 0;
319 }
DltReturnValue dlt_user_log_write_float32(DltContextData *log, float32_t data)
Definition: dlt_user.c:1559
DltReturnValue dlt_user_log_write_string(DltContextData *log, const char *text)
Definition: dlt_user.c:2280
DltReturnValue dlt_user_log_write_int(DltContextData *log, int data)
Definition: dlt_user.c:2062
DltReturnValue dlt_unregister_app(void)
Definition: dlt_user.c:1118
int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length)
DltReturnValue dlt_register_app(const char *appid, const char *description)
Definition: dlt_user.c:800
DltReturnValue dlt_print_mixed_string(char *text, int textlength, uint8_t *ptr, int size, int html)
Definition: dlt_common.c:153
DltReturnValue dlt_register_context(DltContext *handle, const char *contextid, const char *description)
Definition: dlt_user.c:883
DltReturnValue dlt_init_file(const char *name)
Definition: dlt_user.c:350
DltReturnValue dlt_enable_local_print(void)
Definition: dlt_user.c:3280
static char * service_id[]
Definition: dlt_common.c:85
static char data[kDataSize]
Definition: city-test.cc:40
DltReturnValue dlt_user_log_write_uint8(DltContextData *log, uint8_t data)
Definition: dlt_user.c:1682
DltReturnValue dlt_user_log_write_uint32(DltContextData *log, uint32_t data)
Definition: dlt_user.c:1752
DltReturnValue dlt_user_log_write_start_id(DltContext *handle, DltContextData *log, DltLogLevelType loglevel, uint32_t messageid)
Definition: dlt_user.c:1433
int main(int argc, char *argv[])
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
DltContext mycontext
DltReturnValue dlt_register_injection_callback(DltContext *handle, uint32_t service_id, int(*dlt_injection_callback)(uint32_t service_id, void *data, uint32_t length))
Definition: dlt_user.c:2377
DltContextData mycontextdata
DltReturnValue dlt_user_log_write_uint16(DltContextData *log, uint16_t data)
Definition: dlt_user.c:1717
DltReturnValue dlt_nonverbose_mode(void)
Definition: dlt_user.c:3195
DltReturnValue dlt_unregister_context(DltContext *handle)
Definition: dlt_user.c:1148
DltReturnValue dlt_user_log_write_finish(DltContextData *log)
Definition: dlt_user.c:1482
void usage()
DltReturnValue dlt_user_log_write_start(DltContext *handle, DltContextData *log, DltLogLevelType loglevel)
Definition: dlt_user.c:1428