summaryrefslogtreecommitdiff
path: root/application/spidlib
diff options
context:
space:
mode:
authorJérome Lefranc2008-11-27 22:33:18 +0100
committerJérome Lefranc2008-11-27 22:33:18 +0100
commitec95ef94bc26647d2db56af3971c4eecff227420 (patch)
tree5a857b26c65bdc46276dd07917471dcc6b5245fc /application/spidlib
parent646ffabffcc962c92c1f88c5f648427d1e27228d (diff)
[spidlib] add mac_str_to_bin and mac_bin_to_str
Diffstat (limited to 'application/spidlib')
-rw-r--r--application/spidlib/inc/network.h3
-rw-r--r--application/spidlib/src/config/read_config_param.c3
-rw-r--r--application/spidlib/src/network/mac.c38
3 files changed, 43 insertions, 1 deletions
diff --git a/application/spidlib/inc/network.h b/application/spidlib/inc/network.h
index 5f89f6313f..5848a546f5 100644
--- a/application/spidlib/inc/network.h
+++ b/application/spidlib/inc/network.h
@@ -26,6 +26,9 @@
|| !strcmp(iface, SPIDLIB_INTERFACE_PLC0) \
|| !strcmp(iface, SPIDLIB_INTERFACE_ETH0))
+extern int spidlib_mac_str_to_bin(const char *str, unsigned char *bin);
+extern int spidlib_mac_bin_to_str(const unsigned char *bin, char *str);
+
/*---------------------- Function declarations ------------------------------*/
extern int spidlib_get_ip_addr(const char *iface, char *text, int buffer_len);
diff --git a/application/spidlib/src/config/read_config_param.c b/application/spidlib/src/config/read_config_param.c
index 08d21ff293..f672355736 100644
--- a/application/spidlib/src/config/read_config_param.c
+++ b/application/spidlib/src/config/read_config_param.c
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
#include <errno.h>
#include "spidlib.h"
@@ -54,7 +55,7 @@ spidlib_read_config_param(const char *filename, const char *label, char *value,
return -errno;
}
- while(result = fgets(buffer, MAX_LINE_BUFFER_LEN - 1, fp))
+ while((result = fgets(buffer, MAX_LINE_BUFFER_LEN - 1, fp)))
{
/* get the label */
ptr = strtok_r(buffer, SPIDLIB_CONFIG_DELIMITER " \t", &strtok_ctx);
diff --git a/application/spidlib/src/network/mac.c b/application/spidlib/src/network/mac.c
index acbde65474..aba5a31289 100644
--- a/application/spidlib/src/network/mac.c
+++ b/application/spidlib/src/network/mac.c
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <net/ethernet.h>
#include "spidlib.h"
#define MAC_STRING_MAX_LEN 18
@@ -83,3 +84,40 @@ error:
fclose(fp);
return -errno;
}
+
+int
+spidlib_mac_str_to_bin(const char *str, unsigned char *bin)
+{
+ int result, i;
+ unsigned int mac[ETH_ALEN];
+ if((str == NULL) || (bin == NULL))
+ return -(errno = EINVAL);
+ result = sscanf(str,"%2x:%2x:%2x:%2x:%2x:%2x",
+ mac + 0, mac + 1, mac + 2,
+ mac + 3, mac + 4, mac + 5);
+ for(i = 0; i < ETH_ALEN; i++)
+ bin[i]= mac[i];
+
+ if(result == ETH_ALEN)
+ return 0;
+ else
+ return -(errno = EINVAL);
+}
+
+int
+spidlib_mac_bin_to_str(const unsigned char *bin, char *str)
+{
+ int i;
+ char *ptr = str;
+ if((bin == NULL) || (str == NULL))
+ return -(errno = EINVAL);
+ for (i = 0; i < ETH_ALEN; i++) {
+ ptr += sprintf(ptr, "%02x", bin[i]);
+ if (i < 5) {
+ ptr += sprintf(ptr, ":");
+ }
+ }
+ *ptr = '\0';
+ return 0;
+}
+