#ifdef _WIN32
#include <windows.h>
#else
#define WINAPI
#endif
#include <iostream>
#include <string>
#include <iomanip>
#ifdef _WIN32
#include "../../include/kvisotplib_wrapper.h"
#else
#endif
#ifdef _WIN32
const char *LIBRARY_PATH = "C:\\Program Files (x86)\\Kvaser\\IsoTp\\bin_x64\\kvisotplib.dll";
#else
const char *LIBRARY_PATH = "libkvisotplib.so";
#endif
void WINAPI ff_indication_callback(
enum MType m_type,
unsigned int can_id,
unsigned char address_extension,
unsigned int length,
unsigned long long timestamp)
{
std::cout << "First Frame indication callback triggered, length: " << length << ", timestamp: " << timestamp << std::endl;
}
void WINAPI indication_callback(
enum MType m_type,
unsigned int can_id,
unsigned char address_extension,
unsigned int length,
unsigned long long timestamp,
const unsigned char *payload,
unsigned int payload_length,
{
std::cout << "Indication callback triggered, result: " << result << ", timestamp: " << timestamp << std::endl;
std::cout << "Payload: ";
for (unsigned int i = 0; i < payload_length; ++i)
{
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(payload[i]) << " ";
}
std::cout << std::dec << std::endl;
}
void WINAPI confirm_callback(
enum MType m_type,
unsigned int can_id,
unsigned char address_extension,
unsigned long long timestamp,
unsigned int request_handle)
{
std::cout << "Confirm callback triggered, result: " << result << ", timestamp: " << timestamp << std::endl;
}
void print_help()
{
std::cout << "Usage: obd_reader [options]" << std::endl
<< "Options:" << std::endl
<< " --channel <channel>, -c <channel> Set CAN channel (default: 0)" << std::endl
<< " --bit-rate <bit_rate>, -b <bit_rate> Set CAN bit rate (default: 500000)" << std::endl
<< " --data-phase-bit-rate <data_phase_bit_rate>, -d <data_phase_bit_rate> Set data phase bit rate (default: 0)" << std::endl
<< " --local-id <local_id>, -l <local_id> Set local address ID (default: 0x7E8)" << std::endl
<< " --remote-id <remote_id>, -r <remote_id> Set remote address ID (default: 0x7E0)" << std::endl
<< " --use-padding, -P Use padding in messages" << std::endl
<< " --use-bit-rate-switch, -s Use bit rate switching" << std::endl
<< " --mode <mode>, -m <mode> Set OBD-II mode (default: 0x09)" << std::endl
<< " --pid <pid>, -p <pid> Set PID (default: 0x02)" << std::endl
<< " --help, -h Show this help message" << std::endl;
}
int main(int argc, char *argv[])
{
try
{
unsigned int channel = 0;
unsigned int bit_rate = 500000;
unsigned int data_phase_bit_rate = 0;
AddressInfo local_address = {(
unsigned int)0x7E8,
false, 0,
false,
false};
AddressInfo remote_address = {(
unsigned int)0x7E0,
false, 0,
false,
false};
bool use_padding = false;
bool use_bit_rate_switch = false;
unsigned char mode = 0x09;
unsigned char pid = 0x02;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if ((arg == "--channel" || arg == "-c") && i + 1 < argc)
{
channel = std::stoul(argv[++i]);
}
else if ((arg == "--bit-rate" || arg == "-b") && i + 1 < argc)
{
bit_rate = std::stoul(argv[++i]);
}
else if ((arg == "--data-phase-bit-rate" || arg == "-d") && i + 1 < argc)
{
data_phase_bit_rate = std::stoul(argv[++i]);
}
else if ((arg == "--local-id" || arg == "-l") && i + 1 < argc)
{
local_address.
id = std::stoul(argv[++i],
nullptr, 0);
}
else if ((arg == "--remote-id" || arg == "-r") && i + 1 < argc)
{
remote_address.
id = std::stoul(argv[++i],
nullptr, 0);
}
else if (arg == "--use-padding" || arg == "-P")
{
use_padding = true;
}
else if (arg == "--use-bit-rate-switch" || arg == "-s")
{
use_bit_rate_switch = true;
}
else if ((arg == "--mode" || arg == "-m") && i + 1 < argc)
{
mode = static_cast<unsigned char>(std::stoi(argv[++i], nullptr, 0));
}
else if ((arg == "--pid" || arg == "-p") && i + 1 < argc)
{
pid = static_cast<unsigned char>(std::stoi(argv[++i], nullptr, 0));
}
else if (arg == "--help" || arg == "-h")
{
print_help();
return 0;
}
}
{
std::cerr << "Failed to load Kvaser Iso-TP library!" << std::endl;
return 1;
}
CanMode can_mode = data_phase_bit_rate == 0 ? CanMode::Can : CanMode::CanFd;
channel,
can_mode,
true,
bit_rate,
data_phase_bit_rate,
local_address,
remote_address};
int isotp_handle = isotplib.
create(&create_params);
if (isotp_handle < 0)
{
std::cerr << "Failed to create ISO-TP channel!" << std::endl;
return 1;
}
isotplib.
register_callbacks(isotp_handle, ff_indication_callback, indication_callback, confirm_callback);
if (isotplib.
open(isotp_handle) < 0)
{
std::cerr << "Failed to open ISO-TP channel!" << std::endl;
return 1;
}
unsigned char data[] = {mode, pid};
unsigned int data_length = sizeof(data);
MType::Diagnostics,
TAType::PhysicalCanStandard,
0,
data,
data_length,
use_padding,
use_bit_rate_switch,
1
};
if (!isotplib.
write(isotp_handle, &write_params))
{
std::cerr << "Failed to send ISO-TP message!" << std::endl;
}
else
{
std::cout << "ISO-TP message sent successfully!" << std::endl;
}
std::cout << "Press Enter to exit..." << std::endl;
std::cin.get();
if (isotplib.
close(isotp_handle) < 0)
{
std::cerr << "Failed to close ISO-TP channel!" << std::endl;
}
else
{
std::cout << "ISO-TP channel closed successfully!" << std::endl;
}
return 0;
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl
<< std::endl;
print_help();
return 1;
}
}
Wrapper class for loading and using the Kvaser Iso-TP library.
Definition kvisotplib_wrapper.h:34
int close(int isotp_handle)
Closes this channel and takes its CAN channel off-bus.
Definition kvisotplib_wrapper.h:688
int open(int isotp_handle)
Opens the specified ISO-TP channel and takes its CAN channel bus-on.
Definition kvisotplib_wrapper.h:651
bool register_callbacks(int isotp_handle, FFIndicationCallback ff_indication_callback, IndicationCallback indication_callback, ConfirmCallback confirm_callback)
Registers callback functions to be used by the ISO-TP channel to inform the upper layers on events.
Definition kvisotplib_wrapper.h:192
void unload_library()
Unload the Kvaser ISO-TP library.
Definition kvisotplib_wrapper.h:77
int create(const struct CreateParams *create_params)
Creates and opens a new ISO-TP channel.
Definition kvisotplib_wrapper.h:125
void destroy(int isotp_handle)
Closes and removes the ISO-TP channel with the specified handle.
Definition kvisotplib_wrapper.h:156
bool load_library(const char *lib_path)
Load the Kvaser ISO-TP library.
Definition kvisotplib_wrapper.h:68
bool write(int isotp_handle, const struct WriteParams *write_params)
Sends a message.
Definition kvisotplib_wrapper.h:742
NResult
The result of the reception or transmission of a message.
Definition kvisotplib.h:65
TAType
The type of target addressing used for the message.
Definition kvisotplib.h:92
CanMode
Possible CAN modes.
Definition kvisotplib.h:40
MType
The type of message to be sent.
Definition kvisotplib.h:53
Kvaser ISO-TP C++ Wrapper.
ISO-TP address information.
Definition kvisotplib.h:115
bool use_padding
True if message padding should be used.
Definition kvisotplib.h:125
unsigned int id
CAN identifier.
Definition kvisotplib.h:117
A struct that holds the parameters for creating an ISO-TP channel.
Definition kvisotplib.h:132
A struct that holds the parameters for an ISO-TP write request.
Definition kvisotplib.h:160