aboutsummaryrefslogtreecommitdiff
path: root/proto
diff options
context:
space:
mode:
Diffstat (limited to 'proto')
-rw-r--r--proto/puzbusv1.c33
-rw-r--r--proto/puzbusv1.h14
2 files changed, 19 insertions, 28 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);
diff --git a/proto/puzbusv1.h b/proto/puzbusv1.h
index 95130c9..0985b2b 100644
--- a/proto/puzbusv1.h
+++ b/proto/puzbusv1.h
@@ -7,10 +7,12 @@
extern "C" {
#endif
+/** \brief Puzzle bus message (v1) */
struct pb_msg {
- uint16_t addr;
- char* data;
- size_t length;
+ uint16_t addr; //!< I^2^C address
+ char * data; //!< message content
+ size_t length; //!< message size
+ size_t _rdata; //!< \private remaining bytes to read until message is complete
};
/**
@@ -32,7 +34,7 @@ struct pb_msg {
* the message is not fully parsed. This variable must be `free()`d by the
* caller after each complete message to prevent memory leaks.
*/
-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);
/**
* \brief reset the remaining message data counter
@@ -42,7 +44,7 @@ int pb_read(struct pb_msg* target, char* buf, size_t buf_sz);
* before reading a TCP frame's data to mitigate any synchronization issues
* arising from earlier corrupt or otherwise malformed messages.
*/
-void pb_read_reset();
+void pb_read_reset(struct pb_msg * target);
/**
* \brief Allocate and write a msgpack-formatted message to \p buf
@@ -60,7 +62,7 @@ void pb_read_reset();
*
* \note the pointer stored in \p buf must be `free()`d by the caller afterwards
*/
-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);
#ifdef __cplusplus
}