API reference
This section provides a full reference of all the functions, macros and types that LibCanComm offers.
Types
can_comm_t
Opaque pointer for the CAN communication context.
Macros
Macro | Description |
---|---|
CANCOMM_TRUE |
Boolean true value. |
CANCOMM_FALSE |
Boolean false value. |
CANCOMM_FLAG_CANFD_MSG |
Bit flag to indicate that the message is a CAN FD message. |
CANCOMM_FLAG_CANERR_MSG |
Bit flag to indicate that the message is a CAN error frame. |
Functions
cancomm_new
Creates a new CAN communication context. All subsequent library functions need this context. The context makes it possible for multiple applications to make use of this library.
Return value |
---|
Newly created context, if successful. NULL otherwise. |
Example - Create a new context: | |
---|---|
cancomm_free
Releases the context. Should be called for each CAN communication context, created with function cancomm_new()
, once you no longer need it.
Parameter | Description |
---|---|
ctx |
CAN communication context. |
Example - Release a context: | |
---|---|
cancomm_connect
Connects to the specified SocketCAN device. Note that you can use the functions cancomm_devices_buildlist()
and cancomm_devices_name()
to determine the names of the SocketCAN devices known to the system. Alternatively, you can run command ip addr
in the terminal to find out about the SocketCAN devices know to the system. This function automatically figures out if the SocketCAN device supports CAN FD, in addition to CAN classic.
Parameter | Description |
---|---|
ctx |
CAN communication context. |
device |
Null terminated string with the SocketCAN device name, e.g. "can0" . |
Return value |
---|
CANCOMM_TRUE if successfully connected to the SocketCAN device, CANCOMM_FALSE otherwise. |
Example 1 - Connect to the SocketCAN device with a specific name: | |
---|---|
cancomm_disconnect
Disconnects from the SocketCAN device.
Parameter | Description |
---|---|
ctx |
CAN communication context. |
Example - Disconnect from a SocketCAN device: | |
---|---|
cancomm_transmit
uint8_t cancomm_transmit(cancomm_t ctx, uint32_t id, uint8_t ext, uint8_t len,
uint8_t const * data, uint8_t flags, uint64_t * timestamp)
Submits a CAN message for transmission.
Parameter | Description |
---|---|
ctx |
CAN communication context. |
id |
CAN message identifier. |
ext |
Set to CANCOMM_FALSE for an 11-bit message identifier, to CANCOMM_TRUE for 29-bit. |
len |
Number of CAN message data bytes. Max 8 for a CAN classic message, max 64 for a CAN FD message. |
data |
Pointer to array with data bytes. |
flags |
Bit flags for providing additional information about how to transmit the message: - CANCOMM_FLAG_CANFD_MSG : The message is CAN FD and not CAN classic. Ignored for non CAN FD SocketCAN devices. |
timestamp |
Pointer to where the timestamp (microseconds) of the message is stored. |
Return value |
---|
CANCOMM_TRUE if successfully submitted the message for transmission. CANCOMM_FALSE otherwise. |
cancomm_receive
uint8_t cancomm_receive(cancomm_t ctx, uint32_t * id, uint8_t * ext, uint8_t * len,
uint8_t * data, uint8_t * flags, uint64_t * timestamp)
Reads a possibly received CAN message or CAN error frame in a non-blocking manner.
Parameter | Description |
---|---|
ctx |
CAN communication context. |
id |
Pointer to where the CAN message identifier is stored. |
ext |
Pointer to where the CAN identifier type is stored. CANCOMM_FALSE for an 11-bit message identifier, CANCOMM_TRUE for 29-bit. |
len |
Pointer to where the number of CAN message data bytes is stored. |
data |
Pointer to array where the data bytes are stored. |
flags |
Pointer to where the bit flags are stored for providing additional information about the received message: - CANCOMM_FLAG_CANFD_MSG : The message is CAN FD and not CAN classic.- CANCOMM_FLAG_CANERR_MSG : The message is a CAN error frame. |
timestamp |
Pointer to where the timestamp (microseconds) of the message is stored. |
Return value |
---|
CANCOMM_TRUE if a new message was received and copied. CANCOMM_FALSE otherwise. |
cancomm_devices_buildlist
Builds a list with all the CAN device names currently present on the system. Basically an internal array with strings such as "can0"
, "vcan0"
, etc. Afterwards, you can call cancomm_devices_name()
to retrieve the name of a specific SocketCAN device, using its array index.
Parameter | Description |
---|---|
ctx |
CAN communication context. |
Return value |
---|
The total number of CAN devices currently present on the system, or 0 if none were found or in case of an error. |
Example - Detecting all SocketCAN devices known to the system: | |
---|---|
cancomm_devices_name
Obtains the SocketCAN device name at the specified index of the internal list with CAN devices, created by function cancomm_devices_buildlist()
. You could use this SocketCAN device name when calling cancomm_connect()
.
Note that you should call cancomm_devices_buildlist()
at least once, before calling this function.
Parameter | Description |
---|---|
ctx |
CAN communication context. |
idx |
Zero based index into the device list. |
Return value |
---|
The CAN device name at the specified index, or NULL in case of an error. |