23#include <sys/socket.h>
35Input::Input(uint16_t port, std::string lidar_ip,
int packet_size) {
37 inet_aton(lidar_ip.c_str(), &
devip_);
49 Input(port, lidar_ip, packet_size) {
52 sockfd_ = socket(PF_INET, SOCK_DGRAM, 0);
54 AERROR <<
"socket open error";
58 inet_aton(lidar_ip.c_str(), &
devip_);
59 sockaddr_in myAddress;
60 memset(&myAddress, 0,
sizeof(myAddress));
61 myAddress.sin_family = AF_INET;
62 myAddress.sin_port = htons(port);
63 myAddress.sin_addr.s_addr = INADDR_ANY;
66 reinterpret_cast<sockaddr *
>(&myAddress),
69 AERROR <<
"bind error, port:" << port;
73 if (fcntl(
sockfd_, F_SETFL, O_NONBLOCK | FASYNC) < 0) {
87 fds[0].events = POLLIN;
90 sockaddr_in sender_address;
91 socklen_t sender_address_len =
sizeof(sender_address);
101 AERROR <<
"poll() error: " << strerror(errno);
106 AERROR <<
"lslidar poll() timeout, port: " <<
port_;
109 if ((fds[0].revents & POLLERR) || (fds[0].revents & POLLHUP)
110 || (fds[0].revents & POLLNVAL)) {
111 AWARN <<
"poll() reports lslidar error";
114 }
while ((fds[0].revents & POLLIN) == 0);
116 ssize_t nbytes = recvfrom(
121 reinterpret_cast<sockaddr *
>(&sender_address),
122 &sender_address_len);
124 if (errno != EWOULDBLOCK) {
129 if (sender_address.sin_addr.s_addr !=
devip_.s_addr) {
130 AERROR <<
"lidar IP parameter set error,please reset in config "
138 AERROR <<
"incomplete lslidar packet read: " << nbytes <<
" bytes";
150 std::string lidar_ip,
153 std::string filename,
156 double repeat_delay) :
157 Input(port, lidar_ip, packet_size),
158 packet_rate_(packet_rate),
159 filename_(filename) {
162 packet_rate_ = packet_rate;
163 lidar_ip_ = lidar_ip;
165 read_once_ = read_once;
166 read_fast_ = read_fast;
167 repeat_delay_ = repeat_delay;
170 AINFO <<
"Read input file only once.";
172 AINFO <<
"Read input file as quickly as possible.";
173 if (repeat_delay_ > 0.0)
174 AINFO <<
"Delay %.3f seconds before repeating input file."
178 AERROR <<
"Opening PCAP file " << filename_;
179 if ((pcap_ = pcap_open_offline(filename_.c_str(), errbuf_)) == NULL) {
180 AERROR <<
"Error opening lslidar socket dump file.";
184 std::stringstream filter;
185 if (lidar_ip !=
"") {
186 filter <<
"src host " << lidar_ip <<
" && ";
188 filter <<
"udp dst port " << port;
191 &pcap_packet_filter_,
192 filter.str().c_str(),
194 PCAP_NETMASK_UNKNOWN);
204 struct pcap_pkthdr *header;
205 const u_char *pkt_data;
210 if ((res = pcap_next_ex(pcap_, &header, &pkt_data)) >= 0) {
213 if (!lidar_ip_.empty()
215 == pcap_offline_filter(
216 &pcap_packet_filter_, header, pkt_data)))
220 if (read_fast_ ==
false)
221 usleep(
static_cast<int>(1000 * 1000 / packet_rate_ / 1.1));
237 AINFO <<
"Error " << res
238 <<
" reading lslidar packet: " << pcap_geterr(pcap_);
243 AINFO <<
"end of file reached -- done reading.";
247 if (repeat_delay_ > 0.0) {
248 AINFO <<
"end of file reached -- delaying" << repeat_delay_
250 usleep(rint(repeat_delay_ * 1000000.0));
253 AINFO <<
"replaying lslidar dump file";
259 pcap_ = pcap_open_offline(filename_.c_str(), errbuf_);
Cyber has builtin time type Time.
uint64_t ToNanosecond() const
convert time to nanosecond.
static Time Now()
get the current time.
double ToSecond() const
convert time to second.