Line data Source code
1 : /**
2 : * Copyright (C) 2015 Advanced Driver Information Technology.
3 : * This code is developed by Advanced Driver Information Technology.
4 : * Copyright of Advanced Driver Information Technology, Bosch and DENSO.
5 : *
6 : * This file is part of COVESA Project Dlt - Diagnostic Log and Trace console apps.
7 : *
8 : *
9 : * \copyright
10 : * This Source Code Form is subject to the terms of the
11 : * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
12 : * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 : *
14 : *
15 : * \author Christoph Lipka <clipka@jp.adit-jv.com> ADIT 2015
16 : * \author Frederic Berat <fberat@de.adit-jv.com> ADIT 2015
17 : *
18 : * \file dlt-control-common.c
19 : * For further information see http://www.covesa.org/.
20 : */
21 :
22 : /*******************************************************************************
23 : ** **
24 : ** SRC-MODULE: dlt-control-common.c **
25 : ** **
26 : ** TARGET : linux **
27 : ** **
28 : ** PROJECT : DLT **
29 : ** **
30 : ** AUTHOR : Christoph Lipka clipka@jp.adit-jv.com **
31 : ** PURPOSE : **
32 : ** **
33 : ** REMARKS : **
34 : ** **
35 : ** PLATFORM DEPENDANT [yes/no]: yes **
36 : ** **
37 : ** TO BE CHANGED BY USER [yes/no]: no **
38 : ** **
39 : *******************************************************************************/
40 :
41 : /*******************************************************************************
42 : ** Author Identity **
43 : ********************************************************************************
44 : ** **
45 : ** Initials Name Company **
46 : ** -------- ------------------------- ---------------------------------- **
47 : ** cl Christoph Lipka ADIT **
48 : ** fb Frederic Berat ADIT **
49 : *******************************************************************************/
50 : #define pr_fmt(fmt) "Common control: "fmt
51 :
52 : #include <errno.h>
53 : #include <dirent.h>
54 : #include <stdio.h>
55 : #include <stdlib.h>
56 : #include <string.h>
57 : #include <pthread.h>
58 : #include <sys/types.h>
59 : #include <sys/socket.h>
60 :
61 : #include "dlt_common.h"
62 : #include "dlt_protocol.h"
63 : #include "dlt_client.h"
64 :
65 : #include "dlt-control-common.h"
66 :
67 : #ifdef EXTENDED_FILTERING
68 : # if defined(__linux__) || defined(__ANDROID_API__)
69 : # include <json-c/json.h> /* for json filter parsing on Linux and Android */
70 : # endif
71 : # ifdef __QNX__
72 : # include <sys/json.h> /* for json filter parsing on QNX */
73 : # endif
74 : #endif
75 :
76 : #define DLT_CTRL_APID "DLTC"
77 : #define DLT_CTRL_CTID "DLTC"
78 :
79 : /** @brief Analyze the daemon answer
80 : *
81 : * This function as to be provided by the user of the connection.
82 : *
83 : * @param answer The textual answer of the daemon
84 : * @param payload The daemons answer payload
85 : * @param length The daemons answer payload length
86 : *
87 : * @return User defined.
88 : */
89 : static int (*response_analyzer_cb)(char *, void *, int);
90 :
91 : static pthread_t daemon_connect_thread;
92 : static DltClient g_client;
93 : static int callback_return = -1;
94 : static pthread_mutex_t answer_lock = PTHREAD_MUTEX_INITIALIZER;
95 : static pthread_cond_t answer_cond = PTHREAD_COND_INITIALIZER;
96 :
97 : static int local_verbose;
98 : static char local_ecuid[DLT_CTRL_ECUID_LEN]; /* Name of ECU */
99 : static int local_timeout;
100 : static char local_filename[DLT_MOUNT_PATH_MAX]= {0}; /* Path to dlt.conf */
101 :
102 18 : int get_verbosity(void)
103 : {
104 46 : return local_verbose;
105 : }
106 :
107 0 : void set_verbosity(int v)
108 : {
109 2 : local_verbose = !!v;
110 0 : }
111 :
112 4 : char *get_ecuid(void)
113 : {
114 4 : return local_ecuid;
115 : }
116 :
117 2 : void set_ecuid(char *ecuid)
118 : {
119 2 : char *ecuid_conf = NULL;
120 :
121 2 : if (local_ecuid != ecuid) {
122 : /* If user pass NULL, read ECUId from dlt.conf */
123 0 : if (ecuid == NULL) {
124 0 : if (dlt_parse_config_param("ECUId", &ecuid_conf) == 0) {
125 : memset(local_ecuid, 0, DLT_CTRL_ECUID_LEN);
126 0 : strncpy(local_ecuid, ecuid_conf, DLT_CTRL_ECUID_LEN);
127 0 : local_ecuid[DLT_CTRL_ECUID_LEN -1] = '\0';
128 : if (ecuid_conf !=NULL)
129 0 : free(ecuid_conf);
130 : }
131 : else {
132 0 : pr_error("Cannot read ECUid from dlt.conf\n");
133 : }
134 : }
135 : else {
136 : /* Set user passed ECUID */
137 : memset(local_ecuid, 0, DLT_CTRL_ECUID_LEN);
138 : strncpy(local_ecuid, ecuid, DLT_CTRL_ECUID_LEN);
139 0 : local_ecuid[DLT_CTRL_ECUID_LEN - 1] = '\0';
140 : }
141 : }
142 2 : }
143 :
144 2 : void set_conf(char *file_path)
145 : {
146 2 : if (file_path != NULL) {
147 : memset(local_filename, 0, DLT_MOUNT_PATH_MAX);
148 : strncpy(local_filename, file_path, DLT_MOUNT_PATH_MAX);
149 2 : local_filename[DLT_MOUNT_PATH_MAX - 1] = '\0';
150 : }
151 : else {
152 0 : pr_error("Argument is NULL\n");
153 : }
154 2 : }
155 :
156 2 : int get_timeout(void)
157 : {
158 2 : return local_timeout;
159 : }
160 :
161 2 : void set_timeout(int t)
162 : {
163 2 : local_timeout = DLT_CTRL_TIMEOUT;
164 :
165 2 : if (t > 1)
166 2 : local_timeout = t;
167 : else
168 0 : pr_error("Timeout to small. Set to default: %d",
169 : DLT_CTRL_TIMEOUT);
170 2 : }
171 :
172 2 : void set_send_serial_header(const int value)
173 : {
174 2 : g_client.send_serial_header = value;
175 2 : }
176 :
177 2 : void set_resync_serial_header(const int value)
178 : {
179 2 : g_client.resync_serial_header = value;
180 2 : }
181 :
182 2 : int dlt_parse_config_param(char *config_id, char **config_data)
183 : {
184 : FILE *pFile = NULL;
185 : int value_length = DLT_LINE_LEN;
186 2 : char line[DLT_LINE_LEN - 1] = { 0 };
187 2 : char token[DLT_LINE_LEN] = { 0 };
188 2 : char value[DLT_LINE_LEN] = { 0 };
189 : char *pch = NULL;
190 : const char *filename = NULL;
191 :
192 2 : if (*config_data != NULL)
193 0 : *config_data = NULL;
194 :
195 : /* open configuration file */
196 2 : if (local_filename[0] != 0) {
197 : filename = local_filename;
198 : } else {
199 : filename = CONFIGURATION_FILES_DIR "/dlt.conf";
200 : }
201 2 : pFile = fopen(filename, "r");
202 :
203 2 : if (pFile != NULL) {
204 : while (1) {
205 : /* fetch line from configuration file */
206 14 : if (fgets(line, value_length - 1, pFile) != NULL) {
207 12 : if (strncmp(line, config_id, strlen(config_id)) == 0) {
208 2 : pch = strtok(line, " =\r\n");
209 2 : token[0] = 0;
210 2 : value[0] = 0;
211 :
212 4 : while (pch != NULL) {
213 4 : if (token[0] == 0) {
214 : strncpy(token, pch, sizeof(token) - 1);
215 : token[sizeof(token) - 1] = 0;
216 : }
217 : else {
218 : strncpy(value, pch, sizeof(value) - 1);
219 : value[sizeof(value) - 1] = 0;
220 2 : break;
221 : }
222 :
223 2 : pch = strtok(NULL, " =\r\n");
224 : }
225 :
226 2 : if (token[0] && value[0]) {
227 2 : if (strcmp(token, config_id) == 0) {
228 2 : *(config_data) = (char *)
229 2 : calloc(DLT_DAEMON_FLAG_MAX, sizeof(char));
230 : memcpy(*config_data,
231 : value,
232 : DLT_DAEMON_FLAG_MAX - 1);
233 : }
234 : }
235 : }
236 : }
237 : else {
238 : break;
239 : }
240 : }
241 :
242 2 : fclose (pFile);
243 : }
244 : else {
245 0 : fprintf(stderr, "Cannot open configuration file: %s\n", filename);
246 : }
247 :
248 2 : if (*config_data == NULL)
249 0 : return -1;
250 :
251 : return 0;
252 : }
253 :
254 : /** @brief Prepare the extra headers of a DLT message
255 : *
256 : * Modifies the extra headers of the message so that it can be sent.
257 : *
258 : * @param msg The message to be prepared
259 : * @param header The base header to be used.
260 : *
261 : * @return 0 on success, -1 otherwise.
262 : */
263 2 : static int prepare_extra_headers(DltMessage *msg, uint8_t *header)
264 : {
265 : uint32_t shift = 0;
266 :
267 2 : pr_verbose("Preparing extra headers.\n");
268 :
269 2 : if (!msg || !header)
270 : return -1;
271 :
272 2 : shift = (uint32_t) (sizeof(DltStorageHeader) +
273 : sizeof(DltStandardHeader) +
274 2 : DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp));
275 :
276 : /* Set header extra parameters */
277 2 : dlt_set_id(msg->headerextra.ecu, get_ecuid());
278 :
279 2 : msg->headerextra.tmsp = dlt_uptime();
280 :
281 : /* Copy header extra parameters to header buffer */
282 2 : if (dlt_message_set_extraparameters(msg, get_verbosity()) == -1) {
283 0 : pr_error("Cannot copy header extra parameter\n");
284 0 : return -1;
285 : }
286 :
287 : /* prepare extended header */
288 2 : msg->extendedheader = (DltExtendedHeader *)(header + shift);
289 :
290 2 : msg->extendedheader->msin = DLT_MSIN_CONTROL_REQUEST;
291 :
292 2 : msg->extendedheader->noar = 1; /* one payload packet */
293 :
294 : /* Dummy values have to be set */
295 2 : dlt_set_id(msg->extendedheader->apid, DLT_CTRL_APID);
296 2 : dlt_set_id(msg->extendedheader->ctid, DLT_CTRL_CTID);
297 :
298 2 : return 0;
299 : }
300 :
301 : /** @brief Prepare the headers of a DLT message
302 : *
303 : * Modifies the headers of the message so that it can be sent.
304 : *
305 : * @param msg The message to be prepared
306 : * @param header The base header to be used.
307 : *
308 : * @return 0 on success, -1 otherwise.
309 : */
310 2 : static int prepare_headers(DltMessage *msg, uint8_t *header)
311 : {
312 : uint32_t len = 0;
313 :
314 2 : pr_verbose("Preparing headers.\n");
315 :
316 2 : if (!msg || !header)
317 : return -1;
318 :
319 2 : msg->storageheader = (DltStorageHeader *)header;
320 :
321 2 : if (dlt_set_storageheader(msg->storageheader, "") == -1) {
322 0 : pr_error("Storage header initialization failed.\n");
323 0 : return -1;
324 : }
325 :
326 : /* prepare standard header */
327 2 : msg->standardheader =
328 2 : (DltStandardHeader *)(header + sizeof(DltStorageHeader));
329 :
330 2 : msg->standardheader->htyp = DLT_HTYP_WEID |
331 : DLT_HTYP_WTMS | DLT_HTYP_UEH | DLT_HTYP_PROTOCOL_VERSION1;
332 :
333 : #if (BYTE_ORDER == BIG_ENDIAN)
334 : msg->standardheader->htyp = (msg->standardheader->htyp | DLT_HTYP_MSBF);
335 : #endif
336 :
337 2 : msg->standardheader->mcnt = 0;
338 :
339 : /* prepare length information */
340 2 : msg->headersize = (uint32_t) (sizeof(DltStorageHeader) +
341 : sizeof(DltStandardHeader) +
342 : sizeof(DltExtendedHeader) +
343 : DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp));
344 :
345 2 : len = (uint32_t) (msg->headersize - sizeof(DltStorageHeader) + msg->datasize);
346 :
347 2 : if (len > UINT16_MAX) {
348 0 : pr_error("Message header is too long.\n");
349 0 : return -1;
350 : }
351 :
352 2 : msg->standardheader->len = DLT_HTOBE_16(len);
353 :
354 2 : return 0;
355 : }
356 :
357 : /** @brief Prepare a DLT message.
358 : *
359 : * The DLT message is built using the data given by the user.
360 : * The data is basically composed of a buffer and a size.
361 : *
362 : * @param data The message body to be used to build the DLT message.
363 : *
364 : * @return 0 on success, -1 otherwise.
365 : */
366 2 : static DltMessage *dlt_control_prepare_message(DltControlMsgBody *data)
367 : {
368 : DltMessage *msg = NULL;
369 :
370 2 : pr_verbose("Preparing message.\n");
371 :
372 2 : if (data == NULL) {
373 0 : pr_error("Data for message body is NULL\n");
374 0 : return NULL;
375 : }
376 :
377 2 : msg = calloc(1, sizeof(DltMessage));
378 :
379 2 : if (msg == NULL) {
380 0 : pr_error("Cannot allocate memory for Dlt Message\n");
381 0 : return NULL;
382 : }
383 :
384 2 : if (dlt_message_init(msg, get_verbosity()) == -1) {
385 0 : pr_error("Cannot initialize Dlt Message\n");
386 0 : free(msg);
387 0 : return NULL;
388 : }
389 :
390 : /* prepare payload of data */
391 2 : msg->databuffersize = msg->datasize = (uint32_t) data->size;
392 :
393 : /* Allocate memory for Dlt Message's buffer */
394 2 : msg->databuffer = (uint8_t *)calloc(1, data->size);
395 :
396 2 : if (msg->databuffer == NULL) {
397 0 : pr_error("Cannot allocate memory for data buffer\n");
398 0 : free(msg);
399 0 : return NULL;
400 : }
401 :
402 : /* copy data into message */
403 2 : memcpy(msg->databuffer, data->data, data->size);
404 :
405 : /* prepare storage header */
406 2 : if (prepare_headers(msg, msg->headerbuffer)) {
407 0 : dlt_message_free(msg, get_verbosity());
408 0 : free(msg);
409 0 : return NULL;
410 : }
411 :
412 : /* prepare extra headers */
413 2 : if (prepare_extra_headers(msg, msg->headerbuffer)) {
414 0 : dlt_message_free(msg, get_verbosity());
415 0 : free(msg);
416 0 : return NULL;
417 : }
418 :
419 : return msg;
420 : }
421 :
422 : /** @brief Initialize the connection with the daemon
423 : *
424 : * The connection is initialized using an internal callback. The user's
425 : * response analyzer will be finally executed by this callback.
426 : * The client pointer is used to established the connection.
427 : *
428 : * @param client A pointer to a valid client structure
429 : * @param cb The internal callback to be executed while receiving a new message
430 : *
431 : * @return 0 on success, -1 otherwise.
432 : */
433 2 : static int dlt_control_init_connection(DltClient *client, void *cb)
434 : {
435 2 : int (*callback)(DltMessage *message, void *data) = cb;
436 :
437 2 : if (!cb || !client) {
438 0 : pr_error("%s: Invalid parameters\n", __func__);
439 0 : return -1;
440 : }
441 :
442 2 : pr_verbose("Initializing the connection.\n");
443 :
444 2 : if (dlt_client_init(client, get_verbosity()) != 0) {
445 0 : pr_error("Failed to register callback (NULL)\n");
446 0 : return -1;
447 : }
448 :
449 2 : dlt_client_register_message_callback(callback);
450 :
451 2 : client->socketPath = NULL;
452 :
453 2 : if (dlt_parse_config_param("ControlSocketPath", &client->socketPath) != 0) {
454 : /* Failed to read from conf, copy default */
455 0 : if (dlt_client_set_socket_path(client, DLT_DAEMON_DEFAULT_CTRL_SOCK_PATH) == -1) {
456 0 : pr_error("set socket path didn't succeed\n");
457 0 : return -1;
458 : }
459 : }
460 :
461 2 : client->mode = DLT_CLIENT_MODE_UNIX;
462 :
463 2 : return dlt_client_connect(client, get_verbosity());
464 : }
465 :
466 : /** @brief Daemon listener function
467 : *
468 : * This function will continuously read on the DLT socket, until an error occurs
469 : * or the thread executing this function is canceled.
470 : *
471 : * @param data Thread parameter
472 : *
473 : * @return The thread parameter given as argument.
474 : */
475 2 : static void *dlt_control_listen_to_daemon(void *data)
476 : {
477 2 : pr_verbose("Ready to receive DLT answers.\n");
478 2 : dlt_client_main_loop(&g_client, NULL, get_verbosity());
479 2 : return data;
480 : }
481 :
482 : /** @brief Internal callback for DLT response
483 : *
484 : * This function is called by the dlt_client_main_loop once a response is read
485 : * from the DLT socket.
486 : * After some basic checks, the user's response analyzer is called. The return
487 : * value of the analyzer is then provided back to the dlt_control_send_message
488 : * function to be given back as a return value.
489 : * As this function is called in a dedicated thread, the return value is
490 : * provided using a global variable.
491 : * Access to this variable is controlled through a dedicated mutex.
492 : * New values are signaled using a dedicated condition variable.
493 : *
494 : * @param message The DLT answer
495 : * @param data Unused
496 : *
497 : * @return The analyzer return value or -1 on early errors.
498 : */
499 2 : static int dlt_control_callback(DltMessage *message, void *data)
500 : {
501 2 : char text[DLT_RECEIVE_BUFSIZE] = { 0 };
502 : (void)data;
503 :
504 2 : if (message == NULL) {
505 0 : pr_error("Received message is null\n");
506 0 : return -1;
507 : }
508 :
509 : /* prepare storage header */
510 2 : if (DLT_IS_HTYP_WEID(message->standardheader->htyp))
511 2 : dlt_set_storageheader(message->storageheader, message->headerextra.ecu);
512 : else
513 0 : dlt_set_storageheader(message->storageheader, "LCTL");
514 :
515 2 : dlt_message_header(message, text, DLT_RECEIVE_BUFSIZE, get_verbosity());
516 :
517 : /* Extracting payload */
518 2 : dlt_message_payload(message, text,
519 : DLT_RECEIVE_BUFSIZE,
520 : DLT_OUTPUT_ASCII,
521 : get_verbosity());
522 :
523 : /*
524 : * Checking payload with the provided callback and return the result
525 : */
526 2 : pthread_mutex_lock(&answer_lock);
527 4 : callback_return = response_analyzer_cb(text,
528 2 : message->databuffer,
529 2 : (int) message->datasize);
530 2 : pthread_cond_signal(&answer_cond);
531 2 : pthread_mutex_unlock(&answer_lock);
532 :
533 2 : return callback_return;
534 : }
535 :
536 : /** @brief Send a message to the daemon and wait for the asynchronous answer.
537 : *
538 : * The answer is received and analyzed by a dedicated thread. Thus we need
539 : * to wait for the signal from this thread and then read the return value
540 : * to be provided to the user.
541 : * In case of timeout, this function fails.
542 : * The message provided by the user is formated in DLT format before sending.
543 : *
544 : * @param body The message provided by the user
545 : * @param timeout The time to wait before considering that no answer will come
546 : *
547 : * @return The user response analyzer return value, -1 in case of early error.
548 : */
549 2 : int dlt_control_send_message(DltControlMsgBody *body, int timeout)
550 : {
551 : struct timespec t;
552 : DltMessage *msg = NULL;
553 :
554 2 : if (!body) {
555 0 : pr_error("%s: Invalid input.\n", __func__);
556 0 : return -1;
557 : }
558 :
559 2 : if (clock_gettime(CLOCK_REALTIME, &t) == -1) {
560 0 : pr_error("Cannot read system time.\n");
561 0 : return -1;
562 : }
563 :
564 2 : t.tv_sec += timeout;
565 :
566 : /* send command to daemon here */
567 2 : msg = dlt_control_prepare_message(body);
568 :
569 2 : if (msg == NULL) {
570 0 : pr_error("Control message preparation failed\n");
571 0 : return -1;
572 : }
573 :
574 2 : pthread_mutex_lock(&answer_lock);
575 :
576 : /* Re-init the return value */
577 2 : callback_return = -1;
578 :
579 2 : if (dlt_client_send_message_to_socket(&g_client, msg) != DLT_RETURN_OK)
580 : {
581 0 : pr_error("Sending message to daemon failed\n");
582 0 : dlt_message_free(msg, get_verbosity());
583 0 : free(msg);
584 :
585 : /* make sure the mutex is unlocked to prevent deadlocks */
586 0 : pthread_mutex_unlock(&answer_lock);
587 0 : return -1;
588 : }
589 :
590 : /*
591 : * When a timeouts occurs, pthread_cond_timedwait()
592 : * shall nonetheless release and re-acquire the mutex referenced by mutex
593 : */
594 2 : pthread_cond_timedwait(&answer_cond, &answer_lock, &t);
595 2 : pthread_mutex_unlock(&answer_lock);
596 :
597 : /* Destroying the message */
598 2 : dlt_message_free(msg, get_verbosity());
599 2 : free(msg);
600 :
601 : /* At this point either the value is already correct, either it's still -1.
602 : * Then, we don't care to lock the access.
603 : */
604 2 : return callback_return;
605 : }
606 :
607 : /** @brief Control communication initialization
608 : *
609 : * This will prepare the DLT connection and the thread dedicated to the
610 : * response listening.
611 : *
612 : * @param response_analyzer User defined function used to analyze the response
613 : * @param ecuid The ECUID to provide to the daemon
614 : * @param verbosity The verbosity level
615 : *
616 : * @return 0 on success, -1 otherwise.
617 : */
618 2 : int dlt_control_init(int (*response_analyzer)(char *, void *, int),
619 : char *ecuid,
620 : int verbosity)
621 : {
622 2 : if (!response_analyzer || !ecuid) {
623 0 : pr_error("%s: Invalid input.\n", __func__);
624 0 : return -1;
625 : }
626 :
627 2 : response_analyzer_cb = response_analyzer;
628 2 : set_ecuid(ecuid);
629 : set_verbosity(verbosity);
630 :
631 2 : if (dlt_control_init_connection(&g_client, dlt_control_callback) != 0) {
632 0 : pr_error("Connection initialization failed\n");
633 0 : dlt_client_cleanup(&g_client, get_verbosity());
634 0 : return -1;
635 : }
636 :
637 : /* Contact DLT daemon */
638 2 : if (pthread_create(&daemon_connect_thread,
639 : NULL,
640 : dlt_control_listen_to_daemon,
641 : NULL) != 0) {
642 0 : pr_error("Cannot create thread to communicate with DLT daemon.\n");
643 0 : return -1;
644 : }
645 :
646 : return 0;
647 : }
648 :
649 : /** @brief Control communication clean-up
650 : *
651 : * Cancels the listener thread and clean=up the dlt client structure.
652 : *
653 : * @return 0 on success, -1 otherwise.
654 : */
655 2 : int dlt_control_deinit(void)
656 : {
657 : /* At this stage, we want to stop sending/receiving
658 : * from dlt-daemon. So in order to avoid cancellation
659 : * at recv(), shutdown and close the socket
660 : */
661 2 : if (g_client.receiver.fd) {
662 2 : shutdown(g_client.receiver.fd, SHUT_RDWR);
663 2 : close(g_client.receiver.fd);
664 2 : g_client.receiver.fd = -1;
665 : }
666 :
667 2 : if (pthread_join(daemon_connect_thread, NULL)) {
668 0 : pr_error("Unable to join the thread with ERRNO=%s\n", strerror(errno));
669 : }
670 :
671 : /* Closing the socket */
672 2 : return dlt_client_cleanup(&g_client, get_verbosity());
673 : }
674 :
675 :
676 : #ifdef EXTENDED_FILTERING /* EXTENDED_FILTERING */
677 : # if defined(__linux__) || defined(__ANDROID_API__)
678 : DltReturnValue dlt_json_filter_load(DltFilter *filter, const char *filename, int verbose)
679 : {
680 : if ((filter == NULL) || (filename == NULL))
681 : return DLT_RETURN_WRONG_PARAMETER;
682 :
683 : if(verbose)
684 : pr_verbose("dlt_json_filter_load()\n");
685 :
686 : FILE *handle;
687 : char buffer[JSON_FILTER_SIZE*DLT_FILTER_MAX];
688 : struct json_object *j_parsed_json;
689 : struct json_object *j_app_id;
690 : struct json_object *j_context_id;
691 : struct json_object *j_log_level;
692 : struct json_object *j_payload_min;
693 : struct json_object *j_payload_max;
694 : enum json_tokener_error jerr;
695 :
696 : char app_id[DLT_ID_SIZE + 1] = "";
697 : char context_id[DLT_ID_SIZE + 1] = "";
698 : int32_t log_level = 0;
699 : int32_t payload_max = INT32_MAX;
700 : int32_t payload_min = 0;
701 :
702 : handle = fopen(filename, "r");
703 :
704 : if (handle == NULL) {
705 : pr_error("Filter file %s cannot be opened!\n", filename);
706 : return DLT_RETURN_ERROR;
707 : }
708 :
709 : if (fread(buffer, sizeof(buffer), 1, handle) != 0) {
710 : if (!feof(handle)) {
711 : pr_error("Filter file %s is to big for reading it with current buffer!\n", filename);
712 : return DLT_RETURN_ERROR;
713 : }
714 : }
715 :
716 : j_parsed_json = json_tokener_parse_verbose(buffer, &jerr);
717 :
718 : if (jerr != json_tokener_success) {
719 : pr_error("Faild to parse given filter %s: %s\n", filename, json_tokener_error_desc(jerr));
720 : return DLT_RETURN_ERROR;
721 : }
722 :
723 : printf("The following filter(s) are applied: \n");
724 : pr_verbose("The following filter(s) are applied: \n");
725 : int iterator = 0;
726 : json_object_object_foreach(j_parsed_json, key, val)
727 : {
728 : if (iterator >= DLT_FILTER_MAX) {
729 : pr_error("Maximum number (%d) of allowed filters reached, ignoring rest of filters!\n",
730 : DLT_FILTER_MAX);
731 : break;
732 : }
733 :
734 : printf("%s:\n", key);
735 : pr_verbose("%s:\n", key);
736 :
737 : if (json_object_object_get_ex(val, "AppId", &j_app_id))
738 : strncpy(app_id, json_object_get_string(j_app_id), DLT_ID_SIZE);
739 : else
740 : dlt_set_id(app_id, "");
741 :
742 : if (json_object_object_get_ex(val, "ContextId", &j_context_id))
743 : strncpy(context_id, json_object_get_string(j_context_id), DLT_ID_SIZE);
744 : else
745 : dlt_set_id(context_id, "");
746 :
747 : if (json_object_object_get_ex(val, "LogLevel", &j_log_level))
748 : log_level = json_object_get_int(j_log_level);
749 : else
750 : log_level = 0;
751 :
752 : if (json_object_object_get_ex(val, "PayloadMin", &j_payload_min))
753 : payload_min = json_object_get_int(j_payload_min);
754 : else
755 : payload_min = 0;
756 :
757 : if (json_object_object_get_ex(val, "PayloadMax", &j_payload_max))
758 : payload_max = json_object_get_int(j_payload_max);
759 : else
760 : payload_max = INT32_MAX;
761 :
762 : dlt_filter_add(filter, app_id, context_id, log_level, payload_min, payload_max, verbose);
763 :
764 : printf("\tAppId: %.*s\n", DLT_ID_SIZE, app_id);
765 : pr_verbose("\tAppId: %.*s\n", DLT_ID_SIZE, app_id);
766 : printf("\tConextId: %.*s\n", DLT_ID_SIZE, context_id);
767 : pr_verbose("\tConextId: %.*s\n", DLT_ID_SIZE, context_id);
768 : printf("\tLogLevel: %i\n", log_level);
769 : pr_verbose("\tLogLevel: %i\n", log_level);
770 : printf("\tPayloadMin: %i\n", payload_min);
771 : pr_verbose("\tPayloadMin: %i\n", payload_min);
772 : printf("\tPayloadMax: %i\n", payload_max);
773 : pr_verbose("\tPayloadMax: %i\n", payload_max);
774 :
775 : iterator++;
776 : }
777 :
778 : fclose(handle);
779 :
780 : return DLT_RETURN_OK;
781 : }
782 : # endif /* __Linux__ */
783 :
784 : # ifdef __QNX__
785 : DltReturnValue dlt_json_filter_load(DltFilter *filter, const char *filename, int verbose)
786 : {
787 : if ((filter == NULL) || (filename == NULL))
788 : return DLT_RETURN_WRONG_PARAMETER;
789 :
790 : if(verbose)
791 : pr_verbose("dlt_json_filter_load()\n");
792 :
793 : json_decoder_t *j_decoder = json_decoder_create();
794 :
795 : const char *s_app_id;
796 : const char *s_context_id;
797 : int32_t log_level = 0;
798 : int32_t payload_max = INT32_MAX;
799 : int32_t payload_min = 0;
800 :
801 : json_decoder_error_t ret = json_decoder_parse_file(j_decoder, filename);
802 :
803 : if (ret != JSON_DECODER_OK) {
804 : pr_error("Faild to parse given filter %s: json_decoder_error_t is %i\n", filename, ret);
805 : return DLT_RETURN_ERROR;
806 : }
807 :
808 : json_decoder_push_object(j_decoder, NULL, true);
809 :
810 : int iterator = 0;
811 : bool end_of_json = false;
812 :
813 : while (!end_of_json) {
814 : if (iterator >= DLT_FILTER_MAX) {
815 : pr_error("Maximum number (%d) of allowed filters reached, ignoring rest of filters!\n",
816 : DLT_FILTER_MAX);
817 : break;
818 : }
819 :
820 : if (json_decoder_next(j_decoder) == JSON_DECODER_NOT_FOUND)
821 : end_of_json = true;
822 :
823 : json_decoder_previous(j_decoder);
824 :
825 : printf("%s:\n", json_decoder_name(j_decoder));
826 : json_decoder_push_object(j_decoder, NULL, true);
827 :
828 : if (json_decoder_get_string(j_decoder, "AppId", &s_app_id, true) != JSON_DECODER_OK)
829 : s_app_id = "";
830 :
831 : if (json_decoder_get_string(j_decoder, "ContextId", &s_context_id, true) != JSON_DECODER_OK)
832 : s_context_id = "";
833 :
834 : if (json_decoder_get_int(j_decoder, "LogLevel", &log_level, true) != JSON_DECODER_OK)
835 : log_level = 0;
836 :
837 : if (json_decoder_get_int(j_decoder, "PayloadMin", &payload_min, true) != JSON_DECODER_OK)
838 : payload_min = 0;
839 :
840 : if (json_decoder_get_int(j_decoder, "PayloadMax", &payload_max, true) != JSON_DECODER_OK)
841 : payload_max = INT32_MAX;
842 :
843 : char app_id[DLT_ID_SIZE];
844 : char context_id[DLT_ID_SIZE];
845 :
846 : #pragma GCC diagnostic push
847 : #pragma GCC diagnostic ignored "-Wstringop-truncation"
848 : strncpy(app_id, s_app_id, DLT_ID_SIZE);
849 : strncpy(context_id, s_context_id, DLT_ID_SIZE);
850 : #pragma GCC diagnostic pop
851 :
852 : dlt_filter_add(filter, app_id, context_id, log_level, payload_min, payload_max, verbose);
853 :
854 : printf("\tAppId: %.*s\n", DLT_ID_SIZE, app_id);
855 : printf("\tConextId: %.*s\n", DLT_ID_SIZE, context_id);
856 : printf("\tLogLevel: %i\n", log_level);
857 : printf("\tPayloadMin: %i\n", payload_min);
858 : printf("\tPayloadMax: %i\n", payload_max);
859 :
860 : json_decoder_pop(j_decoder);
861 :
862 : iterator++;
863 : }
864 :
865 : json_decoder_destroy(j_decoder);
866 :
867 : return DLT_RETURN_OK;
868 : }
869 : # endif /* __QNX__ */
870 : #endif /* EXTENDED_FILTERING */
871 :
872 : #ifdef EXTENDED_FILTERING /* EXTENDED_FILTERING */
873 : # if defined(__linux__) || defined(__ANDROID_API__)
874 : DltReturnValue dlt_json_filter_save(DltFilter *filter, const char *filename, int verbose)
875 : {
876 : if ((filter == NULL) || (filename == NULL))
877 : return DLT_RETURN_WRONG_PARAMETER;
878 :
879 : if(verbose)
880 : pr_verbose("dlt_json_filter_save()\n");
881 :
882 : struct json_object *json_filter_obj = json_object_new_object();
883 :
884 : for (int num = 0; num < filter->counter; num++) {
885 : struct json_object *tmp_json_obj = json_object_new_object();
886 : char filter_name[JSON_FILTER_NAME_SIZE];
887 : sprintf(filter_name, "filter%i", num);
888 :
889 : if (filter->apid[num][DLT_ID_SIZE - 1] != 0)
890 : json_object_object_add(tmp_json_obj, "AppId", json_object_new_string_len(filter->apid[num], DLT_ID_SIZE));
891 : else
892 : json_object_object_add(tmp_json_obj, "AppId", json_object_new_string(filter->apid[num]));
893 :
894 : if (filter->ctid[num][DLT_ID_SIZE - 1] != 0)
895 : json_object_object_add(tmp_json_obj, "ContextId",
896 : json_object_new_string_len(filter->ctid[num], DLT_ID_SIZE));
897 : else
898 : json_object_object_add(tmp_json_obj, "ContextId", json_object_new_string(filter->ctid[num]));
899 :
900 : json_object_object_add(tmp_json_obj, "LogLevel", json_object_new_int(filter->log_level[num]));
901 : json_object_object_add(tmp_json_obj, "PayloadMin", json_object_new_int(filter->payload_min[num]));
902 : json_object_object_add(tmp_json_obj, "PayloadMax", json_object_new_int(filter->payload_max[num]));
903 :
904 : json_object_object_add(json_filter_obj, filter_name, tmp_json_obj);
905 : }
906 :
907 : printf("Saving current filter into '%s'\n", filename);
908 : json_object_to_file((char*)filename, json_filter_obj);
909 :
910 : return DLT_RETURN_OK;
911 : }
912 : # endif /* __Linux__ */
913 :
914 : # ifdef __QNX__
915 : DltReturnValue dlt_json_filter_save(DltFilter *filter, const char *filename, int verbose)
916 : {
917 : if ((filter == NULL) || (filename == NULL))
918 : return DLT_RETURN_WRONG_PARAMETER;
919 :
920 : if(verbose)
921 : pr_verbose("dlt_json_filter_save()\n");
922 :
923 : char s_app_id[DLT_ID_SIZE + 1];
924 : char s_context_id[DLT_ID_SIZE + 1];
925 :
926 : json_encoder_t *j_encoder = json_encoder_create();
927 : json_encoder_start_object(j_encoder, NULL);
928 :
929 : for (int num = 0; num < filter->counter; num++) {
930 : char filter_name[JSON_FILTER_NAME_SIZE];
931 : sprintf(filter_name, "filter%i", num);
932 : json_encoder_start_object(j_encoder, filter_name);
933 :
934 : strncpy(s_app_id, filter->apid[num], DLT_ID_SIZE);
935 :
936 : if (filter->apid[num][DLT_ID_SIZE - 1] != 0)
937 : s_app_id[DLT_ID_SIZE] = '\0';
938 :
939 : strncpy(s_context_id, filter->ctid[num], DLT_ID_SIZE);
940 :
941 : if (filter->ctid[num][DLT_ID_SIZE - 1] != 0)
942 : s_context_id[DLT_ID_SIZE] = '\0';
943 :
944 : json_encoder_add_string(j_encoder, "AppId", s_app_id);
945 : json_encoder_add_string(j_encoder, "ContextId", s_context_id);
946 : json_encoder_add_int(j_encoder, "LogLevel", filter->log_level[num]);
947 : json_encoder_add_int(j_encoder, "PayloadMin", filter->payload_min[num]);
948 : json_encoder_add_int(j_encoder, "PayloadMax", filter->payload_max[num]);
949 :
950 : json_encoder_end_object(j_encoder);
951 : }
952 :
953 : json_encoder_end_object(j_encoder);
954 :
955 : printf("Saving current filter into '%s'\n", filename);
956 : FILE *handle = fopen(filename, "w");
957 : int filter_buffer_size = 100 * (filter->counter);
958 : char filter_buffer[filter_buffer_size];
959 : snprintf(filter_buffer, filter_buffer_size, json_encoder_buffer(j_encoder));
960 : fprintf(handle, filter_buffer);
961 :
962 : fclose(handle);
963 : json_encoder_destroy(j_encoder);
964 :
965 : return DLT_RETURN_OK;
966 : }
967 : # endif /* __QNX__ */
968 : #endif /* EXTENDED_FILTERING */
|