/
ros2db3_to_mcap.go
367 lines (344 loc) · 8.95 KB
/
ros2db3_to_mcap.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package ros
import (
"bytes"
"database/sql"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/foxglove/mcap/go/mcap"
)
var (
messageTopicRegex = regexp.MustCompile(`\w+/msg/.*`)
errSchemaNotFound = errors.New("schema not found")
)
func getSchema(rosType string, directories []string) ([]byte, error) {
parts := strings.FieldsFunc(rosType, func(c rune) bool { return c == '/' })
if len(parts) < 3 {
return nil, fmt.Errorf("expected type %s to match <package>/msg/<type>", rosType)
}
baseType := parts[2]
rosPkg := parts[0]
for _, dir := range directories {
schemaIndexPath := path.Join(
dir, "share", "ament_index",
"resource_index", "rosidl_interfaces", rosPkg, // cspell:disable-line
)
schemaIndex, err := os.ReadFile(schemaIndexPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
return nil, fmt.Errorf("failed to read schema index: %w", err)
}
lines := strings.Split(string(schemaIndex), "\n")
for _, line := range lines {
expectedMsgDefFilename := baseType + ".msg"
if _, filename := filepath.Split(line); filename == expectedMsgDefFilename {
schemaPath := path.Join(dir, "share", rosPkg, line)
schema, err := os.ReadFile(schemaPath)
if err != nil {
return nil, fmt.Errorf("failed to read schema: %w", err)
}
return schema, nil
}
}
}
return nil, errSchemaNotFound
}
func getSchemas(directories []string, types []string) (map[string][]byte, error) {
messageDefinitions := make(map[string][]byte)
for _, rosType := range types {
rosPackage := strings.Split(rosType, "/")[0]
messageDefinition := &bytes.Buffer{}
schema, err := getSchema(rosType, directories)
if err != nil {
return nil, fmt.Errorf("failed to find schema for %s: %w", rosType, err)
}
subdefinitions := []struct {
parentPackage string
rosType string
schema []byte
}{
{parentPackage: rosPackage, rosType: rosType, schema: schema},
}
seenSubtypes := map[string]struct{}{rosType: {}}
first := true
for len(subdefinitions) > 0 {
subdefinition := subdefinitions[0]
if !first {
// if the previous write did not end with a newline, add one now
if messageDefinition.Bytes()[messageDefinition.Len()-1] != '\n' {
err := messageDefinition.WriteByte('\n')
if err != nil {
return nil, fmt.Errorf("failed to write newline")
}
}
_, err := messageDefinition.Write(MessageDefinitionSeparator)
if err != nil {
return nil, fmt.Errorf("failed to write separator: %w", err)
}
_, err = fmt.Fprintf(messageDefinition, "MSG: %s\n", strings.Replace(subdefinition.rosType, "/msg/", "/", 1))
if err != nil {
return nil, fmt.Errorf("failed to write MSG header to message definition: %w", err)
}
}
_, err = messageDefinition.Write(subdefinition.schema)
if err != nil {
return nil, fmt.Errorf("failed to write subdefinition: %w", err)
}
first = false
subdefinitions = subdefinitions[1:]
lines := strings.FieldsFunc(string(subdefinition.schema), func(c rune) bool { return c == '\n' })
for _, line := range lines {
line = strings.TrimSpace(line)
// skip empty lines
if line == "" {
continue
}
// skip comments
if strings.HasPrefix(line, "#") {
continue
}
// must be a field
parts := strings.FieldsFunc(line, func(c rune) bool { return c == ' ' })
if len(parts) < 1 {
return nil, fmt.Errorf("malformed field: %s. Message definition: %s", line, string(subdefinition.schema))
}
fieldType := parts[0]
// bounded fields & arrays
if i := strings.Index(fieldType, "["); i > 0 {
fieldType = fieldType[:i]
}
if i := strings.Index(fieldType, "<"); i > 0 {
fieldType = fieldType[:i]
}
// if it's a primitive, no action required
if Primitives[fieldType] {
continue
}
parentPackage := subdefinition.parentPackage
if parts := strings.Split(subdefinition.rosType, "/"); len(parts) > 1 {
parentPackage = parts[0]
}
// if it's not a primitive, we need to look it up
qualifiedType := fieldToQualifiedROSType(fieldType, parentPackage)
fieldSchema, err := getSchema(qualifiedType, directories)
if err != nil {
return nil, fmt.Errorf("failed to find schema for %s: %w", fieldType, err)
}
// Only process new subtypes to avoid duplicate type definitions in the schema
_, knownSubtype := seenSubtypes[qualifiedType]
if !knownSubtype {
seenSubtypes[qualifiedType] = struct{}{}
subdefinitions = append(subdefinitions, struct {
parentPackage string
rosType string
schema []byte
}{
parentPackage: parentPackage,
rosType: qualifiedType,
schema: fieldSchema,
})
}
}
}
messageDefinitions[rosType] = messageDefinition.Bytes()
}
return messageDefinitions, nil
}
func fieldToQualifiedROSType(fieldType, rosPackage string) string {
parts := strings.FieldsFunc(fieldType, func(c rune) bool { return c == '/' })
if len(parts) == 1 {
return path.Join(rosPackage, "msg", fieldType)
}
return path.Join(parts[0], "msg", parts[1])
}
type topicsRecord struct {
id uint16
name string
typ string
serializationFormat string
offeredQOSProfiles *string
}
func checkHasQOSProfiles(db *sql.DB) (bool, error) {
var count int
err := db.QueryRow(
`select count(*) from pragma_table_info('topics') where name = 'offered_qos_profiles'`,
).Scan(&count)
if err != nil {
return false, err
}
return count > 0, nil
}
func getTopics(db *sql.DB) ([]topicsRecord, error) {
hasQOSProfiles, err := checkHasQOSProfiles(db)
if err != nil {
return nil, err
}
var rows *sql.Rows
if hasQOSProfiles {
rows, err = db.Query(
`select id, name, type, serialization_format, offered_qos_profiles from topics`,
)
} else {
rows, err = db.Query(
`select id, name, type, serialization_format from topics`,
)
}
if err != nil {
return nil, err
}
defer rows.Close()
topics := []topicsRecord{}
for rows.Next() {
record := topicsRecord{}
if hasQOSProfiles {
err = rows.Scan(
&record.id,
&record.name,
&record.typ,
&record.serializationFormat,
&record.offeredQOSProfiles,
)
} else {
err = rows.Scan(
&record.id,
&record.name,
&record.typ,
&record.serializationFormat,
)
}
if err != nil {
return nil, err
}
if messageTopicRegex.MatchString(record.typ) {
topics = append(topics, record)
}
}
return topics, nil
}
func transformMessages(db *sql.DB, f func(*sql.Rows) error) error {
rows, err := db.Query(`
select
messages.topic_id,
messages.timestamp,
messages.data
from messages
inner join topics
on messages.topic_id = topics.id
order by messages.timestamp asc
`)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
err := f(rows)
if err != nil {
return err
}
}
return nil
}
func DB3ToMCAP(w io.Writer,
db *sql.DB,
opts *mcap.WriterOptions,
searchdirs []string,
callbacks ...func([]byte) error,
) error {
topics, err := getTopics(db)
if err != nil {
return err
}
types := make([]string, len(topics))
for i := range topics {
types[i] = topics[i].typ
}
schemas, err := getSchemas(searchdirs, types)
if err != nil {
return err
}
writer, err := mcap.NewWriter(w, opts)
if err != nil {
return err
}
defer writer.Close()
err = writer.WriteHeader(&mcap.Header{
Profile: "ros2",
})
if err != nil {
return err
}
// for each topic, write a schema and channel info to the output.
for i, t := range topics {
schemaID := uint16(i + 1)
schema, ok := schemas[t.typ]
if !ok {
return fmt.Errorf("unrecognized schema for %s", t.typ)
}
err = writer.WriteSchema(&mcap.Schema{
ID: schemaID,
Data: schema,
Name: t.typ,
Encoding: "ros2msg",
})
if err != nil {
return fmt.Errorf("failed to write schema: %w", err)
}
metadata := make(map[string]string)
if t.offeredQOSProfiles != nil {
metadata["offered_qos_profiles"] = *t.offeredQOSProfiles
}
err = writer.WriteChannel(&mcap.Channel{
ID: t.id,
Topic: t.name,
MessageEncoding: t.serializationFormat,
SchemaID: schemaID,
Metadata: metadata,
})
if err != nil {
return fmt.Errorf("failed to write channel info: %w", err)
}
}
seq := make(map[uint16]uint32)
err = transformMessages(db, func(rows *sql.Rows) error {
var topicID uint16
var messageTimestamp int64
var messageData []byte
err := rows.Scan(
&topicID,
&messageTimestamp,
&messageData,
)
if err != nil {
return err
}
err = writer.WriteMessage(&mcap.Message{
ChannelID: topicID,
Sequence: seq[topicID],
LogTime: uint64(messageTimestamp),
PublishTime: uint64(messageTimestamp),
Data: messageData,
})
if err != nil {
return err
}
seq[topicID]++
for _, callback := range callbacks {
err = callback(messageData)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
return nil
}