command: Add a command_encode_and_frame() helper

Add a helper function that calls command_encodef() followed by
command_add_frame().

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-05-24 13:07:43 -04:00
parent cb4e165071
commit c8af3feee6
6 changed files with 17 additions and 9 deletions

View File

@ -65,8 +65,7 @@ console_sendf(const struct command_encoder *ce, va_list args)
{ {
// Generate message // Generate message
static uint8_t buf[MESSAGE_MAX]; static uint8_t buf[MESSAGE_MAX];
uint8_t msglen = command_encodef(buf, ce, args); uint8_t msglen = command_encode_and_frame(buf, ce, args);
command_add_frame(buf, msglen);
// Transmit message // Transmit message
usb_serial_write((void*)buf, msglen); usb_serial_write((void*)buf, msglen);

View File

@ -167,6 +167,16 @@ command_add_frame(uint8_t *buf, uint_fast8_t msglen)
buf[msglen - MESSAGE_TRAILER_SYNC] = MESSAGE_SYNC; buf[msglen - MESSAGE_TRAILER_SYNC] = MESSAGE_SYNC;
} }
// Encode a message and then add a message block frame around it
uint_fast8_t
command_encode_and_frame(uint8_t *buf, const struct command_encoder *ce
, va_list args)
{
uint_fast8_t msglen = command_encodef(buf, ce, args);
command_add_frame(buf, msglen);
return msglen;
}
static uint8_t in_sendf; static uint8_t in_sendf;
// Encode and transmit a "response" message // Encode and transmit a "response" message

View File

@ -65,8 +65,10 @@ uint8_t *command_parsef(uint8_t *p, uint8_t *maxend
, const struct command_parser *cp, uint32_t *args); , const struct command_parser *cp, uint32_t *args);
uint_fast8_t command_encodef(uint8_t *buf, const struct command_encoder *ce uint_fast8_t command_encodef(uint8_t *buf, const struct command_encoder *ce
, va_list args); , va_list args);
void command_sendf(const struct command_encoder *ce, ...);
void command_add_frame(uint8_t *buf, uint_fast8_t msglen); void command_add_frame(uint8_t *buf, uint_fast8_t msglen);
uint_fast8_t command_encode_and_frame(
uint8_t *buf, const struct command_encoder *ce, va_list args);
void command_sendf(const struct command_encoder *ce, ...);
int_fast8_t command_find_block(uint8_t *buf, uint_fast8_t buf_len int_fast8_t command_find_block(uint8_t *buf, uint_fast8_t buf_len
, uint_fast8_t *pop_count); , uint_fast8_t *pop_count);
void command_dispatch(uint8_t *buf, uint_fast8_t msglen); void command_dispatch(uint8_t *buf, uint_fast8_t msglen);

View File

@ -108,8 +108,7 @@ console_sendf(const struct command_encoder *ce, va_list args)
// Generate message // Generate message
uint8_t *buf = &transmit_buf[tmax]; uint8_t *buf = &transmit_buf[tmax];
uint_fast8_t msglen = command_encodef(buf, ce, args); uint_fast8_t msglen = command_encode_and_frame(buf, ce, args);
command_add_frame(buf, msglen);
// Start message transmit // Start message transmit
writeb(&transmit_max, tmax + msglen); writeb(&transmit_max, tmax + msglen);

View File

@ -65,8 +65,7 @@ console_sendf(const struct command_encoder *ce, va_list args)
// Generate message // Generate message
uint8_t *buf = &transmit_buf[tpos]; uint8_t *buf = &transmit_buf[tpos];
uint_fast8_t msglen = command_encodef(buf, ce, args); uint_fast8_t msglen = command_encode_and_frame(buf, ce, args);
command_add_frame(buf, msglen);
// Start message transmit // Start message transmit
transmit_pos = tpos + msglen; transmit_pos = tpos + msglen;

View File

@ -176,8 +176,7 @@ console_sendf(const struct command_encoder *ce, va_list args)
{ {
// Generate message // Generate message
uint8_t buf[MESSAGE_MAX]; uint8_t buf[MESSAGE_MAX];
uint_fast8_t msglen = command_encodef(buf, ce, args); uint_fast8_t msglen = command_encode_and_frame(buf, ce, args);
command_add_frame(buf, msglen);
// Transmit message // Transmit message
int ret = write(main_pfd[MP_TTY_IDX].fd, buf, msglen); int ret = write(main_pfd[MP_TTY_IDX].fd, buf, msglen);