Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Modules | Macros | Functions
Soletta utility functions.

Contains helpers to manipulate time, error code/string, overflows, encode/decode data and some converters. More...

Modules

 Soletta file utility functions
 Contains helpers to deal with files.
 

Macros

#define sol_util_array_size(arr)   (sizeof(arr) / sizeof((arr)[0]))
 Calculates the number of elements in an array. More...
 
#define sol_util_max(x, y)
 Gets the maximum value. More...
 
#define sol_util_min(x, y)
 Gets the minimum value. More...
 
#define SOL_UTIL_MSEC_PER_SEC   1000ULL
 number of milliseconds in a second: 1,000. More...
 
#define SOL_UTIL_NSEC_PER_MSEC   1000000ULL
 number of nanoseconds in a milliseconds: 1,000,000,000 / 1,000 = 1,000,000. More...
 
#define SOL_UTIL_NSEC_PER_SEC   1000000000ULL
 number of nanoseconds in a second: 1,000,000,000. More...
 
#define SOL_UTIL_NSEC_PER_USEC   1000ULL
 number of nanoseconds in a microsecond: 1,000,000,000 / 1,000,000 = 1,000. More...
 
#define sol_util_strerrora(errnum)
 Gets a string from a given error using the stack. More...
 
#define sol_util_uint16_bytes_swap(val)   ((uint16_t)((((val) >> 8) & 0xff) | (((val) & 0xff) << 8)))
 Swaps the bytes of a 16 bytes unsigned int. More...
 
#define sol_util_uint32_bytes_swap(val)
 Swaps the bytes of a 32 bytes unsigned int. More...
 
#define sol_util_uint64_bytes_swap(val)
 Swaps the bytes of a 32 bytes unsigned int. More...
 
#define SOL_UTIL_USEC_PER_SEC   1000000ULL
 number of microseconds in a second: 1,000,000. More...
 

Functions

static ssize_t sol_util_base16_calculate_decoded_len (const struct sol_str_slice slice)
 Calculate the size necessary to decode a given slice in base16. More...
 
static ssize_t sol_util_base16_calculate_encoded_len (const struct sol_str_slice slice)
 Calculate the size necessary to encode a given slice in base16. More...
 
ssize_t sol_util_base16_decode (void *buf, size_t buflen, const struct sol_str_slice slice, enum sol_decode_case decode_case)
 Decode the binary slice from base16 (hexadecimal). More...
 
ssize_t sol_util_base16_encode (void *buf, size_t buflen, const struct sol_str_slice slice, bool uppercase)
 Encode the binary slice to base16 (hexadecimal). More...
 
ssize_t sol_util_base64_calculate_decoded_len (const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
 Calculate the size necessary to decode a given slice in base64. More...
 
static ssize_t sol_util_base64_calculate_encoded_len (const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
 Calculate the size necessary to encode a given slice in base64. More...
 
ssize_t sol_util_base64_decode (void *buf, size_t buflen, const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
 Decode the binary slice from base64 using the given map. More...
 
ssize_t sol_util_base64_encode (void *buf, size_t buflen, const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
 Encode the binary slice to base64 using the given map. More...
 
static uint16_t sol_util_be16_to_cpu (uint16_t val)
 Convert a 16 bytes big endian integer to cpu endianness. More...
 
static uint32_t sol_util_be32_to_cpu (uint32_t val)
 Convert a 32 bytes big endian integer to cpu endianness. More...
 
static uint64_t sol_util_be64_to_cpu (uint64_t val)
 Convert a 64 bytes big endian integer to cpu endianness. More...
 
static void sol_util_clear_memory_secure (void *buf, size_t len)
 Clear an allocated memory securely. More...
 
static uint16_t sol_util_cpu_to_be16 (uint16_t val)
 Convert a 16 bytes integer to big endian format. More...
 
static uint32_t sol_util_cpu_to_be32 (uint32_t val)
 Convert a 32 bytes integer to big endian format. More...
 
static uint64_t sol_util_cpu_to_be64 (uint64_t val)
 Convert a 64 bytes integer to big endian format. More...
 
static uint16_t sol_util_cpu_to_le16 (uint16_t val)
 Convert a 16 bytes integer to little endian format. More...
 
static uint32_t sol_util_cpu_to_le32 (uint32_t val)
 Convert a 32 bytes integer to little endian format. More...
 
static uint64_t sol_util_cpu_to_le64 (uint64_t val)
 Convert a 64 bytes integer to little endian format. More...
 
bool sol_util_double_eq (double var0, double var1)
 Checks var0 and var1 for equality. More...
 
static int32_t sol_util_int32_clamp (int32_t start, int32_t end, int32_t value)
 Restricts a number between two other numbers. More...
 
int sol_util_int32_mul (int32_t op1, int32_t op2, int32_t *out)
 Multiply two values checking for overflow. More...
 
int sol_util_int64_mul (int64_t op1, int64_t op2, int64_t *out)
 Multiply two values checking for overflow. More...
 
static uint16_t sol_util_le16_to_cpu (uint16_t val)
 Convert a 16 bytes little endian integer to cpu endianness. More...
 
static uint32_t sol_util_le32_to_cpu (uint32_t val)
 Convert a 32 bytes little endian integer to cpu endianness. More...
 
static uint64_t sol_util_le64_to_cpu (uint64_t val)
 Convert a 64 bytes little endian integer to cpu endianness. More...
 
void * sol_util_memdup (const void *data, size_t len)
 Duplicate memory. More...
 
static int sol_util_msec_from_timespec (const struct timespec *ts)
 Gets the number of milliseconds for given time. More...
 
int sol_util_replace_str_from_slice_if_changed (char **str, const struct sol_str_slice slice)
 Replace the string's contents. More...
 
int sol_util_replace_str_if_changed (char **str, const char *new_str)
 Replace the string's contents. More...
 
int sol_util_size_add (size_t op1, size_t op2, size_t *out)
 Add two values checking for overflow. More...
 
int sol_util_size_mul (size_t op1, size_t op2, size_t *out)
 Multiply two values checking for overflow. More...
 
int sol_util_size_sub (size_t op1, size_t op2, size_t *out)
 Subtract two values checking for overflow. More...
 
int sol_util_ssize_mul (ssize_t op1, ssize_t op2, ssize_t *out)
 Multiply two values checking for overflow. More...
 
char * sol_util_strerror (int errnum, struct sol_buffer *buf)
 Gets a string from a given error. More...
 
ssize_t sol_util_strftime (struct sol_buffer *buf, const char *format, const struct tm *timeptr, bool use_locale) SOL_ATTR_STRFTIME(2)
 Wrapper around strftime()/strftime_l() More...
 
double sol_util_strtod_n (const char *nptr, char **endptr, ssize_t len, bool use_locale)
 Wrapper over strtod() that consumes up to len bytes and may not use a locale. More...
 
long int sol_util_strtol_n (const char *nptr, char **endptr, ssize_t len, int base)
 Wrapper over strtol() that consumes up to len bytes. More...
 
unsigned long int sol_util_strtoul_n (const char *nptr, char **endptr, ssize_t len, int base)
 Wrapper over strtoul() that consumes up to len bytes. More...
 
static void sol_util_timespec_add (const struct timespec *t1, const struct timespec *t2, struct timespec *result)
 Sum two time values. More...
 
static int sol_util_timespec_compare (const struct timespec *t1, const struct timespec *t2)
 Compare two time values. More...
 
static struct timespec sol_util_timespec_from_msec (int msec)
 Create a struct timespec from milliseconds. More...
 
static struct timespec sol_util_timespec_from_usec (int usec)
 Create a struct timespec from microseconds. More...
 
struct timespec sol_util_timespec_get_current (void)
 Gets the current time (Monotonic). More...
 
int sol_util_timespec_get_realtime (struct timespec *t)
 Gets the current time (System-wide clock). More...
 
static void sol_util_timespec_sub (const struct timespec *t1, const struct timespec *t2, struct timespec *result)
 Subtracts two time values. More...
 
int sol_util_uint32_mul (uint32_t op1, uint32_t op2, uint32_t *out)
 Multiply two values checking for overflow. More...
 
int sol_util_uint64_add (uint64_t op1, uint64_t op2, uint64_t *out)
 Add two values checking for overflow. More...
 
int sol_util_uint64_mul (uint64_t op1, uint64_t op2, uint64_t *out)
 Multiply two values checking for overflow. More...
 
int sol_util_unescape_quotes (const struct sol_str_slice slice, struct sol_buffer *buf)
 Unescape a string removing quotes from it. More...
 
int32_t sol_util_unicode_code_from_utf8 (const uint8_t *buf, size_t buf_len, uint8_t *bytes_read)
 Convert a utf-8 character to unicode code. More...
 
static int sol_util_usec_from_timespec (const struct timespec *ts)
 Gets the number of microseconds for given time. More...
 
int8_t sol_util_utf8_from_unicode_code (uint8_t *buf, size_t buf_len, uint32_t unicode_code)
 Convert from unicode code to utf-8 string. More...
 
int sol_util_uuid_bytes_from_string (struct sol_str_slice uuid_str, struct sol_buffer *uuid_bytes)
 Convert a UUID in string format to a byte array with UUID bytes. More...
 
int sol_util_uuid_gen (bool uppercase, bool with_hyphens, struct sol_buffer *uuid_buf)
 Generates a new universally unique identifier (UUID) string. More...
 
bool sol_util_uuid_str_is_valid (const struct sol_str_slice uuid)
 Checks if a given universally unique identifier (UUID), in string form, is valid. More...
 
int sol_util_uuid_string_from_bytes (bool uppercase, bool with_hyphens, const uint8_t uuid_bytes[SOL_STATIC_ARRAY_SIZE(16)], struct sol_buffer *uuid_str)
 Convert a UUID in byte format to UUID string format. More...
 

Detailed Description

Contains helpers to manipulate time, error code/string, overflows, encode/decode data and some converters.

Macro Definition Documentation

#define sol_util_array_size (   arr)    (sizeof(arr) / sizeof((arr)[0]))
#define sol_util_max (   x,
 
)
Value:
({ \
__typeof__(x)_max1 = (x); \
__typeof__(y)_max2 = (y); \
(void)(&_max1 == &_max2); \
_max1 > _max2 ? _max1 : _max2; \
})

Gets the maximum value.

This will generate shadowing warnings if the same kind is nested, but the warnings on type mismatch are a fair benefit.

Use temporary variables to address nested call cases.

#define sol_util_min (   x,
 
)
Value:
({ \
__typeof__(x)_min1 = (x); \
__typeof__(y)_min2 = (y); \
(void)(&_min1 == &_min2); \
_min1 < _min2 ? _min1 : _min2; \
})

Gets the minimum value.

This will generate shadowing warnings if the same kind is nested, but the warnings on type mismatch are a fair benefit.

Use temporary variables to address nested call cases.

#define SOL_UTIL_MSEC_PER_SEC   1000ULL

number of milliseconds in a second: 1,000.

Referenced by sol_util_msec_from_timespec(), and sol_util_timespec_from_msec().

#define SOL_UTIL_NSEC_PER_MSEC   1000000ULL

number of nanoseconds in a milliseconds: 1,000,000,000 / 1,000 = 1,000,000.

Referenced by sol_util_msec_from_timespec(), and sol_util_timespec_from_msec().

#define SOL_UTIL_NSEC_PER_SEC   1000000000ULL

number of nanoseconds in a second: 1,000,000,000.

Examples:
/src/samples/crypto/message-digest.c.

Referenced by print_time(), sol_util_timespec_add(), and sol_util_timespec_sub().

#define SOL_UTIL_NSEC_PER_USEC   1000ULL

number of nanoseconds in a microsecond: 1,000,000,000 / 1,000,000 = 1,000.

Referenced by sol_util_timespec_from_usec(), and sol_util_usec_from_timespec().

#define sol_util_strerrora (   errnum)
Value:
({ \
SOL_BUFFER_DECLARE_STATIC(buf ## __COUNT__, 512); \
sol_util_strerror((errnum), &buf ## __COUNT__); \
})
#define SOL_BUFFER_DECLARE_STATIC(name_, size_)
A helper macro to create a static allocated buffer with a fixed capacity.
Definition: sol-buffer.h:203
char * sol_util_strerror(int errnum, struct sol_buffer *buf)
Gets a string from a given error.

Gets a string from a given error using the stack.

The function returns a pointer to a string (created using the stack) that describes the error code passed in the argument errnum.

Parameters
errnumThe error code
Returns
the appropriate error description string.
See Also
sol_util_strerror
Examples:
/src/samples/common/uart.c, /src/samples/crypto/message-digest.c, /src/samples/crypto/sha256sum.c, /src/samples/design_patterns/stream_sample.c, /src/samples/flow/c-api/single-node.c, /src/samples/http/server-sse.c, /src/samples/http/server.c, /src/samples/mavlink/basic.c, /src/samples/mavlink/goto.c, and /src/samples/network/echo-server.c.

Referenced by get_int32_packet_and_log(), mavlink_connect_cb(), mission_reached_cb(), mode_changed_cb(), on_feed_done(), on_stdin(), on_stdin_hash(), position_changed_cb(), producer_data_written(), producer_make_data(), send_blob(), startup(), startup_server(), and takeoff().

#define sol_util_uint16_bytes_swap (   val)    ((uint16_t)((((val) >> 8) & 0xff) | (((val) & 0xff) << 8)))

Swaps the bytes of a 16 bytes unsigned int.

Referenced by sol_util_be16_to_cpu(), sol_util_cpu_to_be16(), sol_util_cpu_to_le16(), and sol_util_le16_to_cpu().

#define sol_util_uint32_bytes_swap (   val)
Value:
((uint32_t)((((val) & 0xff000000) >> 24) | (((val) & 0x00ff0000) >> 8) | \
(((val) & 0x0000ff00) << 8) | (((val) & 0x000000ff) << 24)))

Swaps the bytes of a 32 bytes unsigned int.

Referenced by sol_util_be32_to_cpu(), sol_util_cpu_to_be32(), sol_util_cpu_to_le32(), and sol_util_le32_to_cpu().

#define sol_util_uint64_bytes_swap (   val)
Value:
((uint64_t)((((val) & 0xff00000000000000ull) >> 56) \
| (((val) & 0x00ff000000000000ull) >> 40) | (((val) & 0x0000ff0000000000ull) >> 24) \
| (((val) & 0x000000ff00000000ull) >> 8) | (((val) & 0x00000000ff000000ull) << 8) \
| (((val) & 0x0000000000ff0000ull) << 24) | (((val) & 0x000000000000ff00ull) << 40) \
| (((val) & 0x00000000000000ffull) << 56)))

Swaps the bytes of a 32 bytes unsigned int.

Referenced by sol_util_be64_to_cpu(), sol_util_cpu_to_be64(), sol_util_cpu_to_le64(), and sol_util_le64_to_cpu().

#define SOL_UTIL_USEC_PER_SEC   1000000ULL

number of microseconds in a second: 1,000,000.

Referenced by sol_util_timespec_from_usec(), and sol_util_usec_from_timespec().

Function Documentation

static ssize_t sol_util_base16_calculate_decoded_len ( const struct sol_str_slice  slice)
inlinestatic

Calculate the size necessary to decode a given slice in base16.

Parameters
sliceThe slice that is wanted to know the decoded size.
Returns
the size that will be utilized to decode the slice or a negative number on error.

References sol_str_slice::len.

static ssize_t sol_util_base16_calculate_encoded_len ( const struct sol_str_slice  slice)
inlinestatic

Calculate the size necessary to encode a given slice in base16.

Parameters
sliceThe slice that is wanted to know the encoded size.
Returns
the size that will be utilized to encode the slice or a negative number on error.

References sol_str_slice::len, and sol_util_ssize_mul().

ssize_t sol_util_base16_decode ( void *  buf,
size_t  buflen,
const struct sol_str_slice  slice,
enum sol_decode_case  decode_case 
)

Decode the binary slice from base16 (hexadecimal).

Note
no trailing null '\0' is added!
Parameters
bufa buffer of size buflen that is big enough to hold the decoded string.
buflenthe number of bytes available in buffer. Must be large enough to contain the decoded slice, that is: slice.len / 2.
slicethe slice to decode, it must be a set of 0-9 or letters A-F (if uppercase) or a-f, otherwise decode fails.
decode_caseif SOL_DECODE_UPPERCASE, uppercase letters ABCDEF are used, if SOL_DECODE_LOWERCASE, lowercase abcdef are used instead. If SOL_DECODE_BOTH both, lowercase and uppercase, letters can be used.
Returns
the number of bytes written or -errno if failed.
Examples:
/src/samples/coap/lwm2m-bs-server.c, /src/samples/coap/lwm2m-client.c, and /src/samples/coap/lwm2m-server.c.

Referenced by main().

ssize_t sol_util_base16_encode ( void *  buf,
size_t  buflen,
const struct sol_str_slice  slice,
bool  uppercase 
)

Encode the binary slice to base16 (hexadecimal).

Note
no trailing null '\0' is added!
Parameters
bufa buffer of size buflen that is big enough to hold the encoded string.
buflenthe number of bytes available in buffer. Must be large enough to contain the encoded slice, that is: slice.len * 2
slicethe slice to encode, it may contain null-bytes (\0), the whole size of the slice will be used (slice.len).
uppercaseif true, uppercase letters ABCDEF are used, otherwise lowercase abcdef are used instead.
Returns
the number of bytes written or -errno if failed.
ssize_t sol_util_base64_calculate_decoded_len ( const struct sol_str_slice  slice,
const char  base64_map[SOL_STATIC_ARRAY_SIZE(65)] 
)

Calculate the size necessary to decode a given slice in base64.

Parameters
sliceThe slice that is wanted to know the decode size.
base64_mapthe map to use. The last char is used as the padding character if slice length is not multiple of 3 bytes.
Returns
the size that will be utilized to decode the slice or a negative number on error.
static ssize_t sol_util_base64_calculate_encoded_len ( const struct sol_str_slice  slice,
const char  base64_map[SOL_STATIC_ARRAY_SIZE(65)] 
)
inlinestatic

Calculate the size necessary to encode a given slice in base64.

Parameters
sliceThe slice that is wanted to know the encoded size.
base64_mapthe map to use. The last char is used as the padding character if slice length is not multiple of 3 bytes.
Returns
the size that will be utilized to encode the slice or a negative number on error.

References sol_str_slice::len, and sol_util_ssize_mul().

ssize_t sol_util_base64_decode ( void *  buf,
size_t  buflen,
const struct sol_str_slice  slice,
const char  base64_map[SOL_STATIC_ARRAY_SIZE(65)] 
)

Decode the binary slice from base64 using the given map.

https://en.wikipedia.org/wiki/Base64

Note
no trailing null '\0' is added!
Parameters
bufa buffer of size buflen that is big enough to hold the decoded string.
buflenthe number of bytes available in buffer. Must be large enough to contain the decoded slice, that is: (slice.len / 4) * 3
slicethe slice to decode, it must be composed solely of the base64_map characters or it will fail.
base64_mapthe map to use. The last char is used as the padding character if slice length is not multiple of 3 bytes.
Returns
the number of bytes written or -errno if failed.
ssize_t sol_util_base64_encode ( void *  buf,
size_t  buflen,
const struct sol_str_slice  slice,
const char  base64_map[SOL_STATIC_ARRAY_SIZE(65)] 
)

Encode the binary slice to base64 using the given map.

https://en.wikipedia.org/wiki/Base64

Note
no trailing null '\0' is added!
Parameters
bufa buffer of size buflen that is big enough to hold the encoded string.
buflenthe number of bytes available in buffer. Must be large enough to contain the encoded slice, that is: (slice.len / 3 + 1) * 4
slicethe slice to encode, it may contain null-bytes (\0), the whole size of the slice will be used (slice.len).
base64_mapthe map to use. The last char is used as the padding character if slice length is not multiple of 3 bytes.
Returns
the number of bytes written or -errno if failed.
static uint16_t sol_util_be16_to_cpu ( uint16_t  val)
inlinestatic

Convert a 16 bytes big endian integer to cpu endianness.

This function converts a integer of 16 bytes to little endian format, in case of the integer being in the cpu endiannesst nothing is done.

Parameters
valthe uint16_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_le16_to_cpu

References sol_util_uint16_bytes_swap.

Referenced by sol_network_link_addr_eq_full().

static uint32_t sol_util_be32_to_cpu ( uint32_t  val)
inlinestatic

Convert a 32 bytes big endian integer to cpu endianness.

This function converts a integer of 32 bytes to little endian format, in case of the integer being in the cpu endiannesst nothing is done.

Parameters
valthe uint32_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_le32_to_cpu

References sol_util_uint32_bytes_swap.

static uint64_t sol_util_be64_to_cpu ( uint64_t  val)
inlinestatic

Convert a 64 bytes big endian integer to cpu endianness.

This function converts a integer of 64 bytes to little endian format, in case of the integer being in the cpu endiannesst nothing is done.

Parameters
valthe uint64_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_le64_to_cpu

References sol_util_uint64_bytes_swap.

static void sol_util_clear_memory_secure ( void *  buf,
size_t  len 
)
inlinestatic

Clear an allocated memory securely.

Clobber memory pointed to by buf to prevent the optimizer from eliding the memset() call.

Parameters
bufThe memory block
lenThe buf length
static uint16_t sol_util_cpu_to_be16 ( uint16_t  val)
inlinestatic

Convert a 16 bytes integer to big endian format.

This function converts a integer of 16 bytes to big endian format, in case of the integer being in big endian format nothing is done.

Parameters
valthe uint16_t number to convert.
Returns
the given value on big endian format.
See Also
sol_util_cpu_to_le16

References sol_util_uint16_bytes_swap.

static uint32_t sol_util_cpu_to_be32 ( uint32_t  val)
inlinestatic

Convert a 32 bytes integer to big endian format.

This function converts a integer of 32 bytes to big endian format, in case of the integer being in big endian format nothing is done.

Parameters
valthe uint32_t number to convert.
Returns
the given value on big endian format.
See Also
sol_util_cpu_to_le32

References sol_util_uint32_bytes_swap.

static uint64_t sol_util_cpu_to_be64 ( uint64_t  val)
inlinestatic

Convert a 64 bytes integer to big endian format.

This function converts a integer of 64 bytes to big endian format, in case of the integer being in big endian format nothing is done.

Parameters
valthe uint64_t number to convert.
Returns
the given value on big endian format.
See Also
sol_util_cpu_to_le64

References sol_util_uint64_bytes_swap.

static uint16_t sol_util_cpu_to_le16 ( uint16_t  val)
inlinestatic

Convert a 16 bytes integer to little endian format.

This function converts a integer of 16 bytes to little endian format, in case of the integer being in little endian format nothing is done.

Parameters
valthe uint16_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_cpu_to_be16

References sol_util_uint16_bytes_swap.

static uint32_t sol_util_cpu_to_le32 ( uint32_t  val)
inlinestatic

Convert a 32 bytes integer to little endian format.

This function converts a integer of 32 bytes to little endian format, in case of the integer being in little endian format nothing is done.

Parameters
valthe uint32_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_cpu_to_be32

References sol_util_uint32_bytes_swap.

static uint64_t sol_util_cpu_to_le64 ( uint64_t  val)
inlinestatic

Convert a 64 bytes integer to little endian format.

This function converts a integer of 64 bytes to little endian format, in case of the integer being in little endian format nothing is done.

Parameters
valthe uint64_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_cpu_to_be64

References sol_util_uint64_bytes_swap.

bool sol_util_double_eq ( double  var0,
double  var1 
)

Checks var0 and var1 for equality.

It uses relative comparison to account for impressions caused by floating point arithmetics, so give preference to use this function instead of comparing the numbers directly.

Parameters
var0First argument
var1Second argument
Returns
true if both values are equal, false otherwise.
static int32_t sol_util_int32_clamp ( int32_t  start,
int32_t  end,
int32_t  value 
)
inlinestatic

Restricts a number between two other numbers.

Parameters
startMinimum value.
endMaximum value.
valueThe value to clamp.
Returns
value if living in the range imposed by the start and end, the lower value if initially lower than start, or the higher one if initially higher than end.

References value.

int sol_util_int32_mul ( int32_t  op1,
int32_t  op2,
int32_t *  out 
)

Multiply two values checking for overflow.

This function multiplies two variables of the type int32_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the result.
Returns
0 on success, error code (always negative) otherwise.
int sol_util_int64_mul ( int64_t  op1,
int64_t  op2,
int64_t *  out 
)

Multiply two values checking for overflow.

This function multiply two variables of the type int64_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the result.
Returns
0 on success, error code (always negative) otherwise.
See Also
sol_util_uint64_mul
static uint16_t sol_util_le16_to_cpu ( uint16_t  val)
inlinestatic

Convert a 16 bytes little endian integer to cpu endianness.

This function converts a integer of 16 bytes to little endian format, in case of the integer being in the cpu endianness nothing is done.

Parameters
valthe uint16_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_be16_to_cpu

References sol_util_uint16_bytes_swap.

static uint32_t sol_util_le32_to_cpu ( uint32_t  val)
inlinestatic

Convert a 32 bytes little endian integer to cpu endianness.

This function converts a integer of 32 bytes to little endian format, in case of the integer being in the cpu endianness nothing is done.

Parameters
valthe uint32_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_be32_to_cpu

References sol_util_uint32_bytes_swap.

static uint64_t sol_util_le64_to_cpu ( uint64_t  val)
inlinestatic

Convert a 64 bytes little endian integer to cpu endianness.

This function converts a integer of 64 bytes to little endian format, in case of the integer being in the cpu endianness nothing is done.

Parameters
valthe uint64_t number to convert.
Returns
the given value on little endian format.
See Also
sol_util_be64_to_cpu

References sol_util_uint64_bytes_swap.

void* sol_util_memdup ( const void *  data,
size_t  len 
)

Duplicate memory.

It's a helper function to allocated memory and copy data, returning the pointer to it.

Parameters
dataPointer to the memory to be copied
lenSize in bytes of the memory to be allocated
Returns
A valid pointer to the allocated memory, with copied data or NULL if it fails to allocate it. The pointer need to be freed after usage.
static int sol_util_msec_from_timespec ( const struct timespec *  ts)
inlinestatic

Gets the number of milliseconds for given time.

Parameters
tsThe struct timespec to get the milliseconds.
Returns
the number of milliseconds on ts.

References SOL_UTIL_MSEC_PER_SEC, and SOL_UTIL_NSEC_PER_MSEC.

int sol_util_replace_str_from_slice_if_changed ( char **  str,
const struct sol_str_slice  slice 
)

Replace the string's contents.

This function takes a string and replace its contents if different from the new given slice, otherwise it lets the string intact.

Note
If the string is replaced its memory is properly released.
Parameters
strThe pointer for string which will be changed.
sliceThe slice with string
Returns
1 if changed, 0 if unchanged, error code (always negative) otherwise.
See Also
util_replace_str_from_slice_if_changed
int sol_util_replace_str_if_changed ( char **  str,
const char *  new_str 
)

Replace the string's contents.

This function takes a string and replace its contents if different from the new given string, otherwise it lets the string intact.

Note
If the string is replaced its memory is properly released.
Parameters
strThe pointer for string which will be changed.
new_strThe new string
Returns
1 if changed, 0 if unchanged, error code (always negative) otherwise.
See Also
util_replace_str_from_slice_if_changed
int sol_util_size_add ( size_t  op1,
size_t  op2,
size_t *  out 
)

Add two values checking for overflow.

This function adds two variables of the type size_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the result.
Returns
0 on success, error code (always negative) otherwise.
See Also
sol_util_size_sub
int sol_util_size_mul ( size_t  op1,
size_t  op2,
size_t *  out 
)

Multiply two values checking for overflow.

This function multiply two variables of the type size_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the time.
Returns
0 on success, error code (always negative) otherwise.
See Also
sol_util_ssize_mul

Referenced by _sol_glib_integration_source_prepare().

int sol_util_size_sub ( size_t  op1,
size_t  op2,
size_t *  out 
)

Subtract two values checking for overflow.

This function subtracts two variables of the type size_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the result.
Returns
0 on success, error code (always negative) otherwise.
See Also
sol_util_size_add
int sol_util_ssize_mul ( ssize_t  op1,
ssize_t  op2,
ssize_t *  out 
)

Multiply two values checking for overflow.

This function multiply two variables of the type ssize_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the result.
Returns
0 on success, error code (always negative) otherwise.
See Also
sol_util_size_mul

Referenced by sol_util_base16_calculate_encoded_len(), and sol_util_base64_calculate_encoded_len().

char* sol_util_strerror ( int  errnum,
struct sol_buffer buf 
)

Gets a string from a given error.

The function returns a pointer to a string that describes the error code passed in the argument errnum.

Parameters
errnumThe error code
bufBuffer used to append error the string - It must be already initialized.
Returns
return the appropriate error description string.
See Also
sol_util_strerrora
ssize_t sol_util_strftime ( struct sol_buffer buf,
const char *  format,
const struct tm *  timeptr,
bool  use_locale 
)

Wrapper around strftime()/strftime_l()

This is a simple wrapper around strftime()/strftime_l() functions. The use_locale parameter is only considered if strftime_l() and newlocale() are available, otherwise this wrapper will fallback to strftime() - Thus current system's locale will be considered when formatting the time.

Parameters
bufThe buffer to append the formatted time to - It must be already initialzed.
formatThe date format - check strftime man page for accepted formats.
timeptrThe broken down time struct.
use_localetrue to use current system locale or false to do not use system's locale.
Returns
The number of bytes written, negative errno or 0 on error. (see strftime man page).
double sol_util_strtod_n ( const char *  nptr,
char **  endptr,
ssize_t  len,
bool  use_locale 
)

Wrapper over strtod() that consumes up to len bytes and may not use a locale.

This variation of strtod() will work with buffers that are not null-terminated.

It also offers a way to skip the currently set locale, forcing plain "C". This is required to parse numbers in formats that require '.' as the decimal point while the current locale may use ',' such as in pt_BR.

All the formats accepted by strtod() are accepted and the behavior should be the same, including using information from LC_NUMERIC if locale is configured and use_locale is true.

Parameters
nptrthe string containing the number to convert.
endptrif non-NULL, it will contain the last character used in the conversion. If no conversion was done, endptr is nptr.
use_localeif true, then current locale is used, if false then "C" locale is forced.
lenuse at most this amount of bytes of nptr. If -1, assumes nptr has a trailing NUL and calculate the string length.
Returns
the converted value, if any. The converted value may be NAN, INF (positive or negative). See the strtod(3) documentation for the details.
Examples:
/src/samples/coap/lwm2m-client.c.

Referenced by create_location_obj().

long int sol_util_strtol_n ( const char *  nptr,
char **  endptr,
ssize_t  len,
int  base 
)

Wrapper over strtol() that consumes up to len bytes.

This variation of strtol() will work with buffers that are not null-terminated.

All the formats accepted by strtol() are accepted and the behavior should be the same.

Parameters
nptrthe string containing the number to convert.
endptrif non-NULL, it will contain the last character used in the conversion. If no conversion was done, endptr is nptr.
lenuse at most this amount of bytes of nptr. If -1, assumes nptr has a trailing NUL and calculate the string length.
baseit's the base of conversion, which must be between 2 and 36 inclusive, or be the special value 0. A zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).
Returns
the converted value, if any.
unsigned long int sol_util_strtoul_n ( const char *  nptr,
char **  endptr,
ssize_t  len,
int  base 
)

Wrapper over strtoul() that consumes up to len bytes.

This variation of strtoul() will work with buffers that are not null-terminated.

All the formats accepted by strtoul() are accepted and the behavior should be the same.

Parameters
nptrthe string containing the number to convert.
endptrif non-NULL, it will contain the last character used in the conversion. If no conversion was done, endptr is nptr.
lenuse at most this amount of bytes of nptr. If -1, assumes nptr has a trailing NUL and calculate the string length.
baseit's the base of conversion, which must be between 2 and 36 inclusive, or be the special value 0. A zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).
Returns
the converted value, if any.
static void sol_util_timespec_add ( const struct timespec *  t1,
const struct timespec *  t2,
struct timespec *  result 
)
inlinestatic

Sum two time values.

Parameters
t1First time value used on operation.
t2Second time value used on operation.
resultVariable used to store the sum's result.

References SOL_UTIL_NSEC_PER_SEC.

static int sol_util_timespec_compare ( const struct timespec *  t1,
const struct timespec *  t2 
)
inlinestatic

Compare two time values.

Function to compare two times. It returns an integer less than, equal to, or greater than zero.

Parameters
t1First time value used on operation.
t2Second time value used on operation.
Returns
0 if equal, -1 if t2 is greater or 1 otherwise.
static struct timespec sol_util_timespec_from_msec ( int  msec)
static

Create a struct timespec from milliseconds.

Parameters
msecThe number of milliseconds.
Returns
a struct timespec representing msec milliseconds.

References SOL_UTIL_MSEC_PER_SEC, and SOL_UTIL_NSEC_PER_MSEC.

Referenced by _sol_glib_integration_source_get_next_timeout().

static struct timespec sol_util_timespec_from_usec ( int  usec)
static

Create a struct timespec from microseconds.

Parameters
usecThe number of microseconds.
Returns
a struct timespec representing usec microseconds.

References SOL_UTIL_NSEC_PER_USEC, and SOL_UTIL_USEC_PER_SEC.

struct timespec sol_util_timespec_get_current ( void  )

Gets the current time (Monotonic).

Returns
The current time represented in struct timespec.
Examples:
/src/samples/crypto/message-digest.c.

Referenced by print_time(), and startup().

int sol_util_timespec_get_realtime ( struct timespec *  t)

Gets the current time (System-wide clock).

Parameters
tVariable used to store the time.
Returns
0 for success, or -1 for failure (in which case errno is set appropriately).
static void sol_util_timespec_sub ( const struct timespec *  t1,
const struct timespec *  t2,
struct timespec *  result 
)
inlinestatic

Subtracts two time values.

Parameters
t1First time value used on operation.
t2Second time value used on operation.
resultVariable used to store the subtraction's result.
Examples:
/src/samples/crypto/message-digest.c.

References SOL_UTIL_NSEC_PER_SEC.

Referenced by print_time().

int sol_util_uint32_mul ( uint32_t  op1,
uint32_t  op2,
uint32_t *  out 
)

Multiply two values checking for overflow.

This function multiplies two variables of the type int64_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the result.
Returns
0 on success, error code (always negative) otherwise.
int sol_util_uint64_add ( uint64_t  op1,
uint64_t  op2,
uint64_t *  out 
)

Add two values checking for overflow.

This function add two variables of the type uint64_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the result.
Returns
0 on success, error code (always negative) otherwise.
See Also
sol_util_size_add
int sol_util_uint64_mul ( uint64_t  op1,
uint64_t  op2,
uint64_t *  out 
)

Multiply two values checking for overflow.

This function multiply two unsigned variables of the type uint64_t and check if this operation causes an overflow.

Parameters
op1First operation's operator.
op2Second operation's operator.
outVariable used to store the result.
Returns
0 on success, error code (always negative) otherwise.
See Also
sol_util_int64_mul
int sol_util_unescape_quotes ( const struct sol_str_slice  slice,
struct sol_buffer buf 
)

Unescape a string removing quotes from it.

This function will unescape single quotes (\') and double quotes (") from the slice, it will also remove single and double quotes from the string if they are not escaped. Trying to unescape a character that is not a single quote or double quote it is also considered an error.

Parameters
sliceThe slice to be escaped
buf- The buffer to hold the unescaped string - It will be initialized by this function.
Returns
0 on success, negative errno otherwise.
int32_t sol_util_unicode_code_from_utf8 ( const uint8_t *  buf,
size_t  buf_len,
uint8_t *  bytes_read 
)

Convert a utf-8 character to unicode code.

Parameters
bufBuffer with the utf-8 representation of the unicode character.
buf_lenThe buffer length.
bytes_readOptional pointer to variable to write number of bytes read from buf.
Returns
The code from unicode table of the character in 'buf' or a negative number on error.
static int sol_util_usec_from_timespec ( const struct timespec *  ts)
inlinestatic

Gets the number of microseconds for given time.

Parameters
tsThe struct timespec to get the microseconds.
Returns
the number of microseconds on ts.

References SOL_UTIL_NSEC_PER_USEC, and SOL_UTIL_USEC_PER_SEC.

int8_t sol_util_utf8_from_unicode_code ( uint8_t *  buf,
size_t  buf_len,
uint32_t  unicode_code 
)

Convert from unicode code to utf-8 string.

Write at string buf the bytes needed to represent the unicode charater informed as utf-8. One to four characters will be written on success. No character is written on error.

Note
no trailing null '\0' is added!
Parameters
bufBuffer to write the utf-8 representation of the unicode character
buf_lenThe buffer length
unicode_codeCode from unicode table of the character to be converted
Returns
The number of bytes written in 'buf' or a negative number on error.
int sol_util_uuid_bytes_from_string ( struct sol_str_slice  uuid_str,
struct sol_buffer uuid_bytes 
)

Convert a UUID in string format to a byte array with UUID bytes.

Parameters
uuid_strThe UUID in string format, with or without hyphens, using lowercase or uppercase characters.
uuid_bytesan initialized buffer to be used to append the converted uuid, totalizing 16 bytes.
Returns
0 on success, negative error code otherwise.
int sol_util_uuid_gen ( bool  uppercase,
bool  with_hyphens,
struct sol_buffer uuid_buf 
)

Generates a new universally unique identifier (UUID) string.

The generated string is 16 bytes-long (128 bits) long and conforms to v4 UUIDs (generated from random—or pseudo-random—numbers).

Parameters
uppercaseWhether to generate the UUID in uppercase or not
with_hyphensFormat the resulting UUID string with hyphens (e.g. "de305d54-75b4-431b-adb2-eb6b9e546014") or without them.
uuid_bufAn initialized buffer to be used to append the generated id. It will have 36 bytes of length if with_hyphens is true or 32 bytes of length if with_hyphens is false.
Returns
0 on success, negative error code otherwise.
Examples:
/src/samples/common/uart.c.

Referenced by producer_make_data().

bool sol_util_uuid_str_is_valid ( const struct sol_str_slice  uuid)

Checks if a given universally unique identifier (UUID), in string form, is valid.

All upcase/downcase, hyphenated/non-hyphenated cases are included.

Parameters
uuidThe given UUID formatted in a string, with or without hyphens.
Returns
true if it's valid, false otherwise.
int sol_util_uuid_string_from_bytes ( bool  uppercase,
bool  with_hyphens,
const uint8_t  uuid_bytes[SOL_STATIC_ARRAY_SIZE(16)],
struct sol_buffer uuid_str 
)

Convert a UUID in byte format to UUID string format.

Parameters
uppercaseWhether to create the UUID string in uppercase or not
with_hyphensFormat the resulting UUID string with hyphens (e.g. "de305d54-75b4-431b-adb2-eb6b9e546014") or without them.
uuid_bytesA 16 byte array containing the UUID in byte format.
uuid_strn initialized buffer to be used to append the converted uuid. It will have 36 bytes of length if with_hyphens is true or 32 bytes of length if with_hyphens is false.
Returns
0 on success, negative error code otherwise.