LCOV - code coverage report
Current view: top level - libs/bsw/cpp2ethernet/src/ip - to_str.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.6 % 27 25
Test Date: 2026-11-13 15:04:32 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /********************************************************************************
       2              :  * Copyright (c) 2025 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 "ip/to_str.h"
      12              : 
      13              : #include <etl/format.h>
      14              : 
      15              : namespace ip
      16              : {
      17              : namespace
      18              : {
      19              : /**
      20              :  * Formats into buffer, reserving one character for the terminating NUL (etl::format_to_n does
      21              :  * not terminate the output itself) and returns the span of the written characters.
      22              :  */
      23              : template<typename... Args>
      24              : ::etl::span<char>
      25           28 : formatTo(::etl::span<char> const& buffer, ::etl::format_string<Args...> fmt, Args&&... args)
      26              : {
      27           28 :     char* const begin = buffer.data();
      28              :     char* const end
      29           28 :         = ::etl::format_to_n(begin, buffer.size() - 1U, fmt, ::etl::forward<Args>(args)...);
      30           28 :     *end = '\0';
      31           28 :     return ::etl::span<char>(begin, static_cast<size_t>(end - begin));
      32              : }
      33              : 
      34              : } // namespace
      35              : 
      36            6 : ::etl::span<char> to_str(IPAddress const& ipAddr, ::etl::span<char> const& buffer)
      37              : {
      38            6 :     IPAddress::Family const family = addressFamilyOf(ipAddr);
      39            6 :     size_t const bufferSize        = buffer.size();
      40              : 
      41            6 :     if ((IPAddress::IPV4 == family) && (bufferSize >= IP4_MAX_STRING_LENGTH))
      42              :     {
      43            0 :         return formatTo(
      44              :             buffer,
      45              :             "{}.{}.{}.{}",
      46            5 :             static_cast<unsigned>(ipAddr.raw[internal::RAW_IP4_IDX + 0U]),
      47            5 :             static_cast<unsigned>(ipAddr.raw[internal::RAW_IP4_IDX + 1U]),
      48            5 :             static_cast<unsigned>(ipAddr.raw[internal::RAW_IP4_IDX + 2U]),
      49           15 :             static_cast<unsigned>(ipAddr.raw[internal::RAW_IP4_IDX + 3U]));
      50              :     }
      51              : #ifdef PLATFORM_SUPPORT_IPV6
      52              :     if ((IPAddress::IPV6 == family) && (bufferSize >= IP6_MAX_STRING_LENGTH))
      53              :     {
      54              :         return formatTo(
      55              :             buffer,
      56              :             "{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:"
      57              :             "02x}:{:02x}{:02x}",
      58              :             static_cast<unsigned>(ipAddr.raw[0U]),
      59              :             static_cast<unsigned>(ipAddr.raw[1U]),
      60              :             static_cast<unsigned>(ipAddr.raw[2U]),
      61              :             static_cast<unsigned>(ipAddr.raw[3U]),
      62              :             static_cast<unsigned>(ipAddr.raw[4U]),
      63              :             static_cast<unsigned>(ipAddr.raw[5U]),
      64              :             static_cast<unsigned>(ipAddr.raw[6U]),
      65              :             static_cast<unsigned>(ipAddr.raw[7U]),
      66              :             static_cast<unsigned>(ipAddr.raw[8U]),
      67              :             static_cast<unsigned>(ipAddr.raw[9U]),
      68              :             static_cast<unsigned>(ipAddr.raw[10U]),
      69              :             static_cast<unsigned>(ipAddr.raw[11U]),
      70              :             static_cast<unsigned>(ipAddr.raw[12U]),
      71              :             static_cast<unsigned>(ipAddr.raw[13U]),
      72              :             static_cast<unsigned>(ipAddr.raw[14U]),
      73              :             static_cast<unsigned>(ipAddr.raw[15U]));
      74              :     }
      75              : #endif
      76            1 :     return {};
      77              : }
      78              : 
      79           24 : ::etl::span<char> to_str(IPEndpoint const& endp, ::etl::span<char> const& buffer)
      80              : {
      81           24 :     IPAddress const& ipAddr        = endp.getAddress();
      82           24 :     IPAddress::Family const family = addressFamilyOf(ipAddr);
      83           24 :     size_t const bufferSize        = buffer.size();
      84              : 
      85           24 :     if ((IPAddress::IPV4 == family) && (bufferSize >= IP4_ENDPOINT_MAX_STRING_LENGTH))
      86              :     {
      87            0 :         return formatTo(
      88              :             buffer,
      89              :             "{}.{}.{}.{}:{}",
      90           23 :             static_cast<unsigned>(ipAddr.raw[internal::RAW_IP4_IDX + 0U]),
      91           23 :             static_cast<unsigned>(ipAddr.raw[internal::RAW_IP4_IDX + 1U]),
      92           23 :             static_cast<unsigned>(ipAddr.raw[internal::RAW_IP4_IDX + 2U]),
      93           23 :             static_cast<unsigned>(ipAddr.raw[internal::RAW_IP4_IDX + 3U]),
      94           69 :             static_cast<unsigned>(endp.getPort()));
      95              :     }
      96              : #ifdef PLATFORM_SUPPORT_IPV6
      97              :     if ((IPAddress::IPV6 == family) && (bufferSize >= IP6_ENDPOINT_MAX_STRING_LENGTH))
      98              :     {
      99              :         return formatTo(
     100              :             buffer,
     101              :             "[{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{"
     102              :             ":02x}:{:02x}{:02x}]:{}",
     103              :             static_cast<unsigned>(ipAddr.raw[0U]),
     104              :             static_cast<unsigned>(ipAddr.raw[1U]),
     105              :             static_cast<unsigned>(ipAddr.raw[2U]),
     106              :             static_cast<unsigned>(ipAddr.raw[3U]),
     107              :             static_cast<unsigned>(ipAddr.raw[4U]),
     108              :             static_cast<unsigned>(ipAddr.raw[5U]),
     109              :             static_cast<unsigned>(ipAddr.raw[6U]),
     110              :             static_cast<unsigned>(ipAddr.raw[7U]),
     111              :             static_cast<unsigned>(ipAddr.raw[8U]),
     112              :             static_cast<unsigned>(ipAddr.raw[9U]),
     113              :             static_cast<unsigned>(ipAddr.raw[10U]),
     114              :             static_cast<unsigned>(ipAddr.raw[11U]),
     115              :             static_cast<unsigned>(ipAddr.raw[12U]),
     116              :             static_cast<unsigned>(ipAddr.raw[13U]),
     117              :             static_cast<unsigned>(ipAddr.raw[14U]),
     118              :             static_cast<unsigned>(ipAddr.raw[15U]),
     119              :             static_cast<unsigned>(endp.getPort()));
     120              :     }
     121              : #endif
     122            1 :     return {};
     123              : }
     124              : 
     125              : } /* namespace ip */
        

Generated by: LCOV version 2.0-1