diff options
author | Dan Williams <dcbw@redhat.com> | 2011-09-27 13:13:40 -0500 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2011-09-27 13:13:40 -0500 |
commit | 18fc92ef7304d01c5ec11ced22ff82c4ab6dbbe1 (patch) | |
tree | ee936fbb66ed56b1d2545d3f25550c16fcf1efcc /libwmc/src | |
parent | 8250dee13177a164d61e31fe8d60900f9d5c6797 (diff) |
libwmc: add more test code and start of com tests
Diffstat (limited to 'libwmc/src')
-rw-r--r-- | libwmc/src/Makefile.am | 23 | ||||
-rw-r--r-- | libwmc/src/com.c | 57 | ||||
-rw-r--r-- | libwmc/src/com.h | 33 | ||||
-rw-r--r-- | libwmc/src/errors.c | 109 | ||||
-rw-r--r-- | libwmc/src/errors.h | 74 | ||||
-rw-r--r-- | libwmc/src/utils.c | 14 |
6 files changed, 288 insertions, 22 deletions
diff --git a/libwmc/src/Makefile.am b/libwmc/src/Makefile.am index fe0a9fbc..1a376e36 100644 --- a/libwmc/src/Makefile.am +++ b/libwmc/src/Makefile.am @@ -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) - diff --git a/libwmc/src/com.c b/libwmc/src/com.c new file mode 100644 index 00000000..751dd4a7 --- /dev/null +++ b/libwmc/src/com.c @@ -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; +} + diff --git a/libwmc/src/com.h b/libwmc/src/com.h new file mode 100644 index 00000000..ffcf5d54 --- /dev/null +++ b/libwmc/src/com.h @@ -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 */ diff --git a/libwmc/src/errors.c b/libwmc/src/errors.c new file mode 100644 index 00000000..a1d87ba1 --- /dev/null +++ b/libwmc/src/errors.c @@ -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); +} + diff --git a/libwmc/src/errors.h b/libwmc/src/errors.h new file mode 100644 index 00000000..aebe40f2 --- /dev/null +++ b/libwmc/src/errors.h @@ -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 */ diff --git a/libwmc/src/utils.c b/libwmc/src/utils.c index 99c13db7..3c470330 100644 --- a/libwmc/src/utils.c +++ b/libwmc/src/utils.c @@ -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 */ |