-
Notifications
You must be signed in to change notification settings - Fork 117
/
rmw_serialize.cpp
101 lines (90 loc) · 3.45 KB
/
rmw_serialize.cpp
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
// Copyright 2016-2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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 "fastcdr/FastBuffer.h"
#include "rmw/error_handling.h"
#include "rmw/serialized_message.h"
#include "rmw/rmw.h"
#include "./type_support_common.hpp"
extern "C"
{
rmw_ret_t
rmw_serialize(
const void * ros_message,
const rosidl_message_type_support_t * type_support,
rmw_serialized_message_t * serialized_message)
{
const rosidl_message_type_support_t * ts = get_message_typesupport_handle(
type_support, rosidl_typesupport_introspection_c__identifier);
if (!ts) {
ts = get_message_typesupport_handle(
type_support, rosidl_typesupport_introspection_cpp::typesupport_identifier);
if (!ts) {
RMW_SET_ERROR_MSG("type support not from this implementation");
return RMW_RET_ERROR;
}
}
auto tss = _create_message_type_support(ts->data, ts->typesupport_identifier);
auto data_length = tss->getEstimatedSerializedSize(ros_message);
if (serialized_message->buffer_capacity < data_length) {
if (rmw_serialized_message_resize(serialized_message, data_length) != RMW_RET_OK) {
RMW_SET_ERROR_MSG("unable to dynamically resize serialized message");
return RMW_RET_ERROR;
}
}
eprosima::fastcdr::FastBuffer buffer(
reinterpret_cast<char *>(serialized_message->buffer),
data_length);
eprosima::fastcdr::Cdr ser(
buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
auto ret = tss->serializeROSmessage(ros_message, ser);
serialized_message->buffer_length = data_length;
serialized_message->buffer_capacity = data_length;
delete tss;
return ret == true ? RMW_RET_OK : RMW_RET_ERROR;
}
rmw_ret_t
rmw_deserialize(
const rmw_serialized_message_t * serialized_message,
const rosidl_message_type_support_t * type_support,
void * ros_message)
{
const rosidl_message_type_support_t * ts = get_message_typesupport_handle(
type_support, rosidl_typesupport_introspection_c__identifier);
if (!ts) {
ts = get_message_typesupport_handle(
type_support, rosidl_typesupport_introspection_cpp::typesupport_identifier);
if (!ts) {
RMW_SET_ERROR_MSG("type support not from this implementation");
return RMW_RET_ERROR;
}
}
auto tss = _create_message_type_support(ts->data, ts->typesupport_identifier);
eprosima::fastcdr::FastBuffer buffer(
reinterpret_cast<char *>(serialized_message->buffer), serialized_message->buffer_length);
eprosima::fastcdr::Cdr deser(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::Cdr::DDS_CDR);
auto ret = tss->deserializeROSmessage(deser, ros_message);
delete tss;
return ret == true ? RMW_RET_OK : RMW_RET_ERROR;
}
rmw_ret_t
rmw_get_serialized_message_size(
const rosidl_message_type_support_t * /*type_support*/,
const rosidl_message_bounds_t * /*message_bounds*/,
size_t * /*size*/)
{
RMW_SET_ERROR_MSG("unimplemented");
return RMW_RET_ERROR;
}
} // extern "C"