ros2_control - rolling
Loading...
Searching...
No Matches
codecs.hpp
1// Copyright 2026 Open Source Robotics Foundation, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef ZENBEDDED_RCL__CODECS_HPP_
16#define ZENBEDDED_RCL__CODECS_HPP_
17
18#include <stddef.h>
19#include <stdint.h>
20#include <string.h>
21
22#ifdef CONFIG_ZENBEDDED_TIER_1
23#include <zenbedded_transport/serialization.h>
24#endif // CONFIG_ZENBEDDED_TIER_1
25
26// -------------------------------------------------------------------------------------------
27// The Codec concept
28// -------------------------------------------------------------------------------------------
29// ZenbeddedClient<StateCodec, CommandCodec> (zenbedded_client.hpp) never mentions a concrete
30// ROS 2 message type or a Tier by name. It only relies on this duck-typed contract, checked at
31// compile time when the template is instantiated:
32//
33// Ctx -- per-instance layout/offset state (e.g. precomputed CDR field offsets).
34// Opaque to the client; must be default-constructible.
35// InitParams -- whatever this message type needs at init time (topic-independent framing
36// like frame_id, joint_names, ...). Can be an empty struct.
37// Value -- the value passed to write() / populated by read().
38//
39// static void init(Ctx &, const InitParams &, uint8_t * buf);
40// Called once (per double-buffer slot) during ZenbeddedClient::init(). May write a
41// persistent template into `buf` (e.g. Tier 1's CDR header/field names) that write()
42// will patch on every call rather than rewrite from scratch -- see JointStateCodec.
43// static size_t payload_size(const Ctx &);
44// Wire size in bytes. Fixed after init() for the lifetime of the client.
45//
46// A codec used as a STATE (published/outgoing) channel additionally needs:
47// static void write(const Ctx &, const Value &, uint8_t * buf);
48//
49// A codec used as a COMMAND (subscribed/incoming) channel additionally needs:
50// static bool read(const Ctx &, const uint8_t * buf, size_t size, Value & out);
51//
52// A codec only needs to implement the direction(s) it's actually used for.
53// It Could have both read and write functions so its used for state/command codecs
54// -------------------------------------------------------------------------------------------
55
56// This class is a helper to check if a Codec is a valid Codec Type
57template <typename Codec>
59{
60private:
61 struct true_type
62 {
63 static constexpr bool value = true;
64 };
65 struct false_type
66 {
67 static constexpr bool value = false;
68 };
69
70 template <typename U>
71 static U declval_helper() noexcept;
72
73 // clang-format off
74 template <typename C>
75 static auto check_state_codec(int) -> decltype(C::init(declval_helper<typename C::Ctx &>(), declval_helper<const typename C::InitParams &>(), static_cast<uint8_t *>(nullptr)), C::payload_size(declval_helper<const typename C::Ctx &>()), C::write(declval_helper<const typename C::Ctx &>(), declval_helper<const typename C::Value &>(), static_cast<uint8_t *>(nullptr)), true_type{}); // NOLINT
76
77 template <typename C>
78 static auto check_command_codec(int) -> decltype(C::init(declval_helper<typename C::Ctx &>(), declval_helper<const typename C::InitParams &>(), static_cast<uint8_t *>(nullptr)), C::payload_size(declval_helper<const typename C::Ctx &>()), C::read(declval_helper<const typename C::Ctx &>(), static_cast<const uint8_t *>(nullptr), size_t{0}, declval_helper<typename C::Value &>()), true_type{}); // NOLINT
79 // clang-format on
80
81 template <typename>
82 static false_type check_state_codec(...);
83
84 template <typename>
85 static false_type check_command_codec(...);
86
87public:
88 static constexpr bool is_valid_state_codec = decltype(check_state_codec<Codec>(0))::value;
89 static constexpr bool is_valid_command_codec = decltype(check_command_codec<Codec>(0))::value;
90};
91
92// Codec for any fixed-layout "just copy the bytes" type: a generic
93// passthrough codec for any trivially-copyable POD type T. No CDR framing, no per-instance
94// layout to compute -- InitParams is empty, payload_size is simply sizeof(T), and Ctx carries
95// nothing.
96template <typename T>
98{
99 struct Ctx
100 {
101 };
102
104 {
105 };
106
107 using Value = T;
108
109 static void init(Ctx &, const InitParams &, uint8_t *) {}
110
111 static size_t payload_size(const Ctx &) { return sizeof(T); }
112
113 static void write(const Ctx &, const Value & v, uint8_t * buf) { memcpy(buf, &v, sizeof(T)); }
114
115 static bool read(const Ctx &, const uint8_t * buf, size_t size, Value & out)
116 {
117 if (size < sizeof(T))
118 {
119 return false;
120 }
121 memcpy(&out, buf, sizeof(T));
122 return true;
123 }
124};
125
126#ifdef CONFIG_ZENBEDDED_TIER_1
127
128// Codec for sensor_msgs/msg/JointState
129struct JointStateCodec
130{
131 using Ctx = zcdr_joint_state_ctx_t;
132
133 struct InitParams
134 {
135 const char * frame_id;
136 const char ** joint_names;
137 uint32_t num_joints;
138 };
139
140 struct Value
141 {
142 int32_t stamp_sec;
143 uint32_t stamp_nanosec;
144 const double * positions;
145 const double * velocities;
146 const double * efforts;
147 };
148
149 static void init(Ctx & ctx, const InitParams & p, uint8_t * buf)
150 {
151 zcdr_init_joint_state(&ctx, p.frame_id, p.joint_names, p.num_joints, buf);
152 }
153
154 static size_t payload_size(const Ctx & ctx) { return ctx.payload_size; }
155
156 static void write(const Ctx & ctx, const Value & v, uint8_t * buf)
157 {
158 zcdr_serialize_joint_state(
159 &ctx, v.stamp_sec, v.stamp_nanosec, v.positions, v.velocities, v.efforts, buf);
160 }
161};
162
163// Codec for control_msgs/msg/JointCommand
164struct JointCommandCodec
165{
166 using Ctx = zcdr_joint_command_ctx_t;
167
168 struct InitParams
169 {
170 const char * frame_id;
171 const char ** joint_names;
172 uint32_t num_joints;
173 const char * interface_name;
174 };
175
176 struct Value
177 {
178 int32_t stamp_sec;
179 uint32_t stamp_nanosec;
180 double * values;
181 };
182
183 static void init(Ctx & ctx, const InitParams & p, uint8_t * buf)
184 {
185 zcdr_init_joint_command(&ctx, p.frame_id, p.joint_names, p.num_joints, p.interface_name, buf);
186 }
187
188 static size_t payload_size(const Ctx & ctx) { return ctx.payload_size; }
189
190 static bool read(const Ctx & ctx, const uint8_t * buf, size_t size, Value & out)
191 {
192 return zcdr_deserialize_joint_command(
193 &ctx, buf, size, &out.stamp_sec, &out.stamp_nanosec, out.values);
194 }
195};
196
197#endif // CONFIG_ZENBEDDED_TIER_1
198
199#endif // ZENBEDDED_RCL__CODECS_HPP_
Definition codecs.hpp:59
Definition codecs.hpp:100
Definition codecs.hpp:104
Definition codecs.hpp:98