Lely core libraries 2.3.4
can.h
Go to the documentation of this file.
1
22#ifndef LELY_IO2_CAN_H_
23#define LELY_IO2_CAN_H_
24
25#include <lely/ev/future.h>
26#include <lely/ev/task.h>
27#include <lely/io2/can/err.h>
28#include <lely/io2/can/msg.h>
29#include <lely/io2/dev.h>
30#include <lely/libc/time.h>
31
32#ifndef LELY_IO_CAN_INLINE
33#define LELY_IO_CAN_INLINE static inline
34#endif
35
40#if !LELY_NO_CANFD
45#endif
46 IO_CAN_BUS_FLAG_NONE = 0,
47#if LELY_NO_CANFD
48 IO_CAN_BUS_FLAG_MASK = IO_CAN_BUS_FLAG_ERR
49#else
50 IO_CAN_BUS_FLAG_MASK = IO_CAN_BUS_FLAG_ERR | IO_CAN_BUS_FLAG_FDF
52#endif
53};
54
56typedef const struct io_can_ctrl_vtbl *const io_can_ctrl_t;
57
59typedef const struct io_can_chan_vtbl *const io_can_chan_t;
60
68 int result;
70 int errc;
71};
72
80 struct can_msg *msg;
86 struct can_err *err;
93 struct timespec *tp;
98 struct ev_task task;
101};
102
104#define IO_CAN_CHAN_READ_INIT(msg, err, tp, exec, func) \
105 { \
106 (msg), (err), (tp), EV_TASK_INIT(exec, func), { 0, 0 } \
107 }
108
116 const struct can_msg *msg;
121 struct ev_task task;
126 int errc;
127};
128
130#define IO_CAN_CHAN_WRITE_INIT(msg, exec, func) \
131 { \
132 (msg), EV_TASK_INIT(exec, func), 0 \
133 }
134
135#ifdef __cplusplus
136extern "C" {
137#endif
138
140 int (*stop)(io_can_ctrl_t *ctrl);
141 int (*stopped)(const io_can_ctrl_t *ctrl);
142 int (*restart)(io_can_ctrl_t *ctrl);
143 int (*get_bitrate)(
144 const io_can_ctrl_t *ctrl, int *pnominal, int *pdata);
145 int (*set_bitrate)(io_can_ctrl_t *ctrl, int nominal, int data);
146 int (*get_state)(const io_can_ctrl_t *ctrl);
147};
148
150 io_dev_t *(*get_dev)(const io_can_chan_t *chan);
151 int (*get_flags)(const io_can_chan_t *chan);
152 int (*read)(io_can_chan_t *chan, struct can_msg *msg,
153 struct can_err *err, struct timespec *tp, int timeout);
154 void (*submit_read)(io_can_chan_t *chan, struct io_can_chan_read *read);
155 int (*write)(io_can_chan_t *chan, const struct can_msg *msg,
156 int timeout);
157 void (*submit_write)(
158 io_can_chan_t *chan, struct io_can_chan_write *write);
159};
160
171LELY_IO_CAN_INLINE int io_can_ctrl_stop(io_can_ctrl_t *ctrl);
172
177LELY_IO_CAN_INLINE int io_can_ctrl_stopped(const io_can_ctrl_t *ctrl);
178
187LELY_IO_CAN_INLINE int io_can_ctrl_restart(io_can_ctrl_t *ctrl);
188
203LELY_IO_CAN_INLINE int io_can_ctrl_get_bitrate(
204 const io_can_ctrl_t *ctrl, int *pnominal, int *pdata);
205
220LELY_IO_CAN_INLINE int io_can_ctrl_set_bitrate(
221 io_can_ctrl_t *ctrl, int nominal, int data);
222
229LELY_IO_CAN_INLINE int io_can_ctrl_get_state(const io_can_ctrl_t *ctrl);
230
232static inline io_ctx_t *io_can_chan_get_ctx(const io_can_chan_t *chan);
233
235static inline ev_exec_t *io_can_chan_get_exec(const io_can_chan_t *chan);
236
238static inline size_t io_can_chan_cancel(
239 io_can_chan_t *chan, struct ev_task *task);
240
242static inline size_t io_can_chan_abort(
243 io_can_chan_t *chan, struct ev_task *task);
244
246LELY_IO_CAN_INLINE io_dev_t *io_can_chan_get_dev(const io_can_chan_t *chan);
247
253LELY_IO_CAN_INLINE int io_can_chan_get_flags(const io_can_chan_t *chan);
254
273LELY_IO_CAN_INLINE int io_can_chan_read(io_can_chan_t *chan,
274 struct can_msg *msg, struct can_err *err, struct timespec *tp,
275 int timeout);
276
282LELY_IO_CAN_INLINE void io_can_chan_submit_read(
283 io_can_chan_t *chan, struct io_can_chan_read *read);
284
294static inline size_t io_can_chan_cancel_read(
295 io_can_chan_t *chan, struct io_can_chan_read *read);
296
305static inline size_t io_can_chan_abort_read(
306 io_can_chan_t *chan, struct io_can_chan_read *read);
307
328 struct can_msg *msg, struct can_err *err, struct timespec *tp,
329 struct io_can_chan_read **pread);
330
344LELY_IO_CAN_INLINE int io_can_chan_write(
345 io_can_chan_t *chan, const struct can_msg *msg, int timeout);
346
351LELY_IO_CAN_INLINE void io_can_chan_submit_write(
352 io_can_chan_t *chan, struct io_can_chan_write *write);
353
363static inline size_t io_can_chan_cancel_write(
364 io_can_chan_t *chan, struct io_can_chan_write *write);
365
374static inline size_t io_can_chan_abort_write(
375 io_can_chan_t *chan, struct io_can_chan_write *write);
376
394 const struct can_msg *msg, struct io_can_chan_write **pwrite);
395
401
407
408inline int
410{
411 return (*ctrl)->stop(ctrl);
412}
413
414inline int
416{
417 return (*ctrl)->stopped(ctrl);
418}
419
420inline int
422{
423 return (*ctrl)->restart(ctrl);
424}
425
426inline int
427io_can_ctrl_get_bitrate(const io_can_ctrl_t *ctrl, int *pnominal, int *pdata)
428{
429 return (*ctrl)->get_bitrate(ctrl, pnominal, pdata);
430}
431
432inline int
433io_can_ctrl_set_bitrate(io_can_ctrl_t *ctrl, int nominal, int data)
434{
435 return (*ctrl)->set_bitrate(ctrl, nominal, data);
436}
437
438inline int
440{
441 return (*ctrl)->get_state(ctrl);
442}
443
444static inline io_ctx_t *
446{
448}
449
450static inline ev_exec_t *
452{
454}
455
456static inline size_t
458{
460}
461
462static inline size_t
464{
466}
467
468inline io_dev_t *
470{
471 return (*chan)->get_dev(chan);
472}
473
474inline int
476{
477 return (*chan)->get_flags(chan);
478}
479
480inline int
482 struct timespec *tp, int timeout)
483{
484 return (*chan)->read(chan, msg, err, tp, timeout);
485}
486
487inline void
489{
490 (*chan)->submit_read(chan, read);
491}
492
493static inline size_t
495{
496 return io_can_chan_cancel(chan, &read->task);
497}
498
499static inline size_t
501{
502 return io_can_chan_abort(chan, &read->task);
503}
504
505inline int
506io_can_chan_write(io_can_chan_t *chan, const struct can_msg *msg, int timeout)
507{
508 return (*chan)->write(chan, msg, timeout);
509}
510
511inline void
513{
514 (*chan)->submit_write(chan, write);
515}
516
517static inline size_t
519{
520 return io_can_chan_cancel(chan, &write->task);
521}
522
523static inline size_t
525{
526 return io_can_chan_abort(chan, &write->task);
527}
528
529#ifdef __cplusplus
530}
531#endif
532
533#endif // !LELY_IO2_CAN_H_
This header file is part of the event library; it contains the futures and promises declarations.
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
int io_can_ctrl_stopped(const io_can_ctrl_t *ctrl)
Returns 1 in the CAN controller is stopped, 0 if not, and -1 on error.
Definition: can.h:415
static io_ctx_t * io_can_chan_get_ctx(const io_can_chan_t *chan)
Definition: can.h:445
int io_can_chan_write(io_can_chan_t *chan, const struct can_msg *msg, int timeout)
Writes a CAN frame to a CAN channel.
Definition: can.h:506
const struct io_can_chan_vtbl *const io_can_chan_t
An abstract CAN channel.
Definition: can.h:59
static size_t io_can_chan_abort_write(io_can_chan_t *chan, struct io_can_chan_write *write)
Aborts the specified CAN channel write operation if it is pending.
Definition: can.h:524
static size_t io_can_chan_abort(io_can_chan_t *chan, struct ev_task *task)
Definition: can.h:463
struct io_can_chan_read * io_can_chan_read_from_task(struct ev_task *task)
Obtains a pointer to a CAN channel read operation from a pointer to its completion task.
Definition: can.c:93
static size_t io_can_chan_abort_read(io_can_chan_t *chan, struct io_can_chan_read *read)
Aborts the specified CAN channel read operation if it is pending.
Definition: can.h:500
static size_t io_can_chan_cancel(io_can_chan_t *chan, struct ev_task *task)
Definition: can.h:457
struct io_can_chan_write * io_can_chan_write_from_task(struct ev_task *task)
Obtains a pointer to a CAN channel write operation from a pointer to its completion task.
Definition: can.c:99
const struct io_can_ctrl_vtbl *const io_can_ctrl_t
An abstract CAN controller.
Definition: can.h:56
static size_t io_can_chan_cancel_read(io_can_chan_t *chan, struct io_can_chan_read *read)
Cancels the specified CAN channel read operation if it is pending.
Definition: can.h:494
ev_future_t * io_can_chan_async_write(io_can_chan_t *chan, ev_exec_t *exec, const struct can_msg *msg, struct io_can_chan_write **pwrite)
Submits an asynchronous write operation to a CAN channel and creates a future which becomes ready onc...
Definition: can.c:70
io_dev_t * io_can_chan_get_dev(const io_can_chan_t *chan)
Returns a pointer to the abstract I/O device representing the CAN channel.
Definition: can.h:469
void io_can_chan_submit_write(io_can_chan_t *chan, struct io_can_chan_write *write)
Submits a write operation to a CAN channel.
Definition: can.h:512
int io_can_ctrl_get_state(const io_can_ctrl_t *ctrl)
Returns the state of the CAN controller: one of CAN_STATE_ACTIVE, CAN_STATE_PASSIVE,...
Definition: can.h:439
int io_can_ctrl_stop(io_can_ctrl_t *ctrl)
Stops a CAN controller.
Definition: can.h:409
void io_can_chan_submit_read(io_can_chan_t *chan, struct io_can_chan_read *read)
Submits a read operation to a CAN channel.
Definition: can.h:488
int io_can_chan_get_flags(const io_can_chan_t *chan)
Returns the flafs of the CAN bus: any combination of IO_CAN_BUS_FLAG_ERR, IO_CAN_BUS_FLAG_FDF and IO_...
Definition: can.h:475
int io_can_ctrl_set_bitrate(io_can_ctrl_t *ctrl, int nominal, int data)
Configures the bitrate(s) of a CAN controller.
Definition: can.h:433
io_can_bus_flag
The CAN bus flags.
Definition: can.h:37
@ IO_CAN_BUS_FLAG_BRS
Bit Rate Switch support is enabled.
Definition: can.h:44
@ IO_CAN_BUS_FLAG_FDF
FD Format (formerly Extended Data Length) support is enabled.
Definition: can.h:42
@ IO_CAN_BUS_FLAG_ERR
Reception of error frames is enabled.
Definition: can.h:39
static ev_exec_t * io_can_chan_get_exec(const io_can_chan_t *chan)
Definition: can.h:451
int io_can_chan_read(io_can_chan_t *chan, struct can_msg *msg, struct can_err *err, struct timespec *tp, int timeout)
Reads a CAN frame or CAN error frame from a CAN channel.
Definition: can.h:481
int io_can_ctrl_get_bitrate(const io_can_ctrl_t *ctrl, int *pnominal, int *pdata)
Obtains the bitrate(s) of a CAN controller.
Definition: can.h:427
int io_can_ctrl_restart(io_can_ctrl_t *ctrl)
(Re)starts a CAN contoller.
Definition: can.h:421
static size_t io_can_chan_cancel_write(io_can_chan_t *chan, struct io_can_chan_write *write)
Cancels the specified CAN channel write operation if it is pending.
Definition: can.h:518
ev_future_t * io_can_chan_async_read(io_can_chan_t *chan, ev_exec_t *exec, struct can_msg *msg, struct can_err *err, struct timespec *tp, struct io_can_chan_read **pread)
Submits an asynchronous read operation to a CAN channel and creates a future which becomes ready once...
Definition: can.c:46
This header file is part of the I/O library; it contains CAN bus error definitions.
This header file is part of the I/O library; it contains the CAN frame declarations.
This header file is part of the I/O library; it contains the abstract I/O device interface.
size_t io_dev_abort(io_dev_t *dev, struct ev_task *task)
Aborts the asynchronous operation submitted to *dev, if its task has not yet been submitted to its ex...
Definition: dev.h:98
const struct io_dev_vtbl *const io_dev_t
An abstract I/O device.
Definition: dev.h:35
io_ctx_t * io_dev_get_ctx(const io_dev_t *dev)
Returns a pointer to the I/O context with which the I/O device is registered.
Definition: dev.h:80
size_t io_dev_cancel(io_dev_t *dev, struct ev_task *task)
Cancels the asynchronous operation submitted to *dev, if its task has not yet been submitted to its e...
Definition: dev.h:92
ev_exec_t * io_dev_get_exec(const io_dev_t *dev)
Returns a pointer to the executor used by the I/O device to execute asynchronous tasks.
Definition: dev.h:86
This header file is part of the C11 and POSIX compatibility library; it includes <time....
A CAN error frame.
Definition: err.h:28
A CAN or CAN FD format frame.
Definition: msg.h:87
A future.
Definition: future.c:66
An executable task.
Definition: task.h:41
The result of a CAN channel read operation.
Definition: can.h:62
int result
The result of the read operation: 1 if a CAN frame is received, 0 if an error frame is received,...
Definition: can.h:68
int errc
The error number, obtained as if by get_errc(), if result is -1.
Definition: can.h:70
A CAN channel read operation.
Definition: can.h:74
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the read operation.
Definition: can.h:98
struct io_can_chan_read_result r
The result of the read operation.
Definition: can.h:100
struct timespec * tp
The address at which to store the system time at which the CAN frame or CAN error frame was received.
Definition: can.h:93
struct can_msg * msg
The address at which to store the CAN frame.
Definition: can.h:80
struct can_err * err
The address at which to store the CAN error frame.
Definition: can.h:86
A CAN channel write operation.
Definition: can.h:110
const struct can_msg * msg
A pointer to the CAN frame to be written.
Definition: can.h:116
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the write operation.
Definition: can.h:121
int errc
The error number, obtained as if by get_errc(), if an error occurred or the operation was canceled.
Definition: can.h:126
Definition: ctx.c:38
This header file is part of the event library; it contains the task declarations.