DLT_CSTRING implementation non verbose mode.
[profile/ivi/dlt-daemon.git] / src / lib / dlt_user.c
index c99a646..2d04467 100644 (file)
 #include <sys/prctl.h>
 #endif
 
+#include <sys/types.h> /* needed for getpid() */
+#include <unistd.h>
+
+
 #include "dlt_user.h"
 #include "dlt_user_shared.h"
 #include "dlt_user_shared_cfg.h"
@@ -103,16 +107,16 @@ static int dlt_user_queue_resend(void);
 
 int dlt_user_check_library_version(const char *user_major_version,const char *user_minor_version){
 
-       char str[200];
+       char str[DLT_USER_BUFFER_LENGTH];
        char lib_major_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
        char lib_minor_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
 
-       dlt_get_major_version( lib_major_version);
-       dlt_get_minor_version( lib_minor_version);
+       dlt_get_major_version( lib_major_version,DLT_USER_MAX_LIB_VERSION_LENGTH);
+       dlt_get_minor_version( lib_minor_version,DLT_USER_MAX_LIB_VERSION_LENGTH);
 
        if( (strcmp(lib_major_version,user_major_version)!=0) || (strcmp(lib_minor_version,user_minor_version)!=0))
        {
-               sprintf(str,"DLT Library version check failed! Installed DLT library version is %s.%s - Application using DLT library version %s.%s\n",lib_major_version,lib_minor_version,user_major_version,user_minor_version);
+               snprintf(str,DLT_USER_BUFFER_LENGTH,"DLT Library version check failed! Installed DLT library version is %s.%s - Application using DLT library version %s.%s\n",lib_major_version,lib_minor_version,user_major_version,user_minor_version);
                dlt_log(LOG_WARNING, str);
                return -1;
        }
@@ -152,8 +156,30 @@ int dlt_init(void)
        memset(&(dlt_user.dlt_shm),0,sizeof(DltShm));
 #endif
 
+    /* create dlt pipes directory */
+    ret=mkdir(DLT_USER_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH  | S_IWOTH | S_ISVTX );
+    if (ret==-1 && errno != EEXIST)
+    {
+        snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user dir %s cannot be created!\n", DLT_USER_DIR);
+        dlt_log(LOG_ERR, str);
+        return -1;
+    }
+
+    /* if dlt pipes directory is created by the application also chmod the directory */
+    if(ret == 0)
+    {
+               // S_ISGID cannot be set by mkdir, let's reassign right bits
+               ret=chmod(DLT_USER_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH  | S_IWOTH | S_IXOTH | S_ISGID | S_ISVTX );
+               if (ret==-1)
+               {
+                       snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user dir %s cannot be chmoded!\n", DLT_USER_DIR);
+                       dlt_log(LOG_ERR, str);
+                       return -1;
+               }
+    }
+
     /* create and open DLT user FIFO */
-    sprintf(filename,"%s/dlt%d",DLT_USER_DIR,getpid());
+    snprintf(filename,DLT_USER_MAX_FILENAME_LENGTH,"%s/dlt%d",DLT_USER_DIR,getpid());
      
     /* Try to delete existing pipe, ignore result of unlink */
     unlink(filename);
@@ -161,7 +187,7 @@ int dlt_init(void)
     ret=mkfifo(filename, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP );
     if (ret==-1)
     {
-        sprintf(str,"Loging disabled, FIFO user %s cannot be created!\n",filename);
+        snprintf(str,DLT_USER_BUFFER_LENGTH,"Loging disabled, FIFO user %s cannot be created!\n",filename);
         dlt_log(LOG_WARNING, str);
         /* return 0; */ /* removed to prevent error, when FIFO already exists */
     }
@@ -170,7 +196,7 @@ int dlt_init(void)
     ret=chmod(filename, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP );
     if (ret==-1)
     {
-        sprintf(str,"FIFO user %s cannot be chmoded!\n", DLT_USER_DIR);
+        snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user %s cannot be chmoded!\n", DLT_USER_DIR);
         dlt_log(LOG_ERR, str);
         return -1;
     }
@@ -178,7 +204,7 @@ int dlt_init(void)
     dlt_user.dlt_user_handle = open(filename, O_RDWR | O_CLOEXEC);
     if (dlt_user.dlt_user_handle == DLT_FD_INIT)
     {
-        sprintf(str,"Loging disabled, FIFO user %s cannot be opened!\n",filename);
+        snprintf(str,DLT_USER_BUFFER_LENGTH,"Loging disabled, FIFO user %s cannot be opened!\n",filename);
         dlt_log(LOG_WARNING, str);
         unlink(filename);
         return 0;
@@ -188,7 +214,7 @@ int dlt_init(void)
     dlt_user.dlt_log_handle = open(DLT_USER_FIFO, O_WRONLY | O_NONBLOCK | O_CLOEXEC );
     if (dlt_user.dlt_log_handle==-1)
     {
-        sprintf(str,"Loging disabled, FIFO %s cannot be opened with open()!\n",DLT_USER_FIFO);
+        snprintf(str,DLT_USER_BUFFER_LENGTH,"Loging disabled, FIFO %s cannot be opened with open()!\n",DLT_USER_FIFO);
         dlt_log(LOG_WARNING, str);
         //return 0;
     }
@@ -198,7 +224,7 @@ int dlt_init(void)
                /* init shared memory */
                if (dlt_shm_init_client(&(dlt_user.dlt_shm),DLT_SHM_KEY) < 0)
                {
-                       sprintf(str,"Loging disabled, Shared memory %d cannot be created!\n",DLT_SHM_KEY);
+                       snprintf(str,DLT_USER_BUFFER_LENGTH,"Loging disabled, Shared memory %d cannot be created!\n",DLT_SHM_KEY);
                        dlt_log(LOG_WARNING, str);
                        //return 0; 
                }   
@@ -269,7 +295,7 @@ int dlt_init_file(const char *name)
     dlt_user.dlt_log_handle = open(name,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
     if (dlt_user.dlt_log_handle == -1)
     {
-        sprintf(str,"Log file %s cannot be opened!\n",name);
+        snprintf(str,DLT_USER_BUFFER_LENGTH,"Log file %s cannot be opened!\n",name);
         dlt_log(LOG_ERR, str);
         return -1;
     }
@@ -290,7 +316,7 @@ int dlt_init_message_queue(void)
 
     /* Generate per process name for queue */
     char queue_name[NAME_MAX];
-    sprintf(queue_name, "%s.%d", DLT_MESSAGE_QUEUE_NAME, getpid());
+    snprintf(queue_name,NAME_MAX, "%s.%d", DLT_MESSAGE_QUEUE_NAME, getpid());
 
     /* Maximum queue size is 10, limit to size of pointers */
     struct mq_attr mqatr;
@@ -373,6 +399,18 @@ int dlt_init_common(void)
     /* Verbose mode is enabled by default */
     dlt_user.verbose_mode = 1;
 
+    /* Use extended header for non verbose is enabled by default */
+    dlt_user.use_extende_header_for_non_verbose = DLT_USER_USE_EXTENDED_HEADER_FOR_NONVERBOSE;
+
+    /* WIth session id is enabled by default */
+    dlt_user.with_session_id = DLT_USER_WITH_SESSION_ID;
+
+    /* With timestamp is enabled by default */
+    dlt_user.with_timestamp= DLT_USER_WITH_TIMESTAMP;
+
+    /* With timestamp is enabled by default */
+    dlt_user.with_ecu_id= DLT_USER_WITH_ECU_ID;
+
     /* Local print is disabled by default */
     dlt_user.enable_local_print = 0;
 
@@ -437,7 +475,7 @@ void dlt_user_atexit_handler(void)
 
        if(count != 0){
                char tmp[256];
-               sprintf(tmp,"Lost log messages in user buffer when exiting: %i\n",count);
+               snprintf(tmp,256,"Lost log messages in user buffer when exiting: %i\n",count);
                dlt_log(LOG_ERR, tmp);
        }
 
@@ -507,7 +545,7 @@ int dlt_free(void)
 
     if (dlt_user.dlt_user_handle!=DLT_FD_INIT)
     {
-        sprintf(filename,"%s/dlt%d",DLT_USER_DIR,getpid());
+        snprintf(filename,DLT_USER_MAX_FILENAME_LENGTH,"%s/dlt%d",DLT_USER_DIR,getpid());
 
         close(dlt_user.dlt_user_handle);
         dlt_user.dlt_user_handle=DLT_FD_INIT;
@@ -558,7 +596,7 @@ int dlt_free(void)
     DLT_SEM_FREE();
 
     char queue_name[NAME_MAX];
-    sprintf(queue_name, "%s.%d", DLT_MESSAGE_QUEUE_NAME, getpid());
+    snprintf(queue_name,NAME_MAX, "%s.%d", DLT_MESSAGE_QUEUE_NAME, getpid());
 
     /**
      * Ignore errors from these, to not to spam user if dlt_free
@@ -645,8 +683,6 @@ int dlt_register_app(const char *appid, const char * description)
         dlt_user.application_description= malloc(desc_len+1);
         if (dlt_user.application_description){
             strncpy(dlt_user.application_description, description, desc_len);
-
-            /* Terminate transmitted string with 0 */
             dlt_user.application_description[desc_len]='\0';
         }
         else
@@ -847,8 +883,6 @@ int dlt_register_context_ll_ts(DltContext *handle, const char *contextid, const
                }
 
                strncpy(dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description, description, desc_len);
-
-               /* Terminate transmitted string with 0 */
                dlt_user.dlt_ll_ts[dlt_user.dlt_ll_ts_num_entries].context_description[desc_len]='\0';
        }
 
@@ -1119,7 +1153,7 @@ int dlt_forward_msg(void *msgdata,size_t size)
         {
             if (dlt_user_log_send_overflow()==0)
             {
-                               sprintf(str,"Buffer full! %u messages discarded!\n",dlt_user.overflow_counter);
+                               snprintf(str,DLT_USER_BUFFER_LENGTH,"Buffer full! %u messages discarded!\n",dlt_user.overflow_counter);
                                dlt_log(LOG_ERR, str);
                 dlt_user.overflow_counter=0;            }
         }
@@ -1819,6 +1853,17 @@ int dlt_user_log_write_string(DltContextData *log, const char *text)
     return 0;
 }
 
+int dlt_user_log_write_constant_string(DltContextData *log, const char *text)
+{
+    /* Send parameter only in verbose mode */
+    if (dlt_user.verbose_mode)
+    {
+       return dlt_user_log_write_string(log,text);
+    }
+
+    return 0;
+}
+
 int dlt_user_log_write_utf8_string(DltContextData *log, const char *text)
 {
     uint16_t arg_size;
@@ -2682,6 +2727,70 @@ int dlt_nonverbose_mode(void)
     return 0;
 }
 
+int dlt_use_extended_header_for_non_verbose(int8_t use_extende_header_for_non_verbose)
+{
+    if (dlt_user_initialised==0)
+    {
+        if (dlt_init()<0)
+        {
+            return -1;
+        }
+    }
+
+    /* Set use_extende_header_for_non_verbose */
+    dlt_user.use_extende_header_for_non_verbose = use_extende_header_for_non_verbose;
+
+    return 0;
+}
+
+int dlt_with_session_id(int8_t with_session_id)
+{
+    if (dlt_user_initialised==0)
+    {
+        if (dlt_init()<0)
+        {
+            return -1;
+        }
+    }
+
+    /* Set use_extende_header_for_non_verbose */
+    dlt_user.with_session_id = with_session_id;
+
+    return 0;
+}
+
+int dlt_with_timestamp(int8_t with_timestamp)
+{
+    if (dlt_user_initialised==0)
+    {
+        if (dlt_init()<0)
+        {
+            return -1;
+        }
+    }
+
+    /* Set with_timestamp */
+    dlt_user.with_timestamp = with_timestamp;
+
+    return 0;
+}
+
+int dlt_with_ecu_id(int8_t with_ecu_id)
+{
+    if (dlt_user_initialised==0)
+    {
+        if (dlt_init()<0)
+        {
+            return -1;
+        }
+    }
+
+    /* Set with_timestamp */
+    dlt_user.with_ecu_id = with_ecu_id;
+
+    return 0;
+}
+
 int dlt_enable_local_print(void)
 {
     if (dlt_user_initialised==0)
@@ -2861,7 +2970,26 @@ DltReturnValue dlt_user_log_send_log(DltContextData *log, int mtype)
     }
 
     msg.standardheader = (DltStandardHeader*)(msg.headerbuffer + sizeof(DltStorageHeader));
-    msg.standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_PROTOCOL_VERSION1 ;
+    msg.standardheader->htyp = DLT_HTYP_PROTOCOL_VERSION1 ;
+
+    /* send ecu id */
+    if(dlt_user.with_ecu_id)
+    {
+               msg.standardheader->htyp |= DLT_HTYP_WEID;
+    }
+
+    /* send timestamp */
+    if(dlt_user.with_timestamp)
+    {
+               msg.standardheader->htyp |= DLT_HTYP_WTMS;
+    }
+
+    /* send session id */
+    if(dlt_user.with_session_id)
+    {
+               msg.standardheader->htyp |= DLT_HTYP_WSID;
+               msg.headerextra.seid = getpid();
+    }
 
     if (dlt_user.verbose_mode)
     {
@@ -2871,9 +2999,8 @@ DltReturnValue dlt_user_log_send_log(DltContextData *log, int mtype)
     else
     {
         /* In non-verbose, send extended header if desired */
-#if (DLT_USER_USE_EXTENDED_HEADER_FOR_NONVERBOSE==1)
-        msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_UEH );
-#endif
+       if(dlt_user.use_extende_header_for_non_verbose)
+               msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_UEH );
     }
 
 #if (BYTE_ORDER==BIG_ENDIAN)
@@ -2973,7 +3100,7 @@ DltReturnValue dlt_user_log_send_log(DltContextData *log, int mtype)
         {
             if (dlt_user_log_send_overflow()==0)
             {
-                               sprintf(str,"%u messages discarded!\n",dlt_user.overflow_counter);
+                               snprintf(str,DLT_USER_BUFFER_LENGTH,"%u messages discarded!\n",dlt_user.overflow_counter);
                                dlt_log(LOG_ERR, str);
                 dlt_user.overflow_counter=0;
             }
@@ -3724,7 +3851,7 @@ void dlt_user_log_reattach_to_daemon(void)
                        /* init shared memory */
                        if (dlt_shm_init_client(&dlt_user.dlt_shm,DLT_SHM_KEY) < 0)
                        {
-                               sprintf(str,"Loging disabled, Shared memory %d cannot be created!\n",DLT_SHM_KEY);
+                               snprintf(str,DLT_USER_BUFFER_LENGTH,"Loging disabled, Shared memory %d cannot be created!\n",DLT_SHM_KEY);
                                dlt_log(LOG_WARNING, str);
                                //return 0; 
                        }