Lely core libraries  1.9.2
io_context.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_COAPP_IO_CONTEXT_HPP_
23 #define LELY_COAPP_IO_CONTEXT_HPP_
24 
25 #include <lely/aio/can_bus.hpp>
26 #include <lely/aio/timer.hpp>
27 #include <lely/coapp/coapp.hpp>
28 
29 #include <memory>
30 
31 namespace lely {
32 
33 // The CAN network interface from <lely/can/net.hpp>.
34 class CANNet;
35 
36 namespace canopen {
37 
42 class IoContext {
43  public:
44  using CanState = aio::CanBus::State;
45  using CanError = aio::CanBus::Error;
46 
58  IoContext(aio::TimerBase& timer, aio::CanBusBase& bus,
59  BasicLockable* mutex = nullptr);
60 
61  IoContext(const IoContext&) = delete;
62  IoContext& operator=(const IoContext&) = delete;
63 
65  aio::ExecutorBase GetExecutor() const noexcept;
66 
68  template <class F>
69  void
70  Post(F&& f) {
71  auto* op = new aio::TaskWrapper([=](::std::error_code ec) {
72  if (!ec) f();
73  });
74  GetExecutor().Post(*op);
75  }
76 
77  protected:
78  ~IoContext();
79 
84  CANNet* net() const noexcept;
85 
90  void SetTime();
91 
92 #ifndef DOXYGEN_SHOULD_SKIP_THIS
93  private:
94 #endif
95 
103  virtual void
104  OnCanState(CanState new_state, CanState old_state) noexcept {
105  (void)new_state;
106  (void)old_state;
107  }
108 
116  virtual void
117  OnCanError(CanError error) noexcept {
118  (void)error;
119  }
120 
121  private:
122  struct Impl_;
123  ::std::unique_ptr<Impl_> impl_;
124 };
125 
126 } // namespace canopen
127 
128 } // namespace lely
129 
130 #endif // LELY_COAPP_IO_CONTEXT_HPP_
virtual void OnCanState(CanState new_state, CanState old_state) noexcept
The function invoked when a CAN bus state change is detected.
Definition: io_context.hpp:104
This is the public header file of the C++ CANopen application library.
virtual void OnCanError(CanError error) noexcept
The function invoked when an error is detected on the CAN bus.
Definition: io_context.hpp:117
CANNet * net() const noexcept
Returns a pointer to the internal CAN network interface from <lely/can/net.hpp>.
Definition: io_context.cpp:86
void SetTime()
Update the CAN network time.
Definition: io_context.cpp:91
The type of objects thrown as exceptions to report a system error with an associated error code...
Definition: exception.hpp:54
An abstract interface conforming to the BasicLockable concept.
Definition: coapp.hpp:40
void Post(F &&f)
Schedules the specified Callable object for execution.
Definition: io_context.hpp:70
Global namespace for the Lely Industries N.V. libraries.
Definition: buf.hpp:32
An opaque CAN network interface type.
Definition: net.hpp:83
aio::ExecutorBase GetExecutor() const noexcept
Returns the executor used to process I/O events on the CAN bus.
Definition: io_context.cpp:81
IoContext(aio::TimerBase &timer, aio::CanBusBase &bus, BasicLockable *mutex=nullptr)
Creates a new I/O context.
Definition: io_context.cpp:74
The I/O context.
Definition: io_context.hpp:42