diff options
-rw-r--r-- | lib/pbdrv/pb-types.h | 39 | ||||
-rw-r--r-- | lib/pbdrv/pb-write.c | 73 | ||||
-rw-r--r-- | test/pbdrv/write.cpp | 60 |
3 files changed, 153 insertions, 19 deletions
diff --git a/lib/pbdrv/pb-types.h b/lib/pbdrv/pb-types.h index 96ffc37..76292f6 100644 --- a/lib/pbdrv/pb-types.h +++ b/lib/pbdrv/pb-types.h @@ -44,52 +44,55 @@ typedef enum pb_global_state pb_global_state_t; //! puzzle bus message header (shared by all commands) typedef struct { - const pb_cmd_id_t type; //!< command type - const i2c_addr_t sender; //!< i2c address of sender + pb_cmd_id_t type; //!< command type + i2c_addr_t sender; //!< i2c address of sender } pb_msg_header_t; //! PB_CMD_REQ_READ data typedef struct { - const pb_msg_header_t header; - const uint8_t propid; //!< state property id to return + pb_msg_header_t header; + uint8_t propid; //!< state property id to return } pb_cmd_req_read_t; //! PB_CMD_RES_READ data typedef struct { - const pb_msg_header_t header; - const uint8_t propid; //!< id of returned state property - const uint8_t value[]; + pb_msg_header_t header; + uint8_t propid; //!< id of returned state property + const uint8_t * value; + size_t _value_size; } pb_cmd_res_read_t; //! PB_CMD_REQ_WRITE data typedef struct { - const pb_msg_header_t header; - const uint8_t propid; //!< state property id to write - const uint8_t value[]; //!< new value of property + pb_msg_header_t header; + uint8_t propid; //!< state property id to write + const uint8_t * value; //!< new value of property + size_t _value_size; } pb_cmd_req_write_t; //! PB_CMD_REQ_STATE data typedef struct { - const pb_msg_header_t header; - const pb_global_state_t state; //!< global state of sender + pb_msg_header_t header; + pb_global_state_t state; //!< global state of sender } pb_cmd_req_state_t; //! PB_CMD_RES_STATE data typedef struct { - const pb_msg_header_t header; - const pb_global_state_t state; //!< global state of sender + pb_msg_header_t header; + 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_msg_header_t header; + 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; - const char magic[]; //!< magic value + pb_msg_header_t header; + const char * magic; //!< magic value + size_t _magic_size; } pb_cmd_magic_t; #ifdef __cplusplus diff --git a/lib/pbdrv/pb-write.c b/lib/pbdrv/pb-write.c index 294452e..1a3f24e 100644 --- a/lib/pbdrv/pb-write.c +++ b/lib/pbdrv/pb-write.c @@ -21,14 +21,87 @@ static pbdrv_buf_t pbdrv_write_finish(pbdrv_writer_t * writer) { static void pbdrv_write_msg_header(pbdrv_writer_t * writer, pb_msg_header_t header) { mpack_write_u8(&writer->writer, header.type); + // TODO: if pbdrv is compiled under the pbdrv-mod target, place + // PBDRV_MOD_ADDR in this field mpack_write_u16(&writer->writer, header.sender); } +pbdrv_buf_t pbdrv_write_cmd_req_read(pb_cmd_req_read_t data) { + data.header.type = PB_CMD_REQ_READ; + pbdrv_writer_t writer; + pbdrv_write_init(&writer); + + pbdrv_write_msg_header(&writer, data.header); + mpack_write_u8(&writer.writer, data.propid); + + return pbdrv_write_finish(&writer); +} + +pbdrv_buf_t pbdrv_write_cmd_res_read(pb_cmd_res_read_t data) { + data.header.type = PB_CMD_RES_READ; + pbdrv_writer_t writer; + pbdrv_write_init(&writer); + + pbdrv_write_msg_header(&writer, data.header); + mpack_write_u8(&writer.writer, data.propid); + mpack_write_bin(&writer.writer, (char *) data.value, data._value_size); + + return pbdrv_write_finish(&writer); +} + +pbdrv_buf_t pbdrv_write_cmd_req_write(pb_cmd_req_write_t data) { + data.header.type = PB_CMD_REQ_WRITE; + pbdrv_writer_t writer; + pbdrv_write_init(&writer); + + pbdrv_write_msg_header(&writer, data.header); + mpack_write_u8(&writer.writer, data.propid); + mpack_write_bin(&writer.writer, (char *) data.value, data._value_size); + + return pbdrv_write_finish(&writer); +} + +pbdrv_buf_t pbdrv_write_cmd_req_state(pb_cmd_req_state_t data) { + data.header.type = PB_CMD_REQ_STATE; + pbdrv_writer_t writer; + pbdrv_write_init(&writer); + + pbdrv_write_msg_header(&writer, data.header); + mpack_write_u8(&writer.writer, data.state); + + return pbdrv_write_finish(&writer); +} + +pbdrv_buf_t pbdrv_write_cmd_res_state(pb_cmd_res_state_t data) { + data.header.type = PB_CMD_RES_STATE; + pbdrv_writer_t writer; + pbdrv_write_init(&writer); + + pbdrv_write_msg_header(&writer, data.header); + mpack_write_u8(&writer.writer, data.state); + + return pbdrv_write_finish(&writer); +} + pbdrv_buf_t pbdrv_write_cmd_req_set_state(pb_cmd_req_set_state_t data) { + data.header.type = PB_CMD_REQ_SET_STATE; pbdrv_writer_t writer; pbdrv_write_init(&writer); + pbdrv_write_msg_header(&writer, data.header); mpack_write_u8(&writer.writer, data.state); + + return pbdrv_write_finish(&writer); +} + +pbdrv_buf_t pbdrv_write_cmd_magic(pb_cmd_magic_t data) { + data.header.type = PB_CMD_MAGIC; + pbdrv_writer_t writer; + pbdrv_write_init(&writer); + + pbdrv_write_msg_header(&writer, data.header); + mpack_write_bin(&writer.writer, data.magic, data._magic_size); + return pbdrv_write_finish(&writer); } diff --git a/test/pbdrv/write.cpp b/test/pbdrv/write.cpp index 31e5d8d..ad775bb 100644 --- a/test/pbdrv/write.cpp +++ b/test/pbdrv/write.cpp @@ -2,9 +2,57 @@ #include "pb-write.h" +TEST(pbdrv, write_cmd_req_read) { + pbdrv_buf_t buf = pbdrv_write_cmd_req_read({ + .propid = 0, + }); + + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); +} + +TEST(pbdrv, write_cmd_res_read) { + pbdrv_buf_t buf = pbdrv_write_cmd_res_read({ + .propid = 0, + .value = (uint8_t[]) { 0x00, }, + ._value_size = 1, + }); + + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); +} + +TEST(pbdrv, write_cmd_req_write) { + pbdrv_buf_t buf = pbdrv_write_cmd_req_write({ + .propid = 0, + .value = (uint8_t[]) { 0x00, }, + ._value_size = 1, + }); + + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); +} + +TEST(pbdrv, write_cmd_req_state) { + pbdrv_buf_t buf = pbdrv_write_cmd_req_state({ + .state = PB_GS_PLAYING, + }); + + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); +} + +TEST(pbdrv, write_cmd_res_state) { + pbdrv_buf_t buf = pbdrv_write_cmd_res_state({ + .state = PB_GS_IDLE, + }); + + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); +} + TEST(pbdrv, write_cmd_req_set_state) { pbdrv_buf_t buf = pbdrv_write_cmd_req_set_state({ - .header = { .sender = 0xf0, }, .state = PB_GS_PLAYING, }); @@ -12,3 +60,13 @@ TEST(pbdrv, write_cmd_req_set_state) { ASSERT_GE(buf.size, 0); } +TEST(pbdrv, write_cmd_magic) { + pbdrv_buf_t buf = pbdrv_write_cmd_magic({ + .magic = pb_cmd_magic_msg, + ._magic_size = sizeof(pb_cmd_magic_msg), + }); + + ASSERT_NE(buf.data, nullptr); + ASSERT_GE(buf.size, 0); +} + |