
#ifndef _Sent_FICOSEKCOM_H
#define _Sent_FICOSEKCOM_H

/*-------------------------------- Includes ------------------------------*/
#include "Global.h"
#include "SentDriver.h"

/*-------------------------------- Defines -------------------------------*/

/* Theoretical time between two consecutive executions of FicOsekCom task */
#define SENTFICOSEK_COM_TASK_TICKS ((t_timer_time)2)

/* Return type of the calls OSEK COM */
#ifndef STD_TYPES_H
    #define E_OK         ((UI_8)0x00) /* Service call has succeeded        */
#endif    
#define E_COM_ID         ((UI_8)0x01) /* Given message or mode identifier  */
                             /* is out of range or invalid        */
#define E_COM_LENGTH     ((UI_8)0x02) /* Given data length is out of range */
#define E_COM_LIMIT      ((UI_8)0x03) /* Overflow of message queue         */
#define E_COM_NOMSG      ((UI_8)0x04) /* Message queue is empty            */
#define E_COM_SYS_NOINIT ((UI_8)0x05) /* User defined E_COM_SYS_??         */

/* Node id */ 
#define NODE_ID_MASK ((UI_16) 0xF000U) /* Mask to get nodes id */
#define NODE_ID_DEFC_SENT (((UI_16) 0x1000U) & NODE_ID_MASK) /* Symbolic name of the node DEFC_SENT */

/* Symbolic names for Rx signals */ 
#define SIG_MLX_DIE1_ANGLE (((UI_16) 1) | NODE_ID_DEFC_SENT) /* Symbolic name of the signal MLX_DIE1_ANGLE */
#define SIG_MLX_DIE1_RC (((UI_16) 2) | NODE_ID_DEFC_SENT) /* Symbolic name of the signal MLX_DIE1_RC */
#define SIG_MLX_DIE1_ST (((UI_16) 3) | NODE_ID_DEFC_SENT) /* Symbolic name of the signal MLX_DIE1_ST */
#define SIG_MLX_DIE2_ANGLE (((UI_16) 4) | NODE_ID_DEFC_SENT) /* Symbolic name of the signal MLX_DIE2_ANGLE */
#define SIG_MLX_DIE2_RC (((UI_16) 5) | NODE_ID_DEFC_SENT) /* Symbolic name of the signal MLX_DIE2_RC */
#define SIG_MLX_DIE2_ST (((UI_16) 6) | NODE_ID_DEFC_SENT) /* Symbolic name of the signal MLX_DIE2_ST */
/* Last Rx signal */
#define SENTLAST_RX_STA_LEN_SIGNAL_SYMBOLIC_NAME ((~NODE_ID_MASK) & SIG_MLX_DIE2_ST)

/* Last OsekCom signal */
#define SENTLAST_OSEK_COM_SIGNAL_SYMBOLIC_NAME ((UI_16) 6) 

/* Symbolic names for Dummy signal */ 
#define SENTSIGDUMMY ((UI_16)0x0FFF) /* Symbolic name of the signal SIGDUMMY */


/*----------------------------- Data Types -------------------------------*/
/* Type definition of internal signals */
typedef UI_16 t_sig_mlx_die1_angle;
typedef UI_8 t_sig_mlx_die1_st;
typedef UI_8 t_sig_mlx_die1_rc;
typedef UI_16 t_sig_mlx_die2_angle;
typedef UI_8 t_sig_mlx_die2_rc;
typedef UI_8 t_sig_mlx_die2_st;


#ifndef _FICOSEKCOM_H
#define _FICOSEKCOM_H

/* Type definition of the symbolic names for signals */
typedef UI_16 t_symbolic_name;
typedef UI_16 t_symbolic_frm_name;

/* Return type of the calls OSEK COM */
typedef UI_8 t_status_type;

/* Structure of data type passed by reference in */
/* the calls of OSEK COM                         */
typedef void *t_application_data_ref;
typedef void *t_length_ref; 

/* Flag types of OSEK COM */
typedef enum {
    COM_FALSE = 0x00, /* Flag down  */
    COM_TRUE  = 0x01  /* Flag up */
} t_flag_value;

/* Stop mode of OSEK COM */
typedef enum {
    COM_SHUTDOWN_IMMEDIATE = 0 /* Apagat immediat */
} t_com_shutdown_mode_type;

/* Initialize mode of OSEK COM */
typedef enum {
    COM_NORMAL_MODE = 0 /* No es processen trames de COM de Debug */
} t_com_application_mode_type;

#endif


	
/*--------------------------- Global Variables ---------------------------*/


/* --------------------------- Routine Prototypes --------------------------- */

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Reception of a new frame notification callback
|  * This callback must be launched by the lower COM driver upon the reception
|    of a new frame.
|---------------------------------------------------------------------------
| Parameters description:
|  bhdl: Handler of the buffer where the received frame is stored.
/---------------------------------------------------------------------------*/
void SentOsekComRxNotifCallbackDEFC_SENT(t_com_buf_hdl bhdl);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Service to initialize OsekCom stack
|
| ------------------------------------------------- --------------------------
| Parameters description:
|  app_mode: OsekCom initialization mode (See t_com_application_mode_type)
|  return: E_OK in case of no errors
|          Other (see t_status_type).
/---------------------------------------------------------------------------*/
t_status_type SentStartCom(t_com_application_mode_type app_mode);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
| * Service to inquiry in which mode OsekCom has been initialized.
| * If this service is called before initializing the communications stack
|   a random mode will be returned.
| ---------------------------------------------------------------------------
| Parameters description:
|  return: OsekCom initialization mode(See t_com_application_mode_type)
/ --------------------------------------------------------------------------- */
t_com_application_mode_type SentGetComApplicationMode(void);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
| * Service to close the OsekCom stack.
| * This service stops the transmission of periodic frames.
| * After calling this service communications could be re-established
|   calling again service StartCom.
| * This service does not change the state of any lower COM driver.
| ---------------------------------------------------------------------------
| Parameters description:
|  shtdwn_mode: OsekCom stop mode (See t_com_shutdown_mode_type)
|  return: E_OK in case of no errors
|          Other (see t_status_type).
/ --------------------------------------------------------------------------- */
t_status_type SentStopCom(t_com_shutdown_mode_type shtdwn_mode);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Service to start the transmission of periodic or mixed frames.
|  * If this service is re-executed then transmission timers will be re-started.
|---------------------------------------------------------------------------
| Parameters description:
|  return: E_OK in case of no errors
|          Other (see t_status_type).
/---------------------------------------------------------------------------*/
t_status_type SentStartPeriodic(void);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Service to stop the transmission of periodic or mixed frames.
|  * To re-start the transmission of periodic or mixed frames service
|    StartPeriodic must be called.
|---------------------------------------------------------------------------
| Parameters description:
|  return: E_OK in case of no errors
|          Other (see t_status_type).
/---------------------------------------------------------------------------*/
t_status_type SentStopPeriodic(void);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * This service updates the application variable referenced by >data_ref< with
|    the data stored in the internal stack for the object identified by >message<.
|  * This service will reset the class 1 (RxNotif) and 3 (RxErrorNotif) flags
|    associated to >message<
|  * If >message< is an enqueued signal the service will return the data
|    stored in the internal stack (initial value / last received value / last value
|    set with InitMessage)
|    If >message< is a queued signal the service will return the first value
|    available in the queue or error if the queue is empty. (Mode not supported)
|  * The user is responsible of granting that the parameter >data_ref< 
|    points to a variable correctly allocated and compatible in size with the 
|    received signal type
|  * Usage example: 
|    t_vehicle_speed vehicle_speed;
|    (void)ReceiveMessage(SIG_VEHICLE_SPEED,&vehicle_speed); 
|---------------------------------------------------------------------------
| Parameters description:
|   message: Identification of the signal to be retrieved.
|   data_ref: Pointer to a variable where to store the requested signal. 
|   return: 
|     E_OK in case of no errors
|     E_COM_ID in case the parameter >message< is out of range or if it refers
|              to signal that is sent, dynamic length or zero-length
|     E_COM_NOMSG in case the queued signal identified by >message< is empty.
|     E_COM_LIMIT in case an overflow of the queue of the signal identified by >message<
|                 occurred since the last call to ReceiveMessage for >message<.
|                 E_COM_LIMIT indicates that at least one message has been discarded
|                 since the message queue filled. Nevertheless the service is 
|                 performed and a message is returned. The service ReceiveMessage
|                 clears the overflow condition for >message<.
|     other (see t_status_type).
/---------------------------------------------------------------------------*/
t_status_type SentReceiveMessage(t_symbolic_name message,
                             t_application_data_ref data_ref);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * This service updates OsekCom internal data structure of the signal
|    identified by >message< with the data referenced by the variable
|    referenced by >data_ref<
|  * This service will not reset any class flags associated to >message<
|  * This service will not initiate any transmission.
|  * The user is responsible of granting that the parameter 'data_ref' 
|    points to an address correctly allocated and compatible in size with the 
|    transmitted signal type.
|---------------------------------------------------------------------------
| Parameters description:
|   message: Identification of the signal.
|   data_ref: Pointer to a variable containing the data. 
|   return: 
|     E_OK in case of no errors
|     E_COM_ID if the message or signal to initialize don't exist
|     Other (see t_status_type).
/---------------------------------------------------------------------------*/
t_status_type SentInitMessage(t_symbolic_name message,
                          t_application_data_ref data_ref);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Consult service for the flag of COM traffic.
|---------------------------------------------------------------------------
| Parameters description:
|  return: 
|     COM_FALSE if has not been detected any communication activity since
|               last clear
|     COM_TRUE  if has been detected communication activity since last clear 
/---------------------------------------------------------------------------*/
t_flag_value SentReadFlagComTrafficDEFC_SENT(void);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Reset service for the flag of COM traffic.
|---------------------------------------------------------------------------
| Parameters description:
/---------------------------------------------------------------------------*/
void SentResetFlagComTrafficDEFC_SENT(void);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Consult service to retrieve the state of class 1 (RxNotif), 
|    class 3 (Rx_ErrorNotif), class 2 (TxNotif) and class 4 (Tx_ErrorNotif) flags
|---------------------------------------------------------------------------
| Parameters description:
|   message: Identification of the signal.
|   return: 
|     COM_FALSE if the flag is down
|     COM_TRUE  if the flag is up
/---------------------------------------------------------------------------*/
t_flag_value SentReadFlagRxSig(t_symbolic_name message);
t_flag_value SentReadFlagRxSigMLX_DIE1_ANGLE(void);
t_flag_value SentReadFlagRxSigMLX_DIE2_ANGLE(void);
t_flag_value SentReadFlagRxErrorSig(t_symbolic_name message);
t_flag_value SentReadFlagRxErrorSigMLX_DIE1_ANGLE(void);
t_flag_value SentReadFlagRxErrorSigMLX_DIE2_ANGLE(void);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Reset service for the notify class 1 (RxNotif) class 3 (RxErrorNotif)
|    class 2 (TxNotif) and class 4 (TxErrorNotif)
|---------------------------------------------------------------------------
| Parameters description:
|   message: Identification of the signal.
/---------------------------------------------------------------------------*/
void SentResetFlagRxSig(t_symbolic_name message);
void SentResetFlagRxSigMLX_DIE1_ANGLE(void);
void SentResetFlagRxSigMLX_DIE2_ANGLE(void);
void SentResetFlagRxErrorSig(t_symbolic_name message);
void SentResetFlagRxErrorSigMLX_DIE1_ANGLE(void);
void SentResetFlagRxErrorSigMLX_DIE2_ANGLE(void);

/*---------------------------------------------------------------------------
| Portability:  Target platform independent
|----------------------------------------------------------------------------
| Routine description:
|  * Main runnable of the OsekCom stack intended to be called periodically by
|    the system scheduler with a period equal to SENTFICOSEK_COM_TASK_TICKS
|---------------------------------------------------------------------------
| Parameters description:
/---------------------------------------------------------------------------*/
void SentOsekComTask(void);

#endif

