Lely core libraries  2.3.4
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  ev_future_t *future = ev_promise_get_future(promise);
55 
56  struct io_can_chan_async_read *async_read = ev_promise_data(promise);
57  async_read->promise = promise;
58  async_read->read = (struct io_can_chan_read)IO_CAN_CHAN_READ_INIT(
59  msg, err, tp, exec, &io_can_chan_async_read_func);
60 
61  io_can_chan_submit_read(chan, &async_read->read);
62 
63  if (pread)
64  *pread = &async_read->read;
65 
66  return future;
67 }
68 
71  const struct can_msg *msg, struct io_can_chan_write **pwrite)
72 {
74  sizeof(struct io_can_chan_async_write), NULL);
75  if (!promise)
76  return NULL;
77  ev_future_t *future = ev_promise_get_future(promise);
78 
79  struct io_can_chan_async_write *async_write = ev_promise_data(promise);
80  async_write->promise = promise;
81  async_write->write = (struct io_can_chan_write)IO_CAN_CHAN_WRITE_INIT(
82  msg, exec, &io_can_chan_async_write_func);
83 
84  io_can_chan_submit_write(chan, &async_write->write);
85 
86  if (pwrite)
87  *pwrite = &async_write->write;
88 
89  return future;
90 }
91 
92 struct io_can_chan_read *
94 {
95  return task ? structof(task, struct io_can_chan_read, task) : NULL;
96 }
97 
98 struct io_can_chan_write *
100 {
101  return task ? structof(task, struct io_can_chan_write, task) : NULL;
102 }
103 
104 static void
105 io_can_chan_async_read_func(struct ev_task *task)
106 {
107  assert(task);
109  struct io_can_chan_async_read *async_read =
110  structof(read, struct io_can_chan_async_read, read);
111 
112  ev_promise_set(async_read->promise, &read->r);
113  ev_promise_release(async_read->promise);
114 }
115 
116 static void
117 io_can_chan_async_write_func(struct ev_task *task)
118 {
119  assert(task);
121  struct io_can_chan_async_write *async_write =
122  structof(write, struct io_can_chan_async_write, write);
123 
124  ev_promise_set(async_write->promise, &write->errc);
125  ev_promise_release(async_write->promise);
126 }
ev_future
A future.
Definition: future.c:66
io2.h
io_can_chan_async_write
Definition: can.c:38
ev_exec_t
const struct ev_exec_vtbl *const ev_exec_t
An abstract task executor.
Definition: ev.h:29
can_msg
A CAN or CAN FD format frame.
Definition: msg.h:87
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
ev_promise_data
void * ev_promise_data(const ev_promise_t *promise)
Returns a pointer to the shared state of a promise.
Definition: future.c:236
util.h
ev_promise_create
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:154
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
ev_promise
A promise.
Definition: future.c:95
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_INIT
#define IO_CAN_CHAN_READ_INIT(msg, err, tp, exec, func)
The static initializer for io_can_chan_read.
Definition: can.h:104
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_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_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:70
io_can_chan_read::msg
struct can_msg * msg
The address at which to store the CAN frame.
Definition: can.h:80
ev_promise_set
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:242
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:93
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_chan_write::msg
const struct can_msg * msg
A pointer to the CAN frame to be written.
Definition: can.h:116
IO_CAN_CHAN_WRITE_INIT
#define IO_CAN_CHAN_WRITE_INIT(msg, exec, func)
The static initializer for io_can_chan_write.
Definition: can.h:130
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
can.h
io_can_chan_read::err
struct can_err * err
The address at which to store the CAN error frame.
Definition: can.h:86
structof
#define structof(ptr, type, member)
Obtains the address of a structure from the address of one of its members.
Definition: util.h:93
ev_task
An executable task.
Definition: task.h:41
ev_promise_release
void ev_promise_release(ev_promise_t *promise)
Releases a reference to a promise.
Definition: future.c:198
io_can_chan_async_read
Definition: can.c:31
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:99
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
ev_promise_get_future
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:312
io_can_chan_read::r
struct io_can_chan_read_result r
The result of the read operation.
Definition: can.h:100
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