ros_parameter_handler.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
21 #include <psen_scan/scanner_data.h>
22 #include <arpa/inet.h>
23 #include <algorithm>
24 
25 namespace psen_scan
26 {
37  : nh_(nh)
38  , host_ip_()
39  , host_udp_port_()
40  , frame_id_(DEFAULT_FRAME_ID)
41  , skip_(DEFAULT_SKIP)
42  , angle_start_(DEFAULT_ANGLE_START)
43  , angle_end_(DEFAULT_ANGLE_END)
44  , x_axis_rotation_(DEFAULT_X_AXIS_ROTATION)
45 {
47 }
48 
54 {
55  // required parameters first
56  // update parameter password
57  try
58  {
59  getRequiredParamFromParamServer<std::string>("password", password_);
60  try
61  {
63  }
64  catch (const DecryptPasswordException& e)
65  {
66  throw PSENScanFatalException("Invalid Password: " + std::string(e.what()));
67  }
68  }
69  catch (const GetROSParameterException& e)
70  {
71  ROS_WARN_STREAM(e.what());
72  throw PSENScanFatalException("Reading of required parameter failed!");
73  } // update parameter password
74  // update parameter host_ip
75  try
76  {
77  std::string host_ip;
78  getRequiredParamFromParamServer<std::string>("host_ip", host_ip);
79  in_addr_t ip_addr = inet_network(host_ip.c_str());
80  if (static_cast<in_addr_t>(-1) == ip_addr)
81  {
82  throw PSENScanFatalException("Host IP address conversion failed!");
83  }
84  host_ip_ = htobe32(ip_addr);
85  }
86  catch (const GetROSParameterException& e)
87  {
88  ROS_WARN_STREAM(e.what());
89  throw PSENScanFatalException("Reading of required parameter failed!");
90  } // update parameter host_ip
91  // update parameter host_udp_port
92  try
93  {
94  int host_udp_port;
95  getRequiredParamFromParamServer<int>("host_udp_port", host_udp_port);
96  if (host_udp_port < 0)
97  {
98  throw PSENScanFatalException("Parameter host_udp_port may not be negative!");
99  }
100  if (host_udp_port > 65535)
101  {
102  throw PSENScanFatalException("Parameter host_udp_port too large!");
103  }
104  host_udp_port_ = htole32(static_cast<uint32_t>(host_udp_port));
105  }
106  catch (const GetROSParameterException& e)
107  {
108  ROS_WARN_STREAM(e.what());
109  throw PSENScanFatalException("Reading of required parameter failed!");
110  } // update parameter host_udp_port
111  // update parameter sensor_ip
112  try
113  {
114  std::string sensor_ip;
115  getRequiredParamFromParamServer<std::string>("sensor_ip", sensor_ip);
116  in_addr_t ip_addr = inet_network(sensor_ip.c_str());
117  if (static_cast<in_addr_t>(-1) == ip_addr)
118  {
119  throw PSENScanFatalException("Sensor IP address conversion failed!");
120  }
121  sensor_ip_ = sensor_ip;
122  }
123  catch (const GetROSParameterException& e)
124  {
125  ROS_WARN_STREAM(e.what());
126  throw PSENScanFatalException("Reading of required parameter failed!");
127  } // update parameter sensor_ip
128 
129  // non-required parameters last
130  // update parameter frame_id
131  try
132  {
133  getOptionalParamFromParamServer<std::string>("frame_id", frame_id_);
134  }
135  catch (const GetROSParameterException& e)
136  {
137  ROS_WARN_STREAM(e.what());
138  throw PSENScanFatalException("Reading of required parameter failed!");
139  } // update parameter frame_id
140  // update parameter skip
141  try
142  {
143  int skip;
144  if (getOptionalParamFromParamServer<int>("skip", skip))
145  {
146  if (skip < 0)
147  {
148  throw PSENScanFatalException("Parameter skip may not be negative!");
149  }
150  skip_ = static_cast<uint16_t>(skip);
151  }
152  }
153  catch (const GetROSParameterException& e)
154  {
155  ROS_WARN_STREAM(e.what());
156  throw PSENScanFatalException("Reading of required parameter failed!");
157  } // update parameter skip
158  // update parameter angle_start
159  try
160  {
161  float angle_start;
162  if (getOptionalParamFromParamServer<float>("angle_start", angle_start))
163  {
164  if (PSENscanInternalAngle(Degree(angle_start)) < MIN_SCAN_ANGLE)
165  {
166  throw PSENScanFatalException("Parameter angle_start may not be negative!");
167  }
168  if (PSENscanInternalAngle(Degree(angle_start)) > MAX_SCAN_ANGLE)
169  {
170  throw PSENScanFatalException("Parameter angle_start too large!");
171  }
172  angle_start_ = PSENscanInternalAngle(Degree(angle_start));
173  }
174  }
175  catch (const GetROSParameterException& e)
176  {
177  ROS_WARN_STREAM(e.what());
178  throw PSENScanFatalException("Reading of required parameter failed!");
179  } // update parameter angle_start
180  // update parameter angle_end
181  try
182  {
183  float angle_end;
184  if (getOptionalParamFromParamServer<float>("angle_end", angle_end))
185  {
186  if (PSENscanInternalAngle(Degree(angle_end)) < MIN_SCAN_ANGLE)
187  {
188  throw PSENScanFatalException("Parameter angle_end may not be negative!");
189  }
190  if (PSENscanInternalAngle(Degree(angle_end)) > MAX_SCAN_ANGLE)
191  {
192  throw PSENScanFatalException("Parameter angle_end too large!");
193  }
195  }
196  }
197  catch (const GetROSParameterException& e)
198  {
199  ROS_WARN_STREAM(e.what());
200  throw PSENScanFatalException("Reading of required parameter failed!");
201  } // update parameter angle_end
202  // update parameter x_axis_rotation
203  try
204  {
205  double x_axis_rotation;
206  if (getOptionalParamFromParamServer<double>("x_axis_rotation", x_axis_rotation))
207  {
208  x_axis_rotation_ = Degree(x_axis_rotation);
210  {
211  throw PSENScanFatalException("Parameter x_axis_rotation is out of range. [-360.0 .. 360.0]");
212  }
213  }
214  }
215  catch (const GetROSParameterException& e)
216  {
217  ROS_WARN_STREAM(e.what());
218  throw PSENScanFatalException("Reading of required parameter failed!");
219  } // update parameter x_axis_rotation
220 }
221 
230 template <class T>
231 void RosParameterHandler::getRequiredParamFromParamServer(const std::string& key, T& param)
232 {
233  if (!nh_.hasParam(key))
234  {
235  throw GetROSParameterException("Parameter " + key + " doesn't exist on parameter server.");
236  }
237 
238  if (!nh_.getParam(key, param))
239  {
240  throw GetROSParameterException("Parameter " + key + " has wrong datatype on parameter server.");
241  }
242  return;
243 }
244 
254 template <class T>
255 bool RosParameterHandler::getOptionalParamFromParamServer(const std::string& key, T& param)
256 {
257  if (!nh_.hasParam(key))
258  {
259  ROS_WARN_STREAM("Parameter " + key + " doesn't exist on parameter server. Proceeding with default configuration.");
260  return false;
261  }
262  if (!nh_.getParam(key, param))
263  {
264  throw GetROSParameterException("Parameter " + key + " has wrong datatype on parameter server.");
265  }
266  return true;
267 }
268 
275 {
276  return password_;
277 }
278 
285 {
286  return host_ip_;
287 }
288 
295 {
296  return host_udp_port_;
297 }
298 
305 {
306  return sensor_ip_;
307 }
308 
315 {
316  return frame_id_;
317 }
318 
325 {
326  return skip_;
327 }
328 
335 {
336  return angle_start_;
337 }
338 
345 {
346  return angle_end_;
347 }
348 
355 {
356  return x_axis_rotation_;
357 }
358 
367 std::string RosParameterHandler::decryptPassword(const std::string& encrypted_password)
368 {
369  const int encrypted_char_len = 2;
370  const int encrypted_char_base = 16;
371  const int addition_coeff = 100; // arbitrary
372  const int number_of_ascii_chars = 256;
373  const int encryption_xor_key = 0xCD; // arbitrary
374 
375  std::string decrypted_password = "";
376 
377  std::string encrypted_pw_temp = encrypted_password;
378 
379  encrypted_pw_temp.erase(std::remove(encrypted_pw_temp.begin(), encrypted_pw_temp.end(), ' '),
380  encrypted_pw_temp.end());
381 
382  if (encrypted_pw_temp.length() % 2 != 0)
383  {
384  throw DecryptPasswordException("Password length must be even!");
385  }
386 
387  for (unsigned int i = 0; i < encrypted_pw_temp.length(); i += encrypted_char_len)
388  {
389  char c;
390  try
391  {
392  c = (std::stoi(encrypted_pw_temp.substr(i, encrypted_char_len), nullptr, encrypted_char_base) -
393  i * addition_coeff / encrypted_char_len) %
394  number_of_ascii_chars ^
395  encryption_xor_key;
396  }
397  catch (const std::invalid_argument& e)
398  {
399  throw DecryptPasswordException(e.what());
400  }
401 
402  if (c < 32)
403  {
404  throw DecryptPasswordException("Control characters not allowed!");
405  }
406 
407  decrypted_password += c;
408  }
409 
410  return decrypted_password;
411 }
412 
413 } // namespace psen_scan
PSENscanInternalAngle getAngleEnd() const
Getter Method for angle_end_.
bool getOptionalParamFromParamServer(const std::string &key, T &param)
Gets one optional ROS-parameter from parameter server.
static const uint16_t DEFAULT_SKIP
Class to model angles in degrees from user&#39;s perspective.
PSENscanInternalAngle const MAX_SCAN_ANGLE(2750)
static const Degree DEFAULT_X_AXIS_ROTATION(137.5)
Degree getXAxisRotation() const
Getter Method for x_axis_rotation_.
PSENscanInternalAngle getAngleStart() const
Getter Method for angle_start_.
static const Degree MAX_X_AXIS_ROTATION(360.0)
static std::string decryptPassword(const std::string &encrypted_password)
Decrypt password.
std::string getSensorIP() const
Getter Method for sensor_ip_.
uint16_t getSkip() const
Getter method for skip_.
bool getParam(const std::string &key, std::string &s) const
PSENscanInternalAngle const MIN_SCAN_ANGLE(0)
static const Degree MIN_X_AXIS_ROTATION(-360.0)
#define ROS_WARN_STREAM(args)
bool hasParam(const std::string &key) const
void getRequiredParamFromParamServer(const std::string &key, T &param)
Gets one required ROS-parameter from parameter server.
uint32_t getHostUDPPort() const
Getter method for host_udp_port_.
uint32_t getHostIP() const
Getter method for host_ip_.
std::string getPassword() const
Getter method for password_.
static const std::string DEFAULT_FRAME_ID
std::string getFrameID() const
Getter method for frame_id_.
Class to model angles in PSENscan internal format (tenth of degrees)
RosParameterHandler(const ros::NodeHandle &nh)
Construct a new Ros Parameter Handler:: Ros Parameter Handler object.
static const PSENscanInternalAngle DEFAULT_ANGLE_END(2750)
void updateAllParamsFromParamServer()
Update all Parameters from ROS Parameter Server.
static const PSENscanInternalAngle DEFAULT_ANGLE_START(0)


psen_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:16:20