diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-13 17:46:26 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-13 17:46:26 +0200 |
commit | 557b995f9641f7cb7a0566eefac7e576debec61a (patch) | |
tree | 1b2b0966f6b1671e5accfe0d980ee1f370d21b58 /lib | |
parent | da57aaed1295d72d8d840c69c0e08c0bcab46aee (diff) |
really WIP driver (de)serialization
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pbdrv/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/pbdrv/pb-read.c | 36 | ||||
-rw-r--r-- | lib/pbdrv/pb-read.h | 15 | ||||
-rw-r--r-- | lib/pbdrv/pb-serial.c | 32 | ||||
-rw-r--r-- | lib/pbdrv/pb-serial.h | 38 | ||||
-rw-r--r-- | lib/pbdrv/pb-types.h | 25 | ||||
-rw-r--r-- | lib/pbdrv/pb-write.c | 182 | ||||
-rw-r--r-- | lib/pbdrv/pb-write.h | 10 |
8 files changed, 223 insertions, 117 deletions
diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt index fe464a9..340e863 100644 --- a/lib/pbdrv/CMakeLists.txt +++ b/lib/pbdrv/CMakeLists.txt @@ -13,7 +13,7 @@ project(pbdrv C CXX) add_subdirectory(lib/mpack) # generic puzzle bus (de)serializer library -add_library(pbdrv STATIC pb-read.c pb-write.c) +add_library(pbdrv STATIC pb-read.c pb-write.c pb-serial.c) target_link_libraries(pbdrv mpack) target_include_directories(pbdrv SYSTEM INTERFACE .) diff --git a/lib/pbdrv/pb-read.c b/lib/pbdrv/pb-read.c index 843420d..ce8b5b1 100644 --- a/lib/pbdrv/pb-read.c +++ b/lib/pbdrv/pb-read.c @@ -1,3 +1,39 @@ +#include <mpack.h> + #include "pb-read.h" +// pb_msg_header_t * pbdrv_read_msg_header(mpack_reader_t * reader, pb_msg_header_t * header) { +// header->type = mpack_expect_u8(reader); +// header->sender = mpack_expect_u16(reader); +// +// return header; +// } +// +// pb_cmd_req_read_t * pbdrv_read_cmd_req_read(mpack_reader_t * reader, pb_cmd_req_read_t * msg) { +// msg->propid = mpack_expect_u8(reader); +// +// return msg; +// } +// +// void * pbdrv_read_unknown(const char * data, size_t size) { +// mpack_reader_t reader; +// mpack_reader_init_data(&reader, data, size); +// +// void * msg = NULL; +// +// pb_msg_header_t header; +// pbdrv_read_msg_header(&reader, &header); +// switch (header.type) { +// case PB_CMD_REQ_READ: { +// // pb_cmd_req_read_t msg; +// msg = malloc(sizeof(pb_cmd_req_read_t)); +// pbdrv_read_cmd_req_read(&reader, msg); +// break; +// } +// default: return NULL; +// } +// +// if (mpack_reader_error(&reader) != mpack_ok) return NULL; +// return NULL; +// } diff --git a/lib/pbdrv/pb-read.h b/lib/pbdrv/pb-read.h index a6d7cde..cb61e06 100644 --- a/lib/pbdrv/pb-read.h +++ b/lib/pbdrv/pb-read.h @@ -2,12 +2,25 @@ #include <stdint.h> #include <stddef.h> +#include <mpack.h> + +#include "pb-types.h" #ifdef __cplusplus extern "C" { #endif -void pbdrv_read_cmd(const char * data, size_t size); +// pb_msg_header_t * pbdrv_read_msg_header(mpack_reader_t * reader, pb_msg_header_t * header); +// pb_cmd_req_read_t * pbdrv_read_cmd_req_read(mpack_reader_t * reader, pb_cmd_req_read_t * msg); +// +// pb_cmd_res_read_t * pbdrv_read_cmd_res_read(const char * data, size_t size); +// pb_cmd_req_write_t * pbdrv_read_cmd_req_write(const char * data, size_t size); +// pb_cmd_req_state_t * pbdrv_read_cmd_req_state(const char * data, size_t size); +// pb_cmd_res_state_t * pbdrv_read_cmd_res_state(const char * data, size_t size); +// pb_cmd_req_set_state_t * pbdrv_read_cmd_req_set_state(const char * data, size_t size); +// pb_cmd_magic_t * pbdrv_read_cmd_magic(const char * data, size_t size); + +pb_msg_t * pbdrv_read_unknown(const pbdrv_buf_t * buf); #ifdef __cplusplus } diff --git a/lib/pbdrv/pb-serial.c b/lib/pbdrv/pb-serial.c new file mode 100644 index 0000000..bc213d5 --- /dev/null +++ b/lib/pbdrv/pb-serial.c @@ -0,0 +1,32 @@ +#include <mpack.h> + +#include "pb-serial.h" +#include "pb-types.h" + +void pbdrv_serialize(mpack_writer_t * writer, const pb_msg_t * msg) { + pbdrv_s_msg_header(writer, msg); + if (msg->msg == NULL) return; + + switch (msg->type) { + case PB_CMD_REQ_READ: return pbdrv_s_cmd_req_read(writer, msg->msg); + case PB_CMD_RES_READ: return pbdrv_s_cmd_res_read(writer, msg->msg); + default: break; + } +} + +void pbdrv_s_msg_header(mpack_writer_t * writer, const pb_msg_t * msg) { + mpack_write_u8(writer, msg->type); + // TODO: if pbdrv is compiled under the pbdrv-mod target, place + // PBDRV_MOD_ADDR in this field + mpack_write_u16(writer, msg->sender); +} + +void pbdrv_s_cmd_req_read(mpack_writer_t * writer, const pb_cmd_req_read_t * msg) { + mpack_write_u8(writer, msg->propid); +} + +void pbdrv_s_cmd_res_read(mpack_writer_t * writer, const pb_cmd_res_read_t * msg) { + mpack_write_u8(writer, msg->propid); + mpack_write_bin(writer, (char *) msg->value, msg->_value_size); +} + diff --git a/lib/pbdrv/pb-serial.h b/lib/pbdrv/pb-serial.h new file mode 100644 index 0000000..77e1345 --- /dev/null +++ b/lib/pbdrv/pb-serial.h @@ -0,0 +1,38 @@ +#pragma once + +#include <mpack.h> + +#include "pb-types.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +void pbdrv_serialize(mpack_writer_t * writer, const pb_msg_t * msg); + +void pbdrv_s_msg_header(mpack_writer_t * writer, const pb_msg_t * msg); +void pbdrv_s_cmd_req_read(mpack_writer_t * writer, const pb_cmd_req_read_t * msg); +void pbdrv_s_cmd_res_read(mpack_writer_t * writer, const pb_cmd_res_read_t * msg); +void pbdrv_s_cmd_req_write(mpack_writer_t * writer, const pb_cmd_req_write_t * msg); +void pbdrv_s_cmd_req_state(mpack_writer_t * writer, const pb_cmd_req_state_t * msg); +void pbdrv_s_cmd_res_state(mpack_writer_t * writer, const pb_cmd_res_state_t * msg); +void pbdrv_s_cmd_req_set_state(mpack_writer_t * writer, const pb_cmd_req_set_state_t * msg); +void pbdrv_s_cmd_req_magic(mpack_writer_t * writer, const pb_cmd_req_magic_t * msg); +void pbdrv_s_cmd_res_magic(mpack_writer_t * writer, const pb_cmd_res_magic_t * msg); + +// typedef void pbdrv_deserialize_t(mpack_reader_t * reader); +// pbdrv_deserialize_t pbdrv_d_msg_header; +// pbdrv_deserialize_t pbdrv_d_cmd_req_read; +// pbdrv_deserialize_t pbdrv_d_cmd_res_read; +// pbdrv_deserialize_t pbdrv_d_cmd_req_write; +// pbdrv_deserialize_t pbdrv_d_cmd_req_state; +// pbdrv_deserialize_t pbdrv_d_cmd_res_state; +// pbdrv_deserialize_t pbdrv_d_cmd_req_set_state; +// pbdrv_deserialize_t pbdrv_d_cmd_req_magic; +// pbdrv_deserialize_t pbdrv_d_cmd_res_magic; + +#ifdef __cplusplus +} +#endif + diff --git a/lib/pbdrv/pb-types.h b/lib/pbdrv/pb-types.h index 0cafab5..4974270 100644 --- a/lib/pbdrv/pb-types.h +++ b/lib/pbdrv/pb-types.h @@ -32,12 +32,13 @@ enum pb_cmd_id { 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) + PB_CMD_REQ_MAGIC, //!< magic request + PB_CMD_RES_MAGIC, //!< magic response }; typedef enum pb_cmd_id pb_cmd_id_t; //! magic sent from main controller to puzzle module -static const char pb_cmd_magic_msg[] = { 0x70, 0x75, 0x7a, 0x62, 0x75, 0x73 }; +static const char pb_cmd_magic_req[] = { 0x70, 0x75, 0x7a, 0x62, 0x75, 0x73 }; //! magic reply from puzzle module back to main controller static const char pb_cmd_magic_res[] = { 0x67, 0x61, 0x6d, 0x69, 0x6e, 0x67 }; @@ -54,17 +55,16 @@ typedef enum pb_global_state pb_global_state_t; typedef struct { pb_cmd_id_t type; //!< command type i2c_addr_t sender; //!< i2c address of sender -} pb_msg_header_t; + void * msg; //!< remaining message (type dependant) +} pb_msg_t; //! PB_CMD_REQ_READ data typedef struct { - 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 { - pb_msg_header_t header; uint8_t propid; //!< id of returned state property const uint8_t * value; size_t _value_size; @@ -72,7 +72,6 @@ typedef struct { //! PB_CMD_REQ_WRITE data typedef struct { - 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; @@ -80,28 +79,30 @@ typedef struct { //! PB_CMD_REQ_STATE data typedef struct { - 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 { - 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 { - pb_msg_header_t header; pb_global_state_t state; //!< new global state } pb_cmd_req_set_state_t; -//! PB_CMD_MAGIC data +//! PB_CMD_REQ_MAGIC data typedef struct { - pb_msg_header_t header; const char * magic; //!< magic value size_t _magic_size; -} pb_cmd_magic_t; +} pb_cmd_req_magic_t; + +//! PB_CMD_RES_MAGIC data +typedef struct { + const char * magic; //!< magic value + size_t _magic_size; +} pb_cmd_res_magic_t; #ifdef __cplusplus } diff --git a/lib/pbdrv/pb-write.c b/lib/pbdrv/pb-write.c index 1a3f24e..445dd0b 100644 --- a/lib/pbdrv/pb-write.c +++ b/lib/pbdrv/pb-write.c @@ -1,107 +1,97 @@ #include <mpack.h> #include "pb-write.h" +#include "pb-serial.h" -typedef struct { +pbdrv_buf_t pbdrv_write_msg(const pb_msg_t * msg) { + pbdrv_buf_t buf = { 0 }; mpack_writer_t writer; - pbdrv_buf_t buf; -} pbdrv_writer_t; -static void pbdrv_write_init(pbdrv_writer_t * writer) { - mpack_writer_init_growable(&writer->writer, &writer->buf.data, &writer->buf.size); -} - -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); - // 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); + mpack_writer_init_growable(&writer, &buf.data, &buf.size); - pbdrv_write_msg_header(&writer, data.header); - mpack_write_u8(&writer.writer, data.propid); + pbdrv_serialize(&writer, msg); - return pbdrv_write_finish(&writer); + mpack_writer_destroy(&writer); + return buf; } -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); -} +// +// 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/lib/pbdrv/pb-write.h b/lib/pbdrv/pb-write.h index 3bd53c5..c77db47 100644 --- a/lib/pbdrv/pb-write.h +++ b/lib/pbdrv/pb-write.h @@ -1,18 +1,14 @@ #pragma once +#include <mpack.h> + #include "pb-types.h" #ifdef __cplusplus extern "C" { #endif -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); +pbdrv_buf_t pbdrv_write_msg(const pb_msg_t * msg); #ifdef __cplusplus } |