Skip to content

Commit 78c4ae9

Browse files
author
Jarkko Paso
committed
MAC: Added address and pan id filtering for 802.15.4-2015
1 parent 30bce91 commit 78c4ae9

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

nanostack/platform/arm_hal_phy.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ typedef enum {
5454
PHY_LINK_CCA_PREPARE, /**< Prepare for CCA after CSMA-CA: changes to CCA channel and gives permission to TX. See PHY_LINK_CCA_PREPARE status definitions for return values */
5555
} phy_link_tx_status_e;
5656

57+
/** MAC filtering modes. Set corresponding bit to 1 (1 << MAC_FRAME_VERSION_X) in PHY_EXTENSION_FILTERING_SUPPORT request when PHY can handle the filtering of this frame type.
58+
* NOTE: Currently MAC supports filtering and Acking only 802.15.4-2015 frames. Any other frame version must be filtered and Acked by PHY with either HW or SW solution. */
59+
typedef enum {
60+
MAC_FRAME_VERSION_2 = 2 /**< 802.15.4-2015 */
61+
} phy_link_filters_e;
62+
5763
/** Extension types */
5864
typedef enum {
5965
PHY_EXTENSION_CTRL_PENDING_BIT, /**< Control MAC pending bit for indirect data. */
@@ -70,7 +76,8 @@ typedef enum {
7076
PHY_EXTENSION_GET_TIMESTAMP, /**< Read 32-bit constant monotonic time stamp in us */
7177
PHY_EXTENSION_SET_CSMA_PARAMETERS, /**< CSMA parameter's are given by phy_csma_params_t structure remember type cast uint8_t pointer to structure type*/
7278
PHY_EXTENSION_GET_SYMBOLS_PER_SECOND, /**< Read Symbols per seconds which will help to convert symbol time to real time */
73-
PHY_EXTENSION_SET_RF_CONFIGURATION /**< Set RF configuration using phy_rf_channel_parameters_s structure */
79+
PHY_EXTENSION_SET_RF_CONFIGURATION, /**< Set RF configuration using phy_rf_channel_parameters_s structure */
80+
PHY_EXTENSION_FILTERING_SUPPORT /**< Return filtering modes that can be supported by the PHY driver. See phy_link_filters_e */
7481
} phy_extension_type_e;
7582

7683
/** Address types */

source/MAC/IEEE802_15_4/mac_defines.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ typedef struct protocol_interface_rf_mac_setup {
192192
unsigned macCurrentBE: 4;
193193
uint8_t macMaxCSMABackoffs;
194194
uint8_t backoff_period_in_10us; // max 2550us - it's 320us for standard 250kbps
195+
uint8_t mac_frame_filters;
195196
/* MAC channel parameters */
196197
channel_list_s mac_channel_list;
197198
uint8_t scan_duration; //Needed???
@@ -294,6 +295,21 @@ typedef struct protocol_interface_rf_mac_setup {
294295
#define MAC_FCF_SRC_ADDR_MASK 0xc000
295296
#define MAC_FCF_SRC_ADDR_SHIFT 14
296297

298+
#define MAC_FCF_DST_MODE 0x0C
299+
#define MAC_FCF_DST_ADDR_NONE 0x00
300+
#define MAC_FCF_DST_16_BITS 0x08
301+
#define MAC_FCF_DST_64_BITS 0x0C
302+
#define MAC_FCF_SRC_MODE 0xC0
303+
#define MAC_FCF_SEQUENCE_COMPRESSION 0x01
304+
#define MAC_FCF_PAN_ID_COMPRESSION 0x40
305+
#define MAC_FCF_SRC_64_BITS 0xC0
306+
#define SHIFT_SEQ_COMP_FIELD (0)
307+
#define SHIFT_VERSION_FIELD (4)
308+
#define SHIFT_PANID_COMP_FIELD (6)
309+
#define VERSION_FIELD_MASK 0x30
310+
#define OFFSET_DST_PAN_ID (3)
311+
#define OFFSET_DST_ADDR (5)
312+
297313
/* MAC supported frame types */
298314
#define FC_BEACON_FRAME 0x00
299315
#define FC_DATA_FRAME 0x01

source/MAC/IEEE802_15_4/mac_mlme.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,10 @@ protocol_interface_rf_mac_setup_s *mac_mlme_data_base_allocate(uint8_t *mac64, a
11541154
bool rf_support = false;
11551155
dev_driver->phy_driver->extension(PHY_EXTENSION_DYNAMIC_RF_SUPPORTED, (uint8_t *)&rf_support);
11561156
entry->rf_csma_extension_supported = rf_support;
1157+
dev_driver->phy_driver->extension(PHY_EXTENSION_FILTERING_SUPPORT, (uint8_t *)&entry->mac_frame_filters);
1158+
if (entry->mac_frame_filters & (1 << MAC_FRAME_VERSION_2)) {
1159+
tr_debug("PHY supports 802.15.4-2015 frame filtering");
1160+
}
11571161
mac_mlme_set_symbol_rate(entry);
11581162

11591163
//How many 10us ticks backoff period is for waiting 20symbols which is typically 10 bytes time

source/MAC/IEEE802_15_4/mac_pd_sap.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,120 @@ static int8_t mac_pd_sap_validate_fcf(protocol_interface_rf_mac_setup_s *rf_ptr,
665665
return 0;
666666
}
667667

668+
static bool mac_pd_sap_panid_filter_common(const uint8_t *panid_start, uint8_t *pan_id, uint8_t frame_type)
669+
{
670+
// PHY driver shouldn't drop received Beacon frames as they might be used by load balancing
671+
if (frame_type == MAC_FRAME_BEACON) {
672+
return true;
673+
}
674+
bool retval = true;
675+
uint8_t cmp_table[2] = {0xff, 0xff};
676+
if (!(pan_id[0] == 0xff && pan_id[1] == 0xff)) {
677+
if (memcmp((uint8_t *)panid_start, (uint8_t *) cmp_table, 2)) {
678+
retval = false;
679+
}
680+
if (!retval) {
681+
for (uint8_t i = 0; i < 2; i++) {
682+
cmp_table[1 - i] = panid_start[i];
683+
}
684+
if (!memcmp(pan_id, cmp_table, 2)) {
685+
retval = true;
686+
}
687+
}
688+
}
689+
return retval;
690+
}
691+
692+
static bool mac_pd_sap_panid_v2_filter(const uint8_t *ptr, uint8_t *pan_id, uint8_t dst_mode, uint8_t src_mode, uint8_t seq_compressed, uint8_t panid_compressed, uint8_t frame_type)
693+
{
694+
if ((dst_mode == MAC_FCF_DST_ADDR_NONE) && (frame_type == FC_DATA_FRAME || frame_type == FC_CMD_FRAME)) {
695+
return true;
696+
}
697+
if ((dst_mode == MAC_FCF_DST_64_BITS) && (src_mode == MAC_FCF_SRC_64_BITS) && panid_compressed) {
698+
return true;
699+
}
700+
if (seq_compressed) {
701+
ptr--;
702+
}
703+
return mac_pd_sap_panid_filter_common(ptr, pan_id, frame_type);
704+
}
705+
706+
static bool mac_pd_sap_addr_filter_common(const uint8_t *ptr, uint8_t addr_mode, uint8_t *mac_64bit_addr, uint8_t *mac_16bit_addr)
707+
{
708+
uint8_t cmp_table[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
709+
bool retval = true;
710+
switch (addr_mode) {
711+
case MAC_FCF_DST_16_BITS:
712+
if (memcmp((uint8_t *)ptr, (uint8_t *) cmp_table, 2)) {
713+
retval = false;
714+
}
715+
if (!retval) {
716+
for (uint8_t i = 0; i < 2; i++) {
717+
cmp_table[1 - i] = ptr[i];
718+
}
719+
720+
if (!memcmp((uint8_t *)mac_16bit_addr, (uint8_t *) cmp_table, 2)) {
721+
retval = true;
722+
}
723+
}
724+
break;
725+
case MAC_FCF_DST_64_BITS:
726+
if (memcmp((uint8_t *)ptr, (uint8_t *) cmp_table, 8)) {
727+
retval = false;
728+
}
729+
if (!retval) {
730+
for (uint8_t i = 0; i < 8; i++) {
731+
cmp_table[7 - i] = ptr[i];
732+
}
733+
734+
if (!memcmp((uint8_t *)mac_64bit_addr, (uint8_t *) cmp_table, 8)) {
735+
retval = true;
736+
}
737+
}
738+
break;
739+
case MAC_FCF_DST_ADDR_NONE:
740+
retval = true;
741+
break;
742+
default:
743+
retval = false;
744+
break;
745+
}
746+
return retval;
747+
}
748+
749+
static bool mac_pd_sap_addr_v2_filter(const uint8_t *ptr, uint8_t *mac_64bit_addr, uint8_t *mac_16bit_addr, uint8_t dst_mode, uint8_t seq_compressed, uint8_t panid_compressed)
750+
{
751+
if (seq_compressed) {
752+
ptr--;
753+
}
754+
if (panid_compressed) {
755+
ptr -= 2;
756+
}
757+
return mac_pd_sap_addr_filter_common(ptr, dst_mode, mac_64bit_addr, mac_16bit_addr);
758+
}
759+
760+
static bool mac_pd_sap_rx_filter(const uint8_t *mac_header, uint8_t phy_filter_mask, uint8_t *mac_64bit_addr, uint16_t mac_16bit_addr, uint16_t pan_id)
761+
{
762+
uint8_t dst_mode = (mac_header[1] & MAC_FCF_DST_MODE);
763+
uint8_t src_mode = (mac_header[1] & MAC_FCF_SRC_MODE);
764+
uint8_t seq_compressed = ((mac_header[1] & MAC_FCF_SEQUENCE_COMPRESSION) >> SHIFT_SEQ_COMP_FIELD);
765+
uint8_t panid_compressed = ((mac_header[0] & MAC_FCF_PAN_ID_COMPRESSION) >> SHIFT_PANID_COMP_FIELD);
766+
uint8_t frame_type = mac_header[0] & MAC_FCF_FRAME_TYPE_MASK;
767+
uint8_t version = ((mac_header[1] & VERSION_FIELD_MASK) >> SHIFT_VERSION_FIELD);
768+
if (version == MAC_FRAME_VERSION_2015 && !(phy_filter_mask & (1 << MAC_FRAME_VERSION_2))) {
769+
uint8_t temp[2];
770+
common_write_16_bit(pan_id, temp);
771+
if (!mac_pd_sap_panid_v2_filter(mac_header + OFFSET_DST_PAN_ID, temp, dst_mode, src_mode, seq_compressed, panid_compressed, frame_type)) {
772+
return false;
773+
}
774+
common_write_16_bit(mac_16bit_addr, temp);
775+
if (!mac_pd_sap_addr_v2_filter(mac_header + OFFSET_DST_ADDR, mac_64bit_addr, temp, dst_mode, seq_compressed, panid_compressed)) {
776+
return false;
777+
}
778+
}
779+
return true;
780+
}
781+
668782
static int8_t mac_pd_sap_generate_ack(protocol_interface_rf_mac_setup_s *rf_ptr, mac_fcf_sequence_t fcf_read, arm_pd_sap_generic_ind_t *pd_data_ind)
669783
{
670784
//Generate ACK when Extension is enabled and ACK is requested
@@ -793,6 +907,9 @@ int8_t mac_pd_sap_data_cb(void *identifier, arm_phy_sap_msg_t *message)
793907
if (mac_pd_sap_validate_fcf(rf_ptr, fcf_read, pd_data_ind)) {
794908
goto ERROR_HANDLER;
795909
}
910+
if (!mac_pd_sap_rx_filter(pd_data_ind->data_ptr, rf_ptr->mac_frame_filters, rf_ptr->mac64, rf_ptr->mac_short_address, rf_ptr->pan_id)) {
911+
goto ERROR_HANDLER;
912+
}
796913
if (mac_pd_sap_generate_ack(rf_ptr, fcf_read, pd_data_ind)) {
797914
goto ERROR_HANDLER;
798915
}

0 commit comments

Comments
 (0)