Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add api about doing with basic data type #2195

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 172 additions & 65 deletions include/neuron/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,76 +474,32 @@ static inline void neu_value16_set_bit(neu_value16_u *value, uint8_t index,
}
}

static inline void neu_ntohs24_p(uint8_t *value)
{
uint8_t t = 0;

t = value[0];
value[0] = value[2];
value[2] = t;
}

static inline void neu_htons24_p(uint8_t *value)
static inline uint16_t neu_get_u16(uint8_t *bytes)
{
neu_ntohs24_p(value);
}
uint16_t ret = 0;
uint8_t *t = NULL;

static inline uint64_t neu_htonll(uint64_t u)
{
uint8_t *bytes = (uint8_t *) &u;
uint64_t ret = 0;
uint8_t *ret_bytes = (uint8_t *) &ret;
t = (uint8_t *) &ret;

ret_bytes[0] = bytes[7];
ret_bytes[1] = bytes[6];
ret_bytes[2] = bytes[5];
ret_bytes[3] = bytes[4];
ret_bytes[4] = bytes[3];
ret_bytes[5] = bytes[2];
ret_bytes[6] = bytes[1];
ret_bytes[7] = bytes[0];
t[0] = bytes[0];
t[1] = bytes[1];

return ret;
}

static inline uint64_t neu_ntohll(uint64_t u)
static inline void neu_set_u16(uint8_t *bytes, uint16_t val)
{
return neu_htonll(u);
}
uint8_t *t = NULL;

static inline void neu_ntonll_p(uint64_t *pu)
{
*pu = neu_htonll(*pu);
}
t = (uint8_t *) &val;

static inline void neu_htonll_p(uint64_t *pu)
{
neu_ntonll_p(pu);
bytes[0] = t[0];
bytes[1] = t[1];
}

static inline void neu_ntohl_p(uint32_t *pu)
{
*pu = ntohl(*pu);
}

static inline void neu_htonl_p(uint32_t *pu)
{
neu_ntohl_p(pu);
}

static inline void neu_ntohs_p(uint16_t *pu)
{
*pu = ntohs(*pu);
}

static inline void neu_htons_p(uint16_t *pu)
{
neu_ntohs_p(pu);
}

static inline uint16_t neu_get_u16(uint8_t *bytes)
static inline int16_t neu_get_i16(uint8_t *bytes)
{
uint16_t ret = 0;
int16_t ret = 0;
uint8_t *t = NULL;

t = (uint8_t *) &ret;
Expand All @@ -554,17 +510,14 @@ static inline uint16_t neu_get_u16(uint8_t *bytes)
return ret;
}

static inline int16_t neu_get_i16(uint8_t *bytes)
static inline void neu_set_i16(uint8_t *bytes, int16_t val)
{
int16_t ret = 0;
uint8_t *t = NULL;
uint8_t *t = NULL;

t = (uint8_t *) &ret;
t = (uint8_t *) &val;

t[0] = bytes[0];
t[1] = bytes[1];

return ret;
bytes[0] = t[0];
bytes[1] = t[1];
}

static inline uint32_t neu_get_u32(uint8_t *bytes)
Expand All @@ -582,6 +535,18 @@ static inline uint32_t neu_get_u32(uint8_t *bytes)
return ret;
}

static inline void neu_set_u32(uint8_t *bytes, uint32_t val)
{
uint8_t *t = NULL;

t = (uint8_t *) &val;

bytes[0] = t[0];
bytes[1] = t[1];
bytes[2] = t[2];
bytes[3] = t[3];
}

static inline int32_t neu_get_i32(uint8_t *bytes)
{
int32_t ret = 0;
Expand All @@ -597,6 +562,18 @@ static inline int32_t neu_get_i32(uint8_t *bytes)
return ret;
}

static inline void neu_set_i32(uint8_t *bytes, int32_t val)
{
uint8_t *t = NULL;

t = (uint8_t *) &val;

bytes[0] = t[0];
bytes[1] = t[1];
bytes[2] = t[2];
bytes[3] = t[3];
}

static inline float neu_get_f32(uint8_t *bytes)
{
float ret = 0;
Expand All @@ -612,6 +589,18 @@ static inline float neu_get_f32(uint8_t *bytes)
return ret;
}

static inline void neu_set_f32(uint8_t *bytes, float val)
{
uint8_t *t = NULL;

t = (uint8_t *) &val;

bytes[0] = t[0];
bytes[1] = t[1];
bytes[2] = t[2];
bytes[3] = t[3];
}

static inline uint64_t neu_get_u64(uint8_t *bytes)
{
uint64_t ret = 0;
Expand All @@ -631,6 +620,22 @@ static inline uint64_t neu_get_u64(uint8_t *bytes)
return ret;
}

static inline void neu_set_u64(uint8_t *bytes, uint64_t val)
{
uint8_t *t = NULL;

t = (uint8_t *) &val;

bytes[0] = t[0];
bytes[1] = t[1];
bytes[2] = t[2];
bytes[3] = t[3];
bytes[4] = t[4];
bytes[5] = t[5];
bytes[6] = t[6];
bytes[7] = t[7];
}

static inline int64_t neu_get_i64(uint8_t *bytes)
{
int64_t ret = 0;
Expand All @@ -650,6 +655,22 @@ static inline int64_t neu_get_i64(uint8_t *bytes)
return ret;
}

static inline void neu_set_i64(uint8_t *bytes, int64_t val)
{
uint8_t *t = NULL;

t = (uint8_t *) &val;

bytes[0] = t[0];
bytes[1] = t[1];
bytes[2] = t[2];
bytes[3] = t[3];
bytes[4] = t[4];
bytes[5] = t[5];
bytes[6] = t[6];
bytes[7] = t[7];
}

static inline double neu_get_f64(uint8_t *bytes)
{
double ret = 0;
Expand All @@ -669,6 +690,92 @@ static inline double neu_get_f64(uint8_t *bytes)
return ret;
}

static inline void neu_set_f64(uint8_t *bytes, double val)
{
uint8_t *t = NULL;

t = (uint8_t *) &val;

bytes[0] = t[0];
bytes[1] = t[1];
bytes[2] = t[2];
bytes[3] = t[3];
bytes[4] = t[4];
bytes[5] = t[5];
bytes[6] = t[6];
bytes[7] = t[7];
}

static inline void neu_ntohs24_p(uint8_t *value)
{
uint8_t t = 0;

t = value[0];
value[0] = value[2];
value[2] = t;
}

static inline void neu_htons24_p(uint8_t *value)
{
neu_ntohs24_p(value);
}

static inline uint64_t neu_htonll(uint64_t u)
{
uint8_t *bytes = (uint8_t *) &u;
uint64_t ret = 0;
uint8_t *ret_bytes = (uint8_t *) &ret;

ret_bytes[0] = bytes[7];
ret_bytes[1] = bytes[6];
ret_bytes[2] = bytes[5];
ret_bytes[3] = bytes[4];
ret_bytes[4] = bytes[3];
ret_bytes[5] = bytes[2];
ret_bytes[6] = bytes[1];
ret_bytes[7] = bytes[0];

return ret;
}

static inline uint64_t neu_ntohll(uint64_t u)
{
return neu_htonll(u);
}

static inline void neu_ntonll_p(uint64_t *pu)
{
uint64_t ret = neu_htonll(neu_get_u64((uint8_t *) pu));
neu_set_u64((uint8_t *) pu, ret);
}

static inline void neu_htonll_p(uint64_t *pu)
{
neu_ntonll_p(pu);
}

static inline void neu_ntohl_p(uint32_t *pu)
{
uint32_t ret = ntohl(neu_get_u32((uint8_t *) pu));
neu_set_u32((uint8_t *) pu, ret);
}

static inline void neu_htonl_p(uint32_t *pu)
{
neu_ntohl_p(pu);
}

static inline void neu_ntohs_p(uint16_t *pu)
{
uint16_t ret = ntohs(neu_get_u16((uint8_t *) pu));
neu_set_u16((uint8_t *) pu, ret);
}

static inline void neu_htons_p(uint16_t *pu)
{
neu_ntohs_p(pu);
}

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 9 additions & 0 deletions tests/ut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ target_include_directories(mqtt_client_test PRIVATE
)
target_link_libraries(mqtt_client_test neuron-base gtest_main gtest)


add_executable(common_test common_test.cc)
target_include_directories(common_test PRIVATE
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/include
)
target_link_libraries(common_test neuron-base gtest_main gtest)

include(GoogleTest)
gtest_discover_tests(json_test)
gtest_discover_tests(http_test)
Expand All @@ -81,3 +89,4 @@ gtest_discover_tests(modbus_test)
gtest_discover_tests(async_queue_test)
gtest_discover_tests(rolling_counter_test)
gtest_discover_tests(mqtt_client_test)
gtest_discover_tests(common_test)
Loading
Loading