cake
/
libp7
Archived
1
0
Fork 1

Corrected memory allocation problems.

This commit is contained in:
Thomas Touhey 2017-04-02 13:55:08 +02:00
parent 17ffab9f6b
commit 12d746516b
7 changed files with 134 additions and 33 deletions

View File

@ -152,11 +152,13 @@ typedef struct p7_stream_s {
# define P7_DTRCTL_DISABLE 0x0000 /* disable DTR */
# define P7_DTRCTL_ENABLE 0x0008 /* enable DTR */
# define P7_DTRCTL_HANDSHAKE 0x0010 /* enable DTR and handshake */
# define P7_DTRVAL(F) (((F) & P7_DTRMASK) >> 3)
# define P7_RTSMASK 0x0060
# define P7_RTSCTL_DISABLE 0x0000 /* disable RTS */
# define P7_RTSCTL_ENABLE 0x0020 /* enable RTS */
# define P7_RTSCTL_HANDSHAKE 0x0040 /* enable RTS and handshake */
# define P7_RTSVAL(F) (((F) & P7_RTSMASK) >> 5)
/* Here are the XON/XOFF software control settings.
* XOFF disables the transmission temporarily, usually because the device at
@ -263,6 +265,9 @@ extern int p7_write(p7_stream_t *stream, const void *data, size_t size);
extern int p7_setcomm(p7_stream_t *stream, const p7_streamsettings_t *settings);
extern int p7_settm(p7_stream_t *stream, const p7_streamtimeouts_t *timeouts);
/* Skip bytes from a stream. */
extern int p7_skip(p7_stream_t *stream, size_t size);
# ifdef __cplusplus
}
# endif

View File

@ -73,7 +73,6 @@ int p7_write(p7_stream_t *stream, const void *data, size_t size)
return ((*stream->write)(stream->cookie,
(const unsigned char*)data, size));
}
/* ************************************************************************** */
/* Set stream communication settings and timeouts */
/* ************************************************************************** */
@ -90,6 +89,27 @@ int p7_setcomm(p7_stream_t *stream, const p7_streamsettings_t *settings)
{
if (stream->flags & p7_streamflag_usb || !stream->setcomm)
return (0);
/* describe the settings */
#if LOGLEVEL <= ll_info
const char *es[] = {"disabled", "enabled", "handshake", "<unknown>"};
logr_info("Setting serial settings: %u%c%c",
settings->speed, ~settings->flags & P7_PARENB ? 'N'
: settings->flags & P7_PARODD ? 'O' : 'E',
settings->flags & P7_TWOSTOPBITS ? '2' : '1');
logr_info("DTR mode: %s, RTS mode: %s",
es[P7_DTRVAL(settings->flags)], es[P7_RTSVAL(settings->flags)]);
if (settings->flags & P7_XONMASK)
logr_info("XON is enabled (0x%02X)", settings->cc[P7_XON]);
else logr_info("XON is disabled");
if (settings->flags & P7_XOFFMASK)
logr_info("XOFF is enabled (0x%02X)", settings->cc[P7_XOFF]);
else logr_info("XOFF is disabled");
#endif
/* then make */
return ((*stream->setcomm)(stream->cookie, settings));
}
@ -107,7 +127,6 @@ int p7_settm(p7_stream_t *stream, const p7_streamtimeouts_t *timeouts)
if (!stream->settm) return (0);
return ((*stream->settm)(stream->cookie, timeouts));
}
/* ************************************************************************** */
/* Initialize communication settings */
/* ************************************************************************** */

View File

@ -352,6 +352,13 @@ static int p7_seven_recv_main(p7_handle_t *handle)
COMPLETE_PACKET(4)
data_size = p7_getascii(&buffer[4], 4);
/* check data size */
if (data_size > MAX_ENCDFLD_SIZE) {
int err = p7_skip(&handle->_stream, data_size + 2);
if (err) return (err);
return (p7_error_checksum);
}
/* get data */
COMPLETE_PACKET(data_size + 2)
} else
@ -463,19 +470,23 @@ static int p7_seven_recv_main(p7_handle_t *handle)
int p7_seven_recv(p7_handle_t *handle, int checksum)
{
/* check if handler is initialized */
if (!handle)
return (p7_error_uninitialized);
/* make checks */
chk_handle(handle);
/* main receiving loop */
int tries = 2, err = 0;
int wasresend = 0;
int tries = 2;
int wasresend = 0, err;
do {
/* get packet */
err = p7_seven_recv_main(handle);
/* if there was a fail */
if (err) switch (err) {
switch (err) {
/* no error :D */
case p7_noerror:
tries = 2;
break;
/* if calc couldn't be found, terminate communication */
case p7_error_nocalc:
tries = 0;
@ -507,7 +518,8 @@ int p7_seven_recv(p7_handle_t *handle, int checksum)
if ((handle->_flags & p7_intflag_shifted) || wasresend)
return (p7_error_irrecoverable);
/* otherwise, send resend error */
/* otherwise, check tries and send resend error */
tries--;
err = p7_seven_send_err_resend(handle);
if (err) return (err);

View File

@ -66,7 +66,7 @@ static int p7_seven_send_buf(p7_handle_t *handle,
/* sending loop */
int wasresend = 0, resp_err = 0;
do {
while (1) {
/* log resend request */
if (wasresend) {
if (handle->_flags & p7_intflag_shifted) switch_buffer();
@ -80,9 +80,16 @@ static int p7_seven_send_buf(p7_handle_t *handle,
/* set wasreset for logging */
wasresend = 1;
} while (resp && ((resp_err = p7_seven_recv(handle, 1))
|| (response.type == p7_seven_type_error
&& response.code == p7_seven_err_resend)));
/* check if we want an answer */
if (!resp) break;
if ((resp_err = p7_seven_recv(handle, 1)))
break;
if (response.type == p7_seven_type_error
&& response.code == p7_seven_err_resend)
continue;
break;
}
/* packet sending is finished */
return (resp_err);

View File

@ -109,6 +109,9 @@ static int setcomm_for_fd(const int fd, const p7_streamsettings_t *settings)
/* get the speed */
speed_t speed;
switch (settings->speed) {
case P7_B1200: speed = B1200; break;
case P7_B2400: speed = B2400; break;
case P7_B4800: speed = B4800; break;
case P7_B9600: speed = B9600; break;
case P7_B19200: speed = B19200; break;
case P7_B38400: speed = B38400; break;
@ -128,22 +131,21 @@ static int setcomm_for_fd(const int fd, const p7_streamsettings_t *settings)
/* input flags */
term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR
| ICRNL | IXON | IXOFF);
if (settings->flags & P7_XONCTL_ENABLE) term.c_iflag |= IXON;
if (settings->flags & P7_XOFFCTL_ENABLE) term.c_iflag |= IXOFF;
| ICRNL | IGNPAR | IXON | IXOFF);
if (~settings->flags & P7_PARENB) term.c_iflag |= IGNPAR;
if (settings->flags & P7_XONMASK) term.c_iflag |= IXON;
if (settings->flags & P7_XOFFMASK) term.c_iflag |= IXOFF;
/* output flags, local modes */
term.c_oflag &= ~(OPOST);
term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
/* control flags */
term.c_cflag &= ~(PARENB | PARODD | CREAD | CSTOPB | CRTSCTS | CSIZE);
term.c_cflag &= ~(PARENB | PARODD | CREAD | CSTOPB | CSIZE);
term.c_cflag |= CREAD | CS8;
if (settings->flags & P7_TWOSTOPBITS) term.c_cflag |= CSTOPB;
if (settings->flags & P7_PARENB) term.c_cflag |= PARENB;
if (settings->flags & P7_PARODD) term.c_cflag |= PARODD;
if (settings->flags & (P7_DTRMASK | P7_RTSMASK))
term.c_cflag |= CRTSCTS; /* approximative... */
if (settings->flags & P7_PARENB) term.c_cflag |= PARENB;
if (settings->flags & P7_PARODD) term.c_cflag |= PARODD;
/* control characters */
term.c_cc[VSTART] = settings->cc[P7_XON];
@ -156,9 +158,13 @@ static int setcomm_for_fd(const int fd, const p7_streamsettings_t *settings)
/* activate DTR and RTS */
int status = 0;
if (settings->flags & P7_DTRCTL_ENABLE) status |= TIOCM_DTR;
if (settings->flags & P7_RTSCTL_ENABLE) status |= TIOCM_RTS;
if (ioctl(fd, TIOCMBIS, &status) < 0)
if ((settings->flags & P7_DTRMASK) == P7_DTRCTL_ENABLE
|| (settings->flags & P7_DTRMASK) == P7_DTRCTL_HANDSHAKE)
status |= TIOCM_DTR;
if ((settings->flags & P7_RTSMASK) == P7_RTSCTL_ENABLE
|| (settings->flags & P7_RTSMASK) == P7_RTSCTL_HANDSHAKE)
status |= TIOCM_RTS;
if (status && ioctl(fd, TIOCMBIS, &status) < 0)
return (p7_error_unknown);
/* no error! */

View File

@ -324,18 +324,28 @@ static int p7_win_setcomm(void *vcookie, const p7_streamsettings_t *settings)
logr_info("Setting communication settings");
/* set the DTR control mode */
if (~settings->flags & P7_DTRCTL_ENABLE)
dcb.fDtrControl = DTR_CONTROL_DISABLE;
else if (settings->flags & P7_DTRCTL_HANDSHAKE)
switch (settings->flags & P7_DTRMASK) {
case P7_DTRCTL_ENABLE:
dcb.fDtrControl = DTR_CONTROL_ENABLE;
break;
case P7_DTRCTL_HANDSHAKE:
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
else dcb.fDtrControl = DTR_CONTROL_ENABLE;
break;
default:
dcb.fDtrControl = DTR_CONTROL_DISABLE;
}
/* set the RTS control mode */
if (~settings->flags & P7_RTSCTL_ENABLE)
dcb.fRtsControl = RTS_CONTROL_DISABLE;
else if (settings->flags & P7_RTSCTL_HANDSHAKE)
switch (settings->flags & P7_RTSMASK) {
case P7_RTSCTL_ENABLE:
dcb.fRtsControl = RTS_CONTROL_ENABLE;
break;
case P7_RTSCTL_HANDSHAKE:
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
else dcb.fRtsControl = DTR_CONTROL_ENABLE;
break;
default:
dcb.fRtsControl = RTS_CONTROL_DISABLE;
}
/* speed */
switch (settings->speed) {
@ -355,7 +365,7 @@ static int p7_win_setcomm(void *vcookie, const p7_streamsettings_t *settings)
/* set other things */
dcb.ByteSize = 8;
dcb.XonLim = 0x4000; dcb.XonChar = settings->cc[P7_XON];
dcb.XonLim = 0x4000; dcb.XonChar = settings->cc[P7_XON];
dcb.XoffLim = 0x1000; dcb.XoffChar = settings->cc[P7_XOFF];
/* save new state */

42
src/utils/skip.c Normal file
View File

@ -0,0 +1,42 @@
/* *****************************************************************************
* utils/skip.c -- skip bytes in a stream.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libp7.
* libp7 is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3.0 of the License,
* or (at your option) any later version.
*
* libp7 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libp7; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libp7/internals.h>
#define BUFSIZE 512
/**
* p7_skip:
* Skip bytes in stream.
*
* @arg stream the stream.
* @arg size the size of the data span to skip.
* @return the error code (0 if ok).
*/
int p7_skip(p7_stream_t *stream, size_t size)
{
uint8_t buf[BUFSIZE];
logr_info("Skipping %" PRIuSIZE " bytes.", size);
while (size) {
size_t rd = min(size, BUFSIZE);
int err = p7_read(stream, buf, rd);
if (err) return (err);
size -= rd;
}
return (0);
}