Newer
Older
#ifndef EASYUTILS_H
#define EASYUTILS_H
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/** Convert hex string to byte array
*
* For example:
* "001122334455" --> {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
*
* @param[in] str hex string.
* @param[in] len string length of the hex string.
* @param[out] bytes output buffer to write the converted hex string.
* @return byte array of the converted hex string.
*/
extern uint8_t *strtob(char *str, int len, uint8_t *bytes);
/** Convert byte array to hex string
*
* For example:
* {0x00, 0x11, 0x22, 0x33, 0x44, 0x55} --> "001122334455"
*
* @param[in] bytes byte array.
* @param[in] len length of the byte array.
* @param[out] str output buffer to write the hex string.
* @return hex string.
*/
extern char *btostr(uint8_t *bytes, int len, char *str);
/** Convert macaddress from ':' separated string to byte array
*
* For example:
* "00:11:22:33:44:55" --> {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
*
* @param[in] macstr mac address string.
* @param[out] mac output buffer to write the converted macstr.
* @return output byte array of the converted macstr.
*/
extern uint8_t *hwaddr_aton(const char *macstr, uint8_t *mac);
/** Convert macaddress from byte array to ':' separated string
*
* For example:
* {0x00, 0x11, 0x22, 0x33, 0x44, 0x55} --> "00:11:22:33:44:55"
*
* @param[in] mac macaddress byte array.
* @param[out] macstr mac address string in ':' sepearted format.
* @return output macaddress string in ':' separated format.
*/
extern char *hwaddr_ntoa(const uint8_t *mac, char *macstr);
static inline int hwaddr_equal(const uint8_t *a, const uint8_t *b)
return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])
| (a[3] ^ b[3]) | (a[4] ^ b[4]) | (a[5] ^ b[5])) == 0;
static inline bool hwaddr_is_zero(const uint8_t *a)
{
return (a[0] | a[1] | a[2] | a[3] | a[4] | a[5]) == 0;
}
static inline bool hwaddr_is_bcast(const uint8_t *a)
{
return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
}
#define MACFMT "%02x:%02x:%02x:%02x:%02x:%02x"
#define MAC2STR(_m) (_m)[0], (_m)[1], (_m)[2], (_m)[3], (_m)[4], (_m)[5]
/** struct ipaddress */
struct ipaddress {
uint8_t ver;
union {
struct {
uint8_t rsvd[12];
uint8_t addr[4];
} v4;
struct {
uint8_t addr[16];
} v6;
} ip;
};
typedef struct ipaddress ipaddress_t;
/** IFF_* device flags */
typedef unsigned int ifstatus_t;
/** IF_OPER_* operstate flags */
typedef unsigned char ifopstatus_t;
extern int get_ifstatus(const char *ifname, ifstatus_t *f);
extern int get_ifoperstatus(const char *ifname, ifopstatus_t *opstatus);
#define UNUSED(var) do { (void)(var); } while(0)
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif
#ifndef offsetof
#define offsetof(type, member) ((size_t) &((type *)0)->member)
#endif
#ifndef bit
#define bit(_n) (1 << (_n))
#endif
#ifndef min
#define min(x, y) (x) < (y) ? (x) : (y)
#endif
#ifndef max
#define max(x, y) (x) < (y) ? (y) : (x)
#endif
/* install a signal handler */
extern int set_sighandler(int sig, void (*handler)(int));
/* uninstall a signal handler */
extern int unset_sighandler(int sig);
extern char *trim(char *str);
extern void remove_newline(char *buf);
extern void runCmd(const char *format, ...);
extern char *chrCmd(char *output, size_t output_size, const char *format, ...);
#ifdef __cplusplus
}
#endif