33 #include <boost/test/unit_test.hpp> 34 #include <boost/thread/thread.hpp> 38 BOOST_AUTO_TEST_SUITE(ts_sdo)
46 "-----Running SDO download test-----" <<
endl <<
47 "-----------------------------------" <<
endl);
61 std::vector<uint8_t> data(4);
75 response.
data[1] = index & 0xff;
76 response.
data[2] = index >> 8;
77 response.
data[3] = subindex & 0xff;
78 can_device->addResponse(response);
81 BOOST_REQUIRE(node->m_sdo.download(
false, index, subindex, data));
84 CanMsg msg = can_device->getLastMessage();
86 BOOST_REQUIRE(msg.dlc == 8);
87 BOOST_REQUIRE(msg.rtr == 0);
89 BOOST_REQUIRE(msg.data[1] == (index & 0xff));
90 BOOST_REQUIRE(msg.data[2] == (index >> 8));
91 BOOST_REQUIRE(msg.data[3] == subindex);
92 BOOST_REQUIRE(msg.data[4] == data[0]);
93 BOOST_REQUIRE(msg.data[5] == data[1]);
94 BOOST_REQUIRE(msg.data[6] == data[2]);
95 BOOST_REQUIRE(msg.data[7] == data[3]);
96 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
101 can_device->addResponse(response);
103 BOOST_REQUIRE_THROW(node->m_sdo.download(
false, index, subindex, data), std::exception);
105 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
111 can_device->addResponse(response);
112 BOOST_REQUIRE_THROW(node->m_sdo.download(
false, index, subindex, data),
TimeoutException);
113 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
118 can_device->addResponse(response);
119 BOOST_REQUIRE_THROW(node->m_sdo.download(
false, index, subindex, data),
TimeoutException);
121 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
126 can_device->addResponse(response);
127 BOOST_REQUIRE_THROW(node->m_sdo.download(
false, index, subindex, data),
ResponseException);
129 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
133 response.
data[1] = (index+1) & 0xff;
134 can_device->addResponse(response);
135 BOOST_REQUIRE_THROW(node->m_sdo.download(
false, index, subindex, data),
ResponseException);
136 response.
data[1] = index & 0xff;
137 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
141 response.
data[2] = (index+256) >> 8;
142 can_device->addResponse(response);
143 BOOST_REQUIRE_THROW(node->m_sdo.download(
false, index, subindex, data),
ResponseException);
144 response.
data[2] = index >> 8;
145 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
149 response.
data[3] = (subindex+1) & 0xff;
150 can_device->addResponse(response);
151 BOOST_REQUIRE_THROW(node->m_sdo.download(
false, index, subindex, data),
ResponseException);
152 response.
data[3] = subindex & 0xff;
153 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
157 can_device->addResponse(response);
159 BOOST_REQUIRE(!(node->m_sdo.download(
true, index, subindex, data)));
161 catch (
const std::exception& e)
165 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
169 can_device->addResponse(response);
170 std::vector<uint8_t> empty_data;
171 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, empty_data),
ProtocolException);
172 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
176 can_device->addResponse(response);
177 empty_data.resize(5);
179 BOOST_REQUIRE(!(node->m_sdo.download(
false, index, subindex, empty_data)));
181 catch (
const std::exception& e)
185 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
195 LOGGING_INFO(CanOpen,
"---------------------------------------------" <<
endl <<
196 "-----Running SDO download interface test-----" <<
endl <<
197 "---------------------------------------------" <<
endl);
211 std::vector<uint8_t> data(4);
225 response.
data[1] = index & 0xff;
226 response.
data[2] = index >> 8;
227 response.
data[3] = subindex & 0xff;
228 can_device->addResponse(response);
230 BOOST_REQUIRE_NO_THROW(node->m_sdo.download(
false, index, subindex, data));
232 CanMsg msg1 = can_device->getLastMessage();
234 can_device->addResponse(response);
235 uint32_t data_block = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
237 BOOST_REQUIRE_NO_THROW(node->m_sdo.download(
false, index, subindex, data_block));
240 CanMsg msg2 = can_device->getLastMessage();
243 BOOST_REQUIRE(msg2.dlc == 8);
244 BOOST_REQUIRE(msg2.rtr == 0);
245 BOOST_REQUIRE(msg2.data[1] == (index & 0xff));
247 BOOST_REQUIRE(msg2.data[2] == (index >> 8));
248 BOOST_REQUIRE(msg2.data[3] == subindex);
249 BOOST_REQUIRE(msg2.data[4] == data[0]);
250 BOOST_REQUIRE(msg2.data[5] == data[1]);
251 BOOST_REQUIRE(msg2.data[6] == data[2]);
252 BOOST_REQUIRE(msg2.data[7] == data[3]);
254 BOOST_REQUIRE(msg1.data[4] == msg2.data[4]);
255 BOOST_REQUIRE(msg1.data[5] == msg2.data[5]);
256 BOOST_REQUIRE(msg1.data[6] == msg2.data[6]);
257 BOOST_REQUIRE(msg1.data[7] == msg2.data[7]);
266 "-----Running SDO upload test-----" <<
endl <<
267 "---------------------------------" <<
endl);
284 std::vector<uint8_t> data(4);
296 response.
data[1] = index & 0xff;
297 response.
data[2] = index >> 8;
298 response.
data[3] = subindex & 0xff;
299 response.
data[4] = data[0];
300 response.
data[5] = data[1];
301 response.
data[6] = data[2];
302 response.
data[7] = data[3];
303 can_device->addResponse(response);
305 std::vector<uint8_t> uploaded_data;
310 BOOST_REQUIRE(node->m_sdo.upload(
false, index, subindex, uploaded_data));
312 catch (
const std::exception& e)
318 BOOST_REQUIRE(data == uploaded_data);
321 CanMsg msg = can_device->getLastMessage();
323 BOOST_REQUIRE(msg.
dlc == 8);
324 BOOST_REQUIRE(msg.
rtr == 0);
326 BOOST_REQUIRE(msg.
data[1] == (index & 0xff));
327 BOOST_REQUIRE(msg.
data[2] == (index >> 8));
328 BOOST_REQUIRE(msg.
data[3] == subindex);
330 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
335 can_device->addResponse(response);
336 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, uploaded_data),
TimeoutException);
338 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
344 can_device->addResponse(response);
345 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, uploaded_data),
TimeoutException);
346 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
351 can_device->addResponse(response);
352 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, uploaded_data),
TimeoutException);
354 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
359 can_device->addResponse(response);
360 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, uploaded_data),
ResponseException);
362 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
366 response.
data[1] = (index+1) & 0xff;
367 can_device->addResponse(response);
368 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, uploaded_data),
ResponseException);
369 response.
data[1] = index & 0xff;
370 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
374 response.
data[2] = (index+256) >> 8;
375 can_device->addResponse(response);
376 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, uploaded_data),
ResponseException);
377 response.
data[2] = index >> 8;
378 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
382 response.
data[3] = (subindex+1) & 0xff;
383 can_device->addResponse(response);
384 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, uploaded_data),
ResponseException);
385 response.
data[3] = subindex & 0xff;
386 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
390 can_device->addResponse(response);
391 BOOST_REQUIRE(!(node->m_sdo.upload(
true, index, subindex, uploaded_data)));
393 boost::this_thread::sleep(boost::posix_time::milliseconds(200));
398 response.
data[0] = 0x80;
400 response.
data[4] = char_vec[0];
401 response.
data[5] = char_vec[1];
402 response.
data[6] = char_vec[2];
403 response.
data[7] = char_vec[3];
404 can_device->addResponse(response);
405 BOOST_REQUIRE_THROW (node->m_sdo.download(
false, index, subindex, uploaded_data),
ProtocolException);
406 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
410 BOOST_AUTO_TEST_SUITE_END()
bool initialize(int &argc, char *argv[], bool remove_read_arguments)
static unsigned char const SDO_SEG_REQ_INIT_UPLOAD
CanDevPtr getCanDevice() const
Get a handle to the current CAN device. This is basically for debugging with a Dummy Can Device...
std::string hexArrayToString(const unsigned char *msg, const uint8_t length)
Transforms an array of unsigned chars into a string of Hex representations of those chars...
#define LOGGING_INFO(streamname, arg)
Basic CanOpen exception that contains the Object dictionary index and subindex.
std::vector< uint8_t > convertToCharVector(const T value)
This little helper transforms any datatype that has a size of at most 4 Bytes into a vector of uint8_...
If a device response times out, this exception will be thrown.
The DS301Group class is the base Class for all canOpen device groups, providing basic interfaces to t...
static unsigned char const SDO_SEG_RES_INIT_UPLOAD_4BYTE
#define LOGGING_ERROR(streamname, arg)
The CanOpenController class is the main entry point for any calls to the canOpen System.
Exceptions relating to device responses.
ThreadStream & endl(ThreadStream &stream)
void setLogLevel(icl_core::logging::LogLevel log_level)
The SDO class represents Service Data Objects (SDO) that are used for slow access of the canOpen obje...
boost::shared_ptr< GroupT > getGroup(const std::string &index="default")
Returns a shared pointer to the group with a given index if possible.
BOOST_AUTO_TEST_CASE(sdo_download)
void addNode(const uint8_t node_id, const std::string &group_name="default")
Adds a new node to a group. If the group is not found (e.g. it was not created before), nothing will be done.
static const uint16_t ID_TSDO_MIN
std::string hexToString(const uint64_t num)
Converts a hexadecimal number into its string representation 0xXX.
static unsigned char const SDO_SEG_REQ_INIT_DOWNLOAD_4BYTE
void addGroup(const std::string &identifier)
Adds a new node group with a given identifier. The group's type is given as template parameter...
static unsigned char const SDO_SEG_RES_INIT_DOWNLOAD
#define LOGGING_ERROR_C(streamname, classname, arg)
The DS301Node class Is the base class representation of canOpen devices. It is the access point to th...