ros2-client 0.8.2

ROS2 client library based on RustDDS
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
use std::marker::PhantomData;

use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use log::{debug, error, info, warn};
use bytes::{BufMut, Bytes, BytesMut};
use rustdds::{
  dds::{ReadError, ReadResult, WriteError, WriteResult},
  rpc::*,
  serialization::deserialize_from_cdr_with_rep_id,
  *,
};

use crate::{message::Message, message_info::MessageInfo};
use super::{request_id, RmwRequestId, ServiceMapping};

// trait Wrapper is for interfacing to Service-specific (De)SerializerAdapter.
// These adapters are essentially pass-through, and do no actual serialization.
// (De)Serialization is done in Wrappers, because they know which ServiceMapping
// to apply, unlike (De)Serializer or their adapters. ServiceMapping must be
// known in order to decode or generate the wire representation.
pub(super) trait Wrapper {
  fn from_bytes_and_ri(input_bytes: &[u8], encoding: RepresentationIdentifier) -> Self;
  fn bytes(&self) -> Bytes;
}

pub(crate) struct RequestWrapper<R> {
  serialized_message: Bytes,
  encoding: RepresentationIdentifier,
  phantom: PhantomData<R>,
}

impl<R: Message> Wrapper for RequestWrapper<R> {
  fn from_bytes_and_ri(input_bytes: &[u8], encoding: RepresentationIdentifier) -> Self {
    RequestWrapper {
      serialized_message: Bytes::copy_from_slice(input_bytes), // cloning here
      encoding,
      phantom: PhantomData,
    }
  }
  fn bytes(&self) -> Bytes {
    self.serialized_message.clone()
  }
}

impl<R: Message> RequestWrapper<R> {
  // This will decode the RequestWrapper to Request in Server
  pub(super) fn unwrap(
    &self,
    service_mapping: ServiceMapping,
    message_info: &MessageInfo,
  ) -> ReadResult<(RmwRequestId, R)> {
    match service_mapping {
      ServiceMapping::Basic => {
        // 1. decode "RequestHeader" and
        // 2. decode Request
        let mut bytes = self.serialized_message.clone(); // ref copy only
        let (header, header_size) =
          deserialize_from_cdr_with_rep_id::<BasicRequestHeader>(&bytes, self.encoding)?;
        if bytes.len() < header_size {
          read_error_deserialization!("Service request too short")
        } else {
          let _header_bytes = bytes.split_off(header_size);
          let (request, _request_bytes) =
            deserialize_from_cdr_with_rep_id::<R>(&bytes, self.encoding)?;
          Ok((RmwRequestId::from(header.request_id), request))
        }
      }
      ServiceMapping::Enhanced => {
        // Enhanced mode does not use any header in the DDS payload.
        // Therefore, we use a wrapper that is identical to the payload.
        let (request, _request_bytes) =
          deserialize_from_cdr_with_rep_id::<R>(&self.serialized_message, self.encoding)?;
        let mut rmw_req_id = RmwRequestId::from(
          message_info.related_sample_identity()
            .unwrap_or_else(|| {
              // ServiceMapping::Enhanced is supposed to contain related sample identity as 
              // inline QoS parameter.
              //
              // Use the identity of the incoming request as a default, if there was no
              // related sample identity specified in inline QoS.
              let backup_identity = message_info.sample_identity();
              warn!("RequestWrapper::unwrap: related_sample_identity missing. Using sample_identity = {backup_identity:?}");
              backup_identity
            })
        );

        // Logic added for eProsima FastDDS compatibility:
        //
        // If the SequenceNumber in related_sample_identity (presumable from inline QoS)
        // is SEQUENCENUMBER_UNKNOWN, then it cannot refer to a real and valid DATA
        // submessage. We patch the situation by using the actual SequenceNumber
        // of the Request DATA submessage.
        //
        // Maybe FastDDS just forgets to set the field in RELATED_SAMPLE_IDENTITY inline
        // QoS parameter?
        if rmw_req_id.sequence_number == SequenceNumber::UNKNOWN {
          rmw_req_id.sequence_number = message_info.sample_identity().sequence_number;
        }

        Ok((rmw_req_id, request))
      }
      ServiceMapping::Cyclone => cyclone_unwrap::<R>(
        self.serialized_message.clone(),
        message_info.writer_guid(),
        self.encoding,
      ),
    }
  }

  // Client creates new RequestWrappers from Requests
  pub(super) fn new(
    service_mapping: ServiceMapping,
    r_id: RmwRequestId,
    encoding: RepresentationIdentifier,
    request: R,
  ) -> WriteResult<Self, ()> {
    let mut ser_buffer = BytesMut::with_capacity(std::mem::size_of::<R>() * 3 / 2).writer();

    // First, write header
    match service_mapping {
      ServiceMapping::Basic => {
        let basic_header = BasicRequestHeader::new(r_id.into());
        serialization::to_writer_with_rep_id(&mut ser_buffer, &basic_header, encoding)?;
      }
      ServiceMapping::Enhanced => {
        // This mapping does not use any header, so nothing to do here.
      }
      ServiceMapping::Cyclone => {
        let cyclone_header = CycloneHeader::new(r_id);
        serialization::to_writer_with_rep_id(&mut ser_buffer, &cyclone_header, encoding)?;
      }
    }
    // Second, write request
    serialization::to_writer_with_rep_id(&mut ser_buffer, &request, encoding)?;
    // Ok, assemble result
    Ok(RequestWrapper {
      serialized_message: ser_buffer.into_inner().freeze(),
      encoding,
      phantom: PhantomData,
    })
  }
}

pub(crate) struct ResponseWrapper<R> {
  serialized_message: Bytes,
  encoding: RepresentationIdentifier,
  phantom: PhantomData<R>,
}

impl<R: Message> Wrapper for ResponseWrapper<R> {
  fn from_bytes_and_ri(input_bytes: &[u8], encoding: RepresentationIdentifier) -> Self {
    ResponseWrapper {
      serialized_message: Bytes::copy_from_slice(input_bytes), // cloning here
      encoding,
      phantom: PhantomData,
    }
  }
  fn bytes(&self) -> Bytes {
    self.serialized_message.clone()
  }
}

impl<R: Message> ResponseWrapper<R> {
  // Client decodes ResponseWrapper to Response
  // message_info is from Server's response message
  pub(super) fn unwrap(
    &self,
    service_mapping: ServiceMapping,
    message_info: MessageInfo,
    client_guid: GUID,
  ) -> ReadResult<(RmwRequestId, R)> {
    match service_mapping {
      ServiceMapping::Basic => {
        let mut bytes = self.serialized_message.clone(); // ref copy only
        let (header, header_size) =
          deserialize_from_cdr_with_rep_id::<BasicReplyHeader>(&bytes, self.encoding)?;
        if bytes.len() < header_size {
          read_error_deserialization!("Service response too short")
        } else {
          let _header_bytes = bytes.split_off(header_size);
          let (response, _bytes) = deserialize_from_cdr_with_rep_id::<R>(&bytes, self.encoding)?;
          Ok((RmwRequestId::from(header.related_request_id), response))
        }
      }
      ServiceMapping::Enhanced => {
        // Enhanced mode does not use any header in the DDS payload.
        // Therefore, we use a wrapper that is identical to the payload.
        let (response, _response_bytes) =
          deserialize_from_cdr_with_rep_id::<R>(&self.serialized_message, self.encoding)?;
        let related_sample_identity = match message_info.related_sample_identity() {
          Some(rsi) => rsi,
          None => {
            return read_error_deserialization!("ServiceMapping=Enhanced, but response message did not have related_sample_identity parameter!")
          }
        };
        Ok((RmwRequestId::from(related_sample_identity), response))
      }
      ServiceMapping::Cyclone => {
        // Cyclone constructs the client GUID from two parts
        let mut client_guid_bytes = [0; 16];
        {
          let (first_half, second_half) = client_guid_bytes.split_at_mut(8);

          // This seems a bit odd, but source is
          // https://github.com/ros2/rmw_connextdds/blob/master/rmw_connextdds_common/src/common/rmw_impl.cpp
          // function take_response()
          first_half.copy_from_slice(&client_guid.to_bytes().as_slice()[0..8]);

          // This is received in the wrapper header
          second_half.copy_from_slice(&message_info.writer_guid().to_bytes()[8..16]);
        }
        let client_guid = GUID::from_bytes(client_guid_bytes);

        cyclone_unwrap::<R>(self.serialized_message.clone(), client_guid, self.encoding)
      }
    }
  }

  // Server creates new ResponseWrapper from Response
  pub(super) fn new(
    service_mapping: ServiceMapping,
    r_id: RmwRequestId,
    encoding: RepresentationIdentifier,
    response: R,
  ) -> WriteResult<Self, ()> {
    let mut ser_buffer = BytesMut::with_capacity(std::mem::size_of::<R>() * 3 / 2).writer();
    match service_mapping {
      ServiceMapping::Basic => {
        let basic_header = BasicReplyHeader::new(r_id.into());
        serialization::to_writer_with_rep_id(&mut ser_buffer, &basic_header, encoding)?;
      }
      ServiceMapping::Enhanced => {
        // No header, nothing to write here.
      }
      ServiceMapping::Cyclone => {
        let cyclone_header = CycloneHeader::new(r_id);
        serialization::to_writer_with_rep_id(&mut ser_buffer, &cyclone_header, encoding)?;
      }
    }
    serialization::to_writer_with_rep_id(&mut ser_buffer, &response, encoding)?;
    let serialized_message = ser_buffer.into_inner().freeze();
    Ok(ResponseWrapper {
      serialized_message,
      encoding,
      phantom: PhantomData,
    })
  }
}

// Basic mode header is specified in
// RPC over DDS Section "7.5.1.1.1 Common Types"
#[derive(Serialize, Deserialize)]
pub struct BasicRequestHeader {
  // "struct RequestHeader":
  request_id: SampleIdentity,
  instance_name: String, // This is apparently not used: Always sent as empty string.
}
impl BasicRequestHeader {
  fn new(request_id: SampleIdentity) -> Self {
    BasicRequestHeader {
      request_id,
      instance_name: "".to_string(),
    }
  }
}
impl Message for BasicRequestHeader {}

#[derive(Serialize, Deserialize)]
pub struct BasicReplyHeader {
  // "struct ReplyHeader":
  related_request_id: SampleIdentity,
  remote_exception_code: u32, /* It is uncertain if this is ever used. Transmitted as zero
                               * ("REMOTE_EX_OK"). */
}
impl BasicReplyHeader {
  fn new(related_request_id: SampleIdentity) -> Self {
    BasicReplyHeader {
      related_request_id,
      remote_exception_code: 0,
    }
  }
}
impl Message for BasicReplyHeader {}

// Cyclone mode header
//
// This is reverse-engineered from
// https://github.com/ros2/rmw_cyclonedds/blob/master/rmw_cyclonedds_cpp/src/rmw_node.cpp
// https://github.com/ros2/rmw_cyclonedds/blob/master/rmw_cyclonedds_cpp/src/serdata.hpp
// This is a header that Cyclone puts in DDS messages. Same header is used for
// Request and Response.
#[derive(Serialize, Deserialize)]
pub struct CycloneHeader {
  guid_second_half: [u8; 8], // CycloneDDS RMW only sends last 8 bytes of client GUID
  sequence_number_high: i32,
  sequence_number_low: u32,
}
impl CycloneHeader {
  fn new(r_id: RmwRequestId) -> Self {
    let sn = r_id.sequence_number;
    let mut guid_second_half = [0; 8];
    // writer_guid means client GUID (i.e. request writer)
    guid_second_half.copy_from_slice(&r_id.writer_guid.to_bytes()[8..16]);

    CycloneHeader {
      guid_second_half,
      sequence_number_high: sn.high(),
      sequence_number_low: sn.low(),
    }
  }
}
impl Message for CycloneHeader {}

// helper function, because Cyclone Request and Response unwrapping/decoding are
// the same.
fn cyclone_unwrap<R: Message>(
  serialized_message: Bytes,
  writer_guid: GUID,
  encoding: RepresentationIdentifier,
) -> ReadResult<(RmwRequestId, R)> {
  // 1. decode "CycloneHeader" and
  // 2. decode Request/response
  let mut bytes = serialized_message; // ref copy only, to make "mutable"
  let (header, header_size) = deserialize_from_cdr_with_rep_id::<CycloneHeader>(&bytes, encoding)?;
  if bytes.len() < header_size {
    read_error_deserialization!("Service message too short")
  } else {
    let _header_bytes = bytes.split_off(header_size);
    let (response, _response_bytes) = deserialize_from_cdr_with_rep_id::<R>(&bytes, encoding)?;
    let req_id = RmwRequestId {
      writer_guid, // TODO: This seems to be completely wrong!!!
      // When we are the client, we get half of Client GUID on the CycloneHeader, other half from
      // Client State when we are the server, we get half of Client GUID on the CycloneHeader,
      // other half from writer_guid.
      sequence_number: request_id::SequenceNumber::from_high_low(
        header.sequence_number_high,
        header.sequence_number_low,
      ),
    };
    Ok((req_id, response))
  }
}

pub(super) type SimpleDataReaderR<RW> =
  no_key::SimpleDataReader<RW, ServiceDeserializerAdapter<RW>>;
pub(super) type DataWriterR<RW> = no_key::DataWriter<RW, ServiceSerializerAdapter<RW>>;

pub(super) struct ServiceDeserializerAdapter<RW> {
  phantom: PhantomData<RW>,
}
pub(super) struct ServiceSerializerAdapter<RW> {
  phantom: PhantomData<RW>,
}

impl<RW> ServiceDeserializerAdapter<RW> {
  const REPR_IDS: [RepresentationIdentifier; 2] = [
    RepresentationIdentifier::CDR_BE,
    RepresentationIdentifier::CDR_LE,
  ];
}

impl<RW: Wrapper> no_key::DeserializerAdapter<RW> for ServiceDeserializerAdapter<RW> {
  type Error = ReadError;
  type Decoded = RW;

  fn supported_encodings() -> &'static [RepresentationIdentifier] {
    &Self::REPR_IDS
  }

  fn transform_decoded(decoded: Self::Decoded) -> RW {
    decoded
  }
}

impl<RW: Wrapper> no_key::DefaultDecoder<RW> for ServiceDeserializerAdapter<RW> {
  type Decoder = WrapperDecoder;
  const DECODER: Self::Decoder = WrapperDecoder;
}

#[derive(Clone)]
pub struct WrapperDecoder;

impl<RW> no_key::Decode<RW> for WrapperDecoder
where
  RW: Wrapper,
{
  type Error = ReadError;

  fn decode_bytes(
    self,
    input_bytes: &[u8],
    encoding: RepresentationIdentifier,
  ) -> Result<RW, Self::Error> {
    Ok(RW::from_bytes_and_ri(input_bytes, encoding))
  }
}

impl<RW: Wrapper> no_key::SerializerAdapter<RW> for ServiceSerializerAdapter<RW> {
  type Error = WriteError<()>;
  fn output_encoding() -> RepresentationIdentifier {
    RepresentationIdentifier::CDR_LE
  }

  fn to_bytes(value: &RW) -> WriteResult<Bytes, ()> {
    Ok(value.bytes())
  }
}