aboutsummaryrefslogtreecommitdiff
path: root/proto
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-18 20:11:44 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-18 20:11:44 +0200
commit1152ec32b6086c153dc41da59c9c451aa4465995 (patch)
treede696dc61a4566a75b4df1df907cb1a2da25bd82 /proto
parentcddfbb715d6f4f9d022d383ab8737b6af57a1d6f (diff)
WIP send/receive w/ msgpack
Diffstat (limited to 'proto')
-rw-r--r--proto/include.cmake3
-rw-r--r--proto/puzbusv1.c21
-rw-r--r--proto/puzbusv1.h2
3 files changed, 24 insertions, 2 deletions
diff --git a/proto/include.cmake b/proto/include.cmake
index c8a90b6..ac1305e 100644
--- a/proto/include.cmake
+++ b/proto/include.cmake
@@ -1,4 +1,7 @@
include_directories(${CMAKE_CURRENT_LIST_DIR})
+add_library(puzbus STATIC
+ ${CMAKE_CURRENT_LIST_DIR}/puzbusv1.c
+ )
# mpack
include_directories(${CMAKE_CURRENT_LIST_DIR}/lib/mpack/src/mpack)
diff --git a/proto/puzbusv1.c b/proto/puzbusv1.c
index 9d1335e..3ff7c63 100644
--- a/proto/puzbusv1.c
+++ b/proto/puzbusv1.c
@@ -1,13 +1,32 @@
#include <mpack.h>
+#include <stdio.h>
#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);
+
+ 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;
}
-void pb_free(struct pb_msg* msg);
+int pb_write(struct pb_msg* target, char** buf, size_t* buf_sz) {
+ mpack_writer_t writer;
+ mpack_writer_init_growable(&writer, buf, buf_sz);
+ mpack_write_u16(&writer, target->addr);
+ mpack_write_bin(&writer, target->data, target->length);
+
+ // finish writing
+ return mpack_writer_destroy(&writer) == mpack_ok;
+}
diff --git a/proto/puzbusv1.h b/proto/puzbusv1.h
index 071c887..116dbf9 100644
--- a/proto/puzbusv1.h
+++ b/proto/puzbusv1.h
@@ -14,7 +14,7 @@ struct pb_msg {
};
int pb_read(struct pb_msg* target, char* buf, size_t buf_sz);
-void pb_free(struct pb_msg* msg);
+int pb_write(struct pb_msg* target, char** buf, size_t* buf_sz);
#ifdef __cplusplus
}