LCOV - code coverage report
Current view: top level - daemon - dlt_daemon_client.c (source / functions) Coverage Total Hit
Test: dlt_final_coverage.info Lines: 6.6 % 1922 127
Test Date: 2026-08-27 10:11:10 Functions: 11.7 % 60 7

            Line data    Source code
       1              : /*
       2              :  * SPDX license identifier: MPL-2.0
       3              :  *
       4              :  * Copyright (C) 2011-2015, BMW AG
       5              :  *
       6              :  * This file is part of COVESA Project DLT - Diagnostic Log and Trace.
       7              :  *
       8              :  * This Source Code Form is subject to the terms of the
       9              :  * Mozilla Public License (MPL), v. 2.0.
      10              :  * If a copy of the MPL was not distributed with this file,
      11              :  * You can obtain one at http://mozilla.org/MPL/2.0/.
      12              :  *
      13              :  * For further information see http://www.covesa.org/.
      14              :  */
      15              : 
      16              : /*!
      17              :  * \author
      18              :  * Alexander Wenzel <alexander.aw.wenzel@bmw.de>
      19              :  * Markus Klein <Markus.Klein@esk.fraunhofer.de>
      20              :  * Mikko Rapeli <mikko.rapeli@bmw.de>
      21              :  *
      22              :  * \copyright Copyright © 2011-2015 BMW AG. \n
      23              :  * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
      24              :  *
      25              :  * \file dlt_daemon_client.c
      26              :  */
      27              : 
      28              : #include <netdb.h>
      29              : #include <ctype.h>
      30              : #include <stdio.h>      /* for printf() and fprintf() */
      31              : #include <sys/socket.h> /* for socket(), connect(), (), and recv() */
      32              : #include <sys/stat.h>   /* for stat() */
      33              : #include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
      34              : #include <stdlib.h>     /* for atoi() and exit() */
      35              : #include <string.h>     /* for memset() */
      36              : #include <unistd.h>     /* for close() */
      37              : #include <signal.h>
      38              : #include <syslog.h>
      39              : #include <errno.h>
      40              : #include <pthread.h>
      41              : 
      42              : #ifdef linux
      43              : #include <sys/timerfd.h>
      44              : #endif
      45              : #include <sys/time.h>
      46              : #if defined(linux) && defined(__NR_statx)
      47              : #include <linux/stat.h>
      48              : #endif
      49              : 
      50              : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
      51              : #include <systemd/sd-daemon.h>
      52              : #endif
      53              : 
      54              : #include "dlt_types.h"
      55              : #include "dlt_log.h"
      56              : #include "dlt-daemon.h"
      57              : #include "dlt-daemon_cfg.h"
      58              : #include "dlt_daemon_common_cfg.h"
      59              : 
      60              : #include "dlt_daemon_socket.h"
      61              : #include "dlt_daemon_serial.h"
      62              : 
      63              : #include "dlt_daemon_client.h"
      64              : #include "dlt_daemon_connection.h"
      65              : #include "dlt_daemon_event_handler.h"
      66              : 
      67              : #include "dlt_daemon_offline_logstorage.h"
      68              : #include "dlt_gateway.h"
      69              : #ifdef UDP_CONNECTION_SUPPORT
      70              : #include "dlt_daemon_udp_socket.h"
      71              : #endif
      72              : 
      73              : /** Inline function to calculate/set the requested log level or traces status
      74              :  *  with default log level or trace status when "ForceContextLogLevelAndTraceStatus"
      75              :  *  is enabled and set to 1 in dlt.conf file.
      76              :  *
      77              :  * @param request_log The requested log level (or) trace status
      78              :  * @param context_log The default log level (or) trace status
      79              :  *
      80              :  * @return The log level if requested log level is lower or equal to ContextLogLevel
      81              :  */
      82              : static inline int8_t getStatus(uint8_t request_log, int context_log)
      83              : {
      84            0 :     return (int8_t)((request_log <= context_log) ? request_log : context_log);
      85              : }
      86              : 
      87              : /** @brief Sends up to 2 messages to all the clients.
      88              :  *
      89              :  * Runs through the client list and sends the messages to them. If the message
      90              :  * transfer fails and the connection is a socket connection, the socket is closed.
      91              :  * Takes and release dlt_daemon_mutex.
      92              :  *
      93              :  * @param daemon Daemon structure needed for socket closure.
      94              :  * @param daemon_local Daemon local structure
      95              :  * @param data1 The first message to be sent.
      96              :  * @param size1 The size of the first message.
      97              :  * @param data2 The second message to be send.
      98              :  * @param size2 The second message size.
      99              :  * @param verbose Needed for socket closure.
     100              :  *
     101              :  * @return The amount of data transferred.
     102              :  */
     103            0 : static int dlt_daemon_client_send_all_multiple(
     104              :     DltDaemon* daemon, DltDaemonLocal* daemon_local, void* data1, int size1, void* data2, int size2, int verbose)
     105              : {
     106              :     int sent = 0;
     107              :     nfds_t i = 0;
     108              :     int ret = 0;
     109              :     DltConnection* temp = NULL;
     110              :     int type_mask = (DLT_CON_MASK_CLIENT_MSG_TCP | DLT_CON_MASK_CLIENT_MSG_SERIAL);
     111              : 
     112            0 :     PRINT_FUNCTION_VERBOSE(verbose);
     113              : 
     114            0 :     if ((daemon == NULL) || (daemon_local == NULL)) {
     115            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
     116            0 :         return 0;
     117              :     }
     118              : 
     119            0 :     for (i = 0; i < daemon_local->pEvent.nfds; i++) {
     120              : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
     121              :         bool watchdog_triggered = dlt_daemon_trigger_systemd_watchdog_if_necessary(daemon);
     122              :         if (watchdog_triggered) {
     123              :             dlt_vlog(
     124              :                 LOG_WARNING, "%s notified watchdog, processed %lu/%lu fds already.\n", __func__, i,
     125              :                 daemon_local->pEvent.nfds);
     126              :         }
     127              : #endif
     128            0 :         temp = dlt_event_handler_find_connection(&(daemon_local->pEvent), daemon_local->pEvent.pfd[i].fd);
     129              : 
     130            0 :         if ((temp == NULL) || (temp->receiver == NULL) || !((1 << temp->type) & type_mask)) {
     131            0 :             dlt_log(LOG_DEBUG, "The connection not found or the connection type not TCP/Serial.\n");
     132            0 :             continue;
     133              :         }
     134              : 
     135            0 :         ret = dlt_connection_send_multiple(temp, data1, size1, data2, size2, daemon->sendserialheader);
     136              : 
     137            0 :         if ((ret != DLT_DAEMON_ERROR_OK) && (DLT_CONNECTION_CLIENT_MSG_TCP == temp->type)) {
     138            0 :             dlt_daemon_close_socket(temp->receiver->fd, daemon, daemon_local, verbose);
     139              :         }
     140              : 
     141              :         if (ret != DLT_DAEMON_ERROR_OK)
     142            0 :             dlt_vlog(LOG_WARNING, "%s: send dlt message failed\n", __func__);
     143              :         else
     144              :             /* If sent to at least one client,
     145              :              * then do not store in ring buffer
     146              :              */
     147              :             sent = 1;
     148              :     } /* for */
     149              : 
     150              : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
     151              :     if (sent) {
     152              :         const uint32_t serial_header = daemon->sendserialheader ? sizeof(dltSerialHeader) : 0;
     153              :         daemon->bytes_sent += size1 + size2 + serial_header;
     154              :     }
     155              : #endif
     156              : 
     157              :     return sent;
     158              : }
     159              : 
     160              : /* TODO: Extract the storage header v2 from buffer */
     161          447 : int dlt_daemon_client_send(
     162              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, void* storage_header, int storage_header_size,
     163              :     void* data1, int size1, void* data2, int size2, int verbose)
     164              : {
     165              :     int sent, ret;
     166              :     int ret_logstorage = 0;
     167              :     static int sent_message_overflow_cnt = 0;
     168              : 
     169          447 :     if ((daemon == NULL) || (daemon_local == NULL)) {
     170            0 :         dlt_vlog(LOG_ERR, "%s: Invalid arguments\n", __func__);
     171            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     172              :     }
     173              : 
     174          447 :     if ((sock != DLT_DAEMON_SEND_TO_ALL) && (sock != DLT_DAEMON_SEND_FORCE)) {
     175              :         /* Send message to specific socket */
     176            1 :         if (isatty(sock)) {
     177            0 :             if ((ret = dlt_daemon_serial_send(sock, data1, size1, data2, size2, (char)daemon->sendserialheader))) {
     178            0 :                 dlt_vlog(LOG_WARNING, "%s: serial send dlt message failed\n", __func__);
     179            0 :                 return ret;
     180              :             }
     181              :         } else {
     182            1 :             if ((ret = dlt_daemon_socket_send(sock, data1, size1, data2, size2, (char)daemon->sendserialheader))) {
     183            0 :                 dlt_vlog(LOG_WARNING, "%s: socket send dlt message failed\n", __func__);
     184            0 :                 return ret;
     185              :             }
     186              :         }
     187              : 
     188            1 :         return DLT_DAEMON_ERROR_OK;
     189              :     }
     190              : 
     191              :     /* write message to offline trace */
     192              :     /* In the SEND_BUFFER state we must skip offline tracing because the offline traces */
     193              :     /* are going without buffering directly to the offline trace. Thus we have to filter out */
     194              :     /* the traces that are coming from the buffer. */
     195          446 :     if ((sock != DLT_DAEMON_SEND_FORCE) && (daemon->state != DLT_DAEMON_STATE_SEND_BUFFER)) {
     196          446 :         if (((daemon->mode == DLT_USER_MODE_INTERNAL) || (daemon->mode == DLT_USER_MODE_BOTH))
     197            0 :             && daemon_local->flags.offlineTraceDirectory[0]) {
     198            0 :             if (dlt_offline_trace_write(
     199              :                     &(daemon_local->offlineTrace), storage_header, storage_header_size, data1, size1, data2, size2)) {
     200              :                 static int error_dlt_offline_trace_write_failed = 0;
     201              : 
     202            0 :                 if (!error_dlt_offline_trace_write_failed) {
     203            0 :                     dlt_vlog(LOG_ERR, "%s: dlt_offline_trace_write failed!\n", __func__);
     204            0 :                     error_dlt_offline_trace_write_failed = 1;
     205              :                 }
     206              : 
     207              :                 /*return DLT_DAEMON_ERROR_WRITE_FAILED; */
     208              :             }
     209              :         }
     210              : 
     211              :         /* write messages to offline logstorage only if there is an extended header set
     212              :          * this need to be checked because the function is dlt_daemon_client_send is called by
     213              :          * newly introduced dlt_daemon_log_internal */
     214          446 :         if (daemon_local->flags.offlineLogstorageMaxDevices > 0)
     215          446 :             ret_logstorage = dlt_daemon_logstorage_write(
     216              :                 daemon, &daemon_local->flags, storage_header, storage_header_size, data1, size1, data2, size2);
     217              :     }
     218              : 
     219              :     /* send messages to daemon socket */
     220          446 :     if ((daemon->mode == DLT_USER_MODE_EXTERNAL) || (daemon->mode == DLT_USER_MODE_BOTH)) {
     221              : #ifdef UDP_CONNECTION_SUPPORT
     222              :         if (daemon_local->UDPConnectionSetup == MULTICAST_CONNECTION_ENABLED) {
     223              :             /* Forward message to network client if network routing is not disabled */
     224              :             if (ret_logstorage != 1) {
     225              :                 dlt_daemon_udp_dltmsg_multicast(data1, size1, data2, size2, verbose);
     226              :             }
     227              :         }
     228              : 
     229              : #endif
     230              : 
     231          446 :         if ((sock == DLT_DAEMON_SEND_FORCE) || (daemon->state == DLT_DAEMON_STATE_SEND_DIRECT)) {
     232              :             /* Forward message to network client if network routing is not disabled */
     233            0 :             if (ret_logstorage != 1) {
     234            0 :                 sent = dlt_daemon_client_send_all_multiple(daemon, daemon_local, data1, size1, data2, size2, verbose);
     235              : 
     236            0 :                 if ((sock == DLT_DAEMON_SEND_FORCE) && !sent) {
     237              :                     return DLT_DAEMON_ERROR_SEND_FAILED;
     238              :                 }
     239              :             }
     240              :         }
     241              :     }
     242              : 
     243              :     /* Message was not sent to client, so store it in client ringbuffer */
     244          446 :     if ((sock != DLT_DAEMON_SEND_FORCE)
     245          446 :         && ((daemon->state == DLT_DAEMON_STATE_BUFFER) || (daemon->state == DLT_DAEMON_STATE_SEND_BUFFER)
     246            0 :             || (daemon->state == DLT_DAEMON_STATE_BUFFER_FULL))) {
     247          446 :         if (daemon->state != DLT_DAEMON_STATE_BUFFER_FULL) {
     248              :             /* Store message in history buffer */
     249          446 :             ret = dlt_buffer_push3(
     250              :                 &(daemon->client_ringbuffer), data1, (unsigned int)size1, data2, (unsigned int)size2, 0U, 0U);
     251          446 :             if (ret < DLT_RETURN_OK) {
     252            0 :                 dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_BUFFER_FULL);
     253              :             }
     254              :         }
     255          446 :         if (daemon->state == DLT_DAEMON_STATE_BUFFER_FULL) {
     256            0 :             daemon->overflow_counter += 1;
     257            0 :             if (daemon->overflow_counter == 1)
     258            0 :                 dlt_vlog(LOG_INFO, "%s: Buffer is full! Messages will be discarded.\n", __func__);
     259              : 
     260            0 :             return DLT_DAEMON_ERROR_BUFFER_FULL;
     261              :         }
     262              :     } else {
     263            0 :         if ((daemon->overflow_counter > 0) && (daemon_local->client_connections > 0)) {
     264            0 :             sent_message_overflow_cnt++;
     265            0 :             if (sent_message_overflow_cnt >= 2) {
     266            0 :                 sent_message_overflow_cnt--;
     267              :             } else {
     268            0 :                 if (dlt_daemon_send_message_overflow(daemon, daemon_local, verbose) == DLT_DAEMON_ERROR_OK) {
     269            0 :                     dlt_vlog(
     270              :                         LOG_WARNING, "%s: %u messages discarded! Now able to send messages to the client.\n", __func__,
     271              :                         daemon->overflow_counter);
     272            0 :                     daemon->overflow_counter = 0;
     273            0 :                     sent_message_overflow_cnt--;
     274              :                 }
     275              :             }
     276              :         }
     277              :     }
     278              : 
     279              :     return DLT_DAEMON_ERROR_OK;
     280              : }
     281              : 
     282            0 : int dlt_daemon_client_send_v2(
     283              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, void* storage_header, int storage_header_size,
     284              :     void* data1, int size1, void* data2, int size2, int verbose)
     285              : {
     286              :     int sent, ret;
     287              :     int ret_logstorage = 0;
     288              :     static int sent_message_overflow_cnt = 0;
     289              : 
     290            0 :     if ((daemon == NULL) || (daemon_local == NULL)) {
     291            0 :         dlt_vlog(LOG_ERR, "%s: Invalid arguments\n", __func__);
     292            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     293              :     }
     294              : 
     295            0 :     if ((sock != DLT_DAEMON_SEND_TO_ALL) && (sock != DLT_DAEMON_SEND_FORCE)) {
     296              :         /* Send message to specific socket */
     297            0 :         if (isatty(sock)) {
     298            0 :             if ((ret = dlt_daemon_serial_send(sock, data1, size1, data2, size2, (char)daemon->sendserialheader))) {
     299            0 :                 dlt_vlog(LOG_WARNING, "%s: serial send dlt message failed\n", __func__);
     300            0 :                 return ret;
     301              :             }
     302              :         } else {
     303            0 :             if ((ret = dlt_daemon_socket_send(sock, data1, size1, data2, size2, (char)daemon->sendserialheader))) {
     304            0 :                 dlt_vlog(LOG_WARNING, "%s: socket send dlt message failed\n", __func__);
     305            0 :                 return ret;
     306              :             }
     307              :         }
     308              : 
     309            0 :         return DLT_DAEMON_ERROR_OK;
     310              :     }
     311              : 
     312              :     /* write message to offline trace */
     313              :     /* In the SEND_BUFFER state we must skip offline tracing because the offline traces */
     314              :     /* are going without buffering directly to the offline trace. Thus we have to filter out */
     315              :     /* the traces that are coming from the buffer. */
     316            0 :     if ((sock != DLT_DAEMON_SEND_FORCE) && (daemon->state != DLT_DAEMON_STATE_SEND_BUFFER)) {
     317            0 :         if (((daemon->mode == DLT_USER_MODE_INTERNAL) || (daemon->mode == DLT_USER_MODE_BOTH))
     318            0 :             && daemon_local->flags.offlineTraceDirectory[0]) {
     319              :             /* To update for v2*/
     320            0 :             if (dlt_offline_trace_write(
     321              :                     &(daemon_local->offlineTrace), storage_header, storage_header_size, data1, size1, data2, size2)) {
     322              :                 static int error_dlt_offline_trace_write_failed = 0;
     323              : 
     324            0 :                 if (!error_dlt_offline_trace_write_failed) {
     325            0 :                     dlt_vlog(LOG_ERR, "%s: dlt_offline_trace_write failed!\n", __func__);
     326            0 :                     error_dlt_offline_trace_write_failed = 1;
     327              :                 }
     328              : 
     329              :                 /*return DLT_DAEMON_ERROR_WRITE_FAILED; */
     330              :             }
     331              :         }
     332              : 
     333              :         /* write messages to offline logstorage only if there is an extended header set
     334              :          * this need to be checked because the function is dlt_daemon_client_send is called by
     335              :          * newly introduced dlt_daemon_log_internal */
     336              :         /* To Update to dlt_daemon_logstorage_write_v2*/
     337            0 :         if (daemon_local->flags.offlineLogstorageMaxDevices > 0)
     338            0 :             ret_logstorage = dlt_daemon_logstorage_write(
     339              :                 daemon, &daemon_local->flags, storage_header, storage_header_size, data1, size1, data2, size2);
     340              :     }
     341              : 
     342              :     /* send messages to daemon socket */
     343            0 :     if ((daemon->mode == DLT_USER_MODE_EXTERNAL) || (daemon->mode == DLT_USER_MODE_BOTH)) {
     344              : #ifdef UDP_CONNECTION_SUPPORT
     345              :         if (daemon_local->UDPConnectionSetup == MULTICAST_CONNECTION_ENABLED) {
     346              :             /* Forward message to network client if network routing is not disabled */
     347              :             if (ret_logstorage != 1) {
     348              :                 dlt_daemon_udp_dltmsg_multicast(data1, size1, data2, size2, verbose);
     349              :             }
     350              :         }
     351              : 
     352              : #endif
     353              : 
     354            0 :         if ((sock == DLT_DAEMON_SEND_FORCE) || (daemon->state == DLT_DAEMON_STATE_SEND_DIRECT)) {
     355              :             /* Forward message to network client if network routing is not disabled */
     356            0 :             if (ret_logstorage != 1) {
     357            0 :                 sent = dlt_daemon_client_send_all_multiple(daemon, daemon_local, data1, size1, data2, size2, verbose);
     358              : 
     359            0 :                 if ((sock == DLT_DAEMON_SEND_FORCE) && !sent) {
     360              :                     return DLT_DAEMON_ERROR_SEND_FAILED;
     361              :                 }
     362              :             }
     363              :         }
     364              :     }
     365              : 
     366              :     /* Message was not sent to client, so store it in client ringbuffer */
     367            0 :     if ((sock != DLT_DAEMON_SEND_FORCE)
     368            0 :         && ((daemon->state == DLT_DAEMON_STATE_BUFFER) || (daemon->state == DLT_DAEMON_STATE_SEND_BUFFER)
     369            0 :             || (daemon->state == DLT_DAEMON_STATE_BUFFER_FULL))) {
     370            0 :         if (daemon->state != DLT_DAEMON_STATE_BUFFER_FULL) {
     371              :             /* Store message in history buffer */
     372            0 :             ret = dlt_buffer_push3(
     373              :                 &(daemon->client_ringbuffer), data1, (unsigned int)size1, data2, (unsigned int)size2, 0, 0);
     374            0 :             if (ret < DLT_RETURN_OK) {
     375            0 :                 dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_BUFFER_FULL);
     376              :             }
     377              :         }
     378            0 :         if (daemon->state == DLT_DAEMON_STATE_BUFFER_FULL) {
     379            0 :             daemon->overflow_counter += 1;
     380            0 :             if (daemon->overflow_counter == 1)
     381            0 :                 dlt_vlog(LOG_INFO, "%s: Buffer is full! Messages will be discarded.\n", __func__);
     382              : 
     383            0 :             return DLT_DAEMON_ERROR_BUFFER_FULL;
     384              :         }
     385              :     } else {
     386            0 :         if ((daemon->overflow_counter > 0) && (daemon_local->client_connections > 0)) {
     387            0 :             sent_message_overflow_cnt++;
     388            0 :             if (sent_message_overflow_cnt >= 2) {
     389            0 :                 sent_message_overflow_cnt--;
     390              :             } else {
     391            0 :                 if (dlt_daemon_send_message_overflow_v2(daemon, daemon_local, verbose) == DLT_DAEMON_ERROR_OK) {
     392            0 :                     dlt_vlog(
     393              :                         LOG_WARNING, "%s: %u messages discarded! Now able to send messages to the client.\n", __func__,
     394              :                         daemon->overflow_counter);
     395            0 :                     daemon->overflow_counter = 0;
     396            0 :                     sent_message_overflow_cnt--;
     397              :                 }
     398              :             }
     399              :         }
     400              :     }
     401              : 
     402              :     return DLT_DAEMON_ERROR_OK;
     403              : }
     404              : 
     405          441 : int dlt_daemon_client_send_message_to_all_client(DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
     406              : {
     407              :     static char text[DLT_DAEMON_TEXTSIZE];
     408              :     char* ecu_ptr = NULL;
     409              : 
     410          441 :     PRINT_FUNCTION_VERBOSE(verbose);
     411              : 
     412          441 :     if ((daemon == NULL) || (daemon_local == NULL)) {
     413            0 :         dlt_vlog(LOG_ERR, "%s: invalid arguments\n", __func__);
     414            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     415              :     }
     416              : 
     417              :     /* set overwrite ecu id */
     418          441 :     if ((daemon_local->flags.evalue[0])
     419            0 :         && (strncmp(daemon_local->msg.headerextra.ecu, DLT_DAEMON_ECU_ID, DLT_ID_SIZE) == 0)) {
     420              :         /* Set header extra parameters */
     421            0 :         dlt_set_id(daemon_local->msg.headerextra.ecu, daemon->ecuid);
     422              : 
     423              :         /*msg.headerextra.seid = 0; */
     424            0 :         if (dlt_message_set_extraparameters(&(daemon_local->msg), 0)) {
     425            0 :             dlt_vlog(LOG_WARNING, "%s: failed to set message extra parameters.\n", __func__);
     426            0 :             return DLT_DAEMON_ERROR_UNKNOWN;
     427              :         }
     428              : 
     429              :         /* Correct value of timestamp, this was changed by dlt_message_set_extraparameters() */
     430            0 :         daemon_local->msg.headerextra.tmsp = DLT_BETOH_32(daemon_local->msg.headerextra.tmsp);
     431              :     }
     432              : 
     433              :     /* prepare storage header */
     434          441 :     if (DLT_IS_HTYP_WEID(daemon_local->msg.standardheader->htyp)) {
     435          441 :         ecu_ptr = daemon_local->msg.headerextra.ecu;
     436              :     } else {
     437            0 :         ecu_ptr = daemon->ecuid;
     438              :     }
     439              : 
     440          441 :     if (dlt_set_storageheader(daemon_local->msg.storageheader, ecu_ptr)) {
     441            0 :         dlt_vlog(
     442              :             LOG_WARNING, "%s: failed to set storage header with header type: 0x%x\n", __func__,
     443            0 :             daemon_local->msg.standardheader->htyp);
     444            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     445              :     }
     446              : 
     447              :     /* if no filter set or filter is matching display message */
     448          441 :     if (daemon_local->flags.xflag) {
     449            0 :         if (DLT_RETURN_OK != dlt_message_print_hex(&(daemon_local->msg), text, DLT_DAEMON_TEXTSIZE, verbose))
     450            0 :             dlt_log(LOG_WARNING, "dlt_message_print_hex() failed!\n");
     451          441 :     } else if (daemon_local->flags.aflag) {
     452            0 :         if (DLT_RETURN_OK != dlt_message_print_ascii(&(daemon_local->msg), text, DLT_DAEMON_TEXTSIZE, verbose))
     453            0 :             dlt_log(LOG_WARNING, "dlt_message_print_ascii() failed!\n");
     454          441 :     } else if (daemon_local->flags.sflag) {
     455            0 :         if (DLT_RETURN_OK != dlt_message_print_header(&(daemon_local->msg), text, DLT_DAEMON_TEXTSIZE, verbose))
     456            0 :             dlt_log(LOG_WARNING, "dlt_message_print_header() failed!\n");
     457              :     }
     458              : 
     459              :     /* send message to client or write to log file */
     460          441 :     return dlt_daemon_client_send(
     461          441 :         DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, daemon_local->msg.headerbuffer, sizeof(DltStorageHeader),
     462              :         daemon_local->msg.headerbuffer + sizeof(DltStorageHeader),
     463          441 :         (int)(daemon_local->msg.headersize - (int32_t)sizeof(DltStorageHeader)), daemon_local->msg.databuffer,
     464          441 :         (int)daemon_local->msg.datasize, verbose);
     465              : }
     466              : 
     467            0 : int dlt_daemon_client_send_message_to_all_client_v2(DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
     468              : {
     469              :     static char text[DLT_DAEMON_TEXTSIZE];
     470              :     char* ecu_ptr = NULL;
     471              :     uint8_t ecu_len = 0;
     472              :     uint32_t temp_extended_size = 0;
     473            0 :     uint8_t* temp_buffer = (uint8_t*)malloc((size_t)daemon_local->msgv2.headersizev2);
     474            0 :     if (temp_buffer == NULL) {
     475              :         return DLT_RETURN_ERROR;
     476              :     }
     477              :     /* Store headerbuffer in temp buffer BEFORE replacing the original buffer */
     478            0 :     memcpy(temp_buffer, daemon_local->msgv2.headerbufferv2, (size_t)daemon_local->msgv2.headersizev2);
     479              : 
     480            0 :     PRINT_FUNCTION_VERBOSE(verbose);
     481              : 
     482            0 :     if ((daemon == NULL) || (daemon_local == NULL)) {
     483            0 :         dlt_vlog(LOG_ERR, "%s: invalid arguments\n", __func__);
     484            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     485              :     }
     486              : 
     487            0 :     temp_extended_size = daemon_local->msgv2.extendedheadersizev2;
     488              :     /* set overwrite ecu id */
     489            0 :     if ((daemon_local->flags.evalue[0])
     490            0 :         && (strncmp(
     491            0 :                 daemon_local->msgv2.extendedheaderv2.ecid, DLT_DAEMON_ECU_ID,
     492            0 :                 daemon_local->msgv2.extendedheaderv2.ecidlen)
     493              :             == 0)) {
     494            0 :         daemon_local->msgv2.extendedheaderv2.ecidlen = daemon->ecuid2len;
     495            0 :         dlt_set_id_v2(daemon_local->msgv2.extendedheaderv2.ecid, daemon->ecuid2, daemon->ecuid2len);
     496              : 
     497            0 :         daemon_local->msgv2.extendedheaderv2.seid = 0;
     498              : 
     499              :         /* Get new extended header size and header size, update header buffer*/
     500            0 :         daemon_local->msgv2.extendedheadersizev2 = dlt_message_get_extendedparameters_size_v2(&(daemon_local->msgv2));
     501              :     }
     502              : 
     503              :     /* Save old storage header size before we recalculate it */
     504            0 :     uint32_t old_storage_size = daemon_local->msgv2.storageheadersizev2;
     505              : 
     506              :     /* prepare storage header */
     507            0 :     if (DLT_IS_HTYP2_WEID(daemon_local->msgv2.baseheaderv2->htyp2)) {
     508            0 :         ecu_ptr = daemon_local->msgv2.extendedheaderv2.ecid;
     509            0 :         ecu_len = daemon_local->msgv2.extendedheaderv2.ecidlen;
     510              :     } else {
     511            0 :         ecu_ptr = daemon->ecuid2;
     512            0 :         ecu_len = daemon->ecuid2len;
     513              :     }
     514              : 
     515            0 :     daemon_local->msgv2.storageheadersizev2 = (uint32_t)(STORAGE_HEADER_V2_FIXED_SIZE + ecu_len);
     516              : 
     517            0 :     if (dlt_set_storageheader_v2(&(daemon_local->msgv2.storageheaderv2), ecu_len, ecu_ptr)) {
     518            0 :         dlt_vlog(
     519              :             LOG_WARNING, "%s: failed to set storage header with header type: 0x%x\n", __func__,
     520            0 :             daemon_local->msg.standardheader->htyp);
     521            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     522              :     }
     523              : 
     524            0 :     daemon_local->msgv2.headersizev2 =
     525            0 :         (int32_t)((uint32_t)daemon_local->msgv2.headersizev2 - temp_extended_size
     526            0 :                   + daemon_local->msgv2.extendedheadersizev2 + daemon_local->msgv2.storageheadersizev2);
     527              : 
     528              :     /* allocate new header buffer, copy cached header parts into it, then replace the old buffer */
     529            0 :     uint8_t* new_headerbufferv2 = (uint8_t*)malloc((size_t)daemon_local->msgv2.headersizev2);
     530            0 :     if (new_headerbufferv2 == NULL) {
     531            0 :         free(temp_buffer);
     532            0 :         return DLT_RETURN_ERROR;
     533              :     }
     534              : 
     535            0 :     if (dlt_message_set_storageparameters_v2(&(daemon_local->msgv2), 0) != DLT_RETURN_OK) {
     536            0 :         free(temp_buffer);
     537            0 :         free(new_headerbufferv2);
     538            0 :         return DLT_RETURN_ERROR;
     539              :     }
     540              : 
     541              :     /* Copy base header + base header extra from temp buffer (skip old storage header) */
     542            0 :     memcpy(
     543            0 :         new_headerbufferv2 + daemon_local->msgv2.storageheadersizev2, temp_buffer + old_storage_size,
     544            0 :         (daemon_local->msgv2.baseheadersizev2 + daemon_local->msgv2.baseheaderextrasizev2));
     545              : 
     546              :     /* Copy extended header from temp buffer */
     547            0 :     uint32_t old_extended_offset =
     548            0 :         old_storage_size + daemon_local->msgv2.baseheadersizev2 + daemon_local->msgv2.baseheaderextrasizev2;
     549            0 :     uint32_t new_extended_offset = daemon_local->msgv2.storageheadersizev2 + daemon_local->msgv2.baseheadersizev2
     550              :                                    + daemon_local->msgv2.baseheaderextrasizev2;
     551            0 :     memcpy(new_headerbufferv2 + new_extended_offset, temp_buffer + old_extended_offset, temp_extended_size);
     552              : 
     553            0 :     free(temp_buffer);
     554              : 
     555              :     /* free the original header buffer and install the new one */
     556            0 :     free(daemon_local->msgv2.headerbufferv2);
     557            0 :     daemon_local->msgv2.headerbufferv2 = new_headerbufferv2;
     558              : 
     559              :     /* Update baseheaderv2 pointer to point into the new buffer */
     560            0 :     daemon_local->msgv2.baseheaderv2 = (DltBaseHeaderV2*)(new_headerbufferv2 + daemon_local->msgv2.storageheadersizev2);
     561              : 
     562              :     /* Re-parse extended parameters from the new buffer to update pointers */
     563            0 :     DltHtyp2ContentType msgcontent = daemon_local->msgv2.baseheaderv2->htyp2 & MSGCONTENT_MASK;
     564            0 :     if (dlt_message_get_extendedparameters_from_recievedbuffer_v2(
     565              :             &(daemon_local->msgv2), new_headerbufferv2 + daemon_local->msgv2.storageheadersizev2, msgcontent)
     566              :         != DLT_RETURN_OK) {
     567            0 :         dlt_vlog(LOG_WARNING, "%s: failed to get message extended parameters.\n", __func__);
     568            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     569              :     }
     570              : 
     571            0 :     if (dlt_message_set_extendedparameters_v2(&(daemon_local->msgv2))) {
     572            0 :         dlt_vlog(LOG_WARNING, "%s: failed to set message extended parameters.\n", __func__);
     573            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     574              :     }
     575              : 
     576              :     /* if no filter set or filter is matching display message */
     577            0 :     if (daemon_local->flags.xflag) {
     578            0 :         if (DLT_RETURN_OK != dlt_message_print_hex_v2(&(daemon_local->msgv2), text, DLT_DAEMON_TEXTSIZE, verbose))
     579            0 :             dlt_log(LOG_WARNING, "dlt_message_print_hex() failed!\n");
     580            0 :     } else if (daemon_local->flags.aflag) {
     581            0 :         if (DLT_RETURN_OK != dlt_message_print_ascii_v2(&(daemon_local->msgv2), text, DLT_DAEMON_TEXTSIZE, verbose))
     582            0 :             dlt_log(LOG_WARNING, "dlt_message_print_ascii() failed!\n");
     583            0 :     } else if (daemon_local->flags.sflag) {
     584            0 :         if (DLT_RETURN_OK != dlt_message_print_header_v2(&(daemon_local->msgv2), text, DLT_DAEMON_TEXTSIZE, verbose))
     585            0 :             dlt_log(LOG_WARNING, "dlt_message_print_header() failed!\n");
     586              :     }
     587              : 
     588              :     /* send message to client or write to log file */
     589            0 :     return dlt_daemon_client_send_v2(
     590              :         DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, daemon_local->msgv2.headerbufferv2,
     591              :         (int)daemon_local->msgv2.storageheadersizev2,
     592            0 :         daemon_local->msgv2.headerbufferv2 + daemon_local->msgv2.storageheadersizev2,
     593            0 :         (int)(daemon_local->msgv2.headersizev2 - (int32_t)daemon_local->msgv2.storageheadersizev2),
     594            0 :         daemon_local->msgv2.databuffer, (int)daemon_local->msgv2.datasize, verbose);
     595              : }
     596              : 
     597            2 : int dlt_daemon_client_send_control_message(
     598              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, char* apid, char* ctid, int verbose)
     599              : {
     600              :     int ret;
     601              :     int32_t len;
     602              : 
     603            2 :     PRINT_FUNCTION_VERBOSE(verbose);
     604              : 
     605            2 :     if ((daemon == 0) || (msg == 0) || (apid == 0) || (ctid == 0))
     606              :         return DLT_DAEMON_ERROR_UNKNOWN;
     607              : 
     608              :     /* prepare storage header */
     609            2 :     msg->storageheader = (DltStorageHeader*)msg->headerbuffer;
     610              : 
     611            2 :     if (dlt_set_storageheader(msg->storageheader, daemon->ecuid) == DLT_RETURN_ERROR)
     612              :         return DLT_DAEMON_ERROR_UNKNOWN;
     613              : 
     614              :     /* prepare standard header */
     615            2 :     msg->standardheader = (DltStandardHeader*)(msg->headerbuffer + sizeof(DltStorageHeader));
     616            2 :     msg->standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_UEH | DLT_HTYP_PROTOCOL_VERSION1;
     617              : 
     618              : #if (BYTE_ORDER == BIG_ENDIAN)
     619              :     msg->standardheader->htyp = (msg->standardheader->htyp | DLT_HTYP_MSBF);
     620              : #endif
     621              : 
     622            2 :     msg->standardheader->mcnt = 0;
     623              : 
     624              :     /* Set header extra parameters */
     625            2 :     dlt_set_id(msg->headerextra.ecu, daemon->ecuid);
     626              : 
     627              :     /*msg->headerextra.seid = 0; */
     628              : 
     629            2 :     msg->headerextra.tmsp = dlt_uptime();
     630              : 
     631            2 :     dlt_message_set_extraparameters(msg, verbose);
     632              : 
     633              :     /* prepare extended header */
     634            2 :     msg->extendedheader = (DltExtendedHeader*)(msg->headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
     635            2 :                                                + DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp));
     636            2 :     msg->extendedheader->msin = DLT_MSIN_CONTROL_RESPONSE;
     637              : 
     638            2 :     msg->extendedheader->noar = 1; /* number of arguments */
     639              : 
     640            2 :     if (strcmp(apid, "") == 0)
     641            2 :         dlt_set_id(msg->extendedheader->apid, DLT_DAEMON_CTRL_APID); /* application id */
     642              :     else
     643            0 :         dlt_set_id(msg->extendedheader->apid, apid);
     644              : 
     645            2 :     if (strcmp(ctid, "") == 0)
     646            2 :         dlt_set_id(msg->extendedheader->ctid, DLT_DAEMON_CTRL_CTID); /* context id */
     647              :     else
     648            0 :         dlt_set_id(msg->extendedheader->ctid, ctid);
     649              : 
     650              :     /* prepare length information */
     651            2 :     msg->headersize = (int32_t)((size_t)sizeof(DltStorageHeader) + (size_t)sizeof(DltStandardHeader)
     652              :                                 + (size_t)sizeof(DltExtendedHeader)
     653            2 :                                 + (size_t)DLT_STANDARD_HEADER_EXTRA_SIZE(msg->standardheader->htyp));
     654              : 
     655            2 :     len = (int32_t)((size_t)msg->headersize - (size_t)sizeof(DltStorageHeader) + (size_t)msg->datasize);
     656              : 
     657            2 :     if (len > UINT16_MAX) {
     658            0 :         dlt_log(LOG_WARNING, "Huge control message discarded!\n");
     659            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
     660              :     }
     661              : 
     662            2 :     msg->standardheader->len = DLT_HTOBE_16(((uint16_t)len));
     663              : 
     664            2 :     if ((ret = dlt_daemon_client_send(
     665              :              sock, daemon, daemon_local, msg->headerbuffer, sizeof(DltStorageHeader),
     666              :              msg->headerbuffer + sizeof(DltStorageHeader), (int)(msg->headersize - (int32_t)sizeof(DltStorageHeader)),
     667            2 :              msg->databuffer, (int)msg->datasize, verbose))) {
     668            0 :         dlt_log(LOG_DEBUG, "dlt_daemon_control_send_control_message: DLT message send to all failed!.\n");
     669            0 :         return ret;
     670              :     }
     671              : 
     672              :     return DLT_DAEMON_ERROR_OK;
     673              : }
     674              : 
     675            0 : int dlt_daemon_client_send_control_message_v2(
     676              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, char* apid, char* ctid, int verbose)
     677              : {
     678              :     int ret;
     679              :     int32_t len;
     680              :     uint8_t appidlen;
     681              :     uint8_t ctxidlen;
     682              :     char ecid_buf[DLT_V2_ID_SIZE];
     683              :     char apid_buf[DLT_V2_ID_SIZE];
     684              :     char ctid_buf[DLT_V2_ID_SIZE];
     685              : 
     686            0 :     PRINT_FUNCTION_VERBOSE(verbose);
     687              : 
     688            0 :     if ((daemon == 0) || (msg == 0) || (apid == NULL) || (ctid == NULL))
     689              :         return DLT_DAEMON_ERROR_UNKNOWN;
     690              : 
     691              :     /* prepare storage header */
     692              : 
     693              :     if (apid == NULL) {
     694              :         appidlen = (uint8_t)strlen(DLT_DAEMON_CTRL_APID);
     695              :     } else {
     696            0 :         appidlen = (uint8_t)strlen(apid);
     697              :     }
     698              : 
     699              :     if (ctid == NULL) {
     700              :         ctxidlen = (uint8_t)strlen(DLT_DAEMON_CTRL_CTID);
     701              :     } else {
     702            0 :         ctxidlen = (uint8_t)strlen(ctid);
     703              :     }
     704              : 
     705            0 :     msg->storageheadersizev2 = (uint32_t)(STORAGE_HEADER_V2_FIXED_SIZE + (daemon->ecuid2len));
     706            0 :     msg->baseheadersizev2 = BASE_HEADER_V2_FIXED_SIZE;
     707            0 :     msg->baseheaderextrasizev2 = (int32_t)dlt_message_get_extraparameters_size_v2(DLT_CONTROL_MSG);
     708            0 :     msg->extendedheadersizev2 = (uint32_t)((daemon->ecuid2len) + 1 + appidlen + 1 + ctxidlen + 1);
     709              : 
     710            0 :     msg->headersizev2 = (int32_t)(msg->storageheadersizev2 + msg->baseheadersizev2 + msg->baseheaderextrasizev2
     711            0 :                                   + msg->extendedheadersizev2);
     712              : 
     713            0 :     if (msg->headerbufferv2 != NULL) {
     714            0 :         free(msg->headerbufferv2);
     715              :         msg->headerbufferv2 = NULL;
     716              :     }
     717              : 
     718            0 :     msg->headerbufferv2 = (uint8_t*)malloc((size_t)msg->headersizev2);
     719              : 
     720            0 :     if (dlt_set_storageheader_v2(&(msg->storageheaderv2), daemon->ecuid2len, daemon->ecuid2) == DLT_RETURN_ERROR)
     721              :         return DLT_DAEMON_ERROR_UNKNOWN;
     722              : 
     723            0 :     if (dlt_message_set_storageparameters_v2(msg, 0) != DLT_RETURN_OK) {
     724              :         return DLT_RETURN_ERROR;
     725              :     }
     726              : 
     727              :     /* prepare base header */
     728            0 :     msg->baseheaderv2 = (DltBaseHeaderV2*)(msg->headerbufferv2 + msg->storageheadersizev2);
     729              : 
     730              :     msg->baseheaderv2->htyp2 = DLT_HTYP2_PROTOCOL_VERSION2;
     731              :     msg->baseheaderv2->htyp2 |= DLT_CONTROL_MSG;
     732              :     msg->baseheaderv2->htyp2 |= DLT_HTYP2_WEID;
     733            0 :     msg->baseheaderv2->htyp2 |= DLT_HTYP2_WACID;
     734            0 :     msg->baseheaderv2->mcnt = 0;
     735              : 
     736              :     /* Fill base header conditional parameters */
     737            0 :     msg->headerextrav2.msin = DLT_MSIN_CONTROL_RESPONSE;
     738            0 :     msg->headerextrav2.noar = 1; /* number of arguments */
     739            0 :     memset(msg->headerextrav2.seconds, 0, 5);
     740            0 :     msg->headerextrav2.nanoseconds = 0;
     741              : #if defined(__WIN32__) || defined(_MSC_VER)
     742              :     time_t t = time(NULL);
     743              :     if (t == -1) {
     744              :         uint32_t tcnt = (uint32_t)(GetTickCount()); /* GetTickCount() in 10 ms resolution */
     745              :         tcnt_seconds = tcnt / 100;
     746              :         tcnt_ns = (tcnt - (tcnt * 100)) * 10000;
     747              :         msg->headerextrav2.seconds[0] = (tcnt_seconds >> 32) & 0xFF;
     748              :         msg->headerextrav2.seconds[1] = (tcnt_seconds >> 24) & 0xFF;
     749              :         msg->headerextrav2.seconds[2] = (tcnt_seconds >> 16) & 0xFF;
     750              :         msg->headerextrav2.seconds[3] = (tcnt_seconds >> 8) & 0xFF;
     751              :         msg->headerextrav2.seconds[4] = tcnt_seconds & 0xFF;
     752              :         if (ts.tv_nsec < 0x3B9ACA00) {
     753              :             msg->headerextrav2.nanoseconds = tcnt_ns;
     754              :         }
     755              :     } else {
     756              :         msg->headerextrav2.seconds[0] = (t >> 32) & 0xFF;
     757              :         msg->headerextrav2.seconds[1] = (t >> 24) & 0xFF;
     758              :         msg->headerextrav2.seconds[2] = (t >> 16) & 0xFF;
     759              :         msg->headerextrav2.seconds[3] = (t >> 8) & 0xFF;
     760              :         msg->headerextrav2.seconds[4] = t & 0xFF;
     761              :         msg->headerextrav2.nanoseconds |= 0x80000000;
     762              :     }
     763              : #else
     764              :     struct timespec ts;
     765            0 :     if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
     766            0 :         msg->headerextrav2.seconds[0] = (uint8_t)(((uint64_t)ts.tv_sec >> 32) & 0xFF);
     767            0 :         msg->headerextrav2.seconds[1] = (uint8_t)((ts.tv_sec >> 24) & 0xFF);
     768            0 :         msg->headerextrav2.seconds[2] = (uint8_t)((ts.tv_sec >> 16) & 0xFF);
     769            0 :         msg->headerextrav2.seconds[3] = (uint8_t)((ts.tv_sec >> 8) & 0xFF);
     770            0 :         msg->headerextrav2.seconds[4] = (uint8_t)(ts.tv_sec & 0xFF);
     771            0 :         if (ts.tv_nsec < 0x3B9ACA00) {
     772            0 :             msg->headerextrav2.nanoseconds = (uint32_t)ts.tv_nsec; /* value is long */
     773              :         }
     774            0 :     } else if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
     775            0 :         msg->headerextrav2.seconds[0] = (uint8_t)(((uint64_t)ts.tv_sec >> 32) & 0xFF);
     776            0 :         msg->headerextrav2.seconds[1] = (uint8_t)((ts.tv_sec >> 24) & 0xFF);
     777            0 :         msg->headerextrav2.seconds[2] = (uint8_t)((ts.tv_sec >> 16) & 0xFF);
     778            0 :         msg->headerextrav2.seconds[3] = (uint8_t)((ts.tv_sec >> 8) & 0xFF);
     779            0 :         msg->headerextrav2.seconds[4] = (uint8_t)(ts.tv_sec & 0xFF);
     780            0 :         if (ts.tv_nsec < 0x3B9ACA00) {
     781            0 :             msg->headerextrav2.nanoseconds = (uint32_t)ts.tv_nsec; /* value is long */
     782              :         }
     783            0 :         msg->headerextrav2.nanoseconds |= 0x80000000;
     784              :     }
     785              : #endif
     786              : 
     787              :     /* Copy header extra parameters to headerbuffer */
     788            0 :     if (dlt_message_set_extraparameters_v2(msg, 0) == DLT_RETURN_ERROR) {
     789            0 :         dlt_message_free_v2(msg, 0);
     790            0 :         return DLT_RETURN_ERROR;
     791              :     }
     792              : 
     793              :     /* Fill out extended header */
     794            0 :     if (DLT_IS_HTYP2_WEID(msg->baseheaderv2->htyp2)) {
     795            0 :         msg->extendedheaderv2.ecidlen = daemon->ecuid2len;
     796            0 :         if (msg->extendedheaderv2.ecidlen > 0) {
     797            0 :             dlt_set_id_v2(ecid_buf, daemon->ecuid2, msg->extendedheaderv2.ecidlen);
     798            0 :             msg->extendedheaderv2.ecid = ecid_buf;
     799              :         } else {
     800            0 :             msg->extendedheaderv2.ecid = NULL;
     801              :         }
     802              :     }
     803              : 
     804            0 :     if (DLT_IS_HTYP2_WACID(msg->baseheaderv2->htyp2)) {
     805            0 :         msg->extendedheaderv2.apidlen = appidlen;
     806            0 :         if (msg->extendedheaderv2.apidlen > 0) {
     807              :             if (apid == NULL) {
     808              :                 dlt_set_id_v2(apid_buf, DLT_DAEMON_CTRL_APID, msg->extendedheaderv2.apidlen);
     809              :             } else {
     810            0 :                 dlt_set_id_v2(apid_buf, apid, msg->extendedheaderv2.apidlen);
     811              :             }
     812            0 :             msg->extendedheaderv2.apid = apid_buf;
     813              :         } else {
     814            0 :             msg->extendedheaderv2.apid = NULL;
     815              :         }
     816              : 
     817            0 :         msg->extendedheaderv2.ctidlen = ctxidlen;
     818            0 :         if (msg->extendedheaderv2.ctidlen > 0) {
     819              :             if (ctid == NULL) {
     820              :                 dlt_set_id_v2(ctid_buf, DLT_DAEMON_CTRL_CTID, msg->extendedheaderv2.ctidlen);
     821              :             } else {
     822            0 :                 dlt_set_id_v2(ctid_buf, ctid, msg->extendedheaderv2.ctidlen);
     823              :             }
     824            0 :             msg->extendedheaderv2.ctid = ctid_buf;
     825              :         } else {
     826            0 :             msg->extendedheaderv2.ctid = NULL;
     827              :         }
     828              :     }
     829              : 
     830            0 :     if (dlt_message_set_extendedparameters_v2(msg) != DLT_RETURN_OK) {
     831            0 :         dlt_message_free_v2(msg, 0);
     832            0 :         return DLT_RETURN_ERROR;
     833              :     }
     834              : 
     835            0 :     len = (int32_t)(msg->headersizev2 - (int32_t)msg->storageheadersizev2 + msg->datasize);
     836              : 
     837            0 :     if (len > UINT16_MAX) {
     838            0 :         dlt_vlog(LOG_ERR, "%s: Critical: Huge injection message discarded!\n", __func__);
     839            0 :         dlt_message_free_v2(msg, 0);
     840            0 :         return DLT_RETURN_ERROR;
     841              :     }
     842              : 
     843            0 :     msg->baseheaderv2->len = DLT_HTOBE_16((uint16_t)len);
     844              : 
     845            0 :     if ((ret = dlt_daemon_client_send_v2(
     846              :              sock, daemon, daemon_local, msg->headerbufferv2, (int)msg->storageheadersizev2,
     847            0 :              msg->headerbufferv2 + (int)msg->storageheadersizev2,
     848            0 :              (int)(msg->headersizev2 - (int32_t)msg->storageheadersizev2), msg->databuffer, (int)msg->datasize,
     849              :              verbose))) {
     850            0 :         dlt_log(LOG_DEBUG, "dlt_daemon_control_send_control_message: DLT message send to all failed!.\n");
     851            0 :         return ret;
     852              :     }
     853              : 
     854              :     return DLT_DAEMON_ERROR_OK;
     855              : }
     856              : 
     857            1 : int dlt_daemon_client_process_control(
     858              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
     859              : {
     860              :     uint32_t id, id_tmp = 0;
     861              :     DltStandardHeaderExtra extra;
     862              : 
     863            1 :     PRINT_FUNCTION_VERBOSE(verbose);
     864              : 
     865            1 :     if ((daemon == NULL) || (daemon_local == NULL) || (msg == NULL))
     866              :         return -1;
     867              : 
     868            1 :     if (msg->datasize < (int32_t)sizeof(uint32_t))
     869              :         return -1;
     870              : 
     871            1 :     extra = msg->headerextra;
     872              : 
     873              :     /* TODO: Add length of extra.ecu to dlt_gateway_forward_control_message_v2 */
     874              :     /* check if the message needs to be forwarded */
     875            1 :     if (daemon_local->flags.gatewayMode == 1) {
     876            0 :         if (strncmp(daemon_local->flags.evalue, extra.ecu, DLT_ID_SIZE) != 0)
     877            0 :             return dlt_gateway_forward_control_message(&daemon_local->pGateway, daemon_local, msg, extra.ecu, verbose);
     878              :     }
     879              : 
     880            1 :     id_tmp = *((uint32_t*)(msg->databuffer));
     881            1 :     id = DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp);
     882              : 
     883            1 :     if ((id > DLT_SERVICE_ID) && (id < DLT_SERVICE_ID_CALLSW_CINJECTION)) {
     884              :         /* Control message handling */
     885            1 :         switch (id) {
     886            0 :         case DLT_SERVICE_ID_SET_LOG_LEVEL: {
     887            0 :             dlt_daemon_control_set_log_level(sock, daemon, daemon_local, msg, verbose);
     888            0 :             break;
     889              :         }
     890            0 :         case DLT_SERVICE_ID_SET_TRACE_STATUS: {
     891            0 :             dlt_daemon_control_set_trace_status(sock, daemon, daemon_local, msg, verbose);
     892            0 :             break;
     893              :         }
     894            0 :         case DLT_SERVICE_ID_GET_LOG_INFO: {
     895            0 :             dlt_daemon_control_get_log_info(sock, daemon, daemon_local, msg, verbose);
     896            0 :             break;
     897              :         }
     898            0 :         case DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL: {
     899            0 :             dlt_daemon_control_get_default_log_level(sock, daemon, daemon_local, verbose);
     900            0 :             break;
     901              :         }
     902            0 :         case DLT_SERVICE_ID_STORE_CONFIG: {
     903            0 :             if (dlt_daemon_applications_save(daemon, daemon->runtime_application_cfg, verbose) == 0) {
     904            0 :                 if (dlt_daemon_contexts_save(daemon, daemon->runtime_context_cfg, verbose) == 0) {
     905            0 :                     dlt_daemon_control_service_response(
     906              :                         sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
     907              :                 } else {
     908              :                     /* Delete saved files */
     909            0 :                     dlt_daemon_control_reset_to_factory_default(
     910              :                         daemon, daemon->runtime_application_cfg, daemon->runtime_context_cfg,
     911              :                         daemon_local->flags.contextLogLevel, daemon_local->flags.contextTraceStatus,
     912              :                         daemon_local->flags.enforceContextLLAndTS, verbose);
     913            0 :                     dlt_daemon_control_service_response(
     914              :                         sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
     915              :                 }
     916              :             } else {
     917            0 :                 dlt_daemon_control_service_response(
     918              :                     sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
     919              :             }
     920              : 
     921              :             break;
     922              :         }
     923            0 :         case DLT_SERVICE_ID_RESET_TO_FACTORY_DEFAULT: {
     924            0 :             dlt_daemon_control_reset_to_factory_default(
     925            0 :                 daemon, daemon->runtime_application_cfg, daemon->runtime_context_cfg,
     926              :                 daemon_local->flags.contextLogLevel, daemon_local->flags.contextTraceStatus,
     927              :                 daemon_local->flags.enforceContextLLAndTS, verbose);
     928            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
     929            0 :             break;
     930              :         }
     931            0 :         case DLT_SERVICE_ID_SET_COM_INTERFACE_STATUS: {
     932            0 :             dlt_daemon_control_service_response(
     933              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
     934            0 :             break;
     935              :         }
     936            0 :         case DLT_SERVICE_ID_SET_COM_INTERFACE_MAX_BANDWIDTH: {
     937            0 :             dlt_daemon_control_service_response(
     938              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
     939            0 :             break;
     940              :         }
     941            0 :         case DLT_SERVICE_ID_SET_VERBOSE_MODE: {
     942            0 :             dlt_daemon_control_service_response(
     943              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
     944            0 :             break;
     945              :         }
     946            0 :         case DLT_SERVICE_ID_SET_MESSAGE_FILTERING: {
     947            0 :             dlt_daemon_control_service_response(
     948              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
     949            0 :             break;
     950              :         }
     951            0 :         case DLT_SERVICE_ID_SET_TIMING_PACKETS: {
     952            0 :             dlt_daemon_control_set_timing_packets(sock, daemon, daemon_local, msg, verbose);
     953            0 :             break;
     954              :         }
     955            0 :         case DLT_SERVICE_ID_GET_LOCAL_TIME: {
     956              :             /* Send response with valid timestamp (TMSP) field */
     957            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
     958            0 :             break;
     959              :         }
     960            0 :         case DLT_SERVICE_ID_USE_ECU_ID: {
     961            0 :             dlt_daemon_control_service_response(
     962              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
     963            0 :             break;
     964              :         }
     965            0 :         case DLT_SERVICE_ID_USE_SESSION_ID: {
     966            0 :             dlt_daemon_control_service_response(
     967              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
     968            0 :             break;
     969              :         }
     970            0 :         case DLT_SERVICE_ID_USE_TIMESTAMP: {
     971            0 :             dlt_daemon_control_service_response(
     972              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
     973            0 :             break;
     974              :         }
     975            0 :         case DLT_SERVICE_ID_USE_EXTENDED_HEADER: {
     976            0 :             dlt_daemon_control_service_response(
     977              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
     978            0 :             break;
     979              :         }
     980            0 :         case DLT_SERVICE_ID_SET_DEFAULT_LOG_LEVEL: {
     981            0 :             dlt_daemon_control_set_default_log_level(sock, daemon, daemon_local, msg, verbose);
     982            0 :             break;
     983              :         }
     984            0 :         case DLT_SERVICE_ID_SET_DEFAULT_TRACE_STATUS: {
     985            0 :             dlt_daemon_control_set_default_trace_status(sock, daemon, daemon_local, msg, verbose);
     986            0 :             break;
     987              :         }
     988            0 :         case DLT_SERVICE_ID_GET_SOFTWARE_VERSION: {
     989            0 :             dlt_daemon_control_get_software_version(sock, daemon, daemon_local, verbose);
     990            0 :             break;
     991              :         }
     992            0 :         case DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW: {
     993            0 :             dlt_daemon_control_message_buffer_overflow(
     994              :                 sock, daemon, daemon_local, daemon->overflow_counter, "", verbose);
     995            0 :             break;
     996              :         }
     997            1 :         case DLT_SERVICE_ID_OFFLINE_LOGSTORAGE: {
     998            1 :             dlt_daemon_control_service_logstorage(sock, daemon, daemon_local, msg, verbose);
     999            1 :             break;
    1000              :         }
    1001            0 :         case DLT_SERVICE_ID_PASSIVE_NODE_CONNECT: {
    1002            0 :             dlt_daemon_control_passive_node_connect(sock, daemon, daemon_local, msg, verbose);
    1003            0 :             break;
    1004              :         }
    1005            0 :         case DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS: {
    1006            0 :             dlt_daemon_control_passive_node_connect_status(sock, daemon, daemon_local, verbose);
    1007            0 :             break;
    1008              :         }
    1009            0 :         case DLT_SERVICE_ID_SET_ALL_LOG_LEVEL: {
    1010            0 :             dlt_daemon_control_set_all_log_level(sock, daemon, daemon_local, msg, verbose);
    1011            0 :             break;
    1012              :         }
    1013            0 :         case DLT_SERVICE_ID_SET_ALL_TRACE_STATUS: {
    1014            0 :             dlt_daemon_control_set_all_trace_status(sock, daemon, daemon_local, msg, verbose);
    1015            0 :             break;
    1016              :         }
    1017            0 :         default: {
    1018            0 :             dlt_daemon_control_service_response(
    1019              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1020            0 :             break;
    1021              :         }
    1022              :         }
    1023              :     } else {
    1024              :         /* Injection handling */
    1025            0 :         dlt_daemon_control_callsw_cinjection(sock, daemon, daemon_local, msg, verbose);
    1026              :     }
    1027              : 
    1028              :     return 0;
    1029              : }
    1030              : 
    1031            0 : int dlt_daemon_client_process_control_v2(
    1032              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    1033              : {
    1034              :     uint32_t id, id_tmp = 0;
    1035              :     DltExtendedHeaderV2 extended;
    1036              : 
    1037            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    1038              : 
    1039            0 :     if ((daemon == NULL) || (daemon_local == NULL) || (msg == NULL))
    1040              :         return -1;
    1041              : 
    1042            0 :     if (msg->datasize < (int32_t)sizeof(uint32_t))
    1043              :         return -1;
    1044              : 
    1045            0 :     extended = msg->extendedheaderv2;
    1046              : 
    1047              :     /* check if the message needs to be forwarded */
    1048            0 :     if (daemon_local->flags.gatewayMode == 1) {
    1049            0 :         if (strncmp(daemon_local->flags.evalue, extended.ecid, extended.ecidlen) != 0)
    1050            0 :             return dlt_gateway_forward_control_message_v2(
    1051              :                 &daemon_local->pGateway, daemon_local, msg, extended.ecidlen, extended.ecid, verbose);
    1052              :     }
    1053              : 
    1054            0 :     id_tmp = *((uint32_t*)(msg->databuffer));
    1055              :     id = DLT_LETOH_32(id_tmp);
    1056              : 
    1057            0 :     if ((id > DLT_SERVICE_ID) && (id < DLT_SERVICE_ID_CALLSW_CINJECTION)) {
    1058              :         /* Control message handling */
    1059            0 :         switch (id) {
    1060            0 :         case DLT_SERVICE_ID_SET_LOG_LEVEL: {
    1061            0 :             dlt_daemon_control_set_log_level_v2(sock, daemon, daemon_local, msg, verbose);
    1062            0 :             break;
    1063              :         }
    1064            0 :         case DLT_SERVICE_ID_SET_TRACE_STATUS: {
    1065            0 :             dlt_daemon_control_set_trace_status_v2(sock, daemon, daemon_local, msg, verbose);
    1066            0 :             break;
    1067              :         }
    1068            0 :         case DLT_SERVICE_ID_GET_LOG_INFO: {
    1069            0 :             dlt_daemon_control_get_log_info_v2(sock, daemon, daemon_local, msg, verbose);
    1070            0 :             break;
    1071              :         }
    1072            0 :         case DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL: {
    1073            0 :             dlt_daemon_control_get_default_log_level_v2(sock, daemon, daemon_local, verbose);
    1074            0 :             break;
    1075              :         }
    1076            0 :         case DLT_SERVICE_ID_STORE_CONFIG: {
    1077            0 :             if (dlt_daemon_applications_save_v2(daemon, daemon->runtime_application_cfg, verbose) == 0) {
    1078            0 :                 if (dlt_daemon_contexts_save_v2(daemon, daemon->runtime_context_cfg, verbose) == 0) {
    1079            0 :                     dlt_daemon_control_service_response_v2(
    1080              :                         sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    1081              :                 } else {
    1082              :                     /* Delete saved files */
    1083            0 :                     dlt_daemon_control_reset_to_factory_default_v2(
    1084              :                         daemon, daemon->runtime_application_cfg, daemon->runtime_context_cfg,
    1085              :                         daemon_local->flags.contextLogLevel, daemon_local->flags.contextTraceStatus,
    1086              :                         daemon_local->flags.enforceContextLLAndTS, verbose);
    1087            0 :                     dlt_daemon_control_service_response_v2(
    1088              :                         sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1089              :                 }
    1090              :             } else {
    1091            0 :                 dlt_daemon_control_service_response_v2(
    1092              :                     sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1093              :             }
    1094              : 
    1095              :             break;
    1096              :         }
    1097            0 :         case DLT_SERVICE_ID_RESET_TO_FACTORY_DEFAULT: {
    1098            0 :             dlt_daemon_control_reset_to_factory_default_v2(
    1099            0 :                 daemon, daemon->runtime_application_cfg, daemon->runtime_context_cfg,
    1100              :                 daemon_local->flags.contextLogLevel, daemon_local->flags.contextTraceStatus,
    1101              :                 daemon_local->flags.enforceContextLLAndTS, verbose);
    1102            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    1103            0 :             break;
    1104              :         }
    1105            0 :         case DLT_SERVICE_ID_SET_COM_INTERFACE_STATUS: {
    1106            0 :             dlt_daemon_control_service_response_v2(
    1107              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1108            0 :             break;
    1109              :         }
    1110            0 :         case DLT_SERVICE_ID_SET_COM_INTERFACE_MAX_BANDWIDTH: {
    1111            0 :             dlt_daemon_control_service_response_v2(
    1112              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1113            0 :             break;
    1114              :         }
    1115            0 :         case DLT_SERVICE_ID_SET_VERBOSE_MODE: {
    1116            0 :             dlt_daemon_control_service_response_v2(
    1117              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1118            0 :             break;
    1119              :         }
    1120            0 :         case DLT_SERVICE_ID_SET_MESSAGE_FILTERING: {
    1121            0 :             dlt_daemon_control_service_response_v2(
    1122              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1123            0 :             break;
    1124              :         }
    1125            0 :         case DLT_SERVICE_ID_SET_TIMING_PACKETS: {
    1126            0 :             dlt_daemon_control_set_timing_packets_v2(sock, daemon, daemon_local, msg, verbose);
    1127            0 :             break;
    1128              :         }
    1129            0 :         case DLT_SERVICE_ID_GET_LOCAL_TIME: {
    1130              :             /* Send response with valid timestamp (TMSP) field */
    1131            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    1132            0 :             break;
    1133              :         }
    1134            0 :         case DLT_SERVICE_ID_USE_ECU_ID: {
    1135            0 :             dlt_daemon_control_service_response_v2(
    1136              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1137            0 :             break;
    1138              :         }
    1139            0 :         case DLT_SERVICE_ID_USE_SESSION_ID: {
    1140            0 :             dlt_daemon_control_service_response_v2(
    1141              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1142            0 :             break;
    1143              :         }
    1144            0 :         case DLT_SERVICE_ID_USE_TIMESTAMP: {
    1145            0 :             dlt_daemon_control_service_response_v2(
    1146              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1147            0 :             break;
    1148              :         }
    1149            0 :         case DLT_SERVICE_ID_USE_EXTENDED_HEADER: {
    1150            0 :             dlt_daemon_control_service_response_v2(
    1151              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1152            0 :             break;
    1153              :         }
    1154            0 :         case DLT_SERVICE_ID_SET_DEFAULT_LOG_LEVEL: {
    1155            0 :             dlt_daemon_control_set_default_log_level_v2(sock, daemon, daemon_local, msg, verbose);
    1156            0 :             break;
    1157              :         }
    1158            0 :         case DLT_SERVICE_ID_SET_DEFAULT_TRACE_STATUS: {
    1159            0 :             dlt_daemon_control_set_default_trace_status_v2(sock, daemon, daemon_local, msg, verbose);
    1160            0 :             break;
    1161              :         }
    1162            0 :         case DLT_SERVICE_ID_GET_SOFTWARE_VERSION: {
    1163            0 :             dlt_daemon_control_get_software_version_v2(sock, daemon, daemon_local, verbose);
    1164            0 :             break;
    1165              :         }
    1166            0 :         case DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW: {
    1167            0 :             dlt_daemon_control_message_buffer_overflow_v2(
    1168              :                 sock, daemon, daemon_local, daemon->overflow_counter, "", verbose);
    1169            0 :             break;
    1170              :         }
    1171            0 :         case DLT_SERVICE_ID_OFFLINE_LOGSTORAGE: {
    1172            0 :             dlt_daemon_control_service_logstorage_v2(sock, daemon, daemon_local, msg, verbose);
    1173            0 :             break;
    1174              :         }
    1175            0 :         case DLT_SERVICE_ID_PASSIVE_NODE_CONNECT: {
    1176            0 :             dlt_daemon_control_passive_node_connect_v2(sock, daemon, daemon_local, msg, verbose);
    1177            0 :             break;
    1178              :         }
    1179            0 :         case DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS: {
    1180            0 :             dlt_daemon_control_passive_node_connect_status_v2(sock, daemon, daemon_local, verbose);
    1181            0 :             break;
    1182              :         }
    1183            0 :         case DLT_SERVICE_ID_SET_ALL_LOG_LEVEL: {
    1184            0 :             dlt_daemon_control_set_all_log_level_v2(sock, daemon, daemon_local, msg, verbose);
    1185            0 :             break;
    1186              :         }
    1187            0 :         case DLT_SERVICE_ID_SET_ALL_TRACE_STATUS: {
    1188            0 :             dlt_daemon_control_set_all_trace_status_v2(sock, daemon, daemon_local, msg, verbose);
    1189            0 :             break;
    1190              :         }
    1191            0 :         default: {
    1192            0 :             dlt_daemon_control_service_response_v2(
    1193              :                 sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    1194            0 :             break;
    1195              :         }
    1196              :         }
    1197              :     } else {
    1198              :         /* Injection handling */
    1199            0 :         dlt_daemon_control_callsw_cinjection_v2(sock, daemon, daemon_local, msg, verbose);
    1200              :     }
    1201              : 
    1202              :     return 0;
    1203              : }
    1204              : 
    1205            0 : void dlt_daemon_control_get_software_version(int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    1206              : {
    1207              :     DltMessage msg;
    1208              :     uint32_t len;
    1209              :     DltServiceGetSoftwareVersionResponse* resp;
    1210              : 
    1211            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    1212              : 
    1213            0 :     if (daemon == 0)
    1214            0 :         return;
    1215              : 
    1216              :     /* initialise new message */
    1217            0 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR) {
    1218            0 :         dlt_daemon_control_service_response(
    1219              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_SOFTWARE_VERSION, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1220            0 :         return;
    1221              :     }
    1222              : 
    1223              :     /* prepare payload of data */
    1224            0 :     len = (uint32_t)strlen(daemon->ECUVersionString);
    1225              : 
    1226              :     /* msg.datasize = sizeof(serviceID) + sizeof(status) + sizeof(length) + len */
    1227            0 :     msg.datasize =
    1228            0 :         (int32_t)((size_t)sizeof(uint32_t) + (size_t)sizeof(uint8_t) + (size_t)sizeof(uint32_t) + (size_t)len);
    1229              : 
    1230            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    1231            0 :         free(msg.databuffer);
    1232            0 :         msg.databuffer = 0;
    1233              :     }
    1234              : 
    1235            0 :     if (msg.databuffer == 0) {
    1236            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    1237            0 :         msg.databuffersize = msg.datasize;
    1238              :     }
    1239              : 
    1240            0 :     if (msg.databuffer == 0) {
    1241            0 :         dlt_daemon_control_service_response(
    1242              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_SOFTWARE_VERSION, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1243            0 :         return;
    1244              :     }
    1245              : 
    1246              :     resp = (DltServiceGetSoftwareVersionResponse*)msg.databuffer;
    1247            0 :     resp->service_id = DLT_SERVICE_ID_GET_SOFTWARE_VERSION;
    1248            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    1249            0 :     resp->length = len;
    1250            0 :     memcpy(msg.databuffer + msg.datasize - len, daemon->ECUVersionString, len);
    1251              : 
    1252              :     /* send message */
    1253            0 :     dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, "", "", verbose);
    1254              : 
    1255              :     /* free message */
    1256            0 :     dlt_message_free(&msg, 0);
    1257              : }
    1258              : 
    1259            0 : void dlt_daemon_control_get_software_version_v2(int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    1260              : {
    1261              :     DltMessageV2 msg;
    1262              :     uint32_t len;
    1263              :     DltServiceGetSoftwareVersionResponse* resp;
    1264              : 
    1265            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    1266              : 
    1267            0 :     if (daemon == 0)
    1268            0 :         return;
    1269              : 
    1270              :     /* initialise new message */
    1271            0 :     if (dlt_message_init_v2(&msg, 0) == DLT_RETURN_ERROR) {
    1272            0 :         dlt_daemon_control_service_response_v2(
    1273              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_SOFTWARE_VERSION, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1274            0 :         return;
    1275              :     }
    1276              : 
    1277              :     /* prepare payload of data */
    1278            0 :     len = (uint32_t)strlen(daemon->ECUVersionString);
    1279              : 
    1280              :     /* msg.datasize = sizeof(serviceID) + sizeof(status) + sizeof(length) + len */
    1281            0 :     msg.datasize = (int32_t)(sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint32_t) + len);
    1282              : 
    1283            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    1284            0 :         free(msg.databuffer);
    1285            0 :         msg.databuffer = 0;
    1286              :     }
    1287              : 
    1288            0 :     if (msg.databuffer == 0) {
    1289            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    1290            0 :         msg.databuffersize = msg.datasize;
    1291              :     }
    1292              : 
    1293            0 :     if (msg.databuffer == 0) {
    1294            0 :         dlt_daemon_control_service_response_v2(
    1295              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_SOFTWARE_VERSION, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1296            0 :         return;
    1297              :     }
    1298              : 
    1299              :     resp = (DltServiceGetSoftwareVersionResponse*)msg.databuffer;
    1300            0 :     resp->service_id = DLT_SERVICE_ID_GET_SOFTWARE_VERSION;
    1301            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    1302            0 :     resp->length = len;
    1303            0 :     memcpy(msg.databuffer + msg.datasize - len, daemon->ECUVersionString, len);
    1304              : 
    1305              :     /* send message */
    1306            0 :     dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &msg, "", "", verbose);
    1307              : 
    1308              :     /* free message */
    1309            0 :     dlt_message_free_v2(&msg, 0);
    1310              : }
    1311              : 
    1312            0 : void dlt_daemon_control_get_default_log_level(int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    1313              : {
    1314              :     DltMessage msg;
    1315              :     DltServiceGetDefaultLogLevelResponse* resp;
    1316              : 
    1317            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    1318              : 
    1319            0 :     if (daemon == 0)
    1320            0 :         return;
    1321              : 
    1322              :     /* initialise new message */
    1323            0 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR) {
    1324            0 :         dlt_daemon_control_service_response(
    1325              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1326            0 :         return;
    1327              :     }
    1328              : 
    1329            0 :     msg.datasize = sizeof(DltServiceGetDefaultLogLevelResponse);
    1330              : 
    1331            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    1332            0 :         free(msg.databuffer);
    1333            0 :         msg.databuffer = 0;
    1334              :     }
    1335              : 
    1336            0 :     if (msg.databuffer == 0) {
    1337            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    1338            0 :         msg.databuffersize = msg.datasize;
    1339              :     }
    1340              : 
    1341            0 :     if (msg.databuffer == 0) {
    1342            0 :         dlt_daemon_control_service_response(
    1343              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1344            0 :         return;
    1345              :     }
    1346              : 
    1347              :     resp = (DltServiceGetDefaultLogLevelResponse*)msg.databuffer;
    1348            0 :     resp->service_id = DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL;
    1349            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    1350            0 :     resp->log_level = (uint8_t)daemon->default_log_level;
    1351              : 
    1352              :     /* send message */
    1353            0 :     dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, "", "", verbose);
    1354              : 
    1355              :     /* free message */
    1356            0 :     dlt_message_free(&msg, 0);
    1357              : }
    1358              : 
    1359            0 : void dlt_daemon_control_get_default_log_level_v2(int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    1360              : {
    1361              :     DltMessageV2 msg;
    1362              :     DltServiceGetDefaultLogLevelResponse* resp;
    1363              : 
    1364            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    1365              : 
    1366            0 :     if (daemon == 0)
    1367            0 :         return;
    1368              : 
    1369              :     /* initialise new message */
    1370            0 :     if (dlt_message_init_v2(&msg, 0) == DLT_RETURN_ERROR) {
    1371            0 :         dlt_daemon_control_service_response_v2(
    1372              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1373            0 :         return;
    1374              :     }
    1375              : 
    1376            0 :     msg.datasize = sizeof(DltServiceGetDefaultLogLevelResponse);
    1377              : 
    1378            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    1379            0 :         free(msg.databuffer);
    1380            0 :         msg.databuffer = 0;
    1381              :     }
    1382              : 
    1383            0 :     if (msg.databuffer == 0) {
    1384            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    1385            0 :         msg.databuffersize = msg.datasize;
    1386              :     }
    1387              : 
    1388            0 :     if (msg.databuffer == 0) {
    1389            0 :         dlt_daemon_control_service_response_v2(
    1390              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1391            0 :         return;
    1392              :     }
    1393              : 
    1394              :     resp = (DltServiceGetDefaultLogLevelResponse*)msg.databuffer;
    1395            0 :     resp->service_id = DLT_SERVICE_ID_GET_DEFAULT_LOG_LEVEL;
    1396            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    1397            0 :     resp->log_level = (uint8_t)daemon->default_log_level;
    1398              : 
    1399              :     /* send message */
    1400            0 :     dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &msg, "", "", verbose);
    1401              : 
    1402              :     /* free message */
    1403            0 :     dlt_message_free_v2(&msg, 0);
    1404              : }
    1405              : 
    1406            0 : void dlt_daemon_control_get_log_info(
    1407              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    1408              : {
    1409              :     DltServiceGetLogInfoRequest* req;
    1410              :     DltMessage resp;
    1411              :     DltDaemonContext* context = 0;
    1412              :     DltDaemonApplication* application = 0;
    1413              : 
    1414              :     int num_applications = 0, num_contexts = 0;
    1415            0 :     uint16_t count_app_ids = 0, count_con_ids = 0;
    1416              : 
    1417              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1418              :     char buf[255];
    1419              : #endif
    1420              : 
    1421              :     int32_t i, j;
    1422              :     size_t offset = 0;
    1423              :     char* apid = 0;
    1424              :     int8_t ll, ts;
    1425              :     uint16_t len;
    1426              :     int8_t value;
    1427              :     size_t sizecont = 0;
    1428              :     int offset_base;
    1429              : 
    1430              :     uint32_t sid;
    1431              : 
    1432              :     DltDaemonRegisteredUsers* user_list = NULL;
    1433              : 
    1434            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    1435              : 
    1436            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    1437            0 :         return;
    1438              : 
    1439            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceGetLogInfoRequest)) < 0)
    1440              :         return;
    1441              : 
    1442            0 :     user_list = dlt_daemon_find_users_list(daemon, daemon->ecuid, verbose);
    1443              : 
    1444            0 :     if (user_list == NULL)
    1445              :         return;
    1446              : 
    1447              :     /* prepare pointer to message request */
    1448            0 :     req = (DltServiceGetLogInfoRequest*)(msg->databuffer);
    1449              : 
    1450              :     /* initialise new message */
    1451            0 :     if (dlt_message_init(&resp, 0) == DLT_RETURN_ERROR) {
    1452            0 :         dlt_daemon_control_service_response(
    1453              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1454            0 :         return;
    1455              :     }
    1456              : 
    1457              :     /* check request */
    1458            0 :     if ((req->options < 3) || (req->options > 7)) {
    1459            0 :         dlt_daemon_control_service_response(
    1460              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1461            0 :         return;
    1462              :     }
    1463              : 
    1464            0 :     if (req->apid[0] != '\0') {
    1465            0 :         application = dlt_daemon_application_find(daemon, req->apid, daemon->ecuid, verbose);
    1466              : 
    1467            0 :         if (application) {
    1468              :             num_applications = 1;
    1469              : 
    1470            0 :             if (req->ctid[0] != '\0') {
    1471            0 :                 context = dlt_daemon_context_find(daemon, req->apid, req->ctid, daemon->ecuid, verbose);
    1472              : 
    1473            0 :                 num_contexts = ((context) ? 1 : 0);
    1474              :             } else {
    1475            0 :                 num_contexts = application->num_contexts;
    1476              :             }
    1477              :         } else {
    1478              :             num_applications = 0;
    1479              :             num_contexts = 0;
    1480              :         }
    1481              :     } else {
    1482              :         /* Request all applications and contexts */
    1483            0 :         num_applications = user_list->num_applications;
    1484            0 :         num_contexts = user_list->num_contexts;
    1485              :     }
    1486              : 
    1487              :     /* prepare payload of data */
    1488              : 
    1489              :     /* Calculate maximum size for a response */
    1490              :     resp.datasize = sizeof(uint32_t) /* SID */ + sizeof(int8_t) /* status*/ + sizeof(ID4) /* DLT_DAEMON_REMO_STRING */;
    1491              : 
    1492              :     sizecont = sizeof(uint32_t) /* context_id */;
    1493              : 
    1494              :     /* Add additional size for response of Mode 4, 6, 7 */
    1495            0 :     if ((req->options == 4) || (req->options == 6) || (req->options == 7))
    1496              :         sizecont += sizeof(int8_t); /* log level */
    1497              : 
    1498              :     /* Add additional size for response of Mode 5, 6, 7 */
    1499            0 :     if ((req->options == 5) || (req->options == 6) || (req->options == 7))
    1500            0 :         sizecont += sizeof(int8_t); /* trace status */
    1501              : 
    1502            0 :     resp.datasize += (int32_t)(((size_t)num_applications * (sizeof(uint32_t) + sizeof(uint16_t)))
    1503            0 :                                + ((size_t)num_contexts * sizecont));
    1504              : 
    1505            0 :     resp.datasize += (int32_t)sizeof(uint16_t);
    1506              : 
    1507              :     /* Add additional size for response of Mode 7 */
    1508            0 :     if (req->options == 7) {
    1509            0 :         if (req->apid[0] != '\0') {
    1510            0 :             if (req->ctid[0] != '\0') {
    1511              :                 /* One application, one context */
    1512              :                 /* context = dlt_daemon_context_find(daemon, req->apid, req->ctid, verbose); */
    1513            0 :                 if (context) {
    1514            0 :                     resp.datasize += (int32_t)sizeof(uint16_t);
    1515              : 
    1516            0 :                     if (context->context_description != 0)
    1517            0 :                         resp.datasize += (int32_t)strlen(context->context_description);
    1518              :                 }
    1519              :             } else
    1520              :                 /* One application, all contexts */
    1521            0 :                 if ((user_list->applications) && (application)) {
    1522              :                     /* Calculate start offset within contexts[] */
    1523              :                     offset_base = 0;
    1524              : 
    1525            0 :                     for (i = 0; i < (application - (user_list->applications)); i++)
    1526            0 :                         offset_base += user_list->applications[i].num_contexts;
    1527              : 
    1528              :                     /* Iterate over all contexts belonging to this application */
    1529            0 :                     for (j = 0; j < application->num_contexts; j++) {
    1530            0 :                         context = &(user_list->contexts[offset_base + j]);
    1531              : 
    1532            0 :                         if (context) {
    1533            0 :                             resp.datasize += (int32_t)sizeof(uint16_t);
    1534              : 
    1535            0 :                             if (context->context_description != 0)
    1536            0 :                                 resp.datasize += (int32_t)strlen(context->context_description);
    1537              :                         }
    1538              :                     }
    1539              :                 }
    1540              : 
    1541              :             /* Space for application description */
    1542            0 :             if (application) {
    1543            0 :                 resp.datasize += (int32_t)sizeof(uint16_t);
    1544              : 
    1545            0 :                 if (application->application_description != 0)
    1546            0 :                     resp.datasize += (int32_t)strlen(application->application_description);
    1547              :             }
    1548              :         } else {
    1549              :             /* All applications, all contexts */
    1550            0 :             for (i = 0; i < user_list->num_contexts; i++) {
    1551            0 :                 resp.datasize += (int32_t)sizeof(uint16_t);
    1552              : 
    1553            0 :                 if (user_list->contexts[i].context_description != 0)
    1554            0 :                     resp.datasize += (int32_t)strlen(user_list->contexts[i].context_description);
    1555              :             }
    1556              : 
    1557            0 :             for (i = 0; i < user_list->num_applications; i++) {
    1558            0 :                 resp.datasize += (int32_t)sizeof(uint16_t);
    1559              : 
    1560            0 :                 if (user_list->applications[i].application_description != 0)
    1561            0 :                     resp.datasize += (int32_t)strlen(user_list->applications[i].application_description);
    1562              :             }
    1563              :         }
    1564              :     }
    1565              : 
    1566            0 :     if (verbose)
    1567            0 :         dlt_vlog(LOG_DEBUG, "Allocate %u bytes for response msg databuffer\n", resp.datasize);
    1568              : 
    1569              :     /* Allocate buffer for response message */
    1570            0 :     resp.databuffer = (uint8_t*)malloc((size_t)resp.datasize);
    1571            0 :     resp.databuffersize = resp.datasize;
    1572              : 
    1573            0 :     if (resp.databuffer == 0) {
    1574            0 :         dlt_daemon_control_service_response(
    1575              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1576            0 :         return;
    1577              :     }
    1578              : 
    1579              :     memset(resp.databuffer, 0, (size_t)resp.datasize);
    1580              :     /* Preparation finished */
    1581              : 
    1582              :     /* Prepare response */
    1583            0 :     sid = DLT_SERVICE_ID_GET_LOG_INFO;
    1584              :     memcpy(resp.databuffer, &sid, sizeof(uint32_t));
    1585              :     offset += sizeof(uint32_t);
    1586              : 
    1587            0 :     value = (int8_t)(((num_applications != 0) && (num_contexts != 0)) ? req->options :
    1588              :                                                                         8); /* 8 = no matching context found */
    1589              : 
    1590            0 :     memcpy(resp.databuffer + offset, &value, sizeof(int8_t));
    1591              :     offset += sizeof(int8_t);
    1592              : 
    1593            0 :     count_app_ids = (uint16_t)num_applications;
    1594              : 
    1595            0 :     if (count_app_ids != 0) {
    1596            0 :         memcpy(resp.databuffer + offset, &count_app_ids, sizeof(uint16_t));
    1597              :         offset += sizeof(uint16_t);
    1598              : 
    1599              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1600              :         dlt_vlog(LOG_DEBUG, "#apid: %d \n", count_app_ids);
    1601              : #endif
    1602              : 
    1603            0 :         for (i = 0; i < count_app_ids; i++) {
    1604            0 :             if (req->apid[0] != '\0') {
    1605            0 :                 apid = req->apid;
    1606              :             } else {
    1607            0 :                 if (user_list->applications)
    1608            0 :                     apid = user_list->applications[i].apid;
    1609              :                 else
    1610              :                     /* This should never occur! */
    1611              :                     apid = 0;
    1612              :             }
    1613              : 
    1614            0 :             application = dlt_daemon_application_find(daemon, apid, daemon->ecuid, verbose);
    1615              : 
    1616            0 :             if ((user_list->applications) && (application)) {
    1617              :                 /* Calculate start offset within contexts[] */
    1618              :                 offset_base = 0;
    1619              : 
    1620            0 :                 for (j = 0; j < (application - (user_list->applications)); j++)
    1621            0 :                     offset_base += user_list->applications[j].num_contexts;
    1622              : 
    1623            0 :                 dlt_set_id((char*)(resp.databuffer + offset), apid);
    1624            0 :                 offset += sizeof(ID4);
    1625              : 
    1626              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1627              :                 dlt_print_id(buf, apid);
    1628              :                 dlt_vlog(LOG_DEBUG, "apid: %s\n", buf);
    1629              : #endif
    1630              : 
    1631            0 :                 if (req->apid[0] != '\0')
    1632            0 :                     count_con_ids = (uint16_t)num_contexts;
    1633              :                 else
    1634            0 :                     count_con_ids = (uint16_t)application->num_contexts;
    1635              : 
    1636            0 :                 memcpy(resp.databuffer + offset, &count_con_ids, sizeof(uint16_t));
    1637            0 :                 offset += sizeof(uint16_t);
    1638              : 
    1639              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1640              :                 dlt_vlog(LOG_DEBUG, "#ctid: %d \n", count_con_ids);
    1641              : #endif
    1642              : 
    1643            0 :                 for (j = 0; j < count_con_ids; j++) {
    1644              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1645              :                     dlt_vlog(LOG_DEBUG, "j: %d \n", j);
    1646              : #endif
    1647              : 
    1648            0 :                     if (!((count_con_ids == 1) && (req->apid[0] != '\0') && (req->ctid[0] != '\0')))
    1649            0 :                         context = &(user_list->contexts[offset_base + j]);
    1650              : 
    1651              :                     /* else: context was already searched and found
    1652              :                      *       (one application (found) with one context (found))*/
    1653              : 
    1654            0 :                     if ((context)
    1655            0 :                         && ((req->ctid[0] == '\0')
    1656            0 :                             || ((req->ctid[0] != '\0') && (memcmp(context->ctid, req->ctid, DLT_ID_SIZE) == 0)))) {
    1657            0 :                         dlt_set_id((char*)(resp.databuffer + offset), context->ctid);
    1658            0 :                         offset += sizeof(ID4);
    1659              : 
    1660              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1661              :                         dlt_print_id(buf, context->ctid);
    1662              :                         dlt_vlog(LOG_DEBUG, "ctid: %s \n", buf);
    1663              : #endif
    1664              : 
    1665              :                         /* Mode 4, 6, 7 */
    1666            0 :                         if ((req->options == 4) || (req->options == 6) || (req->options == 7)) {
    1667            0 :                             ll = context->log_level;
    1668            0 :                             memcpy(resp.databuffer + offset, &ll, sizeof(int8_t));
    1669            0 :                             offset += sizeof(int8_t);
    1670              :                         }
    1671              : 
    1672              :                         /* Mode 5, 6, 7 */
    1673            0 :                         if ((req->options == 5) || (req->options == 6) || (req->options == 7)) {
    1674            0 :                             ts = context->trace_status;
    1675            0 :                             memcpy(resp.databuffer + offset, &ts, sizeof(int8_t));
    1676            0 :                             offset += sizeof(int8_t);
    1677              :                         }
    1678              : 
    1679              :                         /* Mode 7 */
    1680            0 :                         if (req->options == 7) {
    1681            0 :                             if (context->context_description) {
    1682            0 :                                 len = (uint16_t)strlen(context->context_description);
    1683            0 :                                 memcpy(resp.databuffer + offset, &len, sizeof(uint16_t));
    1684            0 :                                 offset += sizeof(uint16_t);
    1685            0 :                                 memcpy(
    1686            0 :                                     resp.databuffer + offset, context->context_description,
    1687            0 :                                     strlen(context->context_description));
    1688            0 :                                 offset += strlen(context->context_description);
    1689              :                             } else {
    1690            0 :                                 len = 0;
    1691            0 :                                 memcpy(resp.databuffer + offset, &len, sizeof(uint16_t));
    1692            0 :                                 offset += sizeof(uint16_t);
    1693              :                             }
    1694              :                         }
    1695              : 
    1696              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1697              :                         dlt_vlog(LOG_DEBUG, "ll=%d ts=%d \n", (int32_t)ll, (int32_t)ts);
    1698              : #endif
    1699              :                     }
    1700              : 
    1701              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1702              :                     dlt_log(LOG_DEBUG, "\n");
    1703              : #endif
    1704              :                 }
    1705              : 
    1706              :                 /* Mode 7 */
    1707            0 :                 if (req->options == 7) {
    1708            0 :                     if (application->application_description) {
    1709            0 :                         len = (uint16_t)strlen(application->application_description);
    1710            0 :                         memcpy(resp.databuffer + offset, &len, sizeof(uint16_t));
    1711            0 :                         offset += sizeof(uint16_t);
    1712            0 :                         memcpy(
    1713            0 :                             resp.databuffer + offset, application->application_description,
    1714            0 :                             strlen(application->application_description));
    1715            0 :                         offset += strlen(application->application_description);
    1716              :                     } else {
    1717            0 :                         len = 0;
    1718            0 :                         memcpy(resp.databuffer + offset, &len, sizeof(uint16_t));
    1719            0 :                         offset += sizeof(uint16_t);
    1720              :                     }
    1721              :                 }
    1722              :             } /* if (application) */
    1723              : 
    1724              :         } /* for (i=0;i<count_app_ids;i++) */
    1725              : 
    1726              :     } /* if (count_app_ids!=0) */
    1727              : 
    1728            0 :     dlt_set_id((char*)(resp.databuffer + offset), DLT_DAEMON_REMO_STRING);
    1729              : 
    1730              :     /* send message */
    1731            0 :     dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &resp, "", "", verbose);
    1732              : 
    1733              :     /* free message */
    1734            0 :     dlt_message_free(&resp, 0);
    1735              : }
    1736              : 
    1737            0 : void dlt_daemon_control_get_log_info_v2(
    1738              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    1739              : {
    1740              :     DltServiceGetLogInfoRequestV2* req;
    1741              :     DltMessageV2 resp;
    1742              :     DltDaemonContext* context = NULL;
    1743            0 :     DltDaemonApplication* application = NULL;
    1744              : 
    1745              :     int num_applications = 0, num_contexts = 0;
    1746            0 :     uint16_t count_app_ids = 0, count_con_ids = 0;
    1747              : 
    1748              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1749              :     char buf[255];
    1750              : #endif
    1751              :     size_t offset = 0;
    1752            0 :     uint8_t apidlen = 0;
    1753              :     char* apid = 0;
    1754              :     int8_t ll, ts;
    1755              :     uint16_t len;
    1756              :     int8_t value;
    1757              :     size_t sizecont = 0;
    1758              :     int offset_base;
    1759              : 
    1760              :     uint32_t sid;
    1761              : 
    1762              :     DltDaemonRegisteredUsers* user_list = NULL;
    1763              : 
    1764            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    1765              : 
    1766            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    1767            0 :         return;
    1768              : 
    1769            0 :     if (dlt_check_rcv_data_size(msg->datasize, DLT_SERVICE_GET_LOG_INFO_REQUEST_FIXED_SIZE_V2) < 0)
    1770              :         return;
    1771              : 
    1772            0 :     user_list = dlt_daemon_find_users_list_v2(daemon, daemon->ecuid2len, daemon->ecuid2, verbose);
    1773              : 
    1774            0 :     if (user_list == NULL)
    1775              :         return;
    1776              : 
    1777            0 :     req = (DltServiceGetLogInfoRequestV2*)calloc(1, sizeof(DltServiceGetLogInfoRequestV2));
    1778              : 
    1779            0 :     if (req == NULL)
    1780              :         return;
    1781              : 
    1782              :     /* prepare pointer to message request */
    1783              :     int db_offset = 0;
    1784            0 :     memcpy(&(req->service_id), msg->databuffer + db_offset, sizeof(uint32_t));
    1785              :     db_offset = db_offset + (int)sizeof(uint32_t);
    1786            0 :     memcpy(&(req->options), msg->databuffer + db_offset, sizeof(uint8_t));
    1787              :     db_offset = db_offset + (int)sizeof(uint8_t);
    1788            0 :     memcpy(&(req->apidlen), msg->databuffer + db_offset, sizeof(uint8_t));
    1789              :     db_offset = db_offset + (int)sizeof(uint8_t);
    1790            0 :     dlt_set_id_v2(req->apid, (const char*)(msg->databuffer + db_offset), req->apidlen);
    1791            0 :     db_offset = db_offset + (int)req->apidlen;
    1792            0 :     memcpy(&(req->ctidlen), (const char*)(msg->databuffer + db_offset), sizeof(uint8_t));
    1793            0 :     db_offset = db_offset + (int)sizeof(uint8_t);
    1794            0 :     dlt_set_id_v2(req->ctid, (const char*)(msg->databuffer + db_offset), req->ctidlen);
    1795            0 :     db_offset = db_offset + (int)req->ctidlen;
    1796            0 :     memcpy((req->com), (const char*)(msg->databuffer + db_offset), DLT_ID_SIZE);
    1797              : 
    1798              :     /* initialise new message */
    1799            0 :     if (dlt_message_init_v2(&resp, 0) == DLT_RETURN_ERROR) {
    1800            0 :         dlt_daemon_control_service_response_v2(
    1801              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1802            0 :         return;
    1803              :     }
    1804              : 
    1805              :     /* check request */
    1806            0 :     if ((req->options < 3) || (req->options > 7)) {
    1807            0 :         dlt_daemon_control_service_response_v2(
    1808              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1809            0 :         return;
    1810              :     }
    1811              : 
    1812            0 :     if (req->apidlen != 0) {
    1813            0 :         dlt_daemon_application_find_v2(
    1814            0 :             daemon, req->apidlen, req->apid, daemon->ecuid2len, daemon->ecuid2, verbose, &application);
    1815              : 
    1816            0 :         if (application) {
    1817              :             num_applications = 1;
    1818            0 :             if (req->ctidlen != 0) {
    1819            0 :                 context = dlt_daemon_context_find_v2(
    1820            0 :                     daemon, req->apidlen, req->apid, req->ctidlen, req->ctid, daemon->ecuid2len, daemon->ecuid2,
    1821              :                     verbose);
    1822              : 
    1823            0 :                 num_contexts = ((context) ? 1 : 0);
    1824              :             } else {
    1825            0 :                 num_contexts = application->num_contexts;
    1826              :             }
    1827              :         } else {
    1828              :             num_applications = 0;
    1829              :             num_contexts = 0;
    1830              :         }
    1831              :     } else {
    1832              :         /* Request all applications and contexts */
    1833            0 :         num_applications = user_list->num_applications;
    1834            0 :         num_contexts = user_list->num_contexts;
    1835              :     }
    1836              :     /* prepare payload of data */
    1837              : 
    1838              :     /* Calculate maximum size for a response */
    1839            0 :     resp.datasize = sizeof(uint32_t) /* SID */ + sizeof(int8_t) /* status*/ + sizeof(ID4) /* DLT_DAEMON_REMO_STRING */;
    1840              : 
    1841              :     sizecont = sizeof(uint8_t) /* context_id length */;
    1842              : 
    1843              :     /* Add additional size for response of Mode 4, 6, 7 */
    1844            0 :     if ((req->options == 4) || (req->options == 6) || (req->options == 7))
    1845              :         sizecont += sizeof(int8_t); /* log level */
    1846              : 
    1847              :     /* Add additional size for response of Mode 5, 6, 7 */
    1848            0 :     if ((req->options == 5) || (req->options == 6) || (req->options == 7))
    1849            0 :         sizecont += sizeof(int8_t); /* trace status */
    1850              : 
    1851            0 :     if ((num_applications == user_list->num_applications) && (num_contexts == user_list->num_contexts)) {
    1852            0 :         for (int i = 0; i < num_applications; i++) {
    1853            0 :             resp.datasize += (int32_t)((
    1854            0 :                 sizeof(uint8_t) /* app_id length */ + user_list->applications[i].apid2len /* app_id */
    1855              :                 + sizeof(uint16_t) /* count_con_ids */));
    1856              :         }
    1857            0 :         for (int j = 0; j < num_contexts; j++) {
    1858            0 :             resp.datasize += (int32_t)(sizecont + user_list->contexts[j].ctid2len);
    1859              :         }
    1860            0 :     } else if (num_applications == 1) {
    1861            0 :         resp.datasize += (int32_t)((
    1862            0 :             sizeof(uint8_t) /* app_id length */ + application->apid2len /* app_id */
    1863              :             + sizeof(uint16_t) /* count_con_ids */));
    1864              :         /* 1 application and 1 context*/
    1865            0 :         if (num_contexts == 1) {
    1866            0 :             resp.datasize += (int32_t)(sizecont + context->ctid2len);
    1867            0 :         } else if (num_contexts == application->num_contexts) {
    1868            0 :             if ((user_list->applications) && (application)) {
    1869              :                 /* Calculate start offset within contexts[] */
    1870              :                 offset_base = 0;
    1871              : 
    1872            0 :                 for (int i = 0; i < (application - (user_list->applications)); i++)
    1873            0 :                     offset_base += user_list->applications[i].num_contexts;
    1874              : 
    1875              :                 /* Iterate over all contexts belonging to this application */
    1876            0 :                 for (int j = 0; j < application->num_contexts; j++) {
    1877            0 :                     context = &(user_list->contexts[offset_base + j]);
    1878              : 
    1879            0 :                     if (context) {
    1880            0 :                         resp.datasize += (int32_t)(sizecont + context->ctid2len);
    1881              :                     }
    1882              :                 }
    1883              :             }
    1884              :         }
    1885              :     }
    1886            0 :     resp.datasize += (int32_t)sizeof(uint16_t) /* count_app_ids */;
    1887              : 
    1888              :     /* Add additional size for response of Mode 7 */
    1889            0 :     if (req->options == 7) {
    1890            0 :         if (req->apidlen != 0) {
    1891            0 :             if (req->ctidlen != 0) {
    1892              :                 /* One application, one context */
    1893            0 :                 if (context) {
    1894            0 :                     resp.datasize += (int32_t)sizeof(uint16_t) /* len_context_description */;
    1895              : 
    1896            0 :                     if (context->context_description != 0)
    1897            0 :                         resp.datasize += (int32_t)strlen(context->context_description); /* context_description */
    1898              :                 }
    1899              :             } else
    1900              :                 /* One application, all contexts */
    1901            0 :                 if ((user_list->applications) && (application)) {
    1902              :                     /* Calculate start offset within contexts[] */
    1903              :                     offset_base = 0;
    1904              : 
    1905            0 :                     for (int i = 0; i < (application - (user_list->applications)); i++)
    1906            0 :                         offset_base += user_list->applications[i].num_contexts;
    1907              : 
    1908              :                     /* Iterate over all contexts belonging to this application */
    1909            0 :                     for (int j = 0; j < application->num_contexts; j++) {
    1910            0 :                         context = &(user_list->contexts[offset_base + j]);
    1911              : 
    1912            0 :                         if (context) {
    1913            0 :                             resp.datasize += (int32_t)sizeof(uint16_t) /* len_context_description */;
    1914              : 
    1915            0 :                             if (context->context_description != 0)
    1916            0 :                                 resp.datasize +=
    1917            0 :                                     (int32_t)strlen(context->context_description); /* context_description */
    1918              :                         }
    1919              :                     }
    1920              :                 }
    1921              : 
    1922              :             /* Space for application description */
    1923            0 :             if (application) {
    1924            0 :                 resp.datasize += (int32_t)sizeof(uint16_t) /* len_app_description */;
    1925              : 
    1926            0 :                 if (application->application_description != 0)
    1927            0 :                     resp.datasize += (int32_t)strlen(application->application_description); /* app_description */
    1928              :             }
    1929              :         } else {
    1930              :             /* All applications, all contexts */
    1931            0 :             for (int i = 0; i < user_list->num_contexts; i++) {
    1932            0 :                 resp.datasize += (int32_t)sizeof(uint16_t) /* len_context_description */;
    1933              : 
    1934            0 :                 if (user_list->contexts[i].context_description != 0)
    1935            0 :                     resp.datasize += (int32_t)strlen(user_list->contexts[i].context_description);
    1936              :             }
    1937              : 
    1938            0 :             for (int i = 0; i < user_list->num_applications; i++) {
    1939            0 :                 resp.datasize += (int32_t)sizeof(uint16_t) /* len_app_description */;
    1940            0 :                 if (user_list->applications[i].application_description != 0)
    1941            0 :                     resp.datasize +=
    1942            0 :                         (int32_t)strlen(user_list->applications[i].application_description); /* app_description */
    1943              :             }
    1944              :         }
    1945              :     }
    1946              : 
    1947            0 :     if (verbose)
    1948            0 :         dlt_vlog(LOG_DEBUG, "Allocate %u bytes for response msg databuffer\n", resp.datasize);
    1949              : 
    1950              :     /* Allocate buffer for response message */
    1951            0 :     resp.databuffer = (uint8_t*)malloc((size_t)resp.datasize);
    1952              :     resp.datasize = resp.datasize;
    1953              : 
    1954            0 :     if (resp.databuffer == 0) {
    1955            0 :         dlt_daemon_control_service_response_v2(
    1956              :             sock, daemon, daemon_local, DLT_SERVICE_ID_GET_LOG_INFO, DLT_SERVICE_RESPONSE_ERROR, verbose);
    1957            0 :         return;
    1958              :     }
    1959              : 
    1960              :     memset(resp.databuffer, 0, (size_t)resp.datasize);
    1961              :     /* Preparation finished */
    1962              : 
    1963              :     /* Prepare response */
    1964            0 :     sid = DLT_SERVICE_ID_GET_LOG_INFO;
    1965              :     memcpy(resp.databuffer, &sid, sizeof(uint32_t));
    1966              :     offset += sizeof(uint32_t);
    1967              : 
    1968            0 :     value = (int8_t)(((num_applications != 0) && (num_contexts != 0)) ? req->options :
    1969              :                                                                         8); /* 8 = no matching context found */
    1970              : 
    1971            0 :     memcpy(resp.databuffer + offset, &value, sizeof(int8_t));
    1972              :     offset += sizeof(int8_t);
    1973              : 
    1974            0 :     count_app_ids = (uint16_t)num_applications;
    1975              : 
    1976            0 :     if (count_app_ids != 0) {
    1977            0 :         memcpy(resp.databuffer + offset, &count_app_ids, sizeof(uint16_t));
    1978              :         offset += sizeof(uint16_t);
    1979              : 
    1980              : #if (DLT_DEBUG_GETLOGINFO == 1)
    1981              :         dlt_vlog(LOG_DEBUG, "#apid: %d \n", count_app_ids);
    1982              : #endif
    1983              : 
    1984            0 :         for (int i = 0; i < count_app_ids; i++) {
    1985            0 :             if (req->apidlen != 0) {
    1986              :                 apid = req->apid;
    1987            0 :                 apidlen = req->apidlen;
    1988              :             } else {
    1989            0 :                 if (user_list->applications) {
    1990            0 :                     apid = user_list->applications[i].apid2;
    1991            0 :                     apidlen = user_list->applications[i].apid2len;
    1992              :                 } else {
    1993              :                     /* This should never occur! */
    1994              :                     apid = NULL;
    1995            0 :                     apidlen = 0;
    1996              :                 }
    1997              :             }
    1998              : 
    1999            0 :             dlt_daemon_application_find_v2(
    2000            0 :                 daemon, apidlen, apid, daemon->ecuid2len, daemon->ecuid2, verbose, &application);
    2001              : 
    2002            0 :             if ((user_list->applications) && (application)) {
    2003              :                 /* Calculate start offset within contexts[] */
    2004              :                 offset_base = 0;
    2005              : 
    2006            0 :                 for (int j = 0; j < (application - (user_list->applications)); j++)
    2007            0 :                     offset_base += user_list->applications[j].num_contexts;
    2008            0 :                 memcpy(resp.databuffer + offset, &apidlen, 1);
    2009            0 :                 offset += 1;
    2010            0 :                 memcpy(resp.databuffer + offset, apid, apidlen);
    2011            0 :                 offset += apidlen;
    2012              : 
    2013              : #if (DLT_DEBUG_GETLOGINFO == 1)
    2014              :                 dlt_print_id_v2(buf, apid, apidlen);
    2015              :                 if ((buf == NULL) || (apid == NULL) || (apidlen == 0)) {
    2016              :                     dlt_vlog(LOG_ERR, "Failed to memcpy apid\n");
    2017              :                 }
    2018              : 
    2019              :                 memcpy(buf, apid, apidlen);
    2020              :                 dlt_vlog(LOG_DEBUG, "apid: %s\n", buf);
    2021              : #endif
    2022            0 :                 if (req->apidlen != 0)
    2023            0 :                     count_con_ids = (uint16_t)num_contexts;
    2024              :                 else
    2025            0 :                     count_con_ids = (uint16_t)application->num_contexts;
    2026              : 
    2027            0 :                 memcpy(resp.databuffer + offset, &count_con_ids, sizeof(uint16_t));
    2028            0 :                 offset += sizeof(uint16_t);
    2029              : 
    2030              : #if (DLT_DEBUG_GETLOGINFO == 1)
    2031              :                 dlt_vlog(LOG_DEBUG, "#ctid: %d \n", count_con_ids);
    2032              : #endif
    2033              : 
    2034            0 :                 for (int j = 0; j < count_con_ids; j++) {
    2035              : #if (DLT_DEBUG_GETLOGINFO == 1)
    2036              :                     dlt_vlog(LOG_DEBUG, "j: %d \n", j);
    2037              : #endif
    2038              : 
    2039            0 :                     if (!((count_con_ids == 1) && (req->apidlen != 0) && (req->ctidlen != 0)))
    2040            0 :                         context = &(user_list->contexts[offset_base + j]);
    2041              : 
    2042              :                     /* else: context was already searched and found
    2043              :                      *       (one application (found) with one context (found))*/
    2044              : 
    2045            0 :                     if ((context)
    2046            0 :                         && ((req->ctidlen == 0)
    2047            0 :                             || ((req->ctidlen != 0) && (memcmp(context->ctid2, req->ctid, req->ctidlen) == 0)))) {
    2048            0 :                         memcpy(resp.databuffer + offset, &(context->ctid2len), 1);
    2049            0 :                         offset += 1;
    2050            0 :                         memcpy(resp.databuffer + offset, context->ctid2, context->ctid2len);
    2051            0 :                         offset += context->ctid2len;
    2052              : 
    2053              : #if (DLT_DEBUG_GETLOGINFO == 1)
    2054              :                         // dlt_print_id_v2(buf, context->ctid, context->ctid2len);
    2055              :                         // TBD: Enable below check for NULL
    2056              :                         /*
    2057              :                         if ((buf == NULL) || (context->ctid == NULL) || (context->ctid2len == 0)) {
    2058              :                             dlt_vlog(LOG_ERR, "Failed to memcpy ctid\n");
    2059              :                         }
    2060              :                         */
    2061              : 
    2062              :                         memcpy(buf, context->ctid2, context->ctid2len);
    2063              :                         dlt_vlog(LOG_DEBUG, "ctid: %s \n", buf);
    2064              : #endif
    2065              : 
    2066              :                         /* Mode 4, 6, 7 */
    2067            0 :                         if ((req->options == 4) || (req->options == 6) || (req->options == 7)) {
    2068            0 :                             ll = context->log_level;
    2069            0 :                             memcpy(resp.databuffer + offset, &ll, sizeof(int8_t));
    2070            0 :                             offset += sizeof(int8_t);
    2071              :                         }
    2072              : 
    2073              :                         /* Mode 5, 6, 7 */
    2074            0 :                         if ((req->options == 5) || (req->options == 6) || (req->options == 7)) {
    2075            0 :                             ts = context->trace_status;
    2076            0 :                             memcpy(resp.databuffer + offset, &ts, sizeof(int8_t));
    2077            0 :                             offset += sizeof(int8_t);
    2078              :                         }
    2079              : 
    2080              :                         /* Mode 7 */
    2081            0 :                         if (req->options == 7) {
    2082            0 :                             if (context->context_description) {
    2083            0 :                                 len = (uint16_t)strlen(context->context_description);
    2084            0 :                                 memcpy(resp.databuffer + offset, &len, sizeof(uint16_t));
    2085            0 :                                 offset += sizeof(uint16_t);
    2086            0 :                                 memcpy(
    2087            0 :                                     resp.databuffer + offset, context->context_description,
    2088            0 :                                     strlen(context->context_description));
    2089            0 :                                 offset += strlen(context->context_description);
    2090              :                             } else {
    2091            0 :                                 len = 0;
    2092            0 :                                 memcpy(resp.databuffer + offset, &len, sizeof(uint16_t));
    2093            0 :                                 offset += sizeof(uint16_t);
    2094              :                             }
    2095              :                         }
    2096              : 
    2097              : #if (DLT_DEBUG_GETLOGINFO == 1)
    2098              :                         dlt_vlog(LOG_DEBUG, "ll=%d ts=%d \n", (int32_t)ll, (int32_t)ts);
    2099              : #endif
    2100              :                     }
    2101              : 
    2102              : #if (DLT_DEBUG_GETLOGINFO == 1)
    2103              :                     dlt_log(LOG_DEBUG, "\n");
    2104              : #endif
    2105              :                 }
    2106              : 
    2107              :                 /* Mode 7 */
    2108            0 :                 if (req->options == 7) {
    2109            0 :                     if (application->application_description) {
    2110            0 :                         len = (uint16_t)strlen(application->application_description);
    2111            0 :                         memcpy(resp.databuffer + offset, &len, sizeof(uint16_t));
    2112            0 :                         offset += sizeof(uint16_t);
    2113            0 :                         memcpy(
    2114            0 :                             resp.databuffer + offset, application->application_description,
    2115            0 :                             strlen(application->application_description));
    2116            0 :                         offset += strlen(application->application_description);
    2117              :                     } else {
    2118            0 :                         len = 0;
    2119            0 :                         memcpy(resp.databuffer + offset, &len, sizeof(uint16_t));
    2120            0 :                         offset += sizeof(uint16_t);
    2121              :                     }
    2122              :                 }
    2123              :             } /* if (application) */
    2124              : 
    2125              :         } /* for (i=0;i<count_app_ids;i++) */
    2126              : 
    2127              :     } /* if (count_app_ids!=0) */
    2128              : 
    2129            0 :     dlt_set_id((char*)(resp.databuffer + offset), DLT_DAEMON_REMO_STRING);
    2130              : 
    2131              :     /* send message */
    2132            0 :     dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &resp, "", "", verbose);
    2133              : 
    2134            0 :     free(req);
    2135              : 
    2136              :     /* free message */
    2137            0 :     dlt_message_free_v2(&resp, 0);
    2138              : }
    2139              : 
    2140            0 : int dlt_daemon_control_message_buffer_overflow(
    2141              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, unsigned int overflow_counter, char* apid, int verbose)
    2142              : {
    2143              :     int ret;
    2144              :     DltMessage msg;
    2145              :     DltServiceMessageBufferOverflowResponse* resp;
    2146              : 
    2147            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2148              : 
    2149            0 :     if (daemon == 0)
    2150              :         return DLT_DAEMON_ERROR_UNKNOWN;
    2151              : 
    2152              :     /* initialise new message */
    2153            0 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR) {
    2154            0 :         dlt_daemon_control_service_response(
    2155              :             sock, daemon, daemon_local, DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2156            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
    2157              :     }
    2158              : 
    2159              :     /* prepare payload of data */
    2160            0 :     msg.datasize = sizeof(DltServiceMessageBufferOverflowResponse);
    2161              : 
    2162            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2163            0 :         free(msg.databuffer);
    2164            0 :         msg.databuffer = 0;
    2165              :     }
    2166              : 
    2167            0 :     if (msg.databuffer == 0) {
    2168            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2169            0 :         msg.databuffersize = msg.datasize;
    2170              :     }
    2171              : 
    2172            0 :     if (msg.databuffer == 0)
    2173              :         return DLT_DAEMON_ERROR_UNKNOWN;
    2174              : 
    2175              :     resp = (DltServiceMessageBufferOverflowResponse*)msg.databuffer;
    2176            0 :     resp->service_id = DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW;
    2177            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    2178            0 :     resp->overflow = DLT_MESSAGE_BUFFER_OVERFLOW;
    2179            0 :     resp->overflow_counter = overflow_counter;
    2180              : 
    2181              :     /* send message */
    2182            0 :     if ((ret = dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, apid, "", verbose))) {
    2183            0 :         dlt_message_free(&msg, 0);
    2184            0 :         return ret;
    2185              :     }
    2186              : 
    2187              :     /* free message */
    2188            0 :     dlt_message_free(&msg, 0);
    2189              : 
    2190            0 :     return DLT_DAEMON_ERROR_OK;
    2191              : }
    2192              : 
    2193            0 : int dlt_daemon_control_message_buffer_overflow_v2(
    2194              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, unsigned int overflow_counter, char* apid, int verbose)
    2195              : {
    2196              :     int ret;
    2197              :     DltMessageV2 msg;
    2198              :     DltServiceMessageBufferOverflowResponse* resp;
    2199              : 
    2200            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2201              : 
    2202            0 :     if (daemon == 0)
    2203              :         return DLT_DAEMON_ERROR_UNKNOWN;
    2204              : 
    2205              :     /* initialise new message */
    2206            0 :     if (dlt_message_init_v2(&msg, 0) == DLT_RETURN_ERROR) {
    2207            0 :         dlt_daemon_control_service_response_v2(
    2208              :             sock, daemon, daemon_local, DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2209            0 :         return DLT_DAEMON_ERROR_UNKNOWN;
    2210              :     }
    2211              : 
    2212              :     /* prepare payload of data */
    2213            0 :     msg.datasize = sizeof(DltServiceMessageBufferOverflowResponse);
    2214              : 
    2215            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2216            0 :         free(msg.databuffer);
    2217            0 :         msg.databuffer = 0;
    2218              :     }
    2219              : 
    2220            0 :     if (msg.databuffer == 0) {
    2221            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2222            0 :         msg.databuffersize = msg.datasize;
    2223              :     }
    2224              : 
    2225            0 :     if (msg.databuffer == 0)
    2226              :         return DLT_DAEMON_ERROR_UNKNOWN;
    2227              : 
    2228              :     resp = (DltServiceMessageBufferOverflowResponse*)msg.databuffer;
    2229            0 :     resp->service_id = DLT_SERVICE_ID_MESSAGE_BUFFER_OVERFLOW;
    2230            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    2231            0 :     resp->overflow = DLT_MESSAGE_BUFFER_OVERFLOW;
    2232            0 :     resp->overflow_counter = overflow_counter;
    2233              : 
    2234              :     /* send message */
    2235            0 :     if ((ret = dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &msg, apid, "", verbose))) {
    2236            0 :         dlt_message_free_v2(&msg, 0);
    2237            0 :         return ret;
    2238              :     }
    2239              : 
    2240              :     /* free message */
    2241            0 :     dlt_message_free_v2(&msg, 0);
    2242              : 
    2243            0 :     return DLT_DAEMON_ERROR_OK;
    2244              : }
    2245              : 
    2246            1 : void dlt_daemon_control_service_response(
    2247              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, uint32_t service_id, int8_t status, int verbose)
    2248              : {
    2249              :     DltMessage msg;
    2250              :     DltServiceResponse* resp;
    2251              : 
    2252            1 :     PRINT_FUNCTION_VERBOSE(verbose);
    2253              : 
    2254            1 :     if (daemon == 0)
    2255            0 :         return;
    2256              : 
    2257              :     /* initialise new message */
    2258            1 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR)
    2259              :         return;
    2260              : 
    2261              :     /* prepare payload of data */
    2262            1 :     msg.datasize = sizeof(DltServiceResponse);
    2263              : 
    2264            1 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2265            0 :         free(msg.databuffer);
    2266            0 :         msg.databuffer = 0;
    2267              :     }
    2268              : 
    2269            1 :     if (msg.databuffer == 0) {
    2270            1 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2271            1 :         msg.databuffersize = msg.datasize;
    2272              :     }
    2273              : 
    2274            1 :     if (msg.databuffer == 0)
    2275              :         return;
    2276              : 
    2277              :     resp = (DltServiceResponse*)msg.databuffer;
    2278            1 :     resp->service_id = service_id;
    2279            1 :     resp->status = (uint8_t)status;
    2280              : 
    2281              :     /* send message */
    2282            1 :     dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, "", "", verbose);
    2283              : 
    2284              :     /* free message */
    2285            1 :     dlt_message_free(&msg, 0);
    2286              : }
    2287              : 
    2288            0 : void dlt_daemon_control_service_response_v2(
    2289              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, uint32_t service_id, int8_t status, int verbose)
    2290              : {
    2291              :     DltMessageV2 msg;
    2292              :     DltServiceResponse* resp;
    2293              : 
    2294            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2295              : 
    2296            0 :     if (daemon == 0)
    2297            0 :         return;
    2298              : 
    2299              :     /* initialise new message */
    2300            0 :     if (dlt_message_init_v2(&msg, 0) == DLT_RETURN_ERROR)
    2301              :         return;
    2302              : 
    2303              :     /* prepare payload of data */
    2304            0 :     msg.datasize = sizeof(DltServiceResponse);
    2305              : 
    2306            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2307            0 :         free(msg.databuffer);
    2308            0 :         msg.databuffer = 0;
    2309              :     }
    2310              : 
    2311            0 :     if (msg.databuffer == 0) {
    2312            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2313            0 :         msg.databuffersize = msg.datasize;
    2314              :     }
    2315              : 
    2316            0 :     if (msg.databuffer == 0)
    2317              :         return;
    2318              : 
    2319              :     resp = (DltServiceResponse*)msg.databuffer;
    2320            0 :     resp->service_id = service_id;
    2321            0 :     resp->status = (uint8_t)status;
    2322              : 
    2323              :     /* send message */
    2324            0 :     dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &msg, "", "", verbose);
    2325              : 
    2326              :     /* free message */
    2327            0 :     dlt_message_free_v2(&msg, 0);
    2328              : }
    2329              : 
    2330            0 : int dlt_daemon_control_message_unregister_context(
    2331              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, char* apid, char* ctid, char* comid, int verbose)
    2332              : {
    2333              :     DltMessage msg;
    2334              :     DltServiceUnregisterContext* resp;
    2335              : 
    2336            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2337              : 
    2338            0 :     if (daemon == 0)
    2339              :         return -1;
    2340              : 
    2341              :     /* initialise new message */
    2342            0 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR)
    2343              :         return -1;
    2344              : 
    2345              :     /* prepare payload of data */
    2346            0 :     msg.datasize = sizeof(DltServiceUnregisterContext);
    2347              : 
    2348            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2349            0 :         free(msg.databuffer);
    2350            0 :         msg.databuffer = 0;
    2351              :     }
    2352              : 
    2353            0 :     if (msg.databuffer == 0) {
    2354            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2355            0 :         msg.databuffersize = msg.datasize;
    2356              :     }
    2357              : 
    2358            0 :     if (msg.databuffer == 0)
    2359              :         return -1;
    2360              : 
    2361              :     resp = (DltServiceUnregisterContext*)msg.databuffer;
    2362            0 :     resp->service_id = DLT_SERVICE_ID_UNREGISTER_CONTEXT;
    2363            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    2364            0 :     dlt_set_id(resp->apid, apid);
    2365            0 :     dlt_set_id(resp->ctid, ctid);
    2366            0 :     dlt_set_id(resp->comid, comid);
    2367              : 
    2368              :     /* send message */
    2369            0 :     if (dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, "", "", verbose)) {
    2370            0 :         dlt_message_free(&msg, 0);
    2371            0 :         return -1;
    2372              :     }
    2373              : 
    2374              :     /* free message */
    2375            0 :     dlt_message_free(&msg, 0);
    2376              : 
    2377            0 :     return 0;
    2378              : }
    2379              : 
    2380            0 : int dlt_daemon_control_message_unregister_context_v2(
    2381              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, uint8_t apidlen, char* apid, uint8_t ctidlen, char* ctid,
    2382              :     char* comid, int verbose)
    2383              : {
    2384              :     DltMessageV2 msg;
    2385              :     DltServiceUnregisterContextV2 resp;
    2386              :     uint8_t contextSize = 0;
    2387            0 :     resp.apid = NULL;
    2388            0 :     resp.ctid = NULL;
    2389              :     int offset = 0;
    2390              : 
    2391            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2392              : 
    2393            0 :     if (daemon == 0)
    2394              :         return -1;
    2395              : 
    2396              :     /* initialise new message */
    2397            0 :     if (dlt_message_init_v2(&msg, 0) == DLT_RETURN_ERROR)
    2398              :         return -1;
    2399              : 
    2400              :     /* prepare payload of data */
    2401            0 :     contextSize = (uint8_t)(sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint8_t) + apidlen + sizeof(uint8_t) + ctidlen
    2402              :                             + DLT_ID_SIZE);
    2403              : 
    2404            0 :     msg.datasize = contextSize;
    2405              : 
    2406            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2407            0 :         free(msg.databuffer);
    2408            0 :         msg.databuffer = 0;
    2409              :     }
    2410              : 
    2411            0 :     if (msg.databuffer == 0) {
    2412            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2413            0 :         msg.databuffersize = msg.datasize;
    2414              :     }
    2415              : 
    2416            0 :     if (msg.databuffer == 0)
    2417              :         return -1;
    2418              : 
    2419            0 :     resp.service_id = DLT_SERVICE_ID_UNREGISTER_CONTEXT;
    2420            0 :     resp.status = DLT_SERVICE_RESPONSE_OK;
    2421            0 :     dlt_set_id_v2(resp.apid, apid, resp.apidlen);
    2422            0 :     dlt_set_id_v2(resp.ctid, ctid, resp.ctidlen);
    2423            0 :     dlt_set_id(resp.comid, comid);
    2424              : 
    2425            0 :     memcpy(msg.databuffer + offset, &(resp.service_id), sizeof(uint32_t));
    2426              :     offset = offset + (int)sizeof(uint32_t);
    2427            0 :     memcpy(msg.databuffer + offset, &(resp.status), sizeof(uint8_t));
    2428              :     offset = offset + (int)sizeof(uint8_t);
    2429            0 :     memcpy(msg.databuffer + offset, &(resp.apidlen), sizeof(uint8_t));
    2430              :     offset = offset + (int)sizeof(uint8_t);
    2431            0 :     memcpy(msg.databuffer + offset, resp.apid, resp.apidlen);
    2432            0 :     offset = offset + (int)resp.apidlen;
    2433            0 :     memcpy(msg.databuffer + offset, &(resp.ctidlen), sizeof(uint8_t));
    2434            0 :     offset = offset + (int)sizeof(uint8_t);
    2435            0 :     memcpy(msg.databuffer + offset, resp.ctid, resp.ctidlen);
    2436            0 :     offset = offset + (int)resp.ctidlen;
    2437            0 :     memcpy(msg.databuffer + offset, resp.comid, DLT_ID_SIZE);
    2438              : 
    2439              :     /* send message */
    2440            0 :     if (dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &msg, "", "", verbose)) {
    2441            0 :         dlt_message_free_v2(&msg, 0);
    2442            0 :         return -1;
    2443              :     }
    2444              : 
    2445              :     /* free message */
    2446            0 :     dlt_message_free_v2(&msg, 0);
    2447              : 
    2448            0 :     return 0;
    2449              : }
    2450              : 
    2451            1 : int dlt_daemon_control_message_connection_info(
    2452              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, uint8_t state, char* comid, int verbose)
    2453              : {
    2454              :     DltMessage msg;
    2455              :     DltServiceConnectionInfo* resp;
    2456              : 
    2457            1 :     PRINT_FUNCTION_VERBOSE(verbose);
    2458              : 
    2459            1 :     if (daemon == 0)
    2460              :         return -1;
    2461              : 
    2462              :     /* initialise new message */
    2463            1 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR)
    2464              :         return -1;
    2465              : 
    2466              :     /* prepare payload of data */
    2467            1 :     msg.datasize = sizeof(DltServiceConnectionInfo);
    2468              : 
    2469            1 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2470            0 :         free(msg.databuffer);
    2471            0 :         msg.databuffer = 0;
    2472              :     }
    2473              : 
    2474            1 :     if (msg.databuffer == 0) {
    2475            1 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2476            1 :         msg.databuffersize = msg.datasize;
    2477              :     }
    2478              : 
    2479            1 :     if (msg.databuffer == 0)
    2480              :         return -1;
    2481              : 
    2482              :     resp = (DltServiceConnectionInfo*)msg.databuffer;
    2483            1 :     resp->service_id = DLT_SERVICE_ID_CONNECTION_INFO;
    2484            1 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    2485            1 :     resp->state = state;
    2486            1 :     dlt_set_id(resp->comid, comid);
    2487              : 
    2488              :     /* send message */
    2489            1 :     if (dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, "", "", verbose)) {
    2490            0 :         dlt_message_free(&msg, 0);
    2491            0 :         return -1;
    2492              :     }
    2493              : 
    2494              :     /* free message */
    2495            1 :     dlt_message_free(&msg, 0);
    2496              : 
    2497            1 :     return 0;
    2498              : }
    2499              : 
    2500            0 : int dlt_daemon_control_message_connection_info_v2(
    2501              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, uint8_t state, char* comid, int verbose)
    2502              : {
    2503              :     DltMessageV2 msg;
    2504              :     DltServiceConnectionInfo* resp;
    2505              : 
    2506            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2507              : 
    2508            0 :     if (daemon == 0)
    2509              :         return -1;
    2510              : 
    2511              :     /* initialise new message */
    2512            0 :     if (dlt_message_init_v2(&msg, 0) == DLT_RETURN_ERROR)
    2513              :         return -1;
    2514              : 
    2515              :     /* prepare payload of data */
    2516            0 :     msg.datasize = sizeof(DltServiceConnectionInfo);
    2517              : 
    2518            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2519            0 :         free(msg.databuffer);
    2520            0 :         msg.databuffer = 0;
    2521              :     }
    2522              : 
    2523            0 :     if (msg.databuffer == 0) {
    2524            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2525            0 :         msg.databuffersize = msg.datasize;
    2526              :     }
    2527              : 
    2528            0 :     if (msg.databuffer == 0)
    2529              :         return -1;
    2530              : 
    2531              :     resp = (DltServiceConnectionInfo*)msg.databuffer;
    2532            0 :     resp->service_id = DLT_SERVICE_ID_CONNECTION_INFO;
    2533            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    2534            0 :     resp->state = state;
    2535            0 :     dlt_set_id(resp->comid, comid);
    2536              : 
    2537              :     /* send message */
    2538            0 :     if (dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &msg, "", "", verbose)) {
    2539            0 :         dlt_message_free_v2(&msg, 0);
    2540            0 :         return -1;
    2541              :     }
    2542              : 
    2543              :     /* free message */
    2544            0 :     dlt_message_free_v2(&msg, 0);
    2545              : 
    2546            0 :     return 0;
    2547              : }
    2548              : 
    2549            0 : int dlt_daemon_control_message_timezone(int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    2550              : {
    2551              :     DltMessage msg;
    2552              :     DltServiceTimezone* resp;
    2553              : 
    2554            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2555              : 
    2556            0 :     if (daemon == 0)
    2557              :         return -1;
    2558              : 
    2559              :     /* initialise new message */
    2560            0 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR)
    2561              :         return -1;
    2562              : 
    2563              :     /* prepare payload of data */
    2564            0 :     msg.datasize = sizeof(DltServiceTimezone);
    2565              : 
    2566            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2567            0 :         free(msg.databuffer);
    2568            0 :         msg.databuffer = 0;
    2569              :     }
    2570              : 
    2571            0 :     if (msg.databuffer == 0) {
    2572            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2573            0 :         msg.databuffersize = msg.datasize;
    2574              :     }
    2575              : 
    2576            0 :     if (msg.databuffer == 0)
    2577              :         return -1;
    2578              : 
    2579              :     resp = (DltServiceTimezone*)msg.databuffer;
    2580            0 :     resp->service_id = DLT_SERVICE_ID_TIMEZONE;
    2581            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    2582              : 
    2583            0 :     time_t t = time(NULL);
    2584              :     struct tm lt;
    2585            0 :     tzset();
    2586            0 :     localtime_r(&t, &lt);
    2587              : #if defined(__USE_BSD) || defined(__USE_GNU) || defined(__APPLE__)
    2588            0 :     resp->timezone = (int32_t)lt.tm_gmtoff;
    2589              : #else
    2590              :     /* Portable fallback: timezone is the difference in seconds between UTC and local time */
    2591              :     {
    2592              :         struct tm gmt;
    2593              :         gmtime_r(&t, &gmt);
    2594              :         resp->timezone = (int32_t)difftime(mktime(&lt), mktime(&gmt));
    2595              :     }
    2596              : #endif
    2597            0 :     resp->isdst = (uint8_t)lt.tm_isdst;
    2598              : 
    2599              :     /* send message */
    2600            0 :     if (dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, "", "", verbose)) {
    2601            0 :         dlt_message_free(&msg, 0);
    2602            0 :         return -1;
    2603              :     }
    2604              : 
    2605              :     /* free message */
    2606            0 :     dlt_message_free(&msg, 0);
    2607              : 
    2608            0 :     return 0;
    2609              : }
    2610              : 
    2611            0 : int dlt_daemon_control_message_timezone_v2(int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    2612              : {
    2613              :     DltMessageV2 msg;
    2614              :     DltServiceTimezone* resp;
    2615              : 
    2616            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2617              : 
    2618            0 :     if (daemon == 0)
    2619              :         return -1;
    2620              : 
    2621              :     /* initialise new message */
    2622            0 :     if (dlt_message_init_v2(&msg, 0) == DLT_RETURN_ERROR)
    2623              :         return -1;
    2624              : 
    2625              :     /* prepare payload of data */
    2626            0 :     msg.datasize = sizeof(DltServiceTimezone);
    2627              : 
    2628            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2629            0 :         free(msg.databuffer);
    2630            0 :         msg.databuffer = 0;
    2631              :     }
    2632              : 
    2633            0 :     if (msg.databuffer == 0) {
    2634            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2635            0 :         msg.databuffersize = msg.datasize;
    2636              :     }
    2637              : 
    2638            0 :     if (msg.databuffer == 0)
    2639              :         return -1;
    2640              : 
    2641              :     resp = (DltServiceTimezone*)msg.databuffer;
    2642            0 :     resp->service_id = DLT_SERVICE_ID_TIMEZONE;
    2643            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    2644              : 
    2645            0 :     time_t t = time(NULL);
    2646              :     struct tm lt;
    2647            0 :     tzset();
    2648            0 :     localtime_r(&t, &lt);
    2649              : #if !defined(__CYGWIN__)
    2650            0 :     resp->timezone = (int32_t)lt.tm_gmtoff;
    2651              : #endif
    2652            0 :     resp->isdst = (uint8_t)lt.tm_isdst;
    2653              : 
    2654              :     /* send message */
    2655            0 :     if (dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &msg, "", "", verbose)) {
    2656            0 :         dlt_message_free_v2(&msg, 0);
    2657            0 :         return -1;
    2658              :     }
    2659              : 
    2660              :     /* free message */
    2661            0 :     dlt_message_free_v2(&msg, 0);
    2662              : 
    2663            0 :     return 0;
    2664              : }
    2665              : 
    2666            0 : int dlt_daemon_control_message_marker(int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    2667              : {
    2668              :     DltMessage msg;
    2669              :     DltServiceMarker* resp;
    2670              : 
    2671            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2672              : 
    2673            0 :     if (daemon == 0)
    2674              :         return -1;
    2675              : 
    2676              :     /* initialise new message */
    2677            0 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR)
    2678              :         return -1;
    2679              : 
    2680              :     /* prepare payload of data */
    2681            0 :     msg.datasize = sizeof(DltServiceMarker);
    2682              : 
    2683            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
    2684            0 :         free(msg.databuffer);
    2685            0 :         msg.databuffer = 0;
    2686              :     }
    2687              : 
    2688            0 :     if (msg.databuffer == 0) {
    2689            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    2690            0 :         msg.databuffersize = msg.datasize;
    2691              :     }
    2692              : 
    2693            0 :     if (msg.databuffer == 0)
    2694              :         return -1;
    2695              : 
    2696              :     resp = (DltServiceMarker*)msg.databuffer;
    2697            0 :     resp->service_id = DLT_SERVICE_ID_MARKER;
    2698            0 :     resp->status = DLT_SERVICE_RESPONSE_OK;
    2699              : 
    2700              :     /* send message */
    2701            0 :     if (dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, "", "", verbose)) {
    2702            0 :         dlt_message_free(&msg, 0);
    2703            0 :         return -1;
    2704              :     }
    2705              : 
    2706              :     /* free message */
    2707            0 :     dlt_message_free(&msg, 0);
    2708              : 
    2709            0 :     return 0;
    2710              : }
    2711              : 
    2712            0 : void dlt_daemon_control_callsw_cinjection(
    2713              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    2714              : {
    2715              :     char apid[DLT_ID_SIZE], ctid[DLT_ID_SIZE];
    2716              :     uint32_t id = 0, id_tmp = 0;
    2717              :     uint8_t* ptr;
    2718              :     DltDaemonContext* context;
    2719              :     uint32_t data_length_inject = 0;
    2720              :     uint32_t data_length_inject_tmp = 0;
    2721              : 
    2722              :     int32_t datalength;
    2723              : 
    2724              :     DltUserHeader userheader;
    2725              :     DltUserControlMsgInjection usercontext;
    2726              :     uint8_t* userbuffer;
    2727              : 
    2728            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2729              : 
    2730            0 :     if ((daemon == NULL) || (daemon_local == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    2731            0 :         return;
    2732              : 
    2733            0 :     datalength = (int32_t)msg->datasize;
    2734              :     ptr = msg->databuffer;
    2735              : 
    2736            0 :     DLT_MSG_READ_VALUE(id_tmp, ptr, datalength, uint32_t); /* Get service id */
    2737            0 :     id = DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp);
    2738              : 
    2739              :     /* injectionMode is disabled */
    2740            0 :     if (daemon_local->flags.injectionMode == 0) {
    2741            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_PERM_DENIED, verbose);
    2742            0 :         return;
    2743              :     }
    2744              : 
    2745              :     /* id is always less than DLT_DAEMON_INJECTION_MAX since its type is uinit32_t */
    2746            0 :     if (id >= DLT_DAEMON_INJECTION_MIN) {
    2747              :         /* This a a real SW-C injection call */
    2748              :         data_length_inject = 0;
    2749              :         data_length_inject_tmp = 0;
    2750              : 
    2751            0 :         DLT_MSG_READ_VALUE(data_length_inject_tmp, ptr, datalength, uint32_t); /* Get data length */
    2752            0 :         data_length_inject = DLT_ENDIAN_GET_32(msg->standardheader->htyp, data_length_inject_tmp);
    2753              : 
    2754              :         /* Get context handle for apid, ctid (and seid) */
    2755              :         /* Warning: seid is ignored in this implementation! */
    2756            0 :         if (DLT_IS_HTYP_UEH(msg->standardheader->htyp)) {
    2757            0 :             dlt_set_id(apid, msg->extendedheader->apid);
    2758            0 :             dlt_set_id(ctid, msg->extendedheader->ctid);
    2759              :         } else {
    2760              :             /* No extended header, and therefore no apid and ctid available */
    2761            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2762            0 :             return;
    2763              :         }
    2764              : 
    2765              :         /* At this point, apid and ctid is available */
    2766            0 :         context = dlt_daemon_context_find(daemon, apid, ctid, daemon->ecuid, verbose);
    2767              : 
    2768            0 :         if (context == 0) {
    2769              :             /* dlt_log(LOG_INFO,"No context found!\n"); */
    2770            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2771            0 :             return;
    2772              :         }
    2773              : 
    2774              :         /* Send user message to handle, specified in context */
    2775            0 :         if (dlt_user_set_userheader(&userheader, DLT_USER_MESSAGE_INJECTION) < DLT_RETURN_OK) {
    2776            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2777            0 :             return;
    2778              :         }
    2779              : 
    2780            0 :         usercontext.log_level_pos = context->log_level_pos;
    2781              : 
    2782            0 :         if (data_length_inject > (uint32_t)msg->databuffersize) {
    2783            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2784            0 :             return;
    2785              :         }
    2786              : 
    2787            0 :         userbuffer = malloc(data_length_inject);
    2788              : 
    2789            0 :         if (userbuffer == 0) {
    2790            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2791            0 :             return;
    2792              :         }
    2793              : 
    2794            0 :         usercontext.data_length_inject = (uint32_t)data_length_inject;
    2795            0 :         usercontext.service_id = id;
    2796              : 
    2797              :         memcpy(userbuffer, ptr, (size_t)data_length_inject); /* Copy received injection to send buffer */
    2798              : 
    2799              :         /* write to FIFO */
    2800            0 :         DltReturnValue ret = dlt_user_log_out3_with_timeout(
    2801              :             context->user_handle, &(userheader), sizeof(DltUserHeader), &(usercontext),
    2802              :             sizeof(DltUserControlMsgInjection), userbuffer, (size_t)data_length_inject);
    2803              : 
    2804            0 :         if (ret < DLT_RETURN_OK) {
    2805            0 :             if (ret == DLT_RETURN_PIPE_ERROR) {
    2806              :                 /* Close connection */
    2807            0 :                 close(context->user_handle);
    2808            0 :                 context->user_handle = DLT_FD_INIT;
    2809              :             }
    2810              : 
    2811            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2812              :         } else {
    2813            0 :             dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    2814              :         }
    2815              : 
    2816            0 :         free(userbuffer);
    2817              :         userbuffer = 0;
    2818              : 
    2819              :     } else {
    2820              :         /* Invalid ID */
    2821            0 :         dlt_daemon_control_service_response(
    2822              :             sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    2823              :     }
    2824              : }
    2825              : 
    2826            0 : void dlt_daemon_control_callsw_cinjection_v2(
    2827              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    2828              : {
    2829              :     uint8_t apidlen = 0, ctidlen = 0;
    2830              :     char* apid = NULL;
    2831              :     char* ctid = NULL;
    2832              :     uint32_t id = 0, id_tmp = 0;
    2833              :     uint8_t* ptr;
    2834              :     DltDaemonContext* context;
    2835              :     uint32_t data_length_inject = 0;
    2836              :     uint32_t data_length_inject_tmp = 0;
    2837              : 
    2838              :     int32_t datalength;
    2839              : 
    2840              :     DltUserHeader userheader;
    2841              :     DltUserControlMsgInjection usercontext;
    2842              :     uint8_t* userbuffer;
    2843              : 
    2844            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2845              : 
    2846            0 :     if ((daemon == NULL) || (daemon_local == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    2847            0 :         return;
    2848              : 
    2849            0 :     datalength = (int32_t)msg->datasize;
    2850              :     ptr = msg->databuffer;
    2851              : 
    2852            0 :     DLT_MSG_READ_VALUE(id_tmp, ptr, datalength, uint32_t); /* Get service id */
    2853              :     // id = DLT_ENDIAN_GET_32(msg->standardheader->htyp, id_tmp);
    2854              :     // TBD: Review endianness for V2, id extraction
    2855            0 :     id = DLT_ENDIAN_GET_32(msg->baseheaderv2->htyp2, id_tmp);
    2856              : 
    2857              :     /* injectionMode is disabled */
    2858            0 :     if (daemon_local->flags.injectionMode == 0) {
    2859            0 :         dlt_daemon_control_service_response_v2(
    2860              :             sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_PERM_DENIED, verbose);
    2861            0 :         return;
    2862              :     }
    2863              : 
    2864              :     /* id is always less than DLT_DAEMON_INJECTION_MAX since its type is uinit32_t */
    2865            0 :     if (id >= DLT_DAEMON_INJECTION_MIN) {
    2866              :         /* This a a real SW-C injection call */
    2867              :         data_length_inject = 0;
    2868              :         data_length_inject_tmp = 0;
    2869              : 
    2870            0 :         DLT_MSG_READ_VALUE(data_length_inject_tmp, ptr, datalength, uint32_t); /* Get data length */
    2871            0 :         data_length_inject = DLT_ENDIAN_GET_32(msg->baseheaderv2->htyp2, data_length_inject_tmp);
    2872              : 
    2873              :         /* Get context handle for apid, ctid (and seid) */
    2874              :         /* Warning: seid is ignored in this implementation! */
    2875              :         // TBD: Review EH(htyp2) vs UEH(htyp)
    2876            0 :         if (DLT_IS_HTYP2_EH(msg->baseheaderv2->htyp2)) {
    2877              :             // TBD: Check if apidlen and ctidlen are fetched properly in runtime
    2878            0 :             apidlen = msg->extendedheaderv2.apidlen;
    2879            0 :             dlt_set_id_v2(apid, msg->extendedheaderv2.apid, msg->extendedheaderv2.apidlen);
    2880            0 :             ctidlen = msg->extendedheaderv2.ctidlen;
    2881            0 :             dlt_set_id_v2(ctid, msg->extendedheaderv2.ctid, msg->extendedheaderv2.ctidlen);
    2882              :         } else {
    2883              :             /* No extended header, and therefore no apid and ctid available */
    2884            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2885            0 :             return;
    2886              :         }
    2887              : 
    2888              :         /* At this point, apid and ctid is available */
    2889            0 :         context = dlt_daemon_context_find_v2(
    2890            0 :             daemon, apidlen, apid, ctidlen, ctid, daemon->ecuid2len, daemon->ecuid2, verbose);
    2891              : 
    2892            0 :         if (context == 0) {
    2893              :             /* dlt_log(LOG_INFO,"No context found!\n"); */
    2894            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2895            0 :             return;
    2896              :         }
    2897              : 
    2898              :         /* Send user message to handle, specified in context */
    2899            0 :         if (dlt_user_set_userheader_v2(&userheader, DLT_USER_MESSAGE_INJECTION) < DLT_RETURN_OK) {
    2900            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2901            0 :             return;
    2902              :         }
    2903              : 
    2904            0 :         usercontext.log_level_pos = context->log_level_pos;
    2905              : 
    2906            0 :         if (data_length_inject > (uint32_t)msg->databuffersize) {
    2907            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2908            0 :             return;
    2909              :         }
    2910              : 
    2911            0 :         userbuffer = malloc(data_length_inject);
    2912              : 
    2913            0 :         if (userbuffer == 0) {
    2914            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2915            0 :             return;
    2916              :         }
    2917              : 
    2918            0 :         usercontext.data_length_inject = (uint32_t)data_length_inject;
    2919            0 :         usercontext.service_id = id;
    2920              : 
    2921              :         memcpy(userbuffer, ptr, (size_t)data_length_inject); /* Copy received injection to send buffer */
    2922              : 
    2923              :         /* write to FIFO */
    2924            0 :         DltReturnValue ret = dlt_user_log_out3_with_timeout(
    2925              :             context->user_handle, &(userheader), sizeof(DltUserHeader), &(usercontext),
    2926              :             sizeof(DltUserControlMsgInjection), userbuffer, (size_t)data_length_inject);
    2927              : 
    2928            0 :         if (ret < DLT_RETURN_OK) {
    2929            0 :             if (ret == DLT_RETURN_PIPE_ERROR) {
    2930              :                 /* Close connection */
    2931            0 :                 close(context->user_handle);
    2932            0 :                 context->user_handle = DLT_FD_INIT;
    2933              :             }
    2934              : 
    2935            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2936              :         } else {
    2937            0 :             dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    2938              :         }
    2939              : 
    2940            0 :         free(userbuffer);
    2941              :         userbuffer = 0;
    2942              : 
    2943              :     } else {
    2944              :         /* Invalid ID */
    2945            0 :         dlt_daemon_control_service_response_v2(
    2946              :             sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_NOT_SUPPORTED, verbose);
    2947              :     }
    2948              : }
    2949              : 
    2950            0 : void dlt_daemon_send_log_level(
    2951              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltDaemonContext* context, int8_t loglevel, int verbose)
    2952              : {
    2953            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2954              : 
    2955              :     int32_t id = DLT_SERVICE_ID_SET_LOG_LEVEL;
    2956              :     int8_t old_log_level = 0;
    2957              : 
    2958            0 :     old_log_level = context->log_level;
    2959            0 :     context->log_level = loglevel; /* No endianess conversion necessary*/
    2960              : 
    2961            0 :     if ((context->user_handle >= DLT_FD_MINIMUM) && (dlt_daemon_user_send_log_level(daemon, context, verbose) == 0)) {
    2962            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, (uint32_t)id, DLT_SERVICE_RESPONSE_OK, verbose);
    2963              :     } else {
    2964            0 :         dlt_log(LOG_ERR, "Log level could not be sent!\n");
    2965            0 :         context->log_level = old_log_level;
    2966            0 :         dlt_daemon_control_service_response(
    2967              :             sock, daemon, daemon_local, (uint32_t)id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2968              :     }
    2969            0 : }
    2970              : 
    2971            0 : void dlt_daemon_send_log_level_v2(
    2972              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltDaemonContext* context, int8_t loglevel, int verbose)
    2973              : {
    2974            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2975              : 
    2976              :     int32_t id = DLT_SERVICE_ID_SET_LOG_LEVEL;
    2977              :     int8_t old_log_level = 0;
    2978              : 
    2979            0 :     old_log_level = context->log_level;
    2980            0 :     context->log_level = loglevel; /* No endianess conversion necessary*/
    2981              : 
    2982            0 :     if ((context->user_handle >= DLT_FD_MINIMUM)
    2983            0 :         && (dlt_daemon_user_send_log_level_v2(daemon, context, verbose) == 0)) {
    2984            0 :         dlt_daemon_control_service_response_v2(
    2985              :             sock, daemon, daemon_local, (uint32_t)id, DLT_SERVICE_RESPONSE_OK, verbose);
    2986              :     } else {
    2987            0 :         dlt_log(LOG_ERR, "Log level could not be sent!\n");
    2988            0 :         context->log_level = old_log_level;
    2989            0 :         dlt_daemon_control_service_response_v2(
    2990              :             sock, daemon, daemon_local, (uint32_t)id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    2991              :     }
    2992            0 : }
    2993              : 
    2994            0 : void dlt_daemon_find_multiple_context_and_send_log_level(
    2995              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int8_t app_flag, char* str, int8_t len, int8_t loglevel,
    2996              :     int verbose)
    2997              : {
    2998            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    2999              : 
    3000              :     int count = 0;
    3001              :     DltDaemonContext* context = NULL;
    3002            0 :     char src_str[DLT_ID_SIZE + 1] = {0};
    3003              :     int ret = 0;
    3004              :     DltDaemonRegisteredUsers* user_list = NULL;
    3005              : 
    3006            0 :     if (daemon == 0) {
    3007            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
    3008            0 :         return;
    3009              :     }
    3010              : 
    3011            0 :     user_list = dlt_daemon_find_users_list(daemon, daemon->ecuid, verbose);
    3012              : 
    3013            0 :     if (user_list == NULL)
    3014              :         return;
    3015              : 
    3016            0 :     for (count = 0; count < user_list->num_contexts; count++) {
    3017            0 :         context = &(user_list->contexts[count]);
    3018              : 
    3019            0 :         if (context) {
    3020            0 :             if (app_flag == 1)
    3021            0 :                 strncpy(src_str, context->apid, DLT_ID_SIZE);
    3022              :             else
    3023            0 :                 strncpy(src_str, context->ctid, DLT_ID_SIZE);
    3024              : 
    3025            0 :             ret = strncmp(src_str, str, (size_t)len);
    3026              : 
    3027            0 :             if (ret == 0)
    3028            0 :                 dlt_daemon_send_log_level(sock, daemon, daemon_local, context, loglevel, verbose);
    3029            0 :             else if ((ret > 0) && (app_flag == 1))
    3030              :                 break;
    3031              :             else
    3032            0 :                 continue;
    3033              :         }
    3034              :     }
    3035              : }
    3036              : 
    3037            0 : void dlt_daemon_find_multiple_context_and_send_log_level_v2(
    3038              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int8_t app_flag, char* str, int8_t len, int8_t loglevel,
    3039              :     int verbose)
    3040              : {
    3041            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3042              : 
    3043              :     int count = 0;
    3044              :     DltDaemonContext* context = NULL;
    3045            0 :     char src_str[DLT_V2_ID_SIZE] = {0};
    3046              :     int ret = 0;
    3047              :     DltDaemonRegisteredUsers* user_list = NULL;
    3048              : 
    3049            0 :     if (daemon == 0) {
    3050            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
    3051            0 :         return;
    3052              :     }
    3053              : 
    3054            0 :     user_list = dlt_daemon_find_users_list_v2(daemon, daemon->ecuid2len, daemon->ecuid2, verbose);
    3055              : 
    3056            0 :     if (user_list == NULL)
    3057              :         return;
    3058              : 
    3059            0 :     for (count = 0; count < user_list->num_contexts; count++) {
    3060            0 :         context = &(user_list->contexts[count]);
    3061              : 
    3062            0 :         if (context) {
    3063            0 :             if (app_flag == 1)
    3064            0 :                 dlt_set_id_v2(src_str, context->apid2, context->apid2len);
    3065              :             else
    3066            0 :                 dlt_set_id_v2(src_str, context->ctid2, context->ctid2len);
    3067              : 
    3068            0 :             ret = strncmp(src_str, str, (size_t)len);
    3069              : 
    3070            0 :             if (ret == 0)
    3071            0 :                 dlt_daemon_send_log_level_v2(sock, daemon, daemon_local, context, loglevel, verbose);
    3072            0 :             else if ((ret > 0) && (app_flag == 1))
    3073              :                 break;
    3074              :             else
    3075            0 :                 continue;
    3076              :         }
    3077              :     }
    3078              : }
    3079              : 
    3080              : 
    3081            0 : void dlt_daemon_control_set_log_level(
    3082              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    3083              : {
    3084            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3085              : 
    3086            0 :     char apid[DLT_ID_SIZE + 1] = {0};
    3087            0 :     char ctid[DLT_ID_SIZE + 1] = {0};
    3088              :     DltServiceSetLogLevel* req = NULL;
    3089              :     DltDaemonContext* context = NULL;
    3090              :     int8_t apid_length = 0;
    3091              :     int8_t ctid_length = 0;
    3092              : 
    3093            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3094            0 :         return;
    3095              : 
    3096            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetLogLevel)) < 0)
    3097              :         return;
    3098              : 
    3099            0 :     req = (DltServiceSetLogLevel*)(msg->databuffer);
    3100              : 
    3101            0 :     if (daemon_local->flags.enforceContextLLAndTS)
    3102            0 :         req->log_level = (uint8_t)getStatus(req->log_level, daemon_local->flags.contextLogLevel);
    3103              : 
    3104            0 :     dlt_set_id(apid, req->apid);
    3105            0 :     dlt_set_id(ctid, req->ctid);
    3106            0 :     apid_length = (int8_t)strlen(apid);
    3107            0 :     ctid_length = (int8_t)strlen(ctid);
    3108              : 
    3109            0 :     if ((apid_length != 0) && (apid[apid_length - 1] == '*')
    3110            0 :         && (ctid[0] == 0)) { /*apid provided having '*' in it and ctid is null*/
    3111            0 :         dlt_daemon_find_multiple_context_and_send_log_level(
    3112            0 :             sock, daemon, daemon_local, 1, apid, (int8_t)(apid_length - 1), (int8_t)req->log_level, verbose);
    3113            0 :     } else if ((ctid_length != 0) && (ctid[ctid_length - 1] == '*') && (apid[0] == 0)) /*ctid provided is having '*' in
    3114              :                                                                                           it and apid is null*/
    3115              :     {
    3116            0 :         dlt_daemon_find_multiple_context_and_send_log_level(
    3117            0 :             sock, daemon, daemon_local, 0, ctid, (int8_t)(ctid_length - 1), (int8_t)req->log_level, verbose);
    3118            0 :     } else if ((apid_length != 0) && (apid[apid_length - 1] != '*') && (ctid[0] == 0)) /*only app id case*/
    3119              :     {
    3120            0 :         dlt_daemon_find_multiple_context_and_send_log_level(
    3121            0 :             sock, daemon, daemon_local, 1, apid, DLT_ID_SIZE, (int8_t)req->log_level, verbose);
    3122            0 :     } else if ((ctid_length != 0) && (ctid[ctid_length - 1] != '*') && (apid[0] == 0)) /*only context id case*/
    3123              :     {
    3124            0 :         dlt_daemon_find_multiple_context_and_send_log_level(
    3125            0 :             sock, daemon, daemon_local, 0, ctid, DLT_ID_SIZE, (int8_t)req->log_level, verbose);
    3126              :     } else {
    3127            0 :         context = dlt_daemon_context_find(daemon, apid, ctid, daemon->ecuid, verbose);
    3128              : 
    3129              :         /* Set log level */
    3130            0 :         if (context != 0) {
    3131            0 :             dlt_daemon_send_log_level(sock, daemon, daemon_local, context, (int8_t)req->log_level, verbose);
    3132              :         } else {
    3133            0 :             dlt_vlog(
    3134            0 :                 LOG_ERR, "Could not set log level: %d. Context [%.4s:%.4s] not found:", req->log_level, apid, ctid);
    3135            0 :             dlt_daemon_control_service_response(
    3136              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_SET_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3137              :         }
    3138              :     }
    3139              : }
    3140              : 
    3141            0 : void dlt_daemon_control_set_log_level_v2(
    3142              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    3143              : {
    3144            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3145              :     char* apid = NULL;
    3146              :     char* ctid = NULL;
    3147            0 :     DltServiceSetLogLevelV2 req = {0};
    3148              :     DltDaemonContext* context = NULL;
    3149              :     int8_t apid_length = 0;
    3150              :     int8_t ctid_length = 0;
    3151              :     int offset = 0;
    3152              : 
    3153            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3154            0 :         return;
    3155              : 
    3156            0 :     if (dlt_check_rcv_data_size(msg->datasize, DLT_SERVICE_SET_LOG_LEVEL_FIXED_SIZE_V2) < 0)
    3157              :         return;
    3158              : 
    3159            0 :     memcpy(&(req.service_id), msg->databuffer + offset, sizeof(uint32_t));
    3160              :     offset = offset + (int)sizeof(uint32_t);
    3161              :     memcpy(&(req.apidlen), msg->databuffer + offset, sizeof(uint8_t));
    3162              :     offset = offset + (int)sizeof(uint8_t);
    3163            0 :     dlt_set_id_v2(req.apid, (const char*)(msg->databuffer + offset), req.apidlen);
    3164            0 :     offset = offset + req.apidlen;
    3165            0 :     memcpy(&(req.ctidlen), msg->databuffer + offset, sizeof(uint8_t));
    3166            0 :     offset = offset + (int)sizeof(uint8_t);
    3167            0 :     dlt_set_id_v2(req.ctid, (const char*)(msg->databuffer + offset), req.ctidlen);
    3168            0 :     offset = offset + req.ctidlen;
    3169            0 :     memcpy(&(req.log_level), msg->databuffer + offset, sizeof(uint8_t));
    3170            0 :     offset = offset + (int)sizeof(uint8_t);
    3171            0 :     memcpy(&(req.com), msg->databuffer + offset, DLT_ID_SIZE);
    3172              : 
    3173            0 :     if (daemon_local->flags.enforceContextLLAndTS)
    3174            0 :         req.log_level = (uint8_t)getStatus(req.log_level, daemon_local->flags.contextLogLevel);
    3175              : 
    3176            0 :     apid_length = (int8_t)req.apidlen;
    3177            0 :     dlt_set_id_v2(apid, req.apid, req.apidlen);
    3178            0 :     ctid_length = (int8_t)req.ctidlen;
    3179            0 :     dlt_set_id_v2(ctid, req.ctid, req.ctidlen);
    3180              : 
    3181            0 :     if ((apid_length != 0) && (apid[apid_length - 1] == '*')
    3182              :         && (ctid == NULL)) { /*apid provided having '*' in it and ctid is null*/
    3183            0 :         dlt_daemon_find_multiple_context_and_send_log_level_v2(
    3184            0 :             sock, daemon, daemon_local, 1, apid, (int8_t)(apid_length - 1), (int8_t)req.log_level, verbose);
    3185            0 :     } else if ((ctid_length != 0) && (ctid[ctid_length - 1] == '*') && (apid == NULL)) /*ctid provided is having '*' in
    3186              :                                                                                           it and apid is null*/
    3187              :     {
    3188            0 :         dlt_daemon_find_multiple_context_and_send_log_level_v2(
    3189            0 :             sock, daemon, daemon_local, 0, ctid, (int8_t)(ctid_length - 1), (int8_t)req.log_level, verbose);
    3190            0 :     } else if ((apid_length != 0) && (apid[apid_length - 1] != '*') && (ctid == NULL)) /*only app id case*/
    3191              :     {
    3192            0 :         dlt_daemon_find_multiple_context_and_send_log_level_v2(
    3193            0 :             sock, daemon, daemon_local, 1, apid, apid_length, (int8_t)req.log_level, verbose);
    3194            0 :     } else if ((ctid_length != 0) && (ctid[ctid_length - 1] != '*') && (apid == NULL)) /*only context id case*/
    3195              :     {
    3196            0 :         dlt_daemon_find_multiple_context_and_send_log_level_v2(
    3197            0 :             sock, daemon, daemon_local, 0, ctid, ctid_length, (int8_t)req.log_level, verbose);
    3198              :     } else {
    3199            0 :         context = dlt_daemon_context_find_v2(
    3200            0 :             daemon, (uint8_t)apid_length, apid, (uint8_t)ctid_length, ctid, daemon->ecuid2len, daemon->ecuid2, verbose);
    3201              : 
    3202              :         /* Set log level */
    3203            0 :         if (context != 0) {
    3204            0 :             dlt_daemon_send_log_level_v2(sock, daemon, daemon_local, context, (int8_t)req.log_level, verbose);
    3205              :         } else {
    3206            0 :             dlt_vlog(
    3207            0 :                 LOG_ERR, "Could not set log level: %d. Context [%s:%s] not found:", req.log_level,
    3208              :                 apid ? apid : "<NULL>", ctid ? ctid : "<NULL>");
    3209            0 :             dlt_daemon_control_service_response_v2(
    3210              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_SET_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3211              :         }
    3212              :     }
    3213              : }
    3214              : 
    3215            0 : void dlt_daemon_send_trace_status(
    3216              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltDaemonContext* context, int8_t tracestatus,
    3217              :     int verbose)
    3218              : {
    3219            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3220              : 
    3221              :     int32_t id = DLT_SERVICE_ID_SET_TRACE_STATUS;
    3222              :     int8_t old_trace_status = 0;
    3223              : 
    3224            0 :     old_trace_status = context->trace_status;
    3225            0 :     context->trace_status = tracestatus; /* No endianess conversion necessary*/
    3226              : 
    3227            0 :     if ((context->user_handle >= DLT_FD_MINIMUM) && (dlt_daemon_user_send_log_level(daemon, context, verbose) == 0)) {
    3228            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, (uint32_t)id, DLT_SERVICE_RESPONSE_OK, verbose);
    3229              :     } else {
    3230            0 :         dlt_log(LOG_ERR, "Trace status could not be sent!\n");
    3231            0 :         context->trace_status = old_trace_status;
    3232            0 :         dlt_daemon_control_service_response(
    3233              :             sock, daemon, daemon_local, (uint32_t)id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3234              :     }
    3235            0 : }
    3236              : 
    3237            0 : void dlt_daemon_send_trace_status_v2(
    3238              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltDaemonContext* context, int8_t tracestatus,
    3239              :     int verbose)
    3240              : {
    3241            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3242              : 
    3243              :     int32_t id = DLT_SERVICE_ID_SET_TRACE_STATUS;
    3244              :     int8_t old_trace_status = 0;
    3245              : 
    3246            0 :     old_trace_status = context->trace_status;
    3247            0 :     context->trace_status = tracestatus; /* No endianess conversion necessary*/
    3248              : 
    3249            0 :     if ((context->user_handle >= DLT_FD_MINIMUM)
    3250            0 :         && (dlt_daemon_user_send_log_level_v2(daemon, context, verbose) == 0)) {
    3251            0 :         dlt_daemon_control_service_response_v2(
    3252              :             sock, daemon, daemon_local, (uint32_t)id, DLT_SERVICE_RESPONSE_OK, verbose);
    3253              :     } else {
    3254            0 :         dlt_log(LOG_ERR, "Trace status could not be sent!\n");
    3255            0 :         context->trace_status = old_trace_status;
    3256            0 :         dlt_daemon_control_service_response_v2(
    3257              :             sock, daemon, daemon_local, (uint32_t)id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3258              :     }
    3259            0 : }
    3260              : 
    3261            0 : void dlt_daemon_find_multiple_context_and_send_trace_status(
    3262              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int8_t app_flag, char* str, int8_t len,
    3263              :     int8_t tracestatus, int verbose)
    3264              : {
    3265            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3266              : 
    3267              :     int count = 0;
    3268              :     DltDaemonContext* context = NULL;
    3269            0 :     char src_str[DLT_ID_SIZE + 1] = {0};
    3270              :     int ret = 0;
    3271              :     DltDaemonRegisteredUsers* user_list = NULL;
    3272              : 
    3273            0 :     if (daemon == 0) {
    3274            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
    3275            0 :         return;
    3276              :     }
    3277              : 
    3278            0 :     user_list = dlt_daemon_find_users_list(daemon, daemon->ecuid, verbose);
    3279              : 
    3280            0 :     if (user_list == NULL)
    3281              :         return;
    3282              : 
    3283            0 :     for (count = 0; count < user_list->num_contexts; count++) {
    3284            0 :         context = &(user_list->contexts[count]);
    3285              : 
    3286            0 :         if (context) {
    3287            0 :             if (app_flag == 1)
    3288            0 :                 strncpy(src_str, context->apid, DLT_ID_SIZE);
    3289              :             else
    3290            0 :                 strncpy(src_str, context->ctid, DLT_ID_SIZE);
    3291              : 
    3292            0 :             ret = strncmp(src_str, str, (size_t)len);
    3293              : 
    3294            0 :             if (ret == 0)
    3295            0 :                 dlt_daemon_send_trace_status(sock, daemon, daemon_local, context, tracestatus, verbose);
    3296            0 :             else if ((ret > 0) && (app_flag == 1))
    3297              :                 break;
    3298              :             else
    3299            0 :                 continue;
    3300              :         }
    3301              :     }
    3302              : }
    3303              : 
    3304            0 : void dlt_daemon_find_multiple_context_and_send_trace_status_v2(
    3305              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int8_t app_flag, char* str, int8_t len,
    3306              :     int8_t tracestatus, int verbose)
    3307              : {
    3308            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3309              : 
    3310              :     int count = 0;
    3311              :     DltDaemonContext* context = NULL;
    3312            0 :     char src_str[DLT_V2_ID_SIZE] = {0};
    3313              :     int ret = 0;
    3314              :     DltDaemonRegisteredUsers* user_list = NULL;
    3315              : 
    3316            0 :     if (daemon == 0) {
    3317            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
    3318            0 :         return;
    3319              :     }
    3320              : 
    3321            0 :     user_list = dlt_daemon_find_users_list_v2(daemon, daemon->ecuid2len, daemon->ecuid2, verbose);
    3322              : 
    3323            0 :     if (user_list == NULL)
    3324              :         return;
    3325              : 
    3326            0 :     for (count = 0; count < user_list->num_contexts; count++) {
    3327            0 :         context = &(user_list->contexts[count]);
    3328              : 
    3329            0 :         if (context) {
    3330            0 :             if (app_flag == 1)
    3331            0 :                 dlt_set_id_v2(src_str, context->apid2, context->apid2len);
    3332              :             else
    3333            0 :                 dlt_set_id_v2(src_str, context->ctid2, context->ctid2len);
    3334              : 
    3335            0 :             ret = strncmp(src_str, str, (size_t)len);
    3336              : 
    3337            0 :             if (ret == 0)
    3338            0 :                 dlt_daemon_send_trace_status_v2(sock, daemon, daemon_local, context, tracestatus, verbose);
    3339            0 :             else if ((ret > 0) && (app_flag == 1))
    3340              :                 break;
    3341              :             else
    3342            0 :                 continue;
    3343              :         }
    3344              :     }
    3345              : }
    3346              : 
    3347            0 : void dlt_daemon_control_set_trace_status(
    3348              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    3349              : {
    3350            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3351              : 
    3352            0 :     char apid[DLT_ID_SIZE + 1] = {0};
    3353            0 :     char ctid[DLT_ID_SIZE + 1] = {0};
    3354              :     DltServiceSetLogLevel* req = NULL;
    3355              :     DltDaemonContext* context = NULL;
    3356              :     int8_t apid_length = 0;
    3357              :     int8_t ctid_length = 0;
    3358              : 
    3359            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3360            0 :         return;
    3361              : 
    3362            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetLogLevel)) < 0)
    3363              :         return;
    3364              : 
    3365            0 :     req = (DltServiceSetLogLevel*)(msg->databuffer);
    3366              : 
    3367            0 :     if (daemon_local->flags.enforceContextLLAndTS)
    3368            0 :         req->log_level = (uint8_t)getStatus(req->log_level, daemon_local->flags.contextTraceStatus);
    3369              : 
    3370            0 :     dlt_set_id(apid, req->apid);
    3371            0 :     dlt_set_id(ctid, req->ctid);
    3372            0 :     apid_length = (int8_t)strlen(apid);
    3373            0 :     ctid_length = (int8_t)strlen(ctid);
    3374              : 
    3375            0 :     if ((apid_length != 0) && (apid[apid_length - 1] == '*')
    3376            0 :         && (ctid[0] == 0)) { /*apid provided having '*' in it and ctid is null*/
    3377            0 :         dlt_daemon_find_multiple_context_and_send_trace_status(
    3378            0 :             sock, daemon, daemon_local, 1, apid, (int8_t)(apid_length - 1), (int8_t)req->log_level, verbose);
    3379            0 :     } else if ((ctid_length != 0) && (ctid[ctid_length - 1] == '*') && (apid[0] == 0)) /*ctid provided is having '*' in
    3380              :                                                                                           it and apid is null*/
    3381              : 
    3382              :     {
    3383            0 :         dlt_daemon_find_multiple_context_and_send_trace_status(
    3384            0 :             sock, daemon, daemon_local, 0, ctid, (int8_t)(ctid_length - 1), (int8_t)req->log_level, verbose);
    3385            0 :     } else if ((apid_length != 0) && (apid[apid_length - 1] != '*') && (ctid[0] == 0)) /*only app id case*/
    3386              :     {
    3387            0 :         dlt_daemon_find_multiple_context_and_send_trace_status(
    3388            0 :             sock, daemon, daemon_local, 1, apid, DLT_ID_SIZE, (int8_t)req->log_level, verbose);
    3389            0 :     } else if ((ctid_length != 0) && (ctid[ctid_length - 1] != '*') && (apid[0] == 0)) /*only context id case*/
    3390              :     {
    3391            0 :         dlt_daemon_find_multiple_context_and_send_trace_status(
    3392            0 :             sock, daemon, daemon_local, 0, ctid, DLT_ID_SIZE, (int8_t)req->log_level, verbose);
    3393              :     } else {
    3394            0 :         context = dlt_daemon_context_find(daemon, apid, ctid, daemon->ecuid, verbose);
    3395              : 
    3396              :         /* Set trace status */
    3397            0 :         if (context != 0) {
    3398            0 :             dlt_daemon_send_trace_status(sock, daemon, daemon_local, context, (int8_t)req->log_level, verbose);
    3399              :         } else {
    3400            0 :             dlt_vlog(
    3401            0 :                 LOG_ERR, "Could not set trace status: %d. Context [%.4s:%.4s] not found:", req->log_level, apid, ctid);
    3402            0 :             dlt_daemon_control_service_response(
    3403              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_SET_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3404              :         }
    3405              :     }
    3406              : }
    3407              : 
    3408            0 : void dlt_daemon_control_set_trace_status_v2(
    3409              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    3410              : {
    3411            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3412              : 
    3413              :     char* apid = NULL;
    3414              :     char* ctid = NULL;
    3415              :     DltServiceSetLogLevelV2* req = NULL;
    3416              :     DltDaemonContext* context = NULL;
    3417              :     int8_t apid_length = 0;
    3418              :     int8_t ctid_length = 0;
    3419              : 
    3420            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3421              :         return;
    3422              : 
    3423              :     // TBD: Review sizeof(DltServiceSetLogLevelV2)) or fixed size
    3424            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetLogLevelV2)) < 0)
    3425              :         return;
    3426              : 
    3427            0 :     req = (DltServiceSetLogLevelV2*)(msg->databuffer);
    3428              : 
    3429            0 :     if (daemon_local->flags.enforceContextLLAndTS)
    3430            0 :         req->log_level = (uint8_t)getStatus(req->log_level, daemon_local->flags.contextTraceStatus);
    3431              : 
    3432            0 :     apid_length = (int8_t)req->apidlen;
    3433            0 :     dlt_set_id_v2(apid, req->apid, req->apidlen);
    3434            0 :     ctid_length = (int8_t)req->ctidlen;
    3435            0 :     dlt_set_id_v2(ctid, req->ctid, req->ctidlen);
    3436              : 
    3437              :     // TBD: Review apid[apid_length - 1] == '*'
    3438            0 :     if ((apid_length != 0) && (apid[apid_length - 1] == '*')
    3439              :         && (ctid == NULL)) { /*apid provided having '*' in it and ctid is null*/
    3440            0 :         dlt_daemon_find_multiple_context_and_send_trace_status_v2(
    3441            0 :             sock, daemon, daemon_local, 1, apid, (int8_t)(apid_length - 1), (int8_t)req->log_level, verbose);
    3442            0 :     } else if ((ctid_length != 0) && (ctid[ctid_length - 1] == '*') && (apid == NULL)) /*ctid provided is having '*' in
    3443              :                                                                                           it and apid is null*/
    3444              : 
    3445              :     {
    3446            0 :         dlt_daemon_find_multiple_context_and_send_trace_status_v2(
    3447            0 :             sock, daemon, daemon_local, 0, ctid, (int8_t)(ctid_length - 1), (int8_t)req->log_level, verbose);
    3448            0 :     } else if ((apid_length != 0) && (apid[apid_length - 1] != '*') && (ctid == NULL)) /*only app id case*/
    3449              :     {
    3450            0 :         dlt_daemon_find_multiple_context_and_send_trace_status_v2(
    3451            0 :             sock, daemon, daemon_local, 1, apid, apid_length, (int8_t)req->log_level, verbose);
    3452            0 :     } else if ((ctid_length != 0) && (ctid[ctid_length - 1] != '*') && (apid == NULL)) /*only context id case*/
    3453              :     {
    3454            0 :         dlt_daemon_find_multiple_context_and_send_trace_status_v2(
    3455            0 :             sock, daemon, daemon_local, 0, ctid, ctid_length, (int8_t)req->log_level, verbose);
    3456              :     } else {
    3457            0 :         context = dlt_daemon_context_find_v2(
    3458            0 :             daemon, (uint8_t)apid_length, apid, (uint8_t)ctid_length, ctid, daemon->ecuid2len, daemon->ecuid2, verbose);
    3459              : 
    3460              :         /* Set trace status */
    3461            0 :         if (context != 0) {
    3462            0 :             dlt_daemon_send_trace_status_v2(sock, daemon, daemon_local, context, (int8_t)req->log_level, verbose);
    3463              :         } else {
    3464            0 :             dlt_vlog(
    3465            0 :                 LOG_ERR, "Could not set trace status: %d. Context [%.4s:%.4s] not found:", req->log_level,
    3466              :                 apid ? apid : "<NULL>", ctid ? ctid : "<NULL>");
    3467            0 :             dlt_daemon_control_service_response_v2(
    3468              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_SET_LOG_LEVEL, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3469              :         }
    3470              :     }
    3471              : }
    3472              : 
    3473            0 : void dlt_daemon_control_set_default_log_level(
    3474              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    3475              : {
    3476            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3477              : 
    3478              :     DltServiceSetDefaultLogLevel* req;
    3479              :     uint32_t id = DLT_SERVICE_ID_SET_DEFAULT_LOG_LEVEL;
    3480              : 
    3481            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3482              :         return;
    3483              : 
    3484            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetDefaultLogLevel)) < 0)
    3485              :         return;
    3486              : 
    3487            0 :     req = (DltServiceSetDefaultLogLevel*)(msg->databuffer);
    3488              : 
    3489              :     /* No endianess conversion necessary */
    3490            0 :     if (/*(req->log_level>=0) &&*/
    3491            0 :         (req->log_level <= DLT_LOG_VERBOSE)) {
    3492            0 :         if (daemon_local->flags.enforceContextLLAndTS)
    3493            0 :             daemon->default_log_level = getStatus(req->log_level, daemon_local->flags.contextLogLevel);
    3494              :         else
    3495            0 :             daemon->default_log_level = (int8_t)req->log_level; /* No endianess conversion necessary */
    3496              : 
    3497              :         /* Send Update to all contexts using the default log level */
    3498            0 :         dlt_daemon_user_send_default_update(daemon, verbose);
    3499              : 
    3500            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3501              :     } else {
    3502            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3503              :     }
    3504              : }
    3505              : 
    3506            0 : void dlt_daemon_control_set_default_log_level_v2(
    3507              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    3508              : {
    3509            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3510              : 
    3511              :     DltServiceSetDefaultLogLevel* req;
    3512              :     uint32_t id = DLT_SERVICE_ID_SET_DEFAULT_LOG_LEVEL;
    3513              : 
    3514            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3515              :         return;
    3516              : 
    3517            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetDefaultLogLevel)) < 0)
    3518              :         return;
    3519              : 
    3520            0 :     req = (DltServiceSetDefaultLogLevel*)(msg->databuffer);
    3521              : 
    3522              :     /* No endianess conversion necessary */
    3523            0 :     if (/*(req->log_level>=0) &&*/
    3524            0 :         (req->log_level <= DLT_LOG_VERBOSE)) {
    3525            0 :         if (daemon_local->flags.enforceContextLLAndTS)
    3526            0 :             daemon->default_log_level = getStatus(req->log_level, daemon_local->flags.contextLogLevel);
    3527              :         else
    3528            0 :             daemon->default_log_level = (int8_t)req->log_level; /* No endianess conversion necessary */
    3529              : 
    3530              :         /* Send Update to all contexts using the default log level */
    3531            0 :         dlt_daemon_user_send_default_update(daemon, verbose);
    3532              : 
    3533            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3534              :     } else {
    3535            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3536              :     }
    3537              : }
    3538              : 
    3539            0 : void dlt_daemon_control_set_all_log_level(
    3540              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    3541              : {
    3542            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3543              : 
    3544              :     DltServiceSetDefaultLogLevel* req = NULL;
    3545              :     uint32_t id = DLT_SERVICE_ID_SET_ALL_LOG_LEVEL;
    3546              :     int8_t loglevel = 0;
    3547              : 
    3548            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL)) {
    3549            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
    3550            0 :         return;
    3551              :     }
    3552              : 
    3553            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetDefaultLogLevel)) < 0)
    3554              :         return;
    3555              : 
    3556            0 :     req = (DltServiceSetDefaultLogLevel*)(msg->databuffer);
    3557              : 
    3558              :     /* No endianess conversion necessary */
    3559            0 :     if ((req != NULL) && ((req->log_level <= DLT_LOG_VERBOSE) || (req->log_level == (uint8_t)DLT_LOG_DEFAULT))) {
    3560            0 :         loglevel = (int8_t)req->log_level;
    3561              : 
    3562              :         /* Send Update to all contexts using the new log level */
    3563            0 :         dlt_daemon_user_send_all_log_level_update(
    3564            0 :             daemon, daemon_local->flags.enforceContextLLAndTS, (int8_t)daemon_local->flags.contextLogLevel, loglevel,
    3565              :             verbose);
    3566              : 
    3567            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3568              :     } else {
    3569            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3570              :     }
    3571              : }
    3572              : 
    3573            0 : void dlt_daemon_control_set_all_log_level_v2(
    3574              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    3575              : {
    3576            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3577              : 
    3578              :     DltServiceSetDefaultLogLevel* req = NULL;
    3579              :     uint32_t id = DLT_SERVICE_ID_SET_ALL_LOG_LEVEL;
    3580              :     int8_t loglevel = 0;
    3581              : 
    3582            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL)) {
    3583            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
    3584            0 :         return;
    3585              :     }
    3586              : 
    3587            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetDefaultLogLevel)) < 0)
    3588              :         return;
    3589              : 
    3590            0 :     req = (DltServiceSetDefaultLogLevel*)(msg->databuffer);
    3591              : 
    3592              :     /* No endianess conversion necessary */
    3593            0 :     if ((req != NULL) && ((req->log_level <= DLT_LOG_VERBOSE) || (req->log_level == (uint8_t)DLT_LOG_DEFAULT))) {
    3594            0 :         loglevel = (int8_t)req->log_level;
    3595              : 
    3596              :         /* Send Update to all contexts using the new log level */
    3597            0 :         dlt_daemon_user_send_all_log_level_update_v2(
    3598            0 :             daemon, daemon_local->flags.enforceContextLLAndTS, (int8_t)daemon_local->flags.contextLogLevel, loglevel,
    3599              :             verbose);
    3600              : 
    3601            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3602              :     } else {
    3603            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3604              :     }
    3605              : }
    3606              : 
    3607            0 : void dlt_daemon_control_set_default_trace_status(
    3608              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    3609              : {
    3610            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3611              : 
    3612              :     /* Payload of request message */
    3613              :     DltServiceSetDefaultLogLevel* req;
    3614              :     uint32_t id = DLT_SERVICE_ID_SET_DEFAULT_TRACE_STATUS;
    3615              : 
    3616            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3617              :         return;
    3618              : 
    3619            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetDefaultLogLevel)) < 0)
    3620              :         return;
    3621              : 
    3622            0 :     req = (DltServiceSetDefaultLogLevel*)(msg->databuffer);
    3623              : 
    3624              :     /* No endianess conversion necessary */
    3625            0 :     if ((req->log_level == DLT_TRACE_STATUS_OFF) || (req->log_level == DLT_TRACE_STATUS_ON)) {
    3626            0 :         if (daemon_local->flags.enforceContextLLAndTS)
    3627            0 :             daemon->default_trace_status = getStatus(req->log_level, daemon_local->flags.contextTraceStatus);
    3628              :         else
    3629            0 :             daemon->default_trace_status = (int8_t)req->log_level; /* No endianess conversion necessary*/
    3630              : 
    3631              :         /* Send Update to all contexts using the default trace status */
    3632            0 :         dlt_daemon_user_send_default_update(daemon, verbose);
    3633              : 
    3634            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3635              :     } else {
    3636            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3637              :     }
    3638              : }
    3639              : 
    3640            0 : void dlt_daemon_control_set_default_trace_status_v2(
    3641              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    3642              : {
    3643            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3644              : 
    3645              :     /* Payload of request message */
    3646              :     DltServiceSetDefaultLogLevel* req;
    3647              :     uint32_t id = DLT_SERVICE_ID_SET_DEFAULT_TRACE_STATUS;
    3648              : 
    3649            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3650              :         return;
    3651              : 
    3652            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetDefaultLogLevel)) < 0)
    3653              :         return;
    3654              : 
    3655            0 :     req = (DltServiceSetDefaultLogLevel*)(msg->databuffer);
    3656              : 
    3657              :     /* No endianess conversion necessary */
    3658            0 :     if ((req->log_level == DLT_TRACE_STATUS_OFF) || (req->log_level == DLT_TRACE_STATUS_ON)) {
    3659            0 :         if (daemon_local->flags.enforceContextLLAndTS)
    3660            0 :             daemon->default_trace_status = getStatus(req->log_level, daemon_local->flags.contextTraceStatus);
    3661              :         else
    3662            0 :             daemon->default_trace_status = (int8_t)req->log_level; /* No endianess conversion necessary*/
    3663              : 
    3664              :         /* Send Update to all contexts using the default trace status */
    3665            0 :         dlt_daemon_user_send_default_update_v2(daemon, verbose);
    3666              : 
    3667            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3668              :     } else {
    3669            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3670              :     }
    3671              : }
    3672              : 
    3673            0 : void dlt_daemon_control_set_all_trace_status(
    3674              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    3675              : {
    3676            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3677              : 
    3678              :     DltServiceSetDefaultLogLevel* req = NULL;
    3679              :     uint32_t id = DLT_SERVICE_ID_SET_ALL_TRACE_STATUS;
    3680              :     int8_t tracestatus = 0;
    3681              : 
    3682            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL)) {
    3683            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
    3684            0 :         return;
    3685              :     }
    3686              : 
    3687            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetDefaultLogLevel)) < 0)
    3688              :         return;
    3689              : 
    3690            0 :     req = (DltServiceSetDefaultLogLevel*)(msg->databuffer);
    3691              : 
    3692              :     /* No endianess conversion necessary */
    3693            0 :     if ((req != NULL)
    3694            0 :         && ((req->log_level <= DLT_TRACE_STATUS_ON) || (req->log_level == (uint8_t)DLT_TRACE_STATUS_DEFAULT))) {
    3695            0 :         if (daemon_local->flags.enforceContextLLAndTS)
    3696            0 :             tracestatus = getStatus(req->log_level, daemon_local->flags.contextTraceStatus);
    3697              :         else
    3698            0 :             tracestatus = (int8_t)req->log_level; /* No endianess conversion necessary */
    3699              : 
    3700              :         /* Send Update to all contexts using the new log level */
    3701            0 :         dlt_daemon_user_send_all_trace_status_update(daemon, tracestatus, verbose);
    3702              : 
    3703            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3704              :     } else {
    3705            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3706              :     }
    3707              : }
    3708              : 
    3709            0 : void dlt_daemon_control_set_all_trace_status_v2(
    3710              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    3711              : {
    3712            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3713              : 
    3714              :     DltServiceSetDefaultLogLevel* req = NULL;
    3715              :     uint32_t id = DLT_SERVICE_ID_SET_ALL_TRACE_STATUS;
    3716              :     int8_t tracestatus = 0;
    3717              : 
    3718            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL)) {
    3719            0 :         dlt_vlog(LOG_ERR, "%s: Invalid parameters\n", __func__);
    3720            0 :         return;
    3721              :     }
    3722              : 
    3723            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetDefaultLogLevel)) < 0)
    3724              :         return;
    3725              : 
    3726            0 :     req = (DltServiceSetDefaultLogLevel*)(msg->databuffer);
    3727              : 
    3728              :     /* No endianess conversion necessary */
    3729            0 :     if ((req != NULL)
    3730            0 :         && ((req->log_level <= DLT_TRACE_STATUS_ON) || (req->log_level == (uint8_t)DLT_TRACE_STATUS_DEFAULT))) {
    3731            0 :         if (daemon_local->flags.enforceContextLLAndTS)
    3732            0 :             tracestatus = getStatus(req->log_level, daemon_local->flags.contextTraceStatus);
    3733              :         else
    3734            0 :             tracestatus = (int8_t)req->log_level; /* No endianess conversion necessary */
    3735              : 
    3736              :         /* Send Update to all contexts using the new log level */
    3737            0 :         dlt_daemon_user_send_all_trace_status_update_v2(daemon, tracestatus, verbose);
    3738              : 
    3739            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3740              :     } else {
    3741            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3742              :     }
    3743              : }
    3744              : 
    3745            0 : void dlt_daemon_control_set_timing_packets(
    3746              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    3747              : {
    3748            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3749              : 
    3750              :     DltServiceSetVerboseMode* req; /* request uses same struct as set verbose mode */
    3751              :     uint32_t id = DLT_SERVICE_ID_SET_TIMING_PACKETS;
    3752              : 
    3753            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3754              :         return;
    3755              : 
    3756            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetVerboseMode)) < 0)
    3757              :         return;
    3758              : 
    3759            0 :     req = (DltServiceSetVerboseMode*)(msg->databuffer);
    3760              : 
    3761            0 :     if ((req->new_status == 0) || (req->new_status == 1)) {
    3762            0 :         daemon->timingpackets = req->new_status;
    3763              : 
    3764            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3765              :     } else {
    3766            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3767              :     }
    3768              : }
    3769              : 
    3770            0 : void dlt_daemon_control_set_timing_packets_v2(
    3771              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    3772              : {
    3773            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3774              : 
    3775              :     DltServiceSetVerboseMode* req; /* request uses same struct as set verbose mode */
    3776              :     uint32_t id = DLT_SERVICE_ID_SET_TIMING_PACKETS;
    3777              : 
    3778            0 :     if ((daemon == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    3779              :         return;
    3780              : 
    3781            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceSetVerboseMode)) < 0)
    3782              :         return;
    3783              : 
    3784            0 :     req = (DltServiceSetVerboseMode*)(msg->databuffer);
    3785              : 
    3786            0 :     if ((req->new_status == 0) || (req->new_status == 1)) {
    3787            0 :         daemon->timingpackets = req->new_status;
    3788              : 
    3789            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    3790              :     } else {
    3791            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    3792              :     }
    3793              : }
    3794              : 
    3795            0 : void dlt_daemon_control_message_time(int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    3796              : {
    3797              :     DltMessage msg;
    3798              :     int32_t len;
    3799              : 
    3800            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3801              : 
    3802            0 :     if (daemon == 0)
    3803            0 :         return;
    3804              : 
    3805              :     /* initialise new message */
    3806            0 :     if (dlt_message_init(&msg, 0) == DLT_RETURN_ERROR)
    3807              :         return;
    3808              : 
    3809              :     /* send message */
    3810              : 
    3811              :     /* prepare storage header */
    3812            0 :     msg.storageheader = (DltStorageHeader*)msg.headerbuffer;
    3813            0 :     dlt_set_storageheader(msg.storageheader, daemon->ecuid);
    3814              : 
    3815              :     /* prepare standard header */
    3816            0 :     msg.standardheader = (DltStandardHeader*)(msg.headerbuffer + sizeof(DltStorageHeader));
    3817            0 :     msg.standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_UEH | DLT_HTYP_PROTOCOL_VERSION1;
    3818              : 
    3819              : #if (BYTE_ORDER == BIG_ENDIAN)
    3820              :     msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_MSBF);
    3821              : #endif
    3822              : 
    3823            0 :     msg.standardheader->mcnt = 0;
    3824              : 
    3825              :     /* Set header extra parameters */
    3826            0 :     dlt_set_id(msg.headerextra.ecu, daemon->ecuid);
    3827            0 :     msg.headerextra.tmsp = dlt_uptime();
    3828              : 
    3829            0 :     dlt_message_set_extraparameters(&msg, verbose);
    3830              : 
    3831              :     /* prepare extended header */
    3832            0 :     msg.extendedheader = (DltExtendedHeader*)(msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader)
    3833            0 :                                               + DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp));
    3834            0 :     msg.extendedheader->msin = DLT_MSIN_CONTROL_TIME;
    3835              : 
    3836            0 :     msg.extendedheader->noar = 0;             /* number of arguments */
    3837            0 :     dlt_set_id(msg.extendedheader->apid, ""); /* application id */
    3838            0 :     dlt_set_id(msg.extendedheader->ctid, ""); /* context id */
    3839              : 
    3840              :     /* prepare length information */
    3841            0 :     msg.headersize = (int32_t)((size_t)sizeof(DltStorageHeader) + (size_t)sizeof(DltStandardHeader)
    3842              :                                + (size_t)sizeof(DltExtendedHeader)
    3843            0 :                                + (size_t)DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp));
    3844              : 
    3845            0 :     len = (int32_t)((size_t)msg.headersize - (size_t)sizeof(DltStorageHeader) + (size_t)msg.datasize);
    3846              : 
    3847            0 :     if (len > UINT16_MAX) {
    3848            0 :         dlt_log(LOG_WARNING, "Huge control message discarded!\n");
    3849              : 
    3850              :         /* free message */
    3851            0 :         dlt_message_free(&msg, 0);
    3852              : 
    3853            0 :         return;
    3854              :     }
    3855              : 
    3856            0 :     msg.standardheader->len = DLT_HTOBE_16(((uint16_t)len));
    3857              : 
    3858              :     /* Send message, ignore return value */
    3859            0 :     dlt_daemon_client_send(
    3860              :         sock, daemon, daemon_local, msg.headerbuffer, sizeof(DltStorageHeader),
    3861              :         msg.headerbuffer + sizeof(DltStorageHeader), (int)msg.headersize - (int)sizeof(DltStorageHeader),
    3862            0 :         msg.databuffer, (int)msg.datasize, verbose);
    3863              : 
    3864              :     /* free message */
    3865            0 :     dlt_message_free(&msg, 0);
    3866              : }
    3867              : 
    3868            0 : int dlt_daemon_process_one_s_timer(DltDaemon* daemon, DltDaemonLocal* daemon_local, DltReceiver* receiver, int verbose)
    3869              : {
    3870            0 :     uint64_t expir = 0;
    3871              :     ssize_t res = 0;
    3872              : 
    3873            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3874              : 
    3875            0 :     if ((daemon_local == NULL) || (daemon == NULL) || (receiver == NULL)) {
    3876            0 :         dlt_vlog(LOG_ERR, "%s: invalid parameters", __func__);
    3877            0 :         return -1;
    3878              :     }
    3879              : 
    3880            0 :     res = read(receiver->fd, &expir, sizeof(expir));
    3881              : 
    3882            0 :     if (res < 0) {
    3883            0 :         dlt_vlog(LOG_WARNING, "%s: Fail to read timer (%s)\n", __func__, strerror(errno));
    3884              :         /* Activity received on timer_wd, but unable to read the fd:
    3885              :          * let's go on sending notification */
    3886              :     }
    3887              : 
    3888            0 :     if ((daemon->state == DLT_DAEMON_STATE_SEND_BUFFER) || (daemon->state == DLT_DAEMON_STATE_BUFFER_FULL)) {
    3889            0 :         if (dlt_daemon_send_ringbuffer_to_client(daemon, daemon_local, daemon_local->flags.vflag))
    3890            0 :             dlt_log(LOG_DEBUG, "Can't send contents of ring buffer to clients\n");
    3891              :     }
    3892              : 
    3893            0 :     if ((daemon->timingpackets) && (daemon->state == DLT_DAEMON_STATE_SEND_DIRECT))
    3894            0 :         dlt_daemon_control_message_time(DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, daemon_local->flags.vflag);
    3895              : 
    3896            0 :     dlt_log(LOG_DEBUG, "Timer timingpacket\n");
    3897              : 
    3898            0 :     return 0;
    3899              : }
    3900              : 
    3901            0 : int dlt_daemon_process_sixty_s_timer(
    3902              :     DltDaemon* daemon, DltDaemonLocal* daemon_local, DltReceiver* receiver, int verbose)
    3903              : {
    3904            0 :     uint64_t expir = 0;
    3905              :     ssize_t res = 0;
    3906              : 
    3907            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    3908              : 
    3909            0 :     if ((daemon_local == NULL) || (daemon == NULL) || (receiver == NULL)) {
    3910            0 :         dlt_vlog(LOG_ERR, "%s: invalid parameters", __func__);
    3911            0 :         return -1;
    3912              :     }
    3913              : 
    3914            0 :     res = read(receiver->fd, &expir, sizeof(expir));
    3915              : 
    3916            0 :     if (res < 0) {
    3917            0 :         dlt_vlog(LOG_WARNING, "%s: Fail to read timer (%s)\n", __func__, strerror(errno));
    3918              :         /* Activity received on timer_wd, but unable to read the fd:
    3919              :          * let's go on sending notification */
    3920              :     }
    3921              : 
    3922            0 :     if (daemon_local->flags.sendECUSoftwareVersion > 0) {
    3923            0 :         if (daemon->daemon_version == DLTProtocolV2) {
    3924            0 :             dlt_daemon_control_get_software_version_v2(
    3925              :                 DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, daemon_local->flags.vflag);
    3926            0 :         } else if (daemon->daemon_version == DLTProtocolV1) {
    3927            0 :             dlt_daemon_control_get_software_version(
    3928              :                 DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, daemon_local->flags.vflag);
    3929              :         } else {
    3930            0 :             dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
    3931            0 :             return -1;
    3932              :         }
    3933              :     }
    3934              : 
    3935            0 :     if (daemon_local->flags.sendTimezone > 0) {
    3936              :         /* send timezone information */
    3937            0 :         time_t t = time(NULL);
    3938              :         struct tm lt;
    3939              : 
    3940              :         /*Added memset to avoid compiler warning for near initialization */
    3941              :         memset((void*)&lt, 0, sizeof(lt));
    3942            0 :         tzset();
    3943            0 :         localtime_r(&t, &lt);
    3944            0 :         if (daemon->daemon_version == DLTProtocolV2) {
    3945            0 :             dlt_daemon_control_message_timezone_v2(
    3946              :                 DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, daemon_local->flags.vflag);
    3947            0 :         } else if (daemon->daemon_version == DLTProtocolV1) {
    3948            0 :             dlt_daemon_control_message_timezone(
    3949              :                 DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, daemon_local->flags.vflag);
    3950              :         } else {
    3951            0 :             dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
    3952            0 :             return -1;
    3953              :         }
    3954              :     }
    3955              : 
    3956            0 :     dlt_log(LOG_DEBUG, "Timer ecuversion\n");
    3957              : 
    3958            0 :     return 0;
    3959              : }
    3960              : 
    3961              : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
    3962              : ssize_t dlt_daemon_process_systemd_timer(
    3963              :     DltDaemon* daemon, DltDaemonLocal* daemon_local, DltReceiver* receiver, int verbose)
    3964              : {
    3965              :     uint64_t expir = 0;
    3966              :     ssize_t res = -1;
    3967              : 
    3968              :     PRINT_FUNCTION_VERBOSE(verbose);
    3969              : 
    3970              :     if ((daemon_local == NULL) || (daemon == NULL) || (receiver == NULL)) {
    3971              :         dlt_vlog(LOG_ERR, "%s: invalid parameters", __func__);
    3972              :         return res;
    3973              :     }
    3974              : 
    3975              :     res = read(receiver->fd, &expir, sizeof(expir));
    3976              : 
    3977              :     if (res < 0) {
    3978              :         dlt_vlog(LOG_WARNING, "Failed to read timer_wd; %s\n", strerror(errno));
    3979              :         /* Activity received on timer_wd, but unable to read the fd:
    3980              :          * let's go on sending notification */
    3981              :     }
    3982              : 
    3983              : #ifdef DLT_SYSTEMD_WATCHDOG_ENFORCE_MSG_RX_ENABLE
    3984              :     if (!daemon->received_message_since_last_watchdog_interval) {
    3985              :         dlt_log(LOG_WARNING, "No new messages received since last watchdog timer run\n");
    3986              :         return 0;
    3987              :     }
    3988              :     daemon->received_message_since_last_watchdog_interval = 0;
    3989              : #endif
    3990              : 
    3991              :     dlt_daemon_trigger_systemd_watchdog_if_necessary(daemon);
    3992              : 
    3993              :     dlt_log(LOG_DEBUG, "Timer watchdog\n");
    3994              : 
    3995              :     return 0;
    3996              : }
    3997              : #else
    3998            0 : ssize_t dlt_daemon_process_systemd_timer(
    3999              :     DltDaemon* daemon, DltDaemonLocal* daemon_local, DltReceiver* receiver, int verbose)
    4000              : {
    4001              :     (void)daemon;
    4002              :     (void)daemon_local;
    4003              :     (void)receiver;
    4004              :     (void)verbose;
    4005              : 
    4006            0 :     dlt_log(LOG_DEBUG, "Timer watchdog not enabled\n");
    4007              : 
    4008            0 :     return -1;
    4009              : }
    4010              : #endif
    4011              : 
    4012            1 : void dlt_daemon_control_service_logstorage(
    4013              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    4014              : {
    4015              :     DltServiceOfflineLogstorage* req = NULL;
    4016              :     int ret = 0;
    4017              :     unsigned int connection_type = 0;
    4018              :     DltLogStorage* device = NULL;
    4019              :     int device_index = -1;
    4020              :     uint32_t i = 0;
    4021              : 
    4022              :     int tmp_errno = 0;
    4023              : 
    4024            1 :     struct stat daemon_mpoint_st = {0};
    4025              :     int daemon_st_status = 0;
    4026              : 
    4027            1 :     struct stat req_mpoint_st = {0};
    4028              :     int req_st_status = 0;
    4029              : 
    4030            1 :     PRINT_FUNCTION_VERBOSE(verbose);
    4031              : 
    4032            1 :     if ((daemon == NULL) || (msg == NULL) || (daemon_local == NULL)) {
    4033            0 :         dlt_vlog(LOG_ERR, "%s: Invalid function parameters\n", __func__);
    4034            0 :         return;
    4035              :     }
    4036              : 
    4037            1 :     if ((daemon_local->flags.offlineLogstorageMaxDevices <= 0) || (msg->databuffer == NULL)) {
    4038            0 :         dlt_daemon_control_service_response(
    4039              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4040              : 
    4041            0 :         dlt_log(LOG_INFO, "Logstorage functionality not enabled or MAX device set is 0\n");
    4042            0 :         return;
    4043              :     }
    4044              : 
    4045            1 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceOfflineLogstorage)) < 0)
    4046              :         return;
    4047              : 
    4048            1 :     req = (DltServiceOfflineLogstorage*)(msg->databuffer);
    4049              : 
    4050            1 :     if (req->connection_type != DLT_OFFLINE_LOGSTORAGE_SYNC_CACHES) {
    4051            0 :         req_st_status = stat(req->mount_point, &req_mpoint_st);
    4052            0 :         tmp_errno = errno;
    4053            0 :         if (req_st_status < 0) {
    4054            0 :             dlt_daemon_control_service_response(
    4055              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4056              : 
    4057            0 :             dlt_vlog(
    4058              :                 LOG_WARNING, "%s: Failed to stat requested mount point [%s] with error [%s]\n", __func__,
    4059              :                 req->mount_point, strerror(tmp_errno));
    4060            0 :             return;
    4061              :         }
    4062              :     }
    4063              : 
    4064            2 :     for (i = 0; i < (uint32_t)daemon_local->flags.offlineLogstorageMaxDevices; i++) {
    4065            1 :         connection_type = daemon->storage_handle[i].connection_type;
    4066              : 
    4067              :         memset(&daemon_mpoint_st, 0, sizeof(struct stat));
    4068            1 :         if (strlen(daemon->storage_handle[i].device_mount_point) > 1) {
    4069            0 :             daemon_st_status = stat(daemon->storage_handle[i].device_mount_point, &daemon_mpoint_st);
    4070            0 :             tmp_errno = errno;
    4071              : 
    4072            0 :             if (daemon_st_status < 0) {
    4073            0 :                 dlt_daemon_control_service_response(
    4074              :                     sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4075            0 :                 dlt_vlog(
    4076              :                     LOG_WARNING, "%s: Failed to stat daemon mount point [%s] with error [%s]\n", __func__,
    4077            0 :                     daemon->storage_handle[i].device_mount_point, strerror(tmp_errno));
    4078            0 :                 return;
    4079              :             }
    4080              : 
    4081              :             /* Check if the requested device path is already used as log storage device */
    4082            0 :             if (req_mpoint_st.st_dev == daemon_mpoint_st.st_dev && req_mpoint_st.st_ino == daemon_mpoint_st.st_ino) {
    4083            0 :                 device_index = (int)i;
    4084            0 :                 break;
    4085              :             }
    4086              :         }
    4087              : 
    4088              :         /* Get first available device index here */
    4089            1 :         if ((connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) && (device_index == -1))
    4090            0 :             device_index = (int)i;
    4091              :     }
    4092              : 
    4093              :     /* It might be possible to sync all caches of all devices */
    4094            1 :     if ((req->connection_type == DLT_OFFLINE_LOGSTORAGE_SYNC_CACHES) && (strlen(req->mount_point) == 0)) {
    4095              :         /* It is expected to receive an empty mount point to sync all Logstorage
    4096              :          * devices in this case. */
    4097            0 :     } else if (device_index == -1) {
    4098            0 :         dlt_daemon_control_service_response(
    4099              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4100            0 :         dlt_log(LOG_WARNING, "MAX devices already in use  \n");
    4101            0 :         return;
    4102              :     }
    4103              : 
    4104              :     /* Check for device connection request from log storage ctrl app  */
    4105            1 :     device = &daemon->storage_handle[device_index];
    4106              : 
    4107            1 :     if (req->connection_type == DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) {
    4108            0 :         ret = dlt_logstorage_device_connected(device, req->mount_point);
    4109              : 
    4110            0 :         if (ret == 1) {
    4111            0 :             dlt_daemon_control_service_response(
    4112              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_WARNING, verbose);
    4113            0 :             return;
    4114            0 :         } else if (ret != 0) {
    4115            0 :             dlt_daemon_control_service_response(
    4116              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4117            0 :             return;
    4118              :         }
    4119              : 
    4120            0 :         dlt_daemon_control_service_response(
    4121              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_OK, verbose);
    4122              : 
    4123              :         /* Update maintain logstorage loglevel if necessary */
    4124            0 :         if (daemon->storage_handle[device_index].maintain_logstorage_loglevel
    4125              :             != DLT_MAINTAIN_LOGSTORAGE_LOGLEVEL_UNDEF) {
    4126            0 :             daemon->maintain_logstorage_loglevel = daemon->storage_handle[device_index].maintain_logstorage_loglevel;
    4127              :         }
    4128              : 
    4129              :         /* Check if log level of running application needs an update */
    4130            0 :         dlt_daemon_logstorage_update_application_loglevel(daemon, daemon_local, device_index, verbose);
    4131              : 
    4132              :     }
    4133              :     /* Check for device disconnection request from log storage ctrl app  */
    4134            1 :     else if (req->connection_type == DLT_OFFLINE_LOGSTORAGE_DEVICE_DISCONNECTED) {
    4135              :         /* Check if log level of running application needs to be reset */
    4136            0 :         dlt_daemon_logstorage_reset_application_loglevel(
    4137              :             daemon, daemon_local, device_index, (int)daemon_local->flags.offlineLogstorageMaxDevices, verbose);
    4138              : 
    4139            0 :         dlt_logstorage_device_disconnected(
    4140            0 :             &(daemon->storage_handle[device_index]), DLT_LOGSTORAGE_SYNC_ON_DEVICE_DISCONNECT);
    4141              : 
    4142            0 :         dlt_daemon_control_service_response(
    4143              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_OK, verbose);
    4144              : 
    4145              :     }
    4146              :     /* Check for cache synchronization request from log storage ctrl app */
    4147            1 :     else if (req->connection_type == DLT_OFFLINE_LOGSTORAGE_SYNC_CACHES) {
    4148              :         ret = 0;
    4149              : 
    4150            1 :         if (device_index == -1) { /* sync all Logstorage devices */
    4151              : 
    4152            2 :             for (i = 0; i < (uint32_t)daemon_local->flags.offlineLogstorageMaxDevices; i++)
    4153            1 :                 if (daemon->storage_handle[i].connection_type == DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED)
    4154            1 :                     ret = dlt_daemon_logstorage_sync_cache(
    4155            1 :                         daemon, daemon_local, daemon->storage_handle[i].device_mount_point, verbose);
    4156              :         } else {
    4157              :             /* trigger logstorage to sync caches */
    4158            0 :             ret = dlt_daemon_logstorage_sync_cache(daemon, daemon_local, req->mount_point, verbose);
    4159              :         }
    4160              : 
    4161            1 :         if (ret == 0)
    4162            1 :             dlt_daemon_control_service_response(
    4163              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_OK, verbose);
    4164              :         else
    4165            0 :             dlt_daemon_control_service_response(
    4166              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4167              :     } else {
    4168            0 :         dlt_daemon_control_service_response(
    4169              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4170              :     }
    4171              : }
    4172              : 
    4173            0 : void dlt_daemon_control_service_logstorage_v2(
    4174              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    4175              : {
    4176              :     DltServiceOfflineLogstorage* req = NULL;
    4177              :     int ret = 0;
    4178              :     unsigned int connection_type = 0;
    4179              :     DltLogStorage* device = NULL;
    4180              :     int device_index = -1;
    4181              :     uint32_t i = 0;
    4182              : 
    4183              :     int tmp_errno = 0;
    4184              : 
    4185            0 :     struct stat daemon_mpoint_st = {0};
    4186              :     int daemon_st_status = 0;
    4187              : 
    4188            0 :     struct stat req_mpoint_st = {0};
    4189              :     int req_st_status = 0;
    4190              : 
    4191            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    4192              : 
    4193            0 :     if ((daemon == NULL) || (msg == NULL) || (daemon_local == NULL)) {
    4194            0 :         dlt_vlog(LOG_ERR, "%s: Invalid function parameters\n", __func__);
    4195            0 :         return;
    4196              :     }
    4197              : 
    4198            0 :     if ((daemon_local->flags.offlineLogstorageMaxDevices <= 0) || (msg->databuffer == NULL)) {
    4199            0 :         dlt_daemon_control_service_response_v2(
    4200              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4201              : 
    4202            0 :         dlt_log(LOG_INFO, "Logstorage functionality not enabled or MAX device set is 0\n");
    4203            0 :         return;
    4204              :     }
    4205              : 
    4206            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServiceOfflineLogstorage)) < 0)
    4207              :         return;
    4208              : 
    4209            0 :     req = (DltServiceOfflineLogstorage*)(msg->databuffer);
    4210              : 
    4211            0 :     if (req->connection_type != DLT_OFFLINE_LOGSTORAGE_SYNC_CACHES) {
    4212            0 :         req_st_status = stat(req->mount_point, &req_mpoint_st);
    4213            0 :         tmp_errno = errno;
    4214            0 :         if (req_st_status < 0) {
    4215            0 :             dlt_daemon_control_service_response_v2(
    4216              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4217              : 
    4218            0 :             dlt_vlog(
    4219              :                 LOG_WARNING, "%s: Failed to stat requested mount point [%s] with error [%s]\n", __func__,
    4220              :                 req->mount_point, strerror(tmp_errno));
    4221            0 :             return;
    4222              :         }
    4223              :     }
    4224              : 
    4225            0 :     for (i = 0; i < (uint32_t)daemon_local->flags.offlineLogstorageMaxDevices; i++) {
    4226            0 :         connection_type = daemon->storage_handle[i].connection_type;
    4227              : 
    4228              :         memset(&daemon_mpoint_st, 0, sizeof(struct stat));
    4229            0 :         if (strlen(daemon->storage_handle[i].device_mount_point) > 1) {
    4230            0 :             daemon_st_status = stat(daemon->storage_handle[i].device_mount_point, &daemon_mpoint_st);
    4231            0 :             tmp_errno = errno;
    4232              : 
    4233            0 :             if (daemon_st_status < 0) {
    4234            0 :                 dlt_daemon_control_service_response_v2(
    4235              :                     sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4236            0 :                 dlt_vlog(
    4237              :                     LOG_WARNING, "%s: Failed to stat daemon mount point [%s] with error [%s]\n", __func__,
    4238            0 :                     daemon->storage_handle[i].device_mount_point, strerror(tmp_errno));
    4239            0 :                 return;
    4240              :             }
    4241              : 
    4242              :             /* Check if the requested device path is already used as log storage device */
    4243            0 :             if (req_mpoint_st.st_dev == daemon_mpoint_st.st_dev && req_mpoint_st.st_ino == daemon_mpoint_st.st_ino) {
    4244            0 :                 device_index = (int)i;
    4245            0 :                 break;
    4246              :             }
    4247              :         }
    4248              : 
    4249              :         /* Get first available device index here */
    4250            0 :         if ((connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) && (device_index == -1))
    4251            0 :             device_index = (int)i;
    4252              :     }
    4253              : 
    4254              :     /* It might be possible to sync all caches of all devices */
    4255            0 :     if ((req->connection_type == DLT_OFFLINE_LOGSTORAGE_SYNC_CACHES) && (strlen(req->mount_point) == 0)) {
    4256              :         /* It is expected to receive an empty mount point to sync all Logstorage
    4257              :          * devices in this case. */
    4258            0 :     } else if (device_index == -1) {
    4259            0 :         dlt_daemon_control_service_response_v2(
    4260              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4261            0 :         dlt_log(LOG_WARNING, "MAX devices already in use  \n");
    4262            0 :         return;
    4263              :     }
    4264              : 
    4265              :     /* Check for device connection request from log storage ctrl app  */
    4266            0 :     device = &daemon->storage_handle[device_index];
    4267              : 
    4268            0 :     if (req->connection_type == DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) {
    4269            0 :         ret = dlt_logstorage_device_connected(device, req->mount_point);
    4270              : 
    4271            0 :         if (ret == 1) {
    4272            0 :             dlt_daemon_control_service_response_v2(
    4273              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_WARNING, verbose);
    4274            0 :             return;
    4275            0 :         } else if (ret != 0) {
    4276            0 :             dlt_daemon_control_service_response_v2(
    4277              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4278            0 :             return;
    4279              :         }
    4280              : 
    4281            0 :         dlt_daemon_control_service_response_v2(
    4282              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_OK, verbose);
    4283              : 
    4284              :         /* Update maintain logstorage loglevel if necessary */
    4285            0 :         if (daemon->storage_handle[device_index].maintain_logstorage_loglevel
    4286              :             != DLT_MAINTAIN_LOGSTORAGE_LOGLEVEL_UNDEF) {
    4287            0 :             daemon->maintain_logstorage_loglevel = daemon->storage_handle[device_index].maintain_logstorage_loglevel;
    4288              :         }
    4289              : 
    4290              :         /* Check if log level of running application needs an update */
    4291            0 :         dlt_daemon_logstorage_update_application_loglevel_v2(daemon, daemon_local, device_index, verbose);
    4292              : 
    4293              :     }
    4294              :     /* Check for device disconnection request from log storage ctrl app  */
    4295            0 :     else if (req->connection_type == DLT_OFFLINE_LOGSTORAGE_DEVICE_DISCONNECTED) {
    4296              :         /* Check if log level of running application needs to be reset */
    4297            0 :         dlt_daemon_logstorage_reset_application_loglevel(
    4298              :             daemon, daemon_local, device_index, (int)daemon_local->flags.offlineLogstorageMaxDevices, verbose);
    4299              : 
    4300            0 :         dlt_logstorage_device_disconnected(
    4301            0 :             &(daemon->storage_handle[device_index]), DLT_LOGSTORAGE_SYNC_ON_DEVICE_DISCONNECT);
    4302              : 
    4303            0 :         dlt_daemon_control_service_response_v2(
    4304              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_OK, verbose);
    4305              : 
    4306              :     }
    4307              :     /* Check for cache synchronization request from log storage ctrl app */
    4308            0 :     else if (req->connection_type == DLT_OFFLINE_LOGSTORAGE_SYNC_CACHES) {
    4309              :         ret = 0;
    4310              : 
    4311            0 :         if (device_index == -1) { /* sync all Logstorage devices */
    4312              : 
    4313            0 :             for (i = 0; i < (uint32_t)daemon_local->flags.offlineLogstorageMaxDevices; i++)
    4314            0 :                 if (daemon->storage_handle[i].connection_type == DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED)
    4315            0 :                     ret = dlt_daemon_logstorage_sync_cache(
    4316            0 :                         daemon, daemon_local, daemon->storage_handle[i].device_mount_point, verbose);
    4317              :         } else {
    4318              :             /* trigger logstorage to sync caches */
    4319            0 :             ret = dlt_daemon_logstorage_sync_cache(daemon, daemon_local, req->mount_point, verbose);
    4320              :         }
    4321              : 
    4322            0 :         if (ret == 0)
    4323            0 :             dlt_daemon_control_service_response_v2(
    4324              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_OK, verbose);
    4325              :         else
    4326            0 :             dlt_daemon_control_service_response_v2(
    4327              :                 sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4328              :     } else {
    4329            0 :         dlt_daemon_control_service_response_v2(
    4330              :             sock, daemon, daemon_local, DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4331              :     }
    4332              : }
    4333              : 
    4334            0 : void dlt_daemon_control_passive_node_connect(
    4335              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessage* msg, int verbose)
    4336              : {
    4337            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    4338              : 
    4339              :     DltServicePassiveNodeConnect* req;
    4340              :     uint32_t id = DLT_SERVICE_ID_PASSIVE_NODE_CONNECT;
    4341              : 
    4342            0 :     if ((daemon == NULL) || (daemon_local == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    4343              :         return;
    4344              : 
    4345              :     /* return error, if gateway mode not enabled*/
    4346            0 :     if (daemon_local->flags.gatewayMode == 0) {
    4347            0 :         dlt_log(
    4348              :             LOG_WARNING,
    4349              :             "Received passive node connection status request, "
    4350              :             "but GatewayMode is disabled\n");
    4351              : 
    4352            0 :         dlt_daemon_control_service_response(
    4353              :             sock, daemon, daemon_local, DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS, DLT_SERVICE_RESPONSE_ERROR,
    4354              :             verbose);
    4355              : 
    4356            0 :         return;
    4357              :     }
    4358              : 
    4359            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServicePassiveNodeConnect)) < 0)
    4360              :         return;
    4361              : 
    4362            0 :     req = (DltServicePassiveNodeConnect*)msg->databuffer;
    4363              : 
    4364            0 :     if (dlt_gateway_process_on_demand_request(
    4365            0 :             &daemon_local->pGateway, daemon_local, req->node_id, (int)req->connection_status, verbose)
    4366              :         < 0)
    4367            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4368              :     else
    4369            0 :         dlt_daemon_control_service_response(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    4370              : }
    4371              : 
    4372            0 : void dlt_daemon_control_passive_node_connect_v2(
    4373              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, DltMessageV2* msg, int verbose)
    4374              : {
    4375            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    4376              : 
    4377              :     // TBD: Review node_id member of DltServicePassiveNodeConnect
    4378              :     DltServicePassiveNodeConnect* req;
    4379              :     uint32_t id = DLT_SERVICE_ID_PASSIVE_NODE_CONNECT;
    4380              : 
    4381            0 :     if ((daemon == NULL) || (daemon_local == NULL) || (msg == NULL) || (msg->databuffer == NULL))
    4382              :         return;
    4383              : 
    4384              :     /* return error, if gateway mode not enabled*/
    4385            0 :     if (daemon_local->flags.gatewayMode == 0) {
    4386            0 :         dlt_log(
    4387              :             LOG_WARNING,
    4388              :             "Received passive node connection status request, "
    4389              :             "but GatewayMode is disabled\n");
    4390              : 
    4391            0 :         dlt_daemon_control_service_response_v2(
    4392              :             sock, daemon, daemon_local, DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS, DLT_SERVICE_RESPONSE_ERROR,
    4393              :             verbose);
    4394              : 
    4395            0 :         return;
    4396              :     }
    4397              : 
    4398            0 :     if (dlt_check_rcv_data_size(msg->datasize, sizeof(DltServicePassiveNodeConnect)) < 0)
    4399              :         return;
    4400              : 
    4401            0 :     req = (DltServicePassiveNodeConnect*)msg->databuffer;
    4402              : 
    4403            0 :     if (dlt_gateway_process_on_demand_request(
    4404            0 :             &daemon_local->pGateway, daemon_local, req->node_id, (int)req->connection_status, verbose)
    4405              :         < 0)
    4406            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_ERROR, verbose);
    4407              :     else
    4408            0 :         dlt_daemon_control_service_response_v2(sock, daemon, daemon_local, id, DLT_SERVICE_RESPONSE_OK, verbose);
    4409              : }
    4410              : 
    4411            0 : void dlt_daemon_control_passive_node_connect_status_v2(
    4412              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    4413              : {
    4414              :     DltMessageV2 msg;
    4415              :     DltServicePassiveNodeConnectionInfo* resp;
    4416              :     DltGatewayConnection* con = NULL;
    4417              :     unsigned int i = 0;
    4418              : 
    4419            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    4420              : 
    4421            0 :     if ((daemon == NULL) || (daemon_local == NULL))
    4422            0 :         return;
    4423              : 
    4424            0 :     if (dlt_message_init_v2(&msg, verbose) == -1)
    4425              :         return;
    4426              : 
    4427              :     /* return error, if gateway mode not enabled*/
    4428            0 :     if (daemon_local->flags.gatewayMode == 0) {
    4429            0 :         dlt_log(
    4430              :             LOG_WARNING,
    4431              :             "Received passive node connection status request, "
    4432              :             "but GatewayMode is disabled\n");
    4433              : 
    4434            0 :         dlt_daemon_control_service_response_v2(
    4435              :             sock, daemon, daemon_local, DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS, DLT_SERVICE_RESPONSE_ERROR,
    4436              :             verbose);
    4437              : 
    4438            0 :         return;
    4439              :     }
    4440              : 
    4441              :     /* prepare payload of data */
    4442            0 :     msg.datasize = sizeof(DltServicePassiveNodeConnectionInfo);
    4443              : 
    4444            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize))
    4445            0 :         msg.databuffer = NULL;
    4446              : 
    4447            0 :     if (msg.databuffer == NULL) {
    4448            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    4449              : 
    4450            0 :         if (msg.databuffer == NULL) {
    4451            0 :             dlt_log(LOG_CRIT, "Cannot allocate memory for message response\n");
    4452            0 :             return;
    4453              :         }
    4454              : 
    4455            0 :         msg.databuffersize = msg.datasize;
    4456              :     }
    4457              : 
    4458            0 :     resp = (DltServicePassiveNodeConnectionInfo*)msg.databuffer;
    4459              :     memset(resp, 0, (size_t)msg.datasize);
    4460            0 :     resp->service_id = DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS;
    4461              :     resp->status = DLT_SERVICE_RESPONSE_OK;
    4462            0 :     resp->num_connections = (uint32_t)daemon_local->pGateway.num_connections;
    4463              : 
    4464            0 :     for (i = 0; i < resp->num_connections; i++) {
    4465            0 :         if ((i * DLT_ID_SIZE) > DLT_ENTRY_MAX) {
    4466            0 :             dlt_log(LOG_ERR, "Maximal message size reached. Skip further information\n");
    4467            0 :             break;
    4468              :         }
    4469              : 
    4470            0 :         con = &daemon_local->pGateway.connections[i];
    4471              : 
    4472            0 :         resp->connection_status[i] = con->status;
    4473              :         // TBD: Review node_id[i * con->ecuid2len]
    4474            0 :         memcpy(&resp->node_id[i * con->ecuid2len], con->ecuid2, con->ecuid2len);
    4475              :     }
    4476              : 
    4477            0 :     dlt_daemon_client_send_control_message_v2(sock, daemon, daemon_local, &msg, "", "", verbose);
    4478              :     /* free message */
    4479            0 :     dlt_message_free_v2(&msg, verbose);
    4480              : }
    4481              : 
    4482            0 : void dlt_daemon_control_passive_node_connect_status(
    4483              :     int sock, DltDaemon* daemon, DltDaemonLocal* daemon_local, int verbose)
    4484              : {
    4485              :     DltMessage msg;
    4486              :     DltServicePassiveNodeConnectionInfo* resp;
    4487              :     DltGatewayConnection* con = NULL;
    4488              :     unsigned int i = 0;
    4489              : 
    4490            0 :     PRINT_FUNCTION_VERBOSE(verbose);
    4491              : 
    4492            0 :     if ((daemon == NULL) || (daemon_local == NULL))
    4493            0 :         return;
    4494              : 
    4495            0 :     if (dlt_message_init(&msg, verbose) == -1)
    4496              :         return;
    4497              : 
    4498              :     /* return error, if gateway mode not enabled*/
    4499            0 :     if (daemon_local->flags.gatewayMode == 0) {
    4500            0 :         dlt_log(
    4501              :             LOG_WARNING,
    4502              :             "Received passive node connection status request, "
    4503              :             "but GatewayMode is disabled\n");
    4504              : 
    4505            0 :         dlt_daemon_control_service_response(
    4506              :             sock, daemon, daemon_local, DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS, DLT_SERVICE_RESPONSE_ERROR,
    4507              :             verbose);
    4508              : 
    4509            0 :         return;
    4510              :     }
    4511              : 
    4512              :     /* prepare payload of data */
    4513            0 :     msg.datasize = sizeof(DltServicePassiveNodeConnectionInfo);
    4514              : 
    4515            0 :     if (msg.databuffer && (msg.databuffersize < msg.datasize))
    4516            0 :         msg.databuffer = NULL;
    4517              : 
    4518            0 :     if (msg.databuffer == NULL) {
    4519            0 :         msg.databuffer = (uint8_t*)malloc((size_t)msg.datasize);
    4520              : 
    4521            0 :         if (msg.databuffer == NULL) {
    4522            0 :             dlt_log(LOG_CRIT, "Cannot allocate memory for message response\n");
    4523            0 :             return;
    4524              :         }
    4525              : 
    4526            0 :         msg.databuffersize = msg.datasize;
    4527              :     }
    4528              : 
    4529            0 :     resp = (DltServicePassiveNodeConnectionInfo*)msg.databuffer;
    4530              :     memset(resp, 0, (size_t)msg.datasize);
    4531            0 :     resp->service_id = DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS;
    4532              :     resp->status = DLT_SERVICE_RESPONSE_OK;
    4533            0 :     resp->num_connections = (uint32_t)daemon_local->pGateway.num_connections;
    4534              : 
    4535            0 :     for (i = 0; i < resp->num_connections; i++) {
    4536            0 :         if ((i * DLT_ID_SIZE) > DLT_ENTRY_MAX) {
    4537            0 :             dlt_log(LOG_ERR, "Maximal message size reached. Skip further information\n");
    4538            0 :             break;
    4539              :         }
    4540              : 
    4541            0 :         con = &daemon_local->pGateway.connections[i];
    4542              : 
    4543            0 :         resp->connection_status[i] = con->status;
    4544            0 :         memcpy(&resp->node_id[i * DLT_ID_SIZE], con->ecuid, DLT_ID_SIZE);
    4545              :     }
    4546              : 
    4547            0 :     dlt_daemon_client_send_control_message(sock, daemon, daemon_local, &msg, "", "", verbose);
    4548              :     /* free message */
    4549            0 :     dlt_message_free(&msg, verbose);
    4550              : }
        

Generated by: LCOV version 2.0-1