DLT_CSTRING implementation non verbose mode.
[profile/ivi/dlt-daemon.git] / include / dlt / dlt_common.h
index 44c1f33..d03eca8 100755 (executable)
@@ -1,41 +1,25 @@
-/*
- * Dlt- Diagnostic Log and Trace
+/**
  * @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
+ * 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/.
+ *
  *
- * 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.
+ * \author Alexander Wenzel <alexander.aw.wenzel@bmw.de> BMW 2011-2012
  *
+ * \file dlt_common.h
+ * For further information see http://www.genivi.org/.
  * @licence end@
  */
 
-
 /*******************************************************************************
 **                                                                            **
 **  SRC-MODULE: dlt_common.h                                                  **
 #define DLT_SWAP_32(value) ((((value) >> 24)&0xff) | (((value) << 8)&0xff0000) | (((value) >> 8)&0xff00) | (((value) << 24)&0xff000000))
 
 /* Set Big Endian and Little Endian to a initial value, if not defined */
+#if !defined __USE_BSD
 #ifndef LITTLE_ENDIAN
 #define LITTLE_ENDIAN 1234
 #endif
 #ifndef BIG_ENDIAN
 #define BIG_ENDIAN    4321
 #endif
+#endif /* __USE_BSD */
 
 /* If byte order is not defined, default to little endian */
+#if !defined __USE_BSD
 #ifndef BYTE_ORDER
 #define BYTE_ORDER LITTLE_ENDIAN
 #endif
+#endif /* __USE_BSD */
 
 /* Check for byte-order */
 #if (BYTE_ORDER==BIG_ENDIAN)
 #define        LOG_DAEMON      (3<<3)
 #endif
 
+enum {
+    DLT_LOG_TO_CONSOLE=0,
+    DLT_LOG_TO_SYSLOG=1,
+    DLT_LOG_TO_FILE=2,
+    DLT_LOG_DROPPED=3
+};
+
 /**
  * The standard TCP Port used for DLT daemon
  */
 #define DLT_DAEMON_TCP_PORT 3490
 
+
+/* Initi value for file descritpor */
+#define DLT_FD_INIT -1
+
+/* Minimum value for a file descriptor except the POSIX Standards: stdin=0, stdout=1, stderr=2 */
+#define DLT_FD_MINIMUM 3
+
 /**
  * The size of a DLT ID
  */
     \
     if(_verbose) \
     { \
-               sprintf(_strbuf, "%s()\n",__func__); \
+               snprintf(_strbuf, 255, "%s()\n",__func__); \
                dlt_log(LOG_INFO, _strbuf); \
     } \
 }
@@ -336,7 +338,7 @@ typedef char ID4[DLT_ID_SIZE];
 typedef struct
 {
     char pattern[DLT_ID_SIZE];         /**< This pattern should be DLT0x01 */
-    time_t seconds;                                /**< seconds since 1.1.1970 */
+    uint32_t seconds;                              /**< seconds since 1.1.1970 */
     int32_t microseconds;                      /**< Microseconds */
     char ecu[DLT_ID_SIZE];                     /**< The ECU id is added, if it is not already in the DLT message itself */
 } PACKED DltStorageHeader;
@@ -392,6 +394,7 @@ typedef struct sDltMessage
     uint8_t headerbuffer[sizeof(DltStorageHeader)+
                          sizeof(DltStandardHeader)+sizeof(DltStandardHeaderExtra)+sizeof(DltExtendedHeader)]; /**< buffer for loading complete header */
     uint8_t *databuffer;         /**< buffer for loading payload */
+       int32_t databuffersize;
 
     /* header values of current loaded message */
     DltStorageHeader       *storageheader;  /**< pointer to storage header of current loaded header */
@@ -417,6 +420,7 @@ typedef struct
  */
 typedef struct
 {
+
     uint32_t service_id;            /**< service ID */
     char apid[DLT_ID_SIZE];                   /**< application id */
     char ctid[DLT_ID_SIZE];                   /**< context id */
@@ -481,6 +485,7 @@ typedef struct
     uint32_t service_id;            /**< service ID */
     uint8_t status;                 /**< reponse status */
     uint8_t overflow;               /**< overflow status */
+    uint32_t overflow_counter;      /**< overflow counter */
 } PACKED DltServiceMessageBufferOverflowResponse;
 
 typedef struct
@@ -488,10 +493,44 @@ typedef struct
     uint32_t service_id;            /**< service ID */
     uint8_t  status;                /**< reponse status */
     uint32_t length;                /**< length of following payload */
-    /* char [] payload */
+    /*char [] payload;*/
 } PACKED DltServiceGetSoftwareVersionResponse;
 
 /**
+ * The structure of the DLT Service Unregister Context.
+ */
+typedef struct
+{
+    uint32_t service_id;            /**< service ID */
+    uint8_t status;                 /**< reponse status */
+    char apid[DLT_ID_SIZE];         /**< application id */
+    char ctid[DLT_ID_SIZE];         /**< context id */
+    char comid[DLT_ID_SIZE];        /**< communication interface */
+} PACKED DltServiceUnregisterContext;
+
+/**
+ * The structure of the DLT Service Connection Info
+ */
+typedef struct
+{
+    uint32_t service_id;            /**< service ID */
+    uint8_t status;                 /**< reponse status */
+    uint8_t state;                  /**< new state */
+    char comid[DLT_ID_SIZE];        /**< communication interface */
+} PACKED DltServiceConnectionInfo;
+
+/**
+ * The structure of the DLT Service Timezone
+ */
+typedef struct
+{
+    uint32_t service_id;            /**< service ID */
+    uint8_t status;                 /**< reponse status */
+    int32_t timezone;               /**< Timezone in seconds */
+    uint8_t isdst;                  /**< Is daylight saving time */
+} PACKED DltServiceTimezone;
+
+/**
  * Structure to store filter parameters.
  * ID are maximal four characters. Unused values are filled with zeros.
  * If every value as filter is valid, the id should be empty by having only zero values.
@@ -550,16 +589,35 @@ typedef struct
 
 typedef struct
 {
-    char*   buffer;         /**< Ptr. to buffer */
-    uint32_t    size;       /**< Maximum size of buffer */
-    uint32_t    pos_write;  /**< current writing position in bytes*/
-    uint32_t    pos_read;   /**< current reading position in bytes*/
-    uint32_t    count;      /**< nr. of entries */
-    uint32_t   minimum_size;   /**< minimum value for the buffer  */
-    uint32_t   increasing_size;        /**< increasing value for the buffer    */
-    uint32_t   maximum_size;   /**< maximum value for the buffer*/
-    
-} DltRingBuffer;
+       unsigned char* shm;     /* pointer to beginning of shared memory */
+       int size;       /* size of data area in shared memory */
+       unsigned char* mem;     /* pointer to data area in shared memory */     
+       
+    uint32_t    min_size;  /**< Minimum size of buffer */
+    uint32_t    max_size;  /**< Maximum size of buffer */
+    uint32_t    step_size; /**< Step size of buffer */
+} DltBuffer;
+
+typedef struct
+{
+       int write;
+       int read;
+       int count;      
+} DltBufferHead;
+
+#define DLT_BUFFER_HEAD        "SHM"
+
+typedef struct
+{
+       char head[4];
+       unsigned char status;
+       int size;       
+} DltBufferBlockHead;
+
+#define DLT_MESSAGE_ERROR_OK                   0
+#define DLT_MESSAGE_ERROR_UNKNOWN              -1
+#define DLT_MESSAGE_ERROR_SIZE          -2
+#define DLT_MESSAGE_ERROR_CONTENT      -3
 
 #ifdef __cplusplus
 extern "C"
@@ -866,8 +924,18 @@ extern "C"
     int dlt_file_free(DltFile *file,int verbose);
 
     /**
+     * Set internal logging filename if mode 2
+     * @param filename the filename
+     */
+    void dlt_log_set_filename(const char *filename);
+    /**
+     * Set internal logging level
+     * @param level the level
+     */
+    void dlt_log_set_level(int level);
+    /**
      * Initialize (external) logging facility
-     * @param mode positive, if syslog should be used; zero for console output
+     * @param mode positive, 0 = log to stdout, 1 = log to syslog, 2 = log to file
      */
     void dlt_log_init(int mode);
     /**
@@ -936,90 +1004,140 @@ extern "C"
      */
     int dlt_check_storageheader(DltStorageHeader *storageheader);
 
+
     /**
-     * Initialize ringbuffer of with a maximum size of size
-     * @param dltbuf Pointer to ringbuffer structure
+     * Initialise static ringbuffer with a size of size.
+     * Initialise as server. Init counters. 
+     * Memory is already allocated.
+     * @param buf Pointer to ringbuffer structure
+     * @param ptr Ptr to ringbuffer memory
      * @param size Maximum size of buffer in bytes
      * @return negative value if there was an error
      */
-    int dlt_ringbuffer_init(DltRingBuffer *dltbuf, uint32_t size, uint32_t increase_size, uint32_t max_size);
+    int dlt_buffer_init_static_server(DltBuffer *buf, const unsigned char *ptr, uint32_t size);
 
     /**
-     * Release and free memory used by ringbuffer
-     * @param dltbuf Pointer to ringbuffer structure
+     * Initialize static ringbuffer with a size of size.
+     * Initialise as a client. Do not change counters.
+     * Memory is already allocated.
+     * @param buf Pointer to ringbuffer structure
+     * @param ptr Ptr to ringbuffer memory
+     * @param size Maximum size of buffer in bytes
      * @return negative value if there was an error
      */
-    int dlt_ringbuffer_free(DltRingBuffer *dltbuf);
+    int dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char *ptr, uint32_t size);
+
+    /**
+     * Initialize dynamic ringbuffer with a size of size.
+     * Initialise as a client. Do not change counters.
+     * Memory will be allocated starting with min_size.
+     * If more memory is needed size is increased wit step_size.
+     * The maximum size is max_size.
+     * @param buf Pointer to ringbuffer structure
+     * @param min_size Minimum size of buffer in bytes
+     * @param max_size Maximum size of buffer in bytes
+     * @param step_size size of which ringbuffer is increased
+     * @return negative value if there was an error
+     */
+    int dlt_buffer_init_dynamic(DltBuffer *buf, uint32_t min_size, uint32_t max_size,uint32_t step_size);
+
+    /**
+     * Deinitilaise usage of static ringbuffer
+     * @param buf Pointer to ringbuffer structure
+     * @return negative value if there was an error
+     */
+    int dlt_buffer_free_static(DltBuffer *buf);
+
+    /**
+     * Release and free memory used by dynamic ringbuffer
+     * @param buf Pointer to ringbuffer structure
+     * @return negative value if there was an error
+     */
+    int dlt_buffer_free_dynamic(DltBuffer *buf);
 
     /**
      * Write one entry to ringbuffer
-     * @param dltbuf Pointer to ringbuffer structure
+     * @param buf Pointer to ringbuffer structure
      * @param data Pointer to data to be written to ringbuffer
      * @param size Size of data in bytes to be written to ringbuffer
      * @return negative value if there was an error
      */
-    int dlt_ringbuffer_put(DltRingBuffer *dltbuf, void *data, uint32_t size);
+       int dlt_buffer_push(DltBuffer *buf,const unsigned char *data,unsigned int size);
 
     /**
-     * Write one entry given as 3 chunks to ringbuffer
-     * @param dltbuf Pointer to ringbuffer structure
-     * @param data1 Pointer to data1 to be written to ringbuffer
-     * @param size1 Size of data1 in bytes to be written to ringbuffer
-     * @param data2 Pointer to data2 to be written to ringbuffer
-     * @param size2 Size of data2 in bytes to be written to ringbuffer
-     * @param data3 Pointer to data3 to be written to ringbuffer
-     * @param size3 Size of data3 in bytes to be written to ringbuffer
+     * Write up to three entries to ringbuffer.
+     * Entries are joined to one block.
+     * @param dlt Pointer to ringbuffer structure
+     * @param data1 Pointer to data to be written to ringbuffer
+     * @param size1 Size of data in bytes to be written to ringbuffer
+     * @param data2 Pointer to data to be written to ringbuffer
+     * @param size2 Size of data in bytes to be written to ringbuffer
+     * @param data3 Pointer to data to be written to ringbuffer
+     * @param size3 Size of data in bytes to be written to ringbuffer
      * @return negative value if there was an error
      */
-    int dlt_ringbuffer_put3(DltRingBuffer *dltbuf, void *data1, uint32_t size1, void *data2, uint32_t size2, void *data3, uint32_t size3);
+       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);
 
-        /**
-     * This method writes the size of message
-     * @param dltbuf Pointer to ringbuffer structure
-     * @param data_size Pointer to size of data to be written
-     * @param unit_size Size of uint32_t   
-     */
-       void dlt_ringbuffer_putMessageSize(DltRingBuffer *dltbuf,uint32_t * data_size, uint32_t unit_size);
-       
-        /**
-     * This method reads the size of a message 
-     * @param dltbuf Pointer to ringbuffer structure
-     * @param unit_size Size of uint32_t 
-     * @return size of the message  
+    /**
+     * Read one entry from ringbuffer.
+     * Remove it from ringbuffer.
+     * @param buf Pointer to ringbuffer structure
+     * @param data Pointer to data read from ringbuffer
+     * @param max_size Max size of read data in bytes from ringbuffer
+     * @return size of read data, zero if no data available, negative value if there was an error
      */
-       int dlt_ringbuffer_getMessageSize(DltRingBuffer *dltbuf, uint32_t unit_size);
+       int dlt_buffer_pull(DltBuffer *buf,unsigned char *data, int max_size);
+
     /**
-     * Read one entry from ringbuffer
-     * @param dltbuf Pointer to ringbuffer structure
+     * Read one entry from ringbuffer.
+     * Do not remove it from ringbuffer.
+     * @param buf Pointer to ringbuffer structure
      * @param data Pointer to data read from ringbuffer
-     * @param size Size of read data in bytes from ringbuffer
-     * @return negative value if there was an error
+     * @param max_size Max size of read data in bytes from ringbuffer
+     * @return size of read data, zero if no data available, negative value if there was an error
      */
-    int dlt_ringbuffer_get(DltRingBuffer *dltbuf, void *data, size_t *size);
+       int dlt_buffer_copy(DltBuffer *buf,unsigned char *data, int max_size);
 
     /**
-     * Helper function: Skip one readable entry in ringbuffer
-     * @param dltbuf Pointer to ringbuffer structure
-     * @return negative value if there was an error
+     * Remove entry from ringbuffer.
+     * @param buf Pointer to ringbuffer structure
+     * @return size of read data, zero if no data available, negative value if there was an error
      */
-    int dlt_ringbuffer_get_skip(DltRingBuffer *dltbuf);
+       int dlt_buffer_remove(DltBuffer *buf);
 
     /**
-     * Helper function: Get free space in bytes for writting between write and read position
-     * @param dltbuf Pointer to ringbuffer structure
-     * @param freespace Free Space in bytes for writting is returned
-     * @return negative value if there was an error
+     * Print information about buffer and log to internal DLT log.
+     * @param buf Pointer to ringbuffer structure
      */
-    int dlt_ringbuffer_freespacewrite(DltRingBuffer *dltbuf, uint32_t *freespace);
+       void dlt_buffer_info(DltBuffer *buf);
 
     /**
-     * Helper function: Check free space and if necessary discard entries, so that at least
-     * reqspace bytes are available for writting
-     * @param dltbuf Pointer to ringbuffer structure
-     * @param reqspace Requested space for writting in bytes
-     * @return negative value if there was an error
+     * Print status of buffer and log to internal DLT log.
+     * @param buf Pointer to ringbuffer structure
+     */
+       void dlt_buffer_status(DltBuffer *buf);
+
+    /**
+     * Get total size in bytes of ringbuffer.
+     * If buffer is dynamic, max size is returned.
+     * @param buf Pointer to ringbuffer structure
+     * @return total size of buffer
+     */
+       int dlt_buffer_get_total_size(DltBuffer *buf);
+
+    /**
+     * Get used size in bytes of ringbuffer.
+     * @param buf Pointer to ringbuffer structure
+     * @return used size of buffer
      */
-    int dlt_ringbuffer_checkandfreespace(DltRingBuffer *dltbuf, uint32_t reqspace);
+       int dlt_buffer_get_used_size(DltBuffer *buf);
+
+    /**
+     * Get number of entries in ringbuffer.
+     * @param buf Pointer to ringbuffer structure
+     * @return number of entries
+     */
+       int dlt_buffer_get_message_count(DltBuffer *buf);
 
 #if !defined (__WIN32__)
 
@@ -1041,8 +1159,23 @@ extern "C"
     /**
      * Print dlt version and dlt svn version to buffer
      * @param buf Pointer to buffer
+     * @param size size of buffer
      */
-    void dlt_get_version(char *buf);
+    void dlt_get_version(char *buf, size_t size);
+
+    /**
+    * Print dlt major version to buffer
+    * @param buf Pointer to buffer
+    * @param size size of buffer
+    */
+    void dlt_get_major_version(char *buf, size_t size);
+
+    /**
+    * Print dlt minor version to buffer
+    * @param buf Pointer to buffer
+    * @param size size of buffer
+    */
+    void dlt_get_minor_version(char *buf, size_t size);
 
 #endif
 
@@ -1125,6 +1258,11 @@ extern "C"
      */
     int dlt_message_argument_print(DltMessage *msg,uint32_t type_info,uint8_t **ptr,int32_t *datalength,char *text,int textlength,int byteLength,int verbose);
 
+    /**
+     * Check environment variables.
+     */
+       void dlt_check_envvar();
+
 #ifdef __cplusplus
 }
 #endif