Skip to content

Commit

Permalink
improve tk_buffer_set_value/tk_buffer_get_value
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Mar 12, 2024
1 parent efc0fa5 commit 98a0369
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
3 changes: 3 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 最新动态

2024/03/12
* 完善函数 tk\_buffer\_set\_value/tk\_buffer\_get\_value

2024/03/10
* 增加函数tk\_basic\_type\_from\_name

Expand Down
55 changes: 40 additions & 15 deletions src/tkc/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,8 @@ ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_INT16: {
if (bit_offset < 0) {
*(int16_t*)data = value_int16(value);
int16_t v = value_int16(value);
memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 16, RET_BAD_PARAMS);
bits_stream_set(data, 2, bit_offset, value_int16(value) != 0);
Expand All @@ -2299,7 +2300,8 @@ ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_UINT16: {
if (bit_offset < 0) {
*(uint16_t*)data = value_uint16(value);
uint16_t v = value_uint16(value);
memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 16, RET_BAD_PARAMS);
bits_stream_set(data, 2, bit_offset, value_uint16(value) != 0);
Expand All @@ -2308,7 +2310,8 @@ ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_INT32: {
if (bit_offset < 0) {
*(int32_t*)data = value_int32(value);
int32_t v = value_int32(value);
memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 32, RET_BAD_PARAMS);
bits_stream_set(data, 4, bit_offset, value_int32(value) != 0);
Expand All @@ -2317,7 +2320,8 @@ ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_UINT32: {
if (bit_offset < 0) {
*(uint32_t*)data = value_uint32(value);
uint32_t v = value_uint32(value);
memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 32, RET_BAD_PARAMS);
bits_stream_set(data, 4, bit_offset, value_uint32(value) != 0);
Expand All @@ -2326,7 +2330,8 @@ ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_INT64: {
if (bit_offset < 0) {
*(int64_t*)data = value_int64(value);
int64_t v = value_int64(value);
memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 64, RET_BAD_PARAMS);
bits_stream_set(data, 8, bit_offset, value_int64(value) != 0);
Expand All @@ -2335,7 +2340,8 @@ ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_UINT64: {
if (bit_offset < 0) {
*(uint64_t*)data = value_uint64(value);
uint64_t v = value_uint64(value);
memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 64, RET_BAD_PARAMS);
bits_stream_set(data, 8, bit_offset, value_uint64(value) != 0);
Expand All @@ -2344,12 +2350,14 @@ ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_FLOAT32: {
assert(bit_offset < 0);
*(float*)data = value_float32(value);
float v = value_float32(value);
memcpy(data, &v, sizeof(v));
break;
}
case VALUE_TYPE_DOUBLE: {
assert(bit_offset < 0);
*(double*)data = value_double(value);
double v = value_double(value);
memcpy(data, &v, sizeof(v));
break;
}
default:
Expand Down Expand Up @@ -2388,7 +2396,9 @@ ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_INT16: {
if (bit_offset < 0) {
value_set_int16(value, *(int16_t*)data);
int16_t v = 0;
memcpy(&v, data, sizeof(v));
value_set_int16(value, v);
} else {
return_value_if_fail(bit_offset < 16, RET_BAD_PARAMS);
bits_stream_get(data, 2, bit_offset, &v);
Expand All @@ -2399,6 +2409,9 @@ ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int
case VALUE_TYPE_UINT16: {
if (bit_offset < 0) {
value_set_uint16(value, *(uint16_t*)data);
uint16_t v = 0;
memcpy(&v, data, sizeof(v));
value_set_uint16(value, v);
} else {
return_value_if_fail(bit_offset < 16, RET_BAD_PARAMS);
bits_stream_get(data, 2, bit_offset, &v);
Expand All @@ -2408,7 +2421,9 @@ ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_INT32: {
if (bit_offset < 0) {
value_set_int32(value, *(int32_t*)data);
int32_t v = 0;
memcpy(&v, data, sizeof(v));
value_set_int32(value, v);
} else {
return_value_if_fail(bit_offset < 32, RET_BAD_PARAMS);
bits_stream_get(data, 4, bit_offset, &v);
Expand All @@ -2418,7 +2433,9 @@ ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_UINT32: {
if (bit_offset < 0) {
value_set_uint32(value, *(uint32_t*)data);
uint32_t v = 0;
memcpy(&v, data, sizeof(v));
value_set_uint32(value, v);
} else {
return_value_if_fail(bit_offset < 32, RET_BAD_PARAMS);
bits_stream_get(data, 4, bit_offset, &v);
Expand All @@ -2428,7 +2445,9 @@ ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_INT64: {
if (bit_offset < 0) {
value_set_int64(value, *(int64_t*)data);
int64_t v = 0;
memcpy(&v, data, sizeof(v));
value_set_int64(value, v);
} else {
return_value_if_fail(bit_offset < 64, RET_BAD_PARAMS);
bits_stream_get(data, 8, bit_offset, &v);
Expand All @@ -2438,7 +2457,9 @@ ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int
}
case VALUE_TYPE_UINT64: {
if (bit_offset < 0) {
value_set_uint64(value, *(uint64_t*)data);
uint64_t v = 0;
memcpy(&v, data, sizeof(v));
value_set_uint64(value, v);
} else {
return_value_if_fail(bit_offset < 64, RET_BAD_PARAMS);
bits_stream_get(data, 8, bit_offset, &v);
Expand All @@ -2447,13 +2468,17 @@ ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int
break;
}
case VALUE_TYPE_FLOAT32: {
float v = 0;
assert(bit_offset < 0);
value_set_float32(value, *(float*)data);
memcpy(&v, data, sizeof(v));
value_set_float32(value, v);
break;
}
case VALUE_TYPE_DOUBLE: {
double v = 0;
assert(bit_offset < 0);
value_set_double(value, *(double*)data);
memcpy(&v, data, sizeof(v));
value_set_double(value, v);
break;
}
default:
Expand Down
13 changes: 12 additions & 1 deletion tests/utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,17 @@ TEST(Utils, buffer_get_value_double) {
ASSERT_EQ(value_int(&v), buffer[4]);
}

TEST(Utils, buffer_get_value_double_unaligned) {
value_t v;
double buffer[] = {0, 1,2,3,4,5,6};

value_set_double(&v, 123);
ASSERT_EQ(tk_buffer_set_value((uint8_t*)buffer+1, sizeof(buffer), VALUE_TYPE_DOUBLE, 1, -1, &v), RET_OK);

ASSERT_EQ(tk_buffer_get_value((uint8_t*)buffer+1, sizeof(buffer), VALUE_TYPE_DOUBLE, 1, -1, &v), RET_OK);
ASSERT_EQ(value_double(&v), 123);
}

TEST(Utils, buffer_set_value_double) {
value_t v;
double buffer[] = {0, 0, 0, 0, 0, 0, 0, 0};
Expand Down Expand Up @@ -1610,4 +1621,4 @@ TEST(Utils, tk_basic_type_from_name) {
ASSERT_EQ(tk_basic_type_from_name("double_t"), VALUE_TYPE_DOUBLE);
ASSERT_EQ(tk_basic_type_from_name("bool_t"), VALUE_TYPE_BOOL);
ASSERT_EQ(tk_basic_type_from_name("unknown"), VALUE_TYPE_INVALID);
}
}

0 comments on commit 98a0369

Please sign in to comment.