Diag-Client-Lib
doip_tcp_channel.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 
11 #include <utility>
12 
13 #include "common/logger.h"
15 
16 namespace doip_client {
17 namespace channel {
18 namespace tcp_channel {
19 
20 DoipTcpChannel::DoipTcpChannel(std::string_view tcp_ip_address, std::uint16_t, uds_transport::Connection &connection)
21  : tcp_socket_handler_{tcp_ip_address, *this},
22  tcp_channel_handler_{tcp_socket_handler_, *this},
23  connection_{connection} {}
24 
28 }
29 
33 }
34 
37 }
38 
43 
44  uds_transport::UdsMessage::IpAddress const kHostIpAddress{message->GetHostIpAddress()};
45  uds_transport::UdsMessage::PortNumber const kHostPortNumber{message->GetHostPortNumber()};
46  // Initiate connecting to server
47  if (tcp_socket_handler_.ConnectToHost(kHostIpAddress, kHostPortNumber)) {
48  // Once connected, Send routing activation req and get response
49  ret_val = tcp_channel_handler_.SendRoutingActivationRequest(std::move(message));
50  } else { // failure
51  logger::DoipClientLogger::GetDiagClientLogger().GetLogger().LogError(
52  __FILE__, __LINE__, __func__, [&kHostIpAddress, &kHostPortNumber](std::stringstream &msg) {
53  msg << "Doip Tcp socket connect failed for remote endpoints : "
54  << "<Ip: " << kHostIpAddress << ", Port: " << kHostPortNumber << ">";
55  });
56  }
57  return ret_val;
58 }
59 
65  // Reset the handler
67  }
69  }
70  return ret_val;
71 }
72 
74  tcp_channel_handler_.HandleMessage(std::move(tcp_rx_message));
75 }
76 
81  // Routing activation should be active before sending diag request
83  ret_val = tcp_channel_handler_.SendDiagnosticRequest(std::move(message));
84  } else {
85  logger::DoipClientLogger::GetDiagClientLogger().GetLogger().LogError(
86  __FILE__, __LINE__, __func__,
87  [](std::stringstream &msg) { msg << "Routing Activation required, please connect to server first"; });
88  }
89  return ret_val;
90 }
91 
92 std::pair<uds_transport::UdsTransportProtocolMgr::IndicationResult, uds_transport::UdsMessagePtr>
96  std::size_t size, uds_transport::Priority priority,
97  uds_transport::ProtocolKind protocol_kind, core_type::Span<std::uint8_t> payload_info) {
98  return connection_.IndicateMessage(source_addr, target_addr, type, channel_id, size, priority, protocol_kind,
99  payload_info);
100 }
101 
103  connection_.HandleMessage(std::move(message));
104 }
105 
106 } // namespace tcp_channel
107 } // namespace channel
108 } // namespace doip_client
auto IsRoutingActivated() noexcept -> bool
Check if routing activation is active for this handler.
auto SendDiagnosticRequest(uds_transport::UdsMessageConstPtr diagnostic_request) noexcept -> uds_transport::UdsTransportProtocolMgr::TransmissionResult
Function to send diagnostic request.
auto SendRoutingActivationRequest(uds_transport::UdsMessageConstPtr routing_activation_request) noexcept -> uds_transport::UdsTransportProtocolMgr::ConnectionResult
Function to send routing activation request.
void HandleMessage(TcpMessagePtr tcp_rx_message) noexcept
Function to process the received message.
void Stop()
Function to stop the channel.
uds_transport::Connection & connection_
Store the reference to doip connection.
void HandleMessage(uds_transport::UdsMessagePtr message)
Function to Hands over a valid received Uds message to upper layer.
void Start()
Function to start the channel.
DoipTcpChannelHandler tcp_channel_handler_
Store the doip channel handler.
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)
Function to indicate a start of reception of message.
sockets::TcpSocketHandler::TcpMessagePtr TcpMessagePtr
Type alias for Tcp message pointer.
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.
void ProcessReceivedTcpMessage(TcpMessagePtr tcp_rx_message)
Function to process the received Tcp message from socket layer.
uds_transport::UdsTransportProtocolMgr::DisconnectionResult DisconnectFromHost()
Function to disconnect from remote host server.
DoipTcpChannel(std::string_view tcp_ip_address, std::uint16_t port_num, uds_transport::Connection &connection)
Constructs an instance of TcpChannel.
uds_transport::UdsTransportProtocolMgr::TransmissionResult Transmit(uds_transport::UdsMessageConstPtr message)
Function to transmit a valid Uds message.
TcpSocketHandler tcp_socket_handler_
Store the tcp socket handler.
static auto GetDiagClientLogger() noexcept -> DoipClientLogger &
Definition: logger.h:20
core_type::Result< void > DisconnectFromHost()
Function to disconnect from remote host if already connected.
void Stop()
Function to stop the socket handler.
SocketHandlerState GetSocketHandlerState() const
Function to get the current state of socket handler.
void Start()
Function to start the socket handler.
core_type::Result< void > ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num)
Function to connect to remote ip address and port number.
Interface class to handle connection between two layers.
Definition: connection.h:23
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)=0
Function to indicate a start of reception of message.
virtual void HandleMessage(UdsMessagePtr message)=0
Function to Hands over a valid received Uds message.
std::string_view IpAddress
Definition: uds_message.h:24
std::uint16_t PortNumber
Definition: uds_message.h:26
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