-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathFedROS2CPPSerialization.java
210 lines (186 loc) · 8.57 KB
/
FedROS2CPPSerialization.java
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
/*************
* Copyright (c) 2021, The University of California at Berkeley.
* Copyright (c) 2021, The University of Texas at Dallas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
***************/
package org.lflang.federated.serialization;
import org.lflang.Target;
import org.lflang.generator.GeneratorBase;
/**
* Enables support for ROS 2 serialization in C/C++ code.
*
* @author Soroush Bateni <soroush@utdallas.edu>
*
*/
public class FedROS2CPPSerialization implements FedSerialization {
/**
* Check whether the current generator is compatible with the given
* serialization technique or not.
*
* @param generator The current generator.
* @return true if compatible, false if not.
*/
@Override
public boolean isCompatible(GeneratorBase generator) {
if (generator.getTarget() != Target.C) {
generator.errorReporter.reportError("ROS serialization is currently only supported for the C target.");
return false;
} else if (!generator.getTargetConfig().compiler.equalsIgnoreCase("g++")) {
generator.errorReporter.reportError(
"Please use the 'compiler: \"g++\"' target property \n"+
"for ROS serialization"
);
return false;
}
return true;
}
/**
* @return Expression in target language that corresponds to the length
* of the serialized buffer.
*/
@Override
public String serializedBufferLength() {
return serializedVarName+".size()";
}
/**
* @return Expression in target language that is the buffer variable
* itself.
*/
@Override
public String seializedBufferVar() {
return serializedVarName+".get_rcl_serialized_message().buffer";
}
/**
* Generate code in C++ that serializes 'varName'. This code
* will convert the data in 'varName' from its 'originalType' into an
* uint8_t. The serialized data will be put in a variable called
* 'serialized_message', defined by @see serializedVarName.
*
* @param varName The variable to be serialized.
* @param originalType The original type of the variable.
* @return Target code that serializes the 'varName' from 'type'
* to an unsigned byte array.
*/
@Override
public StringBuilder generateNetworkSerializerCode(String varName, String originalType) {
StringBuilder serializerCode = new StringBuilder();
serializerCode.append("rclcpp::SerializedMessage "+serializedVarName+"(0u);\n");
// Use the port type verbatim here, which can result
// in compile error if it is not a valid ROS type
serializerCode.append("using MessageT = "+originalType+";\n");
serializerCode.append("static rclcpp::Serialization<MessageT> serializer;\n");
serializerCode.append("serializer.serialize_message(&"+varName+"->value , &"+serializedVarName+");\n");
return serializerCode;
}
/**
* Variant of @see generateNetworkSerializerCode(String varName, String originalType)
* that also supports shared pointer (i.e., std::shared_ptr<>) definitions of ROS port
* types.
* @param isSharedPtrType Indicates whether the port type is a shared pointer or not.
*/
public StringBuilder generateNetworkSerializerCode(String varName, String originalType, boolean isSharedPtrType) {
StringBuilder serializerCode = new StringBuilder();
serializerCode.append("rclcpp::SerializedMessage "+serializedVarName+"(0u);\n");
// Use the port type verbatim here, which can result
// in compile error if it is not a valid ROS type
serializerCode.append("using MessageT = "+originalType+";\n");
serializerCode.append("static rclcpp::Serialization<MessageT> serializer;\n");
if (isSharedPtrType) {
serializerCode.append("serializer.serialize_message("+varName+"->value.get() , &"+serializedVarName+");\n");
} else {
serializerCode.append("serializer.serialize_message(&"+varName+"->value , &"+serializedVarName+");\n");
}
return serializerCode;
}
/**
* Generate code in C++ that deserializes 'varName'. This code will
* convert the data in 'varName' from a uint8_t into the 'targetType'.
* The deserialized data will be put in a variable called deserialized_message
* defined by @see deserializedVarName.
*
* @param varName The variable to deserialize.
* @param targetType The type to deserialize into.
* @return Target code that deserializes 'varName' from an unsigned byte array
* to 'type'.
*/
@Override
public StringBuilder generateNetworkDeserializerCode(String varName, String targetType) {
StringBuilder deserializerCode = new StringBuilder();
deserializerCode.append(
"auto message = std::make_unique<rcl_serialized_message_t>( rcl_serialized_message_t{\n"
+ " .buffer = (uint8_t*)"+varName+".token->value,\n"
+ " .buffer_length = "+varName+".token->length,\n"
+ " .buffer_capacity = "+varName+".token->length,\n"
+ " .allocator = rcl_get_default_allocator()\n"
+ "});\n"
);
deserializerCode.append("auto msg = std::make_unique<rclcpp::SerializedMessage>(std::move(*message.get()));\n");
deserializerCode.append(varName+".token->value = NULL; // Manually move the data\n");
// Use the port type verbatim here, which can result
// in compile error if it is not a valid ROS type
deserializerCode.append("using MessageT = "+targetType+";\n");
deserializerCode.append(
"MessageT "+deserializedVarName+" = MessageT();\n"
+ "auto serializer = rclcpp::Serialization<MessageT>();\n"
+ "serializer.deserialize_message(msg.get(), &"+deserializedVarName+");\n"
);
return deserializerCode;
}
/**
* @return Code in C that includes all the necessary preamble to enable
* support for ROS 2 serialization.
*/
@Override
public StringBuilder generatePreambleForSupport() {
StringBuilder preamble = new StringBuilder();
preamble.append(
"#include \"rcutils/allocator.h\"\n"
+ "#include \"rclcpp/rclcpp.hpp\"\n"
+ "#include \"rclcpp/serialization.hpp\"\n"
+ "#include \"rclcpp/serialized_message.hpp\"\n"
);
return preamble;
}
/**
* @return Code that should be appended to the CMakeLists.txt to enable
* support for ROS 2 serialization.
*/
@Override
public StringBuilder generateCompilerExtensionForSupport() {
StringBuilder cMakeExtension = new StringBuilder();
cMakeExtension.append(
"enable_language(CXX)\n"
+ "set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -Wno-write-strings -O2\")\n"
+ "\n"
+ "find_package(ament_cmake REQUIRED)\n"
+ "find_package(rclcpp REQUIRED)\n"
+ "find_package(rclcpp_components REQUIRED)\n"
+ "find_package(rcutils)\n"
+ "find_package(rmw REQUIRED)\n"
+ "\n"
+ "ament_target_dependencies( ${LF_MAIN_TARGET} rclcpp rmw)"
);
return cMakeExtension;
}
}