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 
32 
34 {
36  {
37  setName(config.getName());
38  vendorID = config.getVendorID();
39  productCode = config.getProductCode();
40  serialNumber = config.getSerialNumber();
41  }
42 
43  std::string
45  {
46  return std::string(m_name);
47  }
48 
49  const char*
51  {
52  return &m_name[0];
53  }
54 
55  bool
56  SlaveIdentifier::setName(const std::string& name)
57  {
58  std::string dname = std::string(&m_name[m_start_pos_device_name]);
59  std::size_t old_len = strlen(m_name);
60  int written_chars =
61  std::snprintf(m_name, sizeof(m_name), "%s%s", name.c_str(), dname.c_str());
62  m_start_pos_device_name += static_cast<std::size_t>(written_chars) - old_len;
63  return written_chars > static_cast<int>(MAX_NAME_LENGTH);
64  }
65 
66  bool
67  SlaveIdentifier::setParentDeviceName(const std::string& parentDeviceName)
68  {
69  return std::snprintf(&m_name[m_start_pos_device_name],
70  sizeof(m_name) - m_start_pos_device_name,
71  " (%s)",
72  parentDeviceName.c_str()) > static_cast<int>(MAX_NAME_LENGTH);
73  }
74 
75  std::string
76  SlaveIdentifier::toString(const std::string& linePrefix) const
77  {
78  std::stringstream ss;
79  constexpr std::uint16_t fw = 16;
80  // clang-format off
81  using namespace std;
82  ss << linePrefix << left << setw(fw) << "Name:"
83  << getName() << "\n";
84  ss << linePrefix << left << setw(fw) << "VendorID:"
85  << hex << "0x" << vendorID << dec << " (" << vendorID << ")\n";
86  ss << linePrefix << left << setw(fw) << "ProductCode:"
87  << hex << "0x" << productCode << dec << " (" << productCode << ")\n";
88  ss << linePrefix << left << setw(fw) << "RevisionNumber:"
89  << hex << "0x" << revisionNumber << dec << " (" << revisionNumber << ")\n";
90  ss << linePrefix << left << setw(fw) << "SerialNumber:"
91  << hex << "0x" << serialNumber << dec << " (" << serialNumber << ")\n";
92  ss << linePrefix << left << setw(fw) << "SlaveIndex:"
93  << hex << "0x" << slaveIndex << dec << " (" << slaveIndex << ")";
94  // clang-format on
95 
96  return ss.str();
97  }
98 
99  std::string
100  SlaveIdentifier::toMinimalString(const std::string& linePrefix) const
101  {
102  std::stringstream ss;
103  constexpr std::uint16_t fw = 16;
104  // clang-format off
105  using namespace std;
106  ss << linePrefix << left << setw(fw) << "Name:"
107  << getName() << "\n";
108  ss << linePrefix << left << setw(fw) << "SlaveIndex:"
109  << hex << "0x" << slaveIndex << dec << " (" << slaveIndex << ")";
110  // clang-format on
111 
112  return ss.str();
113  }
114 } // namespace armarx::control::ethercat
armarx::control::hardware_config::SlaveIdentifierConfig
Data structure holding the information necessary to create a SlaveIdentifier.
Definition: SlaveIdentifierConfig.h:15
armarx::control::ethercat::SlaveIdentifier::SlaveIdentifier
SlaveIdentifier()=default
armarx::control::hardware_config::SlaveIdentifierConfig::getProductCode
std::uint32_t getProductCode()
Definition: SlaveIdentifierConfig.cpp:12
armarx::control::hardware_config::SlaveIdentifierConfig::getVendorID
std::uint32_t getVendorID()
Definition: SlaveIdentifierConfig.cpp:6
armarx::control::ethercat::SlaveIdentifier::productCode
std::uint32_t productCode
The product code of the slave.
Definition: SlaveIdentifier.h:75
armarx::control::ethercat::SlaveIdentifier::setParentDeviceName
bool setParentDeviceName(const std::string &parentDeviceName)
Sets the name of the parent device of a slave identifier.
Definition: SlaveIdentifier.cpp:67
armarx::control::ethercat::SlaveIdentifier::setName
bool setName(const std::string &name)
Sets the slave name of a SlaveIdentifier and returns whether the new name fits together with the pare...
Definition: SlaveIdentifier.cpp:56
armarx::control::hardware_config::SlaveIdentifierConfig::getSerialNumber
std::uint32_t getSerialNumber()
Definition: SlaveIdentifierConfig.cpp:18
armarx::control::ethercat::SlaveIdentifier::serialNumber
std::uint32_t serialNumber
The serial number of a slave with a certain productCode.
Definition: SlaveIdentifier.h:77
armarx::control::ethercat::SlaveIdentifier::vendorID
std::uint32_t vendorID
The unique id of the vendor of the slave hardware.
Definition: SlaveIdentifier.h:73
armarx::control::ethercat::SlaveIdentifier::getName
std::string getName() const
Returns the combination of slave name and name of the parent device of a slave identifier as a string...
Definition: SlaveIdentifier.cpp:44
armarx::control::ethercat
Definition: Bus.cpp:24
armarx::control::ethercat::SlaveIdentifier::revisionNumber
std::uint32_t revisionNumber
The revision number of the slave hard- or firmware (is not used for identifying a slave on the bus)
Definition: SlaveIdentifier.h:80
armarx::control::hardware_config::SlaveIdentifierConfig::getName
std::string getName()
Definition: SlaveIdentifierConfig.cpp:24
armarx::control::ethercat::SlaveIdentifier::toMinimalString
std::string toMinimalString(const std::string &linePrefix="") const
Returns a minimal string representation of a SlaveIdentifier.
Definition: SlaveIdentifier.cpp:100
armarx::control::ethercat::SlaveIdentifier::slaveIndex
std::int16_t slaveIndex
The index of the slave on the bus.
Definition: SlaveIdentifier.h:71
std
Definition: Application.h:66
RapidXmlReader.h
armarx::control::ethercat::SlaveIdentifier::getNameAsCStr
const char * getNameAsCStr() const
Returns the combination of slave name and name of the parent device of a slave identifier as char arr...
Definition: SlaveIdentifier.cpp:50
Logging.h
armarx::control::ethercat::SlaveIdentifier::toString
std::string toString(const std::string &linePrefix="") const
Returns a full string representation of a SlaveIdentifier.
Definition: SlaveIdentifier.cpp:76
SlaveIdentifier.h