forked from victl/VeloSLAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvtkPacketFileWriter.cxx
More file actions
168 lines (144 loc) · 4.77 KB
/
vtkPacketFileWriter.cxx
File metadata and controls
168 lines (144 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2013 Velodyne Acoustics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "vtkPacketFileWriter.h"
#include "type_defs.h"
#ifdef _MSC_VER
#include <windows.h>
namespace {
int gettimeofday(struct timeval * tp, void *)
{
FILETIME ft;
::GetSystemTimeAsFileTime( &ft );
long long t = (static_cast<long long>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
t -= 116444736000000000LL;
t /= 10; // microseconds
tp->tv_sec = static_cast<long>( t / 1000000UL);
tp->tv_usec = static_cast<long>( t % 1000000UL);
return 0;
}
}
#endif
#include <cstring>
//--------------------------------------------------------------------------------
const unsigned short vtkPacketFileWriter::LidarPacketHeader[21] = {
0xffff, 0xffff, 0xffff, 0x7660,
0x0088, 0x0000, 0x0008, 0x0045,
0xd204, 0x0000, 0x0040, 0x11ff,
0xaab4, 0xa8c0, 0xc801, 0xffff, // checksum 0xa9b4 //source ip 0xa8c0, 0xc801 is 192.168.1.200
0xffff, 0x4009, 0x4009, 0xbe04, 0x0000};
//--------------------------------------------------------------------------------
const unsigned short vtkPacketFileWriter::PositionPacketHeader[21] = {
0xffff, 0xffff, 0xffff, 0x7660,
0x0088, 0x0000, 0x0008, 0x0045,
0xd204, 0x0000, 0x0040, 0x11ff,
0xaab4, 0xa8c0, 0xc801, 0xffff, // checksum 0xa9b4 //source ip 0xa8c0, 0xc801 is 192.168.1.200
0xffff, 0x7420, 0x7420, 0x0802, 0x0000};
//--------------------------------------------------------------------------------
vtkPacketFileWriter::vtkPacketFileWriter()
{
this->PCAPFile = 0;
this->PCAPDump = 0;
}
//--------------------------------------------------------------------------------
vtkPacketFileWriter::~vtkPacketFileWriter()
{
this->close();
}
//--------------------------------------------------------------------------------
bool vtkPacketFileWriter::open(const std::string& filename)
{
this->PCAPFile = pcap_open_dead(DLT_EN10MB, 65535);
this->PCAPDump = pcap_dump_open(this->PCAPFile, filename.c_str());
if (!this->PCAPDump)
{
this->LastError = pcap_geterr(this->PCAPFile);
pcap_close(this->PCAPFile);
this->PCAPFile = 0;
return false;
}
this->FileName = filename;
return true;
}
//--------------------------------------------------------------------------------
bool vtkPacketFileWriter::isOpen()
{
return (this->PCAPFile != 0);
}
void vtkPacketFileWriter::close()
{
if (this->PCAPFile)
{
pcap_dump_close(this->PCAPDump);
pcap_close(this->PCAPFile);
this->PCAPFile = 0;
this->PCAPDump = 0;
this->FileName.clear();
}
}
//--------------------------------------------------------------------------------
const std::string& vtkPacketFileWriter::GetLastError()
{
return this->LastError;
}
//--------------------------------------------------------------------------------
const std::string& vtkPacketFileWriter::GetFileName()
{
return this->FileName;
}
//--------------------------------------------------------------------------------
bool vtkPacketFileWriter::writePacket(const unsigned char* data, unsigned int dataLength, ptime t)
{
if (!this->PCAPFile)
{
return false;
}
const unsigned short* headerData;
struct pcap_pkthdr header;
std::vector<unsigned char> packetBuffer;
if (dataLength == 1206)
{
headerData = LidarPacketHeader;
header.caplen = 1248;
header.len = 1248;
packetBuffer.resize(1248);
}
else if(dataLength == (554 - 42))
{
headerData = PositionPacketHeader;
header.caplen = 554;
header.len = 554;
packetBuffer.resize(554);
}
else
{
return false;
}
struct timeval currentTime;
if (t.is_special()){ // Means caller didn't provided it (default constructed)
gettimeofday(¤tTime, NULL);
} else {
ptimeToTimeval(t, currentTime);
}
header.ts = currentTime;
memcpy(&(packetBuffer[0]), headerData, 42);
memcpy(&(packetBuffer[0]) + 42, data, dataLength);
pcap_dump((u_char *)this->PCAPDump, &header, &(packetBuffer[0]));
return true;
}
//--------------------------------------------------------------------------------
bool vtkPacketFileWriter::writePacket(pcap_pkthdr* packetHeader, unsigned char* packetData)
{
pcap_dump((u_char *)this->PCAPDump, packetHeader, packetData);
return true;
}