MegaWiFi 1.5
MegaWiFi API documentation
util

General purpose utilities. More...

Macros

#define TRUE   1
 TRUE value for boolean comparisons. More...
 
#define FALSE   0
 TRUE value for boolean comparisons. More...
 
#define NULL   ((void*)0)
 NULL Pointer. More...
 
#define _STR(item)   #item
 Helper for STR macro, to ensure item is expanded. More...
 
#define STR(item)   _STR(item)
 Converts a statically defined item to string. More...
 
#define IN_RANGE(num, lower, upper)    (((number) >= (lower)) && ((number) <= (upper)))
 Returns TRUE if number is in the specified range. More...
 
#define container_of(ptr, type, member)
 The infamous container_of() macro directly the Linux kernel. More...
 
#define PACKED   __attribute__((__packed__))
 Macro for packing structures and enumerates. More...
 
#define SECTION(name)   __attribute__((section(#name)))
 
#define ROM_TEXT(name)   SECTION(.text.ro_text.name)
 Put next symbol in named section .text.ro_text.symbol. More...
 
#define ROM_DATA(name)   SECTION(.text.ro_data.name)
 Put next symbol in named section .text.ro_data.symbol. More...
 
#define ARRAY_ROWS(array_2d)   (sizeof(array_2d) / sizeof(array_2d[0]) / sizeof(array_2d[0][0]))
 Get number of rows of a 2D array. More...
 
#define ARRAY_COLS(array_2d)   (sizeof(array_2d[0]) / sizeof(array_2d[0][0]))
 Get number of columns of a 2D array. More...
 
#define UNUSED_PARAM(x)   (void)x
 Remove compiler warnings when not using a function parameter. More...
 
#define MAX(a, b)   ((a)>(b)?(a):(b))
 Returns the maximum of two numbers. More...
 
#define MIN(a, b)   ((a)<(b)?(a):(b))
 Returns the minimum of two numbers. More...
 
#define ByteSwapWord(w)   (uint16_t)((((uint16_t)(w))>>8) | (((uint16_t)(w))<<8))
 Swaps bytes from a word (16 bit) More...
 
#define ByteSwapDWord(dw)
 Swaps bytes from a dword (32 bits) More...
 

Functions

static void to_upper (char *str)
 Converts input string to uppercase. More...
 
const char * str_is_uint8 (const char *str)
 Evaluates if a string points to a number that can be stored in a uint8_t type variable. More...
 
int ip_validate (const char *str)
 This function evaluates the data entered on the input Menu structure, to guess if it corresponds to a valid IPv4. More...
 
int uint32_to_ip_str (uint32_t ip_u32, char *ip_str)
 Writes the corresponding string representing an IPv4 stored in the input DWORD (32-bit) integer. More...
 
uint32_t ip_str_to_uint32 (const char *ip)
 Returns the binary IP addres (uint32) corresponding to the input string. More...
 
uint16_t uint8_to_str (uint8_t num, char *str)
 Converts an unsigned 8-bit number to its character string representation. More...
 
int8_t int8_to_str (int8_t num, char *str)
 Converts an signed 8-bit number to its character string representation. More...
 
uint8_t uint16_to_str (uint16_t num, char *str)
 Converts an unsigned 16-bit number to its character string representation. More...
 
uint8_t int16_to_str (int16_t num, char *str)
 Converts an signed 16-bit number to its character string representation. More...
 
const char * str_to_uint8 (const char *strIn, uint8_t *result)
 Converts a character string representing an 8-bit unsigned number, to its binary (uint8_t) representation. More...
 
int long_to_str (long num, char *str, int buf_len, int pad_len, char pad_chr)
 Converts an integer to a character string. More...
 
void uint8_to_hex_str (uint8_t num, char *str)
 Converts a 8-bit number to its hexadecimal string representation. More...
 
int uint32_to_hex_str (uint32_t num, char *str, int pad)
 Converts a 32-bit number to its hexadecimal string representation. More...
 
int version_to_str (const uint8_t version[3], char *str)
 Converts version numbers (major, minor, micro) into its string representation [major].[minor].[micro]. More...
 
int version_cmp (const uint8_t old[3], const uint8_t cur[3])
 Compares two version number arrays (major, minor, micro). More...
 
uint16_t concat_strings (const char **str, uint8_t num_strs, char *output, uint16_t max_len)
 Concatenates an array of strings into an output string, separated by a '\0' character. More...
 
uint16_t concat_kv_pairs (const char **key, const char **value, uint8_t num_pairs, char *output, uint16_t max_len)
 Concatenates two arrays of key/value pairs, into an output string, separating each key and value with a '\0' character. More...
 

Detailed Description

General purpose utilities.

Macro Definition Documentation

◆ _STR

#define _STR (   item)    #item

Helper for STR macro, to ensure item is expanded.

Definition at line 33 of file util.h.

◆ ARRAY_COLS

#define ARRAY_COLS (   array_2d)    (sizeof(array_2d[0]) / sizeof(array_2d[0][0]))

Get number of columns of a 2D array.

Definition at line 62 of file util.h.

◆ ARRAY_ROWS

#define ARRAY_ROWS (   array_2d)    (sizeof(array_2d) / sizeof(array_2d[0]) / sizeof(array_2d[0][0]))

Get number of rows of a 2D array.

Definition at line 60 of file util.h.

◆ ByteSwapDWord

#define ByteSwapDWord (   dw)
Value:
(uint32_t)((((uint32_t)(dw))>>24) | \
((((uint32_t)(dw))>>8) & 0xFF00) | ((((uint32_t)(dw)) & 0xFF00)<<8) | \
(((uint32_t)(dw))<<24))

Swaps bytes from a dword (32 bits)

Definition at line 80 of file util.h.

◆ ByteSwapWord

#define ByteSwapWord (   w)    (uint16_t)((((uint16_t)(w))>>8) | (((uint16_t)(w))<<8))

Swaps bytes from a word (16 bit)

Definition at line 77 of file util.h.

◆ container_of

#define container_of (   ptr,
  type,
  member 
)
Value:
({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

The infamous container_of() macro directly the Linux kernel.

Definition at line 42 of file util.h.

◆ FALSE

#define FALSE   0

TRUE value for boolean comparisons.

Definition at line 24 of file util.h.

◆ IN_RANGE

#define IN_RANGE (   num,
  lower,
  upper 
)     (((number) >= (lower)) && ((number) <= (upper)))

Returns TRUE if number is in the specified range.

Definition at line 38 of file util.h.

◆ MAX

#define MAX (   a,
 
)    ((a)>(b)?(a):(b))

Returns the maximum of two numbers.

Definition at line 69 of file util.h.

◆ MIN

#define MIN (   a,
 
)    ((a)<(b)?(a):(b))

Returns the minimum of two numbers.

Definition at line 73 of file util.h.

◆ NULL

#define NULL   ((void*)0)

NULL Pointer.

Definition at line 29 of file util.h.

◆ PACKED

#define PACKED   __attribute__((__packed__))

Macro for packing structures and enumerates.

Definition at line 47 of file util.h.

◆ ROM_DATA

#define ROM_DATA (   name)    SECTION(.text.ro_data.name)

Put next symbol in named section .text.ro_data.symbol.

Definition at line 57 of file util.h.

◆ ROM_TEXT

#define ROM_TEXT (   name)    SECTION(.text.ro_text.name)

Put next symbol in named section .text.ro_text.symbol.

Definition at line 55 of file util.h.

◆ SECTION

#define SECTION (   name)    __attribute__((section(#name)))

Section attribute definition for variables and functions. Examples:

Definition at line 52 of file util.h.

◆ STR

#define STR (   item)    _STR(item)

Converts a statically defined item to string.

Definition at line 35 of file util.h.

◆ TRUE

#define TRUE   1

TRUE value for boolean comparisons.

Definition at line 20 of file util.h.

◆ UNUSED_PARAM

#define UNUSED_PARAM (   x)    (void)x

Remove compiler warnings when not using a function parameter.

Definition at line 65 of file util.h.

Function Documentation

◆ concat_kv_pairs()

uint16_t concat_kv_pairs ( const char **  key,
const char **  value,
uint8_t  num_pairs,
char *  output,
uint16_t  max_len 
)

Concatenates two arrays of key/value pairs, into an output string, separating each key and value with a '\0' character.

Parameters
[in]keyArray of keys to concatenate into output.
[in]valueArray of values to concatenate into output.
[in]num_pairsNumber of key/value pairs in key and value arrays.
[out]outputOutput array that will hold concatenated strings.
[in]max_lenLength of output. Function will error if not enough.
Returns
The length of concatenated output key/val strings, or 0 on error.

◆ concat_strings()

uint16_t concat_strings ( const char **  str,
uint8_t  num_strs,
char *  output,
uint16_t  max_len 
)

Concatenates an array of strings into an output string, separated by a '\0' character.

Parameters
[in]strArray of strings to concatenate into output.
[in]num_strsNumber of strings in str array.
[out]outputOutput array that will hold concatenated strings.
[in]max_lenLength of output. Function will error if not enough.
Returns
The length of concatenated output strings, or 0 on error.

◆ int16_to_str()

uint8_t int16_to_str ( int16_t  num,
char *  str 
)

Converts an signed 16-bit number to its character string representation.

Parameters
[in]numInput number to convert.
[out]strString representing the input number.
Returns
Resulting str length (not including null termination).
Note
str buffer length shall be at least 7 bytes.

◆ int8_to_str()

int8_t int8_to_str ( int8_t  num,
char *  str 
)

Converts an signed 8-bit number to its character string representation.

Parameters
[in]numInput number to convert.
[out]strString representing the input number.
Returns
Resulting str length (not including null termination).
Note
str buffer length shall be at least 5 bytes.

◆ ip_str_to_uint32()

uint32_t ip_str_to_uint32 ( const char *  ip)

Returns the binary IP addres (uint32) corresponding to the input string.

Parameters
[in]ipString representing an IPv4 address.
Returns
Binary representation of the input IPv4 string.

◆ ip_validate()

int ip_validate ( const char *  str)

This function evaluates the data entered on the input Menu structure, to guess if it corresponds to a valid IPv4.

Parameters
[in]strstring to evaluate against an IPv4 pattern.
Returns
TRUE if the evaluated string corresponds to a valid IPv4. False otherwise.

◆ long_to_str()

int long_to_str ( long  num,
char *  str,
int  buf_len,
int  pad_len,
char  pad_chr 
)

Converts an integer to a character string.

Parameters
[in]numNumber to convert to string.
[out]strString that will hold the converted number.
[in]buf_lenLength of str buffer.
[in]pad_lenLength of the padding to introduce. 0 for no padding.
[in]pad_chrCharacter used for padding (typically '0' or ' ').
Returns
Number of characters written to str buffer, not including the null termination. 0 if string does not fin in the buffer and has not been converted.
Warning
Function uses lots of divisions. Maybe it is not the best of the ideas using it in a game loop.

◆ str_is_uint8()

const char * str_is_uint8 ( const char *  str)

Evaluates if a string points to a number that can be stored in a uint8_t type variable.

Parameters
[in]strString to be evaluated as a number.
Returns
The pointer to the character following the last digit of the number, if the string represents a number fittint in a uint_8. NULL if the string does not represent an uint8_t number.

◆ str_to_uint8()

const char * str_to_uint8 ( const char *  strIn,
uint8_t *  result 
)

Converts a character string representing an 8-bit unsigned number, to its binary (uint8_t) representation.

Parameters
[in]strInInput string with the number to convert.
[out]resultConverted number will be left here.
Returns
Pointer to the end of the number received in strIn parameter, or NULL if the strIn does not contain a valid string representation of an uint8_t type.

◆ to_upper()

static void to_upper ( char *  str)
inlinestatic

Converts input string to uppercase.

Parameters
[in,out]strString to be converted to uppercase.

Definition at line 89 of file util.h.

◆ uint16_to_str()

uint8_t uint16_to_str ( uint16_t  num,
char *  str 
)

Converts an unsigned 16-bit number to its character string representation.

Parameters
[in]numInput number to convert.
[out]strString representing the input number.
Returns
Resulting str length (not including null termination).
Note
str buffer length shall be at least 6 bytes.

◆ uint32_to_hex_str()

int uint32_to_hex_str ( uint32_t  num,
char *  str,
int  pad 
)

Converts a 32-bit number to its hexadecimal string representation.

Parameters
[in]numNumber to convert.
[out]strConverted equivalent string. Must have room for at least 9 characters to guarantee an overrun will not accur.
[in]padPadding. If greater than 0, left part of resulting number will be zero-padded to the specified length.
Returns
Number of characters of the resulting converted string, not including the null termination.

◆ uint32_to_ip_str()

int uint32_to_ip_str ( uint32_t  ip_u32,
char *  ip_str 
)

Writes the corresponding string representing an IPv4 stored in the input DWORD (32-bit) integer.

Parameters
[in]ip_u32Input DWORD to translate into string.
[out]ip_strResulting string matching the input DWORD.
Returns
Length of the resulting IP string.

◆ uint8_to_hex_str()

void uint8_to_hex_str ( uint8_t  num,
char *  str 
)

Converts a 8-bit number to its hexadecimal string representation.

Parameters
[in]numNumber to convert.
[out]strConverted equivalent string. Must have room for at least 3 characters to guarantee an overrun will not accur.

◆ uint8_to_str()

uint16_t uint8_to_str ( uint8_t  num,
char *  str 
)

Converts an unsigned 8-bit number to its character string representation.

Parameters
[in]numInput number to convert.
[out]strString representing the input number.
Returns
Resulting str length (not including null termination).
Note
str buffer length shall be at least 4 bytes.

◆ version_cmp()

int version_cmp ( const uint8_t  old[3],
const uint8_t  cur[3] 
)

Compares two version number arrays (major, minor, micro).

Parameters
[in]oldOld version numbers
[in]curNew version numbers against to compare
Returns
  • > 0 if cur > old
  • < 0 if cur < old
  • 0 if cur == old

◆ version_to_str()

int version_to_str ( const uint8_t  version[3],
char *  str 
)

Converts version numbers (major, minor, micro) into its string representation [major].[minor].[micro].

Parameters
[in]versionVersion numbers
[out]strConverted version string from version numbers.
Returns
The number of characters used for the string representation of the version number (excluding the null termination).
Note
str must be at least 12 characters long to guarantee the hightest allowed version numbert (255.255.255) can be represented.