SlaveIdentifier.h
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 
25 #pragma once
26 
27 #include <cstdint>
28 #include <iostream>
29 #include <string>
30 
31 namespace armarx
32 {
33  class RapidXmlReaderNode;
34 }
35 
37 
39 {
40 
41  /**
42  * @class SlaveIdentifier
43  * @ingroup Library-ethercat
44  *
45  * @brief The SlaveIdentifier class is a POD-type representing a unique set of values
46  * identifying an EtherCAT slave.
47  *
48  * The requirement of being a POD-type requires a pre-defined length of the name-variable.
49  * Therefore it might happen, that the name of a SlaveIdentifier is cut after
50  * a pre-defined amount of characters.
51  */
53  {
54  static constexpr std::uint8_t MAX_NAME_LENGTH = 128;
55 
56  public:
57  SlaveIdentifier() = default;
58  /**
59  * @brief Construct a SlaveIdentifier from a RapidXmlReaderNode by parsing the node for
60  * following entries:
61  * \li VendorID
62  * \li ProductCode
63  * \li SerialNumber
64  *
65  * @param node The RapidXmlReaderNode which is used as input.
66  */
68 
69  /** The index of the slave on the bus */
70  std::int16_t slaveIndex = -1;
71  /** The unique id of the vendor of the slave hardware */
72  std::uint32_t vendorID = 0;
73  /** The product code of the slave. The same product code means the same salve hardware */
74  std::uint32_t productCode = 0;
75  /** The serial number of a slave with a certain productCode */
76  std::uint32_t serialNumber = 0;
77  /** The revision number of the slave hard- or firmware (is not used for identifying a
78  slave on the bus) */
79  std::uint32_t revisionNumber = 0;
80 
81  friend std::ostream&
82  operator<<(std::ostream& stream, const SlaveIdentifier& rhs)
83  {
84  stream << rhs.toString();
85  return stream;
86  }
87 
88  /**
89  * @brief Returns the combination of slave name and name of the parent device of a
90  * slave identifier as a string object.
91  *
92  * This creates a new std::string obejct and therefore might allocate memory.
93  *
94  * @return The name as a std::string
95  */
96  std::string getName() const;
97 
98  /**
99  * @brief Returns the combination of slave name and name of the parent device of a
100  * slave identifier as char array.
101  *
102  * This function does \b not allocate memory and is safe to be used in a rt-thread.
103  *
104  * @return The name as a const pointer to a char array
105  */
106  const char* getNameAsCStr() const;
107 
108  /**
109  * @brief Sets the slave name of a SlaveIdentifier and returns whether the new name fits
110  * together with the parent device name into the name-variable.
111  * The name of the parent device will be kept, unless the combined name is too long for the
112  * size of the name-variable and some parts were lost.
113  *
114  * @param name the new slave name
115  * @return true if the provided name could be fully set as the salve name,
116  * false if parts of the combined name needed to be cut
117  */
118  bool setName(const std::string& name);
119 
120  /**
121  * @brief Sets the name of the parent device of a slave identifier.
122  * @see setName for conditions on possible cutting of the provided name.
123  *
124  * @param parentDeviceName the new name of the parent device
125  * @return true if the provided name could be set without losing data, false if not.
126  */
127  bool setParentDeviceName(const std::string& parentDeviceName);
128 
129  /**
130  * @brief Returns a full string representation of a SlaveIdentifier.
131  *
132  * This representation contains all members seperated by newlines.
133  * Each line will be prefixed by the provided linePrefix.
134  *
135  * @param linePrefix the string which will be put in front of every line
136  * @return A full string representation.
137  */
138  std::string toString(const std::string& linePrefix = "") const;
139 
140  /**
141  * @brief Returns a minimal string representation of a SlaveIdentifier.
142  *
143  * This representation contains only the combined name and the slaveIndex.
144  * Each printed member will be prefixed by the provided linePrefix and printed on its own
145  * line.
146  *
147  * @param linePrefix the string which will be put in front of every line
148  * @return A minimal string representation.
149  */
150  std::string toMinimalString(const std::string& linePrefix = "") const;
151 
152  private:
153  char m_name[MAX_NAME_LENGTH] = "unknown (unknown)";
154  std::uint16_t m_start_pos_device_name{7};
155  };
156 
157 } // 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::ethercat::SlaveIdentifier::productCode
std::uint32_t productCode
The product code of the slave.
Definition: SlaveIdentifier.h:74
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:66
armarx::control::ethercat::SlaveIdentifier
The SlaveIdentifier class is a POD-type representing a unique set of values identifying an EtherCAT s...
Definition: SlaveIdentifier.h:52
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:55
armarx::control::ethercat::SlaveIdentifier::operator<<
friend std::ostream & operator<<(std::ostream &stream, const SlaveIdentifier &rhs)
Definition: SlaveIdentifier.h:82
armarx::control::ethercat::SlaveIdentifier::serialNumber
std::uint32_t serialNumber
The serial number of a slave with a certain productCode.
Definition: SlaveIdentifier.h:76
armarx::control::ethercat::SlaveIdentifier::vendorID
std::uint32_t vendorID
The unique id of the vendor of the slave hardware.
Definition: SlaveIdentifier.h:72
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:43
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:79
SlaveIdentifierConfig.h
armarx::control::ethercat::SlaveIdentifier::toMinimalString
std::string toMinimalString(const std::string &linePrefix="") const
Returns a minimal string representation of a SlaveIdentifier.
Definition: SlaveIdentifier.cpp:99
armarx::control::ethercat::SlaveIdentifier::slaveIndex
std::int16_t slaveIndex
The index of the slave on the bus.
Definition: SlaveIdentifier.h:70
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:49
armarx::control::ethercat::SlaveIdentifier::toString
std::string toString(const std::string &linePrefix="") const
Returns a full string representation of a SlaveIdentifier.
Definition: SlaveIdentifier.cpp:75
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27