diff options
author | Dan Williams <dcbw@redhat.com> | 2013-02-08 08:12:04 -0600 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2013-04-18 10:23:50 -0500 |
commit | 43b87d3976bdcf336b111f56df7e7ba0ec5fcad6 (patch) | |
tree | 0de18f223b3da76b345f3182f7772eb99dcdc904 | |
parent | 3c8bdffe8cb9fb0a0965d8025e2cd9508ca3cc44 (diff) |
qcdm: add basic infrastructure for log items
-rw-r--r-- | libqcdm/src/Makefile.am | 2 | ||||
-rw-r--r-- | libqcdm/src/log-items.c | 85 | ||||
-rw-r--r-- | libqcdm/src/log-items.h | 38 |
3 files changed, 125 insertions, 0 deletions
diff --git a/libqcdm/src/Makefile.am b/libqcdm/src/Makefile.am index cece4800..61696761 100644 --- a/libqcdm/src/Makefile.am +++ b/libqcdm/src/Makefile.am @@ -13,6 +13,8 @@ libqcdm_la_SOURCES = \ commands.c \ commands.h \ commands-private.h \ + log-items.c \ + log-items.h \ errors.c \ errors.h \ result.c \ diff --git a/libqcdm/src/log-items.c b/libqcdm/src/log-items.c new file mode 100644 index 00000000..650fb775 --- /dev/null +++ b/libqcdm/src/log-items.c @@ -0,0 +1,85 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2013 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 <string.h> +#include <stdlib.h> +#include <endian.h> + +#include "log-items.h" +#include "errors.h" +#include "dm-commands.h" +#include "dm-log-items.h" +#include "result-private.h" +#include "utils.h" + +/**********************************************************************/ + +static qcdmbool +check_log_item (const char *buf, size_t len, u_int16_t log_code, size_t min_len, int *out_error) +{ + DMCmdLog *log = (DMCmdLog *) buf; + + if (len < sizeof (DMCmdLog)) { + qcdm_err (0, "DM command response malformed (must be at least 1 byte in length)"); + if (out_error) + *out_error = -QCDM_ERROR_RESPONSE_MALFORMED; + return FALSE; + } + + /* Make sure it's a log item */ + if (log->code != 0x10) { + qcdm_err (0, "Not a log item (expected 0x10, got %d)", log->code); + if (out_error) + *out_error = -QCDM_ERROR_RESPONSE_UNEXPECTED; + return FALSE; + } + + /* Make sure it's the log item we want */ + if (log_code && (log->log_code != log_code)) { + qcdm_err (0, "Not the expected log item (expected %d, got %d)", log_code, log->log_code); + if (out_error) + *out_error = -QCDM_ERROR_RESPONSE_UNEXPECTED; + return FALSE; + } + + if (len < min_len) { + qcdm_err (0, "Log item %d response not long enough (got %zu, expected " + "at least %zu).", log_code, len, min_len); + if (out_error) + *out_error = -QCDM_ERROR_RESPONSE_BAD_LENGTH; + return FALSE; + } + + return TRUE; +} + +qcdmbool +qcdm_is_log_item (const char *buf, size_t len, u_int16_t log_code) +{ + return check_log_item (buf, len, log_code, sizeof (DMCmdLog), NULL); +} + +u_int16_t +qcdm_get_log_item_code (const char *buf, size_t len) +{ + if (check_log_item (buf, len, 0, sizeof (DMCmdLog), NULL)) + return le16toh (((DMCmdLog *) buf)->log_code); + return 0; +} + +/**********************************************************************/ + diff --git a/libqcdm/src/log-items.h b/libqcdm/src/log-items.h new file mode 100644 index 00000000..2cae59bf --- /dev/null +++ b/libqcdm/src/log-items.h @@ -0,0 +1,38 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2013 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 LIBQCDM_LOG_ITEMS_H +#define LIBQCDM_LOG_ITEMS_H + +#include "utils.h" +#include "result.h" + +/**********************************************************************/ + +/* Generic fields of the log item header */ +#define QCDM_LOG_ITEM_CODE "code" +#define QCDM_LOG_ITEM_TIMESTAMP "timestamp" + +/* log_code = 0 is a special case for verifying basic log item structure */ +qcdmbool qcdm_is_log_item (const char *buf, size_t len, u_int16_t log_code); + +/* 0 on error or if buf does not contain a log item */ +u_int16_t qcdm_get_log_item_code (const char *buf, size_t len); + +/**********************************************************************/ + +#endif /* LIBQCDM_LOG_ITEMS_H */ |