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.wenzel@bmw.de> BMW 2011-2012
19 * For further information see http://www.genivi.org/.
24 /*******************************************************************************
26 ** SRC-MODULE: dlt_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) $
67 #include <stdlib.h> /* for getenv(), free(), atexit() */
68 #include <string.h> /* for strcmp(), strncmp(), strlen(), memset(), memcpy() */
69 #include <signal.h> /* for signal(), SIGPIPE, SIG_IGN */
71 #if !defined (__WIN32__)
72 #include <syslog.h> /* for LOG_... */
73 #include <semaphore.h>
74 #include <pthread.h> /* POSIX Threads */
81 #include <sys/uio.h> /* writev() */
84 #include "dlt_user_shared.h"
85 #include "dlt_user_shared_cfg.h"
86 #include "dlt_user_cfg.h"
88 static DltUser dlt_user;
89 static int dlt_user_initialised = 0;
91 static char str[DLT_USER_BUFFER_LENGTH];
93 static sem_t dlt_mutex;
94 static pthread_t dlt_receiverthread_handle;
95 static pthread_attr_t dlt_receiverthread_attr;
97 /* Function prototypes for internally used functions */
98 static void dlt_user_receiverthread_function(void *ptr);
99 static void dlt_user_atexit_handler(void);
100 static int dlt_user_log_init(DltContext *handle, DltContextData *log);
101 static int dlt_user_log_send_log(DltContextData *log, int mtype);
102 static int dlt_user_log_send_register_application(void);
103 static int dlt_user_log_send_unregister_application(void);
104 static int dlt_user_log_send_register_context(DltContextData *log);
105 static int dlt_user_log_send_unregister_context(DltContextData *log);
106 static int dlt_send_app_ll_ts_limit(const char *appid, DltLogLevelType loglevel, DltTraceStatusType tracestatus);
107 static int dlt_user_log_send_log_mode(DltUserLogMode mode);
108 static int dlt_user_print_msg(DltMessage *msg, DltContextData *log);
109 static int dlt_user_log_check_user_message(void);
110 static int dlt_user_log_resend_buffer(void);
111 static void dlt_user_log_reattach_to_daemon(void);
112 static int dlt_user_log_send_overflow(void);
114 int dlt_user_check_library_version(const char *user_major_version,const char *user_minor_version){
117 char lib_major_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
118 char lib_minor_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
120 dlt_get_major_version( lib_major_version);
121 dlt_get_minor_version( lib_minor_version);
123 if( (strcmp(lib_major_version,user_major_version)!=0) || (strcmp(lib_minor_version,user_minor_version)!=0))
125 sprintf(str,"DLT Library version check failed! Installed DLT library version is %s.%s - Application using DLT library version %s.%s\n",lib_major_version,lib_minor_version,user_major_version,user_minor_version);
126 dlt_log(LOG_WARNING, str);
134 char filename[DLT_USER_MAX_FILENAME_LENGTH];
137 dlt_user_initialised = 1;
139 /* Initialize common part of dlt_init()/dlt_init_file() */
140 if (dlt_init_common()==-1)
142 dlt_user_initialised = 0;
146 dlt_user.dlt_is_file = 0;
147 dlt_user.overflow = 0;
148 #ifdef DLT_SHM_ENABLE
149 memset(&(dlt_user.dlt_shm),0,sizeof(DltShm));
152 /* create and open DLT user FIFO */
153 sprintf(filename,"%s/dlt%d",DLT_USER_DIR,getpid());
155 /* Try to delete existing pipe, ignore result of unlink */
158 ret=mkfifo(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
161 sprintf(str,"Loging disabled, FIFO user %s cannot be created!\n",filename);
162 dlt_log(LOG_WARNING, str);
163 /* return 0; */ /* removed to prevent error, when FIFO already exists */
166 dlt_user.dlt_user_handle = open(filename, O_RDWR);
167 if (dlt_user.dlt_user_handle == -1)
169 sprintf(str,"Loging disabled, FIFO user %s cannot be opened!\n",filename);
170 dlt_log(LOG_WARNING, str);
175 /* open DLT output FIFO */
176 dlt_user.dlt_log_handle = open(DLT_USER_FIFO, O_WRONLY | O_NONBLOCK);
177 if (dlt_user.dlt_log_handle==-1)
179 sprintf(str,"Loging disabled, FIFO %s cannot be opened with open()!\n",DLT_USER_FIFO);
180 dlt_log(LOG_WARNING, str);
185 #ifdef DLT_SHM_ENABLE
186 /* init shared memory */
187 if (dlt_shm_init_client(&(dlt_user.dlt_shm),DLT_SHM_KEY) < 0)
189 sprintf(str,"Loging disabled, Shared memory %d cannot be created!\n",DLT_SHM_KEY);
190 dlt_log(LOG_WARNING, str);
197 if (dlt_receiver_init(&(dlt_user.receiver),dlt_user.dlt_user_handle, DLT_USER_RCVBUF_MAX_SIZE)==-1)
199 dlt_user_initialised = 0;
203 /* Start receiver thread */
204 if (pthread_create(&(dlt_receiverthread_handle),
206 (void *) &dlt_user_receiverthread_function,
209 if (pthread_attr_destroy(&dlt_receiverthread_attr)!=0)
211 dlt_log(LOG_WARNING, "Can't destroy thread attributes!\n");
214 dlt_log(LOG_CRIT, "Can't create receiver thread!\n");
215 dlt_user_initialised = 0;
219 if (pthread_attr_destroy(&dlt_receiverthread_attr)!=0)
221 dlt_log(LOG_WARNING, "Can't destroy thread attributes!\n");
227 int dlt_init_file(const char *name)
229 dlt_user_initialised = 1;
231 /* Initialize common part of dlt_init()/dlt_init_file() */
232 if (dlt_init_common()==-1)
234 dlt_user_initialised = 0;
238 dlt_user.dlt_is_file = 1;
240 /* open DLT output file */
241 dlt_user.dlt_log_handle = open(name,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
242 if (dlt_user.dlt_log_handle == -1)
244 sprintf(str,"Log file %s cannot be opened!\n",name);
245 dlt_log(LOG_ERR, str);
252 int dlt_init_common(void)
254 char *env_local_print;
256 /* Binary semaphore for threads */
257 if (sem_init(&dlt_mutex, 0, 1)==-1)
259 dlt_user_initialised = 0;
263 /* set to unknown state of connected client */
264 dlt_user.log_state = -1;
266 dlt_user.dlt_log_handle=-1;
267 dlt_user.dlt_user_handle=-1;
269 dlt_set_id(dlt_user.ecuID,DLT_USER_DEFAULT_ECU_ID);
270 dlt_set_id(dlt_user.appID,"");
272 dlt_user.application_description = 0;
274 /* Verbose mode is enabled by default */
275 dlt_user.verbose_mode = 1;
277 /* Local print is disabled by default */
278 dlt_user.enable_local_print = 0;
280 dlt_user.local_print_mode = DLT_PM_UNSET;
282 env_local_print = getenv(DLT_USER_ENV_LOCAL_PRINT_MODE);
285 if (strcmp(env_local_print,"AUTOMATIC")==0)
287 dlt_user.local_print_mode = DLT_PM_AUTOMATIC;
289 else if (strcmp(env_local_print,"FORCE_ON")==0)
291 dlt_user.local_print_mode = DLT_PM_FORCE_ON;
293 else if (strcmp(env_local_print,"FORCE_OFF")==0)
295 dlt_user.local_print_mode = DLT_PM_FORCE_OFF;
299 /* Initialize LogLevel/TraceStatus field */
300 dlt_user.dlt_ll_ts = 0;
301 dlt_user.dlt_ll_ts_max_num_entries = 0;
302 dlt_user.dlt_ll_ts_num_entries = 0;
304 if (dlt_buffer_init_dynamic(&(dlt_user.startup_buffer), DLT_USER_RINGBUFFER_MIN_SIZE, DLT_USER_RINGBUFFER_MAX_SIZE, DLT_USER_RINGBUFFER_STEP_SIZE)==-1)
306 dlt_user_initialised = 0;
310 signal(SIGPIPE,SIG_IGN); /* ignore pipe signals */
312 atexit(dlt_user_atexit_handler);
314 #ifdef DLT_TEST_ENABLE
315 dlt_user.corrupt_user_header = 0;
316 dlt_user.corrupt_message_size = 0;
317 dlt_user.corrupt_message_size_size = 0;
323 void dlt_user_atexit_handler(void)
326 /* Try to resend potential log messages in the user buffer */
327 int count = dlt_user_atexit_blow_out_user_buffer();
331 sprintf(tmp,"Lost log messages in user buffer when exiting: %i\n",count);
332 dlt_log(LOG_ERR, tmp);
335 /* Unregister app (this also unregisters all contexts in daemon) */
336 /* Ignore return value */
337 dlt_unregister_app();
340 /* Ignore return value */
344 int dlt_user_atexit_blow_out_user_buffer(void){
348 uint32_t exitTime = dlt_uptime() + DLT_USER_ATEXIT_RESEND_BUFFER_EXIT_TIMEOUT;
350 while(dlt_uptime() < exitTime ){
352 ret = dlt_user_log_resend_buffer();
359 usleep(DLT_USER_ATEXIT_RESEND_BUFFER_SLEEP);
363 count = dlt_buffer_get_message_count(&(dlt_user.startup_buffer));
372 char filename[DLT_USER_MAX_FILENAME_LENGTH];
374 if (dlt_user_initialised==0)
379 if (dlt_receiverthread_handle)
381 /* Ignore return value */
382 pthread_cancel(dlt_receiverthread_handle);
385 if (dlt_user.dlt_user_handle!=-1)
387 sprintf(filename,"/tmp/dlt%d",getpid());
389 close(dlt_user.dlt_user_handle);
390 dlt_user.dlt_user_handle=-1;
395 #ifdef DLT_SHM_ENABLE
396 /* free shared memory */
397 dlt_shm_free_client(&dlt_user.dlt_shm);
400 if (dlt_user.dlt_log_handle!=-1)
402 /* close log file/output fifo to daemon */
403 close(dlt_user.dlt_log_handle);
404 dlt_user.dlt_log_handle = -1;
407 /* Ignore return value */
408 dlt_receiver_free(&(dlt_user.receiver));
410 /* Ignore return value */
411 dlt_buffer_free_dynamic(&(dlt_user.startup_buffer));
413 if (dlt_user.dlt_ll_ts)
415 for (i=0;i<dlt_user.dlt_ll_ts_max_num_entries;i++)
417 if (dlt_user.dlt_ll_ts[i].injection_table!=0)
419 free(dlt_user.dlt_ll_ts[i].injection_table);
420 dlt_user.dlt_ll_ts[i].injection_table = 0;
422 dlt_user.dlt_ll_ts[i].nrcallbacks = 0;
425 free(dlt_user.dlt_ll_ts);
426 dlt_user.dlt_ll_ts = 0;
427 dlt_user.dlt_ll_ts_max_num_entries = 0;
428 dlt_user.dlt_ll_ts_num_entries = 0;
431 dlt_user_initialised = 0;
436 int dlt_check_library_version(const char * user_major_version,const char * user_minor_version)
438 return dlt_user_check_library_version(user_major_version, user_minor_version);
441 int dlt_register_app(const char *appid, const char * description)
445 if (dlt_user_initialised==0)
453 if ((appid==0) || (appid[0]=='\0'))
460 /* Store locally application id and application description */
461 dlt_set_id(dlt_user.appID, appid);
463 if (dlt_user.application_description!=0)
465 free(dlt_user.application_description);
468 dlt_user.application_description = 0;
472 dlt_user.application_description= malloc(strlen(description)+1);
473 strncpy(dlt_user.application_description, description, strlen(description));
475 /* Terminate transmitted string with 0 */
476 dlt_user.application_description[strlen(description)]='\0';
481 ret = dlt_user_log_send_register_application();
485 int dlt_register_context(DltContext *handle, const char *contextid, const char * description)
487 if (dlt_user_initialised==0)
497 if (dlt_user.appID[0]=='\0')
499 dlt_log(LOG_ERR, "no application registered!\n");
505 if ((contextid==0) || (contextid[0]=='\0'))
513 return dlt_register_context_ll_ts(handle, contextid, description, DLT_USER_LOG_LEVEL_NOT_SET, DLT_USER_TRACE_STATUS_NOT_SET);
516 int dlt_register_context_ll_ts(DltContext *handle, const char *contextid, const char * description, int loglevel, int tracestatus)
521 char ctid[DLT_ID_SIZE+1];
523 if (dlt_user_initialised==0)
533 if (dlt_user.appID[0]=='\0')
535 dlt_log(LOG_ERR, "no application registered!\n");
543 if ((contextid==0) || (contextid[0]=='\0'))
548 if ((loglevel<DLT_USER_LOG_LEVEL_NOT_SET) || (loglevel>DLT_LOG_VERBOSE) || (loglevel==DLT_LOG_DEFAULT))
553 if ((tracestatus<DLT_USER_TRACE_STATUS_NOT_SET) || (tracestatus>DLT_TRACE_STATUS_ON) || (tracestatus==DLT_TRACE_STATUS_DEFAULT))
558 if (dlt_user_log_init(handle, &log)==-1)
563 /* Reset message counter */
566 /* Store context id in log level/trace status field */
568 /* Check if already registered, else register context */
572 for (i=0;i<dlt_user.dlt_ll_ts_num_entries;i++)
574 if (dlt_user.dlt_ll_ts)
576 if (memcmp(dlt_user.dlt_ll_ts[i].contextID, contextid,DLT_ID_SIZE)==0)
580 memset(ctid,0,(DLT_ID_SIZE+1));
581 dlt_print_id(ctid, contextid);
583 sprintf(str,"context '%s' already registered!\n",ctid);
584 dlt_log(LOG_WARNING, str);
593 /* Allocate or expand context array */
594 if (dlt_user.dlt_ll_ts == 0)
596 dlt_user.dlt_ll_ts = (dlt_ll_ts_type*) malloc(sizeof(dlt_ll_ts_type)*DLT_USER_CONTEXT_ALLOC_SIZE);
597 if (dlt_user.dlt_ll_ts==0)
603 dlt_user.dlt_ll_ts_max_num_entries = DLT_USER_CONTEXT_ALLOC_SIZE;
605 /* Initialize new entries */
606 for (i=0;i<dlt_user.dlt_ll_ts_max_num_entries;i++)
608 dlt_set_id(dlt_user.dlt_ll_ts[i].contextID,"");
610 /* At startup, logging and tracing is locally enabled */
611 /* the correct log level/status is set after received from daemon */
612 dlt_user.dlt_ll_ts[i].log_level = DLT_USER_INITIAL_LOG_LEVEL;
613 dlt_user.dlt_ll_ts[i].trace_status = DLT_USER_INITIAL_TRACE_STATUS;
615 dlt_user.dlt_ll_ts[i].context_description = 0;
617 dlt_user.dlt_ll_ts[i].injection_table = 0;
618 dlt_user.dlt_ll_ts[i].nrcallbacks = 0;
623 if ((dlt_user.dlt_ll_ts_num_entries%DLT_USER_CONTEXT_ALLOC_SIZE)==0)
625 /* allocate memory in steps of DLT_USER_CONTEXT_ALLOC_SIZE, e.g. 500 */
627 old = dlt_user.dlt_ll_ts;
628 dlt_user.dlt_ll_ts_max_num_entries = ((dlt_user.dlt_ll_ts_num_entries/DLT_USER_CONTEXT_ALLOC_SIZE)+1)*DLT_USER_CONTEXT_ALLOC_SIZE;
629 dlt_user.dlt_ll_ts = (dlt_ll_ts_type*) malloc(sizeof(dlt_ll_ts_type)*
630 dlt_user.dlt_ll_ts_max_num_entries);
631 if (dlt_user.dlt_ll_ts==0)
637 memcpy(dlt_user.dlt_ll_ts,old,sizeof(dlt_ll_ts_type)*dlt_user.dlt_ll_ts_num_entries);
640 /* Initialize new entries */
641 for (i=dlt_user.dlt_ll_ts_num_entries;i<dlt_user.dlt_ll_ts_max_num_entries;i++)
643 dlt_set_id(dlt_user.dlt_ll_ts[i].contextID,"");
645 /* At startup, logging and tracing is locally enabled */
646 /* the correct log level/status is set after received from daemon */
647 dlt_user.dlt_ll_ts[i].log_level = DLT_USER_INITIAL_LOG_LEVEL;
648 dlt_user.dlt_ll_ts[i].trace_status = DLT_USER_INITIAL_TRACE_STATUS;
650 dlt_user.dlt_ll_ts[i].context_description = 0;
652 dlt_user.dlt_ll_ts[i].injection_table = 0;
653 dlt_user.dlt_ll_ts[i].nrcallbacks = 0;
658 /* Store locally context id and context description */
659 dlt_set_id(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].contextID, contextid);
661 if (dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description!=0)
663 free(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description);
666 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description = 0;
670 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description = malloc(strlen(description)+1);
671 strncpy(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description, description, strlen(description));
673 /* Terminate transmitted string with 0 */
674 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description[strlen(description)]='\0';
677 if (loglevel!=DLT_USER_LOG_LEVEL_NOT_SET)
679 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].log_level = loglevel;
682 if (tracestatus!=DLT_USER_TRACE_STATUS_NOT_SET)
684 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].trace_status = tracestatus;
687 /* Prepare transfer struct */
688 //dlt_set_id(log->appID, dlt_user.appID);
689 dlt_set_id(handle->contextID, contextid);
690 handle->log_level_pos = dlt_user.dlt_ll_ts_num_entries;
692 log.context_description = dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description;
694 if (loglevel!=DLT_USER_LOG_LEVEL_NOT_SET)
696 log.log_level = loglevel;
700 log.log_level = DLT_USER_LOG_LEVEL_NOT_SET;
703 if (tracestatus!=DLT_USER_TRACE_STATUS_NOT_SET)
705 log.trace_status = tracestatus;
709 log.trace_status = DLT_USER_TRACE_STATUS_NOT_SET;
712 dlt_user.dlt_ll_ts_num_entries++;
716 ret=dlt_user_log_send_register_context(&log);
728 int dlt_unregister_app(void)
732 if (dlt_user_initialised==0)
737 /* Inform daemon to unregister application and all of its contexts */
738 ret = dlt_user_log_send_unregister_application();
742 /* Clear and free local stored application information */
743 dlt_set_id(dlt_user.appID, "");
745 if (dlt_user.application_description!=0)
747 free(dlt_user.application_description);
750 dlt_user.application_description = 0;
757 int dlt_unregister_context(DltContext *handle)
762 if (dlt_user_initialised==0)
767 if (dlt_user_log_init(handle, &log) == -1)
774 if (dlt_user.dlt_ll_ts)
776 /* Clear and free local stored context information */
777 dlt_set_id(dlt_user.dlt_ll_ts[handle->log_level_pos].contextID, "");
779 dlt_user.dlt_ll_ts[handle->log_level_pos].log_level = DLT_USER_INITIAL_LOG_LEVEL;
780 dlt_user.dlt_ll_ts[handle->log_level_pos].trace_status = DLT_USER_INITIAL_TRACE_STATUS;
782 if (dlt_user.dlt_ll_ts[handle->log_level_pos].context_description!=0)
784 free(dlt_user.dlt_ll_ts[handle->log_level_pos].context_description);
787 dlt_user.dlt_ll_ts[handle->log_level_pos].context_description = 0;
789 if (dlt_user.dlt_ll_ts[handle->log_level_pos].injection_table)
791 free(dlt_user.dlt_ll_ts[handle->log_level_pos].injection_table);
792 dlt_user.dlt_ll_ts[handle->log_level_pos].injection_table = 0;
795 dlt_user.dlt_ll_ts[handle->log_level_pos].nrcallbacks = 0;
800 /* Inform daemon to unregister context */
801 ret = dlt_user_log_send_unregister_context(&log);
806 int dlt_set_application_ll_ts_limit(DltLogLevelType loglevel, DltTraceStatusType tracestatus)
811 if (dlt_user_initialised==0)
819 /* Removed because of DltLogLevelType and DltTraceStatusType
821 if ((loglevel<DLT_LOG_DEFAULT) || (loglevel>DLT_LOG_VERBOSE))
826 if ((tracestatus<DLT_TRACE_STATUS_DEFAULT) || (tracestatus>DLT_TRACE_STATUS_ON))
831 if (dlt_user.dlt_ll_ts==0)
840 /* Update local structures */
841 for (i=0; i<dlt_user.dlt_ll_ts_num_entries;i++)
843 dlt_user.dlt_ll_ts[i].log_level = loglevel;
844 dlt_user.dlt_ll_ts[i].trace_status = tracestatus;
849 /* Inform DLT server about update */
850 ret = dlt_send_app_ll_ts_limit(dlt_user.appID, loglevel, tracestatus);
855 int dlt_get_log_state()
857 return dlt_user.log_state;
860 int dlt_set_log_mode(DltUserLogMode mode)
862 if (dlt_user_initialised==0)
870 return dlt_user_log_send_log_mode(mode);
873 int dlt_forward_msg(void *msgdata,size_t size)
875 DltUserHeader userheader;
878 if ((msgdata==0) || (size==0))
883 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG)==-1)
885 /* Type of internal user message; same value for Trace messages */
889 if (dlt_user.dlt_is_file)
892 ret = dlt_user_log_out2(dlt_user.dlt_log_handle, msgdata, size, 0, 0);
893 return ((ret==DLT_RETURN_OK)?0:-1);
897 /* Reattach to daemon if neccesary */
898 dlt_user_log_reattach_to_daemon();
900 if (dlt_user.overflow)
902 if (dlt_user_log_send_overflow()==0)
909 ret = dlt_user_log_out3(dlt_user.dlt_log_handle,
910 &(userheader), sizeof(DltUserHeader),
911 msgdata, size, 0, 0);
913 /* store message in ringbuffer, if an error has occured */
914 if (ret!=DLT_RETURN_OK)
918 if (dlt_buffer_push3(&(dlt_user.startup_buffer),
919 (unsigned char *)&(userheader), sizeof(DltUserHeader),
920 msgdata, size, 0, 0)==-1)
922 dlt_log(LOG_ERR,"Storing message to history buffer failed! Message discarded.\n");
930 case DLT_RETURN_PIPE_FULL:
932 /* data could not be written */
933 dlt_user.overflow = 1;
936 case DLT_RETURN_PIPE_ERROR:
938 /* handle not open or pipe error */
939 close(dlt_user.dlt_log_handle);
940 dlt_user.dlt_log_handle = -1;
944 case DLT_RETURN_ERROR:
946 /* other error condition */
955 /* This case should not occur */
964 /* ********************************************************************************************* */
966 int dlt_user_log_write_start(DltContext *handle, DltContextData *log,DltLogLevelType loglevel)
968 return dlt_user_log_write_start_id(handle,log,loglevel,DLT_USER_DEFAULT_MSGID);
971 int dlt_user_log_write_start_id(DltContext *handle, DltContextData *log,DltLogLevelType loglevel, uint32_t messageid)
975 if (dlt_user_initialised==0)
983 if (dlt_user_log_init(handle, log)==-1)
993 /* Removed because of DltLogLevelType
995 if ((loglevel<DLT_LOG_DEFAULT) || (loglevel>DLT_LOG_VERBOSE))
1005 log->log_level = loglevel;
1008 if (dlt_user.dlt_ll_ts==0)
1015 if ((log->log_level<=(int)(dlt_user.dlt_ll_ts[handle->log_level_pos].log_level) ) && (log->log_level!=0))
1019 /* In non-verbose mode, insert message id */
1020 if (dlt_user.verbose_mode==0)
1022 if ((sizeof(uint32_t))>DLT_USER_BUF_MAX_SIZE)
1026 /* Write message id */
1027 memcpy(log->buffer,&(mid),sizeof(uint32_t));
1028 log->size = sizeof(uint32_t);
1030 /* as the message id is part of each message in non-verbose mode,
1031 it doesn't increment the argument counter in extended header (if used) */
1044 int dlt_user_log_write_finish(DltContextData *log)
1051 return dlt_user_log_send_log(log, DLT_TYPE_LOG);
1054 int dlt_user_log_write_raw(DltContextData *log,void *data,uint16_t length)
1064 if ((log->size+length+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1069 if (dlt_user.verbose_mode)
1071 if ((log->size+length+sizeof(uint32_t)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1076 /* Transmit type information */
1077 type_info = DLT_TYPE_INFO_RAWD;
1079 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1080 log->size += sizeof(uint32_t);
1084 /* First transmit length of raw data, then the raw data itself */
1085 arg_size = (uint16_t)length;
1087 memcpy((log->buffer)+log->size,&(arg_size),sizeof(uint16_t));
1088 log->size += sizeof(uint16_t);
1090 memcpy((log->buffer)+log->size,data,arg_size);
1091 log->size += arg_size;
1098 int dlt_user_log_write_float32(DltContextData *log, float32_t data)
1107 if (sizeof(float32_t)!=4)
1112 if ((log->size+sizeof(float32_t))>DLT_USER_BUF_MAX_SIZE)
1117 if (dlt_user.verbose_mode)
1119 if ((log->size+sizeof(uint32_t)+sizeof(float32_t))>DLT_USER_BUF_MAX_SIZE)
1124 type_info = DLT_TYPE_INFO_FLOA | DLT_TYLE_32BIT;
1126 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1127 log->size += sizeof(uint32_t);
1130 memcpy((log->buffer)+log->size,&data, sizeof(float32_t));
1131 log->size += sizeof(float32_t);
1138 int dlt_user_log_write_float64(DltContextData *log, float64_t data)
1147 if (sizeof(float64_t)!=8)
1152 if ((log->size+sizeof(float64_t))>DLT_USER_BUF_MAX_SIZE)
1157 if (dlt_user.verbose_mode)
1159 if ((log->size+sizeof(uint32_t)+sizeof(float64_t))>DLT_USER_BUF_MAX_SIZE)
1164 type_info = DLT_TYPE_INFO_FLOA | DLT_TYLE_64BIT;
1166 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1167 log->size += sizeof(uint32_t);
1170 memcpy((log->buffer)+log->size,&data, sizeof(float64_t));
1171 log->size += sizeof(float64_t);
1178 int dlt_user_log_write_uint( DltContextData *log, unsigned int data)
1185 switch (sizeof(unsigned int))
1189 return dlt_user_log_write_uint8(log, (uint8_t)data);
1194 return dlt_user_log_write_uint16(log, (uint16_t)data);
1199 return dlt_user_log_write_uint32(log, (uint32_t)data);
1204 return dlt_user_log_write_uint64(log, (uint64_t)data);
1217 int dlt_user_log_write_uint8(DltContextData *log, uint8_t data)
1226 if ((log->size+sizeof(uint8_t))>DLT_USER_BUF_MAX_SIZE)
1231 if (dlt_user.verbose_mode)
1233 if ((log->size+sizeof(uint32_t)+sizeof(uint8_t))>DLT_USER_BUF_MAX_SIZE)
1238 type_info = DLT_TYPE_INFO_UINT | DLT_TYLE_8BIT;
1240 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1241 log->size += sizeof(uint32_t);
1244 memcpy((log->buffer)+log->size,&data,sizeof(uint8_t));
1245 log->size += sizeof(uint8_t);
1252 int dlt_user_log_write_uint16(DltContextData *log, uint16_t data)
1261 if ((log->size+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1266 if (dlt_user.verbose_mode)
1268 if ((log->size+sizeof(uint32_t)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1273 type_info = DLT_TYPE_INFO_UINT | DLT_TYLE_16BIT;
1275 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1276 log->size += sizeof(uint32_t);
1279 memcpy((log->buffer)+log->size,&data,sizeof(uint16_t));
1280 log->size += sizeof(uint16_t);
1287 int dlt_user_log_write_uint32(DltContextData *log, uint32_t data)
1296 if ((log->size+sizeof(uint32_t))>DLT_USER_BUF_MAX_SIZE)
1301 if (dlt_user.verbose_mode)
1303 if ((log->size+sizeof(uint32_t)+sizeof(uint32_t))>DLT_USER_BUF_MAX_SIZE)
1308 type_info = DLT_TYPE_INFO_UINT | DLT_TYLE_32BIT;
1310 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1311 log->size += sizeof(uint32_t);
1314 memcpy((log->buffer)+log->size,&data, sizeof(uint32_t));
1315 log->size += sizeof(uint32_t);
1322 int dlt_user_log_write_uint64(DltContextData *log, uint64_t data)
1331 if ((log->size+sizeof(uint64_t))>DLT_USER_BUF_MAX_SIZE)
1336 if (dlt_user.verbose_mode)
1338 if ((log->size+sizeof(uint32_t)+sizeof(uint64_t))>DLT_USER_BUF_MAX_SIZE)
1343 type_info = DLT_TYPE_INFO_UINT | DLT_TYLE_64BIT;
1345 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1346 log->size +=sizeof(uint32_t);
1349 memcpy((log->buffer)+log->size,&data,sizeof(uint64_t));
1350 log->size += sizeof(uint64_t);
1357 int dlt_user_log_write_int(DltContextData *log, int data)
1364 switch (sizeof(int))
1368 return dlt_user_log_write_int8(log, (int8_t)data);
1373 return dlt_user_log_write_int16(log, (int16_t)data);
1378 return dlt_user_log_write_int32(log, (int32_t)data);
1383 return dlt_user_log_write_int64(log, (int64_t)data);
1396 int dlt_user_log_write_int8(DltContextData *log, int8_t data)
1405 if ((log->size+sizeof(int8_t))>DLT_USER_BUF_MAX_SIZE)
1410 if (dlt_user.verbose_mode)
1412 if ((log->size+sizeof(uint32_t)+sizeof(int8_t))>DLT_USER_BUF_MAX_SIZE)
1417 type_info = DLT_TYPE_INFO_SINT | DLT_TYLE_8BIT;
1419 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1420 log->size += sizeof(uint32_t);
1423 memcpy((log->buffer)+log->size,&data,sizeof(int8_t));
1424 log->size += sizeof(int8_t);
1431 int dlt_user_log_write_int16(DltContextData *log, int16_t data)
1440 if ((log->size+sizeof(int16_t))>DLT_USER_BUF_MAX_SIZE)
1445 if (dlt_user.verbose_mode)
1447 if ((log->size+sizeof(uint32_t)+sizeof(int16_t))>DLT_USER_BUF_MAX_SIZE)
1452 type_info = DLT_TYPE_INFO_SINT | DLT_TYLE_16BIT;
1454 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1455 log->size += sizeof(uint32_t);
1458 memcpy((log->buffer)+log->size,&data,sizeof(int16_t));
1459 log->size += sizeof(int16_t);
1466 int dlt_user_log_write_int32(DltContextData *log, int32_t data)
1475 if ((log->size+sizeof(int32_t))>DLT_USER_BUF_MAX_SIZE)
1480 if (dlt_user.verbose_mode)
1482 if ((log->size+sizeof(uint32_t)+sizeof(int32_t))>DLT_USER_BUF_MAX_SIZE)
1487 type_info = DLT_TYPE_INFO_SINT | DLT_TYLE_32BIT;
1489 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1490 log->size += sizeof(uint32_t);
1493 memcpy((log->buffer)+log->size,&data, sizeof(int32_t));
1494 log->size += sizeof(int32_t);
1501 int dlt_user_log_write_int64(DltContextData *log, int64_t data)
1510 if ((log->size+sizeof(int64_t))>DLT_USER_BUF_MAX_SIZE)
1515 if (dlt_user.verbose_mode)
1517 if ((log->size+sizeof(uint32_t)+sizeof(int64_t))>DLT_USER_BUF_MAX_SIZE)
1522 type_info = DLT_TYPE_INFO_SINT | DLT_TYLE_64BIT;
1524 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1525 log->size += sizeof(uint32_t);
1528 memcpy((log->buffer)+log->size,&data,sizeof(int64_t));
1529 log->size += sizeof(int64_t);
1536 int dlt_user_log_write_bool(DltContextData *log, uint8_t data)
1545 if ((log->size+sizeof(uint8_t))>DLT_USER_BUF_MAX_SIZE)
1550 if (dlt_user.verbose_mode)
1552 if ((log->size+sizeof(uint32_t)+sizeof(uint8_t))>DLT_USER_BUF_MAX_SIZE)
1557 type_info = DLT_TYPE_INFO_BOOL;
1559 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1560 log->size += sizeof(uint32_t);
1563 memcpy((log->buffer)+log->size,&data,sizeof(uint8_t));
1564 log->size += sizeof(uint8_t);
1571 int dlt_user_log_write_string(DltContextData *log, const char *text)
1576 if ((log==0) || (text==0))
1581 if ((log->size+(strlen(text)+1)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1586 if (dlt_user.verbose_mode)
1588 if ((log->size+(strlen(text)+1)+sizeof(uint32_t)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1593 type_info = DLT_TYPE_INFO_STRG;
1595 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1596 log->size += sizeof(uint32_t);
1599 arg_size = strlen(text) + 1;
1601 memcpy((log->buffer)+log->size,&(arg_size),sizeof(uint16_t));
1602 log->size += sizeof(uint16_t);
1604 memcpy((log->buffer)+log->size,text,arg_size);
1605 log->size += arg_size;
1612 int dlt_register_injection_callback(DltContext *handle, uint32_t service_id,
1613 int (*dlt_injection_callback)(uint32_t service_id, void *data, uint32_t length))
1619 DltUserInjectionCallback *old;
1626 if (dlt_user_log_init(handle, &log)==-1)
1631 if (service_id<DLT_USER_INJECTION_MIN)
1635 /* This function doesn't make sense storing to local file is choosen;
1636 so terminate this function */
1637 if (dlt_user.dlt_is_file)
1644 if (dlt_user.dlt_ll_ts==0)
1650 /* Insert callback in corresponding table */
1651 i=handle->log_level_pos;
1653 /* Insert each service_id only once */
1654 for (k=0;k<dlt_user.dlt_ll_ts[i].nrcallbacks;k++)
1656 if ((dlt_user.dlt_ll_ts[i].injection_table) &&
1657 (dlt_user.dlt_ll_ts[i].injection_table[k].service_id == service_id))
1670 j=dlt_user.dlt_ll_ts[i].nrcallbacks;
1672 /* Allocate or expand injection table */
1673 if (dlt_user.dlt_ll_ts[i].injection_table == 0)
1675 dlt_user.dlt_ll_ts[i].injection_table = (DltUserInjectionCallback*) malloc(sizeof(DltUserInjectionCallback));
1679 old = dlt_user.dlt_ll_ts[i].injection_table;
1680 dlt_user.dlt_ll_ts[i].injection_table = (DltUserInjectionCallback*) malloc(sizeof(DltUserInjectionCallback)*(j+1));
1681 memcpy(dlt_user.dlt_ll_ts[i].injection_table,old,sizeof(DltUserInjectionCallback)*j);
1685 dlt_user.dlt_ll_ts[i].nrcallbacks++;
1688 /* Store service_id and corresponding function pointer for callback function */
1689 dlt_user.dlt_ll_ts[i].injection_table[j].service_id = service_id;
1690 dlt_user.dlt_ll_ts[i].injection_table[j].injection_callback = dlt_injection_callback;
1696 int dlt_user_trace_network(DltContext *handle, DltNetworkTraceType nw_trace_type, uint16_t header_len, void *header, uint16_t payload_len, void *payload)
1700 if (dlt_user_initialised==0)
1708 if (dlt_user_log_init(handle, &log)==-1)
1718 /* Commented out because of DltNetworkTraceType:
1720 if ((nw_trace_type<=0) || (nw_trace_type>0x15))
1729 if (dlt_user.dlt_ll_ts==0)
1735 if (dlt_user.dlt_ll_ts[handle->log_level_pos].trace_status==DLT_TRACE_STATUS_ON)
1740 log.trace_status = nw_trace_type;
1748 /* Write header and its length */
1749 if (dlt_user_log_write_raw(&log, header, header_len)==-1)
1759 /* Write payload and its length */
1760 if (dlt_user_log_write_raw(&log, payload, payload_len)==-1)
1766 return dlt_user_log_send_log(&log, DLT_TYPE_NW_TRACE);
1776 int dlt_log_string(DltContext *handle,DltLogLevelType loglevel, const char *text)
1780 if (dlt_user.verbose_mode==0)
1785 if ((handle==0) || (text==0))
1790 if (dlt_user_log_write_start(handle,&log,loglevel))
1792 if (dlt_user_log_write_string(&log,text)==-1)
1796 if (dlt_user_log_write_finish(&log)==-1)
1805 int dlt_log_string_int(DltContext *handle,DltLogLevelType loglevel, const char *text, int data)
1809 if (dlt_user.verbose_mode==0)
1814 if ((handle==0) || (text==0))
1819 if (dlt_user_log_write_start(handle,&log,loglevel))
1821 if (dlt_user_log_write_string(&log,text)==-1)
1825 if (dlt_user_log_write_int(&log,data)==-1)
1829 if (dlt_user_log_write_finish(&log)==-1)
1838 int dlt_log_string_uint(DltContext *handle,DltLogLevelType loglevel, const char *text, unsigned int data)
1842 if (dlt_user.verbose_mode==0)
1847 if ((handle==0) || (text==0))
1852 if (dlt_user_log_write_start(handle,&log,loglevel))
1854 if (dlt_user_log_write_string(&log,text)==-1)
1858 if (dlt_user_log_write_uint(&log,data)==-1)
1862 if (dlt_user_log_write_finish(&log)==-1)
1871 int dlt_log_int(DltContext *handle,DltLogLevelType loglevel, int data)
1875 if (dlt_user.verbose_mode==0)
1885 if (dlt_user_log_write_start(handle,&log,loglevel))
1887 if (dlt_user_log_write_int(&log,data)==-1)
1891 if (dlt_user_log_write_finish(&log)==-1)
1900 int dlt_log_uint(DltContext *handle,DltLogLevelType loglevel, unsigned int data)
1904 if (dlt_user.verbose_mode==0)
1914 if (dlt_user_log_write_start(handle,&log,loglevel))
1916 if (dlt_user_log_write_uint(&log,data)==-1)
1920 if (dlt_user_log_write_finish(&log)==-1)
1929 int dlt_log_raw(DltContext *handle,DltLogLevelType loglevel, void *data,uint16_t length)
1933 if (dlt_user.verbose_mode==0)
1943 if (dlt_user_log_write_start(handle,&log,loglevel))
1945 if (dlt_user_log_write_raw(&log,data,length)==-1)
1949 if (dlt_user_log_write_finish(&log)==-1)
1958 int dlt_verbose_mode(void)
1960 if (dlt_user_initialised==0)
1968 /* Switch to verbose mode */
1969 dlt_user.verbose_mode = 1;
1974 int dlt_nonverbose_mode(void)
1976 if (dlt_user_initialised==0)
1984 /* Switch to non-verbose mode */
1985 dlt_user.verbose_mode = 0;
1990 int dlt_enable_local_print(void)
1992 if (dlt_user_initialised==0)
2000 dlt_user.enable_local_print = 1;
2005 int dlt_disable_local_print(void)
2007 if (dlt_user_initialised==0)
2015 dlt_user.enable_local_print = 0;
2020 void dlt_user_receiverthread_function(void *ptr)
2024 /* Check for new messages from DLT daemon */
2025 if (dlt_user_log_check_user_message()==-1)
2027 /* Critical error */
2028 dlt_log(LOG_CRIT,"Receiver thread encountered error condition\n");
2031 usleep(DLT_USER_RECEIVE_DELAY); /* delay */
2035 /* Private functions of user library */
2037 int dlt_user_log_init(DltContext *handle, DltContextData *log)
2039 if (dlt_user_initialised==0)
2047 log->handle = handle;
2052 int dlt_user_log_send_log(DltContextData *log, int mtype)
2055 DltUserHeader userheader;
2058 DltReturnValue ret = 0;
2070 if (dlt_user.appID[0]=='\0')
2075 if (log->handle->contextID[0]=='\0')
2080 if ((mtype<DLT_TYPE_LOG) || (mtype>DLT_TYPE_CONTROL))
2085 /* also for Trace messages */
2086 #ifdef DLT_SHM_ENABLE
2087 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG_SHM)==-1)
2089 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG)==-1)
2095 if (dlt_message_init(&msg,0)==-1)
2100 msg.storageheader = (DltStorageHeader*)msg.headerbuffer;
2102 if (dlt_set_storageheader(msg.storageheader,dlt_user.ecuID)==-1)
2107 msg.standardheader = (DltStandardHeader*)(msg.headerbuffer + sizeof(DltStorageHeader));
2108 msg.standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_PROTOCOL_VERSION1 ;
2110 if (dlt_user.verbose_mode)
2112 /* In verbose mode, send extended header */
2113 msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_UEH );
2117 /* In non-verbose, send extended header if desired */
2118 #if (DLT_USER_USE_EXTENDED_HEADER_FOR_NONVERBOSE==1)
2119 msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_UEH );
2123 #if (BYTE_ORDER==BIG_ENDIAN)
2124 msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_MSBF);
2127 msg.standardheader->mcnt = log->handle->mcnt++;
2129 /* Set header extra parameters */
2130 dlt_set_id(msg.headerextra.ecu,dlt_user.ecuID);
2131 //msg.headerextra.seid = 0;
2132 msg.headerextra.tmsp = dlt_uptime();
2134 if (dlt_message_set_extraparameters(&msg,0)==-1)
2139 /* Fill out extended header, if extended header should be provided */
2140 if (DLT_IS_HTYP_UEH(msg.standardheader->htyp))
2142 /* with extended header */
2143 msg.extendedheader = (DltExtendedHeader*)(msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp) );
2149 msg.extendedheader->msin = (DLT_TYPE_LOG << DLT_MSIN_MSTP_SHIFT) | ((log->log_level << DLT_MSIN_MTIN_SHIFT) & DLT_MSIN_MTIN) ; /* messsage info */
2152 case DLT_TYPE_NW_TRACE:
2154 msg.extendedheader->msin = (DLT_TYPE_NW_TRACE << DLT_MSIN_MSTP_SHIFT) | ((log->trace_status << DLT_MSIN_MTIN_SHIFT) & DLT_MSIN_MTIN) ; /* messsage info */
2159 /* This case should not occur */
2165 /* If in verbose mode, set flag in header for verbose mode */
2166 if (dlt_user.verbose_mode)
2168 msg.extendedheader->msin |= DLT_MSIN_VERB;
2171 msg.extendedheader->noar = log->args_num; /* number of arguments */
2172 dlt_set_id(msg.extendedheader->apid,dlt_user.appID); /* application id */
2173 dlt_set_id(msg.extendedheader->ctid,log->handle->contextID); /* context id */
2175 msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + sizeof(DltExtendedHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp);
2179 /* without extended header */
2180 msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp);
2183 len=msg.headersize - sizeof(DltStorageHeader) +log->size;
2186 dlt_log(LOG_CRIT,"Huge message discarded!\n");
2190 msg.standardheader->len = DLT_HTOBE_16(len);
2192 /* print to std out, if enabled */
2193 if ((dlt_user.local_print_mode != DLT_PM_FORCE_OFF) &&
2194 (dlt_user.local_print_mode != DLT_PM_AUTOMATIC))
2196 if ((dlt_user.enable_local_print) || (dlt_user.local_print_mode == DLT_PM_FORCE_ON))
2198 if (dlt_user_print_msg(&msg, log)==-1)
2205 if (dlt_user.dlt_is_file)
2208 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, msg.headerbuffer, msg.headersize, log->buffer, log->size);
2209 return ((ret==DLT_RETURN_OK)?0:-1);
2213 /* Reattach to daemon if neccesary */
2214 dlt_user_log_reattach_to_daemon();
2216 if (dlt_user.overflow)
2218 if (dlt_user_log_send_overflow()==0)
2220 dlt_user.overflow=0;
2224 /* try to resent old data first */
2226 if(dlt_user.dlt_log_handle!=-1)
2227 ret = dlt_user_log_resend_buffer();
2230 /* resend ok or nothing to resent */
2231 #ifdef DLT_SHM_ENABLE
2232 if(dlt_user.dlt_log_handle!=-1)
2233 dlt_shm_push(&dlt_user.dlt_shm,msg.headerbuffer+sizeof(DltStorageHeader), msg.headersize-sizeof(DltStorageHeader),
2234 log->buffer, log->size,0,0);
2237 ret = dlt_user_log_out3(dlt_user.dlt_log_handle,
2238 &(userheader), sizeof(DltUserHeader),
2243 #ifdef DLT_TEST_ENABLE
2244 if(dlt_user.corrupt_user_header) {
2245 userheader.pattern[0]=0xff;
2246 userheader.pattern[1]=0xff;
2247 userheader.pattern[2]=0xff;
2248 userheader.pattern[3]=0xff;
2250 if(dlt_user.corrupt_message_size) {
2251 msg.standardheader->len = DLT_HTOBE_16(dlt_user.corrupt_message_size_size);
2254 ret = dlt_user_log_out3(dlt_user.dlt_log_handle,
2255 &(userheader), sizeof(DltUserHeader),
2256 msg.headerbuffer+sizeof(DltStorageHeader), msg.headersize-sizeof(DltStorageHeader),
2257 log->buffer, log->size);
2261 /* store message in ringbuffer, if an error has occured */
2262 if (ret!=DLT_RETURN_OK)
2266 if (dlt_buffer_push3(&(dlt_user.startup_buffer),
2267 (unsigned char *)&(userheader), sizeof(DltUserHeader),
2268 msg.headerbuffer+sizeof(DltStorageHeader), msg.headersize-sizeof(DltStorageHeader),
2269 log->buffer, log->size)==-1)
2271 dlt_log(LOG_ERR,"Storing message to history buffer failed! Message discarded.\n");
2279 case DLT_RETURN_PIPE_FULL:
2281 /* data could not be written */
2282 dlt_user.overflow = 1;
2285 case DLT_RETURN_PIPE_ERROR:
2287 /* handle not open or pipe error */
2288 close(dlt_user.dlt_log_handle);
2289 dlt_user.dlt_log_handle = -1;
2291 #ifdef DLT_SHM_ENABLE
2292 /* free shared memory */
2293 dlt_shm_free_client(&dlt_user.dlt_shm);
2296 if (dlt_user.local_print_mode == DLT_PM_AUTOMATIC)
2298 dlt_user_print_msg(&msg, log);
2303 case DLT_RETURN_ERROR:
2305 /* other error condition */
2314 /* This case should never occur. */
2323 int dlt_user_log_send_register_application(void)
2325 DltUserHeader userheader;
2326 DltUserControlMsgRegisterApplication usercontext;
2330 if (dlt_user.appID[0]=='\0')
2335 /* set userheader */
2336 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_REGISTER_APPLICATION)==-1)
2341 /* set usercontext */
2342 dlt_set_id(usercontext.apid,dlt_user.appID); /* application id */
2343 usercontext.pid = getpid();
2345 if (dlt_user.application_description!=0)
2347 usercontext.description_length = strlen(dlt_user.application_description);
2351 usercontext.description_length = 0;
2354 if (dlt_user.dlt_is_file)
2360 ret=dlt_user_log_out3(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgRegisterApplication),dlt_user.application_description,usercontext.description_length);
2361 return ((ret==DLT_RETURN_OK)?0:-1);
2364 int dlt_user_log_send_unregister_application(void)
2366 DltUserHeader userheader;
2367 DltUserControlMsgUnregisterApplication usercontext;
2371 if (dlt_user.appID[0]=='\0')
2376 /* set userheader */
2377 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_UNREGISTER_APPLICATION)==-1)
2382 /* set usercontext */
2383 dlt_set_id(usercontext.apid,dlt_user.appID); /* application id */
2384 usercontext.pid = getpid();
2386 if (dlt_user.dlt_is_file)
2392 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgUnregisterApplication));
2393 return ((ret==DLT_RETURN_OK)?0:-1);
2396 int dlt_user_log_send_register_context(DltContextData *log)
2398 DltUserHeader userheader;
2399 DltUserControlMsgRegisterContext usercontext;
2412 if ((dlt_user.appID[0]=='\0') || (log->handle->contextID=='\0'))
2417 /* set userheader */
2418 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_REGISTER_CONTEXT)==-1)
2423 /* set usercontext */
2424 dlt_set_id(usercontext.apid,dlt_user.appID); /* application id */
2425 dlt_set_id(usercontext.ctid,log->handle->contextID); /* context id */
2426 usercontext.log_level_pos = log->handle->log_level_pos;
2427 usercontext.pid = getpid();
2429 usercontext.log_level = (int8_t)log->log_level;
2430 usercontext.trace_status = (int8_t)log->trace_status;
2432 if (log->context_description!=0)
2434 usercontext.description_length = strlen(log->context_description);
2438 usercontext.description_length = 0;
2441 if (dlt_user.dlt_is_file)
2447 ret=dlt_user_log_out3(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgRegisterContext),log->context_description,usercontext.description_length);
2448 return ((ret==DLT_RETURN_OK)?0:-1);
2451 int dlt_user_log_send_unregister_context(DltContextData *log)
2453 DltUserHeader userheader;
2454 DltUserControlMsgUnregisterContext usercontext;
2467 if ((dlt_user.appID[0]=='\0') || (log->handle->contextID=='\0'))
2472 /* set userheader */
2473 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_UNREGISTER_CONTEXT)==-1)
2478 /* set usercontext */
2479 dlt_set_id(usercontext.apid,dlt_user.appID); /* application id */
2480 dlt_set_id(usercontext.ctid,log->handle->contextID); /* context id */
2481 usercontext.pid = getpid();
2483 if (dlt_user.dlt_is_file)
2489 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgUnregisterContext));
2490 return ((ret==DLT_RETURN_OK)?0:-1);
2493 int dlt_send_app_ll_ts_limit(const char *appid, DltLogLevelType loglevel, DltTraceStatusType tracestatus)
2495 DltUserHeader userheader;
2496 DltUserControlMsgAppLogLevelTraceStatus usercontext;
2499 if ((appid==0) || (appid[0]=='\0'))
2504 /* Removed because of DltLogLevelType and DltTraceStatusType
2506 if ((loglevel<DLT_LOG_DEFAULT) || (loglevel>DLT_LOG_VERBOSE))
2511 if ((tracestatus<DLT_TRACE_STATUS_DEFAULT) || (tracestatus>DLT_TRACE_STATUS_ON))
2518 /* set userheader */
2519 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_APP_LL_TS)==-1)
2524 /* set usercontext */
2525 dlt_set_id(usercontext.apid,appid); /* application id */
2526 usercontext.log_level = loglevel;
2527 usercontext.trace_status = tracestatus;
2529 if (dlt_user.dlt_is_file)
2535 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgAppLogLevelTraceStatus));
2536 return ((ret==DLT_RETURN_OK)?0:-1);
2539 int dlt_user_log_send_log_mode(DltUserLogMode mode)
2541 DltUserHeader userheader;
2542 DltUserControlMsgLogMode logmode;
2546 /* set userheader */
2547 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG_MODE)==-1)
2553 logmode.log_mode = (unsigned char) mode;
2555 if (dlt_user.dlt_is_file)
2561 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(logmode), sizeof(DltUserControlMsgLogMode));
2562 return ((ret==DLT_RETURN_OK)?0:-1);
2565 int dlt_user_print_msg(DltMessage *msg, DltContextData *log)
2567 uint8_t *databuffer_tmp;
2568 int32_t datasize_tmp;
2569 static char text[DLT_USER_TEXT_LENGTH];
2571 if ((msg==0) || (log==0))
2576 /* Save variables before print */
2577 databuffer_tmp = msg->databuffer;
2578 datasize_tmp = msg->datasize;
2580 /* Act like a receiver, convert header back to host format */
2581 msg->standardheader->len = DLT_BETOH_16(msg->standardheader->len);
2582 dlt_message_get_extraparameters(msg,0);
2584 msg->databuffer = log->buffer;
2585 msg->datasize = log->size;
2587 /* Print message as ASCII */
2588 if (dlt_message_print_ascii(msg,text,DLT_USER_TEXT_LENGTH,0)==-1)
2593 /* Restore variables and set len to BE*/
2594 msg->databuffer = databuffer_tmp;
2595 msg->datasize = datasize_tmp;
2597 msg->standardheader->len = DLT_HTOBE_16(msg->standardheader->len);
2602 int dlt_user_log_check_user_message(void)
2609 DltUserHeader *userheader;
2610 DltReceiver *receiver = &(dlt_user.receiver);
2612 DltUserControlMsgLogLevel *usercontextll;
2613 DltUserControlMsgInjection *usercontextinj;
2614 DltUserControlMsgLogState *userlogstate;
2615 unsigned char *userbuffer;
2616 unsigned char *inject_buffer;
2618 if (dlt_user.dlt_user_handle!=-1)
2622 if (dlt_receiver_receive_fd(receiver)<=0)
2624 /* No new message available */
2628 /* look through buffer as long as data is in there */
2631 if (receiver->bytesRcvd < (int32_t)sizeof(DltUserHeader))
2636 /* resync if necessary */
2640 userheader = (DltUserHeader*) (receiver->buf+offset);
2642 /* Check for user header pattern */
2643 if (dlt_user_check_userheader(userheader))
2650 while ((int32_t)(sizeof(DltUserHeader)+offset)<=receiver->bytesRcvd);
2652 /* Check for user header pattern */
2653 if (dlt_user_check_userheader(userheader)==0)
2658 /* Set new start offset */
2661 receiver->buf+=offset;
2662 receiver->bytesRcvd-=offset;
2665 switch (userheader->message)
2667 case DLT_USER_MESSAGE_LOG_LEVEL:
2669 if (receiver->bytesRcvd < (int32_t)(sizeof(DltUserHeader)+sizeof(DltUserControlMsgLogLevel)))
2675 usercontextll = (DltUserControlMsgLogLevel*) (receiver->buf+sizeof(DltUserHeader));
2677 /* Update log level and trace status */
2678 if (usercontextll!=0)
2682 if ((usercontextll->log_level_pos >= 0) && (usercontextll->log_level_pos < (int32_t)dlt_user.dlt_ll_ts_num_entries))
2684 // printf("Store ll, ts\n");
2685 if (dlt_user.dlt_ll_ts)
2687 dlt_user.dlt_ll_ts[usercontextll->log_level_pos].log_level = usercontextll->log_level;
2688 dlt_user.dlt_ll_ts[usercontextll->log_level_pos].trace_status = usercontextll->trace_status;
2695 /* keep not read data in buffer */
2696 if (dlt_receiver_remove(receiver,sizeof(DltUserHeader)+sizeof(DltUserControlMsgLogLevel))==-1)
2702 case DLT_USER_MESSAGE_INJECTION:
2704 /* At least, user header, user context, and service id and data_length of injected message is available */
2705 if (receiver->bytesRcvd < (int32_t)(sizeof(DltUserHeader)+sizeof(DltUserControlMsgInjection)))
2711 usercontextinj = (DltUserControlMsgInjection*) (receiver->buf+sizeof(DltUserHeader));
2712 userbuffer = (unsigned char*) (receiver->buf+sizeof(DltUserHeader)+sizeof(DltUserControlMsgInjection));
2718 if (receiver->bytesRcvd < (int32_t)(sizeof(DltUserHeader)+sizeof(DltUserControlMsgInjection)+usercontextinj->data_length_inject))
2726 if ((usercontextinj->data_length_inject>0) && (dlt_user.dlt_ll_ts))
2728 /* Check if injection callback is registered for this context */
2729 for (i=0; i<dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].nrcallbacks;i++)
2731 if ((dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].injection_table) &&
2732 (dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].injection_table[i].service_id == usercontextinj->service_id))
2734 /* callback available, so prepare data, then call it */
2735 inject_buffer = malloc(usercontextinj->data_length_inject);
2736 if (inject_buffer!=0)
2738 /* copy from receiver to inject_buffer */
2739 memcpy(inject_buffer, userbuffer, usercontextinj->data_length_inject);
2742 if (dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].injection_table[i].injection_callback!=0)
2744 // printf("Got injection(%d), length=%d, '%s' \n", usercontext->service_id, usercontext->data_length_inject, inject_buffer);
2745 dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].injection_table[i].injection_callback(
2746 usercontextinj->service_id, inject_buffer, usercontextinj->data_length_inject);
2749 if (inject_buffer!=0)
2751 free(inject_buffer);
2763 /* keep not read data in buffer */
2764 if (dlt_receiver_remove(receiver,(sizeof(DltUserHeader)+sizeof(DltUserControlMsgInjection)+usercontextinj->data_length_inject))==-1)
2771 case DLT_USER_MESSAGE_LOG_STATE:
2773 /* At least, user header, user context, and service id and data_length of injected message is available */
2774 if (receiver->bytesRcvd < (int32_t)(sizeof(DltUserHeader)+sizeof(DltUserControlMsgLogState)))
2780 userlogstate = (DltUserControlMsgLogState*) (receiver->buf+sizeof(DltUserHeader));
2781 dlt_user.log_state = userlogstate->log_state;
2783 /* keep not read data in buffer */
2784 if (dlt_receiver_remove(receiver,(sizeof(DltUserHeader)+sizeof(DltUserControlMsgLogState)))==-1)
2792 dlt_log(LOG_ERR,"Invalid user message type received!\n");
2794 dlt_receiver_remove(receiver,sizeof(DltUserHeader));
2795 /* In next invocation of while loop, a resync will be triggered if additional data was received */
2808 if (dlt_receiver_move_to_begin(receiver)==-1)
2812 } /* while receive */
2815 return DLT_RETURN_OK;
2818 int dlt_user_log_resend_buffer(void)
2821 uint8_t buf[DLT_USER_RCVBUF_MAX_SIZE];
2825 /* Send content of ringbuffer */
2827 count = dlt_buffer_get_message_count(&(dlt_user.startup_buffer));
2830 for (num=0;num<count;num++)
2834 size = dlt_buffer_copy(&(dlt_user.startup_buffer),buf,sizeof(buf));
2838 #ifdef DLT_SHM_ENABLE
2839 dlt_shm_push(&dlt_user.dlt_shm,buf+sizeof(DltUserHeader),size-sizeof(DltUserHeader),0,0,0,0);
2842 ret = dlt_user_log_out3(dlt_user.dlt_log_handle, buf,sizeof(DltUserHeader),0,0,0,0);
2845 ret = dlt_user_log_out3(dlt_user.dlt_log_handle, buf,size,0,0,0,0);
2848 /* in case of error, keep message in ringbuffer */
2849 if (ret==DLT_RETURN_OK)
2851 dlt_buffer_remove(&(dlt_user.startup_buffer));
2854 if (dlt_buffer_push(&(dlt_user.startup_buffer), buf, size)==-1)
2856 dlt_log(LOG_ERR,"Error pushing back message to history buffer. Message discarded.\n");
2860 // In case of: data could not be written, set overflow flag
2861 if (ret==DLT_RETURN_PIPE_FULL)
2863 dlt_user.overflow = 1;
2869 /* keep message in ringbuffer */
2880 void dlt_user_log_reattach_to_daemon(void)
2882 uint32_t num,reregistered=0;
2885 DltContextData log_new;
2887 if (dlt_user.dlt_log_handle<0)
2889 dlt_user.dlt_log_handle=-1;
2891 /* try to open pipe to dlt daemon */
2892 dlt_user.dlt_log_handle = open(DLT_USER_FIFO, O_WRONLY | O_NONBLOCK);
2893 if (dlt_user.dlt_log_handle > 0)
2895 if (dlt_user_log_init(&handle,&log_new)==-1)
2900 #ifdef DLT_SHM_ENABLE
2901 /* init shared memory */
2902 if (dlt_shm_init_client(&dlt_user.dlt_shm,DLT_SHM_KEY) < 0)
2904 sprintf(str,"Loging disabled, Shared memory %d cannot be created!\n",DLT_SHM_KEY);
2905 dlt_log(LOG_WARNING, str);
2910 dlt_log(LOG_NOTICE, "Logging re-enabled!\n");
2912 /* Re-register application */
2913 if (dlt_user_log_send_register_application()==-1)
2920 /* Re-register all stored contexts */
2921 for (num=0; num<dlt_user.dlt_ll_ts_num_entries; num++)
2923 /* Re-register stored context */
2924 if ((dlt_user.appID[0]!='\0') && (dlt_user.dlt_ll_ts[num].contextID[0]!='\0') && (dlt_user.dlt_ll_ts))
2926 //dlt_set_id(log_new.appID, dlt_user.appID);
2927 dlt_set_id(handle.contextID, dlt_user.dlt_ll_ts[num].contextID);
2928 handle.log_level_pos = num;
2929 log_new.context_description = dlt_user.dlt_ll_ts[num].context_description;
2931 log_new.log_level = DLT_USER_LOG_LEVEL_NOT_SET;
2932 log_new.trace_status = DLT_USER_TRACE_STATUS_NOT_SET;
2934 if (dlt_user_log_send_register_context(&log_new)==-1)
2946 if (reregistered==1)
2948 dlt_user_log_resend_buffer();
2954 int dlt_user_log_send_overflow(void)
2956 DltUserHeader userheader;
2959 /* set userheader */
2960 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_OVERFLOW)==-1)
2965 if (dlt_user.dlt_is_file)
2971 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), 0, 0);
2972 return ((ret==DLT_RETURN_OK)?0:-1);
2975 int dlt_user_check_buffer(int *total_size, int *used_size)
2977 #ifdef DLT_SHM_ENABLE
2978 *total_size = dlt_shm_get_total_size(&(dlt_user.dlt_shm));
2979 *used_size = dlt_shm_get_used_size(&(dlt_user.dlt_shm));
2981 *total_size = dlt_buffer_get_total_size(&(dlt_user.startup_buffer));
2982 *used_size = dlt_buffer_get_used_size(&(dlt_user.startup_buffer));
2988 #ifdef DLT_TEST_ENABLE
2989 void dlt_user_test_corrupt_user_header(int enable)
2991 dlt_user.corrupt_user_header = enable;
2993 void dlt_user_test_corrupt_message_size(int enable,int16_t size)
2995 dlt_user.corrupt_message_size = enable;
2996 dlt_user.corrupt_message_size_size = size;