blob: ee47700ed4b50d47e268095ddccfc796b62c9a79 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#include "pb-route.h"
#include "pb-mod.h"
#include "pb-send.h"
#include "pb-types.h"
#include "pb-mem.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_send_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_cmd_magic_t * cmd = msg->cmd;
// return early if magic has wrong size
if (cmd->_magic_size != sizeof(pb_cmd_magic_req)) return;
// // return early if magic doesn't match
if (pb_memcmp(cmd->magic, pb_cmd_magic_req, sizeof(pb_cmd_magic_req)) != 0) return;
// FIXME: this should be removed (see handover: RP2040 I2C limitations)
pb_mod_blocking_delay_ms(2000);
pb_buf_t buf = pb_send_magic_res();
pb_send_reply(msg, &buf);
pb_buf_free(&buf);
}
__weak void pb_route_cmd_magic_res(pb_msg_t * msg) { }
|