oem7_receiver.hpp
Go to the documentation of this file.
1 //
3 // Copyright (c) 2020 NovAtel Inc.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
24 
26 
27 #include <ros/ros.h>
28 
29 #include <boost/asio.hpp>
30 
31 
32 
33 namespace novatel_oem7_driver
34 {
38  template <typename T>
40  {
41  boost::asio::io_service io_;
42 
43  enum
44  {
46  };
47 
48 
49 
50  protected:
52 
54 
57 
59  {
60  if(num_io_errors_ >= max_num_io_errors_)
61  {
62  ROS_ERROR_STREAM("Oem7Receiver: Max Num IO errors exceeded: " << max_num_io_errors_);
63  return true;
64  }
65  else
66  {
67  return false;
68  }
69  }
70 
71 
75  virtual void endpoint_try_open() = 0;
76 
80  virtual size_t endpoint_read( boost::asio::mutable_buffer buf, boost::system::error_code& err) = 0;
81 
82 
86  virtual size_t endpoint_write(boost::asio::const_buffer buf, boost::system::error_code& err) = 0;
87 
92  {
93  boost::system::error_code err;
94  endpoint_.close(err);
95  ROS_ERROR_STREAM("Oem7Receiver: close error: " << err.value());
96  sleep(1.0);
97  }
98 
99  public:
101  io_(),
102  endpoint_(io_),
103  max_num_io_errors_(DEFAULT_MAX_NUM_IO_ERRORS),
104  num_io_errors_(0)
105  {
106  }
107 
108  virtual ~Oem7Receiver()
109  {
110  }
111 
112  virtual bool initialize(ros::NodeHandle& h)
113  {
114  nh_ = h;
115 
116  this->nh_.getParam("oem7_max_io_errors", max_num_io_errors_);
117 
118  return true;
119  }
120 
121  virtual bool read( boost::asio::mutable_buffer buf, size_t& rlen)
122  {
123  while(!ros::isShuttingDown() && !in_error_state())
124  {
126 
127  boost::system::error_code err;
128  size_t len = endpoint_read(buf, err);
129  if(err.value() == boost::system::errc::success)
130  {
131  num_io_errors_ = 0; // Reset error counter
132 
133  rlen = len;
134  return true;
135  }
136  // else: error condition
137 
138 
139  num_io_errors_++;
140 
141  ROS_ERROR_STREAM("Oem7Receiver: read error: " << err.value()
142  <<"; endpoint open: " << endpoint_.is_open()
143  <<" errors/max: " << num_io_errors_
144  <<"/" << max_num_io_errors_);
145  endpoint_close();
146  }
147 
148  return false;
149  }
150 
151  virtual bool write(boost::asio::const_buffer buf)
152  {
154  return false;
155 
157 
158  boost::system::error_code err;
159  endpoint_write(buf, err);
160  if(err.value() != boost::system::errc::success)
161  {
162  num_io_errors_++;
163 
164  ROS_ERROR_STREAM("Oem7Receiver: write error: " << err.value() << "; endpoint open: " << endpoint_.is_open());
165  endpoint_close();
166  return false;
167  }
168 
169  return true;
170  }
171 };
172 
173 }
174 
175 
int num_io_errors_
Number of consecuitive io errors.
virtual bool initialize(ros::NodeHandle &h)
virtual bool read(boost::asio::mutable_buffer buf, size_t &rlen)
T endpoint_
boost::asio communication endoint; socket, serial port, etc.
int max_num_io_errors_
Number of consecutive io errors before declaring failure and quitting.
virtual bool write(boost::asio::const_buffer buf)
boost::asio::io_service io_
ROSCPP_DECL bool isShuttingDown()
virtual size_t endpoint_write(boost::asio::const_buffer buf, boost::system::error_code &err)=0
virtual size_t endpoint_read(boost::asio::mutable_buffer buf, boost::system::error_code &err)=0
bool getParam(const std::string &key, std::string &s) const
#define ROS_ERROR_STREAM(args)


novatel_oem7_driver
Author(s):
autogenerated on Tue Mar 9 2021 03:48:00