aboutsummaryrefslogtreecommitdiff
path: root/proto/puzbusv1.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-21 10:30:06 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-21 10:30:06 +0200
commit68a5c65f9b0e1df30e9cef490d9b218b2f21f90d (patch)
tree804ec0edc0769764b0a090703cf784c2ac7a6f5c /proto/puzbusv1.c
parentdb8906d54cd9afbc57f0b40a0d618335c552f704 (diff)
clean up puzbusv1 API
Diffstat (limited to 'proto/puzbusv1.c')
-rw-r--r--proto/puzbusv1.c33
1 files changed, 11 insertions, 22 deletions
diff --git a/proto/puzbusv1.c b/proto/puzbusv1.c
index 3be4939..73deda5 100644
--- a/proto/puzbusv1.c
+++ b/proto/puzbusv1.c
@@ -7,24 +7,13 @@
#include "puzbusv1.h"
-/**
- * \brief Remaining bytes to be read to target->data
- *
- * This is the only variable that needs to persist between buffer blocks. It is
- * declared in the global scope to allow resetting using the \c pb_read_reset()
- * function.
- *
- * \note \p rdata may be reset by calling \c pb_read_reset()
- */
-static size_t rdata = 0;
-
-int pb_read(struct pb_msg* target, char* buf, size_t buf_sz) {
+int pb_read(struct pb_msg * target, const char * buf, size_t buf_sz) {
// a new reader is used per buffer block passed to this function
mpack_reader_t reader;
mpack_reader_init_data(&reader, buf, buf_sz);
// at start of message
- if (rdata == 0) {
+ if (target->_rdata == 0) {
// NOTE: The entire start of a message needs to be readable from the buffer
// at this point. When target->addr can be read and target->length is past
// the end of the current buffer block, this function will crash and burn.
@@ -34,26 +23,26 @@ int pb_read(struct pb_msg* target, char* buf, size_t buf_sz) {
if (buf_sz < 4) return -1;
target->addr = mpack_expect_u16(&reader);
- target->length = rdata = mpack_expect_bin(&reader);
- target->data = (char*) malloc(target->length);
+ target->length = target->_rdata = mpack_expect_bin(&reader);
+ target->data = (char *) malloc(target->length);
}
// continue reading chunks of target->data until the amount of bytes
// specified in target->length
- size_t to_read = MIN(mpack_reader_remaining(&reader, NULL), rdata);
- char* data = target->data + target->length - rdata; // 'ol pointer arithmetic
+ size_t to_read = MIN(mpack_reader_remaining(&reader, NULL), target->_rdata);
+ char * data = target->data + target->length - target->_rdata;
mpack_read_bytes(&reader, data, to_read);
- rdata -= to_read;
+ target->_rdata -= to_read;
// if rdata = 0, the message was completely read
- return rdata;
+ return target->_rdata;
}
-void pb_read_reset() {
- rdata = 0;
+void pb_read_reset(struct pb_msg * target) {
+ target->_rdata = 0;
}
-bool pb_write(struct pb_msg* target, char** buf, size_t* buf_sz) {
+bool pb_write(const struct pb_msg * target, char ** buf, size_t * buf_sz) {
mpack_writer_t writer;
mpack_writer_init_growable(&writer, buf, buf_sz);