diff --git a/drivers/at86rf215/at86rf215.c b/drivers/at86rf215/at86rf215.c index 97450a17a35a..b80751a9b1c0 100644 --- a/drivers/at86rf215/at86rf215.c +++ b/drivers/at86rf215/at86rf215.c @@ -17,7 +17,6 @@ * @} */ -#include "luid.h" #include "board.h" #include "byteorder.h" #include "net/ieee802154.h" @@ -65,8 +64,7 @@ void at86rf215_reset_and_cfg(at86rf215_t *dev) netdev_ieee802154_reset(&dev->netdev); /* set device address */ - luid_get_short((network_uint16_t *)&dev->netdev.short_addr); - luid_get_eui64((eui64_t *)&dev->netdev.long_addr); + netdev_ieee802154_setup(&dev->netdev); if (is_subGHz(dev)) { dev->netdev.chan = CONFIG_AT86RF215_DEFAULT_SUBGHZ_CHANNEL; diff --git a/drivers/include/net/netdev/ieee802154.h b/drivers/include/net/netdev/ieee802154.h index b4a129ce0a5b..af87fda668b2 100644 --- a/drivers/include/net/netdev/ieee802154.h +++ b/drivers/include/net/netdev/ieee802154.h @@ -24,6 +24,7 @@ #include "net/gnrc/nettype.h" #include "net/netopt.h" #include "net/netdev.h" +#include "luid.h" #ifdef __cplusplus extern "C" { @@ -194,6 +195,23 @@ int netdev_ieee802154_set(netdev_ieee802154_t *dev, netopt_t opt, const void *va */ int netdev_ieee802154_dst_filter(netdev_ieee802154_t *dev, const uint8_t *mhr); +/** + * @brief Configure the hardware address of a IEEE 802.15.4 devices + * + * This will obtain a long and short address based on the netdev ID. + * The addresses is stored in the netdev's `long_addr` & `short_addr`. + * The caller must take care of writing them to the hardware. + * + * @pre the netdev registered itself with @see netdev_register + * + * @param[out] dev Netdev to configure + */ +static inline void netdev_ieee802154_setup(netdev_ieee802154_t *dev) +{ + luid_netdev_get_eui64(&dev->netdev, (eui64_t *)&dev->long_addr); + luid_get_short((network_uint16_t *)&dev->short_addr); +} + #ifdef __cplusplus } #endif diff --git a/sys/include/luid.h b/sys/include/luid.h index b2aebb9bcde9..870865b3a39c 100644 --- a/sys/include/luid.h +++ b/sys/include/luid.h @@ -57,6 +57,7 @@ #include "net/eui48.h" #include "net/eui64.h" +#include "net/netdev.h" #ifdef __cplusplus extern "C" { @@ -127,6 +128,19 @@ void luid_get_short(network_uint16_t *addr); */ void luid_get_eui48(eui48_t *addr); +/** + * @brief Get a unique EUI48 address + * + * The resulting address is built from the base ID generated with luid_base(), which + * isXORed with the netdev type and netdev index in the least significant bytes. + * + * @pre the netdev registered itself with @see netdev_register + * + * @param[in] netdev Netdev to generate the EUI48 for. + * @param[out] addr memory location to copy the address into. + */ +void luid_netdev_get_eui48(const netdev_t *netdev, eui48_t *addr); + /** * @brief Get a unique EUI64 address * @@ -139,6 +153,19 @@ void luid_get_eui48(eui48_t *addr); */ void luid_get_eui64(eui64_t *addr); +/** + * @brief Get a unique EUI64 address + * + * The resulting address is built from the base ID generated with luid_base(), which + * isXORed with the netdev type and netdev index in the least significant bytes. + * + * @pre the netdev registered itself with @see netdev_register + * + * @param[in] netdev Netdev to generate the EUI64 for. + * @param[out] addr memory location to copy the address into. + */ +void luid_netdev_get_eui64(const netdev_t *netdev, eui64_t *addr); + /** * @brief Get a custom unique ID based on a user given generator value * diff --git a/sys/luid/luid.c b/sys/luid/luid.c index bdaff93ff4b3..baa51584fefe 100644 --- a/sys/luid/luid.c +++ b/sys/luid/luid.c @@ -89,6 +89,21 @@ void luid_get_eui48(eui48_t *addr) eui48_clear_group(addr); } +void luid_netdev_get_eui48(const netdev_t *netdev, eui48_t *addr) +{ + luid_base(addr, sizeof(*addr)); +#ifdef MODULE_NETDEV_REGISTER + addr->uint8[4] ^= netdev->type; + addr->uint8[5] ^= netdev->index; +#else + /* we should only get here with gnrc_netif_single */ + (void)netdev; +#endif + + eui48_set_local(addr); + eui48_clear_group(addr); +} + void luid_get_eui64(eui64_t *addr) { luid_base(addr, sizeof(*addr)); @@ -97,3 +112,18 @@ void luid_get_eui64(eui64_t *addr) eui64_set_local(addr); eui64_clear_group(addr); } + +void luid_netdev_get_eui64(const netdev_t *netdev, eui64_t *addr) +{ + luid_base(addr, sizeof(*addr)); +#ifdef MODULE_NETDEV_REGISTER + addr->uint8[6] ^= netdev->type; + addr->uint8[7] ^= netdev->index; +#else + /* we should only get here with gnrc_netif_single */ + (void)netdev; +#endif + + eui64_set_local(addr); + eui64_clear_group(addr); +}