feat(coap): envelope ↔ CoAP payload translation#40
Merged
Conversation
Pure transport-free decode/encode helpers + return_code → CoAP class mapping table for the CoAP frontend. No dependency on coap_service, sockets, or zbus — the helpers operate on raw buffers and pre- initialised struct coap_packet instances, so they are unit-testable in isolation. Codegen now emits per-method req_max_size / resp_max_size from nanopb's compile-time <MSG>_SIZE constants, so the decoder enforces the exact per-method envelope (no global Kconfig cap) and the Phase 3 dispatcher can size per-type response scratch from a single codegen-derived ceiling. Test target tests/coap_translate/ (harness: ztest, mps2/an385 + native_sim) covers: the full mapping table (success ± payload, each mapped errno, default 5.00 fallback), decode happy/malformed/ oversize/empty paths, and encode for resp-present / resp-empty / zero-encoded / errno-verbatim cases. Refs #30
8b1afc9 to
bd2944f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 2 of the CoAP-frontend umbrella (#27). Adds the pure, transport-free envelope ↔ CoAP payload translation layer plus the
return_code → CoAP classmapping table. No dependency oncoap_service, sockets, or zbus — the helpers operate on raw buffers and pre-initialisedstruct coap_packetinstances, so they are unit-testable in isolation.Wire format
Request and response bodies are nanopb-encoded protobuf messages described by the same
pb_msgdesc_tdescriptors the per-zephlet_interface.calready references. Content-FormatZEPHLET_COAP_CT_NANOPB(65001) tags outbound payloads; the verbatim raw POSIX errno rides every response via custom optionZEPHLET_COAP_OPT_ERRNO(65052) so the (lossy) class mapping never erases the precise handler return code.What landed
frontends/coap/include/zephlet_coap_translate.h— public API:zephlet_coap_map_return_code,_decode_request,_encode_response.frontends/coap/zephlet_coap_translate.c— implementation.frontends/coap/include/zephlet_coap_types.h—struct zephlet_coap_methodgainsreq_max_size/resp_max_size(size_t) — codegen-emitted, see below.codegen/templates/zephlet_coap_interface.c.jinja— emits the two new fields from nanopb's compile-time<MSG>_SIZEmacros (zero forEmpty).frontends/coap/CMakeLists.txt— adds the new TU to the frontend library.tests/coap_translate/— new twister target (harness: ztest,mps2/an385+native_sim) with a tiny localtest_msg.proto. Compileszephlet_coap_translate.cdirectly so the test does not have to enableCONFIG_ZEPHLETS_COAP(no networking/sockets pulled in).Why codegen-derived per-method bounds vs. a Kconfig cap
The decoder enforces
len > m->req_max_size → -EMSGSIZE(→ 4.13). The bound is the exact nanopb-computed maximum encoded size of the method's request message, so anEmpty-typed method rejects any inbound body through the same path (req_max_size == 0). No single tuning knob to keep in sync with the protos — adding a method whose nanopb size exceeds the old default would have been a silent regression.return_code → CoAP class table
0(with payload)0(no payload)-EINVAL-ENODEV-ENOSYS-EALREADY-EMSGSIZE-EBUSY/-EAGAIN-ETIMEDOUT-ENOMEM/ any other negative-EMSGSIZE → 4.13was the only addition vs. the plan's locked-decisions table — confirmedEMSGSIZEis defined inzephyr/lib/libc/minimal/include/errno.h:106.Test results (local, mps2/an385 + QEMU 10.0.2)
tests/coap_translate/: 22/22 ztest cases pass in 0.014s.tests/codegen/pytest suite: 6/6 pass (codegen-template change validated).tick/ui/tampering): 23/23 pass.CONFIG_ZEPHLETS_COAP=n(default): clean —=nsection-hash gate inherits unchanged because the new TU only compiles under=y.Out of scope (deferred to later phases)
/.well-known/core) — Phase 4 (feat(coap): Phase 4 — discovery (/.well-known/core) #32).Test plan
west twister tests/coap_translategreen (mps2/an385).tests/codegen/pytest green (codegen template regression).CONFIG_ZEPHLETS_COAP=n.just testgreen (no integration-test regression).Closes #30