MegaWiFi 1.5
MegaWiFi API documentation
util.h
Go to the documentation of this file.
1/************************************************************************/
12#ifndef _UTIL_H_
13#define _UTIL_H_
14
15#include <stdint.h>
16#include <stddef.h>
17
18#ifndef TRUE
20#define TRUE 1
21#endif
22#ifndef FALSE
24#define FALSE 0
25#endif
26
27#ifndef NULL
29#define NULL ((void*)0)
30#endif
31
33#define _STR(item) #item
35#define STR(item) _STR(item)
36
38#define IN_RANGE(num, lower, upper) \
39 (((number) >= (lower)) && ((number) <= (upper)))
40
42#define container_of(ptr, type, member) ({ \
43 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
44 (type *)( (char *)__mptr - offsetof(type,member) );})
45
47#define PACKED __attribute__((__packed__))
48
52#define SECTION(name) __attribute__((section(#name)))
53
55#define ROM_TEXT(name) SECTION(.text.ro_text.name)
57#define ROM_DATA(name) SECTION(.text.ro_data.name)
58
60#define ARRAY_ROWS(array_2d) (sizeof(array_2d) / sizeof(array_2d[0]) / sizeof(array_2d[0][0]))
62#define ARRAY_COLS(array_2d) (sizeof(array_2d[0]) / sizeof(array_2d[0][0]))
63
65#define UNUSED_PARAM(x) (void)x
66
67#if !defined(MAX)
69#define MAX(a, b) ((a)>(b)?(a):(b))
70#endif
71#if !defined(MIN)
73#define MIN(a, b) ((a)<(b)?(a):(b))
74#endif
75
77#define ByteSwapWord(w) (uint16_t)((((uint16_t)(w))>>8) | (((uint16_t)(w))<<8))
78
80#define ByteSwapDWord(dw) (uint32_t)((((uint32_t)(dw))>>24) | \
81 ((((uint32_t)(dw))>>8) & 0xFF00) | ((((uint32_t)(dw)) & 0xFF00)<<8) | \
82 (((uint32_t)(dw))<<24))
83
84/************************************************************************/
89static inline void to_upper(char *str) {
90 uint16_t i;
91 for (i = 0; str[i] != '\0'; i++) {
92 if ((str[i] >= 'a') && (str[i] <= 'z'))
93 str[i] -= 'a' - 'A';
94 }
95}
96
97/************************************************************************/
107const char *str_is_uint8(const char *str);
108
109/************************************************************************/
118int ip_validate(const char *str);
119
120/************************************************************************/
129int uint32_to_ip_str(uint32_t ip_u32, char *ip_str);
130
131/************************************************************************/
139uint32_t ip_str_to_uint32(const char *ip);
140
141/************************************************************************/
151uint16_t uint8_to_str(uint8_t num, char *str);
152
153/************************************************************************/
163int8_t int8_to_str(int8_t num, char *str);
164
165/************************************************************************/
175uint8_t uint16_to_str(uint16_t num, char *str);
176
177/************************************************************************/
187uint8_t int16_to_str(int16_t num, char *str);
188
189/************************************************************************/
200const char *str_to_uint8(const char *strIn, uint8_t *result);
201
202/************************************************************************/
218int long_to_str(long num, char *str, int buf_len, int pad_len, char pad_chr);
219
220/************************************************************************/
227void uint8_to_hex_str(uint8_t num, char *str);
228
229/************************************************************************/
241int uint32_to_hex_str(uint32_t num, char *str, int pad);
242
243/************************************************************************/
255int version_to_str(const uint8_t version[3], char *str);
256
257/************************************************************************/
268int version_cmp(const uint8_t old[3], const uint8_t cur[3]);
269
270/************************************************************************/
281uint16_t concat_strings(const char **str, uint8_t num_strs, char *output,
282 uint16_t max_len);
283
284/************************************************************************/
296uint16_t concat_kv_pairs(const char **key, const char **value,
297 uint8_t num_pairs, char *output, uint16_t max_len);
298
299#ifndef TRUE
301#define TRUE 1
303#define TRUE 1
304#define FALSE 0
305#endif
306
307#endif //_UTIL_H_
308
uint8_t uint16_to_str(uint16_t num, char *str)
Converts an unsigned 16-bit number to its character string representation.
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.
int long_to_str(long num, char *str, int buf_len, int pad_len, char pad_chr)
Converts an integer to a character string.
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.
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...
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) representa...
int8_t int8_to_str(int8_t num, char *str)
Converts an signed 8-bit number to its character string representation.
int version_to_str(const uint8_t version[3], char *str)
Converts version numbers (major, minor, micro) into its string representation [major]....
int ip_validate(const char *str)
This function evaluates the data entered on the input Menu structure, to guess if it corresponds to a...
int version_cmp(const uint8_t old[3], const uint8_t cur[3])
Compares two version number arrays (major, minor, micro).
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.
static void to_upper(char *str)
Converts input string to uppercase.
Definition: util.h:89
uint8_t int16_to_str(int16_t num, char *str)
Converts an signed 16-bit number to its character string representation.
uint16_t uint8_to_str(uint8_t num, char *str)
Converts an unsigned 8-bit number to its character string representation.
int uint32_to_hex_str(uint32_t num, char *str, int pad)
Converts a 32-bit number to its hexadecimal string representation.
uint32_t ip_str_to_uint32(const char *ip)
Returns the binary IP addres (uint32) corresponding to the input string.
void uint8_to_hex_str(uint8_t num, char *str)
Converts a 8-bit number to its hexadecimal string representation.