LCOV - code coverage report
Current view: top level - shared - dlt_user_shared.c (source / functions) Coverage Total Hit
Test: dlt_final_coverage.info Lines: 85.2 % 61 52
Test Date: 2026-08-27 10:11:10 Functions: 87.5 % 8 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 Alexander Wenzel <alexander.aw.wenzel@bmw.de>
      18              :  *
      19              :  * \copyright Copyright © 2011-2015 BMW AG. \n
      20              :  * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
      21              :  *
      22              :  * \file dlt_user_shared.c
      23              :  */
      24              : 
      25              : 
      26              : /*******************************************************************************
      27              : **                                                                            **
      28              : **  SRC-MODULE: dlt_user_shared.c                                             **
      29              : **                                                                            **
      30              : **  TARGET    : linux                                                         **
      31              : **                                                                            **
      32              : **  PROJECT   : DLT                                                           **
      33              : **                                                                            **
      34              : **  AUTHOR    : Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
      35              : **              Markus Klein                                                  **
      36              : **                                                                            **
      37              : **  PURPOSE   :                                                               **
      38              : **                                                                            **
      39              : **  REMARKS   :                                                               **
      40              : **                                                                            **
      41              : **  PLATFORM DEPENDANT [yes/no]: yes                                          **
      42              : **                                                                            **
      43              : **  TO BE CHANGED BY USER [yes/no]: no                                        **
      44              : **                                                                            **
      45              : *******************************************************************************/
      46              : 
      47              : /*******************************************************************************
      48              : **                      Author Identity                                       **
      49              : ********************************************************************************
      50              : **                                                                            **
      51              : ** Initials     Name                       Company                            **
      52              : ** --------     -------------------------  ---------------------------------- **
      53              : **  aw          Alexander Wenzel           BMW                                **
      54              : **  mk          Markus Klein               Fraunhofer ESK                     **
      55              : *******************************************************************************/
      56              : 
      57              : /*******************************************************************************
      58              : **                      Revision Control History                              **
      59              : *******************************************************************************/
      60              : 
      61              : /*
      62              :  * $LastChangedRevision: 1670 $
      63              :  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
      64              :  * $LastChangedBy$
      65              :  * Initials    Date         Comment
      66              :  * aw          13.01.2010   initial
      67              :  */
      68              : 
      69              : #include <sys/stat.h>
      70              : #include <fcntl.h>
      71              : #include <errno.h>
      72              : #include <poll.h>
      73              : 
      74              : #include <sys/uio.h> /* writev() */
      75              : 
      76              : #include "dlt_user_shared.h"
      77              : #include "dlt_user_shared_cfg.h"
      78              : 
      79         6766 : DltReturnValue dlt_user_set_userheader(DltUserHeader* userheader, uint32_t mtype)
      80              : {
      81         6766 :     if (userheader == 0)
      82              :         return DLT_RETURN_ERROR;
      83              : 
      84         6766 :     if (mtype <= 0)
      85              :         return DLT_RETURN_ERROR;
      86              : 
      87         6766 :     userheader->pattern[0] = 'D';
      88         6766 :     userheader->pattern[1] = 'U';
      89         6766 :     userheader->pattern[2] = 'H';
      90         6766 :     userheader->pattern[3] = 1;
      91         6766 :     userheader->message = mtype;
      92              : 
      93         6766 :     return DLT_RETURN_OK;
      94              : }
      95              : 
      96          142 : DltReturnValue dlt_user_set_userheader_v2(DltUserHeader* userheader, uint32_t mtype)
      97              : {
      98          142 :     if (userheader == 0)
      99              :         return DLT_RETURN_ERROR;
     100              : 
     101          142 :     if (mtype <= 0)
     102              :         return DLT_RETURN_ERROR;
     103              : 
     104          142 :     userheader->pattern[0] = 'D';
     105          142 :     userheader->pattern[1] = 'U';
     106          142 :     userheader->pattern[2] = 'H';
     107          142 :     userheader->pattern[3] = 2;
     108          142 :     userheader->message = mtype;
     109              : 
     110          142 :     return DLT_RETURN_OK;
     111              : }
     112              : 
     113         1338 : int dlt_user_check_userheader(DltUserHeader* userheader)
     114              : {
     115         1338 :     if (userheader == 0)
     116              :         return -1;
     117              : 
     118         1338 :     return (userheader->pattern[0] == 'D') && (userheader->pattern[1] == 'U') && (userheader->pattern[2] == 'H')
     119         2676 :            && ((userheader->pattern[3] == 1) || (userheader->pattern[3] == 2));
     120              : }
     121              : 
     122          150 : int dlt_get_version_from_userheader(DltUserHeader* userheader)
     123              : {
     124          150 :     if (userheader == 0)
     125              :         return -1;
     126              : 
     127          150 :     int version = userheader->pattern[3];
     128              : 
     129          150 :     return version;
     130              : }
     131              : 
     132          448 : DltReturnValue dlt_user_log_out2(int handle, void* ptr1, size_t len1, void* ptr2, size_t len2)
     133              : {
     134              :     struct iovec iov[2];
     135              :     uint32_t bytes_written;
     136              : 
     137          448 :     if (handle < 0)
     138              :         /* Invalid handle */
     139              :         return DLT_RETURN_ERROR;
     140              : 
     141          389 :     iov[0].iov_base = ptr1;
     142          389 :     iov[0].iov_len = len1;
     143          389 :     iov[1].iov_base = ptr2;
     144          389 :     iov[1].iov_len = len2;
     145              : 
     146          389 :     bytes_written = (uint32_t)writev(handle, iov, 2);
     147              : 
     148          389 :     if (bytes_written != (len1 + len2))
     149              :         return DLT_RETURN_ERROR;
     150              : 
     151              :     return DLT_RETURN_OK;
     152              : }
     153              : 
     154            8 : DltReturnValue dlt_user_log_out2_with_timeout(int handle, void* ptr1, size_t len1, void* ptr2, size_t len2)
     155              : {
     156            8 :     if (handle < 0)
     157              :         /* Invalid handle */
     158              :         return DLT_RETURN_ERROR;
     159              : 
     160              :     struct pollfd pfd;
     161            8 :     pfd.fd = handle;
     162            8 :     pfd.events = POLLOUT;
     163              : 
     164            8 :     if (poll(&pfd, 1, DLT_WRITEV_TIMEOUT_MS) < 0) {
     165              :         return DLT_RETURN_ERROR;
     166              :     }
     167              : 
     168            8 :     if (pfd.revents & POLLOUT) {
     169            8 :         return dlt_user_log_out2(handle, ptr1, len1, ptr2, len2);
     170              :     } else {
     171              :         return DLT_RETURN_ERROR;
     172              :     }
     173              : }
     174              : 
     175         6456 : DltReturnValue dlt_user_log_out3(int handle, void* ptr1, size_t len1, void* ptr2, size_t len2, void* ptr3, size_t len3)
     176              : {
     177              :     struct iovec iov[3];
     178              :     uint32_t bytes_written;
     179              : 
     180         6456 :     if (handle < 0)
     181              :         /* Invalid handle */
     182              :         return DLT_RETURN_ERROR;
     183              : 
     184         6375 :     iov[0].iov_base = ptr1;
     185         6375 :     iov[0].iov_len = len1;
     186         6375 :     iov[1].iov_base = ptr2;
     187         6375 :     iov[1].iov_len = len2;
     188         6375 :     iov[2].iov_base = ptr3;
     189         6375 :     iov[2].iov_len = len3;
     190              : 
     191         6375 :     bytes_written = (uint32_t)writev(handle, iov, 3);
     192              : 
     193         6375 :     if (bytes_written != (len1 + len2 + len3)) {
     194         1308 :         switch (errno) {
     195              :         case ETIMEDOUT: {
     196              :             return DLT_RETURN_PIPE_ERROR; /* ETIMEDOUT - connect timeout */
     197              :             break;
     198              :         }
     199              :         case EBADF: {
     200              :             return DLT_RETURN_PIPE_ERROR; /* EBADF - handle not open */
     201              :             break;
     202              :         }
     203              :         case EPIPE: {
     204              :             return DLT_RETURN_PIPE_ERROR; /* EPIPE - pipe error */
     205              :             break;
     206              :         }
     207            0 :         case EAGAIN: {
     208            0 :             return DLT_RETURN_PIPE_FULL; /* EAGAIN - data could not be written */
     209              :             break;
     210              :         }
     211              :         default: {
     212              :             break;
     213              :         }
     214              :         }
     215              : 
     216              :         return DLT_RETURN_ERROR;
     217              :     }
     218              : 
     219              :     return DLT_RETURN_OK;
     220              : }
     221              : 
     222            0 : DltReturnValue dlt_user_log_out3_with_timeout(
     223              :     int handle, void* ptr1, size_t len1, void* ptr2, size_t len2, void* ptr3, size_t len3)
     224              : {
     225            0 :     if (handle < 0)
     226              :         /* Invalid handle */
     227              :         return DLT_RETURN_ERROR;
     228              : 
     229              :     struct pollfd pfd;
     230            0 :     pfd.fd = handle;
     231            0 :     pfd.events = POLLOUT;
     232              : 
     233            0 :     if (poll(&pfd, 1, DLT_WRITEV_TIMEOUT_MS) < 0) {
     234              :         return DLT_RETURN_ERROR;
     235              :     }
     236              : 
     237            0 :     if (pfd.revents & POLLOUT) {
     238            0 :         return dlt_user_log_out3(handle, ptr1, len1, ptr2, len2, ptr3, len3);
     239              :     } else {
     240              :         return DLT_RETURN_ERROR;
     241              :     }
     242              : }
        

Generated by: LCOV version 2.0-1