/**
 * \file    prl_tx_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_TX_STATE_H_
#define __PRL_TX_STATE_H_


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


#pragma pack(1)


/** \name Type-C Protocol Tx State definitions
 * \{ 
 */
#define PRL_TX_PHY_LAYER_RESET          0x00
#define PRL_TX_WAIT_FOR_MSG_REQUEST     0x01
#define PRL_TX_LAYER_RESET_FOR_TRANSMIT 0x02
#define PRL_TX_CONSTRUCT_MSG            0x03
#define PRL_TX_MSG_SENT                 0x04
#define PRL_TX_WAIT_TCPC_RESP           0x05
#define PRL_TX_MSG_DISCARDED            0x06
#define PRL_TX_TRANSMISSION_ERR         0x07
#define PRL_TX_DISCARD_MSG              0x08
/** \}*/

#define TCH_Wait_For_Message_Request_From_Policy_Engine             0x00
#define TCH_Pass_Down_Message                                       0x01
#define TCH_Wait_For_Transmision_Complete                           0x02
#define TCH_Message_Sent                                            0x03
#define TCH_Report_Error                                            0x04
#define TCH_Prepare_To_Send_Chunked_Message                         0x05
#define TCH_Construct_Chunked_Message                               0x06
#define TCH_Sending_Chunked_Message                                 0x07
#define TCH_Wait_Chunk_Request                                      0x08
#define TCH_Message_Received                                        0x09

#pragma pack()



/** Get current protocol tx state */
#define prl_tx_state_get() \
    (g_prl_state_info.tx_state)

/** Set current protocol tx state */
#define prl_tx_state_set(vL) \
    do { \
        g_prl_state_info.tx_state = vL; \
    } while(0)

 /** Get current protocol tx state */
#define prl_tx_chunk_state_get() \
    (g_prl_state_info.chunk_tx_state)
    
 /** Set current protocol tx state */
#define prl_tx_chunk_state_set(vL) \
    do { \
        g_prl_state_info.chunk_tx_state = vL; \
    } while(0)

/** Get tx_msg_req_frm_pe state */
#define prl_tx_msg_req_frm_pe_get() \
    (g_prl_state_info.tx_msg_req_frm_pe)

/** Set tx_msg_req_frm_pe state */
#define prl_tx_msg_req_frm_pe_set(st) \
    do { \
        g_prl_state_info.tx_msg_req_frm_pe = st; \
    } while(0)


#define prl_tx_sop_msg_id_get() \
    (g_prl_state_info.tx_sop_msg_id)

#define prl_tx_sop_msg_id_inc() \
    do { \
        g_prl_state_info.tx_sop_msg_id++; \
    } while(0)

#define prl_tx_sop_msg_id_reset() \
    do { \
        g_prl_state_info.tx_sop_msg_id = 0; \
    } while(0)

#define prl_tx_sop1_msg_id_get() \
    (g_prl_state_info.tx_sop1_msg_id)

#define prl_tx_sop1_msg_id_inc() \
    do { \
        g_prl_state_info.tx_sop1_msg_id++; \
    } while(0)

#define prl_tx_sop1_msg_id_reset() \
    do { \
        g_prl_state_info.tx_sop1_msg_id = 0; \
    } while(0)

#define prl_tx_sop2_msg_id_get() \
    (g_prl_state_info.tx_sop2_msg_id)

#define prl_tx_sop2_msg_id_inc() \
    do { \
        g_prl_state_info.tx_sop2_msg_id++; \
    } while(0)

#define prl_tx_sop2_msg_id_reset() \
    do { \
        g_prl_state_info.tx_sop2_msg_id = 0; \
    } while(0)

#define prl_tx_sop1d_msg_id_get() \
    (g_prl_state_info.tx_sop1d_msg_id)

#define prl_tx_sop1d_msg_id_inc() \
    do { \
        g_prl_state_info.tx_sop1d_msg_id++; \
    } while(0)

#define prl_tx_sop1d_msg_id_reset() \
    do { \
        g_prl_state_info.tx_sop1d_msg_id = 0; \
    } while(0)

#define prl_tx_sop2d_msg_id_get() \
    (g_prl_state_info.tx_sop2d_msg_id)

#define prl_tx_sop2d_msg_id_inc() \
    do { \
        g_prl_state_info.tx_sop2d_msg_id++; \
    } while(0)

#define prl_tx_sop2d_msg_id_reset() \
    do { \
        g_prl_state_info.tx_sop2d_msg_id = 0; \
    } while(0)


/** Get protocol tx message in progress flag */
#define prl_tx_msg_in_prog_get() \
    (g_prl_state_info.tx_msg_in_prog)

/** Set protocol tx message in progress flag */
#define prl_tx_msg_in_prog_set(flg) \
    do { \
        g_prl_state_info.tx_msg_in_prog = flg; \
    } while(0)


/** Get protocol tx message header pointer */
#define prl_tx_msg_head_get() \
    (g_prl_state_info.tx_msg_head.u.u32)

/** Set protocol tx message header */
#define prl_tx_msg_head_set(u32val) \
    do { \
        g_prl_state_info.tx_msg_head.u.u32 = (u32val); \
    } while(0)


/** Get protocol tx sop type */
#define prl_tx_sop_type_get() \
    (g_prl_state_info.tx_msg_head.u.desc.sop)

/** Set protocol tx sop type */
#define prl_tx_sop_type_set(v) \
    do { \
        g_prl_state_info.tx_msg_head.u.desc.sop = v; \
    } while(0)


/** Get protocol tx byte count */
#define prl_tx_byte_cnt_get() \
    (g_prl_state_info.tx_msg_head.u.desc.len)

/** Set protocol tx byte count */
#define prl_tx_byte_cnt_set(v) \
    do { \
        g_prl_state_info.tx_msg_head.u.desc.len = v; \
    } while(0)


/** Get protocol tx packet number */
#define prl_tx_pkt_num_get() \
    (g_prl_state_info.tx_msg_head.u.desc.num)

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



/** Protocol msg2cbl_txerr get */
#define prl_msg2cbl_txerr_get() \
    (g_prl_state_info.prl_msg2cbl_txerr)


/** Protocol msg2cbl_txerr clear */
#define prl_msg2cbl_txerr_clear() \
    do { \
        g_prl_state_info.prl_msg2cbl_txerr = FALSE; \
    } while(0)


/** Protocol msg_txerr get */
#define prl_msg_txerr_get() \
    (g_prl_state_info.prl_msg_txerr)


/** Protocol msg_txerr clear */
#define prl_msg_txerr_clear() \
    do { \
        g_prl_state_info.prl_msg_txerr = FALSE; \
    } while(0)


/** Protocol txmsg_sent_ind get */
#define prl_txmsg_sent_ind_get() \
    (g_prl_state_info.prl_txmsg_sent_ind)


/** Protocol txmsg_sent_ind clear */
#define prl_txmsg_sent_ind_clear() \
    do { \
        g_prl_state_info.prl_txmsg_sent_ind = FALSE; \
    } while(0)


void prl_tx_state_init();
void prl_tx_sop_msg_send();
void prl_tx_inc_msg_id();
void prl_tx_state_machine();
void prl_tx_chunk_state_init();

void prl_tx_chunk_state_machine();

#endif /* #ifndef __PRL_TX_STATE_H_ */

