|
| 1 | +/* Diagnostic Client library |
| 2 | +* Copyright (C) 2023 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 | + |
| 9 | +#include <iostream> |
| 10 | + |
| 11 | +#include "uds_message.h" |
| 12 | + |
| 13 | +// includes from diag-client library |
| 14 | +#include "include/create_diagnostic_client.h" |
| 15 | +#include "include/diagnostic_client.h" |
| 16 | +#include "include/diagnostic_client_uds_message_type.h" |
| 17 | +#include "include/diagnostic_client_vehicle_info_message_type.h" |
| 18 | + |
| 19 | +/* |
| 20 | + * Main Entry point of diag client example |
| 21 | + * Example to find available ECU in a network through vehicle discovery and connecting to it. |
| 22 | + */ |
| 23 | +int main() { |
| 24 | + // Create the Diagnostic client and pass the config for creating internal properties |
| 25 | + std::unique_ptr<diag::client::DiagClient> diag_client{ |
| 26 | + diag::client::CreateDiagnosticClient("etc/diag_client_config.json")}; |
| 27 | + |
| 28 | + // Initialize the Diagnostic Client library |
| 29 | + diag_client->Initialize(); |
| 30 | + |
| 31 | + // Get conversation for tester one by providing the conversation name configured |
| 32 | + // in diag_client_config file passed while creating the diag client |
| 33 | + diag::client::conversation::DiagClientConversation &diag_client_conversation { |
| 34 | + diag_client->GetDiagnosticClientConversation("DiagTesterOne")}; |
| 35 | + |
| 36 | + // Start the conversation for tester one |
| 37 | + diag_client_conversation.Startup(); |
| 38 | + |
| 39 | + // Create a vehicle info request for finding ECU with matching VIN - ABCDEFGH123456789 |
| 40 | + diag::client::vehicle_info::VehicleInfoListRequestType |
| 41 | + vehicle_info_request{1U, "ABCDEFGH123456789"}; |
| 42 | + // Send Vehicle Identification request and get the response with available ECU information |
| 43 | + std::pair<diag::client::DiagClient::VehicleResponseResult, |
| 44 | + diag::client::vehicle_info::VehicleInfoMessageResponseUniquePtr> |
| 45 | + vehicle_response_result{diag_client->SendVehicleIdentificationRequest(vehicle_info_request)}; |
| 46 | + |
| 47 | + |
| 48 | + // Valid vehicle discovery response must be received before connecting to ECU |
| 49 | + if(vehicle_response_result.first == diag::client::DiagClient::VehicleResponseResult::kStatusOk && |
| 50 | + (vehicle_response_result.second)) { |
| 51 | + // Get the list of all vehicle available with matching VIN |
| 52 | + diag::client::vehicle_info::VehicleInfoMessage::VehicleInfoListResponseType vehicle_response_collection{ |
| 53 | + vehicle_response_result.second->GetVehicleList()}; |
| 54 | + |
| 55 | + // Create an uds message using first vehicle available in the list of response collection |
| 56 | + std::string remote_ecu_ip_address{vehicle_response_collection[0].ip_address}; // Remote ECU ip address |
| 57 | + std::uint16_t remote_ecu_server_logical_address{vehicle_response_collection[0].logical_address}; // Remote ECU logical address |
| 58 | + |
| 59 | + diag::client::uds_message::UdsRequestMessagePtr uds_message{ |
| 60 | + std::make_unique<UdsMessage>(remote_ecu_ip_address, UdsMessage::ByteVector{0x10, 0x01})}; |
| 61 | + |
| 62 | + // Connect Tester One to remote ECU |
| 63 | + diag::client::conversation::DiagClientConversation::ConnectResult connect_result{ |
| 64 | + diag_client_conversation.ConnectToDiagServer(remote_ecu_server_logical_address, |
| 65 | + uds_message->GetHostIpAddress())}; |
| 66 | + |
| 67 | + if(connect_result == diag::client::conversation::DiagClientConversation::ConnectResult::kConnectSuccess) { |
| 68 | + // Use Tester One to send the diagnostic message to remote ECU |
| 69 | + std::pair<diag::client::conversation::DiagClientConversation::DiagResult, |
| 70 | + diag::client::uds_message::UdsResponseMessagePtr> |
| 71 | + diag_response{diag_client_conversation.SendDiagnosticRequest(std::move(uds_message))}; |
| 72 | + |
| 73 | + if (diag_response.first == diag::client::conversation::DiagClientConversation::DiagResult::kDiagSuccess) { |
| 74 | + std::cout << "diag_client_conversation Total size: " << diag_response.second->GetPayload().size() << std::endl; |
| 75 | + // Print the payload |
| 76 | + for (auto const byte: diag_response.second->GetPayload()) { |
| 77 | + std::cout << "diag_client_conversation byte: " << std::hex << static_cast<int>(byte) << std::endl; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Disconnect Tester One from ECU1 with remote ip address "172.16.25.128" |
| 82 | + diag_client_conversation.DisconnectFromDiagServer(); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + // shutdown the conversation |
| 88 | + diag_client_conversation.Shutdown(); |
| 89 | + |
| 90 | + // important to release all the resources by calling de-initialize |
| 91 | + diag_client->DeInitialize(); |
| 92 | + return (0); |
| 93 | +} |
0 commit comments