/****************************************************************************
 * @source      : VMSProtocolF.h
 * @description : VMS Protocol header
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2016/09/02   shjung
 *
 ****************************************************************************/

//---------------------------------------------------------------------------
#ifndef __VMSPROTOCOL_H__
#define __VMSPROTOCOL_H__
//---------------------------------------------------------------------------
#pragma pack(push, 1)
//---------------------------------------------------------------------------

#define     DEFAULT_TCP_PACKET_MAX_SIZE	    2048	    // default tcp packet max size

#define     VMS_DEFAULT_PORT                6020        /* 기본 포드 */
#define     VMS_DEFAULT_TIMEOUT             5           /* 5 Sec */

#define     VMS_MAX_PACKET_SIZE             65535       /* max packet size */
#define     VMS_MIN_PACKET_SIZE             8           /* min packet size */

#define     VMS_MAX_DATAGRAM_SIZE	        65535       /* max DatagramSize */


#define     VMS_CHECK_TIME                  60          /* vms check on/off time */
#define     VMS_MAX_DEVICE                  64          // 64 EA, 현장 설치 9대

#define     VMS_MAX_HEARTBEAT_TIME          30			/* max HeartbeatTime */


// Prtocol Data
#define     VMS_DLE                         0x10        // Data Link Escape
#define     VMS_STX                         0x02        // Start of TeXt
#define     VMS_ETX                         0x03        // End of TeXt
#define     VMS_ACK                         0x06        // ACK
#define     VMS_NAK                         0x15        // NAK

// PASIG VMS Protocol
#define     REQ_VMS_STATUS                  0x50        // Status 요청,  센터->VMS
#define     RES_VMS_STATUS                  0x50        // Status 응답,  VMS->센터

#define     REQ_VMS_MSG_DOWNLOAD            0x51        // 문안 전송 요청, 센터->VMS
#define     RES_VMS_MSG_DOWNLOAD            0x51        // 문안 전송 응답, VMS->센터

#define     REQ_VMS_LIB_DOWNLOAD            0x52        // Library download 요청, 센터->VMS
#define     RES_VMS_LIB_DOWNLOAD            0x52        // Library download 응답, VMS->센터

#define     REQ_VMS_PARAM_DOWNLOAD          0x53        // Parameter download 요청, 센터->VMS
#define     RES_VMS_PARAM_DOWNLOAD          0x53        // Parameter download 응답, VMS->센터

#define     REQ_VMS_BLANK_DISP              0x54        // Blank 요청, 센터->VMS
#define     RES_VMS_BLANK_DISP              0x54        // Blank 응답, VMS->센터

#define     REQ_VMS_DEF_MSG_DISP            0x55        // Default message display 요청, 센터->VMS
#define     RES_VMS_DEF_MSG_DISP            0x55        // Default message display 응답, VMS->센터

#define     REQ_VMS_MODULE_STATUS           0x56        // Module status upload 요청, 센터->VMS
#define     RES_VMS_MODULE_STATUS           0x56        // Module status upload 응답, VMS->센터

#define     REQ_VMS_INITIALIZE              0x57        // Initialize 요청, 센터->VMS
#define     RES_VMS_INITIALIZE              0x57        // Initialize 응답, VMS->센터

#define     REQ_VMS_LUMINANCE_LEVEL_SET     0x58        // Luminance level set 요청, 센터->VMS
#define     RES_VMS_LUMINANCE_LEVEL_SET     0x58        // Luminance level set 응답, VMS->센터

#define     REQ_SIGNBOARD_POWER_CONTROL     0x59        // Signboard power control 요청, 센터->VMS
#define     RES_SIGNBOARD_POWER_CONTROL     0x59        // Signboard power control 응답, VMS->센터

typedef struct tabBit
{
    BYTE u0:1;
    BYTE u1:1;
    BYTE u2:1;
    BYTE u3:1;
    BYTE u4:1;
    BYTE u5:1;
    BYTE u6:1;
    BYTE u7:1;
} BIT;

typedef struct tabNAK
{
    BYTE crc:1;     // CRC error
    BYTE cmd:1;     // Bad Command
    BYTE dat:1;     // Bad Data
    BYTE nbr:1;     // Bad Number
    BYTE chk:1;     // Pixel Check
    BYTE res:3;
} NAK;

typedef union tagAckNak
{
    BYTE    ack;
    NAK     nak;
} ACK_NAK;

#define     POS_ACKNAK  11

typedef struct tagVmsStatus1
{
    BYTE localModeActive  : 1;
    BYTE defaultParameter : 1;
    BYTE resetOccured     : 1;
    BYTE longPowerFail    : 1;
    BYTE shortPowerFail   : 1;
    BYTE doorOpen         : 1;
    BYTE moduleErrRateOver: 1;
    BYTE ramRomError      : 1;
} VMS_STATUS1;

typedef struct tagVmsStatus2
{
    BYTE libChecksumErr: 1;
    BYTE fan           : 1;
    BYTE heater        : 1;
    BYTE boardPower    : 1;
    BYTE boardPowerFail: 1;
    BYTE blankDisp     : 1;
    BYTE sirenFail     : 1;
    BYTE reserved      : 1;
} VMS_STATUS2;

typedef union tagVmsStatus
{
    char    tmp[2];
} VMS_STATUS;

// Protocol Packet Header
typedef struct tagPktHead
{
    BYTE    STX;
    BYTE    ID;
    BYTE    OpCode;
    UINT    Size;
    char    ADDR[4];
} PKT_HEAD;

typedef struct tagVmsPaket
{
    PKT_HEAD    hd;
    BYTE        data[VMS_MAX_PACKET_SIZE];
} VMS_PACKET;

// VMS Packet Receive Sate
typedef enum
{
    RX_STX,
    RX_HEAD,
    RX_DATA,
    RX_CRC,
    RX_ETX,
} RX_STATE;


typedef enum en_sys_err_cd
{
    SYS_ERR_NONE,                           //0 no error
    SYS_ERR_OTHER,                          //1 other
    SYS_ERR_INTERNAL,                       //2 internal system error(kernel..)
    SYS_ERR_MEMORY,                         //3 memory overflow
    SYS_ERR_DATABASE,                       //4 system database error
    SYS_ERR_RESET,                          //5 system reset
    SYS_ERR_START,                          //6 system start
    SYS_ERR_OFFLINE,                        //7 offline
    SYS_ERR_NO_RES,                         //8 no response
    SYS_ERR_ACCESS,                         //9 access denied
    SYS_ERR_INVALID,                        //10 invalid data type
    SYS_ERR_UNKNOWN,                        //11 unknown data type
    SYS_ERR_WRITE_LENGTH,                   //12 write length
    SYS_ERR_PACKET_SIZE_LARGE,              //13 packet size large
    SYS_ERR_INVALID_TAG,                    //14 invalid tag
    SYS_ERR_CRC,                            //15 crc
    SYS_ERR_LENGTH,                         //16 length
    SYS_ERR_ADDRESS,                        //17 address
    SYS_ERR_HEADER_OPTIONS,                 //18 header options
    SYS_ERR_UNKNOWN_COMMAND,                //19 unknown command
    SYS_ERR_RECEIVE_TIMEOUT,                //20 receive timeout
    SYS_ERR_DATA_SIZE,                      //21 data size
    SYS_ERR_MISMATCH,                       //22 mismatch command
    SYS_ERR_INVALID_PARA,                   //23 invalid parameter
    SYS_ERR_MOVEMENT,                       //24 movement
	SYS_ERR_UNAUTHORIZED,                   //25 unauthorized

	SYS_ERR_DATEXDATAPACKET_DECODE,         /* 26 DatexDataPacket decode */
	SYS_ERR_INVALID_VERSION_NUMBER,         /* 27 invalid version number */
	SYS_ERR_C2CAUTHENTICATEDMESSAGE_DECODE, /* 28 C2CAuthenticatedMessage decode */
	SYS_ERR_C2CAUTHENTICATEDMESSAGE,        /* 29 C2CAuthenticatedMessage */
	SYS_ERR_NOT_LOGIN,                      /* 30 Not login */
	SYS_ERR_ACCEPT_TYPE,                    /* 31 Accept Type */
	SYS_ERR_DATEXPUBLISH_FORMAT,            /* 32 datexPublish Format */
	SYS_ERR_DATEXPUBLISH_TYPE,              /* 33 datexPublish Type */
	SYS_ERR_UNKNOWN_MESSAGE_ID,             /* 34 unknown Message id */
	SYS_ERR_MESSAGE_DECODE,                 /* 35 Message decode */
	SYS_ERR_DATEXDATAPACKET_ENCODE,         /* 36 DatexDataPacket encode */
	SYS_ERR_C2CAUTHENTICATEDMESSAGE_ENCODE, /* 37 C2CAuthenticatedMessage encode */
	SYS_ERR_MESSAGE_ENCODE,                 /* 38 Message encode */
	SYS_ERR_PUBLICATIONDATA_ENCODE,         /* 39 PublicationData encode */
	SYS_ERR_MISMATCH_DESTINATION_TEXT,      /* 40 mismatch Destination text */
	SYS_ERR_DATEXLOGIN_VALUE,               /* 41 Login Value */
	SYS_ERR_SUBSCRIPTION_TYPE,              /* 42 SubscriptionType Type */
	SYS_ERR_SUBSCRIPTION_STATUS_VALUE,      /* 43 SubscriptionType Status Value */
	SYS_ERR_SUBSCRIPTION_MODE_VALUE,        /* 44 SubscriptionType Mode Value */
	SYS_ERR_SUBSCRIPTION_FORMAT_VALUE,      /* 45 SubscriptionType Format Value */
	SYS_ERR_HANGUL_CONVERSION,              /* 46 Hangul Conversion */
	SYS_ERR_INVALID_TAGANDLEN,              /* 47 invalid Tag And Len */
	SYS_ERR_PUBLICATIONDATA_DECODE          /* 48 PublicationData decode */
} EN_SYS_ERR_CD;


// 클라이언트 요청
typedef struct client_command
{
	BYTE  MsgSeq;                       /* 통신 일련 번호 */
	DWORD ProcessID;                    /* 프로세스 ID */
	WORD  OpCode;                       /* 데이터 내용을 구분하는 업무 코드 */
	WORD  wSize;                        /* Size */
} CLIENT_COMMAND;


typedef enum en_subscription_nbr
{
    NBR_GENERAL_STATUS = 1,
    NBR_MODULE_STATUS,
    NBR_POWER_STATUS,
    NBR_REALTIME_DISPLAY,

    NBR_DOT_STATUS,
    NBR_SCREEN_STATUS,
    NBR_DISPLAYING_DATA,
} EN_SUBSCRIPTION_NBR;

typedef enum en_publication_nbr
{
	NBR_MODULE_POWER = 1,
	NBR_LUMINANCE,
	NBR_CONTROLLER_RESET,
	NBR_STATUS_SETTING,
    NBR_SCREEN_STATUS_REQ,
    NBR_REALTIME_DISPLAY_REQ,
	NBR_BASE_REALTIMEDISPLAY = 100,
	NBR_BASE_SCHEDULEDDISPLAY = 200,
	NBR_BASE_DOWNLOADGRAPHICDATA = 300
} EN_PUBLICATION_NBR;

typedef struct subscription_argument
{
	DWORD nbr;              		    /* datexSubscribe_Serial_nbr */
	int   mode;               		    /* SubscriptionMode */
	int   delay;              		    /* datexRegistered_UpdateDelay_qty */
	int   obj;                		    /* EN_OBJECT_ID */
} SUBSCRIPTION_ARGUMENT;

typedef struct
{
	DWORD SubscribeSerialNbr;    	        /* Subscribe Serial Nbr */
	int   obj;                		/* EN_OBJECT_ID */
    char  szFormId[10];
	union
    {
		struct
        {
			BYTE Mode;              /* EN_VMS_MODE */
			int  Idx;               /* index */
		} Form;

		struct
        {
			int Idx;                /* index */
		} Down;

		BYTE Ctl;                   /* EN_STATUS_CONTROL */

		struct
        {
			int SchMsgTime;			/* scheduledMessage_OperatingTimeQty */
			int	ModuleTemp; 		/* module_OperatingTemperatureQty */
			int	FanTemp; 			/* fan_OperatingTemperatureQty */
			int	HeaterTemp; 		/* heater_OperatingTemperatureQty */
			int	ModuleFail;			/* module_BasicFailureRate */
			int	RetryCount;			/* maximum_RetryQty */
			int	TimeOut;			/* response_TimeOutQty */
		} Set;
		struct
        {
            BYTE  DefCommErr;                           // 통신장애 기본값, 제어기가 Host로부터 Poll 또는 명령을 받지 않고 경과할 수 있는 최대시간 (default ; 60초)
            short SlotCommErr;                          // 통신장애 slot번호, 통신 fail 시 표시할 메시지 번호(default 0번)
            short SlotPowerErr;                         // 전원장애 slot번호, 전원장애 시 표시할 메시지 번호(default 0번)
            BYTE  NightLuminance;                       // 야간 휘도 값, 0x30 ~ 0x3F
            BYTE  DayLuminance;                         // 주간 휘도 값, 0x40 ~ 0x4F
            BYTE  PhaseCycleTime;                       // phase시간주기, 장애시 phase주기 (default : 3초, 범위 : 0 ~ 10초)
            BYTE  ModuleFailRate;                       // 모듈장애율, 한개의 module을 장애로 처리하기 위한 장애 pixel백분율 (default 10%)
            BYTE  ModuleCheckTime;                      // 모듈감시 주기, 0 ~ 255 (단위:초)
            BYTE  BoardOnHour;                          // 전광판 On time(시), 0시 ~ 23시
            BYTE  BoardOnMin;                           // 전광판 On time(분), 0시 ~ 23시
            BYTE  BoardOffHour;                         // 전광판 Off time(시), 0시 ~ 23시
            BYTE  BoardOffMin;                          // 전광판 Off time(분), 0시 ~ 23시
		} Set2;
	} Data;
} PUBLICATION_ARGUMENT;

typedef union command_argument
{
    DWORD   FrED;       /* datexFrED_ConfirmPacket_nbr */
    int     Logout;     /* logout code */
	int     Terminate;  /* terminate code */

	struct
    {
		DWORD nbr;      /* datexAccept_Packet_nbr */
		int   type;     /* accept type */
		DWORD sub;      /* registered subscription */
	} Accept;

    struct
    {
        DWORD nbr;      /* datexReject_Packet_nbr */
		int   type;     /* accept type */
        int   code;     /* reject code */
    } Reject;

	PUBLICATION_ARGUMENT  Publication;      /* Publication */
    SUBSCRIPTION_ARGUMENT Subscription;     /* Subscription */

} COMMAND_ARGUMENT;

//---------------------------------------------------------------------------
#pragma pack(pop)
//---------------------------------------------------------------------------
#endif
