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_ringbuffer_init(&(daemon->client_ringbuffer), DLT_DAEMON_RINGBUFFER_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)
223 int dlt_daemon_applications_clear(DltDaemon *daemon,int verbose)
227 PRINT_FUNCTION_VERBOSE(verbose);
234 for (i=0; i<daemon->num_applications; i++)
236 if (daemon->applications[i].application_description!=0)
238 free(daemon->applications[i].application_description);
239 daemon->applications[i].application_description = 0;
243 if (daemon->applications)
245 free(daemon->applications);
248 daemon->applications = 0;
249 daemon->num_applications = 0;
254 DltDaemonApplication* dlt_daemon_application_add(DltDaemon *daemon,char *apid,pid_t pid,char *description, int verbose)
256 DltDaemonApplication *application;
257 DltDaemonApplication *old;
260 char filename[DLT_DAEMON_TEXTBUFSIZE];
262 if ((daemon==0) || (apid==0) || (apid[0]=='\0'))
264 return (DltDaemonApplication*) 0;
267 if (daemon->applications == 0)
269 daemon->applications = (DltDaemonApplication*) malloc(sizeof(DltDaemonApplication)*DLT_DAEMON_APPL_ALLOC_SIZE);
270 if (daemon->applications==0)
272 return (DltDaemonApplication*) 0;
278 /* Check if application [apid] is already available */
279 application = dlt_daemon_application_find(daemon, apid, verbose);
282 daemon->num_applications += 1;
284 if (daemon->num_applications!=0)
286 if ((daemon->num_applications%DLT_DAEMON_APPL_ALLOC_SIZE)==0)
288 /* allocate memory in steps of DLT_DAEMON_APPL_ALLOC_SIZE, e.g. 100 */
289 old = daemon->applications;
290 daemon->applications = (DltDaemonApplication*) malloc(sizeof(DltDaemonApplication)*
291 ((daemon->num_applications/DLT_DAEMON_APPL_ALLOC_SIZE)+1)*DLT_DAEMON_APPL_ALLOC_SIZE);
292 if (daemon->applications==0)
294 daemon->applications = old;
295 return (DltDaemonApplication*) 0;
297 memcpy(daemon->applications,old,sizeof(DltDaemonApplication)*daemon->num_applications);
302 application = &(daemon->applications[daemon->num_applications-1]);
304 dlt_set_id(application->apid,apid);
305 application->application_description = 0;
306 application->num_contexts = 0;
311 /* Store application description and pid of application */
312 if (application->application_description)
314 free(application->application_description);
317 application->application_description=0;
321 application->application_description = malloc(strlen(description)+1);
322 if (application->application_description)
324 strncpy(application->application_description,description,strlen(description)+1);
325 application->application_description[strlen(description)]='\0';
329 application->pid = pid;
331 application->user_handle = -1;
335 sprintf(filename,"%s/dlt%d",DLT_USER_DIR,application->pid);
337 dlt_user_handle = open(filename, O_WRONLY|O_NONBLOCK);
338 if (dlt_user_handle <0)
340 sprintf(str,"open() failed to %s, errno=%d (%s)!\n",filename,errno,strerror(errno)); /* errno 2: ENOENT - No such file or directory */
341 dlt_log(LOG_ERR, str);
344 application->user_handle = dlt_user_handle;
350 qsort(daemon->applications,daemon->num_applications,sizeof(DltDaemonApplication),dlt_daemon_cmp_apid);
352 /* Find new position of application with apid*/
353 application = dlt_daemon_application_find(daemon, apid, verbose);
359 int dlt_daemon_application_del(DltDaemon *daemon, DltDaemonApplication *application, int verbose)
363 PRINT_FUNCTION_VERBOSE(verbose);
365 if ((daemon==0) || (application==0))
370 if (daemon->num_applications>0)
372 /* Check if user handle is open; if yes, close it */
373 if (application->user_handle!=-1)
375 close(application->user_handle);
376 application->user_handle=-1;
379 /* Free description of application to be deleted */
380 if (application->application_description)
382 free(application->application_description);
383 application->application_description = 0;
386 pos = application-(daemon->applications);
388 /* move all applications above pos to pos */
389 memmove(&(daemon->applications[pos]),&(daemon->applications[pos+1]), sizeof(DltDaemonApplication)*((daemon->num_applications-1)-pos));
391 /* Clear last application */
392 memset(&(daemon->applications[daemon->num_applications-1]),0,sizeof(DltDaemonApplication));
394 daemon->num_applications--;
401 DltDaemonApplication* dlt_daemon_application_find(DltDaemon *daemon,char *apid,int verbose)
403 DltDaemonApplication application;
405 PRINT_FUNCTION_VERBOSE(verbose);
407 if ((daemon==0) || (apid==0) || (apid[0]=='\0') || (daemon->num_applications==0))
409 return (DltDaemonApplication*) 0;
412 /* Check, if apid is smaller than smallest apid or greater than greatest apid */
413 if ((memcmp(apid,daemon->applications[0].apid,DLT_ID_SIZE)<0) ||
414 (memcmp(apid,daemon->applications[daemon->num_applications-1].apid,DLT_ID_SIZE)>0))
416 return (DltDaemonApplication*) 0;
419 dlt_set_id(application.apid,apid);
420 return (DltDaemonApplication*)bsearch(&application,daemon->applications,daemon->num_applications,sizeof(DltDaemonApplication),dlt_daemon_cmp_apid);
423 int dlt_daemon_applications_load(DltDaemon *daemon,const char *filename, int verbose)
427 char buf[DLT_DAEMON_TEXTBUFSIZE];
431 PRINT_FUNCTION_VERBOSE(verbose);
433 if ((daemon==0) || (filename==0) || (filename[0]=='\0'))
438 fd=fopen(filename, "r");
448 memset(buf, 0, sizeof(buf));
451 ret=fgets(buf,sizeof(buf),fd);
453 if (strcmp(buf,"")!=0)
459 /* pb contains now the description */
461 /* pid is unknown at loading time */
462 if (dlt_daemon_application_add(daemon,apid,0,pb,verbose)==0)
474 int dlt_daemon_applications_save(DltDaemon *daemon,const char *filename, int verbose)
479 char apid[DLT_ID_SIZE+1]; /* DLT_ID_SIZE+1, because the 0-termination is required here */
481 PRINT_FUNCTION_VERBOSE(verbose);
483 if ((daemon==0) || (filename==0) || (filename[0]=='\0'))
488 memset(apid,0, sizeof(apid));
490 if ((daemon->applications) && (daemon->num_applications>0))
492 fd=fopen(filename, "w");
495 for (i=0; i<daemon->num_applications; i++)
497 dlt_set_id(apid,daemon->applications[i].apid);
499 if ((daemon->applications[i].application_description) &&
500 (daemon->applications[i].application_description!='\0'))
502 fprintf(fd,"%s:%s:\n",apid, daemon->applications[i].application_description);
506 fprintf(fd,"%s::\n",apid);
516 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)
518 DltDaemonApplication *application;
519 DltDaemonContext *context;
520 DltDaemonContext *old;
523 PRINT_FUNCTION_VERBOSE(verbose);
525 if ((daemon==0) || (apid==0) || (apid[0]=='\0') || (ctid==0) || (ctid[0]=='\0'))
527 return (DltDaemonContext*) 0;
530 if ((log_level<DLT_LOG_DEFAULT) || (log_level>DLT_LOG_VERBOSE))
532 return (DltDaemonContext*) 0;
535 if ((trace_status<DLT_TRACE_STATUS_DEFAULT) || (trace_status>DLT_TRACE_STATUS_ON))
537 return (DltDaemonContext*) 0;
540 if (daemon->contexts == 0)
542 daemon->contexts = (DltDaemonContext*) malloc(sizeof(DltDaemonContext)*DLT_DAEMON_CONTEXT_ALLOC_SIZE);
543 if (daemon->contexts==0)
545 return (DltDaemonContext*) 0;
549 /* Check if application [apid] is available */
550 application = dlt_daemon_application_find(daemon, apid, verbose);
553 return (DltDaemonContext*) 0;
556 /* Check if context [apid, ctid] is already available */
557 context = dlt_daemon_context_find(daemon, apid, ctid, verbose);
560 daemon->num_contexts += 1;
562 if (daemon->num_contexts!=0)
564 if ((daemon->num_contexts%DLT_DAEMON_CONTEXT_ALLOC_SIZE)==0)
566 /* allocate memory for context in steps of DLT_DAEMON_CONTEXT_ALLOC_SIZE, e.g 100 */
567 old = daemon->contexts;
568 daemon->contexts = (DltDaemonContext*) malloc(sizeof(DltDaemonContext)*
569 ((daemon->num_contexts/DLT_DAEMON_CONTEXT_ALLOC_SIZE)+1)*DLT_DAEMON_CONTEXT_ALLOC_SIZE);
570 if (daemon->contexts==0)
572 daemon->contexts = old;
573 return (DltDaemonContext*) 0;
575 memcpy(daemon->contexts,old,sizeof(DltDaemonContext)*daemon->num_contexts);
580 context = &(daemon->contexts[daemon->num_contexts-1]);
582 dlt_set_id(context->apid,apid);
583 dlt_set_id(context->ctid,ctid);
584 context->context_description = 0;
586 application->num_contexts++;
590 /* Set context description */
591 if (context->context_description)
593 free(context->context_description);
596 context->context_description=0;
600 context->context_description = malloc(strlen(description)+1);
602 if (context->context_description)
604 strncpy(context->context_description,description,strlen(description)+1);
605 context->context_description[strlen(description)]='\0';
609 /* Store log level and trace status,
610 if this is a new context, or
611 if this is an old context and the runtime cfg was not loaded */
613 if ((new_context==1) ||
614 ((new_context==0) && (daemon->runtime_context_cfg_loaded==0)))
616 context->log_level = log_level;
617 context->trace_status = trace_status;
620 context->log_level_pos = log_level_pos;
621 context->user_handle = user_handle;
626 qsort(daemon->contexts,daemon->num_contexts, sizeof(DltDaemonContext),dlt_daemon_cmp_apid_ctid);
628 /* Find new position of context with apid, ctid */
629 context = dlt_daemon_context_find(daemon, apid, ctid, verbose);
635 int dlt_daemon_context_del(DltDaemon *daemon, DltDaemonContext* context, int verbose)
638 DltDaemonApplication *application;
640 PRINT_FUNCTION_VERBOSE(verbose);
642 if ((daemon==0) || (context==0))
647 if (daemon->num_contexts>0)
649 application = dlt_daemon_application_find(daemon, context->apid, verbose);
651 /* Free description of context to be deleted */
652 if (context->context_description)
654 free(context->context_description);
655 context->context_description = 0;
658 pos = context-(daemon->contexts);
660 /* move all contexts above pos to pos */
661 memmove(&(daemon->contexts[pos]),&(daemon->contexts[pos+1]), sizeof(DltDaemonContext)*((daemon->num_contexts-1)-pos));
663 /* Clear last context */
664 memset(&(daemon->contexts[daemon->num_contexts-1]),0,sizeof(DltDaemonContext));
666 daemon->num_contexts--;
668 /* Check if application [apid] is available */
671 application->num_contexts--;
678 DltDaemonContext* dlt_daemon_context_find(DltDaemon *daemon,char *apid,char *ctid,int verbose)
680 DltDaemonContext context;
682 PRINT_FUNCTION_VERBOSE(verbose);
684 if ((daemon==0) || (apid==0) || (apid[0]=='\0') || (ctid==0) || (ctid[0]=='\0') || (daemon->num_contexts==0))
686 return (DltDaemonContext*) 0;
689 /* Check, if apid is smaller than smallest apid or greater than greatest apid */
690 if ((memcmp(apid,daemon->contexts[0].apid,DLT_ID_SIZE)<0) ||
691 (memcmp(apid,daemon->contexts[daemon->num_contexts-1].apid,DLT_ID_SIZE)>0))
693 return (DltDaemonContext*) 0;
696 dlt_set_id(context.apid,apid);
697 dlt_set_id(context.ctid,ctid);
699 return (DltDaemonContext*)bsearch(&context,daemon->contexts,daemon->num_contexts,sizeof(DltDaemonContext),dlt_daemon_cmp_apid_ctid);
702 int dlt_daemon_contexts_clear(DltDaemon *daemon,int verbose)
706 PRINT_FUNCTION_VERBOSE(verbose);
713 for (i=0; i<daemon->num_contexts; i++)
715 if (daemon->contexts[i].context_description!=0)
717 free(daemon->contexts[i].context_description);
718 daemon->contexts[i].context_description = 0;
722 if (daemon->contexts)
724 free(daemon->contexts);
727 daemon->contexts = 0;
729 for (i=0; i<daemon->num_applications; i++)
731 daemon->applications[i].num_contexts = 0;
734 daemon->num_contexts = 0;
739 int dlt_daemon_contexts_load(DltDaemon *daemon,const char *filename, int verbose)
743 char buf[DLT_DAEMON_TEXTBUFSIZE];
748 PRINT_FUNCTION_VERBOSE(verbose);
750 if ((daemon==0) || (filename==0) ||( filename[0]=='\0'))
755 fd=fopen(filename, "r");
765 memset(buf, 0, sizeof(buf));
768 ret=fgets(buf,sizeof(buf),fd);
770 if (strcmp(buf,"")!=0)
782 /* pb contains now the description */
784 /* log_level_pos, and user_handle are unknown at loading time */
785 if (dlt_daemon_context_add(daemon,apid,ctid,(int8_t)ll,(int8_t)ts,0,0,pb,verbose)==0)
797 int dlt_daemon_contexts_save(DltDaemon *daemon,const char *filename, int verbose)
802 char apid[DLT_ID_SIZE+1], ctid[DLT_ID_SIZE+1]; /* DLT_ID_SIZE+1, because the 0-termination is required here */
804 PRINT_FUNCTION_VERBOSE(verbose);
806 if ((daemon==0) || (filename==0) ||( filename[0]=='\0'))
811 memset(apid,0, sizeof(apid));
812 memset(ctid,0, sizeof(ctid));
814 if ((daemon->contexts) && (daemon->num_contexts>0))
816 fd=fopen(filename, "w");
819 for (i=0; i<daemon->num_contexts; i++)
821 dlt_set_id(apid,daemon->contexts[i].apid);
822 dlt_set_id(ctid,daemon->contexts[i].ctid);
824 if ((daemon->contexts[i].context_description) &&
825 (daemon->contexts[i].context_description[0]!='\0'))
827 fprintf(fd,"%s:%s:%d:%d:%s:\n",apid,ctid,
828 (int)(daemon->contexts[i].log_level),
829 (int)(daemon->contexts[i].trace_status),
830 daemon->contexts[i].context_description);
834 fprintf(fd,"%s:%s:%d:%d::\n",apid,ctid,
835 (int)(daemon->contexts[i].log_level),
836 (int)(daemon->contexts[i].trace_status));
846 int dlt_daemon_configuration_save(DltDaemon *daemon,const char *filename, int verbose)
850 PRINT_FUNCTION_VERBOSE(verbose);
852 if ((daemon==0) || (filename==0) ||( filename[0]=='\0'))
857 fd=fopen(filename, "w");
860 fprintf(fd,"# 0 = off, 1 = external, 2 = internal, 3 = both\n");
861 fprintf(fd,"LoggingMode = %d\n",daemon->mode);
869 int dlt_daemon_configuration_load(DltDaemon *daemon,const char *filename, int verbose)
877 PRINT_FUNCTION_VERBOSE(verbose);
879 pFile = fopen (filename,"r");
885 /* fetch line from configuration file */
886 if ( fgets (line , 1024 , pFile) != NULL )
888 pch = strtok (line," =\r\n");
894 if(strcmp(pch,"#")==0)
899 strncpy(token,pch,sizeof(token));
903 strncpy(value,pch,sizeof(value));
907 pch = strtok (NULL, " =\r\n");
910 if(token[0] && value[0])
912 /* parse arguments here */
913 if(strcmp(token,"LoggingMode")==0)
915 daemon->mode = atoi(value);
916 sprintf(str,"Runtime Option: %s=%d\n",token,daemon->mode);
917 dlt_log(LOG_INFO, str);
921 sprintf(str,"Unknown option: %s=%s\n",token,value);
922 dlt_log(LOG_ERR, str);
935 sprintf(str,"Cannot open configuration file: %s\n",filename);
936 dlt_log(LOG_WARNING, str);
942 int dlt_daemon_user_send_log_level(DltDaemon *daemon,DltDaemonContext *context,int verbose)
944 DltUserHeader userheader;
945 DltUserControlMsgLogLevel usercontext;
948 PRINT_FUNCTION_VERBOSE(verbose);
950 if ((daemon==0) || (context==0))
955 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG_LEVEL)==-1)
960 usercontext.log_level = ((context->log_level == DLT_LOG_DEFAULT)?daemon->default_log_level:context->log_level);
961 usercontext.trace_status = ((context->trace_status == DLT_TRACE_STATUS_DEFAULT)?daemon->default_trace_status:context->trace_status);
963 usercontext.log_level_pos = context->log_level_pos;
966 ret = dlt_user_log_out2(context->user_handle, &(userheader), sizeof(DltUserHeader), &(usercontext), sizeof(DltUserControlMsgLogLevel));
968 if (ret!=DLT_RETURN_OK)
972 /* Close connection */
973 close(context->user_handle);
974 context->user_handle=0;
978 return ((ret==DLT_RETURN_OK)?0:-1);
981 int dlt_daemon_user_send_log_state(DltDaemon *daemon,DltDaemonApplication *app,int verbose)
983 DltUserHeader userheader;
984 DltUserControlMsgLogState logstate;
987 PRINT_FUNCTION_VERBOSE(verbose);
989 if ((daemon==0) || (app==0))
994 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_LOG_STATE)==-1)
999 logstate.log_state = daemon->state;
1002 ret = dlt_user_log_out2(app->user_handle, &(userheader), sizeof(DltUserHeader), &(logstate), sizeof(DltUserControlMsgLogState));
1004 if (ret!=DLT_RETURN_OK)
1008 /* Close connection */
1009 close(app->user_handle);
1014 return ((ret==DLT_RETURN_OK)?0:-1);
1017 int dlt_daemon_control_process_control(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1019 uint32_t id,id_tmp=0;
1021 PRINT_FUNCTION_VERBOSE(verbose);
1023 if ((daemon==0) || (msg==0))
1028 if (msg->datasize<sizeof(uint32_t))
1033 id_tmp = *((uint32_t*)(msg->databuffer));
1034 id=DLT_ENDIAN_GET_32(msg->standardheader->htyp ,id_tmp);
1036 if ((id > 0) && (id <= DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW))
1038 /* Control message handling */
1041 case DLT_SERVICE_ID_SET_LOG_LEVEL:
1043 dlt_daemon_control_set_log_level(sock, daemon, msg, verbose);
1046 case DLT_SERVICE_ID_SET_TRACE_STATUS:
1048 dlt_daemon_control_set_trace_status(sock, daemon, msg, verbose);
1051 case DLT_SERVICE_ID_GET_LOG_INFO:
1053 dlt_daemon_control_get_log_info(sock, daemon, msg, verbose);
1056 case DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL:
1058 dlt_daemon_control_get_default_log_level(sock, daemon, verbose);
1061 case DLT_SERVICE_ID_STORE_CONFIG:
1063 if (dlt_daemon_applications_save(daemon, daemon->runtime_application_cfg, verbose)==0)
1065 if (dlt_daemon_contexts_save(daemon, daemon->runtime_context_cfg, verbose)==0)
1067 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1071 /* Delete saved files */
1072 dlt_daemon_control_reset_to_factory_default(daemon, daemon->runtime_application_cfg, daemon->runtime_context_cfg, verbose);
1073 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1078 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1082 case DLT_SERVICE_ID_RESET_TO_FACTORY_DEFAULT:
1084 dlt_daemon_control_reset_to_factory_default(daemon, daemon->runtime_application_cfg, daemon->runtime_context_cfg, verbose);
1085 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1088 case DLT_SERVICE_ID_SET_COM_INTERFACE_STATUS:
1090 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1093 case DLT_SERVICE_ID_SET_COM_INTERFACE_MAX_BANDWIDTH:
1095 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1098 case DLT_SERVICE_ID_SET_VERBOSE_MODE:
1100 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1103 case DLT_SERVICE_ID_SET_MESSAGE_FILTERING:
1105 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1108 case DLT_SERVICE_ID_SET_TIMING_PACKETS:
1110 dlt_daemon_control_set_timing_packets(sock, daemon, msg, verbose);
1113 case DLT_SERVICE_ID_GET_LOCAL_TIME:
1115 /* Send response with valid timestamp (TMSP) field */
1116 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1119 case DLT_SERVICE_ID_USE_ECU_ID:
1121 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1124 case DLT_SERVICE_ID_USE_SESSION_ID:
1126 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1129 case DLT_SERVICE_ID_USE_TIMESTAMP:
1131 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1134 case DLT_SERVICE_ID_USE_EXTENDED_HEADER:
1136 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1139 case DLT_SERVICE_ID_SET_DEFAULT_LOG_LEVEL:
1141 dlt_daemon_control_set_default_log_level(sock, daemon, msg, verbose);
1144 case DLT_SERVICE_ID_SET_DEFAULT_TRACE_STATUS:
1146 dlt_daemon_control_set_default_trace_status(sock, daemon, msg, verbose);
1149 case DLT_SERVICE_ID_GET_SOFTWARE_VERSION:
1151 dlt_daemon_control_get_software_version(sock, daemon, verbose);
1154 case DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW:
1156 dlt_daemon_control_message_buffer_overflow(sock, daemon, verbose);
1161 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1168 /* Injection handling */
1169 dlt_daemon_control_callsw_cinjection(sock, daemon, msg, verbose);
1175 void dlt_daemon_control_callsw_cinjection(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1177 char apid[DLT_ID_SIZE],ctid[DLT_ID_SIZE];
1178 uint32_t id=0,id_tmp=0;
1180 DltDaemonContext *context;
1181 uint32_t data_length_inject=0,data_length_inject_tmp=0;
1185 DltUserHeader userheader;
1186 DltUserControlMsgInjection usercontext;
1187 uint8_t *userbuffer;
1189 PRINT_FUNCTION_VERBOSE(verbose);
1191 if ((daemon==0) || (msg==0))
1196 datalength = msg->datasize;
1197 ptr = msg->databuffer;
1204 DLT_MSG_READ_VALUE(id_tmp,ptr,datalength,uint32_t); /* Get service id */
1205 id=DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp);
1207 if ((id>=DLT_DAEMON_INJECTION_MIN) && (id<=DLT_DAEMON_INJECTION_MAX))
1209 /* This a a real SW-C injection call */
1210 data_length_inject=0;
1211 data_length_inject_tmp=0;
1213 DLT_MSG_READ_VALUE(data_length_inject_tmp,ptr,datalength,uint32_t); /* Get data length */
1214 data_length_inject=DLT_ENDIAN_GET_32(msg->standardheader->htyp, data_length_inject_tmp);
1216 /* Get context handle for apid, ctid (and seid) */
1217 /* Warning: seid is ignored in this implementation! */
1218 if (DLT_IS_HTYP_UEH(msg->standardheader->htyp))
1220 dlt_set_id(apid, msg->extendedheader->apid);
1221 dlt_set_id(ctid, msg->extendedheader->ctid);
1225 /* No extended header, and therefore no apid and ctid available */
1226 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1230 /* At this point, apid and ctid is available */
1231 context=dlt_daemon_context_find(daemon, apid, ctid, verbose);
1235 // dlt_log(LOG_INFO,"No context found!\n");
1236 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1240 /* Send user message to handle, specified in context */
1241 if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_INJECTION)==-1)
1243 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1247 usercontext.log_level_pos = context->log_level_pos;
1249 userbuffer = malloc(data_length_inject);
1253 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1257 usercontext.data_length_inject = data_length_inject;
1258 usercontext.service_id = id;
1260 memcpy(userbuffer,ptr,data_length_inject); /* Copy received injection to send buffer */
1263 if (dlt_user_log_out3(context->user_handle, &(userheader), sizeof(DltUserHeader),
1264 &(usercontext), sizeof(DltUserControlMsgInjection),
1265 userbuffer, data_length_inject)!=DLT_RETURN_OK)
1269 /* Close connection */
1270 close(context->user_handle);
1271 context->user_handle=0;
1273 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1277 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1287 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
1291 void dlt_daemon_control_set_log_level(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1293 PRINT_FUNCTION_VERBOSE(verbose);
1295 char apid[DLT_ID_SIZE],ctid[DLT_ID_SIZE];
1296 DltServiceSetLogLevel *req;
1297 DltDaemonContext *context;
1298 int32_t id=DLT_SERVICE_ID_SET_LOG_LEVEL;
1300 int8_t old_log_level;
1302 if ((daemon==0) || (msg==0))
1307 req = (DltServiceSetLogLevel*) (msg->databuffer);
1309 dlt_set_id(apid, req->apid);
1310 dlt_set_id(ctid, req->ctid);
1312 context=dlt_daemon_context_find(daemon, apid, ctid, verbose);
1317 old_log_level = context->log_level;
1318 context->log_level = req->log_level; /* No endianess conversion necessary*/
1320 if ((context->user_handle!=0) &&
1321 (dlt_daemon_user_send_log_level(daemon, context, verbose)==0))
1323 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1327 //dlt_log(LOG_ERR, "Log level could not be sent!\n");
1328 context->log_level = old_log_level;
1329 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1334 //dlt_log(LOG_ERR, "Context not found!\n");
1335 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1339 void dlt_daemon_control_set_trace_status(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1341 PRINT_FUNCTION_VERBOSE(verbose);
1343 char apid[DLT_ID_SIZE],ctid[DLT_ID_SIZE];
1344 DltServiceSetLogLevel *req; /* request uses same struct as set log level */
1345 DltDaemonContext *context;
1346 int32_t id=DLT_SERVICE_ID_SET_TRACE_STATUS;
1348 int8_t old_trace_status;
1350 if ((daemon==0) || (msg==0))
1355 req = (DltServiceSetLogLevel*) (msg->databuffer);
1357 dlt_set_id(apid, req->apid);
1358 dlt_set_id(ctid, req->ctid);
1360 context=dlt_daemon_context_find(daemon, apid, ctid, verbose);
1365 old_trace_status = context->trace_status;
1366 context->trace_status = req->log_level; /* No endianess conversion necessary */
1368 if ((context->user_handle!=0) &&
1369 (dlt_daemon_user_send_log_level(daemon, context, verbose)==0))
1371 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1375 //dlt_log(LOG_ERR, "Trace Status could not be sent!\n");
1376 context->trace_status = old_trace_status;
1377 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1382 //dlt_log(LOG_ERR, "Context not found!\n");
1383 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1387 void dlt_daemon_control_set_default_log_level(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1389 PRINT_FUNCTION_VERBOSE(verbose);
1391 DltServiceSetDefaultLogLevel *req;
1392 int32_t id=DLT_SERVICE_ID_SET_DEFAULT_LOG_LEVEL;
1394 if ((daemon==0) || (msg==0))
1399 req = (DltServiceSetDefaultLogLevel*) (msg->databuffer);
1401 /* No endianess conversion necessary */
1402 if ((req->log_level>=0) &&
1403 (req->log_level<=DLT_LOG_VERBOSE))
1405 daemon->default_log_level = req->log_level; /* No endianess conversion necessary */
1407 /* Send Update to all contexts using the default log level */
1408 dlt_daemon_user_send_default_update(daemon, verbose);
1410 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1414 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1418 void dlt_daemon_control_set_default_trace_status(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1420 PRINT_FUNCTION_VERBOSE(verbose);
1422 /* Payload of request message */
1423 DltServiceSetDefaultLogLevel *req;
1424 int32_t id=DLT_SERVICE_ID_SET_DEFAULT_TRACE_STATUS;
1426 if ((daemon==0) || (msg==0))
1431 req = (DltServiceSetDefaultLogLevel*) (msg->databuffer);
1433 /* No endianess conversion necessary */
1434 if ((req->log_level==DLT_TRACE_STATUS_OFF) ||
1435 (req->log_level==DLT_TRACE_STATUS_ON))
1437 daemon->default_trace_status = req->log_level; /* No endianess conversion necessary*/
1439 /* Send Update to all contexts using the default trace status */
1440 dlt_daemon_user_send_default_update(daemon, verbose);
1442 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1446 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1450 void dlt_daemon_control_set_timing_packets(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1452 PRINT_FUNCTION_VERBOSE(verbose);
1454 DltServiceSetVerboseMode *req; /* request uses same struct as set verbose mode */
1455 int32_t id=DLT_SERVICE_ID_SET_TIMING_PACKETS;
1457 if ((daemon==0) || (msg==0))
1462 req = (DltServiceSetVerboseMode*) (msg->databuffer);
1463 if ((req->new_status==0) || (req->new_status==1))
1465 daemon->timingpackets = req->new_status;
1467 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_OK, verbose);
1471 dlt_daemon_control_service_response(sock, daemon, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
1475 void dlt_daemon_control_get_software_version(int sock, DltDaemon *daemon, int verbose)
1477 char version[DLT_DAEMON_TEXTBUFSIZE];
1480 DltServiceGetSoftwareVersionResponse *resp;
1482 PRINT_FUNCTION_VERBOSE(verbose);
1489 /* initialise new message */
1490 if (dlt_message_init(&msg,0)==-1)
1492 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_SOFTWARE_VERSION, DLT_SERVICE_RESPONSE_ERROR, verbose);
1496 /* prepare payload of data */
1497 dlt_get_version(version);
1498 len = strlen(version);
1500 msg.datasize = sizeof(DltServiceGetSoftwareVersionResponse) + len;
1503 free(msg.databuffer);
1505 msg.databuffer = (uint8_t *) malloc(msg.datasize);
1506 if (msg.databuffer==0)
1508 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_SOFTWARE_VERSION, DLT_SERVICE_RESPONSE_ERROR, verbose);
1512 resp = (DltServiceGetSoftwareVersionResponse*) msg.databuffer;
1513 resp->service_id = DLT_SERVICE_ID_GET_SOFTWARE_VERSION;
1514 resp->status = DLT_SERVICE_RESPONSE_OK;
1516 memcpy(msg.databuffer+sizeof(DltServiceGetSoftwareVersionResponse),version,len);
1519 dlt_daemon_control_send_control_message(sock, daemon, &msg,"","", verbose);
1522 dlt_message_free(&msg,0);
1525 void dlt_daemon_control_get_default_log_level(int sock, DltDaemon *daemon, int verbose)
1528 DltServiceGetDefaultLogLevelResponse *resp;
1530 PRINT_FUNCTION_VERBOSE(verbose);
1537 /* initialise new message */
1538 if (dlt_message_init(&msg,0)==-1)
1540 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
1544 msg.datasize = sizeof(DltServiceGetDefaultLogLevelResponse);
1547 free(msg.databuffer);
1549 msg.databuffer = (uint8_t *) malloc(msg.datasize);
1550 if (msg.databuffer==0)
1552 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
1556 resp = (DltServiceGetDefaultLogLevelResponse*) msg.databuffer;
1557 resp->service_id = DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL;
1558 resp->status = DLT_SERVICE_RESPONSE_OK;
1559 resp->log_level = daemon->default_log_level;
1562 dlt_daemon_control_send_control_message(sock,daemon,&msg,"","", verbose);
1565 dlt_message_free(&msg,0);
1568 void dlt_daemon_control_get_log_info(int sock, DltDaemon *daemon, DltMessage *msg, int verbose)
1570 DltServiceGetLogInfoRequest *req;
1572 DltDaemonContext *context=0;
1573 DltDaemonApplication *application=0;
1575 int num_applications=0, num_contexts=0;
1576 uint16_t count_app_ids=0, count_con_ids=0;
1578 #if (DLT_DEBUG_GETLOGINFO==1)
1582 int32_t i,j,offset=0;
1592 PRINT_FUNCTION_VERBOSE(verbose);
1594 if ((daemon==0) || (msg==0))
1599 /* prepare pointer to message request */
1600 req = (DltServiceGetLogInfoRequest*) (msg->databuffer);
1602 /* initialise new message */
1603 if (dlt_message_init(&resp,0)==-1)
1605 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
1610 if ((req->options < 3 ) || (req->options>7))
1612 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
1616 if (req->apid[0]!='\0')
1618 application = dlt_daemon_application_find(daemon, req->apid, verbose);
1621 num_applications = 1;
1622 if (req->ctid[0]!='\0')
1624 context = dlt_daemon_context_find(daemon, req->apid, req->ctid, verbose);
1626 num_contexts = ((context)?1:0);
1630 num_contexts = application->num_contexts;
1635 num_applications = 0;
1641 /* Request all applications and contexts */
1642 num_applications = daemon->num_applications;
1643 num_contexts = daemon->num_contexts;
1646 /* prepare payload of data */
1648 /* Calculate maximum size for a response */
1649 resp.datasize = sizeof(uint32_t) /* SID */ + sizeof(int8_t) /* status*/ + sizeof(ID4) /* DLT_DAEMON_REMO_STRING */;
1651 sizecont = sizeof(uint32_t) /* context_id */;
1653 /* Add additional size for response of Mode 4, 6, 7 */
1654 if ((req->options==4) || (req->options==6) || (req->options==7))
1656 sizecont += sizeof(int8_t); /* log level */
1659 /* Add additional size for response of Mode 5, 6, 7 */
1660 if ((req->options==5) || (req->options==6) || (req->options==7))
1662 sizecont+= sizeof(int8_t); /* trace status */
1665 resp.datasize+= (num_applications * (sizeof(uint32_t) /* app_id */ + sizeof(uint16_t) /* count_con_ids */)) +
1666 (num_contexts * sizecont);
1668 resp.datasize+= sizeof(uint16_t) /* count_app_ids */;
1670 /* Add additional size for response of Mode 7 */
1671 if (req->options==7)
1673 if (req->apid[0]!='\0')
1675 if (req->ctid[0]!='\0')
1677 /* One application, one context */
1678 // context = dlt_daemon_context_find(daemon, req->apid, req->ctid, verbose);
1681 resp.datasize+=sizeof(uint16_t) /* len_context_description */;
1682 if (context->context_description!=0)
1684 resp.datasize+=strlen(context->context_description); /* context_description */
1690 /* One application, all contexts */
1691 if ((daemon->applications) && (application))
1693 /* Calculate start offset within contexts[] */
1695 for (i=0; i<(application-(daemon->applications)); i++)
1697 offset_base+=daemon->applications[i].num_contexts;
1700 /* Iterate over all contexts belonging to this application */
1701 for (j=0;j<application->num_contexts;j++)
1704 context = &(daemon->contexts[offset_base+j]);
1707 resp.datasize+=sizeof(uint16_t) /* len_context_description */;
1708 if (context->context_description!=0)
1710 resp.datasize+=strlen(context->context_description); /* context_description */
1717 /* Space for application description */
1720 resp.datasize+=sizeof(uint16_t) /* len_app_description */;
1721 if (application->application_description!=0)
1723 resp.datasize+=strlen(application->application_description); /* app_description */
1729 /* All applications, all contexts */
1730 for (i=0;i<daemon->num_contexts;i++)
1732 resp.datasize+=sizeof(uint16_t) /* len_context_description */;
1733 if (daemon->contexts[i].context_description!=0)
1735 resp.datasize+=strlen(daemon->contexts[i].context_description); /* context_description */
1739 for (i=0;i<daemon->num_applications;i++)
1741 resp.datasize+=sizeof(uint16_t) /* len_app_description */;
1742 if (daemon->applications[i].application_description!=0)
1744 resp.datasize+=strlen(daemon->applications[i].application_description); /* app_description */
1752 sprintf(str,"Allocate %d bytes for response msg databuffer\n", resp.datasize);
1753 dlt_log(LOG_INFO, str);
1756 /* Allocate buffer for response message */
1757 resp.databuffer = (uint8_t *) malloc(resp.datasize);
1758 if (resp.databuffer==0)
1760 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
1763 memset(resp.databuffer,0,resp.datasize);
1764 /* Preparation finished */
1766 /* Prepare response */
1767 sid = DLT_SERVICE_ID_GET_LOG_INFO;
1768 memcpy(resp.databuffer,&sid,sizeof(uint32_t));
1769 offset+=sizeof(uint32_t);
1771 value = (((num_applications!=0)&&(num_contexts!=0))?req->options:8); /* 8 = no matching context found */
1773 memcpy(resp.databuffer+offset,&value,sizeof(int8_t));
1774 offset+=sizeof(int8_t);
1776 count_app_ids = num_applications;
1778 if (count_app_ids!=0)
1780 memcpy(resp.databuffer+offset,&count_app_ids,sizeof(uint16_t));
1781 offset+=sizeof(uint16_t);
1783 #if (DLT_DEBUG_GETLOGINFO==1)
1784 sprintf(str,"#apid: %d \n", count_app_ids);
1785 dlt_log(LOG_DEBUG, str);
1788 for (i=0;i<count_app_ids;i++)
1790 if (req->apid[0]!='\0')
1796 if (daemon->applications)
1798 apid = daemon->applications[i].apid;
1802 /* This should never occur! */
1807 application = dlt_daemon_application_find(daemon, apid, verbose);
1811 /* Calculate start offset within contexts[] */
1813 for (j=0; j<(application-(daemon->applications)); j++)
1815 offset_base+=daemon->applications[j].num_contexts;
1818 dlt_set_id((char*)(resp.databuffer+offset),apid);
1819 offset+=sizeof(ID4);
1821 #if (DLT_DEBUG_GETLOGINFO==1)
1822 dlt_print_id(buf, apid);
1823 sprintf(str,"apid: %s\n",buf);
1824 dlt_log(LOG_DEBUG, str);
1827 if (req->apid[0]!='\0')
1829 count_con_ids = num_contexts;
1833 count_con_ids = application->num_contexts;
1836 memcpy(resp.databuffer+offset,&count_con_ids,sizeof(uint16_t));
1837 offset+=sizeof(uint16_t);
1839 #if (DLT_DEBUG_GETLOGINFO==1)
1840 sprintf(str,"#ctid: %d \n", count_con_ids);
1841 dlt_log(LOG_DEBUG, str);
1844 for (j=0;j<count_con_ids;j++)
1846 #if (DLT_DEBUG_GETLOGINFO==1)
1847 sprintf(str,"j: %d \n",j);
1848 dlt_log(LOG_DEBUG, str);
1850 if (!((count_con_ids==1) && (req->apid[0]!='\0') && (req->ctid[0]!='\0')))
1852 context = &(daemon->contexts[offset_base+j]);
1854 /* else: context was already searched and found
1855 (one application (found) with one context (found))*/
1858 ((req->ctid[0]=='\0') ||
1859 ((req->ctid[0]!='\0') && (memcmp(context->ctid,req->ctid,DLT_ID_SIZE)==0)))
1862 dlt_set_id((char*)(resp.databuffer+offset),context->ctid);
1863 offset+=sizeof(ID4);
1865 #if (DLT_DEBUG_GETLOGINFO==1)
1866 dlt_print_id(buf, context->ctid);
1867 sprintf(str,"ctid: %s \n",buf);
1868 dlt_log(LOG_DEBUG, str);
1872 if ((req->options==4) || (req->options==6) || (req->options==7))
1874 ll=context->log_level;
1875 memcpy(resp.databuffer+offset,&ll,sizeof(int8_t));
1876 offset+=sizeof(int8_t);
1880 if ((req->options==5) || (req->options==6) || (req->options==7))
1882 ts=context->trace_status;
1883 memcpy(resp.databuffer+offset,&ts,sizeof(int8_t));
1884 offset+=sizeof(int8_t);
1888 if (req->options==7)
1890 if (context->context_description)
1892 len = strlen(context->context_description);
1893 memcpy(resp.databuffer+offset,&len,sizeof(uint16_t));
1894 offset+=sizeof(uint16_t);
1895 memcpy(resp.databuffer+offset,context->context_description,strlen(context->context_description));
1896 offset+=strlen(context->context_description);
1901 memcpy(resp.databuffer+offset,&len,sizeof(uint16_t));
1902 offset+=sizeof(uint16_t);
1906 #if (DLT_DEBUG_GETLOGINFO==1)
1907 sprintf(str,"ll=%d ts=%d \n",(int32_t)ll,(int32_t)ts);
1908 dlt_log(LOG_DEBUG, str);
1912 #if (DLT_DEBUG_GETLOGINFO==1)
1913 dlt_log(LOG_DEBUG,"\n");
1918 if (req->options==7)
1920 if (application->application_description)
1922 len = strlen(application->application_description);
1923 memcpy(resp.databuffer+offset,&len,sizeof(uint16_t));
1924 offset+=sizeof(uint16_t);
1925 memcpy(resp.databuffer+offset,application->application_description,strlen(application->application_description));
1926 offset+=strlen(application->application_description);
1931 memcpy(resp.databuffer+offset,&len,sizeof(uint16_t));
1932 offset+=sizeof(uint16_t);
1935 } /* if (application) */
1936 } /* for (i=0;i<count_app_ids;i++) */
1937 } /* if (count_app_ids!=0) */
1939 dlt_set_id((char*)(resp.databuffer+offset),DLT_DAEMON_REMO_STRING);
1942 dlt_daemon_control_send_control_message(sock,daemon,&resp,"","", verbose);
1945 dlt_message_free(&resp,0);
1948 void dlt_daemon_control_message_buffer_overflow(int sock, DltDaemon *daemon, int verbose)
1951 DltServiceMessageBufferOverflowResponse *resp;
1953 PRINT_FUNCTION_VERBOSE(verbose);
1960 /* initialise new message */
1961 if (dlt_message_init(&msg,0)==-1)
1963 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW, DLT_SERVICE_RESPONSE_ERROR, verbose);
1967 /* prepare payload of data */
1968 msg.datasize = sizeof(DltServiceMessageBufferOverflowResponse);
1971 free(msg.databuffer);
1973 msg.databuffer = (uint8_t *) malloc(msg.datasize);
1974 if (msg.databuffer==0)
1976 if (sock!=DLT_DAEMON_STORE_TO_BUFFER)
1978 dlt_daemon_control_service_response(sock, daemon, DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW, DLT_SERVICE_RESPONSE_ERROR, verbose);
1983 resp = (DltServiceMessageBufferOverflowResponse*) msg.databuffer;
1984 resp->service_id = DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW;
1985 resp->status = DLT_SERVICE_RESPONSE_OK;
1986 resp->overflow = daemon->message_buffer_overflow;
1989 dlt_daemon_control_send_control_message(sock,daemon,&msg,"","", verbose);
1992 dlt_message_free(&msg,0);
1995 void dlt_daemon_control_service_response( int sock, DltDaemon *daemon, uint32_t service_id, int8_t status , int verbose)
1998 DltServiceResponse *resp;
2000 PRINT_FUNCTION_VERBOSE(verbose);
2007 /* initialise new message */
2008 if (dlt_message_init(&msg,0)==-1)
2013 /* prepare payload of data */
2014 msg.datasize = sizeof(DltServiceResponse);
2017 free(msg.databuffer);
2019 msg.databuffer = (uint8_t *) malloc(msg.datasize);
2020 if (msg.databuffer==0)
2025 resp = (DltServiceResponse*) msg.databuffer;
2026 resp->service_id = service_id;
2027 resp->status = status;
2030 dlt_daemon_control_send_control_message(sock,daemon,&msg,"","", verbose);
2033 dlt_message_free(&msg,0);
2036 void dlt_daemon_control_send_control_message( int sock, DltDaemon *daemon, DltMessage *msg, char* appid, char* ctid, int verbose)
2041 PRINT_FUNCTION_VERBOSE(verbose);
2043 if ((daemon==0) || (msg==0) || (appid==0) || (ctid==0))
2048 /* prepare storage header */
2049 msg->storageheader = (DltStorageHeader*)msg->headerbuffer;
2051 if (dlt_set_storageheader(msg->storageheader,daemon->ecuid)==-1)
2056 /* prepare standard header */
2057 msg->standardheader = (DltStandardHeader*)(msg->headerbuffer + sizeof(DltStorageHeader));
2058 msg->standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_UEH | DLT_HTYP_PROTOCOL_VERSION1 ;
2060 #if (BYTE_ORDER==BIG_ENDIAN)
2061 msg->standardheader->htyp = ( msg->standardheader->htyp | DLT_HTYP_MSBF);
2064 msg->standardheader->mcnt = 0;
2066 /* Set header extra parameters */
2067 dlt_set_id(msg->headerextra.ecu,daemon->ecuid);
2069 //msg->headerextra.seid = 0;
2071 msg->headerextra.tmsp = dlt_uptime();
2073 dlt_message_set_extraparameters(msg, verbose);
2075 /* prepare extended header */
2076 msg->extendedheader = (DltExtendedHeader*)(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp));
2077 msg->extendedheader->msin = DLT_MSIN_CONTROL_RESPONSE;
2079 msg->extendedheader->noar = 1; /* number of arguments */
2080 if (strcmp(appid,"")==0)
2082 dlt_set_id(msg->extendedheader->apid,DLT_DAEMON_CTRL_APID); /* application id */
2086 dlt_set_id(msg->extendedheader->apid, appid);
2088 if (strcmp(ctid,"")==0)
2090 dlt_set_id(msg->extendedheader->ctid,DLT_DAEMON_CTRL_CTID); /* context id */
2094 dlt_set_id(msg->extendedheader->ctid, ctid);
2097 /* prepare length information */
2098 msg->headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + sizeof(DltExtendedHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp);
2100 len=msg->headersize - sizeof(DltStorageHeader) + msg->datasize;
2103 dlt_log(LOG_CRIT,"Huge control message discarded!\n");
2107 msg->standardheader->len = DLT_HTOBE_16(((uint16_t)len));
2109 if (sock!=DLT_DAEMON_STORE_TO_BUFFER)
2114 DLT_DAEMON_SEM_LOCK();
2116 /* Optional: Send serial header, if requested */
2117 if (daemon->sendserialheader)
2119 ret=write(sock,dltSerialHeader,sizeof(dltSerialHeader));
2123 ret=write(sock, msg->headerbuffer+sizeof(DltStorageHeader),msg->headersize-sizeof(DltStorageHeader));
2124 ret=write(sock, msg->databuffer,msg->datasize);
2126 DLT_DAEMON_SEM_FREE();
2130 DLT_DAEMON_SEM_LOCK();
2132 /* Optional: Send serial header, if requested */
2133 if (daemon->sendserialheader)
2135 send(sock, dltSerialHeader,sizeof(dltSerialHeader),0);
2139 send(sock, msg->headerbuffer+sizeof(DltStorageHeader),msg->headersize-sizeof(DltStorageHeader),0);
2140 send(sock, msg->databuffer,msg->datasize,0);
2142 DLT_DAEMON_SEM_FREE();
2147 /* Store message in history buffer */
2148 if (dlt_ringbuffer_put3(&(daemon->client_ringbuffer),
2149 msg->headerbuffer+sizeof(DltStorageHeader),msg->headersize-sizeof(DltStorageHeader),
2150 msg->databuffer,msg->datasize,
2154 dlt_log(LOG_ERR,"Storage of message in history buffer failed! Message discarded.\n");
2160 void dlt_daemon_control_reset_to_factory_default(DltDaemon *daemon,const char *filename, const char *filename1, int verbose)
2164 PRINT_FUNCTION_VERBOSE(verbose);
2166 if ((daemon==0) || (filename==0) || (filename1==0) || (filename[0]=='\0') || (filename1[0]=='\0'))
2171 /* Check for runtime cfg file and delete it, if available */
2172 fd=fopen(filename, "r");
2176 /* Close and delete file */
2181 fd=fopen(filename1, "r");
2185 /* Close and delete file */
2190 daemon->default_log_level = DLT_DAEMON_INITIAL_LOG_LEVEL ;
2191 daemon->default_trace_status = DLT_DAEMON_INITIAL_TRACE_STATUS ;
2193 daemon->message_buffer_overflow = DLT_MESSAGE_BUFFER_NO_OVERFLOW;
2195 /* Reset all other things (log level, trace status, etc.
2196 to default values */
2198 /* Inform user libraries about changed default log level/trace status */
2199 dlt_daemon_user_send_default_update(daemon, verbose);
2202 void dlt_daemon_user_send_default_update(DltDaemon *daemon, int verbose)
2205 DltDaemonContext *context;
2207 PRINT_FUNCTION_VERBOSE(verbose);
2214 for (count=0;count<daemon->num_contexts; count ++)
2216 context = &(daemon->contexts[count]);
2220 if ((context->log_level == DLT_LOG_DEFAULT) ||
2221 (context->trace_status == DLT_TRACE_STATUS_DEFAULT))
2223 if (context->user_handle!=0)
2225 if (dlt_daemon_user_send_log_level(daemon, context, verbose)==-1)
2235 void dlt_daemon_user_send_all_log_state(DltDaemon *daemon, int verbose)
2238 DltDaemonApplication *app;
2240 PRINT_FUNCTION_VERBOSE(verbose);
2247 for (count=0;count<daemon->num_applications; count ++)
2249 app = &(daemon->applications[count]);
2253 if (app->user_handle!=0)
2255 if (dlt_daemon_user_send_log_state(daemon, app, verbose)==-1)
2264 void dlt_daemon_control_message_time(int sock, DltDaemon *daemon, int verbose)
2270 PRINT_FUNCTION_VERBOSE(verbose);
2277 if (sock==DLT_DAEMON_STORE_TO_BUFFER)
2282 /* initialise new message */
2283 if (dlt_message_init(&msg,0)==-1)
2288 /* prepare payload of data */
2292 free(msg.databuffer);
2298 /* prepare storage header */
2299 msg.storageheader = (DltStorageHeader*)msg.headerbuffer;
2300 dlt_set_storageheader(msg.storageheader,daemon->ecuid);
2302 /* prepare standard header */
2303 msg.standardheader = (DltStandardHeader*)(msg.headerbuffer + sizeof(DltStorageHeader));
2304 msg.standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_UEH | DLT_HTYP_PROTOCOL_VERSION1 ;
2306 #if (BYTE_ORDER==BIG_ENDIAN)
2307 msg.standardheader->htyp = ( msg.standardheader->htyp | DLT_HTYP_MSBF);
2310 msg.standardheader->mcnt = 0;
2312 /* Set header extra parameters */
2313 dlt_set_id(msg.headerextra.ecu,daemon->ecuid);
2314 msg.headerextra.tmsp = dlt_uptime();
2316 dlt_message_set_extraparameters(&msg, verbose);
2318 /* prepare extended header */
2319 msg.extendedheader = (DltExtendedHeader*)(msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp));
2320 msg.extendedheader->msin = DLT_MSIN_CONTROL_TIME;
2322 msg.extendedheader->noar = 0; /* number of arguments */
2323 dlt_set_id(msg.extendedheader->apid,""); /* application id */
2324 dlt_set_id(msg.extendedheader->ctid,""); /* context id */
2326 /* prepare length information */
2327 msg.headersize = sizeof(DltStorageHeader) + sizeof(DltStandardHeader) + sizeof(DltExtendedHeader) + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp);
2329 len=msg.headersize - sizeof(DltStorageHeader) + msg.datasize;
2332 dlt_log(LOG_CRIT,"Huge control message discarded!\n");
2335 dlt_message_free(&msg,0);
2340 msg.standardheader->len = DLT_HTOBE_16(((uint16_t)len));
2345 DLT_DAEMON_SEM_LOCK();
2347 /* Optional: Send serial header, if requested */
2348 if (daemon->sendserialheader)
2350 ret=write(sock,dltSerialHeader,sizeof(dltSerialHeader));
2354 ret=write(sock, msg.headerbuffer+sizeof(DltStorageHeader),msg.headersize-sizeof(DltStorageHeader));
2355 ret=write(sock, msg.databuffer,msg.datasize);
2357 DLT_DAEMON_SEM_FREE();
2361 DLT_DAEMON_SEM_LOCK();
2363 /* Optional: Send serial header, if requested */
2364 if (daemon->sendserialheader)
2366 send(sock, dltSerialHeader,sizeof(dltSerialHeader),0);
2370 send(sock, msg.headerbuffer+sizeof(DltStorageHeader),msg.headersize-sizeof(DltStorageHeader),0);
2371 send(sock, msg.databuffer,msg.datasize,0);
2373 DLT_DAEMON_SEM_FREE();
2377 dlt_message_free(&msg,0);