/**
 * \file    prl_rx_state.h
 * \brief   Protocol - USB Type-C Port Manager
 *
 * \par     Copyright (c) 2017 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2017-02-03
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2017-02-03  Original Creation
 * 
 */
 
#ifndef __PRL_RX_STATE_H_
#define __PRL_RX_STATE_H_


#include "types.h"
#include "usbpd_timer_defs.h"
#include "prl_tcpm.h"


#pragma pack(1)

/** \name Type-C Protocol Rx State definitions
 * \{ 
 */
#define PRL_RX_WAIT_FOR_PHY_MSG         0x00
#define PRL_RX_CHECK_MSG_TYPE           0x01
#define PRL_RX_LAYER_RESET_FOR_RECEIVE  0x02
#define PRL_RX_CHECK_MSG_ID             0x03
#define PRL_RX_STORE_MSG_ID             0x04
#define PRL_RX_HR_RESET_LAYER           0x05
/** \}*/

#define RCH_Wait_For_Message_From_Protocol_Layer            0x00
#define RCH_Pass_Up_Message                                 0x01
#define RCH_Processing_Extended_Message                     0x02
#define RCH_Requesting_Chunk                                0x03
#define RCH_Waiting_Chunk                                   0x04
#define RCH_Report_Error                                    0x05

#pragma pack()


/** Get current protocol rx state */
#define prl_rx_state_get() \
    (g_prl_state_info.rx_state)

/** Set current protocol rx state */
#define prl_rx_state_set(vL) \
    do { \
        g_prl_state_info.rx_state = vL; \
    } while(0)

/** Get current protocol rx state */
#define prl_rx_chunk_state_get() \
        (g_prl_state_info.chunk_rx_state)
    
/** Set current protocol rx state */
#define prl_rx_chunk_state_set(vL) \
    do { \
        g_prl_state_info.chunk_rx_state = vL; \
    } while(0)

#define prl_rx_sop_msg_id_reset() \
    do { \
        g_prl_state_info.rx_sop_msg_id = 0; \
        g_prl_state_info.rx_sop_msg_id_stored = 0; \
    } while(0)

/** Get protocol rx message header pointer */
#define prl_rx_msg_head_get() \
    (g_prl_state_info.rx_msg_head.u.u32)

/** Set protocol rx message header */
#define prl_rx_msg_head_set(u32val) \
    do { \
        g_prl_state_info.rx_msg_head.u.u32 = (u32val); \
    } while(0)

/** Get protocol rx sop type */
#define prl_rx_sop_type_get() \
    (g_prl_state_info.rx_msg_head.u.desc.sop)

/** Set protocol rx sop type */
#define prl_rx_sop_type_set(v) \
    do { \
        g_prl_state_info.rx_msg_head.u.desc.sop = v; \
    } while(0)


/** Get protocol current rx byte count */
#define prl_rx_byte_cnt_get() \
    (g_prl_state_info.rx_msg_head.u.desc.len)

/** Set protocol current rx byte count */
#define prl_rx_byte_cnt_set(v) \
    do { \
        g_prl_state_info.rx_msg_head.u.desc.len = v; \
    } while(0)


/** Get protocol rx packet number */
#define prl_rx_pkt_num_get() \
    (g_prl_state_info.rx_msg_head.u.desc.num)

/** Set protocol rx packet number */
#define prl_rx_pkt_num_set(v) \
    do { \
        g_prl_state_info.rx_msg_head.u.desc.num = v; \
    } while(0)


#define prl_rx_sop1_msg_id_reset() \
    do { \
        g_prl_state_info.rx_sop1_msg_id = 0; \
        g_prl_state_info.rx_sop1_msg_id_stored = 0; \
    } while(0)

#define prl_rx_sop2_msg_id_reset() \
    do { \
        g_prl_state_info.rx_sop2_msg_id = 0; \
        g_prl_state_info.rx_sop2_msg_id_stored = 0; \
    } while(0)

#define prl_rx_sop1d_msg_id_reset() \
    do { \
        g_prl_state_info.rx_sop1d_msg_id = 0; \
        g_prl_state_info.rx_sop1d_msg_id_stored = 0; \
    } while(0)

#define prl_rx_sop2d_msg_id_reset() \
    do { \
        g_prl_state_info.rx_sop2d_msg_id = 0; \
        g_prl_state_info.rx_sop2d_msg_id_stored = 0; \
    } while(0)


void prl_rx_state_init();
void prl_rx_state_machine();
void prl_rx_chunk_state_init();
void prl_rx_chunk_state_machine();

#endif /* #ifndef __PRL_RX_STATE_H_ */

