Lely core libraries  2.2.5
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
41  IO_CAN_BUS_FLAG_FDF = 1u << 1,
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 
56 typedef const struct io_can_ctrl_vtbl *const io_can_ctrl_t;
57 
59 typedef 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
136 extern "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 
171 LELY_IO_CAN_INLINE int io_can_ctrl_stop(io_can_ctrl_t *ctrl);
172 
177 LELY_IO_CAN_INLINE int io_can_ctrl_stopped(const io_can_ctrl_t *ctrl);
178 
187 LELY_IO_CAN_INLINE int io_can_ctrl_restart(io_can_ctrl_t *ctrl);
188 
203 LELY_IO_CAN_INLINE int io_can_ctrl_get_bitrate(
204  const io_can_ctrl_t *ctrl, int *pnominal, int *pdata);
205 
220 LELY_IO_CAN_INLINE int io_can_ctrl_set_bitrate(
221  io_can_ctrl_t *ctrl, int nominal, int data);
222 
229 LELY_IO_CAN_INLINE int io_can_ctrl_get_state(const io_can_ctrl_t *ctrl);
230 
232 static inline io_ctx_t *io_can_chan_get_ctx(const io_can_chan_t *chan);
233 
235 static inline ev_exec_t *io_can_chan_get_exec(const io_can_chan_t *chan);
236 
238 static inline size_t io_can_chan_cancel(
239  io_can_chan_t *chan, struct ev_task *task);
240 
242 static inline size_t io_can_chan_abort(
243  io_can_chan_t *chan, struct ev_task *task);
244 
246 LELY_IO_CAN_INLINE io_dev_t *io_can_chan_get_dev(const io_can_chan_t *chan);
247 
253 LELY_IO_CAN_INLINE int io_can_chan_get_flags(const io_can_chan_t *chan);
254 
273 LELY_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 
282 LELY_IO_CAN_INLINE void io_can_chan_submit_read(
283  io_can_chan_t *chan, struct io_can_chan_read *read);
284 
294 static inline size_t io_can_chan_cancel_read(
295  io_can_chan_t *chan, struct io_can_chan_read *read);
296 
305 static 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 
344 LELY_IO_CAN_INLINE int io_can_chan_write(
345  io_can_chan_t *chan, const struct can_msg *msg, int timeout);
346 
351 LELY_IO_CAN_INLINE void io_can_chan_submit_write(
352  io_can_chan_t *chan, struct io_can_chan_write *write);
353 
363 static inline size_t io_can_chan_cancel_write(
364  io_can_chan_t *chan, struct io_can_chan_write *write);
365 
374 static 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 
408 inline int
410 {
411  return (*ctrl)->stop(ctrl);
412 }
413 
414 inline int
416 {
417  return (*ctrl)->stopped(ctrl);
418 }
419 
420 inline int
422 {
423  return (*ctrl)->restart(ctrl);
424 }
425 
426 inline int
427 io_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 
432 inline int
433 io_can_ctrl_set_bitrate(io_can_ctrl_t *ctrl, int nominal, int data)
434 {
435  return (*ctrl)->set_bitrate(ctrl, nominal, data);
436 }
437 
438 inline int
440 {
441  return (*ctrl)->get_state(ctrl);
442 }
443 
444 static inline io_ctx_t *
446 {
447  return io_dev_get_ctx(io_can_chan_get_dev(chan));
448 }
449 
450 static inline ev_exec_t *
452 {
453  return io_dev_get_exec(io_can_chan_get_dev(chan));
454 }
455 
456 static inline size_t
458 {
459  return io_dev_cancel(io_can_chan_get_dev(chan), task);
460 }
461 
462 static inline size_t
464 {
465  return io_dev_abort(io_can_chan_get_dev(chan), task);
466 }
467 
468 inline io_dev_t *
470 {
471  return (*chan)->get_dev(chan);
472 }
473 
474 inline int
476 {
477  return (*chan)->get_flags(chan);
478 }
479 
480 inline int
481 io_can_chan_read(io_can_chan_t *chan, struct can_msg *msg, struct can_err *err,
482  struct timespec *tp, int timeout)
483 {
484  return (*chan)->read(chan, msg, err, tp, timeout);
485 }
486 
487 inline void
489 {
490  (*chan)->submit_read(chan, read);
491 }
492 
493 static inline size_t
495 {
496  return io_can_chan_cancel(chan, &read->task);
497 }
498 
499 static inline size_t
501 {
502  return io_can_chan_abort(chan, &read->task);
503 }
504 
505 inline int
506 io_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 
511 inline void
513 {
514  (*chan)->submit_write(chan, write);
515 }
516 
517 static inline size_t
519 {
520  return io_can_chan_cancel(chan, &write->task);
521 }
522 
523 static 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_
io_can_chan_abort_read
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
io_can_chan_read_result
The result of a CAN channel read operation.
Definition: can.h:62
ev_future
A future.
Definition: future.c:63
io_can_ctrl_restart
int io_can_ctrl_restart(io_can_ctrl_t *ctrl)
(Re)starts a CAN contoller.
Definition: can.h:421
err.h
ev_exec_t
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
task.h
io_can_ctrl_vtbl
Definition: can.h:139
future.h
io_dev_get_exec
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
io_can_chan_read_from_task
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:91
IO_CAN_BUS_FLAG_ERR
@ IO_CAN_BUS_FLAG_ERR
Reception of error frames is enabled.
Definition: can.h:39
time.h
io_can_chan_get_flags
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
can_msg
A CAN or CAN FD format frame.
Definition: msg.h:87
io_can_chan_cancel
static size_t io_can_chan_cancel(io_can_chan_t *chan, struct ev_task *task)
Definition: can.h:457
io_can_chan_write::task
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the write operation.
Definition: can.h:121
msg.h
io_can_chan_vtbl
Definition: can.h:149
io_can_chan_write::errc
int errc
The error number, obtained as if by get_errc(), if an error occurred or the operation was canceled.
Definition: can.h:126
io_dev_get_ctx
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
io_can_chan_read
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
io_can_chan_async_write
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:69
io_dev_cancel
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
can_err
A CAN error frame.
Definition: err.h:28
io_can_chan_read
A CAN channel read operation.
Definition: can.h:74
io_can_chan_read_result::errc
int errc
The error number, obtained as if by get_errc(), if result is -1.
Definition: can.h:70
io_can_bus_flag
io_can_bus_flag
The CAN bus flags.
Definition: can.h:37
io_can_chan_submit_write
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
io_can_chan_read::msg
struct can_msg * msg
The address at which to store the CAN frame.
Definition: can.h:80
IO_CAN_BUS_FLAG_FDF
@ IO_CAN_BUS_FLAG_FDF
FD Format (formerly Extended Data Length) support is enabled.
Definition: can.h:42
io_can_chan_write
A CAN channel write operation.
Definition: can.h:110
io_can_chan_t
const struct io_can_chan_vtbl *const io_can_chan_t
An abstract CAN channel.
Definition: can.h:59
io_can_ctrl_get_bitrate
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
io_can_chan_read_result::result
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
io_can_chan_write::msg
const struct can_msg * msg
A pointer to the CAN frame to be written.
Definition: can.h:116
io_can_chan_cancel_write
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
io_can_chan_get_dev
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
io_can_chan_async_read
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
io_can_chan_read::tp
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
io_can_chan_get_exec
static ev_exec_t * io_can_chan_get_exec(const io_can_chan_t *chan)
Definition: can.h:451
io_can_chan_cancel_read
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
io_can_chan_abort
static size_t io_can_chan_abort(io_can_chan_t *chan, struct ev_task *task)
Definition: can.h:463
IO_CAN_BUS_FLAG_BRS
@ IO_CAN_BUS_FLAG_BRS
Bit Rate Switch support is enabled.
Definition: can.h:44
io_can_chan_read::err
struct can_err * err
The address at which to store the CAN error frame.
Definition: can.h:86
io_can_chan_write_from_task
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:97
ev_task
An executable task.
Definition: task.h:41
io_can_chan_write
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
io_dev_t
const struct io_dev_vtbl *const io_dev_t
An abstract I/O device.
Definition: dev.h:35
io_can_ctrl_stop
int io_can_ctrl_stop(io_can_ctrl_t *ctrl)
Stops a CAN controller.
Definition: can.h:409
dev.h
io_can_chan_get_ctx
static io_ctx_t * io_can_chan_get_ctx(const io_can_chan_t *chan)
Definition: can.h:445
io_ctx
Definition: ctx.c:35
io_can_ctrl_stopped
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
io_can_chan_read::task
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the read operation.
Definition: can.h:98
io_can_chan_abort_write
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
io_can_ctrl_set_bitrate
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_dev_abort
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
io_can_chan_read::r
struct io_can_chan_read_result r
The result of the read operation.
Definition: can.h:100
io_can_ctrl_get_state
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
io_can_chan_submit_read
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
io_can_ctrl_t
const struct io_can_ctrl_vtbl *const io_can_ctrl_t
An abstract CAN controller.
Definition: can.h:56