libwmc: add more test code and start of com tests

This commit is contained in:
Dan Williams
2011-09-27 13:13:40 -05:00
parent 8250dee131
commit 18fc92ef73
10 changed files with 591 additions and 22 deletions
+6 -17
View File
@@ -1,28 +1,17 @@
noinst_LTLIBRARIES = libwmc.la libwmc-test.la
noinst_LTLIBRARIES = libwmc.la
libwmc_la_CPPFLAGS = \
$(MM_CFLAGS)
libwmc_la_SOURCES = \
errors.c \
errors.h \
utils.c \
utils.h
utils.h \
com.c \
com.h
libwmc_la_LIBADD = \
$(MM_LIBS)
###########################################
# Test library without symbol versioning
###########################################
libwmc_test_la_CPPFLAGS = \
$(MM_CFLAGS)
libwmc_test_la_SOURCES = \
utils.c \
utils.h
libwmc_test_la_LIBADD = \
$(MM_LIBS)
+57
View File
@@ -0,0 +1,57 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include "com.h"
#include "errors.h"
wbool
wmc_port_setup (int fd, WmcError **error)
{
struct termios stbuf;
errno = 0;
memset (&stbuf, 0, sizeof (stbuf));
if (tcgetattr (fd, &stbuf) != 0) {
wmc_error_set (error, WMC_SERIAL_ERROR, WMC_SERIAL_ERROR_CONFIG_FAILED,
"tcgetattr() error: %d", errno);
}
stbuf.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB);
stbuf.c_iflag &= ~(HUPCL | IUTF8 | IUCLC | ISTRIP | IXON | IXOFF | IXANY | ICRNL);
stbuf.c_oflag &= ~(OPOST | OCRNL | ONLCR | OLCUC | ONLRET);
stbuf.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL);
stbuf.c_lflag &= ~(NOFLSH | XCASE | TOSTOP | ECHOPRT | ECHOCTL | ECHOKE);
stbuf.c_cc[VMIN] = 1;
stbuf.c_cc[VTIME] = 0;
stbuf.c_cc[VEOF] = 1;
stbuf.c_cflag |= (B115200 | CS8 | CREAD | 0 | 0); /* No parity, 1 stop bit */
errno = 0;
if (tcsetattr (fd, TCSANOW, &stbuf) < 0) {
wmc_error_set (error, WMC_SERIAL_ERROR, WMC_SERIAL_ERROR_CONFIG_FAILED,
"tcsetattr() error: %d", errno);
return FALSE;
}
return TRUE;
}
+33
View File
@@ -0,0 +1,33 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBWMC_COM_H
#define LIBWMC_COM_H
#include "errors.h"
enum {
WMC_SERIAL_ERROR = 1000
};
enum {
WMC_SERIAL_ERROR_CONFIG_FAILED = 1
};
wbool wmc_port_setup (int fd, WmcError **error);
#endif /* LIBWMC_COM_H */
+109
View File
@@ -0,0 +1,109 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "errors.h"
#include <stdlib.h>
#include <string.h>
WmcError *
wmc_error_new (u_int32_t domain,
u_int32_t code,
const char *format,
...)
{
WmcError *error;
va_list args;
int n;
wmc_return_val_if_fail (format != NULL, NULL);
wmc_return_val_if_fail (format[0] != '\0', NULL);
error = malloc (sizeof (WmcError));
wmc_assert (error != NULL);
error->domain = domain;
error->code = code;
va_start (args, format);
n = vasprintf (&error->message, format, args);
va_end (args);
if (n < 0) {
free (error);
return NULL;
}
return error;
}
void
wmc_error_set (WmcError **error,
u_int32_t domain,
u_int32_t code,
const char *format,
...)
{
va_list args;
int n;
if (error == NULL)
return;
wmc_return_if_fail (*error == NULL);
wmc_return_if_fail (format[0] != '\0');
*error = malloc (sizeof (WmcError));
wmc_assert (*error != NULL);
(*error)->domain = domain;
(*error)->code = code;
va_start (args, format);
n = vasprintf (&(*error)->message, format, args);
va_end (args);
if (n < 0) {
free (*error);
*error = NULL;
}
}
static void
free_error (WmcError *error)
{
if (error) {
if (error->message)
free (error->message);
memset (error, 0, sizeof (*error));
free (error);
}
}
void
wmc_clear_error (WmcError **error)
{
if (error) {
free_error (*error);
*error = NULL;
}
}
void
wmc_free_error (WmcError *error)
{
free_error (error);
}
+74
View File
@@ -0,0 +1,74 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBWMC_ERRORS_H
#define LIBWMC_ERRORS_H
#include <config.h>
#include <sys/types.h>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
typedef u_int8_t wbool;
#ifndef TRUE
#define TRUE (u_int8_t) 1
#endif
#ifndef FALSE
#define FALSE (u_int8_t) 0
#endif
typedef struct {
u_int32_t domain;
u_int32_t code;
char *message;
} WmcError;
WmcError *wmc_error_new (u_int32_t domain,
u_int32_t code,
const char *format,
...) __attribute__((__format__ (__printf__, 3, 4)));
void wmc_error_set (WmcError **error,
u_int32_t domain,
u_int32_t code,
const char *format,
...) __attribute__((__format__ (__printf__, 4, 5)));
void wmc_clear_error (WmcError **error);
void wmc_free_error (WmcError *error);
#define wmc_assert assert
#define wmc_return_if_fail(e) \
{ \
if (!(e)) { \
fprintf (stderr, "failed: ##e##\n"); \
return; \
} \
}
#define wmc_return_val_if_fail(e, v) \
{ \
if (!(e)) { \
fprintf (stderr, "failed: ##e##\n"); \
return v; \
} \
}
#endif /* LIBWMC_COM_H */
+9 -5
View File
@@ -274,9 +274,11 @@ uml290_wmc_encapsulate (char *inbuf,
* hdlc_decapsulate_buffer:
* @inbuf: buffer in which to look for an HDLC frame
* @inbuf_len: length of valid data in @inbuf
* @check_known_crc: if %TRUE, validate the CRC using @known_crc
* @check_known_crc: if %TRUE, validate the CRC using @known_crc if the normal
* CRC check fails
* @known_crc: if @check_known_crc is %TRUE, compare the frame's CRC against
* @known_crc. @known_crc must be in Little Endian (LE) byte order.
* @known_crc if the normal CRC check fails. @known_crc must be in Little
* Endian (LE) byte order.
* @outbuf: buffer in which to put decapsulated data from the HDLC frame
* @outbuf_len: max size of @outbuf
* @out_decap_len: on success, size of the decapsulated data
@@ -365,12 +367,14 @@ hdlc_decapsulate_buffer (const char *inbuf,
}
/* Check the CRC of the packet's data */
crc = check_known_crc ? known_crc : crc16 (outbuf, unesc_len - 2, 0);
crc = crc16 (outbuf, unesc_len - 2, 0);
pkt_crc = outbuf[unesc_len - 2] & 0xFF;
pkt_crc |= (outbuf[unesc_len - 1] & 0xFF) << 8;
if (crc != pkt_crc) {
*out_used = pkt_len + 1; /* packet + CRC + 0x7E */
return FALSE;
if (!check_known_crc || (pkt_crc != known_crc)) {
*out_used = pkt_len + 1; /* packet + CRC + 0x7E */
return FALSE;
}
}
*out_used = pkt_len + 1; /* packet + CRC + 0x7E */