3 * Copyright (C) 2012 BMW AG
5 * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
7 * Contributions are licensed to the GENIVI Alliance under one or more
8 * Contribution License Agreements.
11 * This Source Code Form is subject to the terms of the
12 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
13 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
16 * \author Alexander Wenzel <alexander.aw.wenzel@bmw.de> BMW 2011-2012
19 * For further information see http://www.genivi.org/.
24 /*******************************************************************************
26 ** SRC-MODULE: dlt_user.h **
32 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
39 ** PLATFORM DEPENDANT [yes/no]: yes **
41 ** TO BE CHANGED BY USER [yes/no]: no **
43 *******************************************************************************/
45 /*******************************************************************************
47 ********************************************************************************
49 ** Initials Name Company **
50 ** -------- ------------------------- ---------------------------------- **
51 ** aw Alexander Wenzel BMW **
52 ** mk Markus Klein Fraunhofer ESK **
53 *******************************************************************************/
55 /*******************************************************************************
56 ** Revision Control History **
57 *******************************************************************************/
60 * $LastChangedRevision: 1670 $
61 * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
71 \defgroup userapi DLT User API
75 #include "dlt_types.h"
76 #include "dlt_common.h"
77 #include "dlt_user_macros.h"
81 #if !defined (__WIN32__)
82 #include <semaphore.h>
90 * Definitions of DLT log level
94 DLT_LOG_DEFAULT = -1, /**< Default log level */
95 DLT_LOG_OFF = 0x00, /**< Log level off */
96 DLT_LOG_FATAL = 0x01, /**< fatal system error */
97 DLT_LOG_ERROR = 0x02, /**< error with impact to correct functionality */
98 DLT_LOG_WARN = 0x03, /**< warning, correct behaviour could not be ensured */
99 DLT_LOG_INFO = 0x04, /**< informational */
100 DLT_LOG_DEBUG = 0x05, /**< debug */
101 DLT_LOG_VERBOSE = 0x06 /**< highest grade of information */
105 * Definitions of DLT trace status
109 DLT_TRACE_STATUS_DEFAULT = -1, /**< Default trace status */
110 DLT_TRACE_STATUS_OFF = 0x00, /**< Trace status: Off */
111 DLT_TRACE_STATUS_ON = 0x01 /**< Trace status: On */
112 } DltTraceStatusType;
115 * Definitions for dlt_user_trace_network/DLT_TRACE_NETWORK()
116 * as defined in the DLT protocol
120 DLT_NW_TRACE_IPC = 0x01, /**< Interprocess communication */
121 DLT_NW_TRACE_CAN = 0x02, /**< Controller Area Network Bus */
122 DLT_NW_TRACE_FLEXRAY = 0x03, /**< Flexray Bus */
123 DLT_NW_TRACE_MOST = 0x04, /**< Media Oriented System Transport Bus */
124 DLT_NW_TRACE_RESERVED0 = 0x05,
125 DLT_NW_TRACE_RESERVED1 = 0x06,
126 DLT_NW_TRACE_RESERVED2 = 0x07,
127 DLT_NW_TRACE_USER_DEFINED0 = 0x08,
128 DLT_NW_TRACE_USER_DEFINED1 = 0x09,
129 DLT_NW_TRACE_USER_DEFINED2 = 0x0A,
130 DLT_NW_TRACE_USER_DEFINED3 = 0x0B,
131 DLT_NW_TRACE_USER_DEFINED4 = 0x0C,
132 DLT_NW_TRACE_USER_DEFINED5 = 0x0D,
133 DLT_NW_TRACE_USER_DEFINED6 = 0x0E,
134 DLT_NW_TRACE_USER_DEFINED7 = 0x0F
135 } DltNetworkTraceType;
138 * This are the log modes.
142 DLT_USER_MODE_UNDEFINED = -1,
143 DLT_USER_MODE_OFF = 0,
144 DLT_USER_MODE_EXTERNAL ,
145 DLT_USER_MODE_INTERNAL ,
149 #define DLT_USER_BUF_MAX_SIZE 2048 /**< maximum size of each user buffer, also used for injection buffer */
151 #define DLT_USER_RESENDBUF_MAX_SIZE (DLT_USER_BUF_MAX_SIZE + 100) /**< Size of resend buffer; Max DLT message size is 2K plus some extra header space */
153 /* Use a semaphore or mutex from your OS to prevent concurrent access to the DLT buffer. */
154 #define DLT_SEM_LOCK() { sem_wait(&dlt_mutex); }
155 #define DLT_SEM_FREE() { sem_post(&dlt_mutex); }
158 * This structure is used for every context used in an application.
162 char contextID[4]; /**< context id */
163 int32_t log_level_pos; /**< offset in user-application context field */
164 uint8_t mcnt; /**< message counter */
168 * This structure is used for context data used in an application.
172 DltContext *handle; /**< pointer to DltContext */
173 unsigned char buffer[DLT_USER_BUF_MAX_SIZE]; /**< buffer for building log message*/
174 int32_t size; /**< payload size */
175 int32_t log_level; /**< log level */
176 int32_t trace_status; /**< trace status */
177 int32_t args_num; /**< number of arguments for extended header*/
178 char* context_description; /**< description of context */
184 int (*injection_callback)(uint32_t service_id, void *data, uint32_t length);
185 } DltUserInjectionCallback;
188 * This structure is used in a table managing all contexts and the corresponding log levels in an application.
192 char contextID[DLT_ID_SIZE]; /**< Context ID */
193 int8_t log_level; /**< Log level */
194 int8_t trace_status; /**< Trace status */
195 char *context_description; /**< description of context */
196 DltUserInjectionCallback *injection_table; /**< Table with pointer to injection functions and service ids */
197 uint32_t nrcallbacks;
201 * This structure is used once for one application.
205 char ecuID[DLT_ID_SIZE]; /**< ECU ID */
206 char appID[DLT_ID_SIZE]; /**< Application ID */
207 int dlt_log_handle; /**< Handle to fifo of dlt daemon */
208 int dlt_user_handle; /**< Handle to own fifo */
209 mqd_t dlt_segmented_queue_read_handle; /**< Handle message queue */
210 mqd_t dlt_segmented_queue_write_handle; /**< Handle message queue */
211 pthread_t dlt_segmented_nwt_handle; /**< thread handle of segmented sending */
213 int8_t dlt_is_file; /**< Target of logging: 1 to file, 0 to daemon */
215 dlt_ll_ts_type *dlt_ll_ts; //[MAX_DLT_LL_TS_ENTRIES]; /**< Internal management struct for all contexts */
216 uint32_t dlt_ll_ts_max_num_entries; /**< Maximum number of contexts */
218 uint32_t dlt_ll_ts_num_entries; /**< Number of used contexts */
220 int8_t overflow; /**< Overflow marker, set to 1 on overflow, 0 otherwise */
222 char *application_description; /**< description of application */
224 DltReceiver receiver; /**< Receiver for internal user-defined messages from daemon */
226 int8_t verbose_mode; /**< Verbose mode enabled: 1 enabled, 0 disabled */
228 int8_t enable_local_print; /**< Local printing of log messages: 1 enabled, 0 disabled */
229 int8_t local_print_mode; /**< Local print mode, controlled by environment variable */
231 int8_t log_state; /**< Log state of external connection: 1 client connected, 0 not connected, -1 unknown */
233 //DltRingBuffer rbuf;
234 DltBuffer startup_buffer; /**< Ring-buffer for buffering messages during startup and missing connection */
236 // Buffer used for resending, locked by DLT semaphore
237 uint8_t resend_buffer[DLT_USER_RESENDBUF_MAX_SIZE];
239 #ifdef DLT_SHM_ENABLE
242 #ifdef DLT_TEST_ENABLE
243 int corrupt_user_header;
244 int corrupt_message_size;
245 int16_t corrupt_message_size_size;
249 /**************************************************************************************************
250 * The folowing API functions define a low level function interface for DLT
251 **************************************************************************************************/
254 * Initialise the generation of a DLT log message (intended for usage in non-verbose mode)
255 * This function has to be called first, when an application wants to send a new log messages.
256 * @param handle pointer to an object containing information about one special logging context
257 * @param log pointer to an object containing information about logging context data
258 * @param loglevel this is the current log level of the log message to be sent
259 * @return negative value if there was an error
261 int dlt_user_log_write_start(DltContext *handle, DltContextData *log, DltLogLevelType loglevel);
264 * Initialise the generation of a DLT log message (intended for usage in verbose mode)
265 * This function has to be called first, when an application wants to send a new log messages.
266 * @param handle pointer to an object containing information about one special logging context
267 * @param log pointer to an object containing information about logging context data
268 * @param loglevel this is the current log level of the log message to be sent
269 * @param messageid message id of message
270 * @return negative value if there was an error
272 int dlt_user_log_write_start_id(DltContext *handle, DltContextData *log, DltLogLevelType loglevel, uint32_t messageid);
275 * Finishing the generation of a DLT log message and sending it to the DLT daemon.
276 * This function has to be called after writing all the log attributes of a log message.
277 * @param log pointer to an object containing information about logging context data
278 * @return negative value if there was an error
280 int dlt_user_log_write_finish(DltContextData *log);
283 * Write a boolean parameter into a DLT log message.
284 * dlt_user_log_write_start has to be called before adding any attributes to the log message.
285 * Finish sending log message by calling dlt_user_log_write_finish.
286 * @param log pointer to an object containing information about logging context data
287 * @param data boolean parameter written into log message (mapped to uint8)
288 * @return negative value if there was an error
290 int dlt_user_log_write_bool(DltContextData *log, uint8_t data);
293 * Write a float parameter into a DLT log message.
294 * dlt_user_log_write_start has to be called before adding any attributes to the log message.
295 * Finish sending log message by calling dlt_user_log_write_finish.
296 * @param log pointer to an object containing information about logging context data
297 * @param data float32_t parameter written into log message.
298 * @return negative value if there was an error
300 int dlt_user_log_write_float32(DltContextData *log, float32_t data);
303 * Write a double parameter into a DLT log message.
304 * dlt_user_log_write_start has to be called before adding any attributes to the log message.
305 * Finish sending log message by calling dlt_user_log_write_finish.
306 * @param log pointer to an object containing information about logging context data
307 * @param data float64_t parameter written into log message.
308 * @return negative value if there was an error
310 int dlt_user_log_write_float64(DltContextData *log, double data);
313 * Write a uint parameter into a DLT log message.
314 * dlt_user_log_write_start has to be called before adding any attributes to the log message.
315 * Finish sending log message by calling dlt_user_log_write_finish.
316 * @param log pointer to an object containing information about logging context data
317 * @param data unsigned int parameter written into log message.
318 * @return negative value if there was an error
320 int dlt_user_log_write_uint(DltContextData *log, unsigned int data);
321 int dlt_user_log_write_uint8(DltContextData *log, uint8_t data);
322 int dlt_user_log_write_uint16(DltContextData *log, uint16_t data);
323 int dlt_user_log_write_uint32(DltContextData *log, uint32_t data);
324 int dlt_user_log_write_uint64(DltContextData *log, uint64_t data);
326 * Write a int parameter into a DLT log message.
327 * dlt_user_log_write_start has to be called before adding any attributes to the log message.
328 * Finish sending log message by calling dlt_user_log_write_finish.
329 * @param log pointer to an object containing information about logging context data
330 * @param data int parameter written into log message.
331 * @return negative value if there was an error
333 int dlt_user_log_write_int(DltContextData *log, int data);
334 int dlt_user_log_write_int8(DltContextData *log, int8_t data);
335 int dlt_user_log_write_int16(DltContextData *log, int16_t data);
336 int dlt_user_log_write_int32(DltContextData *log, int32_t data);
337 int dlt_user_log_write_int64(DltContextData *log, int64_t data);
339 * Write a null terminated ASCII string into a DLT log message.
340 * dlt_user_log_write_start has to be called before adding any attributes to the log message.
341 * Finish sending log message by calling dlt_user_log_write_finish.
342 * @param log pointer to an object containing information about logging context data
343 * @param text pointer to the parameter written into log message containing null termination.
344 * @return negative value if there was an error
346 int dlt_user_log_write_string( DltContextData *log, const char *text);
349 * Write a null terminated UTF8 string into a DLT log message.
350 * dlt_user_log_write_start has to be called before adding any attributes to the log message.
351 * Finish sending log message by calling dlt_user_log_write_finish.
352 * @param log pointer to an object containing information about logging context data
353 * @param text pointer to the parameter written into log message containing null termination.
354 * @return negative value if there was an error
356 int dlt_user_log_write_utf8_string(DltContextData *log, const char *text);
359 * Write a binary memory block into a DLT log message.
360 * dlt_user_log_write_start has to be called before adding any attributes to the log message.
361 * Finish sending log message by calling dlt_user_log_write_finish.
362 * @param log pointer to an object containing information about logging context data
363 * @param data pointer to the parameter written into log message.
364 * @param length length in bytes of the parameter written into log message.
365 * @return negative value if there was an error
367 int dlt_user_log_write_raw(DltContextData *log,void *data,uint16_t length);
370 * Trace network message
371 * @param handle pointer to an object containing information about one special logging context
372 * @param nw_trace_type type of network trace (DLT_NW_TRACE_IPC, DLT_NW_TRACE_CAN, DLT_NW_TRACE_FLEXRAY, or DLT_NW_TRACE_MOST)
373 * @param header_len length of network message header
374 * @param header pointer to network message header
375 * @param payload_len length of network message payload
376 * @param payload pointer to network message payload
377 * @return negative value if there was an error
379 int dlt_user_trace_network(DltContext *handle, DltNetworkTraceType nw_trace_type, uint16_t header_len, void *header, uint16_t payload_len, void *payload);
382 * Trace network message, truncated if necessary.
383 * @param handle pointer to an object containing information about logging context
384 * @param nw_trace_type type of network trace (DLT_NW_TRACE_IPC, DLT_NW_TRACE_CAN, DLT_NW_TRACE_FLEXRAY, or DLT_NW_TRACE_MOST)
385 * @param header_len length of network message header
386 * @param header pointer to network message header
387 * @param payload_len length of network message payload
388 * @param payload pointer to network message payload
389 * @param allow_truncate Set to > 0 to allow truncating of the message if it is too large.
390 * @return negative value if there was an error
392 int dlt_user_trace_network_truncated(DltContext *handle, DltNetworkTraceType nw_trace_type, uint16_t header_len, void *header, uint16_t payload_len, void *payload, int allow_truncate);
395 * Trace network message in segmented asynchronous mode.
396 * The sending of the data is done in a separate thread.
397 * Please note that handle must exist for the lifetime of the application, because
398 * data chunks are sent asynchronously in undetermined future time.
399 * @param handle pointer to an object containing information about logging context
400 * @param nw_trace_type type of network trace (DLT_NW_TRACE_IPC, DLT_NW_TRACE_CAN, DLT_NW_TRACE_FLEXRAY, or DLT_NW_TRACE_MOST)
401 * @param header_len length of network message header
402 * @param header pointer to network message header
403 * @param payload_len length of network message payload
404 * @param payload pointer to network message payload
405 * @return 0 on success, -1 on failure
407 int dlt_user_trace_network_segmented(DltContext *handle, DltNetworkTraceType nw_trace_type, uint16_t header_len, void *header, uint16_t payload_len, void *payload);
409 /**************************************************************************************************
410 * The folowing API functions define a high level function interface for DLT
411 **************************************************************************************************/
414 * Initialise the user lib communication with daemon.
415 * This function has to be called first, before using any DLT user lib functions.
416 * @return negative value if there was an error
421 * Initialise the user lib writing only to file.
422 * This function has to be called first, before using any DLT user lib functions.
423 * @param name name of an optional log file
424 * @return negative value if there was an error
426 int dlt_init_file(const char *name);
429 * Terminate the user lib.
430 * This function has to be called when finishing using the DLT user lib.
431 * @return negative value if there was an error
436 * Check the library version of DLT library.
437 * @param user_major_version the major version to be compared
438 * @param user_minor_version the minor version to be compared
439 * @return negative value if there was an error
441 int dlt_check_library_version(const char * user_major_version, const char * user_minor_version);
444 * Register an application in the daemon.
445 * @param appid four byte long character array with the application id
446 * @param description long name of the application
447 * @return negative value if there was an error
449 int dlt_register_app(const char *appid, const char * description);
452 * Unregister an application in the daemon.
453 * This function has to be called when finishing using an application.
454 * @return negative value if there was an error
456 int dlt_unregister_app(void);
459 * Register a context in the daemon.
460 * This function has to be called before first usage of the context.
461 * @param handle pointer to an object containing information about one special logging context
462 * @param contextid four byte long character array with the context id
463 * @param description long name of the context
464 * @return negative value if there was an error
466 int dlt_register_context(DltContext *handle, const char *contextid, const char * description);
469 * Register a context in the daemon with pre-defined log level and pre-defined trace status.
470 * This function has to be called before first usage of the context.
471 * @param handle pointer to an object containing information about one special logging context
472 * @param contextid four byte long character array with the context id
473 * @param description long name of the context
474 * @param loglevel This is the log level to be pre-set for this context
475 (DLT_LOG_DEFAULT is not allowed here)
476 * @param tracestatus This is the trace status to be pre-set for this context
477 (DLT_TRACE_STATUS_DEFAULT is not allowed here)
478 * @return negative value if there was an error
480 int dlt_register_context_ll_ts(DltContext *handle, const char *contextid, const char * description, int loglevel, int tracestatus);
483 * Unregister a context in the DLT daemon.
484 * This function has to be called when finishing using a context.
485 * @param handle pointer to an object containing information about one special logging context
486 * @return negative value if there was an error
488 int dlt_unregister_context(DltContext *handle);
491 * Set the logging mode used by the daemon.
492 * The logging mode is stored persistantly by the daemon.
493 * @see DltUserLogMode
494 * @param mode the new logging mode used by the daemon: off, extern, internal, both.
495 * @return negative value if there was an error
497 int dlt_set_log_mode(DltUserLogMode mode);
500 * Get the state of the connected client to the daemon.
501 * The user application gets a message, when client is connected or disconnected.
502 * This value contains the last state.
503 * It needs some time until the application gets state from the daemon.
504 * Until then the state is "unknown state".
505 * @return -1 = unknown state, 0 = client not connected, 1 = client connected
507 int dlt_get_log_state();
510 * Register callback function called when injection message was received
511 * @param handle pointer to an object containing information about one special logging context
512 * @param service_id the service id to be waited for
513 * @param (*dlt_injection_callback) function pointer to callback function
514 * @return negative value if there was an error
516 int dlt_register_injection_callback(DltContext *handle, uint32_t service_id,
517 int (*dlt_injection_callback)(uint32_t service_id, void *data, uint32_t length));
520 * Switch to verbose mode
523 int dlt_verbose_mode(void);
526 * Check the version of dlt library with library version used of the application.
527 * @param Major version number of application - see dlt_version.h
528 * @param Minor version number of application - see dlt_version.h
529 * @return negative value if there is a mismatch
531 int dlt_user_check_library_version(const char *user_major_version,const char *user_minor_version);
534 * Switch to non-verbose mode
537 int dlt_nonverbose_mode(void);
540 * Set maximum logged log level and trace status of application
542 * @param loglevel This is the log level to be set for the whole application
543 * @param tracestatus This is the trace status to be set for the whole application
544 * @return negative value if there was an error
546 int dlt_set_application_ll_ts_limit(DltLogLevelType loglevel, DltTraceStatusType tracestatus);
549 * Enable local printing of messages
552 int dlt_enable_local_print(void);
555 * Disable local printing of messages
558 int dlt_disable_local_print(void);
561 * Write a null terminated ASCII string into a DLT log message.
562 * @param handle pointer to an object containing information about one special logging context
563 * @param loglevel this is the current log level of the log message to be sent
564 * @param text pointer to the ASCII string written into log message containing null termination.
565 * @return negative value if there was an error
567 int dlt_log_string(DltContext *handle,DltLogLevelType loglevel, const char *text);
570 * Write a null terminated ASCII string and an integer value into a DLT log message.
571 * @param handle pointer to an object containing information about one special logging context
572 * @param loglevel this is the current log level of the log message to be sent
573 * @param text pointer to the ASCII string written into log message containing null termination.
574 * @param data integer value written into the log message
575 * @return negative value if there was an error
577 int dlt_log_string_int(DltContext *handle,DltLogLevelType loglevel, const char *text, int data);
580 * Write a null terminated ASCII string and an unsigned integer value into a DLT log message.
581 * @param handle pointer to an object containing information about one special logging context
582 * @param loglevel this is the current log level of the log message to be sent
583 * @param text pointer to the ASCII string written into log message containing null termination.
584 * @param data unsigned integer value written into the log message
585 * @return negative value if there was an error
587 int dlt_log_string_uint(DltContext *handle,DltLogLevelType loglevel, const char *text, unsigned int data);
590 * Write an integer value into a DLT log message.
591 * @param handle pointer to an object containing information about one special logging context
592 * @param loglevel this is the current log level of the log message to be sent
593 * @param data integer value written into the log message
594 * @return negative value if there was an error
596 int dlt_log_int(DltContext *handle,DltLogLevelType loglevel, int data);
599 * Write an unsigned integer value into a DLT log message.
600 * @param handle pointer to an object containing information about one special logging context
601 * @param loglevel this is the current log level of the log message to be sent
602 * @param data unsigned integer value written into the log message
603 * @return negative value if there was an error
605 int dlt_log_uint(DltContext *handle,DltLogLevelType loglevel, unsigned int data);
608 * Write an unsigned integer value into a DLT log message.
609 * @param handle pointer to an object containing information about one special logging context
610 * @param loglevel this is the current log level of the log message to be sent
611 * @param data pointer to the parameter written into log message.
612 * @param length length in bytes of the parameter written into log message.
613 * @return negative value if there was an error
615 int dlt_log_raw(DltContext *handle,DltLogLevelType loglevel, void *data,uint16_t length);
619 * Forward a complete DLT message to the DLT daemon
620 * @param msgdata Message data of DLT message
621 * @param size Size of DLT message
622 * @return negative value if there was an error
624 int dlt_forward_msg(void *msgdata,size_t size);
627 * Get the total size and available size of the shared memory buffer between daemon and applications.
628 * This information is useful to control the flow control between applications and daemon.
629 * For example only 50% of the buffer should be used for file transfer.
630 * @param total_size total size of buffer in bytes
631 * @param used_size used size of buffer in bytes
632 * @return negative value if there was an error
634 int dlt_user_check_buffer(int *total_size, int *used_size);
637 * Try to resend log message in the user buffer. Stops if the dlt_uptime is bigger than
638 * dlt_uptime() + DLT_USER_ATEXIT_RESEND_BUFFER_EXIT_TIMEOUT. A pause between the resending
639 * attempts can be defined with DLT_USER_ATEXIT_RESEND_BUFFER_SLEEP
640 * @return number of messages in the user buffer
642 int dlt_user_atexit_blow_out_user_buffer(void);
645 * Try to resend log message in the user buffer.
646 * @return 0 on success, negative on failure.
648 int dlt_user_log_resend_buffer(void);
650 #ifdef DLT_TEST_ENABLE
651 void dlt_user_test_corrupt_user_header(int enable);
652 void dlt_user_test_corrupt_message_size(int enable,int16_t size);
653 #endif /* DLT_TEST_ENABLE */
663 #endif /* DLT_USER_H */