summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordbiastoch <dbiastoch@de.adit-jv.com>2021-01-13 11:27:17 +0100
committerSaya Sugiura <39760799+ssugiura@users.noreply.github.com>2021-06-30 10:54:59 +0900
commit442688504260bb2bfcfb08a9865f223d2ab2d19a (patch)
treedeb6e007dfd727d0ec91430cac8ec9cd27e03b7e /src
parentf5a22d09b620be0c911815145f0e6486f061aecd (diff)
downloadDLT-daemon-442688504260bb2bfcfb08a9865f223d2ab2d19a.tar.gz
dlt-receive: Enabled more filtering by using json filter files
-Added '-j' flag to dlt-receive for reading a json filter file -Added more attributes to DltFilter (LogLevel, MaxPayload, MinPayload) for extended message filtering, when using old filters these values are set to default. Same behaviour, when they are not defined in json filter file. -extended dlt_common.c to support json filter files and new DltFilter attributes +add dlt_json_filter_load to parse a json filter file into a DltFilter +add dlt_json_filter_save to print a DltFilter into a json file -Two new libraries are used to parse the json filter files: json-c for Linux based systems and the QNX internal libjson for QNX systems -gtest_dlt_common was modified to test the new function 'dlt_json_filter_load' +add testfile_extended.dlt that contains a bigger varity of messages (different context/app IDs, lengths and log levels) +add testfilter.json which is a valid json filter file -New dependency on library was added to related CMakeLists and is described in README.md Signed-off-by: dbiastoch <dbiastoch@de.adit-jv.com>
Diffstat (limited to 'src')
-rw-r--r--src/console/dlt-receive.c73
-rw-r--r--src/daemon/CMakeLists.txt5
-rw-r--r--src/lib/CMakeLists.txt2
-rw-r--r--src/shared/dlt_common.c620
4 files changed, 523 insertions, 177 deletions
diff --git a/src/console/dlt-receive.c b/src/console/dlt-receive.c
index a302dd7..62cc18f 100644
--- a/src/console/dlt-receive.c
+++ b/src/console/dlt-receive.c
@@ -104,6 +104,7 @@ void signal_handler(int signal)
/* This case should never happen! */
break;
} /* switch */
+
}
/* Function prototypes */
@@ -119,7 +120,8 @@ typedef struct {
int uflag;
char *ovalue;
char *ovaluebase; /* ovalue without ".dlt" */
- char *fvalue;
+ char *fvalue; /* filename for space separated filter file (<AppID> <ContextID>) */
+ char *jvalue; /* filename for json filter file */
char *evalue;
int bvalue;
int64_t climit;
@@ -160,7 +162,8 @@ void usage()
printf(" -c limit Restrict file size to <limit> bytes when output to file\n");
printf(" When limit is reached, a new file is opened. Use K,M,G as\n");
printf(" suffix to specify kilo-, mega-, giga-bytes respectively\n");
- printf(" -f filename Enable filtering of messages\n");
+ printf(" -f filename Enable filtering of messages with space separated list (<AppID> <ContextID>)\n");
+ printf(" -j filename Enable filtering of messages with filter defined in json file\n");
printf(" -p port Use the given port instead the default port\n");
printf(" Cannot be used with serial devices\n");
}
@@ -180,17 +183,12 @@ int64_t convert_arg_to_byte_size(char *arg)
/* last character */
factor = 1;
- if ((arg[strlen(arg) - 1] == 'K') || (arg[strlen(arg) - 1] == 'k')) {
+ if ((arg[strlen(arg) - 1] == 'K') || (arg[strlen(arg) - 1] == 'k'))
factor = 1024;
- }
else if ((arg[strlen(arg) - 1] == 'M') || (arg[strlen(arg) - 1] == 'm'))
- {
factor = 1024 * 1024;
- }
else if ((arg[strlen(arg) - 1] == 'G') || (arg[strlen(arg) - 1] == 'g'))
- {
factor = 1024 * 1024 * 1024;
- }
else if (!isdigit(arg[strlen(arg) - 1]))
return -2;
@@ -232,12 +230,11 @@ int dlt_receive_open_output_file(DltReceiveData *dltdata)
if (glob(dltdata->ovalue,
#ifndef __ANDROID_API__
- GLOB_TILDE |
+ GLOB_TILDE |
#endif
- GLOB_NOSORT, NULL, &outer) == 0) {
- if (dltdata->vflag) {
+ GLOB_NOSORT, NULL, &outer) == 0) {
+ if (dltdata->vflag)
dlt_vlog(LOG_INFO, "File %s already exists, need to rename first\n", dltdata->ovalue);
- }
if (dltdata->part_num < 0) {
char pattern[PATH_MAX + 1];
@@ -254,9 +251,9 @@ int dlt_receive_open_output_file(DltReceiveData *dltdata)
*/
if (glob(pattern,
#ifndef __ANDROID_API__
- GLOB_TILDE |
+ GLOB_TILDE |
#endif
- GLOB_NOSORT, NULL, &inner) == 0) {
+ GLOB_NOSORT, NULL, &inner) == 0) {
/* search for the highest number used */
size_t i;
@@ -282,14 +279,12 @@ int dlt_receive_open_output_file(DltReceiveData *dltdata)
snprintf(filename, PATH_MAX, "%s.%i.dlt", dltdata->ovaluebase, dltdata->part_num++);
- if (rename(dltdata->ovalue, filename) != 0) {
+ if (rename(dltdata->ovalue, filename) != 0)
dlt_vlog(LOG_ERR, "ERROR: rename %s to %s failed with error %s\n",
dltdata->ovalue, filename, strerror(errno));
- }
- else if (dltdata->vflag) {
+ else if (dltdata->vflag)
dlt_vlog(LOG_INFO, "Renaming existing file from %s to %s\n",
dltdata->ovalue, filename);
- }
} /* if (file_already_exists) */
globfree(&outer);
@@ -328,6 +323,7 @@ int main(int argc, char *argv[])
dltdata.ovalue = 0;
dltdata.ovaluebase = 0;
dltdata.fvalue = 0;
+ dltdata.jvalue = 0;
dltdata.evalue = 0;
dltdata.bvalue = 0;
dltdata.climit = -1; /* default: -1 = unlimited */
@@ -351,7 +347,7 @@ int main(int argc, char *argv[])
/* Fetch command line arguments */
opterr = 0;
- while ((c = getopt (argc, argv, "vashyuxmf:o:e:b:c:p:")) != -1)
+ while ((c = getopt (argc, argv, "vashyuxmf:j:o:e:b:c:p:")) != -1)
switch (c) {
case 'v':
{
@@ -398,6 +394,17 @@ int main(int argc, char *argv[])
dltdata.fvalue = optarg;
break;
}
+ case 'j':
+ {
+ #ifdef EXTENDED_FILTERING
+ dltdata.jvalue = optarg;
+ break;
+ #else
+ fprintf (stderr,
+ "Extended filtering is not supported. Please build with the corresponding cmake option to use it.\n");
+ return -1;
+ #endif
+ }
case 'o':
{
dltdata.ovalue = optarg;
@@ -488,8 +495,6 @@ int main(int argc, char *argv[])
return -1;
}
-
-
if (dltclient.servIP == 0) {
/* no hostname selected, show usage and terminate */
fprintf(stderr, "ERROR: No hostname selected\n");
@@ -505,8 +510,6 @@ int main(int argc, char *argv[])
return -1;
}
-
-
if (dltclient.serialDevice == 0) {
/* no serial device name selected, show usage and terminate */
fprintf(stderr, "ERROR: No serial device name specified\n");
@@ -532,6 +535,19 @@ int main(int argc, char *argv[])
dlt_file_set_filter(&(dltdata.file), &(dltdata.filter), dltdata.vflag);
}
+ #ifdef EXTENDED_FILTERING
+
+ if (dltdata.jvalue) {
+ if (dlt_json_filter_load(&(dltdata.filter), dltdata.jvalue, dltdata.vflag) < DLT_RETURN_OK) {
+ dlt_file_free(&(dltdata.file), dltdata.vflag);
+ return -1;
+ }
+
+ dlt_file_set_filter(&(dltdata.file), &(dltdata.filter), dltdata.vflag);
+ }
+
+ #endif
+
/* open DLT output file */
if (dltdata.ovalue) {
if (dltdata.climit > -1) {
@@ -597,9 +613,8 @@ int dlt_receive_message_callback(DltMessage *message, void *data)
else
dlt_set_storageheader(message->storageheader, dltdata->ecuid);
- if ((dltdata->fvalue == 0) ||
- (dltdata->fvalue &&
- (dlt_message_filter_check(message, &(dltdata->filter), dltdata->vflag) == DLT_RETURN_TRUE))) {
+ if (((dltdata->fvalue || dltdata->jvalue) == 0) ||
+ (dlt_message_filter_check(message, &(dltdata->filter), dltdata->vflag) == DLT_RETURN_TRUE)) {
/* if no filter set or filter is matching display message */
if (dltdata->xflag) {
dlt_message_print_hex(message, text, DLT_RECEIVE_BUFSIZE, dltdata->vflag);
@@ -630,9 +645,9 @@ int dlt_receive_message_callback(DltMessage *message, void *data)
/* if file output enabled write message */
if (dltdata->ovalue) {
iov[0].iov_base = message->headerbuffer;
- iov[0].iov_len = (uint32_t) message->headersize;
+ iov[0].iov_len = (uint32_t)message->headersize;
iov[1].iov_base = message->databuffer;
- iov[1].iov_len = (uint32_t) message->datasize;
+ iov[1].iov_len = (uint32_t)message->datasize;
if (dltdata->climit > -1) {
uint32_t bytes_to_write = message->headersize + message->datasize;
@@ -650,7 +665,7 @@ int dlt_receive_message_callback(DltMessage *message, void *data)
}
}
- bytes_written = (int) writev(dltdata->ohandle, iov, 2);
+ bytes_written = (int)writev(dltdata->ohandle, iov, 2);
dltdata->totalbytes += bytes_written;
diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt
index adddee3..7d3fd8e 100644
--- a/src/daemon/CMakeLists.txt
+++ b/src/daemon/CMakeLists.txt
@@ -39,6 +39,7 @@ set(dlt_daemon_SRCS
${PROJECT_SOURCE_DIR}/src/offlinelogstorage/dlt_offline_logstorage_behavior.c
)
+
if(WITH_DLT_SHM_ENABLE)
set(dlt_daemon_SRCS
${dlt_daemon_SRCS}
@@ -60,7 +61,7 @@ if(WITH_UDP_CONNECTION)
endif(WITH_UDP_CONNECTION)
add_executable(dlt-daemon ${dlt_daemon_SRCS} ${systemd_SRCS})
-target_link_libraries(dlt-daemon ${RT_LIBRARY} ${SOCKET_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
+target_link_libraries(dlt-daemon ${RT_LIBRARY} ${SOCKET_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${DLT_JSON_LIBRARY})
install(TARGETS dlt-daemon
RUNTIME DESTINATION bin
@@ -78,7 +79,7 @@ if (WITH_DLT_UNIT_TESTS)
endif(WITH_SYSTEMD_WATCHDOG OR WITH_SYSTEMD)
add_library(dlt_daemon ${library_SRCS})
- target_link_libraries(dlt_daemon ${RT_LIBRARY} ${SOCKET_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
+ target_link_libraries(dlt_daemon ${RT_LIBRARY} ${SOCKET_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${DLT_JSON_LIBRARY})
install(TARGETS dlt_daemon
RUNTIME DESTINATION bin
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index 5a94bef..a070730 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -44,7 +44,7 @@ else()
message(STATUS "pthread_setname_np API not available on this platform")
endif()
-target_link_libraries(dlt ${RT_LIBRARY} ${SOCKET_LIBRARY} Threads::Threads)
+target_link_libraries(dlt ${RT_LIBRARY} ${SOCKET_LIBRARY} ${JSON_LIBRARIES} Threads::Threads)
target_include_directories(dlt
PUBLIC
diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c
index c8fdfc3..f07e906 100644
--- a/src/shared/dlt_common.c
+++ b/src/shared/dlt_common.c
@@ -35,6 +35,15 @@
#include <stdarg.h>
#include <err.h>
+#ifdef EXTENDED_FILTERING
+ # if defined(__linux__) || defined(__ANDROID_API__)
+ # include <json-c/json.h> /* for json filter parsing on Linux and Android */
+ # endif
+ # ifdef __QNX__
+ # include <sys/json.h> /* for json filter parsing on QNX */
+ # endif
+#endif
+
#include <errno.h>
#include <sys/stat.h> /* for mkdir() */
#include <sys/wait.h>
@@ -170,15 +179,16 @@ DltReturnValue dlt_print_mixed_string(char *text, int textlength, uint8_t *ptr,
(DLT_COMMON_HEX_LINELEN + (2 * DLT_COMMON_HEX_CHARS + (DLT_COMMON_HEX_CHARS - 1)) + DLT_COMMON_CHARLEN +
DLT_COMMON_HEX_CHARS + DLT_COMMON_CHARLEN) *
((size / DLT_COMMON_HEX_CHARS) + 1);
- /* Example: (8 chars line number + (2*16 chars + 15 spaces) + space + 16 ascii chars + CR) *
- * ((size/16) lines + extra line for the rest) */
+ /* Example: (8 chars line number + (2*16 chars + 15 spaces) + space + 16 ascii chars + CR) *
+ * ((size/16) lines + extra line for the rest) */
else
required_size =
(DLT_COMMON_HEX_LINELEN + (2 * DLT_COMMON_HEX_CHARS + (DLT_COMMON_HEX_CHARS - 1)) + DLT_COMMON_CHARLEN +
DLT_COMMON_HEX_CHARS + 4 * DLT_COMMON_CHARLEN) *
((size / DLT_COMMON_HEX_CHARS) + 1);
- /* Example: (8 chars line number + (2*16 chars + 15 spaces) + space + 16 ascii chars + 4 [HTML CR: <BR>]) *
- * ((size/16) lines + extra line for the rest) */
+
+ /* Example: (8 chars line number + (2*16 chars + 15 spaces) + space + 16 ascii chars + 4 [HTML CR: <BR>]) *
+ * ((size/16) lines + extra line for the rest) */
if (textlength < required_size) {
dlt_vlog(LOG_WARNING,
@@ -444,20 +454,206 @@ DltReturnValue dlt_filter_load(DltFilter *filter, const char *filename, int verb
else
dlt_set_id(ctid, str1);
- if (filter->counter < DLT_FILTER_MAX) {
- dlt_filter_add(filter, apid, ctid, verbose);
- }
- else {
+ if (filter->counter < DLT_FILTER_MAX)
+ dlt_filter_add(filter, apid, ctid, 0, 0, INT32_MAX, verbose);
+ else
dlt_vlog(LOG_WARNING,
"Maximum number (%d) of allowed filters reached, ignoring rest of filters!\n",
DLT_FILTER_MAX);
+ }
+
+ fclose(handle);
+
+ return DLT_RETURN_OK;
+}
+
+#ifdef EXTENDED_FILTERING /* EXTENDED_FILTERING */
+# if defined(__linux__) || defined(__ANDROID_API__)
+DltReturnValue dlt_json_filter_load(DltFilter *filter, const char *filename, int verbose)
+{
+ if ((filter == NULL) || (filename == NULL))
+ return DLT_RETURN_WRONG_PARAMETER;
+
+ FILE *handle;
+ char buffer[1024];
+ long file_size;
+ struct json_object *j_parsed_json;
+ struct json_object *j_app_id;
+ struct json_object *j_context_id;
+ struct json_object *j_log_level;
+ struct json_object *j_payload_min;
+ struct json_object *j_payload_max;
+ enum json_tokener_error jerr;
+
+ char app_id[DLT_ID_SIZE] = "";
+ char context_id[DLT_ID_SIZE] = "";
+ int32_t log_level = 0;
+ int32_t payload_max = INT32_MAX;
+ int32_t payload_min = 0;
+
+ PRINT_FUNCTION_VERBOSE(verbose);
+
+ handle = fopen(filename, "r");
+
+ if (handle == NULL) {
+ dlt_vlog(LOG_WARNING, "Filter file %s cannot be opened!\n", filename);
+ return DLT_RETURN_ERROR;
+ }
+
+ fread(buffer, sizeof(buffer), 1, handle);
+
+ j_parsed_json = json_tokener_parse_verbose(buffer, &jerr);
+
+ if (jerr != json_tokener_success) {
+ dlt_vlog(LOG_WARNING, "Faild to parse given filter %s: %s\n", filename, json_tokener_error_desc(jerr));
+ return DLT_RETURN_ERROR;
+ }
+
+ printf("The following filter(s) are applied: \n");
+ dlt_vlog(LOG_DEBUG, "The following filter(s) are applied: \n");
+ int iterator = 0;
+ json_object_object_foreach(j_parsed_json, key, val)
+ {
+ if (iterator >= DLT_FILTER_MAX) {
+ dlt_vlog(LOG_WARNING,
+ "Maximum number (%d) of allowed filters reached, ignoring rest of filters!\n",
+ DLT_FILTER_MAX);
+ break;
}
+
+ printf("%s:\n", key);
+ dlt_vlog(LOG_DEBUG, "%s:\n", key);
+
+ if (json_object_object_get_ex(val, "AppId", &j_app_id))
+ strncpy(app_id, json_object_get_string(j_app_id), DLT_ID_SIZE);
+ else
+ dlt_set_id(app_id, "");
+
+ if (json_object_object_get_ex(val, "ContextId", &j_context_id))
+ strncpy(context_id, json_object_get_string(j_context_id), DLT_ID_SIZE);
+ else
+ dlt_set_id(context_id, "");
+
+ if (json_object_object_get_ex(val, "LogLevel", &j_log_level))
+ log_level = json_object_get_int(j_log_level);
+ else
+ log_level = 0;
+
+ if (json_object_object_get_ex(val, "PayloadMin", &j_payload_min))
+ payload_min = json_object_get_int(j_payload_min);
+ else
+ payload_min = 0;
+
+ if (json_object_object_get_ex(val, "PayloadMax", &j_payload_max))
+ payload_max = json_object_get_int(j_payload_max);
+ else
+ payload_max = INT32_MAX;
+
+ dlt_filter_add(filter, app_id, context_id, log_level, payload_min, payload_max, verbose);
+
+ printf("\tAppId: %.*s\n", DLT_ID_SIZE, app_id);
+ dlt_vlog(LOG_DEBUG, "\tAppId: %.*s\n", DLT_ID_SIZE, app_id);
+ printf("\tConextId: %.*s\n", DLT_ID_SIZE, context_id);
+ dlt_vlog(LOG_DEBUG, "\tConextId: %.*s\n", DLT_ID_SIZE, context_id);
+ printf("\tLogLevel: %i\n", log_level);
+ dlt_vlog(LOG_DEBUG, "\tLogLevel: %i\n", log_level);
+ printf("\tPayloadMin: %i\n", payload_min);
+ dlt_vlog(LOG_DEBUG, "\tPayloadMin: %i\n", payload_min);
+ printf("\tPayloadMax: %i\n", payload_max);
+ dlt_vlog(LOG_DEBUG, "\tPayloadMax: %i\n", payload_max);
+
+ iterator++;
}
fclose(handle);
return DLT_RETURN_OK;
}
+# endif /* __Linux__ */
+
+# ifdef __QNX__
+DltReturnValue dlt_json_filter_load(DltFilter *filter, const char *filename, int verbose)
+{
+ if ((filter == NULL) || (filename == NULL))
+ return DLT_RETURN_WRONG_PARAMETER;
+
+ json_decoder_t *j_decoder = json_decoder_create();
+
+ const char *s_app_id;
+ const char *s_context_id;
+ int32_t log_level = 0;
+ int32_t payload_max = INT32_MAX;
+ int32_t payload_min = 0;
+
+ PRINT_FUNCTION_VERBOSE(verbose);
+
+ json_decoder_error_t ret = json_decoder_parse_file(j_decoder, filename);
+
+ if (ret != JSON_DECODER_OK) {
+ dlt_vlog(LOG_WARNING, "Faild to parse given filter %s: json_decoder_error_t is %i\n", filename, ret);
+ return DLT_RETURN_ERROR;
+ }
+
+ json_decoder_push_object(j_decoder, NULL, true);
+
+ int iterator = 0;
+ bool end_of_json = false;
+
+ while (!end_of_json) {
+ if (iterator >= DLT_FILTER_MAX) {
+ dlt_vlog(LOG_WARNING,
+ "Maximum number (%d) of allowed filters reached, ignoring rest of filters!\n",
+ DLT_FILTER_MAX);
+ break;
+ }
+
+ if (json_decoder_next(j_decoder) == JSON_DECODER_NOT_FOUND)
+ end_of_json = true;
+
+ json_decoder_previous(j_decoder);
+
+ printf("%s:\n", json_decoder_name(j_decoder));
+ json_decoder_push_object(j_decoder, NULL, true);
+
+ if (json_decoder_get_string(j_decoder, "AppId", &s_app_id, true) != JSON_DECODER_OK)
+ s_app_id = "";
+
+ if (json_decoder_get_string(j_decoder, "ContextId", &s_context_id, true) != JSON_DECODER_OK)
+ s_context_id = "";
+
+ if (json_decoder_get_int(j_decoder, "LogLevel", &log_level, true) != JSON_DECODER_OK)
+ log_level = 0;
+
+ if (json_decoder_get_int(j_decoder, "PayloadMin", &payload_min, true) != JSON_DECODER_OK)
+ payload_min = 0;
+
+ if (json_decoder_get_int(j_decoder, "PayloadMax", &payload_max, true) != JSON_DECODER_OK)
+ payload_max = INT32_MAX;
+
+ char app_id[DLT_ID_SIZE];
+ char context_id[DLT_ID_SIZE];
+ strncpy(app_id, s_app_id, DLT_ID_SIZE);
+ strncpy(context_id, s_context_id, DLT_ID_SIZE);
+
+ dlt_filter_add(filter, app_id, context_id, log_level, payload_min, payload_max, verbose);
+
+ printf("\tAppId: %.*s\n", DLT_ID_SIZE, app_id);
+ printf("\tConextId: %.*s\n", DLT_ID_SIZE, context_id);
+ printf("\tLogLevel: %i\n", log_level);
+ printf("\tPayloadMin: %i\n", payload_min);
+ printf("\tPayloadMax: %i\n", payload_max);
+
+ json_decoder_pop(j_decoder);
+
+ iterator++;
+ }
+
+ json_decoder_destroy(j_decoder);
+
+ return DLT_RETURN_OK;
+}
+# endif /* __QNX__ */
+#endif /* EXTENDED_FILTERING */
DltReturnValue dlt_filter_save(DltFilter *filter, const char *filename, int verbose)
{
@@ -500,7 +696,104 @@ DltReturnValue dlt_filter_save(DltFilter *filter, const char *filename, int verb
return DLT_RETURN_OK;
}
-int dlt_filter_find(DltFilter *filter, const char *apid, const char *ctid, int verbose)
+#ifdef EXTENDED_FILTERING /* EXTENDED_FILTERING */
+# if defined(__linux__) || defined(__ANDROID_API__)
+DltReturnValue dlt_json_filter_save(DltFilter *filter, const char *filename, int verbose)
+{
+ if ((filter == NULL) || (filename == NULL))
+ return DLT_RETURN_WRONG_PARAMETER;
+
+ PRINT_FUNCTION_VERBOSE(verbose);
+
+ struct json_object *json_filter_obj = json_object_new_object();
+
+ for (int num = 0; num < filter->counter; num++) {
+ struct json_object *tmp_json_obj = json_object_new_object();
+ char filter_name[JSON_FILTER_NAME_SIZE];
+ sprintf(filter_name, "filter%i", num);
+
+ if (filter->apid[num][DLT_ID_SIZE - 1] != 0)
+ json_object_object_add(tmp_json_obj, "AppId", json_object_new_string_len(filter->apid[num], DLT_ID_SIZE));
+ else
+ json_object_object_add(tmp_json_obj, "AppId", json_object_new_string(filter->apid[num]));
+
+ if (filter->ctid[num][DLT_ID_SIZE - 1] != 0)
+ json_object_object_add(tmp_json_obj, "ContextId",
+ json_object_new_string_len(filter->ctid[num], DLT_ID_SIZE));
+ else
+ json_object_object_add(tmp_json_obj, "ContextId", json_object_new_string(filter->ctid[num]));
+
+ json_object_object_add(tmp_json_obj, "LogLevel", json_object_new_int(filter->log_level[num]));
+ json_object_object_add(tmp_json_obj, "PayloadMin", json_object_new_int(filter->payload_min[num]));
+ json_object_object_add(tmp_json_obj, "PayloadMax", json_object_new_int(filter->payload_max[num]));
+
+ json_object_object_add(json_filter_obj, filter_name, tmp_json_obj);
+ }
+
+ printf("Saving current filter into '%s'\n", filename);
+ json_object_to_file(filename, json_filter_obj);
+
+ return DLT_RETURN_OK;
+}
+# endif /* __Linux__ */
+
+# ifdef __QNX__
+DltReturnValue dlt_json_filter_save(DltFilter *filter, const char *filename, int verbose)
+{
+ if ((filter == NULL) || (filename == NULL))
+ return DLT_RETURN_WRONG_PARAMETER;
+
+ PRINT_FUNCTION_VERBOSE(verbose);
+
+ char s_app_id[DLT_ID_SIZE + 1];
+ char s_context_id[DLT_ID_SIZE + 1];
+
+ json_encoder_t *j_encoder = json_encoder_create();
+ json_encoder_start_object(j_encoder, NULL);
+
+ for (int num = 0; num < filter->counter; num++) {
+ char filter_name[JSON_FILTER_NAME_SIZE];
+ sprintf(filter_name, "filter%i", num);
+ json_encoder_start_object(j_encoder, filter_name);
+
+ strncpy(s_app_id, filter->apid[num], DLT_ID_SIZE);
+
+ if (filter->apid[num][DLT_ID_SIZE - 1] != 0)
+ s_app_id[DLT_ID_SIZE] = '\0';
+
+ strncpy(s_context_id, filter->ctid[num], DLT_ID_SIZE);
+
+ if (filter->ctid[num][DLT_ID_SIZE - 1] != 0)
+ s_context_id[DLT_ID_SIZE] = '\0';
+
+ json_encoder_add_string(j_encoder, "AppId", s_app_id);
+ json_encoder_add_string(j_encoder, "ContextId", s_context_id);
+ json_encoder_add_int(j_encoder, "LogLevel", filter->log_level[num]);
+ json_encoder_add_int(j_encoder, "PayloadMin", filter->payload_min[num]);
+ json_encoder_add_int(j_encoder, "PayloadMax", filter->payload_max[num]);
+
+ json_encoder_end_object(j_encoder);
+ }
+
+ json_encoder_end_object(j_encoder);
+
+ printf("Saving current filter into '%s'\n", filename);
+ FILE *handle = fopen(filename, "w");
+ int filter_buffer_size = 100 * (filter->counter);
+ char filter_buffer[filter_buffer_size];
+ snprintf(filter_buffer, filter_buffer_size, json_encoder_buffer(j_encoder));
+ fprintf(handle, filter_buffer);
+
+ fclose(handle);
+ json_encoder_destroy(j_encoder);
+
+ return DLT_RETURN_OK;
+}
+# endif /* __QNX__ */
+#endif /* EXTENDED_FILTERING */
+
+int dlt_filter_find(DltFilter *filter, const char *apid, const char *ctid, const int log_level,
+ const int32_t payload_min, const int32_t payload_max, int verbose)
{
int num;
@@ -518,16 +811,25 @@ int dlt_filter_find(DltFilter *filter, const char *apid, const char *ctid, int v
char empty_ctid[DLT_ID_SIZE] = "";
if (memcmp(filter->ctid[num], empty_ctid, DLT_ID_SIZE) == 0)
- return num;
+ if ((filter->log_level[num] == log_level) || (filter->log_level[num] == 0))
+ if (filter->payload_min[num] <= payload_min)
+ if (filter->payload_max[num] >= payload_max)
+ return num;
}
else if (memcmp(filter->ctid[num], ctid, DLT_ID_SIZE) == 0)
- return num;
+ {
+ if ((filter->log_level[num] == log_level) || (filter->log_level[num] == 0))
+ if (filter->payload_min[num] <= payload_min)
+ if (filter->payload_max[num] >= payload_max)
+ return num;
+ }
}
return -1; /* Not found */
}
-DltReturnValue dlt_filter_add(DltFilter *filter, const char *apid, const char *ctid, int verbose)
+DltReturnValue dlt_filter_add(DltFilter *filter, const char *apid, const char *ctid, const int log_level,
+ const int32_t payload_min, const int32_t payload_max, int verbose)
{
PRINT_FUNCTION_VERBOSE(verbose);
@@ -541,23 +843,25 @@ DltReturnValue dlt_filter_add(DltFilter *filter, const char *apid, const char *c
return DLT_RETURN_ERROR;
}
- /* add each filter (apid, ctid) only once to filter array */
- if (dlt_filter_find(filter, apid, ctid, verbose) < 0) {
+ /* add each filter (apid, ctid, log_level, payload_min, payload_max) only once to filter array */
+ if (dlt_filter_find(filter, apid, ctid, log_level, payload_min, payload_max, verbose) < 0) {
/* filter not found, so add it to filter array */
- if (filter->counter < DLT_FILTER_MAX) {
- dlt_set_id(filter->apid[filter->counter], apid);
- dlt_set_id(filter->ctid[filter->counter], (ctid ? ctid : ""));
+ dlt_set_id(filter->apid[filter->counter], apid);
+ dlt_set_id(filter->ctid[filter->counter], (ctid ? ctid : ""));
+ filter->log_level[filter->counter] = log_level;
+ filter->payload_min[filter->counter] = payload_min;
+ filter->payload_max[filter->counter] = payload_max;
- filter->counter++;
+ filter->counter++;
- return DLT_RETURN_OK;
- }
+ return DLT_RETURN_OK;
}
return DLT_RETURN_ERROR;
}
-DltReturnValue dlt_filter_delete(DltFilter *filter, const char *apid, const char *ctid, int verbose)
+DltReturnValue dlt_filter_delete(DltFilter *filter, const char *apid, const char *ctid, const int log_level,
+ const int32_t payload_min, const int32_t payload_max, int verbose)
{
int j, k;
int found = 0;
@@ -571,24 +875,31 @@ DltReturnValue dlt_filter_delete(DltFilter *filter, const char *apid, const char
/* Get first occurence of apid and ctid in filter array */
for (j = 0; j < filter->counter; j++)
if ((memcmp(filter->apid[j], apid, DLT_ID_SIZE) == 0) &&
- (memcmp(filter->ctid[j], ctid, DLT_ID_SIZE) == 0)
+ (memcmp(filter->ctid[j], ctid, DLT_ID_SIZE) == 0) &&
+ ((filter->log_level[j] == log_level) || (filter->log_level[j] == 0)) &&
+ (filter->payload_min[j] == payload_min) &&
+ (filter->payload_max[j] == payload_max)
) {
found = 1;
break;
}
-
-
if (found) {
/* j is index */
/* Copy from j+1 til end to j til end-1 */
dlt_set_id(filter->apid[j], "");
dlt_set_id(filter->ctid[j], "");
+ filter->log_level[j] = 0;
+ filter->payload_min[j] = 0;
+ filter->payload_max[j] = INT32_MAX;
for (k = j; k < (filter->counter - 1); k++) {
dlt_set_id(filter->apid[k], filter->apid[k + 1]);
dlt_set_id(filter->ctid[k], filter->ctid[k + 1]);
+ filter->log_level[k] = filter->log_level[k + 1];
+ filter->payload_min[k] = filter->payload_min[k + 1];
+ filter->payload_max[k] = filter->payload_max[k + 1];
}
filter->counter--;
@@ -803,17 +1114,17 @@ DltReturnValue dlt_message_payload(DltMessage *msg, char *text, size_t textlengt
/* print payload only as hex */
if (type == DLT_OUTPUT_HEX)
- return dlt_print_hex_string(text, (int) textlength, msg->databuffer, (int) msg->datasize);
+ return dlt_print_hex_string(text, (int)textlength, msg->databuffer, (int)msg->datasize);
/* print payload as mixed */
if (type == DLT_OUTPUT_MIXED_FOR_PLAIN)
- return dlt_print_mixed_string(text, (int) textlength, msg->databuffer, (int) msg->datasize, 0);
+ return dlt_print_mixed_string(text, (int)textlength, msg->databuffer, (int)msg->datasize, 0);
if (type == DLT_OUTPUT_MIXED_FOR_HTML)
- return dlt_print_mixed_string(text, (int) textlength, msg->databuffer, (int) msg->datasize, 1);
+ return dlt_print_mixed_string(text, (int)textlength, msg->databuffer, (int)msg->datasize, 1);
ptr = msg->databuffer;
- datalength = (int32_t) msg->datasize;
+ datalength = (int32_t)msg->datasize;
/* Pointer to ptr and datalength */
pptr = &ptr;
@@ -827,7 +1138,7 @@ DltReturnValue dlt_message_payload(DltMessage *msg, char *text, size_t textlengt
DLT_MSG_READ_VALUE(id_tmp, ptr, datalength, uint32_t);
id = DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp);
- if (textlength < (( (unsigned int) datalength * 3) + 20)) {
+ if (textlength < (((unsigned int)datalength * 3) + 20)) {
dlt_vlog(LOG_WARNING,
"String does not fit binary data (available=%d, required=%d) !\n",
(int) textlength, (datalength * 3) + 20);
@@ -836,10 +1147,9 @@ DltReturnValue dlt_message_payload(DltMessage *msg, char *text, size_t textlengt
/* process message id / service id */
if (DLT_MSG_IS_CONTROL(msg)) {
- if ((id > 0) && (id < DLT_SERVICE_ID_LAST_ENTRY)) {
+ if ((id > 0) && (id < DLT_SERVICE_ID_LAST_ENTRY))
snprintf(text + strlen(text), textlength - strlen(text), "%s",
service_id_name[id]); /* service id */
- }
else if (!(DLT_MSG_IS_CONTROL_TIME(msg)))
snprintf(text + strlen(text), textlength - strlen(text), "service(%u)", id); /* service id */
@@ -867,8 +1177,8 @@ DltReturnValue dlt_message_payload(DltMessage *msg, char *text, size_t textlengt
if (type == DLT_OUTPUT_ASCII_LIMITED) {
ret = dlt_print_hex_string(text + strlen(text),
- (int) (textlength - strlen(
- text)),
+ (int)(textlength - strlen(
+ text)),
ptr,
(datalength >
DLT_COMMON_ASCII_LIMIT_MAX_CHARS ? DLT_COMMON_ASCII_LIMIT_MAX_CHARS : datalength));
@@ -878,7 +1188,7 @@ DltReturnValue dlt_message_payload(DltMessage *msg, char *text, size_t textlengt
snprintf(text + strlen(text), textlength - strlen(text), " ...");
}
else {
- ret = dlt_print_hex_string(text + strlen(text),(int) (textlength - strlen(text)), ptr, datalength);
+ ret = dlt_print_hex_string(text + strlen(text), (int)(textlength - strlen(text)), ptr, datalength);
}
return ret;
@@ -893,7 +1203,7 @@ DltReturnValue dlt_message_payload(DltMessage *msg, char *text, size_t textlengt
for (num = 0; num < (int)(msg->extendedheader->noar); num++) {
if (num != 0) {
text_offset = (int)strlen(text);
- snprintf(text + text_offset, textlength - (size_t) text_offset, " ");
+ snprintf(text + text_offset, textlength - (size_t)text_offset, " ");
}
/* first read the type info of the argument */
@@ -902,8 +1212,10 @@ DltReturnValue dlt_message_payload(DltMessage *msg, char *text, size_t textlengt
/* print out argument */
text_offset = (int)strlen(text);
+
if (dlt_message_argument_print(msg, type_info, pptr, pdatalength,
- (text + text_offset), (textlength - (size_t) text_offset), -1, 0) == DLT_RETURN_ERROR)
+ (text + text_offset), (textlength - (size_t)text_offset), -1,
+ 0) == DLT_RETURN_ERROR)
return DLT_RETURN_ERROR;
}
@@ -929,7 +1241,11 @@ DltReturnValue dlt_message_filter_check(DltMessage *msg, DltFilter *filter, int
/* check each filter if it matches */
if ((DLT_IS_HTYP_UEH(msg->standardheader->htyp)) &&
((filter->apid[num][0] == 0) || (memcmp(filter->apid[num], msg->extendedheader->apid, DLT_ID_SIZE) == 0)) &&
- ((filter->ctid[num][0] == 0) || (memcmp(filter->ctid[num], msg->extendedheader->ctid, DLT_ID_SIZE) == 0))) {
+ ((filter->ctid[num][0] == 0) || (memcmp(filter->ctid[num], msg->extendedheader->ctid, DLT_ID_SIZE) == 0)) &&
+ ((filter->log_level[num] == 0) ||
+ (filter->log_level[num] == DLT_GET_MSIN_MTIN(msg->extendedheader->msin))) &&
+ ((filter->payload_min[num] == 0) || (filter->payload_min[num] <= msg->datasize)) &&
+ ((filter->payload_max[num] == 0) || (filter->payload_max[num] >= msg->datasize))) {
found = DLT_RETURN_TRUE;
break;
}
@@ -958,7 +1274,7 @@ int dlt_message_read(DltMessage *msg, uint8_t *buffer, unsigned int length, int
/* serial header found */
msg->found_serialheader = 1;
buffer += sizeof(dltSerialHeader);
- length -= (unsigned int) sizeof(dltSerialHeader);
+ length -= (unsigned int)sizeof(dltSerialHeader);
}
else {
/* serial header not found */
@@ -973,18 +1289,18 @@ int dlt_message_read(DltMessage *msg, uint8_t *buffer, unsigned int length, int
/* serial header found */
msg->found_serialheader = 1;
buffer += sizeof(dltSerialHeader);
- length -= (unsigned int) sizeof(dltSerialHeader);
+ length -= (unsigned int)sizeof(dltSerialHeader);
break;
}
msg->resync_offset++;
- } while ( (sizeof(dltSerialHeader) + (size_t) msg->resync_offset) <= length);
+ } while ((sizeof(dltSerialHeader) + (size_t)msg->resync_offset) <= length);
/* Set new start offset */
if (msg->resync_offset > 0) {
/* Resyncing connection */
buffer += msg->resync_offset;
- length -= (unsigned int) msg->resync_offset;
+ length -= (unsigned int)msg->resync_offset;
}
}
}
@@ -1033,7 +1349,7 @@ int dlt_message_read(DltMessage *msg, uint8_t *buffer, unsigned int length, int
return DLT_MESSAGE_ERROR_SIZE;
memcpy(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader),
- buffer + sizeof(DltStandardHeader), (size_t) extra_size);
+ buffer + sizeof(DltStandardHeader), (size_t)extra_size);
/* set extended header ptr and get standard header extra parameters */
if (DLT_IS_HTYP_UEH(msg->standardheader->htyp))
@@ -1193,7 +1509,6 @@ DltReturnValue dlt_file_read_header(DltFile *file, int verbose)
else
dlt_log(LOG_DEBUG, "Reached end of file\n");
-
return DLT_RETURN_ERROR;
}
@@ -1291,7 +1606,9 @@ DltReturnValue dlt_file_read_header_raw(DltFile *file, int resync, int verbose)
else
/* go back to last file position */
if (0 != fseek(file->handle, file->file_position, SEEK_SET))
+ {
return DLT_RETURN_ERROR;
+ }
}
/* load header from file */
@@ -1463,10 +1780,9 @@ DltReturnValue dlt_file_open(DltFile *file, const char *filename, int verbose)
return DLT_RETURN_ERROR;
}
- if (verbose) {
+ if (verbose)
/* print file length */
dlt_vlog(LOG_DEBUG, "File is %lu bytes long\n", file->file_length);
- }
return DLT_RETURN_OK;
}
@@ -1479,9 +1795,8 @@ DltReturnValue dlt_file_read(DltFile *file, int verbose)
if (file == NULL)
return DLT_RETURN_WRONG_PARAMETER;
- if (verbose) {
+ if (verbose)
dlt_vlog(LOG_DEBUG, "%s: Message %d:\n", __func__, file->counter_total);
- }
/* allocate new memory for index if number of messages exceeds a multiple of DLT_COMMON_INDEX_ALLOC (e.g.: 1000) */
if (file->counter % DLT_COMMON_INDEX_ALLOC == 0) {
@@ -1491,7 +1806,7 @@ DltReturnValue dlt_file_read(DltFile *file, int verbose)
return DLT_RETURN_ERROR;
if (file->index) {
- memcpy(ptr, file->index, (size_t) (file->counter) * sizeof(long));
+ memcpy(ptr, file->index, (size_t)(file->counter) * sizeof(long));
free(file->index);
}
@@ -1506,9 +1821,8 @@ DltReturnValue dlt_file_read(DltFile *file, int verbose)
}
/* get file position at start of DLT message */
- if (verbose) {
+ if (verbose)
dlt_vlog(LOG_INFO, "Position in file: %ld\n", file->file_position);
- }
/* read header */
if (dlt_file_read_header(file, verbose) < DLT_RETURN_OK) {
@@ -1521,9 +1835,8 @@ DltReturnValue dlt_file_read(DltFile *file, int verbose)
/* read the extended header if filter is enabled and extended header exists */
if (dlt_file_read_header_extended(file, verbose) < DLT_RETURN_OK) {
/* go back to last position in file */
- if (0 != fseek(file->handle, file->file_position, SEEK_SET)) {
+ if (0 != fseek(file->handle, file->file_position, SEEK_SET))
dlt_vlog(LOG_WARNING, "Seek to last file pos failed!\n");
- }
return DLT_RETURN_ERROR;
}
@@ -1546,9 +1859,8 @@ DltReturnValue dlt_file_read(DltFile *file, int verbose)
"Seek failed to skip payload data of size %d!\n",
file->msg.datasize);
- if (0 != fseek(file->handle, file->file_position, SEEK_SET)) {
+ if (0 != fseek(file->handle, file->file_position, SEEK_SET))
dlt_log(LOG_WARNING, "Seek back also failed!\n");
- }
return DLT_RETURN_ERROR;
}
@@ -1566,9 +1878,8 @@ DltReturnValue dlt_file_read(DltFile *file, int verbose)
(int32_t)sizeof(DltStandardHeader) + file->msg.datasize);
/* go back to last position in file */
- if (fseek(file->handle, file->file_position, SEEK_SET)) {
+ if (fseek(file->handle, file->file_position, SEEK_SET))
dlt_log(LOG_WARNING, "Seek back also failed!\n");
- }
return DLT_RETURN_ERROR;
}
@@ -1595,9 +1906,8 @@ DltReturnValue dlt_file_read_raw(DltFile *file, int resync, int verbose)
int found = DLT_RETURN_OK;
long *ptr;
- if (verbose) {
+ if (verbose)
dlt_vlog(LOG_DEBUG, "%s: Message %d:\n", __func__, file->counter_total);
- }
if (file == NULL)
return DLT_RETURN_WRONG_PARAMETER;
@@ -1610,7 +1920,7 @@ DltReturnValue dlt_file_read_raw(DltFile *file, int resync, int verbose)
return DLT_RETURN_ERROR;
if (file->index) {
- memcpy(ptr, file->index, (size_t) (file->counter) * sizeof(long));
+ memcpy(ptr, file->index, (size_t)(file->counter) * sizeof(long));
free(file->index);
}
@@ -1622,16 +1932,14 @@ DltReturnValue dlt_file_read_raw(DltFile *file, int resync, int verbose)
return DLT_RETURN_ERROR;
/* get file position at start of DLT message */
- if (verbose) {
+ if (verbose)
dlt_vlog(LOG_DEBUG, "Position in file: %ld\n", file->file_position);
- }
/* read header */
if (dlt_file_read_header_raw(file, resync, verbose) < DLT_RETURN_OK) {
/* go back to last position in file */
- if (0 != fseek(file->handle, file->file_position, SEEK_SET)) {
+ if (0 != fseek(file->handle, file->file_position, SEEK_SET))
dlt_log(LOG_WARNING, "dlt_file_read_raw, fseek failed 1\n");
- }
return DLT_RETURN_ERROR;
}
@@ -1639,18 +1947,16 @@ DltReturnValue dlt_file_read_raw(DltFile *file, int resync, int verbose)
/* read the extended header if filter is enabled and extended header exists */
if (dlt_file_read_header_extended(file, verbose) < DLT_RETURN_OK) {
/* go back to last position in file */
- if (0 != fseek(file->handle, file->file_position, SEEK_SET)) {
+ if (0 != fseek(file->handle, file->file_position, SEEK_SET))
dlt_log(LOG_WARNING, "dlt_file_read_raw, fseek failed 2\n");
- }
return DLT_RETURN_ERROR;
}
if (dlt_file_read_data(file, verbose) < DLT_RETURN_OK) {
/* go back to last position in file */
- if (0 != fseek(file->handle, file->file_position, SEEK_SET)) {
+ if (0 != fseek(file->handle, file->file_position, SEEK_SET))
dlt_log(LOG_WARNING, "dlt_file_read_raw, fseek failed 3\n");
- }
return DLT_RETURN_ERROR;
}
@@ -1778,7 +2084,7 @@ void dlt_log_set_fifo_basedir(const char *pipe_dir)
#endif
#ifdef DLT_SHM_ENABLE
-void dlt_log_set_shm_name(const char * env_shm_name)
+void dlt_log_set_shm_name(const char *env_shm_name)
{
strncpy(dltShmName, env_shm_name, NAME_MAX);
dltShmName[NAME_MAX] = 0;
@@ -1822,18 +2128,20 @@ int dlt_user_printf(const char *format, ...)
va_start(args, format);
int ret = 0;
+
switch (logging_mode) {
- case DLT_LOG_TO_CONSOLE:
- case DLT_LOG_TO_SYSLOG:
- case DLT_LOG_TO_FILE:
- case DLT_LOG_DROPPED:
- default:
- ret = vfprintf(stdout, format, args);
- break;
- case DLT_LOG_TO_STDERR:
- ret = vfprintf(stderr, format, args);
- break;
+ case DLT_LOG_TO_CONSOLE:
+ case DLT_LOG_TO_SYSLOG:
+ case DLT_LOG_TO_FILE:
+ case DLT_LOG_DROPPED:
+ default:
+ ret = vfprintf(stdout, format, args);
+ break;
+ case DLT_LOG_TO_STDERR:
+ ret = vfprintf(stderr, format, args);
+ break;
}
+
va_end(args);
return ret;
@@ -1863,21 +2171,21 @@ DltReturnValue dlt_log(int prio, char *s)
case DLT_LOG_TO_CONSOLE:
/* log to stdout */
fprintf(stdout, sFormatString,
- (unsigned int)sTimeSpec.tv_sec,
- (unsigned int)(sTimeSpec.tv_nsec / 1000),
- getpid(),
- asSeverity[prio],
- s);
+ (unsigned int)sTimeSpec.tv_sec,
+ (unsigned int)(sTimeSpec.tv_nsec / 1000),
+ getpid(),
+ asSeverity[prio],
+ s);
fflush(stdout);
break;
case DLT_LOG_TO_STDERR:
/* log to stderr */
fprintf(stderr, sFormatString,
- (unsigned int)sTimeSpec.tv_sec,
- (unsigned int)(sTimeSpec.tv_nsec / 1000),
- getpid(),
- asSeverity[prio],
- s);
+ (unsigned int)sTimeSpec.tv_sec,
+ (unsigned int)(sTimeSpec.tv_nsec / 1000),
+ getpid(),
+ asSeverity[prio],
+ s);
break;
case DLT_LOG_TO_SYSLOG:
/* log to syslog */
@@ -1981,8 +2289,8 @@ DltReturnValue dlt_receiver_init(DltReceiver *receiver, int fd, DltReceiverType
receiver->totalBytesRcvd = 0;
receiver->buf = NULL;
receiver->backup_buf = NULL;
- receiver->buffer = (char *)calloc(1, (size_t) buffersize);
- receiver->buffersize = (uint32_t) buffersize;
+ receiver->buffer = (char *)calloc(1, (size_t)buffersize);
+ receiver->buffersize = (uint32_t)buffersize;
}
if (NULL == receiver->buffer) {
@@ -2073,7 +2381,7 @@ int dlt_receiver_receive(DltReceiver *receiver)
receiver->lastBytesRcvd = receiver->bytesRcvd;
if ((receiver->lastBytesRcvd) && (receiver->backup_buf != NULL)) {
- memcpy(receiver->buf, receiver->backup_buf, (size_t) receiver->lastBytesRcvd);
+ memcpy(receiver->buf, receiver->backup_buf, (size_t)receiver->lastBytesRcvd);
free(receiver->backup_buf);
receiver->backup_buf = NULL;
}
@@ -2141,14 +2449,14 @@ DltReturnValue dlt_receiver_move_to_begin(DltReceiver *receiver)
return DLT_RETURN_ERROR;
if ((receiver->buffer != receiver->buf) && (receiver->bytesRcvd != 0)) {
- receiver->backup_buf = calloc((size_t) (receiver->bytesRcvd + 1), sizeof(char));
+ receiver->backup_buf = calloc((size_t)(receiver->bytesRcvd + 1), sizeof(char));
if (receiver->backup_buf == NULL)
dlt_vlog(LOG_WARNING,
"Can't allocate memory for backup buf, there will be atleast"
"one corrupted message for fd[%d] \n", receiver->fd);
else
- memcpy(receiver->backup_buf, receiver->buf, (size_t) receiver->bytesRcvd);
+ memcpy(receiver->backup_buf, receiver->buf, (size_t)receiver->bytesRcvd);
}
return DLT_RETURN_OK;
@@ -2159,7 +2467,7 @@ int dlt_receiver_check_and_get(DltReceiver *receiver,
unsigned int to_get,
unsigned int flags)
{
- size_t min_size = (size_t) to_get;
+ size_t min_size = (size_t)to_get;
uint8_t *src = NULL;
if (flags & DLT_RCV_SKIP_HEADER)
@@ -2179,7 +2487,7 @@ int dlt_receiver_check_and_get(DltReceiver *receiver,
memcpy(dest, src, to_get);
if (flags & DLT_RCV_REMOVE) {
- if (dlt_receiver_remove(receiver, (int) min_size) != DLT_RETURN_OK) {
+ if (dlt_receiver_remove(receiver, (int)min_size) != DLT_RETURN_OK) {
dlt_log(LOG_WARNING, "Can't remove bytes from receiver\n");
return DLT_RETURN_ERROR;
}
@@ -2231,6 +2539,7 @@ DltReturnValue dlt_check_rcv_data_size(int received, int required)
dlt_vlog(LOG_WARNING, "%s: Received data not complete\n", __func__);
_ret = DLT_RETURN_ERROR;
}
+
return _ret;
}
@@ -2301,7 +2610,7 @@ DltReturnValue dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char
/* Init pointers */
buf->mem = (unsigned char *)(buf->shm + sizeof(DltBufferHead));
- buf->size = (uint32_t) (buf->min_size - sizeof(DltBufferHead));
+ buf->size = (uint32_t)(buf->min_size - sizeof(DltBufferHead));
dlt_vlog(LOG_DEBUG,
"%s: Buffer: Size %d, Start address %lX\n",
@@ -2365,7 +2674,7 @@ DltReturnValue dlt_buffer_init_dynamic(DltBuffer *buf, uint32_t min_size, uint32
__func__, buf->size, (unsigned long)buf->mem);
/* clear memory */
- memset(buf->mem, 0, (size_t) buf->size);
+ memset(buf->mem, 0, (size_t)buf->size);
return DLT_RETURN_OK; /* OK */
}
@@ -2436,18 +2745,19 @@ void dlt_buffer_read_block(DltBuffer *buf, int *read, unsigned char *data, unsig
{
/* catch nullpointer */
if ((buf != NULL) && (read != NULL) && (data != NULL)) {
- if (((unsigned int) (*read) + size) <= buf->size) {
+ if (((unsigned int)(*read) + size) <= buf->size) {
/* read one block */
memcpy(data, buf->mem + *read, size);
- *read +=(int) size;
+ *read += (int)size;
}
else {
- if(buf->size > (unsigned int) (*read)) {
- /* read two blocks */
- memcpy(data, buf->mem + *read, buf->size - (unsigned int) (*read));
- memcpy(data + buf->size - *read, buf->mem, size - buf->size + (unsigned int) (*read));
+ if (buf->size > (unsigned int)(*read)) {
+ /* read two blocks */
+ memcpy(data, buf->mem + *read, buf->size - (unsigned int)(*read));
+ memcpy(data + buf->size - *read, buf->mem, size - buf->size + (unsigned int)(*read));
}
- *read += (int) (size - buf->size);
+
+ *read += (int)(size - buf->size);
}
}
else {
@@ -2502,16 +2812,16 @@ int dlt_buffer_increase_size(DltBuffer *buf)
new_head = (DltBufferHead *)new_ptr;
if (head->read < head->write) {
- memcpy(new_ptr + sizeof(DltBufferHead), buf->mem + head->read, (size_t) (head->write - head->read));
+ memcpy(new_ptr + sizeof(DltBufferHead), buf->mem + head->read, (size_t)(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 - (uint32_t) (head->read));
- memcpy(new_ptr + sizeof(DltBufferHead) + buf->size - head->read, buf->mem, (size_t) head->write);
+ memcpy(new_ptr + sizeof(DltBufferHead), buf->mem + head->read, buf->size - (uint32_t)(head->read));
+ memcpy(new_ptr + sizeof(DltBufferHead) + buf->size - head->read, buf->mem, (size_t)head->write);
new_head->read = 0;
- new_head->write = (int) (buf->size) + head->write - head->read;
+ new_head->write = (int)(buf->size) + head->write - head->read;
new_head->count = head->count;
}
@@ -2562,7 +2872,7 @@ int dlt_buffer_minimize_size(DltBuffer *buf)
/* update data */
buf->shm = new_ptr;
buf->mem = new_ptr + sizeof(DltBufferHead);
- buf->size = (uint32_t) (buf->min_size - sizeof(DltBufferHead));
+ buf->size = (uint32_t)(buf->min_size - sizeof(DltBufferHead));
/* reset pointers and counters */
((int *)(buf->shm))[0] = 0; /* pointer to write memory */
@@ -2635,7 +2945,7 @@ int dlt_buffer_push3(DltBuffer *buf,
count = ((int *)(buf->shm))[2];
/* check pointers */
- if (((unsigned int) read > buf->size) || ((unsigned int) write > buf->size)) {
+ if (((unsigned int)read > buf->size) || ((unsigned int)write > buf->size)) {
dlt_vlog(LOG_ERR,
"%s: Buffer: Pointer out of range. Read: %d, Write: %d, Size: %d\n",
__func__, read, write, buf->size);
@@ -2649,7 +2959,7 @@ int dlt_buffer_push3(DltBuffer *buf,
else if (count && (write == read))
free_size = 0;
else
- free_size = (int) buf->size - write + read;
+ free_size = (int)buf->size - write + read;
/* check size */
while (free_size < (int) (sizeof(DltBufferBlockHead) + size1 + size2 + size3)) {
@@ -2677,7 +2987,7 @@ int dlt_buffer_push3(DltBuffer *buf,
strncpy(head.head, DLT_BUFFER_HEAD, 4);
head.head[3] = 0;
head.status = 2;
- head.size = (int) (size1 + size2 + size3);
+ head.size = (int)(size1 + size2 + size3);
/* write data */
dlt_buffer_write_block(buf, &write, (unsigned char *)&head, sizeof(DltBufferBlockHead));
@@ -2722,7 +3032,7 @@ int dlt_buffer_get(DltBuffer *buf, unsigned char *data, int max_size, int delete
count = ((int *)(buf->shm))[2];
/* check pointers */
- if (((unsigned int) read > buf->size) || ((unsigned int) write > buf->size) || (count < 0)) {
+ if (((unsigned int)read > buf->size) || ((unsigned int)write > buf->size) || (count < 0)) {
dlt_vlog(LOG_ERR,
"%s: Buffer: Pointer out of range. Read: %d, Write: %d, Count: %d, Size: %d\n",
__func__, read, write, count, buf->size);
@@ -2746,7 +3056,7 @@ int dlt_buffer_get(DltBuffer *buf, unsigned char *data, int max_size, int delete
if (write > read)
used_size = write - read;
else
- used_size = (int) buf->size - read + write;
+ used_size = (int)buf->size - read + write;
/* first check size */
if (used_size < (int)(sizeof(DltBufferBlockHead))) {
@@ -2774,7 +3084,7 @@ int dlt_buffer_get(DltBuffer *buf, unsigned char *data, int max_size, int delete
}
/* second check size */
- if (used_size < ((int) sizeof(DltBufferBlockHead) + head.size)) {
+ if (used_size < ((int)sizeof(DltBufferBlockHead) + head.size)) {
dlt_vlog(LOG_ERR,
"%s: Buffer: Used size is smaller than buffer block header size And read header size. Used size: %d\n",
__func__, used_size);
@@ -2787,22 +3097,24 @@ int dlt_buffer_get(DltBuffer *buf, unsigned char *data, int max_size, int delete
dlt_vlog(LOG_WARNING,
"%s: Buffer: Max size is smaller than read header size. Max size: %d\n",
__func__, max_size);
- /* nothing to do but data does not fit provided buffer */
+ /* nothing to do but data does not fit provided buffer */
if ((data != NULL) && max_size) {
/* read data */
- dlt_buffer_read_block(buf, &read, data, (unsigned int) head.size);
+ dlt_buffer_read_block(buf, &read, data, (unsigned int)head.size);
if (delete)
/* update buffer pointers */
((int *)(buf->shm))[1] = read; /* set new read pointer */
+
}
- else if (delete) {
- if ((unsigned int) (read + head.size) <= buf->size)
+ else if (delete)
+ {
+ if ((unsigned int)(read + head.size) <= buf->size)
((int *)(buf->shm))[1] = read + head.size; /* set new read pointer */
else
- ((int *)(buf->shm))[1] = read + head.size - (int) buf->size; /* set new read pointer */
+ ((int *)(buf->shm))[1] = read + head.size - (int)buf->size; /* set new read pointer */
}
@@ -2899,7 +3211,7 @@ int dlt_buffer_get_used_size(DltBuffer *buf)
if (write > read)
return write - read;
- return (int) buf->size - read + write;
+ return (int)buf->size - read + write;
}
int dlt_buffer_get_message_count(DltBuffer *buf)
@@ -3214,7 +3526,7 @@ uint32_t dlt_uptime(void)
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
- return (uint32_t)ts.tv_sec * 10000 + (uint32_t)ts.tv_nsec / 100000;/* 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
return 0;
@@ -3964,14 +4276,15 @@ void dlt_check_envvar()
dlt_log_set_fifo_basedir(env_pipe_dir);
else
dlt_log_set_fifo_basedir(DLT_USER_IPC_PATH);
+
#endif
#ifdef DLT_SHM_ENABLE
- char* env_shm_name = getenv("DLT_SHM_NAME");
+ char *env_shm_name = getenv("DLT_SHM_NAME");
+
if (env_shm_name != NULL)
- {
dlt_log_set_shm_name(env_shm_name);
- }
+
#endif
}
@@ -4012,8 +4325,8 @@ int16_t dlt_getloginfo_conv_ascii_to_uint16_t(char *rp, int *rp_count)
return -1;
/* ------------------------------------------------------
- * from: [89 13 ] -> to: ['+0x'1389\0] -> to num
- * ------------------------------------------------------ */
+ * from: [89 13 ] -> to: ['+0x'1389\0] -> to num
+ * ------------------------------------------------------ */
num_work[0] = *(rp + *rp_count + 3);
num_work[1] = *(rp + *rp_count + 4);
num_work[2] = *(rp + *rp_count + 0);
@@ -4033,8 +4346,8 @@ int16_t dlt_getloginfo_conv_ascii_to_int16_t(char *rp, int *rp_count)
return -1;
/* ------------------------------------------------------
- * from: [89 ] -> to: ['0x'89\0] -> to num
- * ------------------------------------------------------ */
+ * from: [89 ] -> to: ['0x'89\0] -> to num
+ * ------------------------------------------------------ */
num_work[0] = *(rp + *rp_count + 0);
num_work[1] = *(rp + *rp_count + 1);
num_work[2] = 0;
@@ -4045,7 +4358,7 @@ int16_t dlt_getloginfo_conv_ascii_to_int16_t(char *rp, int *rp_count)
void dlt_getloginfo_conv_ascii_to_id(char *rp, int *rp_count, char *wp, int len)
{
- char number16[3] = {0};
+ char number16[3] = { 0 };
char *endptr;
int count;
@@ -4053,8 +4366,8 @@ void dlt_getloginfo_conv_ascii_to_id(char *rp, int *rp_count, char *wp, int len)
return;
/* ------------------------------------------------------
- * from: [72 65 6d 6f ] -> to: [0x72,0x65,0x6d,0x6f,0x00]
- * ------------------------------------------------------ */
+ * from: [72 65 6d 6f ] -> to: [0x72,0x65,0x6d,0x6f,0x00]
+ * ------------------------------------------------------ */
for (count = 0; count < len; count++) {
number16[0] = *(rp + *rp_count + 0);
number16[1] = *(rp + *rp_count + 1);
@@ -4117,56 +4430,62 @@ void dlt_hex_ascii_to_binary(const char *ptr, uint8_t *binary, int *size)
}
DltReturnValue dlt_file_quick_parsing(DltFile *file, const char *filename,
- int type, int verbose)
+ int type, int verbose)
{
PRINT_FUNCTION_VERBOSE(verbose);
int ret = DLT_RETURN_OK;
char text[DLT_CONVERT_TEXTBUFSIZE] = { 0 };
- if (file == NULL || filename == NULL)
+ if ((file == NULL) || (filename == NULL))
return DLT_RETURN_WRONG_PARAMETER;
FILE *output = fopen(filename, "w+");
+
if (output == NULL) {
dlt_vlog(LOG_ERR, "Cannot open output file %s for parsing\n", filename);
return DLT_RETURN_ERROR;
}
- while(ret >= DLT_RETURN_OK && file->file_position < file->file_length) {
+ while (ret >= DLT_RETURN_OK && file->file_position < file->file_length) {
/* get file position at start of DLT message */
- if (verbose) {
+ if (verbose)
dlt_vlog(LOG_DEBUG, "Position in file: %ld\n", file->file_position);
- }
/* read all header and payload */
ret = dlt_file_read_header(file, verbose);
+
if (ret < DLT_RETURN_OK)
break;
ret = dlt_file_read_header_extended(file, verbose);
+
if (ret < DLT_RETURN_OK)
break;
ret = dlt_file_read_data(file, verbose);
+
if (ret < DLT_RETURN_OK)
break;
if (file->filter) {
/* check the filters if message is used */
ret = dlt_message_filter_check(&(file->msg), file->filter, verbose);
+
if (ret != DLT_RETURN_TRUE)
continue;
}
ret = dlt_message_header(&(file->msg), text,
- DLT_CONVERT_TEXTBUFSIZE, verbose);
+ DLT_CONVERT_TEXTBUFSIZE, verbose);
+
if (ret < DLT_RETURN_OK)
break;
fprintf(output, "%s", text);
ret = dlt_message_payload(&(file->msg), text,
- DLT_CONVERT_TEXTBUFSIZE, type, verbose);
+ DLT_CONVERT_TEXTBUFSIZE, type, verbose);
+
if (ret < DLT_RETURN_OK)
break;
@@ -4179,17 +4498,18 @@ DltReturnValue dlt_file_quick_parsing(DltFile *file, const char *filename,
file->counter_total++;
/* store position to next message */
file->file_position = ftell(file->handle);
- } // while()
+ } /* while() */
fclose(output);
return ret;
}
-int dlt_execute_command(char *filename, char *command, ...){
+int dlt_execute_command(char *filename, char *command, ...)
+{
va_list val;
int argc;
- char ** args = NULL;
+ char **args = NULL;
int ret = 0;
if (command == NULL)
@@ -4197,7 +4517,9 @@ int dlt_execute_command(char *filename, char *command, ...){
/* Determine number of variadic arguments */
va_start(val, command);
- for (argc = 2; va_arg(val, char *) != NULL; argc++);
+
+ for (argc = 2; va_arg(val, char *) != NULL; argc++);
+
va_end(val);
/* Allocate args, put references to command */
@@ -4205,17 +4527,21 @@ int dlt_execute_command(char *filename, char *command, ...){
args[0] = command;
va_start(val, command);
+
for (int i = 0; args[i] != NULL; i++)
- args[i+1] = va_arg(val, char *);
+ args[i + 1] = va_arg(val, char *);
+
va_end(val);
/* Run command in child process */
pid_t pid = fork();
+
if (pid == 0) { /* child process */
/* Redirect output if required */
if (filename != NULL) {
int fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
if (fd < 0)
err(-1, "%s failed on open()", __func__);
@@ -4223,6 +4549,7 @@ int dlt_execute_command(char *filename, char *command, ...){
close(fd);
err(-1, "%s failed on dup2()", __func__);
}
+
close(fd);
}
@@ -4230,9 +4557,12 @@ int dlt_execute_command(char *filename, char *command, ...){
execvp(command, (char **)args);
}
else if (pid == -1) /* error in fork */
+ {
ret = -1;
- else /* parent */
+ }
+ else { /* parent */
wait(&ret);
+ }
free(args);
return ret;