Diag-Client-Lib
connection_manager.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 
10 
14 
15 namespace doip_client {
16 namespace connection {
17 
22  public:
27 
37  DoipTcpConnection(uds_transport::ConversionHandler const &conversation_handler, std::string_view tcp_ip_address,
38  std::uint16_t port_num)
39  : uds_transport::Connection{1, conversation_handler},
40  doip_tcp_channel_{tcp_ip_address, port_num, *this} {}
41 
45  ~DoipTcpConnection() final = default;
46 
52 
56  void Start() override { doip_tcp_channel_.Start(); }
57 
61  void Stop() override { doip_tcp_channel_.Stop(); }
62 
67  bool IsConnectToHost() override { return (doip_tcp_channel_.IsConnectToHost()); }
68 
76  uds_transport::UdsMessageConstPtr message) override {
77  return (doip_tcp_channel_.ConnectToHost(std::move(message)));
78  }
79 
86  }
87 
113  std::pair<uds_transport::UdsTransportProtocolMgr::IndicationResult, uds_transport::UdsMessagePtr> IndicateMessage(
117  core_type::Span<std::uint8_t> payload_info) override {
118  // Send Indication to conversation
119  return (conversation_handler_.IndicateMessage(source_addr, target_addr, type, channel_id, size, priority,
120  protocol_kind, payload_info));
121  }
122 
129  uds_transport::UdsMessageConstPtr message) override {
130  return doip_tcp_channel_.Transmit(std::move(message));
131  }
132 
140  // send full message to conversion
141  conversation_handler_.HandleMessage(std::move(message));
142  }
143 
144  private:
149 };
150 
155  public:
160 
170  DoipUdpConnection(uds_transport::ConversionHandler const &conversation_handler, std::string_view udp_ip_address,
171  std::uint16_t port_num)
172  : uds_transport::Connection(1, conversation_handler),
173  doip_udp_channel_{udp_ip_address, port_num, *this} {}
174 
178  ~DoipUdpConnection() final = default;
179 
185 
189  void Start() override { doip_udp_channel_.Start(); }
190 
194  void Stop() override { doip_udp_channel_.Stop(); }
195 
200  bool IsConnectToHost() override { return false; }
201 
210  }
211 
218  }
219 
245  std::pair<uds_transport::UdsTransportProtocolMgr::IndicationResult, uds_transport::UdsMessagePtr> IndicateMessage(
249  core_type::Span<std::uint8_t> payload_info) override {
250  // Send Indication to conversion
251  return (conversation_handler_.IndicateMessage(source_addr, target_addr, type, channel_id, size, priority,
252  protocol_kind, payload_info));
253  }
254 
261  uds_transport::UdsMessageConstPtr message) override {
262  return (doip_udp_channel_.Transmit(std::move(message)));
263  }
264 
272  // send full message to conversion
273  conversation_handler_.HandleMessage(std::move(message));
274  }
275 
276  private:
281 };
282 
283 std::unique_ptr<uds_transport::Connection> DoipConnectionManager::FindOrCreateTcpConnection(
284  uds_transport::ConversionHandler const &conversation, std::string_view tcp_ip_address, std::uint16_t port_num) {
285  return (std::make_unique<DoipTcpConnection>(conversation, tcp_ip_address, port_num));
286 }
287 
288 std::unique_ptr<uds_transport::Connection> DoipConnectionManager::FindOrCreateUdpConnection(
289  uds_transport::ConversionHandler const &conversation, std::string_view udp_ip_address, std::uint16_t port_num) {
290  return (std::make_unique<DoipUdpConnection>(conversation, udp_ip_address, port_num));
291 }
292 } // namespace connection
293 } // namespace doip_client
Class to manage a tcp channel as per DoIP protocol.
void Stop()
Function to stop the channel.
void Start()
Function to start the channel.
bool IsConnectToHost()
Function to check if connected to host remote server.
uds_transport::UdsTransportProtocolMgr::ConnectionResult ConnectToHost(uds_transport::UdsMessageConstPtr message)
Function to establish connection to remote host server.
uds_transport::UdsTransportProtocolMgr::DisconnectionResult DisconnectFromHost()
Function to disconnect from remote host server.
uds_transport::UdsTransportProtocolMgr::TransmissionResult Transmit(uds_transport::UdsMessageConstPtr message)
Function to transmit a valid Uds message.
Class to manage a udp channel as per DoIP protocol.
uds_transport::UdsTransportProtocolMgr::TransmissionResult Transmit(uds_transport::UdsMessageConstPtr message)
Function to transmit a Vehicle Identification request.
void Start()
Function to start the channel.
void Stop()
Function to stop the channel.
std::unique_ptr< uds_transport::Connection > FindOrCreateTcpConnection(uds_transport::ConversionHandler const &conversation, std::string_view tcp_ip_address, std::uint16_t port_num)
Function to find or create a new Tcp connection.
std::unique_ptr< uds_transport::Connection > FindOrCreateUdpConnection(uds_transport::ConversionHandler const &conversation, std::string_view udp_ip_address, std::uint16_t port_num)
Function to find or create a new Udp connection.
Doip Tcp Connection handle connection between two layers.
InitializationResult Initialize() override
Function to initialize the connection.
bool IsConnectToHost() override
Function to check if connected to host remote server.
DoipTcpConnection(uds_transport::ConversionHandler const &conversation_handler, std::string_view tcp_ip_address, std::uint16_t port_num)
Constructor to create a new tcp connection.
channel::tcp_channel::DoipTcpChannel doip_tcp_channel_
Store the reference to doip tcp channel.
void Start() override
Function to start the connection.
uds_transport::UdsTransportProtocolMgr::TransmissionResult Transmit(uds_transport::UdsMessageConstPtr message) override
Function to transmit a valid Uds message.
void HandleMessage(uds_transport::UdsMessagePtr message) override
Function to Hands over a valid received Uds message.
uds_transport::UdsTransportProtocolMgr::DisconnectionResult DisconnectFromHost() override
Function to disconnect from remote host server.
void Stop() override
Function to stop the connection.
uds_transport::UdsTransportProtocolMgr::ConnectionResult ConnectToHost(uds_transport::UdsMessageConstPtr message) override
Function to establish connection to remote host server.
~DoipTcpConnection() final=default
Destruct an instance of DoipTcpConnection.
std::pair< uds_transport::UdsTransportProtocolMgr::IndicationResult, uds_transport::UdsMessagePtr > IndicateMessage(uds_transport::UdsMessage::Address source_addr, uds_transport::UdsMessage::Address target_addr, uds_transport::UdsMessage::TargetAddressType type, uds_transport::ChannelID channel_id, std::size_t size, uds_transport::Priority priority, uds_transport::ProtocolKind protocol_kind, core_type::Span< std::uint8_t > payload_info) override
Function to indicate a start of reception of message.
Doip Udp Connection handle connection between two layers.
DoipUdpConnection(uds_transport::ConversionHandler const &conversation_handler, std::string_view udp_ip_address, std::uint16_t port_num)
Constructor to create a new udp connection.
InitializationResult Initialize() override
Function to initialize the connection.
channel::udp_channel::DoipUdpChannel doip_udp_channel_
Store the reference to doip udp channel.
void Stop() override
Function to stop the connection.
std::pair< uds_transport::UdsTransportProtocolMgr::IndicationResult, uds_transport::UdsMessagePtr > IndicateMessage(uds_transport::UdsMessage::Address source_addr, uds_transport::UdsMessage::Address target_addr, uds_transport::UdsMessage::TargetAddressType type, uds_transport::ChannelID channel_id, std::size_t size, uds_transport::Priority priority, uds_transport::ProtocolKind protocol_kind, core_type::Span< std::uint8_t > payload_info) override
Function to indicate a start of reception of message.
bool IsConnectToHost() override
Function to check if connected to host remote server.
uds_transport::UdsTransportProtocolMgr::TransmissionResult Transmit(uds_transport::UdsMessageConstPtr message) override
Function to transmit a valid Uds message.
void Start() override
Function to start the connection.
void HandleMessage(uds_transport::UdsMessagePtr message) override
Function to Hands over a valid received Uds message.
uds_transport::UdsTransportProtocolMgr::DisconnectionResult DisconnectFromHost() override
Function to disconnect from remote host server.
~DoipUdpConnection() final=default
Destruct an instance of DoipUdpConnection.
uds_transport::UdsTransportProtocolMgr::ConnectionResult ConnectToHost(uds_transport::UdsMessageConstPtr) override
Function to establish connection to remote host server.
Interface class to handle connection between two layers.
Definition: connection.h:23
uds_transport::UdsTransportProtocolHandler::InitializationResult InitializationResult
Type alias for Initialization result.
Definition: connection.h:33
uds_transport::ConversionHandler const & conversation_handler_
Store the conversation handler.
Definition: connection.h:148
Connection(ConnectionId connection_id, uds_transport::ConversionHandler const &conversation_handler) noexcept
Constructor to create a new connection.
Definition: connection.h:42
Class to manage reception from transport protocol handler to connection handler.
virtual void HandleMessage(UdsMessagePtr message) const noexcept=0
Function to Hands over a valid received Uds message.
virtual std::pair< UdsTransportProtocolMgr::IndicationResult, UdsMessagePtr > IndicateMessage(UdsMessage::Address source_addr, UdsMessage::Address target_addr, UdsMessage::TargetAddressType type, ChannelID channel_id, std::size_t size, Priority priority, ProtocolKind protocol_kind, core_type::Span< std::uint8_t > payload_info) const noexcept=0
Function to indicate a start of reception of message.
InitializationResult
Definitions of different initialization result.
std::string_view ProtocolKind
std::unique_ptr< const UdsMessage > UdsMessageConstPtr
Definition: uds_message.h:69
std::unique_ptr< UdsMessage > UdsMessagePtr
Definition: uds_message.h:71
uint32_t ChannelID
std::uint8_t Priority