VCAN  2.0.0
Virtual CAN bus
vcan.h
Go to the documentation of this file.
1 
28 #ifndef VCAN_H
29 #define VCAN_H
30 
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #endif
35 
37 #define VCAN_VERSION "2.0.0"
38 
39 #include <stdint.h>
40 #include <stddef.h>
41 #include <string.h>
42 #include <stdlib.h>
43 
45 #define VCAN_DATA_MAX_LEN 64
46 
47 #define VCAN_MAX_CONNECTED_NODES 16
48 
50 typedef enum
51 {
53  VCAN_OK = 0,
62 
68 
73 } vcan_err_t;
74 
76 typedef struct
77 {
79  uint32_t id;
80 
82  uint32_t len;
83 
85  uint8_t data[VCAN_DATA_MAX_LEN];
86 } vcan_msg_t;
87 
94 struct vcan_node
95 {
107  void (* callback_on_rx)(struct vcan_node* node, const vcan_msg_t* msg);
108 
112 
114  uint32_t id;
115 };
116 
123 typedef struct vcan_node vcan_node_t;
124 
130 typedef struct
131 {
134 
137 
139  size_t connected;
140 } vcan_bus_t;
141 
151 
173 
188 
210  const vcan_msg_t* msg,
211  const vcan_node_t* src_node);
212 
213 #ifdef __cplusplus
214 }
215 #endif
216 
217 #endif /* VCAN_H */
vcan_node
Virtual node.
Definition: vcan.h:94
VCAN_DATA_MAX_LEN
#define VCAN_DATA_MAX_LEN
Max payload size of a CAN message in bytes.
Definition: vcan.h:45
VCAN_TOO_MANY_CONNECTED
@ VCAN_TOO_MANY_CONNECTED
Max amount of connected nodes reached.
Definition: vcan.h:67
vcan_msg_t::id
uint32_t id
The CAN ID - unused by VCAN.
Definition: vcan.h:79
VCAN_ALREADY_CONNECTED
@ VCAN_ALREADY_CONNECTED
The node is already connected to the bus.
Definition: vcan.h:72
vcan_bus_t::connected
size_t connected
Amount of nodes.
Definition: vcan.h:139
vcan_bus_t::received_msg
vcan_msg_t received_msg
The message just transmitted over the bus.
Definition: vcan.h:133
vcan_connect
vcan_err_t vcan_connect(vcan_bus_t *bus, vcan_node_t *node)
Attaches a new node to the bus, enabling it to receive any transmitted message.
vcan_node::other_custom_data
void * other_custom_data
Any data the callback may need, such as a flag to trigger on reception.
Definition: vcan.h:111
vcan_msg_t::len
uint32_t len
Used bytes in the data field.
Definition: vcan.h:82
vcan_node::id
uint32_t id
Identifier of the node.
Definition: vcan.h:114
VCAN_NODE_NOT_FOUND
@ VCAN_NODE_NOT_FOUND
This node is not connected to the bus, so it cannot be disconnected.
Definition: vcan.h:70
vcan_init
vcan_err_t vcan_init(vcan_bus_t *bus)
Initialises the bus.
vcan_tx
vcan_err_t vcan_tx(vcan_bus_t *bus, const vcan_msg_t *msg, const vcan_node_t *src_node)
Sends a copy of the message to every connected node and calls every nodes's callback to notify them.
vcan_disconnect
vcan_err_t vcan_disconnect(vcan_bus_t *bus, const vcan_node_t *node)
Detaches a node from the bus, disabling it from receiving any further messages.
VCAN_NULL_MSG
@ VCAN_NULL_MSG
The message argument is NULL.
Definition: vcan.h:57
vcan_err_t
vcan_err_t
VCAN error codes.
Definition: vcan.h:50
vcan_msg_t
Message to transmit or receive.
Definition: vcan.h:76
VCAN_MAX_CONNECTED_NODES
#define VCAN_MAX_CONNECTED_NODES
MAx amount of virtual nodes connected to the virtual bus.
Definition: vcan.h:47
VCAN_NULL_CALLBACK
@ VCAN_NULL_CALLBACK
The callback within the node is NULL.
Definition: vcan.h:61
VCAN_NULL_BUS
@ VCAN_NULL_BUS
The bus argument is NULL.
Definition: vcan.h:55
VCAN_OK
@ VCAN_OK
Successfully completed.
Definition: vcan.h:53
vcan_node::callback_on_rx
void(* callback_on_rx)(struct vcan_node *node, const vcan_msg_t *msg)
Callback called when a message has been written into received_msg.
Definition: vcan.h:107
VCAN_NULL_NODE
@ VCAN_NULL_NODE
The node argument is NULL.
Definition: vcan.h:59
vcan_bus_t
Virtual bus.
Definition: vcan.h:130