225 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			225 lines
		
	
	
		
			6.6 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 MAC_PEER_H
							 | 
						||
| 
								 | 
							
								#define MAC_PEER_H
							 | 
						||
| 
								 | 
							
								#include "plc_utils.h"
							 | 
						||
| 
								 | 
							
								#include "mac_vdev.h"
							 | 
						||
| 
								 | 
							
								#include "iot_crc.h"
							 | 
						||
| 
								 | 
							
								#include "iot_queue.h"
							 | 
						||
| 
								 | 
							
								#include "iot_config.h"
							 | 
						||
| 
								 | 
							
								#include "iot_bitmap_api.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								extern "C" {
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#if PLC_SUPPORT_CCO_ROLE
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#if RUN_IN_PSRAM
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define peer_tbl_sz             (1 << 8)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#else /* RUN_IN_PSRAM */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define peer_tbl_sz             (1 << 6)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif /* RUN_IN_PSRAM */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#else /* PLC_SUPPORT_CCO_ROLE */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define peer_tbl_sz             (1 << 6)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif /* PLC_SUPPORT_CCO_ROLE */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define MAC_CONN_TYPE_CONNLESS 0
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define DEL_ALL_STREAM           0
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define DEL_TX_STREAM            1
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define DEL_RX_STREAM            2
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* mac tei bitmap length in bytes */
							 | 
						||
| 
								 | 
							
								#define MAC_TEI_MAP_BYTE_LEN     ((PLC_TEI_MAX_NUM + 8) / 8)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* mac tei bitmap type defination */
							 | 
						||
| 
								 | 
							
								typedef struct _mac_tei_map_t {
							 | 
						||
| 
								 | 
							
								    uint8_t map[MAC_TEI_MAP_BYTE_LEN];
							 | 
						||
| 
								 | 
							
								} mac_tei_map_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _rate_info {
							 | 
						||
| 
								 | 
							
								    uint32_t last_rx_ts;
							 | 
						||
| 
								 | 
							
								    uint16_t rate_idx : 8, /* current rate idx, max 255 rates */
							 | 
						||
| 
								 | 
							
								        con_no_sack_cnt : 4, /* cnt not receiving sack */
							 | 
						||
| 
								 | 
							
								        con_sack_err_cnt : 4; /* cnt no one pb rx correct */
							 | 
						||
| 
								 | 
							
								    int8_t avg_snr; /* snr for current rate */
							 | 
						||
| 
								 | 
							
								    uint8_t tx_power;
							 | 
						||
| 
								 | 
							
								} rate_info_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef struct _mac_peer {
							 | 
						||
| 
								 | 
							
								    struct _mac_peer *next;
							 | 
						||
| 
								 | 
							
								    iot_list_head_t vdev_node;
							 | 
						||
| 
								 | 
							
								    /* stream list */
							 | 
						||
| 
								 | 
							
								    iot_single_list_head_t *data_stream_list;
							 | 
						||
| 
								 | 
							
								    /* rate info */
							 | 
						||
| 
								 | 
							
								    rate_info_t rate_info;
							 | 
						||
| 
								 | 
							
								#if HPLC_RF_DEV_SUPPORT
							 | 
						||
| 
								 | 
							
								    rate_info_t rf_rate_info;
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								    /* dtei, stei if self peer */
							 | 
						||
| 
								 | 
							
								    uint16_t tei            :12,
							 | 
						||
| 
								 | 
							
								             is_self_peer   :1,
							 | 
						||
| 
								 | 
							
								             is_bcast_peer  :1,
							 | 
						||
| 
								 | 
							
								             is_sub_peer    :1,
							 | 
						||
| 
								 | 
							
								             is_proxy_peer  :1;
							 | 
						||
| 
								 | 
							
								    /* pdev id */
							 | 
						||
| 
								 | 
							
								    pdevid_t pdev_id;
							 | 
						||
| 
								 | 
							
								    /* vdev id */
							 | 
						||
| 
								 | 
							
								    vdevid_t vdev_id;
							 | 
						||
| 
								 | 
							
								} mac_peer_t;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								extern mac_peer_t *peer_hash_tbl[peer_tbl_sz];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * API
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* peer table related */
							 | 
						||
| 
								 | 
							
								#define peer_get_hash(input) \
							 | 
						||
| 
								 | 
							
								    (iot_getcrc8((uint8_t*)&(input), sizeof(input)) & (peer_tbl_sz - 1))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* @return peer address if found
							 | 
						||
| 
								 | 
							
								 *            NULL if not found
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								#define get_peer_tbl_entry(tei) \
							 | 
						||
| 
								 | 
							
								    peer_hash_tbl[peer_get_hash(tei)]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define get_peer_tbl_entry_addr(tei) \
							 | 
						||
| 
								 | 
							
								    &peer_hash_tbl[peer_get_hash(tei)]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								 /* @return peer address if found
							 | 
						||
| 
								 | 
							
								 *            NULL if not found
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								mac_peer_t * find_peer(mac_vdev_t *vdev, \
							 | 
						||
| 
								 | 
							
								    nid_t nid, tei_t tei);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* @return - 0 for successful
							 | 
						||
| 
								 | 
							
								 *           other for failed
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t peer_hash_tbl_add(mac_peer_t *peer);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * @return - 0 for successful
							 | 
						||
| 
								 | 
							
								 *           other for failed
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t mac_peer_alloc(mac_vdev_t *vdev, tei_t tei,
							 | 
						||
| 
								 | 
							
								    uint32_t is_self, uint32_t is_direct_conn, \
							 | 
						||
| 
								 | 
							
								    uint32_t is_sub_peer, uint32_t is_proxy_peer, mac_peer_t **peer);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* init peer
							 | 
						||
| 
								 | 
							
								 * peer (IN) - ptr of peer that already allocated
							 | 
						||
| 
								 | 
							
								 * vdev_ptr (IN) - ptr of vdev that the peer refered
							 | 
						||
| 
								 | 
							
								 * is_self_peer (IN) - if the peer is self peer
							 | 
						||
| 
								 | 
							
								 * is_direct_conn (IN) - if the peer is direct connected
							 | 
						||
| 
								 | 
							
								 * return 0 for success, other for error
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								uint32_t mac_peer_init(mac_peer_t *peer,
							 | 
						||
| 
								 | 
							
								    void *vdev_ptr, uint32_t is_self_peer,
							 | 
						||
| 
								 | 
							
								    uint32_t is_direct_conn, tei_t tei);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* return 0 for success, else for failed */
							 | 
						||
| 
								 | 
							
								uint32_t peer_add_stream(mac_peer_t *peer, void *stream);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint32_t peer_del_stream(mac_peer_t *peer, void *stream);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								* @breif peer_del_all_stream() -    del stream
							 | 
						||
| 
								 | 
							
								* @param mac_peer_t *peer           [the pointer of the peer]
							 | 
						||
| 
								 | 
							
								* @param uint32_t del_tx_rx_stream  [0: all stream, 1: tx stream, 2: rx stream]
							 | 
						||
| 
								 | 
							
								* @return uint32                    [free stream cnt]
							 | 
						||
| 
								 | 
							
								*/
							 | 
						||
| 
								 | 
							
								uint32_t peer_del_all_stream(mac_peer_t *peer, uint32_t del_tx_rx_stream);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint32_t mac_peer_free(mac_vdev_t *vdev, mac_peer_t *peer);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint32_t is_peer_empty(mac_peer_t *peer);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint32_t mac_peer_del_temp_peer(mac_peer_t *peer);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								* @breif mac_peer_rx_ts_update() - updata peer rx ts
							 | 
						||
| 
								 | 
							
								* @param mac_peer_t *peer           [the pointer of the peer]
							 | 
						||
| 
								 | 
							
								* @param uint32_t cur_ts            [the current os timestamp]
							 | 
						||
| 
								 | 
							
								* @return uint32                    [0]
							 | 
						||
| 
								 | 
							
								*/
							 | 
						||
| 
								 | 
							
								uint32_t mac_peer_rx_ts_update(mac_peer_t *peer, uint32_t cur_ts);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								* @breif mac_peer_del_overflow_peer - del overflow peer
							 | 
						||
| 
								 | 
							
								* @param mac_peer_t *peer           [which peer to del]
							 | 
						||
| 
								 | 
							
								* @return del peer success return ERR_OK, else ERR_FAIL
							 | 
						||
| 
								 | 
							
								*/
							 | 
						||
| 
								 | 
							
								uint32_t mac_peer_del_overflow_peer(mac_peer_t *peer);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/* mac convert tei to bitmap index */
							 | 
						||
| 
								 | 
							
								#define MAC_TEI_TO_BM(tei)      (tei + 1)
							 | 
						||
| 
								 | 
							
								/* mac convert bitmap index to tei */
							 | 
						||
| 
								 | 
							
								#define MAC_BM_TO_TEI(bm)       (bm - 1)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * @brief mac_tei_map_reset() - mac tei bitmap reset
							 | 
						||
| 
								 | 
							
								 * @param bm                    tei bitmap to reset
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								#define mac_tei_map_reset(bm)                                          \
							 | 
						||
| 
								 | 
							
								    if (bm) {                                                          \
							 | 
						||
| 
								 | 
							
								        os_mem_set((bm), 0, sizeof(*(bm)));                            \
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * @breif mac_tei_map_set() - set mac tei bitmap from parameter tei
							 | 
						||
| 
								 | 
							
								 * @param bm                  which bitmap to set bit
							 | 
						||
| 
								 | 
							
								 * @param tei                 which tei as index to set bit
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								#define mac_tei_map_set(bm, tei) \
							 | 
						||
| 
								 | 
							
								    if (bm) {                                                          \
							 | 
						||
| 
								 | 
							
								        iot_bitmap_set((bm)->map, sizeof(*(bm)), MAC_TEI_TO_BM(tei));  \
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * @breif mac_tei_map_is_set() - check mac bitmap of parameter tei is set
							 | 
						||
| 
								 | 
							
								 * @param bm                     which bitmap to check
							 | 
						||
| 
								 | 
							
								 * @param tei                    which tei as index to check
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								#define mac_tei_map_is_set(bm, tei)                                    \
							 | 
						||
| 
								 | 
							
								    ((bm) ? iot_bitmap_is_set((bm)->map, sizeof(*(bm)),                \
							 | 
						||
| 
								 | 
							
								    MAC_TEI_TO_BM(tei)) : 0)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * @breif mac_tei_map_clear() - clear mac tei bitmap from parameter tei
							 | 
						||
| 
								 | 
							
								 * @param bm                    which bitmap to clear bit
							 | 
						||
| 
								 | 
							
								 * @param tei                   which tei as index to clear bit
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								#define mac_tei_map_clear(bm, tei)                                     \
							 | 
						||
| 
								 | 
							
								    if (bm) {                                                          \
							 | 
						||
| 
								 | 
							
								        iot_bitmap_clear((bm)->map, sizeof(*(bm)), MAC_TEI_TO_BM(tei));\
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifdef __cplusplus
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#endif // !MAC_PEER_H
							 |