summaryrefslogtreecommitdiff
path: root/src/console
diff options
context:
space:
mode:
authorBiastoch, Darian (ADITG/ESM) <dbiastoch@de.adit-jv.com>2021-04-08 06:36:18 +0000
committerSaya Sugiura <39760799+ssugiura@users.noreply.github.com>2021-06-30 10:54:59 +0900
commit57d981f9d765286a85e682f7bee0769d7349e7aa (patch)
tree3fbfef66707cff032da293b1e7ef249131845032 /src/console
parent72dc5bb1fa7bd0823ef17269ab6e5c7dd76f90fd (diff)
downloadDLT-daemon-57d981f9d765286a85e682f7bee0769d7349e7aa.tar.gz
Alternative solutions for json-c dependency
json-c dependency was removed from libdlt ('dlt_common') and shifted into 'dlt-control-common'. By this only the command line tools have a dependency on json-c. dlt-control-common is now built as a static library, so that it can be linked against json-c. Command line tools that included only the .c file of dlt-control-common before, are now linked against this static libarary (see console/CMakeLists.txt). Signed-off-by: dbiastoch <dbiastoch@de.adit-jv.com>
Diffstat (limited to 'src/console')
-rw-r--r--src/console/CMakeLists.txt5
-rw-r--r--src/console/dlt-control-common.c300
-rw-r--r--src/console/dlt-control-common.h21
-rw-r--r--src/console/dlt-receive.c1
-rw-r--r--src/console/logstorage/CMakeLists.txt2
5 files changed, 327 insertions, 2 deletions
diff --git a/src/console/CMakeLists.txt b/src/console/CMakeLists.txt
index 986a0d4..b2b731a 100644
--- a/src/console/CMakeLists.txt
+++ b/src/console/CMakeLists.txt
@@ -14,6 +14,9 @@
#######
set(dlt_control_common_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/dlt-control-common.c)
+add_library(dlt_control_common_lib STATIC ${dlt_control_common_SRCS})
+target_link_libraries(dlt_control_common_lib dlt ${DLT_JSON_LIBRARY})
+
add_subdirectory(logstorage)
set(dlt-sortbytimestamp_SRCS dlt-sortbytimestamp.c)
@@ -30,7 +33,7 @@ foreach(target
dlt-passive-node-ctrl
)
add_executable(${target} ${${target}_SRCS})
- target_link_libraries(${target} dlt)
+ target_link_libraries(${target} dlt dlt_control_common_lib)
set_target_properties(${target} PROPERTIES LINKER_LANGUAGE C)
install(TARGETS ${target}
diff --git a/src/console/dlt-control-common.c b/src/console/dlt-control-common.c
index f4c66cd..8a9d29f 100644
--- a/src/console/dlt-control-common.c
+++ b/src/console/dlt-control-common.c
@@ -64,6 +64,15 @@
#include "dlt-control-common.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
+
#define DLT_CTRL_APID "DLTC"
#define DLT_CTRL_CTID "DLTC"
#define DLT_DAEMON_DEFAULT_CTRL_SOCK_PATH "/tmp/dlt-ctrl.sock"
@@ -640,3 +649,294 @@ int dlt_control_deinit(void)
/* Closing the socket */
return dlt_client_cleanup(&g_client, get_verbosity());
}
+
+
+#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;
+
+ if(verbose)
+ pr_verbose("dlt_json_filter_load()\n");
+
+ FILE *handle;
+ char buffer[JSON_FILTER_SIZE*DLT_FILTER_MAX];
+ 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;
+
+ handle = fopen(filename, "r");
+
+ if (handle == NULL) {
+ pr_error("Filter file %s cannot be opened!\n", filename);
+ return DLT_RETURN_ERROR;
+ }
+
+ if (fread(buffer, sizeof(buffer), 1, handle) != 0) {
+ if (!feof(handle)) {
+ pr_error("Filter file %s is to big for reading it with current buffer!\n", filename);
+ return DLT_RETURN_ERROR;
+ }
+ }
+
+ j_parsed_json = json_tokener_parse_verbose(buffer, &jerr);
+
+ if (jerr != json_tokener_success) {
+ pr_error("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");
+ pr_verbose("The following filter(s) are applied: \n");
+ int iterator = 0;
+ json_object_object_foreach(j_parsed_json, key, val)
+ {
+ if (iterator >= DLT_FILTER_MAX) {
+ pr_error("Maximum number (%d) of allowed filters reached, ignoring rest of filters!\n",
+ DLT_FILTER_MAX);
+ break;
+ }
+
+ printf("%s:\n", key);
+ pr_verbose("%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);
+ pr_verbose("\tAppId: %.*s\n", DLT_ID_SIZE, app_id);
+ printf("\tConextId: %.*s\n", DLT_ID_SIZE, context_id);
+ pr_verbose("\tConextId: %.*s\n", DLT_ID_SIZE, context_id);
+ printf("\tLogLevel: %i\n", log_level);
+ pr_verbose("\tLogLevel: %i\n", log_level);
+ printf("\tPayloadMin: %i\n", payload_min);
+ pr_verbose("\tPayloadMin: %i\n", payload_min);
+ printf("\tPayloadMax: %i\n", payload_max);
+ pr_verbose("\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;
+
+ if(verbose)
+ pr_verbose("dlt_json_filter_load()\n");
+
+ 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;
+
+ json_decoder_error_t ret = json_decoder_parse_file(j_decoder, filename);
+
+ if (ret != JSON_DECODER_OK) {
+ pr_error("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) {
+ pr_error("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 */
+
+#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;
+
+ if(verbose)
+ pr_verbose("dlt_json_filter_save()\n");
+
+ 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((char*)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;
+
+ if(verbose)
+ pr_verbose("dlt_json_filter_save()\n");
+
+ 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 */
diff --git a/src/console/dlt-control-common.h b/src/console/dlt-control-common.h
index 41606bb..697a927 100644
--- a/src/console/dlt-control-common.h
+++ b/src/console/dlt-control-common.h
@@ -31,6 +31,8 @@
#define DLT_CTRL_ECUID_LEN 10
#define DLT_DAEMON_FLAG_MAX 256
+#define JSON_FILTER_SIZE 200 /* Size in bytes, that the definition of one filter with all parameters needs */
+
#ifndef pr_fmt
# define pr_fmt(fmt) fmt
#endif
@@ -84,4 +86,23 @@ int dlt_control_send_message(DltControlMsgBody *, int);
/* Destroys the connection to the daemon */
int dlt_control_deinit(void);
+
+#ifdef EXTENDED_FILTERING
+/**
+ * Load json filter from file.
+ * @param filter pointer to structure of organising DLT filter
+ * @param filename filename to load filters from
+ * @param verbose if set to true verbose information is printed out.
+ * @return negative value if there was an error
+ */
+DltReturnValue dlt_json_filter_load(DltFilter *filter, const char *filename, int verbose);
+/**
+ * Save filter in json format to file.
+ * @param filter pointer to structure of organising DLT filter
+ * @param filename filename to safe filters into
+ * @param verbose if set to true verbose information is printed out.
+ * @return negative value if there was an error
+ */
+DltReturnValue dlt_json_filter_save(DltFilter *filter, const char *filename, int verbose);
#endif
+#endif \ No newline at end of file
diff --git a/src/console/dlt-receive.c b/src/console/dlt-receive.c
index 62cc18f..f41566a 100644
--- a/src/console/dlt-receive.c
+++ b/src/console/dlt-receive.c
@@ -85,6 +85,7 @@
#include <inttypes.h>
#include "dlt_client.h"
+#include "dlt-control-common.h"
#define DLT_RECEIVE_ECU_ID "RECV"
diff --git a/src/console/logstorage/CMakeLists.txt b/src/console/logstorage/CMakeLists.txt
index 5daaae3..ce19f74 100644
--- a/src/console/logstorage/CMakeLists.txt
+++ b/src/console/logstorage/CMakeLists.txt
@@ -36,7 +36,7 @@ add_executable(dlt-logstorage-ctrl
${dlt-logstorage-ctrl_SRCS}
${dlt_control_common_SRCS})
-target_link_libraries(dlt-logstorage-ctrl dlt ${LOGSTORAGE_LIBRARY})
+target_link_libraries(dlt-logstorage-ctrl dlt ${LOGSTORAGE_LIBRARY} dlt_control_common_lib)
set_target_properties(dlt-logstorage-ctrl PROPERTIES LINKER_LANGUAGE C)
install(TARGETS dlt-logstorage-ctrl