3 * Copyright (C) 2012 BMW AG
5 * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
7 * Contributions are licensed to the GENIVI Alliance under one or more
8 * Contribution License Agreements.
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/.
16 * \author Alexander Wenzel <alexander.aw.wenzel@bmw.de> BMW 2011-2012
18 * \file dlt-example-user.c
19 * For further information see http://www.genivi.org/.
24 /*******************************************************************************
26 ** SRC-MODULE: dlt-example-user.c **
32 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
39 ** PLATFORM DEPENDANT [yes/no]: yes **
41 ** TO BE CHANGED BY USER [yes/no]: no **
43 *******************************************************************************/
45 /*******************************************************************************
47 ********************************************************************************
49 ** Initials Name Company **
50 ** -------- ------------------------- ---------------------------------- **
51 ** aw Alexander Wenzel BMW **
52 ** mk Markus Klein Fraunhofer ESK **
53 *******************************************************************************/
55 /*******************************************************************************
56 ** Revision Control History **
57 *******************************************************************************/
60 * $LastChangedRevision: 1670 $
61 * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
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() */
74 #include "dlt_common.h" /* for dlt_get_version() */
76 int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length);
78 DLT_DECLARE_CONTEXT(mycontext);
81 * Print usage information of tool.
87 dlt_get_version(version);
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);
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 */
108 * Main function of tool.
110 int main(int argc, char* argv[])
115 #ifdef DLT_TEST_ENABLE
119 #endif /* DLT_TEST_ENABLE */
133 int state=-1,newstate;
136 #ifdef DLT_TEST_ENABLE
137 while ((c = getopt (argc, argv, "vgacd:f:n:m:z:s:")) != -1)
139 while ((c = getopt (argc, argv, "vgad:f:n:m:")) != -1)
140 #endif /* DLT_TEST_ENABLE */
159 #ifdef DLT_TEST_ENABLE
175 #endif /* DLT_TEST_ENABLE */
198 if (optopt == 'd' || optopt == 'f' || optopt == 'n')
200 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
202 else if (isprint (optopt))
204 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
208 fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
211 /* unknown or wrong option used, show usage information and terminate */
222 for (index = optind; index < argc; index++)
224 message = argv[index];
229 /* no message, show usage and terminate */
230 fprintf(stderr,"ERROR: No message selected\n");
237 /* DLT is intialised automatically, except another output target will be used */
238 if (dlt_init_file(fvalue)<0) /* log to file */
244 DLT_REGISTER_APP("LOG","Test Application for Logging");
245 DLT_REGISTER_CONTEXT(mycontext,"TEST","Test Context for Logging");
247 DLT_REGISTER_INJECTION_CALLBACK(mycontext, 0xFFF, dlt_user_injection_callback);
253 printf("Set log mode to %d\n",atoi(mvalue));
254 dlt_set_log_mode(atoi(mvalue));
260 DLT_NONVERBOSE_MODE();
265 DLT_ENABLE_LOCAL_PRINT();
270 maxnum = atoi(nvalue);
279 delay = atoi(dvalue) * 1000;
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"));
296 #ifdef DLT_TEST_ENABLE
299 dlt_user_test_corrupt_user_header(1);
303 dlt_user_test_corrupt_message_size(1,atoi(svalue));
307 char* buffer = malloc(atoi(zvalue));
308 DLT_LOG(mycontext,DLT_LOG_WARN,DLT_STRING(text),DLT_RAW(buffer,atoi(zvalue)));
311 #endif /* DLT_TEST_ENABLE */
313 for (num=0;num<maxnum;num++)
315 printf("Send %d %s\n",num,text);
317 newstate = dlt_get_log_state();
322 printf("Client unknown state!\n");
324 else if(state == 0) {
325 printf("Client disconnected!\n");
327 else if(state == 1) {
328 printf("Client connected!\n");
334 /* Non-verbose mode */
335 DLT_LOG_ID(mycontext,DLT_LOG_WARN,num,DLT_INT(num),DLT_STRING(text));
340 DLT_LOG(mycontext,DLT_LOG_WARN,DLT_INT(num),DLT_STRING(text));
351 DLT_UNREGISTER_CONTEXT(mycontext);
353 DLT_UNREGISTER_APP();
361 int dlt_user_injection_callback(uint32_t service_id, void *data, uint32_t length)
365 printf("Injection %d, Length=%d \n",service_id,length);
368 dlt_print_mixed_string(text,1024,data,length,0);
369 printf("%s \n", text);