2 * Dlt Daemon - Diagnostic Log and Trace
5 * Copyright (C) 2011, BMW AG - Alexander Wenzel <alexander.wenzel@bmw.de>
7 * This program is free software; you can redistribute it and/or modify it under the terms of the
8 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
11 * Public License, version 2.1, for more details.
13 * You should have received a copy of the GNU Lesser General Public License, version 2.1, along
14 * with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
16 * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may
17 * also be applicable to programs even in cases in which the program is not a library in the technical sense.
19 * Linking DLT statically or dynamically with other modules is making a combined work based on DLT. You may
20 * license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to
21 * license your linked modules under the GNU Lesser General Public License, version 2.1, you
22 * may use the program under the following exception.
24 * As a special exception, the copyright holders of DLT give you permission to combine DLT
25 * with software programs or libraries that are released under any license unless such a combination is not
26 * permitted by the license of such a software program or library. You may copy and distribute such a
27 * system following the terms of the GNU Lesser General Public License, version 2.1, including this
28 * special exception, for DLT and the licenses of the other code concerned.
30 * Note that people who make modified versions of DLT are not obligated to grant this special exception
31 * for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License,
32 * version 2.1, gives permission to release a modified version without this exception; this exception
33 * also makes it possible to release a modified version which carries forward this exception.
38 /*******************************************************************************
40 ** SRC-MODULE: dlt_daemon_common.c **
46 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
53 ** PLATFORM DEPENDANT [yes/no]: yes **
55 ** TO BE CHANGED BY USER [yes/no]: no **
57 *******************************************************************************/
59 /*******************************************************************************
61 ********************************************************************************
63 ** Initials Name Company **
64 ** -------- ------------------------- ---------------------------------- **
65 ** aw Alexander Wenzel BMW **
66 ** mk Markus Klein Fraunhofer ESK **
67 *******************************************************************************/
69 /*******************************************************************************
70 ** Revision Control History **
71 *******************************************************************************/
74 * $LastChangedRevision: 1670 $
75 * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
89 #include <sys/types.h> /* send() */
90 #include <sys/socket.h> /* send() */
92 #include "dlt_types.h"
93 #include "dlt_daemon_common.h"
94 #include "dlt_daemon_common_cfg.h"
95 #include "dlt_user_shared.h"
96 #include "dlt_user_shared_cfg.h"
98 static char str[DLT_DAEMON_TEXTBUFSIZE];
100 sem_t dlt_daemon_mutex;
102 static int dlt_daemon_cmp_apid(const void *m1, const void *m2)
104 DltDaemonApplication *mi1 = (DltDaemonApplication *) m1;
105 DltDaemonApplication *mi2 = (DltDaemonApplication *) m2;
107 return memcmp(mi1->apid, mi2->apid, DLT_ID_SIZE);
110 static int dlt_daemon_cmp_apid_ctid(const void *m1, const void *m2)
114 DltDaemonContext *mi1 = (DltDaemonContext *) m1;
115 DltDaemonContext *mi2 = (DltDaemonContext *) m2;
117 cmp=memcmp(mi1->apid, mi2->apid, DLT_ID_SIZE);
124 ret=memcmp(mi1->ctid, mi2->ctid, DLT_ID_SIZE);
134 int dlt_daemon_init(DltDaemon *daemon,const char *runtime_directory, int verbose)
136 PRINT_FUNCTION_VERBOSE(verbose);
143 daemon->num_contexts = 0;
144 daemon->contexts = 0;
146 daemon->num_applications = 0;
147 daemon->applications = 0;
149 daemon->default_log_level = DLT_DAEMON_INITIAL_LOG_LEVEL ;
150 daemon->default_trace_status = DLT_DAEMON_INITIAL_TRACE_STATUS ;
152 daemon->message_buffer_overflow = DLT_MESSAGE_BUFFER_NO_OVERFLOW;
154 daemon->runtime_context_cfg_loaded = 0;
156 daemon->mode = DLT_USER_MODE_EXTERNAL;
158 /* prepare filenames for configuration */
159 if(runtime_directory[0])
160 strcpy(daemon->runtime_application_cfg,runtime_directory);
162 strcpy(daemon->runtime_application_cfg,DLT_RUNTIME_DEFAULT_DIRECTORY);
163 strcat(daemon->runtime_application_cfg,DLT_RUNTIME_APPLICATION_CFG);
164 if(runtime_directory[0])
165 strcpy(daemon->runtime_context_cfg,runtime_directory);
167 strcpy(daemon->runtime_context_cfg,DLT_RUNTIME_DEFAULT_DIRECTORY);
168 strcat(daemon->runtime_context_cfg,DLT_RUNTIME_CONTEXT_CFG);
169 if(runtime_directory[0])
170 strcpy(daemon->runtime_configuration,runtime_directory);
172 strcpy(daemon->runtime_configuration,DLT_RUNTIME_DEFAULT_DIRECTORY);
173 strcat(daemon->runtime_configuration,DLT_RUNTIME_CONFIGURATION);
175 /* Check for runtime cfg, if it is loadable, load it! */
176 if ((dlt_daemon_applications_load(daemon,daemon->runtime_application_cfg, verbose)==0) &&
177 (dlt_daemon_contexts_load(daemon,daemon->runtime_context_cfg, verbose)==0))
179 daemon->runtime_context_cfg_loaded = 1;
182 /* load configuration if available */
183 dlt_daemon_configuration_load(daemon,daemon->runtime_configuration, verbose);
185 daemon->sendserialheader = 0;
186 daemon->timingpackets = 0;
188 dlt_set_id(daemon->ecuid,"");
190 /* initialize ring buffer for client connection */
191 if (dlt_buffer_init_dynamic(&(daemon->client_ringbuffer), DLT_DAEMON_RINGBUFFER_MIN_SIZE,DLT_DAEMON_RINGBUFFER_MAX_SIZE,DLT_DAEMON_RINGBUFFER_STEP_SIZE)==-1)
199 int dlt_daemon_free(DltDaemon *daemon,int verbose)
201 PRINT_FUNCTION_VERBOSE(verbose);
209 if (dlt_daemon_contexts_clear(daemon, verbose)==-1)
214 /* Free applications */
215 if (dlt_daemon_applications_clear(daemon, verbose)==-1)
220 /* free ringbuffer */
221 dlt_buffer_free_dynamic(&(daemon->client_ringbuffer));
226 int dlt_daemon_applications_clear(DltDaemon *daemon,int verbose)
230 PRINT_FUNCTION_VERBOSE(verbose);
237 for (i=0; i<daemon->num_applications; i++)
239 if (daemon->applications[i].application_description!=0)
241 free(daemon->applications[i].application_description);
242 daemon->applications[i].application_description = 0;
246 if (daemon->applications)
248 free(daemon->applications);
251 daemon->applications = 0;
252 daemon->num_applications = 0;
257 DltDaemonApplication* dlt_daemon_application_add(DltDaemon *daemon,char *apid,pid_t pid,char *description, int verbose)
259 DltDaemonApplication *application;
260 DltDaemonApplication *old;
263 char filename[DLT_DAEMON_TEXTBUFSIZE];
265 if ((daemon==0) || (apid==0) || (apid[0]=='\0'))
267 return (DltDaemonApplication*) 0;
270 if (daemon->applications == 0)
272 daemon->applications = (DltDaemonApplication*) malloc(sizeof(DltDaemonApplication)*DLT_DAEMON_APPL_ALLOC_SIZE);
273 if (daemon->applications==0)
275 return (DltDaemonApplication*) 0;
281 /* Check if application [apid] is already available */
282 application = dlt_daemon_application_find(daemon, apid, verbose);
285 daemon->num_applications += 1;
287 if (daemon->num_applications!=0)
289 if ((daemon->num_applications%DLT_DAEMON_APPL_ALLOC_SIZE)==0)
291 /* allocate memory in steps of DLT_DAEMON_APPL_ALLOC_SIZE, e.g. 100 */
292 old = daemon->applications;
293 daemon->applications = (DltDaemonApplication*) malloc(sizeof(DltDaemonApplication)*
294 ((daemon->num_applications/DLT_DAEMON_APPL_ALLOC_SIZE)+1)*DLT_DAEMON_APPL_ALLOC_SIZE);
295 if (daemon->applications==0)
297 daemon->applications = old;
298 return (DltDaemonApplication*) 0;
300 memcpy(daemon->applications,old,sizeof(DltDaemonApplication)*daemon->num_applications);
305 application = &(daemon->applications[daemon->num_applications-1]);
307 dlt_set_id(application->apid,apid);
308 application->application_description = 0;
309 application->num_contexts = 0;
314 /* Store application description and pid of application */
315 if (application->application_description)
317 free(application->application_description);
320 application->application_description=0;
324 application->application_description = malloc(strlen(description)+1);
325 if (application->application_description)
327 strncpy(application->application_description,description,strlen(description)+1);
328 application->application_description[strlen(description)]='\0';
332 application->pid = pid;
334 application->user_handle = -1;
338 sprintf(filename,"%s/dlt%d",DLT_USER_DIR,application->pid);
340 dlt_user_handle = open(filename, O_WRONLY|O_NONBLOCK);
341 if (dlt_user_handle <0)
343 sprintf(str,"open() failed to %s, errno=%d (%s)!\n",filename,errno,strerror(errno)); /* errno 2: ENOENT - No such file or directory */
344 dlt_log(LOG_ERR, str);
347 application->user_handle = dlt_user_handle;
353 qsort(daemon->applications,daemon->num_applications,sizeof(DltDaemonApplication),dlt_daemon_cmp_apid);
355 /* Find new position of application with apid*/
356 application = dlt_daemon_application_find(daemon, apid, verbose);
362 int dlt_daemon_application_del(DltDaemon *daemon, DltDaemonApplication *application, int verbose)
366 PRINT_FUNCTION_VERBOSE(verbose);
368 if ((daemon==0) || (application==0))
373 if (daemon->num_applications>0)
375 /* Check if user handle is open; if yes, close it */
376 if (application->user_handle!=-1)
378 close(application->user_handle);
379 application->user_handle=-1;
382 /* Free description of application to be deleted */
383 if (application->application_description)
385 free(application->application_description);
386 application->application_description = 0;
389 pos = application-(daemon->applications);
391 /* move all applications above pos to pos */
392 memmove(&(daemon->applications[pos]),&(daemon->applications[pos+1]), sizeof(DltDaemonApplication)*((daemon->num_applications-1)-pos));
394 /* Clear last application */
395 memset(&(daemon->applications[daemon->num_applications-1]),0,sizeof(DltDaemonApplication));
397 daemon->num_applications--;
404 DltDaemonApplication* dlt_daemon_application_find(DltDaemon *daemon,char *apid,int verbose)
406 DltDaemonApplication application;
408 PRINT_FUNCTION_VERBOSE(verbose);
410 if ((daemon==0) || (apid==0) || (apid[0]=='\0') || (daemon->num_applications==0))
412 return (DltDaemonApplication*) 0;
415 /* Check, if apid is smaller than smallest apid or greater than greatest apid */
416 if ((memcmp(apid,daemon->applications[0].apid,DLT_ID_SIZE)<0) ||
417 (memcmp(apid,daemon->applications[daemon->num_applications-1].apid,DLT_ID_SIZE)>0))
419 return (DltDaemonApplication*) 0;
422 dlt_set_id(application.apid,apid);
423 return (DltDaemonApplication*)bsearch(&application,daemon->applications,daemon->num_applications,sizeof(DltDaemonApplication),dlt_daemon_cmp_apid);
426 int dlt_daemon_applications_load(DltDaemon *daemon,const char *filename, int verbose)
430 char buf[DLT_DAEMON_TEXTBUFSIZE];
434 PRINT_FUNCTION_VERBOSE(verbose);
436 if ((daemon==0) || (filename==0) || (filename[0]=='\0'))
441 fd=fopen(filename, "r");
451 memset(buf, 0, sizeof(buf));
454 ret=fgets(buf,sizeof(buf),fd);
456 if (strcmp(buf,"")!=0)
462 /* pb contains now the description */
464 /* pid is unknown at loading time */
465 if (dlt_daemon_application_add(daemon,apid,0,pb,verbose)==0)
477 int dlt_daemon_applications_save(DltDaemon *daemon,const char *filename, int verbose)
482 char apid[DLT_ID_SIZE+1]; /* DLT_ID_SIZE+1, because the 0-termination is required here */
484 PRINT_FUNCTION_VERBOSE(verbose);
486 if ((daemon==0) || (filename==0) || (filename[0]=='\0'))
491 memset(apid,0, sizeof(apid));
493 if ((daemon->applications) && (daemon->num_applications>0))
495 fd=fopen(filename, "w");
498 for (i=0; i<daemon->num_applications; i++)
500 dlt_set_id(apid,daemon->applications[i].apid);
502 if ((daemon->applications[i].application_description) &&
503 (daemon->applications[i].application_description!='\0'))
505 fprintf(fd,"%s:%s:\n",apid, daemon->applications[i].application_description);
509 fprintf(fd,"%s::\n",apid);
519 DltDaemonContext* dlt_daemon_context_add(DltDaemon *daemon,char *apid,char *ctid,int8_t log_level,int8_t trace_status,int log_level_pos, int user_handle,char *description,int verbose)
521 DltDaemonApplication *application;
522 DltDaemonContext *context;
523 DltDaemonContext *old;
526 PRINT_FUNCTION_VERBOSE(verbose);
528 if ((daemon==0) || (apid==0) || (apid[0]=='\0') || (ctid==0) || (ctid[0]=='\0'))
530 return (DltDaemonContext*) 0;
533 if ((log_level<DLT_LOG_DEFAULT) || (log_level>DLT_LOG_VERBOSE))
535 return (DltDaemonContext*) 0;
538 if ((trace_status<DLT_TRACE_STATUS_DEFAULT) || (trace_status>DLT_TRACE_STATUS_ON))
540 return (DltDaemonContext*) 0;
543 if (daemon->contexts == 0)
545 daemon->contexts = (DltDaemonContext*) malloc(sizeof(DltDaemonContext)*DLT_DAEMON_CONTEXT_ALLOC_SIZE);
546 if (daemon->contexts==0)
548 return (DltDaemonContext*) 0;
552 /* Check if application [apid] is available */
553 application = dlt_daemon_application_find(daemon, apid, verbose);
556 return (DltDaemonContext*) 0;
559 /* Check if context [apid, ctid] is already available */
560 context = dlt_daemon_context_find(daemon, apid, ctid, verbose);
563 daemon->num_contexts += 1;
565 if (daemon->num_contexts!=0)
567 if ((daemon->num_contexts%DLT_DAEMON_CONTEXT_ALLOC_SIZE)==0)
569 /* allocate memory for context in steps of DLT_DAEMON_CONTEXT_ALLOC_SIZE, e.g 100 */
570 old = daemon->contexts;
571 daemon->contexts = (DltDaemonContext*) malloc(sizeof(DltDaemonContext)*
572 ((daemon->num_contexts/DLT_DAEMON_CONTEXT_ALLOC_SIZE)+1)*DLT_DAEMON_CONTEXT_ALLOC_SIZE);
573 if (daemon->contexts==0)
575 daemon->contexts = old;
576 return (DltDaemonContext*) 0;
578 memcpy(daemon->contexts,old,sizeof(DltDaemonContext)*daemon->num_contexts);
583 context = &(daemon->contexts[daemon->num_contexts-1]);
585 dlt_set_id(context->apid,apid);
586 dlt_set_id(context->ctid,ctid);
587 context->context_description = 0;
589 application->num_contexts++;
593 /* Set context description */
594 if (context->context_description)
596 free(context->context_description);
599 context->context_description=0;
603 context->context_description = malloc(strlen(description)+1);
605 if (context->context_description)
607 strncpy(context->context_description,description,strlen(description)+1);
608 context->context_description[strlen(description)]='\0';
612 /* Store log level and trace status,
613 if this is a new context, or
614 if this is an old context and the runtime cfg was not loaded */
616 if ((new_context==1) ||
617 ((new_context==0) && (daemon->runtime_context_cfg_loaded==0)))
619 context->log_level = log_level;
620 context->trace_status = trace_status;
623 context->log_level_pos = log_level_pos;
624 context->user_handle = user_handle;
629 qsort(daemon->contexts,daemon->num_contexts, sizeof(DltDaemonContext),dlt_daemon_cmp_apid_ctid);
631 /* Find new position of context with apid, ctid */
632 context = dlt_daemon_context_find(daemon, apid, ctid, verbose);
638 int dlt_daemon_context_del(DltDaemon *daemon, DltDaemonContext* context, int verbose)
641 DltDaemonApplication *application;
643 PRINT_FUNCTION_VERBOSE(verbose);
645 if ((daemon==0) || (context==0))
650 if (daemon->num_contexts>0)
652 application = dlt_daemon_application_find(daemon, context->apid, verbose);
654 /* Free description of context to be deleted */
655 if (context->context_description)
657 free(context->context_description);
658 context->context_description = 0;
661 pos = context-(daemon->contexts);
663 /* move all contexts above pos to pos */
664 memmove(&(daemon->contexts[pos]),&(daemon->contexts[pos+1]), sizeof(DltDaemonContext)*((daemon->num_contexts-1)-pos));
666 /* Clear last context */
667 memset(&(daemon->contexts[daemon->num_contexts-1]),0,sizeof(DltDaemonContext));
669 daemon->num_contexts--;
671 /* Check if application [apid] is available */
674 application->num_contexts--;
681 DltDaemonContext* dlt_daemon_context_find(DltDaemon *daemon,char *apid,char *ctid,int verbose)
683 DltDaemonContext context;
685 PRINT_FUNCTION_VERBOSE(verbose);
687 if ((daemon==0) || (apid==0) || (apid[0]=='\0') || (ctid==0) || (ctid[0]=='\0') || (daemon->num_contexts==0))
689 return (DltDaemonContext*) 0;
692 /* Check, if apid is smaller than smallest apid or greater than greatest apid */
693 if ((memcmp(apid,daemon->contexts[0].apid,DLT_ID_SIZE)<0) ||
694 (memcmp(apid,daemon->contexts[daemon->num_contexts-1].apid,DLT_ID_SIZE)>0))
696 return (DltDaemonContext*) 0;
699 dlt_set_id(context.apid,apid);
700 dlt_set_id(context.ctid,ctid);
702 return (DltDaemonContext*)bsearch(&context,daemon->contexts,daemon->num_contexts,sizeof(DltDaemonContext),dlt_daemon_cmp_apid_ctid);
705 int dlt_daemon_contexts_clear(DltDaemon *daemon,int verbose)
709 PRINT_FUNCTION_VERBOSE(verbose);
716 for (i=0; i<daemon->num_contexts; i++)
718 if (daemon->contexts[i].context_description!=0)
720 free(daemon->contexts[i].context_description);
721 daemon->contexts[i].context_description = 0;
725 if (daemon->contexts)
727 free(daemon->contexts);
730 daemon->contexts = 0;
732 for (i=0; i<daemon->num_applications; i++)
734 daemon->applications[i].num_contexts = 0;
737 daemon->num_contexts = 0;
742 int dlt_daemon_contexts_load(DltDaemon *daemon,const char *filename, int verbose)
746 char buf[DLT_DAEMON_TEXTBUFSIZE];
751 PRINT_FUNCTION_VERBOSE(verbose);
753 if ((daemon==0) || (filename==0) ||( filename[0]=='\0'))
758 fd=fopen(filename, "r");
768 memset(buf, 0, sizeof(buf));
771 ret=fgets(buf,sizeof(buf),fd);
773 if (strcmp(buf,"")!=0)
785 /* pb contains now the description */
787 /* log_level_pos, and user_handle are unknown at loading time */
788 if (dlt_daemon_context_add(daemon,apid,ctid,(int8_t)ll,(int8_t)ts,0,0,pb,verbose)==0)
800 int dlt_daemon_contexts_save(DltDaemon *daemon,const char *filename, int verbose)
805 char apid[DLT_ID_SIZE+1], ctid[DLT_ID_SIZE+1]; /* DLT_ID_SIZE+1, because the 0-termination is required here */
807 PRINT_FUNCTION_VERBOSE(verbose);
809 if ((daemon==0) || (filename==0) ||( filename[0]=='\0'))
814 memset(apid,0, sizeof(apid));
815 memset(ctid,0, sizeof(ctid));
817 if ((daemon->contexts) && (daemon->num_contexts>0))
819 fd=fopen(filename, "w");
822 for (i=0; i<daemon->num_contexts; i++)
824 dlt_set_id(apid,daemon->contexts[i].apid);
825 dlt_set_id(ctid,daemon->contexts[i].ctid);
827 if ((daemon->contexts[i].context_description) &&
828 (daemon->contexts[i].context_description[0]!='\0'))
830 fprintf(fd,"%s:%s:%d:%d:%s:\n",apid,ctid,
831 (int)(daemon->contexts[i].log_level),
832 (int)(daemon->contexts[i].trace_status),
833 daemon->contexts[i].context_description);
837 fprintf(fd,"%s:%s:%d:%d::\n",apid,ctid,
838 (int)(daemon->contexts[i].log_level),
839 (int)(daemon->contexts[i].trace_status));
849 int dlt_daemon_configuration_save(DltDaemon *daemon,const char *filename, int verbose)
853 PRINT_FUNCTION_VERBOSE(verbose);
855 if ((daemon==0) || (filename==0) ||( filename[0]=='\0'))
860 fd=fopen(filename, "w");
863 fprintf(fd,"# 0 = off, 1 = external, 2 = internal, 3 = both\n");
864 fprintf(fd,"LoggingMode = %d\n",daemon->mode);
872 int dlt_daemon_configuration_load(DltDaemon *daemon,const char *filename, int verbose)
880 PRINT_FUNCTION_VERBOSE(verbose);
882 pFile = fopen (filename,"r");
888 /* fetch line from configuration file */
889 if ( fgets (line , 1024 , pFile) != NULL )
891 pch = strtok (line," =\r\n");
897 if(strcmp(pch,"#")==0)
902 strncpy(token,pch,sizeof(token));
906 strncpy(value,pch,sizeof(value));
910 pch = strtok (NULL, " =\r\n");
913 if(token[0] && value[0])
915 /* parse arguments here */
916 if(strcmp(token,"LoggingMode")==0)
918 daemon->mode = atoi(value);
919 sprintf(str,"Runtime Option: %s=%d\n",token,daemon->mode);
920 dlt_log(LOG_INFO, str);
924 sprintf(str,"Unknown option: %s=%s\n",token,value);
925 dlt_log(LOG_ERR, str);
938 sprintf(str,"Cannot open configuration file: %s\n",filename);
939 dlt_log(LOG_WARNING, str);
945 int dlt_daemon_user_send_log_level(DltDaemon *daemon,DltDaemonContext *context,int verbose)
947 DltUserHeader userheader;
948 DltUserControlMsgLogLevel usercontext;
951 PRINT_FUNCTION_VERBOSE(verbose);
953 if ((daemon==0) || (context==0))
958 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG_LEVEL)==-1)
963 usercontext.log_level = ((context->log_level == DLT_LOG_DEFAULT)?daemon->default_log_level:context->log_level);
964 usercontext.trace_status = ((context->trace_status == DLT_TRACE_STATUS_DEFAULT)?daemon->default_trace_status:context->trace_status);
966 usercontext.log_level_pos = context->log_level_pos;
969 ret = dlt_user_log_out2(context->user_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgLogLevel));
971 if (ret!=DLT_RETURN_OK)
975 /* Close connection */
976 close(context->user_handle);
977 context->user_handle=0;
981 return ((ret==DLT_RETURN_OK)?0:-1);
984 int dlt_daemon_user_send_log_state(DltDaemon *daemon,DltDaemonApplication *app,int verbose)
986 DltUserHeader userheader;
987 DltUserControlMsgLogState logstate;
990 PRINT_FUNCTION_VERBOSE(verbose);
992 if ((daemon==0) || (app==0))
997 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG_STATE)==-1)
1002 logstate.log_state = daemon->state;
1005 ret = dlt_user_log_out2(app->user_handle, &(userheader), sizeof(DltUserHeader), &(logstate), sizeof(DltUserControlMsgLogState));
1007 if (ret!=DLT_RETURN_OK)
1011 /* Close connection */
1012 close(app->user_handle);
1017 return ((ret==DLT_RETURN_OK)?0:-1);
1020 int dlt_daemon_control_process_control(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1022 uint32_t id,id_tmp=0;
1024 PRINT_FUNCTION_VERBOSE(verbose);
1026 if ((daemon==0) || (msg==0))
1031 if (msg->datasize < (int32_t)sizeof(uint32_t))
1036 id_tmp = *((uint32_t*)(msg->databuffer));
1037 id=DLT_ENDIAN_GET_32(msg->standardheader->htyp ,id_tmp);
1039 if ((id > 0) && (id <= DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW))
1041 /* Control message handling */
1044 case DLT_SERVICE_ID_SET_LOG_LEVEL:
1046 dlt_daemon_control_set_log_level(sock, daemon, msg, verbose);
1049 case DLT_SERVICE_ID_SET_TRACE_STATUS:
1051 dlt_daemon_control_set_trace_status(sock, daemon, msg, verbose);
1054 case DLT_SERVICE_ID_GET_LOG_INFO:
1056 dlt_daemon_control_get_log_info(sock, daemon, msg, verbose);
1059 case DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL:
1061 dlt_daemon_control_get_default_log_level(sock, daemon, verbose);
1064 case DLT_SERVICE_ID_STORE_CONFIG:
1066 if (dlt_daemon_applications_save(daemon, daemon->runtime_application_cfg, verbose)==0)
1068 if (dlt_daemon_contexts_save(daemon, daemon->runtime_context_cfg, verbose)==0)
1070 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1074 /* Delete saved files */
1075 dlt_daemon_control_reset_to_factory_default(daemon, daemon->runtime_application_cfg, daemon->runtime_context_cfg, verbose);
1076 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1081 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1085 case DLT_SERVICE_ID_RESET_TO_FACTORY_DEFAULT:
1087 dlt_daemon_control_reset_to_factory_default(daemon, daemon->runtime_application_cfg, daemon->runtime_context_cfg, verbose);
1088 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1091 case DLT_SERVICE_ID_SET_COM_INTERFACE_STATUS:
1093 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1096 case DLT_SERVICE_ID_SET_COM_INTERFACE_MAX_BANDWIDTH:
1098 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1101 case DLT_SERVICE_ID_SET_VERBOSE_MODE:
1103 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1106 case DLT_SERVICE_ID_SET_MESSAGE_FILTERING:
1108 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1111 case DLT_SERVICE_ID_SET_TIMING_PACKETS:
1113 dlt_daemon_control_set_timing_packets(sock, daemon, msg, verbose);
1116 case DLT_SERVICE_ID_GET_LOCAL_TIME:
1118 /* Send response with valid timestamp (TMSP) field */
1119 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1122 case DLT_SERVICE_ID_USE_ECU_ID:
1124 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1127 case DLT_SERVICE_ID_USE_SESSION_ID:
1129 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1132 case DLT_SERVICE_ID_USE_TIMESTAMP:
1134 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1137 case DLT_SERVICE_ID_USE_EXTENDED_HEADER:
1139 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1142 case DLT_SERVICE_ID_SET_DEFAULT_LOG_LEVEL:
1144 dlt_daemon_control_set_default_log_level(sock, daemon, msg, verbose);
1147 case DLT_SERVICE_ID_SET_DEFAULT_TRACE_STATUS:
1149 dlt_daemon_control_set_default_trace_status(sock, daemon, msg, verbose);
1152 case DLT_SERVICE_ID_GET_SOFTWARE_VERSION:
1154 dlt_daemon_control_get_software_version(sock, daemon, verbose);
1157 case DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW:
1159 dlt_daemon_control_message_buffer_overflow(sock, daemon, verbose);
1164 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1171 /* Injection handling */
1172 dlt_daemon_control_callsw_cinjection(sock, daemon, msg, verbose);
1178 void dlt_daemon_control_callsw_cinjection(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1180 char apid[DLT_ID_SIZE],ctid[DLT_ID_SIZE];
1181 uint32_t id=0,id_tmp=0;
1183 DltDaemonContext *context;
1184 uint32_t data_length_inject=0,data_length_inject_tmp=0;
1188 DltUserHeader userheader;
1189 DltUserControlMsgInjection usercontext;
1190 uint8_t *userbuffer;
1192 PRINT_FUNCTION_VERBOSE(verbose);
1194 if ((daemon==0) || (msg==0))
1199 datalength = msg->datasize;
1200 ptr = msg->databuffer;
1207 DLT_MSG_READ_VALUE(id_tmp,ptr,datalength,uint32_t); /* Get service id */
1208 id=DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp);
1210 if ((id>=DLT_DAEMON_INJECTION_MIN) && (id<=DLT_DAEMON_INJECTION_MAX))
1212 /* This a a real SW-C injection call */
1213 data_length_inject=0;
1214 data_length_inject_tmp=0;
1216 DLT_MSG_READ_VALUE(data_length_inject_tmp,ptr,datalength,uint32_t); /* Get data length */
1217 data_length_inject=DLT_ENDIAN_GET_32(msg->standardheader->htyp, data_length_inject_tmp);
1219 /* Get context handle for apid, ctid (and seid) */
1220 /* Warning: seid is ignored in this implementation! */
1221 if (DLT_IS_HTYP_UEH(msg->standardheader->htyp))
1223 dlt_set_id(apid, msg->extendedheader->apid);
1224 dlt_set_id(ctid, msg->extendedheader->ctid);
1228 /* No extended header, and therefore no apid and ctid available */
1229 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1233 /* At this point, apid and ctid is available */
1234 context=dlt_daemon_context_find(daemon, apid, ctid, verbose);
1238 // dlt_log(LOG_INFO,"No context found!\n");
1239 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1243 /* Send user message to handle, specified in context */
1244 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_INJECTION)==-1)
1246 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1250 usercontext.log_level_pos = context->log_level_pos;
1252 userbuffer = malloc(data_length_inject);
1256 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1260 usercontext.data_length_inject = data_length_inject;
1261 usercontext.service_id = id;
1263 memcpy(userbuffer,ptr,data_length_inject); /* Copy received injection to send buffer */
1266 if (dlt_user_log_out3(context->user_handle, &(userheader), sizeof(DltUserHeader),
1267 &(usercontext), sizeof(DltUserControlMsgInjection),
1268 userbuffer, data_length_inject)!=DLT_RETURN_OK)
1272 /* Close connection */
1273 close(context->user_handle);
1274 context->user_handle=0;
1276 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1280 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1290 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1294 void dlt_daemon_control_set_log_level(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1296 PRINT_FUNCTION_VERBOSE(verbose);
1298 char apid[DLT_ID_SIZE],ctid[DLT_ID_SIZE];
1299 DltServiceSetLogLevel *req;
1300 DltDaemonContext *context;
1301 int32_t id=DLT_SERVICE_ID_SET_LOG_LEVEL;
1303 int8_t old_log_level;
1305 if ((daemon==0) || (msg==0))
1310 req = (DltServiceSetLogLevel*) (msg->databuffer);
1312 dlt_set_id(apid, req->apid);
1313 dlt_set_id(ctid, req->ctid);
1315 context=dlt_daemon_context_find(daemon, apid, ctid, verbose);
1320 old_log_level = context->log_level;
1321 context->log_level = req->log_level; /* No endianess conversion necessary*/
1323 if ((context->user_handle!=0) &&
1324 (dlt_daemon_user_send_log_level(daemon, context, verbose)==0))
1326 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1330 //dlt_log(LOG_ERR, "Log level could not be sent!\n");
1331 context->log_level = old_log_level;
1332 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1337 //dlt_log(LOG_ERR, "Context not found!\n");
1338 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1342 void dlt_daemon_control_set_trace_status(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1344 PRINT_FUNCTION_VERBOSE(verbose);
1346 char apid[DLT_ID_SIZE],ctid[DLT_ID_SIZE];
1347 DltServiceSetLogLevel *req; /* request uses same struct as set log level */
1348 DltDaemonContext *context;
1349 int32_t id=DLT_SERVICE_ID_SET_TRACE_STATUS;
1351 int8_t old_trace_status;
1353 if ((daemon==0) || (msg==0))
1358 req = (DltServiceSetLogLevel*) (msg->databuffer);
1360 dlt_set_id(apid, req->apid);
1361 dlt_set_id(ctid, req->ctid);
1363 context=dlt_daemon_context_find(daemon, apid, ctid, verbose);
1368 old_trace_status = context->trace_status;
1369 context->trace_status = req->log_level; /* No endianess conversion necessary */
1371 if ((context->user_handle!=0) &&
1372 (dlt_daemon_user_send_log_level(daemon, context, verbose)==0))
1374 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1378 //dlt_log(LOG_ERR, "Trace Status could not be sent!\n");
1379 context->trace_status = old_trace_status;
1380 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1385 //dlt_log(LOG_ERR, "Context not found!\n");
1386 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1390 void dlt_daemon_control_set_default_log_level(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1392 PRINT_FUNCTION_VERBOSE(verbose);
1394 DltServiceSetDefaultLogLevel *req;
1395 int32_t id=DLT_SERVICE_ID_SET_DEFAULT_LOG_LEVEL;
1397 if ((daemon==0) || (msg==0))
1402 req = (DltServiceSetDefaultLogLevel*) (msg->databuffer);
1404 /* No endianess conversion necessary */
1405 if (/*(req->log_level>=0) &&*/
1406 (req->log_level<=DLT_LOG_VERBOSE))
1408 daemon->default_log_level = req->log_level; /* No endianess conversion necessary */
1410 /* Send Update to all contexts using the default log level */
1411 dlt_daemon_user_send_default_update(daemon, verbose);
1413 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1417 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1421 void dlt_daemon_control_set_default_trace_status(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1423 PRINT_FUNCTION_VERBOSE(verbose);
1425 /* Payload of request message */
1426 DltServiceSetDefaultLogLevel *req;
1427 int32_t id=DLT_SERVICE_ID_SET_DEFAULT_TRACE_STATUS;
1429 if ((daemon==0) || (msg==0))
1434 req = (DltServiceSetDefaultLogLevel*) (msg->databuffer);
1436 /* No endianess conversion necessary */
1437 if ((req->log_level==DLT_TRACE_STATUS_OFF) ||
1438 (req->log_level==DLT_TRACE_STATUS_ON))
1440 daemon->default_trace_status = req->log_level; /* No endianess conversion necessary*/
1442 /* Send Update to all contexts using the default trace status */
1443 dlt_daemon_user_send_default_update(daemon, verbose);
1445 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1449 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1453 void dlt_daemon_control_set_timing_packets(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1455 PRINT_FUNCTION_VERBOSE(verbose);
1457 DltServiceSetVerboseMode *req; /* request uses same struct as set verbose mode */
1458 int32_t id=DLT_SERVICE_ID_SET_TIMING_PACKETS;
1460 if ((daemon==0) || (msg==0))
1465 req = (DltServiceSetVerboseMode*) (msg->databuffer);
1466 if ((req->new_status==0) || (req->new_status==1))
1468 daemon->timingpackets = req->new_status;
1470 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1474 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1478 void dlt_daemon_control_get_software_version(int sock, DltDaemon *daemon, int verbose)
1480 char version[DLT_DAEMON_TEXTBUFSIZE];
1483 DltServiceGetSoftwareVersionResponse *resp;
1485 PRINT_FUNCTION_VERBOSE(verbose);
1492 /* initialise new message */
1493 if (dlt_message_init(&msg,0)==-1)
1495 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_SOFTWARE_VERSION, DLT_SERVICE_RESPONSE_ERROR, verbose);
1499 /* prepare payload of data */
1500 dlt_get_version(version);
1501 len = strlen(version);
1503 msg.datasize = sizeof(DltServiceGetSoftwareVersionResponse) + len;
1506 free(msg.databuffer);
1508 msg.databuffer = (uint8_t *) malloc(msg.datasize);
1509 if (msg.databuffer==0)
1511 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_SOFTWARE_VERSION, DLT_SERVICE_RESPONSE_ERROR, verbose);
1515 resp = (DltServiceGetSoftwareVersionResponse*) msg.databuffer;
1516 resp->service_id = DLT_SERVICE_ID_GET_SOFTWARE_VERSION;
1517 resp->status = DLT_SERVICE_RESPONSE_OK;
1519 memcpy(msg.databuffer+sizeof(DltServiceGetSoftwareVersionResponse),version,len);
1522 dlt_daemon_control_send_control_message(sock, daemon, &msg,"","", verbose);
1525 dlt_message_free(&msg,0);
1528 void dlt_daemon_control_get_default_log_level(int sock, DltDaemon *daemon, int verbose)
1531 DltServiceGetDefaultLogLevelResponse *resp;
1533 PRINT_FUNCTION_VERBOSE(verbose);
1540 /* initialise new message */
1541 if (dlt_message_init(&msg,0)==-1)
1543 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
1547 msg.datasize = sizeof(DltServiceGetDefaultLogLevelResponse);
1550 free(msg.databuffer);
1552 msg.databuffer = (uint8_t *) malloc(msg.datasize);
1553 if (msg.databuffer==0)
1555 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
1559 resp = (DltServiceGetDefaultLogLevelResponse*) msg.databuffer;
1560 resp->service_id = DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL;
1561 resp->status = DLT_SERVICE_RESPONSE_OK;
1562 resp->log_level = daemon->default_log_level;
1565 dlt_daemon_control_send_control_message(sock,daemon,&msg,"","", verbose);
1568 dlt_message_free(&msg,0);
1571 void dlt_daemon_control_get_log_info(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1573 DltServiceGetLogInfoRequest *req;
1575 DltDaemonContext *context=0;
1576 DltDaemonApplication *application=0;
1578 int num_applications=0, num_contexts=0;
1579 uint16_t count_app_ids=0, count_con_ids=0;
1581 #if (DLT_DEBUG_GETLOGINFO==1)
1585 int32_t i,j,offset=0;
1595 PRINT_FUNCTION_VERBOSE(verbose);
1597 if ((daemon==0) || (msg==0))
1602 /* prepare pointer to message request */
1603 req = (DltServiceGetLogInfoRequest*) (msg->databuffer);
1605 /* initialise new message */
1606 if (dlt_message_init(&resp,0)==-1)
1608 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
1613 if ((req->options < 3 ) || (req->options>7))
1615 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
1619 if (req->apid[0]!='\0')
1621 application = dlt_daemon_application_find(daemon, req->apid, verbose);
1624 num_applications = 1;
1625 if (req->ctid[0]!='\0')
1627 context = dlt_daemon_context_find(daemon, req->apid, req->ctid, verbose);
1629 num_contexts = ((context)?1:0);
1633 num_contexts = application->num_contexts;
1638 num_applications = 0;
1644 /* Request all applications and contexts */
1645 num_applications = daemon->num_applications;
1646 num_contexts = daemon->num_contexts;
1649 /* prepare payload of data */
1651 /* Calculate maximum size for a response */
1652 resp.datasize = sizeof(uint32_t) /* SID */ + sizeof(int8_t) /* status*/ + sizeof(ID4) /* DLT_DAEMON_REMO_STRING */;
1654 sizecont = sizeof(uint32_t) /* context_id */;
1656 /* Add additional size for response of Mode 4, 6, 7 */
1657 if ((req->options==4) || (req->options==6) || (req->options==7))
1659 sizecont += sizeof(int8_t); /* log level */
1662 /* Add additional size for response of Mode 5, 6, 7 */
1663 if ((req->options==5) || (req->options==6) || (req->options==7))
1665 sizecont+= sizeof(int8_t); /* trace status */
1668 resp.datasize+= (num_applications * (sizeof(uint32_t) /* app_id */ + sizeof(uint16_t) /* count_con_ids */)) +
1669 (num_contexts * sizecont);
1671 resp.datasize+= sizeof(uint16_t) /* count_app_ids */;
1673 /* Add additional size for response of Mode 7 */
1674 if (req->options==7)
1676 if (req->apid[0]!='\0')
1678 if (req->ctid[0]!='\0')
1680 /* One application, one context */
1681 // context = dlt_daemon_context_find(daemon, req->apid, req->ctid, verbose);
1684 resp.datasize+=sizeof(uint16_t) /* len_context_description */;
1685 if (context->context_description!=0)
1687 resp.datasize+=strlen(context->context_description); /* context_description */
1693 /* One application, all contexts */
1694 if ((daemon->applications) && (application))
1696 /* Calculate start offset within contexts[] */
1698 for (i=0; i<(application-(daemon->applications)); i++)
1700 offset_base+=daemon->applications[i].num_contexts;
1703 /* Iterate over all contexts belonging to this application */
1704 for (j=0;j<application->num_contexts;j++)
1707 context = &(daemon->contexts[offset_base+j]);
1710 resp.datasize+=sizeof(uint16_t) /* len_context_description */;
1711 if (context->context_description!=0)
1713 resp.datasize+=strlen(context->context_description); /* context_description */
1720 /* Space for application description */
1723 resp.datasize+=sizeof(uint16_t) /* len_app_description */;
1724 if (application->application_description!=0)
1726 resp.datasize+=strlen(application->application_description); /* app_description */
1732 /* All applications, all contexts */
1733 for (i=0;i<daemon->num_contexts;i++)
1735 resp.datasize+=sizeof(uint16_t) /* len_context_description */;
1736 if (daemon->contexts[i].context_description!=0)
1738 resp.datasize+=strlen(daemon->contexts[i].context_description); /* context_description */
1742 for (i=0;i<daemon->num_applications;i++)
1744 resp.datasize+=sizeof(uint16_t) /* len_app_description */;
1745 if (daemon->applications[i].application_description!=0)
1747 resp.datasize+=strlen(daemon->applications[i].application_description); /* app_description */
1755 sprintf(str,"Allocate %d bytes for response msg databuffer\n", resp.datasize);
1756 dlt_log(LOG_INFO, str);
1759 /* Allocate buffer for response message */
1760 resp.databuffer = (uint8_t *) malloc(resp.datasize);
1761 if (resp.databuffer==0)
1763 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
1766 memset(resp.databuffer,0,resp.datasize);
1767 /* Preparation finished */
1769 /* Prepare response */
1770 sid = DLT_SERVICE_ID_GET_LOG_INFO;
1771 memcpy(resp.databuffer,&sid,sizeof(uint32_t));
1772 offset+=sizeof(uint32_t);
1774 value = (((num_applications!=0)&&(num_contexts!=0))?req->options:8); /* 8 = no matching context found */
1776 memcpy(resp.databuffer+offset,&value,sizeof(int8_t));
1777 offset+=sizeof(int8_t);
1779 count_app_ids = num_applications;
1781 if (count_app_ids!=0)
1783 memcpy(resp.databuffer+offset,&count_app_ids,sizeof(uint16_t));
1784 offset+=sizeof(uint16_t);
1786 #if (DLT_DEBUG_GETLOGINFO==1)
1787 sprintf(str,"#apid: %d \n", count_app_ids);
1788 dlt_log(LOG_DEBUG, str);
1791 for (i=0;i<count_app_ids;i++)
1793 if (req->apid[0]!='\0')
1799 if (daemon->applications)
1801 apid = daemon->applications[i].apid;
1805 /* This should never occur! */
1810 application = dlt_daemon_application_find(daemon, apid, verbose);
1814 /* Calculate start offset within contexts[] */
1816 for (j=0; j<(application-(daemon->applications)); j++)
1818 offset_base+=daemon->applications[j].num_contexts;
1821 dlt_set_id((char*)(resp.databuffer+offset),apid);
1822 offset+=sizeof(ID4);
1824 #if (DLT_DEBUG_GETLOGINFO==1)
1825 dlt_print_id(buf, apid);
1826 sprintf(str,"apid: %s\n",buf);
1827 dlt_log(LOG_DEBUG, str);
1830 if (req->apid[0]!='\0')
1832 count_con_ids = num_contexts;
1836 count_con_ids = application->num_contexts;
1839 memcpy(resp.databuffer+offset,&count_con_ids,sizeof(uint16_t));
1840 offset+=sizeof(uint16_t);
1842 #if (DLT_DEBUG_GETLOGINFO==1)
1843 sprintf(str,"#ctid: %d \n", count_con_ids);
1844 dlt_log(LOG_DEBUG, str);
1847 for (j=0;j<count_con_ids;j++)
1849 #if (DLT_DEBUG_GETLOGINFO==1)
1850 sprintf(str,"j: %d \n",j);
1851 dlt_log(LOG_DEBUG, str);
1853 if (!((count_con_ids==1) && (req->apid[0]!='\0') && (req->ctid[0]!='\0')))
1855 context = &(daemon->contexts[offset_base+j]);
1857 /* else: context was already searched and found
1858 (one application (found) with one context (found))*/
1861 ((req->ctid[0]=='\0') ||
1862 ((req->ctid[0]!='\0') && (memcmp(context->ctid,req->ctid,DLT_ID_SIZE)==0)))
1865 dlt_set_id((char*)(resp.databuffer+offset),context->ctid);
1866 offset+=sizeof(ID4);
1868 #if (DLT_DEBUG_GETLOGINFO==1)
1869 dlt_print_id(buf, context->ctid);
1870 sprintf(str,"ctid: %s \n",buf);
1871 dlt_log(LOG_DEBUG, str);
1875 if ((req->options==4) || (req->options==6) || (req->options==7))
1877 ll=context->log_level;
1878 memcpy(resp.databuffer+offset,&ll,sizeof(int8_t));
1879 offset+=sizeof(int8_t);
1883 if ((req->options==5) || (req->options==6) || (req->options==7))
1885 ts=context->trace_status;
1886 memcpy(resp.databuffer+offset,&ts,sizeof(int8_t));
1887 offset+=sizeof(int8_t);
1891 if (req->options==7)
1893 if (context->context_description)
1895 len = strlen(context->context_description);
1896 memcpy(resp.databuffer+offset,&len,sizeof(uint16_t));
1897 offset+=sizeof(uint16_t);
1898 memcpy(resp.databuffer+offset,context->context_description,strlen(context->context_description));
1899 offset+=strlen(context->context_description);
1904 memcpy(resp.databuffer+offset,&len,sizeof(uint16_t));
1905 offset+=sizeof(uint16_t);
1909 #if (DLT_DEBUG_GETLOGINFO==1)
1910 sprintf(str,"ll=%d ts=%d \n",(int32_t)ll,(int32_t)ts);
1911 dlt_log(LOG_DEBUG, str);
1915 #if (DLT_DEBUG_GETLOGINFO==1)
1916 dlt_log(LOG_DEBUG,"\n");
1921 if (req->options==7)
1923 if (application->application_description)
1925 len = strlen(application->application_description);
1926 memcpy(resp.databuffer+offset,&len,sizeof(uint16_t));
1927 offset+=sizeof(uint16_t);
1928 memcpy(resp.databuffer+offset,application->application_description,strlen(application->application_description));
1929 offset+=strlen(application->application_description);
1934 memcpy(resp.databuffer+offset,&len,sizeof(uint16_t));
1935 offset+=sizeof(uint16_t);
1938 } /* if (application) */
1939 } /* for (i=0;i<count_app_ids;i++) */
1940 } /* if (count_app_ids!=0) */
1942 dlt_set_id((char*)(resp.databuffer+offset),DLT_DAEMON_REMO_STRING);
1945 dlt_daemon_control_send_control_message(sock,daemon,&resp,"","", verbose);
1948 dlt_message_free(&resp,0);
1951 void dlt_daemon_control_message_buffer_overflow(int sock, DltDaemon *daemon, int verbose)
1954 DltServiceMessageBufferOverflowResponse *resp;
1956 PRINT_FUNCTION_VERBOSE(verbose);
1963 /* initialise new message */
1964 if (dlt_message_init(&msg,0)==-1)
1966 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW, DLT_SERVICE_RESPONSE_ERROR, verbose);
1970 /* prepare payload of data */
1971 msg.datasize = sizeof(DltServiceMessageBufferOverflowResponse);
1974 free(msg.databuffer);
1976 msg.databuffer = (uint8_t *) malloc(msg.datasize);
1977 if (msg.databuffer==0)
1979 if (sock!=DLT_DAEMON_STORE_TO_BUFFER)
1981 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW, DLT_SERVICE_RESPONSE_ERROR, verbose);
1986 resp = (DltServiceMessageBufferOverflowResponse*) msg.databuffer;
1987 resp->service_id = DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW;
1988 resp->status = DLT_SERVICE_RESPONSE_OK;
1989 resp->overflow = daemon->message_buffer_overflow;
1992 dlt_daemon_control_send_control_message(sock,daemon,&msg,"","", verbose);
1995 dlt_message_free(&msg,0);
1998 void dlt_daemon_control_service_response( int sock, DltDaemon *daemon, uint32_t service_id, int8_t status , int verbose)
2001 DltServiceResponse *resp;
2003 PRINT_FUNCTION_VERBOSE(verbose);
2010 /* initialise new message */
2011 if (dlt_message_init(&msg,0)==-1)
2016 /* prepare payload of data */
2017 msg.datasize = sizeof(DltServiceResponse);
2020 free(msg.databuffer);
2022 msg.databuffer = (uint8_t *) malloc(msg.datasize);
2023 if (msg.databuffer==0)
2028 resp = (DltServiceResponse*) msg.databuffer;
2029 resp->service_id = service_id;
2030 resp->status = status;
2033 dlt_daemon_control_send_control_message(sock,daemon,&msg,"","", verbose);
2036 dlt_message_free(&msg,0);
2039 void dlt_daemon_control_send_control_message( int sock, DltDaemon *daemon, DltMessage *msg, char* appid, char* ctid, int verbose)
2044 PRINT_FUNCTION_VERBOSE(verbose);
2046 if ((daemon==0) || (msg==0) || (appid==0) || (ctid==0))
2051 /* prepare storage header */
2052 msg->storageheader = (DltStorageHeader*)msg->headerbuffer;
2054 if (dlt_set_storageheader(msg->storageheader,daemon->ecuid)==-1)
2059 /* prepare standard header */
2060 msg->standardheader = (DltStandardHeader*)(msg->headerbuffer + sizeof(DltStorageHeader));
2061 msg->standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_UEH | DLT_HTYP_PROTOCOL_VERSION1 ;
2063 #if (BYTE_ORDER==BIG_ENDIAN)
2064 msg->standardheader->htyp = ( msg->standardheader->htyp | DLT_HTYP_MSBF);
2067 msg->standardheader->mcnt = 0;
2069 /* Set header extra parameters */
2070 dlt_set_id(msg->headerextra.ecu,daemon->ecuid);
2072 //msg->headerextra.seid = 0;
2074 msg->headerextra.tmsp = dlt_uptime();
2076 dlt_message_set_extraparameters(msg, verbose);
2078 /* prepare extended header */
2079 msg->extendedheader = (DltExtendedHeader*)(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp));
2080 msg->extendedheader->msin = DLT_MSIN_CONTROL_RESPONSE;
2082 msg->extendedheader->noar = 1; /* number of arguments */
2083 if (strcmp(appid,"")==0)
2085 dlt_set_id(msg->extendedheader->apid,DLT_DAEMON_CTRL_APID); /* application id */
2089 dlt_set_id(msg->extendedheader->apid, appid);
2091 if (strcmp(ctid,"")==0)
2093 dlt_set_id(msg->extendedheader->ctid,DLT_DAEMON_CTRL_CTID); /* context id */
2097 dlt_set_id(msg->extendedheader->ctid, ctid);
2100 /* prepare length information */
2101 msg->headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + sizeof(DltExtendedHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp);
2103 len=msg->headersize - sizeof(DltStorageHeader) + msg->datasize;
2106 dlt_log(LOG_CRIT,"Huge control message discarded!\n");
2110 msg->standardheader->len = DLT_HTOBE_16(((uint16_t)len));
2112 if (sock!=DLT_DAEMON_STORE_TO_BUFFER)
2117 DLT_DAEMON_SEM_LOCK();
2119 /* Optional: Send serial header, if requested */
2120 if (daemon->sendserialheader)
2122 ret=write(sock,dltSerialHeader,sizeof(dltSerialHeader));
2126 ret=write(sock, msg->headerbuffer+sizeof(DltStorageHeader),msg->headersize-sizeof(DltStorageHeader));
2127 ret=write(sock, msg->databuffer,msg->datasize);
2129 DLT_DAEMON_SEM_FREE();
2133 DLT_DAEMON_SEM_LOCK();
2135 /* Optional: Send serial header, if requested */
2136 if (daemon->sendserialheader)
2138 send(sock, dltSerialHeader,sizeof(dltSerialHeader),0);
2142 send(sock, msg->headerbuffer+sizeof(DltStorageHeader),msg->headersize-sizeof(DltStorageHeader),0);
2143 send(sock, msg->databuffer,msg->datasize,0);
2145 DLT_DAEMON_SEM_FREE();
2150 /* Store message in history buffer */
2151 if (dlt_buffer_push3(&(daemon->client_ringbuffer),
2152 msg->headerbuffer+sizeof(DltStorageHeader),msg->headersize-sizeof(DltStorageHeader),
2153 msg->databuffer,msg->datasize,
2157 dlt_log(LOG_ERR,"Storage of message in history buffer failed! Message discarded.\n");
2163 void dlt_daemon_control_reset_to_factory_default(DltDaemon *daemon,const char *filename, const char *filename1, int verbose)
2167 PRINT_FUNCTION_VERBOSE(verbose);
2169 if ((daemon==0) || (filename==0) || (filename1==0) || (filename[0]=='\0') || (filename1[0]=='\0'))
2174 /* Check for runtime cfg file and delete it, if available */
2175 fd=fopen(filename, "r");
2179 /* Close and delete file */
2184 fd=fopen(filename1, "r");
2188 /* Close and delete file */
2193 daemon->default_log_level = DLT_DAEMON_INITIAL_LOG_LEVEL ;
2194 daemon->default_trace_status = DLT_DAEMON_INITIAL_TRACE_STATUS ;
2196 daemon->message_buffer_overflow = DLT_MESSAGE_BUFFER_NO_OVERFLOW;
2198 /* Reset all other things (log level, trace status, etc.
2199 to default values */
2201 /* Inform user libraries about changed default log level/trace status */
2202 dlt_daemon_user_send_default_update(daemon, verbose);
2205 void dlt_daemon_user_send_default_update(DltDaemon *daemon, int verbose)
2208 DltDaemonContext *context;
2210 PRINT_FUNCTION_VERBOSE(verbose);
2217 for (count=0;count<daemon->num_contexts; count ++)
2219 context = &(daemon->contexts[count]);
2223 if ((context->log_level == DLT_LOG_DEFAULT) ||
2224 (context->trace_status == DLT_TRACE_STATUS_DEFAULT))
2226 if (context->user_handle!=0)
2228 if (dlt_daemon_user_send_log_level(daemon, context, verbose)==-1)
2238 void dlt_daemon_user_send_all_log_state(DltDaemon *daemon, int verbose)
2241 DltDaemonApplication *app;
2243 PRINT_FUNCTION_VERBOSE(verbose);
2250 for (count=0;count<daemon->num_applications; count ++)
2252 app = &(daemon->applications[count]);
2256 if (app->user_handle!=0)
2258 if (dlt_daemon_user_send_log_state(daemon, app, verbose)==-1)
2267 void dlt_daemon_control_message_time(int sock, DltDaemon *daemon, int verbose)
2273 PRINT_FUNCTION_VERBOSE(verbose);
2280 if (sock==DLT_DAEMON_STORE_TO_BUFFER)
2285 /* initialise new message */
2286 if (dlt_message_init(&msg,0)==-1)
2291 /* prepare payload of data */
2295 free(msg.databuffer);
2301 /* prepare storage header */
2302 msg.storageheader = (DltStorageHeader*)msg.headerbuffer;
2303 dlt_set_storageheader(msg.storageheader,daemon->ecuid);
2305 /* prepare standard header */
2306 msg.standardheader = (DltStandardHeader*)(msg.headerbuffer + sizeof(DltStorageHeader));
2307 msg.standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_UEH | DLT_HTYP_PROTOCOL_VERSION1 ;
2309 #if (BYTE_ORDER==BIG_ENDIAN)
2310 msg.standardheader->htyp = ( msg.standardheader->htyp | DLT_HTYP_MSBF);
2313 msg.standardheader->mcnt = 0;
2315 /* Set header extra parameters */
2316 dlt_set_id(msg.headerextra.ecu,daemon->ecuid);
2317 msg.headerextra.tmsp = dlt_uptime();
2319 dlt_message_set_extraparameters(&msg, verbose);
2321 /* prepare extended header */
2322 msg.extendedheader = (DltExtendedHeader*)(msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp));
2323 msg.extendedheader->msin = DLT_MSIN_CONTROL_TIME;
2325 msg.extendedheader->noar = 0; /* number of arguments */
2326 dlt_set_id(msg.extendedheader->apid,""); /* application id */
2327 dlt_set_id(msg.extendedheader->ctid,""); /* context id */
2329 /* prepare length information */
2330 msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + sizeof(DltExtendedHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp);
2332 len=msg.headersize - sizeof(DltStorageHeader) + msg.datasize;
2335 dlt_log(LOG_CRIT,"Huge control message discarded!\n");
2338 dlt_message_free(&msg,0);
2343 msg.standardheader->len = DLT_HTOBE_16(((uint16_t)len));
2348 DLT_DAEMON_SEM_LOCK();
2350 /* Optional: Send serial header, if requested */
2351 if (daemon->sendserialheader)
2353 ret=write(sock,dltSerialHeader,sizeof(dltSerialHeader));
2357 ret=write(sock, msg.headerbuffer+sizeof(DltStorageHeader),msg.headersize-sizeof(DltStorageHeader));
2358 ret=write(sock, msg.databuffer,msg.datasize);
2360 DLT_DAEMON_SEM_FREE();
2364 DLT_DAEMON_SEM_LOCK();
2366 /* Optional: Send serial header, if requested */
2367 if (daemon->sendserialheader)
2369 send(sock, dltSerialHeader,sizeof(dltSerialHeader),0);
2373 send(sock, msg.headerbuffer+sizeof(DltStorageHeader),msg.headersize-sizeof(DltStorageHeader),0);
2374 send(sock, msg.databuffer,msg.datasize,0);
2376 DLT_DAEMON_SEM_FREE();
2380 dlt_message_free(&msg,0);