26 #ifndef LELY_NO_CO_WTM 41 #define CO_WTM_MAX_NIF 127 166 co_wtm_t *wtm, uint_least32_t ac,
void *data);
178 co_wtm_t *wtm,
const void *buf,
size_t nbytes);
186 return "Diagnostic protocol timed out limit reached";
190 return "Data cannot be transferred or stored to the application";
192 return "Data cannot be transferred or stored to the application because of local control";
194 return "Data cannot be transferred or stored to the application because of the present device state";
198 case CO_WTM_AC_DIAG:
return "Diagnostic data generation not supported";
200 return "Diagnostic data generation for requested CAN interface not supported";
202 return "Diagnostic data generation for requested WTM interface not supported";
205 case CO_WTM_AC_SEQ:
return "Invalid sequence counter in generic frame";
209 case CO_WTM_AC_CAN:
return "CAN telegram essentials invalid";
210 default:
return "Unknown abort code";
217 void *ptr = malloc(
sizeof(
struct __co_wtm));
224 __co_wtm_free(
void *ptr)
248 can->
recv_time = (
struct timespec){ 0, 0 };
249 can->
send_time = (
struct timespec){ 0, 0 };
250 can->
send_next = (
struct timespec){ 0, 0 };
297 goto error_alloc_wtm;
300 if (!__co_wtm_init(wtm)) {
348 uint_least8_t
err, uint_least8_t
load, uint_least16_t
ec,
349 uint_least16_t
foc, uint_least16_t
coc)
382 if (load > 100 && load != 0xff) {
402 if (quality > 100 && quality != 0xff) {
524 for (
const uint_least8_t *bp = buf; nbytes;) {
525 uint_least32_t ac = 0;
552 uint_least8_t len = wtm->
recv_buf[1];
572 uint_least8_t seq = wtm->
recv_buf[2];
580 uint_least8_t type = wtm->
recv_buf[3];
604 if (nif != 0x80 + wtm->
nif)
643 if (nif != 0x80 + wtm->
nif) {
660 if (!nif || nif == 0x80) {
671 uint_least8_t
st = 0xf;
672 switch ((wtm->
recv_buf[5] >> 4) & 0xf) {
677 uint_least8_t
err = 0xf;
697 uint_least8_t quality = wtm->
recv_buf[5];
729 if (nif != 0x80 + wtm->
nif) {
754 if (nif != 0x80 + wtm->
nif)
851 #ifndef LELY_NO_CANFD 853 if (msg->
flags & CAN_FLAG_EDL) {
879 uint_least8_t dlc = msg->
len | 0x40;
906 memcpy(bp, msg->
data, msg->
len);
912 stle_u16(bp, (uint_least16_t)(usec / 100));
1030 uint_least8_t len = (uint_least8_t)(wtm->
send_nbytes - 4);
1084 wtm->
send_buf[5] = ((can->
st & 0xf) << 4) | (can->
err & 0xf);
1126 static uint_least32_t
1133 uint_least32_t ac = 0;
1134 for (
const uint_least8_t *bp = buf; nbytes;) {
1137 uint_least8_t dlc = *bp;
1140 msg.
len = dlc & 0x0f;
1148 uint_least8_t nif = 1;
1178 if (nbytes < msg.
len) {
1182 memcpy(msg.
data, bp, msg.
len);
1186 uint_least16_t ts = 0;
1200 struct timespec *tp = NULL;
1227 #endif // !LELY_NO_CO_WTM #define CO_WTM_AC_NO_DATA
CANopen WTM abort code: No data available.
A CAN or CAN FD format frame.
void co_wtm_set_diag_can_con(co_wtm_t *wtm, co_wtm_diag_can_con_t *con, void *data)
Sets the confirmation function invoked when a CAN communication quality response is received by a CAN...
int co_wtm_send_diag_wtm_rst(co_wtm_t *wtm, uint_least8_t nif)
Sends a WTM communication quality reset message.
co_wtm_diag_ac_ind_t * diag_ac_ind
A pointer to the callback function invoked when an abort code is generated or received.
#define CO_WTM_AC_DATA
CANopen WTM abort code: Data cannot be transferred or stored to the application.
uint_least32_t id
The identifier (11 or 29 bits, depending on the CAN_FLAG_IDE flag).
void * diag_ac_data
A pointer to the user-specified data for diag_ac_ind.
void co_wtm_get_diag_can_con(const co_wtm_t *wtm, co_wtm_diag_can_con_t **pcon, void **pdata)
Retrieves the confirmation function invoked when a CAN communication quality response is received by ...
uint_least8_t len
The number of bytes in data (or the requested number of bytes in case of a remote frame)...
#define CO_WTM_AC_CRC
CANopen WTM abort code: CRC error (Generic frame).
This header file is part of the C11 and POSIX compatibility library; it includes <string.h> and defines any missing functionality.
#define CO_WTM_AC_DIAG_WTM
CANopen WTM abort code: Diagnostic data generation for requested WTM interface not supported...
struct timespec send_next
The time at which the next frame is sent.
struct timespec recv_time
The current time of the CAN frame receiver.
int co_wtm_set_diag_can(co_wtm_t *wtm, uint_least8_t nif, uint_least8_t st, uint_least8_t err, uint_least8_t load, uint_least16_t ec, uint_least16_t foc, uint_least16_t coc)
Sets the diagnostic parameters of a CAN interface.
uint_least16_t ldle_u16(const uint_least8_t src[2])
Loads a 16-bit unsigned integer in little-endian byte order.
static void co_wtm_diag_ac(co_wtm_t *wtm, uint_least32_t ac)
Invokes the diagnostic indication function.
#define CO_WTM_AC_NO_IF
CANopen WTM abort code: Requested interface not implemented.
void set_errnum(errnum_t errnum)
Sets the current (thread-specific) platform-independent error number to errnum.
#define CO_WTM_AC_DIAG_CAN
CANopen WTM abort code: Diagnostic data generation for requested CAN interface not supported...
static int co_wtm_send_diag_can_res(co_wtm_t *wtm, uint_least8_t nif)
Sends a communication quality response for a CAN interface.
void diag(enum diag_severity severity, int errc, const char *format,...)
Emits a diagnostic message.
void stle_u16(uint_least8_t dst[2], uint_least16_t x)
Stores a 16-bit unsigned integer in little-endian byte order.
void * send_data
A pointer to the user-specified data for send_func.
void * diag_can_ind_data
A pointer to the user-specified data for diag_can_ind.
This header file is part of the CANopen library; it contains the Wireless Transmission Media (WTM) de...
size_t send_nbytes
The number of bytes in send_buf.
co_wtm_send_func_t * send_func
A pointer to the callback function invoked by co_wtm_send().
uint_least8_t nif
The WTM interface indicator.
#define MIN(a, b)
Returns the minimum of a and b.
#define CO_WTM_AC_DATA_DEV
CANopen WTM abort code: Data cannot be transferred or stored to the application because of the presen...
void co_wtm_get_diag_wtm_ind(const co_wtm_t *wtm, co_wtm_diag_wtm_ind_t **pcon, void **pdata)
Retrieves the indication function invoked when a WTM communication quality reset message is received ...
This header file is part of the CANopen library; it contains the Cyclic Redundancy Check (CRC) declar...
#define CAN_MASK_BID
The mask used to extract the 11-bit Base Identifier from a CAN frame.
void co_wtm_get_diag_ac_ind(const co_wtm_t *wtm, co_wtm_diag_ac_ind_t **pind, void **pdata)
Retrieves the indication function invoked when an abort code is generated or received by a CANopen WT...
uint_least8_t st
The current CAN controller status (one of CAN_STATE_ACTIVE, CAN_STATE_PASSIVE or CAN_STATE_BUSOFF, or 0xf if the information is not available).
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
void co_wtm_set_diag_can_ind(co_wtm_t *wtm, co_wtm_diag_can_ind_t *con, void *data)
Sets the indication function invoked when a CAN communication quality reset message is received by a ...
struct timespec send_time
The current time of the CAN frame sender.
void co_wtm_diag_wtm_ind_t(co_wtm_t *wtm, void *data)
The type of a CANopen WTM diagnostic indication function, invoked when a WTM communication quality re...
void timespec_add_usec(struct timespec *tp, uint_least64_t usec)
Adds usec microseconds to the time at tp.
uint_least8_t data[CAN_MSG_MAX_LEN]
The frame payload (in case of a data frame).
co_wtm_diag_can_ind_t * diag_can_ind
A pointer to the indication function invoked when a CAN communication quality reset message is receiv...
uint_least8_t load
The current busload percentage (in the range [0..100], or 0xff if the information is not available)...
void co_wtm_diag_can_ind_t(co_wtm_t *wtm, uint_least8_t nif, void *data)
The type of a CANopen WTM diagnostic indication function, invoked when a CAN communication quality re...
co_wtm_diag_wtm_ind_t * diag_wtm_ind
A pointer to the indication function invoked when a WTM communication quality reset message is receiv...
This header file is part of the utilities library; it contains the time function declarations.
static uint_least32_t co_wtm_recv_can(co_wtm_t *wtm, const void *buf, size_t nbytes)
Processes a generic frame containing CAN messages.
#define CO_WTM_AC_TIMEOUT
CANopen WTM abort code: Diagnostic protocol timed out limit reached.
uint_least8_t send_nseq
The sequence number for sent generic frames.
#define CO_WTM_AC_DIAG
CANopen WTM abort code: Diagnostic data generation not supported.
This header file is part of the utilities library; it contains the byte order (endianness) function d...
#define CO_WTM_AC_SEQ
CANopen WTM abort code: Invalid sequence counter in generic frame.
The Remote Transmission Request (RTR) flag (unavailable in CAN FD format frames). ...
This is the internal header file of the CANopen library.
#define CO_WTM_MAX_LEN
The maximum size of a CANopen WTM generic frame (4 (header) + 255 (payload) + 2 (CRC checksum) = 261)...
int co_wtm_send_alive(co_wtm_t *wtm)
Sends a keep-alive message from a CANopen WTM interface.
#define CO_WTM_AC_CAN
CANopen WTM abort code: CAN telegram essentials invalid.
const char * co_wtm_ac_str(uint_least32_t ac)
Returns a string describing a CANopen WTM abort code.
#define CAN_MAX_LEN
The maximum number of bytes in the payload of a CAN format frame.
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function...
#define CO_WTM_MAX_NIF
The maximum value of a CAN/WTM interface indicator.
#define MAX(a, b)
Returns the maximum of a and b.
This header file is part of the CAN library; it contains CAN bus error definitions.
uint_least8_t quality
The link quality percentage (in the range [0..100], or 0xff if the information is not available)...
#define CO_WTM_AC_PAYLOAD
CANopen WTM abort code: Payload field in generic frame invalid.
int co_wtm_send_diag_can_req(co_wtm_t *wtm, uint_least8_t nif)
Sends a CAN communication quality request.
#define CO_WTM_AC_PREAMBLE
CANopen WTM abort code: Invalid generic frame preamble.
int co_wtm_recv_func_t(co_wtm_t *wtm, uint_least8_t nif, const struct timespec *tp, const struct can_msg *msg, void *data)
The type of a CANopen WTM receive callback function, invoked when a CAN frame is received.
void co_wtm_recv(co_wtm_t *wtm, const void *buf, size_t nbytes)
Receives and processes a byte stream with a CANopen WTM interface.
void co_wtm_set_diag_wtm_con(co_wtm_t *wtm, co_wtm_diag_wtm_con_t *con, void *data)
Sets the confirmation function invoked when a WTM communication quality response is received by a CAN...
void * diag_wtm_ind_data
A pointer to the user-specified data for diag_wtm_ind.
void co_wtm_set_diag_ac_ind(co_wtm_t *wtm, co_wtm_diag_ac_ind_t *ind, void *data)
Sets the indication function invoked when an abort code is generated or received by a CANopen WTM int...
static void default_wtm_diag_ac_ind(co_wtm_t *wtm, uint_least32_t ac, void *data)
The default diagnostic indication function.
void co_wtm_destroy(co_wtm_t *wtm)
Destroys a CANopen Wireless Transmission Media (WTM) interface.
int errno2c(int errnum)
Transforms a standard C error number to a native error code.
This header file is part of the utilities library; it contains the diagnostic declarations.
#define CO_WTM_AC_DATA_CTL
CANopen WTM abort code: Data cannot be transferred or stored to the application because of local cont...
void stle_u32(uint_least8_t dst[4], uint_least32_t x)
Stores a 32-bit unsigned integer in little-endian byte order.
int co_wtm_flush(co_wtm_t *wtm)
Flushes the current send buffer of a CANopen WTM interface.
void co_wtm_diag_ac_ind_t(co_wtm_t *wtm, uint_least32_t ac, void *data)
The type of a CANopen WTM diagnostic indication function, invoked when an abort code is generated or ...
uint_least8_t err
The last detected error (0 if no error was detected, one of CAN_ERROR_BIT, CAN_ERROR_STUFF, CAN_ERROR_CRC, CAN_ERROR_FORM or CAN_ERROR_ACK in case of an error, or 0xf if the information is not available).
#define CO_WTM_AC_ERROR
CANopen WTM abort code: General error.
uint_least8_t send_buf[CO_WTM_MAX_LEN]
The buffer used to send byte streams.
size_t recv_nbytes
The number of bytes in recv_buf.
uint_least8_t flags
The flags (any combination of CAN_FLAG_IDE, CAN_FLAG_RTR, CAN_FLAG_FDF, CAN_FLAG_BRS and CAN_FLAG_ESI...
uint_least8_t recv_nseq
The sequence number for received generic frames.
The Identifier Extension (IDE) flag.
int_least64_t timespec_diff_usec(const struct timespec *t1, const struct timespec *t2)
Returns the time difference (in microseconds) between *t1 and *t2.
int co_wtm_set_time(co_wtm_t *wtm, uint_least8_t nif, const struct timespec *tp)
Sets the current time of a CANopen WTM interface.
uint_least8_t recv_buf[CO_WTM_MAX_LEN]
The buffer used to receive byte streams.
int co_wtm_send_diag_ac(co_wtm_t *wtm, uint_least32_t ac)
Sends a diagnostic abort message from a CANopen WTM interface.
#define CO_WTM_AC_IF_DOWN
CANopen WTM abort code: Requested interface disabled.
#define CO_WTM_AC_TYPE
CANopen WTM abort code: Message type not valid or unknown.
A CANopen Wireless Transmission Media (WTM) interface.
co_wtm_diag_can_con_t * diag_can_con
A pointer to the confirmation function invoked when a CAN communication quality response is received...
#define CO_WTM_AC_FRAME
CANopen WTM abort code: General generic frame error.
void * recv_data
A pointer to the user-specified data for recv_func.
errnum_t get_errnum(void)
Returns the last (thread-specific) platform-independent error number set by a system call or library ...
int co_wtm_send_diag_can_rst(co_wtm_t *wtm, uint_least8_t nif)
Sends a CAN communication quality reset message.
void co_wtm_get_diag_can_ind(const co_wtm_t *wtm, co_wtm_diag_can_ind_t **pcon, void **pdata)
Retrieves the indication function invoked when a CAN communication quality reset message is received ...
int co_wtm_set_diag_wtm(co_wtm_t *wtm, uint_least8_t quality)
Sets the diagnostic parameters of a WTM interface.
uint_least16_t coc
The CAN controller overrun counter (in the range [0..0xfffe], or 0xffff if the information is not ava...
void co_wtm_set_diag_wtm_ind(co_wtm_t *wtm, co_wtm_diag_wtm_ind_t *con, void *data)
Sets the indication function invoked when a WTM communication quality reset message is received by a ...
void * diag_wtm_con_data
A pointer to the user-specified data for diag_wtm_con.
co_wtm_recv_func_t * recv_func
A pointer to the callback function invoked by co_wtm_recv().
int co_wtm_send_diag_wtm_req(co_wtm_t *wtm, uint_least8_t nif)
Sends a WTM communication quality request.
void co_wtm_get_recv_func(const co_wtm_t *wtm, co_wtm_recv_func_t **pfunc, void **pdata)
Retrieves the callback function invoked when a CAN frame is received by a CANopen WTM interface...
void co_wtm_set_recv_func(co_wtm_t *wtm, co_wtm_recv_func_t *func, void *data)
Sets the callback function invoked when a CAN frame is received by a CANopen WTM interface.
co_wtm_t * co_wtm_create(void)
Creates a new CANopen Wireless Transmission Media (WTM) interface.
#define CO_WTM_AC_HARDWARE
CANopen WTM abort code: Access failed due to a hardware error.
struct co_wtm_can can[CO_WTM_MAX_NIF]
The CAN interfaces.
void co_wtm_diag_wtm_con_t(co_wtm_t *wtm, uint_least8_t nif, uint_least8_t quality, void *data)
The type of a CANopen WTM diagnostic confirmation function, invoked when a WTM communication quality ...
This header file is part of the C11 and POSIX compatibility library; it includes <stdlib.h> and defines any missing functionality.
uint_least16_t foc
The FIFO overrun counter (in the range [0..0xfffe], or 0xffff if the information is not available)...
int co_wtm_send(co_wtm_t *wtm, uint_least8_t nif, const struct can_msg *msg)
Sends a CAN frame from a CANopen WTM interface.
void co_wtm_diag_can_con_t(co_wtm_t *wtm, uint_least8_t nif, uint_least8_t st, uint_least8_t err, uint_least8_t load, uint_least16_t ec, uint_least16_t foc, uint_least16_t coc, void *data)
The type of a CANopen WTM diagnostic confirmation function, invoked when a CAN communication quality ...
void co_wtm_get_diag_wtm_con(const co_wtm_t *wtm, co_wtm_diag_wtm_con_t **pcon, void **pdata)
Retrieves the confirmation function invoked when a WTM communication quality response is received by ...
#define CO_WTM_AC_NO_MEM
CANopen WTM abort code: Out of memory.
uint_least16_t co_crc(uint_least16_t crc, const uint_least8_t *bp, size_t n)
Computes a CRC-16 checksum.
A CANopen WTM CAN interface.
int co_wtm_get_time(const co_wtm_t *wtm, uint_least8_t nif, struct timespec *tp)
Retrieves the current time of a CANopen WTM interface.
uint_least16_t ec
The number of detected errors that led to the increase of one of the CAN controller internal error co...
int co_wtm_send_func_t(co_wtm_t *wtm, const void *buf, size_t nbytes, void *data)
The type of a CANopen WTM send callback function, invoked when a byte stream needs to be sent...
#define CAN_MSG_INIT
The static initializer for can_msg.
The error passive state (TX/RX error count < 256).
int co_wtm_set_nif(co_wtm_t *wtm, uint_least8_t nif)
Sets the interface indicator of a CANopen WTM interface.
static int co_wtm_send_diag_wtm_res(co_wtm_t *wtm)
Sends a communication quality response for a WTM interface.
The bus off state (TX/RX error count >= 256).
co_wtm_diag_wtm_con_t * diag_wtm_con
A pointer to the confirmation function invoked when a WTM communication quality response is received...
void co_wtm_get_send_func(const co_wtm_t *wtm, co_wtm_send_func_t **pfunc, void **pdata)
Retrieves the callback function used to send byte streams from a CANopen WTM interface.
uint_least32_t ldle_u32(const uint_least8_t src[4])
Loads a 32-bit unsigned integer in little-endian byte order.
void co_wtm_set_send_func(co_wtm_t *wtm, co_wtm_send_func_t *func, void *data)
Sets the callback function used to send byte streams from a CANopen WTM interface.
#define CAN_MASK_EID
The mask used to extract the 29-bit Extended Identifier from a CAN frame.
The error active state (TX/RX error count < 128).
uint_least8_t co_wtm_get_nif(const co_wtm_t *wtm)
Returns the interface indicator of a CANopen WTM interface.
void * diag_can_con_data
A pointer to the user-specified data for diag_can_con.