LCOV - code coverage report
Current view: top level - libs/bsw/cpp2someip/src - NetworkChannel.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.2 % 104 100
Test Date: 2026-09-11 12:05:06 Functions: 100.0 % 18 18

            Line data    Source code
       1              : /********************************************************************************
       2              :  * Copyright (c) 2026 Accenture
       3              :  *
       4              :  * This program and the accompanying materials are made available under the
       5              :  * terms of the Apache License Version 2.0 which is available at
       6              :  * https://www.apache.org/licenses/LICENSE-2.0
       7              :  *
       8              :  * SPDX-License-Identifier: Apache-2.0
       9              :  ********************************************************************************/
      10              : 
      11              : #include "someip/NetworkChannel.h"
      12              : 
      13              : #include "someip/NetworkResource.h"
      14              : #include "someip/logger.h"
      15              : 
      16              : // Logger API uses printf-style varargs for fixed diagnostic messages in this module.
      17              : // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg)
      18              : 
      19              : namespace someip
      20              : {
      21              : using ::util::logger::SOMEIP;
      22              : 
      23            9 : NetworkChannel::NetworkChannel()
      24            9 : : _pResource(nullptr), _remoteEndpoint(), _isMulticast(false), _magicCookieEnabled(false)
      25              : {
      26            9 :     _remoteEndpoint.setAddress(NetworkResource::INVALID_IP);
      27            9 :     _remoteEndpoint.setPort(port::INVALID);
      28            9 : }
      29              : 
      30          108 : NetworkChannel::NetworkChannel(
      31              :     NetworkResource& resource,
      32              :     ::ip::IPEndpoint const& remoteEndpoint,
      33              :     bool const isThisMulticast,
      34          108 :     bool const enableMagicCookie)
      35          108 : : _pResource(&resource)
      36          108 : , _remoteEndpoint(remoteEndpoint)
      37          108 : , _isMulticast(::ip::isMulticastAddress(remoteEndpoint.getAddress()) || isThisMulticast)
      38          108 : , _magicCookieEnabled(enableMagicCookie)
      39              : {
      40          108 :     if (_pResource != nullptr)
      41              :     {
      42          108 :         _pResource->incRefCounter();
      43              :     }
      44              :     else
      45              :     {
      46            0 :         ERROR_LOG(SOMEIP, "NetworkChannel: _pResource invalid!");
      47              :     }
      48          108 : }
      49              : 
      50           62 : NetworkChannel::NetworkChannel(NetworkChannel const& channel)
      51           62 : : _pResource(channel._pResource)
      52           62 : , _remoteEndpoint(channel._remoteEndpoint)
      53           62 : , _isMulticast(channel._isMulticast)
      54           62 : , _magicCookieEnabled(channel._magicCookieEnabled)
      55              : {
      56           62 :     if (_pResource != nullptr)
      57              :     {
      58           62 :         _pResource->incRefCounter();
      59              :     }
      60           62 : }
      61              : 
      62          502 : NetworkChannel::~NetworkChannel() { close(); }
      63              : 
      64            1 : NetworkChannel& NetworkChannel::operator=(NetworkChannel const& other)
      65              : {
      66            1 :     if (this != &other)
      67              :     {
      68            1 :         if (_pResource != other._pResource)
      69              :         {
      70            1 :             close();
      71            1 :             _pResource = other._pResource;
      72            1 :             if (_pResource != nullptr)
      73              :             {
      74            1 :                 _pResource->incRefCounter();
      75              :             }
      76              :         }
      77            1 :         _remoteEndpoint     = other._remoteEndpoint;
      78            1 :         _isMulticast        = other._isMulticast;
      79            1 :         _magicCookieEnabled = other._magicCookieEnabled;
      80              :     }
      81              : 
      82            1 :     return *this;
      83              : }
      84              : 
      85          323 : NetworkChannel::NetworkChannel(NetworkChannel&& other) noexcept
      86          323 : : _pResource(other._pResource)
      87          323 : , _remoteEndpoint(other._remoteEndpoint)
      88          323 : , _isMulticast(other._isMulticast)
      89          323 : , _magicCookieEnabled(other._magicCookieEnabled)
      90              : {
      91          323 :     other._pResource = nullptr;
      92          323 : }
      93              : 
      94            1 : NetworkChannel& NetworkChannel::operator=(NetworkChannel&& other) noexcept
      95              : {
      96            1 :     if (this != &other)
      97              :     {
      98            1 :         close();
      99            1 :         _pResource          = other._pResource;
     100            1 :         _remoteEndpoint     = other._remoteEndpoint;
     101            1 :         _isMulticast        = other._isMulticast;
     102            1 :         _magicCookieEnabled = other._magicCookieEnabled;
     103            1 :         other._pResource    = nullptr;
     104              :     }
     105            1 :     return *this;
     106              : }
     107              : 
     108           31 : ::ip::IPEndpoint const& NetworkChannel::getRemoteEndpoint() const { return _remoteEndpoint; }
     109              : 
     110           19 : ::etl::expected<uint16_t, PortError> NetworkChannel::getLocalPort() const
     111              : {
     112           19 :     if (_pResource == nullptr)
     113              :     {
     114            2 :         return ::etl::unexpected<PortError>(PortError::NOT_INITIALIZED);
     115              :     }
     116              : 
     117           18 :     return _pResource->getLocalPort();
     118              : }
     119              : 
     120           18 : uint8_t NetworkChannel::getProto() const
     121              : {
     122           18 :     if (_pResource == nullptr)
     123              :     {
     124            0 :         return SomeIpConstants::INVALID_PROTO;
     125              :     }
     126              : 
     127           18 :     return _pResource->getProto();
     128              : }
     129              : 
     130           23 : ::etl::span<uint8_t> NetworkChannel::getInputBuffer() const
     131              : {
     132           23 :     if (_pResource == nullptr)
     133              :     {
     134            1 :         return {};
     135              :     }
     136              : 
     137           22 :     return _pResource->getInputBuffer();
     138              : }
     139              : 
     140           65 : ::etl::span<uint8_t> NetworkChannel::getOutputBuffer() const
     141              : {
     142           65 :     if (_pResource == nullptr)
     143              :     {
     144            1 :         return {};
     145              :     }
     146              : 
     147           64 :     return _pResource->getOutputBuffer();
     148              : }
     149              : 
     150            2 : bool NetworkChannel::isOpen() const
     151              : {
     152            2 :     if (_pResource == nullptr)
     153              :     {
     154            1 :         return false;
     155              :     }
     156              : 
     157            1 :     return _pResource->isOpen();
     158              : }
     159              : 
     160            4 : bool NetworkChannel::isConnected() const
     161              : {
     162            4 :     if (_pResource == nullptr)
     163              :     {
     164            0 :         return false;
     165              :     }
     166              : 
     167            4 :     return _pResource->isConnected();
     168              : }
     169              : 
     170            4 : bool NetworkChannel::isMulticast() const
     171              : {
     172            4 :     if (_pResource == nullptr)
     173              :     {
     174            1 :         return false;
     175              :     }
     176              : 
     177            3 :     if (!_pResource->isOpen())
     178              :     {
     179            1 :         return false;
     180              :     }
     181              : 
     182            2 :     return _isMulticast;
     183              : }
     184              : 
     185          514 : void NetworkChannel::close()
     186              : {
     187          514 :     if (_pResource != nullptr)
     188              :     {
     189          171 :         _pResource->decRefCounter();
     190          171 :         _pResource->tryClose();
     191          171 :         _pResource = nullptr;
     192              :     }
     193          514 : }
     194              : 
     195           15 : bool NetworkChannel::send(uint32_t const length, ::etl::span<uint8_t> const& buffer)
     196              : {
     197           15 :     if (_pResource == nullptr)
     198              :     {
     199            0 :         return false;
     200              :     }
     201              : 
     202           15 :     auto const output = _pResource->getOutputBuffer();
     203           15 :     _pResource->setOutputBuffer(buffer);
     204              : 
     205           15 :     bool const result = send(length);
     206           15 :     _pResource->setOutputBuffer(output);
     207              : 
     208           15 :     return result;
     209              : }
     210              : 
     211           38 : bool NetworkChannel::send(uint32_t const length)
     212              : {
     213           38 :     if (_pResource == nullptr)
     214              :     {
     215            1 :         return false;
     216              :     }
     217              : 
     218           37 :     if (!_pResource->isOpen())
     219              :     {
     220            4 :         return false;
     221              :     }
     222              : 
     223           33 :     if (_pResource->isConnected())
     224              :     {
     225           10 :         return _pResource->send(length);
     226              :     }
     227              : 
     228           23 :     return _pResource->send(_remoteEndpoint, length);
     229              : }
     230              : 
     231              : } // namespace someip
     232              : 
     233              : // NOLINTEND(cppcoreguidelines-pro-type-vararg)
        

Generated by: LCOV version 2.0-1