ros2_message 0.0.5

Utilities for handling MSG and SRV files in ROS2
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use crate::error::{Error, Result};
use crate::{DataType, FieldCase, FieldInfo, MessagePath, Msg, Value};
use byteorder::{ReadBytesExt, LE};
use lazy_static::lazy_static;
use regex::RegexBuilder;
// use rustc_hash::FxHashMap;
use std::collections::HashMap;
use std::convert::TryInto;
use std::io::{self, Read};

type MyMap<K, V> = HashMap<K, V>;
type MessageValues = Vec<Value>;

// Most of this code is copied from
// https://github.com/adnanademovic/rosrust/blob/master/rosrust/src/dynamic_msg.rs

/// A dynamic Message provides a decoder for ROS2 messages at runtime without
/// the need to compile a message decoder during compilation. See [Self::new()] for more.
#[derive(Clone, Debug)]
pub struct DynamicMsg {
    msg: Msg,
    dependencies: MyMap<MessagePath, Msg>,
}

/// Byte alignment for CDR version 1
const ALIGNMENT: usize = 4;

impl DynamicMsg {
    /// Create a new DynamicMsg by parsing it's message definition. For this to work all of
    /// the messages depencies have to be provided as well, see the example for more.
    ///
    /// # Examples
    ///
    /// ```
    /// use ros2_message::dynamic::DynamicMsg;
    ///
    /// let msg_definition = r#"
    /// builtin_interfaces/Time stamp
    /// float32 value
    ///
    /// ================================================================================
    /// MSG: builtin_interfaces/Time
    ///
    /// int32 sec
    /// uint32 nanosec
    /// "#;
    /// let dynamic_message = DynamicMsg::new("package/msg/SmallMsg", msg_definition);
    /// assert!(dynamic_message.is_ok());
    /// ```
    pub fn new(message_name: &str, message_definition: &str) -> Result<Self> {
        lazy_static! {
            static ref RE_DESCRIPTOR_MESSAGES_SPLITTER: regex::Regex = RegexBuilder::new("^=+$")
                .multi_line(true)
                .build()
                .expect("Invalid regex `^=+$`");
        }
        let mut message_bodies = RE_DESCRIPTOR_MESSAGES_SPLITTER.split(message_definition);
        let message_src = message_bodies
            .next()
            .ok_or(Error::BadMessageContent(format!(
                "Message definition for {} is missing main message body",
                message_name
            )))?;
        let msg = Self::parse_msg(message_name, message_src)?;
        let mut dependencies = MyMap::default();
        for message_body in message_bodies {
            let dependency = Self::parse_dependency(message_body)?;
            dependencies.insert(dependency.path().clone(), dependency);
        }

        Ok(DynamicMsg { msg, dependencies })
    }

    /// Returns the underlying ROS2 message definition
    pub fn msg(&self) -> &Msg {
        &self.msg
    }

    /// Returns the associated dependency of the underlying parsed ROS2 message definition if present
    pub fn dependency(&self, path: &MessagePath) -> Option<&Msg> {
        self.dependencies.get(path)
    }

    fn parse_msg(message_path: &str, message_src: &str) -> Result<Msg> {
        let message_path = message_path.try_into()?;
        let msg = Msg::new(message_path, message_src)?;
        Ok(msg)
    }

    fn parse_dependency(message_body: &str) -> Result<Msg> {
        lazy_static! {
            static ref RE_DESCRIPTOR_MSG_TYPE: regex::Regex =
                regex::Regex::new(r#"^\s*MSG:\s*(\S+)\s*$"#).unwrap();
        }
        let message_body = message_body.trim();
        let (message_type_line, message_src) =
            message_body
                .split_once('\n')
                .ok_or(Error::BadMessageContent(format!(
                    "Message dependency is missing type declaration"
                )))?;
        let cap =
            RE_DESCRIPTOR_MSG_TYPE
                .captures(message_type_line)
                .ok_or(Error::BadMessageContent(format!(
                    "Failed to parse message type line `{}`",
                    message_type_line
                )))?;
        let message_type = cap.get(1).ok_or(Error::BadMessageContent(format!(
            "Failed to parse message type line `{}`",
            message_type_line
        )))?;
        Self::parse_msg(message_type.as_str(), message_src)
    }

    fn get_dependency(&self, path: &MessagePath) -> Result<&Msg> {
        self.dependencies
            .get(path)
            .ok_or(Error::MessageDependencyMissing {
                package: path.package().to_owned(),
                name: path.name().to_owned(),
            })
    }

    /// This will read the provided reader to its end and return a map with all field names mapped to their values.
    /// If you encounter perfomance problems you may want to take a look at the  [Self::decode_raw()] method instead
    ///
    /// # Errors
    ///
    /// This will error if the provided reader is either to long or too short, make sure you only porvide a reader for
    /// the byte slice containing exactly one message.
    ///
    /// # Examples
    ///
    /// ```
    /// use ros2_message::dynamic::DynamicMsg;
    ///
    /// let msg_definition = r#"
    /// builtin_interfaces/Time stamp
    /// float32 value
    ///
    /// ================================================================================
    /// MSG: builtin_interfaces/Time
    ///
    /// int32 sec
    /// uint32 nanosec
    /// "#;
    ///
    /// let dynamic_message = DynamicMsg::new("package/msg/SmallMsg", msg_definition)
    ///     .expect("The message definition was invalid");
    /// let message = dynamic_message.decode(&[0x00u8, 0x01, 0, 0, 157, 47, 136, 102, 42, 0, 0 ,0, 219, 15, 73, 64][..])
    ///     .expect("The supplied bytes do not match the message definition");
    ///
    /// // Reading the value field of the message
    /// assert_eq!(message["value"], ros2_message::Value::F32(core::f32::consts::PI));
    /// ```
    pub fn decode<R: Read>(&self, r: R) -> Result<MyMap<String, Value>> {
        let mut values = self.decode_message(self.msg(), r)?;

        Ok(self.map_field_names(self.msg(), &mut values)?)
    }

    // Map decoded field arrays to their field names for easy usage
    // TODO!: Use better collection method for this operation
    fn map_field_names(&self, msg: &Msg, values: &mut Vec<Value>) -> Result<MyMap<String, Value>> {
        let mut map = MyMap::default();
        for field_info in msg.fields().iter() {
            let field_name = field_info.name().to_owned();

            // println!("{} - {:?}", &field_name, values);

            let field_value = match field_info.datatype() {
                DataType::GlobalMessage(path) => {
                    let msg = self.get_dependency(path)?;
                    let mut nested_values = match values.remove(0) {
                        Value::Array(arr) => arr,
                        _ => {
                            return Err(Error::DecodingError {
                                err: std::io::Error::other("Decoded message does not match the structure in the definition, please report this issue"),
                                field: field_info.clone(),
                                msg: msg.clone(),
                                offset: 0,
                            })
                        }
                    };

                    Value::Message(self.map_field_names(msg, &mut nested_values)?)
                }
                DataType::LocalMessage(name) => {
                    let msg = self.get_dependency(&msg.path().peer(name))?;
                    let mut nested_values = match values.remove(0) {
                        Value::Array(arr) => arr,
                        _ => return Err(Error::DecodingError {
                                err: std::io::Error::other("Decoded message does not match the structure in the definition, please report this issue"),
                                field: field_info.clone(),
                                msg: msg.clone(),
                                offset: 0,
                            }),
                    };

                    Value::Message(self.map_field_names(msg, &mut nested_values)?)
                }
                _ => values.remove(0),
            };

            map.insert(field_name, field_value);
        }

        Ok(map)
    }

    /// This will read the provided reader to its end and return a vector with all field values.
    /// The values are in the same order as the message fields, this is done to save cost from expensive
    /// Map read&writes.
    ///
    /// If you want a HashMap of values instead use the [Self::decode()] method instead
    ///
    /// # Examples
    ///
    /// ```
    /// use ros2_message::dynamic::DynamicMsg;
    ///
    /// let msg_definition = r#"
    /// builtin_interfaces/Time stamp
    /// float32 value
    ///
    /// ================================================================================
    /// MSG: builtin_interfaces/Time
    ///
    /// int32 sec
    /// uint32 nanosec
    /// "#;
    ///
    /// let dynamic_message = DynamicMsg::new("package/msg/SmallMsg", msg_definition).expect("The message definition was invalid");
    /// let message = dynamic_message.decode_raw(&[0x00u8, 0x01, 0, 0, 157, 47, 136, 102, 42, 0, 0 ,0, 219, 15, 73, 64][..])
    ///     .expect("The supplied bytes do not match the message definition");
    ///
    /// // Reading the second field of the message
    /// assert_eq!(message[1], ros2_message::Value::F32(core::f32::consts::PI));
    /// ```
    pub fn decode_raw<R: Read>(&self, r: R) -> Result<MessageValues> {
        self.decode_message(self.msg(), r)
    }

    // This is necessary to prevent the creation of nested ByteCounters
    fn decode_message<R: Read>(&self, msg: &Msg, r: R) -> Result<MessageValues> {
        let mut r = ByteCounter::new(r);

        let mut buf = [0, 0, 0, 0];
        r.read_exact(&mut buf)?;

        // https://github.com/foxglove/cdr/blob/main/src/EncapsulationKind.ts
        // let kind = buf[1];
        if buf != [0, 0x01, 0, 0] {
            return Err(Error::DecodingError {
                msg: msg.clone(),
                field: FieldInfo::new("uint8", "error_placeholder_field", crate::FieldCase::Unit)
                    .unwrap(),
                offset: r.bytes_read(),
                err: io::Error::other(format!(
                    "Invalid CRD kind {:b}, only little endian is supported",
                    buf[1]
                )),
            });
        }

        let decoded_values = self.decode_message_inner(msg, &mut r)?;

        // This is purely a sanity check
        {
            // Read alignment bytes
            let _ = r.align_to(4);

            // Ensure we read the entire message
            let mut buf = Vec::new();
            r.read_to_end(&mut buf)?;

            if buf != [] as [u8; 0] {
                return Err(io::Error::other(format!(
                            "Encountered error after reading message, most likely the message padding was read wrong,\
                             please report this issue. The message was decoded to the following fields:\n\n{decoded_values:#?}\n\n\
                             For further diagnosis please provide the following message definition:\n\n\
                             {msg}\n\nAlso provide the raw byte data: {buf:?}"
                        )).into());
            }
        }

        Ok(decoded_values)
    }

    fn decode_message_inner<R: Read>(
        &self,
        msg: &Msg,
        r: &mut ByteCounter<R>,
    ) -> Result<MessageValues> {
        let mut values = Vec::with_capacity(msg.fields().len());
        for field in msg.fields() {
            let val = match field.case() {
                FieldCase::Const(_) => Ok(field.const_value().unwrap().clone()),
                FieldCase::Unit | FieldCase::Default(_) => self.decode_field(msg.path(), field, r),
                //.expect("Error while decoding unit field"),
                FieldCase::Vector => self.decode_field_array(msg.path(), field, None, r),
                //.expect("Error while decoding vector field"),
                FieldCase::Array(l) => self.decode_field_array(msg.path(), field, Some(*l), r), //.expect("Error while decoding array field"),
            }
            .map_err(|e| match e {
                Error::DecodingError {
                    msg: _,
                    field: _,
                    offset: _,
                    err,
                } => Error::DecodingError {
                    msg: msg.clone(),
                    field: field.clone(),
                    offset: r.bytes_read(),
                    err,
                },
                e => e,
            })?;
            values.push(val);
        }

        Ok(values)
    }

    fn decode_field<R: Read>(
        &self,
        parent: &MessagePath,
        field: &FieldInfo,
        r: &mut ByteCounter<R>,
    ) -> Result<Value> {
        /*
        let field_type = field.datatype().to_string();
        let prev_read = r.bytes_read();
        println!(" {} > at {:X}", field_type, prev_read);
        */

        let value = match field.datatype() {
            DataType::Bool => r.read_u8().map(|i| i != 0)?.into(),
            DataType::I8(_) => r.read_i8()?.into(),
            DataType::I16 => {
                r.align_to(2)?;
                r.read_i16::<LE>()?.into()
            }
            DataType::I32 => {
                r.align_to(4)?;
                r.read_i32::<LE>()?.into()
            }
            DataType::I64 => {
                r.align_to(ALIGNMENT)?;
                r.read_i64::<LE>()?.into()
            }
            DataType::U8(_) => r.read_u8()?.into(),
            DataType::U16 => {
                r.align_to(2)?;
                r.read_u16::<LE>()?.into()
            }
            DataType::U32 => {
                r.align_to(4)?;
                r.read_u32::<LE>()?.into()
            }
            DataType::U64 => {
                r.align_to(ALIGNMENT)?;
                r.read_u64::<LE>()?.into()
            }
            DataType::F32 => {
                r.align_to(4)?;
                r.read_f32::<LE>()?.into()
            }
            DataType::F64 => {
                r.align_to(ALIGNMENT)?;
                r.read_f64::<LE>()?.into()
            }
            DataType::String => {
                r.align_to(4)?;
                let len = r.read_u32::<LE>()?;

                if len == 0 {
                    return Ok(Value::String("".to_owned()));
                }

                let mut v = vec![0; (len - 1) as usize];
                r.read_exact(&mut v)?;

                // Read \0 character
                let null = r.read_u8()?;
                assert_eq!(0, null);

                match String::from_utf8(v) {
                    Ok(s) => Value::String(s),
                    Err(e) => {
                        return Err(Error::DecodingError {
                            err: std::io::Error::other(e),
                            field: field.clone(),
                            msg: self.msg.clone(),
                            offset: r.bytes_read(),
                        })
                    }
                }
            }
            DataType::Time => {
                r.align_to(4)?;
                let sec = r.read_u32::<LE>()?;
                let nsec = r.read_u32::<LE>()?;

                return Ok(Value::Time(crate::Time { sec, nsec }));
            }
            DataType::Duration => panic!("Duration parsing not implemented yet"),
            DataType::LocalMessage(name) => {
                let path = parent.peer(name);
                let dependency = self.get_dependency(&path)?;

                // Decoding is fully unmapped so messages are just expressed as
                // arrays before they get mapped to field names
                Value::Array(self.decode_message_inner(dependency, r)?)
            }
            DataType::GlobalMessage(path) => {
                // panic!("Global messages unsupported (Hasher) {path}");

                let dependency = self.get_dependency(path)?;
                self.decode_message_inner(dependency, r)?.into()
            }
        };

        /*
        println!(
            " {} < at {:X} ( +{}) => {}",
            " ".repeat(field_type.len()),
            r.bytes_read(),
            r.bytes_read() - prev_read,
            &value
        );
         */

        Ok(value)
    }

    fn decode_field_array<R: Read>(
        &self,
        parent: &MessagePath,
        field: &FieldInfo,
        array_length: Option<usize>,
        r: &mut ByteCounter<R>,
    ) -> Result<Value> {
        let array_length = match array_length {
            Some(v) => v,
            None => r.read_u32::<LE>()? as usize,
        };
        // TODO: optimize by checking data type only once

        let mut values = Vec::with_capacity(array_length);
        for _ in 0..array_length {
            values.push(self.decode_field(parent, field, r)?);
        }

        Ok(Value::Array(values))
    }
}

struct ByteCounter<R> {
    inner: R,
    count: usize,
}

impl<R> ByteCounter<R>
where
    R: Read,
{
    fn new(inner: R) -> Self {
        ByteCounter { inner, count: 0 }
    }

    /*
    fn into_inner(self) -> R {
        self.inner
    }
    */

    fn bytes_read(&self) -> usize {
        self.count
    }

    /// Read the necessary amount of bytes so that the next read will be aligned to `size` bytes
    fn align_to(&mut self, size: usize) -> io::Result<()> {
        let cur_pos = self.bytes_read();
        let cur_align = cur_pos % size;

        if cur_align > 0 {
            let needed_offset = size - cur_align;

            // println!("Aligning from {} + {}", cur_pos, needed_offset);

            let mut buf = vec![0; needed_offset];
            self.read_exact(&mut buf)?;
        }

        // println!("Next byte will be {}", self.bytes_read());

        Ok(())
    }
}

impl<R> Read for ByteCounter<R>
where
    R: Read,
{
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let res = self.inner.read(buf);
        if let Ok(size) = res {
            self.count += size
        }
        res
    }
}