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

Generated by: LCOV version 2.0-1