diff --git a/pkg/nordic_softdevice_ble/src/ble-core.c b/pkg/nordic_softdevice_ble/src/ble-core.c index b076cab1a9..cd3c8c9f9c 100644 --- a/pkg/nordic_softdevice_ble/src/ble-core.c +++ b/pkg/nordic_softdevice_ble/src/ble-core.c @@ -46,6 +46,7 @@ */ #include #include +#include //#include "boards.h" //#include "nordic_common.h" @@ -109,8 +110,9 @@ ble_stack_init(void) * @brief Return device EUI64 MAC address * @param addr pointer to a buffer to store the address */ +#include "ble-mac.h" void -ble_get_mac(uint8_t addr[8]) +ble_get_mac(uint8_t addr[6]) { uint32_t err_code; ble_gap_addr_t ble_addr; @@ -118,7 +120,8 @@ ble_get_mac(uint8_t addr[8]) err_code = sd_ble_gap_address_get(&ble_addr); APP_ERROR_CHECK(err_code); - IPV6_EUI64_CREATE_FROM_EUI48(addr, ble_addr.addr, ble_addr.addr_type); + ble_eui48(addr, ble_addr.addr, + ble_addr.addr_type == BLE_GAP_ADDR_TYPE_PUBLIC); } /*---------------------------------------------------------------------------*/ /** diff --git a/pkg/nordic_softdevice_ble/src/ble-core.h b/pkg/nordic_softdevice_ble/src/ble-core.h index 05b43c68f7..e72cd5d799 100644 --- a/pkg/nordic_softdevice_ble/src/ble-core.h +++ b/pkg/nordic_softdevice_ble/src/ble-core.h @@ -70,11 +70,11 @@ void ble_advertising_init(const char *name); void ble_advertising_start(void); /** - * @brief Return device EUI64 MAC address + * @brief Return device MAC address * * @param addr pointer to a buffer to store the address */ -void ble_get_mac(uint8_t addr[8]); +void ble_get_mac(uint8_t addr[6]); #ifdef __cplusplus } diff --git a/pkg/nordic_softdevice_ble/src/ble-mac.c b/pkg/nordic_softdevice_ble/src/ble-mac.c index 4724dbd299..025231b225 100644 --- a/pkg/nordic_softdevice_ble/src/ble-mac.c +++ b/pkg/nordic_softdevice_ble/src/ble-mac.c @@ -52,7 +52,7 @@ #endif typedef struct { - uint8_t peer_addr[8]; + uint8_t peer_addr[BLE_L2_ADDR_LEN]; ble_ipsp_handle_t handle; } ble_mac_interface_t; @@ -96,14 +96,14 @@ static ble_mac_interface_t *ble_mac_interface_lookup(ble_ipsp_handle_t *handle) * @return a pointer to an interface structure on success * @return NULL if interface table is full */ -static ble_mac_interface_t *ble_mac_interface_add(uint8_t peer[8], +static ble_mac_interface_t *ble_mac_interface_add(uint8_t peer[BLE_L2_ADDR_LEN], ble_ipsp_handle_t *handle) { DEBUG("ble_mac_interface_add()\n"); for (int i = 0; i < BLE_MAC_MAX_INTERFACE_NUM; i++) { if (interfaces[i].handle.conn_handle == 0 && interfaces[i].handle.cid == 0) { memcpy(&interfaces[i].handle, handle, sizeof(ble_ipsp_handle_t)); - memcpy(&interfaces[i].peer_addr, peer, 8); + memcpy(&interfaces[i].peer_addr, peer, BLE_L2_ADDR_LEN); /* notify handler thread */ /* msg_t m = { .type = BLE_IFACE_ADDED, .content.ptr = &interfaces[i] }; */ @@ -135,7 +135,7 @@ static void ble_mac_interface_delete(ble_mac_interface_t *interface) static ble_ipsp_handle_t *_find_handle(const uint8_t *addr) { for (int i = 0; i < BLE_MAC_MAX_INTERFACE_NUM; i++) { - if (memcmp(interfaces[i].peer_addr, addr, BLE_SIXLOWPAN_L2_ADDR_LEN) == 0) { + if (memcmp(interfaces[i].peer_addr, addr, BLE_L2_ADDR_LEN) == 0) { return &interfaces[i].handle; } } @@ -155,7 +155,7 @@ static int _send_to_peer(ble_ipsp_handle_t *handle, void *data, size_t len) return ble_ipsp_send(handle, data, len); } -static int _is_broadcast(uint8_t dest[8]) +static int _is_broadcast(uint8_t dest[BLE_L2_ADDR_LEN]) { uint32_t *_dest = (uint32_t*)dest; for (int i = 0; i < 2; i++) { @@ -166,12 +166,12 @@ static int _is_broadcast(uint8_t dest[8]) return 1; } -int ble_mac_send(uint8_t dest[8], void *data, size_t len) +int ble_mac_send(uint8_t dest[BLE_L2_ADDR_LEN], void *data, size_t len) { DEBUG("ble_mac_send(): sending pkt with len %u\n", (unsigned)len); #if defined(MODULE_OD) && ENABLE_DEBUG - od_hex_dump(dest, 8, OD_WIDTH_DEFAULT); + od_hex_dump(dest, BLE_L2_ADDR_LEN, OD_WIDTH_DEFAULT); od_hex_dump(data, len, OD_WIDTH_DEFAULT); #endif @@ -217,13 +217,13 @@ static uint32_t ble_mac_ipsp_evt_handler_irq(ble_ipsp_handle_t *p_handle, ble_ip switch (p_evt->evt_id) { case BLE_IPSP_EVT_CHANNEL_CONNECTED: { - uint8_t peer_addr[8]; + uint8_t peer_addr[BLE_L2_ADDR_LEN]; DEBUG("ble-mac: channel connected\n"); - ble_eui64_from_eui48(peer_addr, p_evt->evt_param->params.ch_conn_request.peer_addr.addr, - p_evt->evt_param->params.ch_conn_request.peer_addr.addr_type == - BLE_GAP_ADDR_TYPE_PUBLIC); - + ble_eui48(peer_addr, + p_evt->evt_param->params.ch_conn_request.peer_addr.addr, + p_evt->evt_param->params.ch_conn_request.peer_addr.addr_type == + BLE_GAP_ADDR_TYPE_PUBLIC); p_instance = ble_mac_interface_add(peer_addr, p_handle); if (p_instance != NULL) { @@ -262,7 +262,7 @@ static uint32_t ble_mac_ipsp_evt_handler_irq(ble_ipsp_handle_t *p_handle, ble_ip inbuf.len = p_evt->evt_param->params.ch_rx.len; memcpy(inbuf.payload, p_evt->evt_param->params.ch_rx.p_data, inbuf.len); - memcpy(inbuf.src, p_instance->peer_addr, 8); + memcpy(inbuf.src, p_instance->peer_addr, BLE_L2_ADDR_LEN); sd_ble_gap_rssi_get(p_handle->conn_handle, &inbuf.rssi); _callback(BLE_EVENT_RX_DONE, &inbuf); diff --git a/pkg/nordic_softdevice_ble/src/ble-mac.h b/pkg/nordic_softdevice_ble/src/ble-mac.h index daf6f2966a..ed120d4a98 100644 --- a/pkg/nordic_softdevice_ble/src/ble-mac.h +++ b/pkg/nordic_softdevice_ble/src/ble-mac.h @@ -51,34 +51,34 @@ typedef enum { #define BLE_IFACE_ADDED (10000) #define BLE_SIXLOWPAN_MTU (1280U) -#define BLE_SIXLOWPAN_L2_ADDR_LEN (8) +#define BLE_L2_ADDR_LEN (6U) +#ifndef IPV6_IID_FLIP_VALUE #define IPV6_IID_FLIP_VALUE (0x02) - -#include "net/eui64.h" +#endif /** * @brief Get BLE EUI64 from EUI48 * - * @param[out] eui64 The output EUI64 (8 bytes long) - * @param[in] eui48 The input EUI48 (6 bytes long) + * @param[out] eui48 The output EUI48 (big-endian, + * 6 bytes long) + * @param[in] ble_addr The input BLE address (little-endian, + * 6 bytes long) * @param[in] _public True if public interface, false otherwise */ -static inline void ble_eui64_from_eui48(uint8_t eui64[8], uint8_t eui48[6], int _public) +static inline void ble_eui48(uint8_t *eui48, const uint8_t *ble_addr, int _public) { - eui64[0] = eui48[5]; - eui64[1] = eui48[4]; - eui64[2] = eui48[3]; - eui64[3] = 0xFF; - eui64[4] = 0xFE; - eui64[5] = eui48[2]; - eui64[6] = eui48[1]; - eui64[7] = eui48[0]; + eui48[0] = ble_addr[5]; + eui48[1] = ble_addr[4]; + eui48[2] = ble_addr[3]; + eui48[3] = ble_addr[2]; + eui48[4] = ble_addr[1]; + eui48[5] = ble_addr[0]; if (_public) { - eui64[0] &= ~(IPV6_IID_FLIP_VALUE); + eui48[0] &= ~(IPV6_IID_FLIP_VALUE); } else { - eui64[0] |= IPV6_IID_FLIP_VALUE; + eui48[0] |= IPV6_IID_FLIP_VALUE; } } @@ -86,8 +86,8 @@ static inline void ble_eui64_from_eui48(uint8_t eui64[8], uint8_t eui48[6], int * @brief Structure handling a received BLE mac packet */ typedef struct { - uint8_t src[8]; /**< Source address of the packet */ uint8_t payload[BLE_SIXLOWPAN_MTU]; /**< Payload of the packet */ + uint8_t src[BLE_L2_ADDR_LEN]; /**< Source address of the packet */ uint16_t len; /**< Length of the packet */ int8_t rssi; /**< RSSI of the received packet */ } ble_mac_inbuf_t; @@ -114,7 +114,8 @@ void ble_mac_init(ble_mac_callback_t callback); * @return 0 if send is successful * @return <0 if send failed */ -int ble_mac_send(uint8_t dest[8], void *data, size_t len); +int ble_mac_send(uint8_t dest[BLE_L2_ADDR_LEN], void *data, + size_t len); extern volatile int ble_mac_busy_tx; /**< Flag is set to 1 when the driver is busy transmitting a packet. */ diff --git a/pkg/nordic_softdevice_ble/src/gnrc_nordic_ble_6lowpan.c b/pkg/nordic_softdevice_ble/src/gnrc_nordic_ble_6lowpan.c index 760bbdd343..224e6eaf32 100644 --- a/pkg/nordic_softdevice_ble/src/gnrc_nordic_ble_6lowpan.c +++ b/pkg/nordic_softdevice_ble/src/gnrc_nordic_ble_6lowpan.c @@ -45,6 +45,7 @@ #include "msg.h" #include "thread.h" +#include "net/eui48.h" #include "net/gnrc.h" #include "net/gnrc/netif.h" #include "net/gnrc/nettype.h" @@ -104,15 +105,15 @@ static void _handle_raw_sixlowpan(ble_mac_inbuf_t *inbuf) return; } - gnrc_netif_hdr_init(netif_hdr->data, BLE_SIXLOWPAN_L2_ADDR_LEN, BLE_SIXLOWPAN_L2_ADDR_LEN); - gnrc_netif_hdr_set_src_addr(netif_hdr->data, inbuf->src, BLE_SIXLOWPAN_L2_ADDR_LEN); - gnrc_netif_hdr_set_dst_addr(netif_hdr->data, _ble_netif->l2addr, BLE_SIXLOWPAN_L2_ADDR_LEN); + gnrc_netif_hdr_init(netif_hdr->data, BLE_L2_ADDR_LEN, BLE_L2_ADDR_LEN); + gnrc_netif_hdr_set_src_addr(netif_hdr->data, inbuf->src, BLE_L2_ADDR_LEN); + gnrc_netif_hdr_set_dst_addr(netif_hdr->data, _ble_netif->l2addr, BLE_L2_ADDR_LEN); ((gnrc_netif_hdr_t *)netif_hdr->data)->if_pid = _ble_netif->pid; - DEBUG("_handle_raw_sixlowpan(): received packet from %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x " + DEBUG("_handle_raw_sixlowpan(): received packet from %02x:%02x:%02x:%02x:%02x:%02x " "of length %d\n", - inbuf->src[0], inbuf->src[1], inbuf->src[2], inbuf->src[3], inbuf->src[4], - inbuf->src[5], inbuf->src[6], inbuf->src[7], inbuf->len); + inbuf->src[0], inbuf->src[1], inbuf->src[2], + inbuf->src[3], inbuf->src[4], inbuf->src[5], inbuf->len); #if defined(MODULE_OD) && ENABLE_DEBUG od_hex_dump(inbuf->payload, inbuf->len, OD_WIDTH_DEFAULT); #endif @@ -183,7 +184,7 @@ static int _netdev_init(netdev_t *dev) _ble_netif = dev->context; ble_stack_init(); ble_mac_init(_ble_mac_callback); - _ble_netif->l2addr_len = BLE_SIXLOWPAN_L2_ADDR_LEN; + _ble_netif->l2addr_len = BLE_L2_ADDR_LEN; ble_get_mac(_ble_netif->l2addr); ble_advertising_init("RIOT BLE"); ble_advertising_start(); @@ -198,15 +199,15 @@ static int _netdev_get(netdev_t *netdev, netopt_t opt, (void)netdev; switch (opt) { - case NETOPT_ADDRESS_LONG: - assert(max_len >= BLE_SIXLOWPAN_L2_ADDR_LEN); - memcpy(value, _ble_netif->l2addr, BLE_SIXLOWPAN_L2_ADDR_LEN); - res = BLE_SIXLOWPAN_L2_ADDR_LEN; + case NETOPT_ADDRESS: + assert(max_len >= BLE_L2_ADDR_LEN); + memcpy(value, _ble_netif->l2addr, BLE_L2_ADDR_LEN); + res = BLE_L2_ADDR_LEN; break; case NETOPT_ADDR_LEN: case NETOPT_SRC_LEN: assert(max_len == sizeof(uint16_t)); - *((uint16_t *)value) = BLE_SIXLOWPAN_L2_ADDR_LEN; + *((uint16_t *)value) = BLE_L2_ADDR_LEN; res = sizeof(uint16_t); break; case NETOPT_PROTO: @@ -220,9 +221,8 @@ static int _netdev_get(netdev_t *netdev, netopt_t opt, res = sizeof(uint16_t); break; case NETOPT_IPV6_IID: - memcpy(value, _ble_netif->l2addr, BLE_SIXLOWPAN_L2_ADDR_LEN); - value[0] ^= IPV6_IID_FLIP_VALUE; - res = BLE_SIXLOWPAN_L2_ADDR_LEN; + eui48_to_ipv6_iid((eui64_t *)value, (eui48_t *)_ble_netif->l2addr); + res = sizeof(uint64_t); break; default: break; diff --git a/sys/net/gnrc/netif/gnrc_netif_device_type.c b/sys/net/gnrc/netif/gnrc_netif_device_type.c index 031a0474db..417371a959 100644 --- a/sys/net/gnrc/netif/gnrc_netif_device_type.c +++ b/sys/net/gnrc/netif/gnrc_netif_device_type.c @@ -142,9 +142,11 @@ int gnrc_netif_ipv6_iid_from_addr(const gnrc_netif_t *netif, #if GNRC_NETIF_L2ADDR_MAXLEN > 0 if (netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR) { switch (netif->device_type) { -#if defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) +#if defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) || \ + defined(MODULE_NORDIC_SOFTDEVICE_BLE) case NETDEV_TYPE_ETHERNET: case NETDEV_TYPE_ESP_NOW: + case NETDEV_TYPE_BLE: if (addr_len == sizeof(eui48_t)) { eui48_to_ipv6_iid(iid, (const eui48_t *)addr); return sizeof(eui64_t); @@ -162,17 +164,6 @@ int gnrc_netif_ipv6_iid_from_addr(const gnrc_netif_t *netif, return -EINVAL; } #endif /* defined(MODULE_NETDEV_IEEE802154) || defined(MODULE_XBEE) */ -#ifdef MODULE_NORDIC_SOFTDEVICE_BLE - case NETDEV_TYPE_BLE: - if (addr_len == sizeof(eui64_t)) { - memcpy(iid, addr, sizeof(eui64_t)); - iid->uint8[0] ^= 0x02; - return sizeof(eui64_t); - } - else { - return -EINVAL; - } -#endif /* MODULE_NORDIC_SOFTDEVICE_BLE */ #if defined(MODULE_CC110X) || defined(MODULE_NRFMIN) case NETDEV_TYPE_CC110X: case NETDEV_TYPE_NRFMIN: @@ -205,9 +196,11 @@ int gnrc_netif_ipv6_iid_to_addr(const gnrc_netif_t *netif, const eui64_t *iid, { assert(netif->flags & GNRC_NETIF_FLAGS_HAS_L2ADDR); switch (netif->device_type) { -#if defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) +#if defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) || \ + defined(MODULE_NORDIC_SOFTDEVICE_BLE) case NETDEV_TYPE_ETHERNET: case NETDEV_TYPE_ESP_NOW: + case NETDEV_TYPE_BLE: eui48_from_ipv6_iid((eui48_t *)addr, iid); return sizeof(eui48_t); #endif /* defined(MODULE_NETDEV_ETH) || defined(MODULE_ESP_NOW) */ @@ -225,12 +218,6 @@ int gnrc_netif_ipv6_iid_to_addr(const gnrc_netif_t *netif, const eui64_t *iid, addr[1] = iid->uint8[7]; return sizeof(uint16_t); #endif /* MODULE_NETDEV_IEEE802154 */ -#ifdef MODULE_NORDIC_SOFTDEVICE_BLE - case NETDEV_TYPE_BLE: - memcpy(addr, iid, sizeof(eui64_t)); - addr[0] ^= 0x02; - return sizeof(eui64_t); -#endif /* MODULE_NORDIC_SOFTDEVICE_BLE */ #ifdef MODULE_CC110X case NETDEV_TYPE_CC110X: addr[0] = iid->uint8[7];