Diag-Client-Lib
doip_message.cpp
Go to the documentation of this file.
1 /* Diagnostic Client library
2 * Copyright (C) 2024 Avijit Dey
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8 #include "common/doip_message.h"
9 
10 namespace doip_client {
11 namespace {
12 
13 auto GetDoIPPayloadType(core_type::Span<std::uint8_t const> payload) noexcept -> std::uint16_t {
14  return (static_cast<std::uint16_t>(((payload[2u] & 0xFF) << 8) | (payload[3u] & 0xFF)));
15 }
16 
17 auto GetDoIPPayloadLength(core_type::Span<std::uint8_t const> payload) noexcept -> std::uint32_t {
18  return ((static_cast<std::uint32_t>(payload[4u] << 24) & 0xFF000000) |
19  (static_cast<std::uint32_t>(payload[5u] << 16) & 0x00FF0000) |
20  (static_cast<std::uint32_t>(payload[6u] << 8) & 0x0000FF00) |
21  (static_cast<std::uint32_t>(payload[7u] & 0x000000FF)));
22 }
23 
24 auto ConvertToAddr(core_type::Span<std::uint8_t const> payload) noexcept -> std::uint16_t {
25  return static_cast<std::uint16_t>(((payload[0u] << 8) & 0xFF00) | payload[1u]);
26 }
27 
28 } // namespace
29 
31  std::uint16_t host_port_number,
33  : host_ip_address_{host_ip_address},
34  host_port_number_{host_port_number},
35  protocol_version_{payload[0u]},
36  protocol_version_inv_{payload[1u]},
37  server_address_{0u},
38  client_address_{0u},
39  payload_type_{GetDoIPPayloadType(payload)},
40  payload_length_{GetDoIPPayloadLength(payload)} {
41  constexpr std::uint8_t kDoipHeaderSize{8u};
42  constexpr std::uint8_t kSourceAddressSize{4u};
43  if (message_type == MessageType::kTcp) {
45  &payload[kDoipHeaderSize + kSourceAddressSize],
46  payload.size() - (kDoipHeaderSize + kSourceAddressSize)};
47  } else {
48  payload_ = core_type::Span<std::uint8_t const>{&payload[kDoipHeaderSize],
49  payload.size() - kDoipHeaderSize};
50  }
51 
52  if (message_type == MessageType::kTcp) {
54  ConvertToAddr(core_type::Span<std::uint8_t const>{&payload[kDoipHeaderSize], 2u});
56  ConvertToAddr(core_type::Span<std::uint8_t const>{&payload[kDoipHeaderSize + 2u], 2u});
57  } // no client
58 }
59 
60 } // namespace doip_client
A view over a contiguous sequence of objects.
Definition: span.h:191
constexpr size_type size() const noexcept
Return the size of this Span.
Definition: span.h:526
DoipMessage(MessageType message_type, IpAddressType host_ip_address, std::uint16_t host_port_number, core_type::Span< std::uint8_t const > payload)
Constructs an instance of DoipMessage.
std::uint16_t client_address_
Store server address.
Definition: doip_message.h:149
core_type::Span< std::uint8_t const > payload_
Store payload.
Definition: doip_message.h:164
MessageType
Definition of message type.
Definition: doip_message.h:26
std::uint16_t server_address_
Store server address.
Definition: doip_message.h:144
std::string_view IpAddressType
Type alias of IP address type.
Definition: doip_message.h:36
auto GetDoIPPayloadLength(core_type::Span< std::uint8_t const > payload) noexcept -> std::uint32_t
auto ConvertToAddr(core_type::Span< std::uint8_t const > payload) noexcept -> std::uint16_t
auto GetDoIPPayloadType(core_type::Span< std::uint8_t const > payload) noexcept -> std::uint16_t