KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
pdo.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 "pdo.h"
33 #include "logger.h"
34 #include "core.h"
35 
36 #include <iostream>
37 #include <cassert>
38 
39 namespace kaco {
40 
41 PDO::PDO(Core& core)
42  : m_core(core)
43  { }
44 
45 void PDO::process_incoming_message(const Message& message) const {
46 
47  uint16_t cob_id = message.cob_id;
48  uint8_t node_id = message.get_node_id();
49  std::vector<uint8_t> data;
50 
51  DEBUG_LOG("Received transmit PDO with cob_id 0x"<<std::hex<<cob_id<<" (usually from node 0x"<<node_id<<") length = "<<message.len);
52 
53  for (unsigned i=0; i<message.len; ++i) {
54  data.push_back(message.data[i]);
55  }
56 
57  // call registered callbacks
58  bool found_callback = false;
59  {
60  std::lock_guard<std::mutex> scoped_lock(m_receive_callbacks_mutex);
61  for (const PDOReceivedCallback& callback : m_receive_callbacks) {
62  if (callback.cob_id == cob_id) {
63  found_callback = true;
64  // This is not async because callbacks are only registered internally.
65  // Copy data vector because there can be multiple callbacks for this PDO.
66  callback.callback(data);
67  }
68  }
69  }
70 
71  DEBUG(
72  if (!found_callback) {
73  PRINT("PDO is unassigned. Here is the data (LSB):");
74  for (unsigned i=0; i<data.size(); ++i) {
75  std::cout << std::hex << (unsigned)data[i] << " ";
76  }
77  std::cout << std::endl;
78  }
79  )
80 
81 }
82 
83 void PDO::send(uint16_t cob_id, const std::vector<uint8_t>& data) {
84 
85  assert(data.size()<=8 && "[PDO::send] A PDO message can have at most 8 data bytes.");
86 
87  Message message;
88  message.cob_id = cob_id;
89  message.rtr = false;
90  message.len = data.size();
91  for (uint8_t i=0; i<data.size(); ++i) {
92  message.data[i] = data[i];
93  }
94 
95  DEBUG_LOG_EXHAUSTIVE("Sending the following PDO:");
96  DEBUG_EXHAUSTIVE(message.print();)
97 
98  m_core.send(message);
99 
100 }
101 
103  std::lock_guard<std::mutex> scoped_lock(m_receive_callbacks_mutex);
104  m_receive_callbacks.push_back({cob_id,callback});
105 }
106 
107 } // end namespace kaco
uint16_t cob_id
Message ID aka COB-ID.
Definition: message.h:42
uint8_t len
Message's length (0 to 8)
Definition: message.h:48
void send(const Message &message)
Sends a message.
Definition: core.cpp:243
void send(uint16_t cob_id, const std::vector< uint8_t > &data)
Sends a PDO message.
Definition: pdo.cpp:83
std::function< void(std::vector< uint8_t >) > Callback
Type of the callback.
Definition: pdo.h:62
This class implements the Core of KaCanOpen It communicates with the CAN driver, sends CAN messages a...
Definition: core.h:59
void process_incoming_message(const Message &message) const
Handler for an incoming PDO message.
Definition: pdo.cpp:45
void add_pdo_received_callback(uint16_t cob_id, PDOReceivedCallback::Callback callback)
Adds a callback which will be called when a PDO has been received with the given COB-ID.
Definition: pdo.cpp:102
uint8_t get_node_id() const
Extracts the node id from the COB-ID.
Definition: message.cpp:40
PDO(Core &core)
Constructor.
Definition: pdo.cpp:41
A PDO message receiver function together with it's COB-ID Important: Never call add_pdo_received_call...
Definition: pdo.h:59
This struct represents a CANOpen message.
Definition: message.h:39
uint8_t data[8]
Data bytes.
Definition: message.h:51