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