|
char * | socan_str_rc (socan_rc rc) |
|
bool | socan_rc_is_error (socan_rc rc) |
|
int | socan_errprintlevel (int level) |
|
int | socan_tracelevel (int level) |
|
int | socan_add_port (const char *devicename) |
|
bool | socan_realtime_setup (bool use_rt_priority, bool require_rt_priority, int reader_priority, int writer_priority) |
|
bool | socan_init (void) |
|
socan_rc | socan_port_bitrate (unsigned char port, unsigned int *bitrate) |
|
socan_hdl | socan_open (void) |
|
bool | socan_close (socan_hdl h) |
|
socan_rc | socan_port_device (unsigned char port, char **dev) |
|
socan_rc | socan_add_obj (socan_hdl hdl, unsigned char port, unsigned short cob, unsigned char length, unsigned int tmo_ms, socan_obj_type type) |
|
socan_rc | socan_del_obj (unsigned char port, unsigned short cob) |
|
socan_rc | socan_obj_info (socan_hdl hdl, unsigned char port, unsigned short cob, unsigned char *length, unsigned int *tmo_ms, socan_obj_type *type) |
|
socan_rc | socan_obj_ts (unsigned char port, unsigned short cob, unsigned long *ts) |
|
socan_rc | socan_ts_to_str (unsigned long ts, char *buf, int buflen) |
|
socan_rc | socan_set_inhibit (unsigned char port, unsigned short cob, unsigned long inhibit_time) |
|
socan_rc | socan_use_queue (unsigned char port, unsigned short cob) |
|
socan_rc | socan_new_user_area (unsigned char port, unsigned short cob, unsigned int size, void **area) |
|
socan_rc | socan_user_area (unsigned char port, unsigned short cob, void **area) |
|
socan_rc | socan_set_user_area (unsigned char port, unsigned short cob, void *ptr) |
|
socan_rc | socan_free_user_area (unsigned char port, unsigned short cob) |
|
socan_rc | socan_write (socan_hdl hdl, unsigned char port, unsigned short cob, const void *data) |
|
socan_rc | socan_writelater (socan_hdl hdl, unsigned char port, unsigned short cob, const void *data) |
|
socan_rc | socan_write_inhibit (socan_hdl hdl, unsigned char port, unsigned short cob, const void *data, unsigned long *inhibit_time) |
|
socan_rc | socan_readnow (socan_hdl hdl, unsigned char port, unsigned short cob, void *data) |
|
socan_rc | socan_read (socan_hdl hdl, unsigned char port, unsigned short cob, void *data) |
|
socan_rc | socan_queue_read (socan_hdl hdl, unsigned char *port, unsigned short *cob, void *data) |
|
c header file for socan object layer library.
This header, socan.h, contains all data types and functions of the socan object layer library.
Main features are:
- The object layer library of socan implements CAN object data types and functions for the Linux SocketCAN interface. The CAN object abstraction allows the definition of CAN objects with a type, read, write, remote-read, remote-write, and a length, 0 to 8 bytes.
- Internal reader threads of the library ensure that incoming CAN data is stored buffers, one for each CAN object, together with a time stamp.
- The library only accepts data from CAN objects that were previously defined.
- Internal writer threads allow the application to write CAN data frames asynchronously. When the data is actually written to the CAN bus, the library stores a write time stamp with the CAN object.
- The reader and writer threads implement an automatic answer of incoming remote (RTR) CAN frames.
- The library keeps track of when incoming data was read or not. The application is informed by a flag, when it has lost CAN data. This happens when two or more CAN frames arrived for the CAN object since the last read operation on the CAN object.
- The library allows the definition of an inhibit time which prevents it from sending out CAN objects too fast for a receiver.
- The library can be used by more than one thread at once.
- CAN interfaces are distinguished by a number, a so called port. The library provides a function to associate port numbers with SocketCAN devices.
- A User defined data block can be associated with each CAN object, the application doesn't need to implement a mapping from port and cob to a data structure.
- The library supports reading of a specific CAN object or waiting for incoming data on an arbitrary number of CAN objects.
Definition in file socan.h.
#define SOCAN_MAX_PORTS 8 |
The maximum number of supported CAN ports.
Since the data type for a CAN port is unsigned char, this constant must never be greater than 255.
This constant means that the socan library can handle no more than SOCAN_MAX_PORTS CAN devices at a time. It means that you can only map up to this number SocketCAN interfaces to port numbers.
You may increase this constant and recompile the library if needed. This constant determines at some places in the code how much memory is allocated.
Definition at line 130 of file socan.h.
socan_rc socan_new_user_area |
( |
unsigned char | port, |
|
|
unsigned short | cob, |
|
|
unsigned int | size, |
|
|
void ** | area ) |
|
extern |
Create a memory block associated with a CAN object.
A memory block of arbitrary size is created with this function. This memory block is associated with the CAN object and can later be retrieved with function socan_user_area(). This can be used for the application to store additional data with a CAN object. Note that this function allocates new memory. If there was already a memory block associated, this block is freed first. When the CAN object is later deleted with socan_del_obj(), the memory block is freed, too.
- Parameters
-
[in] | port | The port number. |
[in] | cob | The CAN object ID. |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Setting the inhibit time was successful.
- SOCAN_PORT_ERR : The port number is invalid
- SOCAN_COB_ERR : The CAN object ID is invalid
- SOCAN_NOT_EXISTS_ERR : The CAN object doesn't exist.
- SOCAN_NOT_OWNED_ERR : The CAN object exists but was defined by a different thread (using a different handle 'hdl').
Definition at line 1621 of file socan.c.
socan_rc socan_obj_ts |
( |
unsigned char | port, |
|
|
unsigned short | cob, |
|
|
unsigned long * | ts ) |
|
extern |
Return the time stamp of the CAN object.
For read objects, this is the time when data arrived for this object. For write objects this is the time when data was written to the CAN bus for this object. The time stamp is measured in microseconds. The absolute value of the time stamp is compatible with the function gettimeofday as provided by libc. You would get a compatible time stamp like this:
#include <sys/time.h>
struct timeval now;
unsigned long timestamp;
gettimeofday(&now, NULL);
timestamp= now->tv_sec * 1000 + now->tv_usec / 1000;
- Parameters
-
[in] | port | The number of the CAN port |
[in] | cob | The CAN object ID. |
[out] | ts | The time stamp is returned here. |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Getting the time stamp was successful.
- SOCAN_PORT_ERR : The port number is invalid
- SOCAN_COB_ERR : The CAN object ID is invalid
- SOCAN_NOT_EXISTS_ERR : The CAN object doesn't exist.
- SOCAN_NOT_OWNED_ERR : The CAN object exists but was defined by a different thread (using a different handle 'hdl').
Definition at line 1549 of file socan.c.
socan_rc socan_queue_read |
( |
socan_hdl | hdl, |
|
|
unsigned char * | port, |
|
|
unsigned short * | cob, |
|
|
void * | data ) |
|
extern |
Blocking read any of the queue-read CAN objects.
Queue-read CAN objects are CAN objects of type SOCAN_READ or SOCAN_READ_RTR which were configured with function socan_use_queue(). The function waits until new data is available on any of the queue read objects. Note that there is no timeout here, if no new data arrives, the function will wait indefinitely. For SOCAN_READ_RTR objects new data will only be received when you create an RTR-read request first. This is done by calling socan_write(), socan_writelater() or socan_write_inhibit() on the SOCAN_READ_RTR object.
- Parameters
-
[in] | hdl | The handle obtained by socan_open(). |
[out] | port | The port number of the CAN object that was read. |
| [cob] | cob The CAN object ID. of the CAN object that was read. |
[out] | data | A pointer to the data. |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Setting the inhibit time was successful.
- SOCAN_LENGTH_ERR : The length of the received data was wrong.
- SOCAN_LOST : Data was lost, this means that since the last call of a read function for the CAN object, more than one new CAN frame has arrived. The returned data is the most recent one.
Definition at line 1867 of file socan.c.
socan_rc socan_readnow |
( |
socan_hdl | hdl, |
|
|
unsigned char | port, |
|
|
unsigned short | cob, |
|
|
void * | data ) |
|
extern |
Read buffer from a SOCAN_READ or SOCAN_READ_RTR object and return immediately.
This function reads the local data buffer of the CAN object. Note that for remote read (SOCAN_READ_RTR) objects, the function does not issue a RTR frame, no data is sent to the CAN bus. The function always returns immediately.
- Parameters
-
[in] | hdl | The handle obtained by socan_open(). |
[in] | port | The port number. |
[in] | cob | The CAN object ID. |
[out] | data | A pointer to the data. |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Setting the inhibit time was successful.
- SOCAN_PORT_ERR : The port number is invalid
- SOCAN_COB_ERR : The CAN object ID is invalid
- SOCAN_NOT_EXISTS_ERR : The CAN object doesn't exist.
- SOCAN_NOT_OWNED_ERR : The CAN object exists but was defined by a different thread (using a different handle 'hdl').
- SOCAN_NOT_RO_ERR : The CAN object is neither a SOCAN_READ nor a SOCAN_READ_RTR object.
- SOCAN_OLD : The data is old, meaning that since the last call of a read function for this CAN object, no new data has arrived.
- SOCAN_LENGTH_ERR : The length of the received data was wrong.
- SOCAN_LOST : Data was lost, this means that since the last call of a read function for this CAN object, more than one new CAN frame has arrived. The returned data is the most recent one.
Definition at line 1698 of file socan.c.
socan_rc socan_set_inhibit |
( |
unsigned char | port, |
|
|
unsigned short | cob, |
|
|
unsigned long | inhibit_time ) |
|
extern |
Sets the inhibit time of a CAN object.
The inhibit time is the minimum time that between two write operations on the same CAN object. This is intended to avoid congestion (data loss) on the receiver. The inhibit time is observed by some of the write functions, see also the documentation of these functions.
- Parameters
-
[in] | port | The port number. |
[in] | cob | The CAN object ID. |
[in] | inhibit_time | The inhibit time in microseconds |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Setting the inhibit time was successful.
- SOCAN_PORT_ERR : The port number is invalid
- SOCAN_COB_ERR : The CAN object ID is invalid
- SOCAN_NOT_EXISTS_ERR : The CAN object doesn't exist.
- SOCAN_NOT_OWNED_ERR : The CAN object exists but was defined by a different thread (using a different handle 'hdl').
- SOCAN_NOT_WO_ERR : The CAN object is not a write object.
Definition at line 1580 of file socan.c.
socan_rc socan_set_user_area |
( |
unsigned char | port, |
|
|
unsigned short | cob, |
|
|
void * | ptr ) |
|
extern |
Associate an allocated memory block with a CAN object.
A memory block that was created with malloc() or calloc() is associated with a CAN object with this function. Note that is is imperative that the memory was allocated as described above since socan will call free() on this block when the CAN object is deleted or socan_new_user_area() is called. Note too, that a previously associated memory block is not freed automatically, the application must ensure that there is no memory leak and free the old memory block itself.
- Parameters
-
[in] | port | The port number. |
[in] | cob | The CAN object ID. |
[in] | ptr | A pointer to the memory block. |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Setting the inhibit time was successful.
- SOCAN_PORT_ERR : The port number is invalid
- SOCAN_COB_ERR : The CAN object ID is invalid
- SOCAN_NOT_EXISTS_ERR : The CAN object doesn't exist.
- SOCAN_NOT_OWNED_ERR : The CAN object exists but was defined by a different thread (using a different handle 'hdl').
Definition at line 1658 of file socan.c.
socan_rc socan_write |
( |
socan_hdl | hdl, |
|
|
unsigned char | port, |
|
|
unsigned short | cob, |
|
|
const void * | data ) |
|
extern |
Write a SOCAN_WRITE or SOCAN_WRITE_RTR object synchronously.
In case of a SOCAN_WRITE object, the function triggers the writing and waits until the write is finished or a timeout occurs. In case of a SOCAN_WRITE_RTR object, the function simply writes to the internal buffer of the CAN object and returns immediately.
- Parameters
-
[in] | hdl | The handle obtained by socan_open(). |
[in] | port | The port number. |
[in] | cob | The CAN object ID. |
[in] | data | A pointer to the data. |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Setting the inhibit time was successful.
- SOCAN_PORT_ERR : The port number is invalid
- SOCAN_COB_ERR : The CAN object ID is invalid
- SOCAN_NOT_EXISTS_ERR : The CAN object doesn't exist.
- SOCAN_NOT_OWNED_ERR : The CAN object exists but was defined by a different thread (using a different handle 'hdl').
- SOCAN_NOT_WO_ERR : The CAN object is neither a SOCAN_WRITE nor a SOCAN_WRITE_RTR object.
- SOCAN_TIMEOUT : Timeout, writing didn't occur within the timeout time defined with socan_add_obj().
- SOCAN_ERROR : Internal error.
Definition at line 1927 of file socan.c.
socan_rc socan_write_inhibit |
( |
socan_hdl | hdl, |
|
|
unsigned char | port, |
|
|
unsigned short | cob, |
|
|
const void * | data, |
|
|
unsigned long * | inhibit_time ) |
|
extern |
Write a SOCAN_WRITE or SOCAN_WRITE_RTR object asynchronously with inhibit time.
In case of a SOCAN_WRITE object, the function triggers the writing and returns immediately if the time passed since the last write is bigger than the inhibit time. If this is not the case, the function returns SOCAN_WAIT and the write is aborted. In case of a SOCAN_WRITE_RTR object, the function simply writes to the internal buffer of the CAN object and returns immediately.
- Parameters
-
[in] | hdl | The handle obtained by socan_open(). |
[in] | port | The port number. |
[in] | cob | The CAN object ID. |
[in] | data | A pointer to the data. |
[out] | inhibit_time | Time the user task has to wait in microseconds. |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Setting the inhibit time was successful.
- SOCAN_PORT_ERR : The port number is invalid
- SOCAN_COB_ERR : The CAN object ID is invalid
- SOCAN_NOT_EXISTS_ERR : The CAN object doesn't exist.
- SOCAN_NOT_OWNED_ERR : The CAN object exists but was defined by a different thread (using a different handle 'hdl').
- SOCAN_NOT_WO_ERR : The CAN object is neither a SOCAN_WRITE nor a SOCAN_WRITE_RTR object.
- SOCAN_WAIT : The previous write of this CAN object either is not yet finished or the time passed since then is smaller than the inhibit time. The write request is aborted and the function returns in parameter inhibit_time the time the application has to wait before it can try to write again.
- SOCAN_TIMEOUT : The previous write didn't take place in the specified timeout time. This is only a warning, the new write request is issued nonetheless.
- SOCAN_ERROR : Internal error.
Definition at line 2072 of file socan.c.
socan_rc socan_writelater |
( |
socan_hdl | hdl, |
|
|
unsigned char | port, |
|
|
unsigned short | cob, |
|
|
const void * | data ) |
|
extern |
Write a SOCAN_WRITE or SOCAN_WRITE_RTR object asynchronously.
In case of a SOCAN_WRITE object, the function triggers the writing and returns immediately. In case of a SOCAN_WRITE_RTR object, the function simply writes to the internal buffer of the CAN object and returns immediately.
- Parameters
-
[in] | hdl | The handle obtained by socan_open(). |
[in] | port | The port number. |
[in] | cob | The CAN object ID. |
[in] | data | A pointer to the data. |
- Returns
- A socan_rc return code, possible other values are:
- SOCAN_OK : Setting the inhibit time was successful.
- SOCAN_PORT_ERR : The port number is invalid
- SOCAN_COB_ERR : The CAN object ID is invalid
- SOCAN_NOT_EXISTS_ERR : The CAN object doesn't exist.
- SOCAN_NOT_OWNED_ERR : The CAN object exists but was defined by a different thread (using a different handle 'hdl').
- SOCAN_NOT_WO_ERR : The CAN object is neither a SOCAN_WRITE nor a SOCAN_WRITE_RTR object.
- SOCAN_WAIT : The previous write of this CAN object is not yet finished, writing aborted.
- SOCAN_TIMEOUT : The previous write didn't take place in the specified timeout time. This is only a warning, the new write request is issued nonetheless.
- SOCAN_ERROR : Internal error.
Definition at line 2000 of file socan.c.