diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pbdrv/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/pbdrv/pb-mod.c | 28 | ||||
-rw-r--r-- | lib/pbdrv/pb-mod.h | 3 | ||||
-rw-r--r-- | lib/pbdrv/pb-route.c | 86 | ||||
-rw-r--r-- | lib/pbdrv/pb-route.h | 39 | ||||
-rw-r--r-- | lib/pbdrv/pb-send.c | 16 | ||||
-rw-r--r-- | lib/pbdrv/pb-send.h | 16 |
7 files changed, 165 insertions, 24 deletions
diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt index ce77d43..b048ec7 100644 --- a/lib/pbdrv/CMakeLists.txt +++ b/lib/pbdrv/CMakeLists.txt @@ -25,6 +25,7 @@ target_include_directories(pbdrv SYSTEM INTERFACE .) add_library(pbdrv-mod STATIC pb-mod.c pb-send.c + pb-route.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 b5626d3..faa321e 100644 --- a/lib/pbdrv/pb-mod.c +++ b/lib/pbdrv/pb-mod.c @@ -1,5 +1,8 @@ +#include "pb-buf.h" +#include "pb-msg.h" #include "pb-types.h" -#include "pb.h" +#include "pb-mod.h" +#include "pb-route.h" //! fallback module name __weak const char * PB_MOD_NAME = "???"; @@ -7,9 +10,6 @@ __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; - __weak pb_global_state_t pb_hook_mod_state_read() { return _global_state; } @@ -18,9 +18,21 @@ __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_i2c_recv(const uint8_t * data, size_t sz) { + pb_buf_t buf = { + .data = (char *) data, + .size = sz, + }; + pb_msg_t * msg = pb_msg_read(&buf); + if (msg == NULL) return; // invalid message + if (msg->cmd == NULL) return; // invalid message + + pb_route_msg(msg); + + pb_msg_free(msg); +} + +void pb_reply(pb_msg_t * msg, pb_buf_t * reply) { + return pb_i2c_send(msg->sender, (uint8_t *) reply->data, reply->size); } - -// __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 7dfc945..4fb3456 100644 --- a/lib/pbdrv/pb-mod.h +++ b/lib/pbdrv/pb-mod.h @@ -14,6 +14,7 @@ #include <stddef.h> #include <stdbool.h> +#include "pb-buf.h" #include "pb-types.h" #ifdef __cplusplus @@ -28,6 +29,8 @@ 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); +void pb_reply(pb_msg_t * msg, pb_buf_t * reply); + pb_global_state_t pb_hook_mod_state_read(); void pb_hook_mod_state_write(pb_global_state_t state); diff --git a/lib/pbdrv/pb-route.c b/lib/pbdrv/pb-route.c new file mode 100644 index 0000000..f26d45c --- /dev/null +++ b/lib/pbdrv/pb-route.c @@ -0,0 +1,86 @@ +#include "pb-route.h" +#include "pb-mod.h" +#include "pb-send.h" + +__weak bool pb_hook_route_msg(pb_msg_t * msg) { return false; } +__weak void pb_route_msg(pb_msg_t * msg) { + if (pb_hook_route_msg(msg)) return; + + switch (msg->type) { + case PB_CMD_PROP: return pb_route_cmd_prop(msg); + case PB_CMD_STATE: return pb_route_cmd_state(msg); + case PB_CMD_MAGIC: return pb_route_cmd_magic(msg); + default: return; + } +} + +__weak bool pb_hook_route_cmd_prop(pb_msg_t * msg) { return false; } +__weak void pb_route_cmd_prop(pb_msg_t * msg) { + if (pb_hook_route_cmd_prop(msg)) return; + + switch (msg->action) { + case PB_ACTION_REQ: return pb_route_cmd_prop_req(msg); + case PB_ACTION_RES: return pb_route_cmd_prop_res(msg); + case PB_ACTION_SET: return pb_route_cmd_prop_set(msg); + default: return; + } +} + +__weak bool pb_hook_route_cmd_state(pb_msg_t * msg) { return false; } +__weak void pb_route_cmd_state(pb_msg_t * msg) { + if (pb_hook_route_cmd_state(msg)) return; + + switch (msg->action) { + case PB_ACTION_REQ: return pb_route_cmd_state_req(msg); + case PB_ACTION_RES: return pb_route_cmd_state_res(msg); + case PB_ACTION_SET: return pb_route_cmd_state_set(msg); + default: return; + } +} + +__weak bool pb_hook_route_cmd_magic(pb_msg_t * msg) { return false; } +__weak void pb_route_cmd_magic(pb_msg_t * msg) { + if (pb_hook_route_cmd_magic(msg)) return; + + switch (msg->action) { + case PB_ACTION_REQ: return pb_route_cmd_magic_req(msg); + case PB_ACTION_RES: return pb_route_cmd_magic_res(msg); + default: return; + } +} + +// all properties are user-defined +__weak void pb_route_cmd_prop_req(pb_msg_t * msg) {} +__weak void pb_route_cmd_prop_res(pb_msg_t * msg) {} +__weak void pb_route_cmd_prop_set(pb_msg_t * msg) {} + +static pb_global_state_t _main_state = PB_GS_NOINIT; +__weak void pb_hook_main_state_update(pb_global_state_t state) {} +__weak void pb_route_cmd_state_req(pb_msg_t * msg) { + pb_global_state_t own_state = pb_hook_mod_state_read(); + pb_buf_t buf = pb_send_state_res(own_state); + pb_reply(msg, &buf); + pb_buf_free(&buf); + + // notify of new global state variable + pb_cmd_state_t * cmd = msg->cmd; + if (cmd->state != _main_state) + pb_hook_main_state_update(cmd->state); + _main_state = cmd->state; +} + +__weak void pb_route_cmd_state_res(pb_msg_t * msg) {} + +__weak void pb_route_cmd_state_set(pb_msg_t * msg) { + pb_cmd_state_t * cmd = msg->cmd; + pb_hook_mod_state_write(cmd->state); +} + +__weak void pb_route_cmd_magic_req(pb_msg_t * msg) { + pb_buf_t buf = pb_send_magic_res(); + pb_reply(msg, &buf); + pb_buf_free(&buf); +} + +__weak void pb_route_cmd_magic_res(pb_msg_t * msg) { } + diff --git a/lib/pbdrv/pb-route.h b/lib/pbdrv/pb-route.h new file mode 100644 index 0000000..aa65057 --- /dev/null +++ b/lib/pbdrv/pb-route.h @@ -0,0 +1,39 @@ +#pragma once + +#include <stdbool.h> + +#include "pb-types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void pb_route_msg(pb_msg_t * msg); + +bool pb_hook_route_msg(pb_msg_t * msg); + +void pb_route_cmd_prop(pb_msg_t * msg); +void pb_route_cmd_state(pb_msg_t * msg); +void pb_route_cmd_magic(pb_msg_t * msg); + +bool pb_hook_route_cmd_prop(pb_msg_t * msg); +bool pb_hook_route_cmd_state(pb_msg_t * msg); +bool pb_hook_route_cmd_magic(pb_msg_t * msg); + +void pb_route_cmd_prop_req(pb_msg_t * msg); +void pb_route_cmd_prop_res(pb_msg_t * msg); +void pb_route_cmd_prop_set(pb_msg_t * msg); + +void pb_route_cmd_state_req(pb_msg_t * msg); +void pb_route_cmd_state_res(pb_msg_t * msg); +void pb_route_cmd_state_set(pb_msg_t * msg); + +void pb_hook_main_state_update(pb_global_state_t state); + +void pb_route_cmd_magic_req(pb_msg_t * msg); +void pb_route_cmd_magic_res(pb_msg_t * msg); + +#ifdef __cplusplus +} +#endif + diff --git a/lib/pbdrv/pb-send.c b/lib/pbdrv/pb-send.c index 09a35a8..f0d5004 100644 --- a/lib/pbdrv/pb-send.c +++ b/lib/pbdrv/pb-send.c @@ -2,7 +2,7 @@ #include "pb-mod.h" #include "pb-msg.h" -pb_buf_t pb_send_req_read(uint8_t propid) { +pb_buf_t pb_send_read_req(uint8_t propid) { pb_cmd_prop_t cmd = { .propid = propid, .value = NULL, @@ -17,7 +17,7 @@ pb_buf_t pb_send_req_read(uint8_t propid) { return pb_msg_write(&msg); } -pb_buf_t pb_send_res_read(uint8_t propid, uint8_t * value, size_t size) { +pb_buf_t pb_send_read_res(uint8_t propid, uint8_t * value, size_t size) { pb_cmd_prop_t cmd = { .propid = propid, .value = value, @@ -32,7 +32,7 @@ pb_buf_t pb_send_res_read(uint8_t propid, uint8_t * value, size_t size) { return pb_msg_write(&msg); } -pb_buf_t pb_send_req_write(uint8_t propid, uint8_t * value, size_t size) { +pb_buf_t pb_send_write_req(uint8_t propid, uint8_t * value, size_t size) { pb_cmd_prop_t cmd = { .propid = propid, .value = value, @@ -47,7 +47,7 @@ pb_buf_t pb_send_req_write(uint8_t propid, uint8_t * value, size_t size) { return pb_msg_write(&msg); } -pb_buf_t pb_send_req_state() { +pb_buf_t pb_send_state_req() { pb_cmd_state_t cmd = { .state = pb_hook_mod_state_read(), }; @@ -60,7 +60,7 @@ pb_buf_t pb_send_req_state() { return pb_msg_write(&msg); } -pb_buf_t pb_send_res_state(pb_global_state_t state) { +pb_buf_t pb_send_state_res(pb_global_state_t state) { pb_cmd_state_t cmd = { .state = state, }; @@ -73,7 +73,7 @@ pb_buf_t pb_send_res_state(pb_global_state_t state) { return pb_msg_write(&msg); } -pb_buf_t pb_send_req_set_state(pb_global_state_t state) { +pb_buf_t pb_send_state_set(pb_global_state_t state) { pb_cmd_state_t cmd = { .state = state, }; @@ -86,7 +86,7 @@ pb_buf_t pb_send_req_set_state(pb_global_state_t state) { return pb_msg_write(&msg); } -pb_buf_t pb_send_req_magic() { +pb_buf_t pb_send_magic_req() { pb_cmd_magic_t cmd = { .magic = (char *) &pb_cmd_magic_req[0], ._magic_size = sizeof(pb_cmd_magic_req), @@ -100,7 +100,7 @@ pb_buf_t pb_send_req_magic() { return pb_msg_write(&msg); } -pb_buf_t pb_send_res_magic() { +pb_buf_t pb_send_magic_res() { pb_cmd_magic_t cmd = { .magic = (char *) &pb_cmd_magic_res[0], ._magic_size = sizeof(pb_cmd_magic_res), diff --git a/lib/pbdrv/pb-send.h b/lib/pbdrv/pb-send.h index fff9956..83051e2 100644 --- a/lib/pbdrv/pb-send.h +++ b/lib/pbdrv/pb-send.h @@ -7,14 +7,14 @@ 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(); +pb_buf_t pb_send_read_req(uint8_t propid); +pb_buf_t pb_send_read_res(uint8_t propid, uint8_t * value, size_t size); +pb_buf_t pb_send_write_req(uint8_t propid, uint8_t * value, size_t size); +pb_buf_t pb_send_state_req(); +pb_buf_t pb_send_state_res(pb_global_state_t state); +pb_buf_t pb_send_state_set(pb_global_state_t state); +pb_buf_t pb_send_magic_req(); +pb_buf_t pb_send_magic_res(); #ifdef __cplusplus } |