Diag-Client-Lib
diagnostic_client.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  */
9 
10 #include <pthread.h>
11 
12 #include <memory>
13 #include <string>
14 #include <thread>
15 
16 #include "core/include/result.h"
17 #include "parser/json_parser.h"
19 #include "src/common/logger.h"
20 #include "src/dcm/dcm_client.h"
22 
23 namespace diag {
24 namespace client {
25 
30  public:
37  explicit DiagClientImpl(std::string_view diag_client_config_path) noexcept
38  : dcm_instance_{},
39  dcm_thread_{},
40  diag_client_config_path_{diag_client_config_path} {}
41 
45  DiagClientImpl(const DiagClientImpl &other) noexcept = delete;
46  DiagClientImpl &operator=(const DiagClientImpl &other) noexcept = delete;
47 
51  DiagClientImpl(DiagClientImpl &&other) noexcept = delete;
52  DiagClientImpl &operator=(DiagClientImpl &&other) noexcept = delete;
53 
58  ~DiagClientImpl() noexcept = default;
59 
66  Result<void> Initialize() noexcept {
68  __FILE__, __LINE__, __func__, [](std::stringstream &msg) { msg << "DiagClient Initialization started"; });
69 
70  // read configuration
73  .MapError([](boost_support::parser::ParsingErrorCode const &) noexcept {
74  logger::DiagClientLogger::GetDiagClientLogger().GetLogger().LogError(
75  __FILE__, __LINE__, "", [](std::stringstream &msg) { msg << "DiagClient Initialization failed"; });
77  })
78  .AndThen([this, &config]() {
79  // create single dcm instance and pass the configuration
80  dcm_instance_ = std::make_unique<diag::client::dcm::DCMClient>(config_parser::ReadDcmClientConfig(config));
81  // start dcm client thread
82  dcm_thread_ = std::thread([this]() noexcept { this->dcm_instance_->Main(); });
83  pthread_setname_np(dcm_thread_.native_handle(), "dcm_client");
85  __FILE__, __LINE__, "", [](std::stringstream &msg) { msg << "DiagClient Initialization completed"; });
86  });
87  }
88 
98  __FILE__, __LINE__, __func__, [](std::stringstream &msg) { msg << "DiagClient De-Initialization started"; });
99  // shutdown DCM module here
100  return dcm_instance_->SignalShutdown()
101  .AndThen([this]() {
102  if (dcm_thread_.joinable()) { dcm_thread_.join(); }
103  logger::DiagClientLogger::GetDiagClientLogger().GetLogger().LogInfo(
104  __FILE__, __LINE__, "", [](std::stringstream &msg) { msg << "DiagClient De-Initialization completed"; });
105  })
106  .OrElse([](core_type::ErrorCode const &) {
108  });
109  }
110 
118  conversation::DiagClientConversation GetDiagnosticClientConversation(std::string_view conversation_name) noexcept {
119  if (!dcm_instance_) {
120  logger::DiagClientLogger::GetDiagClientLogger().GetLogger().LogFatal(
121  __FILE__, __LINE__, "", [](std::stringstream &msg) { msg << "DiagClient is not Initialized"; });
122  }
123  return dcm_instance_->GetDiagnosticClientConversation(conversation_name);
124  }
125 
135  diag::client::vehicle_info::VehicleInfoListRequestType vehicle_info_request) noexcept {
136  if (!dcm_instance_) {
137  logger::DiagClientLogger::GetDiagClientLogger().GetLogger().LogFatal(
138  __FILE__, __LINE__, "", [](std::stringstream &msg) { msg << "DiagClient is not Initialized"; });
139  }
140  return dcm_instance_->SendVehicleIdentificationRequest(std::move(vehicle_info_request));
141  }
142 
143  private:
147  std::unique_ptr<diag::client::common::DiagnosticManager> dcm_instance_;
148 
152  std::thread dcm_thread_;
153 
158 };
159 
160 DiagClient::DiagClient(std::string_view diag_client_config_path) noexcept
161  : diag_client_impl_{std::make_unique<DiagClientImpl>(diag_client_config_path)} {}
162 
163 DiagClient::~DiagClient() noexcept = default;
164 
165 Result<void> DiagClient::Initialize() noexcept { return diag_client_impl_->Initialize(); }
166 
167 Result<void> DiagClient::DeInitialize() noexcept { return diag_client_impl_->DeInitialize(); }
168 
171  diag::client::vehicle_info::VehicleInfoListRequestType vehicle_info_request) noexcept {
172  return diag_client_impl_->SendVehicleIdentificationRequest(std::move(vehicle_info_request));
173 }
174 
176  std::string_view conversation_name) noexcept {
177  return diag_client_impl_->GetDiagnosticClientConversation(conversation_name);
178 }
179 
180 std::unique_ptr<DiagClient> CreateDiagnosticClient(std::string_view diag_client_config_path) {
181  return (std::make_unique<DiagClient>(diag_client_config_path));
182 }
183 
184 } // namespace client
185 } // namespace diag
Encapsulation of an error code. An ErrorCode contains a raw error code value and an error domain....
Definition: error_code.h:22
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29
Class to provide implementation of diag client.
~DiagClientImpl() noexcept=default
Destruct an instance of DiagClient -Destruction.
Result< void > DeInitialize() noexcept
Function to de-initialize the already initialized instance of DiagClient.
Result< void > Initialize() noexcept
Function to initialize the already created instance of DiagClient.
conversation::DiagClientConversation GetDiagnosticClientConversation(std::string_view conversation_name) noexcept
Function to get required diag client conversation object based on conversation name.
Result< vehicle_info::VehicleInfoMessageResponseUniquePtr, DiagClient::VehicleInfoResponseError > SendVehicleIdentificationRequest(diag::client::vehicle_info::VehicleInfoListRequestType vehicle_info_request) noexcept
Function to send vehicle identification request and get the Diagnostic Server list.
DiagClientImpl & operator=(const DiagClientImpl &other) noexcept=delete
DiagClientImpl(DiagClientImpl &&other) noexcept=delete
Deleted move assignment and move constructor.
std::thread dcm_thread_
Thread to handle dcm client lifecycle.
DiagClientImpl(const DiagClientImpl &other) noexcept=delete
Deleted copy assignment and copy constructor.
DiagClientImpl & operator=(DiagClientImpl &&other) noexcept=delete
DiagClientImpl(std::string_view diag_client_config_path) noexcept
Constructs an instance of DiagClient.
std::unique_ptr< diag::client::common::DiagnosticManager > dcm_instance_
Unique pointer to dcm client instance.
std::string diag_client_config_path_
Store the diag client config path.
Class to manage Diagnostic Client.
Result< void > Initialize() noexcept
Function to initialize the already created instance of DiagClient.
Result< vehicle_info::VehicleInfoMessageResponseUniquePtr, VehicleInfoResponseError > SendVehicleIdentificationRequest(diag::client::vehicle_info::VehicleInfoListRequestType vehicle_info_request) noexcept
Function to send vehicle identification request and get the Diagnostic Server list.
std::unique_ptr< DiagClientImpl > diag_client_impl_
Unique pointer to diag client implementation.
DiagClient(std::string_view diag_client_config_path) noexcept
Constructs an instance of DiagClient.
conversation::DiagClientConversation GetDiagnosticClientConversation(std::string_view conversation_name) noexcept
Function to get required diag client conversation object based on conversation name.
~DiagClient() noexcept
Destruct an instance of DiagClient -Destruction.
Result< void > DeInitialize() noexcept
Function to de-initialize the already initialized instance of DiagClient.
Conversation class to establish connection with a Diagnostic Server.
static auto GetDiagClientLogger() noexcept -> DiagClientLogger &
Get the diag client logger instance.
Definition: logger.h:32
ParsingErrorCode
Definitions of Parsing failure error codes.
Definition: json_parser.h:28
core_type::Result< void, ParsingErrorCode > Read(std::string_view config_path, boost_tree &json_tree)
Parser to get the configuration from json file.
Definition: json_parser.cpp:16
boost::property_tree::ptree boost_tree
Type alias for boost property tree.
Definition: json_parser.h:23
diag::client::config_parser::DcmClientConfig ReadDcmClientConfig(boost_support::parser::boost_tree &config_tree)
Function to read from config tree and get the DcmClient configuration.
core_type::ErrorCode MakeErrorCode(DmErrorErrc, core_type::ErrorDomain::SupportDataType data) noexcept
Create a new ErrorCode within DmErrorDomain.
std::unique_ptr< diag::client::DiagClient > CreateDiagnosticClient(std::string_view diag_client_config_path)
Function to get the instance of Diagnostic Client Object. This instance to be further used for all th...