Commit ffe1546b authored by Adam Wujek's avatar Adam Wujek

lib/util: move format_mac, decode_mac and decode_port from cmd_mac to lib/util

Signed-off-by: 's avatarAdam Wujek <adam.wujek@creotech.pl>
parent 3a2fff4c
......@@ -57,6 +57,11 @@ const char *fromhex(const char *hex, int *v);
const char *fromhex64(const char *hex, int64_t *v);
const char *fromdec(const char *dec, int *v);
char *format_mac(char *s, const unsigned char *mac);
void decode_mac(const char *str, unsigned char *mac);
void decode_port(const char *str, int *port);
static inline int within_range(int x, int minval, int maxval, int wrap)
{
int rv;
......
......@@ -211,6 +211,35 @@ const char *fromdec(const char *dec, int *v)
return dec;
}
char *format_mac(char *s, const unsigned char *mac)
{
pp_sprintf(s, "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return s;
}
void decode_mac(const char *str, unsigned char *mac)
{
int i, x;
/* Don't try to detect bad input; need small code */
for (i = 0; i < 6; ++i) {
str = fromhex(str, &x);
mac[i] = x;
if (*str == ':')
++str;
}
}
void decode_port(const char *str, int *port)
{
if( !str )
*port = 0;
else
*port = atoi(str);
}
/*
* This is a minimal atoi, that doesn't call strtol. Since we are only
* calling atoi, it saves XXXX bytes of library code
......
......@@ -18,34 +18,6 @@
#include "dev/onewire.h"
#include "dev/endpoint.h"
void decode_mac(const char *str, unsigned char *mac)
{
int i, x;
/* Don't try to detect bad input; need small code */
for (i = 0; i < 6; ++i) {
str = fromhex(str, &x);
mac[i] = x;
if (*str == ':')
++str;
}
}
void decode_port(const char *str, int *port)
{
if( !str )
*port = 0;
else
*port = atoi(str);
}
char *format_mac(char *s, const unsigned char *mac)
{
pp_sprintf(s, "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return s;
}
static int cmd_mac(const char *args[])
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment