Lely core libraries  2.2.5
can.c
Go to the documentation of this file.
1 
24 #include "io2.h"
25 #define LELY_IO_CAN_INLINE extern inline
26 #include <lely/io2/can.h>
27 #include <lely/util/util.h>
28 
29 #include <assert.h>
30 
32  ev_promise_t *promise;
33  struct io_can_chan_read read;
34 };
35 
36 static void io_can_chan_async_read_func(struct ev_task *task);
37 
39  ev_promise_t *promise;
40  struct io_can_chan_write write;
41 };
42 
43 static void io_can_chan_async_write_func(struct ev_task *task);
44 
47  struct can_msg *msg, struct can_err *err, struct timespec *tp,
48  struct io_can_chan_read **pread)
49 {
51  sizeof(struct io_can_chan_async_read), NULL);
52  if (!promise)
53  return NULL;
54 
55  struct io_can_chan_async_read *async_read = ev_promise_data(promise);
56  async_read->promise = promise;
57  async_read->read = (struct io_can_chan_read)IO_CAN_CHAN_READ_INIT(
58  msg, err, tp, exec, &io_can_chan_async_read_func);
59 
60  io_can_chan_submit_read(chan, &async_read->read);
61 
62  if (pread)
63  *pread = &async_read->read;
64 
65  return ev_promise_get_future(promise);
66 }
67 
70  const struct can_msg *msg, struct io_can_chan_write **pwrite)
71 {
73  sizeof(struct io_can_chan_async_write), NULL);
74  if (!promise)
75  return NULL;
76 
77  struct io_can_chan_async_write *async_write = ev_promise_data(promise);
78  async_write->promise = promise;
79  async_write->write = (struct io_can_chan_write)IO_CAN_CHAN_WRITE_INIT(
80  msg, exec, &io_can_chan_async_write_func);
81 
82  io_can_chan_submit_write(chan, &async_write->write);
83 
84  if (pwrite)
85  *pwrite = &async_write->write;
86 
87  return ev_promise_get_future(promise);
88 }
89 
90 struct io_can_chan_read *
92 {
93  return task ? structof(task, struct io_can_chan_read, task) : NULL;
94 }
95 
96 struct io_can_chan_write *
98 {
99  return task ? structof(task, struct io_can_chan_write, task) : NULL;
100 }
101 
102 static void
103 io_can_chan_async_read_func(struct ev_task *task)
104 {
105  assert(task);
106  struct io_can_chan_read *read = io_can_chan_read_from_task(task);
107  struct io_can_chan_async_read *async_read =
108  structof(read, struct io_can_chan_async_read, read);
109 
110  ev_promise_set(async_read->promise, &read->r);
111  ev_promise_release(async_read->promise);
112 }
113 
114 static void
115 io_can_chan_async_write_func(struct ev_task *task)
116 {
117  assert(task);
118  struct io_can_chan_write *write = io_can_chan_write_from_task(task);
119  struct io_can_chan_async_write *async_write =
120  structof(write, struct io_can_chan_async_write, write);
121 
122  ev_promise_set(async_write->promise, &write->errc);
123  ev_promise_release(async_write->promise);
124 }
A CAN or CAN FD format frame.
Definition: msg.h:87
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
void * ev_promise_data(const ev_promise_t *promise)
Returns a pointer to the shared state of a promise.
Definition: future.c:233
void ev_promise_release(ev_promise_t *promise)
Releases a reference to a promise.
Definition: future.c:195
A CAN channel read operation.
Definition: can.h:74
int errc
The error number, obtained as if by get_errc(), if an error occurred or the operation was canceled...
Definition: can.h:126
ev_promise_t * ev_promise_create(size_t size, ev_promise_dtor_t *dtor)
Constructs a new promise with an optional empty shared state.
Definition: future.c:151
A future.
Definition: future.c:63
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
A CAN error frame.
Definition: err.h:28
A CAN channel write operation.
Definition: can.h:110
const struct io_can_chan_vtbl *const io_can_chan_t
An abstract CAN channel.
Definition: can.h:59
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
struct ev_task task
The task (to be) submitted upon completion (or cancellation) of the read operation.
Definition: can.h:98
ev_future_t * ev_promise_get_future(ev_promise_t *promise)
Returns (a reference to) a future associated with the specified promise.
Definition: future.c:309
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
const struct can_msg * msg
A pointer to the CAN frame to be written.
Definition: can.h:116
This header file is part of the I/O library; it contains the abstract CAN bus interface.
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
struct io_can_chan_read_result r
The result of the read operation.
Definition: can.h:100
An executable task.
Definition: task.h:41
#define structof(ptr, type, member)
Obtains the address of a structure from the address of one of its members.
Definition: util.h:93
int ev_promise_set(ev_promise_t *promise, void *value)
Satiesfies a promise, if it was not aready satisfied, and stores the specified value for retrieval by...
Definition: future.c:239
struct can_msg * msg
The address at which to store the CAN frame.
Definition: can.h:80
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
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
A promise.
Definition: future.c:92
This is the internal header file of the I/O library.
This is the public header file of the utilities library.