aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pbdrv/CMakeLists.txt1
-rw-r--r--lib/pbdrv/pb-mod.c18
-rw-r--r--lib/pbdrv/pb-mod.h3
-rw-r--r--lib/pbdrv/pb-msg.c17
-rw-r--r--lib/pbdrv/pb-msg.h12
-rw-r--r--lib/pbdrv/pb-send.c116
-rw-r--r--lib/pbdrv/pb-send.h22
-rw-r--r--lib/pbdrv/pb-serial.c123
-rw-r--r--lib/pbdrv/pb-types.h2
9 files changed, 224 insertions, 90 deletions
diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt
index f1b1d62..ce77d43 100644
--- a/lib/pbdrv/CMakeLists.txt
+++ b/lib/pbdrv/CMakeLists.txt
@@ -24,6 +24,7 @@ target_include_directories(pbdrv SYSTEM INTERFACE .)
# puzzle bus module specific code (superset of pbdrv)
add_library(pbdrv-mod STATIC
pb-mod.c
+ pb-send.c
)
target_link_libraries(pbdrv-mod pbdrv)
target_include_directories(pbdrv-mod SYSTEM INTERFACE .)
diff --git a/lib/pbdrv/pb-mod.c b/lib/pbdrv/pb-mod.c
index f5c9a34..b5626d3 100644
--- a/lib/pbdrv/pb-mod.c
+++ b/lib/pbdrv/pb-mod.c
@@ -7,20 +7,20 @@ __weak const char * PB_MOD_NAME = "???";
//! [private] placeholder global state variable
static pb_global_state_t _global_state = PB_GS_NOINIT;
-//! [private] main controller global state
-static pb_global_state_t _main_state = PB_GS_NOINIT;
+// //! [private] main controller global state
+// static pb_global_state_t _main_state = PB_GS_NOINIT;
-// __weak enum pb_state pb_hook_mod_state_read() {
-// return _global_state;
-// }
+__weak pb_global_state_t pb_hook_mod_state_read() {
+ return _global_state;
+}
-// __weak void pb_hook_mod_state_write(enum pb_state state) {
-// _global_state = state;
-// }
+__weak void pb_hook_mod_state_write(pb_global_state_t state) {
+ _global_state = state;
+}
__weak void pb_i2c_recv(const uint8_t * buf, size_t sz) {
return;
}
-__weak void pb_hook_main_state_update(pb_global_state_t state) { }
+// __weak void pb_hook_main_state_update(pb_global_state_t state) { }
diff --git a/lib/pbdrv/pb-mod.h b/lib/pbdrv/pb-mod.h
index 549bdcb..7dfc945 100644
--- a/lib/pbdrv/pb-mod.h
+++ b/lib/pbdrv/pb-mod.h
@@ -28,6 +28,9 @@ extern const i2c_addr_t PB_MOD_ADDR;
void pb_i2c_recv(const uint8_t * buf, size_t sz);
void pb_i2c_send(i2c_addr_t i2c_addr, const uint8_t * buf, size_t sz);
+pb_global_state_t pb_hook_mod_state_read();
+void pb_hook_mod_state_write(pb_global_state_t state);
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/pbdrv/pb-msg.c b/lib/pbdrv/pb-msg.c
index 695a696..3f6fa95 100644
--- a/lib/pbdrv/pb-msg.c
+++ b/lib/pbdrv/pb-msg.c
@@ -5,8 +5,9 @@
pb_buf_t pb_msg_write(const pb_msg_t * msg) {
pb_buf_t buf = { 0 };
- mpack_writer_t writer;
+ if (msg == NULL) return buf;
+ mpack_writer_t writer;
mpack_writer_init_growable(&writer, &buf.data, &buf.size);
pb_ser_w(&writer, msg);
@@ -35,17 +36,3 @@ void pb_msg_free(pb_msg_t * msg) {
free(msg);
}
-pb_buf_t pb_msg_write_req_magic() {
- pb_cmd_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_MAGIC,
- .action = PB_ACTION_REQ,
- .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 6759b7c..f27d4c4 100644
--- a/lib/pbdrv/pb-msg.h
+++ b/lib/pbdrv/pb-msg.h
@@ -11,18 +11,6 @@ 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-send.c b/lib/pbdrv/pb-send.c
new file mode 100644
index 0000000..09a35a8
--- /dev/null
+++ b/lib/pbdrv/pb-send.c
@@ -0,0 +1,116 @@
+#include "pb-send.h"
+#include "pb-mod.h"
+#include "pb-msg.h"
+
+pb_buf_t pb_send_req_read(uint8_t propid) {
+ pb_cmd_prop_t cmd = {
+ .propid = propid,
+ .value = NULL,
+ ._value_size = 0,
+ };
+ pb_msg_t msg = {
+ .type = PB_CMD_PROP,
+ .action = PB_ACTION_REQ,
+ .sender = PB_MOD_ADDR,
+ .cmd = &cmd,
+ };
+ return pb_msg_write(&msg);
+}
+
+pb_buf_t pb_send_res_read(uint8_t propid, uint8_t * value, size_t size) {
+ pb_cmd_prop_t cmd = {
+ .propid = propid,
+ .value = value,
+ ._value_size = size,
+ };
+ pb_msg_t msg = {
+ .type = PB_CMD_PROP,
+ .action = PB_ACTION_RES,
+ .sender = PB_MOD_ADDR,
+ .cmd = &cmd,
+ };
+ return pb_msg_write(&msg);
+}
+
+pb_buf_t pb_send_req_write(uint8_t propid, uint8_t * value, size_t size) {
+ pb_cmd_prop_t cmd = {
+ .propid = propid,
+ .value = value,
+ ._value_size = size,
+ };
+ pb_msg_t msg = {
+ .type = PB_CMD_PROP,
+ .action = PB_ACTION_REQ,
+ .sender = PB_MOD_ADDR,
+ .cmd = &cmd,
+ };
+ return pb_msg_write(&msg);
+}
+
+pb_buf_t pb_send_req_state() {
+ pb_cmd_state_t cmd = {
+ .state = pb_hook_mod_state_read(),
+ };
+ pb_msg_t msg = {
+ .type = PB_CMD_STATE,
+ .action = PB_ACTION_REQ,
+ .sender = PB_MOD_ADDR,
+ .cmd = &cmd,
+ };
+ return pb_msg_write(&msg);
+}
+
+pb_buf_t pb_send_res_state(pb_global_state_t state) {
+ pb_cmd_state_t cmd = {
+ .state = state,
+ };
+ pb_msg_t msg = {
+ .type = PB_CMD_STATE,
+ .action = PB_ACTION_RES,
+ .sender = PB_MOD_ADDR,
+ .cmd = &cmd,
+ };
+ return pb_msg_write(&msg);
+}
+
+pb_buf_t pb_send_req_set_state(pb_global_state_t state) {
+ pb_cmd_state_t cmd = {
+ .state = state,
+ };
+ pb_msg_t msg = {
+ .type = PB_CMD_STATE,
+ .action = PB_ACTION_SET,
+ .sender = PB_MOD_ADDR,
+ .cmd = &cmd,
+ };
+ return pb_msg_write(&msg);
+}
+
+pb_buf_t pb_send_req_magic() {
+ pb_cmd_magic_t cmd = {
+ .magic = (char *) &pb_cmd_magic_req[0],
+ ._magic_size = sizeof(pb_cmd_magic_req),
+ };
+ pb_msg_t msg = {
+ .type = PB_CMD_MAGIC,
+ .action = PB_ACTION_REQ,
+ .sender = PB_MOD_ADDR,
+ .cmd = &cmd,
+ };
+ return pb_msg_write(&msg);
+}
+
+pb_buf_t pb_send_res_magic() {
+ pb_cmd_magic_t cmd = {
+ .magic = (char *) &pb_cmd_magic_res[0],
+ ._magic_size = sizeof(pb_cmd_magic_res),
+ };
+ pb_msg_t msg = {
+ .type = PB_CMD_MAGIC,
+ .action = PB_ACTION_RES,
+ .sender = PB_MOD_ADDR,
+ .cmd = &cmd,
+ };
+ return pb_msg_write(&msg);
+}
+
diff --git a/lib/pbdrv/pb-send.h b/lib/pbdrv/pb-send.h
new file mode 100644
index 0000000..fff9956
--- /dev/null
+++ b/lib/pbdrv/pb-send.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "pb-types.h"
+#include "pb-buf.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+pb_buf_t pb_send_req_read(uint8_t propid);
+pb_buf_t pb_send_res_read(uint8_t propid, uint8_t * value, size_t size);
+pb_buf_t pb_send_req_write(uint8_t propid, uint8_t * value, size_t size);
+pb_buf_t pb_send_req_state();
+pb_buf_t pb_send_res_state(pb_global_state_t state);
+pb_buf_t pb_send_req_set_state(pb_global_state_t state);
+pb_buf_t pb_send_req_magic();
+pb_buf_t pb_send_res_magic();
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/lib/pbdrv/pb-serial.c b/lib/pbdrv/pb-serial.c
index e9806e0..29a307c 100644
--- a/lib/pbdrv/pb-serial.c
+++ b/lib/pbdrv/pb-serial.c
@@ -3,91 +3,108 @@
#include "pb-serial.h"
#include "pb-types.h"
-void pb_ser_w(mpack_writer_t * writer, const pb_msg_t * msg) {
- mpack_write_u8(writer, msg->type);
- mpack_write_u8(writer, msg->action);
- // TODO: if pb is compiled under the pb-mod target, place
- // PB_MOD_ADDR in this field
- mpack_write_u16(writer, msg->sender);
-
- if (msg->msg == NULL) return;
- switch (msg->type) {
- case PB_CMD_PROP: return pb_ser_w_cmd_prop(writer, msg);
- // case PB_CMD_STATE: return pb_ser_w_cmd_state(writer, msg);
- case PB_CMD_MAGIC: return pb_ser_w_cmd_magic(writer, msg);
+void pb_ser_w(mpack_writer_t * writer, const pb_msg_t * cmd) {
+ if (cmd == NULL) return;
+
+ mpack_write_u8(writer, cmd->type);
+ mpack_write_u8(writer, cmd->action);
+ mpack_write_u16(writer, cmd->sender);
+ if (cmd->cmd == NULL) return;
+
+ switch (cmd->type) {
+ case PB_CMD_PROP: return pb_ser_w_cmd_prop(writer, cmd);
+ case PB_CMD_STATE: return pb_ser_w_cmd_state(writer, cmd);
+ case PB_CMD_MAGIC: return pb_ser_w_cmd_magic(writer, cmd);
default: break;
}
}
-void pb_ser_r(mpack_reader_t * reader, pb_msg_t * msg) {
- msg->type = mpack_expect_u8(reader);
- msg->action = mpack_expect_u8(reader);
- msg->sender = mpack_expect_u16(reader);
-
- switch (msg->type) {
- case PB_CMD_PROP: return pb_ser_r_cmd_prop(reader, msg);
- // case PB_CMD_STATE: return pb_ser_r_cmd_state(reader, msg);
- case PB_CMD_MAGIC: return pb_ser_r_cmd_magic(reader, msg);
+void pb_ser_r(mpack_reader_t * reader, pb_msg_t * cmd) {
+ cmd->type = mpack_expect_u8(reader);
+ cmd->action = mpack_expect_u8(reader);
+ cmd->sender = mpack_expect_u16(reader);
+
+ switch (cmd->type) {
+ case PB_CMD_PROP: return pb_ser_r_cmd_prop(reader, cmd);
+ case PB_CMD_STATE: return pb_ser_r_cmd_state(reader, cmd);
+ case PB_CMD_MAGIC: return pb_ser_r_cmd_magic(reader, cmd);
default: break;
}
}
-void pb_ser_free(pb_msg_t * msg) {
- if (msg == NULL) return;
+void pb_ser_free(pb_msg_t * cmd) {
+ if (cmd == NULL) return;
- switch (msg->type) {
- case PB_CMD_PROP: return pb_ser_free_cmd_prop(msg);
- // case PB_CMD_STATE: return pb_ser_free_cmd_state(msg);
- case PB_CMD_MAGIC: return pb_ser_free_cmd_magic(msg);
+ switch (cmd->type) {
+ case PB_CMD_PROP: return pb_ser_free_cmd_prop(cmd);
+ case PB_CMD_STATE: return pb_ser_free_cmd_state(cmd);
+ case PB_CMD_MAGIC: return pb_ser_free_cmd_magic(cmd);
default: break;
}
}
void pb_ser_w_cmd_prop(mpack_writer_t * writer, const pb_msg_t * _msg) {
- pb_cmd_prop_t * msg = _msg->msg;
+ pb_cmd_prop_t * cmd = _msg->cmd;
- mpack_write_u8(writer, msg->propid);
- mpack_write_bin(writer, (char *) msg->value, msg->_value_size);
+ mpack_write_u8(writer, cmd->propid);
+ mpack_write_bin(writer, (char *) cmd->value, cmd->_value_size);
}
void pb_ser_r_cmd_prop(mpack_reader_t * reader, pb_msg_t * _msg) {
- pb_cmd_prop_t * msg = _msg->msg = malloc(sizeof(pb_cmd_prop_t));
+ pb_cmd_prop_t * cmd = _msg->cmd = malloc(sizeof(pb_cmd_prop_t));
- msg->propid = mpack_expect_u8(reader);
- msg->_value_size = mpack_expect_bin(reader);
- msg->value = (uint8_t *) mpack_read_bytes_alloc(reader, msg->_value_size);
+ cmd->propid = mpack_expect_u8(reader);
+ cmd->_value_size = mpack_expect_bin(reader);
+ cmd->value = (uint8_t *) mpack_read_bytes_alloc(reader, cmd->_value_size);
mpack_done_bin(reader);
}
void pb_ser_free_cmd_prop(pb_msg_t * _msg) {
- if (_msg->msg != NULL) {
- pb_cmd_prop_t * msg = _msg->msg;
- if (msg->value != NULL) {
- MPACK_FREE(msg->value);
- msg->value = NULL;
+ if (_msg->cmd != NULL) {
+ pb_cmd_prop_t * cmd = _msg->cmd;
+ if (cmd->value != NULL) {
+ MPACK_FREE(cmd->value);
+ cmd->value = NULL;
}
- free(_msg->msg);
- _msg->msg = NULL;
+ free(_msg->cmd);
+ _msg->cmd = NULL;
+ }
+}
+
+void pb_ser_w_cmd_state(mpack_writer_t * writer, const pb_msg_t * _msg) {
+ pb_cmd_state_t * cmd = _msg->cmd;
+
+ mpack_write_u8(writer, cmd->state);
+}
+void pb_ser_r_cmd_state(mpack_reader_t * reader, pb_msg_t * _msg) {
+ pb_cmd_state_t * cmd = _msg->cmd = malloc(sizeof(pb_cmd_state_t));
+
+ cmd->state = mpack_expect_u8(reader);
+}
+void pb_ser_free_cmd_state(pb_msg_t * _msg) {
+ if (_msg->cmd != NULL) {
+ free(_msg->cmd);
+ _msg->cmd = NULL;
}
}
void pb_ser_w_cmd_magic(mpack_writer_t * writer, const pb_msg_t * _msg) {
- pb_cmd_magic_t * msg = _msg->msg;
+ pb_cmd_magic_t * cmd = _msg->cmd;
- mpack_write_bin(writer, (char *) msg->magic, msg->_magic_size);
+ mpack_write_bin(writer, (char *) cmd->magic, cmd->_magic_size);
}
void pb_ser_r_cmd_magic(mpack_reader_t * reader, pb_msg_t * _msg) {
- pb_cmd_magic_t * msg = _msg->msg = malloc(sizeof(pb_cmd_magic_t));
+ pb_cmd_magic_t * cmd = _msg->cmd = malloc(sizeof(pb_cmd_magic_t));
- msg->_magic_size = mpack_expect_bin(reader);
- msg->magic = mpack_read_bytes_alloc(reader, msg->_magic_size);
+ cmd->_magic_size = mpack_expect_bin(reader);
+ cmd->magic = mpack_read_bytes_alloc(reader, cmd->_magic_size);
mpack_done_bin(reader);
}
void pb_ser_free_cmd_magic(pb_msg_t * _msg) {
- if (_msg->msg != NULL) {
- pb_cmd_magic_t * msg = _msg->msg;
- if (msg->magic != NULL) {
- MPACK_FREE(msg->magic);
- msg->magic = NULL;
+ if (_msg->cmd != NULL) {
+ pb_cmd_magic_t * cmd = _msg->cmd;
+ if (cmd->magic != NULL) {
+ MPACK_FREE(cmd->magic);
+ cmd->magic = NULL;
}
- free(_msg->msg);
- _msg->msg = NULL;
+ free(_msg->cmd);
+ _msg->cmd = NULL;
}
}
diff --git a/lib/pbdrv/pb-types.h b/lib/pbdrv/pb-types.h
index e3280da..4d085f9 100644
--- a/lib/pbdrv/pb-types.h
+++ b/lib/pbdrv/pb-types.h
@@ -54,7 +54,7 @@ typedef struct {
pb_cmd_id_t type; //!< command type
pb_action_t action; //!< command action
i2c_addr_t sender; //!< i2c address of sender
- void * msg; //!< remaining message (type dependant)
+ void * cmd; //!< command data (type dependant)
} pb_msg_t;
//! PB_CMD_PROP data