Invalid Sock is set to -1 instead 0 (stdin). Consequent error handling in fseek failu...
[profile/ivi/dlt-daemon.git] / src / shared / dlt_common.c
index 22834dd..b7a865e 100755 (executable)
@@ -1,37 +1,22 @@
-/*
- * Dlt- Diagnostic Log and Trace console apps
+/**
  * @licence app begin@
+ * Copyright (C) 2012  BMW AG
+ *
+ * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
+ *
+ * Contributions are licensed to the GENIVI Alliance under one or more
+ * Contribution License Agreements.
  *
- * Copyright (C) 2011, BMW AG - Alexander Wenzel <alexander.wenzel@bmw.de>
- * 
- * This program is free software; you can redistribute it and/or modify it under the terms of the 
- * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even 
- * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General 
- * Public License, version 2.1, for more details.
- * 
- * You should have received a copy of the GNU Lesser General Public License, version 2.1, along 
- * with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
- * 
- * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may 
- * also be applicable to programs even in cases in which the program is not a library in the technical sense.
- * 
- * Linking DLT statically or dynamically with other modules is making a combined work based on DLT. You may 
- * license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to 
- * license your linked modules under the GNU Lesser General Public License, version 2.1, you 
- * may use the program under the following exception.
- * 
- * As a special exception, the copyright holders of DLT give you permission to combine DLT 
- * with software programs or libraries that are released under any license unless such a combination is not
- * permitted by the license of such a software program or library. You may copy and distribute such a 
- * system following the terms of the GNU Lesser General Public License, version 2.1, including this
- * special exception, for DLT and the licenses of the other code concerned.
- * 
- * Note that people who make modified versions of DLT are not obligated to grant this special exception 
- * for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, 
- * version 2.1, gives permission to release a modified version without this exception; this exception 
- * also makes it possible to release a modified version which carries forward this exception.
+ * \copyright
+ * This Source Code Form is subject to the terms of the
+ * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
+ * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  *
+ *
+ * \author Alexander Wenzel <alexander.aw.wenzel@bmw.de> BMW 2011-2012
+ *
+ * \file dlt_common.c
+ * For further information see http://www.genivi.org/.
  * @licence end@
  */
 
 #include <stdlib.h> /* for malloc(), free() */
 #include <string.h> /* for strlen(), memcmp(), memmove() */
 #include <time.h>   /* for localtime(), strftime() */
+#include <limits.h> /* for NAME_MAX */
 
 #include "dlt_common.h"
 #include "dlt_common_cfg.h"
 
-#include "version.h"
-#include "svnversion.h"
+#include "dlt_version.h"
 
 #if defined (__WIN32__) || defined (_MSC_VER)
 #include <winsock2.h> /* for socket(), connect(), send(), and recv() */
@@ -115,7 +100,11 @@ static char str[DLT_COMMON_BUFFER_LENGTH];
 const char dltSerialHeader[DLT_ID_SIZE] = { 'D','L','S',1 };
 char dltSerialHeaderChar[DLT_ID_SIZE] = { 'D','L','S',1 };
 
-static int log_as_daemon = 0;
+/* internal logging parameters */
+static int logging_mode = 0;
+static int logging_level = 6;
+static char logging_filename[NAME_MAX + 1] = "";
+static FILE *logging_handle = 0;
 
 char *message_type[] = {"log","app_trace","nw_trace","control","","","",""};
 char *log_info[] = {"","fatal","error","warn","info","debug","verbose","","","","","","","","",""};
@@ -129,6 +118,14 @@ static char *service_id[] = {"","set_log_level","set_trace_status","get_log_info
                             };
 static char *return_type[] = {"ok","not_supported","error","","","","","","no_matching_context_id"};
 
+/* internal function definitions */
+int dlt_buffer_get(DltBuffer *buf,unsigned char *data, int max_size,int delete);
+int dlt_buffer_reset(DltBuffer *buf);
+int dlt_buffer_increase_size(DltBuffer *buf);
+int dlt_buffer_minimize_size(DltBuffer *buf);
+void dlt_buffer_write_block(DltBuffer *buf,int *write, const unsigned char *data,unsigned int size);
+void dlt_buffer_read_block(DltBuffer *buf,int *read,unsigned char *data,unsigned int size);    
+
 void dlt_print_hex(uint8_t *ptr,int size)
 {
     int num;
@@ -587,8 +584,10 @@ int dlt_filter_find(DltFilter *filter,const char *apid,const char *ctid, int ver
             /* apid matches, now check for ctid */
             if (ctid==0)
             {
-                /* check if empty ctid matches */
-                if (memcmp(filter->ctid[num],"",DLT_ID_SIZE)==0)
+                /* check if empty ctid matches */                
+                //if (memcmp(filter->ctid[num],"",DLT_ID_SIZE)==0)//coverity complains here about Out-of-bounds access.
+                char empty_ctid[DLT_ID_SIZE]="";
+                if (memcmp(filter->ctid[num],empty_ctid,DLT_ID_SIZE)==0)
                 {
                     return num;
                 }
@@ -702,6 +701,7 @@ int dlt_message_init(DltMessage *msg,int verbose)
     msg->datasize = 0;
 
     msg->databuffer = 0;
+    msg->databuffersize = 0;
 
     msg->storageheader = 0;
     msg->standardheader = 0;
@@ -724,8 +724,9 @@ int dlt_message_free(DltMessage *msg,int verbose)
     if (msg->databuffer)
     {
         free(msg->databuffer);
+        msg->databuffer = 0;
+        msg->databuffersize = 0;
     }
-    msg->databuffer = 0;
 
     return 0;
 }
@@ -1245,11 +1246,16 @@ int dlt_message_read(DltMessage *msg,uint8_t *buffer,unsigned int length,int res
     /* free last used memory for buffer */
     if (msg->databuffer)
     {
-        free(msg->databuffer);
+       if (msg->datasize>msg->databuffersize){
+               free(msg->databuffer);
+               msg->databuffer=(uint8_t *)malloc(msg->datasize);
+               msg->databuffersize = msg->datasize;
+       }
+    }else{
+       /* get new memory for buffer */
+       msg->databuffer = (uint8_t *)malloc(msg->datasize);
+       msg->databuffersize = msg->datasize;
     }
-
-    /* get new memory for buffer */
-    msg->databuffer = (uint8_t *)malloc(msg->datasize);
     if (msg->databuffer == 0)
     {
         sprintf(str,"Cannot allocate memory for payload buffer of size %d!\n",msg->datasize);
@@ -1478,7 +1484,8 @@ int dlt_file_read_header_raw(DltFile *file,int resync,int verbose)
         else
         {
             /* go back to last file position */
-            fseek(file->handle,file->file_position,SEEK_SET);
+            if (0 != fseek(file->handle,file->file_position,SEEK_SET))
+                return -1;
         }
     }
 
@@ -1592,13 +1599,17 @@ int dlt_file_read_data(DltFile *file, int verbose)
     }
 
     /* free last used memory for buffer */
-    if (file->msg.databuffer)
+    if (file->msg.databuffer && (file->msg.databuffersize < file->msg.datasize))
     {
         free(file->msg.databuffer);
+        file->msg.databuffer=0;
     }
 
-    /* get new memory for buffer */
-    file->msg.databuffer = (uint8_t *)malloc(file->msg.datasize);
+    if (file->msg.databuffer == 0){
+       /* get new memory for buffer */
+       file->msg.databuffer = (uint8_t *)malloc(file->msg.datasize);
+       file->msg.databuffersize = file->msg.datasize;
+    }
 
     if (file->msg.databuffer == 0)
     {
@@ -1647,14 +1658,26 @@ int dlt_file_open(DltFile *file,const char *filename,int verbose)
     file->handle = fopen(filename,"rb");
     if (file->handle == 0)
     {
-        sprintf(str,"File %s cannot be opened!\n",filename);
+        snprintf(str, DLT_COMMON_BUFFER_LENGTH - 1 ,"File %s cannot be opened!\n",filename);
+
         dlt_log(LOG_ERR, str);
         return -1;
     }
 
-    fseek(file->handle,0,SEEK_END);
+    if (0 != fseek(file->handle,0,SEEK_END))
+    {
+        sprintf(str,"dlt_file_open: Seek failed to 0,SEEK_END");
+        dlt_log(LOG_ERR, str);
+        return -1;
+    }
     file->file_length = ftell(file->handle);
-    fseek(file->handle,0,SEEK_SET);
+
+    if (0 != fseek(file->handle,0,SEEK_SET))
+    {
+        sprintf(str,"dlt_file_open: Seek failed to 0,SEEK_SET");
+        dlt_log(LOG_ERR, str);
+        return -1;
+    }
 
     if (verbose)
     {
@@ -1700,7 +1723,12 @@ int dlt_file_read(DltFile *file,int verbose)
     }
 
     /* set to end of last succesful read message, because of conflicting calls to dlt_file_read and dlt_file_message */
-    fseek(file->handle,file->file_position,SEEK_SET);
+    if (0 !=  fseek(file->handle,file->file_position,SEEK_SET))
+    {
+        sprintf(str,"Seek failed to file_position %ld \n",file->file_position);
+        dlt_log(LOG_ERR, str);
+        return -1;
+    }
 
     /* get file position at start of DLT message */
     if (verbose)
@@ -1723,7 +1751,11 @@ int dlt_file_read(DltFile *file,int verbose)
         if (dlt_file_read_header_extended(file, verbose)<0)
         {
             /* go back to last position in file */
-            fseek(file->handle,file->file_position,SEEK_SET);
+            if (0 != fseek(file->handle,file->file_position,SEEK_SET))
+            {
+                sprintf(str,"Seek to last file pos failed!\n");
+                dlt_log(LOG_ERR, str);
+            }
             return-1;
         }
 
@@ -1743,9 +1775,15 @@ int dlt_file_read(DltFile *file,int verbose)
         if (fseek(file->handle,file->msg.datasize,SEEK_CUR)!=0)
         {
             /* go back to last position in file */
-            fseek(file->handle,file->file_position,SEEK_SET);
             sprintf(str,"Seek failed to skip payload data of size %d!\n",file->msg.datasize);
             dlt_log(LOG_ERR, str);
+
+            if (0 != fseek(file->handle,file->file_position,SEEK_SET))
+            {
+                sprintf(str,"Seek back also failed!\n");
+                dlt_log(LOG_ERR, str);
+            }
+
             return -1;
         }
     }
@@ -1755,11 +1793,16 @@ int dlt_file_read(DltFile *file,int verbose)
         /* skip additional header parameters and payload data */
         if (fseek(file->handle,file->msg.headersize - sizeof(DltStorageHeader) - sizeof(DltStandardHeader) + file->msg.datasize,SEEK_CUR))
         {
-            /* go back to last position in file */
-            fseek(file->handle,file->file_position,SEEK_SET);
+
             sprintf(str,"Seek failed to skip extra header and payload data from file of size %d!\n",
                     file->msg.headersize - sizeof(DltStorageHeader) - sizeof(DltStandardHeader) + file->msg.datasize);
             dlt_log(LOG_ERR, str);
+            /* go back to last position in file */
+            if (fseek(file->handle,file->file_position,SEEK_SET))
+            {
+                sprintf(str,"Seek back also failed!\n");
+                dlt_log(LOG_ERR, str);
+            }
             return -1;
         }
 
@@ -1812,8 +1855,9 @@ int dlt_file_read_raw(DltFile *file,int resync, int verbose)
         file->index = ptr;
     }
 
-    /* set to end of last succesful read message, because of conflicting calls to dlt_file_read and dlt_file_message */
-    fseek(file->handle,file->file_position,SEEK_SET);
+    /* set to end of last successful read message, because of conflicting calls to dlt_file_read and dlt_file_message */
+    if (0 != fseek(file->handle,file->file_position,SEEK_SET))
+        return -1;
 
     /* get file position at start of DLT message */
     if (verbose)
@@ -1826,7 +1870,11 @@ int dlt_file_read_raw(DltFile *file,int resync, int verbose)
     if (dlt_file_read_header_raw(file,resync,verbose)<0)
     {
         /* go back to last position in file */
-        fseek(file->handle,file->file_position,SEEK_SET);
+        if (0!= fseek(file->handle,file->file_position,SEEK_SET))
+        {
+            sprintf(str,"dlt_file_read_raw, fseek failed 1\n");
+            dlt_log(LOG_ERR, str);
+        }
         return -1;
     }
 
@@ -1834,14 +1882,22 @@ int dlt_file_read_raw(DltFile *file,int resync, int verbose)
     if (dlt_file_read_header_extended(file, verbose)<0)
     {
         /* go back to last position in file */
-        fseek(file->handle,file->file_position,SEEK_SET);
+        if (0 != fseek(file->handle,file->file_position,SEEK_SET))
+        {
+            sprintf(str,"dlt_file_read_raw, fseek failed 2\n");
+            dlt_log(LOG_ERR, str);
+        }
         return-1;
     }
 
     if (dlt_file_read_data(file,verbose)<0)
     {
         /* go back to last position in file */
-        fseek(file->handle,file->file_position,SEEK_SET);
+        if (0 != fseek(file->handle,file->file_position,SEEK_SET))
+        {
+            sprintf(str,"dlt_file_read_raw, fseek failed 3\n");
+            dlt_log(LOG_ERR, str);
+        }
         return-1;
     }
 
@@ -1953,14 +2009,38 @@ int dlt_file_free(DltFile *file,int verbose)
     return dlt_message_free(&(file->msg),verbose);
 }
 
+void dlt_log_set_level(int level)
+{
+       logging_level = level;
+}
+
+void dlt_log_set_filename(const char *filename)
+{
+        strncpy(logging_filename,filename,NAME_MAX);
+
+}
+
 void dlt_log_init(int mode)
 {
-    log_as_daemon = mode;
+    logging_mode = mode;
+    
+       if(logging_mode == 2)
+       {
+               /* internal logging to file */
+               logging_handle = fopen(logging_filename,"w");
+               if (logging_handle == 0)
+               {
+                       printf("Internal log file %s cannot be opened!\n",logging_filename);
+                       return;
+               }
+       }
 }
 
 void dlt_log_free(void)
 {
-    /* Nothing to be done yet */
+       if(logging_mode == 2) {
+               fclose(logging_handle);
+       }
 }
 
 int dlt_log(int prio, char *s)
@@ -1971,68 +2051,82 @@ int dlt_log(int prio, char *s)
     {
         return -1;
     }
+       if(logging_level<prio)
+       {
+               return 0;
+       }
 
     switch (prio)
     {
         case   LOG_EMERG:
         {
-            strcpy(logfmtstring,"DLT| EMERGENCY: %s");
+            strncpy(logfmtstring,"DLT| EMERGENCY: %s",sizeof(logfmtstring));
             break;
         }
         case   LOG_ALERT:
         {
-            strcpy(logfmtstring,"DLT| ALERT:     %s");
+            strncpy(logfmtstring,"DLT| ALERT:     %s",sizeof(logfmtstring));
             break;
         }
         case   LOG_CRIT:
         {
-            strcpy(logfmtstring,"DLT| CRITICAL:  %s");
+            strncpy(logfmtstring,"DLT| CRITICAL:  %s",sizeof(logfmtstring));
             break;
         }
         case   LOG_ERR:
         {
-            strcpy(logfmtstring,"DLT| ERROR:     %s");
+            strncpy(logfmtstring,"DLT| ERROR:     %s",sizeof(logfmtstring));
             break;
         }
         case   LOG_WARNING:
         {
-            strcpy(logfmtstring,"DLT| WARNING:   %s");
+            strncpy(logfmtstring,"DLT| WARNING:   %s",sizeof(logfmtstring));
             break;
         }
         case   LOG_NOTICE:
         {
-            strcpy(logfmtstring,"DLT| NOTICE:    %s");
+            strncpy(logfmtstring,"DLT| NOTICE:    %s",sizeof(logfmtstring));
             break;
         }
         case   LOG_INFO:
         {
-            strcpy(logfmtstring,"DLT| INFO:      %s");
+            strncpy(logfmtstring,"DLT| INFO:      %s",sizeof(logfmtstring));
             break;
         }
         case   LOG_DEBUG:
         {
-            strcpy(logfmtstring,"DLT| DEBUG:     %s");
+            strncpy(logfmtstring,"DLT| DEBUG:     %s",sizeof(logfmtstring));
             break;
         }
         default:
         {
-            strcpy(logfmtstring,"DLT|            %s");
+            strncpy(logfmtstring,"DLT|            %s",sizeof(logfmtstring));
             break;
         }
     }
 
+       switch(logging_mode)
+       {
+               case 0:
+                       /* log to stdout */
+                       printf(logfmtstring, s);
+                       break;
+               case 1:
+                       /* log to syslog */
 #if !defined (__WIN32__) && !defined(_MSC_VER)
-    if (log_as_daemon)
-    {
-        openlog("DLT",LOG_PID,LOG_DAEMON);
-        syslog(prio, logfmtstring, s);
-        closelog();
-    }
-    else
+                       openlog("DLT",LOG_PID,LOG_DAEMON);
+                       syslog(prio, logfmtstring, s);
+                       closelog();
 #endif
-    {
-        printf(logfmtstring, s);
-    }
+                       break;
+               case 2:
+                       /* log to file */
+                       if(logging_handle) {
+                               fprintf(logging_handle,logfmtstring, s);
+                               fflush(logging_handle);
+                       }
+                       break;
+       }
 
     return 0;
 }
@@ -2155,6 +2249,13 @@ int dlt_receiver_remove(DltReceiver *receiver,int size)
         return -1;
     }
 
+    if (size > receiver->bytesRcvd || size <= 0)
+    {
+       receiver->buf = receiver->buf + receiver->bytesRcvd;
+       receiver->bytesRcvd=0;
+       return -1;
+    }
+
     receiver->bytesRcvd = receiver->bytesRcvd - size;
     receiver->buf = receiver->buf + size;
 
@@ -2232,601 +2333,517 @@ int dlt_check_storageheader(DltStorageHeader *storageheader)
              (storageheader->pattern[3] == 1));
 }
 
-int dlt_ringbuffer_init(DltRingBuffer *dltbuf, uint32_t size, uint32_t increasesize, uint32_t maxsize)
-{
 
-    if (dltbuf==0)
-    {
-        return -1;
-    }
 
-    if (size<=sizeof(uint32_t))
-    {
-        return -1;
-    }
 
-       dltbuf->minimum_size = size;
-    dltbuf->buffer=(char*)malloc(dltbuf->minimum_size);
-    
-    if (dltbuf->buffer==0)
-    {
-        return -1;
-    }
 
-    dltbuf->size= dltbuf->minimum_size;
-    dltbuf->increasing_size = increasesize;
-    dltbuf->maximum_size = maxsize;
 
-    dltbuf->pos_write=0;
-    dltbuf->pos_read=0;
 
-    dltbuf->count=0;
 
-    return 0;
-}
 
-int dlt_ringbuffer_free(DltRingBuffer *dltbuf)
-{
 
-    if (dltbuf==0)
-    {
-        return -1;
-    }
-
-    if (dltbuf->buffer)
-    {
-        free(dltbuf->buffer);
-    }
-
-    dltbuf->buffer=0;
 
-    dltbuf->size=0;
 
-    dltbuf->pos_write=0;
-    dltbuf->pos_read=0;
+int dlt_buffer_init_static_server(DltBuffer *buf, const unsigned char *ptr, uint32_t size)
+{
+       char str[256];
+       DltBufferHead *head;
 
-    dltbuf->count=0;
+       // Init parameters
+       buf->shm = (unsigned char *)ptr;
+       buf->min_size = size;
+       buf->max_size = size;
+       buf->step_size = 0;
+       
+       // Init pointers
+       head = (DltBufferHead*)buf->shm;
+       head->read = 0;
+       head->write = 0;
+       head->count = 0;
+    buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
+       buf->size = buf->min_size - sizeof(DltBufferHead);
+
+       // clear memory
+       memset(buf->mem,0,buf->size);
+    
+       snprintf(str,sizeof(str),"Buffer: Size %d\n",buf->size);
+       dlt_log(LOG_INFO, str);
 
-    return 0;
+       return 0; /* OK */
 }
 
-int dlt_ringbuffer_put(DltRingBuffer *dltbuf, void *data, uint32_t size)
+int dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char *ptr, uint32_t size)
 {
-       uint32_t sui, part1, part2;      
+       char str[256];
 
-    if (dltbuf==0)
-    {
-        return -1;
-    }
+       // Init parameters
+       buf->shm = (unsigned char *)ptr;
+       buf->min_size = size;
+       buf->max_size = size;
+       buf->step_size = 0;
+       
+       // Init pointers
+    buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
+    buf->size = buf->min_size - sizeof(DltBufferHead);
 
-    if (dltbuf->buffer==0)
-    {
-        return -1;
-    }
+       snprintf(str,sizeof(str),"Buffer: Size %d\n",buf->size);
+       dlt_log(LOG_INFO, str);
 
-    if (data==0)
-    {
-        return -1;
-    }
-
-    sui = sizeof(uint32_t);
+       return 0; /* OK */
+}
 
-       dlt_ringbuffer_checkandfreespace(dltbuf, (size+sui));
-    
-    if ((size+sui)>dltbuf->size)
-    {
-        return -1;
-    }
+int dlt_buffer_init_dynamic(DltBuffer *buf, uint32_t min_size, uint32_t max_size,uint32_t step_size)
+{
+    //Do not DLT_SEM_LOCK inside here!
+       char str[256];
+       DltBufferHead *head;
 
-    if (dltbuf->pos_write >= dltbuf->size)
-    {
-        dltbuf->pos_write = 0;
-    }
+       // Init parameters
+       buf->min_size = min_size;
+       buf->max_size = max_size;
+       buf->step_size = step_size;
 
-    /* Not enough space for one uint available before end of linear buffer */
-       dlt_ringbuffer_putMessageSize(dltbuf, &size,sui); 
-   
-    if (dltbuf->pos_write >= dltbuf->size)
-    {
-        dltbuf->pos_write = 0;
-    }
+       // allocat memory
+       buf->shm = malloc(buf->min_size);
+       if(buf->shm == NULL) {
+               snprintf(str,sizeof(str),"Buffer: Cannot allocate %d bytes\n",buf->min_size);
+               dlt_log(LOG_EMERG, str);
+               return -1;
+       }
+       
+       // Init pointers
+       head = (DltBufferHead*)buf->shm;
+       head->read = 0;
+       head->write = 0;
+       head->count = 0;
+    buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
+    buf->size = buf->min_size - sizeof(DltBufferHead);
+
+       // clear memory
+       memset(buf->mem,0,buf->size);
+    
+       //snprintf(str,sizeof(str),"Buffer: Size %d bytes\n",buf->size);
+       //dlt_log(LOG_INFO, str);
 
-    if ((dltbuf->size - dltbuf->pos_write) < size)
-    {
-        /* Not enough space til end of linear buffer, */
-        /* split up write call */
-        part1 = dltbuf->size - dltbuf->pos_write;
-        part2 = size - part1;
+       return 0; /* OK */
+}
 
-        memcpy(dltbuf->buffer + dltbuf->pos_write, data, part1);
-        memcpy(dltbuf->buffer, ((char*)data) + part1, part2);
-        dltbuf->pos_write = part2;
+int dlt_buffer_free_static(DltBuffer *buf)
+{
+       if(!buf->mem) {
+               // buffer not initialised
+               dlt_log(LOG_ERR,"Buffer: Buffer not initialised\n");
+               return -1; /* ERROR */
+       }
 
-    }
-    else
-    {
-        /* Enough space til end of linear buffer */
-        memcpy(&(dltbuf->buffer[dltbuf->pos_write]), data, size);
-        dltbuf->pos_write+=size;
-    }
+       return 0;
+}
 
-    dltbuf->count++;
+int dlt_buffer_free_dynamic(DltBuffer *buf)
+{
+       if(!buf->mem) {
+               // buffer not initialised
+               dlt_log(LOG_ERR,"Buffer: Buffer not initialised\n");
+               return -1; /* ERROR */
+       }
 
-    return 0;
+       free(buf->shm);
+       
+       return 0;
 }
 
-// This function is for writting messages size into buffer
-void dlt_ringbuffer_putMessageSize(DltRingBuffer *dltbuf,uint32_t * data_size, uint32_t unit_size)
+void dlt_buffer_write_block(DltBuffer *buf,int *write, const unsigned char *data,unsigned int size)
 {
-       unsigned char firstByte;
-       unsigned char secondByte;
-       unsigned char thirdByte;
-       unsigned char fourthByte;       
-       
-       if ((dltbuf->size - dltbuf->pos_write) < unit_size)
-    {
-               firstByte = (*data_size)>>0;
-               secondByte = (*data_size)>>8;
-               thirdByte = (*data_size)>>16;
-               fourthByte = (*data_size)>>24;
-               
-               switch(dltbuf->size - dltbuf->pos_write)
-               {
-                       case 1:
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write]), &firstByte, 1);
-                               dltbuf->pos_write = 0;
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]),&secondByte,1);
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]),&thirdByte,1);
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]),&fourthByte,1);
-                               break;                  
-                       case 2:
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]), &firstByte, 1);                  
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write]),&secondByte,1);
-                               dltbuf->pos_write = 0;
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]),&thirdByte,1);
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]),&fourthByte,1);                   
-                               break;                  
-                       case 3:
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]), &firstByte, 1);                  
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]),&secondByte,1);                   
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write]),&thirdByte,1);
-                               dltbuf->pos_write = 0;
-                               memcpy(&(dltbuf->buffer[dltbuf->pos_write++]),&fourthByte,1);                   
-                               break;
-               }       
-      
-    }
-    else
-       {
-               /* Write length of following data to buffer */
-               memcpy(&(dltbuf->buffer[dltbuf->pos_write]), data_size, unit_size);
-               dltbuf->pos_write +=unit_size;          
+       if((int)(*write+size) <= buf->size) {
+               // write one block
+               memcpy(buf->mem+*write,data,size);
+               *write += size;
+       }
+       else {
+               // write two blocks
+               memcpy(buf->mem+*write, data, buf->size-*write);
+               memcpy(buf->mem, data+buf->size-*write, size-buf->size+*write);
+               *write += size-buf->size;
        }       
 }
 
+void dlt_buffer_read_block(DltBuffer *buf,int *read,unsigned char *data,unsigned int size)
+{
+       if((int)(*read+size) <= buf->size) {
+               // read one block
+               memcpy(data,buf->mem+*read,size);
+               *read += size;
+       }
+       else {
+               // read two blocks
+               memcpy(data, buf->mem+*read, buf->size-*read);
+               memcpy(data+buf->size-*read, buf->mem, size-buf->size+*read);
+               *read += size-buf->size;
+       }       
+}      
 
-int dlt_ringbuffer_put3(DltRingBuffer *dltbuf, void *data1, uint32_t size1, void *data2, uint32_t size2, void *data3, uint32_t size3)
+int dlt_buffer_increase_size(DltBuffer *buf)
 {
-   
-    uint32_t sui, part1, part2;
-    uint32_t total_size;
+       DltBufferHead *head,*new_head;
+       unsigned char *new_ptr;
 
-    if (dltbuf==0)
-    {
-        return -1;
-    }
+       /* check size */
+       if(buf->step_size==0) {
+               /* cannot increase size */
+               return -1;
+       }
 
-    if (dltbuf->buffer==0)
-    {
-        return -1;
-    }
+       /* check size */
+       if((buf->size + sizeof(DltBufferHead) + buf->step_size) > buf->max_size) {
+               /* max size reached, do not increase */
+               return -1;
+       }
 
-    sui = sizeof(uint32_t);
+       /* allocate new buffer */
+       new_ptr = malloc(buf->size + sizeof(DltBufferHead) + buf->step_size);
+       if(new_ptr == NULL) {
+               snprintf(str,sizeof(str),"Buffer: Cannot increase size because allocate %d bytes failed\n",buf->min_size);
+               dlt_log(LOG_WARNING, str);
+               return -1;
+       }
+               
+       /* copy data */
+       head = (DltBufferHead*)buf->shm;
+       new_head = (DltBufferHead*)new_ptr;
+       if(head->read < head->write) {
+               memcpy(new_ptr+sizeof(DltBufferHead) , buf->mem+head->read , head->write-head->read);
+               new_head->read = 0;     
+               new_head->write = head->write-head->read;       
+               new_head->count = head->count;  
+       }
+       else {
+               memcpy(new_ptr+sizeof(DltBufferHead) , buf->mem+head->read , buf->size-head->read);
+               memcpy(new_ptr+sizeof(DltBufferHead)+buf->size-head->read , buf->mem , head->write);
+               new_head->read = 0;     
+               new_head->write = buf->size-head->read+head->write;     
+               new_head->count = head->count;                  
+       }
+       
+       /* free old data */
+       free(buf->shm);
+       
+       /* update data */
+       buf->shm = new_ptr;
+       buf->mem = new_ptr+sizeof(DltBufferHead);
+       buf->size += buf->step_size;
+       
+       snprintf(str,sizeof(str),"Buffer: Size increased to %d bytes\n",buf->size+sizeof(DltBufferHead));
+       dlt_log(LOG_INFO, str);
 
-    total_size = size1+size2+size3;
+       return 0; // OK         
+}
 
-       dlt_ringbuffer_checkandfreespace(dltbuf, (total_size+sui));
-    
-    if ((total_size+sui)>dltbuf->size)
-    {
-        return -1;
-    }   
+int dlt_buffer_minimize_size(DltBuffer *buf)
+{
+       unsigned char *new_ptr;
 
-    if (dltbuf->pos_write >= dltbuf->size)
-    {
-        dltbuf->pos_write = 0;
-    }
+       if((buf->size + sizeof(DltBufferHead)) == buf->min_size)
+       {
+               /* already minimized */
+               return 0;
+       }
 
-    //Write size of data
-    /* Not enough space for one uint available before end of linear buffer */
-    dlt_ringbuffer_putMessageSize(dltbuf, &total_size,sui); 
+       /* allocate new buffer */
+       new_ptr = malloc(buf->min_size);
+       if(new_ptr == NULL) {
+               snprintf(str,sizeof(str),"Buffer: Cannot set to min size of %d bytes\n",buf->min_size);
+               dlt_log(LOG_WARNING, str);
+               return -1;
+       }
 
-    if (dltbuf->pos_write >= dltbuf->size)
-    {
-        dltbuf->pos_write = 0;
-    }
+       /* free old data */
+       free(buf->shm);
+       
+       /* update data */
+       buf->shm = new_ptr;
+       buf->mem = new_ptr+sizeof(DltBufferHead);
+       buf->size = buf->min_size - sizeof(DltBufferHead);
 
-    /* First chunk of data (data1, size1) */
-    if ((dltbuf->size - dltbuf->pos_write) < size1)
-    {
-        /* Not enough space til end of linear buffer, */
-        /* split up write call */
-        part1 = dltbuf->size - dltbuf->pos_write;
-        part2 = size1 - part1;
+       /* reset pointers and counters */       
+       ((int*)(buf->shm))[0] = 0;  // pointer to write memory  
+       ((int*)(buf->shm))[1] = 0;  // pointer to read memory
+       ((int*)(buf->shm))[2] = 0;  // number of packets
 
-        memcpy(dltbuf->buffer + dltbuf->pos_write, data1, part1);
-        memcpy(dltbuf->buffer, ((char*)data1) + part1, part2);
-        dltbuf->pos_write = part2;
+       // clear memory
+       memset(buf->mem,0,buf->size);
 
-    }
-    else
-    {
-        /* Enough space til end of linear buffer */
-        memcpy(&(dltbuf->buffer[dltbuf->pos_write]), data1, size1);
-        dltbuf->pos_write+=size1;
-    }
+       dlt_log(LOG_INFO,"Buffer: Buffer minimized.\n");
 
-    if (dltbuf->pos_write >= dltbuf->size)
-    {
-        dltbuf->pos_write = 0;
-    }
+       return 0; /* OK */      
+}
 
-    /* Second chunk of data (data2, size2) */
-    if ((dltbuf->size - dltbuf->pos_write) < size2)
-    {
-        /* Not enough space til end of linear buffer, */
-        /* split up write call */
-        part1 = dltbuf->size - dltbuf->pos_write;
-        part2 = size2 - part1;
+int dlt_buffer_reset(DltBuffer *buf)
+{
+       dlt_log(LOG_ERR,"Buffer: Buffer reset triggered.\n");
 
-        memcpy(dltbuf->buffer + dltbuf->pos_write, data2, part1);
-        memcpy(dltbuf->buffer, ((char*)data2) + part1, part2);
-        dltbuf->pos_write = part2;
+       /* reset pointers and counters */       
+       ((int*)(buf->shm))[0] = 0;  // pointer to write memory  
+       ((int*)(buf->shm))[1] = 0;  // pointer to read memory
+       ((int*)(buf->shm))[2] = 0;  // number of packets
 
-    }
-    else
-    {
-        /* Enough space til end of linear buffer */
-        memcpy(&(dltbuf->buffer[dltbuf->pos_write]), data2, size2);
-        dltbuf->pos_write+=size2;
-    }
+       // clear memory
+       memset(buf->mem,0,buf->size);
 
-    if (dltbuf->pos_write >= dltbuf->size)
-    {
-        dltbuf->pos_write = 0;
-    }
+       return 0; /* OK */
+}
 
-    /* Third chunk of data (data3, size3) */
-    if ((dltbuf->size - dltbuf->pos_write) < size3)
-    {
-        /* Not enough space til end of linear buffer, */
-        /* split up write call */
-        part1 = dltbuf->size - dltbuf->pos_write;
-        part2 = size3 - part1;
+int dlt_buffer_push(DltBuffer *buf,const unsigned char *data,unsigned int size)
+{
+       return dlt_buffer_push3(buf,data,size,0,0,0,0);
+}
 
-        memcpy(dltbuf->buffer + dltbuf->pos_write, data3, part1);
-        memcpy(dltbuf->buffer, ((char*)data3) + part1, part2);
-        dltbuf->pos_write = part2;
+int dlt_buffer_push3(DltBuffer *buf,const unsigned char *data1,unsigned int size1,const unsigned char *data2,unsigned int size2,const unsigned char *data3,unsigned int size3)
+{
+       int free_size;  
+       int write, read, count;
+       DltBufferBlockHead head;
+       
+       if(!buf->mem) {
+               // buffer not initialised
+               dlt_log(LOG_ERR,"Buffer: Buffer not initialised\n");
+               return -1; /* ERROR */
+       }
 
-    }
-    else
-    {
-        /* Enough space til end of linear buffer */
-        memcpy(dltbuf->buffer + dltbuf->pos_write, data3, size3);
-        dltbuf->pos_write+=size3;
-    }
+       // get current write pointer
+       write = ((int*)(buf->shm))[0];
+       read = ((int*)(buf->shm))[1];
+       count = ((int*)(buf->shm))[2];
 
-    dltbuf->count++;
+       // check pointers
+       if((read>buf->size) || (write>buf->size))
+       {
+               dlt_log(LOG_ERR,"Buffer: Pointer out of range\n");
+               dlt_buffer_reset(buf);
+               return -1; // ERROR             
+       }
 
-    return 0;
-}
+       // calculate free size
+       if(read>write)
+               free_size = read - write;
+       else if(count && (write == read))
+               free_size = 0;  
+       else
+               free_size = buf->size - write + read;
+       
+       // check size
+       if(free_size < (int)(sizeof(DltBufferBlockHead)+size1+size2+size3)) {
+               // try to increase size if possible
+               if(dlt_buffer_increase_size(buf)) {
+                       /* increase size is not possible */
+                       dlt_log(LOG_ERR,"Buffer: Buffer is full\n");
+                       return -1; // ERROR
+               }
+               // update pointers
+               write = ((int*)(buf->shm))[0];
+               read = ((int*)(buf->shm))[1];
+       }
 
-int dlt_ringbuffer_get(DltRingBuffer *dltbuf, void *data, size_t *size)
-{
-    uint32_t tmpsize=0;
-    uint32_t sui;
+       // set header
+       strcpy(head.head,DLT_BUFFER_HEAD);
+       head.status = 2;
+       head.size = size1+size2+size3;
 
-    uint32_t part1, part2;
+       // write data
+       dlt_buffer_write_block(buf,&write,(unsigned char*)&head,sizeof(DltBufferBlockHead));
+       if(size1) dlt_buffer_write_block(buf,&write,data1,size1);
+       if(size2) dlt_buffer_write_block(buf,&write,data2,size2);
+       if(size3) dlt_buffer_write_block(buf,&write,data3,size3);
 
-    if (dltbuf==0)
-    {
-        return -1;
-    }
+       // update global shm pointers
+       ((int*)(buf->shm))[0] = write; // set new write pointer         
+       ((int*)(buf->shm))[2] += 1; // increase counter
 
-    if (dltbuf->buffer==0)
-    {
-        return -1;
-    }
+       return 0; // OK
+       
+}
 
-    if (dltbuf->count==0)
-    {
-        return -1;
-    }
+int dlt_buffer_get(DltBuffer *buf,unsigned char *data, int max_size,int delete)
+{
+       int used_size;  
+       int write, read, count;
+       char head_compare[] = DLT_BUFFER_HEAD;
+       DltBufferBlockHead head;
+       
+       if(!buf->mem) {
+               // shm not initialised
+               dlt_log(LOG_ERR,"Buffer: SHM not initialised\n");
+               return -1; /* ERROR */
+       }
 
-    sui = sizeof(uint32_t);
+       // get current write pointer
+       write = ((int*)(buf->shm))[0];
+       read = ((int*)(buf->shm))[1];
+       count = ((int*)(buf->shm))[2];
 
-    if (dltbuf->pos_read >= dltbuf->size)
-    {
-        dltbuf->pos_read = 0;
-    }
+       // check pointers
+       if((read>buf->size) || (write>buf->size) || (count<0))
+       {
+               dlt_log(LOG_ERR,"Buffer: Pointer out of range\n");
+               dlt_buffer_reset(buf);
+               return -1; // ERROR             
+       }
 
-  // get size of data 
-       tmpsize = dlt_ringbuffer_getMessageSize(dltbuf, sui);               
+       // check if data is in there
+       if(count==0) {
+               if(write!=read)
+               {
+                       dlt_log(LOG_ERR,"Buffer: SHM should be empty, but is not\n");
+                       dlt_buffer_reset(buf);
+               }
+               return -1; // ERROR             
+       }
 
-    if (dltbuf->pos_read >= dltbuf->size)
-    {
-        dltbuf->pos_read = 0;
-    }
+       // calculate used size
+       if(write>read)
+               used_size = write - read;
+       else    
+               used_size = buf->size - read + write;
+
+       // first check size
+       if(used_size < (int)(sizeof(DltBufferBlockHead))) {
+               dlt_log(LOG_ERR,"Buffer: Size check 1 failed\n");
+               dlt_buffer_reset(buf);
+               return -1; // ERROR
+       }
 
-    if ((tmpsize>0) && ((tmpsize+sizeof(uint32_t))<=dltbuf->size))
-    {
-        if ((dltbuf->size - dltbuf->pos_read) < tmpsize)
-        {
-            /* Not enough space til end of linear buffer, */
-            /* split up read call */
-            part1 = dltbuf->size - dltbuf->pos_read;
-            part2 = tmpsize - part1;
+       // read header
+       dlt_buffer_read_block(buf,&read,(unsigned char*)&head,sizeof(DltBufferBlockHead));
 
-            memcpy(data, dltbuf->buffer + dltbuf->pos_read, part1);
-            memcpy(((char*)data)+part1, dltbuf->buffer, part2);
-            dltbuf->pos_read = part2;
-        }
-        else
-        {
-            /* Enough space til end of linear buffer */
-            /* no split up read call */
-            memcpy(data, &(dltbuf->buffer[dltbuf->pos_read]), tmpsize);
-            dltbuf->pos_read+=tmpsize;
-        }
-        *size = tmpsize;
-    }
-    else
-    {
-        data=0;
-        *size=0;
-    }
+       // check header
+       if(memcmp((unsigned char*)(head.head),head_compare,sizeof(head_compare))!=0)
+       {
+               dlt_log(LOG_ERR,"Buffer: Header head check failed\n");
+               dlt_buffer_reset(buf);
+               return -1; // ERROR
+       }
+       if(head.status != 2)
+       {
+               dlt_log(LOG_ERR,"Buffer: Header status check failed\n");
+               dlt_buffer_reset(buf);
+               return -1; // ERROR
+       }
+
+       // second check size
+       if(used_size < (int)(sizeof(DltBufferBlockHead)+head.size)) {
+               dlt_log(LOG_ERR,"Buffer: Size check 2 failed\n");
+               dlt_buffer_reset(buf);
+               return -1; // ERROR
+       }
 
-    dltbuf->count--;
+       // third check size
+       if(max_size && (head.size > max_size)) {
+               dlt_log(LOG_ERR,"Buffer: Size check 3 failed\n");
+               // nothing to do but data does not fit provided buffer
+       }
 
-    return 0;
-}
+       if(data && max_size)
+       {
+               // read data
+               dlt_buffer_read_block(buf,&read,data,head.size);
 
-int dlt_ringbuffer_getMessageSize(DltRingBuffer *dltbuf, uint32_t unit_size)
-{
-       uint32_t temp;
-       uint32_t retVal;
-       unsigned char firstByte = 0;
-       unsigned char secondByte = 0;
-       unsigned char thirdByte = 0;
-       unsigned char fourthByte = 0;   
-       
-       if ((dltbuf->size - dltbuf->pos_read) < unit_size)
-    {
-               switch(dltbuf->size - dltbuf->pos_read)
+               if(delete)
                {
-                       case 1:
-                               memcpy(&firstByte,&(dltbuf->buffer[dltbuf->pos_read]), 1);
-                               dltbuf->pos_read = 0;
-                               memcpy(&secondByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);
-                               memcpy(&thirdByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);
-                               memcpy(&fourthByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);                   
-                               break;
-                       case 2:
-                               memcpy(&firstByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);                            
-                               memcpy(&secondByte,&(dltbuf->buffer[dltbuf->pos_read]), 1);
-                               dltbuf->pos_read = 0;
-                               memcpy(&thirdByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);
-                               memcpy(&fourthByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);                   
-                               break;
-                       case 3:
-                               memcpy(&firstByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);                            
-                               memcpy(&secondByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);
-                               memcpy(&thirdByte,&(dltbuf->buffer[dltbuf->pos_read]), 1);
-                               dltbuf->pos_read = 0;
-                               memcpy(&fourthByte,&(dltbuf->buffer[dltbuf->pos_read++]), 1);                   
-                               break;
-               }               
-               
-               retVal = firstByte;
-               
-               temp = secondByte;
-               temp = temp<<8;
-               retVal |= temp;
-               
-               temp = thirdByte;
-               temp = temp<<16;
-               retVal |= temp;
-               
-               temp = fourthByte;
-               temp = temp<<24;
-               retVal |= temp;
+                       // update buffer pointers
+                       ((int*)(buf->shm))[1] = read; // set new read pointer   
+               }
        }
-    else
-    {
-               /* printf("Reading at offset: %d\n", dltbuf->pos_read); */
-               memcpy(&retVal,&(dltbuf->buffer[dltbuf->pos_read]), unit_size);
-               dltbuf->pos_read += unit_size;
+       else
+       {
+               if(delete) {
+                       if( (read+head.size) <= buf->size)
+                               ((int*)(buf->shm))[1] = read+head.size; // set new read pointer         
+                       else
+                               ((int*)(buf->shm))[1] = read+head.size-buf->size; // set new read pointer               
+               }
        }
+       if(delete) {
+               ((int*)(buf->shm))[2] -= 1; // decrease counter
        
-       return retVal;  
+               if(((int*)(buf->shm))[2] == 0)
+               {
+                       // try to minimize size
+                       dlt_buffer_minimize_size(buf);
+               }
+       }
+       
+       return head.size; // OK 
 }
 
-int dlt_ringbuffer_get_skip(DltRingBuffer *dltbuf)
+int dlt_buffer_pull(DltBuffer *buf,unsigned char *data, int max_size)
 {
-    uint32_t tmpsize=0;
-    uint32_t sui;
-
-    uint32_t part1, part2;
-
-    if (dltbuf==0)
-    {
-        return -1;
-    }
+       return dlt_buffer_get(buf,data,max_size,1);
+}
 
-    if (dltbuf->buffer==0)
-    {
-        return -1;
-    }
+int dlt_buffer_copy(DltBuffer *buf,unsigned char *data, int max_size)
+{
+       return dlt_buffer_get(buf,data,max_size,0);
+}
 
-    if (dltbuf->count==0)
-    {
-        return -1;
-    }
+int dlt_buffer_remove(DltBuffer *buf)
+{
+       return dlt_buffer_get(buf,0,0,1);
+}
 
-    sui = sizeof(uint32_t);
+void dlt_buffer_info(DltBuffer *buf)
+{
+       char str[256];
 
-    if (dltbuf->pos_read >= dltbuf->size)
-    {
-        dltbuf->pos_read = 0;
-    }
-    
-       tmpsize = dlt_ringbuffer_getMessageSize(dltbuf, sui);
+       snprintf(str,sizeof(str),"Buffer: Available size: %d\n",buf->size);
+       dlt_log(LOG_INFO, str);
+       snprintf(str,sizeof(str),"Buffer: Buffer full start address: %lX\n",(unsigned long)buf->shm);
+       dlt_log(LOG_INFO, str);
+       snprintf(str,sizeof(str),"Buffer: Buffer start address: %lX\n",(unsigned long)buf->mem);
+       dlt_log(LOG_INFO, str);
        
-    if (dltbuf->pos_read >= dltbuf->size)
-    {
-        dltbuf->pos_read = 0;
-    }
-
-    if ((tmpsize>0) && ((tmpsize+sui)<=dltbuf->size))
-    {
-        if ((dltbuf->size - dltbuf->pos_read) < tmpsize)
-        {
-            /* Not enough space til end of linear buffer */
-            part1 = dltbuf->size - dltbuf->pos_read;
-            part2 = tmpsize - part1;
-
-            dltbuf->pos_read = part2;
-        }
-        else
-        {
-            /* Enough space til end of linear buffer */
-            dltbuf->pos_read+=tmpsize;
-        }
-    }
-
-    dltbuf->count--;
-
-    return 0;
 }
 
-
-int dlt_ringbuffer_freespacewrite(DltRingBuffer *dltbuf, uint32_t *freespace)
+void dlt_buffer_status(DltBuffer *buf)
 {
-    if ((dltbuf==0) || (freespace==0))
-    {
-        return -1;
-    }
+       int write, read, count;
+       char str[256];
 
-    *freespace=0;
+       write = ((int*)(buf->shm))[0];
+       read = ((int*)(buf->shm))[1];
+       count = ((int*)(buf->shm))[2];
 
-    /* Space til pos_read */
-    if (dltbuf->pos_read > dltbuf->pos_write)
-    {
-        *freespace=(dltbuf->pos_read - dltbuf->pos_write);
-        return 0;
-    }
-    else if (dltbuf->pos_read < dltbuf->pos_write)
-    {
-        *freespace=(dltbuf->size - dltbuf->pos_write + dltbuf->pos_read );
-        return 0;
-    }
-    else
-    {
-        if (dltbuf->count)
-        {
-            return 0;
-        }
-        else
-        {
-            *freespace=dltbuf->size;
-            return 0;
-        }
-    }
-    return 0;
+       snprintf(str,sizeof(str),"Buffer: Write: %d\n",write);
+       dlt_log(LOG_INFO, str);
+       snprintf(str,sizeof(str),"Buffer: Read: %d\n",read);
+       dlt_log(LOG_INFO, str);
+       snprintf(str,sizeof(str),"Buffer: Count: %d\n",count);
+       dlt_log(LOG_INFO, str); 
 }
 
-int dlt_ringbuffer_checkandfreespace(DltRingBuffer *dltbuf, uint32_t reqspace)
+int dlt_buffer_get_total_size(DltBuffer *buf)
 {
-    uint32_t space_left;
-    uint32_t newSize;
-    uint32_t sizeOfNonReadData; 
-    
-    if (dltbuf==0)
-    {
-        return -1;
-    }
+       return buf->max_size;   
+}
 
-    if (dlt_ringbuffer_freespacewrite(dltbuf,&space_left) == -1)
-    {
-       return -1;
-    }
+int dlt_buffer_get_used_size(DltBuffer *buf)
+{
+       int write, read, count;
 
-    /* printf("Now reading at: %d, space_left = %d, req = %d, r=%d, w=%d, count=%d \n",
-              dltbuf->pos_read,space_left, reqspace, dltbuf->pos_read, dltbuf->pos_write, dltbuf->count); */
+       write = ((int*)(buf->shm))[0];
+       read = ((int*)(buf->shm))[1];
+       count = ((int*)(buf->shm))[2];
 
-       if(space_left<reqspace)
-       {
-               if(dltbuf->size < dltbuf->maximum_size)
-               {
-                       char * extendedBuffer;
-                       
-                       newSize = dltbuf->increasing_size + dltbuf->size;
-                       if(newSize > dltbuf->maximum_size)
-                       {
-                               newSize = dltbuf->maximum_size;
-                       }
-                       
-                       extendedBuffer = (char *) malloc(newSize);
-                       
+       if(count == 0)
+               return 0;
                
-                       if(dltbuf->pos_write > dltbuf->pos_read)
-                       {
-                               //printf("Buffer read < write \n");
-                               sizeOfNonReadData = dltbuf->pos_write - dltbuf->pos_read;                       
-                               memcpy(extendedBuffer,&(dltbuf->buffer[dltbuf->pos_read]),sizeOfNonReadData);
-                               free(dltbuf->buffer);
-                               dltbuf->buffer = extendedBuffer;
-                               dltbuf->pos_read = dltbuf->buffer[0];
-                               dltbuf->pos_write = sizeOfNonReadData;
-                               dltbuf->size = newSize;
-                       }
-                       else if(dltbuf->pos_write <= dltbuf->pos_read && dltbuf->count >0)
-                       {
-                               
-                               //printf("Buffer read = write \n");
-                               sizeOfNonReadData = (dltbuf->size - dltbuf->pos_read) + (dltbuf->pos_write - dltbuf->buffer[0]);                
-                               memcpy(extendedBuffer,&(dltbuf->buffer[dltbuf->pos_read]),dltbuf->size - dltbuf->pos_read);
-                               memcpy(&(extendedBuffer[dltbuf->size - dltbuf->pos_read]),&(dltbuf->buffer[0]),dltbuf->pos_write - dltbuf->buffer[0]);          
-                               free(dltbuf->buffer);
-                               dltbuf->buffer = extendedBuffer;
-                               dltbuf->pos_read = dltbuf->buffer[0];
-                               dltbuf->pos_write = sizeOfNonReadData;
-                               dltbuf->size = newSize;
-                       }                                               
-                       //printf("Buffer size is increased to %d!\n",newSize);
-               }
-               else
-               {
-                       //printf("Maximum buffer size reached!\n");
-                       while (space_left<reqspace)
-                       {
-                               /* Overwrite, correct read position */
-                               /* Read and skip one element */
-                               dlt_ringbuffer_get_skip(dltbuf);
-                               
-                               /* Space until pos_read */
-                               if (dlt_ringbuffer_freespacewrite(dltbuf,&space_left) == -1)
-                               {
-                                       return -1;
-                               }
-                               
-                               /* printf("Overwrite: Now reading at: %d, space_left = %d, req = %d, r=%d, w=%d, count=%d \n",
-                                                 dltbuf->pos_read,space_left, reqspace, dltbuf->pos_read, dltbuf->pos_write, dltbuf->count); */
-                       } 
-               }                                       
-       }
+       if(write>read)
+               return (write - read);
        
-       return 0; 
+       return (buf->size - read + write);      
+}
+
+int dlt_buffer_get_message_count(DltBuffer *buf)
+{
+       return ((int*)(buf->shm))[2];
 }
 
 #if !defined (__WIN32__)
@@ -3080,10 +3097,21 @@ speed_t dlt_convert_serial_speed(int baudrate)
 
 void dlt_get_version(char *buf)
 {
-    sprintf(buf,"DLT Package Version: %s %s, Package Revision: %s, build on %s %s\n",
-            PACKAGE_VERSION, PACKAGE_VERSION_STATE, SVN_VERSION, __DATE__ , __TIME__ );
+    sprintf(buf,"DLT Package Version: %s %s, Package Revision: %s, build on %s %s\n%s %s %s %s\n",
+            _DLT_PACKAGE_VERSION, _DLT_PACKAGE_VERSION_STATE, _DLT_PACKAGE_REVISION, __DATE__ , __TIME__,_DLT_SYSTEMD_ENABLE,_DLT_SYSTEMD_WATCHDOG_ENABLE,_DLT_TEST_ENABLE,_DLT_SHM_ENABLE );
+}
+
+void dlt_get_major_version(char *buf)
+{
+       sprintf(buf,"%s",_DLT_PACKAGE_MAJOR_VERSION);
 }
 
+void dlt_get_minor_version(char *buf)
+{
+    sprintf(buf,"%s",_DLT_PACKAGE_MINOR_VERSION);
+}
+
+
 uint32_t dlt_uptime(void)
 {
 
@@ -3095,8 +3123,8 @@ uint32_t dlt_uptime(void)
     struct timespec ts;
 
     if (clock_gettime(CLOCK_MONOTONIC,&ts)==0)
-    {
-        return (uint32_t)((((ts.tv_sec*1000000)+(ts.tv_nsec/1000)))/100); // in 0.1 ms = 100 us
+    {        
+        return ( (uint32_t)ts.tv_sec*10000 + (uint32_t)ts.tv_nsec/100000 );// in 0.1 ms = 100 us
     }
     else
     {
@@ -3200,7 +3228,7 @@ int dlt_message_argument_print(DltMessage *msg,uint32_t type_info,uint8_t **ptr,
     float64_t value64f=0,value64f_tmp=0;
     int64_t value64f_tmp_int64i=0,value64f_tmp_int64i_swaped=0;
 
-    uint32_t quantisation=0, quantisation_tmp=0;
+    uint32_t quantisation_tmp=0;
 
     if (type_info & DLT_TYPE_INFO_STRG)
     {
@@ -3255,7 +3283,7 @@ int dlt_message_argument_print(DltMessage *msg,uint32_t type_info,uint8_t **ptr,
             return -1;
         sprintf(text+strlen(text),"%d",value8u);
     }
-    else if (type_info & DLT_TYPE_INFO_SINT || type_info & DLT_TYPE_INFO_UINT)
+    else if ((type_info & DLT_TYPE_INFO_SINT) || (type_info & DLT_TYPE_INFO_UINT))
     {
         /* signed or unsigned argument received */
         if (type_info & DLT_TYPE_INFO_VARI)
@@ -3279,13 +3307,11 @@ int dlt_message_argument_print(DltMessage *msg,uint32_t type_info,uint8_t **ptr,
         }
         if (type_info & DLT_TYPE_INFO_FIXP)
         {
-            quantisation=0;
-            quantisation_tmp=0;
+            //compiler warning: variable ‘quantisation_tmp’ set but not used [-Wunused-but-set-variable], but: DLT_MSG_READ_VALUE wants a parameter, "0" does not work
             DLT_MSG_READ_VALUE(quantisation_tmp,*ptr,*datalength,uint32_t);
+
             if((*datalength)<0)
                 return -1;
-            quantisation=DLT_ENDIAN_GET_32(msg->standardheader->htyp, quantisation_tmp);
-
             switch (   type_info & DLT_TYPE_INFO_TYLE)
             {
                                case DLT_TYLE_8BIT: