diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-14 17:23:22 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-14 17:23:22 +0200 |
commit | 15ee8bd8885c8b3d0b4650fe609c253780f04bdf (patch) | |
tree | df952b8f62dce49283a8f3937771ccc572fc13ad /lib/pbdrv | |
parent | ef162ca3445d9adb000d7dfd1b68b181ef958926 (diff) |
WIP more puzzle bus driver code
Diffstat (limited to 'lib/pbdrv')
-rw-r--r-- | lib/pbdrv/drv/arduino/mod.cpp | 1 | ||||
-rw-r--r-- | lib/pbdrv/pb-msg.c | 12 | ||||
-rw-r--r-- | lib/pbdrv/pb-msg.h | 12 | ||||
-rw-r--r-- | lib/pbdrv/pb-serial.c | 54 | ||||
-rw-r--r-- | lib/pbdrv/pb-types.h | 4 |
5 files changed, 81 insertions, 2 deletions
diff --git a/lib/pbdrv/drv/arduino/mod.cpp b/lib/pbdrv/drv/arduino/mod.cpp index fab9dd5..ac051db 100644 --- a/lib/pbdrv/drv/arduino/mod.cpp +++ b/lib/pbdrv/drv/arduino/mod.cpp @@ -25,6 +25,7 @@ void pb_setup() { Wire.begin((int) PB_MOD_ADDR); Wire.setWireTimeout(PB_TIMEOUT_US, true); Wire.setClock(PB_CLOCK_SPEED_HZ); + // TODO: check if onReceive replaces or appends a handler function Wire.onReceive(recv_event); } diff --git a/lib/pbdrv/pb-msg.c b/lib/pbdrv/pb-msg.c index 8ffd7cc..8bf96ef 100644 --- a/lib/pbdrv/pb-msg.c +++ b/lib/pbdrv/pb-msg.c @@ -35,3 +35,15 @@ void pb_msg_free(pb_msg_t * msg) { free(msg); } +pb_buf_t pb_msg_write_req_magic() { + pb_cmd_req_magic_t content = { + .magic = (char *) &pb_cmd_magic_req[0], + ._magic_size = sizeof(pb_cmd_magic_req), + }; + pb_msg_t msg_write = { + .type = PB_CMD_REQ_MAGIC, + .sender = 0, + .msg = &content, + }; + return pb_msg_write(&msg_write); +} diff --git a/lib/pbdrv/pb-msg.h b/lib/pbdrv/pb-msg.h index f27d4c4..6759b7c 100644 --- a/lib/pbdrv/pb-msg.h +++ b/lib/pbdrv/pb-msg.h @@ -11,6 +11,18 @@ pb_buf_t pb_msg_write(const pb_msg_t * msg); pb_msg_t * pb_msg_read(const pb_buf_t * buf); void pb_msg_free(pb_msg_t * msg); +// pb_buf_t pb_msg_write_cmd_req_read(const pb_cmd_req_read_t); +// pb_buf_t pb_msg_write_cmd_res_read(const pb_cmd_res_read_t); +// pb_buf_t pb_msg_write_cmd_req_write +// pb_buf_t pb_msg_write_cmd_req_state +// pb_buf_t pb_msg_write_cmd_res_state +// pb_buf_t pb_msg_write_cmd_req_set_state +// pb_buf_t pb_msg_write_cmd_req_magic +// pb_buf_t pb_msg_write_cmd_res_magic + +pb_buf_t pb_msg_write_req_magic(); +pb_buf_t pb_msg_write_res_magic(); + #ifdef __cplusplus } #endif diff --git a/lib/pbdrv/pb-serial.c b/lib/pbdrv/pb-serial.c index 9b20bf9..7eb738b 100644 --- a/lib/pbdrv/pb-serial.c +++ b/lib/pbdrv/pb-serial.c @@ -9,6 +9,8 @@ void pb_ser_w(mpack_writer_t * writer, const pb_msg_t * msg) { switch (msg->type) { case PB_CMD_REQ_READ: return pb_ser_w_cmd_req_read(writer, msg); case PB_CMD_RES_READ: return pb_ser_w_cmd_res_read(writer, msg); + case PB_CMD_REQ_MAGIC: return pb_ser_w_cmd_req_magic(writer, msg); + case PB_CMD_RES_MAGIC: return pb_ser_w_cmd_res_magic(writer, msg); default: break; } } @@ -17,6 +19,8 @@ void pb_ser_r(mpack_reader_t * reader, pb_msg_t * msg) { switch (msg->type) { case PB_CMD_REQ_READ: return pb_ser_r_cmd_req_read(reader, msg); case PB_CMD_RES_READ: return pb_ser_r_cmd_res_read(reader, msg); + case PB_CMD_REQ_MAGIC: return pb_ser_r_cmd_req_magic(reader, msg); + case PB_CMD_RES_MAGIC: return pb_ser_r_cmd_res_magic(reader, msg); default: break; } } @@ -26,6 +30,8 @@ void pb_ser_free(pb_msg_t * msg) { switch (msg->type) { case PB_CMD_REQ_READ: return pb_ser_free_cmd_req_read(msg); case PB_CMD_RES_READ: return pb_ser_free_cmd_res_read(msg); + case PB_CMD_REQ_MAGIC: return pb_ser_free_cmd_req_magic(msg); + case PB_CMD_RES_MAGIC: return pb_ser_free_cmd_res_magic(msg); default: break; } pb_ser_free_msg_header(msg); @@ -84,3 +90,51 @@ void pb_ser_free_cmd_res_read(pb_msg_t * _msg) { } } +void pb_ser_w_cmd_req_magic(mpack_writer_t * writer, const pb_msg_t * _msg) { + pb_cmd_req_magic_t * msg = _msg->msg; + + mpack_write_bin(writer, (char *) msg->magic, msg->_magic_size); +} +void pb_ser_r_cmd_req_magic(mpack_reader_t * reader, pb_msg_t * _msg) { + pb_cmd_req_magic_t * msg = _msg->msg = malloc(sizeof(pb_cmd_req_magic_t)); + + msg->_magic_size = mpack_expect_bin(reader); + msg->magic = mpack_read_bytes_alloc(reader, msg->_magic_size); + mpack_done_bin(reader); +} +void pb_ser_free_cmd_req_magic(pb_msg_t * _msg) { + if (_msg->msg != NULL) { + pb_cmd_req_magic_t * msg = _msg->msg; + if (msg->magic != NULL) { + MPACK_FREE(msg->magic); + msg->magic = NULL; + } + free(_msg->msg); + _msg->msg = NULL; + } +} + +void pb_ser_w_cmd_res_magic(mpack_writer_t * writer, const pb_msg_t * _msg) { + pb_cmd_res_magic_t * msg = _msg->msg; + + mpack_write_bin(writer, (char *) msg->magic, msg->_magic_size); +} +void pb_ser_r_cmd_res_magic(mpack_reader_t * reader, pb_msg_t * _msg) { + pb_cmd_res_magic_t * msg = _msg->msg = malloc(sizeof(pb_cmd_res_magic_t)); + + msg->_magic_size = mpack_expect_bin(reader); + msg->magic = mpack_read_bytes_alloc(reader, msg->_magic_size); + mpack_done_bin(reader); +} +void pb_ser_free_cmd_res_magic(pb_msg_t * _msg) { + if (_msg->msg != NULL) { + pb_cmd_res_magic_t * msg = _msg->msg; + if (msg->magic != NULL) { + MPACK_FREE(msg->magic); + msg->magic = NULL; + } + free(_msg->msg); + _msg->msg = NULL; + } +} + diff --git a/lib/pbdrv/pb-types.h b/lib/pbdrv/pb-types.h index 7156272..53060ae 100644 --- a/lib/pbdrv/pb-types.h +++ b/lib/pbdrv/pb-types.h @@ -88,13 +88,13 @@ typedef struct { //! PB_CMD_REQ_MAGIC data typedef struct { - const char * magic; //!< magic value + char * magic; //!< magic value size_t _magic_size; } pb_cmd_req_magic_t; //! PB_CMD_RES_MAGIC data typedef struct { - const char * magic; //!< magic value + char * magic; //!< magic value size_t _magic_size; } pb_cmd_res_magic_t; |