diff options
Diffstat (limited to 'proto')
-rw-r--r-- | proto/puzbusv1.c | 43 | ||||
-rw-r--r-- | proto/puzbusv1.h | 37 |
2 files changed, 66 insertions, 14 deletions
diff --git a/proto/puzbusv1.c b/proto/puzbusv1.c index 3ff7c63..d6cd597 100644 --- a/proto/puzbusv1.c +++ b/proto/puzbusv1.c @@ -1,25 +1,44 @@ #include <mpack.h> #include <stdio.h> +// MIN() macro +#include <sys/param.h> +// TODO: check if this works on pico as well + #include "puzbusv1.h" -int pb_read(struct pb_msg* target, char* buf, size_t buf_sz) { - mpack_reader_t reader; - printf("read %lu bytes...\n", buf_sz); +bool pb_read(struct pb_msg* target, char* buf, size_t buf_sz) { + // remaining bytes to be read to target->data; this is the only variable that + // needs to persist between buffer blocks, and is therefore static + static size_t rdata = 0; + // a new reader is used per buffer block passed to this function + mpack_reader_t reader; mpack_reader_init_data(&reader, buf, buf_sz); - uint16_t address = mpack_expect_u16(&reader); - char data_buf[80]; - size_t data_size = mpack_expect_bin_buf(&reader, data_buf, sizeof(data_buf)); - - printf("0x%02x\n", address); - printf("\"%.*s\"\n", data_size, data_buf); - - return 0; + // at start of message + if (rdata == 0) { + // NOTE: This approach will crash and burn when target->addr can be read + // and target->length is past the end of the current buffer block. This is + // a highly unlikely scenario, as pb_read is called for each chunk of a TCP + // frame, and frames (should) include only one puzzle bus message. + target->addr = mpack_expect_u16(&reader); + target->length = 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 + mpack_read_bytes(&reader, data, to_read); + rdata -= to_read; + + // if rdata = 0, the message was completely read + return rdata == 0; } -int pb_write(struct pb_msg* target, char** buf, size_t* buf_sz) { +bool pb_write(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 116dbf9..814bc93 100644 --- a/proto/puzbusv1.h +++ b/proto/puzbusv1.h @@ -13,8 +13,41 @@ struct pb_msg { size_t length; }; -int pb_read(struct pb_msg* target, char* buf, size_t buf_sz); -int pb_write(struct pb_msg* target, char** buf, size_t* buf_sz); +/** + * \brief Read chunk of input stream, and store resulting message in \p target + * + * This function is called for each chunk of data from an input stream, and + * will parse the next puzzle bus message into \p target. The input stream is + * assumed to only contain messages encoded by \p pb_write() + * + * \param target pointer to struct that will contain the finished message data + * \param buf pointer to input stream data chunk + * \param buf_sz size of \p buf + * + * \returns boolean true if a message was completely read, false if more data + * is required + * + * \note target->data will automatically be allocated by this function, and + * must be `free()`d by the caller when finished + */ +bool pb_read(struct pb_msg* target, char* buf, size_t buf_sz); +/** + * \brief Allocate and write a msgpack-formatted message to \p buf + * + * This function allocates a buffer large enough to fit the message specified + * in \p target, and encodes the data in \p target in a format that can be + * decoded later using \p pb_read() + * + * \param target pointer to struct that contains the message data + * \param buf pointer to \c char* that will contain the formatted message + * \param buf_sz pointer to \c size_t that will represent the final size of \p buf + * + * \returns boolean true if a the message could be encoded successfully, false + * if there was some kind of error + * + * \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); #ifdef __cplusplus } |