aboutsummaryrefslogtreecommitdiff
path: root/lib/pbdrv
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pbdrv')
-rw-r--r--lib/pbdrv/CMakeLists.txt5
-rw-r--r--lib/pbdrv/pb-read.h8
-rw-r--r--lib/pbdrv/pb-types.h7
-rw-r--r--lib/pbdrv/pb-write.c34
-rw-r--r--lib/pbdrv/pb-write.h23
5 files changed, 77 insertions, 0 deletions
diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt
index 08894cc..dfaac01 100644
--- a/lib/pbdrv/CMakeLists.txt
+++ b/lib/pbdrv/CMakeLists.txt
@@ -10,6 +10,8 @@ add_compile_definitions(DEBUG)
project(pbdrv C CXX)
+add_subdirectory(../mpack ${CMAKE_CURRENT_BINARY_DIR}/mpack)
+
if(DEFINED ARDUINO)
set(PBDRV_ARDUINO true)
endif()
@@ -19,6 +21,9 @@ add_library(pbdrv STATIC
pb-read.c
pb-write.c
)
+target_link_libraries(pbdrv
+ mpack
+ )
target_include_directories(pbdrv SYSTEM INTERFACE .)
list(APPEND PBDRV_SRCS pb-mod.c)
diff --git a/lib/pbdrv/pb-read.h b/lib/pbdrv/pb-read.h
index 3f59c93..7a6f0df 100644
--- a/lib/pbdrv/pb-read.h
+++ b/lib/pbdrv/pb-read.h
@@ -1,2 +1,10 @@
#pragma once
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/lib/pbdrv/pb-types.h b/lib/pbdrv/pb-types.h
index 3c1314f..96ffc37 100644
--- a/lib/pbdrv/pb-types.h
+++ b/lib/pbdrv/pb-types.h
@@ -23,6 +23,7 @@ enum pb_cmd_id {
PB_CMD_REQ_WRITE, //!< request to write a puzzle module property
PB_CMD_REQ_STATE, //!< request global state
PB_CMD_RES_STATE, //!< respond to a global state request
+ PB_CMD_REQ_SET_STATE, //!< request to overwrite module global state
PB_CMD_MAGIC, //!< magic message (regular i2c command)
};
typedef enum pb_cmd_id pb_cmd_id_t;
@@ -79,6 +80,12 @@ typedef struct {
const pb_global_state_t state; //!< global state of sender
} pb_cmd_res_state_t;
+//! PB_CMD_REQ_SET_STATE data
+typedef struct {
+ const pb_msg_header_t header;
+ const pb_global_state_t state; //!< new global state
+} pb_cmd_req_set_state_t;
+
//! PB_CMD_MAGIC data
typedef struct {
const pb_msg_header_t header;
diff --git a/lib/pbdrv/pb-write.c b/lib/pbdrv/pb-write.c
index 65a2932..752a4ac 100644
--- a/lib/pbdrv/pb-write.c
+++ b/lib/pbdrv/pb-write.c
@@ -1 +1,35 @@
+#include <mpack.h>
+
#include "pb-write.h"
+
+typedef struct {
+ mpack_writer_t writer;
+ pbdrv_buf_t buf;
+} pbdrv_writer_t;
+
+static pbdrv_writer_t pbdrv_write_init() {
+ pbdrv_writer_t writer;
+ mpack_writer_init_growable(&writer.writer, &writer.buf.data, &writer.buf.size);
+ return writer;
+}
+
+static pbdrv_buf_t pbdrv_write_finish(pbdrv_writer_t * writer) {
+ if (mpack_writer_destroy(&writer->writer) != mpack_ok) {
+ writer->buf.data = NULL;
+ writer->buf.size = 0;
+ }
+ return writer->buf;
+}
+
+static void pbdrv_write_msg_header(pbdrv_writer_t * writer, pb_msg_header_t header) {
+ mpack_write_u8(&writer->writer, header.type);
+ mpack_write_u16(&writer->writer, header.sender);
+}
+
+pbdrv_buf_t pbdrv_write_cmd_req_set_state(pb_cmd_req_set_state_t data) {
+ pbdrv_writer_t writer = pbdrv_write_init();
+ pbdrv_write_msg_header(&writer, data.header);
+ mpack_write_u8(&writer.writer, data.state);
+ return pbdrv_write_finish(&writer);
+}
+
diff --git a/lib/pbdrv/pb-write.h b/lib/pbdrv/pb-write.h
index 45dcbb0..3245898 100644
--- a/lib/pbdrv/pb-write.h
+++ b/lib/pbdrv/pb-write.h
@@ -1,3 +1,26 @@
#pragma once
+#include "pb-types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//! binary buffer struct
+typedef struct {
+ char * data; //! pointer to data
+ size_t size; //! size of data
+} pbdrv_buf_t;
+
+pbdrv_buf_t pbdrv_write_cmd_req_read(pb_cmd_req_read_t data);
+pbdrv_buf_t pbdrv_write_cmd_res_read(pb_cmd_res_read_t data);
+pbdrv_buf_t pbdrv_write_cmd_req_write(pb_cmd_req_write_t data);
+pbdrv_buf_t pbdrv_write_cmd_req_state(pb_cmd_req_state_t data);
+pbdrv_buf_t pbdrv_write_cmd_res_state(pb_cmd_res_state_t data);
+pbdrv_buf_t pbdrv_write_cmd_req_set_state(pb_cmd_req_set_state_t data);
+pbdrv_buf_t pbdrv_write_cmd_magic(pb_cmd_magic_t data);
+
+#ifdef __cplusplus
+}
+#endif