/*
*========================================================================
*
*   SYSTEM   : AZN_PLAZA_LANE APP
*   FILE     : rcv_function.c
*   Progrmer : JBY (baarong@neighbor21.co.kr)
*   Date     : 2018.08.30
*   Modified :
*   Remark   :
*
*========================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <string.h>
#include <uuid/uuid.h>

#include "hi_ctrl.h"
#include "snd_rcv_knd.h"
#include "hi_packet_format.h"
#include "dir_path.h"
#include "file_lb.h"
#include "func_list.h"
#include "ipc_def.h"
#include "lb_func_list.h"
#include "iss_comp.h"
#include "path_def.h"

extern int oracon_snd;

#define    ATT_A  0x01            /* 축적 DATA                  */
#define    ATT_P  0x02            /* 통행원시 저장              */
#define    ATT_T  0x04            /* 단말수신 저장              */
#define    ATT_M  0x08            /* 모니터링                   */
#define    ATT_W  0x10            /* 근무자료처리로 전달        */
#define    ATT_R  0x20            /* 실시간 자료처리로 전달     */
#define    _DEV_
extern    int    snd_snd_no_data()      , rq_accuml_data_resnd()  , snd_tbl_cmd();
extern    int    snd_tm_dt()            , snd_tbl_dt()            , snd_tbl_dt_hpss();
extern    int    confrm_reply_snd_hnd() , lane_cir_test()         , snd_crd_no_srch_result();
extern    int    lane_stat_info_chng()  , wrk_stat_info_chng()    , fsave_bkout_cnt_spec();
extern    int    rq_lane_fw_data()      , snd_open_key_rslt()     , snd_welfare_crd_no_result();

extern    int    date_dt_init()         , fsave_rprspec()         , fsave_clslane_spec();
extern    int    fsave_ofc_hnd()        , fsave_hr_trf_spec()     , fsave_5min_trf_spec();
extern    int    fsave_tbl_dmp()        , fsave_passraw()         , fsave_trmn_rcv();

extern    int    fsave_ecrd_pay_dt()    , fsave_ecrd_chrg_dt();
extern    int    srch_rmotlane_stat()   , fsave_mtnc_dt()         , fsave_crd_dt();

extern    int    snd_non_pay_srch_rlt()  , snd_crd_no_srch_rlt(), snd_imm_red_srch_rlt();

int    rcv_rq_seq_no(), rcv_comm_continue_rq(), rcv_accuml_data_ok();
int    rcv_tbl_order(), rcv_lane_stat(), rcv_rpr_data();
int    rcv_close_pass(), rcv_wrk_strt(), rcv_hnd_data();
int    rcv_card_data(), rcv_wrk_end(), rcv_plz_line_reply();
int    remote_lane_stat_rlt(), rcv_non_pay_srch(), rcv_crd_srch(), rcv_refund_data();

static char *path;
int err_chk = 0;
int lst_rcv_acc_no;

static struct {
    int    kind;              /* DATA 종별             */
    uchar  desc[50];          /* DATA 제목             */
    int    attr;              /* DATA ATTRIBUTE        */
    int    (*func[5])();      /* 처리 FUNCTION         */
                              /* func[0]()  수신처리   */
                              /* func[1]()  상태처리   */
                              /* func[2]()  상태처리   */
                              /* func[3]()  저장처리   */
                              /* func[4]()  응답처리   */
} rcv_handle[31] = {
    /*DATA 종별*/             /* DATA 제목 */                            /*DATA ATTRIBUTE*/            /*수신처리*/         /*상태처리*/             /*상태처리*/         /*저장처리 */         /*응답처리*/
    { NS_RQ_SEQ_NO             , "전송연번요구"                        ,                         ATT_W,        rcv_rq_seq_no,  lane_stat_info_chng,               NULL,              NULL,        snd_rq_seq_no },
    { NS_COMM_CONTINUE_RQ      , "교신재개요구"                        ,                             0, rcv_comm_continue_rq,                 NULL,               NULL,              NULL, rq_accuml_data_resnd },
    { NS_ACCUML_DATA_OK        , "축적데이터 전송완료"                 ,                             0,   rcv_accuml_data_ok,                 NULL,               NULL,              NULL,                 NULL },
    { NS_TM_RQ                 , "시각요구"                            ,                             0,                 NULL,                 NULL,               NULL,              NULL,            snd_tm_dt },
    { NS_CMD_REPLY             , "지령응답"                            ,                             0,                 NULL,  lane_stat_info_chng,               NULL,              NULL,                 NULL },
    { NS_TBL_ORDER             , "테이블 요구"                         ,                             0,        rcv_tbl_order,                 NULL,               NULL,              NULL,           snd_tbl_dt },
    { NS_LANE_STAT             , "차로상태"                            ,                         ATT_W,        rcv_lane_stat,  lane_stat_info_chng,               NULL,              NULL,          snd_tbl_cmd },
    { NS_RPR_DATA              , "보수데이터"                          ,                   ATT_A|ATT_W,         rcv_rpr_data,  lane_stat_info_chng,               NULL,              NULL,                 NULL },
    { NS_CLOSE_PASS            , "야간계수 데이터"                     ,                   ATT_A|ATT_W,       rcv_close_pass,                 NULL,               NULL,              NULL,                 NULL },
    { NS_WRK_START             , "근무개시보고"                        , ATT_A|ATT_P|ATT_T|ATT_M|ATT_W,         rcv_wrk_strt,  lane_stat_info_chng, wrk_stat_info_chng,              NULL,                 NULL },
    { NS_HND_DATA              , "처리 데이터"                         , ATT_A|ATT_P|ATT_T|ATT_M|ATT_W,         rcv_hnd_data,                 NULL,               NULL,              NULL,                 NULL },
    { NS_CRD_HND_DATA          , "카드처리 데이터"                     , ATT_A|ATT_P|ATT_T|ATT_M|ATT_W,        rcv_card_data,                 NULL,               NULL,              NULL,                 NULL },
    { NS_WRK_END               , "근무종료데이터"                      , ATT_A|ATT_P|ATT_T|ATT_M|ATT_W,          rcv_wrk_end,  lane_stat_info_chng, wrk_stat_info_chng,              NULL,          snd_tbl_cmd },
    { NS_DUMP_TBL              , "테이블 덤프"                         ,                             0,                 NULL,                 NULL,               NULL,              NULL, confrm_reply_snd_hnd },
    { NS_LANE_LINE_TEST        , "차로측기동회선테스트 테이터"         ,                             0,                 NULL,                 NULL,               NULL,              NULL,        lane_cir_test },
    { NS_PLZ_LINE_TEST_REPLY   , "영업소측 기동회선테스트 테이터 응답" ,                             0,   rcv_plz_line_reply,                 NULL,               NULL,              NULL,                 NULL },
    { NS_REMOTE_LANE_STAT_REPLY, "원격차로상태응답"                    ,                             0, remote_lane_stat_rlt,                 NULL,               NULL,              NULL,                 NULL },
    { NS_NON_PAY_SRCH          , "미납정보조회"                        ,                         ATT_W,                 NULL,                 NULL,               NULL,              NULL, snd_non_pay_srch_rlt },
    { NS_CRD_SRCH              , "카드번호조회"                        ,                         ATT_W,                 NULL,                 NULL,               NULL,              NULL,  snd_crd_no_srch_rlt },
    { NS_IMMRED_SRCH           , "면제감면정보조회"                    ,                         ATT_W,                 NULL,                 NULL,               NULL,              NULL, snd_imm_red_srch_rlt },
	{ NS_REFUND_DATA           , "환불처리 데이터"                     ,                   ATT_A|ATT_W,      rcv_refund_data,                 NULL,               NULL,              NULL,                 NULL },
    { -1                      , ""                                     ,                             0,                 NULL,                 NULL,               NULL,              NULL,                 NULL }
};

/*=========================== FUNCTION LIST =================================*/
/* recv_process()    하위차로에서 수신된 DATA FRAME 처리 MAIN FUNCTION         */
/* rcv_xxxxx()       xxxxx FRAME DATA의 수신 처리                            */
/* chk_seq_no()     수신 전송연번의 CHECKING                                 */
/*===========================================================================*/

/*
 *========================================================================
 *
 *   Function : recv_process()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.24
 *   Modified :
 *   Remark   : 차로와 통신에서 수신된 DATA 처리
 *
 *===========================================================================
*/

int recv_process(char *p)
{
    struct    write_info    wi;
    char      log_msg[128];
    int       retc,  ret_dt;
    int       ii, jj, backup_seq = 0;
	int       kk;
    int       chkdiv;
	int       ret;

    hi_comm_ctrl.be_stat_info = hi_comm_ctrl.now_stat_info;

    if(!(hi_comm_ctrl.now_stat_info & H_ACC_RESND))
    {
        e_memcpy(hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.bas_tbl.plz_id);
    }
//lane_log("수신데이터 종별[%x]\n", hi_comm_ctrl.rcv_ctrl.cmd);
    /*************************************************/
    /* 수신 자료의 DATA 종별 및 처리 PROCEDURE CHECK */
    /*************************************************/
    for (ii = 0; rcv_handle[ii].kind != -1; ii++)
    {
        if ((int)hi_comm_ctrl.rcv_ctrl.cmd == rcv_handle[ii].kind) break;
    }
#if 0
	for (kk = 0; web_rcv_handle[kk].kind != -1; kk++)
    {
        if ((int)hi_comm_ctrl.rcv_ctrl.cmd == web_rcv_handle[kk].kind) break;
    }
#endif
//lane_log("recv 1[%x] [%x]\n", hi_comm_ctrl.rcv_ctrl.cmd, rcv_handle[ii].kind);
    if (rcv_handle[ii].kind == -1)
    {
        sprintf(log_msg, "수신 DATA 종별(%02X) ERROR.", hi_comm_ctrl.rcv_ctrl.cmd);
        lane_log("수신 DATA 종별(%02X) ERROR.", hi_comm_ctrl.rcv_ctrl.cmd);
        err_log(log_msg, 0);

        return (-1);
    }

    chkdiv = chk_seq_no();
//lane_log("recv 2 [%d] \n", chkdiv);
    /********************************************************/
    /* 전처리 : 1. 축적 DATA인 경우, 전송연번 CHECK         */
    /*          2. 재송요구시 첫번째 DATA가 근무개시 DATA가 */
    /*             아닌 경우 ERROR 처리                     */
    /********************************************************/
    if ((rcv_handle[ii].attr & ATT_A) && chkdiv != 0)
    {
        sprintf(log_msg, "축적 데이타 전송연번 오류(관리: %08X 수신=%08X)",
                         hi_comm_ctrl.write_info.rcv_snd_no,
                         hi_comm_ctrl.rcv_ctrl.snd_no);
        lane_log(log_msg, "축적 데이타 전송연번 오류(관리: %08X 수신=%08X)",
                         hi_comm_ctrl.write_info.rcv_snd_no,
                         hi_comm_ctrl.rcv_ctrl.snd_no);
        err_log(log_msg, 0);
        
        return (-1);        /* 축적 DATA 전송연번 오류 */
    }
//lane_log("recv 3\n");
    if ((rcv_handle[ii].attr & ATT_A) && (hi_comm_ctrl.now_stat_info & (H_ACC_RESND|H_FRAME_WAIT)) == (H_ACC_RESND|H_FRAME_WAIT))
    {
        lane_log(log_msg, "축적 데이타 관리 %04X 수신=%04X 수신 상태 = %04X", hi_comm_ctrl.write_info.rcv_snd_no, hi_comm_ctrl.rcv_ctrl.snd_no, hi_comm_ctrl.now_stat_info);
        lane_log(log_msg, "축적 데이타 재전송중:0x0004, 축적재전송 1st FRAME 수신대기:0x0008");
        if (hi_comm_ctrl.rcv_ctrl.cmd == NS_ACCUML_DATA_OK)
        {
            sprintf(log_msg, "축적데이타 재전송 : 재전송 자료가 없습니다.");
            lane_log("축적데이타 재전송 : 재전송 자료가 없습니다.");
            err_log(log_msg, 0);
            lcc_reply(LW_RESND_NO, 1, 1, log_msg);
            hi_comm_ctrl.now_stat_info = 0;
            return (-1);
        }
        else if (hi_comm_ctrl.rcv_ctrl.cmd == NS_WRK_START)
        {
            return (0);
        }
        hi_comm_ctrl.now_stat_info &= ~H_FRAME_WAIT;
        lane_log(log_msg, "축적 데이타 관리 수신 상태 = %04X", hi_comm_ctrl.now_stat_info);
        lane_log(log_msg, "축적 데이타 재전송중:0x0004, 축적재전송 1st FRAME 수신대기:0x0008");
    }
//lane_log("recv 4\n");
    /********************************************************/
    /* 재전송이 아닌 축적 DATA인 경우, 전송연번 저장        */
    /********************************************************/
    if ((rcv_handle[ii].attr & ATT_A) && !(hi_comm_ctrl.now_stat_info & H_ACC_RESND))
    {
        backup_seq = hi_comm_ctrl.write_info.rcv_snd_no;
		/* 중간에 데이터 장애로 누락 발생 시, 아래의 2개 값을 수정하면 해당 전송연번 다시 수신 가능 */
        hi_comm_ctrl.write_info.rcv_snd_no = hi_comm_ctrl.rcv_ctrl.snd_no;   /* 최종수신 전송연번  */
        hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = hi_comm_ctrl.rcv_ctrl.snd_no;   /* 최종수신 전송연번  */
        hi_comm_ctrl.lane_stat_info.last_rcv_knd   = hi_comm_ctrl.rcv_ctrl.cmd;      /* 최종수신 DATA 종별 */
    }
//lane_log("recv 5\n");
    /********************************************************/
    /* DATA 종별에 따른 각 처리 함수를 순차적으로 호출      */
    /* 1. 모니터링 자료처리                                 */
    /* 2. 각 DATA 종별에 따른 처리 함수 호출                */
    /********************************************************/

    ret_dt = 0;
    if ((hi_comm_ctrl.now_stat_info & H_ERR_REST) || (hi_comm_ctrl.now_stat_info & H_ACC_RESND))
        ret_dt = get_data_time(hi_comm_ctrl.rcv_ctrl.buf, &wi);

    path = p;

    for (jj = 0; jj < 4 && ret_dt == 0; jj++)
    {
//lane_log("recv 8[%d]\n", ii);
        if (rcv_handle[ii].func[jj] == NULL) continue;
//lane_log("recv 9[%d]\n", jj);
        retc = (*rcv_handle[ii].func[jj])(p);
//lane_log("recv 10[%d]\n", retc);
        switch (retc)
        {
            case -1:  /* ATT_A 축적 DATA ,H_ACC_RESND 0x0004 축적 DATA 재전송 중 */
                lane_log("RECEIVE PROC ERR [%d]\n", jj);
				err_chk = 1;
                if ((rcv_handle[ii].attr & ATT_A) && !(hi_comm_ctrl.now_stat_info & H_ACC_RESND))
                {
                    hi_comm_ctrl.write_info.rcv_snd_no = backup_seq;
                    hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = backup_seq;
                }
                return (-1);
            case -2: lane_log("RECEIVE PROC SKIP \n");
                return (0);
            default:    break;
        }
#if 0
		if (web_rcv_handle[kk].func[jj] == NULL) continue;
		retc = (*web_rcv_handle[kk].func[jj])(p);
#endif
    }
//lane_log("recv 11[%d][%d]\n", ii, jj);
    if (rcv_handle[ii].func[jj] != NULL)
    {
//lane_log("수신처리 Start \n");
        hi_comm_ctrl.snd.len = (*rcv_handle[ii].func[jj])(1);
//lane_log("수신처리 [%d]\n", hi_comm_ctrl.snd.len);
        /* 차로 상태응답
           1.지령이 있으면 지령으로 응답처리
           2.지령이 없으면 확인 응답처리 */
        lane_log("(rcv_function.cpp) hi_comm_ctrl.snd.len = %d\n", hi_comm_ctrl.snd.len);

        if(hi_comm_ctrl.snd.len == 0)
        {
//lane_log("Chk [%x] [%x]\n", hi_comm_ctrl.rcv_ctrl.cmd, NS_LANE_STAT, NS_TBL_ORDER);
            if(hi_comm_ctrl.rcv_ctrl.cmd == NS_LANE_STAT || hi_comm_ctrl.rcv_ctrl.cmd == NS_TBL_ORDER)
            {
                lane_log("(rcv_function.cpp) 확인응답 송신, hi_comm_ctrl.rcv_ctrl.cmd(%d)\n", hi_comm_ctrl.rcv_ctrl.cmd);
                hi_comm_ctrl.snd.len = confrm_reply_snd_hnd(1);
            }
            
            if(hi_comm_ctrl.rcv_ctrl.cmd == NS_WRK_END)
            {
                lane_log("보증전송연번 송신 확인[%02x]\n", hi_comm_ctrl.rcv_ctrl.cmd);
                hi_comm_ctrl.snd.len = snd_warr_snd_no(0);
                lane_comm_snd();
            }
        }
		
		
    }

    if (rcv_handle[ii].attr & ATT_T && ret_dt == 0)
    {
        ret = fsave_trmn_rcv();            /* 단말수신-F 저장 */

        if(ret < 0)
        {
            lane_log("단말수신-F 저장 실패 ret = %d\n", ret);
            return (-1);
        }
    }

    /* 이상복구 수순중 */
    if ((rcv_handle[ii].attr & ATT_A) && ((hi_comm_ctrl.now_stat_info & H_ERR_REST) || (hi_comm_ctrl.now_stat_info & H_ACC_RESND)))
    {
        hi_comm_ctrl.snd.len = confrm_reply_snd_hnd(1);
    }
//lane_log("recv 13\n");
    /********************************************************/
    /* 축적 DATA 재전송요구시 수신 전송연번이 재전송요구    */
    /* 최종전송연번과 동일시 통신 단절후, 재개처리          */
    /********************************************************/
    if (hi_comm_ctrl.now_stat_info & H_ACC_RESND)
    {
        if (hi_comm_ctrl.rcv_ctrl.snd_no == hi_comm_ctrl.rtr.rq_resnd_endno ||
            hi_comm_ctrl.rcv_ctrl.cmd == NS_WRK_END)
        {
            sprintf(log_msg, "축적데이타 재전송 완료....");
            //lane_log("[%s]\n", log_msg);
            //lane_log(log_msg, "축적데이타 재전송 완료....");
            err_log(log_msg, 0);
            //lane_log("err_log 끝?\n");
            lcc_reply(LW_RESND_NO, 1, 1, log_msg);
            hi_comm_ctrl.now_stat_info &= ~(H_ACC_RESND|H_FRAME_WAIT);
            return (-1);
        }
    }
/*
	if(rcv_handle[ii].kind == NS_ACCUML_DATA_OK && err_chk != 1 && hi_comm_ctrl.cday.tm_hour >= 21 || hi_comm_ctrl.cday.tm_hour < 7)
	{
		err_chk = 0;
lane_log("축적데이터 전송완료[%02d : %02d]\n", hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min);
		hi_comm_ctrl.warr_snd_no = lst_rcv_acc_no;
		hi_comm_ctrl.snd.len = snd_warr_snd_no(0);
        lane_comm_snd();
	}
*/
//lane_log("recv 14\n");
    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_rq_seq_no()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.28
 *   Modified :
 *   Remark   : 전송연번 요구
 *
 *========================================================================
*/
int rcv_rq_seq_no()
{

    struct rcv_lane_data  rd;
    struct hi_rq_seq_no   *hrsn;
    int ret;
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
//lane_log("rcv_rq1\n");
    hi_comm_ctrl.now_stat_info |= H_INIT;        /* 초기화     */
//lane_log("rcv_rq2\n");
    hi_comm_ctrl.write_info.con_stat = CONNECT_STAT;

#if 0
    hrsn = (struct hi_rq_seq_no *)hi_comm_ctrl.rcv_ctrl.buf;
    sprintf((char *)hi_comm_ctrl.write_info.year, "%04d", hi_comm_ctrl.cday.tm_year+1900);
    sprintf((char *)hi_comm_ctrl.write_info.mon,  "%02d", hi_comm_ctrl.cday.tm_mon+1);
    sprintf((char *)hi_comm_ctrl.write_info.day,  "%02d", hi_comm_ctrl.cday.tm_mday);
    sprintf((char *)hi_comm_ctrl.write_info.hr,   "%02d", hi_comm_ctrl.cday.tm_hour);
    sprintf((char *)hi_comm_ctrl.write_info.min,  "%02d", hi_comm_ctrl.cday.tm_min);
    sprintf((char *)hi_comm_ctrl.write_info.sec,  "%02d", hi_comm_ctrl.cday.tm_sec);
    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    sprintf((char *)hi_comm_ctrl.write_info.lane_no, "%02d", hi_comm_ctrl.lane_no);

    memset(&rd, 0x00, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.write_info.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%02d00", hi_comm_ctrl.lane_no);
//lane_log("====전송연번요구 일자[%.14s]\n", rd.wrk_datetm);
    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].lane_type[1] - '0' == 2)
            sprintf((char *)rd.wrkr_no, "ETCS");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].lane_type[1] - '0' == 4)
            sprintf((char *)rd.wrkr_no, "MLFF");
        else
            sprintf((char *)rd.wrkr_no, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s  ", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
    sprintf((char *)rd.buf, "%s", (char *)hrsn);

    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 전송연번요구[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }

//lane_log("전송연번 요구[%s]\n", rtn);
    hi_comm_ctrl.snd.len = snd_rq_seq_no(rtn);/* 전송연번 요청 결과 송신 */

    lcc_reply(LW_RQ_SEQ_NO, 0, 1, "전송연번 전송이 완료 되었읍니다");
#endif
//lane_log("rcv_rq3\n");
    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_comm_continue_rq()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.28
 *   Modified :
 *   Remark   : 교신재개요구 수신 처리
 *
 *========================================================================
*/
int rcv_comm_continue_rq()
{
    struct rcv_lane_data  rd;
    struct hi_comm_continue_rq   *hccr;
    struct rq_warr_info *rq_no;
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
    int  ret;

    hi_comm_ctrl.now_stat_info |= H_ERR_REST;        /* 이상복구 수순 */
    if (hi_comm_ctrl.now_stat_info & H_ACC_RESND)    /* 축적자료 재전송중 ? */
        hi_comm_ctrl.now_stat_info |= H_FRAME_WAIT;  /* 축적자료 1st DATA 대기중 */
    hi_comm_ctrl.now_stat_info |= H_INIT;            /* 초기화     */
#if 0
    hccr = (struct hi_rq_seq_no *)hi_comm_ctrl.rcv_ctrl.buf;

    memset(&rd, 0x00, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.write_info.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%02d00", hi_comm_ctrl.lane_no);

    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].lane_type[1] - '0' == 2)
            sprintf((char *)rd.wrkr_no, "ETCS");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].lane_type[1] - '0' == 4)
            sprintf((char *)rd.wrkr_no, "MLFF");
        else
            sprintf((char *)rd.wrkr_no, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s  ", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
    sprintf((char *)rd.buf, "%s", (char *)hccr);

    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 교신재개요구[%.2s]처리실패\n", hi_comm_ctrl.write_info.plz_id);
        return(-1);
    }
    
    rq_no = rtn;
//lane_log("교신재개요구 수신 데이터[%s]\n", rtn);
    sprintf((char *)hi_comm_ctrl.rtr.wrk_year, "%.4s", rq_no->wrk_date);
    sprintf((char *)hi_comm_ctrl.rtr.wrk_mon, "%.2s", rq_no->wrk_date+4);
    sprintf((char *)hi_comm_ctrl.rtr.wrk_day, "%.2s", rq_no->wrk_date+6);
    sprintf(tmp, "%.8s%c", rq_no->rq_seq_no, '\0');
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
    hi_comm_ctrl.rtr.rq_resnd_strtno = strtoul(tmp, NULL, 16);
    //hi_comm_ctrl.sndrq_sndno = strtoul(tmp, NULL, 16);
    sprintf(tmp, "%.8s%c", rq_no->warr_snd_no, '\0');
    hi_comm_ctrl.warr_snd_no = strtoul(tmp, NULL, 16);
//lane_log("수신받은 최종 수신 연번[%08x][%.8s]\n", hi_comm_ctrl.write_info.rcv_snd_no, rq_no->wrk_date);
#endif
    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_accuml_data_ok()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.28
 *   Modified :
 *   Remark   : 축적 DATA 전송완료 수신 처리
 *
 *========================================================================
*/
int rcv_accuml_data_ok()
{
    hi_comm_ctrl.now_stat_info &= ~H_ERR_REST;        /* 이상복구 수순 */
    if (hi_comm_ctrl.now_stat_info & H_ACC_RESND)    /* 축적자료 재전송중 ? */
    {
//lane_log("############# 축적데이터 전송 완료 1 [%d]\n", lst_rcv_acc_no);
        hi_comm_ctrl.now_stat_info &= ~H_ACC_RESND;    /* 축적자료 재전송요구 CLR  */
        hi_comm_ctrl.now_stat_info &= ~H_FRAME_WAIT;    /* 축적자료 1st DATA 대기중 */
        lcc_reply(LW_RESND_NO, 0, 1, "축적자료 재수신이 완료 되었읍니다");
    }
//lane_log("############# 축적데이터 전송 완료 2 [%d]\n", lst_rcv_acc_no);
//lane_log("############# 축적데이터 전송 완료 3 [%02d]\n", hi_comm_ctrl.cday.tm_hour);
    if(hi_comm_ctrl.cday.tm_hour >= 21 && !(hi_comm_ctrl.now_stat_info & H_ERR_REST) || hi_comm_ctrl.cday.tm_hour < 7 && !(hi_comm_ctrl.now_stat_info & H_ERR_REST))
	{
		//lane_log("############# 축적데이터 전송 완료 4 [%02d]\n", hi_comm_ctrl.cday.tm_hour);
/* 업무 시간 외에 축적데이터 전송 완료 시, 보증전송연번 변경 */
		hi_comm_ctrl.warr_snd_no = lst_rcv_acc_no;
	}
    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_tbl_order()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.28
 *   Modified :
 *   Remark   : TABLE 요구 DATA 처리
 *
 *========================================================================
*/
int rcv_tbl_order()
{
    struct hi_tbl_order    *hto;
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));

    hto = (struct hi_tbl_order *)hi_comm_ctrl.rcv_ctrl.buf;

    sprintf(tmp, "%.8s%c", hto->frame_no, '\0');

    if (strtoul(tmp, NULL, 16) != FRAME_LAST_I)        /* 마지막 FRAME    */
        return (0);

    if (lb_data2atoi((char *)hto->tbl_typ, 2) != TB_LAST)            /* 최종 TABLE ?    */
        return (0);

    /* TABLE 전송 완료 */
    hi_comm_ctrl.now_stat_info &= ~H_INIT;

    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_lane_stat()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.28
 *   Modified :
 *   Remark   : 차로상태정보 수신 처리
 *
 *========================================================================
*/
int rcv_lane_stat(int rsp)
{
    struct hi_lane_stat  *hls;
    struct rcv_lane_data  rd;
#if 0
    struct    write_info    wi;
#endif
    int ret = 0, ii, jj;
    static time_t c_clock = 0;
	struct tm *t;

    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
	char bak[MAX_STRING_SIZE]; memset(bak, 0x00, sizeof(bak));
	char date[8+1];
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));

    struct {
		uchar plz_id[2];
		int lane_no;
		uchar anpr[10];
	} anpr_id[20] = 
	{
		{"01", 1, "A20201107"},
		{"01", 2, "A20201108"},
		{"01", 3, "A20201109"},
		{"01", 4, "A20211110"},
		{"01", 5, "A20201111"},
		{"01", 6, "A20211112"},
		{"01", 7, "A20201113"},
		{"01", 8, "A20211114"},
		{"03", 1, "A20203115"},
		{"03", 2, "A20213116"},
		{"03", 3, "A10103117"},
		{"03", 4, "A10103118"},
		{"03", 5, "CLOSE    "},
		{"03", 6, "CLOSE    "},
		{"03", 7, "A10103119"},
		{"03", 8, "A10103120"},
		{"03", 9, "A10103121"},
		{"03", 10, "A10103122"},
		{"00", -1, ""}
	};

    /******************************************************/
    /* 기초사항 TABLE의 수정유무 CHECK, 최신정보로 READ   */
    /******************************************************/
//lane_log("PLZ_ID[%.2s] [%.2s]\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.lane_stat_info.plz_id);
if(strncmp(hi_comm_ctrl.write_info.plz_id, PLZ_CODE2, 2) != 0)
{
	//lane_log("######## PLZ_ID[%.2s] [%.2s]\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.lane_stat_info.plz_id);
	snprintf(hi_comm_ctrl.write_info.plz_id, 2+1, "%.2s", PLZ_CODE2);
}

    lb_freadx(&hi_comm_ctrl.write_info, FNO_BASICTBL, &hi_comm_ctrl.bas_tbl, FFIRST);
//lane_log("(rcv_function.cpp) 차로상태정보 영업소ID : %.2s\n", hi_comm_ctrl.bas_tbl.plz_id);
//lane_log("(rcv_function.cpp) 차로상태정보 차로번호[%.8s] : %2d\n",&hi_comm_ctrl.write_info.year[0], hi_comm_ctrl.lane_no);

    hls = (struct hi_lane_stat *)hi_comm_ctrl.rcv_ctrl.buf;

    sprintf((char *)hi_comm_ctrl.write_info.year, "%04d", hi_comm_ctrl.cday.tm_year+1900);
    sprintf((char *)hi_comm_ctrl.write_info.mon,  "%02d", hi_comm_ctrl.cday.tm_mon+1);
    sprintf((char *)hi_comm_ctrl.write_info.day,  "%02d", hi_comm_ctrl.cday.tm_mday);
    sprintf((char *)hi_comm_ctrl.write_info.hr,   "%02d", hi_comm_ctrl.cday.tm_hour);
    sprintf((char *)hi_comm_ctrl.write_info.min,  "%02d", hi_comm_ctrl.cday.tm_min);
    sprintf((char *)hi_comm_ctrl.write_info.sec,  "%02d", hi_comm_ctrl.cday.tm_sec);
    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    sprintf(tmp, "%.8s%c", hls->seq_no, '\0');

	for(jj = 0; jj < 20; jj++)
	{
		if(anpr_id[jj].lane_no == -1)
			break;
		
		if(strncmp(anpr_id[jj].plz_id, hi_comm_ctrl.write_info.plz_id, 2) == 0 && anpr_id[jj].lane_no == hi_comm_ctrl.lane_no)
			break;
	}

    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
    hi_comm_ctrl.write_info.con_stat = 'C';
//차로상태정보 저장
	hi_comm_ctrl.lane_stat_info.mchi_stat0 = hls->mchi_stat[0];
	hi_comm_ctrl.lane_stat_info.mchi_stat1 = hls->mchi_stat[1];
	hi_comm_ctrl.lane_stat_info.mchi_stat2 = hls->mchi_stat[2];
	hi_comm_ctrl.lane_stat_info.mchi_stat3 = hls->mchi_stat[3];
	hi_comm_ctrl.lane_stat_info.mchi_stat4 = hls->mchi_stat[4];
	hi_comm_ctrl.lane_stat_info.mchi_stat5 = hls->mchi_stat[5];
	hi_comm_ctrl.lane_stat_info.mchi_stat6 = hls->mchi_stat[6];
	hi_comm_ctrl.lane_stat_info.mchi_stat7 = hls->mchi_stat[7];

    memset(tmp, 0x00, sizeof(tmp));
	sprintf(tmp, "%.8s%c", hls->bl_seq_no, '\0');
    hi_comm_ctrl.lane_stat_info.bl_lst_hnd_no = strtoul(tmp, NULL, 16);
	memset(tmp, 0x00, sizeof(tmp));
	sprintf(tmp, "%.8s%c", hls->bef_seq_no, '\0');
    hi_comm_ctrl.lane_stat_info.bef_lst_hnd_no = strtoul(tmp, NULL, 16);
	memset(tmp, 0x00, sizeof(tmp));
	sprintf(tmp, "%.8s%c", hls->imm_carno_seq_no, '\0');
    hi_comm_ctrl.lane_stat_info.imm_lst_hnd_no = strtoul(tmp, NULL, 16);
	memset(tmp, 0x00, sizeof(tmp));
	sprintf(tmp, "%.8s%c", hls->rfid_seq_no, '\0');
    hi_comm_ctrl.lane_stat_info.rfid_lst_hnd_no = strtoul(tmp, NULL, 16);
	memset(tmp, 0x00, sizeof(tmp));
	sprintf(tmp, "%.8s%c", hls->red_carno_seq_no, '\0');
    hi_comm_ctrl.lane_stat_info.red_lst_hnd_no = strtoul(tmp, NULL, 16);

    hi_comm_ctrl.lane_stat_info.comm_stat = CONNECT_STAT;

    for(ii = 0; ii < 20; ii++)
	{
		hi_comm_ctrl.lane_stat_info.tbl_stat[ii][0] = lb_data2atoi(hls->tbl_stat[ii].rvs_no, 2);
		hi_comm_ctrl.lane_stat_info.tbl_stat[ii][1] = hls->tbl_stat[ii].stat;
	}
#if 0
//차로상태정보 저장 끝
    memset(&rd, 0x20, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.write_info.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);

    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            sprintf((char *)rd.wrkr_no, "SYSI ");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            sprintf((char *)rd.wrkr_no, "SYSO");
        else
            sprintf((char *)rd.wrkr_no, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s  ", hi_comm_ctrl.write_info.wrkr_no);

//lane_log("======차로상태정보 수신 메시지 확인[%08x]\n", hi_comm_ctrl.rcv_ctrl.snd_no);
//lane_log("차로상태 연결 확인 [%c]\n", hi_comm_ctrl.lane_stat_info.comm_stat);
    rd.con_stat = hi_comm_ctrl.lane_stat_info.comm_stat;

    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
    sprintf((char *)rd.buf, "%s", (char *)hls);
    //lane_log("###### 차로상태정보[%s]\n", rd.buf);
#else
    sprintf((char *)bak, "%s%04d%02d%02d%02d%02d%02d%c%-4s%c%c%c", anpr_id[jj].anpr, 
			hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
			hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec,
			hls->anpr_cam, hls->anpr_tempa, hls->anpr_door, hls->anpr_sd, '\0');
#endif
#if 0
//lane_log("차로상태 송신[%s]\n", bak);
    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&bak, path, rtn);
//lane_log("DB처리 결과(MQ) [%d]\n", ret);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 차로상태정보[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        //return(-1);
    }
#endif
//lane_log("차로상태 RET [%d]\n", ret);
    hi_comm_ctrl.now_stat_info &= ~H_INIT;
	
	c_clock = hi_comm_ctrl.time;
    t = localtime(&c_clock);
	if( t->tm_hour == 00 && t->tm_min == 00 )
	{
//lane_log("Time Check In ");
		sprintf(date, "%04d%02d%02d", t->tm_year+1900, t->tm_mon+1, t->tm_mday);
        //log_open(date);
        //packet_open(date);

//lane_log("보증지시 LANE_STAT Go [%02d : %02d : %02d]\n", t->tm_hour, t->tm_min, t->tm_sec);
        hi_comm_ctrl.warr_snd_no = lst_rcv_acc_no;
        //hi_comm_ctrl.warr_snd_no = hi_comm_ctrl.lane_stat_info.lst_rev_snd_no;
        hi_comm_ctrl.snd.len = snd_warr_snd_no(0);
		lane_comm_snd();
	}

//lane_log("차로상태정보 끝\n");
    return 0;
}

/*
 *========================================================================
 *
 *   Function : rcv_rpr_data()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.28
 *   Modified :
 *   Remark   : 보수 DATA 수신처리
 *
 *========================================================================
*/
int rcv_rpr_data()
{
    struct rcv_lane_data  rd;
#if 0
    struct    write_info      wi;
#endif
    struct    hi_rpr_data    *hrd;
    int ret = 0;
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));

    hrd = (struct hi_rpr_data *)hi_comm_ctrl.rcv_ctrl.buf;
    memcpy(hi_comm_ctrl.write_info.year, &hrd->year, 4);
    memcpy(hi_comm_ctrl.write_info.mon, &hrd->month, 2);
    memcpy(hi_comm_ctrl.write_info.day, &hrd->day, 2);
    memcpy(hi_comm_ctrl.write_info.hr, &hrd->hour, 2);
    memcpy(hi_comm_ctrl.write_info.min, &hrd->min, 2);
    memcpy(hi_comm_ctrl.write_info.sec, &hrd->sec, 2);

    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;

    if(memcmp(hrd->wrk_no, "0000", 4) == 0)
        memcpy(hi_comm_ctrl.write_info.wrk_no, hrd->terminal_no, sizeof(hi_comm_ctrl.write_info.wrk_no));

    sprintf(tmp, "%.8s%c", hrd->seq_no, '\0');
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
	hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = strtoul(tmp, NULL, 16);

#if 0
    memset(&rd, 0x20, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.bas_tbl.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);

    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            sprintf((char *)rd.wrkr_no, "SYSI");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            sprintf((char *)rd.wrkr_no, "SYSO");
        else
            sprintf((char *)rd.wrkr_no, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
    sprintf((char *)rd.buf, "%s", (char *)hrd);

    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 보수처리[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }
#endif
	if(ret == 0)
	{
		lst_rcv_acc_no = strtoul(tmp, NULL, 16);
	}

    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_close_pass()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2019.05.12
 *   Modified :
 *   Remark   : 야간계수 통행 DATA
 *
 *========================================================================
*/
int rcv_close_pass()
{
    struct rcv_lane_data  rd;
    struct    write_info      wi;
    struct    hi_close_pass    *hclp;
    int ret, retw;
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
	char bak[MAX_STRING_SIZE]; memset(bak, 0x00, sizeof(bak));
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));
	char mCarNo[3+1];

    hclp = (struct hi_close_pass *)hi_comm_ctrl.rcv_ctrl.buf;
    e_memcpy(hi_comm_ctrl.write_info.year, hclp->year);
    e_memcpy(hi_comm_ctrl.write_info.mon, hclp->month);
    e_memcpy(hi_comm_ctrl.write_info.day, hclp->day);
    e_memcpy(hi_comm_ctrl.write_info.hr, hclp->hour);
    e_memcpy(hi_comm_ctrl.write_info.min, hclp->min);
    e_memcpy(hi_comm_ctrl.write_info.sec, hclp->sec);

    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    sprintf(tmp, "%.8s%c", hclp->seq_no, '\0');
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
	hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = strtoul(tmp, NULL, 16);
	sprintf(wi.plz_id, "%.2s", PLZ_CODE2);
    lb_freadx(&wi, FNO_BASICTBL, &hi_comm_ctrl.bas_tbl, FFIRST);
    memset(&rd, 0x20, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.bas_tbl.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);
	if(strlen(rd.wrk_no) == 0)
		sprintf((char *)rd.wrk_no, "%.2s00", rd.lane_no);

    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            sprintf((char *)rd.wrkr_no, "SYSI");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            sprintf((char *)rd.wrkr_no, "SYSO");
        else
            sprintf((char *)rd.wrkr_no, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
	sprintf((char *)bak, "%.8s%.2s%.8s%.2s%.6s%.4s%.2s%.2s%.2s%.2s%.2s%c%.2s%c%c%-16s%.4s%.4s%.4s%.4s",
	        hclp->seq_no, hclp->kind, hclp->wrk_date, hclp->lane_no, hclp->serial_no, hclp->year, hclp->month, hclp->day,
			hclp->hour, hclp->min, hclp->sec, hclp->io_div, hclp->plz_id, hclp->car_kind, hclp->image_yn,
			hclp->car_no, hclp->x_point, hclp->y_point, hclp->img_width, hclp->img_heigh);
    //sprintf((char *)rd.buf, "%s", (char *)hclp);
	snprintf((char *)rd.buf, strlen(bak)+1, "%s", bak);

    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 야간계수[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }
#if 0
    memset(bak, 0x00, sizeof(bak));

	//RemoveTerminatedThread();

    sprintf((char *)bak, "%.2s%.2s%.4s%.2s%.2s%.2s%.2s%.2s%.6s%-16s%c%c%c", 
			hclp->plz_id, hclp->lane_no, hclp->year, hclp->month, hclp->day, hclp->hour, hclp->min, hclp->sec,
			hclp->serial_no, hclp->car_no, hclp->car_kind, hclp->io_div, '\0');

//lane_log("야간계수 Web 송신[%s]\n", bak);
    retw = MessageSender(NS_WEB_CLOSE_PASS, (char *)&bak, path, rtn);
    //usleep(300000);

    if(retw < 0)
    {
        lane_log("(rcv_function.cpp) Web 야간계수데이터[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
    }
#endif
	if(ret == 0)
	{
		lst_rcv_acc_no = strtoul(tmp, NULL, 16);
	}
#ifdef _DEV_
    //장애 대응을 위한 처리자료 파일화
    char queue_d[128]; memset(queue_d, 0x00, sizeof(queue_d));
    char queue_f[128]; memset(queue_f, 0x00, sizeof(queue_f));
	char back_car[128]; memset(back_car, 0x00, sizeof(back_car));
	int div_carkind;
	FILE *backup_f;
	sprintf(queue_d, "%s/%.8s%c", LOG_DIR, rd.wrk_datetm, '\0');
	mkdir(queue_d,0755);
	sprintf(queue_f, "%s/queue%.2s%.2s.log", queue_d, rd.plz_id, rd.lane_no);
/*
	div_carkind = hclp->car_kind - '0';
	sprintf(back_car, "%.16s", hclp->car_no);
	del_space(back_car);

	if(strlen(back_car) == 0)
		sprintf(back_car, "????????????");
    else
		div_carkind = chg_car_kind(back_car, hclp->car_kind);
*/
	backup_f = fopen(queue_f, "a+t");
	fprintf(backup_f, "%.2s%.2s%.2s|%.2s%.2s%.14s%c%.2s%.4s%.4s%c%.10s%.10s%.10s%s\n",
                    hclp->hour, hclp->min, hclp->sec, rd.kind, rd.plz_id, rd.wrk_datetm, rd.io_div, rd.lane_no, rd.wrk_no, rd.wrkr_no, rd.con_stat, rd.rcv_snd_no, rd.st_snd_no, rd.ed_snd_no, rd.buf);
/*
	fprintf(backup_f, "INSERT INTO DAY_TRAFFIC_INFO(FARE_OFFICE_ID, BOOTH_ID, PASS_DATE, PASS_TIME, PASS_SEQNO, CAR_NO, CARS_TYPE_ID, IN_OUT_KBN, CREATE_DATE, CREATER, UPDATE_DATE, UPDATER) VALUES ('%.2s','%.2s','%.4s%.2s%.2s','%.2s%.2s%.2s',%.6s,'%s','%s','%c',sysdate,'system',sysdate,'system');\n",
                      rd.plz_id, rd.lane_no, hclp->year, hclp->month, hclp->day, hclp->hour, hclp->min, hclp->sec, hclp->serial_no, back_car, div_carkind, hclp->io_div);
*/
    fclose(backup_f);
#endif
    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_wrk_strt()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.28
 *   Modified :
 *   Remark   : 근무개시보고
 *
 *========================================================================
*/
int rcv_wrk_strt()
{
    struct rcv_lane_data  rd;
#if 1
    struct    write_info      wi;
#endif
    struct    hi_wrk_start    *hws;
    int    wrk_order, ret = 0;
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));

    hws = (struct hi_wrk_start *)hi_comm_ctrl.rcv_ctrl.buf;

    if (hi_comm_ctrl.now_stat_info & H_ACC_RESND)
    {
        /* 재송요구시 : 재송요구 근무 DATA  */
        if (memcmp(hws->wrk_no, hi_comm_ctrl.manager[LW_RESND_NO-1].lo_lane_ctrl_cmd.wrk_no, 4) != 0 ||
            memcmp(hws->wrk_start_year, hi_comm_ctrl.manager[LW_RESND_NO-1].lo_lane_ctrl_cmd.wrk_year, 4) != 0 ||
            memcmp(hws->wrk_start_month, hi_comm_ctrl.manager[LW_RESND_NO-1].lo_lane_ctrl_cmd.wrk_mon, 2) != 0 ||
            memcmp(hws->wrk_start_day, hi_comm_ctrl.manager[LW_RESND_NO-1].lo_lane_ctrl_cmd.wrk_day, 2) != 0)
        {
            hi_comm_ctrl.now_stat_info &= ~(H_ACC_RESND|H_FRAME_WAIT);
            lcc_reply(LW_RESND_NO, 1, 1, "재전송 자료의 일시 및 근무번호가 틀립니다.");
            return (-1);
        }
    }

    wrk_order = natoi(&hws->wrk_no[2], 2);

    if (wrk_order <= 0 || wrk_order > MAX_WRKCNT)
        return (-1);                /* 최대근무수초과 */

    if(natoi(&hws->wrk_no[0], 2) != (int)hi_comm_ctrl.lane_no){
        lane_log("근무 차로번호 불일치 %.4s != %d\n",hws->wrk_no,hi_comm_ctrl.lane_no);
        return (-1);         /* 근무번호 앞2비교 */
    }

    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_no         , hws->wrk_no         ); /* 근무번호    */
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrkr_no        , hws->wrkr_no        );

    sprintf(tmp, "%.8s%c", hws->seq_no, '\0');
    hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = strtoul(tmp, NULL, 16);

    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_strt_year  , hws->wrk_start_year );
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_strt_mon   , hws->wrk_start_month);
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_strt_day   , hws->wrk_start_day  );
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_strt_hr    , hws->wrk_start_hour );
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_strt_min   , hws->wrk_start_min  );
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_strt_sec   , hws->wrk_start_sec  );

	//유지보수 근무자번호 정의시 수정
	if(memcmp(hws->wrkr_no, "6", 1) == 0)
	{
		hi_comm_ctrl.write_info.wrk_knd = WS_RPR;
		hi_comm_ctrl.lane_stat_info.wrk_stat = WS_RPR;
    }else{
		hi_comm_ctrl.write_info.wrk_knd = WS_NORMAL;
		hi_comm_ctrl.lane_stat_info.wrk_stat = WS_NORMAL;
	}

    memcpy(hi_comm_ctrl.write_info.year, hws->wrk_year, 4); /* 근무일자 */
    memcpy(hi_comm_ctrl.write_info.mon, hws->wrk_month, 2);
    memcpy(hi_comm_ctrl.write_info.day, hws->wrk_day, 2);

    e_memcpy(hi_comm_ctrl.write_info.wrk_no, hws->wrk_no); /* 근무번호    */
    e_memcpy(hi_comm_ctrl.write_info.wrkr_no, hws->wrkr_no); /* 근무번호    */

    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;

//lane_log("개시10[%d]\n", hws->lane_typ[0]-'0');
    hi_comm_ctrl.write_info.io_div = hws->io_div;

//lane_log("개시11[%x]\n", hws->seq_no);
//    hi_comm_ctrl.write_info.io_div = hws->lane_typ[0];

//lane_log("개시 11-1[%x]\n", strtoul(tmp, NULL, 16));
    hi_comm_ctrl.write_info.strt_snd_no = strtoul(tmp, NULL, 16);
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);

	sprintf(wi.plz_id, "%.2s", PLZ_CODE2);
    lb_freadx(&wi, FNO_BASICTBL, &hi_comm_ctrl.bas_tbl, FFIRST);

    memset(&rd, 0x20, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.bas_tbl.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);
//lane_log("차로번호[%.2s]\n", rd.lane_no);
    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            sprintf((char *)rd.wrkr_no, "SYSI");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            sprintf((char *)rd.wrkr_no, "SYSO");
        else
            sprintf((char *)rd.wrkr_no, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
    sprintf((char *)rd.buf, "%s", (char *)hws);

    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 근무개시[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }

    if (wkr_no2wrk_stat(hws->wrkr_no) == W_MTNC)        /* 근무상태 */
        hi_comm_ctrl.now_stat_info |= H_MTNC;            /* 유지보수원 근무중 */
    else
        hi_comm_ctrl.now_stat_info &= ~H_MTNC;

    if (!(hi_comm_ctrl.now_stat_info & H_ERR_REST))
        hi_comm_ctrl.now_stat_info &= ~H_LANE_CLS;            /* 폐쇄 : 해제 */
//printf("W15\n");
	if(ret == 0)
	{
		lst_rcv_acc_no = strtoul(tmp, NULL, 16);
	}

    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_hnd_data()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2019.05.12
 *   Modified :
 *   Remark   : 처리 데이터
 *
 *========================================================================
*/
int rcv_hnd_data()
{
    struct rcv_lane_data  rd;
    struct    write_info      wi;
    struct    hi_hnd_data    *hcd;
    int ret, retw;
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
	char bak[MAX_STRING_SIZE]; memset(bak, 0x00, sizeof(bak));
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));
	char rfid_kbn[2]; memset(rfid_kbn, 0x00, sizeof(rfid_kbn));
	char serial_no[8]; memset(serial_no, 0x00, sizeof(serial_no));
	char car_kind;

    hcd = (struct hi_out_way_hnd_data *)hi_comm_ctrl.rcv_ctrl.buf;
    e_memcpy(hi_comm_ctrl.write_info.year, hcd->hnd_info.hnd_year );
    e_memcpy(hi_comm_ctrl.write_info.mon,  hcd->hnd_info.hnd_month);
    e_memcpy(hi_comm_ctrl.write_info.day,  hcd->hnd_info.hnd_day  );
    e_memcpy(hi_comm_ctrl.write_info.hr,   hcd->hnd_info.hnd_hour );
    e_memcpy(hi_comm_ctrl.write_info.min,  hcd->hnd_info.hnd_min  );
    e_memcpy(hi_comm_ctrl.write_info.sec,  hcd->hnd_info.hnd_sec  );

    e_memcpy(hi_comm_ctrl.write_info.wrk_no, hcd->wrk_no);        /* 근무번호     */
	hi_comm_ctrl.write_info.io_div = hcd->io_div;

/* 2019. 04.17 추가 */
    e_memcpy(hi_comm_ctrl.write_info.wrkr_no, hcd->wrkr_no);
    hi_comm_ctrl.write_info.io_div = hcd->io_div;

    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    sprintf(tmp, "%.8s%c", hcd->seq_no, '\0');
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
	hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = strtoul(tmp, NULL, 16);

	sprintf(wi.plz_id, "%.2s", PLZ_CODE2);
    lb_freadx(&wi, FNO_BASICTBL, &hi_comm_ctrl.bas_tbl, FFIRST);

    memset(&rd, 0x00, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.bas_tbl.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);

    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            snprintf((char *)rd.wrkr_no, 4+1, "SYSI");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            snprintf((char *)rd.wrkr_no, 4+1, "SYSO");
        else
            snprintf((char *)rd.wrkr_no, 4+1, "0000");
    }else
        snprintf((char *)rd.wrkr_no, 4+1, "%.4s", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
#if 1
/* 2019.07.26 감지시간 추가 */
	sprintf(bak, "%.8s%.2s%.8s%.4s%.4s%.6s%c%.2s%.2s%c%.6s%.6s%.4s%.2s%.2s%.2s%.2s%.2s%.6s%.6s%.6s%c%c%.2s%c%.7s%.20s%.2s%c%c%-16s%-16s%.4s%.4s%.4s%.4s%.2s%.24s%.24s%c",
	             hcd->seq_no, hcd->kind, hcd->wrk_date, hcd->wrk_no, hcd->wrkr_no, hcd->serial_no,
				 hcd->io_div, hcd->plz_id, hcd->lane_no, hcd->car_kind, hcd->sence_seq_no, hcd->sence_time,
				 hcd->hnd_info.hnd_year, hcd->hnd_info.hnd_month, hcd->hnd_info.hnd_day,
				 hcd->hnd_info.hnd_hour, hcd->hnd_info.hnd_min, hcd->hnd_info.hnd_sec, hcd->hnd_info.org_pass_fare, 
				 hcd->hnd_info.pass_fare, hcd->hnd_info.rcpt_money, hcd->hnd_info.refund_yn, hcd->hnd_info.key_yn,
				 hcd->hnd_info.rcpt_hnd, hcd->hnd_info.red_div,
				 hcd->hnd_info.issue_code, hcd->hnd_info.crd_no, hcd->hnd_info.rcpt_div, hcd->pre_yn, hcd->img_yn,
				 hcd->car_no1, hcd->car_no2, hcd->x_point, hcd->y_point, hcd->img_width, hcd->img_heigh, hcd->dc_typ,
				 hcd->tag_id1, hcd->tag_id2, '\0');
#else
	sprintf(bak, "%.8s%.2s%.8s%.4s%.4s%.6s%c%.2s%.2s%c%.6s%.4s%.2s%.2s%.2s%.2s%.2s%.6s%.6s%.6s%c%c%.2s%c%.7s%.20s%.2s%c%c%-16s%-16s%.4s%.4s%.4s%.4s%.2s%.24s%.24s%c",
	             hcd->seq_no, hcd->kind, hcd->wrk_date, hcd->wrk_no, hcd->wrkr_no, hcd->serial_no,
				 hcd->io_div, hcd->plz_id, hcd->lane_no, hcd->car_kind, hcd->sence_seq_no,
				 hcd->hnd_info.hnd_year, hcd->hnd_info.hnd_month, hcd->hnd_info.hnd_day,
				 hcd->hnd_info.hnd_hour, hcd->hnd_info.hnd_min, hcd->hnd_info.hnd_sec, hcd->hnd_info.org_pass_fare, 
				 hcd->hnd_info.pass_fare, hcd->hnd_info.rcpt_money, hcd->hnd_info.refund_yn, hcd->hnd_info.key_yn,
				 hcd->hnd_info.rcpt_hnd, hcd->hnd_info.red_div,
				 hcd->hnd_info.issue_code, hcd->hnd_info.crd_no, hcd->hnd_info.rcpt_div, hcd->pre_yn, hcd->img_yn,
				 hcd->car_no1, hcd->car_no2, hcd->x_point, hcd->y_point, hcd->img_width, hcd->img_heigh, hcd->dc_typ,
				 hcd->tag_id1, hcd->tag_id2, '\0');
#endif
    //sprintf((char *)rd.buf, "%s", (char *)hcd);
	snprintf((char *)rd.buf, strlen(bak)+1, "%s", bak);
//lane_log("처리데이터[%s]\n", rd.buf);
    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 처리[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }
   
    //usleep(300000);
#if 0
    memset(bak, 0x00, sizeof(bak));

    RemoveTerminatedThread();

	switch(lb_data2atoi(hcd->hnd_info.rcpt_hnd, 2))
	{
		case 10:
		case 11:
		case 20:
		case 21:
		case 22:
		case 23:
		case 24:
		case 30:
		case 31:
		case 32:
		case 34:
		case 35:
		case 36:
		case 37:
		case 38:
		case 39:
		case 70:
		case 71:
		case 74:
		case 75:
		case 76:
		case 77:
		case 78:
		case 79:
		case 40:
		case 41:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 60:
		case 61:
		case 62:
		case 63:
		case 64:
		case 65:
		case 66:
		case 67:
			sprintf(rfid_kbn, "00");
			break;
		case 12:
		case 25:
		case 26:
		case 55:
			sprintf(rfid_kbn, "01");
			break;
		case 13:
		case 14:
		case 27:
		case 28:
		case 56:
			sprintf(rfid_kbn, "03");
			break;
		case 33:
			sprintf(rfid_kbn, "02");
			break;
		default:
			sprintf(rfid_kbn, "00");
			break;
	}

    switch(lb_data2atoi(hcd->hnd_info.rcpt_hnd, 2))
	{
		case 31:
            car_kind = 0x36;
		break;
		case 36:
			car_kind = 0x37;
		break;
		default:
			car_kind = hcd->car_kind;
		break;
	}

    if(lb_data2atoi(hcd->hnd_info.rcpt_hnd, 2) > 19 && lb_data2atoi(hcd->hnd_info.rcpt_hnd, 2) < 30)
	{
		sprintf(serial_no, "%c%.2s%05u", hcd->plz_id[1], hcd->lane_no, lb_data2atoi(hcd->serial_no, 6));
	}else
	{
		sprintf(serial_no, "%08u", lb_data2atoi(hcd->serial_no, 6));
	}
    sprintf((char *)bak, "%.4s%.2s%.2s%.8s%.2s%.2s%.2s%.8s%.8s%-16s%.2s0  %.6s%.3s%.3s%.3s%.3s%c%c%.2s%-24s%-24s%c%c", 
			hcd->wrkr_no, hcd->plz_id, hcd->lane_no, hcd->wrk_date, hcd->hnd_info.hnd_hour, hcd->hnd_info.hnd_min, hcd->hnd_info.hnd_sec,
			serial_no, serial_no, hcd->car_no1, hcd->hnd_info.rcpt_hnd,
			hcd->hnd_info.pass_fare, hcd->x_point+1, hcd->y_point+1, hcd->img_width+1, hcd->img_heigh+1,
			hcd->hnd_info.refund_yn, hcd->wrk_no[3], rfid_kbn, hcd->tag_id1, hcd->tag_id2, car_kind, '\0');

//lane_log("처리데이터 Web 송신[%s]\n", bak);
    retw = MessageSender(NS_WEB_HND_DATA, (char *)&bak, path, rtn);

    if(retw < 0)
    {
        lane_log("(rcv_function.cpp) Web 처리데이터[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
    }
#endif

	if(ret == 0)
	{
		lst_rcv_acc_no = strtoul(tmp, NULL, 16);
	}
#ifdef _DEV_
    //장애 대응을 위한 처리자료 파일화
    char queue_d[128]; memset(queue_d, 0x00, sizeof(queue_d));
    char queue_f[128]; memset(queue_f, 0x00, sizeof(queue_f));
	char back_car[128]; memset(back_car, 0x00, sizeof(back_car));
	int div_carkind;
	FILE *backup_f;
	sprintf(queue_d, "%s/%.8s%c", LOG_DIR, rd.wrk_datetm, '\0');
	mkdir(queue_d,0755);
	sprintf(queue_f, "%s/queue%.2s%.2s.log", queue_d, rd.plz_id, rd.lane_no);

	backup_f = fopen(queue_f, "a+t");
	fprintf(backup_f, "%.2s%.2s%.2s|%.2s%.2s%.14s%c%.2s%.4s%.4s%c%.10s%.10s%.10s%s\n",
                    hcd->hnd_info.hnd_hour, hcd->hnd_info.hnd_min, hcd->hnd_info.hnd_sec, rd.kind, rd.plz_id, rd.wrk_datetm, rd.io_div, rd.lane_no, rd.wrk_no, rd.wrkr_no, rd.con_stat, rd.rcv_snd_no, rd.st_snd_no, rd.ed_snd_no, rd.buf);

    fclose(backup_f);
#endif

    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_card_data()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2019.05.12
 *   Modified :
 *   Remark   : 카드처리 데이터
 *
 *========================================================================
*/
int rcv_card_data()
{
    struct rcv_lane_data  rd;
    struct    write_info      wi;
    struct    hi_crd_hnd_data    *hcd;
    int ret, retw;
	int ii, size;
	
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
	char bak[MAX_STRING_SIZE]; memset(bak, 0x00, sizeof(bak));
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));

	char sam_seq[10+1]; memset(sam_seq, 0x00, sizeof(sam_seq));
	char tot_sam_seq[10+1]; memset(tot_sam_seq, 0x00, sizeof(tot_sam_seq));
	char sam_coll_cnt[5+1]; memset(sam_coll_cnt, 0x00, sizeof(sam_coll_cnt));
	char sam_tot_amt[10+1]; memset(sam_tot_amt, 0x00, sizeof(sam_tot_amt));
	char tmp_iss_cd[6+1]; memset(tmp_iss_cd, 0x00, sizeof(tmp_iss_cd));
	char issue_id[7+1]; memset(issue_id, 0x00, sizeof(issue_id));
	char path[256]; memset(path, 0x00, sizeof(path));
	
	FILE  *fp;
	struct stat file_info;
	struct iss_comp_tbl iss_comp_tbl_f;

    hcd = (struct hi_in_way_hnd_data *)hi_comm_ctrl.rcv_ctrl.buf;
    e_memcpy(hi_comm_ctrl.write_info.year, hcd->hnd_year );
    e_memcpy(hi_comm_ctrl.write_info.mon,  hcd->hnd_month);
    e_memcpy(hi_comm_ctrl.write_info.day,  hcd->hnd_day  );
    e_memcpy(hi_comm_ctrl.write_info.hr,   hcd->hnd_hour );
    e_memcpy(hi_comm_ctrl.write_info.min,  hcd->hnd_min  );
    e_memcpy(hi_comm_ctrl.write_info.sec,  hcd->hnd_sec  );

    e_memcpy(hi_comm_ctrl.write_info.wrk_no , hcd->wrk_no);        /* 근무번호     */
    e_memcpy(hi_comm_ctrl.write_info.wrkr_no, hcd->wrkr_no);

    hi_comm_ctrl.write_info.io_div = hcd->io_div;
	sprintf(wi.plz_id, "%.2s", PLZ_CODE2);
    lb_freadx(&wi, FNO_BASICTBL, &hi_comm_ctrl.bas_tbl, FFIRST);

    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    sprintf(tmp, "%.8s%c", hcd->seq_no, '\0');
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
	hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = strtoul(tmp, NULL, 16);
    
    memset(&rd, 0x00, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.bas_tbl.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);

    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            snprintf((char *)rd.wrkr_no, 4+1, "SYSI");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            snprintf((char *)rd.wrkr_no, 4+1, "SYSO");
        else
            snprintf((char *)rd.wrkr_no, 4+1, "0000");
    }else
        snprintf((char *)rd.wrkr_no, 4+1, "%.4s", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
    //sprintf((char *)rd.buf, "%s", (char *)hcd);
	sprintf((char *)bak, "%.8s%.2s%.8s%.4s%.4s%.6s%c%.2s%.4s%.2s%.2s%.2s%.2s%.2s%.6s%.6s%.8s%.8s%.2s%.2s%c%c%.20s%.2s%.10s%.10s%.2s%.16s%.8s%.2s%.6s%.10s%.8s%.8s%.10s%.8s%.4s%.8s%.2s%.8s%.2s%.8s%.2s%.7s%.2s%.10s%-16s%-16s%.12s%c",
	        hcd->seq_no, hcd->kind, hcd->wrk_date, hcd->wrk_no, hcd->wrkr_no, hcd->serial_no, hcd->io_div, hcd->plz_id,
			hcd->hnd_year, hcd->hnd_month, hcd->hnd_day, hcd->hnd_hour, hcd->hnd_min, hcd->hnd_sec,
			hcd->pass_fare, hcd->rcpt_money, hcd->bef_rcpt_balc, hcd->aft_rcpt_balc, hcd->rcpt_div, hcd->crd_kind,
			hcd->pre_div, hcd->car_kind, hcd->crd_no, hcd->pay_div, hcd->terminal_no, hcd->terminal_cnt, 
			hcd->alg_id, hcd->psam_id, hcd->purch_dt, hcd->deal_div, hcd->elec_iss_id, hcd->elec_id, hcd->elec_ser_no, hcd->deal_ser_no, 
			hcd->crd_ser_no, hcd->deal_tot, hcd->ind_iss_cnt, hcd->rcpt_deal_ser, hcd->ind_sign1, hcd->psam_sign1, hcd->ind_sign2,
			hcd->psam_sign2,  hcd->id_center, hcd->iss_code, hcd->another, hcd->alias_no, hcd->car_no1, hcd->car_no2, hcd->tot_id, '\0');
    snprintf((char *)rd.buf, strlen(bak)+1, "%s", bak);
//lane_log("카드처리데이터[%s]\n", rd.buf);
    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);
    //usleep(300000);
    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 카드처리[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }
#if 0
    memset(bak, 0x00, sizeof(bak));

	RemoveTerminatedThread();

    snprintf(sam_seq, 8+1, "%s%c", hcd->deal_ser_no, '\0');
	snprintf(tot_sam_seq, 8+1, "%s%c", hcd->rcpt_deal_ser, '\0');
	snprintf(sam_coll_cnt, 4+1, "%s%c", hcd->ind_iss_cnt, '\0');
	snprintf(sam_tot_amt, 8+1, "%s%c", hcd->deal_tot, '\0');
    sprintf(tmp_iss_cd, "%.6s%c", hcd->crd_no, '\0');

	sprintf(path, "%s/%s/ISSTBL", DIR_TCS, DIR_BASEINFO);
	fp = fopen(path, "r+");
	if( 0 > stat(path, &file_info))
    {
        proc_log("발행사정보 없음\n");
    }else
    {
		size = file_info.st_size;
//proc_log("파일 Size[%d]\n", size);
		for(ii = 0; ii < (size / sizeof(struct iss_comp_tbl)); ii++)
		{
			fseek(fp, ii * sizeof(struct iss_comp_tbl), SEEK_SET);
			fread(&iss_comp_tbl_f, sizeof(struct iss_comp_tbl), 1, fp);
//proc_log("[%d]FILE BIN[%.6s] ISSCD[%.6s]\n", ii, iss_comp_tbl_f.bin_code+2, tmp_iss_cd);
			if(strncmp(iss_comp_tbl_f.bin_code+2, tmp_iss_cd, 6) == 0)
				break;
		}
	}

    snprintf(issue_id, 7+1, "%.7s", iss_comp_tbl_f.issue_cd);
	
    sprintf((char *)bak, "%.4s%.2s%.2s%.8s%.2s%.2s%.2s%.8s%c%-16s%.2s%.8s%.8s%.8s%.9s%-16s%010u%.10s%.2s%.2s%.2s%10u%05u%010u%.8s%.2s9100000000%.2s%.10s%.7s%c", 
			hcd->wrkr_no, hcd->plz_id, hcd->lane_no, hcd->wrk_date, hcd->hnd_hour, hcd->hnd_min, hcd->hnd_sec,
			hcd->serial_no, hcd->pre_div, hcd->crd_no, hcd->rcpt_div, hcd->bef_rcpt_balc, hcd->rcpt_money, hcd->aft_rcpt_balc,
			hcd->terminal_no, hcd->psam_id, strtoul(sam_seq, NULL, 16), hcd->crd_ser_no, hcd->alg_id, hcd->ind_sign1, hcd->id_center, 
			strtoul(tot_sam_seq, NULL, 16), strtoul(sam_coll_cnt, NULL, 16), strtoul(sam_tot_amt, NULL, 16), hcd->psam_sign1,
			hcd->another, hcd->lane_no, hcd->in_alias_no, issue_id, '\0');

//lane_log("카드처리데이터 Web 송신[%s]\n", bak);
    retw = MessageSender(NS_WEB_CRD_HND_DATA, (char *)&bak, path, rtn);

    if(retw < 0)
    {
        lane_log("(rcv_function.cpp) Web 카드처리데이터[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
    }
#endif
	if(ret == 0)
	{
		lst_rcv_acc_no = strtoul(tmp, NULL, 16);
	}
#ifdef _DEV_
    //장애 대응을 위한 처리자료 파일화
    char queue_d[128]; memset(queue_d, 0x00, sizeof(queue_d));
    char queue_f[128]; memset(queue_f, 0x00, sizeof(queue_f));
	char back_car[128]; memset(back_car, 0x00, sizeof(back_car));
	int div_carkind;
	FILE *backup_f;
	sprintf(queue_d, "%s/%.8s%c", LOG_DIR, rd.wrk_datetm, '\0');
	mkdir(queue_d,0755);
	sprintf(queue_f, "%s/queue%.2s%.2s.log", queue_d, rd.plz_id, rd.lane_no);

	backup_f = fopen(queue_f, "a+t");
	fprintf(backup_f, "%.2s%.2s%.2s|%.2s%.2s%.14s%c%.2s%.4s%.4s%c%.10s%.10s%.10s%s\n",
                    hcd->hnd_hour, hcd->hnd_min, hcd->hnd_sec, rd.kind, rd.plz_id, rd.wrk_datetm, rd.io_div, rd.lane_no, rd.wrk_no, rd.wrkr_no, rd.con_stat, rd.rcv_snd_no, rd.st_snd_no, rd.ed_snd_no, rd.buf);

    fclose(backup_f);
#endif

    return (0);
}

/*
 *========================================================================
 *
 *   Function : remote_lane_stat_rlt()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2019.05.12
 *   Modified :
 *   Remark   : 원격차로상태응답
 *
 *========================================================================
*/
int remote_lane_stat_rlt()
{
    struct rcv_lane_data  rd;
    struct    hi_remote_lane_stat_reply    *hrlsr;
    int ret;
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));

    hrlsr = (struct hi_hnd_data *)hi_comm_ctrl.rcv_ctrl.buf;
    sprintf((char *)hi_comm_ctrl.write_info.year, "%04d", hi_comm_ctrl.cday.tm_year+1900);
    sprintf((char *)hi_comm_ctrl.write_info.mon,  "%02d", hi_comm_ctrl.cday.tm_mon+1);
    sprintf((char *)hi_comm_ctrl.write_info.day,  "%02d", hi_comm_ctrl.cday.tm_mday);
    sprintf((char *)hi_comm_ctrl.write_info.hr,   "%02d", hi_comm_ctrl.cday.tm_hour);
    sprintf((char *)hi_comm_ctrl.write_info.min,  "%02d", hi_comm_ctrl.cday.tm_min);
    sprintf((char *)hi_comm_ctrl.write_info.sec,  "%02d", hi_comm_ctrl.cday.tm_sec);

    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    sprintf(tmp, "%.8s%c", hrlsr->seq_no, '\0');
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
    
    memset(&rd, 0x20, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.write_info.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);

    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            sprintf((char *)rd.wrkr_no, "SYSI");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            sprintf((char *)rd.wrkr_no, "SYSO");
        else
            sprintf((char *)rd.wrkr_no, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
    sprintf((char *)rd.buf, "%s", (char *)hrlsr);

    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 원격차로상태응답[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }

    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_wrk_end()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.31
 *   Modified :
 *   Remark   : 근무 종료
 *
 *========================================================================
*/
int rcv_wrk_end()
{
    struct rcv_lane_data  rd;
    struct    write_info wi;
    struct    hi_wrk_end *howwe;
    int    wrk_order, ret, retw;
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
	char bak[MAX_STRING_SIZE]; memset(bak, 0x00, sizeof(bak));
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));

	int cash = 0, k_cash = 0, free = 0, minap = 0, coupon = 0, k_coupon = 0;
	int sun = 0, k_sun = 0, who = 0, k_who = 0;
	int sale = 0, k_sale = 0, tot_amt = 0, repay_q = 0, repay_m = 0;

    howwe = (struct hi_out_way_wrk_end *)hi_comm_ctrl.rcv_ctrl.buf;
    e_memcpy(hi_comm_ctrl.write_info.year, howwe->wrk_end_year );
    e_memcpy(hi_comm_ctrl.write_info.mon,  howwe->wrk_end_month);
    e_memcpy(hi_comm_ctrl.write_info.day,  howwe->wrk_end_day  );
    e_memcpy(hi_comm_ctrl.write_info.hr,   howwe->wrk_end_hour );
    e_memcpy(hi_comm_ctrl.write_info.min,  howwe->wrk_end_min  );
    e_memcpy(hi_comm_ctrl.write_info.sec,  howwe->wrk_end_sec  );
    e_memcpy(hi_comm_ctrl.write_info.wrk_no, howwe->wrk_no);    /* 근무번호 */
    e_memcpy(hi_comm_ctrl.write_info.wrkr_no, howwe->wrkr_no);

    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    sprintf(tmp, "%.8s%c", howwe->seq_no, '\0');
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
    hi_comm_ctrl.write_info.end_snd_no = strtoul(tmp, NULL, 16);
	hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = strtoul(tmp, NULL, 16);

    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_end_year  , howwe->wrk_end_year );
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_end_mon   , howwe->wrk_end_month);
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_end_day   , howwe->wrk_end_day  );
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_end_hr    , howwe->wrk_end_hour );
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_end_min   , howwe->wrk_end_min  );
    e_memcpy(hi_comm_ctrl.lane_stat_info.wrk_end_sec   , howwe->wrk_end_sec  );
	
	hi_comm_ctrl.lane_stat_info.wrk_stat = WS_NONE;

    wrk_order = natoi(&hi_comm_ctrl.write_info.wrk_no[2], 2);
    if (wrk_order <= 0 || wrk_order > MAX_WRKCNT)
        return (-1);

	sprintf(wi.plz_id, "%.2s", PLZ_CODE2);
    lb_freadx(&wi, FNO_BASICTBL, &hi_comm_ctrl.bas_tbl, FFIRST);

    memset(&rd, 0x20, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.bas_tbl.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);
//lane_log("차로번호[%.2s]\n", rd.lane_no);
    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            sprintf((char *)rd.wrkr_no, "SYSI");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            sprintf((char *)rd.wrkr_no, "SYSO");
        else
            sprintf((char *)rd.wrkr_no, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
    sprintf((char *)rd.buf, "%s", (char *)howwe);

    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 근무종료[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }
    hi_comm_ctrl.warr_snd_no = hi_comm_ctrl.write_info.end_snd_no;
    if(!(hi_comm_ctrl.now_stat_info & H_ERR_REST) || !(hi_comm_ctrl.now_stat_info & H_ACC_RESND))
	{
		hi_comm_ctrl.snd.len = snd_warr_snd_no(0);
		lane_comm_snd();
	}
    //hi_comm_ctrl.snd.len = snd_warr_snd_no();/* 보증 전송연번 송신 */
//lane_log("출구 보증전송연번 송신?[%d]\n", hi_comm_ctrl.snd.len);
    hi_comm_ctrl.now_stat_info |= H_LANE_CLS;            /* 차로폐쇄중    */

	if(ret == 0)
	{
		lst_rcv_acc_no = strtoul(tmp, NULL, 16);
	}

#if 0
    memset(bak, 0x00, sizeof(bak));

	RemoveTerminatedThread();

    for(ii = 0; ii < 50; ii++)
    {
        switch(lb_data2atoi(howwe->hnd_cnt[ii].div, 2))
        {
            case 10:
                cash += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 11:
            case 12:
            case 13:
            case 14:
                k_cash += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 30:
            case 31:
            case 32:
            case 33:
            case 34:
            case 35:
            case 36:
            case 37:
            case 38:
            case 39:
            case 70:
            case 71:
            case 74:
            case 75:
            case 76:
            case 77:
            case 78:
            case 79:
                free += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 40:
            case 41:
            case 60:
            case 61:
            case 62:
			case 63:
			case 64:
			case 65:
			case 66:
			case 67:
                minap += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 51:
                coupon += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 53:
            case 55:
            case 56:
                k_coupon += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 21:
                sun += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 23:
            case 25:
            case 27:
                k_sun += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 22:
                who += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 24:
            case 26:
            case 28:
                k_who += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 52:
                sale += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
            case 54:
                k_sale += lb_data2atoi(howwe->hnd_cnt[ii].cnt, 4);
                break;
        }
    }

    sprintf((char *)bak, "%.4s%.2s%02d%.4s%.2s%.2s%.2s%.2s%.2s%.4s%.2s%.2s%.2s%.2s%.2s%04d%04d%04d%04d%04d%04d%04d%04d%04d%04d%04d%04d%08d%04d%08d%c%c", 
			howwe->wrkr_no, hi_comm_ctrl.bas_tbl.plz_id, hi_comm_ctrl.lane_no,
			howwe->wrk_start_year, howwe->wrk_start_month, howwe->wrk_start_day, howwe->wrk_start_hour, howwe->wrk_start_min, howwe->wrk_start_sec, 
			howwe->wrk_end_year, howwe->wrk_end_month, howwe->wrk_end_day, howwe->wrk_end_hour, howwe->wrk_end_min, howwe->wrk_end_sec, 
			cash, k_cash, free, minap, coupon, k_coupon, sun, k_sun, who, k_who, sale, k_sale, lb_data2atoi(howwe->tot_fare, 8),
			lb_data2atoi(howwe->repay_cnt, 4), lb_data2atoi(howwe->repay_fare, 8), pk_in->wrk_no[3], '\0');

//lane_log("근무종료 Web 송신[%s]\n", bak);
    retw = MessageSender(NS_WEB_CRD_HND_DATA, (char *)&bak, path, rtn);

    if(retw < 0)
    {
        lane_log("(rcv_function.cpp) Web 근무종료[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
    }
#endif
#ifdef _DEV_
    //장애 대응을 위한 처리자료 파일화
    char queue_d[128]; memset(queue_d, 0x00, sizeof(queue_d));
    char queue_f[128]; memset(queue_f, 0x00, sizeof(queue_f));
	char back_car[128]; memset(back_car, 0x00, sizeof(back_car));
	int div_carkind;
	FILE *backup_f;
	sprintf(queue_d, "%s/%.8s%c", LOG_DIR, rd.wrk_datetm, '\0');
	mkdir(queue_d,0755);
	sprintf(queue_f, "%s/queue%.2s%.2s.log", queue_d, rd.plz_id, rd.lane_no);

	backup_f = fopen(queue_f, "a+t");
	fprintf(backup_f, "%.2s%.2s%.2s|%.2s%.2s%.14s%c%.2s%.4s%.4s%c%.10s%.10s%.10s%s\n",
                    howwe->wrk_end_hour, howwe->wrk_end_min, howwe->wrk_end_sec, rd.kind, rd.plz_id, rd.wrk_datetm, rd.io_div, rd.lane_no, rd.wrk_no, rd.wrkr_no, rd.con_stat, rd.rcv_snd_no, rd.st_snd_no, rd.ed_snd_no, rd.buf);

    fclose(backup_f);
#endif
    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_refund_data()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2019.07.01
 *   Modified :
 *   Remark   : 환불처리 데이터
 *
 *========================================================================
*/
int rcv_refund_data()
{
    struct rcv_lane_data  rd;
    struct    write_info wi;
    struct    hi_refund_data *hrd;
    int    wrk_order, ret;
    char rtn[MAX_STRING_SIZE]; memset(rtn, 0x00, sizeof(rtn));
	char bak[MAX_STRING_SIZE]; memset(bak, 0x00, sizeof(bak));
    char tmp[10]; memset(tmp, 0x00, sizeof(tmp));

    hrd = (struct hi_out_way_wrk_end *)hi_comm_ctrl.rcv_ctrl.buf;
#if 0
    e_memcpy(hi_comm_ctrl.write_info.year, hrd->hnd_year );
    e_memcpy(hi_comm_ctrl.write_info.mon,  hrd->hnd_month);
    e_memcpy(hi_comm_ctrl.write_info.day,  hrd->hnd_day  );
    e_memcpy(hi_comm_ctrl.write_info.hr,   hrd->hnd_hour );
    e_memcpy(hi_comm_ctrl.write_info.min,  hrd->hnd_min  );
    e_memcpy(hi_comm_ctrl.write_info.sec,  hrd->hnd_sec  );
#else
    e_memcpy(hi_comm_ctrl.write_info.year, hrd->hnd_info.hnd_year );
    e_memcpy(hi_comm_ctrl.write_info.mon,  hrd->hnd_info.hnd_month);
    e_memcpy(hi_comm_ctrl.write_info.day,  hrd->hnd_info.hnd_day  );
    e_memcpy(hi_comm_ctrl.write_info.hr,   hrd->hnd_info.hnd_hour );
    e_memcpy(hi_comm_ctrl.write_info.min,  hrd->hnd_info.hnd_min  );
    e_memcpy(hi_comm_ctrl.write_info.sec,  hrd->hnd_info.hnd_sec  );
#endif
    e_memcpy(hi_comm_ctrl.write_info.wrk_no, hrd->wrk_no);    /* 근무번호 */
    e_memcpy(hi_comm_ctrl.write_info.wrkr_no, hrd->wrkr_no);
	sprintf(wi.plz_id, "%.2s", PLZ_CODE2);
    lb_freadx(&wi, FNO_BASICTBL, &hi_comm_ctrl.bas_tbl, FFIRST);

    hi_comm_ctrl.write_info.knd = hi_comm_ctrl.rcv_ctrl.cmd;
    sprintf(tmp, "%.8s%c", hrd->seq_no, '\0');
    hi_comm_ctrl.write_info.rcv_snd_no = strtoul(tmp, NULL, 16);
	hi_comm_ctrl.lane_stat_info.lst_rev_snd_no = strtoul(tmp, NULL, 16);

    wrk_order = natoi(&hi_comm_ctrl.write_info.wrk_no[2], 2);
    if (wrk_order <= 0 || wrk_order > MAX_WRKCNT)
        return (-1);

    memset(&rd, 0x20, sizeof(struct rcv_lane_data));
	sprintf((char *)rd.kind, "%02x", hi_comm_ctrl.rcv_ctrl.cmd);
    sprintf((char *)rd.plz_id, "%.2s", hi_comm_ctrl.write_info.plz_id);
    sprintf((char *)rd.wrk_datetm, "%04d%02d%02d%02d%02d%02d",
            hi_comm_ctrl.cday.tm_year+1900, hi_comm_ctrl.cday.tm_mon+1, hi_comm_ctrl.cday.tm_mday,
            hi_comm_ctrl.cday.tm_hour, hi_comm_ctrl.cday.tm_min, hi_comm_ctrl.cday.tm_sec);
    rd.io_div = lb_data2atoi((char *)hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div, 2) + '0';
    sprintf((char *)rd.lane_no, "%02d", hi_comm_ctrl.lane_no);
    sprintf((char *)rd.wrk_no, "%.4s", hi_comm_ctrl.write_info.wrk_no);
//lane_log("차로번호[%.2s]\n", rd.lane_no);
    if(lb_data2atoi(hi_comm_ctrl.write_info.wrkr_no, 4) == 0)
    {
        if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 1)
            snprintf((char *)rd.wrkr_no, 4+1, "SYSI");
        else if(hi_comm_ctrl.bas_tbl.lane_info[hi_comm_ctrl.lane_no -1].io_div[1] - '0' == 2)
            snprintf((char *)rd.wrkr_no, 4+1, "SYSO");
        else
            snprintf((char *)rd.wrkr_no, 4+1, "0000");
    }else
        sprintf((char *)rd.wrkr_no, "%.4s", hi_comm_ctrl.write_info.wrkr_no);

    rd.con_stat = CONNECT_STAT;;
    sprintf((char *)rd.rcv_snd_no, "%010d", hi_comm_ctrl.rcv_ctrl.snd_no);
    sprintf((char *)rd.st_snd_no, "%010d", hi_comm_ctrl.write_info.strt_snd_no);
    sprintf((char *)rd.ed_snd_no, "%010d", hi_comm_ctrl.write_info.end_snd_no);
#if 0
	sprintf((char *)bak, "%.8s%.2s%.8s%.4s%.4s%.6s%.2s%.2s%.4s%.2s%.2s%.2s%.2s%.2s%.6s%.6s%.6s%.4s%.2s%.2s%.2s%.2s%.2s%c",
			hrd->seq_no, hrd->kind, hrd->wrk_date, hrd->wrk_no, hrd->wrkr_no, hrd->serial_no, hrd->plz_id, hrd->lane_no,
			hrd->hnd_year, hrd->hnd_month, hrd->hnd_day, hrd->hnd_hour, hrd->hnd_min, hrd->hnd_sec,
			hrd->org_pass_fare, hrd->pass_fare, hrd->toll_amt,
			hrd->refund_year, hrd->refund_month, hrd->refund_day, hrd->refund_hour, hrd->refund_min, hrd->refund_sec,
			hrd->refund_key);
#else
	sprintf(bak, "%.8s%.2s%.8s%.4s%.4s%.6s%c%.2s%.2s%c%.6s%.6s%.4s%.2s%.2s%.2s%.2s%.2s%.6s%.6s%.6s%c%c%.2s%c%.7s%.20s%.2s%c%c%-16s%-16s%.4s%.4s%.4s%.4s%.2s%.24s%.24s%c",
	             hrd->seq_no, hrd->kind, hrd->wrk_date, hrd->wrk_no, hrd->wrkr_no, hrd->serial_no,
				 hrd->io_div, hrd->plz_id, hrd->lane_no, hrd->car_kind, hrd->sence_seq_no, hrd->sence_time,
				 hrd->hnd_info.hnd_year, hrd->hnd_info.hnd_month, hrd->hnd_info.hnd_day,
				 hrd->hnd_info.hnd_hour, hrd->hnd_info.hnd_min, hrd->hnd_info.hnd_sec, hrd->hnd_info.org_pass_fare, 
				 hrd->hnd_info.pass_fare, hrd->hnd_info.rcpt_money, hrd->hnd_info.refund_yn, hrd->hnd_info.key_yn,
				 hrd->hnd_info.rcpt_hnd, hrd->hnd_info.red_div,
				 hrd->hnd_info.issue_code, hrd->hnd_info.crd_no, hrd->hnd_info.rcpt_div, hrd->pre_yn, hrd->img_yn,
				 hrd->car_no1, hrd->car_no2, hrd->x_point, hrd->y_point, hrd->img_width, hrd->img_heigh, hrd->dc_typ,
				 hrd->tag_id1, hrd->tag_id2, '\0');
#endif
//lane_log("환불[%s]\n", bak);
    snprintf((char *)rd.buf, strlen(bak)+1, "%s", (char *)bak);

    ret = MessageSender(hi_comm_ctrl.rcv_ctrl.cmd, (char *)&rd, path, rtn);

    if(ret < 0)
    {
        lane_log("(rcv_function.cpp) 환불처리[%.2s][%.4s] 처리실패\n", hi_comm_ctrl.write_info.plz_id, hi_comm_ctrl.write_info.wrk_no);
        return(-1);
    }

	if(ret == 0)
	{
		lst_rcv_acc_no = strtoul(tmp, NULL, 16);
	}
#ifdef _DEV_
    //장애 대응을 위한 처리자료 파일화
    char queue_d[128]; memset(queue_d, 0x00, sizeof(queue_d));
    char queue_f[128]; memset(queue_f, 0x00, sizeof(queue_f));
	char back_car[128]; memset(back_car, 0x00, sizeof(back_car));
	int div_carkind;
	FILE *backup_f;
	sprintf(queue_d, "%s/%.8s%c", LOG_DIR, rd.wrk_datetm, '\0');
	mkdir(queue_d,0755);
	sprintf(queue_f, "%s/queue%.2s%.2s.log", queue_d, rd.plz_id, rd.lane_no);

	backup_f = fopen(queue_f, "a+t");
	fprintf(backup_f, "%.2s%.2s%.2s|%.2s%.2s%.14s%c%.2s%.4s%.4s%c%.10s%.10s%.10s%s\n",
                    hrd->hnd_info.hnd_hour, hrd->hnd_info.hnd_min, hrd->hnd_info.hnd_sec, rd.kind, rd.plz_id, rd.wrk_datetm, rd.io_div, rd.lane_no, rd.wrk_no, rd.wrkr_no, rd.con_stat, rd.rcv_snd_no, rd.st_snd_no, rd.ed_snd_no, rd.buf);

    fclose(backup_f);
#endif

    return (0);
}

/*
 *========================================================================
 *
 *   Function : rcv_plz_line_reply()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.31
 *   Modified :
 *   Remark   : 회선 TEST 응답 DATA 수신처리
 *
 *========================================================================
*/
int rcv_plz_line_reply()
{
    if (hi_comm_ctrl.now_stat_info & H_CIR)
    {
        hi_comm_ctrl.now_stat_info &= ~H_CIR;
        lcc_reply(LW_LANECOMMTEST, 0, 1, "회선 테스트가 완료 되었습니다.");
    }

    return (0);
}

/*
 *========================================================================
 *
 *   Function : chk_seq_no()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2018.08.28
 *   Modified :
 *   Remark   : 수신 전송연번 CHECKING
 *
 *========================================================================
*/
int chk_seq_no()
{
    if (hi_comm_ctrl.now_stat_info & H_INIT)
        return (0);
//lane_log("전송연번 Check1[%x][%x]\n", hi_comm_ctrl.write_info.rcv_snd_no, hi_comm_ctrl.rcv_ctrl.snd_no);
    if (hi_comm_ctrl.write_info.rcv_snd_no == hi_comm_ctrl.rcv_ctrl.snd_no)
        return (0);            /* 전송연번 중복 */
//lane_log("전송연번 Check2[%x][%x]\n", hi_comm_ctrl.write_info.rcv_snd_no+1, hi_comm_ctrl.rcv_ctrl.snd_no);
    if ((hi_comm_ctrl.write_info.rcv_snd_no+1) == hi_comm_ctrl.rcv_ctrl.snd_no)
        return (0);
//lane_log("전송연번 Check3[%x][%x]\n", hi_comm_ctrl.write_info.rcv_snd_no, hi_comm_ctrl.rcv_ctrl.snd_no);
    if (hi_comm_ctrl.write_info.rcv_snd_no == 0xffff && hi_comm_ctrl.rcv_ctrl.snd_no == 1)
        return (0);
//lane_log("전송연번 Check4[%x][%x]\n", hi_comm_ctrl.write_info.rcv_snd_no, hi_comm_ctrl.rcv_ctrl.snd_no);
    if (hi_comm_ctrl.write_info.rcv_snd_no == 0)        /* 최종수신 전송연번 무 */
        return (0);
//lane_log("전송연번 Check5[%x][%x]\n", hi_comm_ctrl.write_info.rcv_snd_no, hi_comm_ctrl.rcv_ctrl.snd_no);
    if (hi_comm_ctrl.now_stat_info & H_ERR_REST)
        return (0);
//lane_log("전송연번 Check6[%x][%x]\n", hi_comm_ctrl.write_info.rcv_snd_no, hi_comm_ctrl.rcv_ctrl.snd_no);
    if (hi_comm_ctrl.now_stat_info & H_ACC_RESND) /* KSW */
        return (0);
//lane_log("전송연번 Check7[%x][%x]\n", hi_comm_ctrl.write_info.rcv_snd_no, hi_comm_ctrl.rcv_ctrl.snd_no);
    return (-1);                 /* 전송연번 오류 */
}

/*
 *========================================================================
 *
 *   Function : chg_car_kind()
 *   Progrmer : JBY (baarong@neighbor21.co.kr)
 *   Date     : 2020.04.07
 *   Modified :
 *   Remark   : 차량번호로 차종 변경
 *
 *========================================================================
*/
int chg_car_kind(char *car_no, char car_kind)
{
    char back_car[128]; memset(back_car, 0x00, sizeof(back_car));
	char mCarNo[3+1]; memset(mCarNo, 0x00, sizeof(mCarNo));
	int div_carkind;
	
	snprintf(back_car, strlen(car_no)+1, "%s", car_no);
	del_space(back_car);

    div_carkind = car_kind - '0';
	sprintf(mCarNo, "%.2s%c", back_car + strlen(back_car) - 6, '\0');
	if(strcmp(mCarNo, "바") == 0 || strcmp(mCarNo, "사") == 0 || strcmp(mCarNo, "아") == 0 || strcmp(mCarNo, "자") == 0)
	{
		if(div_carkind == 1)
		{
			div_carkind = 6;
		}else if(div_carkind == 2)
		{
			div_carkind = 7;
		}
	}
	
	return div_carkind;
}
