diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-05 17:44:57 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-05 17:44:57 +0200 |
commit | 0fc049aeaf3dab41e92ed0bd7872094d7c5c9bdc (patch) | |
tree | c96ed327f4fedaa95937b1c4dd6bf53d12aa4ecc | |
parent | 68471e5800b81285453a26547721d264dbcf49b9 (diff) |
WIP pb-write cmd req set state
-rw-r--r-- | lib/pbdrv/CMakeLists.txt | 5 | ||||
-rw-r--r-- | lib/pbdrv/pb-read.h | 8 | ||||
-rw-r--r-- | lib/pbdrv/pb-types.h | 7 | ||||
-rw-r--r-- | lib/pbdrv/pb-write.c | 34 | ||||
-rw-r--r-- | lib/pbdrv/pb-write.h | 23 | ||||
-rw-r--r-- | test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | test/ExampleTest.cpp | 10 | ||||
-rw-r--r-- | test/pbdrv/main.cpp | 18 |
8 files changed, 96 insertions, 12 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 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 818c533..d5d6e0d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,15 +7,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1) project(pbtest C CXX ASM) add_executable(test - ExampleTest.cpp i2ctcp/main.cpp + pbdrv/main.cpp ) add_subdirectory(lib/googletest) add_subdirectory(lib/pbdrv) add_subdirectory(lib/i2ctcp) -# target_include_directories(tests PRIVATE ${CMAKE_CURRENT_LIST_DIR}) target_link_libraries(test gtest_main i2ctcp diff --git a/test/ExampleTest.cpp b/test/ExampleTest.cpp deleted file mode 100644 index 3da58ff..0000000 --- a/test/ExampleTest.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include <gtest/gtest.h> - -class ExampleTest : public testing::Test { -protected: -}; - -TEST_F(ExampleTest, Test) { - EXPECT_EQ(5, 5); -} - diff --git a/test/pbdrv/main.cpp b/test/pbdrv/main.cpp new file mode 100644 index 0000000..de7e88a --- /dev/null +++ b/test/pbdrv/main.cpp @@ -0,0 +1,18 @@ +#include <gtest/gtest.h> + +#include "pb-write.h" + +TEST(pbdrv, write) { + + pbdrv_buf_t buf = pbdrv_write_cmd_req_set_state({ + .header = { .sender = 0xf0, }, + .state = PB_GS_PLAYING, + }); + for (size_t i = 0; i < buf.size; i++) { + printf("%02x ", buf.data[i] & 0xff); + } + printf("\n"); + + ASSERT_TRUE(true); +} + |