device.h
Go to the documentation of this file.
1 
41 class CoLaFrame {
42  std::vector<char> buffer_;
43 
44  uint8_t checksum() {
45  uint8_t c=0;
46  for(size_t i=8; i<buffer_.size(); i++)
47  c^=buffer_[i];
48  return c;
49  }
50 
51 public:
52  CoLaFrame() : buffer_(8, (char)0) {
53  }
54 
55  CoLaFrame(const char *str) : buffer_(8+strlen(str), (char)0) {
56  int i=8;
57  while(*str)
58  buffer_[i++] = *(str++);
59  }
60 
61  std::vector<char> finish() {
62  char *data = &buffer_[0];
63  *(uint32_t*)(data+0) = htonl(0x02020202);
64  *(uint32_t*)(data+4) = htonl(buffer_.size()-8);
65 
66  buffer_.push_back(checksum());
67 
68  return buffer_;
69  }
70 };
71 
72 
73 /* all methods that use the control channel (sopas) */
74 class Control : public TCP_Session {
76  const std::string remote_device_ip_;
78 
79  void on_data(const char *data, const size_t size, Any_Session *writer)
80  {
81  ROS_DEBUG("got data for control");
82  }
83 public:
84  Control(boost::asio::io_service& io_service, const std::string &remote_device_ip) :
85  TCP_Session(io_service, on_data_),
86  remote_device_ip_(remote_device_ip),
87  stream_started_(false)
88  {
89  on_data_.connect( boost::bind(&Control::on_data, this, _1, _2, _3) );
90  }
91 
92  /* establish the control channel to the device */
93  bool open() {
94  ROS_DEBUG("Connecting to device...");
95 
96  if(!connect(remote_device_ip_, "2112")) {
97  ROS_ERROR("Error on connecting to %s", remote_device_ip_.c_str());
98  return false;
99  }
100 
101  ROS_DEBUG("done.");
102  return true;
103  }
104 
105  /* Tells the device that there is a streaming channel by invoking a
106  * method named GetBlobClientConfig.
107  */
108  bool initStream() {
109 
110  ROS_DEBUG("Initializing streaming...");
111  CoLaFrame fr("sMN GetBlobClientConfig");
112  ROS_DEBUG("Sending on sMN GetBlobClientConfig");
113  write(fr.finish());
114  ROS_DEBUG("done.");
115 
116  return true;
117  }
118 
119  /* Start streaming the data by calling the "PLAYSTART" method on the
120  * device and sending a "Blob request" afterwards.
121  */
122  bool startStream() {
123  if(stream_started_) return true;
124 
125  CoLaFrame fr("sMN PLAYSTART");
126  ROS_DEBUG("Sending on sopas sMN PLAYSTART");
127  write(fr.finish());
128  stream_started_ = true;
129 
130  return true;
131  }
132 
133 
134  /* Stops the data stream. */
135  bool stopStream() {
136  if(!stream_started_) return true;
137 
138  CoLaFrame fr("sMN PLAYSTOP");
139  ROS_DEBUG("Sending on sopas sMN PLAYSTOP");
140  write(fr.finish());
141  stream_started_ = false;
142 
143  return true;
144  }
145 };
146 
147 
148 
149 /* All methods that use the streaming channel. */
150 class Streaming : public TCP_Session {
151 public:
152 
153  struct SFrame {
154  std::vector<char> buffer;
155 
157  };
158 
159  typedef boost::signals2::signal<void (const boost::shared_ptr<Data> &)> SIG_ON_FRAME;
160 
161 private:
162  const std::string remote_device_ip_;
167 
168 public:
169 
170  Streaming(boost::asio::io_service& io_service, const std::string &remote_device_ip) :
171  TCP_Session(io_service, on_data_),
172  remote_device_ip_(remote_device_ip),
173  debugOutput_(false)
174  {
175  on_data_.connect( boost::bind(&Streaming::on_data, this, _1, _2, _3) );
176  }
177 
178  SIG_ON_FRAME &getSignal() {return on_frame_;}
179 
180  bool &debugFlag() {return debugOutput_;}
181  bool debugFlag() const {return debugOutput_;}
182 
183  /* Opens the streaming channel. */
184  bool openStream() {
185  ROS_DEBUG("Opening streaming socket...");
186  if(!connect(remote_device_ip_, "2113")) {
187  ROS_DEBUG("Error on connecting to %s", remote_device_ip_.c_str());
188  return false;
189  }
190  ROS_DEBUG("done.");
191 
192  // saying hello
193  const char HEARTBEAT_MSG[] = "BlbReq";
194 
195  ROS_DEBUG("Sending BlbReq: %s", HEARTBEAT_MSG);
196  write(std::string(HEARTBEAT_MSG, sizeof(HEARTBEAT_MSG)-1));
197 
198  return true;
199  }
200 
201  /* Closes the streaming channel. */
202  bool closeStream() {
203  ROS_DEBUG("Closing streaming connection...");
204  close();
205  cur_frame_.reset();
206  ROS_DEBUG("done.");
207 
208  return true;
209  }
210 
211  /*
212  * Receives the raw data frame from the device via the streamingbchannel.
213  */
214  void on_data(const char *data, const size_t size, Any_Session *writer)
215  {
216  if(debugOutput_)
217  ROS_DEBUG("Reading image from stream...");
218 
219  if(!data || size<0) {
220  ROS_DEBUG("Socket not connected, terminating.");
221  return;
222  }
223 
224  if(cur_frame_) {
225  cur_frame_->buffer.insert(cur_frame_->buffer.end(), data, data+size );
226 
227  bool goon=true;
228  while(goon) {
229  goon=false;
230 
231  if(Data::check_header(&cur_frame_->buffer[0], cur_frame_->buffer.size())) {
232  boost::shared_ptr<Data> parser(new Data);
233  const size_t actual_size = parser->actual_size(&cur_frame_->buffer[0], cur_frame_->buffer.size());
234  if(parser->read(&cur_frame_->buffer[0], actual_size)) {
235  on_frame_(parser);
236  goon=true;
237  }
238  else {
239  cur_frame_.reset();
240  ROS_ERROR("failed to parse frame");
241  break;
242  }
243 
244  cur_frame_->buffer.erase(cur_frame_->buffer.begin(), cur_frame_->buffer.begin()+actual_size);
245  }
246  }
247  }
248  else {
249  cur_frame_.reset(new SFrame());
250  on_data(data, size, writer);
251  }
252 
253  if(debugOutput_)
254  ROS_DEBUG("done.");
255  }
256 };
static bool check_header(const char *data, const size_t size)
Definition: data.h:114
abstract connection handler to process incoming data (independent of protocol)
Definition: network.h:53
bool stopStream()
Definition: device.h:135
const std::string remote_device_ip_
Definition: device.h:76
bool closeStream()
Definition: device.h:202
bool openStream()
Definition: device.h:184
bool debugOutput_
Definition: device.h:165
boost::shared_ptr< SFrame > Ptr
Definition: device.h:156
bool open()
Definition: device.h:93
bool startStream()
Definition: device.h:122
boost::signals2::signal< void(const char *data, const size_t size, Any_Session *writer)> SIG_ON_DATA
incoming data with "data" of size "size" and handler to write back ("writer")
Definition: network.h:57
uint8_t checksum()
Definition: device.h:44
SIG_ON_FRAME & getSignal()
Definition: device.h:178
void on_data(const char *data, const size_t size, Any_Session *writer)
Definition: device.h:214
bool stream_started_
Definition: device.h:77
boost::signals2::signal< void(const boost::shared_ptr< Data > &)> SIG_ON_FRAME
Definition: device.h:159
SIG_ON_FRAME on_frame_
Definition: device.h:164
CoLaFrame(const char *str)
Definition: device.h:55
bool & debugFlag()
Definition: device.h:180
bool debugFlag() const
Definition: device.h:181
Any_Session::SIG_ON_DATA on_data_
signal handler for incoming data
Definition: device.h:75
std::vector< char > buffer
Definition: device.h:154
std::vector< char > buffer_
Definition: device.h:42
CoLaFrame()
Definition: device.h:52
Streaming(boost::asio::io_service &io_service, const std::string &remote_device_ip)
Definition: device.h:170
virtual bool connect(const std::string &path, const std::string &service)
Definition: network.h:95
Any_Session::SIG_ON_DATA on_data_
signal handler for incoming data
Definition: device.h:163
Definition: device.h:74
const std::string remote_device_ip_
Definition: device.h:162
bool initStream()
Definition: device.h:108
Definition: data.h:87
Control(boost::asio::io_service &io_service, const std::string &remote_device_ip)
Definition: device.h:84
#define ROS_ERROR(...)
std::vector< char > finish()
Definition: device.h:61
void on_data(const char *data, const size_t size, Any_Session *writer)
Definition: device.h:79
implementation connection handler for TCP
Definition: network.h:77
SFrame::Ptr cur_frame_
Definition: device.h:166
#define ROS_DEBUG(...)


sick_visionary_t_driver
Author(s): Joshua Hampp
autogenerated on Mon Jun 10 2019 15:09:27