command: Don't pass max_size to command_encodef()

The command_encodef() can read the max_size parameter directly from
the 'struct command_encoder' passed into it.  Also, there is no need
to check that a message will fit in a buffer if the buffer is declared
to be MESSAGE_MAX in size.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-08-13 16:48:27 -04:00
parent f3da473285
commit f8bd8b97be
7 changed files with 24 additions and 29 deletions

View File

@ -1,6 +1,6 @@
// AVR serial port code.
//
// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@ -8,7 +8,7 @@
#include <string.h> // memmove
#include "autoconf.h" // CONFIG_SERIAL_BAUD
#include "board/io.h" // readb
#include "board/misc.h" // console_get_input
#include "board/misc.h" // console_sendf
#include "command.h" // DECL_CONSTANT
#include "irq.h" // irq_save
#include "pgm.h" // READP
@ -161,7 +161,7 @@ console_sendf(const struct command_encoder *ce, va_list args)
// Generate message
char *buf = &transmit_buf[tmax];
uint8_t msglen = command_encodef(buf, max_size, ce, args);
uint8_t msglen = command_encodef(buf, ce, args);
command_add_frame(buf, msglen);
// Start message transmit

View File

@ -1,20 +1,17 @@
// Wrappers for AVR usb serial.
//
// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <avr/interrupt.h> // USART0_RX_vect
#include <string.h> // memmove
#include "../lib/pjrc_usb_serial/usb_serial.h"
#include "board/misc.h" // console_sendf
#include "command.h" // command_dispatch
#include "pgm.h" // READP
#include "sched.h" // DECL_INIT
#define USBSERIAL_BUFFER_SIZE 64
static char receive_buf[USBSERIAL_BUFFER_SIZE];
static char receive_buf[MESSAGE_MAX];
static uint8_t receive_pos;
static char transmit_buf[USBSERIAL_BUFFER_SIZE];
void
usbserial_init(void)
@ -68,13 +65,11 @@ void
console_sendf(const struct command_encoder *ce, va_list args)
{
// Generate message
uint8_t max_size = READP(ce->max_size);
if (max_size > sizeof(transmit_buf))
return;
uint8_t msglen = command_encodef(transmit_buf, max_size, ce, args);
command_add_frame(transmit_buf, msglen);
static char buf[MESSAGE_MAX];
uint8_t msglen = command_encodef(buf, ce, args);
command_add_frame(buf, msglen);
// Transmit message
usb_serial_write((void*)transmit_buf, msglen);
usb_serial_write((void*)buf, msglen);
usb_serial_flush_output();
}

View File

@ -94,14 +94,14 @@ error:
// Encode a message
uint8_t
command_encodef(char *buf, uint8_t buf_len
, const struct command_encoder *ce, va_list args)
command_encodef(char *buf, const struct command_encoder *ce, va_list args)
{
if (buf_len <= MESSAGE_MIN)
uint8_t max_size = READP(ce->max_size);
if (max_size <= MESSAGE_MIN)
// Ack/Nak message
return buf_len;
return max_size;
char *p = &buf[MESSAGE_HEADER_SIZE];
char *maxend = &p[buf_len - MESSAGE_MIN];
char *maxend = &p[max_size - MESSAGE_MIN];
uint8_t num_params = READP(ce->num_params);
const uint8_t *param_types = READP(ce->param_types);
*p++ = READP(ce->msg_id);

View File

@ -63,8 +63,8 @@ enum {
// command.c
char *command_parsef(char *p, char *maxend
, const struct command_parser *cp, uint32_t *args);
uint8_t command_encodef(char *buf, uint8_t buf_len
, const struct command_encoder *ce, va_list args);
uint8_t command_encodef(char *buf, const struct command_encoder *ce
, va_list args);
void command_sendf(const struct command_encoder *ce, ...);
void command_add_frame(char *buf, uint8_t msglen);
int8_t command_find_block(char *buf, uint8_t buf_len, uint8_t *pop_count);

View File

@ -3,6 +3,7 @@
// Local definitions for PRU code
#include <stdint.h> // uint32_t
#include "command.h" // MESSAGE_MAX
#define IEP_EVENT 7
#define KICK_ARM_EVENT 16
@ -24,7 +25,7 @@
// Layout of shared memory
struct shared_response_buffer {
uint32_t count;
char data[64];
char data[MESSAGE_MAX];
};
struct shared_mem {
uint32_t signal;

View File

@ -135,14 +135,13 @@ void
console_sendf(const struct command_encoder *ce, va_list args)
{
// Verify space for message
uint32_t max_size = ce->max_size;
uint32_t send_push_pos = SHARED_MEM->send_push_pos;
struct shared_response_buffer *s = &SHARED_MEM->send_data[send_push_pos];
if (max_size > sizeof(s->data) || readl(&s->count))
if (readl(&s->count))
return;
// Generate message
uint32_t msglen = command_encodef(s->data, max_size, ce, args);
uint32_t msglen = command_encodef(s->data, ce, args);
// Signal PRU0 to transmit message
writel(&s->count, msglen);

View File

@ -1,6 +1,6 @@
// sam3x8e serial port
//
// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@ -9,7 +9,7 @@
#include "board/gpio.h" // gpio_peripheral
#include "board/io.h" // readl
#include "board/irq.h" // irq_save
#include "board/misc.h" // console_get_input
#include "board/misc.h" // console_sendf
#include "command.h" // DECL_CONSTANT
#include "sam3x8e.h" // UART
#include "sched.h" // DECL_INIT
@ -152,7 +152,7 @@ console_sendf(const struct command_encoder *ce, va_list args)
// Generate message
char *buf = &transmit_buf[tmax];
uint32_t msglen = command_encodef(buf, max_size, ce, args);
uint32_t msglen = command_encodef(buf, ce, args);
command_add_frame(buf, msglen);
// Start message transmit