aboutsummaryrefslogtreecommitdiff
path: root/lib/pbdrv/pb-route.c
blob: 94d15d13335873080f13a5164606ee43efc30627 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "pb-route.h"
#include "pb-mem.h"
#include "pb-mod.h"
#include "pb-send.h"
#include "pb-types.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) {}

//! last known global state of last STATE REQ sender (i.e. main controller)
static pb_global_state_t _main_state = PB_GS_NOINIT;
__weak void pb_hook_ev_main_state_update(pb_global_state_t state) {}
__weak void pb_hook_ev_module_init() { pb_hook_mod_state_write(PB_GS_IDLE); }
__weak void pb_route_cmd_state_req(pb_msg_t * msg) {
	pb_global_state_t own_state = pb_hook_mod_state_read();

	// notify of new global state variable
	pb_cmd_state_t * cmd = msg->cmd;
	if (cmd->state != _main_state) {
		// first STATE REQ = module init OK
		if (_main_state == PB_GS_NOINIT) pb_hook_ev_module_init();
		pb_hook_ev_main_state_update(cmd->state);
	}
	_main_state = cmd->state;

	pb_buf_t buf = pb_send_state_res();
	pb_send_reply(msg, &buf);
	pb_buf_free(&buf);
}

__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;

	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) {}