Commit 2e959755 authored by Benoit Rat's avatar Benoit Rat

rtu: add function to parse MAC address from cmd line

parent 4bb1a1ba
/*
* White Rabbit RTU (Routing Table Unit)
* White Rabbit MAC utils
* Copyright (C) 2010, CERN.
*
* Version: wrsw_rtud v1.0
......@@ -8,7 +8,7 @@
*
* Description: MAC address type related operations.
*
* Fixes:
* Fixes: Benoit Rat
*
*
* This program is free software; you can redistribute it and/or
......@@ -35,8 +35,26 @@
*/
char *mac_to_string(uint8_t mac[ETH_ALEN])
{
char str[40];
static char str[40];
snprintf(str, 40, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
return strdup(str); //FIXME: can't be static but this takes memory
return str;
}
/**
* \brief Write mac address into a buffer to avoid concurrent access on static variable.
*/
char *mac_to_buffer(uint8_t mac[ETH_ALEN],char buffer[ETH_ALEN_STR])
{
if(mac && buffer)
snprintf(buffer, ETH_ALEN_STR, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
return buffer;
}
/**
* \brief Function to retrieve mac address from text input (argument in terminal)
*/
int mac_from_str(uint8_t* tomac, const char *fromstr)
{
if(tomac==0 || fromstr==0) return -1;
return sscanf(fromstr,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",tomac+0,tomac+1,tomac+2,tomac+3,tomac+4,tomac+5);
}
......@@ -6,11 +6,12 @@
*
* Authors: Juan Luis Manas (juan.manas@integrasys.es)
*
* Description: MAC address type related operations.
* Description: MAC address type related operations.
*
* Fixes:
* Fixes:
* Alessandro Rubini
*
* Benoit Rat
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
......@@ -34,9 +35,10 @@
#include <string.h>
#define ETH_ALEN 6
#define ETH_ALEN_STR 18
/**
* \brief Check whether two mac addresses are equal.
/**
* \brief Check whether two mac addresses are equal.
* @return 1 if both addresses are equal. 0 otherwise.
*/
static inline int mac_equal(uint8_t a[ETH_ALEN], uint8_t b[ETH_ALEN])
......@@ -57,12 +59,18 @@ static inline uint8_t* mac_copy(uint8_t dst[ETH_ALEN], uint8_t src[ETH_ALEN])
* \brief Set MAC address to 00:00:00:00:00:00.
* @return pointer to mac address
*/
static inline uint8_t* mac_clean(uint8_t mac[ETH_ALEN])
static inline uint8_t* mac_clean(uint8_t mac[ETH_ALEN])
{
return memset(mac, 0x00, ETH_ALEN);
}
char *mac_to_string(uint8_t mac[ETH_ALEN]);
char *mac_to_buffer(uint8_t mac[ETH_ALEN],char buffer[ETH_ALEN_STR]);
int mac_from_str(uint8_t* tomac, const char *fromstr);
#endif /* __WHITERABBIT_RTU_MAC_H */
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