Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
84e5611
feat(buffer): add a static buffer pool for large RPC returns
Apehaenger Nov 13, 2026
2017388
feat(schema): allow blob parameters and returns in RPC functions
Apehaenger Nov 13, 2026
fb67176
docs: note version bump required when changing fixed param to blob
Apehaenger Nov 13, 2026
cc1a3fb
Fix rabbit findings
Apehaenger Nov 13, 2026
41a72af
feat(codegen): allow blob for inputs and outputs
Apehaenger Nov 14, 2026
4108e65
feat(codegen): emit the frame budget for blob parameters
Apehaenger Nov 14, 2026
228b641
fix(codegen): saturate the frame budgets and normalize the helper names
Apehaenger Nov 14, 2026
f1d89ac
docs(readme): clarify the frame budget and the empty lease cases
Apehaenger Nov 14, 2026
f01e043
fix(codegen): prevent size_t underflow in payload budget calculation
Apehaenger Nov 15, 2026
b887f06
feat(codegen): add a string type for text parameters and returns
Apehaenger Nov 15, 2026
f958635
fix(codegen): Fix new CodeRabbit findings on the new string type
Apehaenger Nov 16, 2026
b073c1a
Fixed the generator to cap at `max_length`
Apehaenger Nov 16, 2026
34a69b2
fix(service): check lengths against the bytes that are left
ClemensElflein Nov 18, 2026
4b96366
fix(buffer): refuse a pool that cannot hold a return value
ClemensElflein Nov 18, 2026
552ad6b
fix(codegen): hand handlers a cleared return buffer, bound the parame…
ClemensElflein Nov 18, 2026
ac5572e
fix(interface): refuse what does not fit into one packet, report BUSY
ClemensElflein Nov 18, 2026
70dd4b6
fix(go): validate a schema like the C++ generator does
ClemensElflein Nov 18, 2026
4c305b3
fix(py): enum returns, oversized sends and non-text string arguments
ClemensElflein Nov 18, 2026
3b52dfe
test: build the unit tests and the generated template code in CI
ClemensElflein Nov 18, 2026
0cef969
docs(readme): return buffers, pool lifetime and the packet limit
ClemensElflein Nov 18, 2026
c648295
fix(buffer): make the one-time warning flags atomic
ClemensElflein Nov 18, 2026
84dda8f
fix(interface): record RPC_ERROR when a generated call fails on its own
ClemensElflein Nov 18, 2026
6ffad4e
fix(py): reject array responses with a partial element
ClemensElflein Nov 18, 2026
5f23772
fix(go): convert enum array elements to and from the base type
ClemensElflein Nov 18, 2026
03b809b
fix(py): do not report a configuration that was not sent
ClemensElflein Nov 18, 2026
8b24786
test(interface): never wait for an answer thread while SendRpc holds …
ClemensElflein Nov 18, 2026
ee68aeb
docs: a timed out RPC has an unknown outcome
ClemensElflein Nov 18, 2026
c979c0f
build: let the CI build preset use the CI configure preset
ClemensElflein Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ if (XBOT_BUILD_EXAMPLES)
set(XBOT_BUILD_LIB_SERVICE ON)
endif ()

# Unit tests of the libraries and of the generated code, run with ctest. The CI preset enables them.
option(XBOT_BUILD_TESTS "Build the unit tests" OFF)
if (XBOT_BUILD_TESTS)
enable_testing()
endif ()

add_subdirectory(ext EXCLUDE_FROM_ALL)
add_subdirectory(codegen EXCLUDE_FROM_ALL)

Expand Down
5 changes: 3 additions & 2 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"buildPresets": [
{
"configurePreset": "Release",
"configurePreset": "CI",
"name": "CI"
},
{
Expand Down Expand Up @@ -56,7 +56,8 @@
},
{
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
"CMAKE_BUILD_TYPE": "Release",
"XBOT_BUILD_TESTS": "ON"
},
"inherits": "default",
"name": "CI"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN if [ -n "$RELEASE_VERSION" ]; then \
cmake --preset=CI -DCPACK_PACKAGE_VERSION="$RELEASE_VERSION" -DCPACK_GENERATOR=TGZ .; \
else \
cmake --preset=CI .; \
fi && cd build/CI && cmake --build . -j$(nproc) && cpack
fi && cd build/CI && cmake --build . -j$(nproc) && ctest --output-on-failure && cpack

FROM scratch AS export
COPY --from=build /workspace/build/CI/xbot_framework*.tar.gz /
181 changes: 167 additions & 14 deletions Readme.md

Large diffs are not rendered by default.

133 changes: 119 additions & 14 deletions codegen/templates/ServiceInterfaceTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ void ServiceTemplateInterfaceBase::OnData(uint16_t service_id, uint64_t timestam
/*[[[cog
for o in service['outputs']:
cog.outl(f"case {o['id']}:");
if o['is_array']:
if o['is_blob']:
cog.outl(f"{o['callback_name']}(static_cast<const uint8_t*>(payload), length);")
elif o['is_array']:
cog.outl(f"if(length % sizeof({o['type']}) != 0) {{");
cog.outl(" spdlog::error(\"Invalid data size\");");
cog.outl(" return;");
Expand Down Expand Up @@ -56,6 +58,9 @@ void ServiceTemplateInterfaceBase::OnData(uint16_t service_id, uint64_t timestam
}
OnExampleOutput2Changed(*static_cast<const uint32_t*>(payload));
break;
case 2:
OnExampleOutputBlobChanged(static_cast<const uint8_t*>(payload), length);
break;
//[[[end]]]
default:
return;
Expand Down Expand Up @@ -96,7 +101,11 @@ bool ServiceTemplateInterfaceBase::SetRegisterRegister4Optional(const uint32_t &
/*[[[cog
# Generate send function implementations.
for input in service["inputs"]:
if input['is_array']:
if input['is_blob']:
cog.outl(f"bool {service['interface_class_name']}::{input['send_method_name']}(const uint8_t* data, uint32_t length) {{")
cog.outl(f" return SendData({input['id']}, data, length, false);")
cog.outl("}")
elif input['is_array']:
cog.outl(f"bool {service['interface_class_name']}::{input['send_method_name']}(const {input['type']}* data, uint32_t length) {{")
cog.outl(f" return SendData({input['id']}, data, length*sizeof({input['type']}), false);")
cog.outl("}")
Expand All @@ -111,6 +120,9 @@ bool ServiceTemplateInterfaceBase::SendExampleInput1(const char* data, uint32_t
bool ServiceTemplateInterfaceBase::SendExampleInput2(const uint32_t &data) {
return SendData(1, &data, sizeof(uint32_t), false);
}
bool ServiceTemplateInterfaceBase::SendExampleInputBlob(const uint8_t* data, uint32_t length) {
return SendData(2, data, length, false);
}
//[[[end]]]

/*[[[cog
Expand All @@ -119,30 +131,53 @@ for func in service["functions"]:
# Build method signature
sig_params = []
for p in func["parameters"]:
if p['is_array']:
if p['is_blob']:
sig_params.append(f"const uint8_t* {p['name']}, uint32_t {p['name']}Len")
elif p['is_string']:
sig_params.append(f"const char* {p['name']}")
elif p['is_array']:
sig_params.append(f"const {p['type']}* {p['name']}, uint32_t {p['name']}Len")
else:
sig_params.append(f"const {p['type']}& {p['name']}")
if func['return_is_array']:
if func['return_is_blob']:
sig_params.append("uint8_t* data, uint16_t& result_length")
elif func['return_is_string']:
sig_params.append("std::string& result")
elif func['return_is_array']:
sig_params.append(f"{func['return_base_type']}* data, uint16_t& result_length")
elif func["return_type"] != "void":
sig_params.append(f"{func['return_type']}& result")
sig_params.append("uint32_t timeout_ms")
params_str = ", ".join(sig_params)
cog.outl(f"bool {service['interface_class_name']}::Call{func['name']}({params_str}) {{")

# Serialize parameters into a DataDescriptor-framed byte vector
# Serialize parameters into a DataDescriptor-framed byte vector. A call refused here, or a
# response refused below, returns through FailRpc() so GetLastRpcResult() says RPC_ERROR;
# only a failed SendRpc() returns plain false, as it has recorded its own result already.
if func["parameters"]:
cog.outl(" std::vector<uint8_t> params_buf;")
for p in func["parameters"]:
if p['is_array']:
if p['is_blob'] or p['is_array'] or p['is_string']:
if p['is_blob']:
byte_len_expr = f"{p['name']}Len"
elif p['is_string']:
# Text on the wire: the bytes only, the terminator is local. A value
# above the declared limit is refused here instead of on the server.
byte_len_expr = f"strlen({p['name']})"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
else:
byte_len_expr = f"{p['name']}Len * sizeof({p['type']})"
cog.outl(f" {{")
cog.outl(f" const size_t byte_len = {p['name']}Len * sizeof({p['type']});")
if p['is_string']:
# Before strlen(): a null C string is a caller bug, not a crash.
cog.outl(f" if ({p['name']} == nullptr) return FailRpc();")
cog.outl(f" const size_t byte_len = {byte_len_expr};")
if p['is_string']:
cog.outl(f" if (byte_len > {p['max_length']}) return FailRpc();")
cog.outl(f" const size_t off = params_buf.size();")
cog.outl(f" params_buf.resize(off + sizeof(xbot::datatypes::DataDescriptor) + byte_len);")
cog.outl(f" auto* desc = reinterpret_cast<xbot::datatypes::DataDescriptor*>(params_buf.data() + off);")
cog.outl(f" desc->target_id = {p['id']}; desc->reserved = 0; desc->payload_size = static_cast<uint32_t>(byte_len);")
cog.outl(f" if (byte_len > 0 && {p['name']} == nullptr) return false;")
cog.outl(f" if (byte_len > 0 && {p['name']} == nullptr) return FailRpc();")
cog.outl(f" memcpy(params_buf.data() + off + sizeof(xbot::datatypes::DataDescriptor), {p['name']}, byte_len);")
cog.outl(f" }}")
else:
Expand All @@ -158,7 +193,21 @@ for func in service["functions"]:
params_call = "nullptr, 0"

# Call SendRpc with appropriate response buffer
if func['return_is_array']:
if func['return_is_blob']:
cog.outl(f" size_t resp_size = result_length;")
cog.outl(f" const auto r = SendRpc({func['id']}, {params_call}, data, &resp_size, timeout_ms);")
cog.outl(f" if (r != RPC_OK) return false;")
cog.outl(f" result_length = static_cast<uint16_t>(resp_size);")
elif func['return_is_string']:
cog.outl(f" char resp_buf[{func['return_max_length']} + 1] = {{}};")
cog.outl(f" size_t resp_size = {func['return_max_length']};")
cog.outl(f" const auto r = SendRpc({func['id']}, {params_call}, reinterpret_cast<uint8_t*>(resp_buf), &resp_size,"
f" timeout_ms);")
cog.outl(f" if (r != RPC_OK) return false;")
cog.outl(f" if (resp_size > {func['return_max_length']}) return FailRpc();")
cog.outl(f" resp_buf[resp_size] = '\\0';")
cog.outl(f" result.assign(resp_buf, resp_size);")
elif func['return_is_array']:
cog.outl(f" size_t resp_size = result_length * sizeof({func['return_base_type']});")
cog.outl(f" const auto r = SendRpc({func['id']}, {params_call}, reinterpret_cast<uint8_t*>(data), &resp_size, timeout_ms);")
cog.outl(f" if (r != RPC_OK) return false;")
Expand All @@ -168,7 +217,7 @@ for func in service["functions"]:
cog.outl(f" size_t resp_size = sizeof({func['return_type']});")
cog.outl(f" const auto r = SendRpc({func['id']}, {params_call}, reinterpret_cast<uint8_t*>(&resp_buf), &resp_size, timeout_ms);")
cog.outl(f" if (r != RPC_OK) return false;")
cog.outl(f" if (resp_size < sizeof({func['return_type']})) return false;")
cog.outl(f" if (resp_size < sizeof({func['return_type']})) return FailRpc();")
cog.outl(f" result = resp_buf;")
else:
cog.outl(f" size_t resp_size = 0;")
Expand Down Expand Up @@ -211,7 +260,7 @@ bool ServiceTemplateInterfaceBase::CallScalarParamsWithReturn(const float& Speed
size_t resp_size = sizeof(int32_t);
const auto r = SendRpc(1, params_buf.data(), params_buf.size(), reinterpret_cast<uint8_t*>(&resp_buf), &resp_size, timeout_ms);
if (r != RPC_OK) return false;
if (resp_size < sizeof(int32_t)) return false;
if (resp_size < sizeof(int32_t)) return FailRpc();
result = resp_buf;
return true;
}
Expand All @@ -223,7 +272,7 @@ bool ServiceTemplateInterfaceBase::CallArrayParamNoReturn(const char* Label, uin
params_buf.resize(off + sizeof(xbot::datatypes::DataDescriptor) + byte_len);
auto* desc = reinterpret_cast<xbot::datatypes::DataDescriptor*>(params_buf.data() + off);
desc->target_id = 0; desc->reserved = 0; desc->payload_size = static_cast<uint32_t>(byte_len);
if (byte_len > 0 && Label == nullptr) return false;
if (byte_len > 0 && Label == nullptr) return FailRpc();
memcpy(params_buf.data() + off + sizeof(xbot::datatypes::DataDescriptor), Label, byte_len);
}
size_t resp_size = 0;
Expand All @@ -239,7 +288,7 @@ bool ServiceTemplateInterfaceBase::CallMixedParamsWithReturn(const char* Name, u
params_buf.resize(off + sizeof(xbot::datatypes::DataDescriptor) + byte_len);
auto* desc = reinterpret_cast<xbot::datatypes::DataDescriptor*>(params_buf.data() + off);
desc->target_id = 0; desc->reserved = 0; desc->payload_size = static_cast<uint32_t>(byte_len);
if (byte_len > 0 && Name == nullptr) return false;
if (byte_len > 0 && Name == nullptr) return FailRpc();
memcpy(params_buf.data() + off + sizeof(xbot::datatypes::DataDescriptor), Name, byte_len);
}
{
Expand All @@ -253,8 +302,64 @@ bool ServiceTemplateInterfaceBase::CallMixedParamsWithReturn(const char* Name, u
size_t resp_size = sizeof(uint8_t);
const auto r = SendRpc(3, params_buf.data(), params_buf.size(), reinterpret_cast<uint8_t*>(&resp_buf), &resp_size, timeout_ms);
if (r != RPC_OK) return false;
if (resp_size < sizeof(uint8_t)) return false;
if (resp_size < sizeof(uint8_t)) return FailRpc();
result = resp_buf;
return true;
}
bool ServiceTemplateInterfaceBase::CallBlobParamsWithBlobReturn(const uint8_t* Data, uint32_t DataLen, uint8_t* data, uint16_t& result_length, uint32_t timeout_ms) {
std::vector<uint8_t> params_buf;
{
const size_t byte_len = DataLen;
const size_t off = params_buf.size();
params_buf.resize(off + sizeof(xbot::datatypes::DataDescriptor) + byte_len);
auto* desc = reinterpret_cast<xbot::datatypes::DataDescriptor*>(params_buf.data() + off);
desc->target_id = 0; desc->reserved = 0; desc->payload_size = static_cast<uint32_t>(byte_len);
if (byte_len > 0 && Data == nullptr) return FailRpc();
memcpy(params_buf.data() + off + sizeof(xbot::datatypes::DataDescriptor), Data, byte_len);
}
size_t resp_size = result_length;
const auto r = SendRpc(4, params_buf.data(), params_buf.size(), data, &resp_size, timeout_ms);
if (r != RPC_OK) return false;
result_length = static_cast<uint16_t>(resp_size);
return true;
}
bool ServiceTemplateInterfaceBase::CallLargeArrayReturn(uint8_t* data, uint16_t& result_length, uint32_t timeout_ms) {
size_t resp_size = result_length * sizeof(uint8_t);
const auto r = SendRpc(5, nullptr, 0, reinterpret_cast<uint8_t*>(data), &resp_size, timeout_ms);
if (r != RPC_OK) return false;
result_length = static_cast<uint16_t>(resp_size / sizeof(uint8_t));
return true;
}
bool ServiceTemplateInterfaceBase::CallStringParamsWithStringReturn(const char* Path, std::string& result, uint32_t timeout_ms) {
std::vector<uint8_t> params_buf;
{
if (Path == nullptr) return FailRpc();
const size_t byte_len = strlen(Path);
if (byte_len > 64) return FailRpc();
const size_t off = params_buf.size();
params_buf.resize(off + sizeof(xbot::datatypes::DataDescriptor) + byte_len);
auto* desc = reinterpret_cast<xbot::datatypes::DataDescriptor*>(params_buf.data() + off);
desc->target_id = 0; desc->reserved = 0; desc->payload_size = static_cast<uint32_t>(byte_len);
if (byte_len > 0 && Path == nullptr) return FailRpc();
memcpy(params_buf.data() + off + sizeof(xbot::datatypes::DataDescriptor), Path, byte_len);
}
char resp_buf[96 + 1] = {};
size_t resp_size = 96;
const auto r = SendRpc(6, params_buf.data(), params_buf.size(), reinterpret_cast<uint8_t*>(resp_buf), &resp_size, timeout_ms);
if (r != RPC_OK) return false;
if (resp_size > 96) return FailRpc();
resp_buf[resp_size] = '\0';
result.assign(resp_buf, resp_size);
return true;
}
bool ServiceTemplateInterfaceBase::CallLargeStringReturn(std::string& result, uint32_t timeout_ms) {
char resp_buf[200 + 1] = {};
size_t resp_size = 200;
const auto r = SendRpc(7, nullptr, 0, reinterpret_cast<uint8_t*>(resp_buf), &resp_size, timeout_ms);
if (r != RPC_OK) return false;
if (resp_size > 200) return FailRpc();
resp_buf[resp_size] = '\0';
result.assign(resp_buf, resp_size);
return true;
}
//[[[end]]]
Loading
Loading