From a64b4d58e611694f69bc0523ff71b43962071b07 Mon Sep 17 00:00:00 2001 From: Juri Glass Date: Sun, 17 Jul 2011 12:27:00 +0200 Subject: [PATCH] added write, parse, init for sml_status --- sml/include/sml/sml_shared.h | 1 + sml/include/sml/sml_status.h | 18 ++++++--- sml/src/sml_status.c | 62 +++++++++++++++------------- test/Makefile | 1 + test/src/sml_status_test.c | 78 ++++++++++++++++++++++++++++++++++++ test/test_main.c | 1 + 6 files changed, 127 insertions(+), 34 deletions(-) create mode 100644 test/src/sml_status_test.c diff --git a/sml/include/sml/sml_shared.h b/sml/include/sml/sml_shared.h index bf4f74a..1681624 100644 --- a/sml/include/sml/sml_shared.h +++ b/sml/include/sml/sml_shared.h @@ -21,6 +21,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { diff --git a/sml/include/sml/sml_status.h b/sml/include/sml/sml_status.h index ed5160e..f41c390 100644 --- a/sml/include/sml/sml_status.h +++ b/sml/include/sml/sml_status.h @@ -20,22 +20,28 @@ #define _SML_STATUS_H #include "sml_number.h" +#include "sml_shared.h" #ifdef __cplusplus extern "C" { #endif -typedef union { - u8 *status8; - u16 *status16; - u32 *status32; - u64 *status64; +typedef struct { + u8 type; + union { + u8 *status8; + u16 *status16; + u32 *status32; + u64 *status64; + } data; } sml_status; +sml_status *sml_status_init(); sml_status *sml_status_parse(sml_buffer *buf); - +void sml_status_write(sml_status *status, sml_buffer *buf); void sml_status_free(sml_status *status); + #ifdef __cplusplus } #endif diff --git a/sml/src/sml_status.c b/sml/src/sml_status.c index 65ffc2b..babdc8f 100644 --- a/sml/src/sml_status.c +++ b/sml/src/sml_status.c @@ -18,52 +18,58 @@ #include -#include + +sml_status *sml_status_init() { + sml_status *status = (sml_status *) malloc(sizeof(sml_status)); + memset(status, 0, sizeof(sml_status)); + return status; +} sml_status *sml_status_parse(sml_buffer *buf) { + if (sml_buf_optional_is_skipped(buf)) { + return 0; + } + + int max = 1; int type = sml_buf_get_next_type(buf); unsigned char byte = sml_buf_get_current_byte(buf); - sml_status *state = (sml_status *) malloc(sizeof(sml_status)); + sml_status *status = (sml_status *) malloc(sizeof(sml_status)); + status->type = type; switch (type) { case SML_TYPE_UNSIGNED: - switch (byte & SML_LENGTH_FIELD) { - case SML_TYPE_NUMBER_8: - state->status8 = sml_u8_parse(buf); - break; - case SML_TYPE_NUMBER_16: - state->status16 = sml_u16_parse(buf); - break; - case SML_TYPE_NUMBER_16 + 1: - case SML_TYPE_NUMBER_32: - state->status32 = sml_u32_parse(buf); - break; - case SML_TYPE_NUMBER_32 + 1: - case SML_TYPE_NUMBER_32 + 2: - case SML_TYPE_NUMBER_32 + 3: - case SML_TYPE_NUMBER_64: - state->status64 = sml_u64_parse(buf); - break; - default: - buf->error = 1; - break; + // get maximal size, if not all bytes are used (example: only 6 bytes for a u64) + while (max < ((byte & SML_LENGTH_FIELD) - 1)) { + max <<= 1; } + + status->data.status8 = sml_number_parse(buf, type, max); + status->type |= max; break; default: buf->error = 1; break; } if (sml_buf_has_errors(buf)) { - sml_status_free(state); + sml_status_free(status); return 0; } - return state; + return status; } +void sml_status_write(sml_status *status, sml_buffer *buf){ + if (status == 0) { + sml_buf_optional_write(buf); + return; + } + sml_number_write(status->data.status8, (status->type & SML_TYPE_FIELD), + (status->type & SML_LENGTH_FIELD), buf); +}; + void sml_status_free(sml_status *status) { - if (status) { - sml_number_free(status->status8); - free(status); - } + if (status) { + sml_number_free(status->data.status8); + free(status); + } } diff --git a/test/Makefile b/test/Makefile index f58e303..0f232bb 100644 --- a/test/Makefile +++ b/test/Makefile @@ -12,6 +12,7 @@ OBJS = \ src/sml_number_test.o \ src/sml_boolean_test.o \ src/sml_value_test.o \ + src/sml_status_test.o \ src/sml_file_test.o \ src/sml_message_test.o diff --git a/test/src/sml_status_test.c b/test/src/sml_status_test.c new file mode 100644 index 0000000..805498b --- /dev/null +++ b/test/src/sml_status_test.c @@ -0,0 +1,78 @@ +// Copyright 2011 Juri Glass, Mathias Runge, Nadim El Sayed +// DAI-Labor, TU-Berlin +// +// This file is part of libSML. +// +// libSML is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// libSML 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 libSML. If not, see . + +#include "../unity/unity_fixture.h" +#include "test_helper.h" +#include + +TEST_GROUP(sml_status); + +sml_buffer *buf; + +TEST_SETUP(sml_status) { + buf = sml_buffer_init(512); +} + +TEST_TEAR_DOWN(sml_status) { + sml_buffer_free(buf); +} + +TEST(sml_status, init) { + sml_status *s = sml_status_init(); + TEST_ASSERT_NOT_NULL(s); +} + +TEST(sml_status, parse_status8) { + hex2binary("6201", sml_buf_get_current_buf(buf)); + sml_status *s = sml_status_parse(buf); + + TEST_ASSERT_NOT_NULL(s); + TEST_ASSERT_EQUAL(1, *(s->data.status8)); + TEST_ASSERT_EQUAL((SML_TYPE_UNSIGNED | SML_TYPE_NUMBER_8), s->type); +} + +TEST(sml_status, parse_optional) { + hex2binary("01", sml_buf_get_current_buf(buf)); + sml_status *s = sml_status_parse(buf); + + TEST_ASSERT_NULL(s); + TEST_ASSERT_FALSE(sml_buf_has_errors(buf)); +} + +TEST(sml_status, write_status32) { + sml_status *s = sml_status_init(); + s->type = SML_TYPE_UNSIGNED | SML_TYPE_NUMBER_32; + s->data.status32 = sml_u32_init(42); + + sml_status_write(s, buf); + expected_buf(buf, "650000002A", 5); +} + +TEST(sml_status, write_optional) { + sml_status_write(0, buf); + expected_buf(buf, "01", 1); +} + + +TEST_GROUP_RUNNER(sml_status) { + RUN_TEST_CASE(sml_status, init); + RUN_TEST_CASE(sml_status, parse_status8); + RUN_TEST_CASE(sml_status, parse_optional); + RUN_TEST_CASE(sml_status, write_status32); + RUN_TEST_CASE(sml_status, write_optional); +} diff --git a/test/test_main.c b/test/test_main.c index a57377d..a188db1 100644 --- a/test/test_main.c +++ b/test/test_main.c @@ -24,6 +24,7 @@ static void runAllTests() { RUN_TEST_GROUP(sml_number); RUN_TEST_GROUP(sml_boolean); RUN_TEST_GROUP(sml_value); + RUN_TEST_GROUP(sml_status); RUN_TEST_GROUP(sml_message); RUN_TEST_GROUP(sml_file); }