Modified library for new test cases to corrupt data - related to the bug fix for...
[profile/ivi/dlt-daemon.git] / src / examples / dlt-example-user.c
1 /*
2  * Dlt Client console utilities - Diagnostic Log and Trace
3  * @licence app begin@
4  *
5  * Copyright (C) 2011, BMW AG - Alexander Wenzel <alexander.wenzel@bmw.de>
6  * 
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.
12  * 
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>.
15  * 
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.
18  * 
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.
23  * 
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.
29  * 
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.
34  *
35  * @licence end@
36  */
37
38
39 /*******************************************************************************
40 **                                                                            **
41 **  SRC-MODULE: dlt-example-user.c                                            **
42 **                                                                            **
43 **  TARGET    : linux                                                         **
44 **                                                                            **
45 **  PROJECT   : DLT                                                           **
46 **                                                                            **
47 **  AUTHOR    : Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
48 **              Markus Klein                                                  **
49 **                                                                            **
50 **  PURPOSE   :                                                               **
51 **                                                                            **
52 **  REMARKS   :                                                               **
53 **                                                                            **
54 **  PLATFORM DEPENDANT [yes/no]: yes                                          **
55 **                                                                            **
56 **  TO BE CHANGED BY USER [yes/no]: no                                        **
57 **                                                                            **
58 *******************************************************************************/
59
60 /*******************************************************************************
61 **                      Author Identity                                       **
62 ********************************************************************************
63 **                                                                            **
64 ** Initials     Name                       Company                            **
65 ** --------     -------------------------  ---------------------------------- **
66 **  aw          Alexander Wenzel           BMW                                **
67 **  mk          Markus Klein               Fraunhofer ESK                     **
68 *******************************************************************************/
69
70 /*******************************************************************************
71 **                      Revision Control History                              **
72 *******************************************************************************/
73
74 /*
75  * $LastChangedRevision: 1670 $
76  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
77  * $LastChangedBy$
78  Initials    Date         Comment
79  aw          13.01.2010   initial
80  */
81 #include <netdb.h>
82 #include <ctype.h>
83 #include <stdio.h>      /* for printf() and fprintf() */
84 #include <stdlib.h>     /* for atoi() and exit() */
85 #include <string.h>     /* for memset() */
86 #include <unistd.h>     /* for close() */
87
88 #include "dlt.h"
89 #include "dlt_common.h" /* for dlt_get_version() */
90
91 int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length);
92
93 DLT_DECLARE_CONTEXT(mycontext);
94
95 /**
96  * Print usage information of tool.
97  */
98 void usage()
99 {
100     char version[255];
101
102     dlt_get_version(version);
103
104     printf("Usage: dlt-example-user [options] message\n");
105     printf("Generate DLT messages and store them to file or send them to daemon.\n");
106     printf("%s \n", version);
107     printf("Options:\n");
108     printf("  -v            Verbose mode\n");
109     printf("  -d delay      Milliseconds to wait between sending messages (Default: 500)\n");
110     printf("  -f filename   Use local log file instead of sending to daemon\n");
111     printf("  -n count      Number of messages to be generated (Default: 10)\n");
112     printf("  -g            Switch to non-verbose mode (Default: verbose mode)\n");
113     printf("  -a            Enable local printing of DLT messages (Default: disabled)\n");
114     printf("  -m mode       Set log mode 0=off,1=external,2=internal,3=both\n");
115 #ifdef DLT_TEST_ENABLE
116     printf("  -c                Corrupt user header\n");
117     printf("  -s size       Corrupt message size\n");
118     printf("  -z size           Size of message\n");
119 #endif /* DLT_TEST_ENABLE */
120 }
121
122 /**
123  * Main function of tool.
124  */
125 int main(int argc, char* argv[])
126 {
127     int vflag = 0;
128     int gflag = 0;
129     int aflag = 0;
130 #ifdef DLT_TEST_ENABLE
131     int cflag = 0;    
132     char *svalue = 0;
133     char *zvalue = 0;
134 #endif /* DLT_TEST_ENABLE */
135     char *dvalue = 0;
136     char *fvalue = 0;
137     char *nvalue = 0;
138     char *mvalue = 0;
139     char *message = 0;
140
141     int index;
142     int c;
143
144         char *text;
145         int num,maxnum;
146         int delay;
147         
148         int state=-1,newstate;
149
150     opterr = 0;
151 #ifdef DLT_TEST_ENABLE
152     while ((c = getopt (argc, argv, "vgacd:f:n:m:z:s:")) != -1)
153 #else
154     while ((c = getopt (argc, argv, "vgad:f:n:m:")) != -1)
155 #endif /* DLT_TEST_ENABLE */
156     {
157         switch (c)
158         {
159         case 'v':
160         {
161             vflag = 1;
162             break;
163         }
164         case 'g':
165         {
166             gflag = 1;
167             break;
168         }
169         case 'a':
170         {
171             aflag = 1;
172             break;
173         }
174 #ifdef DLT_TEST_ENABLE
175         case 'c':
176         {
177             cflag = 1;
178             break;
179         }
180         case 's':
181         {
182             svalue = optarg;
183             break;
184         }
185         case 'z':
186         {
187             zvalue = optarg;
188             break;
189         }
190 #endif /* DLT_TEST_ENABLE */
191         case 'd':
192         {
193             dvalue = optarg;
194             break;
195         }
196         case 'f':
197         {
198             fvalue = optarg;
199             break;
200         }
201         case 'n':
202         {
203             nvalue = optarg;
204             break;
205         }
206         case 'm':
207         {
208             mvalue = optarg;
209             break;
210         }
211         case '?':
212         {
213             if (optopt == 'd' || optopt == 'f' || optopt == 'n')
214             {
215                 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
216             }
217             else if (isprint (optopt))
218             {
219                 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
220             }
221             else
222             {
223                 fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
224             }
225
226             /* unknown or wrong option used, show usage information and terminate */
227             usage();
228             return -1;
229         }
230         default:
231         {
232             abort ();
233         }
234         }
235     }
236
237     for (index = optind; index < argc; index++)
238     {
239         message = argv[index];
240     }
241
242     if (message == 0)
243     {
244         /* no message, show usage and terminate */
245         fprintf(stderr,"ERROR: No message selected\n");
246         usage();
247         return -1;
248     }
249
250     if (fvalue)
251     {
252         /* DLT is intialised automatically, except another output target will be used */
253         if (dlt_init_file(fvalue)<0) /* log to file */
254         {
255             return -1;
256         }
257     }
258
259     DLT_REGISTER_APP("LOG","Test Application for Logging");
260     DLT_REGISTER_CONTEXT(mycontext,"TEST","Test Context for Logging");
261
262     DLT_REGISTER_INJECTION_CALLBACK(mycontext, 0xFFF, dlt_user_injection_callback);
263
264     text = message;
265
266         if(mvalue)
267         {
268         printf("Set log mode to %d\n",atoi(mvalue));
269                 dlt_set_log_mode(atoi(mvalue)); 
270         }
271         
272
273     if (gflag)
274     {
275         DLT_NONVERBOSE_MODE();
276     }
277
278     if (aflag)
279     {
280         DLT_ENABLE_LOCAL_PRINT();
281     }
282
283     if (nvalue)
284     {
285         maxnum = atoi(nvalue);
286     }
287     else
288     {
289                 maxnum = 10;
290     }
291
292     if (dvalue)
293     {
294         delay = atoi(dvalue) * 1000;
295     }
296     else
297     {
298         delay = 500 * 1000;
299     }
300
301     if (gflag)
302     {
303         /* DLT messages to test Fibex non-verbose description: dlt-example-non-verbose.xml */
304         DLT_LOG_ID(mycontext,DLT_LOG_INFO,10);
305         DLT_LOG_ID(mycontext,DLT_LOG_INFO,11,DLT_UINT16(1011));
306         DLT_LOG_ID(mycontext,DLT_LOG_INFO,12,DLT_UINT32(1012),DLT_UINT32(1013));
307         DLT_LOG_ID(mycontext,DLT_LOG_INFO,13,DLT_UINT8(123),DLT_FLOAT32(1.12));
308         DLT_LOG_ID(mycontext,DLT_LOG_INFO,14,DLT_STRING("DEAD BEEF"));
309     }
310
311 #ifdef DLT_TEST_ENABLE
312     if (cflag)
313     {
314                 dlt_user_test_corrupt_user_header(1);
315     }
316     if (svalue)
317     {
318                 dlt_user_test_corrupt_message_size(1,atoi(svalue));
319     }
320         if (zvalue)
321         {
322                 char* buffer = malloc(atoi(zvalue));
323         DLT_LOG(mycontext,DLT_LOG_WARN,DLT_STRING(text),DLT_RAW(buffer,atoi(zvalue)));          
324                 free(buffer);
325         }
326 #endif /* DLT_TEST_ENABLE */
327
328     for (num=0;num<maxnum;num++)
329     {
330         printf("Send %d %s\n",num,text);
331
332                 newstate = dlt_get_log_state();
333                 if(state!=newstate)
334                 {
335                         state = newstate;
336                         if(state == -1) {
337                                 printf("Client unknown state!\n");
338                         }
339                         else if(state == 0) {
340                                 printf("Client disconnected!\n");
341                         }
342                         else if(state == 1) {
343                                 printf("Client connected!\n");
344                         }
345                 }
346                                 
347         if (gflag)
348         {
349             /* Non-verbose mode */
350             DLT_LOG_ID(mycontext,DLT_LOG_WARN,num,DLT_INT(num),DLT_STRING(text));
351         }
352         else
353         {
354             /* Verbose mode */
355             DLT_LOG(mycontext,DLT_LOG_WARN,DLT_INT(num),DLT_STRING(text));
356         }
357
358         if (delay>0)
359         {
360             usleep(delay);
361         }
362     }
363
364     sleep(1);
365
366     DLT_UNREGISTER_CONTEXT(mycontext);
367
368     DLT_UNREGISTER_APP();
369
370     dlt_free();
371
372     return 0;
373
374 }
375
376 int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length)
377 {
378     char text[1024];
379
380     printf("Injection %d, Length=%d \n",service_id,length);
381     if (length>0)
382     {
383         dlt_print_mixed_string(text,1024,data,length,0);
384         printf("%s \n", text);
385     }
386
387     return 0;
388 }
389