yarp-devices
EmcyConsumer.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __EMCY_CONSUMER_HPP__
4 #define __EMCY_CONSUMER_HPP__
5 
6 #include <cstdint>
7 
8 #include <functional>
9 #include <string>
10 
11 namespace roboticslab
12 {
13 
19 {
20 public:
22  virtual ~EmcyCodeRegistry() = default;
23 
25  virtual std::string codeToMessage(std::uint16_t code);
26 };
27 
32 class EmcyConsumer final
33 {
34 public:
35  using code_t = std::pair<std::uint16_t, std::string>;
36 
38  EmcyConsumer() : codeRegistry(new EmcyCodeRegistry)
39  { }
40 
43  { delete codeRegistry; }
44 
46  bool accept(const std::uint8_t * data);
47 
49  template<typename T>
51  { delete codeRegistry;
52  codeRegistry = new T; }
53 
55  template<typename Fn>
56  void registerHandler(Fn && fn)
57  { callback = fn; }
58 
61  { callback = HandlerFn(); }
62 
63 private:
64  using HandlerFn = std::function<void(code_t, std::uint8_t, const std::uint8_t *)>;
65 
66  HandlerFn callback;
67  EmcyCodeRegistry * codeRegistry;
68 };
69 
70 } // namespace roboticslab
71 
72 #endif // __EMCY_CONSUMER_HPP__
Generic EMCY message parser.
Definition: EmcyConsumer.hpp:19
virtual std::string codeToMessage(std::uint16_t code)
Obtain string representation of an EMCY code.
Definition: EmcyConsumer.cpp:9
virtual ~EmcyCodeRegistry()=default
Virtual destructor.
Representation of CAN EMCY protocol.
Definition: EmcyConsumer.hpp:33
EmcyConsumer()
Constructor.
Definition: EmcyConsumer.hpp:38
void registerHandler(Fn &&fn)
Register callback.
Definition: EmcyConsumer.hpp:56
void unregisterHandler()
Unregister callback.
Definition: EmcyConsumer.hpp:60
bool accept(const std::uint8_t *data)
Invoke callback on parsed CAN message data.
Definition: EmcyConsumer.cpp:88
~EmcyConsumer()
Destructor.
Definition: EmcyConsumer.hpp:42
std::pair< std::uint16_t, std::string > code_t
Emergency error code.
Definition: EmcyConsumer.hpp:35
void setErrorCodeRegistry()
Instantiate a non-default EMCY message parser.
Definition: EmcyConsumer.hpp:50
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6