SlaveIdentifier.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5 * Karlsruhe Institute of Technology (KIT), all rights reserved.
6 *
7 * ArmarX is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * ArmarX is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24#include "SlaveIdentifier.h"
25
26#include <iomanip>
27#include <iostream>
28
31
33{
41
42 std::string
44 {
45 return std::string(m_name);
46 }
47
48 const char*
50 {
51 return &m_name[0];
52 }
53
54 bool
55 SlaveIdentifier::setName(const std::string& name)
56 {
57 std::string dname = std::string(&m_name[m_start_pos_device_name]);
58 std::size_t old_len = strlen(m_name);
59 int written_chars =
60 std::snprintf(m_name, sizeof(m_name), "%s%s", name.c_str(), dname.c_str());
61 m_start_pos_device_name += static_cast<std::size_t>(written_chars) - old_len;
62 return written_chars > static_cast<int>(MAX_NAME_LENGTH);
63 }
64
65 bool
66 SlaveIdentifier::setParentDeviceName(const std::string& parentDeviceName)
67 {
68 return std::snprintf(&m_name[m_start_pos_device_name],
69 sizeof(m_name) - m_start_pos_device_name,
70 " (%s)",
71 parentDeviceName.c_str()) > static_cast<int>(MAX_NAME_LENGTH);
72 }
73
74 std::string
75 SlaveIdentifier::toString(const std::string& linePrefix) const
76 {
77 std::stringstream ss;
78 constexpr std::uint16_t fw = 16;
79 // clang-format off
80 using namespace std;
81 ss << linePrefix << left << setw(fw) << "Name:"
82 << getName() << "\n";
83 ss << linePrefix << left << setw(fw) << "VendorID:"
84 << hex << "0x" << vendorID << dec << " (" << vendorID << ")\n";
85 ss << linePrefix << left << setw(fw) << "ProductCode:"
86 << hex << "0x" << productCode << dec << " (" << productCode << ")\n";
87 ss << linePrefix << left << setw(fw) << "RevisionNumber:"
88 << hex << "0x" << revisionNumber << dec << " (" << revisionNumber << ")\n";
89 ss << linePrefix << left << setw(fw) << "SerialNumber:"
90 << hex << "0x" << serialNumber << dec << " (" << serialNumber << ")\n";
91 ss << linePrefix << left << setw(fw) << "SlaveIndex:"
92 << hex << "0x" << slaveIndex << dec << " (" << slaveIndex << ")";
93 // clang-format on
94
95 return ss.str();
96 }
97
98 std::string
99 SlaveIdentifier::toMinimalString(const std::string& linePrefix) const
100 {
101 std::stringstream ss;
102 constexpr std::uint16_t fw = 16;
103 // clang-format off
104 using namespace std;
105 ss << linePrefix << left << setw(fw) << "Name:"
106 << getName() << "\n";
107 ss << linePrefix << left << setw(fw) << "SlaveIndex:"
108 << hex << "0x" << slaveIndex << dec << " (" << slaveIndex << ")";
109 // clang-format on
110
111 return ss.str();
112 }
113} // namespace armarx::control::ethercat
std::uint32_t revisionNumber
The revision number of the slave hard- or firmware (is not used for identifying a slave on the bus)
std::string toMinimalString(const std::string &linePrefix="") const
Returns a minimal string representation of a SlaveIdentifier.
bool setParentDeviceName(const std::string &parentDeviceName)
Sets the name of the parent device of a slave identifier.
std::uint32_t vendorID
The unique id of the vendor of the slave hardware.
std::uint32_t serialNumber
The serial number of a slave with a certain productCode.
bool setName(const std::string &name)
Sets the slave name of a SlaveIdentifier and returns whether the new name fits together with the pare...
std::int16_t slaveIndex
The index of the slave on the bus.
std::uint32_t productCode
The product code of the slave.
std::string getName() const
Returns the combination of slave name and name of the parent device of a slave identifier as a string...
const char * getNameAsCStr() const
Returns the combination of slave name and name of the parent device of a slave identifier as char arr...
std::string toString(const std::string &linePrefix="") const
Returns a full string representation of a SlaveIdentifier.
Data structure holding the information necessary to create a SlaveIdentifier.