diff options
Diffstat (limited to 'lib/pbdrv/pb-serial.c')
-rw-r--r-- | lib/pbdrv/pb-serial.c | 123 |
1 files changed, 70 insertions, 53 deletions
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; } } |