-
Notifications
You must be signed in to change notification settings - Fork 8
/
rosbag.go
255 lines (226 loc) · 6.59 KB
/
rosbag.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package goros
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
const (
MessageData = 0x02
BagHeader = 0x03
IndexData = 0x04
ChunkRecord = 0x05
ChunkInfo = 0x06
ConnectionRecord = 0x07
)
var (
typeMap = map[string]interface{}{
"bool": reflect.TypeOf((*bool)(nil)).Elem(),
"float32": reflect.TypeOf((*float32)(nil)).Elem(),
"float64": reflect.TypeOf((*float64)(nil)).Elem(),
"uint32": reflect.TypeOf((*uint32)(nil)).Elem(),
"uint64": reflect.TypeOf((*uint32)(nil)).Elem(),
"int32": reflect.TypeOf((*int32)(nil)).Elem(),
"int64": reflect.TypeOf((*int64)(nil)).Elem(),
"uint8": reflect.TypeOf((*byte)(nil)).Elem(),
"time": reflect.TypeOf((*RosTime)(nil)).Elem(),
"string": reflect.TypeOf((*string)(nil)).Elem(),
}
)
type CustomMessage map[string]interface{}
type CustomMessageSlice struct {
CustomMessage
}
type RosTime struct {
Secs int32
NSecs int32
}
func parseSubMessageDefinition(subMessage string, superMessageTypeString string) {
commentRegexp, _ := regexp.Compile("\\s*#[^\n]*")
subMessageWithoutComments := commentRegexp.ReplaceAllString(subMessage, "")
subMessageMap := make(CustomMessage)
var subMessageTypeString string
for _, line := range strings.Split(subMessageWithoutComments, "\n") {
if len(line) != 0 {
if strings.HasPrefix(line, "MSG") {
subMessageTypeString = strings.TrimPrefix(line, "MSG: ")
continue
}
split := strings.Split(line, " ")
typeString := split[0]
fieldName := split[1]
if strings.HasSuffix(typeString, "[]") {
elementType := typeMap[typeString[:len(typeString)-2]]
switch elementType.(type) {
case reflect.Type:
subMessageMap[fieldName] = reflect.SliceOf(elementType.(reflect.Type))
case CustomMessage:
subMessageMap[fieldName] = elementType
}
} else {
subMessageMap[fieldName] = typeMap[typeString]
}
}
}
//return subMessageTypeString, subMessageMap
if len(subMessageTypeString) == 0 {
subMessageTypeString = superMessageTypeString
}
typeString := strings.Split(subMessageTypeString, "/")[1]
typeMap[typeString] = subMessageMap
log.Printf("%s, %s", typeString, subMessageMap)
}
func parseMessageDefinition(dataMap map[string]interface{}) {
//fmt.Println(dataMap)
messageSplitterRegex, _ := regexp.Compile("[=]+\n")
subMessages := messageSplitterRegex.Split(dataMap["message_definition"].(string), -1)
//fmt.Println(subMessages)
/*
for _, line := range strings.Split(dataMap["message_definition"].(string), "\n") {
fmt.Println(line)
}
*/
for _, subMessage := range subMessages {
//defer fmt.Println(subMessage)
defer parseSubMessageDefinition(subMessage, dataMap["type"].(string)) //log.Println(subMessage)
}
}
func parseRecordHeader(buffer []byte) map[string]interface{} {
//log.Println(string(buffer))
reader := bufio.NewReader(bytes.NewReader(buffer))
//log.Println(reader.Buffered())
valueMap := make(map[string]interface{})
var fieldLength uint32
for binary.Read(reader, binary.LittleEndian, &fieldLength) == nil {
fieldName, err := reader.ReadString('=')
fieldName = fieldName[:len(fieldName)-1]
//log.Println(fieldName)
//reader.Discard(int(fieldLength) - len(fieldName))
valueLength := int(fieldLength) - len(fieldName) - 1
switch fieldName {
case "op":
//var value byte
//binary.Read(reader, binary.LittleEndian, value)
value, _ := reader.ReadByte()
valueMap[fieldName] = value
case "conn", "size", "conn_count", "chunk_count", "count", "ver":
var value uint32
err = binary.Read(reader, binary.LittleEndian, &value)
valueMap[fieldName] = value
case "index_pos":
var value uint64
err = binary.Read(reader, binary.LittleEndian, &value)
valueMap[fieldName] = value
case "time", "start_time", "end_time", "chunk_pos":
var timestamp RosTime
err = binary.Read(reader, binary.LittleEndian, ×tamp)
valueMap[fieldName] = time.Unix(int64(timestamp.Secs), int64(timestamp.NSecs))
case "compression", "topic", "callerid", "type", "md5sum", "message_definition":
value := make([]byte, valueLength)
_, err = io.ReadFull(reader, value)
valueMap[fieldName] = string(value)
case "latching":
value := make([]byte, valueLength)
_, err = io.ReadFull(reader, value)
intVal, _ := strconv.Atoi(string(value))
valueMap[fieldName] = intVal
default:
value := make([]byte, valueLength)
_, err = io.ReadFull(reader, value)
valueMap[fieldName] = string(value)
log.Printf("Congratulations! I don't know what to do with %s", fieldName)
}
if err != nil {
panic(err)
}
}
return valueMap
}
func parseRecord(reader *bufio.Reader) error {
var headerLength uint32
err := binary.Read(reader, binary.LittleEndian, &headerLength)
if err != nil {
return err
}
if headerLength == 0 {
panic(fmt.Sprintf("cannot read header of size %d", headerLength))
}
//log.Println(headerLength)
buffer := make([]byte, int(headerLength))
io.ReadFull(reader, buffer)
valueMap := parseRecordHeader(buffer)
fmt.Println(valueMap)
var dataLength uint32
binary.Read(reader, binary.LittleEndian, &dataLength)
//log.Println(dataLength)
//if skip {
//reader.Discard(int(dataLength))
var dataBuffer []byte
if valueMap["op"] != MessageData {
dataBuffer = make([]byte, int(dataLength))
bytesRead, _ := io.ReadFull(reader, dataBuffer)
//log.Println(bytesRead)
if bytesRead != int(dataLength) {
panic(fmt.Sprintf("expected %d bytes, got %d", dataLength, bytesRead))
}
} else {
reader.Discard(int(dataLength))
}
/*} else {
buffer := make([]byte, int(dataLength))
reader.Read(buffer)
log.Println(string(buffer))
}
*/
/*
switch valueMap["op"].(byte) {
case BagHeader:
case ChunkRecord:
chunkReader := bufio.NewReader(bytes.NewReader(dataBuffer))
var chunkErr error
for chunkErr == nil {
chunkErr = parseRecord(chunkReader)
}
case ConnectionRecord:
fmt.Println(valueMap)
//log.Println(string(dataBuffer))
dataMap := parseRecordHeader(dataBuffer)
//log.Println(dataMap)
parseMessageDefinition(dataMap)
case MessageData:
//fmt.Println(valueMap)
case IndexData:
case ChunkInfo:
}
*/
return nil
}
func ParseRosbag(path string) {
log.Println(path)
file, err := os.Open(path) // For read access.
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := bufio.NewReader(file)
line, _ := reader.ReadString('\n')
log.Print(line)
//parseRecord(reader)
//parseRecord(reader)
var recordError error
for recordError == nil {
recordError = parseRecord(reader)
}
/*chunkHeader := make([]byte, 4096)
_, _ = file.Read(chunkHeader)
log.Println(string(buffer))
*/
}