iso14229 0.9.0
ISO14229-1 (UDS) C Library
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Functions
iso14229.c File Reference

ISO14229-1 (UDS) library. More...

#include "iso14229.h"
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <linux/can/isotp.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <assert.h>
#include <stddef.h>
Include dependency graph for iso14229.c:

Go to the source code of this file.

Classes

struct  Msg
 

Macros

#define STATE_IDLE   0
 
#define STATE_SENDING   1
 
#define STATE_AWAIT_SEND_COMPLETE   2
 
#define STATE_AWAIT_RESPONSE   3
 
#define MAX_NUM_TP   16
 
#define NUM_MSGS   8
 

Typedefs

typedef UDSErr_t(* UDSService) (UDSServer_t *srv, UDSReq_t *r)
 

Functions

UDSErr_t UDSClientInit (UDSClient_t *client)
 
UDSErr_t UDSSendBytes (UDSClient_t *client, const uint8_t *data, uint16_t size)
 
UDSErr_t UDSSendECUReset (UDSClient_t *client, uint8_t type)
 
UDSErr_t UDSSendDiagSessCtrl (UDSClient_t *client, uint8_t mode)
 
UDSErr_t UDSSendCommCtrl (UDSClient_t *client, uint8_t ctrl, uint8_t comm)
 
UDSErr_t UDSSendTesterPresent (UDSClient_t *client)
 
UDSErr_t UDSSendRDBI (UDSClient_t *client, const uint16_t *didList, const uint16_t numDataIdentifiers)
 
UDSErr_t UDSSendWDBI (UDSClient_t *client, uint16_t dataIdentifier, const uint8_t *data, uint16_t size)
 
UDSErr_t UDSSendRoutineCtrl (UDSClient_t *client, uint8_t type, uint16_t routineIdentifier, const uint8_t *data, uint16_t size)
 
UDSErr_t UDSSendRequestDownload (UDSClient_t *client, uint8_t dataFormatIdentifier, uint8_t addressAndLengthFormatIdentifier, size_t memoryAddress, size_t memorySize)
 
UDSErr_t UDSSendRequestUpload (UDSClient_t *client, uint8_t dataFormatIdentifier, uint8_t addressAndLengthFormatIdentifier, size_t memoryAddress, size_t memorySize)
 
UDSErr_t UDSSendTransferData (UDSClient_t *client, uint8_t blockSequenceCounter, const uint16_t blockLength, const uint8_t *data, uint16_t size)
 
UDSErr_t UDSSendTransferDataStream (UDSClient_t *client, uint8_t blockSequenceCounter, const uint16_t blockLength, FILE *fd)
 
UDSErr_t UDSSendRequestTransferExit (UDSClient_t *client)
 
UDSErr_t UDSSendRequestFileTransfer (UDSClient_t *client, uint8_t mode, const char *filePath, uint8_t dataFormatIdentifier, uint8_t fileSizeParameterLength, size_t fileSizeUncompressed, size_t fileSizeCompressed)
 
UDSErr_t UDSCtrlDTCSetting (UDSClient_t *client, uint8_t dtcSettingType, uint8_t *data, uint16_t size)
 
UDSErr_t UDSSendSecurityAccess (UDSClient_t *client, uint8_t level, uint8_t *data, uint16_t size)
 
UDSErr_t UDSUnpackSecurityAccessResponse (const UDSClient_t *client, struct SecurityAccessResponse *resp)
 
UDSErr_t UDSUnpackRoutineControlResponse (const UDSClient_t *client, struct RoutineControlResponse *resp)
 
UDSErr_t UDSUnpackRequestDownloadResponse (const UDSClient_t *client, struct RequestDownloadResponse *resp)
 
UDSErr_t UDSClientPoll (UDSClient_t *client)
 
UDSErr_t UDSUnpackRDBIResponse (UDSClient_t *client, UDSRDBIVar_t *vars, uint16_t numVars)
 
UDSErr_t UDSServerInit (UDSServer_t *srv)
 
void UDSServerPoll (UDSServer_t *srv)
 
ssize_t UDSTpSend (struct UDSTp *hdl, const uint8_t *buf, ssize_t len, UDSSDU_t *info)
 
ssize_t UDSTpRecv (struct UDSTp *hdl, uint8_t *buf, size_t bufsize, UDSSDU_t *info)
 
UDSTpStatus_t UDSTpPoll (struct UDSTp *hdl)
 
uint32_t UDSMillis (void)
 Get time in milliseconds.
 
bool UDSSecurityAccessLevelIsReserved (uint8_t subFunction)
 Check if a security level is reserved per ISO14229-1:2020 Table 42.
 
const char * UDSErrToStr (UDSErr_t err)
 
const char * UDSEventToStr (UDSEvent_t evt)
 
bool UDSErrIsNRC (UDSErr_t err)
 
UDSErr_t UDSISOTpCInit (UDSISOTpC_t *tp, const UDSISOTpCConfig_t *cfg)
 
uint32_t isotp_user_get_us (void)
 user implemented, gets the amount of time passed since the last call in microseconds
 
 __attribute__ ((format(printf, 1, 2)))
 
int isotp_user_send_can (const uint32_t arbitration_id, const uint8_t *data, const uint8_t size, void *user_data)
 user implemented, send can message. should return ISOTP_RET_OK when success.
 
UDSErr_t UDSTpISOTpCInit (UDSTpISOTpC_t *tp, const char *ifname, uint32_t source_addr, uint32_t target_addr, uint32_t source_addr_func, uint32_t target_addr_func)
 
void UDSTpISOTpCDeinit (UDSTpISOTpC_t *tp)
 
UDSErr_t UDSTpIsoTpSockInitServer (UDSTpIsoTpSock_t *tp, const char *ifname, uint32_t source_addr, uint32_t target_addr, uint32_t source_addr_func)
 
UDSErr_t UDSTpIsoTpSockInitClient (UDSTpIsoTpSock_t *tp, const char *ifname, uint32_t source_addr, uint32_t target_addr, uint32_t target_addr_func)
 
void UDSTpIsoTpSockDeinit (UDSTpIsoTpSock_t *tp)
 
UDSTp_tISOTPMockNew (const char *name, ISOTPMockArgs_t *args)
 Create a mock transport. It is connected by default to a broadcast network of all other mock transports in the same process.
 
void ISOTPMockConnect (UDSTp_t *tp1, UDSTp_t *tp2)
 
void ISOTPMockLogToFile (const char *filename)
 write all messages to a file
 
void ISOTPMockLogToStdout (void)
 
void ISOTPMockReset (void)
 clear all transports and close the log file
 
void ISOTPMockFree (UDSTp_t *tp)
 
int isotp_send (IsoTpLink *link, const uint8_t payload[], uint16_t size)
 PUBLIC FUNCTIONS ///.
 
int isotp_send_with_id (IsoTpLink *link, uint32_t id, const uint8_t payload[], uint16_t size)
 See isotp_send, with the exception that this function is used only for functional addressing.
 
void isotp_on_can_message (IsoTpLink *link, const uint8_t *data, uint8_t len)
 Handles incoming CAN messages. Determines whether an incoming message is a valid ISO-TP frame or not and handles it accordingly.
 
int isotp_receive (IsoTpLink *link, uint8_t *payload, const uint16_t payload_size, uint16_t *out_size)
 Receives and parses the received data and copies the parsed data in to the internal buffer.
 
void isotp_init_link (IsoTpLink *link, uint32_t sendid, uint8_t *sendbuf, uint16_t sendbufsize, uint8_t *recvbuf, uint16_t recvbufsize)
 Initialises the ISO-TP library.
 
void isotp_poll (IsoTpLink *link)
 Polling function; call this function periodically to handle timeouts, send consecutive frames, etc.
 

Detailed Description

ISO14229-1 (UDS) library.

See also
https://github.com/driftregion/iso14229

Definition in file iso14229.c.

Macro Definition Documentation

◆ MAX_NUM_TP

#define MAX_NUM_TP   16

Definition at line 3505 of file iso14229.c.

◆ NUM_MSGS

#define NUM_MSGS   8

Definition at line 3506 of file iso14229.c.

◆ STATE_AWAIT_RESPONSE

#define STATE_AWAIT_RESPONSE   3

Definition at line 18 of file iso14229.c.

◆ STATE_AWAIT_SEND_COMPLETE

#define STATE_AWAIT_SEND_COMPLETE   2

Definition at line 17 of file iso14229.c.

◆ STATE_IDLE

#define STATE_IDLE   0

Definition at line 15 of file iso14229.c.

◆ STATE_SENDING

#define STATE_SENDING   1

Definition at line 16 of file iso14229.c.

Typedef Documentation

◆ UDSService

typedef UDSErr_t(* UDSService) (UDSServer_t *srv, UDSReq_t *r)

Definition at line 2219 of file iso14229.c.

Function Documentation

◆ __attribute__()

__attribute__ ( (format(printf, 1, 2))  )

Definition at line 3031 of file iso14229.c.

◆ isotp_init_link()

void isotp_init_link ( IsoTpLink link,
uint32_t  sendid,
uint8_t *  sendbuf,
uint16_t  sendbufsize,
uint8_t *  recvbuf,
uint16_t  recvbufsize 
)

Initialises the ISO-TP library.

Parameters
linkThe
instance used for transceiving data.
sendidThe ID used to send data to other CAN nodes.
sendbufA pointer to an area in memory which can be used as a buffer for data to be sent.
sendbufsizeThe size of the buffer area.
recvbufA pointer to an area in memory which can be used as a buffer for data to be received.
recvbufsizeThe size of the buffer area.

Definition at line 4205 of file iso14229.c.

◆ isotp_on_can_message()

void isotp_on_can_message ( IsoTpLink link,
const uint8_t *  data,
uint8_t  len 
)

Handles incoming CAN messages. Determines whether an incoming message is a valid ISO-TP frame or not and handles it accordingly.

Parameters
linkThe instance used for transceiving data.
dataThe data received via CAN.
lenThe length of the data received.

Definition at line 4036 of file iso14229.c.

◆ isotp_poll()

void isotp_poll ( IsoTpLink link)

Polling function; call this function periodically to handle timeouts, send consecutive frames, etc.

Parameters
linkThe instance used.

Definition at line 4218 of file iso14229.c.

◆ isotp_receive()

int isotp_receive ( IsoTpLink link,
uint8_t *  payload,
const uint16_t  payload_size,
uint16_t *  out_size 
)

Receives and parses the received data and copies the parsed data in to the internal buffer.

Parameters
linkThe IsoTpLink instance used to transceive data.
payloadA pointer to an area in memory where the raw data is copied from.
payload_sizeThe size of the received (raw) CAN data.
out_sizeA reference to a variable which will contain the size of the actual (parsed) data.
Returns
Possible return values:

Definition at line 4185 of file iso14229.c.

◆ isotp_send()

int isotp_send ( IsoTpLink link,
const uint8_t  payload[],
uint16_t  size 
)

PUBLIC FUNCTIONS ///.

Sends ISO-TP frames via CAN, using the ID set in the initialising function.

Definition at line 3979 of file iso14229.c.

◆ isotp_send_with_id()

int isotp_send_with_id ( IsoTpLink link,
uint32_t  id,
const uint8_t  payload[],
uint16_t  size 
)

See isotp_send, with the exception that this function is used only for functional addressing.

Definition at line 3983 of file iso14229.c.

◆ isotp_user_get_us()

uint32_t isotp_user_get_us ( void  )

user implemented, gets the amount of time passed since the last call in microseconds

Definition at line 3029 of file iso14229.c.

◆ isotp_user_send_can()

int isotp_user_send_can ( const uint32_t  arbitration_id,
const uint8_t *  data,
const uint8_t  size,
void *  arg 
)

user implemented, send can message. should return ISOTP_RET_OK when success.

Returns
may return ISOTP_RET_NOSPACE if the CAN transfer should be retried later or ISOTP_RET_ERROR if transmission couldn't be completed

Definition at line 3041 of file iso14229.c.

◆ ISOTPMockFree()

void ISOTPMockFree ( UDSTp_t tp)

Definition at line 3711 of file iso14229.c.

◆ ISOTPMockLogToFile()

void ISOTPMockLogToFile ( const char *  filename)

write all messages to a file

Note
uses UDSMillis() to get the current time
Parameters
filenamelog file name (will be overwritten)

Definition at line 3680 of file iso14229.c.

◆ ISOTPMockLogToStdout()

void ISOTPMockLogToStdout ( void  )

Definition at line 3697 of file iso14229.c.

◆ ISOTPMockNew()

UDSTp_t * ISOTPMockNew ( const char *  name,
ISOTPMockArgs_t args 
)

Create a mock transport. It is connected by default to a broadcast network of all other mock transports in the same process.

Parameters
nameoptional name of the transport (can be NULL)
Returns
UDSTp_t*

Definition at line 3660 of file iso14229.c.

◆ ISOTPMockReset()

void ISOTPMockReset ( void  )

clear all transports and close the log file

Definition at line 3704 of file iso14229.c.

◆ UDSClientInit()

UDSErr_t UDSClientInit ( UDSClient_t client)

Definition at line 20 of file iso14229.c.

◆ UDSClientPoll()

UDSErr_t UDSClientPoll ( UDSClient_t client)

Definition at line 827 of file iso14229.c.

◆ UDSCtrlDTCSetting()

UDSErr_t UDSCtrlDTCSetting ( UDSClient_t client,
uint8_t  dtcSettingType,
uint8_t *  data,
uint16_t  size 
)

Definition at line 667 of file iso14229.c.

◆ UDSErrIsNRC()

bool UDSErrIsNRC ( UDSErr_t  err)

Definition at line 2777 of file iso14229.c.

◆ UDSErrToStr()

const char * UDSErrToStr ( UDSErr_t  err)

Definition at line 2574 of file iso14229.c.

◆ UDSEventToStr()

const char * UDSEventToStr ( UDSEvent_t  evt)

Definition at line 2723 of file iso14229.c.

◆ UDSISOTpCInit()

UDSErr_t UDSISOTpCInit ( UDSISOTpC_t tp,
const UDSISOTpCConfig_t cfg 
)

Definition at line 2962 of file iso14229.c.

◆ UDSMillis()

uint32_t UDSMillis ( void  )

Get time in milliseconds.

Returns
current time in milliseconds

Definition at line 2532 of file iso14229.c.

◆ UDSSecurityAccessLevelIsReserved()

bool UDSSecurityAccessLevelIsReserved ( uint8_t  subFunction)

Check if a security level is reserved per ISO14229-1:2020 Table 42.

Parameters
securityLevel
Returns
true
false

Definition at line 2560 of file iso14229.c.

◆ UDSSendBytes()

UDSErr_t UDSSendBytes ( UDSClient_t client,
const uint8_t *  data,
uint16_t  size 
)

Definition at line 306 of file iso14229.c.

◆ UDSSendCommCtrl()

UDSErr_t UDSSendCommCtrl ( UDSClient_t client,
uint8_t  ctrl,
uint8_t  comm 
)

Definition at line 341 of file iso14229.c.

◆ UDSSendDiagSessCtrl()

UDSErr_t UDSSendDiagSessCtrl ( UDSClient_t client,
uint8_t  mode 
)

Definition at line 330 of file iso14229.c.

◆ UDSSendECUReset()

UDSErr_t UDSSendECUReset ( UDSClient_t client,
uint8_t  type 
)

Definition at line 319 of file iso14229.c.

◆ UDSSendRDBI()

UDSErr_t UDSSendRDBI ( UDSClient_t client,
const uint16_t *  didList,
const uint16_t  numDataIdentifiers 
)

Definition at line 364 of file iso14229.c.

◆ UDSSendRequestDownload()

UDSErr_t UDSSendRequestDownload ( UDSClient_t client,
uint8_t  dataFormatIdentifier,
uint8_t  addressAndLengthFormatIdentifier,
size_t  memoryAddress,
size_t  memorySize 
)

Definition at line 456 of file iso14229.c.

◆ UDSSendRequestFileTransfer()

UDSErr_t UDSSendRequestFileTransfer ( UDSClient_t client,
uint8_t  mode,
const char *  filePath,
uint8_t  dataFormatIdentifier,
uint8_t  fileSizeParameterLength,
size_t  fileSizeUncompressed,
size_t  fileSizeCompressed 
)

Definition at line 599 of file iso14229.c.

◆ UDSSendRequestTransferExit()

UDSErr_t UDSSendRequestTransferExit ( UDSClient_t client)

Definition at line 589 of file iso14229.c.

◆ UDSSendRequestUpload()

UDSErr_t UDSSendRequestUpload ( UDSClient_t client,
uint8_t  dataFormatIdentifier,
uint8_t  addressAndLengthFormatIdentifier,
size_t  memoryAddress,
size_t  memorySize 
)

Definition at line 497 of file iso14229.c.

◆ UDSSendRoutineCtrl()

UDSErr_t UDSSendRoutineCtrl ( UDSClient_t client,
uint8_t  type,
uint16_t  routineIdentifier,
const uint8_t *  data,
uint16_t  size 
)

Definition at line 418 of file iso14229.c.

◆ UDSSendSecurityAccess()

UDSErr_t UDSSendSecurityAccess ( UDSClient_t client,
uint8_t  level,
uint8_t *  data,
uint16_t  size 
)

Definition at line 710 of file iso14229.c.

◆ UDSSendTesterPresent()

UDSErr_t UDSSendTesterPresent ( UDSClient_t client)

Definition at line 353 of file iso14229.c.

◆ UDSSendTransferData()

UDSErr_t UDSSendTransferData ( UDSClient_t client,
uint8_t  blockSequenceCounter,
const uint16_t  blockLength,
const uint8_t *  data,
uint16_t  size 
)

Definition at line 537 of file iso14229.c.

◆ UDSSendTransferDataStream()

UDSErr_t UDSSendTransferDataStream ( UDSClient_t client,
uint8_t  blockSequenceCounter,
const uint16_t  blockLength,
FILE *  fd 
)

Definition at line 561 of file iso14229.c.

◆ UDSSendWDBI()

UDSErr_t UDSSendWDBI ( UDSClient_t client,
uint16_t  dataIdentifier,
const uint8_t *  data,
uint16_t  size 
)

Definition at line 387 of file iso14229.c.

◆ UDSServerInit()

UDSErr_t UDSServerInit ( UDSServer_t srv)

Definition at line 2404 of file iso14229.c.

◆ UDSServerPoll()

void UDSServerPoll ( UDSServer_t srv)

Definition at line 2421 of file iso14229.c.

◆ UDSTpISOTpCDeinit()

void UDSTpISOTpCDeinit ( UDSTpISOTpC_t tp)

Definition at line 3205 of file iso14229.c.

◆ UDSTpISOTpCInit()

UDSErr_t UDSTpISOTpCInit ( UDSTpISOTpC_t tp,
const char *  ifname,
uint32_t  source_addr,
uint32_t  target_addr,
uint32_t  source_addr_func,
uint32_t  target_addr_func 
)

Definition at line 3180 of file iso14229.c.

◆ UDSTpIsoTpSockDeinit()

void UDSTpIsoTpSockDeinit ( UDSTpIsoTpSock_t tp)

Definition at line 3480 of file iso14229.c.

◆ UDSTpIsoTpSockInitClient()

UDSErr_t UDSTpIsoTpSockInitClient ( UDSTpIsoTpSock_t tp,
const char *  ifname,
uint32_t  source_addr,
uint32_t  target_addr,
uint32_t  target_addr_func 
)

Definition at line 3454 of file iso14229.c.

◆ UDSTpIsoTpSockInitServer()

UDSErr_t UDSTpIsoTpSockInitServer ( UDSTpIsoTpSock_t tp,
const char *  ifname,
uint32_t  source_addr,
uint32_t  target_addr,
uint32_t  source_addr_func 
)

Definition at line 3428 of file iso14229.c.

◆ UDSTpPoll()

UDSTpStatus_t UDSTpPoll ( struct UDSTp *  hdl)

Definition at line 2520 of file iso14229.c.

◆ UDSTpRecv()

ssize_t UDSTpRecv ( struct UDSTp *  hdl,
uint8_t *  buf,
size_t  bufsize,
UDSSDU_t info 
)

Definition at line 2514 of file iso14229.c.

◆ UDSTpSend()

ssize_t UDSTpSend ( struct UDSTp *  hdl,
const uint8_t *  buf,
ssize_t  len,
UDSSDU_t info 
)

Definition at line 2508 of file iso14229.c.

◆ UDSUnpackRDBIResponse()

UDSErr_t UDSUnpackRDBIResponse ( UDSClient_t client,
UDSRDBIVar_t vars,
uint16_t  numVars 
)

Definition at line 845 of file iso14229.c.

◆ UDSUnpackRequestDownloadResponse()

UDSErr_t UDSUnpackRequestDownloadResponse ( const UDSClient_t client,
struct RequestDownloadResponse resp 
)

Definition at line 801 of file iso14229.c.

◆ UDSUnpackRoutineControlResponse()

UDSErr_t UDSUnpackRoutineControlResponse ( const UDSClient_t client,
struct RoutineControlResponse resp 
)

Definition at line 773 of file iso14229.c.

◆ UDSUnpackSecurityAccessResponse()

UDSErr_t UDSUnpackSecurityAccessResponse ( const UDSClient_t client,
struct SecurityAccessResponse resp 
)

Definition at line 748 of file iso14229.c.