Lely core libraries 1.9.2
net.hpp
Go to the documentation of this file.
1
22#ifndef LELY_CAN_NET_HPP_
23#define LELY_CAN_NET_HPP_
24
25#ifndef __cplusplus
26#error "include <lely/can/net.h> for the C interface"
27#endif
28
29#include <lely/util/c_call.hpp>
30#include <lely/util/c_type.hpp>
31
32namespace lely {
33class CANNet;
34}
37
38namespace lely {
39class CANTimer;
40}
43
44namespace lely {
45class CANRecv;
46}
49
50#include <lely/can/net.h>
51
52namespace lely {
53
55template <>
57 typedef __can_net value_type;
58 typedef value_type& reference;
59 typedef const value_type& const_reference;
60 typedef value_type* pointer;
61 typedef const value_type* const_pointer;
62
63 static void*
64 alloc() noexcept {
65 return __can_net_alloc();
66 }
67 static void
68 free(void* ptr) noexcept {
69 __can_net_free(ptr);
70 }
71
72 static pointer
73 init(pointer p) noexcept {
74 return __can_net_init(p);
75 }
76 static void
77 fini(pointer p) noexcept {
78 __can_net_fini(p);
79 }
80};
81
83class CANNet : public incomplete_c_type<__can_net> {
85
86 public:
87 CANNet() : c_base() {}
88
89 void
90 getTime(timespec* tp) const noexcept {
91 can_net_get_time(this, tp);
92 }
93
94 int
95 setTime(const timespec& tp) noexcept {
96 return can_net_set_time(this, &tp);
97 }
98
99 void
100 getNextFunc(can_timer_func_t** pfunc, void** pdata) const noexcept {
101 can_net_get_next_func(this, pfunc, pdata);
102 }
103
104 void
105 setNextFunc(can_timer_func_t* func, void* data) noexcept {
106 can_net_set_next_func(this, func, data);
107 }
108
109 template <class F>
110 void
111 setNextFunc(F* f) noexcept {
113 static_cast<void*>(f));
114 }
115
116 template <class C, typename c_mem_fn<can_timer_func_t*, C>::type M>
117 void
118 setNextFunc(C* obj) noexcept {
120 static_cast<void*>(obj));
121 }
122
123 int
124 recv(const can_msg& msg) noexcept {
125 return can_net_recv(this, &msg);
126 }
127
128 int
129 send(const can_msg& msg) noexcept {
130 return can_net_send(this, &msg);
131 }
132
133 void
134 getSendFunc(can_send_func_t** pfunc, void** pdata) const noexcept {
135 can_net_get_send_func(this, pfunc, pdata);
136 }
137
138 void
139 setSendFunc(can_send_func_t* func, void* data) noexcept {
140 can_net_set_send_func(this, func, data);
141 }
142
143 template <class F>
144 void
145 setSendFunc(F* f) noexcept {
147 static_cast<void*>(f));
148 }
149
150 template <class C, typename c_mem_fn<can_send_func_t*, C>::type M>
151 void
152 setSendFunc(C* obj) noexcept {
154 static_cast<void*>(obj));
155 }
156
157 protected:
158 ~CANNet() {}
159};
160
162template <>
164 typedef __can_timer value_type;
165 typedef value_type& reference;
166 typedef const value_type& const_reference;
167 typedef value_type* pointer;
168 typedef const value_type* const_pointer;
169
170 static void*
171 alloc() noexcept {
172 return __can_timer_alloc();
173 }
174 static void
175 free(void* ptr) noexcept {
176 __can_timer_free(ptr);
177 }
178
179 static pointer
180 init(pointer p) noexcept {
181 return __can_timer_init(p);
182 }
183 static void
184 fini(pointer p) noexcept {
185 __can_timer_fini(p);
186 }
187};
188
190class CANTimer : public incomplete_c_type<__can_timer> {
192
193 public:
194 CANTimer() : c_base() {}
195
196 void
197 getFunc(can_timer_func_t** pfunc, void** pdata) const noexcept {
198 can_timer_get_func(this, pfunc, pdata);
199 }
200
201 void
202 setFunc(can_timer_func_t* func, void* data) noexcept {
203 can_timer_set_func(this, func, data);
204 }
205
206 template <class F>
207 void
208 setFunc(F* f) noexcept {
209 setFunc(&c_obj_call<can_timer_func_t*, F>::function, static_cast<void*>(f));
210 }
211
212 template <class C, typename c_mem_fn<can_timer_func_t*, C>::type M>
213 void
214 setFunc(C* obj) noexcept {
216 static_cast<void*>(obj));
217 }
218
219 void
220 start(CANNet& net, const timespec* start = 0,
221 const timespec* interval = 0) noexcept {
222 can_timer_start(this, &net, start, interval);
223 }
224
225 void
226 stop() noexcept {
227 can_timer_stop(this);
228 }
229
230 void
231 timeout(CANNet& net, int timeout) noexcept {
232 can_timer_timeout(this, &net, timeout);
233 }
234
235 protected:
236 ~CANTimer() {}
237};
238
240template <>
242 typedef __can_recv value_type;
243 typedef value_type& reference;
244 typedef const value_type& const_reference;
245 typedef value_type* pointer;
246 typedef const value_type* const_pointer;
247
248 static void*
249 alloc() noexcept {
250 return __can_recv_alloc();
251 }
252 static void
253 free(void* ptr) noexcept {
254 __can_recv_free(ptr);
255 }
256
257 static pointer
258 init(pointer p) noexcept {
259 return __can_recv_init(p);
260 }
261 static void
262 fini(pointer p) noexcept {
263 __can_recv_fini(p);
264 }
265};
266
268class CANRecv : public incomplete_c_type<__can_recv> {
270
271 public:
272 CANRecv() : c_base() {}
273
274 void
275 getFunc(can_recv_func_t** pfunc, void** pdata) const noexcept {
276 can_recv_get_func(this, pfunc, pdata);
277 }
278
279 void
280 setFunc(can_recv_func_t* func, void* data) noexcept {
281 can_recv_set_func(this, func, data);
282 }
283
284 template <class F>
285 void
286 setFunc(F* f) noexcept {
287 setFunc(&c_obj_call<can_recv_func_t*, F>::function, static_cast<void*>(f));
288 }
289
290 template <class C, typename c_mem_fn<can_recv_func_t*, C>::type M>
291 void
292 setFunc(C* obj) noexcept {
294 static_cast<void*>(obj));
295 }
296
297 void
298 start(CANNet& net, uint_least32_t id, uint_least8_t flags = 0) noexcept {
299 can_recv_start(this, &net, id, flags);
300 }
301
302 void
303 stop() noexcept {
304 can_recv_stop(this);
305 }
306
307 protected:
308 ~CANRecv() {}
309};
310
311} // namespace lely
312
313#endif // !LELY_CAN_NET_HPP_
This header file is part of the utilities library; it contains the C callback wrapper declarations.
This header file is part of the utilities library; it contains the C to C++ interface declarations.
An opaque CAN network interface type.
Definition: net.hpp:83
An opaque CAN frame receiver type.
Definition: net.hpp:268
An opaque CAN timer type.
Definition: net.hpp:190
The base class for a C++ interface to an incomplete C type.
Definition: c_type.hpp:245
Global namespace for the Lely Industries N.V. libraries.
Definition: buf.hpp:32
This header file is part of the CAN library; it contains the CAN network interface declarations.
void can_timer_stop(can_timer_t *timer)
Stops a CAN timer and unregisters it with a network interface.
Definition: net.c:468
int can_net_send(can_net_t *net, const struct can_msg *msg)
Sends a CAN frame from a network interface.
Definition: net.c:308
void can_timer_start(can_timer_t *timer, can_net_t *net, const struct timespec *start, const struct timespec *interval)
Starts a CAN timer and registers it with a network interface.
Definition: net.c:437
void can_net_get_time(const can_net_t *net, struct timespec *tp)
Retrieves the current time of a CAN network interface.
Definition: net.c:204
void can_timer_set_func(can_timer_t *timer, can_timer_func_t *func, void *data)
Sets the callback function invoked when a CAN timer is triggered.
Definition: net.c:428
void can_timer_get_func(const can_timer_t *timer, can_timer_func_t **pfunc, void **pdata)
Retrieves the callback function invoked when a CAN timer is triggered.
Definition: net.c:416
void can_net_set_send_func(can_net_t *net, can_send_func_t *func, void *data)
Sets the callback function used to send CAN frames from a network interface.
Definition: net.c:334
void can_recv_stop(can_recv_t *recv)
Stops a CAN frame receiver from processing frames and unregisters it with the network interface.
Definition: net.c:613
void can_net_get_send_func(const can_net_t *net, can_send_func_t **pfunc, void **pdata)
Retrieves the callback function used to send CAN frames from a network interface.
Definition: net.c:322
int can_send_func_t(const struct can_msg *msg, void *data)
The type of a CAN send callback function, invoked by a CAN network interface when a frame needs to be...
Definition: net.h:85
void can_recv_set_func(can_recv_t *recv, can_recv_func_t *func, void *data)
Sets the callback function used to process CAN frames with a receiver.
Definition: net.c:582
void can_timer_timeout(can_timer_t *timer, can_net_t *net, int timeout)
Starts a CAN timer and registers it with a network interface.
Definition: net.c:484
void can_net_get_next_func(const can_net_t *net, can_timer_func_t **pfunc, void **pdata)
Retrieves the callback function invoked when the time at which the next CAN timer triggers is updated...
Definition: net.c:257
int can_recv_func_t(const struct can_msg *msg, void *data)
The type of a CAN receive callback function, invoked by a CAN frame receiver when a frame is received...
Definition: net.h:73
void can_net_set_next_func(can_net_t *net, can_timer_func_t *func, void *data)
Sets the callback function invoked when the time at which the next CAN timer triggers is updated.
Definition: net.c:269
void can_recv_start(can_recv_t *recv, can_net_t *net, uint_least32_t id, uint_least8_t flags)
Registers a CAN frame receiver with a network interface and starts processing frames.
Definition: net.c:591
void can_recv_get_func(const can_recv_t *recv, can_recv_func_t **pfunc, void **pdata)
Retrieves the callback function used to process CAN frames with a receiver.
Definition: net.c:571
int can_net_recv(can_net_t *net, const struct can_msg *msg)
Receives a CAN frame with a network interface and processes it with the corresponding receiver(s).
Definition: net.c:278
int can_net_set_time(can_net_t *net, const struct timespec *tp)
Sets the current time of a CAN network interface.
Definition: net.c:213
int can_timer_func_t(const struct timespec *tp, void *data)
The type of a CAN timer callback function, invoked by a CAN timer when the time is updated,...
Definition: net.h:61
lely::CANRecv can_recv_t
An opaque CAN frame receiver type.
Definition: net.hpp:48
lely::CANNet can_net_t
An opaque CAN network interface type.
Definition: net.hpp:36
lely::CANTimer can_timer_t
An opaque CAN timer type.
Definition: net.hpp:42
A CAN network interface.
Definition: net.c:37
A CAN frame receiver.
Definition: net.c:99
A CAN timer.
Definition: net.c:63
A CAN or CAN FD format frame.
Definition: msg.h:88
A class template supplying a uniform interface to certain attributes of C types.
Definition: c_type.hpp:344
A time type with nanosecond resolution.
Definition: time.h:83