345 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			345 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | /****************************************************************************
 | ||
|  | 
 | ||
|  | Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. | ||
|  | 
 | ||
|  | This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT | ||
|  | be copied by any method or incorporated into another program without | ||
|  | the express written consent of Aerospace C.Power. This Information or any portion | ||
|  | thereof remains the property of Aerospace C.Power. The Information contained herein | ||
|  | is believed to be accurate and Aerospace C.Power assumes no responsibility or | ||
|  | liability for its use in any way and conveys no license or title under | ||
|  | any patent or copyright and makes no representation or warranty that this | ||
|  | Information is free from patent or copyright infringement. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | 
 | ||
|  | #ifndef PLC_MAC_HEADER_H
 | ||
|  | #define PLC_MAC_HEADER_H
 | ||
|  | 
 | ||
|  | 
 | ||
|  | /* os shim includes */ | ||
|  | #include "iot_config.h"
 | ||
|  | #include "os_types.h"
 | ||
|  | #include "iot_utils.h"
 | ||
|  | #include "plc_utils.h"
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | /* pack for the structures in the whole file */ | ||
|  | #pragma pack(push)  // save the pack status
 | ||
|  | #pragma pack(1)     // 1 byte align
 | ||
|  | 
 | ||
|  | /* sg mac header version: mac standard frame protocol */ | ||
|  | #define MAC_HEADER_VERSION      0
 | ||
|  | /* sg mac header version: mac single-hop frame protocol */ | ||
|  | #define MAC_HEADER_SHORT_VER    1
 | ||
|  | 
 | ||
|  | // MSDU content type of mac single-hop frame
 | ||
|  | #define MSDU_TYPE_S_DIS_NODE    0
 | ||
|  | #define MSDU_TYPE_S_APP_DATA    128
 | ||
|  | #define MSDU_TYPE_S_IP          129
 | ||
|  | 
 | ||
|  | // MSDU content type
 | ||
|  | #define MSDU_TYPE_MME           0       // MSDU TYPE MME
 | ||
|  | #define MSDU_TYPE_AUTH          1       // MSDU TYPE AUTHENTICATION MESSAGE
 | ||
|  | #define MSDU_TYPE_APP_DATA      48      // MSDU TYPE APP DATA
 | ||
|  | #define MSDU_TYPE_IP            49      // MSDU TYPE IP DATA
 | ||
|  | #define MSDU_TYPE_SILA_NHM      53      // MSDU TYPE SILA NHM
 | ||
|  | 
 | ||
|  | // MSDU send type
 | ||
|  | #define ST_UNICAST              0
 | ||
|  | #define ST_BCAST_NETWORK        1
 | ||
|  | #define ST_BCAST_LOCAL          2
 | ||
|  | #define ST_BCAST_PROXY          3
 | ||
|  | 
 | ||
|  | /* define local broadcast hop */ | ||
|  | #define ST_BCAST_LOCAL_HOP      1
 | ||
|  | /* define connectionless whole network broadcast hop */ | ||
|  | #define ST_BCAST_NETWORK_CL_HOP 5
 | ||
|  | 
 | ||
|  | // broadcast direction
 | ||
|  | #define BCAST_DIR_TWOWAY        0       // 2 way broadcast
 | ||
|  | #define BCAST_DIR_DOWNLINK      1       // downlink broadcast
 | ||
|  | #define BCAST_DIR_UPLINK        2       // uplink broadcast
 | ||
|  | 
 | ||
|  | /* max reboot count */ | ||
|  | #define MAC_HDR_REBOOT_CNT_MAX  15
 | ||
|  | 
 | ||
|  | #define SG_MAC_MSDU_CRC_LEN     4
 | ||
|  | 
 | ||
|  | /* define encryption key type */ | ||
|  | #define ENCRYPT_KEY_TYPE_NONE   0
 | ||
|  | #define ENCRYPT_KEY_TYPE_CMK    1
 | ||
|  | #define ENCRYPT_KEY_TYPE_CEK    2
 | ||
|  | 
 | ||
|  | typedef struct _mac_header { | ||
|  |     uint16_t    version     :   4;      // version of MAC header.
 | ||
|  |     uint16_t    org_src_tei :   12;     // TEI of original source STA
 | ||
|  | 
 | ||
|  |     uint16_t    org_dest_tei:   12;     // TEI of original dest STA
 | ||
|  |     uint16_t    send_type   :   4;      // send type. unicast or broadcast.
 | ||
|  | 
 | ||
|  |     uint8_t     send_max_cnt:   5;      // maxmum times of sending a packet
 | ||
|  |     uint8_t     _reseved1   :   3; | ||
|  | 
 | ||
|  |     uint16_t    msdu_sn;                // MSDU sequence number
 | ||
|  |     uint8_t     msdu_type;              // type of MSDU. see MSDU_TYPE_XXX
 | ||
|  |     uint16_t    msdu_len    :   11;     // length of MSDU
 | ||
|  |     uint16_t    reboot_cnt  :   4;      // reboot count
 | ||
|  |     uint16_t    main_path_flag: 1;      // if main path is used
 | ||
|  | 
 | ||
|  |     uint8_t     total_hop_cnt:  4;      // maxmum hop when delivery a packet
 | ||
|  |     uint8_t     rest_hop_cnt:   4;      // rest hop for a packet
 | ||
|  | 
 | ||
|  |     uint16_t    bc_direction:   2;      // broadcast direction
 | ||
|  |     uint16_t    path_fix_flag : 1;      // path fixing triggered
 | ||
|  |     uint16_t    mac_exist_flag: 1;      // if mac address exist in the end
 | ||
|  |     uint16_t    key_type      : 2;      // encryption key type
 | ||
|  |     uint16_t    cek_seq       : 2;      // communication encryption key sequence
 | ||
|  |     uint16_t    zero_pad_len  : 4; // length of compensation zero for encryption
 | ||
|  |     uint16_t    _reserved2    : 4; | ||
|  | 
 | ||
|  |     uint8_t     network_sn;             // network sequence number
 | ||
|  |     uint16_t    _reserved3; | ||
|  |     uint8_t     mac[0][IOT_MAC_ADDR_LEN];// mac[0] is src. mac[1] is dest MAC.
 | ||
|  | } mac_header_t; | ||
|  | 
 | ||
|  | /* mac short header */ | ||
|  | typedef struct _mac_short_header { | ||
|  |     uint8_t     version     :   4,      // version of MAC header.
 | ||
|  |                 cek_seq     :   2,      // communication encryption key sequence
 | ||
|  |                 key_type    :   2;      // encryption key type
 | ||
|  |     uint8_t     msg_type;               // msg type
 | ||
|  |     uint16_t    msdu_len    :   11,     // length of MSDU
 | ||
|  |                 zero_pad_len:   4, // length of compensation zero for encryption
 | ||
|  |                 resv1       :   1;      // reserved
 | ||
|  | } mac_short_header_t; | ||
|  | 
 | ||
|  | /* SPG define start */ | ||
|  | 
 | ||
|  | /* spg mac header version: mac standard frame protocol */ | ||
|  | #define SPG_MAC_HEADER_VERSION      1
 | ||
|  | /* spg mac header version: mac single-hop frame protocol */ | ||
|  | #define SPG_MAC_HEADER_SHORT_VER    2
 | ||
|  | 
 | ||
|  | // MSDU content type of mac single-hop frame
 | ||
|  | #define SPG_MSDU_TYPE_S_APP_DATA    1
 | ||
|  | #define SPG_MSDU_TYPE_S_DIS_NODE    2
 | ||
|  | #define SPG_MSDU_TYPE_S_IP          128
 | ||
|  | 
 | ||
|  | // MSDU content type
 | ||
|  | #define SPG_MSDU_TYPE_MME           0x88E1       // SPG MSDU TYPE MME
 | ||
|  | #define SPG_MSDU_TYPE_DATA          0x0001       // SPG MSDU TYPE DATA
 | ||
|  | #define SPG_MSDU_SFRM_VT_MAX        0x0003       // SPG short msdu frame header vlan tag maxmum
 | ||
|  | #define SPG_MSDU_LFRM_VT            0x8100
 | ||
|  | 
 | ||
|  | /* MSDU send type, the whole network broadcast is same as the proxy broadcast
 | ||
|  |  * for southern grid | ||
|  |  */ | ||
|  | #define SPG_ST_UNICAST_ACK          0
 | ||
|  | #define SPG_ST_BCAST_NETWORK_NOACK  1
 | ||
|  | #define SPG_ST_BCAST_LOCAL_NOACK    2
 | ||
|  | #define SPG_ST_BCAST_NETWORK_ACK    3
 | ||
|  | #define SPG_ST_BCAST_LOCAL_ACK      4
 | ||
|  | 
 | ||
|  | // broadcast direction
 | ||
|  | #define SPG_BCAST_DIR_DOWNLINK      1       // cco->sta
 | ||
|  | #define SPG_BCAST_DIR_UPLINK        2       // sta->cco
 | ||
|  | 
 | ||
|  | #define SPG_MAC_MSDU_CRC_LEN    4
 | ||
|  | 
 | ||
|  | typedef struct _spg_mac_lheader_tail { | ||
|  |     uint8_t     mac[IOT_MAC_ADDR_LEN];   // mac address
 | ||
|  |     uint8_t     reseved[14];             // 14 Bytes resv for future use
 | ||
|  | } spg_mac_lheader_tail_t; | ||
|  | 
 | ||
|  | typedef struct _spg_mac_header { | ||
|  |     /* type of MAC header, 1 - short, 0 - long */ | ||
|  |     uint16_t    mac_hdr_type       :  1, | ||
|  |     /* version of MAC header */ | ||
|  |                 version            :  2, | ||
|  |     /* reserved */ | ||
|  |                 _reseved1          : 13; | ||
|  |     /* length of msdu */ | ||
|  |     uint16_t    msdu_len; | ||
|  |     /* origin dest TEI */ | ||
|  |     uint32_t    org_dest_tei       : 12, | ||
|  |     /* origin src TEI */ | ||
|  |                 org_src_tei        : 12, | ||
|  |     /* short network identifier, range 1-15 */ | ||
|  |                 snid               :  4, | ||
|  |     /* reboot count */ | ||
|  |                 reboot_cnt         :  4; | ||
|  |     /* route hop count */ | ||
|  |     uint16_t    route_hop_cnt      :  4, | ||
|  |     /* direction of broadcast, see SPG_BCAST_DIR_XXX */ | ||
|  |                 bc_direction       :  4, | ||
|  |     /* send type, see SPG_ST_XXX */ | ||
|  |                 send_type          :  3, | ||
|  |     /* send maxmum count */ | ||
|  |                 send_max_cnt       :  5; | ||
|  |     /* msdu sequence */ | ||
|  |     uint16_t    msdu_sn; | ||
|  |     spg_mac_lheader_tail_t   spg_head_tail[0]; | ||
|  | } spg_mac_header_t; | ||
|  | 
 | ||
|  | /*
 | ||
|  | **SPG MSDU frame struture define | ||
|  | */ | ||
|  | typedef struct _spg_msdu_long_frame { | ||
|  |     uint8_t  org_dst_mac_addr[IOT_MAC_ADDR_LEN]; | ||
|  |     uint8_t  org_src_mac_addr[IOT_MAC_ADDR_LEN]; | ||
|  |     uint32_t vlan_tag; | ||
|  |     uint16_t msdu_type; | ||
|  |     uint8_t  payload[0]; | ||
|  | } spg_msdu_lfrm_t; | ||
|  | 
 | ||
|  | typedef struct _spg_msdu_short_frame { | ||
|  |     uint8_t vlan_tag; | ||
|  |     uint8_t msdu_type; | ||
|  |     uint8_t payload[0]; | ||
|  | } spg_msdu_sfrm_t; | ||
|  | 
 | ||
|  | /* mac short header */ | ||
|  | typedef struct _spg_mac_short_header { | ||
|  |     /* type of MAC header, the field is meaningless */ | ||
|  |     uint8_t     mac_hdr_type       :  1, | ||
|  |     /* version of MAC header, see SPG_MAC_HEADER_XXX */ | ||
|  |                 version            :  2, | ||
|  |     /* reserved */ | ||
|  |                 resv0              :  5; | ||
|  |     /* msg type, see SPG_MSDU_TYPE_S_XXX */ | ||
|  |     uint8_t     msg_type; | ||
|  |     /* length of msdu */ | ||
|  |     uint16_t    msdu_len; | ||
|  | } spg_mac_short_header_t; | ||
|  | 
 | ||
|  | /* SPG define end */ | ||
|  | 
 | ||
|  | #if SUPPORT_GREEN_PHY
 | ||
|  | 
 | ||
|  | #define GP_MAC_MSDU_CRC_LEN    4
 | ||
|  | 
 | ||
|  | typedef union _gp_ats_or_cfder { | ||
|  |     uint32_t ats; | ||
|  |     uint32_t cfder; | ||
|  | }gp_ats_or_cfder_t; | ||
|  | 
 | ||
|  | typedef struct _gp_mac_header { | ||
|  |     uint16_t mft : 2, | ||
|  |         mfl : 14; | ||
|  | } gp_mac_header_t; | ||
|  | 
 | ||
|  | #endif //SUPPORT_GREEN_PHY
 | ||
|  | 
 | ||
|  | /* ieee1901 define start */ | ||
|  | 
 | ||
|  | /* ieee1901 mac header standard protocol version */ | ||
|  | #define I1901_MAC_HDR_STD_VER           0
 | ||
|  | 
 | ||
|  | /* ieee1901 msdu send type */ | ||
|  | #define I1901_ST_UNICAST                0
 | ||
|  | #define I1901_ST_BCAST_NETWORK          1
 | ||
|  | #define I1901_ST_BCAST_LOCAL            2
 | ||
|  | #define I1901_ST_BCAST_PROXY            3
 | ||
|  | #define I1901_ST_BCAST_ADAPTIVE         4
 | ||
|  | 
 | ||
|  | /* ieee1901 MSDU content type */ | ||
|  | #define I1901_MSDU_TYPE_MME             0
 | ||
|  | #define I1901_MSDU_TYPE_APP_DATA        48
 | ||
|  | #define I1901_MSDU_TYPE_IP              49
 | ||
|  | #define I1901_MSDU_TYPE_TEST            50
 | ||
|  | #define I1901_MSDU_TYPE_ETH             51
 | ||
|  | 
 | ||
|  | /* ieee1901 broadcast direction */ | ||
|  | #define I1901_BCAST_DIR_TWOWAY          0   // 2 way broadcast
 | ||
|  | #define I1901_BCAST_DIR_DOWNLINK        1   // downlink broadcast
 | ||
|  | #define I1901_BCAST_DIR_UPLINK          2   // uplink broadcast
 | ||
|  | #define I1901_BCAST_DIR_ADAPTIVE        3   // adaptive relay
 | ||
|  | 
 | ||
|  | /* ieee1901 mac msdu frame  integrity check value */ | ||
|  | #define I1901_MAC_MSDU_CRC_LEN          4
 | ||
|  | 
 | ||
|  | /* ieee1901 mac header relay variant */ | ||
|  | typedef struct _i1901_mac_header_var { | ||
|  |     /* TEI of original source STA */ | ||
|  |     uint32_t    org_src_tei         : 12, | ||
|  |     /* TEI of original dest STA */ | ||
|  |                 org_dest_tei        : 12, | ||
|  |     /* maximum hop count for MAC frame forwarding */ | ||
|  |                 total_hop_cnt       : 4, | ||
|  |     /* remaining forwarding hop count */ | ||
|  |                 rest_hop_cnt        : 4; | ||
|  |     /* disable/enable proxy main path mode */ | ||
|  |     uint8_t     main_path_flag      : 1, | ||
|  | #if PLC_SILA_NHM_ENABLE
 | ||
|  |     /* transmission direction of a broadcast packet */ | ||
|  |                 bc_direction        : 2, | ||
|  |     /* if mac address exist in the end */ | ||
|  |                 mac_exist_flag      : 1, | ||
|  |     /* leave network delay  */ | ||
|  |                 leave_delay         : 4; | ||
|  | #else /* PLC_SILA_NHM_ENABLE */
 | ||
|  |     /* transmission direction of a broadcast packet */ | ||
|  |                 bc_direction        : 1, | ||
|  |                 rsvd                : 6; | ||
|  | #endif /* PLC_SILA_NHM_ENABLE */
 | ||
|  |     /* current networking sequence number */ | ||
|  |     uint8_t     network_sn; | ||
|  | #if PLC_SILA_NHM_ENABLE
 | ||
|  |     /* dumy code */ | ||
|  |     uint16_t     dumy; | ||
|  | #endif
 | ||
|  | } i1901_mac_header_var_t; | ||
|  | 
 | ||
|  | typedef struct _i1901_mac_header { | ||
|  |     /* version of MAC header */ | ||
|  |     uint8_t     version             : 4, | ||
|  |     /* send type. unicast or broadcast, see I1901_ST_XXX */ | ||
|  |                 send_type           : 4; | ||
|  |     /* maxmum times of sending a packet */ | ||
|  |     uint8_t     send_max_cnt        : 5, | ||
|  |     /* reseved */ | ||
|  |                 rsvd                : 3; | ||
|  |     /* MSDU sequence number */ | ||
|  |     uint16_t    msdu_sn; | ||
|  |     /* type of MSDU. see I1901_MSDU_TYPE_XXX */ | ||
|  |     uint8_t     msdu_type; | ||
|  |     /* length of MSDU */ | ||
|  |     uint16_t    msdu_len            : 11, | ||
|  |     /* reset count */ | ||
|  |                 reboot_cnt          : 4, | ||
|  |     /* indicate the type of the mac frame */ | ||
|  |                 mac_frame_type      : 1; | ||
|  | #if PLC_SILA_NHM_ENABLE
 | ||
|  |     /* dumy code */ | ||
|  |     uint8_t     dumy; | ||
|  | #endif
 | ||
|  |     /* mac header relay variant */ | ||
|  |     i1901_mac_header_var_t var[0]; | ||
|  | } i1901_mac_header_t; | ||
|  | 
 | ||
|  | /* ieee1901 define end */ | ||
|  | 
 | ||
|  | #if PLC_SILA_NHM_ENABLE
 | ||
|  | #define i1901_mac_hdr_var_is_valid(mac_hdr)     \
 | ||
|  |     ((((i1901_mac_header_t *)(mac_hdr))->mac_frame_type) == 1) | ||
|  | #define i1901_mac_hdr_var_addr_is_valid(mac_hdr)     \
 | ||
|  |     ((((i1901_mac_header_t *)(mac_hdr))->var[0].mac_exist_flag) == 1) | ||
|  | #else
 | ||
|  | #define i1901_mac_hdr_var_is_valid(mac_hdr)     \
 | ||
|  |     ((((i1901_mac_header_t *)(mac_hdr))->mac_frame_type) == 0) | ||
|  | #define i1901_mac_hdr_var_addr_is_valid(mac_hdr)  0
 | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #pragma pack(pop)   // restore the pack status
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif /* PLC_MAC_HEADER_H */
 | ||
|  | 
 |