Skip to content

Commit 04a9b9b

Browse files
committed
Add example with use of vehicle discovery
1 parent 1b634dc commit 04a9b9b

File tree

7 files changed

+220
-3
lines changed

7 files changed

+220
-3
lines changed

examples/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ cmake_minimum_required(VERSION 3.5)
99

1010
# build example_1
1111
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/example_1)
12+
13+
# build example_2
14+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/example_2)

examples/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Examples
2-
32
This folder contains several examples of applications using the diag client library.
43

54
## Example1:
6-
Minimal example to use diag client library
5+
Example to connect to multiple ECU by creating multiple diagnostic tester instance.
6+
7+
## Example2:
8+
Example to find available ECU in a network through vehicle discovery and connecting to it.

examples/example_1/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/*
1919
* Main Entry point of diag client example
20-
* This example explains how to use the diag client library efficiently
20+
* Example to connect to multiple ECU by creating multiple diagnostic tester instance.
2121
*/
2222
int main() {
2323
// Create the Diagnostic client and pass the config for creating internal properties

examples/example_2/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Diagnostic Client library CMake File
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+
cmake_minimum_required(VERSION 3.5)
9+
10+
project(diag-client-example-2 LANGUAGES CXX)
11+
12+
set(CMAKE_CXX_STANDARD 17)
13+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
14+
15+
# find needed packages
16+
find_package(Threads REQUIRED)
17+
18+
#find main sources
19+
file(GLOB MAIN_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
20+
21+
add_executable(${PROJECT_NAME}
22+
${MAIN_SRCS}
23+
)
24+
25+
# diag client lib headers
26+
set(diag_client_header_path ${CMAKE_CURRENT_SOURCE_DIR}/../diag_client_lib/build-bin/diag-client/lib/include)
27+
28+
# Src headers
29+
set(src_header_path src)
30+
31+
32+
target_include_directories(${PROJECT_NAME} PRIVATE
33+
${src_header_path}
34+
${diag_client_header_path}
35+
)
36+
37+
# link all dynamic libraries
38+
target_link_libraries(${PROJECT_NAME} PRIVATE
39+
diag-client
40+
Threads::Threads
41+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"UdpIpAddress": "172.16.25.127",
3+
"UdpBroadcastAddress": "172.16.255.255",
4+
"Conversation": {
5+
"NumberOfConversation": 2,
6+
"ConversationProperty": [
7+
{
8+
"p2ClientMax": 1000,
9+
"p2StarClientMax": 5000,
10+
"RxBufferSize": 4095,
11+
"SourceAddress": 1,
12+
"TargetAddressType": "Physical",
13+
"Network": {
14+
"ProtocolKind": "DoIP",
15+
"TcpIpAddress": "172.16.25.127",
16+
"TLS": false
17+
},
18+
"ConversationName": "DiagTesterOne"
19+
},
20+
{
21+
"p2ClientMax": 2000,
22+
"p2StarClientMax": 5000,
23+
"RxBufferSize": 4095,
24+
"SourceAddress": 2,
25+
"TargetAddressType": "Functional",
26+
"Network": {
27+
"ProtocolKind": "DoIP",
28+
"TcpIpAddress": "172.16.25.127",
29+
"TLS": false
30+
},
31+
"ConversationName": "DiagTesterTwo"
32+
}
33+
]
34+
}
35+
}

examples/example_2/src/main.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

examples/example_2/src/uds_message.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
#ifndef EXAMPLES_EXAMPLE_1_SRC_UDS_MESSAGE_H
10+
#define EXAMPLES_EXAMPLE_1_SRC_UDS_MESSAGE_H
11+
12+
#include <string_view>
13+
// includes from diag-client library
14+
#include "include/create_diagnostic_client.h"
15+
#include "include/diagnostic_client_uds_message_type.h"
16+
17+
class UdsMessage final : public diag::client::uds_message::UdsMessage {
18+
public:
19+
// ctor
20+
UdsMessage(std::string_view host_ip_address, diag::client::uds_message::UdsMessage::ByteVector payload)
21+
: host_ip_address_{host_ip_address},
22+
uds_payload_{std::move(payload)} {}
23+
24+
// dtor
25+
~UdsMessage() = default;
26+
27+
private:
28+
// host ip address
29+
IpAddress host_ip_address_;
30+
31+
// store only UDS payload to be sent
32+
diag::client::uds_message::UdsMessage::ByteVector uds_payload_;
33+
34+
const diag::client::uds_message::UdsMessage::ByteVector &GetPayload() const override { return uds_payload_; }
35+
36+
// return the underlying buffer for write access
37+
diag::client::uds_message::UdsMessage::ByteVector &GetPayload() override { return uds_payload_; }
38+
39+
// Get Host Ip address
40+
IpAddress GetHostIpAddress() const noexcept override { return host_ip_address_; };
41+
};
42+
43+
#endif // EXAMPLES_EXAMPLE_1_SRC_UDS_MESSAGE_H

0 commit comments

Comments
 (0)