KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
master.cpp
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 #include "master.h"
33 #include "core.h"
34 #include "logger.h"
35 
36 #include <memory>
37 
38 namespace kaco {
39 
41  m_device_alive_callback_functional = std::bind(&Master::device_alive_callback, this, std::placeholders::_1);
42  core.nmt.register_device_alive_callback(m_device_alive_callback_functional);
43 }
44 
46  if (m_running) {
47  stop();
48  }
49 }
50 
51 bool Master::start(const std::string busname, const std::string& baudrate) {
52  bool success = core.start(busname, baudrate);
53  if (!success) {
54  return false;
55  }
56  m_running = true;
57  //core.nmt.reset_all_nodes();
58  // TODO: let user do this explicitly?
60  return true;
61 }
62 
63 bool Master::start(const std::string busname, const unsigned baudrate) {
64  bool success = core.start(busname, baudrate);
65  if (!success) {
66  return false;
67  }
68  m_running = true;
69  //core.nmt.reset_all_nodes();
70  // TODO: let user do this explicitly?
72  return true;
73 }
74 
75 void Master::stop() {
76  m_running = false;
77  core.stop();
78 }
79 
80 size_t Master::num_devices() const {
81  return m_devices.size();
82 }
83 
84 Device& Master::get_device(size_t index) const {
85  assert(m_devices.size()>index);
86  return *(m_devices.at(index).get());
87 }
88 
89 void Master::device_alive_callback(const uint8_t node_id) {
90  if (!m_device_alive.test(node_id)) {
91  m_device_alive.set(node_id);
92  m_devices.emplace_back(new Device(core, node_id));
93  } else {
94  WARN("Device with node ID "<<node_id<<" already exists. Ignoring...");
95  }
96 
97 }
98 
99 
100 } // end namespace kaco
Device & get_device(size_t index) const
Returns a reference to a slave device object.
Definition: master.cpp:84
NMT nmt
The NMT sub-protocol.
Definition: core.h:109
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
~Master()
Destructor.
Definition: master.cpp:45
size_t num_devices() const
Returns the number of slave devices in the network.
Definition: master.cpp:80
void stop()
Stops master and core.
Definition: master.cpp:75
bool start(const std::string busname, const std::string &baudrate)
Starts master and creates Core.
Definition: master.cpp:51
Master()
Constructor. Creates Core instance and adds NMT listener for new devices.
Definition: master.cpp:40
void discover_nodes()
Discovers nodes in the network via node guard protocol.
Definition: nmt.cpp:67
Core core
Core instance.
Definition: master.h:96
void stop()
Stops the receive loop and closes the driver.
Definition: core.cpp:112
This class represents a CanOpen slave device in the network.
Definition: device.h:83
bool start(const std::string busname, const std::string &baudrate)
Opens CAN driver and starts CAN message receive loop.
Definition: core.cpp:84