automotive-dlt
dlt-example-user-common-api.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 
28 /*******************************************************************************
29 ** **
30 ** SRC-MODULE: dlt-example-common-api.c **
31 ** **
32 ** TARGET : linux **
33 ** **
34 ** PROJECT : DLT **
35 ** **
36 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
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 *******************************************************************************/
56 
57 #include <netdb.h>
58 #include <ctype.h>
59 #include <stdio.h> /* for printf() and fprintf() */
60 #include <stdlib.h> /* for atoi() and exit() */
61 #include <string.h> /* for memset() */
62 #include <unistd.h> /* for close() */
63 
64 #include "dlt_common_api.h"
65 
67 
68 
71 void usage()
72 {
73  char version[255];
74 
75  dlt_get_version(version,255);
76 
77  printf("Usage: dlt-example-common-api [options] message\n");
78  printf("Generate DLT messages and store them to file or send them to daemon.\n");
79  printf("%s \n", version);
80  printf("Options:\n");
81  printf(" -d delay Milliseconds to wait between sending messages (Default: 500)\n");
82  printf(" -f filename Use local log file instead of sending to daemon\n");
83  printf(" -n count Number of messages to be generated (Default: 10)\n");
84  printf(" -g Switch to non-verbose mode (Default: verbose mode)\n");
85  printf(" -a Enable local printing of DLT messages (Default: disabled)\n");
86  printf(" -m mode Set log mode 0=off,1=external,2=internal,3=both\n");
87 #ifdef DLT_TEST_ENABLE
88  printf(" -c Corrupt user header\n");
89  printf(" -s size Corrupt message size\n");
90  printf(" -z size Size of message\n");
91 #endif /* DLT_TEST_ENABLE */
92 }
93 
97 int main(int argc, char* argv[])
98 {
99 #ifdef DLT_TEST_ENABLE
100  int cflag = 0;
101  char *svalue = 0;
102  char *zvalue = 0;
103 #endif /* DLT_TEST_ENABLE */
104  int gflag = 0;
105  char *dvalue = 0;
106  char *nvalue = 0;
107  char *message = 0;
108 
109  int index;
110  int c;
111 
112  char *text;
113  int num,maxnum;
114  int delay;
115 
116  int state=-1,newstate;
117 
118  opterr = 0;
119 #ifdef DLT_TEST_ENABLE
120  while ((c = getopt (argc, argv, "vgcd:n:z:s:")) != -1)
121 #else
122  while ((c = getopt (argc, argv, "vgd:n:")) != -1)
123 #endif /* DLT_TEST_ENABLE */
124  {
125  switch (c)
126  {
127 #ifdef DLT_TEST_ENABLE
128  case 'c':
129  {
130  cflag = 1;
131  break;
132  }
133  case 's':
134  {
135  svalue = optarg;
136  break;
137  }
138  case 'z':
139  {
140  zvalue = optarg;
141  break;
142  }
143 #endif /* DLT_TEST_ENABLE */
144  case 'g':
145  {
146  gflag = 1;
147  break;
148  }
149  case 'd':
150  {
151  dvalue = optarg;
152  break;
153  }
154  case 'n':
155  {
156  nvalue = optarg;
157  break;
158  }
159  case '?':
160  {
161  if (optopt == 'd' || optopt == 'f' || optopt == 'n')
162  {
163  fprintf (stderr, "Option -%c requires an argument.\n", optopt);
164  }
165  else if (isprint (optopt))
166  {
167  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
168  }
169  else
170  {
171  fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
172  }
173 
174  /* unknown or wrong option used, show usage information and terminate */
175  usage();
176  return -1;
177  }
178  default:
179  {
180  abort ();
181  break;//for parasoft
182  }
183  }
184  }
185 
186  for (index = optind; index < argc; index++)
187  {
188  message = argv[index];
189  }
190 
191  if (message == 0)
192  {
193  /* no message, show usage and terminate */
194  fprintf(stderr,"ERROR: No message selected\n");
195  usage();
196  return -1;
197  }
198 
199  DLT_REGISTER_APP("LOG","Test Application for Logging");
200  DLT_REGISTER_CONTEXT_APP(mycontext,"TEST","LOG","Test Context for Logging");
201 
202  text = message;
203 
204  if (nvalue)
205  {
206  maxnum = atoi(nvalue);
207  }
208  else
209  {
210  maxnum = 10;
211  }
212 
213  if (dvalue)
214  {
215  delay = atoi(dvalue) * 1000;
216  }
217  else
218  {
219  delay = 500 * 1000;
220  }
221 
222  if (gflag)
223  {
224  /* DLT messages to test Fibex non-verbose description: dlt-example-non-verbose.xml */
229  DLT_LOG_ID1(mycontext,DLT_LOG_INFO,14,DLT_STRING("DEAD BEEF"));
230  }
231 
232 #ifdef DLT_TEST_ENABLE
233  if (cflag)
234  {
235  dlt_user_test_corrupt_user_header(1);
236  }
237  if (svalue)
238  {
239  dlt_user_test_corrupt_message_size(1,atoi(svalue));
240  }
241  if (zvalue)
242  {
243  char* buffer = malloc(atoi(zvalue));
244  if(buffer==0)
245  {
246  /* no message, show usage and terminate */
247  fprintf(stderr,"Cannot allocate buffer memory!\n");
248  return -1;
249  }
250  DLT_LOG2(mycontext,DLT_LOG_WARN,DLT_STRING(text),DLT_RAW(buffer,atoi(zvalue)));
251  free(buffer);
252  }
253 #endif /* DLT_TEST_ENABLE */
254 
255  for (num=0;num<maxnum;num++)
256  {
257  printf("Send %d %s\n",num,text);
258 
259  newstate = dlt_get_log_state();
260  if(state!=newstate)
261  {
262  state = newstate;
263  if(state == -1) {
264  printf("Client unknown state!\n");
265  }
266  else if(state == 0) {
267  printf("Client disconnected!\n");
268  }
269  else if(state == 1) {
270  printf("Client connected!\n");
271  }
272  }
273 
274  if (gflag)
275  {
276  /* Non-verbose mode */
278  }
279  else
280  {
281  /* Verbose mode */
283  }
284 
285  if (delay>0)
286  {
287  usleep(delay);
288  }
289  }
290 
291  sleep(1);
292 
294 
296 
297  return 0;
298 
299 }
300 
301 
#define DLT_RAW(BUF, LEN)
#define DLT_REGISTER_CONTEXT_APP(CONTEXT, CONTEXTID, APPID, DESCRIPTION)
DLT_DECLARE_CONTEXT(mycontext)
#define DLT_UNREGISTER_APP()
#define DLT_UINT16(UINT_VAR)
#define DLT_LOG_ID2(CONTEXT, LOGLEVEL, MSGID, ARGS1, ARGS2)
void usage()
Definition: dlt-control.c:190
#define DLT_INT(INT_VAR)
int dlt_get_log_state()
Definition: dlt_user.c:1276
#define DLT_FLOAT32(FLOAT32_VAR)
#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_LOG2(CONTEXT, LOGLEVEL, ARGS1, ARGS2)
#define DLT_UNREGISTER_CONTEXT(CONTEXT)
#define DLT_UINT8(UINT_VAR)
#define DLT_LOG_ID1(CONTEXT, LOGLEVEL, MSGID, ARGS1)
int main(int argc, char *argv[])
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
DltContext mycontext
#define DLT_UINT32(UINT_VAR)
#define DLT_LOG_ID0(CONTEXT, LOGLEVEL, MSGID)