5#ifndef __PROSECCOAP_UTILS_H__
6#define __PROSECCOAP_UTILS_H__
35#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
37 memcpy(result, &value,
sizeof(T));
40 for (
size_t i = 0; i <
sizeof(T); ++i)
43 result[i] =
static_cast<uint8_t
>((value >> ((
sizeof(T) - 1 - i) * 8)) & 0xFF);
57 memcpy(&int_value, &value,
sizeof(
float));
70 memcpy(&int_value, &value,
sizeof(
double));
95 return static_cast<uint16_t
>((
static_cast<uint16_t
>(bytes[0]) << 8) |
96 static_cast<uint16_t
>(bytes[1]));
104 return (
static_cast<uint32_t
>(bytes[0]) << 24) |
105 (
static_cast<uint32_t
>(bytes[1]) << 16) |
106 (
static_cast<uint32_t
>(bytes[2]) << 8) |
107 (
static_cast<uint32_t
>(bytes[3]));
115 return (
static_cast<uint64_t
>(bytes[0]) << 56) |
116 (
static_cast<uint64_t
>(bytes[1]) << 48) |
117 (
static_cast<uint64_t
>(bytes[2]) << 40) |
118 (
static_cast<uint64_t
>(bytes[3]) << 32) |
119 (
static_cast<uint64_t
>(bytes[4]) << 24) |
120 (
static_cast<uint64_t
>(bytes[5]) << 16) |
121 (
static_cast<uint64_t
>(bytes[6]) << 8) |
122 (
static_cast<uint64_t
>(bytes[7]));
157 memcpy(&host_float, &value,
sizeof(
float));
166 template <
typename T =
double>
169 static_assert(
sizeof(T) == 8,
"Cannot read a 64-bit network double on a 32-bit double architecture (like AVR).");
172 memcpy(&host_double, &value,
sizeof(
double));
constexpr int32_t read_int32(const uint8_t *bytes)
Reads a 32-bit signed integer from a Big-Endian byte stream.
Definition Utils.h:137
T read_double(const uint8_t *bytes)
Reads a 64-bit double from a Big-Endian byte stream.
Definition Utils.h:167
float read_float(const uint8_t *bytes)
Reads a 32-bit float from a Big-Endian byte stream.
Definition Utils.h:153
constexpr uint32_t read_uint32(const uint8_t *bytes)
Reads a 32-bit unsigned integer from a Big-Endian byte stream.
Definition Utils.h:102
constexpr int16_t read_int16(const uint8_t *bytes)
Reads a 16-bit signed integer from a Big-Endian byte stream.
Definition Utils.h:128
constexpr uint16_t read_uint16(const uint8_t *bytes)
Reads a 16-bit unsigned integer from a Big-Endian byte stream.
Definition Utils.h:93
void toNetworkByteOrder(T value, uint8_t *result)
Convert an integer-like value to network byte order (i.e. big-endian).
Definition Utils.h:33
constexpr uint64_t read_uint64(const uint8_t *bytes)
Reads a 64-bit unsigned integer from a Big-Endian byte stream.
Definition Utils.h:113
constexpr int64_t read_int64(const uint8_t *bytes)
Reads a 64-bit signed integer from a Big-Endian byte stream.
Definition Utils.h:145
Namespace for the library.
Definition Definitions.h:161