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
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) $
66 #include <stdlib.h> /* for getenv(), free(), atexit() */
67 #include <string.h> /* for strcmp(), strncmp(), strlen(), memset(), memcpy() */
68 #include <signal.h> /* for signal(), SIGPIPE, SIG_IGN */
70 #if !defined (__WIN32__)
71 #include <syslog.h> /* for LOG_... */
72 #include <semaphore.h>
73 #include <pthread.h> /* POSIX Threads */
83 #include <sys/uio.h> /* writev() */
86 #include <sys/prctl.h>
89 #include "dlt_user_shared.h"
90 #include "dlt_user_shared_cfg.h"
91 #include "dlt_user_cfg.h"
93 static DltUser dlt_user;
94 static int dlt_user_initialised = 0;
95 static int dlt_user_freeing = 0;
97 static char str[DLT_USER_BUFFER_LENGTH];
99 static sem_t dlt_mutex;
100 static pthread_t dlt_receiverthread_handle;
101 static pthread_attr_t dlt_receiverthread_attr;
103 /* Segmented Network Trace */
104 #define DLT_MAX_TRACE_SEGMENT_SIZE 1024
105 #define DLT_MESSAGE_QUEUE_NAME "/dlt_message_queue"
106 #define DLT_DELAYED_RESEND_INDICATOR_PATTERN 0xFFFF
108 /* Mutex to wait on while message queue is not initialized */
109 pthread_mutex_t mq_mutex;
110 pthread_cond_t mq_init_condition;
112 /* Structure to pass data to segmented thread */
116 DltNetworkTraceType nw_trace_type;
119 uint16_t payload_len;
123 /* Function prototypes for internally used functions */
124 static void dlt_user_receiverthread_function(void *ptr);
125 static void dlt_user_atexit_handler(void);
126 static int dlt_user_log_init(DltContext *handle, DltContextData *log);
127 static DltReturnValue dlt_user_log_send_log(DltContextData *log, int mtype);
128 static int dlt_user_log_send_register_application(void);
129 static int dlt_user_log_send_unregister_application(void);
130 static int dlt_user_log_send_register_context(DltContextData *log);
131 static int dlt_user_log_send_unregister_context(DltContextData *log);
132 static int dlt_send_app_ll_ts_limit(const char *appid, DltLogLevelType loglevel, DltTraceStatusType tracestatus);
133 static int dlt_user_log_send_log_mode(DltUserLogMode mode);
134 static int dlt_user_print_msg(DltMessage *msg, DltContextData *log);
135 static int dlt_user_log_check_user_message(void);
136 static void dlt_user_log_reattach_to_daemon(void);
137 static int dlt_user_log_send_overflow(void);
138 static void dlt_user_trace_network_segmented_thread(void *unused);
139 static void dlt_user_trace_network_segmented_thread_segmenter(s_segmented_data *data);
140 static int dlt_user_queue_resend(void);
142 int dlt_user_check_library_version(const char *user_major_version,const char *user_minor_version){
145 char lib_major_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
146 char lib_minor_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
148 dlt_get_major_version( lib_major_version);
149 dlt_get_minor_version( lib_minor_version);
151 if( (strcmp(lib_major_version,user_major_version)!=0) || (strcmp(lib_minor_version,user_minor_version)!=0))
153 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);
154 dlt_log(LOG_WARNING, str);
162 char filename[DLT_USER_MAX_FILENAME_LENGTH];
165 // process is exiting. Do not allocate new resources.
166 if (dlt_user_freeing != 0)
168 // return negative value, to stop the current log
172 // WARNING: multithread unsafe !
173 // Another thread will check that dlt_user_initialised != 0, but the lib is not initialised !
174 dlt_user_initialised = 1;
176 /* Initialize common part of dlt_init()/dlt_init_file() */
177 if (dlt_init_common()==-1)
179 dlt_user_initialised = 0;
183 /* check environment variables */
186 dlt_user.dlt_is_file = 0;
187 dlt_user.overflow = 0;
188 dlt_user.overflow_counter = 0;
189 #ifdef DLT_SHM_ENABLE
190 memset(&(dlt_user.dlt_shm),0,sizeof(DltShm));
193 /* create and open DLT user FIFO */
194 sprintf(filename,"%s/dlt%d",DLT_USER_DIR,getpid());
196 /* Try to delete existing pipe, ignore result of unlink */
199 ret=mkfifo(filename, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP );
202 sprintf(str,"Loging disabled, FIFO user %s cannot be created!\n",filename);
203 dlt_log(LOG_WARNING, str);
204 /* return 0; */ /* removed to prevent error, when FIFO already exists */
207 // S_IWGRP cannot be set by mkfifo (???), let's reassign right bits
208 ret=chmod(filename, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP );
211 sprintf(str,"FIFO user %s cannot be chmoded!\n", DLT_USER_DIR);
212 dlt_log(LOG_ERR, str);
216 dlt_user.dlt_user_handle = open(filename, O_RDWR | O_CLOEXEC);
217 if (dlt_user.dlt_user_handle == DLT_FD_INIT)
219 sprintf(str,"Loging disabled, FIFO user %s cannot be opened!\n",filename);
220 dlt_log(LOG_WARNING, str);
225 /* open DLT output FIFO */
226 dlt_user.dlt_log_handle = open(DLT_USER_FIFO, O_WRONLY | O_NONBLOCK | O_CLOEXEC );
227 if (dlt_user.dlt_log_handle==-1)
229 sprintf(str,"Loging disabled, FIFO %s cannot be opened with open()!\n",DLT_USER_FIFO);
230 dlt_log(LOG_WARNING, str);
235 #ifdef DLT_SHM_ENABLE
236 /* init shared memory */
237 if (dlt_shm_init_client(&(dlt_user.dlt_shm),DLT_SHM_KEY) < 0)
239 sprintf(str,"Loging disabled, Shared memory %d cannot be created!\n",DLT_SHM_KEY);
240 dlt_log(LOG_WARNING, str);
247 if (dlt_receiver_init(&(dlt_user.receiver),dlt_user.dlt_user_handle, DLT_USER_RCVBUF_MAX_SIZE)==-1)
249 dlt_user_initialised = 0;
253 /* Start receiver thread */
254 if (pthread_create(&(dlt_receiverthread_handle),
256 (void *) &dlt_user_receiverthread_function,
259 if (pthread_attr_destroy(&dlt_receiverthread_attr)!=0)
261 dlt_log(LOG_WARNING, "Can't destroy thread attributes!\n");
264 dlt_log(LOG_CRIT, "Can't create receiver thread!\n");
265 dlt_user_initialised = 0;
269 if (pthread_attr_destroy(&dlt_receiverthread_attr)!=0)
271 dlt_log(LOG_WARNING, "Can't destroy thread attributes!\n");
274 /* These will be lazy initialized only when needed */
275 dlt_user.dlt_segmented_queue_read_handle = -1;
276 dlt_user.dlt_segmented_queue_write_handle = -1;
278 /* Wait mutext for segmented thread */
279 pthread_mutex_init(&mq_mutex, NULL);
280 pthread_cond_init(&mq_init_condition, NULL);
282 /* Start the segmented thread */
283 if(pthread_create(&(dlt_user.dlt_segmented_nwt_handle), NULL,
284 (void *)dlt_user_trace_network_segmented_thread, NULL))
286 dlt_log(LOG_CRIT, "Can't start segmented thread!\n");
293 int dlt_init_file(const char *name)
295 dlt_user_initialised = 1;
297 /* Initialize common part of dlt_init()/dlt_init_file() */
298 if (dlt_init_common()==-1)
300 dlt_user_initialised = 0;
304 dlt_user.dlt_is_file = 1;
306 /* open DLT output file */
307 dlt_user.dlt_log_handle = open(name,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
308 if (dlt_user.dlt_log_handle == -1)
310 sprintf(str,"Log file %s cannot be opened!\n",name);
311 dlt_log(LOG_ERR, str);
318 int dlt_init_message_queue(void)
320 pthread_mutex_lock(&mq_mutex);
321 if(dlt_user.dlt_segmented_queue_read_handle >= 0 &&
322 dlt_user.dlt_segmented_queue_write_handle >= 0)
324 // Already intialized
325 pthread_mutex_unlock(&mq_mutex);
329 /* Generate per process name for queue */
330 char queue_name[NAME_MAX];
331 sprintf(queue_name, "%s.%d", DLT_MESSAGE_QUEUE_NAME, getpid());
333 /* Maximum queue size is 10, limit to size of pointers */
334 struct mq_attr mqatr;
336 mqatr.mq_maxmsg = 10;
337 mqatr.mq_msgsize = sizeof(s_segmented_data *);
338 mqatr.mq_curmsgs = 0;
341 * Create the message queue. It must be newly created
342 * if old one was left by a crashing process.
344 dlt_user.dlt_segmented_queue_read_handle = mq_open(queue_name, O_CREAT| O_RDONLY | O_EXCL,
345 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, &mqatr);
346 if(dlt_user.dlt_segmented_queue_read_handle < 0)
350 dlt_log(LOG_WARNING, "Old message queue exists, trying to delete.\n");
351 if(mq_unlink(queue_name) < 0)
354 snprintf(str,255,"Could not delete existing message queue!: %s \n",strerror(errno));
355 dlt_log(LOG_CRIT, str);
359 dlt_user.dlt_segmented_queue_read_handle = mq_open(queue_name, O_CREAT| O_RDONLY | O_EXCL,
360 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, &mqatr);
363 if(dlt_user.dlt_segmented_queue_read_handle < 0)
366 snprintf(str,255,"Can't create message queue read handle!: %s \n",strerror(errno));
367 dlt_log(LOG_CRIT, str);
368 pthread_mutex_unlock(&mq_mutex);
373 dlt_user.dlt_segmented_queue_write_handle = mq_open(queue_name, O_WRONLY|O_NONBLOCK);
374 if(dlt_user.dlt_segmented_queue_write_handle < 0)
378 snprintf(str,255,"Can't open message queue write handle!: %s \n",strerror(errno));
379 dlt_log(LOG_CRIT, str);
380 pthread_mutex_unlock(&mq_mutex);
384 pthread_cond_signal(&mq_init_condition);
385 pthread_mutex_unlock(&mq_mutex);
389 int dlt_init_common(void)
391 char *env_local_print;
393 /* Binary semaphore for threads */
394 if (sem_init(&dlt_mutex, 0, 1)==-1)
396 dlt_user_initialised = 0;
400 /* set to unknown state of connected client */
401 dlt_user.log_state = -1;
403 dlt_user.dlt_log_handle=-1;
404 dlt_user.dlt_user_handle=DLT_FD_INIT;
406 dlt_set_id(dlt_user.ecuID,DLT_USER_DEFAULT_ECU_ID);
407 dlt_set_id(dlt_user.appID,"");
409 dlt_user.application_description = 0;
411 /* Verbose mode is enabled by default */
412 dlt_user.verbose_mode = 1;
414 /* Local print is disabled by default */
415 dlt_user.enable_local_print = 0;
417 dlt_user.local_print_mode = DLT_PM_UNSET;
419 env_local_print = getenv(DLT_USER_ENV_LOCAL_PRINT_MODE);
422 if (strcmp(env_local_print,"AUTOMATIC")==0)
424 dlt_user.local_print_mode = DLT_PM_AUTOMATIC;
426 else if (strcmp(env_local_print,"FORCE_ON")==0)
428 dlt_user.local_print_mode = DLT_PM_FORCE_ON;
430 else if (strcmp(env_local_print,"FORCE_OFF")==0)
432 dlt_user.local_print_mode = DLT_PM_FORCE_OFF;
436 /* Initialize LogLevel/TraceStatus field */
438 dlt_user.dlt_ll_ts = 0;
439 dlt_user.dlt_ll_ts_max_num_entries = 0;
440 dlt_user.dlt_ll_ts_num_entries = 0;
442 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)
444 dlt_user_initialised = 0;
450 signal(SIGPIPE,SIG_IGN); /* ignore pipe signals */
452 atexit(dlt_user_atexit_handler);
454 #ifdef DLT_TEST_ENABLE
455 dlt_user.corrupt_user_header = 0;
456 dlt_user.corrupt_message_size = 0;
457 dlt_user.corrupt_message_size_size = 0;
463 void dlt_user_atexit_handler(void)
465 if (dlt_user_initialised==0)
467 dlt_log(LOG_WARNING, "dlt_user_atexit_handler dlt_user_initialised==0\n");
473 /* Try to resend potential log messages in the user buffer */
474 int count = dlt_user_atexit_blow_out_user_buffer();
478 sprintf(tmp,"Lost log messages in user buffer when exiting: %i\n",count);
479 dlt_log(LOG_ERR, tmp);
482 /* Unregister app (this also unregisters all contexts in daemon) */
483 /* Ignore return value */
484 dlt_unregister_app();
487 /* Ignore return value */
491 int dlt_user_atexit_blow_out_user_buffer(void){
495 uint32_t exitTime = dlt_uptime() + DLT_USER_ATEXIT_RESEND_BUFFER_EXIT_TIMEOUT;
497 while(dlt_uptime() < exitTime ){
499 ret = dlt_user_log_resend_buffer();
506 usleep(DLT_USER_ATEXIT_RESEND_BUFFER_SLEEP);
510 count = dlt_buffer_get_message_count(&(dlt_user.startup_buffer));
519 char filename[DLT_USER_MAX_FILENAME_LENGTH];
521 if( dlt_user_freeing != 0 )
522 // resources are already being freed. Do nothing and return.
525 // library is freeing its resources. Avoid to allocate it in dlt_init()
526 dlt_user_freeing = 1;
528 if (dlt_user_initialised==0)
530 dlt_user_freeing = 0;
533 dlt_user_initialised = 0;
535 if (dlt_receiverthread_handle)
537 /* Ignore return value */
538 pthread_cancel(dlt_receiverthread_handle);
541 if (dlt_user.dlt_segmented_nwt_handle)
543 pthread_cancel(dlt_user.dlt_segmented_nwt_handle);
546 if (dlt_user.dlt_user_handle!=DLT_FD_INIT)
548 sprintf(filename,"%s/dlt%d",DLT_USER_DIR,getpid());
550 close(dlt_user.dlt_user_handle);
551 dlt_user.dlt_user_handle=DLT_FD_INIT;
556 #ifdef DLT_SHM_ENABLE
557 /* free shared memory */
558 dlt_shm_free_client(&dlt_user.dlt_shm);
561 if (dlt_user.dlt_log_handle!=-1)
563 /* close log file/output fifo to daemon */
564 close(dlt_user.dlt_log_handle);
565 dlt_user.dlt_log_handle = -1;
568 /* Ignore return value */
570 dlt_receiver_free(&(dlt_user.receiver));
573 /* Ignore return value */
575 dlt_buffer_free_dynamic(&(dlt_user.startup_buffer));
579 if (dlt_user.dlt_ll_ts)
581 for (i=0;i<dlt_user.dlt_ll_ts_max_num_entries;i++)
583 if (dlt_user.dlt_ll_ts[i].injection_table!=0)
585 free(dlt_user.dlt_ll_ts[i].injection_table);
586 dlt_user.dlt_ll_ts[i].injection_table = 0;
588 dlt_user.dlt_ll_ts[i].nrcallbacks = 0;
591 free(dlt_user.dlt_ll_ts);
592 dlt_user.dlt_ll_ts = 0;
593 dlt_user.dlt_ll_ts_max_num_entries = 0;
594 dlt_user.dlt_ll_ts_num_entries = 0;
598 char queue_name[NAME_MAX];
599 sprintf(queue_name, "%s.%d", DLT_MESSAGE_QUEUE_NAME, getpid());
602 * Ignore errors from these, to not to spam user if dlt_free
603 * is accidentally called multiple times.
605 mq_close(dlt_user.dlt_segmented_queue_write_handle);
606 mq_close(dlt_user.dlt_segmented_queue_read_handle);
607 mq_unlink(queue_name);
609 // allow the user app to do dlt_init() again.
610 // The flag is unset only to keep almost the same behaviour as before, on EntryNav
611 // This should be removed for other projects (see documentation of dlt_free()
612 dlt_user_freeing = 0;
617 int dlt_check_library_version(const char * user_major_version,const char * user_minor_version)
619 return dlt_user_check_library_version(user_major_version, user_minor_version);
622 int dlt_register_app(const char *appid, const char * description)
626 if (dlt_user_initialised==0)
634 if ((appid==0) || (appid[0]=='\0'))
639 /* check if application already registered */
640 /* if yes do not register again */
643 if(appid[0]==dlt_user.appID[0])
648 if(appid[0]==dlt_user.appID[0] &&
649 appid[1]==dlt_user.appID[1])
654 if(appid[0]==dlt_user.appID[0] &&
655 appid[1]==dlt_user.appID[1] &&
656 appid[2]==dlt_user.appID[2])
661 if(appid[0]==dlt_user.appID[0] &&
662 appid[1]==dlt_user.appID[1] &&
663 appid[2]==dlt_user.appID[2] &&
664 appid[3]==dlt_user.appID[3])
670 /* Store locally application id and application description */
671 dlt_set_id(dlt_user.appID, appid);
673 if (dlt_user.application_description!=0)
675 free(dlt_user.application_description);
678 dlt_user.application_description = 0;
682 size_t desc_len = strlen(description);
683 dlt_user.application_description= malloc(desc_len+1);
684 if (dlt_user.application_description){
685 strncpy(dlt_user.application_description, description, desc_len);
687 /* Terminate transmitted string with 0 */
688 dlt_user.application_description[desc_len]='\0';
699 ret = dlt_user_log_send_register_application();
703 int dlt_register_context(DltContext *handle, const char *contextid, const char * description)
705 if (dlt_user_initialised==0)
715 if (dlt_user.appID[0]=='\0')
717 dlt_log(LOG_ERR, "no application registered!\n");
723 if ((contextid==0) || (contextid[0]=='\0'))
731 return dlt_register_context_ll_ts(handle, contextid, description, DLT_USER_LOG_LEVEL_NOT_SET, DLT_USER_TRACE_STATUS_NOT_SET);
734 int dlt_register_context_ll_ts(DltContext *handle, const char *contextid, const char * description, int loglevel, int tracestatus)
739 if (dlt_user_initialised==0)
749 if (dlt_user.appID[0]=='\0')
751 dlt_log(LOG_ERR, "no application registered!\n");
759 if ((contextid==0) || (contextid[0]=='\0'))
764 if ((loglevel<DLT_USER_LOG_LEVEL_NOT_SET) || (loglevel>DLT_LOG_VERBOSE) || (loglevel==DLT_LOG_DEFAULT))
769 if ((tracestatus<DLT_USER_TRACE_STATUS_NOT_SET) || (tracestatus>DLT_TRACE_STATUS_ON) || (tracestatus==DLT_TRACE_STATUS_DEFAULT))
774 if (dlt_user_log_init(handle, &log)==-1)
779 /* Reset message counter */
782 /* Store context id in log level/trace status field */
784 /* Check if already registered, else register context */
787 /* Check of double context registration removed */
788 /* Double registration is already checked by daemon */
790 /* Allocate or expand context array */
791 if (dlt_user.dlt_ll_ts == 0)
793 dlt_user.dlt_ll_ts = (dlt_ll_ts_type*) malloc(sizeof(dlt_ll_ts_type)*DLT_USER_CONTEXT_ALLOC_SIZE);
794 if (dlt_user.dlt_ll_ts==0)
800 dlt_user.dlt_ll_ts_max_num_entries = DLT_USER_CONTEXT_ALLOC_SIZE;
802 /* Initialize new entries */
803 for (i=0;i<dlt_user.dlt_ll_ts_max_num_entries;i++)
805 dlt_set_id(dlt_user.dlt_ll_ts[i].contextID,"");
807 /* At startup, logging and tracing is locally enabled */
808 /* the correct log level/status is set after received from daemon */
809 dlt_user.dlt_ll_ts[i].log_level = DLT_USER_INITIAL_LOG_LEVEL;
810 dlt_user.dlt_ll_ts[i].trace_status = DLT_USER_INITIAL_TRACE_STATUS;
812 dlt_user.dlt_ll_ts[i].log_level_ptr = 0;
813 dlt_user.dlt_ll_ts[i].trace_status_ptr = 0;
815 dlt_user.dlt_ll_ts[i].context_description = 0;
817 dlt_user.dlt_ll_ts[i].injection_table = 0;
818 dlt_user.dlt_ll_ts[i].nrcallbacks = 0;
823 if ((dlt_user.dlt_ll_ts_num_entries%DLT_USER_CONTEXT_ALLOC_SIZE)==0)
825 /* allocate memory in steps of DLT_USER_CONTEXT_ALLOC_SIZE, e.g. 500 */
826 dlt_ll_ts_type *old_ll_ts;
827 uint32_t old_max_entries;
829 old_ll_ts = dlt_user.dlt_ll_ts;
830 old_max_entries = dlt_user.dlt_ll_ts_max_num_entries;
832 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;
833 dlt_user.dlt_ll_ts = (dlt_ll_ts_type*) malloc(sizeof(dlt_ll_ts_type)*
834 dlt_user.dlt_ll_ts_max_num_entries);
835 if (dlt_user.dlt_ll_ts==0)
837 dlt_user.dlt_ll_ts = old_ll_ts;
838 dlt_user.dlt_ll_ts_max_num_entries = old_max_entries;
843 memcpy(dlt_user.dlt_ll_ts,old_ll_ts,sizeof(dlt_ll_ts_type)*dlt_user.dlt_ll_ts_num_entries);
846 /* Initialize new entries */
847 for (i=dlt_user.dlt_ll_ts_num_entries;i<dlt_user.dlt_ll_ts_max_num_entries;i++)
849 dlt_set_id(dlt_user.dlt_ll_ts[i].contextID,"");
851 /* At startup, logging and tracing is locally enabled */
852 /* the correct log level/status is set after received from daemon */
853 dlt_user.dlt_ll_ts[i].log_level = DLT_USER_INITIAL_LOG_LEVEL;
854 dlt_user.dlt_ll_ts[i].trace_status = DLT_USER_INITIAL_TRACE_STATUS;
856 dlt_user.dlt_ll_ts[i].log_level_ptr = 0;
857 dlt_user.dlt_ll_ts[i].trace_status_ptr = 0;
859 dlt_user.dlt_ll_ts[i].context_description = 0;
861 dlt_user.dlt_ll_ts[i].injection_table = 0;
862 dlt_user.dlt_ll_ts[i].nrcallbacks = 0;
867 /* Store locally context id and context description */
868 dlt_set_id(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].contextID, contextid);
870 if (dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description!=0)
872 free(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description);
875 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description = 0;
879 size_t desc_len = strlen(description);
880 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description = malloc(desc_len+1);
881 if(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description == 0)
887 strncpy(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description, description, desc_len);
889 /* Terminate transmitted string with 0 */
890 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description[desc_len]='\0';
893 if(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].log_level_ptr == 0)
895 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].log_level_ptr = malloc(sizeof(int8_t));
896 if(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].log_level_ptr == 0)
902 if(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].trace_status_ptr == 0)
904 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].trace_status_ptr = malloc(sizeof(int8_t));
905 if(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].trace_status_ptr == 0)
912 if (loglevel!=DLT_USER_LOG_LEVEL_NOT_SET)
914 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].log_level = loglevel;
917 if (tracestatus!=DLT_USER_TRACE_STATUS_NOT_SET)
919 dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].trace_status = tracestatus;
922 /* Prepare transfer struct */
923 //dlt_set_id(log->appID, dlt_user.appID);
924 dlt_set_id(handle->contextID, contextid);
925 handle->log_level_pos = dlt_user.dlt_ll_ts_num_entries;
927 handle->log_level_ptr = dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].log_level_ptr;
928 handle->trace_status_ptr = dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].trace_status_ptr;
930 log.context_description = dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description;
932 *(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].log_level_ptr) = dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].log_level;
933 *(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].trace_status_ptr) = dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].trace_status = tracestatus;
935 if (loglevel!=DLT_USER_LOG_LEVEL_NOT_SET)
937 log.log_level = loglevel;
941 log.log_level = DLT_USER_LOG_LEVEL_NOT_SET;
944 if (tracestatus!=DLT_USER_TRACE_STATUS_NOT_SET)
946 log.trace_status = tracestatus;
950 log.trace_status = DLT_USER_TRACE_STATUS_NOT_SET;
953 dlt_user.dlt_ll_ts_num_entries++;
957 return dlt_user_log_send_register_context(&log);
960 int dlt_unregister_app(void)
964 if (dlt_user_initialised==0)
969 /* Inform daemon to unregister application and all of its contexts */
970 ret = dlt_user_log_send_unregister_application();
974 /* Clear and free local stored application information */
975 dlt_set_id(dlt_user.appID, "");
977 if (dlt_user.application_description!=0)
979 free(dlt_user.application_description);
982 dlt_user.application_description = 0;
989 int dlt_unregister_context(DltContext *handle)
994 if (dlt_user_initialised==0)
999 if (dlt_user_log_init(handle, &log) == -1)
1006 handle->log_level_ptr = 0;
1007 handle->trace_status_ptr = 0;
1009 if (dlt_user.dlt_ll_ts)
1011 /* Clear and free local stored context information */
1012 dlt_set_id(dlt_user.dlt_ll_ts[handle->log_level_pos].contextID, "");
1014 dlt_user.dlt_ll_ts[handle->log_level_pos].log_level = DLT_USER_INITIAL_LOG_LEVEL;
1015 dlt_user.dlt_ll_ts[handle->log_level_pos].trace_status = DLT_USER_INITIAL_TRACE_STATUS;
1017 if (dlt_user.dlt_ll_ts[handle->log_level_pos].context_description!=0)
1019 free(dlt_user.dlt_ll_ts[handle->log_level_pos].context_description);
1022 if (dlt_user.dlt_ll_ts[handle->log_level_pos].log_level_ptr!=0)
1024 free(dlt_user.dlt_ll_ts[handle->log_level_pos].log_level_ptr);
1025 dlt_user.dlt_ll_ts[handle->log_level_pos].log_level_ptr = 0;
1028 if (dlt_user.dlt_ll_ts[handle->log_level_pos].trace_status_ptr!=0)
1030 free(dlt_user.dlt_ll_ts[handle->log_level_pos].trace_status_ptr);
1031 dlt_user.dlt_ll_ts[handle->log_level_pos].trace_status_ptr = 0;
1034 dlt_user.dlt_ll_ts[handle->log_level_pos].context_description = 0;
1036 if (dlt_user.dlt_ll_ts[handle->log_level_pos].injection_table)
1038 free(dlt_user.dlt_ll_ts[handle->log_level_pos].injection_table);
1039 dlt_user.dlt_ll_ts[handle->log_level_pos].injection_table = 0;
1042 dlt_user.dlt_ll_ts[handle->log_level_pos].nrcallbacks = 0;
1047 /* Inform daemon to unregister context */
1048 ret = dlt_user_log_send_unregister_context(&log);
1053 int dlt_set_application_ll_ts_limit(DltLogLevelType loglevel, DltTraceStatusType tracestatus)
1058 if (dlt_user_initialised==0)
1066 /* Removed because of DltLogLevelType and DltTraceStatusType
1068 if ((loglevel<DLT_LOG_DEFAULT) || (loglevel>DLT_LOG_VERBOSE))
1073 if ((tracestatus<DLT_TRACE_STATUS_DEFAULT) || (tracestatus>DLT_TRACE_STATUS_ON))
1078 if (dlt_user.dlt_ll_ts==0)
1086 if (dlt_user.dlt_ll_ts==0)
1092 /* Update local structures */
1093 for (i=0; i<dlt_user.dlt_ll_ts_num_entries;i++)
1095 dlt_user.dlt_ll_ts[i].log_level = loglevel;
1096 dlt_user.dlt_ll_ts[i].trace_status = tracestatus;
1097 if(dlt_user.dlt_ll_ts[i].log_level_ptr)
1098 *(dlt_user.dlt_ll_ts[i].log_level_ptr) = loglevel;
1099 if(dlt_user.dlt_ll_ts[i].trace_status_ptr)
1100 *(dlt_user.dlt_ll_ts[i].trace_status_ptr) = tracestatus;
1105 /* Inform DLT server about update */
1106 ret = dlt_send_app_ll_ts_limit(dlt_user.appID, loglevel, tracestatus);
1111 int dlt_get_log_state()
1113 return dlt_user.log_state;
1116 int dlt_set_log_mode(DltUserLogMode mode)
1118 if (dlt_user_initialised==0)
1126 return dlt_user_log_send_log_mode(mode);
1129 int dlt_forward_msg(void *msgdata,size_t size)
1131 DltUserHeader userheader;
1134 if ((msgdata==0) || (size==0))
1139 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG)==-1)
1141 /* Type of internal user message; same value for Trace messages */
1145 if (dlt_user.dlt_is_file)
1148 ret = dlt_user_log_out2(dlt_user.dlt_log_handle, msgdata, size, 0, 0);
1149 return ((ret==DLT_RETURN_OK)?0:-1);
1153 /* Reattach to daemon if neccesary */
1154 dlt_user_log_reattach_to_daemon();
1156 if (dlt_user.overflow_counter)
1158 if (dlt_user_log_send_overflow()==0)
1160 sprintf(str,"Buffer full! %u messages discarded!\n",dlt_user.overflow_counter);
1161 dlt_log(LOG_ERR, str);
1162 dlt_user.overflow_counter=0; }
1166 ret = dlt_user_log_out3(dlt_user.dlt_log_handle,
1167 &(userheader), sizeof(DltUserHeader),
1168 msgdata, size, 0, 0);
1170 /* store message in ringbuffer, if an error has occured */
1171 if (ret!=DLT_RETURN_OK)
1175 if (dlt_buffer_push3(&(dlt_user.startup_buffer),
1176 (unsigned char *)&(userheader), sizeof(DltUserHeader),
1177 msgdata, size, 0, 0)==-1)
1179 if(dlt_user.overflow_counter==0)
1181 dlt_log(LOG_ERR,"Buffer full! First message discarded!\n");
1183 ret = DLT_RETURN_BUFFER_FULL;
1188 if(dlt_user_queue_resend() < 0 && dlt_user.dlt_log_handle >= 0)
1190 ;//dlt_log(LOG_WARNING, "dlt_forward_msg: Failed to queue resending.\n");
1196 case DLT_RETURN_BUFFER_FULL:
1199 dlt_user.overflow_counter += 1;
1202 case DLT_RETURN_PIPE_FULL:
1204 /* data could not be written */
1207 case DLT_RETURN_PIPE_ERROR:
1209 /* handle not open or pipe error */
1210 close(dlt_user.dlt_log_handle);
1211 dlt_user.dlt_log_handle = -1;
1215 case DLT_RETURN_ERROR:
1217 /* other error condition */
1226 /* This case should not occur */
1235 /* ********************************************************************************************* */
1237 inline int dlt_user_log_write_start(DltContext *handle, DltContextData *log,DltLogLevelType loglevel)
1239 return dlt_user_log_write_start_id(handle,log,loglevel,DLT_USER_DEFAULT_MSGID);
1242 int dlt_user_log_write_start_id(DltContext *handle, DltContextData *log,DltLogLevelType loglevel, uint32_t messageid)
1244 if(dlt_user_initialised==0)
1256 if (dlt_user_log_init(handle, log)==-1)
1261 if (dlt_user.dlt_ll_ts==0)
1266 if (handle->log_level_ptr && (loglevel<=(int)*(handle->log_level_ptr) ) && (loglevel!=0))
1269 log->log_level = loglevel;
1271 /* In non-verbose mode, insert message id */
1272 if (dlt_user.verbose_mode==0)
1274 if ((sizeof(uint32_t))>DLT_USER_BUF_MAX_SIZE)
1278 /* Write message id */
1279 memcpy(log->buffer,&(messageid),sizeof(uint32_t));
1280 log->size = sizeof(uint32_t);
1282 /* as the message id is part of each message in non-verbose mode,
1283 it doesn't increment the argument counter in extended header (if used) */
1292 int dlt_user_log_write_finish(DltContextData *log)
1299 return dlt_user_log_send_log(log, DLT_TYPE_LOG) < 0 ? -1 : 0;
1302 int dlt_user_log_write_raw(DltContextData *log,void *data,uint16_t length)
1312 if ((log->size+length+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1317 if (dlt_user.verbose_mode)
1319 if ((log->size+length+sizeof(uint32_t)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1324 /* Transmit type information */
1325 type_info = DLT_TYPE_INFO_RAWD;
1327 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1328 log->size += sizeof(uint32_t);
1332 /* First transmit length of raw data, then the raw data itself */
1333 arg_size = (uint16_t)length;
1335 memcpy((log->buffer)+log->size,&(arg_size),sizeof(uint16_t));
1336 log->size += sizeof(uint16_t);
1338 memcpy((log->buffer)+log->size,data,arg_size);
1339 log->size += arg_size;
1346 int dlt_user_log_write_float32(DltContextData *log, float32_t data)
1355 if (sizeof(float32_t)!=4)
1360 if ((log->size+sizeof(float32_t))>DLT_USER_BUF_MAX_SIZE)
1365 if (dlt_user.verbose_mode)
1367 if ((log->size+sizeof(uint32_t)+sizeof(float32_t))>DLT_USER_BUF_MAX_SIZE)
1372 type_info = DLT_TYPE_INFO_FLOA | DLT_TYLE_32BIT;
1374 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1375 log->size += sizeof(uint32_t);
1378 memcpy((log->buffer)+log->size,&data, sizeof(float32_t));
1379 log->size += sizeof(float32_t);
1386 int dlt_user_log_write_float64(DltContextData *log, float64_t data)
1395 if (sizeof(float64_t)!=8)
1400 if ((log->size+sizeof(float64_t))>DLT_USER_BUF_MAX_SIZE)
1405 if (dlt_user.verbose_mode)
1407 if ((log->size+sizeof(uint32_t)+sizeof(float64_t))>DLT_USER_BUF_MAX_SIZE)
1412 type_info = DLT_TYPE_INFO_FLOA | DLT_TYLE_64BIT;
1414 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1415 log->size += sizeof(uint32_t);
1418 memcpy((log->buffer)+log->size,&data, sizeof(float64_t));
1419 log->size += sizeof(float64_t);
1426 int dlt_user_log_write_uint( DltContextData *log, unsigned int data)
1433 switch (sizeof(unsigned int))
1437 return dlt_user_log_write_uint8(log, (uint8_t)data);
1442 return dlt_user_log_write_uint16(log, (uint16_t)data);
1447 return dlt_user_log_write_uint32(log, (uint32_t)data);
1452 return dlt_user_log_write_uint64(log, (uint64_t)data);
1465 int dlt_user_log_write_uint8(DltContextData *log, uint8_t data)
1474 if ((log->size+sizeof(uint8_t))>DLT_USER_BUF_MAX_SIZE)
1479 if (dlt_user.verbose_mode)
1481 if ((log->size+sizeof(uint32_t)+sizeof(uint8_t))>DLT_USER_BUF_MAX_SIZE)
1486 type_info = DLT_TYPE_INFO_UINT | DLT_TYLE_8BIT;
1488 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1489 log->size += sizeof(uint32_t);
1492 memcpy((log->buffer)+log->size,&data,sizeof(uint8_t));
1493 log->size += sizeof(uint8_t);
1500 int dlt_user_log_write_uint16(DltContextData *log, uint16_t data)
1509 if ((log->size+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1514 if (dlt_user.verbose_mode)
1516 if ((log->size+sizeof(uint32_t)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1521 type_info = DLT_TYPE_INFO_UINT | DLT_TYLE_16BIT;
1523 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1524 log->size += sizeof(uint32_t);
1527 memcpy((log->buffer)+log->size,&data,sizeof(uint16_t));
1528 log->size += sizeof(uint16_t);
1535 int dlt_user_log_write_uint32(DltContextData *log, uint32_t data)
1544 if ((log->size+sizeof(uint32_t))>DLT_USER_BUF_MAX_SIZE)
1549 if (dlt_user.verbose_mode)
1551 if ((log->size+sizeof(uint32_t)+sizeof(uint32_t))>DLT_USER_BUF_MAX_SIZE)
1556 type_info = DLT_TYPE_INFO_UINT | DLT_TYLE_32BIT;
1558 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1559 log->size += sizeof(uint32_t);
1562 memcpy((log->buffer)+log->size,&data, sizeof(uint32_t));
1563 log->size += sizeof(uint32_t);
1570 int dlt_user_log_write_uint64(DltContextData *log, uint64_t data)
1579 if ((log->size+sizeof(uint64_t))>DLT_USER_BUF_MAX_SIZE)
1584 if (dlt_user.verbose_mode)
1586 if ((log->size+sizeof(uint32_t)+sizeof(uint64_t))>DLT_USER_BUF_MAX_SIZE)
1591 type_info = DLT_TYPE_INFO_UINT | DLT_TYLE_64BIT;
1593 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1594 log->size +=sizeof(uint32_t);
1597 memcpy((log->buffer)+log->size,&data,sizeof(uint64_t));
1598 log->size += sizeof(uint64_t);
1605 int dlt_user_log_write_int(DltContextData *log, int data)
1612 switch (sizeof(int))
1616 return dlt_user_log_write_int8(log, (int8_t)data);
1621 return dlt_user_log_write_int16(log, (int16_t)data);
1626 return dlt_user_log_write_int32(log, (int32_t)data);
1631 return dlt_user_log_write_int64(log, (int64_t)data);
1644 int dlt_user_log_write_int8(DltContextData *log, int8_t data)
1653 if ((log->size+sizeof(int8_t))>DLT_USER_BUF_MAX_SIZE)
1658 if (dlt_user.verbose_mode)
1660 if ((log->size+sizeof(uint32_t)+sizeof(int8_t))>DLT_USER_BUF_MAX_SIZE)
1665 type_info = DLT_TYPE_INFO_SINT | DLT_TYLE_8BIT;
1667 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1668 log->size += sizeof(uint32_t);
1671 memcpy((log->buffer)+log->size,&data,sizeof(int8_t));
1672 log->size += sizeof(int8_t);
1679 int dlt_user_log_write_int16(DltContextData *log, int16_t data)
1688 if ((log->size+sizeof(int16_t))>DLT_USER_BUF_MAX_SIZE)
1693 if (dlt_user.verbose_mode)
1695 if ((log->size+sizeof(uint32_t)+sizeof(int16_t))>DLT_USER_BUF_MAX_SIZE)
1700 type_info = DLT_TYPE_INFO_SINT | DLT_TYLE_16BIT;
1702 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1703 log->size += sizeof(uint32_t);
1706 memcpy((log->buffer)+log->size,&data,sizeof(int16_t));
1707 log->size += sizeof(int16_t);
1714 int dlt_user_log_write_int32(DltContextData *log, int32_t data)
1723 if ((log->size+sizeof(int32_t))>DLT_USER_BUF_MAX_SIZE)
1728 if (dlt_user.verbose_mode)
1730 if ((log->size+sizeof(uint32_t)+sizeof(int32_t))>DLT_USER_BUF_MAX_SIZE)
1735 type_info = DLT_TYPE_INFO_SINT | DLT_TYLE_32BIT;
1737 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1738 log->size += sizeof(uint32_t);
1741 memcpy((log->buffer)+log->size,&data, sizeof(int32_t));
1742 log->size += sizeof(int32_t);
1749 int dlt_user_log_write_int64(DltContextData *log, int64_t data)
1758 if ((log->size+sizeof(int64_t))>DLT_USER_BUF_MAX_SIZE)
1763 if (dlt_user.verbose_mode)
1765 if ((log->size+sizeof(uint32_t)+sizeof(int64_t))>DLT_USER_BUF_MAX_SIZE)
1770 type_info = DLT_TYPE_INFO_SINT | DLT_TYLE_64BIT;
1772 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1773 log->size += sizeof(uint32_t);
1776 memcpy((log->buffer)+log->size,&data,sizeof(int64_t));
1777 log->size += sizeof(int64_t);
1784 int dlt_user_log_write_bool(DltContextData *log, uint8_t data)
1793 if ((log->size+sizeof(uint8_t))>DLT_USER_BUF_MAX_SIZE)
1798 if (dlt_user.verbose_mode)
1800 if ((log->size+sizeof(uint32_t)+sizeof(uint8_t))>DLT_USER_BUF_MAX_SIZE)
1805 type_info = DLT_TYPE_INFO_BOOL;
1807 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1808 log->size += sizeof(uint32_t);
1811 memcpy((log->buffer)+log->size,&data,sizeof(uint8_t));
1812 log->size += sizeof(uint8_t);
1819 int dlt_user_log_write_string(DltContextData *log, const char *text)
1824 if ((log==0) || (text==0))
1829 if ((log->size+(strlen(text)+1)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1834 if (dlt_user.verbose_mode)
1836 if ((log->size+(strlen(text)+1)+sizeof(uint32_t)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1841 type_info = DLT_TYPE_INFO_STRG | DLT_SCOD_ASCII;
1843 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1844 log->size += sizeof(uint32_t);
1847 arg_size = strlen(text) + 1;
1849 memcpy((log->buffer)+log->size,&(arg_size),sizeof(uint16_t));
1850 log->size += sizeof(uint16_t);
1852 memcpy((log->buffer)+log->size,text,arg_size);
1853 log->size += arg_size;
1860 int dlt_user_log_write_utf8_string(DltContextData *log, const char *text)
1865 if ((log==0) || (text==0))
1870 arg_size = strlen(text)+1;
1872 if ((log->size+arg_size+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1877 if (dlt_user.verbose_mode)
1879 if ((log->size+arg_size+sizeof(uint32_t)+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
1884 type_info = DLT_TYPE_INFO_STRG | DLT_SCOD_UTF8;
1886 memcpy((log->buffer)+log->size,&(type_info),sizeof(uint32_t));
1887 log->size += sizeof(uint32_t);
1890 arg_size = strlen(text) + 1;
1892 memcpy((log->buffer)+log->size,&(arg_size),sizeof(uint16_t));
1893 log->size += sizeof(uint16_t);
1895 memcpy((log->buffer)+log->size,text,arg_size);
1896 log->size += arg_size;
1903 int dlt_register_injection_callback(DltContext *handle, uint32_t service_id,
1904 int (*dlt_injection_callback)(uint32_t service_id, void *data, uint32_t length))
1910 DltUserInjectionCallback *old;
1917 if (dlt_user_log_init(handle, &log)==-1)
1922 if (service_id<DLT_USER_INJECTION_MIN)
1926 /* This function doesn't make sense storing to local file is choosen;
1927 so terminate this function */
1928 if (dlt_user.dlt_is_file)
1935 if (dlt_user.dlt_ll_ts==0)
1941 /* Insert callback in corresponding table */
1942 i=handle->log_level_pos;
1944 /* Insert each service_id only once */
1945 for (k=0;k<dlt_user.dlt_ll_ts[i].nrcallbacks;k++)
1947 if ((dlt_user.dlt_ll_ts[i].injection_table) &&
1948 (dlt_user.dlt_ll_ts[i].injection_table[k].service_id == service_id))
1961 j=dlt_user.dlt_ll_ts[i].nrcallbacks;
1963 /* Allocate or expand injection table */
1964 if (dlt_user.dlt_ll_ts[i].injection_table == 0)
1966 dlt_user.dlt_ll_ts[i].injection_table = (DltUserInjectionCallback*) malloc(sizeof(DltUserInjectionCallback));
1967 if(dlt_user.dlt_ll_ts[i].injection_table == 0)
1975 old = dlt_user.dlt_ll_ts[i].injection_table;
1976 dlt_user.dlt_ll_ts[i].injection_table = (DltUserInjectionCallback*) malloc(sizeof(DltUserInjectionCallback)*(j+1));
1977 if(dlt_user.dlt_ll_ts[i].injection_table == 0)
1979 dlt_user.dlt_ll_ts[i].injection_table = old;
1983 memcpy(dlt_user.dlt_ll_ts[i].injection_table,old,sizeof(DltUserInjectionCallback)*j);
1987 dlt_user.dlt_ll_ts[i].nrcallbacks++;
1990 /* Store service_id and corresponding function pointer for callback function */
1991 dlt_user.dlt_ll_ts[i].injection_table[j].service_id = service_id;
1992 dlt_user.dlt_ll_ts[i].injection_table[j].injection_callback = dlt_injection_callback;
2005 int total_size, used_size;
2006 dlt_user_check_buffer(&total_size, &used_size);
2008 if((total_size - used_size) < (total_size/2))
2016 * Send the start of a segment chain.
2017 * Returns -1 on failure
2019 int dlt_user_trace_network_segmented_start(uint16_t *id, DltContext *handle, DltNetworkTraceType nw_trace_type, uint16_t header_len, void *header, uint16_t payload_len)
2030 if (dlt_user_log_init(handle, &log)==-1)
2036 if (dlt_user.dlt_ll_ts==0)
2041 if (handle->trace_status_ptr && *(handle->trace_status_ptr)==DLT_TRACE_STATUS_ON)
2045 log.trace_status = nw_trace_type;
2048 gettimeofday(&tv, NULL);
2051 /* Write identifier */
2052 if(dlt_user_log_write_string(&log, "NWST") < 0)
2057 /* Write stream handle */
2058 if(dlt_user_log_write_uint16(&log, *id) < 0)
2064 if(dlt_user_log_write_raw(&log, header, header_len) < 0)
2069 /* Write size of payload */
2070 if(dlt_user_log_write_uint16(&log, payload_len) < 0)
2075 /* Write expected segment count */
2076 uint16_t segment_count = payload_len/DLT_MAX_TRACE_SEGMENT_SIZE+1;
2078 /* If segments align perfectly with segment size, avoid sending empty segment */
2079 if((payload_len % DLT_MAX_TRACE_SEGMENT_SIZE) == 0)
2084 if(dlt_user_log_write_uint16(&log, segment_count) < 0)
2089 /* Write length of one segment */
2090 if(dlt_user_log_write_uint16(&log, DLT_MAX_TRACE_SEGMENT_SIZE) < 0)
2096 return dlt_user_log_send_log(&log, DLT_TYPE_NW_TRACE);
2101 int dlt_user_trace_network_segmented_segment(uint16_t id, DltContext *handle, DltNetworkTraceType nw_trace_type, int sequence, uint16_t payload_len, void *payload)
2103 while(check_buffer() < 0)
2105 usleep(1000*50); // Wait 50ms
2106 dlt_user_log_resend_buffer();
2116 if (dlt_user_log_init(handle, &log)==-1)
2121 if (dlt_user.dlt_ll_ts==0)
2126 if (handle->trace_status_ptr && *(handle->trace_status_ptr)==DLT_TRACE_STATUS_ON)
2130 log.trace_status = nw_trace_type;
2133 /* Write identifier */
2134 if(dlt_user_log_write_string(&log, "NWCH") < 0)
2139 /* Write stream handle */
2140 if(dlt_user_log_write_uint16(&log, id) < 0)
2145 /* Write segment sequence number */
2146 if(dlt_user_log_write_uint16(&log, sequence) < 0)
2152 if(dlt_user_log_write_raw(&log, payload, payload_len) < 0)
2158 return dlt_user_log_send_log(&log, DLT_TYPE_NW_TRACE);
2161 /* Allow other threads to log between chunks */
2166 int dlt_user_trace_network_segmented_end(uint16_t id, DltContext *handle, DltNetworkTraceType nw_trace_type)
2175 if (dlt_user_log_init(handle, &log)==-1)
2182 if (dlt_user.dlt_ll_ts==0)
2187 if (handle->trace_status_ptr && *(handle->trace_status_ptr)==DLT_TRACE_STATUS_ON)
2191 log.trace_status = nw_trace_type;
2194 /* Write identifier */
2195 if(dlt_user_log_write_string(&log, "NWEN") < 0)
2200 /* Write stream handle */
2201 if(dlt_user_log_write_uint(&log, id) < 0)
2207 return dlt_user_log_send_log(&log, DLT_TYPE_NW_TRACE);
2213 void dlt_user_trace_network_segmented_thread(void *unused)
2215 /* Unused on purpose. */
2218 prctl(PR_SET_NAME, "dlt_segmented", 0, 0, 0);
2220 s_segmented_data *data;
2224 // Wait until message queue is initialized
2225 pthread_mutex_lock(&mq_mutex);
2226 if(dlt_user.dlt_segmented_queue_read_handle < 0)
2228 pthread_cond_wait(&mq_init_condition, &mq_mutex);
2230 pthread_mutex_unlock(&mq_mutex);
2232 ssize_t read = mq_receive(dlt_user.dlt_segmented_queue_read_handle, (char *)&data,
2233 sizeof(s_segmented_data * ), NULL);
2235 if(read != sizeof(s_segmented_data *))
2239 snprintf(str,254,"NWTSegmented: Error while reading queue: %s \n",strerror(errno));
2240 dlt_log(LOG_CRIT, str);
2244 /* Indicator just to try to flush the buffer */
2245 if(data->payload_len == DLT_DELAYED_RESEND_INDICATOR_PATTERN)
2247 // Sleep 100ms, to allow other process to read FIFO
2249 if(dlt_user_log_resend_buffer() < 0)
2251 // Requeue if still not empty
2252 if ( dlt_user_queue_resend() < 0 )
2254 //dlt_log(LOG_WARNING, "Failed to queue resending in dlt_user_trace_network_segmented_thread.\n");
2261 dlt_user_trace_network_segmented_thread_segmenter(data);
2263 /* Send the end message */
2264 DltReturnValue err = dlt_user_trace_network_segmented_end(data->id, data->handle, data->nw_trace_type);
2265 if(err == DLT_RETURN_BUFFER_FULL || err == DLT_RETURN_ERROR)
2267 dlt_log(LOG_ERR,"NWTSegmented: Could not send end segment.\n");
2270 /* Free resources */
2272 free(data->payload);
2277 void dlt_user_trace_network_segmented_thread_segmenter(s_segmented_data *data)
2279 /* Segment the data and send the chunks */
2281 uint16_t offset = 0;
2282 uint16_t sequence = 0;
2286 if(offset + DLT_MAX_TRACE_SEGMENT_SIZE > data->payload_len)
2288 len = data->payload_len - offset;
2292 len = DLT_MAX_TRACE_SEGMENT_SIZE;
2294 /* If payload size aligns perfectly with segment size, avoid sending empty segment */
2300 ptr = data->payload + offset;
2301 DltReturnValue err = dlt_user_trace_network_segmented_segment(data->id, data->handle, data->nw_trace_type, sequence++, len, ptr);
2302 if(err == DLT_RETURN_BUFFER_FULL || err == DLT_RETURN_ERROR)
2304 dlt_log(LOG_ERR,"NWTSegmented: Could not send segment. Aborting.\n");
2308 }while(ptr < data->payload + data->payload_len);
2312 int dlt_user_trace_network_segmented(DltContext *handle, DltNetworkTraceType nw_trace_type, uint16_t header_len, void *header, uint16_t payload_len, void *payload)
2314 /* Send as normal trace if possible */
2315 if(header_len+payload_len+sizeof(uint16_t) < DLT_USER_BUF_MAX_SIZE) {
2316 return dlt_user_trace_network(handle, nw_trace_type, header_len, header, payload_len, payload);
2319 /* Allocate Memory */
2320 s_segmented_data *thread_data = malloc(sizeof(s_segmented_data));
2321 if(thread_data == NULL)
2325 thread_data->header = malloc(header_len);
2326 if(thread_data->header == NULL)
2331 thread_data->payload = malloc(payload_len);
2332 if(thread_data->payload == NULL)
2334 free(thread_data->header);
2340 thread_data->handle = handle;
2341 thread_data->nw_trace_type = nw_trace_type;
2342 thread_data->header_len = header_len;
2343 memcpy(thread_data->header, header, header_len);
2344 thread_data->payload_len = payload_len;
2345 memcpy(thread_data->payload, payload, payload_len);
2347 /* Send start message */
2348 DltReturnValue err = dlt_user_trace_network_segmented_start(&(thread_data->id),
2349 thread_data->handle, thread_data->nw_trace_type,
2350 thread_data->header_len, thread_data->header,
2351 thread_data->payload_len);
2352 if(err == DLT_RETURN_BUFFER_FULL || err == DLT_RETURN_ERROR)
2354 dlt_log(LOG_ERR,"NWTSegmented: Could not send start segment. Aborting.\n");
2355 free(thread_data->header);
2356 free(thread_data->payload);
2361 /* Open queue if it is not open */
2362 if(dlt_init_message_queue() < 0)
2364 dlt_log(LOG_ERR, "NWTSegmented: Could not open queue.\n");
2365 free(thread_data->header);
2366 free(thread_data->payload);
2373 if(mq_send(dlt_user.dlt_segmented_queue_write_handle,
2374 (char *)&thread_data, sizeof(s_segmented_data *), 1) < 0)
2378 dlt_log(LOG_ERR, "NWTSegmented: Queue full. Message discarded.\n");
2380 free(thread_data->header);
2381 free(thread_data->payload);
2384 snprintf(str,255,"NWTSegmented: Could not write into queue: %s \n",strerror(errno));
2385 dlt_log(LOG_CRIT, str);
2389 //thread_data will be freed by the receiver function
2390 //coverity[leaked_storage]
2394 int dlt_user_trace_network(DltContext *handle, DltNetworkTraceType nw_trace_type, uint16_t header_len, void *header, uint16_t payload_len, void *payload)
2396 return dlt_user_trace_network_truncated(handle, nw_trace_type, header_len, header, payload_len, payload, 1);
2399 int dlt_user_trace_network_truncated(DltContext *handle, DltNetworkTraceType nw_trace_type, uint16_t header_len, void *header, uint16_t payload_len, void *payload, int allow_truncate)
2403 if (dlt_user_initialised==0)
2411 if (dlt_user_log_init(handle, &log)==-1)
2421 /* Commented out because of DltNetworkTraceType:
2423 if ((nw_trace_type<=0) || (nw_trace_type>0x15))
2430 if (dlt_user.dlt_ll_ts==0)
2435 if (handle->trace_status_ptr && *(handle->trace_status_ptr)==DLT_TRACE_STATUS_ON)
2439 log.trace_status = nw_trace_type;
2447 /* If truncation is allowed, check if we must do it */
2448 if(allow_truncate > 0 && (header_len+payload_len+sizeof(uint16_t))>DLT_USER_BUF_MAX_SIZE)
2450 /* Identify as truncated */
2451 if(dlt_user_log_write_string(&log, "NWTR") < 0)
2456 /* Write header and its length */
2457 if (dlt_user_log_write_raw(&log, header, header_len) < 0)
2462 /* Write original size of payload */
2463 if(dlt_user_log_write_uint16(&log, payload_len) < 0)
2469 * Calculate maximum avaialble space in sending buffer after headers.
2472 int truncated_payload_len = DLT_USER_BUF_MAX_SIZE -
2473 log.size - sizeof(uint16_t) - sizeof(uint32_t);
2475 /* Write truncated payload */
2476 if (dlt_user_log_write_raw(&log, payload, truncated_payload_len) < 0)
2481 else /* Truncation not allowed or data short enough */
2483 /* Write header and its length */
2484 if (dlt_user_log_write_raw(&log, header, header_len)==-1)
2494 /* Write payload and its length */
2495 if (dlt_user_log_write_raw(&log, payload, payload_len)==-1)
2502 return dlt_user_log_send_log(&log, DLT_TYPE_NW_TRACE);
2508 int dlt_log_string(DltContext *handle,DltLogLevelType loglevel, const char *text)
2512 if (dlt_user.verbose_mode==0)
2517 if ((handle==0) || (text==0))
2522 if (dlt_user_log_write_start(handle,&log,loglevel)>0)
2524 if (dlt_user_log_write_string(&log,text)==-1)
2528 if (dlt_user_log_write_finish(&log)==-1)
2537 int dlt_log_string_int(DltContext *handle,DltLogLevelType loglevel, const char *text, int data)
2541 if (dlt_user.verbose_mode==0)
2546 if ((handle==0) || (text==0))
2551 if (dlt_user_log_write_start(handle,&log,loglevel)>0)
2553 if (dlt_user_log_write_string(&log,text)==-1)
2557 if (dlt_user_log_write_int(&log,data)==-1)
2561 if (dlt_user_log_write_finish(&log)==-1)
2570 int dlt_log_string_uint(DltContext *handle,DltLogLevelType loglevel, const char *text, unsigned int data)
2574 if (dlt_user.verbose_mode==0)
2579 if ((handle==0) || (text==0))
2584 if (dlt_user_log_write_start(handle,&log,loglevel)>0)
2586 if (dlt_user_log_write_string(&log,text)==-1)
2590 if (dlt_user_log_write_uint(&log,data)==-1)
2594 if (dlt_user_log_write_finish(&log)==-1)
2603 int dlt_log_int(DltContext *handle,DltLogLevelType loglevel, int data)
2607 if (dlt_user.verbose_mode==0)
2617 if (dlt_user_log_write_start(handle,&log,loglevel)>0)
2619 if (dlt_user_log_write_int(&log,data)==-1)
2623 if (dlt_user_log_write_finish(&log)==-1)
2632 int dlt_log_uint(DltContext *handle,DltLogLevelType loglevel, unsigned int data)
2636 if (dlt_user.verbose_mode==0)
2646 if (dlt_user_log_write_start(handle,&log,loglevel)>0)
2648 if (dlt_user_log_write_uint(&log,data)==-1)
2652 if (dlt_user_log_write_finish(&log)==-1)
2661 int dlt_log_raw(DltContext *handle,DltLogLevelType loglevel, void *data,uint16_t length)
2665 if (dlt_user.verbose_mode==0)
2675 if (dlt_user_log_write_start(handle,&log,loglevel)>0)
2677 if (dlt_user_log_write_raw(&log,data,length)==-1)
2681 if (dlt_user_log_write_finish(&log)==-1)
2690 int dlt_verbose_mode(void)
2692 if (dlt_user_initialised==0)
2700 /* Switch to verbose mode */
2701 dlt_user.verbose_mode = 1;
2706 int dlt_nonverbose_mode(void)
2708 if (dlt_user_initialised==0)
2716 /* Switch to non-verbose mode */
2717 dlt_user.verbose_mode = 0;
2722 int dlt_enable_local_print(void)
2724 if (dlt_user_initialised==0)
2732 dlt_user.enable_local_print = 1;
2737 int dlt_disable_local_print(void)
2739 if (dlt_user_initialised==0)
2747 dlt_user.enable_local_print = 0;
2752 void dlt_user_receiverthread_function(__attribute__((unused)) void *ptr)
2754 prctl(PR_SET_NAME, "dlt_receiver", 0, 0, 0);
2757 /* Check for new messages from DLT daemon */
2758 if (dlt_user_log_check_user_message()==-1)
2760 /* Critical error */
2761 dlt_log(LOG_CRIT,"Receiver thread encountered error condition\n");
2764 usleep(DLT_USER_RECEIVE_DELAY); /* delay */
2768 /* Private functions of user library */
2770 int dlt_user_log_init(DltContext *handle, DltContextData *log)
2772 if (dlt_user_initialised==0)
2780 log->handle = handle;
2785 int dlt_user_queue_resend(void)
2787 static unsigned char dlt_user_queue_resend_error_counter = 0;
2789 if(dlt_user.dlt_log_handle < 0)
2791 // Fail silenty. FIFO is not open yet
2795 * Ask segmented thread to try emptying the buffer soon.
2796 * This will be freed in dlt_user_trace_network_segmented_thread
2798 s_segmented_data *resend_data = malloc(sizeof(s_segmented_data));
2800 if (NULL == resend_data)
2805 resend_data->payload_len = DLT_DELAYED_RESEND_INDICATOR_PATTERN;
2809 /* Open queue if it is not open */
2810 if(dlt_init_message_queue() < 0)
2812 if(!dlt_user_queue_resend_error_counter)
2814 // log error only when problem occurred first time
2815 dlt_log(LOG_ERR, "NWTSegmented: Could not open queue.\n");
2817 dlt_user_queue_resend_error_counter = 1;
2822 if(mq_send(dlt_user.dlt_segmented_queue_write_handle, (char *)&resend_data, sizeof(s_segmented_data *), 1) < 0)
2824 if(!dlt_user_queue_resend_error_counter)
2826 // log error only when problem occurred first time
2828 snprintf(str,254,"Could not request resending.: %s \n",strerror(errno));
2829 dlt_log(LOG_CRIT, str);
2831 dlt_user_queue_resend_error_counter = 1;
2836 dlt_user_queue_resend_error_counter = 0;
2838 //thread_data will be freed by the receiver function
2839 //coverity[leaked_storage]
2843 DltReturnValue dlt_user_log_send_log(DltContextData *log, int mtype)
2846 DltUserHeader userheader;
2849 DltReturnValue ret = DLT_RETURN_OK;
2853 return DLT_RETURN_ERROR;
2858 return DLT_RETURN_ERROR;
2861 if (dlt_user.appID[0]=='\0')
2863 return DLT_RETURN_ERROR;
2866 if (log->handle->contextID[0]=='\0')
2868 return DLT_RETURN_ERROR;
2871 if ((mtype<DLT_TYPE_LOG) || (mtype>DLT_TYPE_CONTROL))
2873 return DLT_RETURN_ERROR;
2876 /* also for Trace messages */
2877 #ifdef DLT_SHM_ENABLE
2878 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG_SHM)==-1)
2880 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG)==-1)
2883 return DLT_RETURN_ERROR;
2886 if (dlt_message_init(&msg,0)==-1)
2888 return DLT_RETURN_ERROR;
2891 msg.storageheader = (DltStorageHeader*)msg.headerbuffer;
2893 if (dlt_set_storageheader(msg.storageheader,dlt_user.ecuID)==-1)
2895 return DLT_RETURN_ERROR;
2898 msg.standardheader = (DltStandardHeader*)(msg.headerbuffer + sizeof(DltStorageHeader));
2899 msg.standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_PROTOCOL_VERSION1 ;
2901 if (dlt_user.verbose_mode)
2903 /* In verbose mode, send extended header */
2904 msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_UEH );
2908 /* In non-verbose, send extended header if desired */
2909 #if (DLT_USER_USE_EXTENDED_HEADER_FOR_NONVERBOSE==1)
2910 msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_UEH );
2914 #if (BYTE_ORDER==BIG_ENDIAN)
2915 msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_MSBF);
2918 msg.standardheader->mcnt = log->handle->mcnt++;
2920 /* Set header extra parameters */
2921 dlt_set_id(msg.headerextra.ecu,dlt_user.ecuID);
2922 //msg.headerextra.seid = 0;
2923 msg.headerextra.tmsp = dlt_uptime();
2925 if (dlt_message_set_extraparameters(&msg,0)==-1)
2927 return DLT_RETURN_ERROR;
2930 /* Fill out extended header, if extended header should be provided */
2931 if (DLT_IS_HTYP_UEH(msg.standardheader->htyp))
2933 /* with extended header */
2934 msg.extendedheader = (DltExtendedHeader*)(msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp) );
2940 msg.extendedheader->msin = (DLT_TYPE_LOG << DLT_MSIN_MSTP_SHIFT) | ((log->log_level << DLT_MSIN_MTIN_SHIFT) & DLT_MSIN_MTIN) ; /* messsage info */
2943 case DLT_TYPE_NW_TRACE:
2945 msg.extendedheader->msin = (DLT_TYPE_NW_TRACE << DLT_MSIN_MSTP_SHIFT) | ((log->trace_status << DLT_MSIN_MTIN_SHIFT) & DLT_MSIN_MTIN) ; /* messsage info */
2950 /* This case should not occur */
2951 return DLT_RETURN_ERROR;
2956 /* If in verbose mode, set flag in header for verbose mode */
2957 if (dlt_user.verbose_mode)
2959 msg.extendedheader->msin |= DLT_MSIN_VERB;
2962 msg.extendedheader->noar = log->args_num; /* number of arguments */
2963 dlt_set_id(msg.extendedheader->apid,dlt_user.appID); /* application id */
2964 dlt_set_id(msg.extendedheader->ctid,log->handle->contextID); /* context id */
2966 msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + sizeof(DltExtendedHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp);
2970 /* without extended header */
2971 msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp);
2974 len=msg.headersize - sizeof(DltStorageHeader) +log->size;
2977 dlt_log(LOG_CRIT,"Huge message discarded!\n");
2978 return DLT_RETURN_ERROR;
2981 msg.standardheader->len = DLT_HTOBE_16(len);
2983 /* print to std out, if enabled */
2984 if ((dlt_user.local_print_mode != DLT_PM_FORCE_OFF) &&
2985 (dlt_user.local_print_mode != DLT_PM_AUTOMATIC))
2987 if ((dlt_user.enable_local_print) || (dlt_user.local_print_mode == DLT_PM_FORCE_ON))
2989 if (dlt_user_print_msg(&msg, log)==-1)
2991 return DLT_RETURN_ERROR;
2996 if (dlt_user.dlt_is_file)
2999 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, msg.headerbuffer, msg.headersize, log->buffer, log->size);
3004 /* Reattach to daemon if neccesary */
3005 dlt_user_log_reattach_to_daemon();
3007 if (dlt_user.overflow_counter)
3009 if (dlt_user_log_send_overflow()==0)
3011 sprintf(str,"%u messages discarded!\n",dlt_user.overflow_counter);
3012 dlt_log(LOG_ERR, str);
3013 dlt_user.overflow_counter=0;
3017 /* try to resent old data first */
3018 ret = DLT_RETURN_OK;
3019 if(dlt_user.dlt_log_handle!=-1)
3020 ret = dlt_user_log_resend_buffer();
3021 if(ret==DLT_RETURN_OK)
3023 /* resend ok or nothing to resent */
3024 #ifdef DLT_SHM_ENABLE
3025 if(dlt_user.dlt_log_handle!=-1)
3026 dlt_shm_push(&dlt_user.dlt_shm,msg.headerbuffer+sizeof(DltStorageHeader), msg.headersize-sizeof(DltStorageHeader),
3027 log->buffer, log->size,0,0);
3030 ret = dlt_user_log_out3(dlt_user.dlt_log_handle,
3031 &(userheader), sizeof(DltUserHeader),
3036 #ifdef DLT_TEST_ENABLE
3037 if(dlt_user.corrupt_user_header) {
3038 userheader.pattern[0]=0xff;
3039 userheader.pattern[1]=0xff;
3040 userheader.pattern[2]=0xff;
3041 userheader.pattern[3]=0xff;
3043 if(dlt_user.corrupt_message_size) {
3044 msg.standardheader->len = DLT_HTOBE_16(dlt_user.corrupt_message_size_size);
3047 ret = dlt_user_log_out3(dlt_user.dlt_log_handle,
3048 &(userheader), sizeof(DltUserHeader),
3049 msg.headerbuffer+sizeof(DltStorageHeader), msg.headersize-sizeof(DltStorageHeader),
3050 log->buffer, log->size);
3054 /* store message in ringbuffer, if an error has occured */
3055 if (ret!=DLT_RETURN_OK)
3059 if (dlt_buffer_push3(&(dlt_user.startup_buffer),
3060 (unsigned char *)&(userheader), sizeof(DltUserHeader),
3061 msg.headerbuffer+sizeof(DltStorageHeader), msg.headersize-sizeof(DltStorageHeader),
3062 log->buffer, log->size)==-1)
3064 if(dlt_user.overflow_counter==0)
3067 dlt_log(LOG_ERR,"Buffer full! Messages will be discarded.\n");
3069 ret = DLT_RETURN_BUFFER_FULL;
3074 // Fail silenty if FIFO is not open
3075 if(dlt_user_queue_resend() < 0 && dlt_user.dlt_log_handle >= 0)
3077 ;//dlt_log(LOG_WARNING, "dlt_user_log_send_log: Failed to queue resending.\n");
3083 case DLT_RETURN_BUFFER_FULL:
3086 dlt_user.overflow_counter += 1;
3087 return DLT_RETURN_BUFFER_FULL;
3089 case DLT_RETURN_PIPE_FULL:
3091 /* data could not be written */
3092 return DLT_RETURN_PIPE_FULL;
3094 case DLT_RETURN_PIPE_ERROR:
3096 /* handle not open or pipe error */
3097 close(dlt_user.dlt_log_handle);
3098 dlt_user.dlt_log_handle = -1;
3100 #ifdef DLT_SHM_ENABLE
3101 /* free shared memory */
3102 dlt_shm_free_client(&dlt_user.dlt_shm);
3105 if (dlt_user.local_print_mode == DLT_PM_AUTOMATIC)
3107 dlt_user_print_msg(&msg, log);
3110 return DLT_RETURN_PIPE_ERROR;
3112 case DLT_RETURN_ERROR:
3114 /* other error condition */
3115 return DLT_RETURN_ERROR;
3119 return DLT_RETURN_OK;
3123 /* This case should never occur. */
3124 return DLT_RETURN_ERROR;
3129 return DLT_RETURN_OK;
3132 int dlt_user_log_send_register_application(void)
3134 DltUserHeader userheader;
3135 DltUserControlMsgRegisterApplication usercontext;
3139 if (dlt_user.appID[0]=='\0')
3144 /* set userheader */
3145 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_REGISTER_APPLICATION)==-1)
3150 /* set usercontext */
3151 dlt_set_id(usercontext.apid,dlt_user.appID); /* application id */
3152 usercontext.pid = getpid();
3154 if (dlt_user.application_description!=0)
3156 usercontext.description_length = strlen(dlt_user.application_description);
3160 usercontext.description_length = 0;
3163 if (dlt_user.dlt_is_file)
3169 ret=dlt_user_log_out3(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgRegisterApplication),dlt_user.application_description,usercontext.description_length);
3171 /* store message in ringbuffer, if an error has occured */
3172 if (ret!=DLT_RETURN_OK)
3176 if (dlt_buffer_push3(&(dlt_user.startup_buffer),
3177 (unsigned char *)&(userheader), sizeof(DltUserHeader),
3178 (const unsigned char*)&(usercontext), sizeof(DltUserControlMsgRegisterApplication),
3179 (const unsigned char*)dlt_user.application_description, usercontext.description_length)==-1)
3181 dlt_log(LOG_ERR,"Storing message to history buffer failed! Message discarded.\n");
3188 if(dlt_user_queue_resend() < 0 && dlt_user.dlt_log_handle >= 0)
3190 ;//dlt_log(LOG_WARNING, "dlt_user_log_send_register_application: Failed to queue resending.\n");
3197 int dlt_user_log_send_unregister_application(void)
3199 DltUserHeader userheader;
3200 DltUserControlMsgUnregisterApplication usercontext;
3204 if (dlt_user.appID[0]=='\0')
3209 /* set userheader */
3210 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_UNREGISTER_APPLICATION)==-1)
3215 /* set usercontext */
3216 dlt_set_id(usercontext.apid,dlt_user.appID); /* application id */
3217 usercontext.pid = getpid();
3219 if (dlt_user.dlt_is_file)
3225 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgUnregisterApplication));
3226 return ((ret==DLT_RETURN_OK)?0:-1);
3229 int dlt_user_log_send_register_context(DltContextData *log)
3231 DltUserHeader userheader;
3232 DltUserControlMsgRegisterContext usercontext;
3245 if ((dlt_user.appID[0]=='\0') || (log->handle->contextID=='\0'))
3250 /* set userheader */
3251 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_REGISTER_CONTEXT)==-1)
3256 /* set usercontext */
3257 dlt_set_id(usercontext.apid,dlt_user.appID); /* application id */
3258 dlt_set_id(usercontext.ctid,log->handle->contextID); /* context id */
3259 usercontext.log_level_pos = log->handle->log_level_pos;
3260 usercontext.pid = getpid();
3262 usercontext.log_level = (int8_t)log->log_level;
3263 usercontext.trace_status = (int8_t)log->trace_status;
3265 if (log->context_description!=0)
3267 usercontext.description_length = strlen(log->context_description);
3271 usercontext.description_length = 0;
3274 if (dlt_user.dlt_is_file)
3280 ret=dlt_user_log_out3(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgRegisterContext),log->context_description,usercontext.description_length);
3282 /* store message in ringbuffer, if an error has occured */
3283 if (ret!=DLT_RETURN_OK)
3287 if (dlt_buffer_push3(&(dlt_user.startup_buffer),
3288 (unsigned char *)&(userheader), sizeof(DltUserHeader),
3289 (const unsigned char*)&(usercontext), sizeof(DltUserControlMsgRegisterContext),
3290 (const unsigned char*)log->context_description, usercontext.description_length)==-1)
3292 dlt_log(LOG_ERR,"Storing message to history buffer failed! Message discarded.\n");
3299 if(dlt_user_queue_resend() < 0 && dlt_user.dlt_log_handle >= 0)
3301 ;//dlt_log(LOG_WARNING, "dlt_user_log_send_register_context: Failed to queue resending.\n");
3309 int dlt_user_log_send_unregister_context(DltContextData *log)
3311 DltUserHeader userheader;
3312 DltUserControlMsgUnregisterContext usercontext;
3325 if ((dlt_user.appID[0]=='\0') || (log->handle->contextID=='\0'))
3330 /* set userheader */
3331 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_UNREGISTER_CONTEXT)==-1)
3336 /* set usercontext */
3337 dlt_set_id(usercontext.apid,dlt_user.appID); /* application id */
3338 dlt_set_id(usercontext.ctid,log->handle->contextID); /* context id */
3339 usercontext.pid = getpid();
3341 if (dlt_user.dlt_is_file)
3347 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgUnregisterContext));
3348 return ((ret==DLT_RETURN_OK)?0:-1);
3351 int dlt_send_app_ll_ts_limit(const char *appid, DltLogLevelType loglevel, DltTraceStatusType tracestatus)
3353 DltUserHeader userheader;
3354 DltUserControlMsgAppLogLevelTraceStatus usercontext;
3357 if ((appid==0) || (appid[0]=='\0'))
3362 /* Removed because of DltLogLevelType and DltTraceStatusType
3364 if ((loglevel<DLT_LOG_DEFAULT) || (loglevel>DLT_LOG_VERBOSE))
3369 if ((tracestatus<DLT_TRACE_STATUS_DEFAULT) || (tracestatus>DLT_TRACE_STATUS_ON))
3376 /* set userheader */
3377 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_APP_LL_TS)==-1)
3382 /* set usercontext */
3383 dlt_set_id(usercontext.apid,appid); /* application id */
3384 usercontext.log_level = loglevel;
3385 usercontext.trace_status = tracestatus;
3387 if (dlt_user.dlt_is_file)
3393 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgAppLogLevelTraceStatus));
3394 return ((ret==DLT_RETURN_OK)?0:-1);
3397 int dlt_user_log_send_log_mode(DltUserLogMode mode)
3399 DltUserHeader userheader;
3400 DltUserControlMsgLogMode logmode;
3404 /* set userheader */
3405 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG_MODE)==-1)
3411 logmode.log_mode = (unsigned char) mode;
3413 if (dlt_user.dlt_is_file)
3419 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader), &(logmode), sizeof(DltUserControlMsgLogMode));
3420 return ((ret==DLT_RETURN_OK)?0:-1);
3423 int dlt_user_print_msg(DltMessage *msg, DltContextData *log)
3425 uint8_t *databuffer_tmp;
3426 int32_t datasize_tmp;
3427 int32_t databuffersize_tmp;
3428 static char text[DLT_USER_TEXT_LENGTH];
3430 if ((msg==0) || (log==0))
3435 /* Save variables before print */
3436 databuffer_tmp = msg->databuffer;
3437 datasize_tmp = msg->datasize;
3438 databuffersize_tmp = msg->databuffersize;
3440 /* Act like a receiver, convert header back to host format */
3441 msg->standardheader->len = DLT_BETOH_16(msg->standardheader->len);
3442 dlt_message_get_extraparameters(msg,0);
3444 msg->databuffer = log->buffer;
3445 msg->datasize = log->size;
3446 msg->databuffersize = log->size;
3448 /* Print message as ASCII */
3449 if (dlt_message_print_ascii(msg,text,DLT_USER_TEXT_LENGTH,0)==-1)
3454 /* Restore variables and set len to BE*/
3455 msg->databuffer = databuffer_tmp;
3456 msg->databuffersize = databuffersize_tmp;
3457 msg->datasize = datasize_tmp;
3459 msg->standardheader->len = DLT_HTOBE_16(msg->standardheader->len);
3464 int dlt_user_log_check_user_message(void)
3471 DltUserHeader *userheader;
3472 DltReceiver *receiver = &(dlt_user.receiver);
3474 DltUserControlMsgLogLevel *usercontextll;
3475 DltUserControlMsgInjection *usercontextinj;
3476 DltUserControlMsgLogState *userlogstate;
3477 unsigned char *userbuffer;
3479 /* For delayed calling of injection callback, to avoid deadlock */
3480 DltUserInjectionCallback delayed_injection_callback;
3481 unsigned char *delayed_inject_buffer = 0;
3482 uint32_t delayed_inject_data_length = 0;
3484 /* Ensure that callback is null before searching for it */
3485 delayed_injection_callback.injection_callback = 0;
3486 delayed_injection_callback.service_id = 0;
3488 if (dlt_user.dlt_user_handle!=DLT_FD_INIT)
3492 if (dlt_receiver_receive_fd(receiver)<=0)
3494 /* No new message available */
3498 /* look through buffer as long as data is in there */
3501 if (receiver->bytesRcvd < (int32_t)sizeof(DltUserHeader))
3506 /* resync if necessary */
3510 userheader = (DltUserHeader*) (receiver->buf+offset);
3512 /* Check for user header pattern */
3513 if (dlt_user_check_userheader(userheader))
3520 while ((int32_t)(sizeof(DltUserHeader)+offset)<=receiver->bytesRcvd);
3522 /* Check for user header pattern */
3523 if (dlt_user_check_userheader(userheader)<0 ||
3524 dlt_user_check_userheader(userheader)==0)
3529 /* Set new start offset */
3532 receiver->buf+=offset;
3533 receiver->bytesRcvd-=offset;
3536 switch (userheader->message)
3538 case DLT_USER_MESSAGE_LOG_LEVEL:
3540 if (receiver->bytesRcvd < (int32_t)(sizeof(DltUserHeader)+sizeof(DltUserControlMsgLogLevel)))
3546 usercontextll = (DltUserControlMsgLogLevel*) (receiver->buf+sizeof(DltUserHeader));
3548 /* Update log level and trace status */
3549 if (usercontextll!=0)
3553 if ((usercontextll->log_level_pos >= 0) && (usercontextll->log_level_pos < (int32_t)dlt_user.dlt_ll_ts_num_entries))
3555 // printf("Store ll, ts\n");
3556 if (dlt_user.dlt_ll_ts)
3558 dlt_user.dlt_ll_ts[usercontextll->log_level_pos].log_level = usercontextll->log_level;
3559 dlt_user.dlt_ll_ts[usercontextll->log_level_pos].trace_status = usercontextll->trace_status;
3560 if(dlt_user.dlt_ll_ts[usercontextll->log_level_pos].log_level_ptr)
3561 *(dlt_user.dlt_ll_ts[usercontextll->log_level_pos].log_level_ptr) = usercontextll->log_level;
3562 if(dlt_user.dlt_ll_ts[usercontextll->log_level_pos].trace_status_ptr)
3563 *(dlt_user.dlt_ll_ts[usercontextll->log_level_pos].trace_status_ptr) = usercontextll->trace_status;
3570 /* keep not read data in buffer */
3571 if (dlt_receiver_remove(receiver,sizeof(DltUserHeader)+sizeof(DltUserControlMsgLogLevel))==-1)
3577 case DLT_USER_MESSAGE_INJECTION:
3579 /* At least, user header, user context, and service id and data_length of injected message is available */
3580 if (receiver->bytesRcvd < (int32_t)(sizeof(DltUserHeader)+sizeof(DltUserControlMsgInjection)))
3586 usercontextinj = (DltUserControlMsgInjection*) (receiver->buf+sizeof(DltUserHeader));
3587 userbuffer = (unsigned char*) (receiver->buf+sizeof(DltUserHeader)+sizeof(DltUserControlMsgInjection));
3592 if (receiver->bytesRcvd < (int32_t)(sizeof(DltUserHeader)+sizeof(DltUserControlMsgInjection)+usercontextinj->data_length_inject))
3600 if ((usercontextinj->data_length_inject>0) && (dlt_user.dlt_ll_ts))
3602 /* Check if injection callback is registered for this context */
3603 for (i=0; i<dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].nrcallbacks;i++)
3605 if ((dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].injection_table) &&
3606 (dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].injection_table[i].service_id == usercontextinj->service_id))
3608 /* Prepare delayed injection callback call */
3609 if (dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].injection_table[i].injection_callback!=0)
3611 delayed_injection_callback.injection_callback = dlt_user.dlt_ll_ts[usercontextinj->log_level_pos].injection_table[i].injection_callback;
3612 delayed_injection_callback.service_id = usercontextinj->service_id;
3613 delayed_inject_data_length = usercontextinj->data_length_inject;
3614 delayed_inject_buffer = malloc(delayed_inject_data_length);
3615 if(delayed_inject_buffer != 0) {
3616 memcpy(delayed_inject_buffer, userbuffer, delayed_inject_data_length);
3627 /* Delayed injection callback call */
3628 if(delayed_inject_buffer != 0 && delayed_injection_callback.injection_callback != 0) {
3629 delayed_injection_callback.injection_callback(delayed_injection_callback.service_id, delayed_inject_buffer, delayed_inject_data_length);
3630 delayed_injection_callback.injection_callback = 0;
3631 free(delayed_inject_buffer);
3632 delayed_inject_buffer = NULL;
3636 /* keep not read data in buffer */
3637 if (dlt_receiver_remove(receiver,(sizeof(DltUserHeader)+sizeof(DltUserControlMsgInjection)+usercontextinj->data_length_inject))==-1)
3644 case DLT_USER_MESSAGE_LOG_STATE:
3646 /* At least, user header, user context, and service id and data_length of injected message is available */
3647 if (receiver->bytesRcvd < (int32_t)(sizeof(DltUserHeader)+sizeof(DltUserControlMsgLogState)))
3653 userlogstate = (DltUserControlMsgLogState*) (receiver->buf+sizeof(DltUserHeader));
3654 dlt_user.log_state = userlogstate->log_state;
3656 /* keep not read data in buffer */
3657 if (dlt_receiver_remove(receiver,(sizeof(DltUserHeader)+sizeof(DltUserControlMsgLogState)))==-1)
3665 dlt_log(LOG_ERR,"Invalid user message type received!\n");
3667 dlt_receiver_remove(receiver,sizeof(DltUserHeader));
3668 /* In next invocation of while loop, a resync will be triggered if additional data was received */
3681 if (dlt_receiver_move_to_begin(receiver)==-1)
3685 } /* while receive */
3688 return DLT_RETURN_OK;
3691 int dlt_user_log_resend_buffer(void)
3697 /* Send content of ringbuffer */
3699 count = dlt_buffer_get_message_count(&(dlt_user.startup_buffer));
3702 for (num=0;num<count;num++)
3706 size = dlt_buffer_copy(&(dlt_user.startup_buffer),dlt_user.resend_buffer,sizeof(dlt_user.resend_buffer));
3710 #ifdef DLT_SHM_ENABLE
3711 dlt_shm_push(&dlt_user.dlt_shm,dlt_user.resend_buffer+sizeof(DltUserHeader),size-sizeof(DltUserHeader),0,0,0,0);
3714 ret = dlt_user_log_out3(dlt_user.dlt_log_handle, dlt_user.resend_buffer,sizeof(DltUserHeader),0,0,0,0);
3717 ret = dlt_user_log_out3(dlt_user.dlt_log_handle, dlt_user.resend_buffer,size,0,0,0,0);
3720 /* in case of error, keep message in ringbuffer */
3721 if (ret==DLT_RETURN_OK)
3723 dlt_buffer_remove(&(dlt_user.startup_buffer));
3727 /* keep message in ringbuffer */
3738 void dlt_user_log_reattach_to_daemon(void)
3740 uint32_t num,reregistered=0;
3743 DltContextData log_new;
3745 if (dlt_user.dlt_log_handle<0)
3747 dlt_user.dlt_log_handle=-1;
3749 /* try to open pipe to dlt daemon */
3750 dlt_user.dlt_log_handle = open(DLT_USER_FIFO, O_WRONLY | O_NONBLOCK);
3751 if (dlt_user.dlt_log_handle > 0)
3753 if (dlt_user_log_init(&handle,&log_new)==-1)
3758 #ifdef DLT_SHM_ENABLE
3759 /* init shared memory */
3760 if (dlt_shm_init_client(&dlt_user.dlt_shm,DLT_SHM_KEY) < 0)
3762 sprintf(str,"Loging disabled, Shared memory %d cannot be created!\n",DLT_SHM_KEY);
3763 dlt_log(LOG_WARNING, str);
3768 dlt_log(LOG_NOTICE, "Logging re-enabled!\n");
3770 /* Re-register application */
3771 if (dlt_user_log_send_register_application()==-1)
3778 /* Re-register all stored contexts */
3779 for (num=0; num<dlt_user.dlt_ll_ts_num_entries; num++)
3781 /* Re-register stored context */
3782 if ((dlt_user.appID[0]!='\0') && (dlt_user.dlt_ll_ts) && (dlt_user.dlt_ll_ts[num].contextID[0]!='\0'))
3784 //dlt_set_id(log_new.appID, dlt_user.appID);
3785 dlt_set_id(handle.contextID, dlt_user.dlt_ll_ts[num].contextID);
3786 handle.log_level_pos = num;
3787 log_new.context_description = dlt_user.dlt_ll_ts[num].context_description;
3789 // Release the mutex for sending context registration:
3790 // function dlt_user_log_send_register_context() can take the mutex to write to the DLT buffer. => dead lock
3793 log_new.log_level = DLT_USER_LOG_LEVEL_NOT_SET;
3794 log_new.trace_status = DLT_USER_TRACE_STATUS_NOT_SET;
3796 if (dlt_user_log_send_register_context(&log_new)==-1)
3803 // Lock again the mutex
3804 // it is necessary in the for(;;) test, in order to have coherent dlt_user data all over the critical section.
3812 if (reregistered==1)
3814 dlt_user_log_resend_buffer();
3820 int dlt_user_log_send_overflow(void)
3822 DltUserHeader userheader;
3823 DltUserControlMsgBufferOverflow userpayload;
3826 /* set userheader */
3827 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_OVERFLOW)==-1)
3832 if (dlt_user.dlt_is_file)
3837 /* set user message parameters */
3838 userpayload.overflow_counter = dlt_user.overflow_counter;
3839 dlt_set_id(userpayload.apid,dlt_user.appID);
3842 ret=dlt_user_log_out2(dlt_user.dlt_log_handle, &(userheader), sizeof(DltUserHeader),
3843 &(userpayload), sizeof(DltUserControlMsgBufferOverflow));
3844 return ((ret==DLT_RETURN_OK)?0:-1);
3847 int dlt_user_check_buffer(int *total_size, int *used_size)
3849 #ifdef DLT_SHM_ENABLE
3850 *total_size = dlt_shm_get_total_size(&(dlt_user.dlt_shm));
3851 *used_size = dlt_shm_get_used_size(&(dlt_user.dlt_shm));
3853 *total_size = dlt_buffer_get_total_size(&(dlt_user.startup_buffer));
3854 *used_size = dlt_buffer_get_used_size(&(dlt_user.startup_buffer));
3860 #ifdef DLT_TEST_ENABLE
3861 void dlt_user_test_corrupt_user_header(int enable)
3863 dlt_user.corrupt_user_header = enable;
3865 void dlt_user_test_corrupt_message_size(int enable,int16_t size)
3867 dlt_user.corrupt_message_size = enable;
3868 dlt_user.corrupt_message_size_size = size;