@@ -572,7 +572,7 @@ static int8_t mac_data_interface_tx_done_by_ack_cb(protocol_interface_rf_mac_set
572572 return 0 ;
573573}
574574
575- static bool mac_pd_sap_ack_validation (protocol_interface_rf_mac_setup_s * rf_ptr , mac_fcf_sequence_t * fcf_dsn , const uint8_t * data_ptr )
575+ static bool mac_pd_sap_ack_validation (protocol_interface_rf_mac_setup_s * rf_ptr , const mac_fcf_sequence_t * fcf_dsn , const uint8_t * data_ptr )
576576{
577577 if (!rf_ptr -> active_pd_data_request || !rf_ptr -> active_pd_data_request -> fcf_dsn .ackRequested ) {
578578 return false; //No active Data request anymore or no ACK request for current TX
@@ -628,32 +628,32 @@ static bool mac_pd_sap_ack_validation(protocol_interface_rf_mac_setup_s *rf_ptr,
628628 return true;
629629}
630630
631- static int8_t mac_pd_sap_validate_fcf (protocol_interface_rf_mac_setup_s * rf_ptr , mac_fcf_sequence_t fcf_read , arm_pd_sap_generic_ind_t * pd_data_ind )
631+ static int8_t mac_pd_sap_validate_fcf (protocol_interface_rf_mac_setup_s * rf_ptr , const mac_fcf_sequence_t * fcf_read , arm_pd_sap_generic_ind_t * pd_data_ind )
632632{
633- switch (fcf_read . frametype ) {
633+ switch (fcf_read -> frametype ) {
634634 case FC_DATA_FRAME :
635- if (fcf_read . SrcAddrMode == MAC_ADDR_MODE_NONE ) {
635+ if (fcf_read -> SrcAddrMode == MAC_ADDR_MODE_NONE ) {
636636 return -1 ;
637- } else if (fcf_read . DstAddrMode == MAC_ADDR_MODE_NONE && fcf_read . frameVersion != MAC_FRAME_VERSION_2015 ) {
637+ } else if (fcf_read -> DstAddrMode == MAC_ADDR_MODE_NONE && fcf_read -> frameVersion != MAC_FRAME_VERSION_2015 ) {
638638 return -1 ;
639639 }
640640 break ;
641641 case FC_BEACON_FRAME :
642- if (fcf_read . SrcAddrMode == MAC_ADDR_MODE_NONE || fcf_read . DstAddrMode != MAC_ADDR_MODE_NONE ) {
642+ if (fcf_read -> SrcAddrMode == MAC_ADDR_MODE_NONE || fcf_read -> DstAddrMode != MAC_ADDR_MODE_NONE ) {
643643 return -1 ;
644644 }
645645 break ;
646646 case FC_ACK_FRAME :
647647 // Only accept version 2015 Acks
648- if (fcf_read . frameVersion != MAC_FRAME_VERSION_2015 ) {
648+ if (fcf_read -> frameVersion != MAC_FRAME_VERSION_2015 ) {
649649 return -1 ;
650650 }
651651 //Validate Ack doesn't request Ack
652- if (fcf_read . ackRequested ) {
652+ if (fcf_read -> ackRequested ) {
653653 return -1 ;
654654 }
655655 //Validate ACK
656- if (!mac_pd_sap_ack_validation (rf_ptr , & fcf_read , pd_data_ind -> data_ptr )) {
656+ if (!mac_pd_sap_ack_validation (rf_ptr , fcf_read , pd_data_ind -> data_ptr )) {
657657 return -1 ;
658658 }
659659 break ;
@@ -665,150 +665,117 @@ 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 )
668+ static bool mac_pd_sap_panid_filter_common (const uint8_t * mac_header , const mac_fcf_sequence_t * fcf_read , uint16_t own_pan_id )
669669{
670- // PHY driver shouldn't drop received Beacon frames as they might be used by load balancing
671- if (frame_type == MAC_FRAME_BEACON ) {
670+ // Beacon frames shouldn't be dropped as they might be used by load balancing
671+ if (fcf_read -> frametype == MAC_FRAME_BEACON ) {
672672 return true;
673673 }
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- }
674+ if (own_pan_id == 0xffff ) {
675+ return true;
676+ }
677+ uint16_t dst_pan_id = mac_header_get_dst_panid (fcf_read , mac_header , 0xffff );
678+ if (dst_pan_id == 0xffff ) {
679+ return true;
680+ }
681+ if (own_pan_id == dst_pan_id ) {
682+ return true;
688683 }
689- return retval ;
684+ return false ;
690685}
691686
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 )
687+ static bool mac_pd_sap_panid_v2_filter (const uint8_t * ptr , const mac_fcf_sequence_t * fcf_read , uint16_t pan_id )
693688{
694- if ((dst_mode == MAC_FCF_DST_ADDR_NONE ) && (frame_type == FC_DATA_FRAME || frame_type == FC_CMD_FRAME )) {
689+ if ((fcf_read -> DstAddrMode == MAC_ADDR_MODE_NONE ) && (fcf_read -> frametype == FC_DATA_FRAME || fcf_read -> frametype == FC_CMD_FRAME )) {
695690 return true;
696691 }
697- if ((dst_mode == MAC_FCF_DST_64_BITS ) && (src_mode == MAC_FCF_SRC_64_BITS ) && panid_compressed ) {
692+ if ((fcf_read -> DstAddrMode == MAC_ADDR_MODE_64_BIT ) && (fcf_read -> SrcAddrMode == MAC_ADDR_MODE_64_BIT ) && fcf_read -> intraPan ) {
698693 return true;
699694 }
700- if (seq_compressed ) {
701- ptr -- ;
702- }
703- return mac_pd_sap_panid_filter_common (ptr , pan_id , frame_type );
695+ return mac_pd_sap_panid_filter_common (ptr , fcf_read , pan_id );
704696}
705697
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 )
698+ static bool mac_pd_sap_addr_filter_common (const uint8_t * mac_header , const mac_fcf_sequence_t * fcf_read , uint8_t * mac_64bit_addr , uint16_t mac_16bit_addr )
707699{
708700 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- }
701+ uint8_t dst_addr [8 ];
702+ mac_header_get_dst_address (fcf_read , mac_header , dst_addr );
719703
720- if (!memcmp ((uint8_t * )mac_16bit_addr , (uint8_t * ) cmp_table , 2 )) {
721- retval = true;
722- }
704+ switch (fcf_read -> DstAddrMode ) {
705+ case MAC_ADDR_MODE_16_BIT :
706+ if (!memcmp (dst_addr , cmp_table , 2 )) {
707+ return true;
708+ }
709+ uint8_t temp [2 ];
710+ common_write_16_bit (mac_16bit_addr , temp );
711+ if (!memcmp (temp , dst_addr , 2 )) {
712+ return true;
723713 }
724714 break ;
725- case MAC_FCF_DST_64_BITS :
726- if (memcmp (( uint8_t * ) ptr , ( uint8_t * ) cmp_table , 8 )) {
727- retval = false ;
715+ case MAC_ADDR_MODE_64_BIT :
716+ if (! memcmp (dst_addr , cmp_table , 8 )) {
717+ return true ;
728718 }
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- }
719+ if (!memcmp (mac_64bit_addr , dst_addr , 8 )) {
720+ return true;
737721 }
738722 break ;
739- case MAC_FCF_DST_ADDR_NONE :
740- retval = true;
723+ case MAC_ADDR_MODE_NONE :
724+ return true;
741725 break ;
742726 default :
743- retval = false;
744727 break ;
745728 }
746- return retval ;
729+ return false ;
747730}
748731
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 )
732+ static bool mac_pd_sap_addr_v2_filter (const uint8_t * mac_header , const mac_fcf_sequence_t * fcf_read , uint8_t * mac_64bit_addr , uint16_t mac_16bit_addr )
750733{
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 );
734+ return mac_pd_sap_addr_filter_common (mac_header , fcf_read , mac_64bit_addr , mac_16bit_addr );
758735}
759736
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 )
737+ static bool mac_pd_sap_rx_filter (const uint8_t * mac_header , const mac_fcf_sequence_t * fcf_read , uint8_t phy_filter_mask , uint8_t * mac_64bit_addr , uint16_t mac_16bit_addr , uint16_t pan_id )
761738{
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 );
739+ uint8_t version = fcf_read -> frameVersion ;
740+
768741 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 )) {
742+ if (!mac_pd_sap_panid_v2_filter (mac_header , fcf_read , pan_id )) {
772743 return false;
773744 }
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 )) {
745+ if (!mac_pd_sap_addr_v2_filter (mac_header , fcf_read , mac_64bit_addr , mac_16bit_addr )) {
776746 return false;
777747 }
778748 }
779749 return true;
780750}
781751
782- 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 )
752+ static int8_t mac_pd_sap_generate_ack (protocol_interface_rf_mac_setup_s * rf_ptr , const mac_fcf_sequence_t * fcf_read , arm_pd_sap_generic_ind_t * pd_data_ind )
783753{
784- //Generate ACK when Extension is enabled and ACK is requested
785- if (rf_ptr -> mac_extension_enabled && fcf_read .ackRequested && fcf_read .frameVersion == MAC_FRAME_VERSION_2015 ) {
786- //SEND ACK here
787- if (rf_ptr -> mac_ack_tx_active ) {
788- return -1 ;
789- }
754+ //Generate ACK when Extension is enabled and ACK is requested only for version 2 frames.
755+ if (!rf_ptr -> mac_extension_enabled || !fcf_read -> ackRequested || (fcf_read -> frameVersion != MAC_FRAME_VERSION_2015 )) {
756+ return 0 ;
757+ }
758+ if (rf_ptr -> mac_ack_tx_active ) {
759+ return -1 ;
760+ }
790761
791- mcps_ack_data_payload_t ack_payload ;
792- mac_api_t * mac_api = get_sw_mac_api (rf_ptr );
793- mac_api -> enhanced_ack_data_req_cb (mac_api , & ack_payload , pd_data_ind -> dbm , pd_data_ind -> link_quality );
794- //Calculate Delta time
762+ mcps_ack_data_payload_t ack_payload ;
763+ mac_api_t * mac_api = get_sw_mac_api (rf_ptr );
764+ mac_api -> enhanced_ack_data_req_cb (mac_api , & ack_payload , pd_data_ind -> dbm , pd_data_ind -> link_quality );
765+ //Calculate Delta time
795766
796- if (mcps_generic_ack_build (rf_ptr , & fcf_read , pd_data_ind -> data_ptr , & ack_payload ) != 0 ) {
797- return -1 ;
798- }
799- }
800- return 0 ;
767+ return mcps_generic_ack_build (rf_ptr , fcf_read , pd_data_ind -> data_ptr , & ack_payload );
801768}
802769
803- static mac_pre_parsed_frame_t * mac_pd_sap_allocate_receive_buffer (protocol_interface_rf_mac_setup_s * rf_ptr , mac_fcf_sequence_t fcf_read , arm_pd_sap_generic_ind_t * pd_data_ind )
770+ static mac_pre_parsed_frame_t * mac_pd_sap_allocate_receive_buffer (protocol_interface_rf_mac_setup_s * rf_ptr , const mac_fcf_sequence_t * fcf_read , arm_pd_sap_generic_ind_t * pd_data_ind )
804771{
805772 mac_pre_parsed_frame_t * buffer = mcps_sap_pre_parsed_frame_buffer_get (pd_data_ind -> data_ptr , pd_data_ind -> data_len );
806773 if (!buffer ) {
807774 return NULL ;
808775 }
809776
810777 //Copy Pre Parsed values
811- buffer -> fcf_dsn = fcf_read ;
778+ buffer -> fcf_dsn = * fcf_read ;
812779 buffer -> timestamp = mac_pd_sap_get_phy_rx_time (rf_ptr );
813780 buffer -> ack_pendinfg_status = mac_data_interface_read_last_ack_pending_status (rf_ptr );
814781 /* Set default flags */
@@ -896,21 +863,22 @@ int8_t mac_pd_sap_data_cb(void *identifier, arm_phy_sap_msg_t *message)
896863 if (pd_data_ind -> data_len < 3 ) {
897864 return -1 ;
898865 }
866+
899867 mac_fcf_sequence_t fcf_read ;
900868 const uint8_t * ptr = mac_header_parse_fcf_dsn (& fcf_read , pd_data_ind -> data_ptr );
901869
902- mac_pre_parsed_frame_t * buffer = mac_pd_sap_allocate_receive_buffer (rf_ptr , fcf_read , pd_data_ind );
870+ mac_pre_parsed_frame_t * buffer = mac_pd_sap_allocate_receive_buffer (rf_ptr , & fcf_read , pd_data_ind );
903871 if (buffer && mac_filter_modify_link_quality (rf_ptr -> mac_interface_id , buffer ) == 1 ) {
904872 goto ERROR_HANDLER ;
905873 }
906874 if (!rf_ptr -> macProminousMode ) {
907- if (mac_pd_sap_validate_fcf (rf_ptr , fcf_read , pd_data_ind )) {
875+ if (mac_pd_sap_validate_fcf (rf_ptr , & fcf_read , pd_data_ind )) {
908876 goto ERROR_HANDLER ;
909877 }
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 )) {
878+ if (!mac_pd_sap_rx_filter (pd_data_ind -> data_ptr , & fcf_read , rf_ptr -> mac_frame_filters , rf_ptr -> mac64 , rf_ptr -> mac_short_address , rf_ptr -> pan_id )) {
911879 goto ERROR_HANDLER ;
912880 }
913- if (mac_pd_sap_generate_ack (rf_ptr , fcf_read , pd_data_ind )) {
881+ if (mac_pd_sap_generate_ack (rf_ptr , & fcf_read , pd_data_ind )) {
914882 goto ERROR_HANDLER ;
915883 }
916884 if (buffer ) {
0 commit comments