Lely core libraries  2.3.4
net.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_CAN_NET_HPP_
23 #define LELY_CAN_NET_HPP_
24 
25 #if !defined(__cplusplus) || LELY_NO_CXX
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 
32 namespace lely {
33 class CANNet;
34 }
37 
38 namespace lely {
39 class CANTimer;
40 }
43 
44 namespace lely {
45 class CANRecv;
46 }
49 
50 #include <lely/can/net.h>
51 
52 namespace lely {
53 
55 template <>
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 
68  static void
69  free(void* ptr) noexcept {
70  __can_net_free(ptr);
71  }
72 
73  static pointer
74  init(pointer p) noexcept {
75  return __can_net_init(p);
76  }
77 
78  static void
79  fini(pointer p) noexcept {
80  __can_net_fini(p);
81  }
82 };
83 
85 class CANNet : public incomplete_c_type<__can_net> {
87 
88  public:
89  CANNet() : c_base() {}
90 
91  void
92  getTime(timespec* tp) const noexcept {
93  can_net_get_time(this, tp);
94  }
95 
96  int
97  setTime(const timespec& tp) noexcept {
98  return can_net_set_time(this, &tp);
99  }
100 
101  void
102  getNextFunc(can_timer_func_t** pfunc, void** pdata) const noexcept {
103  can_net_get_next_func(this, pfunc, pdata);
104  }
105 
106  void
107  setNextFunc(can_timer_func_t* func, void* data) noexcept {
108  can_net_set_next_func(this, func, data);
109  }
110 
111  template <class F>
112  void
113  setNextFunc(F* f) noexcept {
115  static_cast<void*>(f));
116  }
117 
118  template <class C, typename c_mem_fn<can_timer_func_t*, C>::type M>
119  void
120  setNextFunc(C* obj) noexcept {
122  static_cast<void*>(obj));
123  }
124 
125  int
126  recv(const can_msg& msg) noexcept {
127  return can_net_recv(this, &msg);
128  }
129 
130  int
131  send(const can_msg& msg) noexcept {
132  return can_net_send(this, &msg);
133  }
134 
135  void
136  getSendFunc(can_send_func_t** pfunc, void** pdata) const noexcept {
137  can_net_get_send_func(this, pfunc, pdata);
138  }
139 
140  void
141  setSendFunc(can_send_func_t* func, void* data) noexcept {
142  can_net_set_send_func(this, func, data);
143  }
144 
145  template <class F>
146  void
147  setSendFunc(F* f) noexcept {
149  static_cast<void*>(f));
150  }
151 
152  template <class C, typename c_mem_fn<can_send_func_t*, C>::type M>
153  void
154  setSendFunc(C* obj) noexcept {
156  static_cast<void*>(obj));
157  }
158 
159  protected:
160  ~CANNet() = default;
161 };
162 
164 template <>
166  typedef __can_timer value_type;
167  typedef value_type& reference;
168  typedef const value_type& const_reference;
169  typedef value_type* pointer;
170  typedef const value_type* const_pointer;
171 
172  static void*
173  alloc() noexcept {
174  return __can_timer_alloc();
175  }
176  static void
177  free(void* ptr) noexcept {
178  __can_timer_free(ptr);
179  }
180 
181  static pointer
182  init(pointer p) noexcept {
183  return __can_timer_init(p);
184  }
185  static void
186  fini(pointer p) noexcept {
187  __can_timer_fini(p);
188  }
189 };
190 
192 class CANTimer : public incomplete_c_type<__can_timer> {
194 
195  public:
196  CANTimer() : c_base() {}
197 
198  void
199  getFunc(can_timer_func_t** pfunc, void** pdata) const noexcept {
200  can_timer_get_func(this, pfunc, pdata);
201  }
202 
203  void
204  setFunc(can_timer_func_t* func, void* data) noexcept {
205  can_timer_set_func(this, func, data);
206  }
207 
208  template <class F>
209  void
210  setFunc(F* f) noexcept {
211  setFunc(&c_obj_call<can_timer_func_t*, F>::function, static_cast<void*>(f));
212  }
213 
214  template <class C, typename c_mem_fn<can_timer_func_t*, C>::type M>
215  void
216  setFunc(C* obj) noexcept {
218  static_cast<void*>(obj));
219  }
220 
221  void
222  start(CANNet& net, const timespec* start = 0,
223  const timespec* interval = 0) noexcept {
224  can_timer_start(this, &net, start, interval);
225  }
226 
227  void
228  stop() noexcept {
229  can_timer_stop(this);
230  }
231 
232  void
233  timeout(CANNet& net, int timeout) noexcept {
234  can_timer_timeout(this, &net, timeout);
235  }
236 
237  protected:
238  ~CANTimer() = default;
239 };
240 
242 template <>
244  typedef __can_recv value_type;
245  typedef value_type& reference;
246  typedef const value_type& const_reference;
247  typedef value_type* pointer;
248  typedef const value_type* const_pointer;
249 
250  static void*
251  alloc() noexcept {
252  return __can_recv_alloc();
253  }
254  static void
255  free(void* ptr) noexcept {
256  __can_recv_free(ptr);
257  }
258 
259  static pointer
260  init(pointer p) noexcept {
261  return __can_recv_init(p);
262  }
263  static void
264  fini(pointer p) noexcept {
265  __can_recv_fini(p);
266  }
267 };
268 
270 class CANRecv : public incomplete_c_type<__can_recv> {
272 
273  public:
274  CANRecv() : c_base() {}
275 
276  void
277  getFunc(can_recv_func_t** pfunc, void** pdata) const noexcept {
278  can_recv_get_func(this, pfunc, pdata);
279  }
280 
281  void
282  setFunc(can_recv_func_t* func, void* data) noexcept {
283  can_recv_set_func(this, func, data);
284  }
285 
286  template <class F>
287  void
288  setFunc(F* f) noexcept {
289  setFunc(&c_obj_call<can_recv_func_t*, F>::function, static_cast<void*>(f));
290  }
291 
292  template <class C, typename c_mem_fn<can_recv_func_t*, C>::type M>
293  void
294  setFunc(C* obj) noexcept {
296  static_cast<void*>(obj));
297  }
298 
299  void
300  start(CANNet& net, uint_least32_t id, uint_least8_t flags = 0) noexcept {
301  can_recv_start(this, &net, id, flags);
302  }
303 
304  void
305  stop() noexcept {
306  can_recv_stop(this);
307  }
308 
309  protected:
310  ~CANRecv() = default;
311 };
312 
313 } // namespace lely
314 
315 #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:85
An opaque CAN frame receiver type.
Definition: net.hpp:270
An opaque CAN timer type.
Definition: net.hpp:192
The base class for a C++ interface to an incomplete C type.
Definition: c_type.hpp:249
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:462
int can_net_send(can_net_t *net, const struct can_msg *msg)
Sends a CAN frame from a network interface.
Definition: net.c:300
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:431
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:196
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:422
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:410
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:326
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:609
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:314
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:578
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:478
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:249
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:261
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:587
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:567
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:270
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:205
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:86
A CAN timer.
Definition: net.c:63
A CAN or CAN FD format frame.
Definition: msg.h:87
A class template supplying a uniform interface to certain attributes of C types.
Definition: c_type.hpp:350