Kvaser ISO-TP SDK
Loading...
Searching...
No Matches
kvisotplib.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 by KVASER AB, SWEDEN
3 *
4 * WWW: https://www.kvaser.com
5 *
6 * This software is furnished under a license and may be used and copied
7 * only in accordance with the terms of such license.
8 *
9 */
10
21#ifndef KVISOTP_H
22#define KVISOTP_H
23
24#include <stdarg.h>
25#include <stdbool.h>
26#include <stdint.h>
27#include <stdlib.h>
28
29#ifdef _WIN32
30#include <windows.h>
31#else
32#define WINAPI
33#include <dlfcn.h>
34#endif
35
48
59
87
110
114typedef struct AddressInfo
115{
117 unsigned int id;
121 unsigned char address_extension;
127
155
159typedef struct WriteParams
160{
165 unsigned int can_id;
174 unsigned char address_extension;
176 const unsigned char *payload;
178 unsigned int payload_length;
185 unsigned int request_handle;
187
209typedef void(WINAPI *FFIndicationCallback)(enum MType m_type,
210 unsigned int can_id,
211 enum TAType ta_type,
212 unsigned char address_extension,
213 unsigned int length,
214 unsigned long long timestamp);
215
240typedef void(WINAPI *IndicationCallback)(enum MType m_type,
241 unsigned int can_id,
242 enum TAType ta_type,
243 unsigned char address_extension,
244 unsigned int length,
245 unsigned long long timestamp,
246 const unsigned char *payload,
247 unsigned int payload_length,
248 enum NResult result);
249
272typedef void(WINAPI *ConfirmCallback)(enum MType m_type,
273 unsigned int can_id,
274 enum TAType ta_type,
275 unsigned char address_extension,
276 unsigned long long timestamp,
277 enum NResult result,
278 unsigned int request_handle);
279
287typedef bool(WINAPI *isotp_start_log_t)();
288
296typedef int(WINAPI *isotp_create_t)(const struct CreateParams *create_params);
297
303typedef void(WINAPI *isotp_destroy_t)(int isotp_handle);
304
317typedef bool(WINAPI *isotp_register_callbacks_t)(int isotp_handle,
318 FFIndicationCallback ff_indication_callback,
319 IndicationCallback indication_callback,
320 ConfirmCallback confirm_callback);
321
333typedef bool(WINAPI *isotp_set_full_duplex_t)(int isotp_handle, bool value);
334
351typedef bool(WINAPI *isotp_set_j1939_priority_masking_t)(int isotp_handle,
352 bool value);
353
365typedef bool(WINAPI *isotp_set_pad_value_t)(int isotp_handle, unsigned char value);
366
378typedef bool(WINAPI *isotp_set_block_size_t)(int isotp_handle, unsigned char value);
379
394typedef bool(WINAPI *isotp_set_min_separation_time_t)(int isotp_handle,
395 unsigned char value);
396
411typedef bool(WINAPI *isotp_set_block_size_override_t)(int isotp_handle, unsigned char value);
412
431typedef bool(WINAPI *isotp_set_min_separation_time_override_t)(int isotp_handle, unsigned char value);
432
444typedef bool(WINAPI *isotp_set_cr_max_t)(int isotp_handle,
445 unsigned int value);
446
458typedef bool(WINAPI *isotp_set_a_max_t)(int isotp_handle,
459 unsigned int value);
460
472typedef bool(WINAPI *isotp_set_bs_max_t)(int isotp_handle,
473 unsigned int value);
474
486typedef bool(WINAPI *isotp_set_wft_max_t)(int isotp_handle,
487 unsigned int value);
488
496typedef int(WINAPI *isotp_open_t)(int isotp_handle);
497
505typedef int(WINAPI *isotp_close_t)(int isotp_handle);
506
521typedef bool(WINAPI *isotp_write_t)(int isotp_handle,
522 const struct WriteParams *write_params);
523
657
664{
665 pApi->handle = NULL;
666 pApi->is_valid = false;
667 pApi->isotp_start_log = NULL;
668 pApi->isotp_create = NULL;
669 pApi->isotp_destroy = NULL;
670 pApi->isotp_register_callbacks = NULL;
671 pApi->isotp_set_full_duplex = NULL;
673 pApi->isotp_set_pad_value = NULL;
674 pApi->isotp_set_block_size = NULL;
678 pApi->isotp_set_cr_max = NULL;
679 pApi->isotp_set_a_max = NULL;
680 pApi->isotp_set_bs_max = NULL;
681 pApi->isotp_set_wft_max = NULL;
682 pApi->isotp_open = NULL;
683 pApi->isotp_close = NULL;
684 pApi->isotp_write = NULL;
685}
686
693{
694 IsoTpApi api;
695 clear_api(&api);
696 return api;
697}
698
705typedef bool(WINAPI *isotp_get_api_t)(IsoTpApi *pApi);
706
713{
714 // Check if the handle is valid
715 if (!pApi->handle)
716 {
717 clear_api(pApi);
718 return;
719 }
720
721 // Unload the library
722#ifdef _WIN32
723 FreeLibrary((HMODULE)pApi->handle);
724#else
725 dlclose(pApi->handle);
726#endif
727
728 clear_api(pApi);
729}
730
737IsoTpApi load_library(const char *lib_path)
738{
739 IsoTpApi api = create_api();
740
741 // Load the library
742#ifdef _WIN32
743 HMODULE handle = LoadLibraryA(lib_path != NULL ? lib_path : "kvisotplib.dll");
744 if (!handle)
745 return api;
746 isotp_get_api_t get_api = (isotp_get_api_t)GetProcAddress(handle, "isotp_get_api");
747 if (!get_api || !get_api(&api))
748 {
749 unload_library(&api);
750 return api;
751 }
752 api.handle = handle;
753 api.is_valid = handle != NULL;
754 return api;
755#else
756 void *handle = dlopen(lib_path != NULL ? lib_path : "libkvisotplib.so", RTLD_LAZY);
757 if (!handle)
758 return api;
759 isotp_get_api_t get_api = (isotp_get_api_t)dlsym(handle, "isotp_get_api");
760 if (!get_api || !get_api(&api))
761 {
762 unload_library(&api);
763 return api;
764 }
765 api.handle = handle;
766 api.is_valid = handle != NULL;
767 return api;
768#endif
769}
770
771#endif /* KVISOTP_H */
void(WINAPI * FFIndicationCallback)(enum MType m_type, unsigned int can_id, enum TAType ta_type, unsigned char address_extension, unsigned int length, unsigned long long timestamp)
This callback prototype is used for indicating to the upper layers that a FirstFrame (FF) of a segmen...
Definition kvisotplib.h:209
bool(WINAPI * isotp_start_log_t)()
Function prototype for starting the logger for the library.
Definition kvisotplib.h:287
bool(WINAPI * isotp_set_min_separation_time_t)(int isotp_handle, unsigned char value)
Function prototype for setting the min_separation_time settings.
Definition kvisotplib.h:394
int(WINAPI * isotp_open_t)(int isotp_handle)
Function prototype for opening the specified ISO-TP channel and taking its CAN channel bus-on.
Definition kvisotplib.h:496
bool(WINAPI * isotp_set_full_duplex_t)(int isotp_handle, bool value)
Function prototype for setting the full_duplex settings.
Definition kvisotplib.h:333
NResult
The result of the reception or transmission of a message.
Definition kvisotplib.h:65
@ Ok
The reception or transmission was successful.
Definition kvisotplib.h:67
@ BufferOvflw
Indicates that the receiver has transmitted a FlowControl (FC) frame with FlowStatus = OVFLW.
Definition kvisotplib.h:83
@ TimeoutBs
Indicates that the timer N_Bs has passed its timeout value N_Bsmax.
Definition kvisotplib.h:71
@ Error
This is a general error code that indicates that the reception or transmission failed.
Definition kvisotplib.h:85
@ TimeoutA
Indicates that the timer N_Ar/N_As has passed its timeout value N_Armax/N_Asmax.
Definition kvisotplib.h:69
@ WftOvrn
Indicates that the receiver has transmitted N_WFTmax FlowControl (FC) frames with FlowStatus = WAIT i...
Definition kvisotplib.h:81
@ WrongSN
Indicates that the received sequence number is wrong.
Definition kvisotplib.h:75
@ InvalidFS
Indicates that an invalid or unknown FlowStatus was received in a FlowControl (FC) frame.
Definition kvisotplib.h:77
@ TimeoutCr
Indicates that the timer N_Cr has passed its timeout value N_Crmax.
Definition kvisotplib.h:73
@ UnexpPDU
Indicates that an unexpected PDU was received.
Definition kvisotplib.h:79
bool(WINAPI * isotp_register_callbacks_t)(int isotp_handle, FFIndicationCallback ff_indication_callback, IndicationCallback indication_callback, ConfirmCallback confirm_callback)
Function prototype for registering callback functions for the ISO-TP channel.
Definition kvisotplib.h:317
bool(WINAPI * isotp_set_cr_max_t)(int isotp_handle, unsigned int value)
Function prototype for setting the cr_max settings.
Definition kvisotplib.h:444
IsoTpApi load_library(const char *lib_path)
Load the Kvaser ISO-TP library.
Definition kvisotplib.h:737
TAType
The type of target addressing used for the message.
Definition kvisotplib.h:92
@ FunctionalCanStandard
Functional addressing over classical CAN with standard (11-bit) identifier.
Definition kvisotplib.h:96
@ PhysicalCanStandard
Physical addressing over classical CAN with standard (11-bit) identifier.
Definition kvisotplib.h:94
@ PhysicalCanFdExtended
Physical addressing over CAN FD with extended (29-bit) identifier.
Definition kvisotplib.h:106
@ FunctionalCanFdExtended
Functional addressing over CAN FD with extended (29-bit) identifier.
Definition kvisotplib.h:108
@ PhysicalCanFdStandard
Physical addressing over CAN FD with standard (11-bit) identifier.
Definition kvisotplib.h:98
@ FunctionalCanExtended
Functional addressing over classical CAN with extended (29-bit) identifier.
Definition kvisotplib.h:104
@ FunctionalCanFdStandard
Functional addressing over CAN FD with standard (11-bit) identifier.
Definition kvisotplib.h:100
@ PhysicalCanExtended
Physical addressing over classical CAN with extended (29-bit) identifier.
Definition kvisotplib.h:102
bool(WINAPI * isotp_set_block_size_t)(int isotp_handle, unsigned char value)
Function prototype for setting the block_size settings.
Definition kvisotplib.h:378
bool(WINAPI * isotp_set_j1939_priority_masking_t)(int isotp_handle, bool value)
Function prototype for setting the j1939_priority_masking settings.
Definition kvisotplib.h:351
bool(WINAPI * isotp_set_pad_value_t)(int isotp_handle, unsigned char value)
Function prototype for setting the pad_value settings.
Definition kvisotplib.h:365
void(WINAPI * isotp_destroy_t)(int isotp_handle)
Function prototype for closing and removing the ISO-TP channel with the specified handle.
Definition kvisotplib.h:303
bool(WINAPI * isotp_get_api_t)(IsoTpApi *pApi)
Get the ISO-TP API.
Definition kvisotplib.h:705
IsoTpApi create_api()
Create a new ISO-TP API structure.
Definition kvisotplib.h:692
bool(WINAPI * isotp_set_bs_max_t)(int isotp_handle, unsigned int value)
Function prototype for setting the bs_max settings.
Definition kvisotplib.h:472
bool(WINAPI * isotp_set_wft_max_t)(int isotp_handle, unsigned int value)
Function prototype for setting the wft_max settings.
Definition kvisotplib.h:486
int(WINAPI * isotp_close_t)(int isotp_handle)
Function prototype for closing this channel and taking its CAN channel off-bus.
Definition kvisotplib.h:505
void(WINAPI * ConfirmCallback)(enum MType m_type, unsigned int can_id, enum TAType ta_type, unsigned char address_extension, unsigned long long timestamp, enum NResult result, unsigned int request_handle)
This callback prototype is used for confirming to the upper layers that a write request has been comp...
Definition kvisotplib.h:272
CanMode
Possible CAN modes.
Definition kvisotplib.h:40
@ CanFdNonIso
CAN FD Non-ISO mode.
Definition kvisotplib.h:46
@ Can
Classic CAN mode.
Definition kvisotplib.h:42
@ CanFd
CAN FD mode.
Definition kvisotplib.h:44
bool(WINAPI * isotp_set_min_separation_time_override_t)(int isotp_handle, unsigned char value)
Function prototype for setting the min_separation_time_override settings.
Definition kvisotplib.h:431
void(WINAPI * IndicationCallback)(enum MType m_type, unsigned int can_id, enum TAType ta_type, unsigned char address_extension, unsigned int length, unsigned long long timestamp, const unsigned char *payload, unsigned int payload_length, enum NResult result)
This callback prototype is used for indicating to the upper layers that a complete message has been r...
Definition kvisotplib.h:240
bool(WINAPI * isotp_set_a_max_t)(int isotp_handle, unsigned int value)
Function prototype for setting the a_max settings.
Definition kvisotplib.h:458
bool(WINAPI * isotp_set_block_size_override_t)(int isotp_handle, unsigned char value)
Function prototype for setting the block_size_override settings.
Definition kvisotplib.h:411
bool(WINAPI * isotp_write_t)(int isotp_handle, const struct WriteParams *write_params)
Function prototype for sending a message.
Definition kvisotplib.h:521
void clear_api(IsoTpApi *pApi)
Clears the contents of an ISO-TP API structure.
Definition kvisotplib.h:663
void unload_library(IsoTpApi *pApi)
Unload the Kvaser ISO-TP library and clear the API structure.
Definition kvisotplib.h:712
MType
The type of message to be sent.
Definition kvisotplib.h:53
@ Diagnostics
A message that is sent using normal addressing.
Definition kvisotplib.h:55
@ RemoteDiagnostics
A message that is sent using extended addressing.
Definition kvisotplib.h:57
int(WINAPI * isotp_create_t)(const struct CreateParams *create_params)
Function prototype for creating and opening a new ISO-TP channel.
Definition kvisotplib.h:296
ISO-TP address information.
Definition kvisotplib.h:115
bool use_padding
True if message padding should be used.
Definition kvisotplib.h:125
unsigned char address_extension
Address extension.
Definition kvisotplib.h:121
unsigned int id
CAN identifier.
Definition kvisotplib.h:117
bool extended_id
True if the CAN identifier is an extended (29-bit) identifier.
Definition kvisotplib.h:119
bool use_address_extension
True if the message is using extended addressing.
Definition kvisotplib.h:123
A struct that holds the parameters for creating an ISO-TP channel.
Definition kvisotplib.h:132
unsigned int channel
The number of the channel. The CANlib channel number to use.
Definition kvisotplib.h:134
enum CanMode can_mode
The CAN mode to use.
Definition kvisotplib.h:136
unsigned int data_phase_bit_rate
The data phase bit rate to use for the channel. This is only used if the channel is opened with CAN F...
Definition kvisotplib.h:144
struct AddressInfo local_address_info
Contains the CAN ID and address extension (if applicable) for the device end point of the ISO 15765 c...
Definition kvisotplib.h:148
struct AddressInfo remote_address_info
Contains the CAN ID and address extension (if applicable) for the remote/vehicle end point of the ISO...
Definition kvisotplib.h:153
unsigned int bit_rate
The bit rate to use for the channel. If CAN FD, then this is the bit rate used in arbitration....
Definition kvisotplib.h:141
bool support_virtual
True if virtual channels are supported.
Definition kvisotplib.h:138
A struct with all function pointers for the ISO-TP API.
Definition kvisotplib.h:575
isotp_open_t isotp_open
Open the ISO-TP channel.
Definition kvisotplib.h:647
isotp_set_cr_max_t isotp_set_cr_max
Set N_Cr_Max timeout.
Definition kvisotplib.h:631
isotp_set_block_size_override_t isotp_set_block_size_override
Set block size override.
Definition kvisotplib.h:623
isotp_set_block_size_t isotp_set_block_size
Set block size.
Definition kvisotplib.h:615
isotp_destroy_t isotp_destroy
Destroy an existing ISO-TP channel.
Definition kvisotplib.h:595
isotp_start_log_t isotp_start_log
Start logging.
Definition kvisotplib.h:587
isotp_set_full_duplex_t isotp_set_full_duplex
Set full duplex mode.
Definition kvisotplib.h:603
isotp_write_t isotp_write
Write a message to the ISO-TP channel.
Definition kvisotplib.h:655
isotp_set_pad_value_t isotp_set_pad_value
Set padding value.
Definition kvisotplib.h:611
bool is_valid
A flag indicating if the library was successfully loaded and if the API is available.
Definition kvisotplib.h:583
isotp_create_t isotp_create
Create a new ISO-TP channel.
Definition kvisotplib.h:591
isotp_set_wft_max_t isotp_set_wft_max
Set wft_max setting.
Definition kvisotplib.h:643
isotp_set_min_separation_time_override_t isotp_set_min_separation_time_override
Set minimum separation time override.
Definition kvisotplib.h:627
isotp_set_min_separation_time_t isotp_set_min_separation_time
Set minimum separation time.
Definition kvisotplib.h:619
isotp_close_t isotp_close
Close the ISO-TP channel.
Definition kvisotplib.h:651
isotp_register_callbacks_t isotp_register_callbacks
Register callback functions.
Definition kvisotplib.h:599
void * handle
Handle to the loaded library.
Definition kvisotplib.h:579
isotp_set_bs_max_t isotp_set_bs_max
Set N_Bs_Max timeout.
Definition kvisotplib.h:639
isotp_set_j1939_priority_masking_t isotp_set_j1939_priority_masking
Set J1939 priority masking.
Definition kvisotplib.h:607
isotp_set_a_max_t isotp_set_a_max
Set N_As_Max and N_Ar_Max timeout.
Definition kvisotplib.h:635
A struct that holds the parameters for an ISO-TP write request.
Definition kvisotplib.h:160
bool use_bit_rate_switch
True if the message should be sent as a CAN FD message and should use bit rate switching....
Definition kvisotplib.h:183
unsigned int can_id
The CAN ID to be used for the message.
Definition kvisotplib.h:165
unsigned char address_extension
The address extension to be used for the message. This field is ignored if the message type is not Re...
Definition kvisotplib.h:174
unsigned int request_handle
A unique request handle, if set to 0 then ConfirmCallback won't be called.
Definition kvisotplib.h:185
enum MType m_type
The message type, diagnostics or remote diagnostics. This is used to determine if the message should ...
Definition kvisotplib.h:163
enum TAType ta_type
The target addressing type. This is used to determine if the message should use standard (11-bit) ide...
Definition kvisotplib.h:170
const unsigned char * payload
The message data to be sent.
Definition kvisotplib.h:176
bool use_padding
True if the message should be padded.
Definition kvisotplib.h:180
unsigned int payload_length
The length of the message data to be sent.
Definition kvisotplib.h:178