From c5bba84940399899a402c04acc479424aa2f1740 Mon Sep 17 00:00:00 2001 From: icex2 Date: Sun, 11 Jun 2023 17:03:34 +0200 Subject: [PATCH] refactor(p3io/cmd): Add and improve p3io command helper functions With the overhaul of commands, some helpers needed tweaks while new ones were added to abstract p3io command details with a common interface, e.g. dealing with the command length field. --- src/main/p3io/cmd.c | 48 +++++++++++++++++++++++++++++++++------------ src/main/p3io/cmd.h | 11 ++++++++--- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/main/p3io/cmd.c b/src/main/p3io/cmd.c index 6a0a7ff..bfb4e82 100644 --- a/src/main/p3io/cmd.c +++ b/src/main/p3io/cmd.c @@ -4,25 +4,49 @@ #include "util/log.h" -uint8_t p3io_req_cmd(const union p3io_req_any *src) +uint8_t p3io_get_full_req_size(const union p3io_req_any *req) { - log_assert(src != NULL); + /* Length byte in this packet format counts everything from the length + byte onwards. The length byte itself occurs at the start of the frame. */ - /* In requests, the command byte is the first byte after the header. */ - - return src->raw[sizeof(struct p3io_hdr)]; + return req->hdr.nbytes + 1; } -void p3io_resp_init( - struct p3io_hdr *dest, size_t nbytes, const struct p3io_hdr *req) +uint8_t p3io_get_full_resp_size(const union p3io_resp_any *resp) { - log_assert(dest != NULL); - log_assert(req != NULL); - log_assert(nbytes < 0x100); + /* Length byte in this packet format counts everything from the length + byte onwards. The length byte itself occurs at the start of the frame. */ + + return resp->hdr.nbytes + 1; +} + +void p3io_req_hdr_init( + struct p3io_hdr *req_hdr, uint8_t seq_no, uint8_t cmd, size_t size) +{ + log_assert(req_hdr != NULL); + log_assert(size < P3IO_MAX_MESSAGE_SIZE); + + memset(req_hdr, 0, sizeof(struct p3io_hdr)); /* Length byte in this packet format counts everything from the length byte onwards. The length byte itself occurs at the start of the frame. */ - dest->nbytes = nbytes - 1; - dest->seq_no = req->seq_no; + req_hdr->nbytes = size - 1; + req_hdr->seq_no = seq_no; + req_hdr->cmd = cmd; +} + +void p3io_resp_hdr_init( + struct p3io_hdr *resp_hdr, size_t nbytes, const struct p3io_hdr *req_hdr) +{ + log_assert(resp_hdr != NULL); + log_assert(req_hdr != NULL); + log_assert(nbytes < P3IO_MAX_MESSAGE_SIZE); + + /* Length byte in this packet format counts everything from the length + byte onwards. The length byte itself occurs at the start of the frame. */ + + resp_hdr->nbytes = nbytes - 1; + resp_hdr->seq_no = req_hdr->seq_no; + resp_hdr->cmd = req_hdr->cmd; } diff --git a/src/main/p3io/cmd.h b/src/main/p3io/cmd.h index d70b192..5359ec4 100644 --- a/src/main/p3io/cmd.h +++ b/src/main/p3io/cmd.h @@ -293,9 +293,14 @@ union p3io_resp_any { #pragma pack(pop) -uint8_t p3io_req_cmd(const union p3io_req_any *src); +uint8_t p3io_get_full_req_size(const union p3io_req_any *req); -void p3io_resp_init( - struct p3io_hdr *dest, size_t nbytes, const struct p3io_hdr *req); +uint8_t p3io_get_full_resp_size(const union p3io_resp_any *resp); + +void p3io_req_hdr_init( + struct p3io_hdr *hdr, uint8_t seq_no, uint8_t cmd, size_t size); + +void p3io_resp_hdr_init( + struct p3io_hdr *resp_hdr, size_t nbytes, const struct p3io_hdr *req_hdr); #endif