Skip to content

Commit

Permalink
CRC64 Encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Oct 14, 2023
1 parent 42d73e7 commit 6687a0d
Show file tree
Hide file tree
Showing 20 changed files with 1,150 additions and 309 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,11 @@ if(BUILD_TESTING AND(UNIX OR MINGW))
list(APPEND TEST_LINK_LIBS Threads::Threads)
endif()

try_compile(HAVE_FLOAT16 ${CMAKE_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tools/test_float16.c)
if(HAVE_FLOAT16)
list(APPEND TEST_COMPILE_DEFINITIONS "HAVE_FLOAT16")
endif()

# Disable clang-tidy for gtest
oc_disable_clang_tidy()

Expand Down Expand Up @@ -980,7 +985,7 @@ if(BUILD_TESTING AND(UNIX OR MINGW))
file(GLOB COMMONTEST_SRC tests/gtest/*.cpp tests/gtest/tls/*.cpp)

# Unit tests
file(GLOB APITEST_SRC api/unittest/*.cpp api/client/unittest/*.cpp)
file(GLOB APITEST_SRC api/unittest/*.cpp api/unittest/encoder/*.cpp api/client/unittest/*.cpp)
set(apitest_files)
if (OC_INTROSPECTION_ENABLED AND OC_IDD_API_ENABLED)
set(apitest_files ${PROJECT_SOURCE_DIR}/api/unittest/introspectiontest_IDD.cbor)
Expand Down
2 changes: 1 addition & 1 deletion api/oc_etag.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ oc_resource_get_crc64(oc_resource_t *resource, uint64_t *crc64)
#endif /* OC_DYNAMIC_ALLOCATION */
return false;
}
*crc64 = oc_crc64(response_buffer.buffer, payload_size);
*crc64 = oc_crc64(0, response_buffer.buffer, payload_size);

#ifdef OC_DYNAMIC_ALLOCATION
free(response_buffer.buffer);
Expand Down
54 changes: 35 additions & 19 deletions api/oc_rep_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
*
****************************************************************************/

#include "api/oc_rep_encode_internal.h"
#include "oc_rep.h"
#include "oc_rep_encode_internal.h"
#include "port/oc_log_internal.h"
#include "util/oc_features.h"

#ifdef OC_JSON_ENCODER
#include "oc_rep_encode_json_internal.h"
#include "api/oc_rep_encode_json_internal.h"
#endif /* OC_JSON_ENCODER */

#ifdef OC_HAS_FEATURE_CRC_ENCODER
#include "api/oc_rep_encode_crc_internal.h"
#endif /* OC_HAS_FEATURE_CRC_ENCODER */

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
Expand All @@ -41,7 +46,7 @@ typedef struct
#endif /* OC_DYNAMIC_ALLOCATION */
} oc_rep_buffer_t;

CborEncoder g_encoder;
static CborEncoder g_encoder;

static oc_rep_buffer_t g_rep_buffer = {
.buffer = NULL,
Expand All @@ -57,6 +62,9 @@ static oc_rep_buffer_t g_rep_buffer = {
{ \
.type = OC_REP_CBOR_ENCODER, \
\
.get_buffer_size = &cbor_encoder_get_buffer_size, \
.get_extra_bytes_needed = &cbor_encoder_get_extra_bytes_needed, \
\
.encode_null = &cbor_encode_null, .encode_boolean = &cbor_encode_boolean, \
.encode_int = &cbor_encode_int, .encode_uint = &cbor_encode_uint, \
.encode_floating_point = &cbor_encode_floating_point, \
Expand Down Expand Up @@ -110,13 +118,13 @@ static size_t
oc_rep_encoder_get_extra_bytes_needed(CborEncoder *encoder)
{
oc_rep_encoder_convert_offset_to_ptr(encoder);
size_t size = cbor_encoder_get_extra_bytes_needed(encoder);
size_t size = g_rep_encoder.get_extra_bytes_needed(encoder);
oc_rep_encoder_convert_ptr_to_offset(encoder);
return size;
}

static CborError
realloc_buffer(size_t needed)
rep_buffer_realloc(size_t needed)
{
if (!g_rep_buffer.enable_realloc ||
g_rep_buffer.buffer_size + needed > g_rep_buffer.buffer_max_size) {
Expand Down Expand Up @@ -156,6 +164,13 @@ oc_rep_encoder_set_type(oc_rep_encoder_type_t encoder_type)
return;
}
#endif /* OC_JSON_ENCODER */

#ifdef OC_HAS_FEATURE_CRC_ENCODER
if (encoder_type == OC_REP_CRC_ENCODER) {
g_rep_encoder = oc_rep_crc_encoder();
return;
}
#endif /* OC_HAS_FEATURE_CRC_ENCODER */
}

oc_rep_encoder_type_t
Expand Down Expand Up @@ -272,8 +287,8 @@ int
oc_rep_get_encoded_payload_size(void)
{
oc_rep_encoder_convert_offset_to_ptr(&g_encoder);
size_t size = cbor_encoder_get_buffer_size(&g_encoder, g_rep_buffer.buffer);
size_t needed = cbor_encoder_get_extra_bytes_needed(&g_encoder);
size_t size = g_rep_encoder.get_buffer_size(&g_encoder, g_rep_buffer.buffer);
size_t needed = g_rep_encoder.get_extra_bytes_needed(&g_encoder);
oc_rep_encoder_convert_ptr_to_offset(&g_encoder);
if (g_err == CborErrorOutOfMemory) {
OC_WRN("Insufficient memory: Increase OC_MAX_APP_DATA_SIZE to "
Expand Down Expand Up @@ -308,7 +323,7 @@ oc_rep_encode_raw(const uint8_t *data, size_t len)
}
CborEncoder prevEncoder;
memcpy(&prevEncoder, &g_encoder, sizeof(prevEncoder));
CborError err = realloc_buffer(needed);
CborError err = rep_buffer_realloc(needed);
if (err != CborNoError) {
g_err = err;
return;
Expand Down Expand Up @@ -351,7 +366,7 @@ oc_rep_encode_null(CborEncoder *encoder)
memcpy(&prevEncoder, encoder, sizeof(prevEncoder));
CborError err = oc_rep_encode_null_internal(encoder);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -381,7 +396,7 @@ oc_rep_encode_boolean(CborEncoder *encoder, bool value)
memcpy(&prevEncoder, encoder, sizeof(prevEncoder));
CborError err = oc_rep_encode_boolean_internal(encoder, value);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -411,7 +426,7 @@ oc_rep_encode_int(CborEncoder *encoder, int64_t value)
memcpy(&prevEncoder, encoder, sizeof(prevEncoder));
CborError err = oc_rep_encode_int_internal(encoder, value);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -441,7 +456,7 @@ oc_rep_encode_uint(CborEncoder *encoder, uint64_t value)
memcpy(&prevEncoder, encoder, sizeof(prevEncoder));
CborError err = oc_rep_encode_uint_internal(encoder, value);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -473,7 +488,7 @@ oc_rep_encode_floating_point(CborEncoder *encoder, CborType fpType,
memcpy(&prevEncoder, encoder, sizeof(prevEncoder));
CborError err = oc_rep_encode_floating_point_internal(encoder, fpType, value);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -503,7 +518,7 @@ oc_rep_encode_double(CborEncoder *encoder, double value)
memcpy(&prevEncoder, encoder, sizeof(prevEncoder));
CborError err = oc_rep_encode_double_internal(encoder, value);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -535,7 +550,7 @@ oc_rep_encode_text_string(CborEncoder *encoder, const char *string,
memcpy(&prevEncoder, encoder, sizeof(prevEncoder));
CborError err = oc_rep_encode_text_string_internal(encoder, string, length);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -568,7 +583,7 @@ oc_rep_encode_byte_string(CborEncoder *encoder, const uint8_t *string,
memcpy(&prevEncoder, encoder, sizeof(prevEncoder));
CborError err = oc_rep_encode_byte_string_internal(encoder, string, length);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -605,7 +620,8 @@ oc_rep_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder,
CborError err =
oc_rep_encoder_create_array_internal(encoder, arrayEncoder, length);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(arrayEncoder));
err =
rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(arrayEncoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -643,7 +659,7 @@ oc_rep_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder,
CborError err =
oc_rep_encoder_create_map_internal(encoder, mapEncoder, length);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(mapEncoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(mapEncoder));
if (err != CborNoError) {
return err;
}
Expand Down Expand Up @@ -681,7 +697,7 @@ oc_rep_encoder_close_container(CborEncoder *encoder,
CborError err =
oc_rep_encoder_close_container_internal(encoder, containerEncoder);
if (err == CborErrorOutOfMemory) {
err = realloc_buffer(oc_rep_encoder_get_extra_bytes_needed(encoder));
err = rep_buffer_realloc(oc_rep_encoder_get_extra_bytes_needed(encoder));
if (err != CborNoError) {
return err;
}
Expand Down
Loading

0 comments on commit 6687a0d

Please sign in to comment.