KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
core.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 <functional>
35 #include <string>
36 #include <vector>
37 #include <thread>
38 #include <atomic>
39 #include <forward_list>
40 #include <future>
41 #include <mutex>
42 
43 #include "nmt.h"
44 #include "sdo.h"
45 #include "pdo.h"
46 #include "message.h"
47 
48 namespace kaco {
49 
59  class Core {
60 
61  public:
62 
66  using MessageReceivedCallback = std::function< void(const Message&) >;
67 
69  Core();
70 
72  ~Core();
73 
75  Core(const Core&) = delete;
76 
84  bool start(const std::string busname, const std::string& baudrate);
85 
94  bool start(const std::string busname, const unsigned baudrate);
95 
98  void stop();
99 
102  void send(const Message& message);
103 
107 
110 
113 
116 
117 
118  private:
119 
120  void receive_loop(std::atomic<bool>& running);
121  void received_message(const Message& m);
122 
123  static const bool debug = false;
124 
125  std::atomic<bool> m_running{false};
126  std::thread m_loop_thread;
127  void* m_handle;
128 
129  std::vector<MessageReceivedCallback> m_receive_callbacks;
130  mutable std::mutex m_receive_callbacks_mutex;
131 
132  static const bool m_cleanup_futures = true;
133  std::forward_list<std::future<void>> m_callback_futures;
134  mutable std::mutex m_callback_futures_mutex;
135 
136  static const bool m_lock_send = true;
137  mutable std::mutex m_send_mutex;
138 
139  };
140 
141 } // end namespace kaco
Core()
Constructor.
Definition: core.cpp:72
This class implements the CanOpen SDO protocol.
Definition: sdo.h:58
This class implements the CanOpen PDO protocol.
Definition: pdo.h:51
void send(const Message &message)
Sends a message.
Definition: core.cpp:243
NMT nmt
The NMT sub-protocol.
Definition: core.h:109
PDO pdo
The PDO sub-protocol.
Definition: core.h:115
This class implements the CanOpen NMT protocol.
Definition: nmt.h:50
This class implements the Core of KaCanOpen It communicates with the CAN driver, sends CAN messages a...
Definition: core.h:59
std::function< void(const Message &) > MessageReceivedCallback
Type of a message receiver function Important: Never call register_receive_callback() from within (->...
Definition: core.h:66
void stop()
Stops the receive loop and closes the driver.
Definition: core.cpp:112
SDO sdo
The SDO sub-protocol.
Definition: core.h:112
bool start(const std::string busname, const std::string &baudrate)
Opens CAN driver and starts CAN message receive loop.
Definition: core.cpp:84
void register_receive_callback(const MessageReceivedCallback &callback)
Registers a callback function which is called when a message has been received.
Definition: core.cpp:137
This struct represents a CANOpen message.
Definition: message.h:39
~Core()
Destructor.
Definition: core.cpp:78