1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #16576 from jia200x/pr/netdev/migrate_dst_filter

ieee802154: migrate `netdev_ieee802154_dst_filter` to a common ieee802154
This commit is contained in:
Kevin "Tristate Tom" Weiss 2021-06-29 10:50:08 +02:00 committed by GitHub
commit 25c871ed9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 182 additions and 2 deletions

View File

@ -492,8 +492,10 @@ void isr_radio(void)
/* only process packet if event callback is set and CRC is valid */
if ((nrf802154_dev->netdev.event_callback) &&
(NRF_RADIO->CRCSTATUS == 1) &&
(netdev_ieee802154_dst_filter(nrf802154_dev,
&rxbuf[1]) == 0)) {
(ieee802154_dst_filter(&rxbuf[1],
nrf802154_dev->pan,
(network_uint16_t*) nrf802154_dev->short_addr,
nrf802154_dev->long_addr) == 0)) {
_state |= RX_COMPLETE;
}
else {

View File

@ -208,6 +208,9 @@ int netdev_ieee802154_set(netdev_ieee802154_t *dev, netopt_t opt, const void *va
* this function is meant top be used by drivers that do not support address
* filtering in hw
*
* @deprecated This function is currently deprecated and will be removed
* after Release 2022.01. Use @ref ieee802154_dst_filter instead.
*
* @param[in] dev network device descriptor
* @param[in] mhr mac header
*

View File

@ -396,6 +396,25 @@ int ieee802154_get_src(const uint8_t *mhr, uint8_t *src, le_uint16_t *src_pan);
*/
int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan);
/**
* @brief Check whether a frame pass the IEEE 802.15.4 frame filter.
*
* A frame passes the frame filter only if:
* - The PAN ID matches the PAN ID of the frame filter or the broadcast PAN ID
* - Either the Short or Extended Address matches the frame filter OR the
* Short Address is the broadcast address.
*
* @param[in] mhr MAC header (PSDU)
* @param[in] pan PAN ID of the frame filter.
* @param[in] short_addr Short Address of the frame filter.
* @param[in] ext_addr Extended Address of the frame filter.
*
* @return 0 if frame passes the frame filter.
* @return 1 if frame doesn't pass the frame filter.
*/
int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan,
network_uint16_t short_addr, const eui64_t *ext_addr);
/**
* @brief Gets sequence number from MAC header.
*

View File

@ -241,4 +241,31 @@ int ieee802154_get_dst(const uint8_t *mhr, uint8_t *dst, le_uint16_t *dst_pan)
return 0;
}
int ieee802154_dst_filter(const uint8_t *mhr, uint16_t pan,
network_uint16_t short_addr, const eui64_t *ext_addr)
{
uint8_t dst_addr[IEEE802154_LONG_ADDRESS_LEN];
le_uint16_t dst_pan;
uint8_t pan_bcast[] = IEEE802154_PANID_BCAST;
int addr_len = ieee802154_get_dst(mhr, dst_addr, &dst_pan);
/* filter PAN ID */
if ((memcmp(pan_bcast, dst_pan.u8, 2) != 0) &&
(memcmp(&pan, dst_pan.u8, 2) != 0)) {
return 1;
}
/* check destination address */
if (((addr_len == IEEE802154_SHORT_ADDRESS_LEN) &&
(memcmp(&short_addr.u8, dst_addr, addr_len) == 0 ||
memcmp(ieee802154_addr_bcast, dst_addr, addr_len) == 0)) ||
((addr_len == IEEE802154_LONG_ADDRESS_LEN) &&
(memcmp(ext_addr->uint8, dst_addr, addr_len) == 0))) {
return 0;
}
return 1;
}
/** @} */

View File

@ -953,6 +953,128 @@ static void test_ieee802154_get_dst_dst8_pancomp(void)
TEST_ASSERT_EQUAL_INT(exp_pan.u16, res_pan.u16);
}
static void test_ieee802154_dst_filter_pan_fail(void)
{
const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16);
const eui64_t long_addr = {.uint64.u64 = TEST_UINT64};
const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1);
const uint8_t mhr[] = { 0,
IEEE802154_FCF_DST_ADDR_SHORT,
TEST_UINT8,
exp_pan.u8[0], exp_pan.u8[1],
exp_addr.u8[1], exp_addr.u8[0] };
TEST_ASSERT_EQUAL_INT(1, ieee802154_dst_filter(mhr,
exp_pan.u16 + 1,
exp_addr,
&long_addr));
}
static void test_ieee802154_dst_filter_short_addr_fail(void)
{
const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16);
const eui64_t long_addr = {.uint64.u64 = TEST_UINT64};
const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1);
const uint8_t mhr[] = { 0,
IEEE802154_FCF_DST_ADDR_SHORT,
TEST_UINT8,
exp_pan.u8[0], exp_pan.u8[1],
exp_addr.u8[1], exp_addr.u8[0] };
TEST_ASSERT_EQUAL_INT(1, ieee802154_dst_filter(mhr,
exp_pan.u16,
byteorder_htons(exp_addr.u16 + 1),
&long_addr));
}
static void test_ieee802154_dst_filter_long_addr_fail(void)
{
const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16);
const eui64_t long_addr = {.uint64.u64 = TEST_UINT64};
const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1);
const eui64_t long_addr_fail = {.uint64.u64 = TEST_UINT64 + 1};
const uint8_t mhr[] = { IEEE802154_FCF_PAN_COMP,
IEEE802154_FCF_DST_ADDR_LONG,
TEST_UINT8,
exp_pan.u8[0], exp_pan.u8[1],
long_addr.uint8[7], long_addr.uint8[6],
long_addr.uint8[5], long_addr.uint8[4],
long_addr.uint8[3], long_addr.uint8[2],
long_addr.uint8[1], long_addr.uint8[0] };
TEST_ASSERT_EQUAL_INT(1, ieee802154_dst_filter(mhr,
exp_pan.u16,
exp_addr,
&long_addr_fail));
}
static void test_ieee802154_dst_filter_pan_short(void)
{
const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16);
const eui64_t long_addr = {.uint64.u64 = TEST_UINT64};
const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1);
const uint8_t mhr[] = { 0,
IEEE802154_FCF_DST_ADDR_SHORT,
TEST_UINT8,
exp_pan.u8[0], exp_pan.u8[1],
exp_addr.u8[1], exp_addr.u8[0] };
TEST_ASSERT_EQUAL_INT(0, ieee802154_dst_filter(mhr,
exp_pan.u16,
exp_addr,
&long_addr));
}
static void test_ieee802154_dst_filter_bcast_short(void)
{
const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16);
const eui64_t long_addr = {.uint64.u64 = TEST_UINT64};
const uint8_t pan_bcast[] = IEEE802154_PANID_BCAST;
const uint8_t mhr[] = { 0,
IEEE802154_FCF_DST_ADDR_SHORT,
TEST_UINT8,
pan_bcast[0], pan_bcast[1],
exp_addr.u8[1], exp_addr.u8[0] };
TEST_ASSERT_EQUAL_INT(0, ieee802154_dst_filter(mhr,
TEST_UINT16,
exp_addr,
&long_addr));
}
static void test_ieee802154_dst_filter_pan_long(void)
{
const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16);
const eui64_t long_addr = {.uint64.u64 = TEST_UINT64};
const le_uint16_t exp_pan = byteorder_htols(TEST_UINT16 + 1);
const uint8_t mhr[] = { IEEE802154_FCF_PAN_COMP,
IEEE802154_FCF_DST_ADDR_LONG,
TEST_UINT8,
exp_pan.u8[0], exp_pan.u8[1],
long_addr.uint8[7], long_addr.uint8[6],
long_addr.uint8[5], long_addr.uint8[4],
long_addr.uint8[3], long_addr.uint8[2],
long_addr.uint8[1], long_addr.uint8[0] };
TEST_ASSERT_EQUAL_INT(0, ieee802154_dst_filter(mhr,
exp_pan.u16,
exp_addr,
&long_addr));
}
static void test_ieee802154_dst_filter_bcast_long(void)
{
const network_uint16_t exp_addr = byteorder_htons(TEST_UINT16);
const eui64_t long_addr = {.uint64.u64 = TEST_UINT64};
const uint8_t pan_bcast[] = IEEE802154_PANID_BCAST;
const uint8_t mhr[] = { IEEE802154_FCF_PAN_COMP,
IEEE802154_FCF_DST_ADDR_LONG,
TEST_UINT8,
pan_bcast[0], pan_bcast[1],
long_addr.uint8[7], long_addr.uint8[6],
long_addr.uint8[5], long_addr.uint8[4],
long_addr.uint8[3], long_addr.uint8[2],
long_addr.uint8[1], long_addr.uint8[0] };
TEST_ASSERT_EQUAL_INT(0, ieee802154_dst_filter(mhr,
TEST_UINT16,
exp_addr,
&long_addr));
}
static void test_ieee802154_get_seq(void)
{
const uint8_t mhr[] = { 0x00, 0x00, TEST_UINT8 };
@ -1088,6 +1210,13 @@ Test *tests_ieee802154_tests(void)
new_TestFixture(test_ieee802154_get_dst_dst2_pancomp),
new_TestFixture(test_ieee802154_get_dst_dst8),
new_TestFixture(test_ieee802154_get_dst_dst8_pancomp),
new_TestFixture(test_ieee802154_dst_filter_pan_fail),
new_TestFixture(test_ieee802154_dst_filter_short_addr_fail),
new_TestFixture(test_ieee802154_dst_filter_long_addr_fail),
new_TestFixture(test_ieee802154_dst_filter_pan_short),
new_TestFixture(test_ieee802154_dst_filter_bcast_short),
new_TestFixture(test_ieee802154_dst_filter_pan_long),
new_TestFixture(test_ieee802154_dst_filter_bcast_long),
new_TestFixture(test_ieee802154_get_seq),
new_TestFixture(test_ieee802154_get_iid_addr_len_0),
new_TestFixture(test_ieee802154_get_iid_addr_len_SIZE_MAX),