KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
nmt.h
1 /*
2  * Copyright (c) 2015, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #pragma once
33 
34 #include "message.h"
35 
36 #include <vector>
37 #include <functional>
38 #include <future>
39 #include <forward_list>
40 #include <mutex>
41 
42 namespace kaco {
43 
44  // forward declaration
45  class Core;
46 
50  class NMT {
51 
52  public:
53 
57  using DeviceAliveCallback = std::function< void(const uint8_t node_id) >;
58 
62 
64  enum class Command : uint8_t {
65  start_node = 0x01,
66  stop_node = 0x02,
67  enter_preoperational = 0x80,
68  reset_node = 0x81,
69  reset_communication = 0x82
70  };
71 
74  NMT(Core& core);
75 
77  NMT(const NMT&) = delete;
78 
82  void process_incoming_message(const Message& message);
83 
88  void send_nmt_message(uint8_t node_id, Command cmd);
89 
94 
97  void reset_all_nodes();
98 
101  void discover_nodes();
102 
108 
112  void register_new_device_callback(const NewDeviceCallback& callback);
113 
114  private:
115 
116  static const bool debug = false;
117  Core& m_core;
118 
120  std::vector<NewDeviceCallback> m_device_alive_callbacks;
121  mutable std::mutex m_device_alive_callbacks_mutex;
122 
123  static const bool m_cleanup_futures = true;
124  std::forward_list<std::future<void>> m_callback_futures; // forward_list because of remove_if
125  mutable std::mutex m_callback_futures_mutex;
126 
127  };
128 
129 } // end namespace kaco
void send_nmt_message(uint8_t node_id, Command cmd)
Sends a NMT message to a given device.
Definition: nmt.cpp:47
void broadcast_nmt_message(Command cmd)
Sends a broadcast NMT message.
Definition: nmt.cpp:53
void register_device_alive_callback(const DeviceAliveCallback &callback)
Registers a callback which will be called when a slave sends it's state via NMT and the state indicat...
Definition: nmt.cpp:181
Command
NMT commands.
Definition: nmt.h:64
This class implements the CanOpen NMT protocol.
Definition: nmt.h:50
DeviceAliveCallback NewDeviceCallback
Type of a new device callback function.
Definition: nmt.h:61
This class implements the Core of KaCanOpen It communicates with the CAN driver, sends CAN messages a...
Definition: core.h:59
void discover_nodes()
Discovers nodes in the network via node guard protocol.
Definition: nmt.cpp:67
void register_new_device_callback(const NewDeviceCallback &callback)
Registers a callback which will be called when a new slave device is discovered.
Definition: nmt.cpp:186
void process_incoming_message(const Message &message)
Process incoming NMT message.
Definition: nmt.cpp:79
NMT(Core &core)
Constructor.
Definition: nmt.cpp:43
std::function< void(const uint8_t node_id) > DeviceAliveCallback
Type of a device alive callback function Important: Never call register_device_alive_callback() from ...
Definition: nmt.h:57
This struct represents a CANOpen message.
Definition: message.h:39
void reset_all_nodes()
Resets all nodes in the network.
Definition: nmt.cpp:57