From 352d6997f5d81a514436937f5345b9aac45dc7d7 Mon Sep 17 00:00:00 2001 From: Juri Glass Date: Mon, 25 Jul 2011 19:46:45 +0200 Subject: [PATCH] sml_time implemented --- sml/include/sml/sml_time.h | 11 ++++- sml/src/sml_time.c | 26 +++++++++--- test/Makefile | 1 + test/src/sml_time_test.c | 87 ++++++++++++++++++++++++++++++++++++++ test/test_main.c | 1 + 5 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 test/src/sml_time_test.c diff --git a/sml/include/sml/sml_time.h b/sml/include/sml/sml_time.h index 3b01a23..c1b1a7b 100644 --- a/sml/include/sml/sml_time.h +++ b/sml/include/sml/sml_time.h @@ -21,18 +21,25 @@ #define _SML_TIME_H #include "sml_shared.h" +#include "sml_number.h" #ifdef __cplusplus extern "C" { #endif +#define SML_TIME_SEC_INDEX 0x01 +#define SML_TIME_TIMESTAMP 0x02 typedef struct { u8 *tag; - u32 *data; + union { + u32 *sec_index; + u32 *timestamp; + } + data; } sml_time; -// Parses a time, updates the buffer accordingly, memory must be free'd elsewhere. +sml_time *sml_time_init(); sml_time *sml_time_parse(sml_buffer *buf); void sml_time_write(sml_time *time, sml_buffer *buf); void sml_time_free(sml_time *time); diff --git a/sml/src/sml_time.c b/sml/src/sml_time.c index 83da1ad..caf17a3 100644 --- a/sml/src/sml_time.c +++ b/sml/src/sml_time.c @@ -22,8 +22,18 @@ #include #include +sml_time *sml_time_init() { + sml_time *t = (sml_time *) malloc(sizeof(sml_time)); + memset(t, 0, sizeof(sml_time)); + return t; +} + sml_time *sml_time_parse(sml_buffer *buf) { - sml_time *tme = (sml_time *) malloc(sizeof(sml_time)); + if (sml_buf_optional_is_skipped(buf)) { + return 0; + } + + sml_time *tme = sml_time_init(); if (sml_buf_get_next_type(buf) != SML_TYPE_LIST) { buf->error = 1; @@ -38,7 +48,7 @@ sml_time *sml_time_parse(sml_buffer *buf) { tme->tag = sml_u8_parse(buf); if (sml_buf_has_errors(buf)) goto error; - tme->data = sml_u32_parse(buf); + tme->data.timestamp = sml_u32_parse(buf); if (sml_buf_has_errors(buf)) goto error; return tme; @@ -48,19 +58,21 @@ error: return 0; } -void sml_time_write(sml_time *time, sml_buffer *buf) { - if (time == 0) { +void sml_time_write(sml_time *t, sml_buffer *buf) { + if (t == 0) { sml_buf_optional_write(buf); return; } - printf("NYI: %s (writing optional flag instead)\n", __FUNCTION__); - sml_buf_optional_write(buf); + + sml_buf_set_type_and_length(buf, SML_TYPE_LIST, 2); + sml_u8_write(t->tag, buf); + sml_u32_write(t->data.timestamp, buf); } void sml_time_free(sml_time *tme) { if (tme) { sml_number_free(tme->tag); - sml_number_free(tme->data); + sml_number_free(tme->data.timestamp); free(tme); } } diff --git a/test/Makefile b/test/Makefile index e8c71a6..d6e91dd 100644 --- a/test/Makefile +++ b/test/Makefile @@ -14,6 +14,7 @@ OBJS = \ src/sml_value_test.o \ src/sml_status_test.o \ src/sml_list_test.o \ + src/sml_time_test.o \ src/sml_file_test.o \ src/sml_message_test.o diff --git a/test/src/sml_time_test.c b/test/src/sml_time_test.c new file mode 100644 index 0000000..1ddde6d --- /dev/null +++ b/test/src/sml_time_test.c @@ -0,0 +1,87 @@ +// 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_time); + +sml_buffer *buf; + +TEST_SETUP(sml_time) { + buf = sml_buffer_init(512); +} + +TEST_TEAR_DOWN(sml_time) { + sml_buffer_free(buf); +} + +TEST(sml_time, init) { + sml_time *t = sml_time_init(); + TEST_ASSERT_NOT_NULL(t); +} + +TEST(sml_time, parse_sec_index) { + hex2binary("72620165000000FF", sml_buf_get_current_buf(buf)); + sml_time *t = sml_time_parse(buf); + + TEST_ASSERT_NOT_NULL(t); + TEST_ASSERT_EQUAL(SML_TIME_SEC_INDEX, *(t->tag)); + TEST_ASSERT_EQUAL(8, buf->cursor); +} + +TEST(sml_time, parse_timestamp) { + hex2binary("72620265000000FF", sml_buf_get_current_buf(buf)); + sml_time *t = sml_time_parse(buf); + + TEST_ASSERT_NOT_NULL(t); + TEST_ASSERT_EQUAL(SML_TIME_TIMESTAMP, *(t->tag)); + TEST_ASSERT_EQUAL(8, buf->cursor); +} + +TEST(sml_time, parse_optional) { + hex2binary("01", sml_buf_get_current_buf(buf)); + sml_time *t = sml_time_parse(buf); + + TEST_ASSERT_NULL(t); + TEST_ASSERT_EQUAL(1, buf->cursor); +} + +TEST(sml_time, write_sec_index) { + sml_time *t = sml_time_init(); + t->data.sec_index = sml_u32_init(255); + t->tag = sml_u8_init(SML_TIME_SEC_INDEX); + + sml_time_write(t, buf); + expected_buf(buf, "72620165000000FF", 8); +} + +TEST(sml_time, write_optional) { + sml_time_write(0, buf); + expected_buf(buf, "01", 1); +} + +TEST_GROUP_RUNNER(sml_time) { + RUN_TEST_CASE(sml_time, init); + RUN_TEST_CASE(sml_time, parse_sec_index); + RUN_TEST_CASE(sml_time, parse_timestamp); + RUN_TEST_CASE(sml_time, parse_optional); + RUN_TEST_CASE(sml_time, write_sec_index); + RUN_TEST_CASE(sml_time, write_optional); +} diff --git a/test/test_main.c b/test/test_main.c index 0cf8a40..f9e0c15 100644 --- a/test/test_main.c +++ b/test/test_main.c @@ -26,6 +26,7 @@ static void runAllTests() { RUN_TEST_GROUP(sml_value); RUN_TEST_GROUP(sml_status); RUN_TEST_GROUP(sml_list); + RUN_TEST_GROUP(sml_time); RUN_TEST_GROUP(sml_message); RUN_TEST_GROUP(sml_file); }