2020-04-24 01:13:14 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Linaro Ltd
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZEPHYR_DRIVERS_ETHERNET_ETH_H_
|
|
|
|
#define ZEPHYR_DRIVERS_ETHERNET_ETH_H_
|
|
|
|
|
|
|
|
#include <zephyr/types.h>
|
2020-06-12 12:28:55 +08:00
|
|
|
#include <random/rand32.h>
|
2020-04-24 01:13:14 +08:00
|
|
|
|
2020-04-24 01:37:38 +08:00
|
|
|
/* helper macro to return mac address octet from local_mac_address prop */
|
|
|
|
#define NODE_MAC_ADDR_OCTET(node, n) DT_PROP_BY_IDX(node, local_mac_address, n)
|
|
|
|
|
|
|
|
/* Determine if a mac address is all 0's */
|
|
|
|
#define NODE_MAC_ADDR_NULL(node) \
|
|
|
|
((NODE_MAC_ADDR_OCTET(node, 0) == 0) && \
|
|
|
|
(NODE_MAC_ADDR_OCTET(node, 1) == 0) && \
|
|
|
|
(NODE_MAC_ADDR_OCTET(node, 2) == 0) && \
|
|
|
|
(NODE_MAC_ADDR_OCTET(node, 3) == 0) && \
|
|
|
|
(NODE_MAC_ADDR_OCTET(node, 4) == 0) && \
|
|
|
|
(NODE_MAC_ADDR_OCTET(node, 5) == 0))
|
|
|
|
|
|
|
|
/* Given a device tree node for an ethernet controller will
|
|
|
|
* returns false if there is no local-mac-address property or
|
|
|
|
* the property is all zero's. Otherwise will return True
|
|
|
|
*/
|
|
|
|
#define NODE_HAS_VALID_MAC_ADDR(node) \
|
|
|
|
UTIL_AND(DT_NODE_HAS_PROP(node, local_mac_address),\
|
|
|
|
(!NODE_MAC_ADDR_NULL(node)))
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static inline void gen_random_mac(uint8_t *mac_addr, uint8_t b0, uint8_t b1, uint8_t b2)
|
2020-04-24 01:13:14 +08:00
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t entropy;
|
2020-04-24 01:13:14 +08:00
|
|
|
|
|
|
|
entropy = sys_rand32_get();
|
|
|
|
|
|
|
|
mac_addr[0] = b0;
|
|
|
|
mac_addr[1] = b1;
|
|
|
|
mac_addr[2] = b2;
|
|
|
|
|
|
|
|
/* Set MAC address locally administered, unicast (LAA) */
|
|
|
|
mac_addr[0] |= 0x02;
|
|
|
|
|
|
|
|
mac_addr[3] = (entropy >> 16) & 0xff;
|
|
|
|
mac_addr[4] = (entropy >> 8) & 0xff;
|
|
|
|
mac_addr[5] = (entropy >> 0) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ZEPHYR_DRIVERS_ETHERNET_ETH_H_ */
|