15#ifndef ZENBEDDED_RCL__CODECS_HPP_
16#define ZENBEDDED_RCL__CODECS_HPP_
22#ifdef CONFIG_ZENBEDDED_TIER_1
23#include <zenbedded_transport/serialization.h>
57template <
typename Codec>
63 static constexpr bool value =
true;
67 static constexpr bool value =
false;
71 static U declval_helper()
noexcept;
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{});
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{});
82 static false_type check_state_codec(...);
85 static false_type check_command_codec(...);
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;
111 static size_t payload_size(
const Ctx &) {
return sizeof(T); }
113 static void write(
const Ctx &,
const Value & v, uint8_t * buf) { memcpy(buf, &v,
sizeof(T)); }
115 static bool read(
const Ctx &,
const uint8_t * buf,
size_t size, Value & out)
117 if (size <
sizeof(T))
121 memcpy(&out, buf,
sizeof(T));
126#ifdef CONFIG_ZENBEDDED_TIER_1
129struct JointStateCodec
131 using Ctx = zcdr_joint_state_ctx_t;
135 const char * frame_id;
136 const char ** joint_names;
143 uint32_t stamp_nanosec;
144 const double * positions;
145 const double * velocities;
146 const double * efforts;
149 static void init(Ctx & ctx,
const InitParams & p, uint8_t * buf)
151 zcdr_init_joint_state(&ctx, p.frame_id, p.joint_names, p.num_joints, buf);
154 static size_t payload_size(
const Ctx & ctx) {
return ctx.payload_size; }
156 static void write(
const Ctx & ctx,
const Value & v, uint8_t * buf)
158 zcdr_serialize_joint_state(
159 &ctx, v.stamp_sec, v.stamp_nanosec, v.positions, v.velocities, v.efforts, buf);
164struct JointCommandCodec
166 using Ctx = zcdr_joint_command_ctx_t;
170 const char * frame_id;
171 const char ** joint_names;
173 const char * interface_name;
179 uint32_t stamp_nanosec;
183 static void init(Ctx & ctx,
const InitParams & p, uint8_t * buf)
185 zcdr_init_joint_command(&ctx, p.frame_id, p.joint_names, p.num_joints, p.interface_name, buf);
188 static size_t payload_size(
const Ctx & ctx) {
return ctx.payload_size; }
190 static bool read(
const Ctx & ctx,
const uint8_t * buf,
size_t size, Value & out)
192 return zcdr_deserialize_joint_command(
193 &ctx, buf, size, &out.stamp_sec, &out.stamp_nanosec, out.values);
Definition codecs.hpp:100
Definition codecs.hpp:104