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