From 29e8bd1351666e36e7d6f237753d29d4f89ba84e Mon Sep 17 00:00:00 2001 From: Pieter Willemsen Date: Wed, 21 Jun 2017 15:19:52 +0200 Subject: [PATCH] sys: cbor: makefiles can enable cbor functionality new cbor pseudomodules: - cbor_ctime - cbor_float - cbor_semantic_tagging --- makefiles/pseudomodules.inc.mk | 3 + sys/cbor/Makefile | 6 -- sys/cbor/cbor.c | 77 ++++++++++----------- sys/include/cbor.h | 18 +++-- tests/unittests/tests-cbor/Makefile | 6 -- tests/unittests/tests-cbor/Makefile.include | 3 + tests/unittests/tests-cbor/tests-cbor.c | 55 +++++++-------- 7 files changed, 74 insertions(+), 94 deletions(-) diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 8f9bcfee5..3a738ad65 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -2,6 +2,9 @@ PSEUDOMODULES += auto_init_gnrc_rpl PSEUDOMODULES += can_mbox PSEUDOMODULES += can_pm PSEUDOMODULES += can_raw +PSEUDOMODULES += cbor_ctime +PSEUDOMODULES += cbor_float +PSEUDOMODULES += cbor_semantic_tagging PSEUDOMODULES += conn_can_isotp_multi PSEUDOMODULES += core_% PSEUDOMODULES += emb6_router diff --git a/sys/cbor/Makefile b/sys/cbor/Makefile index 871bd3925..8e1c52a54 100644 --- a/sys/cbor/Makefile +++ b/sys/cbor/Makefile @@ -1,13 +1,7 @@ MODULE = cbor -CFLAGS += -DCBOR_NO_PRINT ifneq ($(shell uname -s),Darwin) CFLAGS += -D_XOPEN_SOURCE=600 endif -ifeq (,$(filter native,$(BOARD))) - # build the minimal subset for non-native - CFLAGS += -DCBOR_NO_FLOAT -DCBOR_NO_PRINT -DCBOR_NO_SEMANTIC_TAGGING -endif - include $(RIOTBASE)/Makefile.base diff --git a/sys/cbor/cbor.c b/sys/cbor/cbor.c index 2afdf6bdd..37f4fbb9a 100644 --- a/sys/cbor/cbor.c +++ b/sys/cbor/cbor.c @@ -24,11 +24,8 @@ #include #include -/* Automatically enable/disable ENABLE_DEBUG based on CBOR_NO_PRINT */ -#ifndef CBOR_NO_PRINT -#define ENABLE_DEBUG (1) +#define ENABLE_DEBUG (0) #include "debug.h" -#endif #define CBOR_TYPE_MASK 0xE0 /* top 3 bits */ #define CBOR_INFO_MASK 0x1F /* low 5 bits */ @@ -107,7 +104,7 @@ typedef struct __attribute__((packed)) { } u; } cast_align_u8_t; -#ifndef CBOR_NO_FLOAT +#ifdef MODULE_CBOR_FLOAT /** * Convert float @p x to network format @@ -224,9 +221,8 @@ static uint16_t encode_float_half(float x) bits += m & 1; return bits; } -#endif /* CBOR_NO_FLOAT */ +#endif /* MODULE_CBOR_FLOAT */ -#ifndef CBOR_NO_PRINT /** * Print @p size bytes at @p data in hexadecimal display format */ @@ -236,14 +232,13 @@ void dump_memory(const unsigned char *data, size_t size) return; } - DEBUG("0x"); + printf("0x"); for (size_t i = 0; i < size; ++i) { - DEBUG("%02X", data[i]); + printf("%02X", data[i]); } - DEBUG("\n"); + puts(""); } -#endif /* CBOR_NO_PRINT */ void cbor_init(cbor_stream_t *stream, unsigned char *buffer, size_t size) { @@ -542,7 +537,7 @@ size_t cbor_serialize_bool(cbor_stream_t *s, bool val) return 1; } -#ifndef CBOR_NO_FLOAT +#ifdef MODULE_CBOR_FLOAT size_t cbor_deserialize_float_half(const cbor_stream_t *stream, size_t offset, float *val) { if (CBOR_TYPE(stream, offset) != CBOR_7 || !val) { @@ -623,7 +618,7 @@ size_t cbor_serialize_double(cbor_stream_t *s, double val) s->pos += 8; return 9; } -#endif /* CBOR_NO_FLOAT */ +#endif /* MODULE_CBOR_FLOAT */ size_t cbor_deserialize_byte_string(const cbor_stream_t *stream, size_t offset, char *val, size_t length) @@ -757,8 +752,8 @@ size_t cbor_serialize_map(cbor_stream_t *s, size_t map_length) return encode_int(CBOR_MAP, s, map_length); } -#ifndef CBOR_NO_SEMANTIC_TAGGING -#ifndef CBOR_NO_CTIME +#ifdef MODULE_CBOR_SEMANTIC_TAGGING +#ifdef MODULE_CBOR_CTIME size_t cbor_deserialize_date_time(const cbor_stream_t *stream, size_t offset, struct tm *val) { if ((CBOR_TYPE(stream, offset) != CBOR_TAG) @@ -840,7 +835,7 @@ size_t cbor_serialize_date_time_epoch(cbor_stream_t *stream, time_t val) size_t written_bytes = encode_int(CBOR_UINT, stream, time); return written_bytes + 1; /* + 1 tag byte */ } -#endif /* CBOR_NO_CTIME */ +#endif /* MODULE_CBOR_CTIME */ size_t cbor_write_tag(cbor_stream_t *s, unsigned char tag) @@ -854,7 +849,7 @@ bool cbor_at_tag(const cbor_stream_t *s, size_t offset) { return cbor_at_end(s, offset) || CBOR_TYPE(s, offset) == CBOR_TAG; } -#endif /* CBOR_NO_SEMANTIC_TAGGING */ +#endif /* MODULE_CBOR_SEMANTIC_TAGGING */ size_t cbor_write_break(cbor_stream_t *s) { @@ -874,7 +869,6 @@ bool cbor_at_end(const cbor_stream_t *s, size_t offset) return s ? offset >= s->pos - 1 : true; } -#ifndef CBOR_NO_PRINT /* BEGIN: Printers */ void cbor_stream_print(const cbor_stream_t *stream) { @@ -901,9 +895,9 @@ static size_t cbor_stream_decode_skip(cbor_stream_t *stream, size_t offset) break; } - DEBUG("(unsupported, "); + printf("(unsupported, "); dump_memory(stream->data + offset, consume_bytes); - DEBUG(")\n"); + puts(")"); return consume_bytes; } @@ -917,11 +911,11 @@ static size_t cbor_stream_decode_at(cbor_stream_t *stream, size_t offset, int in #define DESERIALIZE_AND_PRINT(type, suffix, format_string) { \ type val; \ size_t read_bytes = cbor_deserialize_##suffix(stream, offset, &val); \ - DEBUG("("#type", "format_string")\n", val); \ + printf("("#type", "format_string")\n", val); \ return read_bytes; \ } - DEBUG("%*s", indent, ""); + printf("%*s", indent, ""); switch (CBOR_TYPE(stream, offset)) { case CBOR_UINT: @@ -931,29 +925,29 @@ static size_t cbor_stream_decode_at(cbor_stream_t *stream, size_t offset, int in case CBOR_BYTES: { char buffer[CBOR_STREAM_PRINT_BUFFERSIZE]; size_t read_bytes = cbor_deserialize_byte_string(stream, offset, buffer, sizeof(buffer)); - DEBUG("(byte string, \"%s\")\n", buffer); + printf("(byte string, \"%s\")\n", buffer); return read_bytes; } case CBOR_TEXT: { char buffer[CBOR_STREAM_PRINT_BUFFERSIZE]; size_t read_bytes = cbor_deserialize_unicode_string(stream, offset, buffer, sizeof(buffer)); - DEBUG("(unicode string, \"%s\")\n", buffer); + printf("(unicode string, \"%s\")\n", buffer); return read_bytes; } case CBOR_ARRAY: { const bool is_indefinite = (stream->data[offset] == (CBOR_ARRAY | CBOR_VAR_FOLLOWS)); - uint64_t array_length; + uint64_t array_length = 0; size_t read_bytes; if (is_indefinite) { offset += read_bytes = cbor_deserialize_array_indefinite(stream, offset); - DEBUG("(array, length: [indefinite])\n"); + puts("(array, length: [indefinite])"); } else { offset += read_bytes = decode_int(stream, offset, &array_length); - DEBUG("(array, length: %"PRIu64")\n", array_length); + printf("(array, length: %"PRIu64")\n", array_length); } size_t i = 0; @@ -977,16 +971,16 @@ static size_t cbor_stream_decode_at(cbor_stream_t *stream, size_t offset, int in case CBOR_MAP: { const bool is_indefinite = (stream->data[offset] == (CBOR_MAP | CBOR_VAR_FOLLOWS)); - uint64_t map_length; + uint64_t map_length = 0; size_t read_bytes; if (is_indefinite) { offset += read_bytes = cbor_deserialize_map_indefinite(stream, offset); - DEBUG("(map, length: [indefinite])\n"); + puts("(map, length: [indefinite])"); } else { offset += read_bytes = decode_int(stream, offset, &map_length); - DEBUG("(map, length: %"PRIu64")\n", map_length); + printf("(map, length: %"PRIu64")\n", map_length); } size_t i = 0; @@ -1008,50 +1002,52 @@ static size_t cbor_stream_decode_at(cbor_stream_t *stream, size_t offset, int in read_bytes += cbor_at_break(stream, offset); return read_bytes; } - +#ifdef MODULE_CBOR_SEMANTIC_TAGGING case CBOR_TAG: { unsigned char tag = CBOR_ADDITIONAL_INFO(stream, offset); switch (tag) { /* Non-native builds likely don't have support for ctime (hence disable it there) * TODO: Better check for availability of ctime functions? */ -#ifndef CBOR_NO_CTIME +#ifdef MODULE_CBOR_CTIME case CBOR_DATETIME_STRING_FOLLOWS: { char buf[64]; struct tm timeinfo; size_t read_bytes = cbor_deserialize_date_time(stream, offset, &timeinfo); strftime(buf, sizeof(buf), "%c", &timeinfo); - DEBUG("(tag: %u, date/time string: \"%s\")\n", tag, buf); + printf("(tag: %u, date/time string: \"%s\")\n", tag, buf); return read_bytes; } case CBOR_DATETIME_EPOCH_FOLLOWS: { time_t time; size_t read_bytes = cbor_deserialize_date_time_epoch(stream, offset, &time); - DEBUG("(tag: %u, date/time epoch: %d)\n", tag, (int)time); + printf("(tag: %u, date/time epoch: %d)\n", tag, (int)time); return read_bytes; } -#endif /* CBOR_NO_CTIME */ +#endif /* MODULE_CBOR_CTIME */ default: break; } + break; } +#endif /* MODULE_CBOR_SEMANTIC_TAGGING */ case CBOR_7: { switch (stream->data[offset]) { case CBOR_FALSE: case CBOR_TRUE: DESERIALIZE_AND_PRINT(bool, bool, "%d") -#ifndef CBOR_NO_FLOAT +#ifdef MODULE_CBOR_FLOAT case CBOR_FLOAT16: DESERIALIZE_AND_PRINT(float, float_half, "%f") case CBOR_FLOAT32: DESERIALIZE_AND_PRINT(float, float, "%f") case CBOR_FLOAT64: DESERIALIZE_AND_PRINT(double, double, "%lf") -#endif /* CBOR_NO_FLOAT */ +#endif /* MODULE_CBOR_FLOAT */ default: break; } @@ -1067,7 +1063,7 @@ static size_t cbor_stream_decode_at(cbor_stream_t *stream, size_t offset, int in void cbor_stream_decode(cbor_stream_t *stream) { - DEBUG("Data:\n"); + puts("Data:"); size_t offset = 0; while (offset < stream->pos) { @@ -1082,9 +1078,6 @@ void cbor_stream_decode(cbor_stream_t *stream) offset += read_bytes; } - DEBUG("\n"); + puts(""); } - -#endif /* CBOR_NO_PRINT */ - /* END: Printers */ diff --git a/sys/include/cbor.h b/sys/include/cbor.h index 0acffface..27648e831 100644 --- a/sys/include/cbor.h +++ b/sys/include/cbor.h @@ -120,9 +120,9 @@ #include #include -#ifndef CBOR_NO_CTIME +#ifdef MODULE_CBOR_CTIME #include -#endif /* CBOR_NO_CTIME */ +#endif /* MODULE_CBOR_CTIME */ #ifdef __cplusplus extern "C" { @@ -183,7 +183,6 @@ void cbor_clear(cbor_stream_t *stream); */ void cbor_destroy(cbor_stream_t *stream); -#ifndef CBOR_NO_PRINT /** * @brief Print @p stream in hex representation * @@ -210,7 +209,6 @@ void cbor_stream_print(const cbor_stream_t *stream); * @param[in] stream Pointer to the cbor struct */ void cbor_stream_decode(cbor_stream_t *stream); -#endif /* CBOR_NO_PRINT */ /** * @brief Serializes an integer @@ -300,7 +298,7 @@ size_t cbor_serialize_bool(cbor_stream_t *stream, bool val); size_t cbor_deserialize_bool(const cbor_stream_t *stream, size_t offset, bool *val); -#ifndef CBOR_NO_FLOAT +#ifdef MODULE_CBOR_FLOAT /** * @brief Serializes a half-width floating point value * @@ -366,7 +364,7 @@ size_t cbor_serialize_double(cbor_stream_t *stream, double val); */ size_t cbor_deserialize_double(const cbor_stream_t *stream, size_t offset, double *val); -#endif /* CBOR_NO_FLOAT */ +#endif /* MODULE_CBOR_FLOAT */ /** * @brief Serializes a signed 64 bit value @@ -580,8 +578,8 @@ size_t cbor_serialize_map_indefinite(cbor_stream_t *stream); */ size_t cbor_deserialize_map_indefinite(const cbor_stream_t *stream, size_t offset); -#ifndef CBOR_NO_SEMANTIC_TAGGING -#ifndef CBOR_NO_CTIME +#ifdef MODULE_CBOR_SEMANTIC_TAGGING +#ifdef MODULE_CBOR_CTIME /** * @brief Serialize date and time * @@ -643,7 +641,7 @@ size_t cbor_serialize_date_time_epoch(cbor_stream_t *stream, time_t val); */ size_t cbor_deserialize_date_time_epoch(const cbor_stream_t *stream, size_t offset, time_t *val); -#endif /* CBOR_NO_CTIME */ +#endif /* MODULE_CBOR_CTIME */ /** * @brief Write a tag to give the next CBOR item additional semantics @@ -667,7 +665,7 @@ size_t cbor_write_tag(cbor_stream_t *stream, unsigned char tag); */ bool cbor_at_tag(const cbor_stream_t *stream, size_t offset); -#endif /* CBOR_NO_SEMANTIC_TAGGING */ +#endif /* MODULE_CBOR_SEMANTIC_TAGGING */ /** * @brief Write a break symbol at the current offset in stream @p stream diff --git a/tests/unittests/tests-cbor/Makefile b/tests/unittests/tests-cbor/Makefile index 849c1e6b3..48422e909 100644 --- a/tests/unittests/tests-cbor/Makefile +++ b/tests/unittests/tests-cbor/Makefile @@ -1,7 +1 @@ -CFLAGS += -DCBOR_NO_PRINT - -ifeq (,$(filter native,$(BOARD))) - CFLAGS += -DCBOR_NO_FLOAT -DCBOR_NO_PRINT -DCBOR_NO_SEMANTIC_TAGGING -endif - include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-cbor/Makefile.include b/tests/unittests/tests-cbor/Makefile.include index 292ca1d0a..fd958e6d0 100644 --- a/tests/unittests/tests-cbor/Makefile.include +++ b/tests/unittests/tests-cbor/Makefile.include @@ -1 +1,4 @@ USEMODULE += cbor +USEMODULE += cbor_ctime +USEMODULE += cbor_float +USEMODULE += cbor_semantic_tagging diff --git a/tests/unittests/tests-cbor/tests-cbor.c b/tests/unittests/tests-cbor/tests-cbor.c index 76e2a7f74..541568f88 100644 --- a/tests/unittests/tests-cbor/tests-cbor.c +++ b/tests/unittests/tests-cbor/tests-cbor.c @@ -25,18 +25,13 @@ #include #include #include -#ifndef CBOR_NO_CTIME +#ifdef MODULE_CBOR_CTIME #include -#endif /* CBOR_NO_CTIME */ +#endif /* MODULE_CBOR_CTIME */ static void my_cbor_print(const cbor_stream_t *stream) { -#ifndef CBOR_NO_PRINT cbor_stream_print(stream); -#else - (void)stream; - printf(""); -#endif } #define CBOR_CHECK_SERIALIZED(stream, expected_value, expected_value_size) do { \ @@ -573,7 +568,7 @@ static void test_map_invalid(void) } } -#ifndef CBOR_NO_SEMANTIC_TAGGING +#ifdef MODULE_CBOR_SEMANTIC_TAGGING static void test_semantic_tagging(void) { char buffer[128]; @@ -592,7 +587,7 @@ static void test_semantic_tagging(void) CBOR_CHECK_DESERIALIZED(input, buffer, EQUAL_STRING); } -#ifndef CBOR_NO_CTIME +#ifdef MODULE_CBOR_CTIME static void test_date_time(void) { /* CBOR: UTF-8 string marked with a tag 0 to indicate it is a standard date/time string */ @@ -634,8 +629,8 @@ static void test_date_time_epoch(void) TEST_ASSERT(cbor_deserialize_date_time_epoch(&stream, 0, &val2)); CBOR_CHECK_DESERIALIZED(val, val2, EQUAL_INT); } -#endif /* CBOR_NO_CTIME */ -#endif /* CBOR_NO_SEMANTIC_TAGGING */ +#endif /* MODULE_CBOR_CTIME */ +#endif /* MODULE_CBOR_SEMANTIC_TAGGING */ static void test_bool(void) { @@ -652,7 +647,7 @@ static void test_bool_invalid(void) TEST_ASSERT_EQUAL_INT(0, cbor_deserialize_bool(&invalid_stream, 0, &val_bool)); } -#ifndef CBOR_NO_FLOAT +#ifdef MODULE_CBOR_FLOAT static void test_float_half(void) { /* check border conditions */ @@ -751,9 +746,9 @@ static void test_double_invalid(void) double val_double = 0; TEST_ASSERT_EQUAL_INT(0, cbor_deserialize_double(&invalid_stream, 0, &val_double)); } -#endif /* CBOR_NO_FLOAT */ +#endif /* MODULE_CBOR_FLOAT */ -#ifndef CBOR_NO_PRINT +#ifdef MODULE_CBOR_PRINT /** * Manual test for testing the cbor_stream_decode function */ @@ -766,11 +761,11 @@ void test_stream_decode(void) cbor_serialize_int64_t(&stream, 3); cbor_serialize_int64_t(&stream, -5); cbor_serialize_bool(&stream, true); -#ifndef CBOR_NO_FLOAT +#ifdef MODULE_CBOR_FLOAT cbor_serialize_float_half(&stream, 1.1f); cbor_serialize_float(&stream, 1.5f); cbor_serialize_double(&stream, 2.0); -#endif /* CBOR_NO_FLOAT */ +#endif /* MODULE_CBOR_FLOAT */ cbor_serialize_byte_string(&stream, "abc"); cbor_serialize_unicode_string(&stream, "def"); @@ -796,23 +791,23 @@ void test_stream_decode(void) cbor_serialize_byte_string(&stream, "11"); cbor_write_break(&stream); -#ifndef CBOR_NO_SEMANTIC_TAGGING -#ifndef CBOR_NO_CTIME +#ifdef MODULE_CBOR_SEMANTIC_TAGGING +#ifdef MODULE_CBOR_CTIME time_t rawtime; time(&rawtime); struct tm *timeinfo = localtime(&rawtime); cbor_serialize_date_time(&stream, timeinfo); cbor_serialize_date_time_epoch(&stream, rawtime); -#endif /* CBOR_NO_CTIME */ +#endif /* MODULE_CBOR_CTIME */ /* decoder should skip the tag and print 'unsupported' here */ cbor_write_tag(&stream, 2); cbor_serialize_byte_string(&stream, "1"); -#endif /* CBOR_NO_SEMANTIC_TAGGING */ +#endif /* MODULE_CBOR_SEMANTIC_TAGGING */ - cbor_stream_decode(&stream); + //cbor_stream_decode(&stream); } -#endif /* CBOR_NO_PRINT */ +#endif /* MODULE_CBOR_PRINT */ /** * See examples from CBOR RFC (cf. Appendix A. Examples) @@ -838,23 +833,23 @@ TestRef tests_cbor_all(void) new_TestFixture(test_map), new_TestFixture(test_map_indefinite), new_TestFixture(test_map_invalid), -#ifndef CBOR_NO_SEMANTIC_TAGGING +#ifdef MODULE_CBOR_SEMANTIC_TAGGING new_TestFixture(test_semantic_tagging), -#ifndef CBOR_NO_CTIME +#ifdef MODULE_CBOR_CTIME new_TestFixture(test_date_time), new_TestFixture(test_date_time_epoch), -#endif /* CBOR_NO_CTIME */ -#endif /* CBOR_NO_SEMANTIC_TAGGING */ +#endif /* MODULE_CBOR_CTIME */ +#endif /* MODULE_CBOR_SEMANTIC_TAGGING */ new_TestFixture(test_bool), new_TestFixture(test_bool_invalid), -#ifndef CBOR_NO_FLOAT +#ifdef MODULE_CBOR_FLOAT new_TestFixture(test_float_half), new_TestFixture(test_float_half_invalid), new_TestFixture(test_float), new_TestFixture(test_float_invalid), new_TestFixture(test_double), new_TestFixture(test_double_invalid), -#endif /* CBOR_NO_FLOAT */ +#endif /* MODULE_CBOR_FLOAT */ }; EMB_UNIT_TESTCALLER(CborTest, setUp, tearDown, fixtures); @@ -863,9 +858,9 @@ TestRef tests_cbor_all(void) void tests_cbor(void) { -#ifndef CBOR_NO_PRINT +#ifdef MODULE_CBOR_PRINT test_stream_decode(); -#endif /* CBOR_NO_PRINT */ +#endif /* MODULE_CBOR_PRINT */ TESTS_RUN(tests_cbor_all()); }