KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
utils.cpp
1 /*
2  * Copyright (c) 2015, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "utils.h"
33 #include "logger.h"
34 
35 #include <string>
36 #include <cstdint>
37 #include <algorithm>
38 #include <stdexcept>
39 
40 namespace kaco {
41 
42 std::string Utils::type_to_string(Type type) {
43  switch(type) {
44  case Type::uint8:
45  return "uint8";
46  case Type::uint16:
47  return "uint16";
48  case Type::uint32:
49  return "uint32";
50  case Type::uint64:
51  return "uint64";
52  case Type::int8:
53  return "int8";
54  case Type::int16:
55  return "int16";
56  case Type::int32:
57  return "int32";
58  case Type::int64:
59  return "int64";
60  case Type::real32:
61  return "real32";
62  case Type::real64:
63  return "real64";
64  case Type::boolean:
65  return "boolean";
66  case Type::string:
67  return "string";
68  case Type::octet_string:
69  return "octet_string";
70  default:
71  return "unknown type";
72  }
73 }
74 
75 std::string Utils::data_type_to_string(DataType type) {
76  #define ENUM_CASE(name) case DataType::name: return #name;
77  switch(type) {
78  ENUM_CASE(BOOLEAN)
79  ENUM_CASE(INTEGER8)
80  ENUM_CASE(INTEGER16)
81  ENUM_CASE(INTEGER32)
82  ENUM_CASE(UNSIGNED8)
83  ENUM_CASE(UNSIGNED16)
84  ENUM_CASE(UNSIGNED32)
85  ENUM_CASE(REAL32)
86  ENUM_CASE(VISIBLE_STRING)
87  ENUM_CASE(OCTET_STRING)
88  ENUM_CASE(UNICODE_STRING)
89  ENUM_CASE(TIME_OF_DAY)
90  ENUM_CASE(TIME_DIFFERENCE)
91  ENUM_CASE(LARGEDATA)
92  ENUM_CASE(INTEGER24)
93  ENUM_CASE(REAL64)
94  ENUM_CASE(INTEGER40)
95  ENUM_CASE(INTEGER48)
96  ENUM_CASE(INTEGER56)
97  ENUM_CASE(INTEGER64)
98  ENUM_CASE(UNSIGNED40)
99  ENUM_CASE(UNSIGNED48)
100  ENUM_CASE(UNSIGNED56)
101  ENUM_CASE(UNSIGNED64)
102  ENUM_CASE(PDO_COMMUNICATION_PARAMETER)
103  ENUM_CASE(PDO_MAPPING)
104  ENUM_CASE(SDO_PARAMETER)
105  ENUM_CASE(IDENTITY)
106  default:
107  return "[Utils::data_type_to_string] Unknown type with id "+std::to_string(static_cast<uint16_t>(type));
108  }
109  #undef ENUM_CASE
110 }
111 
112 uint8_t Utils::get_type_size(Type type) {
113  switch(type) {
114  case Type::uint8:
115  case Type::int8:
116  case Type::boolean:
117  return 1;
118  case Type::uint16:
119  case Type::int16:
120  return 2;
121  case Type::uint32:
122  case Type::int32:
123  case Type::real32:
124  return 4;
125  case Type::uint64:
126  case Type::int64:
127  case Type::real64:
128  return 8;
129  case Type::string:
130  case Type::octet_string:
131  default:
132  ERROR("[Utils::get_type_size] Unknown type or type with variable size.");
133  return 0;
134  }
135 }
136 
137 Type Utils::type_code_to_type(uint16_t code) {
138  switch (code) {
139  case (uint16_t) DataType::BOOLEAN: return Type::boolean;
140  case (uint16_t) DataType::INTEGER8: return Type::int8;
141  case (uint16_t) DataType::INTEGER16: return Type::int16;
142  case (uint16_t) DataType::INTEGER32: return Type::int32;
143  case (uint16_t) DataType::UNSIGNED8: return Type::uint8;
144  case (uint16_t) DataType::UNSIGNED16: return Type::uint16;
145  case (uint16_t) DataType::UNSIGNED32: return Type::uint32;
146  case (uint16_t) DataType::REAL32: return Type::real32;
147  case (uint16_t) DataType::VISIBLE_STRING: return Type::string;
148  case (uint16_t) DataType::OCTET_STRING: return Type::octet_string;
149  //case UNICODE_STRING: return Type::;
150  //case TIME_OF_DAY: return Type::;
151  //case TIME_DIFFERENCE: return Type::;
152  //case LARGEDATA: return Type::;
153  //case INTEGER24: return Type::;
154  case (uint16_t) DataType::REAL64: return Type::real64;
155  //case INTEGER40: return Type::;
156  //...
157  case (uint16_t) DataType::INTEGER64: return Type::int64;
158  case (uint16_t) DataType::UNSIGNED64: return Type::uint64;
159  //...
160  default:
161  ERROR("[Utils::type_code_to_type] Data type "<<data_type_to_string(static_cast<DataType>(code))<<" not yet supported.");
162  return Type::invalid;
163  }
164 }
165 
166 std::string Utils::escape(const std::string& str) {
167  std::string out = str;
168  std::transform(out.begin(), out.end(), out.begin(), ::tolower);
169  std::replace(out.begin(), out.end(), ' ', '_');
170  std::replace(out.begin(), out.end(), '-', '_');
171  return out;
172 }
173 
174 unsigned long long Utils::hexstr_to_uint(std::string str) {
175  try {
176  return std::stoull(str, nullptr, 16);
177  } catch ( const std::invalid_argument& e ) {
178  ERROR("[Utils::hexstr_to_uint] Invalid argument: \""<<str<<"\" ("<<e.what()<<")");
179  return 0;
180  } catch ( const std::out_of_range& e ) {
181  ERROR("[Utils::hexstr_to_uint] Out of range: \""<<str<<"\" ("<<e.what()<<")");
182  return 0;
183  }
184 }
185 
186 unsigned long long Utils::decstr_to_uint(std::string str) {
187  try {
188  return std::stoull(str, nullptr, 10);
189  } catch ( const std::invalid_argument& e ) {
190  ERROR("[Utils::decstr_to_uint] Invalid argument: \""<<str<<"\" ("<<e.what()<<")");
191  return 0;
192  } catch ( const std::out_of_range& e ) {
193  ERROR("[Utils::decstr_to_uint] Out of range: \""<<str<<"\" ("<<e.what()<<")");
194  return 0;
195  }
196 }
197 
198 AccessType Utils::string_to_access_type(std::string str) {
199  if (str == "ro") {
200  return AccessType::read_only;
201  } else if (str == "wo") {
202  return AccessType::write_only;
203  } else if (str == "const") {
204  return AccessType::constant;
205  } else if (str == "rw" || str == "rwr" || str == "rww") {
206  return AccessType::read_write;
207  } else {
208  ERROR("[Utils::str_to_access_type] Invalid access type string \""<<str<<"\". Returning AccessType::read_write.")
209  return AccessType::read_write;
210  }
211 }
212 
213 std::string Utils::access_type_to_string(AccessType type) {
214  switch(type) {
215  case AccessType::read_only: return "ro";
216  case AccessType::write_only: return "wo";
217  case AccessType::constant: return "const";
218  case AccessType::read_write: return "rw";
219  default:
220  ERROR("[Utils::access_type_to_string] Unknown access type!");
221  return "unknown access type";
222  }
223 }
224 
225 } // end namespace kaco
static std::string access_type_to_string(AccessType type)
Converts access types to a string.
Definition: utils.cpp:213
static unsigned long long hexstr_to_uint(std::string str)
Converts a string containing a hexadecimal numer to unsigned.
Definition: utils.cpp:174
static Type type_code_to_type(uint16_t code)
Maps type codes from an EDS file to a data type.
Definition: utils.cpp:137
static std::string data_type_to_string(DataType type)
Converts CanOpen data types to a string.
Definition: utils.cpp:75
static std::string escape(const std::string &str)
Converts entry names to lower case and replaces all spaces and '-' by underscores.
Definition: utils.cpp:166
static AccessType string_to_access_type(std::string str)
Converts a string representation of AccessType from an EDS file to AccessType.
Definition: utils.cpp:198
static std::string type_to_string(Type type)
Converts data types to a string.
Definition: utils.cpp:42
static unsigned long long decstr_to_uint(std::string str)
Converts a string containing a decimal numer to unsigned.
Definition: utils.cpp:186
static uint8_t get_type_size(Type type)
Returns the size of a data type in bytes.
Definition: utils.cpp:112