Skip to content

Commit

Permalink
feat: use endianness module in api/slice
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Jun 21, 2024
1 parent a6ee010 commit 4fafb7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
13 changes: 5 additions & 8 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "zenoh-pico/system/platform.h"
#include "zenoh-pico/transport/multicast.h"
#include "zenoh-pico/transport/unicast.h"
#include "zenoh-pico/utils/endianness.h"
#include "zenoh-pico/utils/logging.h"
#include "zenoh-pico/utils/result.h"
#include "zenoh-pico/utils/uuid.h"
Expand Down Expand Up @@ -351,9 +352,7 @@ int8_t zp_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_byt
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
// Extract first item size
size_t first_len = 0;
// FIXME: size endianness, Issue #420
memcpy(&first_len, &bytes->_slice.start[*curr_idx], sizeof(uint32_t));
size_t first_len = _z_host_le_load32(&bytes->_slice.start[*curr_idx]);
*curr_idx += sizeof(uint32_t);
// Allocate first item bytes
*first->_val = _z_bytes_make(first_len);
Expand All @@ -365,8 +364,7 @@ int8_t zp_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_byt
*curr_idx += first_len;

// Extract second item size
size_t second_len = 0;
memcpy(&second_len, &bytes->_slice.start[*curr_idx], sizeof(uint32_t));
size_t second_len = _z_host_le_load32(&bytes->_slice.start[*curr_idx]);
*curr_idx += sizeof(uint32_t);
// Allocate second item bytes
*second->_val = _z_bytes_make(second_len);
Expand Down Expand Up @@ -571,12 +569,11 @@ int8_t zp_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *fir
size_t first_len = z_slice_len(&first->_val->_slice);
size_t second_len = z_slice_len(&second->_val->_slice);
// Copy data
// FIXME: size endianness, Issue #420
memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], &first_len, sizeof(uint32_t));
_z_host_le_store32((uint32_t)first_len, (uint8_t *)&bytes->_val->_slice.start[*curr_idx]);
*curr_idx += sizeof(uint32_t);
memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], z_slice_data(&first->_val->_slice), first_len);
*curr_idx += first_len;
memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], &second_len, sizeof(uint32_t));
_z_host_le_store32((uint32_t)second_len, (uint8_t *)&bytes->_val->_slice.start[*curr_idx]);
*curr_idx += sizeof(uint32_t);
memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], z_slice_data(&second->_val->_slice), second_len);
*curr_idx += second_len;
Expand Down
26 changes: 7 additions & 19 deletions src/collections/slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <string.h>

#include "zenoh-pico/system/platform.h"
#include "zenoh-pico/utils/endianness.h"
#include "zenoh-pico/utils/result.h"

/*-------- Slice --------*/
Expand Down Expand Up @@ -162,24 +163,11 @@ uint8_t _z_bytes_to_uint8(const _z_bytes_t *bs) {
return val;
}

// FIXME: int16+ endianness, Issue #420
uint16_t _z_bytes_to_uint16(const _z_bytes_t *bs) {
uint16_t val = 0;
memcpy(&val, bs->_slice.start, sizeof(val));
return val;
}
uint16_t _z_bytes_to_uint16(const _z_bytes_t *bs) { return _z_host_le_load16(bs->_slice.start); }

uint32_t _z_bytes_to_uint32(const _z_bytes_t *bs) {
uint32_t val = 0;
memcpy(&val, bs->_slice.start, sizeof(val));
return val;
}
uint32_t _z_bytes_to_uint32(const _z_bytes_t *bs) { return _z_host_le_load32(bs->_slice.start); }

uint64_t _z_bytes_to_uint64(const _z_bytes_t *bs) {
uint64_t val = 0;
memcpy(&val, bs->_slice.start, sizeof(val));
return val;
}
uint64_t _z_bytes_to_uint64(const _z_bytes_t *bs) { return _z_host_le_load64(bs->_slice.start); }

float _z_bytes_to_float(const _z_bytes_t *bs) {
float val = 0;
Expand Down Expand Up @@ -222,7 +210,7 @@ _z_bytes_t _z_bytes_from_uint16(uint16_t val) {
return ret;
}
// Encode int
memcpy((uint8_t *)ret._slice.start, &val, sizeof(val));
_z_host_le_store16(val, (uint8_t *)ret._slice.start);
return ret;
}

Expand All @@ -233,7 +221,7 @@ _z_bytes_t _z_bytes_from_uint32(uint32_t val) {
return ret;
}
// Encode int
memcpy((uint8_t *)ret._slice.start, &val, sizeof(val));
_z_host_le_store32(val, (uint8_t *)ret._slice.start);
return ret;
}

Expand All @@ -244,7 +232,7 @@ _z_bytes_t _z_bytes_from_uint64(uint64_t val) {
return ret;
}
// Encode int
memcpy((uint8_t *)ret._slice.start, &val, sizeof(val));
_z_host_le_store64(val, (uint8_t *)ret._slice.start);
return ret;
}

Expand Down

0 comments on commit 4fafb7f

Please sign in to comment.