yarp-devices
NmtProtocol.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __NMT_PROTOCOL_HPP__
4 #define __NMT_PROTOCOL_HPP__
5 
6 #include <cstdint>
7 
8 #include <functional>
9 
10 #include "ICanSenderDelegate.hpp"
11 
12 namespace roboticslab
13 {
14 
19 enum class NmtService : std::uint8_t
20 {
21  START_REMOTE_NODE = 1,
22  STOP_REMOTE_NODE = 2,
23  ENTER_PRE_OPERATIONAL = 128,
24  RESET_NODE = 129,
25  RESET_COMMUNICATION = 130
26 };
27 
32 enum class NmtState : std::uint8_t
33 {
34  BOOTUP = 0,
35  STOPPED = 4,
36  OPERATIONAL = 5,
37  PRE_OPERATIONAL = 127
38 };
39 
44 class NmtProtocol final
45 {
46 public:
47  static constexpr std::uint8_t BROADCAST = 0;
48 
50  NmtProtocol(std::uint8_t id, ICanSenderDelegate * sender = nullptr)
51  : id(id), sender(sender)
52  { }
53 
56  { this->sender = sender; }
57 
59  bool issueServiceCommand(NmtService command);
60 
62  bool accept(const std::uint8_t * data);
63 
65  template<typename Fn>
66  void registerHandler(Fn && fn)
67  { callback = fn; }
68 
71  { callback = HandlerFn(); }
72 
73 private:
74  using HandlerFn = std::function<void(NmtState)>;
75 
76  std::uint8_t id;
77  ICanSenderDelegate * sender;
78 
79  HandlerFn callback;
80 };
81 
82 } // namespace roboticslab
83 
84 #endif // __NMT_PROTOCOL_HPP__
Implementation-agnostic consumer for TX CAN transfers.
Definition: ICanSenderDelegate.hpp:22
Representation of NMT protocol.
Definition: NmtProtocol.hpp:45
NmtProtocol(std::uint8_t id, ICanSenderDelegate *sender=nullptr)
Constructor, registers CAN sender handle.
Definition: NmtProtocol.hpp:50
bool accept(const std::uint8_t *data)
Invoke callback on parsed CAN message data.
Definition: NmtProtocol.cpp:13
void configureSender(ICanSenderDelegate *sender)
Configure CAN sender delegate handle.
Definition: NmtProtocol.hpp:55
static constexpr std::uint8_t BROADCAST
Broadcast CAN ID.
Definition: NmtProtocol.hpp:47
void registerHandler(Fn &&fn)
Register callback.
Definition: NmtProtocol.hpp:66
void unregisterHandler()
Unregister callback.
Definition: NmtProtocol.hpp:70
bool issueServiceCommand(NmtService command)
Send NMT service indication.
Definition: NmtProtocol.cpp:7
NmtState
NMT state machine.
Definition: NmtProtocol.hpp:33
NmtService
NMT service commands.
Definition: NmtProtocol.hpp:20
@ STOPPED
Stopped state.
@ BOOTUP
Initial bootup.
@ PRE_OPERATIONAL
Pre-operational state.
@ OPERATIONAL
Operational state.
@ ENTER_PRE_OPERATIONAL
Enter pre-operational indication.
@ START_REMOTE_NODE
Start remote node indication.
@ RESET_COMMUNICATION
Reset communication indication.
@ RESET_NODE
Reset node indication.
@ STOP_REMOTE_NODE
Stop remote node indication.
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6