/*
 *========================================================================
 *
 *    : AZN_PLAZA_LANE APP
 *      : hi_ctrl.h
 *      : JBY (baarong@neighbor21.co.kr)
 *      : 2018.07.13
 *      : 
 *        : high-level comm. control block
 *
 *========================================================================
*/

#ifndef    _HI_CTRL
#define    _HI_CTRL

#include <sys/types.h>
#include <time.h>
#include "bas_tbl.h"
#include "lane_stat_info.h"
#include "wrk_stat.h"
#include "file_no.h"
#include "ipc_def.h"

#ifdef DEBUG
extern FILE *debug;
#endif
#ifdef TRACE
extern FILE *trace;
#endif

#define    YEAR(x)        (x+1900)
#define    MONTH(x)       (x+1)
#define    DAY(x)         (x)
#define    DAYIT(x,y,z)   ((x*10000)+(y*100)+z)

#define    HIBUFF_SIZE    4096            /* raw buffer size        */

struct    h_date_tm {           /* Year/Month/Day/Hour/Minute/Second */
    unsigned char   year[4];           /* Year                              */
    unsigned char   mon[2];            /* Month                             */
    unsigned char   day[2];            /* Day                               */
    unsigned char   hr[2];             /* Hour                              */
    unsigned char   min[2];            /* Minute                            */
    unsigned char   sec[2];            /* Second                            */
};

struct    h_date_hm {           /* Year/Month/Day/Hour/Minute        */
    unsigned char   year[4];           /* Year                              */
    unsigned char   mon[2];            /* Month                             */
    unsigned char   day[2];            /* Day                               */
    unsigned char   hr[2];             /* Hour                              */
    unsigned char   min[2];            /* Minute                            */
};

struct    h_date {              /* Year/Month/Day                    */
    unsigned char   year[4];           /* Year                              */
    unsigned char   mon[2];            /* Month                             */
    unsigned char   day[2];            /* Day                               */
};
struct    h_tm {                /* Year/Month                        */
    unsigned char   year[4];           /* Year                              */
    unsigned char   mon[2];            /* Month                             */
};

struct    hi_comm_ctrl {             /* high-level comm. control block */
    unsigned int  now_stat_info;            /* 현 처리 상태 정보              */
    unsigned int  be_stat_info;             /* 처리전 상태정보                */
    unsigned int  lane_no;                  /* 차로번호                       */
    unsigned int  asc_des_div;              /* 상/하행 구분                   */
    unsigned char device_nm[24];            /* device name(inet port)         */
    int    hi_comm_iffd[2];                 /*  0:Listen 1:Accept             */
    unsigned int  tbl_mngt_iffd;            /* TABLE  MANAGER I/F FD          */
    unsigned int  chk_event;                /* checked event                  */
    unsigned int  last_snd_knd;             /*  DATA                          */

    struct    {                      /* 보수정보 */
        unsigned int     rq_resnd_strtno;   /* 재송 요구 개시 연번       */
        unsigned int     rq_resnd_endno;    /* 재송 요구 종료 연번       */
        unsigned char    wrk_year[4];       /* 근무일자(년)              */
        unsigned char    wrk_mon[2];        /*          월               */
        unsigned char    wrk_day[2];        /*          일               */
        unsigned char    wrk_no[4];         /* 근무번호                  */
    } rtr;

    unsigned int  sndrq_sndno;              /* 재송요구 연번          */
    unsigned int  warr_snd_no;              /* 보증 전송연번          */
    unsigned int  dmp_tbl_no;               /* DUMP TABLE NUMBER      */
    unsigned int  frame_no;                 /* TABLE  FRAME NUMBER    */

    struct    {                      /* -- 송신 제어 --          */
        unsigned int  snd_no;               /* 전송연번                 */
        unsigned int  cmd;                  /* COMMAND                  */
        unsigned int  len;                  /* DATA LENGTH              */
        unsigned char buf[HIBUFF_SIZE];     /* DATA BUFFER              */
    } snd;

    struct    {                      /* -- 수신 제어 --          */
        unsigned int  snd_no;               /* 전송연번                 */
        unsigned int  cmd;                  /* COMMAND                  */
        unsigned int  len;                  /* DATA LENGTH              */
        unsigned char buf[HIBUFF_SIZE];     /*  DATA BUFFER             */
    } rcv_ctrl;

    struct    {
        unsigned int  sock;                 /* 관리명령 응답 SOCKET   */
        time_t req_tm;                      /* 요청시간               */
        unsigned int  chck;                 /* 체크                   */
        struct lo_lane_ctrl_cmd lo_lane_ctrl_cmd;    /* 관리명령 수신 DATA          */
    } manager[40];

    struct    basic_tbl bas_tbl;              /* 기초사항 TABLE 내용        */
    struct    lane_stat_info lane_stat_info;  /* 차로상태 정보              */
    struct    write_info write_info;          /* 수신 DATA에 의한 정보      */
    time_t    time;                           /* 현재시각                   */
    struct    tm cday;                        /* 일자 및 시간               */
};

/* hccb.stat */
#define    H_INIT             0x0001        /* 초기화 중             */
#define    H_ERR_REST         0x0002        /* 이상복구 수순         */
#define    H_ACC_RESND        0x0004        /* 축적 DATA 재전송 중         */
#define    H_FRAME_WAIT       0x0008        /* 축적재전송 1st FRAME 수신대기*/
#define    H_MTNC             0x0010        /* 유지보수원 근무중         */
#define    H_LANE_CLS         0x0020        /* 차로폐쇄중             */
#define    H_TRF_RCV          0x0040        /* 입구 IC별 교통량 수신 중     */
#define    H_RSV              0x0080        /* 예비권작성중            */
#define    H_CIR              0x1000        /* 회선 TEST 중         */

/*            */
#define    H_PLURAL        (H_INIT|H_ERR_REST|H_ACC_RESND|H_TRF_RCV|H_RSV)

/* hccb.chck */
#define    CHK_PORT           0x01    /* 차로측 PORT EVENT            */
#define    CHK_LISTEN         0x02    /* 응용프로그램의 LISTEN CHECK  */
#define    CHK_DATA           0x04    /* 응용프로그램의 DATA EVENT    */

#define    chg_date_tm(x, y)   memcpy(x, y, sizeof(struct h_date_tm))
#define    chg_date_hm(x, y)    memcpy(x, y, sizeof(struct h_date_hm) )
#define    chg_date(x,y)       memcpy(x, y, sizeof(struct h_date))
#define    chg_tm(x, y)     memcpy(x, y, sizeof(struct h_tm)  )

extern    struct    hi_comm_ctrl    hi_comm_ctrl;

#endif
