diff options
Diffstat (limited to 'proto/puzbusv1.c')
-rw-r--r-- | proto/puzbusv1.c | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/proto/puzbusv1.c b/proto/puzbusv1.c index 3ff7c63..73deda5 100644 --- a/proto/puzbusv1.c +++ b/proto/puzbusv1.c @@ -1,25 +1,48 @@ #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) { +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; - printf("read %lu bytes...\n", buf_sz); - 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); + // at start of message + 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. + // 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. + // The check here is kind of optional. + if (buf_sz < 4) return -1; + + target->addr = mpack_expect_u16(&reader); + 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), target->_rdata); + char * data = target->data + target->length - target->_rdata; + mpack_read_bytes(&reader, data, to_read); + target->_rdata -= to_read; + + // if rdata = 0, the message was completely read + return target->_rdata; +} - return 0; +void pb_read_reset(struct pb_msg * target) { + target->_rdata = 0; } -int 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); |