Diag-Client-Lib
conversation_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 /* includes */
10 
12 #include "src/common/logger.h"
14 
15 namespace diag {
16 namespace client {
17 namespace conversation_manager {
18 
22  : uds_transport_mgr_{uds_transport_mgr} {
23  // store the conversation config (vd & dm) out of passed config
24  StoreConversationConfig(config);
25 }
26 
27 void ConversationManager::Startup() noexcept {}
28 
30 
32  std::string_view conversation_name) noexcept {
33  // find the conversation from config stored
34  auto it = conversation_map_.find(std::string{conversation_name});
35  if (it != conversation_map_.end()) {
36  std::string const conversation_name_in_map{it->first};
37  it->second.conversation = std::visit(
39  [this, &conversation_name_in_map](conversation::DMConversationType conversation_type) noexcept {
40  // Create the conversation
41  std::unique_ptr<diag::client::conversation::Conversation> conversation{
42  std::make_unique<diag::client::conversation::DmConversation>(conversation_name_in_map,
43  conversation_type)};
44  // Register the connection
45  conversation->RegisterConnection(uds_transport_mgr_.GetTransportProtocolHandler().CreateTcpConnection(
46  conversation->GetConversationHandler(), conversation_type.tcp_address, conversation_type.port_num));
47  return conversation;
48  },
49  [this, &conversation_name_in_map](conversation::VDConversationType conversation_type) noexcept {
50  // Create the conversation
51  std::unique_ptr<diag::client::conversation::Conversation> conversation{
52  std::make_unique<diag::client::conversation::VdConversation>(conversation_name_in_map,
53  conversation_type)};
54  // Register the connection
55  conversation->RegisterConnection(uds_transport_mgr_.GetTransportProtocolHandler().CreateUdpConnection(
56  conversation->GetConversationHandler(), conversation_type.udp_address, conversation_type.port_num));
57  return conversation;
58  }},
59  it->second.conversation_type);
60  } else {
61  logger::DiagClientLogger::GetDiagClientLogger().GetLogger().LogFatal(
62  __FILE__, __LINE__, __func__, [conversation_name](std::stringstream &msg) {
63  msg << "Invalid conversation name: '" << conversation_name << "', provide correct name as per config file";
64  });
65  }
66  return *(it->second.conversation);
67 }
68 
70  { // Create Vehicle discovery config
71  conversation::VDConversationType conversion_identifier{};
72  conversion_identifier.udp_address = config.udp_ip_address;
73  conversion_identifier.udp_broadcast_address = config.udp_broadcast_address;
74  conversion_identifier.port_num = 0U; // random selection of port number
75  (void) conversation_map_.emplace("VehicleDiscovery", ConversationStorage{conversion_identifier, nullptr});
76  }
77 
78  { // Create Conversation config
79  for (std::uint8_t conv_count{0U}; conv_count < config.num_of_conversation; conv_count++) {
80  conversation::DMConversationType conversion_identifier{};
81  conversion_identifier.rx_buffer_size = config.conversations[conv_count].rx_buffer_size;
82  conversion_identifier.p2_client_max = config.conversations[conv_count].p2_client_max;
83  conversion_identifier.p2_star_client_max = config.conversations[conv_count].p2_star_client_max;
84  conversion_identifier.source_address = config.conversations[conv_count].source_address;
85  conversion_identifier.tcp_address = config.conversations[conv_count].network.tcp_ip_address;
86  conversion_identifier.port_num = 0U; // random selection of port number
87  // push to config map
88  (void) conversation_map_.emplace(config.conversations[conv_count].conversation_name,
89  ConversationStorage{conversion_identifier, nullptr});
90  }
91  }
92 }
93 
94 } // namespace conversation_manager
95 } // namespace client
96 } // namespace diag
Interface for diag client conversation.
Definition: conversation.h:24
void StoreConversationConfig(diag::client::config_parser::DcmClientConfig &config) noexcept
Function to store the dcm client configuration internally.
diag::client::conversation::Conversation & GetDiagnosticClientConversation(std::string_view conversation_name) noexcept
Function to get DM conversation object based on conversation name.
void Shutdown() noexcept
Function to shutdown the ConversationManager.
ConversationManager(diag::client::config_parser::DcmClientConfig config, diag::client::uds_transport::UdsTransportProtocolManager &uds_transport_mgr) noexcept
Constructs an instance of ConversationManager.
void Startup() noexcept
Function to start the ConversationManager.
static auto GetDiagClientLogger() noexcept -> DiagClientLogger &
Get the diag client logger instance.
Definition: logger.h:32
Structure containing DM conversation type.
std::uint32_t rx_buffer_size
The reception buffer size type.
Structure containing VD conversation type.
std::string udp_address
The Udp IP address of conversation.