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
116
117
118
119
120
121
122
|
#include <mpack.h>
#include "pb-mem.h"
#include "pb-serial.h"
#include "pb-types.h"
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 * 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 * cmd) {
if (cmd == NULL) return;
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 * cmd = _msg->cmd;
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 * cmd = _msg->cmd = pb_malloc(sizeof(pb_cmd_prop_t));
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->cmd != NULL) {
pb_cmd_prop_t * cmd = _msg->cmd;
if (cmd->value != NULL) {
MPACK_FREE(cmd->value);
cmd->value = NULL;
}
pb_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 = pb_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) {
pb_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 * cmd = _msg->cmd;
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 * cmd = _msg->cmd = pb_malloc(sizeof(pb_cmd_magic_t));
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->cmd != NULL) {
pb_cmd_magic_t * cmd = _msg->cmd;
if (cmd->magic != NULL) {
MPACK_FREE(cmd->magic);
cmd->magic = NULL;
}
pb_free(_msg->cmd);
_msg->cmd = NULL;
}
}
|