1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432 | /* File: coe_utilities.cpp
* This file is part of the program ethercatcpp-core
* Program description : EtherCAT driver libraries for UNIX
* Copyright (C) 2017-2024 - Robin Passama (LIRMM / CNRS) Arnaud Meline
* (LIRMM / CNRS) Benjamin Navarro (LIRMM / CNRS). All Right reserved.
*
* This software is free software: you can redistribute it and/or modify
* it under the terms of the CeCILL-C license as published by
* the CEA CNRS INRIA, either version 1
* of the License, or (at your option) any later version.
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* CeCILL-C License for more details.
*
* You should have received a copy of the CeCILL-C License
* along with this software. If not, it can be found on the official
* website of the CeCILL licenses family (http://www.cecill.info/index.en.html).
*/
#include <ethercatcpp/coe_utilities.h>
#include <ethercatcpp/slave_device.h>
#include <iostream>
#include <pid/hashed_string.h>
#include <stdexcept>
#include <sstream>
#include <iomanip>
#include <pid/log/ethercatcpp-core_ethercatcpp-core.h>
namespace ethercatcpp::coe {
/////////// Object Dictionary /////////////
ObjectDictionary::ObjectDictionary(
std::initializer_list<std::pair<std::string_view, entry>> dictionary)
: dictionary_{} {
for (auto& [str, obj] : dictionary) {
dictionary_[pid::hashed_string(str)] = obj;
}
}
const ObjectDictionary::DictionaryEntry&
ObjectDictionary::get_entry_safe(std::string_view name,
std::string_view caller) const {
auto id = pid::hashed_string(name);
auto it = dictionary_.find(id);
if (it == dictionary_.end()) {
throw std::logic_error(std::string(caller) + ": no object called " +
std::string(name) + " in the dictionary.");
}
return it->second;
}
std::tuple<uint16_t, uint8_t, uint8_t>
ObjectDictionary::object(std::string_view name) const {<--- Function parameter 'name' should be passed by const reference. [+]Parameter 'name' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
auto& obj = get_entry_safe(name, "object");
return {obj.addr, obj.subindex, obj.bits};
}
uint32_t ObjectDictionary::mapped_pdo_object(std::string_view name) const {<--- Function parameter 'name' should be passed by const reference. [+]Parameter 'name' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
auto& obj = get_entry_safe(name, "mapped_pdo_object");
uint32_t ret{0};
ret = obj.addr;
ret <<= 8;
ret |= obj.subindex;
ret <<= 8;
ret |= obj.bits;
return ret;
}
uint16_t ObjectDictionary::addr(std::string_view name) const {<--- Function parameter 'name' should be passed by const reference. [+]Parameter 'name' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
auto& obj = get_entry_safe(name, "addr");
return obj.addr;
}
void ObjectDictionary::add_entry(std::string_view name,
const DictionaryEntry& object) {
auto id = pid::hashed_string(name);
auto it = dictionary_.find(id);
if (it != dictionary_.end()) {
throw std::logic_error("add_entry: object " + std::string(name) +
" in already in the dictionary.");
}
dictionary_[id] = object;
}
void ObjectDictionary::add_entries(
std::initializer_list<std::pair<std::string_view, entry>> entries) {
for (auto& an_entry : entries) {
add_entry(an_entry.first, an_entry.second);
}
}
std::tuple<uint16_t, uint8_t, uint8_t>
ObjectDictionary::pdo_object_specs(uint32_t full_spec) {
std::tuple<uint16_t, uint8_t, uint8_t> ret;
std::get<0>(ret) = static_cast<uint16_t>(full_spec >> 16);
std::get<1>(ret) = static_cast<uint8_t>((full_spec >> 8) & 0x0000FF);
std::get<2>(ret) = static_cast<uint8_t>(full_spec & 0x000000FF);
return ret;
}
size_t ObjectDictionary::size(std::string_view name) const {<--- Function parameter 'name' should be passed by const reference. [+]Parameter 'name' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
auto& obj = get_entry_safe(name, "size");
return obj.bits / 8;
}
/////////// PDO Mapping /////////////
PDOMapping::PDOMapping(ObjectDictionary& dictionary, uint8_t idx, bool is_tx)<--- Function 'PDOMapping' argument 1 names different: declaration 'dico' definition 'dictionary'.
: reference_dictionnary_{&dictionary}, index_{idx}, is_tx_{is_tx} {
}
PDOMapping::PDOMapping(ObjectDictionary& dictionary, uint8_t idx, bool is_tx,<--- Function 'PDOMapping' argument 1 names different: declaration 'dico' definition 'dictionary'.
std::initializer_list<std::string_view> init)<--- Function 'PDOMapping' argument 4 names different: declaration 'objects' definition 'init'.
: PDOMapping(dictionary, idx, is_tx) {
for (auto& obj : init) {
add_object(obj);
}
}
uint16_t PDOMapping::map_addr() const {
return is_tx_ ? coe_tx_pdo_map_1 | index_ : coe_rx_pdo_map_1 | index_;
}
PDOMapping::const_iterator PDOMapping::begin() const {
return objects_mapping_.begin();
}
PDOMapping::const_iterator PDOMapping::end() const {
return objects_mapping_.end();
}
void PDOMapping::add_object(std::string_view obj) {<--- Function parameter 'obj' should be passed by const reference. [+]Parameter 'obj' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
objects_mapping_.push_back(std::pair<uint8_t, uint32_t>(
objects_mapping_.size() + 1, // index
reference_dictionnary_->mapped_pdo_object(obj))); // value
}
void PDOMapping::reset() {
objects_mapping_.clear();
}
bool PDOMapping::configure(SlaveDevice& eth_slave) const {
uint8_t val = 0;
bool ret{true};
// Reset the mapping first (size =0)
ret = eth_slave.write_sdo(map_addr(), 0x00, val) != 0;
if (not ret) {
pid_log << pid::error << "cannot reset mapping" << pid::flush;
return false;
}
// then add objects one by one
for (auto [idx, target] : objects_mapping_) {
ret &= eth_slave.write_sdo(map_addr(), idx, target) != 0;
if (not ret) {
std::stringstream ss;
ss << "cannot add object " << std::hex << target << " to mapping "
<< map_addr() << " subindex " << idx << "";
pid_log << pid::error << ss.str() << pid::flush;
}
}
// Set mapping size
val = static_cast<uint8_t>(objects_mapping_.size());
ret &= eth_slave.write_sdo(map_addr(), 0x00, val) != 0;
if (not ret) {
pid_log << pid::error << "cannot set mapping size" << pid::flush;
}
return ret;
}
size_t PDOMapping::memory_size() const {
size_t ret{0};
for (auto [idx, target] : objects_mapping_) {
ret += std::get<2>(ObjectDictionary::pdo_object_specs(target));
}
return ret / 8; // size in bytes
}
bool PDOMapping::is_tx() const {
return is_tx_;
}
bool PDOMapping::has_entry(std::string_view entry) const {<--- Function parameter 'entry' should be passed by const reference. [+]Parameter 'entry' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
uint32_t obj = reference_dictionnary_->mapped_pdo_object(entry);
for (auto& mapped : objects_mapping_) {
if (mapped.second == obj) {<--- Consider using std::any_of algorithm instead of a raw loop.
return true;
}
}
return false;
}
void PDOMapping::check_entry_throws(std::string_view obj) const {
if (not has_entry(obj)) {
std::string descr = "mapping ";
descr += std::to_string(map_addr());
descr += " has no entry named " + std::string(obj);
throw std::logic_error(descr);
}
}
size_t PDOMapping::memory_shift(std::string_view obj) const {<--- Function parameter 'obj' should be passed by const reference. [+]Parameter 'obj' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
check_entry_throws(obj);
size_t shift{0};
uint32_t obj_spec = reference_dictionnary_->mapped_pdo_object(obj);
for (auto [idx, target] : objects_mapping_) {
if (obj_spec == target) {
return shift / 8;
}
shift += std::get<2>(ObjectDictionary::pdo_object_specs(target));
}
return shift / 8;
}
bool PDOMapping::check_entry_size(std::string_view obj, size_t bytes) const {<--- Function parameter 'obj' should be passed by const reference. [+]Parameter 'obj' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
check_entry_throws(obj);
size_t obj_bytes = std::get<2>(reference_dictionnary_->object(obj)) / 8;
return obj_bytes == bytes;
}
const ObjectDictionary& PDOMapping::dictionary() const {
return *reference_dictionnary_;
}
/////////// PDO Buffer /////////////
PDOBuffer::PDOBuffer(bool is_tx, uint16_t addr, uint32_t flags)<--- Member variable 'PDOBuffer::data_' is not initialized in the constructor.<--- Function 'PDOBuffer' argument 2 names different: declaration 'addr_' definition 'addr'.<--- Function 'PDOBuffer' argument 3 names different: declaration 'flags_' definition 'flags'.
: is_tx_{is_tx}, addr_{addr}, flags_{flags} {
}
bool PDOBuffer::add_mapping(const PDOMapping& mapping, bool may_throw) {
if (mapping.is_tx() != is_tx_) {
if (may_throw) {
std::string descr = "Error trying to assign a ";
descr += (mapping.is_tx() ? "TX mapping" : "RX mapping");
descr += " to a ";
descr += (mapping.is_tx() ? "RX buffer" : "TX buffer");
throw std::logic_error(descr);
}
return false;
}
if (contains_mapping(mapping)) {
if (may_throw) {
std::string descr = (is_tx_ ? "TX" : "RX");
descr += " buffer already contains mapping ";
descr += std::to_string(mapping.map_addr());
throw std::logic_error(descr);
}
return false;
}
mappings_.push_back(&mapping);
return true;
}
void PDOBuffer::bind(uint8_t* zone) {<--- Function 'bind' argument 1 names different: declaration 'data' definition 'zone'.
data_ = zone;
}
bool PDOBuffer::contains_mapping(const PDOMapping& mapping) const {
for (const auto& a_map : mappings_) {
if (a_map->map_addr() == mapping.map_addr()) {<--- Consider using std::any_of algorithm instead of a raw loop.
return true;
}
}
return false;
}
void PDOBuffer::check_mapping_throws(const PDOMapping& mapping) const {
if (not contains_mapping(mapping)) {
std::string descr = (is_tx_ ? "TX" : "RX");
descr += " buffer does not contain mapping ";
descr += std::to_string(mapping.map_addr());
throw std::logic_error(descr);
}
}
size_t PDOBuffer::mapping_memory_shift(const PDOMapping& mapping) const {
check_mapping_throws(mapping);
size_t incr = 0;
for (const auto& a_map : mappings_) {
if (a_map->map_addr() == mapping.map_addr()) {
return incr;
}
incr += a_map->memory_size();
}
return incr;
}
size_t PDOBuffer::entry_memory_shift(const PDOMapping& mapping,
std::string_view entry) const {
check_mapping_throws(mapping);
if (not mapping.has_entry(entry)) {
std::string descr = "object " + std::string(entry);
descr +=
"does not belong to mapping " + std::to_string(mapping.map_addr());
throw std::logic_error(descr);
}
return compute_shift(mapping, entry);
}
void PDOBuffer::check_entry_throws(const PDOMapping& mapping,
std::string_view entry_name,
size_t bytes) const {
if (not mapping.check_entry_size(entry_name, bytes)) {
std::string descr = "size of object " + std::string(entry_name) + "(";
descr += std::to_string(mapping.dictionary().size(entry_name)) + ") ";
descr +=
"does not match the variable size (" + std::to_string(bytes) + ")";
throw std::logic_error(descr);
}
}
size_t PDOBuffer::compute_shift(const PDOMapping& mapping,
std::string_view entry_name) const {<--- Function parameter 'entry_name' should be passed by const reference. [+]Parameter 'entry_name' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
return mapping_memory_shift(mapping) + mapping.memory_shift(entry_name);
}
const uint8_t* PDOBuffer::map_memory_internal(const PDOMapping& mapping,
std::string_view entry_name,<--- Function parameter 'entry_name' should be passed by const reference. [+]Parameter 'entry_name' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
size_t bytes) const {
check_entry_throws(mapping, entry_name, bytes);
return data_ + compute_shift(mapping, entry_name);
}
uint8_t* PDOBuffer::map_memory_internal(const PDOMapping& mapping,
std::string_view entry_name,<--- Function parameter 'entry_name' should be passed by const reference. [+]Parameter 'entry_name' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
size_t bytes) {
check_entry_throws(mapping, entry_name, bytes);
return data_ + compute_shift(mapping, entry_name);
}
static std::string to_hex(uint16_t value) {
std::ostringstream oss;
oss << "0x" << std::hex << std::setw(4) << std::setfill('0') << value;
return oss.str();
}
void PDOBuffer::check_mapping_size_throws(const PDOMapping& mapping,
size_t bytes) const {
check_mapping_throws(mapping);
if (mapping.memory_size() != bytes) {
std::string descr =
"size of mapping " + to_hex(mapping.map_addr()) + "(";
descr += std::to_string(mapping.memory_size()) + ") ";
descr +=
"does not match the variable size (" + std::to_string(bytes) + ")";
throw std::logic_error(descr);
}
}
uint8_t* PDOBuffer::map_memory_internal(const PDOMapping& mapping,
size_t bytes) {
check_mapping_size_throws(mapping, bytes);
return data_ + mapping_memory_shift(mapping);
}
const uint8_t* PDOBuffer::map_memory_internal(const PDOMapping& mapping,
size_t bytes) const {
check_mapping_size_throws(mapping, bytes);
return data_ + mapping_memory_shift(mapping);
}
uint16_t PDOBuffer::memory_size() const {
size_t res{0};
for (const auto& a_map : mappings_) {
res += a_map->memory_size();<--- Consider using std::accumulate algorithm instead of a raw loop.
}
return static_cast<uint16_t>(res);
}
const PDOMapping& PDOBuffer::access_mapping(size_t idx) const {
if (idx >= mappings_.size()) {
throw std::logic_error("mapping with index " + std::to_string(idx) +
" doe not exist in buffer");
}
return *mappings_[idx];
}
void PDOBuffer::define_physical_buffer(SlaveDevice& eth_slave) {
if (is_tx_) {
eth_slave.define_physical_buffer(SlaveDevice::SYNCHROS_IN, addr_,
flags_, memory_size());
} else {
eth_slave.define_physical_buffer(SlaveDevice::SYNCHROS_OUT, addr_,
flags_, memory_size());
}
}
void PDOBuffer::bind_physical_buffer(SlaveDevice& eth_slave) {
if (is_tx_) {
bind(eth_slave.input_buffer(addr_));
} else {
bind(eth_slave.output_buffer(addr_));
}
}
bool PDOBuffer::configure(SlaveDevice& eth_slave) const {
// configure mappings first
for (const auto& a_map : mappings_) {
if (not a_map->configure(eth_slave)) {<--- Consider using std::find_if algorithm instead of a raw loop.
std::stringstream ss;
ss << std::hex << a_map->map_addr();
pid_log << pid::error << "cannot configure mapping 0x" << ss.str()
<< pid::flush;
return false;
}
}
// then configure PDO Assignment
return assign(eth_slave);
}
bool PDOBuffer::assign(SlaveDevice& eth_slave) const {
bool res{true};
if (is_tx_) {
res &= eth_slave.start_status_pdo_mapping<uint16_t>();
for (const auto& a_map : mappings_) {
res &=<--- Consider using std::any_of, std::all_of, std::none_of, or std::accumulate algorithm instead of a raw loop.
eth_slave.add_status_pdo_mapping<uint16_t>(a_map->map_addr());
}
res &= eth_slave.end_status_pdo_mapping<uint16_t>();
} else {
res &= eth_slave.start_command_pdo_mapping<uint16_t>();
for (const auto& a_map : mappings_) {
res &=<--- Consider using std::any_of, std::all_of, std::none_of, or std::accumulate algorithm instead of a raw loop.
eth_slave.add_command_pdo_mapping<uint16_t>(a_map->map_addr());
}
res &= eth_slave.end_command_pdo_mapping<uint16_t>();
}
return res;
}
} // namespace ethercatcpp::coe
|