added write, parse, init for sml_status
This commit is contained in:
parent
b5eb45377d
commit
a64b4d58e6
6 changed files with 127 additions and 34 deletions
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
|
|
@ -20,22 +20,28 @@
|
|||
#define _SML_STATUS_H
|
||||
|
||||
#include "sml_number.h"
|
||||
#include "sml_shared.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef union {
|
||||
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
|
||||
|
|
|
@ -18,52 +18,58 @@
|
|||
|
||||
|
||||
#include <sml/sml_status.h>
|
||||
#include <sml/sml_shared.h>
|
||||
|
||||
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);
|
||||
sml_number_free(status->data.status8);
|
||||
free(status);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
78
test/src/sml_status_test.c
Normal file
78
test/src/sml_status_test.c
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "../unity/unity_fixture.h"
|
||||
#include "test_helper.h"
|
||||
#include <sml/sml_status.h>
|
||||
|
||||
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);
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue