47 #include <arpa/inet.h>
55 #include <sys/socket.h>
64 sizeof(velodyne_msgs::VelodynePacket().data);
76 private_nh_(private_nh),
96 Input(private_nh, port)
106 sockfd_ = socket(PF_INET, SOCK_DGRAM, 0);
114 memset(&my_addr, 0,
sizeof(my_addr));
115 my_addr.sin_family = AF_INET;
116 my_addr.sin_port = htons(port);
117 my_addr.sin_addr.s_addr = INADDR_ANY;
121 if(setsockopt(
sockfd_, SOL_SOCKET, SO_REUSEADDR, &val,
sizeof(val)) == -1) {
126 if (bind(
sockfd_, (sockaddr *)&my_addr,
sizeof(sockaddr)) == -1)
132 if (fcntl(
sockfd_,F_SETFL, O_NONBLOCK|FASYNC) < 0)
152 struct pollfd fds[1];
154 fds[0].events = POLLIN;
155 static const int POLL_TIMEOUT = 1000;
157 sockaddr_in sender_address;
158 socklen_t sender_address_len =
sizeof(sender_address);
182 int retval = poll(fds, 1, POLL_TIMEOUT);
186 ROS_ERROR(
"poll() error: %s", strerror(errno));
191 ROS_WARN(
"Velodyne poll() timeout");
194 if ((fds[0].revents & POLLERR)
195 || (fds[0].revents & POLLHUP)
196 || (fds[0].revents & POLLNVAL))
198 ROS_ERROR(
"poll() reports Velodyne error");
201 }
while ((fds[0].revents & POLLIN) == 0);
205 ssize_t nbytes = recvfrom(
sockfd_, &pkt->data[0],
207 (sockaddr*) &sender_address,
208 &sender_address_len);
212 if (errno != EWOULDBLOCK)
225 && sender_address.sin_addr.s_addr !=
devip_.s_addr)
232 << nbytes <<
" bytes");
239 pkt->stamp =
ros::Time((time2 + time1) / 2.0 + time_offset);
261 double packet_rate, std::string filename,
262 bool read_once,
bool read_fast,
double repeat_delay):
263 Input(private_nh, port),
264 packet_rate_(packet_rate),
277 ROS_INFO(
"Read input file only once.");
279 ROS_INFO(
"Read input file as quickly as possible.");
281 ROS_INFO(
"Delay %.3f seconds before repeating input file.",
288 ROS_FATAL(
"Error opening Velodyne socket dump file.");
292 std::stringstream filter;
295 filter <<
"src host " <<
devip_str_ <<
" && ";
297 filter <<
"udp dst port " << port;
299 filter.str().c_str(), 1, PCAP_NETMASK_UNKNOWN);
311 struct pcap_pkthdr *
header;
312 const u_char *pkt_data;
317 if ((res = pcap_next_ex(
pcap_, &
header, &pkt_data)) >= 0)
347 ROS_WARN(
"Error %d reading Velodyne packet: %s",
348 res, pcap_geterr(
pcap_));
354 ROS_INFO(
"end of file reached -- done reading.");
360 ROS_INFO(
"end of file reached -- delaying %.3f seconds.",
365 ROS_DEBUG(
"replaying Velodyne dump file");