[PATCH 2/8] w1: introduce helper to generate mac address from 1-wire id
Jean-Christophe PLAGNIOL-VILLARD
plagnioj at jcrosoft.com
Sat Oct 27 20:24:06 EDT 2012
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>
---
include/w1_mac_address.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 64 insertions(+)
create mode 100644 include/w1_mac_address.h
diff --git a/include/w1_mac_address.h b/include/w1_mac_address.h
new file mode 100644
index 0000000..ed93e0e
--- /dev/null
+++ b/include/w1_mac_address.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnio at jcrosoft.com>
+ *
+ * Under GPLv2 only
+ */
+
+#ifndef __W1_MAC_ADDRESS_H__
+#define __W1_MAC_ADDRESS_H__
+
+static inline int w1_mac_address_register(int eth_num, bool local,
+ char * oui, char *w1_dev)
+{
+ char buf[6];
+ const char *val;
+ u64 id;
+ int nb_oui = 0;
+ int i, shift;
+ char *tmp = NULL;
+ int ret = 0;
+
+ if (!local && !oui)
+ return -EINVAL;
+
+ if (oui) {
+ nb_oui = strlen(oui);
+ if (!local && nb_oui != 3)
+ return -EINVAL;
+
+ nb_oui = min(nb_oui, 3);
+
+ for (i = 0; i < nb_oui; i++)
+ buf[i] = oui[i];
+ }
+
+ tmp = asprintf("%s.id", w1_dev);
+ if (!tmp)
+ return -ENOMEM;
+
+ val = getenv("w1-1-0.id");
+ if (!val) {
+ ret = -EINVAL;
+ goto err;
+ }
+
+ id = simple_strtoull(val, NULL, 16);
+ if (!id) {
+ ret = -EINVAL;
+ goto err;
+ }
+
+ for (i = nb_oui, shift = 40; i < 6; i++, shift -= 8)
+ buf[i] = (id >> shift) & 0xff;
+
+ if (local)
+ buf[0] |= 0x2;
+
+ eth_register_ethaddr(eth_num, buf);
+
+err:
+ free(tmp);
+ return ret;
+}
+
+#endif /* __W1_MAC_ADDRESS_H__ */
--
1.7.10.4
More information about the barebox
mailing list