drivers/ieee802154: Add hw filtering support for upipe driver
Implements HW filtering for IEEE802.15.4 UART Pipe driver. Signed-off-by: Pedro Martucci <pedropaulomartucci@gmail.com>
This commit is contained in:
parent
023628f3d7
commit
ede5b8ffc3
2 changed files with 181 additions and 2 deletions
|
@ -57,7 +57,7 @@ source "drivers/ieee802154/Kconfig.cc1200"
|
|||
|
||||
menuconfig IEEE802154_UPIPE
|
||||
bool "UART PIPE fake radio driver support for QEMU"
|
||||
depends on BOARD_QEMU_X86 && NETWORKING
|
||||
depends on (BOARD_QEMU_X86 || BOARD_QEMU_CORTEX_M3) && NETWORKING
|
||||
select UART_PIPE
|
||||
default n
|
||||
|
||||
|
@ -66,4 +66,48 @@ config IEEE802154_UPIPE_DRV_NAME
|
|||
default "IEEE802154_UPIPE"
|
||||
depends on IEEE802154_UPIPE
|
||||
|
||||
config IEEE802154_UPIPE_HW_FILTER
|
||||
bool "Hw Filtering"
|
||||
default y
|
||||
help
|
||||
This option assure the driver will process just frames addressed to him.
|
||||
|
||||
config IEEE802154_UPIPE_RANDOM_MAC
|
||||
bool "Random MAC address"
|
||||
default y
|
||||
help
|
||||
Generate a random MAC address dynamically.
|
||||
|
||||
if ! IEEE802154_UPIPE_RANDOM_MAC
|
||||
|
||||
config IEEE802154_UPIPE_MAC4
|
||||
hex "MAC Address Byte 4"
|
||||
default 0
|
||||
range 0 ff
|
||||
help
|
||||
This is the byte 4 of the MAC address.
|
||||
|
||||
config IEEE802154_UPIPE_MAC5
|
||||
hex "MAC Address Byte 5"
|
||||
default 0
|
||||
range 0 ff
|
||||
help
|
||||
This is the byte 5 of the MAC address.
|
||||
|
||||
config IEEE802154_UPIPE_MAC6
|
||||
hex "MAC Address Byte 6"
|
||||
default 0
|
||||
range 0 ff
|
||||
help
|
||||
This is the byte 6 of the MAC address.
|
||||
|
||||
config IEEE802154_UPIPE_MAC7
|
||||
hex "MAC Address Byte 7"
|
||||
default 0
|
||||
range 0 ff
|
||||
help
|
||||
This is the byte 7 of the MAC address.
|
||||
|
||||
endif # IEEE802154_UPIPE_RANDOM_MAC
|
||||
|
||||
endif # IEEE802154
|
||||
|
|
|
@ -24,9 +24,77 @@
|
|||
|
||||
#include "ieee802154_uart_pipe.h"
|
||||
|
||||
#define PAN_ID_OFFSET 3 /* Pan Id offset */
|
||||
#define DEST_ADDR_OFFSET 5 /* Destination offset address*/
|
||||
#define DEST_ADDR_TYPE_OFFSET 1 /* Destination address type */
|
||||
|
||||
#define DEST_ADDR_TYPE_MASK 0x0c /* Mask for destination address type */
|
||||
|
||||
#define DEST_ADDR_TYPE_SHORT 0x08 /* Short destination address type */
|
||||
#define DEST_ADDR_TYPE_EXTENDED 0x0c /* Extended destination address type */
|
||||
|
||||
#define PAN_ID_SIZE 2 /* Size of Pan Id */
|
||||
#define SHORT_ADDRESS_SIZE 2 /* Size of Short Mac Address */
|
||||
#define EXTENDED_ADDRESS_SIZE 8 /* Size of Extended Mac Address */
|
||||
|
||||
/* Broadcast Short Address */
|
||||
#define BROADCAST_ADDRESS ((uint8_t [SHORT_ADDRESS_SIZE]) {0xff, 0xff})
|
||||
|
||||
static u8_t dev_pan_id[PAN_ID_SIZE]; /* Device Pan Id */
|
||||
static u8_t dev_short_addr[SHORT_ADDRESS_SIZE]; /* Device Short Address */
|
||||
static u8_t dev_ext_addr[EXTENDED_ADDRESS_SIZE]; /* Device Extended Address */
|
||||
|
||||
/** Singleton device used in uart pipe callback */
|
||||
static struct device *upipe_dev;
|
||||
|
||||
#if defined(CONFIG_IEEE802154_UPIPE_HW_FILTER)
|
||||
|
||||
static bool received_dest_addr_matched(u8_t *rx_buffer)
|
||||
{
|
||||
struct upipe_context *upipe = upipe_dev->driver_data;
|
||||
|
||||
/* Check destination PAN Id */
|
||||
if (memcmp(&rx_buffer[PAN_ID_OFFSET],
|
||||
dev_pan_id, PAN_ID_SIZE) != 0 &&
|
||||
memcmp(&rx_buffer[PAN_ID_OFFSET],
|
||||
BROADCAST_ADDRESS, PAN_ID_SIZE) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Check destination address */
|
||||
switch (rx_buffer[DEST_ADDR_TYPE_OFFSET] & DEST_ADDR_TYPE_MASK) {
|
||||
case DEST_ADDR_TYPE_SHORT:
|
||||
/* First check if the destination is broadcast */
|
||||
/* If not broadcast, check if lenght and address matches */
|
||||
if (memcmp(&rx_buffer[DEST_ADDR_OFFSET],
|
||||
BROADCAST_ADDRESS,
|
||||
SHORT_ADDRESS_SIZE) != 0 &&
|
||||
(upipe->iface->link_addr.len != SHORT_ADDRESS_SIZE ||
|
||||
memcmp(&rx_buffer[DEST_ADDR_OFFSET],
|
||||
dev_short_addr,
|
||||
SHORT_ADDRESS_SIZE) != 0)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case DEST_ADDR_TYPE_EXTENDED:
|
||||
/* If not broadcast, check if lenght and address matches */
|
||||
if (upipe->iface->link_addr.len != EXTENDED_ADDRESS_SIZE ||
|
||||
memcmp(&rx_buffer[DEST_ADDR_OFFSET],
|
||||
dev_ext_addr, EXTENDED_ADDRESS_SIZE) != 0) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static u8_t *upipe_rx(u8_t *buf, size_t *off)
|
||||
{
|
||||
struct upipe_context *upipe = upipe_dev->driver_data;
|
||||
|
@ -71,6 +139,13 @@ static u8_t *upipe_rx(u8_t *buf, size_t *off)
|
|||
memcpy(frag->data, upipe->rx_buf, upipe->rx_len);
|
||||
net_buf_add(frag, upipe->rx_len);
|
||||
|
||||
#if defined(CONFIG_IEEE802154_UPIPE_HW_FILTER)
|
||||
if (received_dest_addr_matched(frag->data) == false) {
|
||||
SYS_LOG_DBG("Packet received is not addressed to me");
|
||||
goto out;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ieee802154_radio_handle_ack(upipe->iface, pkt) == NET_OK) {
|
||||
SYS_LOG_DBG("ACK packet handled");
|
||||
goto out;
|
||||
|
@ -98,7 +173,9 @@ done:
|
|||
|
||||
static enum ieee802154_hw_caps upipe_get_capabilities(struct device *dev)
|
||||
{
|
||||
return IEEE802154_HW_FCS | IEEE802154_HW_2_4_GHZ;
|
||||
return IEEE802154_HW_FCS |
|
||||
IEEE802154_HW_2_4_GHZ |
|
||||
IEEE802154_HW_FILTER;
|
||||
}
|
||||
|
||||
static int upipe_cca(struct device *dev)
|
||||
|
@ -123,6 +200,56 @@ static int upipe_set_channel(struct device *dev, u16_t channel)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int upipe_set_pan_id(struct device *dev, u16_t pan_id)
|
||||
{
|
||||
u8_t pan_id_le[2];
|
||||
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
sys_put_le16(pan_id, pan_id_le);
|
||||
memcpy(dev_pan_id, pan_id_le, PAN_ID_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int upipe_set_short_addr(struct device *dev, u16_t short_addr)
|
||||
{
|
||||
u8_t short_addr_le[2];
|
||||
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
sys_put_le16(short_addr, short_addr_le);
|
||||
memcpy(dev_short_addr, short_addr_le, SHORT_ADDRESS_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int upipe_set_ieee_addr(struct device *dev, const u8_t *ieee_addr)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
memcpy(dev_ext_addr, ieee_addr, EXTENDED_ADDRESS_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int upipe_set_filter(struct device *dev,
|
||||
enum ieee802154_filter_type type,
|
||||
const struct ieee802154_filter *filter)
|
||||
{
|
||||
SYS_LOG_DBG("Applying filter %u", type);
|
||||
|
||||
if (type == IEEE802154_FILTER_TYPE_IEEE_ADDR) {
|
||||
return upipe_set_ieee_addr(dev, filter->ieee_addr);
|
||||
} else if (type == IEEE802154_FILTER_TYPE_SHORT_ADDR) {
|
||||
return upipe_set_short_addr(dev, filter->short_addr);
|
||||
} else if (type == IEEE802154_FILTER_TYPE_PAN_ID) {
|
||||
return upipe_set_pan_id(dev, filter->pan_id);
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static int upipe_set_txpower(struct device *dev, s16_t dbm)
|
||||
{
|
||||
struct upipe_context *upipe = dev->driver_data;
|
||||
|
@ -210,8 +337,15 @@ static inline u8_t *get_mac(struct device *dev)
|
|||
upipe->mac_addr[2] = 0x20;
|
||||
upipe->mac_addr[3] = 0x30;
|
||||
|
||||
#if defined(CONFIG_IEEE802154_UPIPE_RANDOM_MAC)
|
||||
UNALIGNED_PUT(sys_cpu_to_be32(sys_rand32_get()),
|
||||
(u32_t *) ((void *)upipe->mac_addr+4));
|
||||
#else
|
||||
upipe->mac_addr[4] = CONFIG_IEEE802154_UPIPE_MAC4;
|
||||
upipe->mac_addr[5] = CONFIG_IEEE802154_UPIPE_MAC5;
|
||||
upipe->mac_addr[6] = CONFIG_IEEE802154_UPIPE_MAC6;
|
||||
upipe->mac_addr[7] = CONFIG_IEEE802154_UPIPE_MAC7;
|
||||
#endif
|
||||
|
||||
return upipe->mac_addr;
|
||||
}
|
||||
|
@ -241,6 +375,7 @@ static struct ieee802154_radio_api upipe_radio_api = {
|
|||
.get_capabilities = upipe_get_capabilities,
|
||||
.cca = upipe_cca,
|
||||
.set_channel = upipe_set_channel,
|
||||
.set_filter = upipe_set_filter,
|
||||
.set_txpower = upipe_set_txpower,
|
||||
.tx = upipe_tx,
|
||||
.start = upipe_start,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue